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
158 if (obj != noreg) {
159 assert_different_registers(obj, pre_val, tmp);
160 assert(pre_val != rax, "check this code");
161 }
162
163 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
164 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
165
166 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
167 __ testb(gc_state, ShenandoahHeap::MARKING);
168 __ jcc(Assembler::zero, done);
169
170 // Do we need to load the previous value?
171 if (obj != noreg) {
172 if (UseCompressedOops) {
173 __ movl(pre_val, Address(obj, 0));
174 __ decode_heap_oop(pre_val);
175 } else {
176 __ movq(pre_val, Address(obj, 0));
177 }
178 }
179
180 // Is the previous value null?
181 __ cmpptr(pre_val, NULL_WORD);
182 __ jcc(Assembler::equal, done);
183
184 // Can we store original value in the thread's buffer?
185 // Is index == 0?
186 // (The index field is typed as size_t.)
187
188 __ movptr(tmp, index); // tmp := *index_adr
189 __ cmpptr(tmp, 0); // tmp == 0?
190 __ jcc(Assembler::equal, runtime); // If yes, goto runtime
191
192 __ subptr(tmp, wordSize); // tmp := tmp - wordSize
193 __ movptr(index, tmp); // *index_adr := tmp
194 __ addptr(tmp, buffer); // tmp := tmp + *buffer_adr
195
196 // Record the previous value
197 __ movptr(Address(tmp, 0), pre_val);
198 __ jmp(done);
199
200 __ bind(runtime);
201
202 // Slow-path call.
203 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
204 __ enter();
205 __ push_call_clobbered_registers(/* save_fpu = */ true);
206
207 assert(thread != c_rarg0, "smashed arg");
208 if (c_rarg0 != pre_val) {
209 __ mov(c_rarg0, pre_val);
210 }
211
212 // Calling with super_call_VM_leaf with c_rarg0 bypasses interpreter checks and avoids any moves.
213 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
214
215 __ pop_call_clobbered_registers(/* restore_fpu = */ true);
216 __ leave();
217
218 __ bind(done);
219 }
220
221 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address src, DecoratorSet decorators) {
222 assert(ShenandoahLoadRefBarrier, "Should be enabled");
223
224 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
225 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
226 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
227 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
228 bool is_narrow = UseCompressedOops && !is_native;
229
230 Label heap_stable, not_cset;
231
232 __ block_comment("load_reference_barrier { ");
233
234 // Check if GC is active
235 Register thread = r15_thread;
236
237 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
238 int flags = ShenandoahHeap::HAS_FORWARDED;
239 if (!is_strong) {
240 flags |= ShenandoahHeap::WEAK_ROOTS;
241 }
242 __ testb(gc_state, flags);
243 __ jcc(Assembler::zero, heap_stable);
244
245 Register tmp1 = noreg, tmp2 = noreg;
246 if (is_strong) {
247 // Test for object in cset
248 // Allocate temporary registers
249 for (int i = 0; i < Register::available_gp_registers(); i++) {
250 Register r = as_Register(i);
251 if (r != rsp && r != rbp && r != rcx && r != dst && r != src.base() && r != src.index() ) {
252 if (tmp1 == noreg) {
253 tmp1 = r;
254 } else {
255 tmp2 = r;
256 break;
257 }
258 }
259 }
260 assert(tmp1 != noreg, "tmp1 allocated");
261 assert(tmp2 != noreg, "tmp2 allocated");
262 assert_different_registers(tmp1, tmp2, src.base(), src.index());
263 assert_different_registers(tmp1, tmp2, dst);
264
265 __ push(tmp1);
266 __ push(tmp2);
267
268 // Optimized cset-test
269 __ movptr(tmp1, dst);
270 if (AOTCodeCache::is_on_for_dump()) {
271 assert_different_registers(tmp1, tmp2, rcx);
272 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
273 __ push(rcx);
274 __ movb(rcx, Address(tmp2));
275 __ shrptr(tmp1);
276 __ pop(rcx);
277 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
278 __ movptr(tmp2, Address(tmp2));
279 } else {
280 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
281 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
282 }
283 __ movbool(tmp1, Address(tmp1, tmp2, Address::times_1));
284 __ testbool(tmp1);
285 __ jcc(Assembler::zero, not_cset);
286 }
287
288 // Slow-path call.
289 // Save registers that can be clobbered by call.
290 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
291 __ enter();
292 if (dst != rax) {
293 __ push(rax);
294 }
295 __ push_call_clobbered_registers_except(rax, /* save_fpu = */ true);
296
297 // Shuffle registers such that dst is in c_rarg0 and addr in c_rarg1.
298 if (dst == c_rarg1) {
299 __ lea(c_rarg0, src);
300 __ xchgptr(c_rarg1, c_rarg0);
301 } else {
302 __ lea(c_rarg1, src);
303 __ movptr(c_rarg0, dst);
304 }
305
306 address target = nullptr;
307 if (is_strong) {
308 if (is_narrow) {
309 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
310 } else {
311 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
312 }
313 } else if (is_weak) {
314 if (is_narrow) {
315 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
316 } else {
317 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
318 }
319 } else {
320 assert(is_phantom, "only remaining strength");
321 assert(!is_narrow, "phantom access cannot be narrow");
322 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
323 }
324
325 // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
326 __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
327 __ pop_call_clobbered_registers_except(rax, /* restore_fpu = */ true);
328 if (dst != rax) {
329 __ movptr(dst, rax);
330 __ pop(rax);
331 }
332 __ leave();
333
334 __ bind(not_cset);
335
336 if (is_strong) {
337 __ pop(tmp2);
338 __ pop(tmp1);
339 }
340
341 __ bind(heap_stable);
342
343 __ block_comment("} load_reference_barrier");
344 }
345
346 //
347 // Arguments:
348 //
349 // Inputs:
350 // src: oop location, might be clobbered
351 // tmp1: scratch register, might not be valid.
352 //
353 // Output:
354 // dst: oop loaded from src location
355 //
356 // Kill:
357 // tmp1 (if it is valid)
358 //
359 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
360 Register dst, Address src, Register tmp1) {
361 // 1: non-reference load, no additional barrier is needed
362 if (!is_reference_type(type)) {
363 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
364 return;
365 }
366
367 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Not expected");
368
369 // 2: load a reference from src location and apply LRB if needed
370 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
371 Register result_dst = dst;
372 bool use_tmp1_for_dst = false;
373
374 // Preserve src location for LRB
375 if (dst == src.base() || dst == src.index()) {
376 // Use tmp1 for dst if possible, as it is not used in BarrierAssembler::load_at()
377 if (tmp1->is_valid() && tmp1 != src.base() && tmp1 != src.index()) {
378 dst = tmp1;
379 use_tmp1_for_dst = true;
380 } else {
381 dst = rdi;
382 __ push(dst);
383 }
384 assert_different_registers(dst, src.base(), src.index());
385 }
386
387 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
388
389 load_reference_barrier(masm, dst, src, decorators);
390
391 // Move loaded oop to final destination
392 if (dst != result_dst) {
393 __ movptr(result_dst, dst);
394
395 if (!use_tmp1_for_dst) {
396 __ pop(dst);
397 }
398
399 dst = result_dst;
400 }
401 } else {
402 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
403 }
404
405 // 3: apply keep-alive barrier if needed
406 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
407 satb_barrier(masm /* masm */,
408 noreg /* obj */,
409 dst /* pre_val */,
410 tmp1 /* tmp */);
411 }
412 }
413
414 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
415 assert(ShenandoahCardBarrier, "Should have been checked by caller");
416
417 // Does a store check for the oop in register obj. The content of
418 // register obj is destroyed afterwards.
419 __ shrptr(obj, CardTable::card_shift());
420
421 // We'll use this register as the TLS base address and also later on
422 // to hold the byte_map_base.
423 Register thread = r15_thread;
424 Register tmp = rscratch1;
425
426 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
427 __ movptr(tmp, curr_ct_holder_addr);
428 Address card_addr(tmp, obj, Address::times_1);
429
430 int dirty = CardTable::dirty_card_val();
431 if (UseCondCardMark) {
432 Label L_already_dirty;
433 __ cmpb(card_addr, dirty);
434 __ jccb(Assembler::equal, L_already_dirty);
435 __ movb(card_addr, dirty);
436 __ bind(L_already_dirty);
437 } else {
438 __ movb(card_addr, dirty);
439 }
440 }
441
442 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
443 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
444
445 // 1: non-reference types require no barriers
446 if (!is_reference_type(type)) {
447 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
448 return;
449 }
450
451 // Flatten object address right away for simplicity: likely needed by barriers
452 assert_different_registers(val, tmp1, tmp2, tmp3, r15_thread);
453 if (dst.index() == noreg && dst.disp() == 0) {
454 if (dst.base() != tmp1) {
455 __ movptr(tmp1, dst.base());
456 }
457 } else {
458 __ lea(tmp1, dst);
459 }
460
461 // 2: pre-barrier: SATB needs the previous value
462 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
463 satb_barrier(masm,
464 tmp1 /* obj */,
465 tmp2 /* pre_val */,
466 tmp3 /* tmp */);
467 }
468
469 // Store!
470 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
471
472 // 3: post-barrier: card barrier needs store address
473 bool storing_non_null = (val != noreg);
474 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
475 card_barrier(masm, tmp1);
476 }
477 }
478
479 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
480 Register obj, Register tmp, Label& slowpath) {
481 Label done;
482 // Resolve jobject
483 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
484
485 // Check for null.
486 __ testptr(obj, obj);
487 __ jcc(Assembler::zero, done);
488
489 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
490 __ testb(gc_state, ShenandoahHeap::EVACUATION);
491 __ jccb(Assembler::notZero, slowpath);
492 __ bind(done);
493 }
494
495 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler* masm, Register weak_handle, Register obj, Label& slowpath) {
496 Label done;
497
498 // Peek weak handle using the standard implementation.
499 BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, slowpath);
500
501 // Check if the reference is null, and if it is, take the fast path.
502 __ testptr(obj, obj);
503 __ jcc(Assembler::zero, done);
504
505 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
506
507 // Check if the heap is under weak-reference/roots processing, in
508 // which case we need to take the slow path.
509 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
510 __ jcc(Assembler::notZero, slowpath);
511 __ bind(done);
512 }
513
514 #ifdef PRODUCT
515 #define BLOCK_COMMENT(str) /* nothing */
516 #else
517 #define BLOCK_COMMENT(str) __ block_comment(str)
518 #endif
519
520 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
521
522 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
523
524 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
525 Register addr, Register count,
526 Register tmp) {
527 assert(ShenandoahCardBarrier, "Should have been checked by caller");
528
529 Label L_loop, L_done;
530 const Register end = count;
531 assert_different_registers(addr, end);
532
533 // Zero count? Nothing to do.
534 __ testl(count, count);
535 __ jccb(Assembler::zero, L_done);
536
537 const Register thread = r15_thread;
538 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
539 __ movptr(tmp, curr_ct_holder_addr);
540
541 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
542 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
543 __ shrptr(addr, CardTable::card_shift());
544 __ shrptr(end, CardTable::card_shift());
545 __ subptr(end, addr); // end --> cards count
546
547 __ addptr(addr, tmp);
548
549 __ BIND(L_loop);
550 __ movb(Address(addr, count, Address::times_1), 0);
551 __ decrement(count);
552 __ jccb(Assembler::greaterEqual, L_loop);
553
554 __ BIND(L_done);
555 }
556
557 #undef __
558
559 #ifdef COMPILER1
560
561 #define __ ce->masm()->
562
563 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
564 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
565 // At this point we know that marking is in progress.
566 // If do_load() is true then we have to emit the
567 // load of the previous value; otherwise it has already
568 // been loaded into _pre_val.
569
570 __ bind(*stub->entry());
571 assert(stub->pre_val()->is_register(), "Precondition.");
572
573 Register pre_val_reg = stub->pre_val()->as_register();
574
575 if (stub->do_load()) {
576 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
577 }
578
579 __ cmpptr(pre_val_reg, NULL_WORD);
580 __ jcc(Assembler::equal, *stub->continuation());
581 ce->store_parameter(stub->pre_val()->as_register(), 0);
582 __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
583 __ jmp(*stub->continuation());
584
585 }
586
587 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
588 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
589 __ bind(*stub->entry());
590
591 DecoratorSet decorators = stub->decorators();
592 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
593 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
594 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
595 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
596
597 Register obj = stub->obj()->as_register();
598 Register res = stub->result()->as_register();
599 Register addr = stub->addr()->as_pointer_register();
600 Register tmp1 = stub->tmp1()->as_register();
601 Register tmp2 = stub->tmp2()->as_register();
602 assert_different_registers(obj, res, addr, tmp1, tmp2);
603
604 Label slow_path;
605
606 assert(res == rax, "result must arrive in rax");
607
608 if (res != obj) {
609 __ mov(res, obj);
610 }
611
612 if (is_strong) {
613 // Check for object being in the collection set.
614 __ mov(tmp1, res);
615 if (AOTCodeCache::is_on_for_dump()) {
616 __ push(rcx);
617 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
618 __ movl(rcx, Address(rcx));
619 if (tmp1 != rcx) {
620 __ mov(tmp1, res);
621 __ shrptr(tmp1);
622 __ pop(rcx);
623 } else {
624 assert_different_registers(tmp2, rcx);
625 __ mov(tmp2, res);
626 __ shrptr(tmp2);
627 __ pop(rcx);
628 __ movptr(tmp1, tmp2);
629 }
630 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
631 __ movptr(tmp2, Address(tmp2));
632 } else {
633 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
634 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
635 }
636 __ movbool(tmp2, Address(tmp2, tmp1, Address::times_1));
637 __ testbool(tmp2);
638 __ jcc(Assembler::zero, *stub->continuation());
639 }
640
641 __ bind(slow_path);
642 ce->store_parameter(res, 0);
643 ce->store_parameter(addr, 1);
644 if (is_strong) {
645 if (is_native) {
646 __ call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
647 } else {
648 __ call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
649 }
650 } else if (is_weak) {
651 __ call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
652 } else {
653 assert(is_phantom, "only remaining strength");
654 __ call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
655 }
656 __ jmp(*stub->continuation());
657 }
658
659 #undef __
660
661 #define __ sasm->
662
663 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
664 __ prologue("shenandoah_pre_barrier", false);
665 // arg0 : previous value of memory
666
667 __ push(rax);
668 __ push(rdx);
669
670 const Register pre_val = rax;
671 const Register thread = r15_thread;
672 const Register tmp = rdx;
673
674 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
675 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
676
677 Label done;
678 Label runtime;
679
680 // Is SATB still active?
681 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
682 __ testb(gc_state, ShenandoahHeap::MARKING);
683 __ jcc(Assembler::zero, done);
684
685 // Can we store original value in the thread's buffer?
686
687 __ movptr(tmp, queue_index);
688 __ testptr(tmp, tmp);
689 __ jcc(Assembler::zero, runtime);
690 __ subptr(tmp, wordSize);
691 __ movptr(queue_index, tmp);
692 __ addptr(tmp, buffer);
693
694 // prev_val (rax)
695 __ load_parameter(0, pre_val);
696 __ movptr(Address(tmp, 0), pre_val);
697 __ jmp(done);
698
699 __ bind(runtime);
700
701 __ save_live_registers_no_oop_map(true);
702
703 // load the pre-value
704 __ load_parameter(0, rcx);
705 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), rcx);
706
707 __ restore_live_registers(true);
708
709 __ bind(done);
710
711 __ pop(rdx);
712 __ pop(rax);
713
714 __ epilogue();
715 }
716
717 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
718 __ prologue("shenandoah_load_reference_barrier", false);
719 // arg0 : object to be resolved
720
721 __ save_live_registers_no_oop_map(true);
722
723 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
724 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
725 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
726 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
727
728 __ load_parameter(0, c_rarg0);
729 __ load_parameter(1, c_rarg1);
730 if (is_strong) {
731 if (is_native) {
732 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
733 } else {
734 if (UseCompressedOops) {
735 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), c_rarg0, c_rarg1);
736 } else {
737 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
738 }
739 }
740 } else if (is_weak) {
741 assert(!is_native, "weak must not be called off-heap");
742 if (UseCompressedOops) {
743 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow), c_rarg0, c_rarg1);
744 } else {
745 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak), c_rarg0, c_rarg1);
746 }
747 } else {
748 assert(is_phantom, "only remaining strength");
749 assert(is_native, "phantom must only be called off-heap");
750 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom), c_rarg0, c_rarg1);
751 }
752
753 __ restore_live_registers_except_rax(true);
754
755 __ epilogue();
756 }
757
758 #undef __
759
760 #endif // COMPILER1
761
762 #ifdef COMPILER2
763
764 #undef __
765 #define __ masm->
766
767 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
768 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
769 if (narrow) {
770 __ movl(dst, src);
771 } else {
772 __ movq(dst, src);
773 }
774
775 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, noreg, noreg, narrow);
776 }
777
778 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
779 Address dst, bool dst_narrow,
780 Register src, bool src_narrow,
781 Register tmp) {
782
783 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp, noreg, noreg, dst_narrow);
784
785 // Need to encode into tmp, because we cannot clobber src.
786 if (dst_narrow && !src_narrow) {
787 __ movq(tmp, src);
788 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
789 __ encode_heap_oop(tmp);
790 } else {
791 __ encode_heap_oop_not_null(tmp);
792 }
793 src = tmp;
794 }
795
796 // Do the actual store
797 if (dst_narrow) {
798 __ movl(dst, src);
799 } else {
800 __ movq(dst, src);
801 }
802
803 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp, noreg);
804 }
805
806 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
807 Register res, Address addr,
808 Register oldval, Register newval, Register tmp,
809 bool narrow) {
810
811 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
812
813 // Oldval and newval can be in the same register, but all other registers should be
814 // distinct for extra safety, as we shuffle register values around.
815 assert_different_registers(oldval, tmp, addr.base(), addr.index());
816 assert_different_registers(newval, tmp, addr.base(), addr.index());
817
818 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
819
820 // CAS!
821 __ lock();
822 if (narrow) {
823 __ cmpxchgl(newval, addr);
824 } else {
825 __ cmpxchgptr(newval, addr);
826 }
827
828 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
829 if (res != noreg) {
830 __ setcc(Assembler::equal, res);
831 }
832
833 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
834 }
835
836 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
837 assert_different_registers(newval, tmp, addr.base(), addr.index());
838
839 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
840
841 if (narrow) {
842 __ xchgl(newval, addr);
843 } else {
844 __ xchgq(newval, addr);
845 }
846
847 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
848 }
849
850 #undef __
851 #define __ masm.
852
853 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2) {
854 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
855
856 __ lea(tmp1, addr);
857 __ shrptr(tmp1, CardTable::card_shift());
858 __ addptr(tmp1, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
859 Address card_address(tmp1, 0);
860
861 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
862 Label L_done;
863 if (UseCondCardMark) {
864 __ cmpb(card_address, 0);
865 __ jccb(Assembler::equal, L_done);
866 }
867 if (UseCompressedOops && CompressedOops::base() == nullptr) {
868 __ movb(card_address, r12);
869 } else {
870 __ movb(card_address, 0);
871 }
872 __ bind(L_done);
873 }
874
875 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
876 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
877
878 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
879 __ cmpb(gc_state_fast, 0);
880 __ jcc(Assembler::notEqual, *entry());
881 __ bind(*continuation());
882 }
883
884 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
885 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
886 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
887
888 // On x86, there is a significant penalty with unaligned branch target, for example
889 // when the target instruction straggles the fetch line. It makes (performance) sense
890 // to spend some code size to align the target better.
891 __ align(16);
892 __ bind(*entry());
893
894 // If we need to load ourselves, do it here.
895 if (_do_load) {
896 if (_narrow) {
897 __ movl(_obj, _addr);
898 } else {
899 __ movq(_obj, _addr);
900 }
901 }
902
903 // If the object is null, there is no point in applying barriers.
904 maybe_far_jump_if_zero(masm, _obj);
905
906 // We need to make sure that loads done by callers survive across slow-path calls.
907 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
908 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
909 if (!_do_load || needs_both_barriers) {
910 preserve(_obj);
911 }
912
913 // Go for barriers. Barriers can return straight to continuation, as long
914 // as another barrier is not needed.
915 if (needs_both_barriers) {
916 keepalive(masm, nullptr);
917 lrb(masm);
918 } else if (_needs_keep_alive_barrier) {
919 keepalive(masm, continuation());
920 } else if (_needs_load_ref_barrier) {
921 lrb(masm);
922 } else {
923 ShouldNotReachHere();
924 }
925 }
926
927 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
928 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
929 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
930 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
931
932 Label L_through, L_pop_and_slow;
933
934 // If another barrier is enabled as well, do a runtime check for a specific barrier.
935 if (_needs_load_ref_barrier) {
936 assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
937 __ cmpb(gc_state_fast, 0);
938 __ jcc(Assembler::equal, L_through);
939 }
940
941 // Need temp to work, allocate one now.
942 bool tmp_live;
943 Register tmp = select_temp_register(tmp_live);
944 if (tmp_live) {
945 __ push(tmp);
946 }
947
948 // Fast-path: put object into buffer.
949 // If buffer is already full, go slow.
950 __ movptr(tmp, index);
951 __ subptr(tmp, wordSize);
952 __ jccb(Assembler::below, L_pop_and_slow);
953 __ movptr(index, tmp);
954 __ addptr(tmp, buffer);
955
956 // Store the object in queue.
957 // If object is narrow, we need to decode it before inserting.
958 // We can skip the re-encoding if we know that object is not preserved.
959 if (_narrow) {
960 __ decode_heap_oop_not_null(_obj);
961 }
962 __ movptr(Address(tmp, 0), _obj);
963 if (_narrow && is_preserved(_obj)) {
964 __ encode_heap_oop_not_null(_obj);
965 }
966
967 // Fast-path exits here.
968 if (tmp_live) {
969 __ pop(tmp);
970 }
971
972 if (L_done != nullptr) {
973 __ jmp(*L_done);
974 } else {
975 __ jmp(L_through);
976 }
977
978 // Slow-path: call runtime to handle.
979 // Need to pop tmp immediately for stack to remain aligned.
980 __ bind(L_pop_and_slow);
981 if (tmp_live) {
982 __ pop(tmp);
983 }
984 {
985 SaveLiveRegisters slr(&masm, this);
986
987 // Shuffle in the arguments. The end result should be:
988 // c_rarg0 <-- obj
989 if (c_rarg0 != _obj) {
990 __ mov(c_rarg0, _obj);
991 }
992
993 // Go to runtime and handle the rest there.
994 // Use rax as scratch, as it will be saved if live.
995 __ call(RuntimeAddress(keepalive_runtime_entry_addr()), rax);
996 }
997 if (L_done != nullptr) {
998 __ jmp(*L_done);
999 } else {
1000 __ bind(L_through);
1001 }
1002 }
1003
1004 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
1005 Label L_pop_and_slow, L_slow;
1006
1007 // If another barrier is enabled as well, do a runtime check for a specific barrier.
1008 if (_needs_keep_alive_barrier) {
1009 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
1010 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
1011 __ cmpb(gc_state_fast, 0);
1012 __ jcc(Assembler::equal, *continuation());
1013 }
1014
1015 // If weak references are being processed, weak/phantom loads need to go slow,
1016 // regardless of their cset status.
1017 if (_needs_load_ref_weak_barrier) {
1018 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
1019 __ cmpb(gc_state_fast, 0);
1020 __ jccb(Assembler::notEqual, L_slow);
1021 }
1022
1023 bool is_aot = AOTCodeCache::is_on_for_dump();
1024
1025 // Need temp to work, allocate one now.
1026 bool tmp_live;
1027 Register tmp = select_temp_register(tmp_live, /* skip_reg1 = */ is_aot ? rcx : noreg);
1028 if (tmp_live) {
1029 __ push(tmp);
1030 }
1031
1032 // Compute the cset bitmap index
1033 if (_narrow) {
1034 __ decode_heap_oop_not_null(tmp, _obj);
1035 } else {
1036 __ movptr(tmp, _obj);
1037 }
1038
1039 Address cset_addr_arg;
1040 intptr_t cset_addr = reinterpret_cast<intptr_t>(ShenandoahHeap::in_cset_fast_test_addr());
1041 if (!is_aot && cset_addr < INT32_MAX) {
1042 // Cset bitmap is at easily encodeable address. Just use it as displacement.
1043 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1044 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr));
1045 } else {
1046 bool tmp2_live;
1047 Register tmp2 = select_temp_register(tmp2_live, /* skip_reg1 = */ tmp, /* skip_reg2 = */ is_aot ? rcx : noreg);
1048 if (tmp2_live) {
1049 __ push(tmp2);
1050 }
1051 if (is_aot) {
1052 // Generating AOT code, pull the cset bitmap and region shift from AOT table.
1053 assert_different_registers(tmp, tmp2, rcx);
1054 __ push(rcx);
1055 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
1056 __ movl(rcx, Address(rcx));
1057 __ shrptr(tmp);
1058 __ pop(rcx);
1059 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
1060 __ addptr(tmp, Address(tmp2));
1061 } else {
1062 // Cset bitmap is far away. Add its address fully.
1063 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1064 __ movptr(tmp2, cset_addr);
1065 __ addptr(tmp, tmp2);
1066 }
1067 if (tmp2_live) {
1068 __ pop(tmp2);
1069 }
1070 cset_addr_arg = Address(tmp, 0);
1071 }
1072
1073 // Cset-check. Fall-through to slow if in collection set.
1074 __ cmpb(cset_addr_arg, 0);
1075 if (tmp_live) {
1076 __ jccb(Assembler::notEqual, L_pop_and_slow);
1077 __ pop(tmp);
1078 __ jmp(*continuation());
1079 } else {
1080 // Nothing else to do, jump back
1081 __ jcc(Assembler::equal, *continuation());
1082 }
1083
1084 // Slow path
1085 __ bind(L_pop_and_slow);
1086 // Need to pop tmp immediately for stack to remain aligned.
1087 if (tmp_live) {
1088 __ pop(tmp);
1089 }
1090 __ bind(L_slow);
1091
1092 // Obj is the result, need to temporarily stop preserving it.
1093 bool is_obj_preserved = is_preserved(_obj);
1094 if (is_obj_preserved) {
1095 dont_preserve(_obj);
1096 }
1097 {
1098 SaveLiveRegisters slr(&masm, this);
1099
1100 assert_different_registers(rax, c_rarg0, c_rarg1);
1101
1102 // Shuffle in the arguments. The end result should be:
1103 // c_rarg0 <-- obj
1104 // c_rarg1 <-- lea(addr)
1105 if (_obj == c_rarg0) {
1106 __ lea(c_rarg1, _addr);
1107 } else if (_obj == c_rarg1) {
1108 // Set up arguments in reverse, and then flip them
1109 __ lea(c_rarg0, _addr);
1110 __ xchgptr(c_rarg0, c_rarg1);
1111 } else {
1112 assert_different_registers(_obj, c_rarg0, c_rarg1);
1113 __ lea(c_rarg1, _addr);
1114 __ movptr(c_rarg0, _obj);
1115 }
1116
1117 // Go to runtime and handle the rest there.
1118 // Use rax as scratch, as it will be clobbered by result anyway.
1119 __ call(RuntimeAddress(lrb_runtime_entry_addr()), rax);
1120
1121 // Save the result where needed.
1122 if (_narrow) {
1123 __ movl(_obj, rax);
1124 } else if (_obj != rax) {
1125 __ movptr(_obj, rax);
1126 }
1127 }
1128 if (is_obj_preserved) {
1129 preserve(_obj);
1130 }
1131
1132 __ jmp(*continuation());
1133 }
1134
1135 int ShenandoahBarrierStubC2::available_gp_registers() {
1136 return Register::available_gp_registers();
1137 }
1138
1139 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
1140 return r == rsp || r == rbp || r == r12_heapbase || r == r15_thread;
1141 }
1142
1143 void ShenandoahBarrierStubC2::post_init() {
1144 // Do nothing.
1145 }
1146
1147 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
1148 if (_narrow) {
1149 __ testl(reg, reg);
1150 } else {
1151 __ testq(reg, reg);
1152 }
1153 __ jcc(Assembler::zero, *continuation());
1154 }
1155
1156 #endif // COMPILER2