1 /*
   2  * Copyright (c) 2015, 2018, 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/shenandoahBarrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahBrooksPointer.inline.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 
  32 bool ShenandoahBarrierSet::need_update_refs_barrier() {
  33   return _heap->is_update_refs_in_progress() ||
  34          _heap->is_concurrent_traversal_in_progress() ||
  35          (_heap->is_concurrent_mark_in_progress() && _heap->has_forwarded_objects());
  36 }
  37 
  38 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
  39   return ShenandoahBrooksPointer::forwardee(p);
  40 }
  41 
  42 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
  43   if (((HeapWord*) p) != NULL) {
  44     return resolve_forwarded_not_null(p);
  45   } else {
  46     return p;
  47   }
  48 }
  49 
  50 template <DecoratorSet decorators, typename BarrierSetT>
  51 template <typename T>
  52 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
  53   oop value = Raw::oop_load_in_heap(addr);
  54   value = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(value);
  55   keep_alive_if_weak(decorators, value);
  56   return value;
  57 }
  58 
  59 template <DecoratorSet decorators, typename BarrierSetT>
  60 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
  61   oop value = Raw::oop_load_in_heap_at(base, offset);
  62   value = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(value);
  63   keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), value);
  64   return value;
  65 }
  66 
  67 template <DecoratorSet decorators, typename BarrierSetT>
  68 template <typename T>
  69 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
  70   oop value = Raw::oop_load_not_in_heap(addr);
  71   value = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(value);
  72   keep_alive_if_weak(decorators, value);
  73   return value;
  74 }
  75 
  76 template <DecoratorSet decorators, typename BarrierSetT>
  77 template <typename T>
  78 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
  79   ShenandoahBarrierSet::barrier_set()->storeval_barrier(value);
  80   const bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
  81   if (keep_alive) {
  82     ShenandoahBarrierSet::barrier_set()->write_ref_field_pre_work(addr, value);
  83   }
  84   Raw::oop_store_in_heap(addr, value);
  85 }
  86 
  87 template <DecoratorSet decorators, typename BarrierSetT>
  88 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
  89   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
  90 }
  91 
  92 template <DecoratorSet decorators, typename BarrierSetT>
  93 template <typename T>
  94 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
  95   oop res;
  96   oop expected = compare_value;
  97   do {
  98     compare_value = expected;
  99     res = Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
 100     expected = res;
 101   } while ((! oopDesc::equals_raw(compare_value, expected)) && oopDesc::equals_raw(resolve_forwarded(compare_value), resolve_forwarded(expected)));
 102   if (res != NULL) {
 103     return ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(res);
 104   } else {
 105     return res;
 106   }
 107 }
 108 
 109 template <DecoratorSet decorators, typename BarrierSetT>
 110 template <typename T>
 111 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_impl(oop new_value, T* addr, oop compare_value) {
 112   ShenandoahBarrierSet::barrier_set()->storeval_barrier(new_value);
 113   oop result = oop_atomic_cmpxchg_not_in_heap(new_value, addr, compare_value);
 114   const bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 115   if (keep_alive && ShenandoahSATBBarrier && !CompressedOops::is_null(result) &&
 116       oopDesc::equals_raw(result, compare_value)) {
 117     ShenandoahBarrierSet::barrier_set()->enqueue(result);
 118   }
 119   return result;
 120 }
 121 
 122 template <DecoratorSet decorators, typename BarrierSetT>
 123 template <typename T>
 124 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
 125   oop result = oop_atomic_cmpxchg_in_heap_impl(new_value, addr, compare_value);
 126   keep_alive_if_weak(decorators, result);
 127   return result;
 128 }
 129 
 130 template <DecoratorSet decorators, typename BarrierSetT>
 131 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 132   oop result = oop_atomic_cmpxchg_in_heap_impl(new_value, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value);
 133   keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), result);
 134   return result;
 135 }
 136 
 137 template <DecoratorSet decorators, typename BarrierSetT>
 138 template <typename T>
 139 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
 140   oop previous = Raw::oop_atomic_xchg(new_value, addr);
 141   if (previous != NULL) {
 142     return ShenandoahBarrierSet::barrier_set()->load_reference_barrier_not_null(previous);
 143   } else {
 144     return previous;
 145   }
 146 }
 147 
 148 template <DecoratorSet decorators, typename BarrierSetT>
 149 template <typename T>
 150 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_impl(oop new_value, T* addr) {
 151   ShenandoahBarrierSet::barrier_set()->storeval_barrier(new_value);
 152   oop result = oop_atomic_xchg_not_in_heap(new_value, addr);
 153   const bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 154   if (keep_alive && ShenandoahSATBBarrier && !CompressedOops::is_null(result)) {
 155     ShenandoahBarrierSet::barrier_set()->enqueue(result);
 156   }
 157   return result;
 158 }
 159 
 160 template <DecoratorSet decorators, typename BarrierSetT>
 161 template <typename T>
 162 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(oop new_value, T* addr) {
 163   oop result = oop_atomic_xchg_in_heap_impl(new_value, addr);
 164   keep_alive_if_weak(addr, result);
 165   return result;
 166 }
 167 
 168 template <DecoratorSet decorators, typename BarrierSetT>
 169 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
 170   oop result = oop_atomic_xchg_in_heap_impl(new_value, AccessInternal::oop_field_addr<decorators>(base, offset));
 171   keep_alive_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), result);
 172   return result;
 173 }
 174 
 175 template <typename T>
 176 bool ShenandoahBarrierSet::arraycopy_loop_1(T* src, T* dst, size_t length, Klass* bound,
 177                                             bool checkcast, bool satb, bool disjoint,
 178                                             ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode) {
 179   if (checkcast) {
 180     return arraycopy_loop_2<T, true>(src, dst, length, bound, satb, disjoint, storeval_mode);
 181   } else {
 182     return arraycopy_loop_2<T, false>(src, dst, length, bound, satb, disjoint, storeval_mode);
 183   }
 184 }
 185 
 186 template <typename T, bool CHECKCAST>
 187 bool ShenandoahBarrierSet::arraycopy_loop_2(T* src, T* dst, size_t length, Klass* bound,
 188                                             bool satb, bool disjoint,
 189                                             ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode) {
 190   if (satb) {
 191     return arraycopy_loop_3<T, CHECKCAST, true>(src, dst, length, bound, disjoint, storeval_mode);
 192   } else {
 193     return arraycopy_loop_3<T, CHECKCAST, false>(src, dst, length, bound, disjoint, storeval_mode);
 194   }
 195 }
 196 
 197 template <typename T, bool CHECKCAST, bool SATB>
 198 bool ShenandoahBarrierSet::arraycopy_loop_3(T* src, T* dst, size_t length, Klass* bound, bool disjoint,
 199                                             ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode) {
 200   switch (storeval_mode) {
 201     case NONE:
 202       return arraycopy_loop<T, CHECKCAST, SATB, NONE>(src, dst, length, bound, disjoint);
 203     case READ_BARRIER:
 204       return arraycopy_loop<T, CHECKCAST, SATB, READ_BARRIER>(src, dst, length, bound, disjoint);
 205     case WRITE_BARRIER:
 206       return arraycopy_loop<T, CHECKCAST, SATB, WRITE_BARRIER>(src, dst, length, bound, disjoint);
 207     default:
 208       ShouldNotReachHere();
 209       return true; // happy compiler
 210   }
 211 }
 212 
 213 template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 214 bool ShenandoahBarrierSet::arraycopy_loop(T* src, T* dst, size_t length, Klass* bound, bool disjoint) {
 215   Thread* thread = Thread::current();
 216 
 217   ShenandoahEvacOOMScope oom_evac_scope;
 218 
 219   // We need to handle four cases:
 220   //
 221   // a) src < dst, conjoint, can only copy backward only
 222   //   [...src...]
 223   //         [...dst...]
 224   //
 225   // b) src < dst, disjoint, can only copy forward, because types may mismatch
 226   //   [...src...]
 227   //              [...dst...]
 228   //
 229   // c) src > dst, conjoint, can copy forward only
 230   //         [...src...]
 231   //   [...dst...]
 232   //
 233   // d) src > dst, disjoint, can only copy forward, because types may mismatch
 234   //              [...src...]
 235   //   [...dst...]
 236   //
 237   if (src > dst || disjoint) {
 238     // copy forward:
 239     T* cur_src = src;
 240     T* cur_dst = dst;
 241     T* src_end = src + length;
 242     for (; cur_src < src_end; cur_src++, cur_dst++) {
 243       if (!arraycopy_element<T, CHECKCAST, SATB, STOREVAL_MODE>(cur_src, cur_dst, bound, thread)) {
 244         return false;
 245       }
 246     }
 247   } else {
 248     // copy backward:
 249     T* cur_src = src + length - 1;
 250     T* cur_dst = dst + length - 1;
 251     for (; cur_src >= src; cur_src--, cur_dst--) {
 252       if (!arraycopy_element<T, CHECKCAST, SATB, STOREVAL_MODE>(cur_src, cur_dst, bound, thread)) {
 253         return false;
 254       }
 255     }
 256   }
 257   return true;
 258 }
 259 
 260 template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 261 bool ShenandoahBarrierSet::arraycopy_element(T* cur_src, T* cur_dst, Klass* bound, Thread* thread) {
 262   T o = RawAccess<>::oop_load(cur_src);
 263 
 264   if (SATB) {
 265     T prev = RawAccess<>::oop_load(cur_dst);
 266     if (!CompressedOops::is_null(prev)) {
 267       oop prev_obj = CompressedOops::decode_not_null(prev);
 268       enqueue(prev_obj);
 269     }
 270   }
 271 
 272   if (!CompressedOops::is_null(o)) {
 273     oop obj = CompressedOops::decode_not_null(o);
 274 
 275     if (CHECKCAST) {
 276       assert(bound != NULL, "need element klass for checkcast");
 277       if (!oopDesc::is_instanceof_or_null(obj, bound)) {
 278         return false;
 279       }
 280     }
 281 
 282     switch (STOREVAL_MODE) {
 283     case NONE:
 284       break;
 285     case READ_BARRIER:
 286       obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 287       break;
 288     case WRITE_BARRIER:
 289       if (_heap->in_collection_set(obj)) {
 290         oop forw = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 291         if (oopDesc::equals_raw(forw, obj)) {
 292           forw = _heap->evacuate_object(forw, thread);
 293         }
 294         obj = forw;
 295       }
 296       enqueue(obj);
 297       break;
 298     default:
 299       ShouldNotReachHere();
 300     }
 301 
 302     RawAccess<IS_NOT_NULL>::oop_store(cur_dst, obj);
 303   } else {
 304     // Store null.
 305     RawAccess<>::oop_store(cur_dst, o);
 306   }
 307   return true;
 308 }
 309 
 310 // Clone barrier support
 311 template <DecoratorSet decorators, typename BarrierSetT>
 312 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
 313   Raw::clone(src, dst, size);
 314   ShenandoahBarrierSet::barrier_set()->write_region(MemRegion((HeapWord*) dst, size));
 315 }
 316 
 317 template <DecoratorSet decorators, typename BarrierSetT>
 318 template <typename T>
 319 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 320                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 321                                                                                          size_t length) {
 322   ShenandoahHeap* heap = ShenandoahHeap::heap();
 323   bool satb = ShenandoahSATBBarrier && heap->is_concurrent_mark_in_progress();
 324   bool checkcast = HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value;
 325   bool disjoint = HasDecorator<decorators, ARRAYCOPY_DISJOINT>::value;
 326   ArrayCopyStoreValMode storeval_mode;
 327   if (heap->has_forwarded_objects()) {
 328     if (heap->is_concurrent_traversal_in_progress()) {
 329       storeval_mode = WRITE_BARRIER;
 330     } else if (heap->is_concurrent_mark_in_progress() || heap->is_update_refs_in_progress()) {
 331       storeval_mode = READ_BARRIER;
 332     } else {
 333       assert(heap->is_idle() || heap->is_evacuation_in_progress(), "must not have anything in progress");
 334       storeval_mode = NONE; // E.g. during evac or outside cycle
 335     }
 336   } else {
 337     assert(heap->is_stable() || heap->is_concurrent_mark_in_progress(), "must not have anything in progress");
 338     storeval_mode = NONE;
 339   }
 340 
 341   if (!satb && !checkcast && storeval_mode == NONE) {
 342     // Short-circuit to bulk copy.
 343     return Raw::oop_arraycopy(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
 344   }
 345 
 346   src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
 347   dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
 348 
 349   Klass* bound = objArrayOop(dst_obj)->element_klass();
 350   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
 351   return bs->arraycopy_loop_1(src_raw, dst_raw, length, bound, checkcast, satb, disjoint, storeval_mode);
 352 }
 353 
 354 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP