1 /*
   2  * Copyright (c) 2015, 2019, 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     bs->keep_alive_if_weak<decorators>(value);
 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   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(cast_from_oop<HeapWord *>(obj))) {
 272         assert(_heap->has_forwarded_objects(), "only get here with forwarded objects");
 273         oop fwd = resolve_forwarded_not_null(obj);
 274         if (EVAC && obj == fwd) {
 275           fwd = _heap->evacuate_object(obj, thread);
 276         }
 277         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
 278         oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);
 279         obj = fwd;
 280       }
 281       if (ENQUEUE && !ctx->is_marked(obj)) {
 282         queue.enqueue_known_active(obj);
 283       }
 284     }
 285   }
 286 }
 287 
 288 template <class T>
 289 void ShenandoahBarrierSet::arraycopy_pre_work(T* src, T* dst, size_t count) {
 290   if (_heap->is_concurrent_mark_in_progress()) {
 291     if (_heap->has_forwarded_objects()) {
 292       arraycopy_work<T, true, false, true>(dst, count);
 293     } else {
 294       arraycopy_work<T, false, false, true>(dst, count);
 295     }
 296   }
 297 
 298   arraycopy_update_impl(src, count);
 299 }
 300 
 301 void ShenandoahBarrierSet::arraycopy_pre(oop* src, oop* dst, size_t count) {
 302   arraycopy_pre_work(src, dst, count);
 303 }
 304 
 305 void ShenandoahBarrierSet::arraycopy_pre(narrowOop* src, narrowOop* dst, size_t count) {
 306   arraycopy_pre_work(src, dst, count);
 307 }
 308 
 309 template <class T>
 310 void ShenandoahBarrierSet::arraycopy_update_impl(T* src, size_t count) {
 311   if (_heap->is_evacuation_in_progress()) {
 312     ShenandoahEvacOOMScope oom_evac;
 313     arraycopy_work<T, true, true, false>(src, count);
 314   } else if (_heap->is_concurrent_traversal_in_progress()){
 315     ShenandoahEvacOOMScope oom_evac;
 316     arraycopy_work<T, true, true, true>(src, count);
 317   } else if (_heap->has_forwarded_objects()) {
 318     arraycopy_work<T, true, false, false>(src, count);
 319   }
 320 }
 321 
 322 void ShenandoahBarrierSet::arraycopy_update(oop* src, size_t count) {
 323   arraycopy_update_impl(src, count);
 324 }
 325 
 326 void ShenandoahBarrierSet::arraycopy_update(narrowOop* src, size_t count) {
 327   arraycopy_update_impl(src, count);
 328 }
 329 
 330 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP