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