1 /*
2 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
4 * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. 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/interp_masm.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "runtime/sharedRuntime.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 #include "opto/output.hpp"
47 #endif
48
49 #define __ masm->
50
51 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
52 Register src, Register dst, Register count, RegSet saved_regs) {
53 if (is_oop) {
54 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
55 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
56
57 Label done;
58
59 // Avoid calling runtime if count == 0
60 __ beqz(count, done);
61
62 // Is GC active?
63 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
64 assert_different_registers(src, dst, count, t0);
65
66 assert(!saved_regs.contains(t0), "Sanity: about to clobber t0");
67
68 __ lbu(t0, gc_state);
69 if (ShenandoahSATBBarrier && dest_uninitialized) {
70 __ test_bit(t0, t0, ShenandoahHeap::HAS_FORWARDED_BITPOS);
71 __ beqz(t0, done);
72 } else {
73 __ andi(t0, t0, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
74 __ beqz(t0, done);
75 }
76
77 __ push_call_clobbered_registers();
78 // If arguments are not in proper places, shuffle them.
79 // Doing this via the stack is the most straight-forward way to avoid
80 // accidentally smashing any register.
81 if (c_rarg0 != src || c_rarg1 != dst || c_rarg2 != count) {
82 __ push_reg(RegSet::of(src), sp);
83 __ push_reg(RegSet::of(dst), sp);
84 __ push_reg(RegSet::of(count), sp);
85 __ pop_reg(RegSet::of(c_rarg2), sp);
86 __ pop_reg(RegSet::of(c_rarg1), sp);
87 __ pop_reg(RegSet::of(c_rarg0), sp);
88 }
89 address target = nullptr;
90 if (UseCompressedOops) {
91 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop);
92 } else {
93 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop);
94 }
95 __ call_VM_leaf(target, 3);
96 __ pop_call_clobbered_registers();
97 __ bind(done);
98 }
99 }
100 }
101
102 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
103 Register start, Register count, Register tmp) {
104 if (ShenandoahCardBarrier && is_oop) {
105 gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp);
106 }
107 }
108
109 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
110 Register obj,
111 Register pre_val,
112 Register thread,
113 Register tmp1,
114 Register tmp2) {
115 assert(ShenandoahSATBBarrier, "Should be checked by caller");
116 assert(thread == xthread, "must be");
117
118 Label done;
119 Label runtime;
120
121 assert_different_registers(obj, pre_val, tmp1, tmp2);
122 assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register");
123
124 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
125 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
126
127 // Is marking active?
128 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
129 __ lbu(t1, gc_state);
130 __ test_bit(t1, t1, ShenandoahHeap::MARKING_BITPOS);
131 __ beqz(t1, done);
132
133 // Do we need to load the previous value?
134 if (obj != noreg) {
135 if (UseCompressedOops) {
136 __ lwu(pre_val, Address(obj, 0));
137 __ decode_heap_oop(pre_val);
138 } else {
139 __ ld(pre_val, Address(obj, 0));
140 }
141 }
142
143 // Is the previous value null?
144 __ beqz(pre_val, done);
145
146 // Can we store original value in the thread's buffer?
147 // Is index == 0?
148 // (The index field is typed as size_t.)
149 __ ld(tmp1, index); // tmp := *index_adr
150 __ beqz(tmp1, runtime); // tmp == 0? If yes, goto runtime
151
152 __ subi(tmp1, tmp1, wordSize); // tmp := tmp - wordSize
153 __ sd(tmp1, index); // *index_adr := tmp
154 __ ld(tmp2, buffer);
155 __ add(tmp1, tmp1, tmp2); // tmp := tmp + *buffer_adr
156
157 // Record the previous value
158 __ sd(pre_val, Address(tmp1, 0));
159 __ j(done);
160
161 // Slow-path call.
162 __ bind(runtime);
163 __ enter();
164 __ push_call_clobbered_registers();
165 if (c_rarg0 != pre_val) {
166 __ mv(c_rarg0, pre_val);
167 }
168 // Calling with super_call_VM_leaf with c_rarg0 bypasses interpreter checks and avoids any moves.
169 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
170 __ pop_call_clobbered_registers();
171 __ leave();
172
173 __ bind(done);
174 }
175
176 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm,
177 Register dst,
178 Address load_addr,
179 DecoratorSet decorators) {
180 assert(ShenandoahLoadRefBarrier, "Should be enabled");
181 assert(dst != t1 && load_addr.base() != t1, "need t1");
182 assert_different_registers(load_addr.base(), t0, t1);
183
184 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
185 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
186 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
187 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
188 bool is_narrow = UseCompressedOops && !is_native;
189
190 Label heap_stable, not_cset;
191 Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
192 __ lbu(t1, gc_state);
193
194 // Check for heap stability
195 if (is_strong) {
196 __ test_bit(t1, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
197 __ beqz(t1, heap_stable);
198 } else {
199 Label lrb;
200 __ test_bit(t0, t1, ShenandoahHeap::WEAK_ROOTS_BITPOS);
201 __ bnez(t0, lrb);
202 __ test_bit(t0, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
203 __ beqz(t0, heap_stable);
204 __ bind(lrb);
205 }
206
207 // use x11 for load address
208 Register result_dst = dst;
209 if (dst == x11) {
210 __ mv(t1, dst);
211 dst = t1;
212 }
213
214 // Save x10 and x11, unless it is an output register
215 RegSet saved_regs = RegSet::of(x10, x11) - result_dst;
216 __ push_reg(saved_regs, sp);
217 __ la(x11, load_addr);
218 __ mv(x10, dst);
219
220 // Test for in-cset
221 if (is_strong) {
222 __ mv(t1, ShenandoahHeap::in_cset_fast_test_addr());
223 __ srli(t0, x10, ShenandoahHeapRegion::region_size_bytes_shift_jint());
224 __ add(t1, t1, t0);
225 __ lbu(t1, Address(t1));
226 __ test_bit(t0, t1, 0);
227 __ beqz(t0, not_cset);
228 }
229
230 // Slow-path call
231 __ enter();
232 __ push_call_clobbered_registers();
233 address target = nullptr;
234 if (is_strong) {
235 if (is_narrow) {
236 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
237 } else {
238 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
239 }
240 } else if (is_weak) {
241 if (is_narrow) {
242 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
243 } else {
244 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
245 }
246 } else {
247 assert(is_phantom, "only remaining strength");
248 assert(!is_narrow, "phantom access cannot be narrow");
249 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
250 }
251 // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
252 __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
253 __ mv(t0, x10);
254 __ pop_call_clobbered_registers();
255 __ mv(x10, t0);
256 __ leave();
257
258 __ bind(not_cset);
259 __ mv(result_dst, x10);
260 __ pop_reg(saved_regs, sp);
261
262 __ bind(heap_stable);
263 }
264
265 //
266 // Arguments:
267 //
268 // Inputs:
269 // src: oop location to load from, might be clobbered
270 //
271 // Output:
272 // dst: oop loaded from src location
273 //
274 // Kill:
275 // x30 (tmp reg)
276 //
277 // Alias:
278 // dst: x30 (might use x30 as temporary output register to avoid clobbering src)
279 //
280 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm,
281 DecoratorSet decorators,
282 BasicType type,
283 Register dst,
284 Address src,
285 Register tmp1,
286 Register tmp2) {
287 // 1: non-reference load, no additional barrier is needed
288 if (!is_reference_type(type)) {
289 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
290 return;
291 }
292
293 // 2: load a reference from src location and apply LRB if needed
294 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
295 Register result_dst = dst;
296
297 // Preserve src location for LRB
298 RegSet saved_regs;
299 if (dst == src.base()) {
300 dst = (src.base() == x28) ? x29 : x28;
301 saved_regs = RegSet::of(dst);
302 __ push_reg(saved_regs, sp);
303 }
304 assert_different_registers(dst, src.base());
305
306 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
307
308 load_reference_barrier(masm, dst, src, decorators);
309
310 if (dst != result_dst) {
311 __ mv(result_dst, dst);
312 dst = result_dst;
313 }
314
315 if (saved_regs.bits() != 0) {
316 __ pop_reg(saved_regs, sp);
317 }
318 } else {
319 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
320 }
321
322 // 3: apply keep-alive barrier if needed
323 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
324 satb_barrier(masm /* masm */,
325 noreg /* obj */,
326 dst /* pre_val */,
327 xthread /* thread */,
328 tmp1 /* tmp1 */,
329 tmp2 /* tmp2 */);
330 }
331 }
332
333 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
334 assert(ShenandoahCardBarrier, "Should have been checked by caller");
335
336 __ srli(obj, obj, CardTable::card_shift());
337
338 assert(CardTable::dirty_card_val() == 0, "must be");
339
340 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
341 __ ld(t1, curr_ct_holder_addr);
342 __ add(t1, obj, t1);
343
344 if (UseCondCardMark) {
345 Label L_already_dirty;
346 __ lbu(t0, Address(t1));
347 __ beqz(t0, L_already_dirty);
348 __ sb(zr, Address(t1));
349 __ bind(L_already_dirty);
350 } else {
351 __ sb(zr, Address(t1));
352 }
353 }
354
355 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
356 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
357 // 1: non-reference types require no barriers
358 if (!is_reference_type(type)) {
359 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
360 return;
361 }
362
363 // Flatten object address right away for simplicity: likely needed by barriers
364 if (dst.offset() == 0) {
365 if (dst.base() != tmp3) {
366 __ mv(tmp3, dst.base());
367 }
368 } else {
369 __ la(tmp3, dst);
370 }
371
372 // 2: pre-barrier: SATB needs the previous value
373 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
374 satb_barrier(masm,
375 tmp3 /* obj */,
376 tmp2 /* pre_val */,
377 xthread /* thread */,
378 tmp1 /* tmp */,
379 t0 /* tmp2 */);
380 }
381
382 // Store!
383 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
384
385 // 3: post-barrier: card barrier needs store address
386 bool storing_non_null = (val != noreg);
387 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
388 card_barrier(masm, tmp3);
389 }
390 }
391
392 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
393 Register obj, Register tmp, Label& slowpath) {
394 Label done;
395 // Resolve jobject
396 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
397
398 // Check for null.
399 __ beqz(obj, done);
400
401 assert(obj != t1, "need t1");
402 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
403 __ lbu(t1, gc_state);
404
405 // Check for heap in evacuation phase
406 __ test_bit(t0, t1, ShenandoahHeap::EVACUATION_BITPOS);
407 __ bnez(t0, slowpath);
408
409 __ bind(done);
410 }
411
412 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler *masm, Register weak_handle,
413 Register obj, Register tmp, Label& slow_path) {
414 assert_different_registers(weak_handle, tmp, noreg);
415 assert_different_registers(obj, tmp, noreg);
416
417
418 Label done;
419
420 // Peek weak handle using the standard implementation.
421 BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, tmp, slow_path);
422
423 // Check if the reference is null, and if it is, take the fast path.
424 __ beqz(obj, done);
425
426 Address gc_state(xthread, ShenandoahThreadLocalData::gc_state_offset());
427 __ lbu(tmp, gc_state);
428
429 // Check if the heap is under weak-reference/roots processing, in
430 // which case we need to take the slow path.
431 __ test_bit(tmp, tmp, ShenandoahHeap::WEAK_ROOTS_BITPOS);
432 __ bnez(tmp, slow_path);
433 __ bind(done);
434 }
435
436 void ShenandoahBarrierSetAssembler::check_oop(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& L_error) {
437 // Check if the oop is in the right area of memory
438 __ mv(tmp2, (intptr_t) Universe::verify_oop_mask());
439 __ andr(tmp1, obj, tmp2);
440 __ mv(tmp2, (intptr_t) Universe::verify_oop_bits());
441
442 // Compare tmp1 and tmp2.
443 __ bne(tmp1, tmp2, L_error);
444
445 // This routine is sometimes called before applying GC barriers.
446 // With +COH, loading the klass may end up loading forwarding pointer instead.
447 Label L_skip;
448 if (UseCompactObjectHeaders) {
449 Address gc_state(xthread, ShenandoahThreadLocalData::gc_state_offset());
450 __ lbu(tmp1, gc_state);
451 __ test_bit(tmp1, tmp1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
452 __ bnez(tmp1, L_skip);
453 }
454
455 // Make sure klass is 'reasonable', which is not zero.
456 __ load_narrow_klass(tmp1, obj);
457 __ beqz(tmp1, L_error);
458
459 __ bind(L_skip);
460 }
461
462 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
463 Register start, Register count, Register tmp) {
464 assert(ShenandoahCardBarrier, "Did you mean to enable ShenandoahCardBarrier?");
465
466 Label L_loop, L_done;
467 const Register end = count;
468
469 // Zero count? Nothing to do.
470 __ beqz(count, L_done);
471
472 // end = start + count << LogBytesPerHeapOop
473 // last element address to make inclusive
474 __ shadd(end, count, start, tmp, LogBytesPerHeapOop);
475 __ subi(end, end, BytesPerHeapOop);
476 __ srli(start, start, CardTable::card_shift());
477 __ srli(end, end, CardTable::card_shift());
478
479 // number of bytes to copy
480 __ sub(count, end, start);
481
482 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
483 __ ld(tmp, curr_ct_holder_addr);
484 __ add(start, start, tmp);
485
486 __ bind(L_loop);
487 __ add(tmp, start, count);
488 __ sb(zr, Address(tmp));
489 __ subi(count, count, 1);
490 __ bgez(count, L_loop);
491 __ bind(L_done);
492 }
493
494 #undef __
495
496 #ifdef COMPILER1
497
498 #define __ ce->masm()->
499
500 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
501 __ bind(*stub->entry());
502
503 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
504
505 Register obj = stub->obj()->as_register();
506
507 if (stub->do_load()) {
508 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, false /* wide */);
509 }
510 __ beqz(obj, *stub->continuation(), /* is_far */ true);
511
512 ce->store_parameter(obj, 0);
513 __ far_call(RuntimeAddress(bs->keepalive_barrier_stub()));
514 __ j(*stub->continuation());
515 }
516
517 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
518 __ bind(*stub->entry());
519
520 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*) BarrierSet::barrier_set()->barrier_set_c1();
521
522 Register obj = stub->obj()->as_register();
523 Register addr = stub->addr()->as_pointer_register();
524 Register slow_result = stub->slow_result()->as_register();
525 assert_different_registers(obj, addr, slow_result);
526 assert(slow_result == x10, "C1 must know about our slow call result register");
527
528 ce->store_parameter(obj, 0);
529 ce->store_parameter(addr, 1);
530 __ far_call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
531 if (obj != slow_result) {
532 __ mv(obj, slow_result);
533 }
534
535 __ j(*stub->continuation());
536 }
537
538 #undef __
539
540 #define __ sasm->
541
542 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
543 __ prologue("shenandoah_keepalive_barrier", false);
544 const Register tmp_obj = x10;
545 const Register tmp1 = x11;
546 const Register tmp2 = x12;
547 __ push_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
548 __ load_parameter(0, tmp_obj);
549 satb_barrier(sasm, noreg, tmp_obj, xthread, tmp1, tmp2);
550 __ pop_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
551 __ epilogue();
552 }
553
554 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
555 __ prologue("shenandoah_load_reference_barrier", false);
556 const Register tmp_obj = x10;
557 const Register tmp_addr = x11;
558 __ push_reg(RegSet::of(tmp_addr), sp);
559 __ load_parameter(0, tmp_obj);
560 __ load_parameter(1, tmp_addr);
561 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
562 __ pop_reg(RegSet::of(tmp_addr), sp);
563 __ epilogue();
564 }
565
566 #undef __
567
568 #endif // COMPILER1
569
570 #ifdef COMPILER2
571
572 #undef __
573 #define __ masm->
574
575 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, Register tmp1, Register tmp2, bool is_narrow) {
576 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
577 if (is_narrow) {
578 __ lwu(dst, src);
579 } else {
580 __ ld(dst, src);
581 }
582
583 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, tmp1, tmp2, is_narrow);
584 }
585
586 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm, Address dst, bool dst_narrow,
587 Register src, bool src_narrow, Register tmp1, Register tmp2, Register tmp3) {
588
589 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp1, tmp2, tmp3, dst_narrow);
590
591 // Do the actual store
592 if (dst_narrow) {
593 if (!src_narrow) {
594 // Need to encode into tmp, because we cannot clobber src.
595 assert(tmp1 != noreg, "need temp register");
596 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
597 __ encode_heap_oop(tmp1, src);
598 } else {
599 __ encode_heap_oop_not_null(tmp1, src);
600 }
601 src = tmp1;
602 }
603 __ sw(src, dst);
604 } else {
605 __ sd(src, dst);
606 }
607
608 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp2, tmp3);
609 }
610
611 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm, Register res, Register addr,
612 Register oldval, Register newval, Register tmp1, Register tmp2, Register tmp3, bool exchange, bool narrow, bool is_acquire) {
613 const Assembler::Aqrl acquire = is_acquire ? Assembler::aq : Assembler::relaxed;
614 const Assembler::Aqrl release = Assembler::rl;
615 const Assembler::operand_size size = narrow ? Assembler::uint32 : Assembler::int64;
616
617 ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr), tmp1, tmp2, tmp3, narrow);
618
619 // CAS!
620 __ cmpxchg(addr, oldval, newval, size, acquire, release, /* result */ res, !exchange /* result_as_bool */);
621
622 ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
623 }
624
625 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register preval,
626 Register newval, Register addr, Register tmp1, Register tmp2, Register tmp3, bool is_acquire) {
627 const bool is_narrow = node->bottom_type()->isa_narrowoop();
628
629 ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr, 0), tmp1, tmp2, tmp3, is_narrow);
630
631 if (is_narrow) {
632 if (is_acquire) {
633 __ atomic_xchgalwu(preval, newval, addr);
634 } else {
635 __ atomic_xchgwu(preval, newval, addr);
636 }
637 } else {
638 if (is_acquire) {
639 __ atomic_xchgal(preval, newval, addr);
640 } else {
641 __ atomic_xchg(preval, newval, addr);
642 }
643 }
644
645 ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
646 }
647
648 #undef __
649 #define __ masm.
650
651 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address address, Register tmp1, Register tmp2) {
652 assert(CardTable::dirty_card_val() == 0, "must be");
653 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
654
655 // tmp1 = card table base (holder)
656 Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
657 __ ld(tmp1, curr_ct_holder_addr);
658
659 // tmp1 = effective address
660 __ la(tmp2, address);
661
662 // tmp2 = &card_table[ addr >> CardTable::card_shift() ] ; card index
663 __ srli(tmp2, tmp2, CardTable::card_shift());
664 __ add(tmp2, tmp2, tmp1);
665
666 if (UseCondCardMark) {
667 Label L_already_dirty;
668 __ lbu(tmp1, Address(tmp2));
669 __ beqz(tmp1, L_already_dirty);
670 __ sb(zr, Address(tmp2));
671 __ bind(L_already_dirty);
672 } else {
673 __ sb(zr, Address(tmp2));
674 }
675 }
676
677 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
678 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
679
680 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
681 __ lbu(tmp, gc_state_fast);
682 __ beqz(tmp, *continuation());
683 __ j(*entry());
684
685 // This is were the slowpath stub will return to or the code above will
686 // jump to if the checks are false
687 __ bind(*continuation());
688 }
689
690 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
691 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
692 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
693
694 __ bind(*entry());
695
696 // If we need to load ourselves, do it here.
697 if (_do_load) {
698 if (_narrow) {
699 __ lwu(_obj, _addr);
700 } else {
701 __ ld(_obj, _addr);
702 }
703 }
704
705 // If the object is null, there is no point in applying barriers.
706 maybe_far_jump_if_zero(masm, _obj);
707
708 // We need to make sure that loads done by callers survive across slow-path calls.
709 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
710 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
711 if (!_do_load || needs_both_barriers) {
712 preserve(_obj);
713 }
714
715 // Go for barriers. Barriers can return straight to continuation, as long
716 // as another barrier is not needed and we can reach the fastpath.
717 if (needs_both_barriers) {
718 keepalive(masm, nullptr);
719 lrb(masm);
720 } else if (_needs_keep_alive_barrier) {
721 keepalive(masm, continuation());
722 } else if (_needs_load_ref_barrier) {
723 lrb(masm);
724 } else {
725 ShouldNotReachHere();
726 }
727 }
728
729 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
730 Label L_short_jump;
731 __ bnez(reg, L_short_jump);
732 __ j(*continuation());
733 __ bind(L_short_jump);
734 }
735
736 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
737 Address index(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
738 Address buffer(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
739 Label L_through, L_slowpath;
740
741 // If another barrier is enabled as well, do a runtime check for a specific barrier.
742 if (_needs_load_ref_barrier) {
743 assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
744 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
745 __ lbu(_tmp1, gc_state_fast);
746 __ beqz(_tmp1, L_through);
747 }
748
749 // Fast-path: put object into buffer.
750 // If buffer is already full, go slow.
751 __ ld(_tmp1, index);
752 __ beqz(_tmp1, L_slowpath);
753 __ subi(_tmp1, _tmp1, wordSize);
754 __ sd(_tmp1, index);
755 __ ld(_tmp2, buffer);
756
757 // Store the object in queue.
758 // If object is narrow, we need to decode it before inserting.
759 __ add(_tmp1, _tmp1, _tmp2);
760 if (_narrow) {
761 __ decode_heap_oop_not_null(_tmp2, _obj);
762 __ sd(_tmp2, Address(_tmp1));
763 } else {
764 __ sd(_obj, Address(_tmp1));
765 }
766
767 // Fast-path exits here.
768 if (L_done != nullptr) {
769 __ j(*L_done);
770 } else {
771 __ j(L_through);
772 }
773
774 // Slow-path: call runtime to handle.
775 __ bind(L_slowpath);
776
777 {
778 SaveLiveRegisters slr(&masm, this);
779
780 // Go to runtime and handle the rest there.
781 __ mv(c_rarg0, _obj);
782 __ la(ra, RuntimeAddress(keepalive_runtime_entry_addr()));
783 __ jalr(ra);
784 }
785 if (L_done != nullptr) {
786 __ j(*L_done);
787 } else {
788 __ bind(L_through);
789 }
790 }
791
792 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
793 Label L_slow;
794
795 // If another barrier is enabled as well, do a runtime check for a specific barrier.
796 if (_needs_keep_alive_barrier) {
797 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
798 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
799 __ lbu(_tmp1, gc_state_fast);
800 maybe_far_jump_if_zero(masm, _tmp1);
801 }
802
803 // If weak references are being processed, weak/phantom loads need to go slow,
804 // regardless of their cset status.
805 if (_needs_load_ref_weak_barrier) {
806 Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
807 __ lbu(_tmp1, gc_state_fast);
808 __ bnez(_tmp1, L_slow);
809 }
810
811 // Cset-check. Fall-through to slow if in collection set.
812 if (_narrow) {
813 __ decode_heap_oop_not_null(_tmp2, _obj);
814 } else {
815 __ mv(_tmp2, _obj);
816 }
817
818 __ mv(_tmp1, ShenandoahHeap::in_cset_fast_test_addr());
819 __ srli(_tmp2, _tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
820 __ add(_tmp1, _tmp1, _tmp2);
821 __ lbu(_tmp1, Address(_tmp1, 0));
822 maybe_far_jump_if_zero(masm, _tmp1);
823
824 // Slow path
825 __ bind(L_slow);
826
827 // Obj is the result, need to temporarily stop preserving it.
828 bool is_obj_preserved = is_preserved(_obj);
829 if (is_obj_preserved) {
830 dont_preserve(_obj);
831 }
832 {
833 SaveLiveRegisters slr(&masm, this);
834
835 // Shuffle in the arguments. The end result should be:
836 // c_rarg0 <- obj
837 // c_rarg1 <- lea(addr)
838 if (c_rarg0 == _obj) {
839 __ la(c_rarg1, _addr);
840 } else if (c_rarg1 == _obj) {
841 __ mv(_tmp1, c_rarg1);
842 __ la(c_rarg1, _addr);
843 __ mv(c_rarg0, _tmp1);
844 } else {
845 assert_different_registers(c_rarg1, _obj);
846 __ la(c_rarg1, _addr);
847 __ mv(c_rarg0, _obj);
848 }
849
850 // Go to runtime and handle the rest there.
851 __ la(ra, RuntimeAddress(lrb_runtime_entry_addr()));
852 __ jalr(ra);
853
854 // Save the result where needed. Narrow entries return narrowOop (32 bits)
855 // we need to zero the upper 32 bits of x10.
856 if (_narrow) {
857 __ zext_w(_obj, x10);
858 } else {
859 __ mv(_obj, x10);
860 }
861 }
862 if (is_obj_preserved) {
863 preserve(_obj);
864 }
865
866 __ j(*continuation());
867 }
868
869 int ShenandoahBarrierStubC2::available_gp_registers() {
870 Unimplemented(); // Not used
871 return 0;
872 }
873
874 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
875 Unimplemented(); // Not used
876 return true;
877 }
878
879 void ShenandoahBarrierStubC2::post_init() {
880 // Do nothing.
881 }
882
883 #endif // COMPILER2