1 /*
   2  * Copyright (c) 2015, 2020, Red Hat, Inc. 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_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  27 
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahAsserts.hpp"
  30 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  31 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  32 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
  33 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  34 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  35 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  36 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  37 #include "memory/iterator.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 
  40 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
  41   return ShenandoahForwarding::get_forwardee(p);
  42 }
  43 
  44 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
  45   if (p != NULL) {
  46     return resolve_forwarded_not_null(p);
  47   } else {
  48     return p;
  49   }
  50 }
  51 
  52 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {
  53   return ShenandoahForwarding::get_forwardee_mutator(p);
  54 }
  55 
  56 inline void ShenandoahBarrierSet::enqueue(oop obj) {
  57   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
  58 
  59   // Filter marked objects before hitting the SATB queues. The same predicate would
  60   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
  61   // filtering here helps to avoid wasteful SATB queueing work to begin with.
  62   if (!_heap->requires_marking<false>(obj)) return;
  63 
  64   ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj);
  65 }
  66 
  67 template <DecoratorSet decorators, typename T>
  68 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
  69   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
  70       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  71     return;
  72   }
  73   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
  74     T heap_oop = RawAccess<>::oop_load(field);
  75     if (!CompressedOops::is_null(heap_oop)) {
  76       enqueue(CompressedOops::decode(heap_oop));
  77     }
  78   }
  79 }
  80 
  81 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
  82   assert(value != NULL, "checked before");
  83   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
  84     enqueue(value);
  85   }
  86 }
  87 
  88 inline void ShenandoahBarrierSet::storeval_barrier(oop obj) {
  89   if (obj != NULL && ShenandoahStoreValEnqueueBarrier) {
  90     enqueue(obj);
  91   }
  92 }
  93 
  94 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
  95   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
  96   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
  97   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
  98   if (!peek && !on_strong_oop_ref) {
  99     satb_enqueue(value);
 100   }
 101 }
 102 
 103 template <DecoratorSet decorators>
 104 inline void ShenandoahBarrierSet::keep_alive_if_weak(oop value) {
 105   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 106   if (!HasDecorator<decorators, ON_STRONG_OOP_REF>::value &&
 107       !HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
 108     satb_enqueue(value);
 109   }
 110 }
 111 
 112 template <DecoratorSet decorators, typename BarrierSetT>
 113 template <typename T>
 114 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
 115   oop value = Raw::oop_load_not_in_heap(addr);
 116   if (value != NULL) {
 117     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 118     value = bs->load_reference_barrier_native(value, addr);
 119     if (value != NULL) {
 120       bs->keep_alive_if_weak<decorators>(value);
 121     }
 122   }
 123   return value;
 124 }
 125 
 126 template <DecoratorSet decorators, typename BarrierSetT>
 127 template <typename T>
 128 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
 129   oop value = Raw::oop_load_in_heap(addr);
 130   if (value != NULL) {
 131     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 132     value = bs->load_reference_barrier_not_null(value);
 133     bs->keep_alive_if_weak<decorators>(value);
 134   }
 135   return value;
 136 }
 137 
 138 template <DecoratorSet decorators, typename BarrierSetT>
 139 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 140   oop value = Raw::oop_load_in_heap_at(base, offset);
 141   if (value != NULL) {
 142     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 143     value = bs->load_reference_barrier_not_null(value);
 144     bs->keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset),
 145                            value);
 146   }
 147   return value;
 148 }
 149 
 150 template <DecoratorSet decorators, typename BarrierSetT>
 151 template <typename T>
 152 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
 153   shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());
 154   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
 155   bs->storeval_barrier(value);
 156   bs->satb_barrier<decorators>(addr);
 157   Raw::oop_store(addr, value);
 158 }
 159 
 160 template <DecoratorSet decorators, typename BarrierSetT>
 161 template <typename T>
 162 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
 163   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
 164   shenandoah_assert_not_forwarded_except  (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 165   shenandoah_assert_not_in_cset_except    (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 166 
 167   oop_store_not_in_heap(addr, value);
 168 }
 169 
 170 template <DecoratorSet decorators, typename BarrierSetT>
 171 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 172   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
 173 }
 174 
 175 template <DecoratorSet decorators, typename BarrierSetT>
 176 template <typename T>
 177 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
 178   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 179   bs->storeval_barrier(new_value);
 180 
 181   oop res;
 182   oop expected = compare_value;
 183   do {
 184     compare_value = expected;
 185     res = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
 186     expected = res;
 187   } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
 188 
 189   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 190   // because it must be the previous value.
 191   if (res != NULL) {
 192     res = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(res);
 193     bs->satb_enqueue(res);
 194   }
 195   return res;
 196 }
 197 
 198 template <DecoratorSet decorators, typename BarrierSetT>
 199 template <typename T>
 200 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
 201   return oop_atomic_cmpxchg_not_in_heap(addr, compare_value, new_value);
 202 }
 203 
 204 template <DecoratorSet decorators, typename BarrierSetT>
 205 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
 206   return oop_atomic_cmpxchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);
 207 }
 208 
 209 template <DecoratorSet decorators, typename BarrierSetT>
 210 template <typename T>
 211 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
 212   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 213   bs->storeval_barrier(new_value);
 214 
 215   oop previous = Raw::oop_atomic_xchg(addr, new_value);
 216 
 217   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 218   // because it must be the previous value.
 219   if (previous != NULL) {
 220     previous = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(previous);
 221     bs->satb_enqueue(previous);
 222   }
 223   return previous;
 224 }
 225 
 226 template <DecoratorSet decorators, typename BarrierSetT>
 227 template <typename T>
 228 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
 229   return oop_atomic_xchg_not_in_heap(addr, new_value);
 230 }
 231 
 232 template <DecoratorSet decorators, typename BarrierSetT>
 233 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
 234   return oop_atomic_xchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), new_value);
 235 }
 236 
 237 // Clone barrier support
 238 template <DecoratorSet decorators, typename BarrierSetT>
 239 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
 240   if (ShenandoahCloneBarrier) {
 241     ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
 242   }
 243   Raw::clone(src, dst, size);
 244 }
 245 
 246 template <DecoratorSet decorators, typename BarrierSetT>
 247 template <typename T>
 248 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 249                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 250                                                                                          size_t length) {
 251   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 252   bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
 253                         arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
 254                         length);
 255   return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
 256 }
 257 
 258 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 259 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
 260   assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");
 261 
 262   Thread* thread = Thread::current();
 263   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 264   ShenandoahMarkingContext* ctx = _heap->marking_context();
 265   const ShenandoahCollectionSet* const cset = _heap->collection_set();
 266   T* end = src + count;
 267   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
 268     T o = RawAccess<>::oop_load(elem_ptr);
 269     if (!CompressedOops::is_null(o)) {
 270       oop obj = CompressedOops::decode_not_null(o);
 271       if (HAS_FWD && cset->is_in(obj)) {
 272         oop fwd = resolve_forwarded_not_null(obj);
 273         if (EVAC && obj == fwd) {
 274           fwd = _heap->evacuate_object(obj, thread);
 275         }
 276         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
 277         oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);
 278         obj = fwd;
 279       }
 280       if (ENQUEUE && !ctx->is_marked(obj)) {
 281         queue.enqueue_known_active(obj);
 282       }
 283     }
 284   }
 285 }
 286 
 287 template <class T>
 288 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
 289   if (count == 0) {
 290     return;
 291   }
 292   int gc_state = _heap->gc_state();
 293   if ((gc_state & ShenandoahHeap::MARKING) != 0) {
 294     arraycopy_marking(dst, count);
 295   } else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
 296     arraycopy_evacuation(src, count);
 297   } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
 298     arraycopy_update(src, count);
 299   }
 300 }
 301 
 302 template <class T>
 303 void ShenandoahBarrierSet::arraycopy_marking(T* array, size_t count) {
 304   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
 305   if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {
 306     arraycopy_work<T, false, false, true>(array, count);
 307   }
 308 }
 309 
 310 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
 311   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
 312 }
 313 
 314 template <class T>
 315 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
 316   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
 317   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
 318     ShenandoahEvacOOMScope oom_evac;
 319     arraycopy_work<T, true, true, false>(src, count);
 320   }
 321 }
 322 
 323 template <class T>
 324 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
 325   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
 326   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
 327     arraycopy_work<T, true, false, false>(src, count);
 328   }
 329 }
 330 
 331 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP