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