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