1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. 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 #ifndef SHARE_GC_Z_ZBARRIERSET_INLINE_HPP
  25 #define SHARE_GC_Z_ZBARRIERSET_INLINE_HPP
  26 
  27 #include "gc/shared/accessBarrierSupport.inline.hpp"
  28 #include "gc/z/zBarrier.inline.hpp"
  29 #include "gc/z/zBarrierSet.hpp"
  30 #include "utilities/debug.hpp"
  31 
  32 template <DecoratorSet decorators, typename BarrierSetT>
  33 template <DecoratorSet expected>
  34 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::verify_decorators_present() {
  35   if ((decorators & expected) == 0) {
  36     fatal("Using unsupported access decorators");
  37   }
  38 }
  39 
  40 template <DecoratorSet decorators, typename BarrierSetT>
  41 template <DecoratorSet expected>
  42 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::verify_decorators_absent() {
  43   if ((decorators & expected) != 0) {
  44     fatal("Using unsupported access decorators");
  45   }
  46 }
  47 
  48 template <DecoratorSet decorators, typename BarrierSetT>
  49 inline oop* ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::field_addr(oop base, ptrdiff_t offset) {
  50   assert(base != NULL, "Invalid base");
  51   return reinterpret_cast<oop*>(reinterpret_cast<intptr_t>((void*)base) + offset);
  52 }
  53 
  54 template <DecoratorSet decorators, typename BarrierSetT>
  55 template <typename T>
  56 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::load_barrier_on_oop_field_preloaded(T* addr, oop o) {
  57   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
  58 
  59   if (HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  60     if (HasDecorator<decorators, ON_STRONG_OOP_REF>::value) {
  61       return ZBarrier::weak_load_barrier_on_oop_field_preloaded(addr, o);
  62     } else if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value) {
  63       return ZBarrier::weak_load_barrier_on_weak_oop_field_preloaded(addr, o);
  64     } else {
  65       assert((HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value), "Must be");
  66       return ZBarrier::weak_load_barrier_on_phantom_oop_field_preloaded(addr, o);
  67     }
  68   } else {
  69     if (HasDecorator<decorators, ON_STRONG_OOP_REF>::value) {
  70       return ZBarrier::load_barrier_on_oop_field_preloaded(addr, o);
  71     } else if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value) {
  72       return ZBarrier::load_barrier_on_weak_oop_field_preloaded(addr, o);
  73     } else {
  74       assert((HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value), "Must be");
  75       return ZBarrier::load_barrier_on_phantom_oop_field_preloaded(addr, o);
  76     }
  77   }
  78 }
  79 
  80 template <DecoratorSet decorators, typename BarrierSetT>
  81 template <typename T>
  82 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::load_barrier_on_unknown_oop_field_preloaded(oop base, ptrdiff_t offset, T* addr, oop o) {
  83   verify_decorators_present<ON_UNKNOWN_OOP_REF>();
  84 
  85   const DecoratorSet decorators_known_strength =
  86     AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
  87 
  88   if (HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  89     if (decorators_known_strength & ON_STRONG_OOP_REF) {
  90       return ZBarrier::weak_load_barrier_on_oop_field_preloaded(addr, o);
  91     } else if (decorators_known_strength & ON_WEAK_OOP_REF) {
  92       return ZBarrier::weak_load_barrier_on_weak_oop_field_preloaded(addr, o);
  93     } else {
  94       assert(decorators_known_strength & ON_PHANTOM_OOP_REF, "Must be");
  95       return ZBarrier::weak_load_barrier_on_phantom_oop_field_preloaded(addr, o);
  96     }
  97   } else {
  98     if (decorators_known_strength & ON_STRONG_OOP_REF) {
  99       return ZBarrier::load_barrier_on_oop_field_preloaded(addr, o);
 100     } else if (decorators_known_strength & ON_WEAK_OOP_REF) {
 101       return ZBarrier::load_barrier_on_weak_oop_field_preloaded(addr, o);
 102     } else {
 103       assert(decorators_known_strength & ON_PHANTOM_OOP_REF, "Must be");
 104       return ZBarrier::load_barrier_on_phantom_oop_field_preloaded(addr, o);
 105     }
 106   }
 107 }
 108 
 109 //
 110 // In heap
 111 //
 112 template <DecoratorSet decorators, typename BarrierSetT>
 113 template <typename T>
 114 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
 115   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
 116 
 117   const oop o = Raw::oop_load_in_heap(addr);
 118   return load_barrier_on_oop_field_preloaded(addr, o);
 119 }
 120 
 121 template <DecoratorSet decorators, typename BarrierSetT>
 122 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 123   oop* const addr = field_addr(base, offset);
 124   const oop o = Raw::oop_load_in_heap(addr);
 125 
 126   if (HasDecorator<decorators, ON_UNKNOWN_OOP_REF>::value) {
 127     return load_barrier_on_unknown_oop_field_preloaded(base, offset, addr, o);
 128   }
 129 
 130   return load_barrier_on_oop_field_preloaded(addr, o);
 131 }
 132 
 133 template <DecoratorSet decorators, typename BarrierSetT>
 134 template <typename T>
 135 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
 136   verify_decorators_present<ON_STRONG_OOP_REF>();
 137   verify_decorators_absent<AS_NO_KEEPALIVE>();
 138 
 139   ZBarrier::load_barrier_on_oop_field(addr);
 140   return Raw::oop_atomic_cmpxchg_in_heap(new_value, addr, compare_value);
 141 }
 142 
 143 template <DecoratorSet decorators, typename BarrierSetT>
 144 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 145   verify_decorators_present<ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF>();
 146   verify_decorators_absent<AS_NO_KEEPALIVE>();
 147 
 148   // Through Unsafe.CompareAndExchangeObject()/CompareAndSetObject() we can receive
 149   // calls with ON_UNKNOWN_OOP_REF set. However, we treat these as ON_STRONG_OOP_REF,
 150   // with the motivation that if you're doing Unsafe operations on a Reference.referent
 151   // field, then you're on your own anyway.
 152   ZBarrier::load_barrier_on_oop_field(field_addr(base, offset));
 153   return Raw::oop_atomic_cmpxchg_in_heap_at(new_value, base, offset, compare_value);
 154 }
 155 
 156 template <DecoratorSet decorators, typename BarrierSetT>
 157 template <typename T>
 158 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(oop new_value, T* addr) {
 159   verify_decorators_present<ON_STRONG_OOP_REF>();
 160   verify_decorators_absent<AS_NO_KEEPALIVE>();
 161 
 162   const oop o = Raw::oop_atomic_xchg_in_heap(new_value, addr);
 163   return ZBarrier::load_barrier_on_oop(o);
 164 }
 165 
 166 template <DecoratorSet decorators, typename BarrierSetT>
 167 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
 168   verify_decorators_present<ON_STRONG_OOP_REF>();
 169   verify_decorators_absent<AS_NO_KEEPALIVE>();
 170 
 171   const oop o = Raw::oop_atomic_xchg_in_heap_at(new_value, base, offset);
 172   return ZBarrier::load_barrier_on_oop(o);
 173 }
 174 
 175 template <DecoratorSet decorators, typename BarrierSetT>
 176 template <typename T>
 177 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 178                                                                                        arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 179                                                                                        size_t length) {
 180   T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
 181   T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
 182 
 183   if ((!HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value) &&
 184       (!HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value)) {
 185     // No check cast, bulk barrier and bulk copy
 186     ZBarrier::load_barrier_on_oop_array(src, length);
 187     Raw::oop_arraycopy_in_heap(NULL, 0, src, NULL, 0, dst, length);
 188     return;
 189   }
 190 
 191   // Check cast and copy each elements
 192   Klass* const dst_klass = objArrayOop(dst_obj)->element_klass();
 193   for (const T* const end = src + length; src < end; src++, dst++) {
 194     const oop elem = ZBarrier::load_barrier_on_oop_field(src);
 195     if (HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value && elem == NULL) {
 196       throw_array_null_pointer_store_exception(src_obj, dst_obj, Thread::current());
 197       return;
 198     }
 199     if (HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value &&
 200         (!oopDesc::is_instanceof_or_null(elem, dst_klass))) {
 201       // Check cast failed
 202       throw_array_store_exception(src_obj, dst_obj, Thread::current());
 203       return;
 204     }
 205 
 206     // Cast is safe, since we know it's never a narrowOop
 207     *(oop*)dst = elem;
 208   }
 209 }
 210 
 211 template <DecoratorSet decorators, typename BarrierSetT>
 212 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
 213   ZBarrier::load_barrier_on_oop_fields(src);
 214   Raw::clone_in_heap(src, dst, size);
 215 }
 216 
 217 template <DecoratorSet decorators, typename BarrierSetT>
 218 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::value_copy_in_heap(void* src, void* dst, ValueKlass* md) {
 219   if (md->contains_oops()) {
 220     // src/dst aren't oops, need offset to adjust oop map offset
 221     const address src_oop_addr_offset = ((address) src) - md->first_field_offset();
 222 
 223     OopMapBlock* map = md->start_of_nonstatic_oop_maps();
 224     OopMapBlock* const end = map + md->nonstatic_oop_map_count();
 225     while (map != end) {
 226       address soop_address = src_oop_addr_offset + map->offset();
 227       ZBarrier::load_barrier_on_oop_array((oop*) soop_address, map->count());
 228       map++;
 229     }
 230   }
 231   Raw::value_copy_in_heap(src, dst, md);
 232 }
 233 
 234 //
 235 // Not in heap
 236 //
 237 template <DecoratorSet decorators, typename BarrierSetT>
 238 template <typename T>
 239 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
 240   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
 241 
 242   const oop o = Raw::oop_load_not_in_heap(addr);
 243   return load_barrier_on_oop_field_preloaded(addr, o);
 244 }
 245 
 246 template <DecoratorSet decorators, typename BarrierSetT>
 247 template <typename T>
 248 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value) {
 249   verify_decorators_present<ON_STRONG_OOP_REF>();
 250   verify_decorators_absent<AS_NO_KEEPALIVE>();
 251 
 252   return Raw::oop_atomic_cmpxchg_not_in_heap(new_value, addr, compare_value);
 253 }
 254 
 255 template <DecoratorSet decorators, typename BarrierSetT>
 256 template <typename T>
 257 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(oop new_value, T* addr) {
 258   verify_decorators_present<ON_STRONG_OOP_REF>();
 259   verify_decorators_absent<AS_NO_KEEPALIVE>();
 260 
 261   return Raw::oop_atomic_xchg_not_in_heap(new_value, addr);
 262 }
 263 
 264 #endif // SHARE_GC_Z_ZBARRIERSET_INLINE_HPP