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