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 
  37 // This class provides the interface between a barrier implementation and
  38 // the rest of the system.
  39 
  40 class BarrierSet: public CHeapObj<mtGC> {
  41   friend class VMStructs;
  42 
  43   static BarrierSet* _bs;
  44 
  45 public:
  46   enum Name {
  47 #define BARRIER_SET_DECLARE_BS_ENUM(bs_name) bs_name ,
  48     FOR_EACH_BARRIER_SET_DO(BARRIER_SET_DECLARE_BS_ENUM)
  49 #undef BARRIER_SET_DECLARE_BS_ENUM
  50     UnknownBS
  51   };
  52 
  53   static BarrierSet* barrier_set() { return _bs; }
  54 
  55 protected:
  56   // Fake RTTI support.  For a derived class T to participate
  57   // - T must have a corresponding Name entry.
  58   // - GetName<T> must be specialized to return the corresponding Name
  59   //   entry.
  60   // - If T is a base class, the constructor must have a FakeRtti
  61   //   parameter and pass it up to its base class, with the tag set
  62   //   augmented with the corresponding Name entry.
  63   // - If T is a concrete class, the constructor must create a
  64   //   FakeRtti object whose tag set includes the corresponding Name
  65   //   entry, and pass it up to its base class.
  66   typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;
  67 
  68 private:
  69   FakeRtti _fake_rtti;
  70 
  71 public:
  72   // Metafunction mapping a class derived from BarrierSet to the
  73   // corresponding Name enum tag.
  74   template<typename T> struct GetName;
  75 
  76   // Metafunction mapping a Name enum type to the corresponding
  77   // lass derived from BarrierSet.
  78   template<BarrierSet::Name T> struct GetType;
  79 
  80   // Note: This is not presently the Name corresponding to the
  81   // concrete class of this object.
  82   BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }
  83 
  84   // Test whether this object is of the type corresponding to bsn.
  85   bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }
  86 
  87   // End of fake RTTI support.
  88 
  89 protected:
  90   BarrierSet(const FakeRtti& fake_rtti) : _fake_rtti(fake_rtti) { }
  91   ~BarrierSet() { }
  92 
  93 public:
  94   // Operations on arrays, or general regions (e.g., for "clone") may be
  95   // optimized by some barriers.
  96 
  97   // Below length is the # array elements being written
  98   virtual void write_ref_array_pre(oop* dst, int length,
  99                                    bool dest_uninitialized = false) {}
 100   virtual void write_ref_array_pre(narrowOop* dst, int length,
 101                                    bool dest_uninitialized = false) {}
 102   // Below count is the # array elements being written, starting
 103   // at the address "start", which may not necessarily be HeapWord-aligned
 104   inline void write_ref_array(HeapWord* start, size_t count);
 105 
 106   // Static versions, suitable for calling from generated code;
 107   // count is # array elements being written, starting with "start",
 108   // which may not necessarily be HeapWord-aligned.
 109   static void static_write_ref_array_pre(HeapWord* start, size_t count);
 110   static void static_write_ref_array_post(HeapWord* start, size_t count);
 111 
 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 flush_deferred_barriers(JavaThread* thread) {}
 119   virtual void make_parsable(JavaThread* thread) {}
 120 
 121 protected:
 122   virtual void write_ref_array_work(MemRegion mr) = 0;
 123 
 124 public:
 125   // Inform the BarrierSet that the the covered heap region that starts
 126   // with "base" has been changed to have the given size (possibly from 0,
 127   // for initialization.)
 128   virtual void resize_covered_region(MemRegion new_region) = 0;
 129 
 130   // If the barrier set imposes any alignment restrictions on boundaries
 131   // within the heap, this function tells whether they are met.
 132   virtual bool is_aligned(HeapWord* addr) = 0;
 133 
 134   // Print a description of the memory for the barrier set
 135   virtual void print_on(outputStream* st) const = 0;
 136 
 137   static void set_bs(BarrierSet* bs) { _bs = bs; }
 138 
 139   // The AccessBarrier of a BarrierSet subclass is called by the Access API
 140   // (cf. oops/access.hpp) to perform decorated accesses. GC implementations
 141   // may override these default access operations by declaring an
 142   // AccessBarrier class in its BarrierSet. Its accessors will then be
 143   // automatically resolved at runtime.
 144   //
 145   // In order to register a new FooBarrierSet::AccessBarrier with the Access API,
 146   // the following steps should be taken:
 147   // 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp
 148   // 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp
 149   // 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.
 150   template <DecoratorSet decorators, typename BarrierSetT>
 151   class AccessBarrier: protected RawAccessBarrier<decorators> {
 152   private:
 153     typedef RawAccessBarrier<decorators> Raw;
 154 
 155   public:
 156     // Primitive heap accesses. These accessors get resolved when
 157     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 158     // not an oop_* overload, and the barrier strength is AS_NORMAL.
 159     template <typename T>
 160     static T load_in_heap(T* addr) {
 161       return Raw::template load<T>(addr);
 162     }
 163 
 164     template <typename T>
 165     static T load_in_heap_at(oop base, ptrdiff_t offset) {
 166       return Raw::template load_at<T>(base, offset);
 167     }
 168 
 169     template <typename T>
 170     static void store_in_heap(T* addr, T value) {
 171       Raw::store(addr, value);
 172     }
 173 
 174     template <typename T>
 175     static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {
 176       Raw::store_at(base, offset, value);
 177     }
 178 
 179     template <typename T>
 180     static T atomic_cmpxchg_in_heap(T new_value, T* addr, T compare_value) {
 181       return Raw::atomic_cmpxchg(new_value, addr, compare_value);
 182     }
 183 
 184     template <typename T>
 185     static T atomic_cmpxchg_in_heap_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
 186       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
 187     }
 188 
 189     template <typename T>
 190     static T atomic_xchg_in_heap(T new_value, T* addr) {
 191       return Raw::atomic_xchg(new_value, addr);
 192     }
 193 
 194     template <typename T>
 195     static T atomic_xchg_in_heap_at(T new_value, oop base, ptrdiff_t offset) {
 196       return Raw::atomic_xchg_at(new_value, base, offset);
 197     }
 198 
 199     template <typename T>
 200     static bool arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
 201       return Raw::arraycopy(src_obj, dst_obj, src, dst, length);
 202     }
 203 
 204     // Heap oop accesses. These accessors get resolved when
 205     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 206     // an oop_* overload, and the barrier strength is AS_NORMAL.
 207     template <typename T>
 208     static oop oop_load_in_heap(T* addr) {
 209       return Raw::template oop_load<oop>(addr);
 210     }
 211 
 212     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 213       return Raw::template oop_load_at<oop>(base, offset);
 214     }
 215 
 216     template <typename T>
 217     static void oop_store_in_heap(T* addr, oop value) {
 218       Raw::oop_store(addr, value);
 219     }
 220 
 221     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 222       Raw::oop_store_at(base, offset, value);
 223     }
 224 
 225     template <typename T>
 226     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
 227       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
 228     }
 229 
 230     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 231       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
 232     }
 233 
 234     template <typename T>
 235     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr) {
 236       return Raw::oop_atomic_xchg(new_value, addr);
 237     }
 238 
 239     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
 240       return Raw::oop_atomic_xchg_at(new_value, base, offset);
 241     }
 242 
 243     template <typename T>
 244     static bool oop_arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
 245       return Raw::oop_arraycopy(src_obj, dst_obj, src, dst, length);
 246     }
 247 
 248     // Off-heap oop accesses. These accessors get resolved when
 249     // IN_HEAP is not set (e.g. when using the RootAccess API), it is
 250     // an oop* overload, and the barrier strength is AS_NORMAL.
 251     template <typename T>
 252     static oop oop_load_not_in_heap(T* addr) {
 253       return Raw::template oop_load<oop>(addr);
 254     }
 255 
 256     template <typename T>
 257     static void oop_store_not_in_heap(T* addr, oop value) {
 258       Raw::oop_store(addr, value);
 259     }
 260 
 261     template <typename T>
 262     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
 263       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
 264     }
 265 
 266     template <typename T>
 267     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
 268       return Raw::oop_atomic_xchg(new_value, addr);
 269     }
 270 
 271     // Clone barrier support
 272     static void clone_in_heap(oop src, oop dst, size_t size) {
 273       Raw::clone(src, dst, size);
 274     }
 275   };
 276 };
 277 
 278 template<typename T>
 279 inline T* barrier_set_cast(BarrierSet* bs) {
 280   assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
 281   return static_cast<T*>(bs);
 282 }
 283 
 284 #endif // SHARE_VM_GC_SHARED_BARRIERSET_HPP