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 #include "utilities/macros.hpp"
 35 
 36 class BarrierSetAssembler;
 37 class BarrierSetC1;
 38 class JavaThread;
 39 
 40 // This class provides the interface between a barrier implementation and
 41 // the rest of the system.
 42 
 43 class BarrierSet: public CHeapObj<mtGC> {
 44   friend class VMStructs;
 45 
 46   static BarrierSet* _barrier_set;
 47 
 48 public:
 49   enum Name {
 50 #define BARRIER_SET_DECLARE_BS_ENUM(bs_name) bs_name ,
 51     FOR_EACH_BARRIER_SET_DO(BARRIER_SET_DECLARE_BS_ENUM)
 52 #undef BARRIER_SET_DECLARE_BS_ENUM
 53     UnknownBS
 54   };
 55 
 56 protected:
 57   // Fake RTTI support.  For a derived class T to participate
 58   // - T must have a corresponding Name entry.
 59   // - GetName<T> must be specialized to return the corresponding Name
 60   //   entry.
 61   // - If T is a base class, the constructor must have a FakeRtti
 62   //   parameter and pass it up to its base class, with the tag set
 63   //   augmented with the corresponding Name entry.
 64   // - If T is a concrete class, the constructor must create a
 65   //   FakeRtti object whose tag set includes the corresponding Name
 66   //   entry, and pass it up to its base class.
 67   typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;
 68 
 69 private:
 70   FakeRtti _fake_rtti;
 71   BarrierSetAssembler* _barrier_set_assembler;
 72   BarrierSetC1* _barrier_set_c1;
 73 
 74 public:
 75   // Metafunction mapping a class derived from BarrierSet to the
 76   // corresponding Name enum tag.
 77   template<typename T> struct GetName;
 78 
 79   // Metafunction mapping a Name enum type to the corresponding
 80   // lass derived from BarrierSet.
 81   template<BarrierSet::Name T> struct GetType;
 82 
 83   // Note: This is not presently the Name corresponding to the
 84   // concrete class of this object.
 85   BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }
 86 
 87   // Test whether this object is of the type corresponding to bsn.
 88   bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }
 89 
 90   // End of fake RTTI support.
 91 
 92 protected:
 93   BarrierSet(BarrierSetAssembler* barrier_set_assembler,
 94              BarrierSetC1* barrier_set_c1,
 95              const FakeRtti& fake_rtti) :
 96     _fake_rtti(fake_rtti),
 97     _barrier_set_assembler(barrier_set_assembler),
 98     _barrier_set_c1(barrier_set_c1) {}
 99   ~BarrierSet() { }
100 
101   template <class BarrierSetAssemblerT>
102   BarrierSetAssembler* make_barrier_set_assembler() {
103     return NOT_ZERO(new BarrierSetAssemblerT()) ZERO_ONLY(NULL);
104   }
105 
106   template <class BarrierSetC1T>
107   BarrierSetC1* make_barrier_set_c1() {
108     return COMPILER1_PRESENT(new BarrierSetC1T()) NOT_COMPILER1(NULL);
109   }
110 
111 public:
112   // Support for optimizing compilers to call the barrier set on slow path allocations
113   // that did not enter a TLAB. Used for e.g. ReduceInitialCardMarks.
114   // The allocation is safe to use iff it returns true. If not, the slow-path allocation
115   // is redone until it succeeds. This can e.g. prevent allocations from the slow path
116   // to be in old.
117   virtual void on_slowpath_allocation_exit(JavaThread* thread, oop new_obj) {}
118   virtual void on_thread_create(Thread* thread) {}
119   virtual void on_thread_destroy(Thread* thread) {}
120   virtual void on_thread_attach(JavaThread* thread) {}
121   virtual void on_thread_detach(JavaThread* thread) {}
122   virtual void make_parsable(JavaThread* thread) {}
123 
124 public:
125   // Print a description of the memory for the barrier set
126   virtual void print_on(outputStream* st) const = 0;
127 
128   static BarrierSet* barrier_set() { return _barrier_set; }
129   static void set_barrier_set(BarrierSet* barrier_set);
130 
131   BarrierSetAssembler* barrier_set_assembler() {
132     assert(_barrier_set_assembler != NULL, "should be set");
133     return _barrier_set_assembler;
134   }
135 
136   BarrierSetC1* barrier_set_c1() {
137     assert(_barrier_set_c1 != NULL, "should be set");
138     return _barrier_set_c1;
139   }
140 
141   // The AccessBarrier of a BarrierSet subclass is called by the Access API
142   // (cf. oops/access.hpp) to perform decorated accesses. GC implementations
143   // may override these default access operations by declaring an
144   // AccessBarrier class in its BarrierSet. Its accessors will then be
145   // automatically resolved at runtime.
146   //
147   // In order to register a new FooBarrierSet::AccessBarrier with the Access API,
148   // the following steps should be taken:
149   // 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp
150   // 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp
151   // 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.
152   template <DecoratorSet decorators, typename BarrierSetT>
153   class AccessBarrier: protected RawAccessBarrier<decorators> {
154   private:
155     typedef RawAccessBarrier<decorators> Raw;
156 
157   public:
158     // Primitive heap accesses. These accessors get resolved when
159     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
160     // not an oop_* overload, and the barrier strength is AS_NORMAL.
161     template <typename T>
162     static T load_in_heap(T* addr) {
163       return Raw::template load<T>(addr);
164     }
165 
166     template <typename T>
167     static T load_in_heap_at(oop base, ptrdiff_t offset) {
168       return Raw::template load_at<T>(base, offset);
169     }
170 
171     template <typename T>
172     static void store_in_heap(T* addr, T value) {
173       Raw::store(addr, value);
174     }
175 
176     template <typename T>
177     static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {
178       Raw::store_at(base, offset, value);
179     }
180 
181     template <typename T>
182     static T atomic_cmpxchg_in_heap(T new_value, T* addr, T compare_value) {
183       return Raw::atomic_cmpxchg(new_value, addr, compare_value);
184     }
185 
186     template <typename T>
187     static T atomic_cmpxchg_in_heap_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
188       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
189     }
190 
191     template <typename T>
192     static T atomic_xchg_in_heap(T new_value, T* addr) {
193       return Raw::atomic_xchg(new_value, addr);
194     }
195 
196     template <typename T>
197     static T atomic_xchg_in_heap_at(T new_value, oop base, ptrdiff_t offset) {
198       return Raw::atomic_xchg_at(new_value, base, offset);
199     }
200 
201     template <typename T>
202     static void arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
203       Raw::arraycopy(src_obj, dst_obj, src, dst, length);
204     }
205 
206     // Heap oop accesses. These accessors get resolved when
207     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
208     // an oop_* overload, and the barrier strength is AS_NORMAL.
209     template <typename T>
210     static oop oop_load_in_heap(T* addr) {
211       return Raw::template oop_load<oop>(addr);
212     }
213 
214     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
215       return Raw::template oop_load_at<oop>(base, offset);
216     }
217 
218     template <typename T>
219     static void oop_store_in_heap(T* addr, oop value) {
220       Raw::oop_store(addr, value);
221     }
222 
223     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
224       Raw::oop_store_at(base, offset, value);
225     }
226 
227     template <typename T>
228     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
229       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
230     }
231 
232     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
233       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
234     }
235 
236     template <typename T>
237     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr) {
238       return Raw::oop_atomic_xchg(new_value, addr);
239     }
240 
241     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
242       return Raw::oop_atomic_xchg_at(new_value, base, offset);
243     }
244 
245     template <typename T>
246     static bool oop_arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
247       return Raw::oop_arraycopy(src_obj, dst_obj, src, dst, length);
248     }
249 
250     // Off-heap oop accesses. These accessors get resolved when
251     // IN_HEAP is not set (e.g. when using the RootAccess API), it is
252     // an oop* overload, and the barrier strength is AS_NORMAL.
253     template <typename T>
254     static oop oop_load_not_in_heap(T* addr) {
255       return Raw::template oop_load<oop>(addr);
256     }
257 
258     template <typename T>
259     static void oop_store_not_in_heap(T* addr, oop value) {
260       Raw::oop_store(addr, value);
261     }
262 
263     template <typename T>
264     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
265       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
266     }
267 
268     template <typename T>
269     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
270       return Raw::oop_atomic_xchg(new_value, addr);
271     }
272 
273     // Clone barrier support
274     static void clone_in_heap(oop src, oop dst, size_t size) {
275       Raw::clone(src, dst, size);
276     }
277 
278     static oop resolve(oop obj) {
279       return Raw::resolve(obj);
280     }
281 
282     static bool equals(oop o1, oop o2) {
283       return Raw::equals(o1, o2);
284     }
285   };
286 };
287 
288 template<typename T>
289 inline T* barrier_set_cast(BarrierSet* bs) {
290   assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
291   return static_cast<T*>(bs);
292 }
293 
294 #endif // SHARE_VM_GC_SHARED_BARRIERSET_HPP