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/shenandoahForwarding.hpp"
32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
33 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
34 #include "gc/shenandoah/shenandoahRuntime.hpp"
35 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "runtime/sharedRuntime.hpp"
39 #include "utilities/macros.hpp"
40 #ifdef COMPILER1
41 #include "c1/c1_LIRAssembler.hpp"
42 #include "c1/c1_MacroAssembler.hpp"
43 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
44 #endif
45
46 #define __ masm->
47
48 static void save_machine_state(MacroAssembler* masm, bool handle_gpr, bool handle_fp) {
49 if (handle_gpr) {
50 __ push_IU_state();
51 }
52
53 if (handle_fp) {
54 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
55 assert(Argument::n_float_register_parameters_j == 8, "8 fp registers to save at java call");
56
57 const int xmm_size = wordSize * 2;
58 __ subptr(rsp, xmm_size * 8);
59 __ movdbl(Address(rsp, xmm_size * 0), xmm0);
60 __ movdbl(Address(rsp, xmm_size * 1), xmm1);
61 __ movdbl(Address(rsp, xmm_size * 2), xmm2);
62 __ movdbl(Address(rsp, xmm_size * 3), xmm3);
63 __ movdbl(Address(rsp, xmm_size * 4), xmm4);
64 __ movdbl(Address(rsp, xmm_size * 5), xmm5);
65 __ movdbl(Address(rsp, xmm_size * 6), xmm6);
66 __ movdbl(Address(rsp, xmm_size * 7), xmm7);
67 }
68 }
69
70 static void restore_machine_state(MacroAssembler* masm, bool handle_gpr, bool handle_fp) {
71 if (handle_fp) {
72 const int xmm_size = wordSize * 2;
73 __ movdbl(xmm0, Address(rsp, xmm_size * 0));
74 __ movdbl(xmm1, Address(rsp, xmm_size * 1));
75 __ movdbl(xmm2, Address(rsp, xmm_size * 2));
76 __ movdbl(xmm3, Address(rsp, xmm_size * 3));
77 __ movdbl(xmm4, Address(rsp, xmm_size * 4));
78 __ movdbl(xmm5, Address(rsp, xmm_size * 5));
79 __ movdbl(xmm6, Address(rsp, xmm_size * 6));
80 __ movdbl(xmm7, Address(rsp, xmm_size * 7));
81 __ addptr(rsp, xmm_size * 8);
82 }
83
84 if (handle_gpr) {
85 __ pop_IU_state();
86 }
87 }
88
89 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
90 Register src, Register dst, Register count) {
91
92 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
93
94 if (is_reference_type(type)) {
95 if (ShenandoahCardBarrier) {
96 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
97 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
98 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
99
100 // We need to save the original element count because the array copy stub
101 // will destroy the value and we need it for the card marking barrier.
102 if (!checkcast) {
103 if (!obj_int) {
104 // Save count for barrier
105 __ movptr(r11, count);
106 } else if (disjoint) {
107 // Save dst in r11 in the disjoint case
108 __ movq(r11, dst);
109 }
110 }
111 }
112
113 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
114 Register thread = r15_thread;
115 assert_different_registers(src, dst, count, thread);
116
117 Label L_done;
118 // Short-circuit if count == 0.
119 __ testptr(count, count);
120 __ jcc(Assembler::zero, L_done);
121
122 // Avoid runtime call when not active.
123 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
124 int flags;
125 if (ShenandoahSATBBarrier && dest_uninitialized) {
126 flags = ShenandoahHeap::HAS_FORWARDED;
127 } else {
128 flags = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING;
129 }
130 __ testb(gc_state, flags);
131 __ jcc(Assembler::zero, L_done);
132
133 save_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ false);
134
135 assert(src == rdi, "expected");
136 assert(dst == rsi, "expected");
137 assert(count == rdx, "expected");
138 if (UseCompressedOops) {
139 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop),
140 src, dst, count);
141 } else {
142 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop),
143 src, dst, count);
144 }
145
146 restore_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ false);
147
148 __ bind(L_done);
149 }
150 }
151
152 }
153
154 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
155 Register src, Register dst, Register count) {
156
157 if (ShenandoahCardBarrier && is_reference_type(type)) {
158 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
159 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
160 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
161 Register tmp = rax;
162
163 if (!checkcast) {
164 if (!obj_int) {
165 // Save count for barrier
166 count = r11;
167 } else if (disjoint) {
168 // Use the saved dst in the disjoint case
169 dst = r11;
170 }
171 } else {
172 tmp = rscratch1;
173 }
174 gen_write_ref_array_post_barrier(masm, decorators, dst, count, tmp);
175 }
176 }
177
178 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
179 Register obj,
180 Register pre_val,
181 Register tmp,
182 bool tosca_live,
183 bool expand_call) {
184 assert(ShenandoahSATBBarrier, "Should be checked by caller");
185
186 // If expand_call is true then we expand the call_VM_leaf macro
187 // directly to skip generating the check by
188 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
189
190 const Register thread = r15_thread;
191
192 Label done;
193 Label runtime;
194
195 assert(pre_val != noreg, "check this code");
196
197 if (obj != noreg) {
198 assert_different_registers(obj, pre_val, tmp);
199 assert(pre_val != rax, "check this code");
200 }
201
202 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
203 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
204
205 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
206 __ testb(gc_state, ShenandoahHeap::MARKING);
207 __ jcc(Assembler::zero, done);
208
209 // Do we need to load the previous value?
210 if (obj != noreg) {
211 __ load_heap_oop(pre_val, Address(obj, 0), noreg, AS_RAW);
212 }
213
214 // Is the previous value null?
215 __ cmpptr(pre_val, NULL_WORD);
216 __ jcc(Assembler::equal, done);
217
218 // Can we store original value in the thread's buffer?
219 // Is index == 0?
220 // (The index field is typed as size_t.)
221
222 __ movptr(tmp, index); // tmp := *index_adr
223 __ cmpptr(tmp, 0); // tmp == 0?
224 __ jcc(Assembler::equal, runtime); // If yes, goto runtime
225
226 __ subptr(tmp, wordSize); // tmp := tmp - wordSize
227 __ movptr(index, tmp); // *index_adr := tmp
228 __ addptr(tmp, buffer); // tmp := tmp + *buffer_adr
229
230 // Record the previous value
231 __ movptr(Address(tmp, 0), pre_val);
232 __ jmp(done);
233
234 __ bind(runtime);
235 // save the live input values
236 if(tosca_live) __ push(rax);
237
238 if (obj != noreg && obj != rax)
239 __ push(obj);
240
241 if (pre_val != rax)
242 __ push(pre_val);
243
244 // Calling the runtime using the regular call_VM_leaf mechanism generates
245 // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
246 // that checks that the *(ebp+frame::interpreter_frame_last_sp) == nullptr.
247 //
248 // If we care generating the pre-barrier without a frame (e.g. in the
249 // intrinsified Reference.get() routine) then ebp might be pointing to
250 // the caller frame and so this check will most likely fail at runtime.
251 //
252 // Expanding the call directly bypasses the generation of the check.
253 // So when we do not have have a full interpreter frame on the stack
254 // expand_call should be passed true.
255
256 // We move pre_val into c_rarg0 early, in order to avoid smashing it, should
257 // pre_val be c_rarg1 (where the call prologue would copy thread argument).
258 // Note: this should not accidentally smash thread, because thread is always r15.
259 assert(thread != c_rarg0, "smashed arg");
260 if (c_rarg0 != pre_val) {
261 __ mov(c_rarg0, pre_val);
262 }
263
264 if (expand_call) {
265 assert(pre_val != c_rarg1, "smashed arg");
266 if (c_rarg1 != thread) {
267 __ mov(c_rarg1, thread);
268 }
269 // Already moved pre_val into c_rarg0 above
270 __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), 1);
271 } else {
272 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
273 }
274
275 // save the live input values
276 if (pre_val != rax)
277 __ pop(pre_val);
278
279 if (obj != noreg && obj != rax)
280 __ pop(obj);
281
282 if(tosca_live) __ pop(rax);
283
284 __ bind(done);
285 }
286
287 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address src, DecoratorSet decorators) {
288 assert(ShenandoahLoadRefBarrier, "Should be enabled");
289
290 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
291 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
292 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
293 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
294 bool is_narrow = UseCompressedOops && !is_native;
295
296 Label heap_stable, not_cset;
297
298 __ block_comment("load_reference_barrier { ");
299
300 // Check if GC is active
301 Register thread = r15_thread;
302
303 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
304 int flags = ShenandoahHeap::HAS_FORWARDED;
305 if (!is_strong) {
306 flags |= ShenandoahHeap::WEAK_ROOTS;
307 }
308 __ testb(gc_state, flags);
309 __ jcc(Assembler::zero, heap_stable);
310
311 Register tmp1 = noreg, tmp2 = noreg;
312 if (is_strong) {
313 // Test for object in cset
314 // Allocate temporary registers
315 for (int i = 0; i < 8; i++) {
316 Register r = as_Register(i);
317 if (r != rsp && r != rbp && r != dst && r != src.base() && r != src.index()) {
318 if (tmp1 == noreg) {
319 tmp1 = r;
320 } else {
321 tmp2 = r;
322 break;
323 }
324 }
325 }
326 assert(tmp1 != noreg, "tmp1 allocated");
327 assert(tmp2 != noreg, "tmp2 allocated");
328 assert_different_registers(tmp1, tmp2, src.base(), src.index());
329 assert_different_registers(tmp1, tmp2, dst);
330
331 __ push(tmp1);
332 __ push(tmp2);
333
334 // Optimized cset-test
335 __ movptr(tmp1, dst);
336 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
337 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
338 __ movbool(tmp1, Address(tmp1, tmp2, Address::times_1));
339 __ testbool(tmp1);
340 __ jcc(Assembler::zero, not_cset);
341 }
342
343 save_machine_state(masm, /* handle_gpr = */ false, /* handle_fp = */ true);
344
345 // The rest is saved with the optimized path
346
347 uint num_saved_regs = 4 + (dst != rax ? 1 : 0) + 4 + (UseAPX ? 16 : 0);
348 __ subptr(rsp, num_saved_regs * wordSize);
349 uint slot = num_saved_regs;
350 if (dst != rax) {
351 __ movptr(Address(rsp, (--slot) * wordSize), rax);
352 }
353 __ movptr(Address(rsp, (--slot) * wordSize), rcx);
354 __ movptr(Address(rsp, (--slot) * wordSize), rdx);
355 __ movptr(Address(rsp, (--slot) * wordSize), rdi);
356 __ movptr(Address(rsp, (--slot) * wordSize), rsi);
357 __ movptr(Address(rsp, (--slot) * wordSize), r8);
358 __ movptr(Address(rsp, (--slot) * wordSize), r9);
359 __ movptr(Address(rsp, (--slot) * wordSize), r10);
360 __ movptr(Address(rsp, (--slot) * wordSize), r11);
361 // Save APX extended registers r16–r31 if enabled
362 if (UseAPX) {
363 __ movptr(Address(rsp, (--slot) * wordSize), r16);
364 __ movptr(Address(rsp, (--slot) * wordSize), r17);
365 __ movptr(Address(rsp, (--slot) * wordSize), r18);
366 __ movptr(Address(rsp, (--slot) * wordSize), r19);
367 __ movptr(Address(rsp, (--slot) * wordSize), r20);
368 __ movptr(Address(rsp, (--slot) * wordSize), r21);
369 __ movptr(Address(rsp, (--slot) * wordSize), r22);
370 __ movptr(Address(rsp, (--slot) * wordSize), r23);
371 __ movptr(Address(rsp, (--slot) * wordSize), r24);
372 __ movptr(Address(rsp, (--slot) * wordSize), r25);
373 __ movptr(Address(rsp, (--slot) * wordSize), r26);
374 __ movptr(Address(rsp, (--slot) * wordSize), r27);
375 __ movptr(Address(rsp, (--slot) * wordSize), r28);
376 __ movptr(Address(rsp, (--slot) * wordSize), r29);
377 __ movptr(Address(rsp, (--slot) * wordSize), r30);
378 __ movptr(Address(rsp, (--slot) * wordSize), r31);
379 }
380 // r12-r15 are callee saved in all calling conventions
381 assert(slot == 0, "must use all slots");
382
383 // Shuffle registers such that dst is in c_rarg0 and addr in c_rarg1.
384 Register arg0 = c_rarg0, arg1 = c_rarg1;
385 if (dst == arg1) {
386 __ lea(arg0, src);
387 __ xchgptr(arg1, arg0);
388 } else {
389 __ lea(arg1, src);
390 __ movptr(arg0, dst);
391 }
392
393 if (is_strong) {
394 if (is_narrow) {
395 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), arg0, arg1);
396 } else {
397 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), arg0, arg1);
398 }
399 } else if (is_weak) {
400 if (is_narrow) {
401 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow), arg0, arg1);
402 } else {
403 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak), arg0, arg1);
404 }
405 } else {
406 assert(is_phantom, "only remaining strength");
407 assert(!is_narrow, "phantom access cannot be narrow");
408 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom), arg0, arg1);
409 }
410
411 // Restore APX extended registers r31–r16 if previously saved
412 if (UseAPX) {
413 __ movptr(r31, Address(rsp, (slot++) * wordSize));
414 __ movptr(r30, Address(rsp, (slot++) * wordSize));
415 __ movptr(r29, Address(rsp, (slot++) * wordSize));
416 __ movptr(r28, Address(rsp, (slot++) * wordSize));
417 __ movptr(r27, Address(rsp, (slot++) * wordSize));
418 __ movptr(r26, Address(rsp, (slot++) * wordSize));
419 __ movptr(r25, Address(rsp, (slot++) * wordSize));
420 __ movptr(r24, Address(rsp, (slot++) * wordSize));
421 __ movptr(r23, Address(rsp, (slot++) * wordSize));
422 __ movptr(r22, Address(rsp, (slot++) * wordSize));
423 __ movptr(r21, Address(rsp, (slot++) * wordSize));
424 __ movptr(r20, Address(rsp, (slot++) * wordSize));
425 __ movptr(r19, Address(rsp, (slot++) * wordSize));
426 __ movptr(r18, Address(rsp, (slot++) * wordSize));
427 __ movptr(r17, Address(rsp, (slot++) * wordSize));
428 __ movptr(r16, Address(rsp, (slot++) * wordSize));
429 }
430 __ movptr(r11, Address(rsp, (slot++) * wordSize));
431 __ movptr(r10, Address(rsp, (slot++) * wordSize));
432 __ movptr(r9, Address(rsp, (slot++) * wordSize));
433 __ movptr(r8, Address(rsp, (slot++) * wordSize));
434 __ movptr(rsi, Address(rsp, (slot++) * wordSize));
435 __ movptr(rdi, Address(rsp, (slot++) * wordSize));
436 __ movptr(rdx, Address(rsp, (slot++) * wordSize));
437 __ movptr(rcx, Address(rsp, (slot++) * wordSize));
438
439 if (dst != rax) {
440 __ movptr(dst, rax);
441 __ movptr(rax, Address(rsp, (slot++) * wordSize));
442 }
443
444 assert(slot == num_saved_regs, "must use all slots");
445 __ addptr(rsp, num_saved_regs * wordSize);
446
447 restore_machine_state(masm, /* handle_gpr = */ false, /* handle_fp = */ true);
448
449 __ bind(not_cset);
450
451 if (is_strong) {
452 __ pop(tmp2);
453 __ pop(tmp1);
454 }
455
456 __ bind(heap_stable);
457
458 __ block_comment("} load_reference_barrier");
459 }
460
461 //
462 // Arguments:
463 //
464 // Inputs:
465 // src: oop location, might be clobbered
466 // tmp1: scratch register, might not be valid.
467 //
468 // Output:
469 // dst: oop loaded from src location
470 //
471 // Kill:
472 // tmp1 (if it is valid)
473 //
474 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
475 Register dst, Address src, Register tmp1) {
476 // 1: non-reference load, no additional barrier is needed
477 if (!is_reference_type(type)) {
478 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
479 return;
480 }
481
482 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Not expected");
483
484 // 2: load a reference from src location and apply LRB if needed
485 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
486 Register result_dst = dst;
487 bool use_tmp1_for_dst = false;
488
489 // Preserve src location for LRB
490 if (dst == src.base() || dst == src.index()) {
491 // Use tmp1 for dst if possible, as it is not used in BarrierAssembler::load_at()
492 if (tmp1->is_valid() && tmp1 != src.base() && tmp1 != src.index()) {
493 dst = tmp1;
494 use_tmp1_for_dst = true;
495 } else {
496 dst = rdi;
497 __ push(dst);
498 }
499 assert_different_registers(dst, src.base(), src.index());
500 }
501
502 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
503
504 load_reference_barrier(masm, dst, src, decorators);
505
506 // Move loaded oop to final destination
507 if (dst != result_dst) {
508 __ movptr(result_dst, dst);
509
510 if (!use_tmp1_for_dst) {
511 __ pop(dst);
512 }
513
514 dst = result_dst;
515 }
516 } else {
517 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
518 }
519
520 // 3: apply keep-alive barrier if needed
521 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
522 save_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ true);
523
524 assert_different_registers(dst, tmp1, r15_thread);
525 // Generate the SATB pre-barrier code to log the value of
526 // the referent field in an SATB buffer.
527 satb_barrier(masm /* masm */,
528 noreg /* obj */,
529 dst /* pre_val */,
530 tmp1 /* tmp */,
531 true /* tosca_live */,
532 true /* expand_call */);
533
534 restore_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ true);
535 }
536 }
537
538 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
539 assert(ShenandoahCardBarrier, "Should have been checked by caller");
540
541 // Does a store check for the oop in register obj. The content of
542 // register obj is destroyed afterwards.
543 __ shrptr(obj, CardTable::card_shift());
544
545 // We'll use this register as the TLS base address and also later on
546 // to hold the byte_map_base.
547 Register thread = r15_thread;
548 Register tmp = rscratch1;
549
550 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
551 __ movptr(tmp, curr_ct_holder_addr);
552 Address card_addr(tmp, obj, Address::times_1);
553
554 int dirty = CardTable::dirty_card_val();
555 if (UseCondCardMark) {
556 Label L_already_dirty;
557 __ cmpb(card_addr, dirty);
558 __ jccb(Assembler::equal, L_already_dirty);
559 __ movb(card_addr, dirty);
560 __ bind(L_already_dirty);
561 } else {
562 __ movb(card_addr, dirty);
563 }
564 }
565
566 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
567 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
568
569 // 1: non-reference types require no barriers
570 if (!is_reference_type(type)) {
571 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
572 return;
573 }
574
575 // Flatten object address right away for simplicity: likely needed by barriers
576 assert_different_registers(val, tmp1, tmp2, tmp3, r15_thread);
577 if (dst.index() == noreg && dst.disp() == 0) {
578 if (dst.base() != tmp1) {
579 __ movptr(tmp1, dst.base());
580 }
581 } else {
582 __ lea(tmp1, dst);
583 }
584
585 bool storing_non_null = (val != noreg);
586
587 // 2: pre-barrier: SATB needs the previous value
588 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
589 satb_barrier(masm,
590 tmp1 /* obj */,
591 tmp2 /* pre_val */,
592 tmp3 /* tmp */,
593 storing_non_null /* tosca_live */,
594 false /* expand_call */);
595 }
596
597 // Store!
598 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
599
600 // 3: post-barrier: card barrier needs store address
601 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
602 card_barrier(masm, tmp1);
603 }
604 }
605
606 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
607 Register obj, Register tmp, Label& slowpath) {
608 Label done;
609 // Resolve jobject
610 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
611
612 // Check for null.
613 __ testptr(obj, obj);
614 __ jcc(Assembler::zero, done);
615
616 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
617 __ testb(gc_state, ShenandoahHeap::EVACUATION);
618 __ jccb(Assembler::notZero, slowpath);
619 __ bind(done);
620 }
621
622 #ifdef COMPILER2
623 void ShenandoahBarrierSetAssembler::try_resolve_weak_handle_in_c2(MacroAssembler* masm, Register obj, Label& slowpath) {
624 Label done;
625
626 // Resolve weak handle using the standard implementation.
627 BarrierSetAssembler::try_resolve_weak_handle_in_c2(masm, obj, slowpath);
628
629 // Check if the reference is null, and if it is, take the fast path.
630 __ testptr(obj, obj);
631 __ jcc(Assembler::zero, done);
632
633 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
634
635 // Check if the heap is under weak-reference/roots processing, in
636 // which case we need to take the slow path.
637 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
638 __ jcc(Assembler::notZero, slowpath);
639 __ bind(done);
640 }
641 #endif // COMPILER2
642
643 // Special Shenandoah CAS implementation that handles false negatives
644 // due to concurrent evacuation.
645 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm,
646 Register res, Address addr, Register oldval, Register newval,
647 bool exchange, Register tmp1, Register tmp2) {
648 assert(ShenandoahCASBarrier, "Should only be used when CAS barrier is enabled");
649 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
650 assert_different_registers(oldval, tmp1, tmp2);
651 assert_different_registers(newval, tmp1, tmp2);
652
653 Label L_success, L_failure;
654
655 // Remember oldval for retry logic below
656 if (UseCompressedOops) {
657 __ movl(tmp1, oldval);
658 } else {
659 __ movptr(tmp1, oldval);
660 }
661
662 // Step 1. Fast-path.
663 //
664 // Try to CAS with given arguments. If successful, then we are done.
665
666 if (UseCompressedOops) {
667 __ lock();
668 __ cmpxchgl(newval, addr);
669 } else {
670 __ lock();
671 __ cmpxchgptr(newval, addr);
672 }
673 __ jcc(Assembler::equal, L_success);
674
675 // Step 2. CAS had failed. This may be a false negative.
676 //
677 // The trouble comes when we compare the to-space pointer with the from-space
678 // pointer to the same object. To resolve this, it will suffice to resolve
679 // the value from memory -- this will give both to-space pointers.
680 // If they mismatch, then it was a legitimate failure.
681 //
682 // Before reaching to resolve sequence, see if we can avoid the whole shebang
683 // with filters.
684
685 // Filter: when offending in-memory value is null, the failure is definitely legitimate
686 __ testptr(oldval, oldval);
687 __ jcc(Assembler::zero, L_failure);
688
689 // Filter: when heap is stable, the failure is definitely legitimate
690 const Register thread = r15_thread;
691 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
692 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
693 __ jcc(Assembler::zero, L_failure);
694
695 if (UseCompressedOops) {
696 __ movl(tmp2, oldval);
697 __ decode_heap_oop(tmp2);
698 } else {
699 __ movptr(tmp2, oldval);
700 }
701
702 // Decode offending in-memory value.
703 // Test if-forwarded
704 __ testb(Address(tmp2, oopDesc::mark_offset_in_bytes()), markWord::marked_value);
705 __ jcc(Assembler::noParity, L_failure); // When odd number of bits, then not forwarded
706 __ jcc(Assembler::zero, L_failure); // When it is 00, then also not forwarded
707
708 // Load and mask forwarding pointer
709 __ movptr(tmp2, Address(tmp2, oopDesc::mark_offset_in_bytes()));
710 __ shrptr(tmp2, 2);
711 __ shlptr(tmp2, 2);
712
713 if (UseCompressedOops) {
714 __ decode_heap_oop(tmp1); // decode for comparison
715 }
716
717 // Now we have the forwarded offender in tmp2.
718 // Compare and if they don't match, we have legitimate failure
719 __ cmpptr(tmp1, tmp2);
720 __ jcc(Assembler::notEqual, L_failure);
721
722 // Step 3. Need to fix the memory ptr before continuing.
723 //
724 // At this point, we have from-space oldval in the register, and its to-space
725 // address is in tmp2. Let's try to update it into memory. We don't care if it
726 // succeeds or not. If it does, then the retrying CAS would see it and succeed.
727 // If this fixup fails, this means somebody else beat us to it, and necessarily
728 // with to-space ptr store. We still have to do the retry, because the GC might
729 // have updated the reference for us.
730
731 if (UseCompressedOops) {
732 __ encode_heap_oop(tmp2); // previously decoded at step 2.
733 }
734
735 if (UseCompressedOops) {
736 __ lock();
737 __ cmpxchgl(tmp2, addr);
738 } else {
739 __ lock();
740 __ cmpxchgptr(tmp2, addr);
741 }
742
743 // Step 4. Try to CAS again.
744 //
745 // This is guaranteed not to have false negatives, because oldval is definitely
746 // to-space, and memory pointer is to-space as well. Nothing is able to store
747 // from-space ptr into memory anymore. Make sure oldval is restored, after being
748 // garbled during retries.
749 //
750 if (UseCompressedOops) {
751 __ movl(oldval, tmp2);
752 } else {
753 __ movptr(oldval, tmp2);
754 }
755
756 if (UseCompressedOops) {
757 __ lock();
758 __ cmpxchgl(newval, addr);
759 } else {
760 __ lock();
761 __ cmpxchgptr(newval, addr);
762 }
763 if (!exchange) {
764 __ jccb(Assembler::equal, L_success); // fastpath, peeking into Step 5, no need to jump
765 }
766
767 // Step 5. If we need a boolean result out of CAS, set the flag appropriately.
768 // and promote the result. Note that we handle the flag from both the 1st and 2nd CAS.
769 // Otherwise, failure witness for CAE is in oldval on all paths, and we can return.
770
771 if (exchange) {
772 __ bind(L_failure);
773 __ bind(L_success);
774 } else {
775 assert(res != noreg, "need result register");
776
777 Label exit;
778 __ bind(L_failure);
779 __ xorptr(res, res);
780 __ jmpb(exit);
781
782 __ bind(L_success);
783 __ movptr(res, 1);
784 __ bind(exit);
785 }
786 }
787
788 #ifdef PRODUCT
789 #define BLOCK_COMMENT(str) /* nothing */
790 #else
791 #define BLOCK_COMMENT(str) __ block_comment(str)
792 #endif
793
794 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
795
796 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
797
798 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
799 Register addr, Register count,
800 Register tmp) {
801 assert(ShenandoahCardBarrier, "Should have been checked by caller");
802
803 Label L_loop, L_done;
804 const Register end = count;
805 assert_different_registers(addr, end);
806
807 // Zero count? Nothing to do.
808 __ testl(count, count);
809 __ jccb(Assembler::zero, L_done);
810
811 const Register thread = r15_thread;
812 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
813 __ movptr(tmp, curr_ct_holder_addr);
814
815 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
816 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
817 __ shrptr(addr, CardTable::card_shift());
818 __ shrptr(end, CardTable::card_shift());
819 __ subptr(end, addr); // end --> cards count
820
821 __ addptr(addr, tmp);
822
823 __ BIND(L_loop);
824 __ movb(Address(addr, count, Address::times_1), 0);
825 __ decrement(count);
826 __ jccb(Assembler::greaterEqual, L_loop);
827
828 __ BIND(L_done);
829 }
830
831 #undef __
832
833 #ifdef COMPILER1
834
835 #define __ ce->masm()->
836
837 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
838 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
839 // At this point we know that marking is in progress.
840 // If do_load() is true then we have to emit the
841 // load of the previous value; otherwise it has already
842 // been loaded into _pre_val.
843
844 __ bind(*stub->entry());
845 assert(stub->pre_val()->is_register(), "Precondition.");
846
847 Register pre_val_reg = stub->pre_val()->as_register();
848
849 if (stub->do_load()) {
850 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
851 }
852
853 __ cmpptr(pre_val_reg, NULL_WORD);
854 __ jcc(Assembler::equal, *stub->continuation());
855 ce->store_parameter(stub->pre_val()->as_register(), 0);
856 __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
857 __ jmp(*stub->continuation());
858
859 }
860
861 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
862 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
863 __ bind(*stub->entry());
864
865 DecoratorSet decorators = stub->decorators();
866 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
867 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
868 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
869 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
870
871 Register obj = stub->obj()->as_register();
872 Register res = stub->result()->as_register();
873 Register addr = stub->addr()->as_pointer_register();
874 Register tmp1 = stub->tmp1()->as_register();
875 Register tmp2 = stub->tmp2()->as_register();
876 assert_different_registers(obj, res, addr, tmp1, tmp2);
877
878 Label slow_path;
879
880 assert(res == rax, "result must arrive in rax");
881
882 if (res != obj) {
883 __ mov(res, obj);
884 }
885
886 if (is_strong) {
887 // Check for object being in the collection set.
888 __ mov(tmp1, res);
889 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
890 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
891 __ movbool(tmp2, Address(tmp2, tmp1, Address::times_1));
892 __ testbool(tmp2);
893 __ jcc(Assembler::zero, *stub->continuation());
894 }
895
896 __ bind(slow_path);
897 ce->store_parameter(res, 0);
898 ce->store_parameter(addr, 1);
899 if (is_strong) {
900 if (is_native) {
901 __ call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
902 } else {
903 __ call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
904 }
905 } else if (is_weak) {
906 __ call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
907 } else {
908 assert(is_phantom, "only remaining strength");
909 __ call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
910 }
911 __ jmp(*stub->continuation());
912 }
913
914 #undef __
915
916 #define __ sasm->
917
918 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
919 __ prologue("shenandoah_pre_barrier", false);
920 // arg0 : previous value of memory
921
922 __ push(rax);
923 __ push(rdx);
924
925 const Register pre_val = rax;
926 const Register thread = r15_thread;
927 const Register tmp = rdx;
928
929 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
930 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
931
932 Label done;
933 Label runtime;
934
935 // Is SATB still active?
936 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
937 __ testb(gc_state, ShenandoahHeap::MARKING);
938 __ jcc(Assembler::zero, done);
939
940 // Can we store original value in the thread's buffer?
941
942 __ movptr(tmp, queue_index);
943 __ testptr(tmp, tmp);
944 __ jcc(Assembler::zero, runtime);
945 __ subptr(tmp, wordSize);
946 __ movptr(queue_index, tmp);
947 __ addptr(tmp, buffer);
948
949 // prev_val (rax)
950 __ load_parameter(0, pre_val);
951 __ movptr(Address(tmp, 0), pre_val);
952 __ jmp(done);
953
954 __ bind(runtime);
955
956 __ save_live_registers_no_oop_map(true);
957
958 // load the pre-value
959 __ load_parameter(0, rcx);
960 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), rcx);
961
962 __ restore_live_registers(true);
963
964 __ bind(done);
965
966 __ pop(rdx);
967 __ pop(rax);
968
969 __ epilogue();
970 }
971
972 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
973 __ prologue("shenandoah_load_reference_barrier", false);
974 // arg0 : object to be resolved
975
976 __ save_live_registers_no_oop_map(true);
977
978 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
979 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
980 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
981 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
982
983 __ load_parameter(0, c_rarg0);
984 __ load_parameter(1, c_rarg1);
985 if (is_strong) {
986 if (is_native) {
987 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
988 } else {
989 if (UseCompressedOops) {
990 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), c_rarg0, c_rarg1);
991 } else {
992 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
993 }
994 }
995 } else if (is_weak) {
996 assert(!is_native, "weak must not be called off-heap");
997 if (UseCompressedOops) {
998 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow), c_rarg0, c_rarg1);
999 } else {
1000 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak), c_rarg0, c_rarg1);
1001 }
1002 } else {
1003 assert(is_phantom, "only remaining strength");
1004 assert(is_native, "phantom must only be called off-heap");
1005 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom), c_rarg0, c_rarg1);
1006 }
1007
1008 __ restore_live_registers_except_rax(true);
1009
1010 __ epilogue();
1011 }
1012
1013 #undef __
1014
1015 #endif // COMPILER1