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 "code/aotCodeCache.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 "gc/shared/gc_globals.hpp"
32
33 #define __ masm->
34
35 #ifdef PRODUCT
36 #define BLOCK_COMMENT(str) /* nothing */
37 #else
38 #define BLOCK_COMMENT(str) __ block_comment(str)
39 #endif
40
41 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
42
43 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
44
45 void CardTableBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
46 Register src, Register dst, Register count) {
47 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
48 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
49 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
50
51 if (is_reference_type(type)) {
52 if (!checkcast) {
53 if (!obj_int) {
54 // Save count for barrier
55 __ movptr(r11, count);
56 } else if (disjoint) {
57 // Save dst in r11 in the disjoint case
58 __ movq(r11, dst);
59 }
60 }
61 gen_write_ref_array_pre_barrier(masm, decorators, dst, count);
62 }
63 }
64
65 void CardTableBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
66 Register src, Register dst, Register count) {
67 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
68 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
69 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
70 Register tmp = rax;
71
72 if (is_reference_type(type)) {
73 if (!checkcast) {
74 if (!obj_int) {
75 // Save count for barrier
76 count = r11;
77 } else if (disjoint) {
78 // Use the saved dst in the disjoint case
79 dst = r11;
80 }
81 } else {
82 tmp = rscratch1;
83 }
84 gen_write_ref_array_post_barrier(masm, decorators, dst, count, tmp);
85 }
86 }
87
88 void CardTableBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
89 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
90 if (is_reference_type(type)) {
91 oop_store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
92 } else {
93 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
94 }
95 }
96
97 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
98 Register addr, Register count, Register tmp) {
99 CardTableBarrierSet* ctbs = CardTableBarrierSet::barrier_set();
100
101 Label L_loop, L_done;
102 const Register end = count;
103 assert_different_registers(addr, end);
104
105 __ testl(count, count);
106 __ jcc(Assembler::zero, L_done); // zero count - nothing to do
107
108
109 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
110 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
111 __ shrptr(addr, CardTable::card_shift());
112 __ shrptr(end, CardTable::card_shift());
113 __ subptr(end, addr); // end --> cards count
114
115 #if INCLUDE_CDS
116 if (AOTCodeCache::is_on_for_dump()) {
117 __ lea(tmp, ExternalAddress(AOTRuntimeConstants::card_table_base_address()));
118 __ movq(tmp, Address(tmp, 0));
119 } else
120 #endif
121 {
122 __ mov64(tmp, (intptr_t)ctbs->card_table_base_const());
123 }
124 __ addptr(addr, tmp);
125 __ BIND(L_loop);
126 __ movb(Address(addr, count, Address::times_1), 0);
127 __ decrement(count);
128 __ jcc(Assembler::greaterEqual, L_loop);
129
130 __ BIND(L_done);
131 }
132
133 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj, Address dst, Register rscratch) {
134 // Does a store check for the oop in register obj. The content of
135 // register obj is destroyed afterwards.
136 CardTableBarrierSet* ctbs = CardTableBarrierSet::barrier_set();
137
138 __ shrptr(obj, CardTable::card_shift());
139
140 Address card_addr;
141 precond(rscratch != noreg);
142
143 // The calculation for byte_map_base is as follows:
144 // byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift);
145 // So this essentially converts an address to a displacement and it will
146 // never need to be relocated. On 64bit however the value may be too
147 // large for a 32bit displacement.
148 intptr_t byte_map_base = (intptr_t)ctbs->card_table_base_const();
149 #if INCLUDE_CDS
150 if (AOTCodeCache::is_on_for_dump()) {
151 __ lea(rscratch, ExternalAddress(AOTRuntimeConstants::card_table_base_address()));
152 __ movq(rscratch, Address(rscratch, 0));
153 card_addr = Address(rscratch, obj, Address::times_1, 0);
154 } else
155 #endif
156 if (__ is_simm32(byte_map_base)) {
157 card_addr = Address(noreg, obj, Address::times_1, byte_map_base);
158 } else {
159 // By doing it as an ExternalAddress 'byte_map_base' could be converted to a rip-relative
160 // displacement and done in a single instruction given favorable mapping and a
161 // smarter version of as_Address. However, 'ExternalAddress' generates a relocation
162 // entry and that entry is not properly handled by the relocation code.
163 AddressLiteral cardtable((address)byte_map_base, relocInfo::none);
164 Address index(noreg, obj, Address::times_1);
165 card_addr = __ as_Address(ArrayAddress(cardtable, index), rscratch);
166 }
167
168 int dirty = CardTable::dirty_card_val();
169 if (UseCondCardMark) {
170 Label L_already_dirty;
171 __ cmpb(card_addr, dirty);
172 __ jccb(Assembler::equal, L_already_dirty);
173 __ movb(card_addr, dirty);
174 __ bind(L_already_dirty);
175 } else {
176 __ movb(card_addr, dirty);
177 }
178 }
179
180 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
181 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
182 bool in_heap = (decorators & IN_HEAP) != 0;
183
184 bool is_array = (decorators & IS_ARRAY) != 0;
185 bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
186 bool precise = is_array || on_anonymous;
187
188 bool needs_post_barrier = val != noreg && in_heap;
189
190 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, noreg, noreg, noreg);
191 if (needs_post_barrier) {
192 // flatten object address if needed
193 if (!precise || (dst.index() == noreg && dst.disp() == 0)) {
194 store_check(masm, dst.base(), dst, tmp2);
195 } else {
196 __ lea(tmp1, dst);
197 store_check(masm, tmp1, dst, tmp2);
198 }
199 }
200 }