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 void ShenandoahBarrierSetAssembler::check_oop(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& L_error) {
511 // Check if the oop is in the right area of memory
512 __ movptr(tmp1, obj);
513 __ movptr(tmp2, (intptr_t) Universe::verify_oop_mask());
514 __ andptr(tmp1, tmp2);
515 __ movptr(tmp2, (intptr_t) Universe::verify_oop_bits());
516 __ cmpptr(tmp1, tmp2);
517 __ jcc(Assembler::notZero, L_error);
518
519 // This routine is sometimes called before applying GC barriers.
520 // With +COH, loading the klass may end up loading forwarding pointer instead.
521 Label L_skip;
522 if (UseCompactObjectHeaders) {
523 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
524 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
525 __ jcc(Assembler::notZero, L_skip);
526 }
527
528 // Make sure klass is 'reasonable', which is not zero.
529 __ load_narrow_klass(tmp1, obj);
530 __ testl(tmp1, tmp1);
531 __ jcc(Assembler::zero, L_error);
532
533 __ bind(L_skip);
534 }
535
536 #ifdef PRODUCT
537 #define BLOCK_COMMENT(str) /* nothing */
538 #else
539 #define BLOCK_COMMENT(str) __ block_comment(str)
540 #endif
541
542 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
543
544 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
545
546 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
547 Register addr, Register count,
548 Register tmp) {
549 assert(ShenandoahCardBarrier, "Should have been checked by caller");
550
551 Label L_loop, L_done;
552 const Register end = count;
553 assert_different_registers(addr, end);
554
555 // Zero count? Nothing to do.
556 __ testl(count, count);
557 __ jccb(Assembler::zero, L_done);
558
559 const Register thread = r15_thread;
560 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
561 __ movptr(tmp, curr_ct_holder_addr);
562
563 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
564 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
565 __ shrptr(addr, CardTable::card_shift());
566 __ shrptr(end, CardTable::card_shift());
567 __ subptr(end, addr); // end --> cards count
568
569 __ addptr(addr, tmp);
570
571 __ BIND(L_loop);
572 __ movb(Address(addr, count, Address::times_1), 0);
573 __ decrement(count);
574 __ jccb(Assembler::greaterEqual, L_loop);
575
576 __ BIND(L_done);
577 }
578
579 #undef __
580
581 #ifdef COMPILER1
582
583 #define __ ce->masm()->
584
585 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
586 __ bind(*stub->entry());
587
588 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
589
590 Register obj = stub->obj()->as_register();
591
592 if (stub->do_load()) {
593 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, /* wide = */ false);
594 }
595 __ cmpptr(obj, NULL_WORD);
596 __ jcc(Assembler::equal, *stub->continuation());
597
598 ce->store_parameter(obj, 0);
599 __ call(RuntimeAddress(bs->keepalive_barrier_stub()));
600 __ jmp(*stub->continuation());
601 }
602
603 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
604 __ bind(*stub->entry());
605
606 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
607
608 Register obj = stub->obj()->as_register();
609 Register addr = stub->addr()->as_pointer_register();
610 Register slow_result = stub->slow_result()->as_register();
611 assert_different_registers(obj, addr, slow_result);
612 assert(slow_result == rax, "C1 must know about our slow call result register");
613
614 ce->store_parameter(obj, 0);
615 ce->store_parameter(addr, 1);
616 __ call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
617 if (obj != slow_result) {
618 __ mov(obj, slow_result);
619 }
620
621 __ jmp(*stub->continuation());
622 }
623
624 #undef __
625
626 #define __ sasm->
627
628 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
629 __ prologue("shenandoah_keepalive_barrier", false);
630 const Register tmp_obj = rax;
631 const Register tmp = rdx;
632 __ push(tmp);
633 __ push(tmp_obj);
634 __ load_parameter(0, tmp_obj);
635 satb_barrier(sasm, noreg, tmp_obj, tmp);
636 __ pop(tmp_obj);
637 __ pop(tmp);
638 __ epilogue();
639 }
640
641 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
642 __ prologue("shenandoah_load_reference_barrier", false);
643 const Register tmp_obj = rax;
644 const Register tmp_addr = rdx;
645 __ push(tmp_addr);
646 __ load_parameter(0, tmp_obj);
647 __ load_parameter(1, tmp_addr);
648 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
649 __ pop(tmp_addr);
650 __ epilogue();
651 }
652
653 #undef __
654
655 #endif // COMPILER1
656
657 #ifdef COMPILER2
658
659 #undef __
660 #define __ masm->
661
662 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
663 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
664 if (narrow) {
665 __ movl(dst, src);
666 } else {
667 __ movq(dst, src);
668 }
669
670 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, noreg, noreg, narrow);
671 }
672
673 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
674 Address dst, bool dst_narrow,
675 Register src, bool src_narrow,
676 Register tmp) {
677
678 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp, noreg, noreg, dst_narrow);
679
680 // Need to encode into tmp, because we cannot clobber src.
681 if (dst_narrow && !src_narrow) {
682 __ movq(tmp, src);
683 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
684 __ encode_heap_oop(tmp);
685 } else {
686 __ encode_heap_oop_not_null(tmp);
687 }
688 src = tmp;
689 }
690
691 // Do the actual store
692 if (dst_narrow) {
693 __ movl(dst, src);
694 } else {
695 __ movq(dst, src);
696 }
697
698 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp, noreg);
699 }
700
701 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
702 Register res, Address addr,
703 Register oldval, Register newval, Register tmp,
704 bool narrow) {
705
706 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
707
708 // Oldval and newval cannot be clobbered by aliasing with tmp.
709 assert_different_registers(oldval, tmp);
710 assert_different_registers(newval, tmp);
711
712 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
713
714 // CAS!
715 __ lock();
716 if (narrow) {
717 __ cmpxchgl(newval, addr);
718 } else {
719 __ cmpxchgptr(newval, addr);
720 }
721
722 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
723 if (res != noreg) {
724 __ setcc(Assembler::equal, res);
725 }
726
727 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
728 }
729
730 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
731 assert_different_registers(newval, tmp);
732
733 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
734
735 if (narrow) {
736 __ xchgl(newval, addr);
737 } else {
738 __ xchgq(newval, addr);
739 }
740
741 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
742 }
743
744 #undef __
745 #define __ masm.
746
747 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2) {
748 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
749
750 __ lea(tmp1, addr);
751 __ shrptr(tmp1, CardTable::card_shift());
752 __ addptr(tmp1, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
753 Address card_address(tmp1, 0);
754
755 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
756 Label L_done;
757 if (UseCondCardMark) {
758 __ cmpb(card_address, 0);
759 __ jccb(Assembler::equal, L_done);
760 }
761 if (UseCompressedOops && CompressedOops::base() == nullptr) {
762 __ movb(card_address, r12);
763 } else {
764 __ movb(card_address, 0);
765 }
766 __ bind(L_done);
767 }
768
769 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
770 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
771
772 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
773 __ cmpb(gc_state_fast, 0);
774 __ jcc(Assembler::notEqual, *entry());
775 __ bind(*continuation());
776 }
777
778 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
779 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
780 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
781
782 // On x86, there is a significant penalty with unaligned branch target, for example
783 // when the target instruction straggles the fetch line. It makes (performance) sense
784 // to spend some code size to align the target better.
785 __ align(16);
786 __ bind(*entry());
787
788 // If we need to load ourselves, do it here.
789 if (_do_load) {
790 if (_narrow) {
791 __ movl(_obj, _addr);
792 } else {
793 __ movq(_obj, _addr);
794 }
795 }
796
797 // If the object is null, there is no point in applying barriers.
798 maybe_far_jump_if_zero(masm, _obj);
799
800 // We need to make sure that loads done by callers survive across slow-path calls.
801 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
802 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
803 if (!_do_load || needs_both_barriers) {
804 preserve(_obj);
805 }
806
807 // Go for barriers. Barriers can return straight to continuation, as long
808 // as another barrier is not needed.
809 if (needs_both_barriers) {
810 keepalive(masm, nullptr);
811 lrb(masm);
812 } else if (_needs_keep_alive_barrier) {
813 keepalive(masm, continuation());
814 } else if (_needs_load_ref_barrier) {
815 lrb(masm);
816 } else {
817 ShouldNotReachHere();
818 }
819 }
820
821 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
822 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
823 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
824 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
825
826 Label L_through, L_pop_and_slow;
827
828 // If another barrier is enabled as well, do a runtime check for a specific barrier.
829 if (_needs_load_ref_barrier) {
830 assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
831 __ cmpb(gc_state_fast, 0);
832 __ jcc(Assembler::equal, L_through);
833 }
834
835 // Need temp to work, allocate one now.
836 bool tmp_live;
837 Register tmp = select_temp_register(tmp_live);
838 if (tmp_live) {
839 __ push(tmp);
840 }
841
842 // Fast-path: put object into buffer.
843 // If buffer is already full, go slow.
844 __ movptr(tmp, index);
845 __ subptr(tmp, wordSize);
846 __ jccb(Assembler::below, L_pop_and_slow);
847 __ movptr(index, tmp);
848 __ addptr(tmp, buffer);
849
850 // Store the object in queue.
851 // If object is narrow, we need to decode it before inserting.
852 // We can skip the re-encoding if we know that object is not preserved.
853 if (_narrow) {
854 __ decode_heap_oop_not_null(_obj);
855 }
856 __ movptr(Address(tmp, 0), _obj);
857 if (_narrow && is_preserved(_obj)) {
858 __ encode_heap_oop_not_null(_obj);
859 }
860
861 // Fast-path exits here.
862 if (tmp_live) {
863 __ pop(tmp);
864 }
865
866 if (L_done != nullptr) {
867 __ jmp(*L_done);
868 } else {
869 __ jmp(L_through);
870 }
871
872 // Slow-path: call runtime to handle.
873 // Need to pop tmp immediately for stack to remain aligned.
874 __ bind(L_pop_and_slow);
875 if (tmp_live) {
876 __ pop(tmp);
877 }
878 {
879 SaveLiveRegisters slr(&masm, this);
880
881 // Shuffle in the arguments. The end result should be:
882 // c_rarg0 <-- obj
883 if (c_rarg0 != _obj) {
884 __ mov(c_rarg0, _obj);
885 }
886
887 // Go to runtime and handle the rest there.
888 // Use rax as scratch, as it will be saved if live.
889 __ call(RuntimeAddress(keepalive_runtime_entry_addr()), rax);
890 }
891 if (L_done != nullptr) {
892 __ jmp(*L_done);
893 } else {
894 __ bind(L_through);
895 }
896 }
897
898 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
899 Label L_pop_and_slow, L_slow;
900
901 // If another barrier is enabled as well, do a runtime check for a specific barrier.
902 if (_needs_keep_alive_barrier) {
903 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
904 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
905 __ cmpb(gc_state_fast, 0);
906 __ jcc(Assembler::equal, *continuation());
907 }
908
909 // If weak references are being processed, weak/phantom loads need to go slow,
910 // regardless of their cset status.
911 if (_needs_load_ref_weak_barrier) {
912 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
913 __ cmpb(gc_state_fast, 0);
914 __ jccb(Assembler::notEqual, L_slow);
915 }
916
917 bool is_aot = AOTCodeCache::is_on_for_dump();
918
919 // Need temp to work, allocate one now.
920 bool tmp_live;
921 Register tmp = select_temp_register(tmp_live, /* skip_reg1 = */ is_aot ? rcx : noreg);
922 if (tmp_live) {
923 __ push(tmp);
924 }
925
926 // Compute the cset bitmap index
927 if (_narrow) {
928 __ decode_heap_oop_not_null(tmp, _obj);
929 } else {
930 __ movptr(tmp, _obj);
931 }
932
933 Address cset_addr_arg;
934 intptr_t cset_addr = reinterpret_cast<intptr_t>(ShenandoahHeap::in_cset_fast_test_addr());
935 if (!is_aot && cset_addr < INT32_MAX) {
936 // Cset bitmap is at easily encodeable address. Just use it as displacement.
937 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
938 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr));
939 } else {
940 bool tmp2_live;
941 Register tmp2 = select_temp_register(tmp2_live, /* skip_reg1 = */ tmp, /* skip_reg2 = */ is_aot ? rcx : noreg);
942 if (tmp2_live) {
943 __ push(tmp2);
944 }
945 if (is_aot) {
946 // Generating AOT code, pull the cset bitmap and region shift from AOT table.
947 assert_different_registers(tmp, tmp2, rcx);
948 __ push(rcx);
949 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
950 __ movl(rcx, Address(rcx));
951 __ shrptr(tmp);
952 __ pop(rcx);
953 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
954 __ addptr(tmp, Address(tmp2));
955 } else {
956 // Cset bitmap is far away. Add its address fully.
957 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
958 __ movptr(tmp2, cset_addr);
959 __ addptr(tmp, tmp2);
960 }
961 if (tmp2_live) {
962 __ pop(tmp2);
963 }
964 cset_addr_arg = Address(tmp, 0);
965 }
966
967 // Cset-check. Fall-through to slow if in collection set.
968 __ cmpb(cset_addr_arg, 0);
969 if (tmp_live) {
970 __ jccb(Assembler::notEqual, L_pop_and_slow);
971 __ pop(tmp);
972 __ jmp(*continuation());
973 } else {
974 // Nothing else to do, jump back
975 __ jcc(Assembler::equal, *continuation());
976 }
977
978 // Slow path
979 __ bind(L_pop_and_slow);
980 // Need to pop tmp immediately for stack to remain aligned.
981 if (tmp_live) {
982 __ pop(tmp);
983 }
984 __ bind(L_slow);
985
986 // Obj is the result, need to temporarily stop preserving it.
987 bool is_obj_preserved = is_preserved(_obj);
988 if (is_obj_preserved) {
989 dont_preserve(_obj);
990 }
991 {
992 SaveLiveRegisters slr(&masm, this);
993
994 assert_different_registers(rax, c_rarg0, c_rarg1);
995
996 // Shuffle in the arguments. The end result should be:
997 // c_rarg0 <-- obj
998 // c_rarg1 <-- lea(addr)
999 if (_obj == c_rarg0) {
1000 __ lea(c_rarg1, _addr);
1001 } else if (_obj == c_rarg1) {
1002 // Set up arguments in reverse, and then flip them
1003 __ lea(c_rarg0, _addr);
1004 __ xchgptr(c_rarg0, c_rarg1);
1005 } else {
1006 assert_different_registers(_obj, c_rarg0, c_rarg1);
1007 __ lea(c_rarg1, _addr);
1008 __ movptr(c_rarg0, _obj);
1009 }
1010
1011 // Go to runtime and handle the rest there.
1012 // Use rax as scratch, as it will be clobbered by result anyway.
1013 __ call(RuntimeAddress(lrb_runtime_entry_addr()), rax);
1014
1015 // Save the result where needed.
1016 if (_narrow) {
1017 __ movl(_obj, rax);
1018 } else if (_obj != rax) {
1019 __ movptr(_obj, rax);
1020 }
1021 }
1022 if (is_obj_preserved) {
1023 preserve(_obj);
1024 }
1025
1026 __ jmp(*continuation());
1027 }
1028
1029 int ShenandoahBarrierStubC2::available_gp_registers() {
1030 return Register::available_gp_registers();
1031 }
1032
1033 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
1034 return r == rsp || r == rbp || r == r12_heapbase || r == r15_thread;
1035 }
1036
1037 void ShenandoahBarrierStubC2::post_init() {
1038 // Do nothing.
1039 }
1040
1041 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
1042 if (_narrow) {
1043 __ testl(reg, reg);
1044 } else {
1045 __ testq(reg, reg);
1046 }
1047 __ jcc(Assembler::zero, *continuation());
1048 }
1049
1050 #endif // COMPILER2