1 /*
  2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_VM_GC_SHARED_BARRIERSET_HPP
 26 #define SHARE_VM_GC_SHARED_BARRIERSET_HPP
 27 
 28 #include "gc/shared/barrierSetConfig.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "oops/access.hpp"
 31 #include "oops/accessBackend.hpp"
 32 #include "oops/oopsHierarchy.hpp"
 33 #include "utilities/fakeRttiSupport.hpp"
 34 
 35 // This class provides the interface between a barrier implementation and
 36 // the rest of the system.
 37 
 38 class BarrierSet: public CHeapObj<mtGC> {
 39   friend class VMStructs;
 40 
 41   static BarrierSet* _bs;
 42 
 43 public:
 44   enum Name {
 45 #define BARRIER_SET_DECLARE_BS_ENUM(bs_name) bs_name ,
 46     FOR_EACH_BARRIER_SET_DO(BARRIER_SET_DECLARE_BS_ENUM)
 47 #undef BARRIER_SET_DECLARE_BS_ENUM
 48     UnknownBS
 49   };
 50 
 51   static BarrierSet* barrier_set() { return _bs; }
 52 
 53 protected:
 54   // Fake RTTI support.  For a derived class T to participate
 55   // - T must have a corresponding Name entry.
 56   // - GetName<T> must be specialized to return the corresponding Name
 57   //   entry.
 58   // - If T is a base class, the constructor must have a FakeRtti
 59   //   parameter and pass it up to its base class, with the tag set
 60   //   augmented with the corresponding Name entry.
 61   // - If T is a concrete class, the constructor must create a
 62   //   FakeRtti object whose tag set includes the corresponding Name
 63   //   entry, and pass it up to its base class.
 64   typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;
 65 
 66 private:
 67   FakeRtti _fake_rtti;
 68 
 69 public:
 70   // Metafunction mapping a class derived from BarrierSet to the
 71   // corresponding Name enum tag.
 72   template<typename T> struct GetName;
 73 
 74   // Metafunction mapping a Name enum type to the corresponding
 75   // lass derived from BarrierSet.
 76   template<BarrierSet::Name T> struct GetType;
 77 
 78   // Note: This is not presently the Name corresponding to the
 79   // concrete class of this object.
 80   BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }
 81 
 82   // Test whether this object is of the type corresponding to bsn.
 83   bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }
 84 
 85   // End of fake RTTI support.
 86 
 87 protected:
 88   BarrierSet(const FakeRtti& fake_rtti) : _fake_rtti(fake_rtti) { }
 89   ~BarrierSet() { }
 90 
 91 public:
 92   // Operations on arrays, or general regions (e.g., for "clone") may be
 93   // optimized by some barriers.
 94 
 95   // Below length is the # array elements being written
 96   virtual void write_ref_array_pre(oop* dst, int length,
 97                                    bool dest_uninitialized = false) {}
 98   virtual void write_ref_array_pre(narrowOop* dst, int length,
 99                                    bool dest_uninitialized = false) {}
100   // Below count is the # array elements being written, starting
101   // at the address "start", which may not necessarily be HeapWord-aligned
102   inline void write_ref_array(HeapWord* start, size_t count);
103 
104   // Static versions, suitable for calling from generated code;
105   // count is # array elements being written, starting with "start",
106   // which may not necessarily be HeapWord-aligned.
107   static void static_write_ref_array_pre(HeapWord* start, size_t count);
108   static void static_write_ref_array_post(HeapWord* start, size_t count);
109 
110 protected:
111   virtual void write_ref_array_work(MemRegion mr) = 0;
112 
113 public:
114   // (For efficiency reasons, this operation is specialized for certain
115   // barrier types.  Semantically, it should be thought of as a call to the
116   // virtual "_work" function below, which must implement the barrier.)
117   void write_region(MemRegion mr);
118 
119 protected:
120   virtual void write_region_work(MemRegion mr) = 0;
121 
122 public:
123   // Inform the BarrierSet that the the covered heap region that starts
124   // with "base" has been changed to have the given size (possibly from 0,
125   // for initialization.)
126   virtual void resize_covered_region(MemRegion new_region) = 0;
127 
128   // If the barrier set imposes any alignment restrictions on boundaries
129   // within the heap, this function tells whether they are met.
130   virtual bool is_aligned(HeapWord* addr) = 0;
131 
132   // Print a description of the memory for the barrier set
133   virtual void print_on(outputStream* st) const = 0;
134 
135   static void set_bs(BarrierSet* bs) { _bs = bs; }
136 
137   // The AccessBarrier of a BarrierSet subclass is called by the Access API
138   // (cf. oops/access.hpp) to perform decorated accesses. GC implementations
139   // may override these default access operations by declaring an
140   // AccessBarrier class in its BarrierSet. Its accessors will then be
141   // automatically resolved at runtime.
142   //
143   // In order to register a new FooBarrierSet::AccessBarrier with the Access API,
144   // the following steps should be taken:
145   // 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp
146   // 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp
147   // 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.
148   template <DecoratorSet decorators, typename BarrierSetT>
149   class AccessBarrier: protected RawAccessBarrier<decorators> {
150   private:
151     typedef RawAccessBarrier<decorators> Raw;
152 
153   public:
154     // Primitive heap accesses. These accessors get resolved when
155     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
156     // not an oop_* overload, and the barrier strength is AS_NORMAL.
157     template <typename T>
158     static T load_in_heap(T* addr) {
159       return Raw::template load<T>(addr);
160     }
161 
162     template <typename T>
163     static T load_in_heap_at(oop base, ptrdiff_t offset) {
164       return Raw::template load_at<T>(base, offset);
165     }
166 
167     template <typename T>
168     static void store_in_heap(T* addr, T value) {
169       Raw::store(addr, value);
170     }
171 
172     template <typename T>
173     static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {
174       Raw::store_at(base, offset, value);
175     }
176 
177     template <typename T>
178     static T atomic_cmpxchg_in_heap(T new_value, T* addr, T compare_value) {
179       return Raw::atomic_cmpxchg(new_value, addr, compare_value);
180     }
181 
182     template <typename T>
183     static T atomic_cmpxchg_in_heap_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
184       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
185     }
186 
187     template <typename T>
188     static T atomic_xchg_in_heap(T new_value, T* addr) {
189       return Raw::atomic_xchg(new_value, addr);
190     }
191 
192     template <typename T>
193     static T atomic_xchg_in_heap_at(T new_value, oop base, ptrdiff_t offset) {
194       return Raw::atomic_xchg_at(new_value, base, offset);
195     }
196 
197     template <typename T>
198     static bool arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
199       return Raw::arraycopy(src_obj, dst_obj, src, dst, length);
200     }
201 
202     // Heap oop accesses. These accessors get resolved when
203     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
204     // an oop_* overload, and the barrier strength is AS_NORMAL.
205     template <typename T>
206     static oop oop_load_in_heap(T* addr) {
207       return Raw::template oop_load<oop>(addr);
208     }
209 
210     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
211       return Raw::template oop_load_at<oop>(base, offset);
212     }
213 
214     template <typename T>
215     static void oop_store_in_heap(T* addr, oop value) {
216       Raw::oop_store(addr, value);
217     }
218 
219     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
220       Raw::oop_store_at(base, offset, value);
221     }
222 
223     template <typename T>
224     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
225       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
226     }
227 
228     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
229       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
230     }
231 
232     template <typename T>
233     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr) {
234       return Raw::oop_atomic_xchg(new_value, addr);
235     }
236 
237     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
238       return Raw::oop_atomic_xchg_at(new_value, base, offset);
239     }
240 
241     template <typename T>
242     static bool oop_arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
243       return Raw::oop_arraycopy(src_obj, dst_obj, src, dst, length);
244     }
245 
246     // Off-heap oop accesses. These accessors get resolved when
247     // IN_HEAP is not set (e.g. when using the RootAccess API), it is
248     // an oop* overload, and the barrier strength is AS_NORMAL.
249     template <typename T>
250     static oop oop_load_not_in_heap(T* addr) {
251       return Raw::template oop_load<oop>(addr);
252     }
253 
254     template <typename T>
255     static void oop_store_not_in_heap(T* addr, oop value) {
256       Raw::oop_store(addr, value);
257     }
258 
259     template <typename T>
260     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
261       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
262     }
263 
264     template <typename T>
265     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
266       return Raw::oop_atomic_xchg(new_value, addr);
267     }
268 
269     // Clone barrier support
270     static void clone_in_heap(oop src, oop dst, size_t size) {
271       Raw::clone(src, dst, size);
272     }
273   };
274 };
275 
276 template<typename T>
277 inline T* barrier_set_cast(BarrierSet* bs) {
278   assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
279   return static_cast<T*>(bs);
280 }
281 
282 #endif // SHARE_VM_GC_SHARED_BARRIERSET_HPP