1 /*
   2  * Copyright (c) 2015, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  26 
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shenandoah/shenandoahAsserts.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  30 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  31 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
  32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  33 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  34 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  35 #include "gc/shenandoah/shenandoahSafepoint.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   assert(ShenandoahHeap::heap()->in_collection_set(p) ||
  42          ShenandoahSafepoint::is_at_shenandoah_safepoint(),
  43          "No forwarding oops outside cset");
  44   return ShenandoahForwarding::get_forwardee(p);
  45 }
  46 
  47 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
  48   if (((HeapWord*) p) != NULL) {
  49     return resolve_forwarded_not_null(p);
  50   } else {
  51     return p;
  52   }
  53 }
  54 
  55 inline oop ShenandoahBarrierSet::resolve_forwarded_checked(oop p) {
  56   if (p == NULL) return p;
  57   ShenandoahHeap* const heap = ShenandoahHeap::heap();
  58   if (heap->in_collection_set(p) ||
  59      (heap->is_concurrent_traversal_in_progress() && heap->is_degenerated_gc_in_progress())) {
  60     return resolve_forwarded_not_null(p);
  61   } else {
  62     return p;
  63   }
  64 }
  65 
  66 inline void ShenandoahBarrierSet::enqueue(oop obj) {
  67   shenandoah_assert_not_forwarded_if(NULL, obj, _heap->is_concurrent_traversal_in_progress());
  68   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
  69 
  70   // Filter marked objects before hitting the SATB queues. The same predicate would
  71   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
  72   // filtering here helps to avoid wasteful SATB queueing work to begin with.
  73   if (!_heap->requires_marking<false>(obj)) return;
  74 
  75   ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj);
  76 }
  77 
  78 template <DecoratorSet decorators, typename T>
  79 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
  80   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
  81       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  82     return;
  83   }
  84   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
  85     T heap_oop = RawAccess<>::oop_load(field);
  86     if (!CompressedOops::is_null(heap_oop)) {
  87       enqueue(CompressedOops::decode(heap_oop));
  88     }
  89   }
  90 }
  91 
  92 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
  93   assert(value != NULL, "checked before");
  94   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
  95     enqueue(value);
  96   }
  97 }
  98 
  99 inline void ShenandoahBarrierSet::storeval_barrier(oop obj) {
 100   if (obj != NULL && ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_traversal_in_progress()) {
 101     enqueue(obj);
 102   }
 103 }
 104 
 105 inline void ShenandoahBarrierSet::keep_alive_barrier(oop value) {
 106   assert(value != NULL, "checked before");
 107   if (ShenandoahKeepAliveBarrier && _heap->is_concurrent_mark_in_progress()) {
 108     enqueue(value);
 109   }
 110 }
 111 
 112 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
 113   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 114   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
 115   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
 116   if (!peek && !on_strong_oop_ref) {
 117     keep_alive_barrier(value);
 118   }
 119 }
 120 
 121 template <DecoratorSet decorators>
 122 inline void ShenandoahBarrierSet::keep_alive_if_weak(oop value) {
 123   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 124   if (!HasDecorator<decorators, ON_STRONG_OOP_REF>::value &&
 125       !HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
 126     keep_alive_barrier(value);
 127   }
 128 }
 129 
 130 template <DecoratorSet decorators, typename BarrierSetT>
 131 template <typename T>
 132 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
 133   oop value = Raw::oop_load_not_in_heap(addr);
 134   if (value != NULL) {
 135     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 136     value = bs->load_reference_barrier_native(value, addr);
 137     if (value != NULL) {
 138       bs->keep_alive_if_weak<decorators>(value);
 139     }
 140   }
 141   return value;
 142 }
 143 
 144 template <DecoratorSet decorators, typename BarrierSetT>
 145 template <typename T>
 146 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
 147   oop value = Raw::oop_load_in_heap(addr);
 148   if (value != NULL) {
 149     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 150     value = bs->load_reference_barrier_not_null(value);
 151     bs->keep_alive_if_weak<decorators>(value);
 152   }
 153   return value;
 154 }
 155 
 156 template <DecoratorSet decorators, typename BarrierSetT>
 157 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 158   oop value = Raw::oop_load_in_heap_at(base, offset);
 159   if (value != NULL) {
 160     ShenandoahBarrierSet *const bs = ShenandoahBarrierSet::barrier_set();
 161     value = bs->load_reference_barrier_not_null(value);
 162     bs->keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset),
 163                            value);
 164   }
 165   return value;
 166 }
 167 
 168 template <DecoratorSet decorators, typename BarrierSetT>
 169 template <typename T>
 170 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
 171   shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());
 172   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
 173   bs->storeval_barrier(value);
 174   bs->satb_barrier<decorators>(addr);
 175   Raw::oop_store(addr, value);
 176 }
 177 
 178 template <DecoratorSet decorators, typename BarrierSetT>
 179 template <typename T>
 180 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
 181   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
 182   shenandoah_assert_not_forwarded_except  (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 183   shenandoah_assert_not_in_cset_except    (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 184 
 185   oop_store_not_in_heap(addr, value);
 186 }
 187 
 188 template <DecoratorSet decorators, typename BarrierSetT>
 189 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
 190   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
 191 }
 192 
 193 template <DecoratorSet decorators, typename BarrierSetT>
 194 template <typename T>
 195 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
 196   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 197   bs->storeval_barrier(new_value);
 198 
 199   oop res;
 200   oop expected = compare_value;
 201   do {
 202     compare_value = expected;
 203     res = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
 204     expected = res;
 205   } while ((compare_value != expected) && (resolve_forwarded_checked(compare_value) == resolve_forwarded_checked(expected)));
 206 
 207   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 208   // because it must be the previous value.
 209   if (res != NULL) {
 210     res = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(res);
 211     bs->satb_enqueue(res);
 212   }
 213   return res;
 214 }
 215 
 216 template <DecoratorSet decorators, typename BarrierSetT>
 217 template <typename T>
 218 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
 219   return oop_atomic_cmpxchg_not_in_heap(addr, compare_value, new_value);
 220 }
 221 
 222 template <DecoratorSet decorators, typename BarrierSetT>
 223 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
 224   return oop_atomic_cmpxchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);
 225 }
 226 
 227 template <DecoratorSet decorators, typename BarrierSetT>
 228 template <typename T>
 229 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
 230   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 231   bs->storeval_barrier(new_value);
 232 
 233   oop previous = Raw::oop_atomic_xchg(addr, new_value);
 234 
 235   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
 236   // because it must be the previous value.
 237   if (previous != NULL) {
 238     previous = ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(previous);
 239     bs->satb_enqueue(previous);
 240   }
 241   return previous;
 242 }
 243 
 244 template <DecoratorSet decorators, typename BarrierSetT>
 245 template <typename T>
 246 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
 247   return oop_atomic_xchg_not_in_heap(addr, new_value);
 248 }
 249 
 250 template <DecoratorSet decorators, typename BarrierSetT>
 251 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
 252   return oop_atomic_xchg_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), new_value);
 253 }
 254 
 255 // Clone barrier support
 256 template <DecoratorSet decorators, typename BarrierSetT>
 257 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
 258   if (ShenandoahCloneBarrier) {
 259     ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
 260   }
 261   Raw::clone(src, dst, size);
 262 }
 263 
 264 template <DecoratorSet decorators, typename BarrierSetT>
 265 template <typename T>
 266 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 267                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 268                                                                                          size_t length) {
 269   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 270   bs->arraycopy_pre(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
 271                     arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
 272                     length);
 273   return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
 274 }
 275 
 276 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 277 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
 278   Thread* thread = Thread::current();
 279   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 280   ShenandoahMarkingContext* ctx = _heap->marking_context();
 281   const ShenandoahCollectionSet* const cset = _heap->collection_set();
 282   T* end = src + count;
 283   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
 284     T o = RawAccess<>::oop_load(elem_ptr);
 285     if (!CompressedOops::is_null(o)) {
 286       oop obj = CompressedOops::decode_not_null(o);
 287       if (HAS_FWD && cset->is_in((HeapWord *) obj)) {
 288         assert(_heap->has_forwarded_objects(), "only get here with forwarded objects");
 289         oop fwd = resolve_forwarded_not_null(obj);
 290         if (EVAC && obj == fwd) {
 291           fwd = _heap->evacuate_object(obj, thread);
 292         }
 293         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
 294         oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);
 295         obj = fwd;
 296       }
 297       if (ENQUEUE && !ctx->is_marked(obj)) {
 298         queue.enqueue_known_active(obj);
 299       }
 300     }
 301   }
 302 }
 303 
 304 template <class T>
 305 void ShenandoahBarrierSet::arraycopy_pre_work(T* src, T* dst, size_t count) {
 306   if (_heap->is_concurrent_mark_in_progress()) {
 307     if (_heap->has_forwarded_objects()) {
 308       arraycopy_work<T, true, false, true>(dst, count);
 309     } else {
 310       arraycopy_work<T, false, false, true>(dst, count);
 311     }
 312   }
 313 
 314   arraycopy_update_impl(src, count);
 315 }
 316 
 317 void ShenandoahBarrierSet::arraycopy_pre(oop* src, oop* dst, size_t count) {
 318   arraycopy_pre_work(src, dst, count);
 319 }
 320 
 321 void ShenandoahBarrierSet::arraycopy_pre(narrowOop* src, narrowOop* dst, size_t count) {
 322   arraycopy_pre_work(src, dst, count);
 323 }
 324 
 325 template <class T>
 326 void ShenandoahBarrierSet::arraycopy_update_impl(T* src, size_t count) {
 327   if (_heap->is_evacuation_in_progress()) {
 328     ShenandoahEvacOOMScope oom_evac;
 329     arraycopy_work<T, true, true, false>(src, count);
 330   } else if (_heap->is_concurrent_traversal_in_progress()){
 331     ShenandoahEvacOOMScope oom_evac;
 332     arraycopy_work<T, true, true, true>(src, count);
 333   } else if (_heap->has_forwarded_objects()) {
 334     arraycopy_work<T, true, false, false>(src, count);
 335   }
 336 }
 337 
 338 void ShenandoahBarrierSet::arraycopy_update(oop* src, size_t count) {
 339   arraycopy_update_impl(src, count);
 340 }
 341 
 342 void ShenandoahBarrierSet::arraycopy_update(narrowOop* src, size_t count) {
 343   arraycopy_update_impl(src, count);
 344 }
 345 
 346 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP