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