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