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