1 /*
  2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "asm/macroAssembler.inline.hpp"
 28 #include "gc/shared/barrierSet.hpp"
 29 #include "gc/shared/cardTable.hpp"
 30 #include "gc/shared/cardTableBarrierSet.hpp"
 31 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
 32 #include "gc/shared/gc_globals.hpp"
 33 #include "interpreter/interp_masm.hpp"
 34 
 35 #define __ masm->
 36 
 37 
 38 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj, Register tmp) {
 39   assert_cond(masm != NULL);
 40   assert_different_registers(obj, tmp);
 41   BarrierSet* bs = BarrierSet::barrier_set();
 42   assert(bs->kind() == BarrierSet::CardTableBarrierSet, "Wrong barrier set kind");
 43 
 44   __ srli(obj, obj, CardTable::card_shift);
 45 
 46   assert(CardTable::dirty_card_val() == 0, "must be");
 47 
 48   __ load_byte_map_base(tmp);
 49   __ add(tmp, obj, tmp);
 50 
 51   if (UseCondCardMark) {
 52     Label L_already_dirty;
 53     __ membar(MacroAssembler::StoreLoad);
 54     __ lbu(t1,  Address(tmp));
 55     __ beqz(t1, L_already_dirty);
 56     __ sb(zr, Address(tmp));
 57     __ bind(L_already_dirty);
 58   } else {
 59     __ sb(zr, Address(tmp));
 60   }
 61 }
 62 
 63 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 64                                                                     Register start, Register count, Register tmp, RegSet saved_regs) {
 65   assert_cond(masm != NULL);
 66   assert_different_registers(start, tmp);
 67   assert_different_registers(count, tmp);
 68 
 69   Label L_loop, L_done;
 70   const Register end = count;
 71 
 72   __ beqz(count, L_done); // zero count - nothing to do
 73   __ slli(count, count, LogBytesPerHeapOop);
 74   __ add(end, start, count); // end = start + count << LogBytesPerHeapOop
 75   __ sub(end, end, BytesPerHeapOop); // last element address to make inclusive
 76 
 77   __ srli(start, start, CardTable::card_shift);
 78   __ srli(end, end, CardTable::card_shift);
 79   __ sub(count, end, start); // number of bytes to copy
 80 
 81   __ load_byte_map_base(tmp);
 82   __ add(start, start, tmp);
 83 
 84   __ bind(L_loop);
 85   __ add(tmp, start, count);
 86   __ sb(zr, Address(tmp));
 87   __ sub(count, count, 1);
 88   __ bgez(count, L_loop);
 89   __ bind(L_done);
 90 }
 91 
 92 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 93                                                 Address dst, Register val, Register tmp1, Register tmp2) {
 94   bool in_heap = (decorators & IN_HEAP) != 0;
 95   bool is_array = (decorators & IS_ARRAY) != 0;
 96   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 97   bool precise = is_array || on_anonymous;
 98 
 99   bool needs_post_barrier = val != noreg && in_heap;
100   BarrierSetAssembler::store_at(masm, decorators, type, dst, val, noreg, noreg);
101   if (needs_post_barrier) {
102     // flatten object address if needed
103     if (!precise || dst.offset() == 0) {
104       store_check(masm, dst.base(), x13);
105     } else {
106       assert_cond(masm != NULL);
107       __ la(x13, dst);
108       store_check(masm, x13, t0);
109     }
110   }
111 }