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 
 25 #ifndef SHARE_GC_SHARED_MODREFBARRIERSET_INLINE_HPP
 26 #define SHARE_GC_SHARED_MODREFBARRIERSET_INLINE_HPP
 27 
 28 #include "gc/shared/modRefBarrierSet.hpp"
 29 
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "oops/compressedOops.inline.hpp"
 32 #include "oops/objArrayOop.hpp"
 33 #include "oops/oop.hpp"
 34 #include "oops/inlineKlass.inline.hpp"
 35 #include "runtime/thread.hpp"
 36 
 37 class Klass;
 38 
 39 // count is number of array elements being written
 40 void ModRefBarrierSet::write_ref_array(HeapWord* start, size_t count) {
 41   HeapWord* end = (HeapWord*)((char*)start + (count*heapOopSize));
 42   // In the case of compressed oops, start and end may potentially be misaligned;
 43   // so we need to conservatively align the first downward (this is not
 44   // strictly necessary for current uses, but a case of good hygiene and,
 45   // if you will, aesthetics) and the second upward (this is essential for
 46   // current uses) to a HeapWord boundary, so we mark all cards overlapping
 47   // this write. If this evolves in the future to calling a
 48   // logging barrier of narrow oop granularity, like the pre-barrier for G1
 49   // (mentioned here merely by way of example), we will need to change this
 50   // interface, so it is "exactly precise" (if i may be allowed the adverbial
 51   // redundancy for emphasis) and does not include narrow oop slots not
 52   // included in the original write interval.
 53   HeapWord* aligned_start = align_down(start, HeapWordSize);
 54   HeapWord* aligned_end   = align_up  (end,   HeapWordSize);
 55   // If compressed oops were not being used, these should already be aligned
 56   assert(UseCompressedOops || (aligned_start == start && aligned_end == end),
 57          "Expected heap word alignment of start and end");
 58   write_region(MemRegion(aligned_start, aligned_end));
 59 }
 60 
 61 template <DecoratorSet decorators, typename BarrierSetT>
 62 template <typename T>
 63 inline void ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 64 oop_store_in_heap(T* addr, oop value) {
 65   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 66   bs->template write_ref_field_pre<decorators>(addr);
 67   Raw::oop_store(addr, value);
 68   bs->template write_ref_field_post<decorators>(addr);
 69 }
 70 
 71 template <DecoratorSet decorators, typename BarrierSetT>
 72 template <typename T>
 73 inline oop ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 74 oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
 75   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 76   bs->template write_ref_field_pre<decorators>(addr);
 77   oop result = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
 78   if (result == compare_value) {
 79     bs->template write_ref_field_post<decorators>(addr);
 80   }
 81   return result;
 82 }
 83 
 84 template <DecoratorSet decorators, typename BarrierSetT>
 85 template <typename T>
 86 inline oop ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 87 oop_atomic_xchg_in_heap(T* addr, oop new_value) {
 88   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 89   bs->template write_ref_field_pre<decorators>(addr);
 90   oop result = Raw::oop_atomic_xchg(addr, new_value);
 91   bs->template write_ref_field_post<decorators>(addr);
 92   return result;
 93 }
 94 
 95 template <DecoratorSet decorators, typename BarrierSetT>
 96 template <typename T>
 97 inline void ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 98 oop_arraycopy_partial_barrier(BarrierSetT *bs, T* dst_raw, T* p) {
 99   const size_t pd = pointer_delta(p, dst_raw, (size_t)heapOopSize);
100   // pointer delta is scaled to number of elements (length field in
101   // objArrayOop) which we assume is 32 bit.
102   assert(pd == (size_t)(int)pd, "length field overflow");
103   bs->write_ref_array((HeapWord*)dst_raw, pd);
104 }
105 
106 template <DecoratorSet decorators, typename BarrierSetT>
107 template <typename T>
108 inline void ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
109 oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
110                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
111                       size_t length) {
112   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
113 
114   src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
115   dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
116 
117   if ((!HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value) &&
118       (!HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value)) {
119     // Optimized covariant case
120     bs->write_ref_array_pre(dst_raw, length,
121                             HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value);
122     Raw::oop_arraycopy(nullptr, 0, src_raw, nullptr, 0, dst_raw, length);
123     bs->write_ref_array((HeapWord*)dst_raw, length);
124   } else {
125     assert(dst_obj != nullptr, "better have an actual oop");
126     Klass* bound = objArrayOop(dst_obj)->element_klass();
127     T* from = const_cast<T*>(src_raw);
128     T* end = from + length;
129     for (T* p = dst_raw; from < end; from++, p++) {
130       T element = *from;
131       // Apply any required checks
132       if (HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value && CompressedOops::is_null(element)) {
133         oop_arraycopy_partial_barrier(bs, dst_raw, p);
134         throw_array_null_pointer_store_exception(src_obj, dst_obj, JavaThread::current());
135         return;
136       }
137       if (HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value &&
138           (!oopDesc::is_instanceof_or_null(CompressedOops::decode(element), bound))) {
139         oop_arraycopy_partial_barrier(bs, dst_raw, p);
140         throw_array_store_exception(src_obj, dst_obj, JavaThread::current());
141         return;
142       }
143       // write
144       bs->template write_ref_field_pre<decorators>(p);
145       *p = element;
146     }
147     bs->write_ref_array((HeapWord*)dst_raw, length);
148   }
149 }
150 
151 template <DecoratorSet decorators, typename BarrierSetT>
152 inline void ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
153 clone_in_heap(oop src, oop dst, size_t size) {
154   Raw::clone(src, dst, size);
155   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
156   bs->write_region(MemRegion((HeapWord*)(void*)dst, size));
157 }
158 
159 template <DecoratorSet decorators, typename BarrierSetT>
160 inline void ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT>::
161 value_copy_in_heap(void* src, void* dst, InlineKlass* md, LayoutKind lk) {
162   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value || (!md->contains_oops())) {
163     Raw::value_copy(src, dst, md, lk);
164   } else {
165     BarrierSetT* bs = barrier_set_cast<BarrierSetT>(BarrierSet::barrier_set());
166     // src/dst aren't oops, need offset to adjust oop map offset
167     const address dst_oop_addr_offset = ((address) dst) - md->first_field_offset();
168     typedef typename ValueOopType<decorators>::type OopType;
169 
170     // Pre-barriers...
171     OopMapBlock* map = md->start_of_nonstatic_oop_maps();
172     OopMapBlock* const end = map + md->nonstatic_oop_map_count();
173     while (map != end) {
174       address doop_address = dst_oop_addr_offset + map->offset();
175       bs->write_ref_array_pre((OopType*) doop_address, map->count(), false);
176       map++;
177     }
178 
179     Raw::value_copy(src, dst, md, lk);
180 
181     // Post-barriers...
182     map = md->start_of_nonstatic_oop_maps();
183     while (map != end) {
184       address doop_address = dst_oop_addr_offset + map->offset();
185       bs->write_ref_array((HeapWord*) doop_address, map->count());
186       map++;
187     }
188   }
189 }
190 
191 #endif // SHARE_GC_SHARED_MODREFBARRIERSET_INLINE_HPP