1 /*
   2  * Copyright (c) 2018, 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 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.inline.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/cardTable.hpp"
  29 #include "gc/shared/cardTableBarrierSet.hpp"
  30 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
  31 #include "interpreter/interp_masm.hpp"
  32 
  33 #define __ masm->
  34 
  35 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj, Address dst) {
  36 
  37   BarrierSet* bs = BarrierSet::barrier_set();
  38   assert(bs->kind() == BarrierSet::CardTableBarrierSet, "Wrong barrier set kind");
  39 
  40   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
  41   CardTable* ct = ctbs->card_table();
  42 
  43   __ lsr(obj, obj, CardTable::card_shift);
  44 
  45   assert(CardTable::dirty_card_val() == 0, "must be");
  46 
  47   __ load_byte_map_base(rscratch1);
  48 
  49   if (UseCondCardMark) {
  50     Label L_already_dirty;
  51     __ membar(Assembler::StoreLoad);
  52     __ ldrb(rscratch2,  Address(obj, rscratch1));
  53     __ cbz(rscratch2, L_already_dirty);
  54     __ strb(zr, Address(obj, rscratch1));
  55     __ bind(L_already_dirty);
  56   } else {
  57     if (ct->scanned_concurrently()) {
  58       __ membar(Assembler::StoreStore);
  59     }
  60     __ strb(zr, Address(obj, rscratch1));
  61   }
  62 }
  63 
  64 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
  65                                                                     Register start, Register count, Register scratch, RegSet saved_regs) {
  66   BarrierSet* bs = BarrierSet::barrier_set();
  67   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
  68   CardTable* ct = ctbs->card_table();
  69 
  70   Label L_loop, L_done;
  71   const Register end = count;
  72 
  73   __ cbz(count, L_done); // zero count - nothing to do
  74 
  75   __ lea(end, Address(start, count, Address::lsl(LogBytesPerHeapOop))); // end = start + count << LogBytesPerHeapOop
  76   __ sub(end, end, BytesPerHeapOop); // last element address to make inclusive
  77   __ lsr(start, start, CardTable::card_shift);
  78   __ lsr(end, end, CardTable::card_shift);
  79   __ sub(count, end, start); // number of bytes to copy
  80 
  81   __ load_byte_map_base(scratch);
  82   __ add(start, start, scratch);
  83   if (ct->scanned_concurrently()) {
  84     __ membar(__ StoreStore);
  85   }
  86   __ bind(L_loop);
  87   __ strb(zr, Address(start, count));
  88   __ subs(count, count, 1);
  89   __ br(Assembler::GE, L_loop);
  90   __ bind(L_done);
  91 }
  92 
  93 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  94                                                 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
  95   bool in_heap = (decorators & IN_HEAP) != 0;
  96   bool is_array = (decorators & IS_ARRAY) != 0;
  97   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
  98   bool precise = is_array || on_anonymous;
  99 
 100   bool needs_post_barrier = val != noreg && in_heap;
 101   BarrierSetAssembler::store_at(masm, decorators, type, dst, val, noreg, noreg, noreg);
 102   if (needs_post_barrier) {
 103     // flatten object address if needed
 104     if (!precise || (dst.index() == noreg && dst.offset() == 0)) {
 105       if (tmp3 != noreg) {
 106         // Called by MacroAssembler::pack_value_helper. We cannot corrupt the dst.base() register
 107         __ mov(tmp3, dst.base());
 108         store_check(masm, tmp3, dst);
 109       } else {
 110         // It's OK to corrupt the dst.base() register.
 111         store_check(masm, dst.base(), dst);
 112       }
 113 
 114     } else {
 115       __ lea(r3, dst);
 116       store_check(masm, r3, dst);
 117     }
 118   }
 119 }