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