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