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 "code/aotCodeCache.hpp"
28 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
29 #include "gc/shenandoah/mode/shenandoahMode.hpp"
30 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
31 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
33 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
34 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
35 #include "gc/shenandoah/shenandoahRuntime.hpp"
36 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
37 #include "interpreter/interpreter.hpp"
38 #include "nativeInst_x86.hpp"
39 #include "runtime/javaThread.hpp"
40 #include "runtime/sharedRuntime.hpp"
41 #include "utilities/macros.hpp"
42 #ifdef COMPILER1
43 #include "c1/c1_LIRAssembler.hpp"
44 #include "c1/c1_MacroAssembler.hpp"
45 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
46 #endif
47 #ifdef COMPILER2
48 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
49 #include "opto/output.hpp"
50 #endif
51
52 #define __ masm->
53
54 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
55 Register src, Register dst, Register count) {
56
57 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
58
59 if (is_reference_type(type)) {
60 if (ShenandoahCardBarrier) {
61 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
62 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
63 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
64
65 // We need to save the original element count because the array copy stub
66 // will destroy the value and we need it for the card marking barrier.
67 if (!checkcast) {
68 if (!obj_int) {
69 // Save count for barrier
70 __ movptr(r11, count);
71 } else if (disjoint) {
72 // Save dst in r11 in the disjoint case
73 __ movq(r11, dst);
74 }
75 }
76 }
77
78 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
79 Register thread = r15_thread;
80 assert_different_registers(src, dst, count, thread);
81
82 Label L_done;
83 // Short-circuit if count == 0.
84 __ testptr(count, count);
85 __ jcc(Assembler::zero, L_done);
86
87 // Avoid runtime call when not active.
88 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
89 int flags;
90 if (ShenandoahSATBBarrier && dest_uninitialized) {
91 flags = ShenandoahHeap::HAS_FORWARDED;
92 } else {
93 flags = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING;
94 }
95 __ testb(gc_state, flags);
96 __ jcc(Assembler::zero, L_done);
97
98 __ push_call_clobbered_registers(/* save_fpu = */ false);
99 // If arguments are not in proper places, shuffle them.
100 // Doing this via the stack is the most straight-forward way to avoid
101 // accidentally smashing any register.
102 if (c_rarg0 != src || c_rarg1 != dst || c_rarg2 != count) {
103 __ push(src);
104 __ push(dst);
105 __ push(count);
106 __ pop(c_rarg2);
107 __ pop(c_rarg1);
108 __ pop(c_rarg0);
109 }
110 address target = nullptr;
111 if (UseCompressedOops) {
112 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop);
113 } else {
114 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop);
115 }
116 __ call_VM_leaf(target, 3);
117
118 __ pop_call_clobbered_registers(/* restore_fpu = */ false);
119
120 __ bind(L_done);
121 }
122 }
123
124 }
125
126 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
127 Register src, Register dst, Register count) {
128
129 if (ShenandoahCardBarrier && is_reference_type(type)) {
130 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
131 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
132 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
133 Register tmp = rax;
134
135 if (!checkcast) {
136 if (!obj_int) {
137 // Save count for barrier
138 count = r11;
139 } else if (disjoint) {
140 // Use the saved dst in the disjoint case
141 dst = r11;
142 }
143 } else {
144 tmp = rscratch1;
145 }
146 gen_write_ref_array_post_barrier(masm, decorators, dst, count, tmp);
147 }
148 }
149
150 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
151 Register obj,
152 Register pre_val,
153 Register tmp) {
154 assert(ShenandoahSATBBarrier, "Should be checked by caller");
155 const Register thread = r15_thread;
156
157 Label done;
158 Label runtime;
159
160 assert(pre_val != noreg, "check this code");
161 assert_different_registers(obj, pre_val, tmp);
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 INCLUDE_CDS
271 if (AOTCodeCache::is_on_for_dump()) {
272 assert_different_registers(tmp1, tmp2, rcx);
273 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
274 __ push(rcx);
275 __ movb(rcx, Address(tmp2));
276 __ shrptr(tmp1);
277 __ pop(rcx);
278 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
279 __ movptr(tmp2, Address(tmp2));
280 } else
281 #endif
282 {
283 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
284 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
285 }
286 __ movbool(tmp1, Address(tmp1, tmp2, Address::times_1));
287 __ testbool(tmp1);
288 __ jcc(Assembler::zero, not_cset);
289 }
290
291 // Slow-path call.
292 // Save registers that can be clobbered by call.
293 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
294 __ enter();
295 if (dst != rax) {
296 __ push(rax);
297 }
298 __ push_call_clobbered_registers_except(rax, /* save_fpu = */ true);
299
300 // Shuffle registers such that dst is in c_rarg0 and addr in c_rarg1.
301 if (dst == c_rarg1) {
302 __ lea(c_rarg0, src);
303 __ xchgptr(c_rarg1, c_rarg0);
304 } else {
305 __ lea(c_rarg1, src);
306 __ movptr(c_rarg0, dst);
307 }
308
309 address target = nullptr;
310 if (is_strong) {
311 if (is_narrow) {
312 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
313 } else {
314 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
315 }
316 } else if (is_weak) {
317 if (is_narrow) {
318 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
319 } else {
320 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
321 }
322 } else {
323 assert(is_phantom, "only remaining strength");
324 assert(!is_narrow, "phantom access cannot be narrow");
325 target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
326 }
327
328 // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
329 __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
330 __ pop_call_clobbered_registers_except(rax, /* restore_fpu = */ true);
331 if (dst != rax) {
332 __ movptr(dst, rax);
333 __ pop(rax);
334 }
335 __ leave();
336
337 __ bind(not_cset);
338
339 if (is_strong) {
340 __ pop(tmp2);
341 __ pop(tmp1);
342 }
343
344 __ bind(heap_stable);
345
346 __ block_comment("} load_reference_barrier");
347 }
348
349 //
350 // Arguments:
351 //
352 // Inputs:
353 // src: oop location, might be clobbered
354 // tmp1: scratch register, might not be valid.
355 //
356 // Output:
357 // dst: oop loaded from src location
358 //
359 // Kill:
360 // tmp1 (if it is valid)
361 //
362 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
363 Register dst, Address src, Register tmp1) {
364 // 1: non-reference load, no additional barrier is needed
365 if (!is_reference_type(type)) {
366 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
367 return;
368 }
369
370 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Not expected");
371
372 // 2: load a reference from src location and apply LRB if needed
373 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
374 Register result_dst = dst;
375 bool use_tmp1_for_dst = false;
376
377 // Preserve src location for LRB
378 if (dst == src.base() || dst == src.index()) {
379 // Use tmp1 for dst if possible, as it is not used in BarrierAssembler::load_at()
380 if (tmp1->is_valid() && tmp1 != src.base() && tmp1 != src.index()) {
381 dst = tmp1;
382 use_tmp1_for_dst = true;
383 } else {
384 dst = rdi;
385 __ push(dst);
386 }
387 assert_different_registers(dst, src.base(), src.index());
388 }
389
390 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
391
392 load_reference_barrier(masm, dst, src, decorators);
393
394 // Move loaded oop to final destination
395 if (dst != result_dst) {
396 __ movptr(result_dst, dst);
397
398 if (!use_tmp1_for_dst) {
399 __ pop(dst);
400 }
401
402 dst = result_dst;
403 }
404 } else {
405 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
406 }
407
408 // 3: apply keep-alive barrier if needed
409 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
410 satb_barrier(masm /* masm */,
411 noreg /* obj */,
412 dst /* pre_val */,
413 tmp1 /* tmp */);
414 }
415 }
416
417 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj, Register tmp) {
418 assert(ShenandoahCardBarrier, "Should have been checked by caller");
419 assert_different_registers(obj, tmp);
420
421 // Does a store check for the oop in register obj. The content of
422 // register obj is destroyed afterwards.
423 __ shrptr(obj, CardTable::card_shift());
424
425 Address curr_ct_holder_addr(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
426 __ movptr(tmp, curr_ct_holder_addr);
427 Address card_addr(tmp, obj, Address::times_1);
428
429 int dirty = CardTable::dirty_card_val();
430 if (UseCondCardMark) {
431 Label L_already_dirty;
432 __ cmpb(card_addr, dirty);
433 __ jccb(Assembler::equal, L_already_dirty);
434 __ movb(card_addr, dirty);
435 __ bind(L_already_dirty);
436 } else {
437 __ movb(card_addr, dirty);
438 }
439 }
440
441 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
442 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
443
444 // 1: non-reference types require no barriers
445 if (!is_reference_type(type)) {
446 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
447 return;
448 }
449
450 // Flatten object address right away for simplicity: likely needed by barriers
451 assert_different_registers(val, tmp1, tmp2, tmp3, r15_thread);
452 if (dst.index() == noreg && dst.disp() == 0) {
453 if (dst.base() != tmp1) {
454 __ movptr(tmp1, dst.base());
455 }
456 } else {
457 __ lea(tmp1, dst);
458 }
459
460 // 2: pre-barrier: SATB needs the previous value
461 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
462 satb_barrier(masm,
463 tmp1 /* obj */,
464 tmp2 /* pre_val */,
465 tmp3 /* tmp */);
466 }
467
468 // Store!
469 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
470
471 // 3: post-barrier: card barrier needs store address
472 bool storing_non_null = (val != noreg);
473 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
474 card_barrier(masm, tmp1, tmp2);
475 }
476 }
477
478 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
479 Register obj, Register tmp, Label& slowpath) {
480 Label done;
481 // Resolve jobject
482 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
483
484 // Check for null.
485 __ testptr(obj, obj);
486 __ jcc(Assembler::zero, done);
487
488 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
489 __ testb(gc_state, ShenandoahHeap::EVACUATION);
490 __ jccb(Assembler::notZero, slowpath);
491 __ bind(done);
492 }
493
494 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler* masm, Register weak_handle, Register obj, Label& slowpath) {
495 Label done;
496
497 // Peek weak handle using the standard implementation.
498 BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, slowpath);
499
500 // Check if the reference is null, and if it is, take the fast path.
501 __ testptr(obj, obj);
502 __ jcc(Assembler::zero, done);
503
504 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
505
506 // Check if the heap is under weak-reference/roots processing, in
507 // which case we need to take the slow path.
508 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
509 __ jcc(Assembler::notZero, slowpath);
510 __ bind(done);
511 }
512
513 void ShenandoahBarrierSetAssembler::check_oop(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& L_error) {
514 assert_different_registers(obj, tmp1, tmp2);
515 // Check if the oop is in the right area of memory
516 __ movptr(tmp1, obj);
517 #if INCLUDE_CDS
518 if (AOTCodeCache::is_on_for_dump()) {
519 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::verify_oop_mask_address()));
520 __ movptr(tmp2, Address(tmp2));
521 __ andptr(tmp1, tmp2);
522 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::verify_oop_bits_address()));
523 __ movptr(tmp2, Address(tmp2));
524 } else
525 #endif
526 {
527 __ movptr(tmp2, (intptr_t) Universe::verify_oop_mask());
528 __ andptr(tmp1, tmp2);
529 __ movptr(tmp2, (intptr_t) Universe::verify_oop_bits());
530 }
531 __ cmpptr(tmp1, tmp2);
532 __ jcc(Assembler::notZero, L_error);
533
534 // This routine is sometimes called before applying GC barriers.
535 // With +COH, loading the klass may end up loading forwarding pointer instead.
536 Label L_skip;
537 if (UseCompactObjectHeaders) {
538 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
539 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
540 __ jcc(Assembler::notZero, L_skip);
541 }
542
543 // Make sure klass is 'reasonable', which is not zero.
544 __ load_narrow_klass(tmp1, obj);
545 __ testl(tmp1, tmp1);
546 __ jcc(Assembler::zero, L_error);
547
548 __ bind(L_skip);
549 }
550
551 #ifdef PRODUCT
552 #define BLOCK_COMMENT(str) /* nothing */
553 #else
554 #define BLOCK_COMMENT(str) __ block_comment(str)
555 #endif
556
557 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
558
559 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
560
561 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
562 Register addr, Register count,
563 Register tmp) {
564 assert(ShenandoahCardBarrier, "Should have been checked by caller");
565
566 Label L_loop, L_done;
567 const Register end = count;
568 assert_different_registers(addr, end);
569
570 // Zero count? Nothing to do.
571 __ testl(count, count);
572 __ jccb(Assembler::zero, L_done);
573
574 const Register thread = r15_thread;
575 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
576 __ movptr(tmp, curr_ct_holder_addr);
577
578 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
579 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
580 __ shrptr(addr, CardTable::card_shift());
581 __ shrptr(end, CardTable::card_shift());
582 __ subptr(end, addr); // end --> cards count
583
584 __ addptr(addr, tmp);
585
586 __ BIND(L_loop);
587 __ movb(Address(addr, count, Address::times_1), 0);
588 __ decrement(count);
589 __ jccb(Assembler::greaterEqual, L_loop);
590
591 __ BIND(L_done);
592 }
593
594 #undef __
595
596 address ShenandoahBarrierSetAssembler::parse_jump_address(address pc) {
597 NativeInstruction* ni = nativeInstruction_at(pc);
598 assert(ni->is_jump(), "Must be a jump");
599 NativeJump* jmp = nativeJump_at(pc);
600 return jmp->jump_destination();
601 }
602
603 void ShenandoahBarrierSetAssembler::insert_patchable_nop(address pc) {
604 *(pc + 0) = 0x0F;
605 *(pc + 1) = 0x1F;
606 *(pc + 2) = 0x44;
607 *(pc + 3) = 0x00;
608 *(pc + 4) = 0x00;
609 // No invalidation is needed: relies on reader-side cross-modify-fence.
610 }
611
612 bool ShenandoahBarrierSetAssembler::is_patchable_nop(address pc) {
613 if (*(pc + 0) != 0x0F) return false;
614 if (*(pc + 1) != 0x1F) return false;
615 if (*(pc + 2) != 0x44) return false;
616 if (*(pc + 3) != 0x00) return false;
617 if (*(pc + 4) != 0x00) return false;
618 return true;
619 }
620
621 void ShenandoahBarrierSetAssembler::insert_patchable_jump(address pc, address target_pc) {
622 int32_t disp = checked_cast<int32_t>((intptr_t)target_pc - ((intptr_t)pc + 5));
623
624 *(pc + 0) = 0xE9;
625 *(pc + 1) = (disp >> 0) & 0xFF;
626 *(pc + 2) = (disp >> 8) & 0xFF;
627 *(pc + 3) = (disp >> 16) & 0xFF;
628 *(pc + 4) = (disp >> 24) & 0xFF;
629 // No invalidation is needed: relies on reader-side cross-modify-fence.
630 }
631
632 bool ShenandoahBarrierSetAssembler::is_patchable_jump(address pc, address target_pc) {
633 int32_t disp = checked_cast<int32_t>((intptr_t)target_pc - ((intptr_t)pc + 5));
634
635 if (*(pc + 0) != 0xE9) return false;
636 if (*(pc + 1) != ((disp >> 0) & 0xFF)) return false;
637 if (*(pc + 2) != ((disp >> 8) & 0xFF)) return false;
638 if (*(pc + 3) != ((disp >> 16) & 0xFF)) return false;
639 if (*(pc + 4) != ((disp >> 24) & 0xFF)) return false;
640 return true;
641 }
642
643 #ifdef COMPILER1
644
645 #define __ ce->masm()->
646
647 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
648 __ bind(*stub->entry());
649
650 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
651
652 Register obj = stub->obj()->as_register();
653
654 if (stub->do_load()) {
655 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, /* wide = */ false);
656 }
657 __ cmpptr(obj, NULL_WORD);
658 __ jcc(Assembler::equal, *stub->continuation());
659
660 ce->store_parameter(obj, 0);
661 __ call(RuntimeAddress(bs->keepalive_barrier_stub()));
662 __ jmp(*stub->continuation());
663 }
664
665 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
666 __ bind(*stub->entry());
667
668 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
669
670 Register obj = stub->obj()->as_register();
671 Register addr = stub->addr()->as_pointer_register();
672 Register slow_result = stub->slow_result()->as_register();
673 assert_different_registers(obj, addr, slow_result);
674 assert(slow_result == rax, "C1 must know about our slow call result register");
675
676 ce->store_parameter(obj, 0);
677 ce->store_parameter(addr, 1);
678 __ call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
679 if (obj != slow_result) {
680 __ mov(obj, slow_result);
681 }
682
683 __ jmp(*stub->continuation());
684 }
685
686 #undef __
687
688 #define __ sasm->
689
690 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
691 __ prologue("shenandoah_keepalive_barrier", false);
692 const Register tmp_obj = rax;
693 const Register tmp = rdx;
694 __ push(tmp);
695 __ push(tmp_obj);
696 __ load_parameter(0, tmp_obj);
697 satb_barrier(sasm, noreg, tmp_obj, tmp);
698 __ pop(tmp_obj);
699 __ pop(tmp);
700 __ epilogue();
701 }
702
703 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
704 __ prologue("shenandoah_load_reference_barrier", false);
705 const Register tmp_obj = rax;
706 const Register tmp_addr = rdx;
707 __ push(tmp_addr);
708 __ load_parameter(0, tmp_obj);
709 __ load_parameter(1, tmp_addr);
710 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
711 __ pop(tmp_addr);
712 __ epilogue();
713 }
714
715 #undef __
716
717 #endif // COMPILER1
718
719 #ifdef COMPILER2
720
721 #undef __
722 #define __ masm->
723
724 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
725 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
726 if (narrow) {
727 __ movl(dst, src);
728 } else {
729 __ movq(dst, src);
730 }
731
732 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, noreg, noreg, narrow);
733 }
734
735 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
736 Address dst, bool dst_narrow,
737 Register src, bool src_narrow,
738 Register tmp) {
739
740 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp, noreg, noreg, dst_narrow);
741
742 // Need to encode into tmp, because we cannot clobber src.
743 if (dst_narrow && !src_narrow) {
744 __ movq(tmp, src);
745 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
746 __ encode_heap_oop(tmp);
747 } else {
748 __ encode_heap_oop_not_null(tmp);
749 }
750 src = tmp;
751 }
752
753 // Do the actual store
754 if (dst_narrow) {
755 __ movl(dst, src);
756 } else {
757 __ movq(dst, src);
758 }
759
760 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp, noreg);
761 }
762
763 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
764 Register res, Address addr,
765 Register oldval, Register newval, Register tmp,
766 bool narrow) {
767
768 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
769
770 // Oldval and newval cannot be clobbered by aliasing with tmp.
771 assert_different_registers(oldval, tmp);
772 assert_different_registers(newval, tmp);
773
774 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
775
776 // CAS!
777 __ lock();
778 if (narrow) {
779 __ cmpxchgl(newval, addr);
780 } else {
781 __ cmpxchgptr(newval, addr);
782 }
783
784 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
785 if (res != noreg) {
786 __ setcc(Assembler::equal, res);
787 }
788
789 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
790 }
791
792 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
793 assert_different_registers(newval, tmp);
794
795 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
796
797 if (narrow) {
798 __ xchgl(newval, addr);
799 } else {
800 __ xchgq(newval, addr);
801 }
802
803 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
804 }
805
806 #undef __
807 #define __ masm.
808
809 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2) {
810 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
811
812 __ lea(tmp1, addr);
813 __ shrptr(tmp1, CardTable::card_shift());
814 __ addptr(tmp1, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
815 Address card_address(tmp1, 0);
816
817 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
818 Label L_done;
819 if (UseCondCardMark) {
820 __ cmpb(card_address, 0);
821 __ jccb(Assembler::equal, L_done);
822 }
823 if (UseCompressedOops && CompressedOops::base() == nullptr) {
824 __ movb(card_address, r12);
825 } else {
826 __ movb(card_address, 0);
827 }
828 __ bind(L_done);
829 }
830
831 void ShenandoahBarrierStubC2::patchable_jump(MacroAssembler& masm, const char gc_state, bool jump_when_state, Register tmp1, Register tmp2, Label* L_target) {
832 #ifdef ASSERT
833 Label L_fake_entry, L_good;
834 Address gc_state_addr(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
835
836 // Emit the secondary jump and use it to cross-check against the actual GC state.
837 // This also checks that all interesting GC state transitions are done non-racily
838 // from the perspective of the thread executing the nmethod.
839 __ relocate(patchable_barrier_Relocation::spec(ShenandoahNMethod::encode_to_reloc(gc_state, jump_when_state)));
840 __ jmp(L_fake_entry, /* maybe_short = */ false);
841
842 // Currently hot-patched to NOP.
843 __ testb(gc_state_addr, gc_state);
844 __ jccb(jump_when_state ? Assembler::zero : Assembler::notZero, L_good);
845 __ hlt();
846
847 // Currently hot-patched to JUMP.
848 __ bind(L_fake_entry);
849 __ testb(gc_state_addr, gc_state);
850 __ jccb(jump_when_state ? Assembler::notZero : Assembler::zero, L_good);
851 __ hlt();
852
853 __ bind(L_good);
854 #endif
855
856 PhaseOutput* const output = Compile::current()->output();
857 if (!output->in_scratch_emit_size()) {
858 // Emit the unconditional branch in the first version of the method.
859 // Let the rest of runtime figure out how to manage it.
860 __ relocate(patchable_barrier_Relocation::spec(ShenandoahNMethod::encode_to_reloc(gc_state, jump_when_state)));
861 __ jmp(*L_target, /* maybe_short = */ false);
862 } else {
863 // Avoid binding L_target in scratch emits.
864 // We know the patchable check is exactly 5 bytes long.
865 __ nop(5);
866 }
867 }
868
869 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp1, Register tmp2) {
870 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
871 patchable_jump_if_gc_state(masm, test_state, tmp1, tmp2, entry());
872 __ bind(*continuation());
873 }
874
875
876 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
877 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
878 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
879
880 // On x86, there is a significant penalty with unaligned branch target, for example
881 // when the target instruction straggles the fetch line. It makes (performance) sense
882 // to spend some code size to align the target better.
883 __ align(16);
884 __ bind(*entry());
885
886 // If we need to load ourselves, do it here.
887 if (_do_load) {
888 if (_narrow) {
889 __ movl(_obj, _addr);
890 } else {
891 __ movq(_obj, _addr);
892 }
893 }
894
895 // If the object is null, there is no point in applying barriers.
896 maybe_far_jump_if_zero(masm, _obj);
897
898 // We need to make sure that loads done by callers survive across slow-path calls.
899 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
900 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
901 if (!_do_load || needs_both_barriers) {
902 preserve(_obj);
903 }
904
905 // Go for barriers. Barriers can return straight to continuation, as long
906 // as another barrier is not needed.
907 if (needs_both_barriers) {
908 keepalive(masm, nullptr);
909 lrb(masm);
910 } else if (_needs_keep_alive_barrier) {
911 keepalive(masm, continuation());
912 } else if (_needs_load_ref_barrier) {
913 lrb(masm);
914 } else {
915 ShouldNotReachHere();
916 }
917 }
918
919 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
920 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
921 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
922
923 Label L_through, L_pop_and_slow;
924
925 // If another barrier is enabled as well, do a check for a specific barrier.
926 if (_needs_load_ref_barrier) {
927 assert(L_done == nullptr, "Should be");
928 char state_to_check = ShenandoahHeap::MARKING;
929 patchable_jump_if_not_gc_state(masm, state_to_check, noreg, noreg, &L_through);
930 }
931
932 // Need temp to work, allocate one now.
933 bool tmp_live;
934 Register tmp = select_temp_register(tmp_live);
935 if (tmp_live) {
936 __ push(tmp);
937 }
938
939 // Fast-path: put object into buffer.
940 // If buffer is already full, go slow.
941 __ movptr(tmp, index);
942 __ subptr(tmp, wordSize);
943 // VerifyOops adds code for decoded oop and needs long jump here
944 __ jcc(Assembler::below, L_pop_and_slow);
945 __ movptr(index, tmp);
946 __ addptr(tmp, buffer);
947
948 // Store the object in queue.
949 // If object is narrow, we need to decode it before inserting.
950 // We can skip the re-encoding if we know that object is not preserved.
951 if (_narrow) {
952 __ decode_heap_oop_not_null(_obj);
953 }
954 __ movptr(Address(tmp, 0), _obj);
955 if (_narrow && is_preserved(_obj)) {
956 __ encode_heap_oop_not_null(_obj);
957 }
958
959 // Fast-path exits here.
960 if (tmp_live) {
961 __ pop(tmp);
962 }
963
964 if (L_done != nullptr) {
965 __ jmp(*L_done);
966 } else {
967 __ jmp(L_through);
968 }
969
970 // Slow-path: call runtime to handle.
971 // Need to pop tmp immediately for stack to remain aligned.
972 __ bind(L_pop_and_slow);
973 if (tmp_live) {
974 __ pop(tmp);
975 }
976 {
977 SaveLiveRegisters slr(&masm, this);
978
979 // Shuffle in the arguments. The end result should be:
980 // c_rarg0 <-- obj
981 if (c_rarg0 != _obj) {
982 __ mov(c_rarg0, _obj);
983 }
984
985 // Go to runtime and handle the rest there.
986 // Use rax as scratch, as it will be saved if live.
987 __ call(RuntimeAddress(keepalive_runtime_entry_addr()), rax);
988 }
989 if (L_done != nullptr) {
990 __ jmp(*L_done);
991 } else {
992 __ bind(L_through);
993 }
994 }
995
996 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
997 Label L_pop_and_slow, L_slow;
998
999 // If another barrier is enabled as well, do a check for a specific barrier.
1000 if (_needs_keep_alive_barrier) {
1001 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
1002 patchable_jump_if_not_gc_state(masm, state_to_check, noreg, noreg, continuation());
1003 }
1004
1005 // If weak references are being processed, weak/phantom loads need to go slow,
1006 // regardless of their cset status.
1007 if (_needs_load_ref_weak_barrier) {
1008 char state_to_check = ShenandoahHeap::WEAK_ROOTS;
1009 patchable_jump_if_gc_state(masm, state_to_check, noreg, noreg, &L_slow);
1010 }
1011
1012 bool is_aot = AOTCodeCache::is_on_for_dump();
1013
1014 // Need temp to work, allocate one now.
1015 bool tmp_live;
1016 Register tmp = select_temp_register(tmp_live, /* skip_reg1 = */ is_aot ? rcx : noreg);
1017 if (tmp_live) {
1018 __ push(tmp);
1019 }
1020
1021 // Compute the cset bitmap index
1022 if (_narrow) {
1023 __ decode_heap_oop_not_null(tmp, _obj);
1024 } else {
1025 __ movptr(tmp, _obj);
1026 }
1027
1028 Address cset_addr_arg;
1029 intptr_t cset_addr = reinterpret_cast<intptr_t>(ShenandoahHeap::in_cset_fast_test_addr());
1030 if (!is_aot && cset_addr < INT32_MAX) {
1031 // Cset bitmap is at easily encodeable address. Just use it as displacement.
1032 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1033 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr));
1034 } else {
1035 bool tmp2_live;
1036 Register tmp2 = select_temp_register(tmp2_live, /* skip_reg1 = */ tmp, /* skip_reg2 = */ is_aot ? rcx : noreg);
1037 if (tmp2_live) {
1038 __ push(tmp2);
1039 }
1040 if (is_aot) {
1041 // Generating AOT code, pull the cset bitmap and region shift from AOT table.
1042 assert_different_registers(tmp, tmp2, rcx);
1043 __ push(rcx);
1044 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
1045 __ movl(rcx, Address(rcx));
1046 __ shrptr(tmp);
1047 __ pop(rcx);
1048 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
1049 __ addptr(tmp, Address(tmp2));
1050 } else {
1051 // Cset bitmap is far away. Add its address fully.
1052 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1053 __ movptr(tmp2, cset_addr);
1054 __ addptr(tmp, tmp2);
1055 }
1056 if (tmp2_live) {
1057 __ pop(tmp2);
1058 }
1059 cset_addr_arg = Address(tmp, 0);
1060 }
1061
1062 // Cset-check. Fall-through to slow if in collection set.
1063 __ cmpb(cset_addr_arg, 0);
1064 if (tmp_live) {
1065 __ jccb(Assembler::notEqual, L_pop_and_slow);
1066 __ pop(tmp);
1067 __ jmp(*continuation());
1068 } else {
1069 // Nothing else to do, jump back
1070 __ jcc(Assembler::equal, *continuation());
1071 }
1072
1073 // Slow path
1074 __ bind(L_pop_and_slow);
1075 // Need to pop tmp immediately for stack to remain aligned.
1076 if (tmp_live) {
1077 __ pop(tmp);
1078 }
1079 __ bind(L_slow);
1080
1081 // Obj is the result, need to temporarily stop preserving it.
1082 bool is_obj_preserved = is_preserved(_obj);
1083 if (is_obj_preserved) {
1084 dont_preserve(_obj);
1085 }
1086 {
1087 SaveLiveRegisters slr(&masm, this);
1088
1089 assert_different_registers(rax, c_rarg0, c_rarg1);
1090
1091 // Shuffle in the arguments. The end result should be:
1092 // c_rarg0 <-- obj
1093 // c_rarg1 <-- lea(addr)
1094 if (_obj == c_rarg0) {
1095 __ lea(c_rarg1, _addr);
1096 } else if (_obj == c_rarg1) {
1097 // Set up arguments in reverse, and then flip them
1098 __ lea(c_rarg0, _addr);
1099 __ xchgptr(c_rarg0, c_rarg1);
1100 } else {
1101 assert_different_registers(_obj, c_rarg0, c_rarg1);
1102 __ lea(c_rarg1, _addr);
1103 __ movptr(c_rarg0, _obj);
1104 }
1105
1106 // Go to runtime and handle the rest there.
1107 // Use rax as scratch, as it will be clobbered by result anyway.
1108 __ call(RuntimeAddress(lrb_runtime_entry_addr()), rax);
1109
1110 // Save the result where needed.
1111 if (_narrow) {
1112 __ movl(_obj, rax);
1113 } else if (_obj != rax) {
1114 __ movptr(_obj, rax);
1115 }
1116 }
1117 if (is_obj_preserved) {
1118 preserve(_obj);
1119 }
1120
1121 __ jmp(*continuation());
1122 }
1123
1124 int ShenandoahBarrierStubC2::available_gp_registers() {
1125 return Register::available_gp_registers();
1126 }
1127
1128 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
1129 return r == rsp || r == rbp || r == r12_heapbase || r == r15_thread;
1130 }
1131
1132 void ShenandoahBarrierStubC2::post_init() {
1133 // Do nothing.
1134 }
1135
1136 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
1137 if (_narrow) {
1138 __ testl(reg, reg);
1139 } else {
1140 __ testq(reg, reg);
1141 }
1142 __ jcc(Assembler::zero, *continuation());
1143 }
1144
1145 #endif // COMPILER2