1 /*
  2  * Copyright (c) 2017, 2023, 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/z/zBarrierSet.hpp"
 28 
 29 #include "gc/shared/accessBarrierSupport.inline.hpp"
 30 #include "gc/z/zAddress.inline.hpp"
 31 #include "gc/z/zBarrier.inline.hpp"
 32 #include "gc/z/zIterator.inline.hpp"
 33 #include "gc/z/zNMethod.hpp"
 34 #include "memory/iterator.inline.hpp"
 35 #include "utilities/debug.hpp"
 36 
 37 template <DecoratorSet decorators, typename BarrierSetT>
 38 template <DecoratorSet expected>
 39 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::verify_decorators_present() {
 40   if ((decorators & expected) == 0) {
 41     fatal("Using unsupported access decorators");
 42   }
 43 }
 44 
 45 template <DecoratorSet decorators, typename BarrierSetT>
 46 template <DecoratorSet expected>
 47 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::verify_decorators_absent() {
 48   if ((decorators & expected) != 0) {
 49     fatal("Using unsupported access decorators");
 50   }
 51 }
 52 
 53 template <DecoratorSet decorators, typename BarrierSetT>
 54 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::unsupported() {
 55   ShouldNotReachHere();
 56 }
 57 
 58 template <DecoratorSet decorators, typename BarrierSetT>
 59 inline zpointer* ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::field_addr(oop base, ptrdiff_t offset) {
 60   assert(base != nullptr, "Invalid base");
 61   return reinterpret_cast<zpointer*>(reinterpret_cast<intptr_t>((void*)base) + offset);
 62 }
 63 
 64 template <DecoratorSet decorators, typename BarrierSetT>
 65 inline zaddress ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::load_barrier(zpointer* p, zpointer o) {
 66   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
 67 
 68   if (HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
 69     if (HasDecorator<decorators, ON_STRONG_OOP_REF>::value) {
 70       // Load barriers on strong oop refs don't keep objects alive
 71       return ZBarrier::load_barrier_on_oop_field_preloaded(p, o);
 72     } else if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value) {
 73       return ZBarrier::no_keep_alive_load_barrier_on_weak_oop_field_preloaded(p, o);
 74     } else {
 75       assert((HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value), "Must be");
 76       return ZBarrier::no_keep_alive_load_barrier_on_phantom_oop_field_preloaded(p, o);
 77     }
 78   } else {
 79     if (HasDecorator<decorators, ON_STRONG_OOP_REF>::value) {
 80       return ZBarrier::load_barrier_on_oop_field_preloaded(p, o);
 81     } else if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value) {
 82       return ZBarrier::load_barrier_on_weak_oop_field_preloaded(p, o);
 83     } else {
 84       assert((HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value), "Must be");
 85       return ZBarrier::load_barrier_on_phantom_oop_field_preloaded(p, o);
 86     }
 87   }
 88 }
 89 
 90 template <DecoratorSet decorators, typename BarrierSetT>
 91 inline zaddress ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::load_barrier_on_unknown_oop_ref(oop base, ptrdiff_t offset, zpointer* p, zpointer o) {
 92   verify_decorators_present<ON_UNKNOWN_OOP_REF>();
 93 
 94   const DecoratorSet decorators_known_strength =
 95     AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
 96 
 97   if (HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
 98     if (decorators_known_strength & ON_STRONG_OOP_REF) {
 99       // Load barriers on strong oop refs don't keep objects alive
100       return ZBarrier::load_barrier_on_oop_field_preloaded(p, o);
101     } else if (decorators_known_strength & ON_WEAK_OOP_REF) {
102       return ZBarrier::no_keep_alive_load_barrier_on_weak_oop_field_preloaded(p, o);
103     } else {
104       assert(decorators_known_strength & ON_PHANTOM_OOP_REF, "Must be");
105       return ZBarrier::no_keep_alive_load_barrier_on_phantom_oop_field_preloaded(p, o);
106     }
107   } else {
108     if (decorators_known_strength & ON_STRONG_OOP_REF) {
109       return ZBarrier::load_barrier_on_oop_field_preloaded(p, o);
110     } else if (decorators_known_strength & ON_WEAK_OOP_REF) {
111       return ZBarrier::load_barrier_on_weak_oop_field_preloaded(p, o);
112     } else {
113       assert(decorators_known_strength & ON_PHANTOM_OOP_REF, "Must be");
114       return ZBarrier::load_barrier_on_phantom_oop_field_preloaded(p, o);
115     }
116   }
117 }
118 
119 inline zpointer ZBarrierSet::store_good(oop obj) {
120   assert(ZPointerStoreGoodMask != 0, "sanity");
121 
122   const zaddress addr = to_zaddress(obj);
123   return ZAddress::store_good(addr);
124 }
125 
126 template <DecoratorSet decorators, typename BarrierSetT>
127 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::store_barrier_heap_with_healing(zpointer* p) {
128   if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
129     ZBarrier::store_barrier_on_heap_oop_field(p, true /* heal */);
130   } else {
131     assert(false, "Should not be used on uninitialized memory");
132   }
133 }
134 
135 template <DecoratorSet decorators, typename BarrierSetT>
136 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::store_barrier_heap_without_healing(zpointer* p) {
137   if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
138     ZBarrier::store_barrier_on_heap_oop_field(p, false /* heal */);
139   }
140 }
141 
142 template <DecoratorSet decorators, typename BarrierSetT>
143 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::no_keep_alive_store_barrier_heap(zpointer* p) {
144   if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
145     ZBarrier::no_keep_alive_store_barrier_on_heap_oop_field(p);
146   }
147 }
148 
149 template <DecoratorSet decorators, typename BarrierSetT>
150 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::store_barrier_native_with_healing(zpointer* p) {
151   if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
152     ZBarrier::store_barrier_on_native_oop_field(p, true /* heal */);
153   } else {
154     assert(false, "Should not be used on uninitialized memory");
155   }
156 }
157 
158 template <DecoratorSet decorators, typename BarrierSetT>
159 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::store_barrier_native_without_healing(zpointer* p) {
160   if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
161     ZBarrier::store_barrier_on_native_oop_field(p, false /* heal */);
162   }
163 }
164 
165 //
166 // In heap
167 //
168 template <DecoratorSet decorators, typename BarrierSetT>
169 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(zpointer* p) {
170   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
171 
172   const zpointer o = Raw::load_in_heap(p);
173   assert_is_valid(o);
174 
175   return to_oop(load_barrier(p, o));
176 }
177 
178 template <DecoratorSet decorators, typename BarrierSetT>
179 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
180   zpointer* const p = field_addr(base, offset);
181 
182   const zpointer o = Raw::load_in_heap(p);
183   assert_is_valid(o);
184 
185   if (HasDecorator<decorators, ON_UNKNOWN_OOP_REF>::value) {
186     return to_oop(load_barrier_on_unknown_oop_ref(base, offset, p, o));
187   }
188 
189   return to_oop(load_barrier(p, o));
190 }
191 
192 template <DecoratorSet decorators>
193 bool is_store_barrier_no_keep_alive() {
194   if (HasDecorator<decorators, ON_STRONG_OOP_REF>::value) {
195     return HasDecorator<decorators, AS_NO_KEEPALIVE>::value;
196   }
197 
198   if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value) {
199     return true;
200   }
201 
202   assert((decorators & ON_PHANTOM_OOP_REF) != 0, "Must be");
203   return true;
204 }
205 
206 template <DecoratorSet decorators>
207 inline bool is_store_barrier_no_keep_alive(oop base, ptrdiff_t offset) {
208   if (!HasDecorator<decorators, ON_UNKNOWN_OOP_REF>::value) {
209     return is_store_barrier_no_keep_alive<decorators>();
210   }
211 
212   const DecoratorSet decorators_known_strength =
213       AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
214 
215   if ((decorators_known_strength & ON_STRONG_OOP_REF) != 0) {
216     return (decorators & AS_NO_KEEPALIVE) != 0;
217   }
218 
219   if ((decorators_known_strength & ON_WEAK_OOP_REF) != 0) {
220     return true;
221   }
222 
223   assert((decorators_known_strength & ON_PHANTOM_OOP_REF) != 0, "Must be");
224   return true;
225 }
226 
227 template <DecoratorSet decorators, typename BarrierSetT>
228 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(zpointer* p, oop value) {
229   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
230 
231   if (is_store_barrier_no_keep_alive<decorators>()) {
232     no_keep_alive_store_barrier_heap(p);
233   } else {
234     store_barrier_heap_without_healing(p);
235   }
236 
237   Raw::store_in_heap(p, store_good(value));
238 }
239 
240 template <DecoratorSet decorators, typename BarrierSetT>
241 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
242   zpointer* const p = field_addr(base, offset);
243 
244   if (is_store_barrier_no_keep_alive<decorators>(base, offset)) {
245     no_keep_alive_store_barrier_heap(p);
246   } else {
247     store_barrier_heap_without_healing(p);
248   }
249 
250   Raw::store_in_heap(p, store_good(value));
251 }
252 
253 template <DecoratorSet decorators, typename BarrierSetT>
254 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(zpointer* p, oop value) {
255   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
256 
257   if (!is_store_barrier_no_keep_alive<decorators>()) {
258     store_barrier_native_without_healing(p);
259   }
260 
261   Raw::store(p, store_good(value));
262 }
263 
264 template <DecoratorSet decorators, typename BarrierSetT>
265 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(zpointer* p, oop compare_value, oop new_value) {
266   verify_decorators_present<ON_STRONG_OOP_REF>();
267   verify_decorators_absent<AS_NO_KEEPALIVE>();
268 
269   store_barrier_heap_with_healing(p);
270 
271   const zpointer o = Raw::atomic_cmpxchg_in_heap(p, store_good(compare_value), store_good(new_value));
272   assert_is_valid(o);
273 
274   return to_oop(ZPointer::uncolor_store_good(o));
275 }
276 
277 template <DecoratorSet decorators, typename BarrierSetT>
278 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
279   verify_decorators_present<ON_STRONG_OOP_REF | ON_UNKNOWN_OOP_REF>();
280   verify_decorators_absent<AS_NO_KEEPALIVE>();
281 
282   // Through Unsafe.CompareAndExchangeObject()/CompareAndSetObject() we can receive
283   // calls with ON_UNKNOWN_OOP_REF set. However, we treat these as ON_STRONG_OOP_REF,
284   // with the motivation that if you're doing Unsafe operations on a Reference.referent
285   // field, then you're on your own anyway.
286   zpointer* const p = field_addr(base, offset);
287 
288   store_barrier_heap_with_healing(p);
289 
290   const zpointer o = Raw::atomic_cmpxchg_in_heap(p, store_good(compare_value), store_good(new_value));
291   assert_is_valid(o);
292 
293   return to_oop(ZPointer::uncolor_store_good(o));
294 }
295 
296 template <DecoratorSet decorators, typename BarrierSetT>
297 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(zpointer* p, oop new_value) {
298   verify_decorators_present<ON_STRONG_OOP_REF>();
299   verify_decorators_absent<AS_NO_KEEPALIVE>();
300 
301   store_barrier_heap_with_healing(p);
302 
303   const zpointer o = Raw::atomic_xchg_in_heap(p, store_good(new_value));
304   assert_is_valid(o);
305 
306   return to_oop(ZPointer::uncolor_store_good(o));
307 }
308 
309 template <DecoratorSet decorators, typename BarrierSetT>
310 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
311   verify_decorators_present<ON_STRONG_OOP_REF>();
312   verify_decorators_absent<AS_NO_KEEPALIVE>();
313 
314   zpointer* const p = field_addr(base, offset);
315 
316   store_barrier_heap_with_healing(p);
317 
318   const zpointer o = Raw::atomic_xchg_in_heap(p, store_good(new_value));
319   assert_is_valid(o);
320 
321   return to_oop(ZPointer::uncolor_store_good(o));
322 }
323 
324 template <DecoratorSet decorators, typename BarrierSetT>
325 inline zaddress ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_copy_one_barriers(zpointer* dst, zpointer* src) {
326   store_barrier_heap_without_healing(dst);
327 
328   return ZBarrier::load_barrier_on_oop_field(src);
329 }
330 
331 template <DecoratorSet decorators, typename BarrierSetT>
332 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_copy_one(zpointer* dst, zpointer* src) {
333   const zaddress obj = oop_copy_one_barriers(dst, src);
334 
335   Atomic::store(dst, ZAddress::store_good(obj));
336 }
337 
338 template <DecoratorSet decorators, typename BarrierSetT>
339 inline bool ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_copy_one_check_cast(zpointer* dst, zpointer* src, Klass* dst_klass) {
340   const zaddress obj = oop_copy_one_barriers(dst, src);
341 
342   if (!oopDesc::is_instanceof_or_null(to_oop(obj), dst_klass)) {
343     // Check cast failed
344     return false;
345   }
346 
347   Atomic::store(dst, ZAddress::store_good(obj));
348 
349   return true;
350 }
351 
352 
353 template <DecoratorSet decorators, typename BarrierSetT>
354 inline bool ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap_check_cast(zpointer* dst, zpointer* src, size_t length, Klass* dst_klass) {
355   // Check cast and copy each elements
356   for (const zpointer* const end = src + length; src < end; src++, dst++) {
357     if (!oop_copy_one_check_cast(dst, src, dst_klass)) {
358       // Check cast failed
359       return false;
360     }
361   }
362 
363   return true;
364 }
365 
366 template <DecoratorSet decorators, typename BarrierSetT>
367 inline bool ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap_no_check_cast(zpointer* dst, zpointer* src, size_t length) {
368   const bool is_disjoint = HasDecorator<decorators, ARRAYCOPY_DISJOINT>::value;
369 
370   if (is_disjoint || src > dst) {
371     for (const zpointer* const end = src + length; src < end; src++, dst++) {
372       oop_copy_one(dst, src);
373     }
374     return true;
375   }
376 
377   if (src < dst) {
378     const zpointer* const end = src;
379     src += length - 1;
380     dst += length - 1;
381     for ( ; src >= end; src--, dst--) {
382       oop_copy_one(dst, src);
383     }
384     return true;
385   }
386 
387   // src and dst are the same; nothing to do
388   return true;
389 }
390 
391 template <DecoratorSet decorators, typename BarrierSetT>
392 inline bool ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, zpointer* src_raw,
393                                                                                        arrayOop dst_obj, size_t dst_offset_in_bytes, zpointer* dst_raw,
394                                                                                        size_t length) {
395   zpointer* const src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
396   zpointer* const dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
397 
398   if (HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value) {
399     Klass* const dst_klass = objArrayOop(dst_obj)->element_klass();
400     return oop_arraycopy_in_heap_check_cast(dst, src, length, dst_klass);
401   }
402 
403   return oop_arraycopy_in_heap_no_check_cast(dst, src, length);
404 }
405 
406 class ZStoreBarrierOopClosure : public BasicOopIterateClosure {
407 public:
408   virtual void do_oop(oop* p_) {
409     volatile zpointer* const p = (volatile zpointer*)p_;
410     const zpointer ptr = ZBarrier::load_atomic(p);
411     const zaddress addr = ZPointer::uncolor(ptr);
412     ZBarrier::store_barrier_on_heap_oop_field(p, false /* heal */);
413     *p = ZAddress::store_good(addr);
414   }
415 
416   virtual void do_oop(narrowOop* p) {
417     ShouldNotReachHere();
418   }
419 };
420 
421 class ZLoadBarrierOopClosure : public BasicOopIterateClosure {
422 public:
423   virtual void do_oop(oop* p) {
424     ZBarrier::load_barrier_on_oop_field((zpointer*)p);
425   }
426 
427   virtual void do_oop(narrowOop* p) {
428     ShouldNotReachHere();
429   }
430 };
431 
432 template <DecoratorSet decorators, typename BarrierSetT>
433 inline void ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
434   assert_is_valid(to_zaddress(src));
435 
436   // Fix the oops
437   ZLoadBarrierOopClosure cl;
438   ZIterator::oop_iterate(src, &cl);
439 
440   // Clone the object
441   Raw::clone_in_heap(src, dst, size);
442 
443   assert(ZHeap::heap()->is_young(to_zaddress(dst)), "ZColorStoreGoodOopClosure is only valid for young objects");
444 
445   // Color store good before handing out
446   ZStoreBarrierOopClosure cl_sg;
447   ZIterator::oop_iterate(dst, &cl_sg);
448 }
449 
450 //
451 // Not in heap
452 //
453 template <DecoratorSet decorators, typename BarrierSetT>
454 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(zpointer* p) {
455   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
456 
457   const zpointer o = Raw::template load<zpointer>(p);
458   assert_is_valid(o);
459   return to_oop(load_barrier(p, o));
460 }
461 
462 template <DecoratorSet decorators, typename BarrierSetT>
463 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(oop* p) {
464   verify_decorators_absent<ON_UNKNOWN_OOP_REF>();
465 
466   if (HasDecorator<decorators, IN_NMETHOD>::value) {
467     return ZNMethod::load_oop(p, decorators);
468   } else {
469     return oop_load_not_in_heap((zpointer*)p);
470   }
471 }
472 
473 template <DecoratorSet decorators, typename BarrierSetT>
474 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(zpointer* p, oop compare_value, oop new_value) {
475   verify_decorators_present<ON_STRONG_OOP_REF>();
476   verify_decorators_absent<AS_NO_KEEPALIVE>();
477 
478   store_barrier_native_with_healing(p);
479 
480   const zpointer o = Raw::atomic_cmpxchg(p, store_good(compare_value), store_good(new_value));
481   assert_is_valid(o);
482 
483   return to_oop(ZPointer::uncolor_store_good(o));
484 }
485 
486 template <DecoratorSet decorators, typename BarrierSetT>
487 inline oop ZBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(zpointer* p, oop new_value) {
488   verify_decorators_present<ON_STRONG_OOP_REF>();
489   verify_decorators_absent<AS_NO_KEEPALIVE>();
490 
491   store_barrier_native_with_healing(p);
492 
493   const zpointer o = Raw::atomic_xchg(p, store_good(new_value));
494   assert_is_valid(o);
495 
496   return to_oop(ZPointer::uncolor_store_good(o));
497 }
498 
499 #endif // SHARE_GC_Z_ZBARRIERSET_INLINE_HPP