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 }
610
611 bool ShenandoahBarrierSetAssembler::is_patchable_nop(address pc) {
612 if (*(pc + 0) != 0x0F) return false;
613 if (*(pc + 1) != 0x1F) return false;
614 if (*(pc + 2) != 0x44) return false;
615 if (*(pc + 3) != 0x00) return false;
616 if (*(pc + 4) != 0x00) return false;
617 return true;
618 }
619
620 void ShenandoahBarrierSetAssembler::insert_patchable_jump(address pc, address target_pc) {
621 int32_t disp = checked_cast<int32_t>((intptr_t)target_pc - ((intptr_t)pc + 5));
622
623 *(pc + 0) = 0xE9;
624 *(pc + 1) = (disp >> 0) & 0xFF;
625 *(pc + 2) = (disp >> 8) & 0xFF;
626 *(pc + 3) = (disp >> 16) & 0xFF;
627 *(pc + 4) = (disp >> 24) & 0xFF;
628 }
629
630 bool ShenandoahBarrierSetAssembler::is_patchable_jump(address pc, address target_pc) {
631 int32_t disp = checked_cast<int32_t>((intptr_t)target_pc - ((intptr_t)pc + 5));
632
633 if (*(pc + 0) != 0xE9) return false;
634 if (*(pc + 1) != ((disp >> 0) & 0xFF)) return false;
635 if (*(pc + 2) != ((disp >> 8) & 0xFF)) return false;
636 if (*(pc + 3) != ((disp >> 16) & 0xFF)) return false;
637 if (*(pc + 4) != ((disp >> 24) & 0xFF)) return false;
638 return true;
639 }
640
641 #ifdef COMPILER1
642
643 #define __ ce->masm()->
644
645 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
646 __ bind(*stub->entry());
647
648 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
649
650 Register obj = stub->obj()->as_register();
651
652 if (stub->do_load()) {
653 ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, /* wide = */ false);
654 }
655 __ cmpptr(obj, NULL_WORD);
656 __ jcc(Assembler::equal, *stub->continuation());
657
658 ce->store_parameter(obj, 0);
659 __ call(RuntimeAddress(bs->keepalive_barrier_stub()));
660 __ jmp(*stub->continuation());
661 }
662
663 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
664 __ bind(*stub->entry());
665
666 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
667
668 Register obj = stub->obj()->as_register();
669 Register addr = stub->addr()->as_pointer_register();
670 Register slow_result = stub->slow_result()->as_register();
671 assert_different_registers(obj, addr, slow_result);
672 assert(slow_result == rax, "C1 must know about our slow call result register");
673
674 ce->store_parameter(obj, 0);
675 ce->store_parameter(addr, 1);
676 __ call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
677 if (obj != slow_result) {
678 __ mov(obj, slow_result);
679 }
680
681 __ jmp(*stub->continuation());
682 }
683
684 #undef __
685
686 #define __ sasm->
687
688 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
689 __ prologue("shenandoah_keepalive_barrier", false);
690 const Register tmp_obj = rax;
691 const Register tmp = rdx;
692 __ push(tmp);
693 __ push(tmp_obj);
694 __ load_parameter(0, tmp_obj);
695 satb_barrier(sasm, noreg, tmp_obj, tmp);
696 __ pop(tmp_obj);
697 __ pop(tmp);
698 __ epilogue();
699 }
700
701 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
702 __ prologue("shenandoah_load_reference_barrier", false);
703 const Register tmp_obj = rax;
704 const Register tmp_addr = rdx;
705 __ push(tmp_addr);
706 __ load_parameter(0, tmp_obj);
707 __ load_parameter(1, tmp_addr);
708 load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
709 __ pop(tmp_addr);
710 __ epilogue();
711 }
712
713 #undef __
714
715 #endif // COMPILER1
716
717 #ifdef COMPILER2
718
719 #undef __
720 #define __ masm->
721
722 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
723 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
724 if (narrow) {
725 __ movl(dst, src);
726 } else {
727 __ movq(dst, src);
728 }
729
730 ShenandoahBarrierStubC2::load_post(masm, node, dst, src, noreg, noreg, narrow);
731 }
732
733 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
734 Address dst, bool dst_narrow,
735 Register src, bool src_narrow,
736 Register tmp) {
737
738 ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp, noreg, noreg, dst_narrow);
739
740 // Need to encode into tmp, because we cannot clobber src.
741 if (dst_narrow && !src_narrow) {
742 __ movq(tmp, src);
743 if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
744 __ encode_heap_oop(tmp);
745 } else {
746 __ encode_heap_oop_not_null(tmp);
747 }
748 src = tmp;
749 }
750
751 // Do the actual store
752 if (dst_narrow) {
753 __ movl(dst, src);
754 } else {
755 __ movq(dst, src);
756 }
757
758 ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp, noreg);
759 }
760
761 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
762 Register res, Address addr,
763 Register oldval, Register newval, Register tmp,
764 bool narrow) {
765
766 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
767
768 // Oldval and newval cannot be clobbered by aliasing with tmp.
769 assert_different_registers(oldval, tmp);
770 assert_different_registers(newval, tmp);
771
772 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
773
774 // CAS!
775 __ lock();
776 if (narrow) {
777 __ cmpxchgl(newval, addr);
778 } else {
779 __ cmpxchgptr(newval, addr);
780 }
781
782 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
783 if (res != noreg) {
784 __ setcc(Assembler::equal, res);
785 }
786
787 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
788 }
789
790 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
791 assert_different_registers(newval, tmp);
792
793 ShenandoahBarrierStubC2::load_store_pre(masm, node, addr, tmp, noreg, noreg, narrow);
794
795 if (narrow) {
796 __ xchgl(newval, addr);
797 } else {
798 __ xchgq(newval, addr);
799 }
800
801 ShenandoahBarrierStubC2::load_store_post(masm, node, addr, tmp, noreg);
802 }
803
804 #undef __
805 #define __ masm.
806
807 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2) {
808 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
809
810 __ lea(tmp1, addr);
811 __ shrptr(tmp1, CardTable::card_shift());
812 __ addptr(tmp1, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
813 Address card_address(tmp1, 0);
814
815 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
816 Label L_done;
817 if (UseCondCardMark) {
818 __ cmpb(card_address, 0);
819 __ jccb(Assembler::equal, L_done);
820 }
821 if (UseCompressedOops && CompressedOops::base() == nullptr) {
822 __ movb(card_address, r12);
823 } else {
824 __ movb(card_address, 0);
825 }
826 __ bind(L_done);
827 }
828
829 void ShenandoahBarrierStubC2::patchable_jump(MacroAssembler& masm, const char gc_state, bool jump_when_state, Register tmp1, Register tmp2, Label* L_target) {
830 #ifdef ASSERT
831 Label L_fake_entry, L_good;
832 Address gc_state_addr(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
833
834 // Emit the secondary jump and use it to cross-check against the actual GC state.
835 // This also checks that all interesting GC state transitions are done non-racily
836 // from the perspective of the thread executing the nmethod.
837 __ relocate(patchable_barrier_Relocation::spec(ShenandoahNMethod::encode_to_reloc(gc_state, jump_when_state)));
838 __ jmp(L_fake_entry, /* maybe_short = */ false);
839
840 // Currently hot-patched to NOP.
841 __ testb(gc_state_addr, gc_state);
842 __ jccb(jump_when_state ? Assembler::zero : Assembler::notZero, L_good);
843 __ hlt();
844
845 // Currently hot-patched to JUMP.
846 __ bind(L_fake_entry);
847 __ testb(gc_state_addr, gc_state);
848 __ jccb(jump_when_state ? Assembler::notZero : Assembler::zero, L_good);
849 __ hlt();
850
851 __ bind(L_good);
852 #endif
853
854 PhaseOutput* const output = Compile::current()->output();
855 if (!output->in_scratch_emit_size()) {
856 // Emit the unconditional branch in the first version of the method.
857 // Let the rest of runtime figure out how to manage it.
858 __ relocate(patchable_barrier_Relocation::spec(ShenandoahNMethod::encode_to_reloc(gc_state, jump_when_state)));
859 __ jmp(*L_target, /* maybe_short = */ false);
860 } else {
861 // Avoid binding L_target in scratch emits.
862 // We know the patchable check is exactly 5 bytes long.
863 __ nop(5);
864 }
865 }
866
867 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp1, Register tmp2) {
868 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
869 patchable_jump_if_gc_state(masm, test_state, tmp1, tmp2, entry());
870 __ bind(*continuation());
871 }
872
873
874 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
875 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
876 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
877
878 // On x86, there is a significant penalty with unaligned branch target, for example
879 // when the target instruction straggles the fetch line. It makes (performance) sense
880 // to spend some code size to align the target better.
881 __ align(16);
882 __ bind(*entry());
883
884 // If we need to load ourselves, do it here.
885 if (_do_load) {
886 if (_narrow) {
887 __ movl(_obj, _addr);
888 } else {
889 __ movq(_obj, _addr);
890 }
891 }
892
893 // If the object is null, there is no point in applying barriers.
894 maybe_far_jump_if_zero(masm, _obj);
895
896 // We need to make sure that loads done by callers survive across slow-path calls.
897 // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
898 bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
899 if (!_do_load || needs_both_barriers) {
900 preserve(_obj);
901 }
902
903 // Go for barriers. Barriers can return straight to continuation, as long
904 // as another barrier is not needed.
905 if (needs_both_barriers) {
906 keepalive(masm, nullptr);
907 lrb(masm);
908 } else if (_needs_keep_alive_barrier) {
909 keepalive(masm, continuation());
910 } else if (_needs_load_ref_barrier) {
911 lrb(masm);
912 } else {
913 ShouldNotReachHere();
914 }
915 }
916
917 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
918 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
919 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
920
921 Label L_through, L_pop_and_slow;
922
923 // If another barrier is enabled as well, do a check for a specific barrier.
924 if (_needs_load_ref_barrier) {
925 assert(L_done == nullptr, "Should be");
926 char state_to_check = ShenandoahHeap::MARKING;
927 patchable_jump_if_not_gc_state(masm, state_to_check, noreg, noreg, &L_through);
928 }
929
930 // Need temp to work, allocate one now.
931 bool tmp_live;
932 Register tmp = select_temp_register(tmp_live);
933 if (tmp_live) {
934 __ push(tmp);
935 }
936
937 // Fast-path: put object into buffer.
938 // If buffer is already full, go slow.
939 __ movptr(tmp, index);
940 __ subptr(tmp, wordSize);
941 // VerifyOops adds code for decoded oop and needs long jump here
942 __ jcc(Assembler::below, L_pop_and_slow);
943 __ movptr(index, tmp);
944 __ addptr(tmp, buffer);
945
946 // Store the object in queue.
947 // If object is narrow, we need to decode it before inserting.
948 // We can skip the re-encoding if we know that object is not preserved.
949 if (_narrow) {
950 __ decode_heap_oop_not_null(_obj);
951 }
952 __ movptr(Address(tmp, 0), _obj);
953 if (_narrow && is_preserved(_obj)) {
954 __ encode_heap_oop_not_null(_obj);
955 }
956
957 // Fast-path exits here.
958 if (tmp_live) {
959 __ pop(tmp);
960 }
961
962 if (L_done != nullptr) {
963 __ jmp(*L_done);
964 } else {
965 __ jmp(L_through);
966 }
967
968 // Slow-path: call runtime to handle.
969 // Need to pop tmp immediately for stack to remain aligned.
970 __ bind(L_pop_and_slow);
971 if (tmp_live) {
972 __ pop(tmp);
973 }
974 {
975 SaveLiveRegisters slr(&masm, this);
976
977 // Shuffle in the arguments. The end result should be:
978 // c_rarg0 <-- obj
979 if (c_rarg0 != _obj) {
980 __ mov(c_rarg0, _obj);
981 }
982
983 // Go to runtime and handle the rest there.
984 // Use rax as scratch, as it will be saved if live.
985 __ call(RuntimeAddress(keepalive_runtime_entry_addr()), rax);
986 }
987 if (L_done != nullptr) {
988 __ jmp(*L_done);
989 } else {
990 __ bind(L_through);
991 }
992 }
993
994 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
995 Label L_pop_and_slow, L_slow;
996
997 // If another barrier is enabled as well, do a check for a specific barrier.
998 if (_needs_keep_alive_barrier) {
999 char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
1000 patchable_jump_if_not_gc_state(masm, state_to_check, noreg, noreg, continuation());
1001 }
1002
1003 // If weak references are being processed, weak/phantom loads need to go slow,
1004 // regardless of their cset status.
1005 if (_needs_load_ref_weak_barrier) {
1006 char state_to_check = ShenandoahHeap::WEAK_ROOTS;
1007 patchable_jump_if_gc_state(masm, state_to_check, noreg, noreg, &L_slow);
1008 }
1009
1010 bool is_aot = AOTCodeCache::is_on_for_dump();
1011
1012 // Need temp to work, allocate one now.
1013 bool tmp_live;
1014 Register tmp = select_temp_register(tmp_live, /* skip_reg1 = */ is_aot ? rcx : noreg);
1015 if (tmp_live) {
1016 __ push(tmp);
1017 }
1018
1019 // Compute the cset bitmap index
1020 if (_narrow) {
1021 __ decode_heap_oop_not_null(tmp, _obj);
1022 } else {
1023 __ movptr(tmp, _obj);
1024 }
1025
1026 Address cset_addr_arg;
1027 intptr_t cset_addr = reinterpret_cast<intptr_t>(ShenandoahHeap::in_cset_fast_test_addr());
1028 if (!is_aot && cset_addr < INT32_MAX) {
1029 // Cset bitmap is at easily encodeable address. Just use it as displacement.
1030 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1031 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr));
1032 } else {
1033 bool tmp2_live;
1034 Register tmp2 = select_temp_register(tmp2_live, /* skip_reg1 = */ tmp, /* skip_reg2 = */ is_aot ? rcx : noreg);
1035 if (tmp2_live) {
1036 __ push(tmp2);
1037 }
1038 if (is_aot) {
1039 // Generating AOT code, pull the cset bitmap and region shift from AOT table.
1040 assert_different_registers(tmp, tmp2, rcx);
1041 __ push(rcx);
1042 __ lea(rcx, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
1043 __ movl(rcx, Address(rcx));
1044 __ shrptr(tmp);
1045 __ pop(rcx);
1046 __ lea(tmp2, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
1047 __ addptr(tmp, Address(tmp2));
1048 } else {
1049 // Cset bitmap is far away. Add its address fully.
1050 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1051 __ movptr(tmp2, cset_addr);
1052 __ addptr(tmp, tmp2);
1053 }
1054 if (tmp2_live) {
1055 __ pop(tmp2);
1056 }
1057 cset_addr_arg = Address(tmp, 0);
1058 }
1059
1060 // Cset-check. Fall-through to slow if in collection set.
1061 __ cmpb(cset_addr_arg, 0);
1062 if (tmp_live) {
1063 __ jccb(Assembler::notEqual, L_pop_and_slow);
1064 __ pop(tmp);
1065 __ jmp(*continuation());
1066 } else {
1067 // Nothing else to do, jump back
1068 __ jcc(Assembler::equal, *continuation());
1069 }
1070
1071 // Slow path
1072 __ bind(L_pop_and_slow);
1073 // Need to pop tmp immediately for stack to remain aligned.
1074 if (tmp_live) {
1075 __ pop(tmp);
1076 }
1077 __ bind(L_slow);
1078
1079 // Obj is the result, need to temporarily stop preserving it.
1080 bool is_obj_preserved = is_preserved(_obj);
1081 if (is_obj_preserved) {
1082 dont_preserve(_obj);
1083 }
1084 {
1085 SaveLiveRegisters slr(&masm, this);
1086
1087 assert_different_registers(rax, c_rarg0, c_rarg1);
1088
1089 // Shuffle in the arguments. The end result should be:
1090 // c_rarg0 <-- obj
1091 // c_rarg1 <-- lea(addr)
1092 if (_obj == c_rarg0) {
1093 __ lea(c_rarg1, _addr);
1094 } else if (_obj == c_rarg1) {
1095 // Set up arguments in reverse, and then flip them
1096 __ lea(c_rarg0, _addr);
1097 __ xchgptr(c_rarg0, c_rarg1);
1098 } else {
1099 assert_different_registers(_obj, c_rarg0, c_rarg1);
1100 __ lea(c_rarg1, _addr);
1101 __ movptr(c_rarg0, _obj);
1102 }
1103
1104 // Go to runtime and handle the rest there.
1105 // Use rax as scratch, as it will be clobbered by result anyway.
1106 __ call(RuntimeAddress(lrb_runtime_entry_addr()), rax);
1107
1108 // Save the result where needed.
1109 if (_narrow) {
1110 __ movl(_obj, rax);
1111 } else if (_obj != rax) {
1112 __ movptr(_obj, rax);
1113 }
1114 }
1115 if (is_obj_preserved) {
1116 preserve(_obj);
1117 }
1118
1119 __ jmp(*continuation());
1120 }
1121
1122 int ShenandoahBarrierStubC2::available_gp_registers() {
1123 return Register::available_gp_registers();
1124 }
1125
1126 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
1127 return r == rsp || r == rbp || r == r12_heapbase || r == r15_thread;
1128 }
1129
1130 void ShenandoahBarrierStubC2::post_init() {
1131 // Do nothing.
1132 }
1133
1134 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
1135 if (_narrow) {
1136 __ testl(reg, reg);
1137 } else {
1138 __ testq(reg, reg);
1139 }
1140 __ jcc(Assembler::zero, *continuation());
1141 }
1142
1143 #endif // COMPILER2