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