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/g1/g1BarrierSet.hpp"
27 #include "gc/g1/g1BarrierSetAssembler.hpp"
28 #include "gc/g1/g1BarrierSetRuntime.hpp"
29 #include "gc/g1/g1CardTable.hpp"
30 #include "gc/g1/g1HeapRegion.hpp"
31 #include "gc/g1/g1ThreadLocalData.hpp"
32 #include "interpreter/interp_masm.hpp"
33 #include "runtime/arguments.hpp"
34 #include "runtime/sharedRuntime.hpp"
35 #include "utilities/debug.hpp"
36 #include "utilities/macros.hpp"
37 #ifdef COMPILER1
38 #include "c1/c1_LIRAssembler.hpp"
39 #include "c1/c1_MacroAssembler.hpp"
40 #include "gc/g1/c1/g1BarrierSetC1.hpp"
41 #endif // COMPILER1
42 #ifdef COMPILER2
43 #include "gc/g1/c2/g1BarrierSetC2.hpp"
44 #endif // COMPILER2
45
46 #define __ masm->
47
48 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
49 Register addr, Register count) {
50 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
51
52 if (!dest_uninitialized) {
53 Register thread = r15_thread;
54
55 Label filtered;
56 Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
57 // Is marking active?
58 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
59 __ cmpl(in_progress, 0);
60 } else {
61 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
62 __ cmpb(in_progress, 0);
63 }
64
65 __ jcc(Assembler::equal, filtered);
66
67 __ push_call_clobbered_registers(false /* save_fpu */);
68 if (count == c_rarg0) {
69 if (addr == c_rarg1) {
70 // exactly backwards!!
71 __ xchgptr(c_rarg1, c_rarg0);
72 } else {
73 __ movptr(c_rarg1, count);
74 __ movptr(c_rarg0, addr);
75 }
76 } else {
77 __ movptr(c_rarg0, addr);
78 __ movptr(c_rarg1, count);
79 }
80 if (UseCompressedOops) {
81 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry), 2);
82 } else {
83 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry), 2);
84 }
85 __ pop_call_clobbered_registers(false /* save_fpu */);
86
87 __ bind(filtered);
88 }
89 }
90
91 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
92 Register addr, Register count, Register tmp) {
93 Label L_done;
94
95 __ testptr(count, count);
96 __ jccb(Assembler::zero, L_done);
97
98 // Calculate end address in "count".
99 Address::ScaleFactor scale = UseCompressedOops ? Address::times_4 : Address::times_8;
100 __ leaq(count, Address(addr, count, scale));
101
102 // Calculate start card address in "addr".
103 __ shrptr(addr, CardTable::card_shift());
104
105 Register thread = r15_thread;
106
107 __ movptr(tmp, Address(thread, in_bytes(G1ThreadLocalData::card_table_base_offset())));
108 __ addptr(addr, tmp);
109
110 // Calculate address of card of last word in the array.
111 __ subptr(count, 1);
112 __ shrptr(count, CardTable::card_shift());
113 __ addptr(count, tmp);
114
115 Label L_loop;
116 // Iterate from start card to end card (inclusive).
117 __ bind(L_loop);
118
119 Label L_is_clean_card;
120 if (UseCondCardMark) {
121 __ cmpb(Address(addr, 0), G1CardTable::clean_card_val());
122 __ jccb(Assembler::equal, L_is_clean_card);
123 } else {
124 __ movb(Address(addr, 0), G1CardTable::dirty_card_val());
125 }
126
127 Label L_next_card;
128 __ bind(L_next_card);
129 __ addptr(addr, sizeof(CardTable::CardValue));
130 __ cmpptr(addr, count);
131 __ jccb(Assembler::belowEqual, L_loop);
132 __ jmpb(L_done);
133
134 __ bind(L_is_clean_card);
135 // Card was clean. Dirty card and go to next.
136 __ movb(Address(addr, 0), G1CardTable::dirty_card_val());
137 __ jmpb(L_next_card);
138
139 __ bind(L_done);
140 }
141
142 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
143 Register dst, Address src, Register tmp1) {
144 bool on_oop = is_reference_type(type);
145 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
146 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
147 bool on_reference = on_weak || on_phantom;
148 CardTableBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
149 if (on_oop && on_reference) {
150 // Generate the G1 pre-barrier code to log the value of
151 // the referent field in an SATB buffer.
152 g1_write_barrier_pre(masm /* masm */,
153 noreg /* obj */,
154 dst /* pre_val */,
155 tmp1 /* tmp */,
156 true /* tosca_live */,
157 true /* expand_call */);
158 }
159 }
160
161 static void generate_pre_barrier_fast_path(MacroAssembler* masm,
162 const Register thread) {
163 Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
164 // Is marking active?
165 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
166 __ cmpl(in_progress, 0);
167 } else {
168 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
169 __ cmpb(in_progress, 0);
170 }
171 }
172
173 static void generate_pre_barrier_slow_path(MacroAssembler* masm,
174 const Register obj,
175 const Register pre_val,
176 const Register thread,
177 const Register tmp,
178 Label& L_done) {
179 Address index_addr(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
180 Address buffer_addr(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
181
182 // This code assumes that buffer index is pointer sized.
183 STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
184
185 Label L_runtime;
186
187 // Do we need to load the previous value?
188 if (obj != noreg) {
189 __ load_heap_oop(pre_val, Address(obj, 0), noreg, AS_RAW);
190 }
191
192 // Is the previous value null?
193 __ testptr(pre_val, pre_val);
194 __ jcc(Assembler::equal, L_done);
195
196 // Can we store a value in the given thread's buffer?
197 // (The index field is typed as size_t.)
198 __ movptr(tmp, index_addr); // temp := *(index address)
199 __ testptr(tmp, tmp); // index == 0?
200 __ jccb(Assembler::zero, L_runtime); // jump to runtime if index == 0 (full buffer)
201
202 // The buffer is not full, store value into it.
203 __ subptr(tmp, wordSize); // temp := next index
204 __ movptr(index_addr, tmp); // *(index address) := next index
205 __ addptr(tmp, buffer_addr); // temp := buffer address + next index
206 __ movptr(Address(tmp, 0), pre_val); // *(buffer address + next index) := value
207
208 // Jump out if done, or fall-through to runtime.
209 // "L_done" is far away, so jump cannot be short.
210 __ jmp(L_done);
211 __ bind(L_runtime);
212 }
213
214 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,
215 Register obj,
216 Register pre_val,
217 Register tmp,
218 bool tosca_live,
219 bool expand_call) {
220 // If expand_call is true then we expand the call_VM_leaf macro
221 // directly to skip generating the check by
222 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
223 const Register thread = r15_thread;
224
225 Label done;
226
227 assert(pre_val != noreg, "check this code");
228
229 if (obj != noreg) {
230 assert_different_registers(obj, pre_val, tmp);
231 assert(pre_val != rax, "check this code");
232 }
233
234 generate_pre_barrier_fast_path(masm, thread);
235 // If marking is not active (*(mark queue active address) == 0), jump to done
236 __ jcc(Assembler::equal, done);
237 generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, done);
238
239 if (Arguments::is_valhalla_enabled() && InlineTypePassFieldsAsArgs) {
240 // Barriers might be emitted when converting between (scalarized) calling conventions for inline
241 // types. Save all argument registers before calling into the runtime.
242 // TODO 8366717: use push_set() (see JDK-8283327 push/pop_call_clobbered_registers & aarch64 )
243 __ pusha();
244 __ subptr(rsp, 64);
245 __ movdbl(Address(rsp, 0), j_farg0);
246 __ movdbl(Address(rsp, 8), j_farg1);
247 __ movdbl(Address(rsp, 16), j_farg2);
248 __ movdbl(Address(rsp, 24), j_farg3);
249 __ movdbl(Address(rsp, 32), j_farg4);
250 __ movdbl(Address(rsp, 40), j_farg5);
251 __ movdbl(Address(rsp, 48), j_farg6);
252 __ movdbl(Address(rsp, 56), j_farg7);
253 } else {
254 // Determine and save the live input values
255 __ push_call_clobbered_registers();
256 }
257
258 // Calling the runtime using the regular call_VM_leaf mechanism generates
259 // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
260 // that checks that the *(ebp+frame::interpreter_frame_last_sp) == nullptr.
261 //
262 // If we care generating the pre-barrier without a frame (e.g. in the
263 // intrinsified Reference.get() routine) then ebp might be pointing to
264 // the caller frame and so this check will most likely fail at runtime.
265 //
266 // Expanding the call directly bypasses the generation of the check.
267 // So when we do not have have a full interpreter frame on the stack
268 // expand_call should be passed true.
269
270 if (expand_call) {
271 assert(pre_val != c_rarg1, "smashed arg");
272 if (c_rarg1 != thread) {
273 __ mov(c_rarg1, thread);
274 }
275 if (c_rarg0 != pre_val) {
276 __ mov(c_rarg0, pre_val);
277 }
278 __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), 2);
279 } else {
280 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread);
281 }
282
283 if (Arguments::is_valhalla_enabled() && InlineTypePassFieldsAsArgs) {
284 // Restore registers
285 __ movdbl(j_farg0, Address(rsp, 0));
286 __ movdbl(j_farg1, Address(rsp, 8));
287 __ movdbl(j_farg2, Address(rsp, 16));
288 __ movdbl(j_farg3, Address(rsp, 24));
289 __ movdbl(j_farg4, Address(rsp, 32));
290 __ movdbl(j_farg5, Address(rsp, 40));
291 __ movdbl(j_farg6, Address(rsp, 48));
292 __ movdbl(j_farg7, Address(rsp, 56));
293 __ addptr(rsp, 64);
294 __ popa();
295 } else {
296 __ pop_call_clobbered_registers();
297 }
298
299 __ bind(done);
300 }
301
302 static void generate_post_barrier(MacroAssembler* masm,
303 const Register store_addr,
304 const Register new_val,
305 const Register tmp1,
306 bool new_val_may_be_null) {
307
308 assert_different_registers(store_addr, new_val, tmp1, noreg);
309
310 Register thread = r15_thread;
311
312 Label L_done;
313 // Does store cross heap regions?
314 __ movptr(tmp1, store_addr); // tmp1 := store address
315 __ xorptr(tmp1, new_val); // tmp1 := store address ^ new value
316 __ shrptr(tmp1, G1HeapRegion::LogOfHRGrainBytes); // ((store address ^ new value) >> LogOfHRGrainBytes) == 0?
317 __ jccb(Assembler::equal, L_done);
318
319 // Crosses regions, storing null?
320 if (new_val_may_be_null) {
321 __ testptr(new_val, new_val); // new value == null?
322 __ jccb(Assembler::equal, L_done);
323 }
324
325 __ movptr(tmp1, store_addr); // tmp1 := store address
326 __ shrptr(tmp1, CardTable::card_shift()); // tmp1 := card address relative to card table base
327
328 Address card_table_addr(thread, in_bytes(G1ThreadLocalData::card_table_base_offset()));
329 __ addptr(tmp1, card_table_addr); // tmp1 := card address
330 if (UseCondCardMark) {
331 __ cmpb(Address(tmp1, 0), G1CardTable::clean_card_val()); // *(card address) == clean_card_val?
332 __ jccb(Assembler::notEqual, L_done);
333 }
334 // Storing a region crossing, non-null oop, card is clean.
335 // Dirty card.
336 __ movb(Address(tmp1, 0), G1CardTable::dirty_card_val()); // *(card address) := dirty_card_val
337 __ bind(L_done);
338 }
339
340 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,
341 Register store_addr,
342 Register new_val,
343 Register tmp) {
344 generate_post_barrier(masm, store_addr, new_val, tmp, true /* new_val_may_be_null */);
345 }
346
347 #if defined(COMPILER2)
348
349 static void generate_c2_barrier_runtime_call(MacroAssembler* masm, G1BarrierStubC2* stub, const Register arg, const address runtime_path) {
350 SaveLiveRegisters save_registers(masm, stub);
351 if (c_rarg0 != arg) {
352 __ mov(c_rarg0, arg);
353 }
354 __ mov(c_rarg1, r15_thread);
355 // rax is a caller-saved, non-argument-passing register, so it does not
356 // interfere with c_rarg0 or c_rarg1. If it contained any live value before
357 // entering this stub, it is saved at this point, and restored after the
358 // call. If it did not contain any live value, it is free to be used. In
359 // either case, it is safe to use it here as a call scratch register.
360 __ call(RuntimeAddress(runtime_path), rax);
361 }
362
363 void G1BarrierSetAssembler::g1_write_barrier_pre_c2(MacroAssembler* masm,
364 Register obj,
365 Register pre_val,
366 Register tmp,
367 G1PreBarrierStubC2* stub) {
368 const Register thread = r15_thread;
369
370 assert(pre_val != noreg, "check this code");
371 if (obj != noreg) {
372 assert_different_registers(obj, pre_val, tmp);
373 }
374
375 stub->initialize_registers(obj, pre_val, thread, tmp);
376
377 generate_pre_barrier_fast_path(masm, thread);
378 // If marking is active (*(mark queue active address) != 0), jump to stub (slow path)
379 __ jcc(Assembler::notEqual, *stub->entry());
380
381 __ bind(*stub->continuation());
382 }
383
384 void G1BarrierSetAssembler::generate_c2_pre_barrier_stub(MacroAssembler* masm,
385 G1PreBarrierStubC2* stub) const {
386 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
387 Register obj = stub->obj();
388 Register pre_val = stub->pre_val();
389 Register thread = stub->thread();
390 Register tmp = stub->tmp1();
391 assert(stub->tmp2() == noreg, "not needed in this platform");
392
393 __ bind(*stub->entry());
394 generate_pre_barrier_slow_path(masm, obj, pre_val, thread, tmp, *stub->continuation());
395
396 generate_c2_barrier_runtime_call(masm, stub, pre_val, CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry));
397 __ jmp(*stub->continuation());
398 }
399
400 void G1BarrierSetAssembler::g1_write_barrier_post_c2(MacroAssembler* masm,
401 Register store_addr,
402 Register new_val,
403 Register tmp,
404 bool new_val_may_be_null) {
405 generate_post_barrier(masm, store_addr, new_val, tmp, new_val_may_be_null);
406 }
407
408 #endif // COMPILER2
409
410 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
411 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
412 bool in_heap = (decorators & IN_HEAP) != 0;
413 bool as_normal = (decorators & AS_NORMAL) != 0;
414 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
415
416 bool needs_pre_barrier = as_normal && !dest_uninitialized;
417 bool needs_post_barrier = val != noreg && in_heap;
418
419 // flatten object address if needed
420 // We do it regardless of precise because we need the registers
421 if (dst.index() == noreg && dst.disp() == 0) {
422 if (dst.base() != tmp1) {
423 __ movptr(tmp1, dst.base());
424 }
425 } else {
426 __ lea(tmp1, dst);
427 }
428
429 if (needs_pre_barrier) {
430 g1_write_barrier_pre(masm /*masm*/,
431 tmp1 /* obj */,
432 tmp2 /* pre_val */,
433 tmp3 /* tmp */,
434 val != noreg /* tosca_live */,
435 false /* expand_call */);
436 }
437 if (val == noreg) {
438 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
439 } else {
440 Register new_val = val;
441 if (needs_post_barrier) {
442 // G1 barrier needs uncompressed oop for region cross check.
443 if (UseCompressedOops) {
444 new_val = tmp2;
445 __ movptr(new_val, val);
446 }
447 }
448 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
449 if (needs_post_barrier) {
450 g1_write_barrier_post(masm /*masm*/,
451 tmp1 /* store_adr */,
452 new_val /* new_val */,
453 tmp3 /* tmp */);
454 }
455 }
456 }
457
458 #ifdef COMPILER1
459
460 #undef __
461 #define __ ce->masm()->
462
463 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
464 G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
465 // At this point we know that marking is in progress.
466 // If do_load() is true then we have to emit the
467 // load of the previous value; otherwise it has already
468 // been loaded into _pre_val.
469
470 __ bind(*stub->entry());
471 assert(stub->pre_val()->is_register(), "Precondition.");
472
473 Register pre_val_reg = stub->pre_val()->as_register();
474
475 if (stub->do_load()) {
476 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
477 }
478
479 __ testptr(pre_val_reg, pre_val_reg);
480 __ jcc(Assembler::equal, *stub->continuation());
481 ce->store_parameter(stub->pre_val()->as_register(), 0);
482 __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
483 __ jmp(*stub->continuation());
484
485 }
486
487 #undef __
488
489 void G1BarrierSetAssembler::g1_write_barrier_post_c1(MacroAssembler* masm,
490 Register store_addr,
491 Register new_val,
492 Register thread,
493 Register tmp1,
494 Register tmp2 /* unused on x86 */) {
495 generate_post_barrier(masm, store_addr, new_val, tmp1, true /* new_val_may_be_null */);
496 }
497
498 #define __ sasm->
499
500 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
501 // Generated code assumes that buffer index is pointer sized.
502 STATIC_ASSERT(in_bytes(SATBMarkQueue::byte_width_of_index()) == sizeof(intptr_t));
503
504 __ prologue("g1_pre_barrier", false);
505 // arg0 : previous value of memory
506
507 __ push_ppx(rax);
508 __ push_ppx(rdx);
509
510 const Register pre_val = rax;
511 const Register thread = r15_thread;
512 const Register tmp = rdx;
513
514 Address queue_active(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));
515 Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));
516 Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));
517
518 Label L_done, L_runtime;
519
520 // Is marking still active?
521 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
522 __ cmpl(queue_active, 0);
523 } else {
524 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
525 __ cmpb(queue_active, 0);
526 }
527 __ jcc(Assembler::equal, L_done);
528
529 // Can we store original value in the thread's buffer?
530
531 __ movptr(tmp, queue_index);
532 __ testptr(tmp, tmp);
533 __ jccb(Assembler::zero, L_runtime);
534 __ subptr(tmp, wordSize);
535 __ movptr(queue_index, tmp);
536 __ addptr(tmp, buffer);
537
538 // prev_val (rax)
539 __ load_parameter(0, pre_val);
540 __ movptr(Address(tmp, 0), pre_val);
541 __ jmp(L_done);
542
543 __ bind(L_runtime);
544
545 __ push_call_clobbered_registers();
546
547 // load the pre-value
548 __ load_parameter(0, rcx);
549 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), rcx, thread);
550
551 __ pop_call_clobbered_registers();
552
553 __ bind(L_done);
554
555 __ pop_ppx(rdx);
556 __ pop_ppx(rax);
557
558 __ epilogue();
559 }
560
561 #undef __
562
563 #endif // COMPILER1