1 /*
   2  * Copyright (c) 2000, 2017, 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 "asm/register.hpp"
  34 #include "utilities/fakeRttiSupport.hpp"
  35 
  36 // This class provides the interface between a barrier implementation and
  37 // the rest of the system.
  38 
  39 class MacroAssembler;
  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) { }
  92   ~BarrierSet() { }
  93 
  94 public:
  95   // Operations on arrays, or general regions (e.g., for "clone") may be
  96   // optimized by some barriers.
  97 
  98   // Below length is the # array elements being written
  99   virtual void write_ref_array_pre(oop* dst, int length,
 100                                    bool dest_uninitialized = false) {}
 101   virtual void write_ref_array_pre(narrowOop* dst, int length,
 102                                    bool dest_uninitialized = false) {}
 103   // Below count is the # array elements being written, starting
 104   // at the address "start", which may not necessarily be HeapWord-aligned
 105   virtual void write_ref_array(HeapWord* start, size_t count);
 106 
 107   // Static versions, suitable for calling from generated code;
 108   // count is # array elements being written, starting with "start",
 109   // which may not necessarily be HeapWord-aligned.
 110   static void static_write_ref_array_pre(HeapWord* start, size_t count);
 111   static void static_write_ref_array_post(HeapWord* start, size_t count);
 112 
 113 protected:
 114   virtual void write_ref_array_work(MemRegion mr) = 0;
 115 
 116 public:
 117   // (For efficiency reasons, this operation is specialized for certain
 118   // barrier types.  Semantically, it should be thought of as a call to the
 119   // virtual "_work" function below, which must implement the barrier.)
 120   void write_region(MemRegion mr);
 121 
 122 protected:
 123   virtual void write_region_work(MemRegion mr) = 0;
 124 
 125 public:
 126   // Inform the BarrierSet that the the covered heap region that starts
 127   // with "base" has been changed to have the given size (possibly from 0,
 128   // for initialization.)
 129   virtual void resize_covered_region(MemRegion new_region) = 0;
 130 
 131   // If the barrier set imposes any alignment restrictions on boundaries
 132   // within the heap, this function tells whether they are met.
 133   virtual bool is_aligned(HeapWord* addr) = 0;
 134 
 135   // Print a description of the memory for the barrier set
 136   virtual void print_on(outputStream* st) const = 0;
 137 
 138   static void set_bs(BarrierSet* bs) { _bs = bs; }
 139 
 140   virtual oop read_barrier(oop src) {
 141     return src;
 142   }
 143   virtual oop write_barrier(oop src) {
 144     return src;
 145   }
 146   virtual oop storeval_barrier(oop src) {
 147     return src;
 148   }
 149 
 150   virtual void keep_alive_barrier(oop obj) {
 151     // Default impl does nothing.
 152   }
 153 
 154   virtual bool obj_equals(oop obj1, oop obj2);
 155 
 156   virtual bool obj_equals(narrowOop obj1, narrowOop obj2);
 157 
 158 #ifdef ASSERT
 159   virtual void verify_safe_oop(oop p);
 160   virtual void verify_safe_oop(narrowOop p);
 161 #endif
 162 
 163 #ifndef CC_INTERP
 164   virtual void interpreter_read_barrier(MacroAssembler* masm, Register dst) {
 165     // Default implementation does nothing.
 166   }
 167 
 168   virtual void interpreter_read_barrier_not_null(MacroAssembler* masm, Register dst) {
 169     // Default implementation does nothing.
 170   }
 171 
 172   virtual void interpreter_write_barrier(MacroAssembler* masm, Register dst) {
 173     // Default implementation does nothing.
 174   }
 175   virtual void interpreter_storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 176     // Default implementation does nothing.
 177   }
 178   virtual void asm_acmp_barrier(MacroAssembler* masm, Register op1, Register op2) {
 179     // Default implementation does nothing.
 180   }
 181 #endif
 182 
 183   // The AccessBarrier of a BarrierSet subclass is called by the Access API
 184   // (cf. oops/access.hpp) to perform decorated accesses. GC implementations
 185   // may override these default access operations by declaring an
 186   // AccessBarrier class in its BarrierSet. Its accessors will then be
 187   // automatically resolved at runtime.
 188   //
 189   // In order to register a new FooBarrierSet::AccessBarrier with the Access API,
 190   // the following steps should be taken:
 191   // 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp
 192   // 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp
 193   // 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.
 194   template <DecoratorSet decorators, typename BarrierSetT>
 195   class AccessBarrier: protected RawAccessBarrier<decorators> {
 196   protected:
 197     typedef RawAccessBarrier<decorators> Raw;
 198     typedef typename BarrierSetT::template AccessBarrier<decorators> CRTPAccessBarrier;
 199 
 200   public:
 201     // Primitive heap accesses. These accessors get resolved when
 202     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 203     // not an oop_* overload, and the barrier strength is AS_NORMAL.
 204     template <typename T>
 205     static T load_in_heap(T* addr) {
 206       return Raw::template load<T>(addr);
 207     }
 208 
 209     template <typename T>
 210     static T load_in_heap_at(oop base, ptrdiff_t offset) {
 211       return Raw::template load_at<T>(base, offset);
 212     }
 213 
 214     template <typename T>
 215     static void store_in_heap(T* addr, T value) {
 216       Raw::store(addr, value);
 217     }
 218 
 219     template <typename T>
 220     static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {
 221       Raw::store_at(base, offset, value);
 222     }
 223 
 224     template <typename T>
 225     static T atomic_cmpxchg_in_heap(T new_value, T* addr, T compare_value) {
 226       return Raw::atomic_cmpxchg(new_value, addr, compare_value);
 227     }
 228 
 229     template <typename T>
 230     static T atomic_cmpxchg_in_heap_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
 231       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
 232     }
 233 
 234     template <typename T>
 235     static T atomic_xchg_in_heap(T new_value, T* addr) {
 236       return Raw::atomic_xchg(new_value, addr);
 237     }
 238 
 239     template <typename T>
 240     static T atomic_xchg_in_heap_at(T new_value, oop base, ptrdiff_t offset) {
 241       return Raw::atomic_xchg_at(new_value, base, offset);
 242     }
 243 
 244     template <typename T>
 245     static bool arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
 246       return Raw::arraycopy(src, dst, length);
 247     }
 248 
 249     // Heap oop accesses. These accessors get resolved when
 250     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 251     // an oop_* overload, and the barrier strength is AS_NORMAL.
 252     template <typename T>
 253     static oop oop_load_in_heap(T* addr) {
 254       return Raw::template oop_load<oop>(addr);
 255     }
 256 
 257     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 258       return Raw::template oop_load_at<oop>(base, offset);
 259     }
 260 
 261     template <typename T>
 262     static void oop_store_in_heap(T* addr, oop value) {
 263       Raw::oop_store(addr, value);
 264     }
 265 
 266     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 267       Raw::oop_store_at(base, offset, value);
 268     }
 269 
 270     template <typename T>
 271     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
 272       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
 273     }
 274 
 275     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 276       return Raw::oop_atomic_cmpxchg_at(new_value, base, offset, compare_value);
 277     }
 278 
 279     template <typename T>
 280     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr) {
 281       return Raw::oop_atomic_xchg(new_value, addr);
 282     }
 283 
 284     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
 285       return Raw::oop_atomic_xchg_at(new_value, base, offset);
 286     }
 287 
 288     template <typename T>
 289     static bool oop_arraycopy_in_heap(arrayOop src_obj, arrayOop dst_obj, T* src, T* dst, size_t length) {
 290       return Raw::oop_arraycopy(src_obj, dst_obj, src, dst, length);
 291     }
 292 
 293     // Off-heap oop accesses. These accessors get resolved when
 294     // IN_HEAP is not set (e.g. when using the RootAccess API), it is
 295     // an oop* overload, and the barrier strength is AS_NORMAL.
 296     template <typename T>
 297     static oop oop_load_not_in_heap(T* addr) {
 298       return Raw::template oop_load<oop>(addr);
 299     }
 300 
 301     template <typename T>
 302     static void oop_store_not_in_heap(T* addr, oop value) {
 303       Raw::oop_store(addr, value);
 304     }
 305 
 306     template <typename T>
 307     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
 308       return Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
 309     }
 310 
 311     template <typename T>
 312     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
 313       return Raw::oop_atomic_xchg(new_value, addr);
 314     }
 315 
 316     // Clone barrier support
 317     static void clone_in_heap(oop src, oop dst, size_t size) {
 318       Raw::clone(src, dst, size);
 319     }
 320   };
 321 };
 322 
 323 template<typename T>
 324 inline T* barrier_set_cast(BarrierSet* bs) {
 325   assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
 326   return static_cast<T*>(bs);
 327 }
 328 
 329 #endif // SHARE_VM_GC_SHARED_BARRIERSET_HPP