1 /*
2 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2018, 2021, Red Hat, Inc. All rights reserved.
4 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
28 #include "gc/shenandoah/mode/shenandoahMode.hpp"
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
32 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
33 #include "gc/shenandoah/shenandoahRuntime.hpp"
34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
35 #include "interpreter/interpreter.hpp"
36 #include "runtime/javaThread.hpp"
37 #include "runtime/sharedRuntime.hpp"
38 #include "utilities/macros.hpp"
39 #ifdef COMPILER1
40 #include "c1/c1_LIRAssembler.hpp"
41 #include "c1/c1_MacroAssembler.hpp"
42 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
43 #endif
44 #ifdef COMPILER2
45 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
46 #endif
47
48 #define __ masm->
49
50 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
51 Register src, Register dst, Register count) {
52
53 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
54
55 if (is_reference_type(type)) {
56 if (ShenandoahCardBarrier) {
57 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
58 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
59 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
60
61 // We need to save the original element count because the array copy stub
62 // will destroy the value and we need it for the card marking barrier.
63 if (!checkcast) {
64 if (!obj_int) {
65 // Save count for barrier
66 __ movptr(r11, count);
67 } else if (disjoint) {
68 // Save dst in r11 in the disjoint case
69 __ movq(r11, dst);
70 }
71 }
72 }
73
74 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
75 Register thread = r15_thread;
76 assert_different_registers(src, dst, count, thread);
77
78 Label L_done;
79 // Short-circuit if count == 0.
80 __ testptr(count, count);
81 __ jcc(Assembler::zero, L_done);
82
83 // Avoid runtime call when not active.
84 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
85 int flags;
86 if (ShenandoahSATBBarrier && dest_uninitialized) {
87 flags = ShenandoahHeap::HAS_FORWARDED;
88 } else {
89 flags = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING;
90 }
91 __ testb(gc_state, flags);
92 __ jcc(Assembler::zero, L_done);
93
94 __ push_call_clobbered_registers(/* save_fpu = */ false);
95 // If arguments are not in proper places, shuffle them.
96 // Doing this via the stack is the most straight-forward way to avoid
97 // accidentally smashing any register.
98 if (c_rarg0 != src || c_rarg1 != dst || c_rarg2 != count) {
99 __ push(src);
100 __ push(dst);
101 __ push(count);
102 __ pop(c_rarg2);
103 __ pop(c_rarg1);
104 __ pop(c_rarg0);
105 }
106 address target = nullptr;
107 if (UseCompressedOops) {
108 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop);
109 } else {
110 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop);
111 }
112 __ call_VM_leaf(target, 3);
113
114 __ pop_call_clobbered_registers(/* restore_fpu = */ false);
115
116 __ bind(L_done);
117 }
118 }
119
120 }
121
122 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
123 Register src, Register dst, Register count) {
124
125 if (ShenandoahCardBarrier && is_reference_type(type)) {
126 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
127 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
128 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
129 Register tmp = rax;
130
131 if (!checkcast) {
132 if (!obj_int) {
133 // Save count for barrier
134 count = r11;
135 } else if (disjoint) {
136 // Use the saved dst in the disjoint case
137 dst = r11;
138 }
139 } else {
140 tmp = rscratch1;
141 }
142 gen_write_ref_array_post_barrier(masm, decorators, dst, count, tmp);
143 }
144 }
145
146 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
147 Register obj,
148 Register pre_val,
149 Register tmp) {
150 assert(ShenandoahSATBBarrier, "Should be checked by caller");
151 const Register thread = r15_thread;
152
153 Label done;
154 Label runtime;
155
156 assert(pre_val != noreg, "check this code");
157 assert_different_registers(obj, pre_val, tmp);
158
159 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
160 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
161
162 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
163 __ testb(gc_state, ShenandoahHeap::MARKING);
164 __ jcc(Assembler::zero, done);
165
166 // Do we need to load the previous value?
167 if (obj != noreg) {
168 if (UseCompressedOops) {
169 __ movl(pre_val, Address(obj, 0));
170 __ decode_heap_oop(pre_val);
171 } else {
172 __ movq(pre_val, Address(obj, 0));
173 }
174 }
175
176 // Is the previous value null?
177 __ cmpptr(pre_val, NULL_WORD);
178 __ jcc(Assembler::equal, done);
179
180 // Can we store original value in the thread's buffer?
181 // Is index == 0?
182 // (The index field is typed as size_t.)
183
184 __ movptr(tmp, index); // tmp := *index_adr
185 __ cmpptr(tmp, 0); // tmp == 0?
186 __ jcc(Assembler::equal, runtime); // If yes, goto runtime
187
188 __ subptr(tmp, wordSize); // tmp := tmp - wordSize
189 __ movptr(index, tmp); // *index_adr := tmp
190 __ addptr(tmp, buffer); // tmp := tmp + *buffer_adr
191
192 // Record the previous value
193 __ movptr(Address(tmp, 0), pre_val);
194 __ jmp(done);
195
196 __ bind(runtime);
197
198 // Slow-path call.
199 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
200 __ enter();
201 __ push_call_clobbered_registers(/* save_fpu = */ true);
202
203 assert(thread != c_rarg0, "smashed arg");
204 if (c_rarg0 != pre_val) {
205 __ mov(c_rarg0, pre_val);
206 }
207
208 // Calling with super_call_VM_leaf with c_rarg0 bypasses interpreter checks and avoids any moves.
209 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
210
211 __ pop_call_clobbered_registers(/* restore_fpu = */ true);
212 __ leave();
213
214 __ bind(done);
215 }
216
217 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address src, DecoratorSet decorators) {
218 assert(ShenandoahLoadRefBarrier, "Should be enabled");
219
220 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
221 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
222 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
223 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
224 bool is_narrow = UseCompressedOops && !is_native;
225
226 Label heap_stable, not_cset;
227
228 __ block_comment("load_reference_barrier { ");
229
230 // Check if GC is active
231 Register thread = r15_thread;
232
233 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
234 int flags = ShenandoahHeap::HAS_FORWARDED;
235 if (!is_strong) {
236 flags |= ShenandoahHeap::WEAK_ROOTS;
237 }
238 __ testb(gc_state, flags);
239 __ jcc(Assembler::zero, heap_stable);
240
241 Register tmp1 = noreg, tmp2 = noreg;
242 if (is_strong) {
243 // Test for object in cset
244 // Allocate temporary registers
245 for (int i = 0; i < Register::available_gp_registers(); i++) {
246 Register r = as_Register(i);
247 if (r != rsp && r != rbp && r != rcx && r != dst && r != src.base() && r != src.index() ) {
248 if (tmp1 == noreg) {
249 tmp1 = r;
250 } else {
251 tmp2 = r;
252 break;
253 }
254 }
255 }
256 assert(tmp1 != noreg, "tmp1 allocated");
257 assert(tmp2 != noreg, "tmp2 allocated");
258 assert_different_registers(tmp1, tmp2, src.base(), src.index());
259 assert_different_registers(tmp1, tmp2, dst);
260
261 __ push(tmp1);
262 __ push(tmp2);
263
264 // Optimized cset-test
265 __ movptr(tmp1, dst);
266 if (AOTCodeCache::is_on_for_dump()) {
267 assert_different_registers(tmp1, tmp2, rcx);
268 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
269 __ push(rcx);
270 __ movb(rcx, Address(tmp2));
271 __ shrptr(tmp1);
272 __ pop(rcx);
273 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
274 __ movptr(tmp2, Address(tmp2));
275 } else {
276 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
277 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
278 }
279 __ movbool(tmp1, Address(tmp1, tmp2, Address::times_1));
280 __ testbool(tmp1);
281 __ jcc(Assembler::zero, not_cset);
282 }
283
284 // Slow-path call.
285 // Save registers that can be clobbered by call.
286 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
287 __ enter();
288 if (dst != rax) {
289 __ push(rax);
290 }
291 __ push_call_clobbered_registers_except(rax, /* save_fpu = */ true);
292
293 // Shuffle registers such that dst is in c_rarg0 and addr in c_rarg1.
294 if (dst == c_rarg1) {
295 __ lea(c_rarg0, src);
296 __ xchgptr(c_rarg1, c_rarg0);
297 } else {
298 __ lea(c_rarg1, src);
299 __ movptr(c_rarg0, dst);
300 }
301
302 address target = nullptr;
303 if (is_strong) {
304 if (is_narrow) {
305 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
306 } else {
307 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
308 }
309 } else if (is_weak) {
310 if (is_narrow) {
311 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
312 } else {
313 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
314 }
315 } else {
316 assert(is_phantom, "only remaining strength");
317 assert(!is_narrow, "phantom access cannot be narrow");
318 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
319 }
320
321 // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
322 __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
323 __ pop_call_clobbered_registers_except(rax, /* restore_fpu = */ true);
324 if (dst != rax) {
325 __ movptr(dst, rax);
326 __ pop(rax);
327 }
328 __ leave();
329
330 __ bind(not_cset);
331
332 if (is_strong) {
333 __ pop(tmp2);
334 __ pop(tmp1);
335 }
336
337 __ bind(heap_stable);
338
339 __ block_comment("} load_reference_barrier");
340 }
341
342 //
343 // Arguments:
344 //
345 // Inputs:
346 // src: oop location, might be clobbered
347 // tmp1: scratch register, might not be valid.
348 //
349 // Output:
350 // dst: oop loaded from src location
351 //
352 // Kill:
353 // tmp1 (if it is valid)
354 //
355 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
356 Register dst, Address src, Register tmp1) {
357 // 1: non-reference load, no additional barrier is needed
358 if (!is_reference_type(type)) {
359 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
360 return;
361 }
362
363 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Not expected");
364
365 // 2: load a reference from src location and apply LRB if needed
366 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
367 Register result_dst = dst;
368 bool use_tmp1_for_dst = false;
369
370 // Preserve src location for LRB
371 if (dst == src.base() || dst == src.index()) {
372 // Use tmp1 for dst if possible, as it is not used in BarrierAssembler::load_at()
373 if (tmp1->is_valid() && tmp1 != src.base() && tmp1 != src.index()) {
374 dst = tmp1;
375 use_tmp1_for_dst = true;
376 } else {
377 dst = rdi;
378 __ push(dst);
379 }
380 assert_different_registers(dst, src.base(), src.index());
381 }
382
383 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
384
385 load_reference_barrier(masm, dst, src, decorators);
386
387 // Move loaded oop to final destination
388 if (dst != result_dst) {
389 __ movptr(result_dst, dst);
390
391 if (!use_tmp1_for_dst) {
392 __ pop(dst);
393 }
394
395 dst = result_dst;
396 }
397 } else {
398 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
399 }
400
401 // 3: apply keep-alive barrier if needed
402 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
403 satb_barrier(masm /* masm */,
404 noreg /* obj */,
405 dst /* pre_val */,
406 tmp1 /* tmp */);
407 }
408 }
409
410 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
411 assert(ShenandoahCardBarrier, "Should have been checked by caller");
412
413 // Does a store check for the oop in register obj. The content of
414 // register obj is destroyed afterwards.
415 __ shrptr(obj, CardTable::card_shift());
416
417 // We'll use this register as the TLS base address and also later on
418 // to hold the byte_map_base.
419 Register thread = r15_thread;
420 Register tmp = rscratch1;
421
422 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
423 __ movptr(tmp, curr_ct_holder_addr);
424 Address card_addr(tmp, obj, Address::times_1);
425
426 int dirty = CardTable::dirty_card_val();
427 if (UseCondCardMark) {
428 Label L_already_dirty;
429 __ cmpb(card_addr, dirty);
430 __ jccb(Assembler::equal, L_already_dirty);
431 __ movb(card_addr, dirty);
432 __ bind(L_already_dirty);
433 } else {
434 __ movb(card_addr, dirty);
435 }
436 }
437
438 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
439 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
440
441 // 1: non-reference types require no barriers
442 if (!is_reference_type(type)) {
443 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
444 return;
445 }
446
447 // Flatten object address right away for simplicity: likely needed by barriers
448 assert_different_registers(val, tmp1, tmp2, tmp3, r15_thread);
449 if (dst.index() == noreg && dst.disp() == 0) {
450 if (dst.base() != tmp1) {
451 __ movptr(tmp1, dst.base());
452 }
453 } else {
454 __ lea(tmp1, dst);
455 }
456
457 // 2: pre-barrier: SATB needs the previous value
458 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
459 satb_barrier(masm,
460 tmp1 /* obj */,
461 tmp2 /* pre_val */,
462 tmp3 /* tmp */);
463 }
464
465 // Store!
466 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
467
468 // 3: post-barrier: card barrier needs store address
469 bool storing_non_null = (val != noreg);
470 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
471 card_barrier(masm, tmp1);
472 }
473 }
474
475 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
476 Register obj, Register tmp, Label& slowpath) {
477 Label done;
478 // Resolve jobject
479 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
480
481 // Check for null.
482 __ testptr(obj, obj);
483 __ jcc(Assembler::zero, done);
484
485 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
486 __ testb(gc_state, ShenandoahHeap::EVACUATION);
487 __ jccb(Assembler::notZero, slowpath);
488 __ bind(done);
489 }
490
491 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler* masm, Register weak_handle, Register obj, Label& slowpath) {
492 Label done;
493
494 // Peek weak handle using the standard implementation.
495 BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, slowpath);
496
497 // Check if the reference is null, and if it is, take the fast path.
498 __ testptr(obj, obj);
499 __ jcc(Assembler::zero, done);
500
501 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
502
503 // Check if the heap is under weak-reference/roots processing, in
504 // which case we need to take the slow path.
505 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
506 __ jcc(Assembler::notZero, slowpath);
507 __ bind(done);
508 }
509
510 #ifdef PRODUCT
511 #define BLOCK_COMMENT(str) /* nothing */
512 #else
513 #define BLOCK_COMMENT(str) __ block_comment(str)
514 #endif
515
516 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
517
518 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
519
520 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
521 Register addr, Register count,
522 Register tmp) {
523 assert(ShenandoahCardBarrier, "Should have been checked by caller");
524
525 Label L_loop, L_done;
526 const Register end = count;
527 assert_different_registers(addr, end);
528
529 // Zero count? Nothing to do.
530 __ testl(count, count);
531 __ jccb(Assembler::zero, L_done);
532
533 const Register thread = r15_thread;
534 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
535 __ movptr(tmp, curr_ct_holder_addr);
536
537 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
538 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
539 __ shrptr(addr, CardTable::card_shift());
540 __ shrptr(end, CardTable::card_shift());
541 __ subptr(end, addr); // end --> cards count
542
543 __ addptr(addr, tmp);
544
545 __ BIND(L_loop);
546 __ movb(Address(addr, count, Address::times_1), 0);
547 __ decrement(count);
548 __ jccb(Assembler::greaterEqual, L_loop);
549
550 __ BIND(L_done);
551 }
552
553 #undef __
554
555 #ifdef COMPILER1
556
557 #define __ ce->masm()->
558
559 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
560 __ bind(*stub->entry());
561
562 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
563
564 Register obj = stub->obj()->as_register();
565
566 if (stub->do_load()) {
567 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, /* wide = */ false);
568 }
569 __ cmpptr(obj, NULL_WORD);
570 __ jcc(Assembler::equal, *stub->continuation());
571
572 ce->store_parameter(obj, 0);
573 __ call(RuntimeAddress(bs->keepalive_barrier_stub()));
574 __ jmp(*stub->continuation());
575 }
576
577 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
578 __ bind(*stub->entry());
579
580 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
581
582 Register obj = stub->obj()->as_register();
583 Register addr = stub->addr()->as_pointer_register();
584 Register slow_result = stub->slow_result()->as_register();
585 assert_different_registers(obj, addr, slow_result);
586 assert(slow_result == rax, "C1 must know about our slow call result register");
587
588 ce->store_parameter(obj, 0);
589 ce->store_parameter(addr, 1);
590 __ call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
591 if (obj != slow_result) {
592 __ mov(obj, slow_result);
593 }
594
595 __ jmp(*stub->continuation());
596 }
597
598 #undef __
599
600 #define __ sasm->
601
602 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
603 __ prologue("shenandoah_keepalive_barrier", false);
604 const Register tmp_obj = rax;
605 const Register tmp = rdx;
606 __ push(tmp);
607 __ push(tmp_obj);
608 __ load_parameter(0, tmp_obj);
609 satb_barrier(sasm, noreg, tmp_obj, tmp);
610 __ pop(tmp_obj);
611 __ pop(tmp);
612 __ epilogue();
613 }
614
615 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
616 __ prologue("shenandoah_load_reference_barrier", false);
617 const Register tmp_obj = rax;
618 const Register tmp_addr = rdx;
619 __ push(tmp_addr);
620 __ load_parameter(0, tmp_obj);
621 __ load_parameter(1, tmp_addr);
622 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
623 __ pop(tmp_addr);
624 __ epilogue();
625 }
626
627 #undef __
628
629 #endif // COMPILER1
630
631 #ifdef COMPILER2
632
633 #undef __
634 #define __ masm->
635
636 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
637 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
638 if (narrow) {
639 __ movl(dst, src);
640 } else {
641 __ movq(dst, src);
642 }
643
644 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, noreg, noreg, narrow);
645 }
646
647 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
648 Address dst, bool dst_narrow,
649 Register src, bool src_narrow,
650 Register tmp) {
651
652 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp, noreg, noreg, dst_narrow);
653
654 // Need to encode into tmp, because we cannot clobber src.
655 if (dst_narrow && !src_narrow) {
656 __ movq(tmp, src);
657 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
658 __ encode_heap_oop(tmp);
659 } else {
660 __ encode_heap_oop_not_null(tmp);
661 }
662 src = tmp;
663 }
664
665 // Do the actual store
666 if (dst_narrow) {
667 __ movl(dst, src);
668 } else {
669 __ movq(dst, src);
670 }
671
672 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp, noreg);
673 }
674
675 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
676 Register res, Address addr,
677 Register oldval, Register newval, Register tmp,
678 bool narrow) {
679
680 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
681
682 // Oldval and newval can be in the same register, but all other registers should be
683 // distinct for extra safety, as we shuffle register values around.
684 assert_different_registers(oldval, tmp, addr.base(), addr.index());
685 assert_different_registers(newval, tmp, addr.base(), addr.index());
686
687 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
688
689 // CAS!
690 __ lock();
691 if (narrow) {
692 __ cmpxchgl(newval, addr);
693 } else {
694 __ cmpxchgptr(newval, addr);
695 }
696
697 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
698 if (res != noreg) {
699 __ setcc(Assembler::equal, res);
700 }
701
702 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
703 }
704
705 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
706 assert_different_registers(newval, tmp, addr.base(), addr.index());
707
708 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
709
710 if (narrow) {
711 __ xchgl(newval, addr);
712 } else {
713 __ xchgq(newval, addr);
714 }
715
716 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
717 }
718
719 #undef __
720 #define __ masm.
721
722 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2) {
723 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
724
725 __ lea(tmp1, addr);
726 __ shrptr(tmp1, CardTable::card_shift());
727 __ addptr(tmp1, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
728 Address card_address(tmp1, 0);
729
730 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
731 Label L_done;
732 if (UseCondCardMark) {
733 __ cmpb(card_address, 0);
734 __ jccb(Assembler::equal, L_done);
735 }
736 if (UseCompressedOops && CompressedOops::base() == nullptr) {
737 __ movb(card_address, r12);
738 } else {
739 __ movb(card_address, 0);
740 }
741 __ bind(L_done);
742 }
743
744 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
745 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
746
747 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
748 __ cmpb(gc_state_fast, 0);
749 __ jcc(Assembler::notEqual, *entry());
750 __ bind(*continuation());
751 }
752
753 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
754 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
755 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
756
757 // On x86, there is a significant penalty with unaligned branch target, for example
758 // when the target instruction straggles the fetch line. It makes (performance) sense
759 // to spend some code size to align the target better.
760 __ align(16);
761 __ bind(*entry());
762
763 // If we need to load ourselves, do it here.
764 if (_do_load) {
765 if (_narrow) {
766 __ movl(_obj, _addr);
767 } else {
768 __ movq(_obj, _addr);
769 }
770 }
771
772 // If the object is null, there is no point in applying barriers.
773 maybe_far_jump_if_zero(masm, _obj);
774
775 // We need to make sure that loads done by callers survive across slow-path calls.
776 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
777 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
778 if (!_do_load || needs_both_barriers) {
779 preserve(_obj);
780 }
781
782 // Go for barriers. Barriers can return straight to continuation, as long
783 // as another barrier is not needed.
784 if (needs_both_barriers) {
785 keepalive(masm, nullptr);
786 lrb(masm);
787 } else if (_needs_keep_alive_barrier) {
788 keepalive(masm, continuation());
789 } else if (_needs_load_ref_barrier) {
790 lrb(masm);
791 } else {
792 ShouldNotReachHere();
793 }
794 }
795
796 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
797 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
798 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
799 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
800
801 Label L_through, L_pop_and_slow;
802
803 // If another barrier is enabled as well, do a runtime check for a specific barrier.
804 if (_needs_load_ref_barrier) {
805 assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
806 __ cmpb(gc_state_fast, 0);
807 __ jcc(Assembler::equal, L_through);
808 }
809
810 // Need temp to work, allocate one now.
811 bool tmp_live;
812 Register tmp = select_temp_register(tmp_live);
813 if (tmp_live) {
814 __ push(tmp);
815 }
816
817 // Fast-path: put object into buffer.
818 // If buffer is already full, go slow.
819 __ movptr(tmp, index);
820 __ subptr(tmp, wordSize);
821 __ jccb(Assembler::below, L_pop_and_slow);
822 __ movptr(index, tmp);
823 __ addptr(tmp, buffer);
824
825 // Store the object in queue.
826 // If object is narrow, we need to decode it before inserting.
827 // We can skip the re-encoding if we know that object is not preserved.
828 if (_narrow) {
829 __ decode_heap_oop_not_null(_obj);
830 }
831 __ movptr(Address(tmp, 0), _obj);
832 if (_narrow && is_preserved(_obj)) {
833 __ encode_heap_oop_not_null(_obj);
834 }
835
836 // Fast-path exits here.
837 if (tmp_live) {
838 __ pop(tmp);
839 }
840
841 if (L_done != nullptr) {
842 __ jmp(*L_done);
843 } else {
844 __ jmp(L_through);
845 }
846
847 // Slow-path: call runtime to handle.
848 // Need to pop tmp immediately for stack to remain aligned.
849 __ bind(L_pop_and_slow);
850 if (tmp_live) {
851 __ pop(tmp);
852 }
853 {
854 SaveLiveRegisters slr(&masm, this);
855
856 // Shuffle in the arguments. The end result should be:
857 // c_rarg0 <-- obj
858 if (c_rarg0 != _obj) {
859 __ mov(c_rarg0, _obj);
860 }
861
862 // Go to runtime and handle the rest there.
863 // Use rax as scratch, as it will be saved if live.
864 __ call(RuntimeAddress(keepalive_runtime_entry_addr()), rax);
865 }
866 if (L_done != nullptr) {
867 __ jmp(*L_done);
868 } else {
869 __ bind(L_through);
870 }
871 }
872
873 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
874 Label L_pop_and_slow, L_slow;
875
876 // If another barrier is enabled as well, do a runtime check for a specific barrier.
877 if (_needs_keep_alive_barrier) {
878 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
879 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
880 __ cmpb(gc_state_fast, 0);
881 __ jcc(Assembler::equal, *continuation());
882 }
883
884 // If weak references are being processed, weak/phantom loads need to go slow,
885 // regardless of their cset status.
886 if (_needs_load_ref_weak_barrier) {
887 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
888 __ cmpb(gc_state_fast, 0);
889 __ jccb(Assembler::notEqual, L_slow);
890 }
891
892 bool is_aot = AOTCodeCache::is_on_for_dump();
893
894 // Need temp to work, allocate one now.
895 bool tmp_live;
896 Register tmp = select_temp_register(tmp_live, /* skip_reg1 = */ is_aot ? rcx : noreg);
897 if (tmp_live) {
898 __ push(tmp);
899 }
900
901 // Compute the cset bitmap index
902 if (_narrow) {
903 __ decode_heap_oop_not_null(tmp, _obj);
904 } else {
905 __ movptr(tmp, _obj);
906 }
907
908 Address cset_addr_arg;
909 intptr_t cset_addr = reinterpret_cast<intptr_t>(ShenandoahHeap::in_cset_fast_test_addr());
910 if (!is_aot && cset_addr < INT32_MAX) {
911 // Cset bitmap is at easily encodeable address. Just use it as displacement.
912 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
913 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr));
914 } else {
915 bool tmp2_live;
916 Register tmp2 = select_temp_register(tmp2_live, /* skip_reg1 = */ tmp, /* skip_reg2 = */ is_aot ? rcx : noreg);
917 if (tmp2_live) {
918 __ push(tmp2);
919 }
920 if (is_aot) {
921 // Generating AOT code, pull the cset bitmap and region shift from AOT table.
922 assert_different_registers(tmp, tmp2, rcx);
923 __ push(rcx);
924 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
925 __ movl(rcx, Address(rcx));
926 __ shrptr(tmp);
927 __ pop(rcx);
928 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
929 __ addptr(tmp, Address(tmp2));
930 } else {
931 // Cset bitmap is far away. Add its address fully.
932 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
933 __ movptr(tmp2, cset_addr);
934 __ addptr(tmp, tmp2);
935 }
936 if (tmp2_live) {
937 __ pop(tmp2);
938 }
939 cset_addr_arg = Address(tmp, 0);
940 }
941
942 // Cset-check. Fall-through to slow if in collection set.
943 __ cmpb(cset_addr_arg, 0);
944 if (tmp_live) {
945 __ jccb(Assembler::notEqual, L_pop_and_slow);
946 __ pop(tmp);
947 __ jmp(*continuation());
948 } else {
949 // Nothing else to do, jump back
950 __ jcc(Assembler::equal, *continuation());
951 }
952
953 // Slow path
954 __ bind(L_pop_and_slow);
955 // Need to pop tmp immediately for stack to remain aligned.
956 if (tmp_live) {
957 __ pop(tmp);
958 }
959 __ bind(L_slow);
960
961 // Obj is the result, need to temporarily stop preserving it.
962 bool is_obj_preserved = is_preserved(_obj);
963 if (is_obj_preserved) {
964 dont_preserve(_obj);
965 }
966 {
967 SaveLiveRegisters slr(&masm, this);
968
969 assert_different_registers(rax, c_rarg0, c_rarg1);
970
971 // Shuffle in the arguments. The end result should be:
972 // c_rarg0 <-- obj
973 // c_rarg1 <-- lea(addr)
974 if (_obj == c_rarg0) {
975 __ lea(c_rarg1, _addr);
976 } else if (_obj == c_rarg1) {
977 // Set up arguments in reverse, and then flip them
978 __ lea(c_rarg0, _addr);
979 __ xchgptr(c_rarg0, c_rarg1);
980 } else {
981 assert_different_registers(_obj, c_rarg0, c_rarg1);
982 __ lea(c_rarg1, _addr);
983 __ movptr(c_rarg0, _obj);
984 }
985
986 // Go to runtime and handle the rest there.
987 // Use rax as scratch, as it will be clobbered by result anyway.
988 __ call(RuntimeAddress(lrb_runtime_entry_addr()), rax);
989
990 // Save the result where needed.
991 if (_narrow) {
992 __ movl(_obj, rax);
993 } else if (_obj != rax) {
994 __ movptr(_obj, rax);
995 }
996 }
997 if (is_obj_preserved) {
998 preserve(_obj);
999 }
1000
1001 __ jmp(*continuation());
1002 }
1003
1004 int ShenandoahBarrierStubC2::available_gp_registers() {
1005 return Register::available_gp_registers();
1006 }
1007
1008 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
1009 return r == rsp || r == rbp || r == r12_heapbase || r == r15_thread;
1010 }
1011
1012 void ShenandoahBarrierStubC2::post_init() {
1013 // Do nothing.
1014 }
1015
1016 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
1017 if (_narrow) {
1018 __ testl(reg, reg);
1019 } else {
1020 __ testq(reg, reg);
1021 }
1022 __ jcc(Assembler::zero, *continuation());
1023 }
1024
1025 #endif // COMPILER2