1 /*
   2  * Copyright (c) 2013, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  26 
  27 #include "gc/shared/accessBarrierSupport.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
  31 
  32 class ShenandoahBarrierSetAssembler;
  33 
  34 class ShenandoahBarrierSet: public BarrierSet {
  35 private:
  36   enum ArrayCopyStoreValMode {
  37     NONE,
  38     READ_BARRIER,
  39     WRITE_BARRIER
  40   };
  41 
  42   ShenandoahHeap* _heap;
  43   ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
  44 
  45 public:
  46   ShenandoahBarrierSet(ShenandoahHeap* heap);
  47 
  48   static ShenandoahBarrierSetAssembler* assembler();
  49 
  50   inline static ShenandoahBarrierSet* barrier_set() {
  51     return barrier_set_cast<ShenandoahBarrierSet>(BarrierSet::barrier_set());
  52   }
  53 
  54   static ShenandoahSATBMarkQueueSet& satb_mark_queue_set() {
  55     return barrier_set()->_satb_mark_queue_set;
  56   }
  57 
  58   void print_on(outputStream* st) const;
  59 
  60   bool is_a(BarrierSet::Name bsn);
  61 
  62   bool is_aligned(HeapWord* hw);
  63 
  64   void write_ref_array(HeapWord* start, size_t count);
  65 
  66   template <class T> void
  67   write_ref_array_pre_work(T* dst, size_t count);
  68 
  69   void write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized);
  70 
  71   void write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized);
  72 
  73   // We export this to make it available in cases where the static
  74   // type of the barrier set is known.  Note that it is non-virtual.
  75   template <class T> inline void inline_write_ref_field_pre(T* field, oop new_val);
  76 
  77   // These are the more general virtual versions.
  78   void write_ref_field_pre_work(oop* field, oop new_val);
  79   void write_ref_field_pre_work(narrowOop* field, oop new_val);
  80   void write_ref_field_pre_work(void* field, oop new_val);
  81 
  82   void write_ref_field_work(void* v, oop o, bool release = false);
  83   void write_region(MemRegion mr);
  84 
  85   virtual void on_thread_create(Thread* thread);
  86   virtual void on_thread_destroy(Thread* thread);
  87   virtual void on_thread_attach(JavaThread* thread);
  88   virtual void on_thread_detach(JavaThread* thread);
  89 
  90   virtual oop read_barrier(oop src);
  91 
  92   static inline oop resolve_forwarded_not_null(oop p);
  93   static inline oop resolve_forwarded(oop p);
  94 
  95   virtual oop write_barrier(oop obj);
  96 
  97   oop write_barrier_mutator(oop obj);
  98 
  99   virtual oop storeval_barrier(oop obj);
 100 
 101   virtual void keep_alive_barrier(oop obj);
 102 
 103   bool obj_equals(oop obj1, oop obj2);
 104 
 105 #ifdef CHECK_UNHANDLED_OOPS
 106   bool oop_equals_operator_allowed() { return !ShenandoahVerifyObjectEquals; }
 107 #endif
 108 
 109   void enqueue(oop obj);
 110 
 111 private:
 112   inline bool need_update_refs_barrier();
 113 
 114   template <class T, bool STOREVAL_WRITE_BARRIER>
 115   void write_ref_array_loop(HeapWord* start, size_t count);
 116 
 117   oop write_barrier_impl(oop obj);
 118 
 119   static void keep_alive_if_weak(DecoratorSet decorators, oop value) {
 120     assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 121     const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
 122     const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
 123     if (!peek && !on_strong_oop_ref && value != NULL) {
 124       ShenandoahBarrierSet::barrier_set()->keep_alive_barrier(value);
 125     }
 126   }
 127 
 128   template <typename T>
 129   bool arraycopy_loop_1(T* src, T* dst, size_t length, Klass* bound,
 130                         bool checkcast, bool satb, bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 131 
 132   template <typename T, bool CHECKCAST>
 133   bool arraycopy_loop_2(T* src, T* dst, size_t length, Klass* bound,
 134                         bool satb, bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 135 
 136   template <typename T, bool CHECKCAST, bool SATB>
 137   bool arraycopy_loop_3(T* src, T* dst, size_t length, Klass* bound,
 138                         bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 139 
 140   template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 141   bool arraycopy_loop(T* src, T* dst, size_t length, Klass* bound, bool disjoint);
 142 
 143   template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 144   bool arraycopy_element(T* cur_src, T* cur_dst, Klass* bound, Thread* thread);
 145 
 146 public:
 147   // Callbacks for runtime accesses.
 148   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
 149   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
 150     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
 151 
 152   public:
 153     // Primitive heap accesses. These accessors get resolved when
 154     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 155     // not an oop_* overload, and the barrier strength is AS_NORMAL.
 156     template <typename T>
 157     static T load_in_heap(T* addr) {
 158       ShouldNotReachHere();
 159       return Raw::template load<T>(addr);
 160     }
 161 
 162     template <typename T>
 163     static T load_in_heap_at(oop base, ptrdiff_t offset) {
 164       base = ShenandoahBarrierSet::resolve_forwarded(base);
 165       return Raw::template load_at<T>(base, offset);
 166     }
 167 
 168     template <typename T>
 169     static void store_in_heap(T* addr, T value) {
 170       ShouldNotReachHere();
 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       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 177       Raw::store_at(base, offset, value);
 178     }
 179 
 180     template <typename T>
 181     static T atomic_cmpxchg_in_heap(T new_value, T* addr, T compare_value) {
 182       ShouldNotReachHere();
 183       return Raw::atomic_cmpxchg(new_value, addr, compare_value);
 184     }
 185 
 186     template <typename T>
 187     static T atomic_cmpxchg_in_heap_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
 188       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 189       return Raw::atomic_cmpxchg_at(new_value, base, offset, compare_value);
 190     }
 191 
 192     template <typename T>
 193     static T atomic_xchg_in_heap(T new_value, T* addr) {
 194       ShouldNotReachHere();
 195       return Raw::atomic_xchg(new_value, addr);
 196     }
 197 
 198     template <typename T>
 199     static T atomic_xchg_in_heap_at(T new_value, oop base, ptrdiff_t offset) {
 200       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 201       return Raw::atomic_xchg_at(new_value, base, offset);
 202     }
 203 
 204     template <typename T>
 205     static void arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 206                                   arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 207                                   size_t length);
 208 
 209     // Heap oop accesses. These accessors get resolved when
 210     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 211     // an oop_* overload, and the barrier strength is AS_NORMAL.
 212     template <typename T>
 213     static oop oop_load_in_heap(T* addr) {
 214       // ShouldNotReachHere();
 215       oop value = Raw::template oop_load<oop>(addr);
 216       keep_alive_if_weak(decorators, value);
 217       return value;
 218     }
 219 
 220     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 221       base = ShenandoahBarrierSet::resolve_forwarded(base);
 222       oop value = Raw::template oop_load_at<oop>(base, offset);
 223       keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), value);
 224       return value;
 225     }
 226 
 227     template <typename T>
 228     static void oop_store_in_heap(T* addr, oop value) {
 229       ShenandoahBarrierSet::barrier_set()->write_ref_field_pre_work(addr, value);
 230       Raw::oop_store(addr, value);
 231     }
 232 
 233     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 234       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 235       value = ShenandoahBarrierSet::barrier_set()->storeval_barrier(value);
 236 
 237       oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
 238     }
 239 
 240     template <typename T>
 241     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value);
 242 
 243     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 244       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 245       new_value = ShenandoahBarrierSet::barrier_set()->storeval_barrier(new_value);
 246       return oop_atomic_cmpxchg_in_heap(new_value, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value);
 247     }
 248 
 249     template <typename T>
 250     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr);
 251 
 252     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
 253       base = ShenandoahBarrierSet::barrier_set()->write_barrier(base);
 254       new_value = ShenandoahBarrierSet::barrier_set()->storeval_barrier(new_value);
 255       return oop_atomic_xchg_in_heap(new_value, AccessInternal::oop_field_addr<decorators>(base, offset));
 256     }
 257 
 258     template <typename T>
 259     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 260                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 261                                       size_t length);
 262 
 263     // Clone barrier support
 264     static void clone_in_heap(oop src, oop dst, size_t size);
 265 
 266     // Needed for loads on non-heap weak references
 267     template <typename T>
 268     static oop oop_load_not_in_heap(T* addr) {
 269       oop value = Raw::oop_load_not_in_heap(addr);
 270       keep_alive_if_weak(decorators, value);
 271       return value;
 272     }
 273 
 274     static oop resolve(oop obj) {
 275       return ShenandoahBarrierSet::barrier_set()->write_barrier(obj);
 276     }
 277 
 278     static bool equals(oop o1, oop o2) {
 279       return ShenandoahBarrierSet::barrier_set()->obj_equals(o1, o2);
 280     }
 281 
 282   };
 283 
 284 };
 285 
 286 template<>
 287 struct BarrierSet::GetName<ShenandoahBarrierSet> {
 288   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
 289 };
 290 
 291 template<>
 292 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
 293   typedef ::ShenandoahBarrierSet type;
 294 };
 295 
 296 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP