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