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