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