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 "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 #endif
49
50 #define __ masm->
51
52 static void save_machine_state(MacroAssembler* masm, bool handle_gpr, bool handle_fp) {
53 if (handle_gpr) {
54 __ push_IU_state();
55 }
56
57 if (handle_fp) {
58 // Some paths can be reached from the c2i adapter with live fp arguments in registers.
59 assert(Argument::n_float_register_parameters_j == 8, "8 fp registers to save at java call");
60
61 const int xmm_size = wordSize * 2;
62 __ subptr(rsp, xmm_size * 8);
63 __ movdbl(Address(rsp, xmm_size * 0), xmm0);
64 __ movdbl(Address(rsp, xmm_size * 1), xmm1);
65 __ movdbl(Address(rsp, xmm_size * 2), xmm2);
66 __ movdbl(Address(rsp, xmm_size * 3), xmm3);
67 __ movdbl(Address(rsp, xmm_size * 4), xmm4);
68 __ movdbl(Address(rsp, xmm_size * 5), xmm5);
69 __ movdbl(Address(rsp, xmm_size * 6), xmm6);
70 __ movdbl(Address(rsp, xmm_size * 7), xmm7);
71 }
72 }
73
74 static void restore_machine_state(MacroAssembler* masm, bool handle_gpr, bool handle_fp) {
75 if (handle_fp) {
76 const int xmm_size = wordSize * 2;
77 __ movdbl(xmm0, Address(rsp, xmm_size * 0));
78 __ movdbl(xmm1, Address(rsp, xmm_size * 1));
79 __ movdbl(xmm2, Address(rsp, xmm_size * 2));
80 __ movdbl(xmm3, Address(rsp, xmm_size * 3));
81 __ movdbl(xmm4, Address(rsp, xmm_size * 4));
82 __ movdbl(xmm5, Address(rsp, xmm_size * 5));
83 __ movdbl(xmm6, Address(rsp, xmm_size * 6));
84 __ movdbl(xmm7, Address(rsp, xmm_size * 7));
85 __ addptr(rsp, xmm_size * 8);
86 }
87
88 if (handle_gpr) {
89 __ pop_IU_state();
90 }
91 }
92
93 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
94 Register src, Register dst, Register count) {
95
96 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
97
98 if (is_reference_type(type)) {
99 if (ShenandoahCardBarrier) {
100 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
101 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
102 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
103
104 // We need to save the original element count because the array copy stub
105 // will destroy the value and we need it for the card marking barrier.
106 if (!checkcast) {
107 if (!obj_int) {
108 // Save count for barrier
109 __ movptr(r11, count);
110 } else if (disjoint) {
111 // Save dst in r11 in the disjoint case
112 __ movq(r11, dst);
113 }
114 }
115 }
116
117 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
118 Register thread = r15_thread;
119 assert_different_registers(src, dst, count, thread);
120
121 Label L_done;
122 // Short-circuit if count == 0.
123 __ testptr(count, count);
124 __ jcc(Assembler::zero, L_done);
125
126 // Avoid runtime call when not active.
127 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
128 int flags;
129 if (ShenandoahSATBBarrier && dest_uninitialized) {
130 flags = ShenandoahHeap::HAS_FORWARDED;
131 } else {
132 flags = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING;
133 }
134 __ testb(gc_state, flags);
135 __ jcc(Assembler::zero, L_done);
136
137 save_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ false);
138
139 assert(src == rdi, "expected");
140 assert(dst == rsi, "expected");
141 assert(count == rdx, "expected");
142 if (UseCompressedOops) {
143 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop),
144 src, dst, count);
145 } else {
146 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop),
147 src, dst, count);
148 }
149
150 restore_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ false);
151
152 __ bind(L_done);
153 }
154 }
155
156 }
157
158 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
159 Register src, Register dst, Register count) {
160
161 if (ShenandoahCardBarrier && is_reference_type(type)) {
162 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
163 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
164 bool obj_int = (type == T_OBJECT) && UseCompressedOops;
165 Register tmp = rax;
166
167 if (!checkcast) {
168 if (!obj_int) {
169 // Save count for barrier
170 count = r11;
171 } else if (disjoint) {
172 // Use the saved dst in the disjoint case
173 dst = r11;
174 }
175 } else {
176 tmp = rscratch1;
177 }
178 gen_write_ref_array_post_barrier(masm, decorators, dst, count, tmp);
179 }
180 }
181
182 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
183 Register obj,
184 Register pre_val,
185 Register tmp,
186 bool tosca_live,
187 bool expand_call) {
188 assert(ShenandoahSATBBarrier, "Should be checked by caller");
189
190 // If expand_call is true then we expand the call_VM_leaf macro
191 // directly to skip generating the check by
192 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
193
194 const Register thread = r15_thread;
195
196 Label done;
197 Label runtime;
198
199 assert(pre_val != noreg, "check this code");
200
201 if (obj != noreg) {
202 assert_different_registers(obj, pre_val, tmp);
203 assert(pre_val != rax, "check this code");
204 }
205
206 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
207 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
208
209 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
210 __ testb(gc_state, ShenandoahHeap::MARKING);
211 __ jcc(Assembler::zero, done);
212
213 // Do we need to load the previous value?
214 if (obj != noreg) {
215 __ load_heap_oop(pre_val, Address(obj, 0), noreg, AS_RAW);
216 }
217
218 // Is the previous value null?
219 __ cmpptr(pre_val, NULL_WORD);
220 __ jcc(Assembler::equal, done);
221
222 // Can we store original value in the thread's buffer?
223 // Is index == 0?
224 // (The index field is typed as size_t.)
225
226 __ movptr(tmp, index); // tmp := *index_adr
227 __ cmpptr(tmp, 0); // tmp == 0?
228 __ jcc(Assembler::equal, runtime); // If yes, goto runtime
229
230 __ subptr(tmp, wordSize); // tmp := tmp - wordSize
231 __ movptr(index, tmp); // *index_adr := tmp
232 __ addptr(tmp, buffer); // tmp := tmp + *buffer_adr
233
234 // Record the previous value
235 __ movptr(Address(tmp, 0), pre_val);
236 __ jmp(done);
237
238 __ bind(runtime);
239 // save the live input values
240 if(tosca_live) __ push(rax);
241
242 if (obj != noreg && obj != rax)
243 __ push(obj);
244
245 if (pre_val != rax)
246 __ push(pre_val);
247
248 // Calling the runtime using the regular call_VM_leaf mechanism generates
249 // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
250 // that checks that the *(ebp+frame::interpreter_frame_last_sp) == nullptr.
251 //
252 // If we care generating the pre-barrier without a frame (e.g. in the
253 // intrinsified Reference.get() routine) then ebp might be pointing to
254 // the caller frame and so this check will most likely fail at runtime.
255 //
256 // Expanding the call directly bypasses the generation of the check.
257 // So when we do not have have a full interpreter frame on the stack
258 // expand_call should be passed true.
259
260 // We move pre_val into c_rarg0 early, in order to avoid smashing it, should
261 // pre_val be c_rarg1 (where the call prologue would copy thread argument).
262 // Note: this should not accidentally smash thread, because thread is always r15.
263 assert(thread != c_rarg0, "smashed arg");
264 if (c_rarg0 != pre_val) {
265 __ mov(c_rarg0, pre_val);
266 }
267
268 if (expand_call) {
269 assert(pre_val != c_rarg1, "smashed arg");
270 if (c_rarg1 != thread) {
271 __ mov(c_rarg1, thread);
272 }
273 // Already moved pre_val into c_rarg0 above
274 __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), 1);
275 } else {
276 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
277 }
278
279 // save the live input values
280 if (pre_val != rax)
281 __ pop(pre_val);
282
283 if (obj != noreg && obj != rax)
284 __ pop(obj);
285
286 if(tosca_live) __ pop(rax);
287
288 __ bind(done);
289 }
290
291 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address src, DecoratorSet decorators) {
292 assert(ShenandoahLoadRefBarrier, "Should be enabled");
293
294 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
295 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
296 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
297 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
298 bool is_narrow = UseCompressedOops && !is_native;
299
300 Label heap_stable, not_cset;
301
302 __ block_comment("load_reference_barrier { ");
303
304 // Check if GC is active
305 Register thread = r15_thread;
306
307 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
308 int flags = ShenandoahHeap::HAS_FORWARDED;
309 if (!is_strong) {
310 flags |= ShenandoahHeap::WEAK_ROOTS;
311 }
312 __ testb(gc_state, flags);
313 __ jcc(Assembler::zero, heap_stable);
314
315 Register tmp1 = noreg, tmp2 = noreg;
316 if (is_strong) {
317 // Test for object in cset
318 // Allocate temporary registers
319 for (int i = 0; i < 8; i++) {
320 Register r = as_Register(i);
321 if (r != rsp && r != rbp && r != dst && r != src.base() && r != src.index()) {
322 if (tmp1 == noreg) {
323 tmp1 = r;
324 } else {
325 tmp2 = r;
326 break;
327 }
328 }
329 }
330 assert(tmp1 != noreg, "tmp1 allocated");
331 assert(tmp2 != noreg, "tmp2 allocated");
332 assert_different_registers(tmp1, tmp2, src.base(), src.index());
333 assert_different_registers(tmp1, tmp2, dst);
334
335 __ push(tmp1);
336 __ push(tmp2);
337
338 // Optimized cset-test
339 __ movptr(tmp1, dst);
340 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
341 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
342 __ movbool(tmp1, Address(tmp1, tmp2, Address::times_1));
343 __ testbool(tmp1);
344 __ jcc(Assembler::zero, not_cset);
345 }
346
347 save_machine_state(masm, /* handle_gpr = */ false, /* handle_fp = */ true);
348
349 // The rest is saved with the optimized path
350
351 uint num_saved_regs = 4 + (dst != rax ? 1 : 0) + 4 + (UseAPX ? 16 : 0);
352 __ subptr(rsp, num_saved_regs * wordSize);
353 uint slot = num_saved_regs;
354 if (dst != rax) {
355 __ movptr(Address(rsp, (--slot) * wordSize), rax);
356 }
357 __ movptr(Address(rsp, (--slot) * wordSize), rcx);
358 __ movptr(Address(rsp, (--slot) * wordSize), rdx);
359 __ movptr(Address(rsp, (--slot) * wordSize), rdi);
360 __ movptr(Address(rsp, (--slot) * wordSize), rsi);
361 __ movptr(Address(rsp, (--slot) * wordSize), r8);
362 __ movptr(Address(rsp, (--slot) * wordSize), r9);
363 __ movptr(Address(rsp, (--slot) * wordSize), r10);
364 __ movptr(Address(rsp, (--slot) * wordSize), r11);
365 // Save APX extended registers r16–r31 if enabled
366 if (UseAPX) {
367 __ movptr(Address(rsp, (--slot) * wordSize), r16);
368 __ movptr(Address(rsp, (--slot) * wordSize), r17);
369 __ movptr(Address(rsp, (--slot) * wordSize), r18);
370 __ movptr(Address(rsp, (--slot) * wordSize), r19);
371 __ movptr(Address(rsp, (--slot) * wordSize), r20);
372 __ movptr(Address(rsp, (--slot) * wordSize), r21);
373 __ movptr(Address(rsp, (--slot) * wordSize), r22);
374 __ movptr(Address(rsp, (--slot) * wordSize), r23);
375 __ movptr(Address(rsp, (--slot) * wordSize), r24);
376 __ movptr(Address(rsp, (--slot) * wordSize), r25);
377 __ movptr(Address(rsp, (--slot) * wordSize), r26);
378 __ movptr(Address(rsp, (--slot) * wordSize), r27);
379 __ movptr(Address(rsp, (--slot) * wordSize), r28);
380 __ movptr(Address(rsp, (--slot) * wordSize), r29);
381 __ movptr(Address(rsp, (--slot) * wordSize), r30);
382 __ movptr(Address(rsp, (--slot) * wordSize), r31);
383 }
384 // r12-r15 are callee saved in all calling conventions
385 assert(slot == 0, "must use all slots");
386
387 // Shuffle registers such that dst is in c_rarg0 and addr in c_rarg1.
388 Register arg0 = c_rarg0, arg1 = c_rarg1;
389 if (dst == arg1) {
390 __ lea(arg0, src);
391 __ xchgptr(arg1, arg0);
392 } else {
393 __ lea(arg1, src);
394 __ movptr(arg0, dst);
395 }
396
397 if (is_strong) {
398 if (is_narrow) {
399 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), arg0, arg1);
400 } else {
401 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), arg0, arg1);
402 }
403 } else if (is_weak) {
404 if (is_narrow) {
405 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow), arg0, arg1);
406 } else {
407 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak), arg0, arg1);
408 }
409 } else {
410 assert(is_phantom, "only remaining strength");
411 assert(!is_narrow, "phantom access cannot be narrow");
412 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom), arg0, arg1);
413 }
414
415 // Restore APX extended registers r31–r16 if previously saved
416 if (UseAPX) {
417 __ movptr(r31, Address(rsp, (slot++) * wordSize));
418 __ movptr(r30, Address(rsp, (slot++) * wordSize));
419 __ movptr(r29, Address(rsp, (slot++) * wordSize));
420 __ movptr(r28, Address(rsp, (slot++) * wordSize));
421 __ movptr(r27, Address(rsp, (slot++) * wordSize));
422 __ movptr(r26, Address(rsp, (slot++) * wordSize));
423 __ movptr(r25, Address(rsp, (slot++) * wordSize));
424 __ movptr(r24, Address(rsp, (slot++) * wordSize));
425 __ movptr(r23, Address(rsp, (slot++) * wordSize));
426 __ movptr(r22, Address(rsp, (slot++) * wordSize));
427 __ movptr(r21, Address(rsp, (slot++) * wordSize));
428 __ movptr(r20, Address(rsp, (slot++) * wordSize));
429 __ movptr(r19, Address(rsp, (slot++) * wordSize));
430 __ movptr(r18, Address(rsp, (slot++) * wordSize));
431 __ movptr(r17, Address(rsp, (slot++) * wordSize));
432 __ movptr(r16, Address(rsp, (slot++) * wordSize));
433 }
434 __ movptr(r11, Address(rsp, (slot++) * wordSize));
435 __ movptr(r10, Address(rsp, (slot++) * wordSize));
436 __ movptr(r9, Address(rsp, (slot++) * wordSize));
437 __ movptr(r8, Address(rsp, (slot++) * wordSize));
438 __ movptr(rsi, Address(rsp, (slot++) * wordSize));
439 __ movptr(rdi, Address(rsp, (slot++) * wordSize));
440 __ movptr(rdx, Address(rsp, (slot++) * wordSize));
441 __ movptr(rcx, Address(rsp, (slot++) * wordSize));
442
443 if (dst != rax) {
444 __ movptr(dst, rax);
445 __ movptr(rax, Address(rsp, (slot++) * wordSize));
446 }
447
448 assert(slot == num_saved_regs, "must use all slots");
449 __ addptr(rsp, num_saved_regs * wordSize);
450
451 restore_machine_state(masm, /* handle_gpr = */ false, /* handle_fp = */ true);
452
453 __ bind(not_cset);
454
455 if (is_strong) {
456 __ pop(tmp2);
457 __ pop(tmp1);
458 }
459
460 __ bind(heap_stable);
461
462 __ block_comment("} load_reference_barrier");
463 }
464
465 //
466 // Arguments:
467 //
468 // Inputs:
469 // src: oop location, might be clobbered
470 // tmp1: scratch register, might not be valid.
471 //
472 // Output:
473 // dst: oop loaded from src location
474 //
475 // Kill:
476 // tmp1 (if it is valid)
477 //
478 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
479 Register dst, Address src, Register tmp1) {
480 // 1: non-reference load, no additional barrier is needed
481 if (!is_reference_type(type)) {
482 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
483 return;
484 }
485
486 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Not expected");
487
488 // 2: load a reference from src location and apply LRB if needed
489 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
490 Register result_dst = dst;
491 bool use_tmp1_for_dst = false;
492
493 // Preserve src location for LRB
494 if (dst == src.base() || dst == src.index()) {
495 // Use tmp1 for dst if possible, as it is not used in BarrierAssembler::load_at()
496 if (tmp1->is_valid() && tmp1 != src.base() && tmp1 != src.index()) {
497 dst = tmp1;
498 use_tmp1_for_dst = true;
499 } else {
500 dst = rdi;
501 __ push(dst);
502 }
503 assert_different_registers(dst, src.base(), src.index());
504 }
505
506 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
507
508 load_reference_barrier(masm, dst, src, decorators);
509
510 // Move loaded oop to final destination
511 if (dst != result_dst) {
512 __ movptr(result_dst, dst);
513
514 if (!use_tmp1_for_dst) {
515 __ pop(dst);
516 }
517
518 dst = result_dst;
519 }
520 } else {
521 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1);
522 }
523
524 // 3: apply keep-alive barrier if needed
525 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
526 save_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ true);
527
528 assert_different_registers(dst, tmp1, r15_thread);
529 // Generate the SATB pre-barrier code to log the value of
530 // the referent field in an SATB buffer.
531 satb_barrier(masm /* masm */,
532 noreg /* obj */,
533 dst /* pre_val */,
534 tmp1 /* tmp */,
535 true /* tosca_live */,
536 true /* expand_call */);
537
538 restore_machine_state(masm, /* handle_gpr = */ true, /* handle_fp = */ true);
539 }
540 }
541
542 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj) {
543 assert(ShenandoahCardBarrier, "Should have been checked by caller");
544
545 // Does a store check for the oop in register obj. The content of
546 // register obj is destroyed afterwards.
547 __ shrptr(obj, CardTable::card_shift());
548
549 // We'll use this register as the TLS base address and also later on
550 // to hold the byte_map_base.
551 Register thread = r15_thread;
552 Register tmp = rscratch1;
553
554 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
555 __ movptr(tmp, curr_ct_holder_addr);
556 Address card_addr(tmp, obj, Address::times_1);
557
558 int dirty = CardTable::dirty_card_val();
559 if (UseCondCardMark) {
560 Label L_already_dirty;
561 __ cmpb(card_addr, dirty);
562 __ jccb(Assembler::equal, L_already_dirty);
563 __ movb(card_addr, dirty);
564 __ bind(L_already_dirty);
565 } else {
566 __ movb(card_addr, dirty);
567 }
568 }
569
570 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
571 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
572
573 // 1: non-reference types require no barriers
574 if (!is_reference_type(type)) {
575 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
576 return;
577 }
578
579 // Flatten object address right away for simplicity: likely needed by barriers
580 assert_different_registers(val, tmp1, tmp2, tmp3, r15_thread);
581 if (dst.index() == noreg && dst.disp() == 0) {
582 if (dst.base() != tmp1) {
583 __ movptr(tmp1, dst.base());
584 }
585 } else {
586 __ lea(tmp1, dst);
587 }
588
589 bool storing_non_null = (val != noreg);
590
591 // 2: pre-barrier: SATB needs the previous value
592 if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
593 satb_barrier(masm,
594 tmp1 /* obj */,
595 tmp2 /* pre_val */,
596 tmp3 /* tmp */,
597 storing_non_null /* tosca_live */,
598 false /* expand_call */);
599 }
600
601 // Store!
602 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg);
603
604 // 3: post-barrier: card barrier needs store address
605 if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
606 card_barrier(masm, tmp1);
607 }
608 }
609
610 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
611 Register obj, Register tmp, Label& slowpath) {
612 Label done;
613 // Resolve jobject
614 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
615
616 // Check for null.
617 __ testptr(obj, obj);
618 __ jcc(Assembler::zero, done);
619
620 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
621 __ testb(gc_state, ShenandoahHeap::EVACUATION);
622 __ jccb(Assembler::notZero, slowpath);
623 __ bind(done);
624 }
625
626 #ifdef COMPILER2
627 void ShenandoahBarrierSetAssembler::try_resolve_weak_handle_in_c2(MacroAssembler* masm, Register obj, Label& slowpath) {
628 Label done;
629
630 // Resolve weak handle using the standard implementation.
631 BarrierSetAssembler::try_resolve_weak_handle_in_c2(masm, obj, slowpath);
632
633 // Check if the reference is null, and if it is, take the fast path.
634 __ testptr(obj, obj);
635 __ jcc(Assembler::zero, done);
636
637 Address gc_state(r15_thread, ShenandoahThreadLocalData::gc_state_offset());
638
639 // Check if the heap is under weak-reference/roots processing, in
640 // which case we need to take the slow path.
641 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
642 __ jcc(Assembler::notZero, slowpath);
643 __ bind(done);
644 }
645 #endif // COMPILER2
646
647 // Special Shenandoah CAS implementation that handles false negatives
648 // due to concurrent evacuation.
649 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm,
650 Register res, Address addr, Register oldval, Register newval,
651 bool exchange, Register tmp1, Register tmp2) {
652 assert(ShenandoahCASBarrier, "Should only be used when CAS barrier is enabled");
653 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
654 assert_different_registers(oldval, tmp1, tmp2);
655 assert_different_registers(newval, tmp1, tmp2);
656
657 Label L_success, L_failure;
658
659 // Remember oldval for retry logic below
660 if (UseCompressedOops) {
661 __ movl(tmp1, oldval);
662 } else {
663 __ movptr(tmp1, oldval);
664 }
665
666 // Step 1. Fast-path.
667 //
668 // Try to CAS with given arguments. If successful, then we are done.
669
670 if (UseCompressedOops) {
671 __ lock();
672 __ cmpxchgl(newval, addr);
673 } else {
674 __ lock();
675 __ cmpxchgptr(newval, addr);
676 }
677 __ jcc(Assembler::equal, L_success);
678
679 // Step 2. CAS had failed. This may be a false negative.
680 //
681 // The trouble comes when we compare the to-space pointer with the from-space
682 // pointer to the same object. To resolve this, it will suffice to resolve
683 // the value from memory -- this will give both to-space pointers.
684 // If they mismatch, then it was a legitimate failure.
685 //
686 // Before reaching to resolve sequence, see if we can avoid the whole shebang
687 // with filters.
688
689 // Filter: when offending in-memory value is null, the failure is definitely legitimate
690 __ testptr(oldval, oldval);
691 __ jcc(Assembler::zero, L_failure);
692
693 // Filter: when heap is stable, the failure is definitely legitimate
694 const Register thread = r15_thread;
695 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
696 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
697 __ jcc(Assembler::zero, L_failure);
698
699 if (UseCompressedOops) {
700 __ movl(tmp2, oldval);
701 __ decode_heap_oop(tmp2);
702 } else {
703 __ movptr(tmp2, oldval);
704 }
705
706 // Decode offending in-memory value.
707 // Test if-forwarded
708 __ testb(Address(tmp2, oopDesc::mark_offset_in_bytes()), markWord::marked_value);
709 __ jcc(Assembler::noParity, L_failure); // When odd number of bits, then not forwarded
710 __ jcc(Assembler::zero, L_failure); // When it is 00, then also not forwarded
711
712 // Load and mask forwarding pointer
713 __ movptr(tmp2, Address(tmp2, oopDesc::mark_offset_in_bytes()));
714 __ shrptr(tmp2, 2);
715 __ shlptr(tmp2, 2);
716
717 if (UseCompressedOops) {
718 __ decode_heap_oop(tmp1); // decode for comparison
719 }
720
721 // Now we have the forwarded offender in tmp2.
722 // Compare and if they don't match, we have legitimate failure
723 __ cmpptr(tmp1, tmp2);
724 __ jcc(Assembler::notEqual, L_failure);
725
726 // Step 3. Need to fix the memory ptr before continuing.
727 //
728 // At this point, we have from-space oldval in the register, and its to-space
729 // address is in tmp2. Let's try to update it into memory. We don't care if it
730 // succeeds or not. If it does, then the retrying CAS would see it and succeed.
731 // If this fixup fails, this means somebody else beat us to it, and necessarily
732 // with to-space ptr store. We still have to do the retry, because the GC might
733 // have updated the reference for us.
734
735 if (UseCompressedOops) {
736 __ encode_heap_oop(tmp2); // previously decoded at step 2.
737 }
738
739 if (UseCompressedOops) {
740 __ lock();
741 __ cmpxchgl(tmp2, addr);
742 } else {
743 __ lock();
744 __ cmpxchgptr(tmp2, addr);
745 }
746
747 // Step 4. Try to CAS again.
748 //
749 // This is guaranteed not to have false negatives, because oldval is definitely
750 // to-space, and memory pointer is to-space as well. Nothing is able to store
751 // from-space ptr into memory anymore. Make sure oldval is restored, after being
752 // garbled during retries.
753 //
754 if (UseCompressedOops) {
755 __ movl(oldval, tmp2);
756 } else {
757 __ movptr(oldval, tmp2);
758 }
759
760 if (UseCompressedOops) {
761 __ lock();
762 __ cmpxchgl(newval, addr);
763 } else {
764 __ lock();
765 __ cmpxchgptr(newval, addr);
766 }
767 if (!exchange) {
768 __ jccb(Assembler::equal, L_success); // fastpath, peeking into Step 5, no need to jump
769 }
770
771 // Step 5. If we need a boolean result out of CAS, set the flag appropriately.
772 // and promote the result. Note that we handle the flag from both the 1st and 2nd CAS.
773 // Otherwise, failure witness for CAE is in oldval on all paths, and we can return.
774
775 if (exchange) {
776 __ bind(L_failure);
777 __ bind(L_success);
778 } else {
779 assert(res != noreg, "need result register");
780
781 Label exit;
782 __ bind(L_failure);
783 __ xorptr(res, res);
784 __ jmpb(exit);
785
786 __ bind(L_success);
787 __ movptr(res, 1);
788 __ bind(exit);
789 }
790 }
791
792 #ifdef PRODUCT
793 #define BLOCK_COMMENT(str) /* nothing */
794 #else
795 #define BLOCK_COMMENT(str) __ block_comment(str)
796 #endif
797
798 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
799
800 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
801
802 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
803 Register addr, Register count,
804 Register tmp) {
805 assert(ShenandoahCardBarrier, "Should have been checked by caller");
806
807 Label L_loop, L_done;
808 const Register end = count;
809 assert_different_registers(addr, end);
810
811 // Zero count? Nothing to do.
812 __ testl(count, count);
813 __ jccb(Assembler::zero, L_done);
814
815 const Register thread = r15_thread;
816 Address curr_ct_holder_addr(thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
817 __ movptr(tmp, curr_ct_holder_addr);
818
819 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
820 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
821 __ shrptr(addr, CardTable::card_shift());
822 __ shrptr(end, CardTable::card_shift());
823 __ subptr(end, addr); // end --> cards count
824
825 __ addptr(addr, tmp);
826
827 __ BIND(L_loop);
828 __ movb(Address(addr, count, Address::times_1), 0);
829 __ decrement(count);
830 __ jccb(Assembler::greaterEqual, L_loop);
831
832 __ BIND(L_done);
833 }
834
835 #undef __
836
837 #ifdef COMPILER1
838
839 #define __ ce->masm()->
840
841 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
842 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
843 // At this point we know that marking is in progress.
844 // If do_load() is true then we have to emit the
845 // load of the previous value; otherwise it has already
846 // been loaded into _pre_val.
847
848 __ bind(*stub->entry());
849 assert(stub->pre_val()->is_register(), "Precondition.");
850
851 Register pre_val_reg = stub->pre_val()->as_register();
852
853 if (stub->do_load()) {
854 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
855 }
856
857 __ cmpptr(pre_val_reg, NULL_WORD);
858 __ jcc(Assembler::equal, *stub->continuation());
859 ce->store_parameter(stub->pre_val()->as_register(), 0);
860 __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
861 __ jmp(*stub->continuation());
862
863 }
864
865 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
866 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
867 __ bind(*stub->entry());
868
869 DecoratorSet decorators = stub->decorators();
870 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
871 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
872 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
873 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
874
875 Register obj = stub->obj()->as_register();
876 Register res = stub->result()->as_register();
877 Register addr = stub->addr()->as_pointer_register();
878 Register tmp1 = stub->tmp1()->as_register();
879 Register tmp2 = stub->tmp2()->as_register();
880 assert_different_registers(obj, res, addr, tmp1, tmp2);
881
882 Label slow_path;
883
884 assert(res == rax, "result must arrive in rax");
885
886 if (res != obj) {
887 __ mov(res, obj);
888 }
889
890 if (is_strong) {
891 // Check for object being in the collection set.
892 __ mov(tmp1, res);
893 __ shrptr(tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
894 __ movptr(tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
895 __ movbool(tmp2, Address(tmp2, tmp1, Address::times_1));
896 __ testbool(tmp2);
897 __ jcc(Assembler::zero, *stub->continuation());
898 }
899
900 __ bind(slow_path);
901 ce->store_parameter(res, 0);
902 ce->store_parameter(addr, 1);
903 if (is_strong) {
904 if (is_native) {
905 __ call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
906 } else {
907 __ call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
908 }
909 } else if (is_weak) {
910 __ call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
911 } else {
912 assert(is_phantom, "only remaining strength");
913 __ call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
914 }
915 __ jmp(*stub->continuation());
916 }
917
918 #undef __
919
920 #define __ sasm->
921
922 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
923 __ prologue("shenandoah_pre_barrier", false);
924 // arg0 : previous value of memory
925
926 __ push(rax);
927 __ push(rdx);
928
929 const Register pre_val = rax;
930 const Register thread = r15_thread;
931 const Register tmp = rdx;
932
933 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
934 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
935
936 Label done;
937 Label runtime;
938
939 // Is SATB still active?
940 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
941 __ testb(gc_state, ShenandoahHeap::MARKING);
942 __ jcc(Assembler::zero, done);
943
944 // Can we store original value in the thread's buffer?
945
946 __ movptr(tmp, queue_index);
947 __ testptr(tmp, tmp);
948 __ jcc(Assembler::zero, runtime);
949 __ subptr(tmp, wordSize);
950 __ movptr(queue_index, tmp);
951 __ addptr(tmp, buffer);
952
953 // prev_val (rax)
954 __ load_parameter(0, pre_val);
955 __ movptr(Address(tmp, 0), pre_val);
956 __ jmp(done);
957
958 __ bind(runtime);
959
960 __ save_live_registers_no_oop_map(true);
961
962 // load the pre-value
963 __ load_parameter(0, rcx);
964 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), rcx);
965
966 __ restore_live_registers(true);
967
968 __ bind(done);
969
970 __ pop(rdx);
971 __ pop(rax);
972
973 __ epilogue();
974 }
975
976 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
977 __ prologue("shenandoah_load_reference_barrier", false);
978 // arg0 : object to be resolved
979
980 __ save_live_registers_no_oop_map(true);
981
982 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
983 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
984 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
985 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
986
987 __ load_parameter(0, c_rarg0);
988 __ load_parameter(1, c_rarg1);
989 if (is_strong) {
990 if (is_native) {
991 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
992 } else {
993 if (UseCompressedOops) {
994 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), c_rarg0, c_rarg1);
995 } else {
996 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), c_rarg0, c_rarg1);
997 }
998 }
999 } else if (is_weak) {
1000 assert(!is_native, "weak must not be called off-heap");
1001 if (UseCompressedOops) {
1002 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow), c_rarg0, c_rarg1);
1003 } else {
1004 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak), c_rarg0, c_rarg1);
1005 }
1006 } else {
1007 assert(is_phantom, "only remaining strength");
1008 assert(is_native, "phantom must only be called off-heap");
1009 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom), c_rarg0, c_rarg1);
1010 }
1011
1012 __ restore_live_registers_except_rax(true);
1013
1014 __ epilogue();
1015 }
1016
1017 #undef __
1018
1019 #endif // COMPILER1
1020
1021 #ifdef COMPILER2
1022
1023 address ShenandoahBarrierSetAssembler::parse_stub_address(address pc) {
1024 NativeInstruction* ni = nativeInstruction_at(pc);
1025 assert(ni->is_jump(), "Initial code version: GC barrier fastpath must be a jump");
1026 NativeJump* jmp = nativeJump_at(pc);
1027 return jmp->jump_destination();
1028 }
1029
1030 void insert_5_byte_nop(address pc) {
1031 *(pc + 0) = 0x0F;
1032 *(pc + 1) = 0x1F;
1033 *(pc + 2) = 0x44;
1034 *(pc + 3) = 0x00;
1035 *(pc + 4) = 0x00;
1036 ICache::invalidate_range(pc, 5);
1037 }
1038
1039 bool is_5_byte_nop(address pc) {
1040 if (*(pc + 0) != 0x0F) return false;
1041 if (*(pc + 1) != 0x1F) return false;
1042 if (*(pc + 2) != 0x44) return false;
1043 if (*(pc + 3) != 0x00) return false;
1044 if (*(pc + 4) != 0x00) return false;
1045 return true;
1046 }
1047
1048 void check_at(bool cond, address pc, const char* msg) {
1049 assert(cond, "%s: at PC " PTR_FORMAT ": %02x%02x%02x%02x%02x",
1050 msg, p2i(pc), *(pc + 0), *(pc + 1), *(pc + 2), *(pc + 3), *(pc + 4));
1051 }
1052
1053 void ShenandoahBarrierSetAssembler::patch_branch_to_nop(address pc) {
1054 NativeInstruction* ni = nativeInstruction_at(pc);
1055 if (ni->is_jump()) {
1056 insert_5_byte_nop(pc);
1057 } else {
1058 check_at(is_5_byte_nop(pc), pc, "Should already be nop");
1059 }
1060 }
1061
1062 void ShenandoahBarrierSetAssembler::patch_nop_to_branch(address pc, address stub_addr) {
1063 NativeInstruction* ni = nativeInstruction_at(pc);
1064 if (is_5_byte_nop(pc)) {
1065 NativeJump::insert(pc, stub_addr);
1066 } else {
1067 check_at(ni->is_jump(), pc, "Should already be jump");
1068 check_at(nativeJump_at(pc)->jump_destination() == stub_addr, pc, "Jump should be to the same address");
1069 }
1070 }
1071
1072 #undef __
1073 #define __ masm->
1074
1075 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
1076 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
1077 if (narrow) {
1078 __ movl(dst, src);
1079 } else {
1080 __ movq(dst, src);
1081 }
1082
1083 // Post-barrier: LRB
1084 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1085 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, dst, src, narrow, false);
1086 char check = 0;
1087 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1088 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1089 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node) ? ShenandoahHeap::WEAK_ROOTS : 0;
1090 stub->enter_if_gc_state(*masm, check);
1091 }
1092 }
1093
1094 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
1095 Address dst, bool dst_narrow,
1096 Register src, bool src_narrow,
1097 Register tmp) {
1098
1099 // Pre-barrier: SATB, keep-alive the current memory value.
1100 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1101 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier(node), "Should not be required for stores");
1102 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, dst, dst_narrow, true);
1103 stub->enter_if_gc_state(*masm, ShenandoahHeap::MARKING);
1104 }
1105
1106 // Need to encode into tmp, because we cannot clobber src.
1107 // TODO: Maybe there is a matcher way to test that src is unused after this?
1108 if (dst_narrow && !src_narrow) {
1109 __ movq(tmp, src);
1110 if (ShenandoahBarrierStubC2::maybe_null(node)) {
1111 __ encode_heap_oop(tmp);
1112 } else {
1113 __ encode_heap_oop_not_null(tmp);
1114 }
1115 src = tmp;
1116 }
1117
1118 // Do the actual store
1119 if (dst_narrow) {
1120 __ movl(dst, src);
1121 } else {
1122 __ movq(dst, src);
1123 }
1124
1125 // Post-barrier: card updates.
1126 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1127 card_barrier_c2(masm, dst, tmp);
1128 }
1129 }
1130
1131 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
1132 Register res, Address addr,
1133 Register oldval, Register newval, Register tmp,
1134 bool narrow) {
1135
1136 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
1137
1138 // Oldval and newval can be in the same register, but all other registers should be
1139 // distinct for extra safety, as we shuffle register values around.
1140 assert_different_registers(oldval, tmp, addr.base(), addr.index());
1141 assert_different_registers(newval, tmp, addr.base(), addr.index());
1142
1143 // Pre-barrier covers several things:
1144 // a. Avoids false positives from CAS encountering to-space memory values.
1145 // b. Satisfies the need for LRB for the CAE result.
1146 // c. Records old value for the sake of SATB.
1147 //
1148 // (a) and (b) are covered because load barrier does memory location fixup.
1149 // (c) is covered by KA on the current memory value.
1150 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1151 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, addr, narrow, true);
1152 char check = 0;
1153 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1154 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1155 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node), "Not supported for CAS");
1156 stub->enter_if_gc_state(*masm, check);
1157 }
1158
1159 // CAS!
1160 __ lock();
1161 if (narrow) {
1162 __ cmpxchgl(newval, addr);
1163 } else {
1164 __ cmpxchgptr(newval, addr);
1165 }
1166
1167 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
1168 if (res != noreg) {
1169 __ setcc(Assembler::equal, res);
1170 }
1171
1172 // Post-barrier deals with card updates.
1173 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1174 card_barrier_c2(masm, addr, tmp);
1175 }
1176 }
1177
1178 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
1179 assert_different_registers(newval, tmp, addr.base(), addr.index());
1180
1181 // Pre-barrier covers several things:
1182 // a. Satisfies the need for LRB for the GAS result.
1183 // b. Records old value for the sake of SATB.
1184 //
1185 // (a) is covered because load barrier does memory location fixup.
1186 // (b) is covered by KA on the current memory value.
1187 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1188 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, addr, narrow, true);
1189 char check = 0;
1190 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1191 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1192 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node), "Not supported for GAS");
1193 stub->enter_if_gc_state(*masm, check);
1194 }
1195
1196 if (narrow) {
1197 __ xchgl(newval, addr);
1198 } else {
1199 __ xchgq(newval, addr);
1200 }
1201
1202 // Post-barrier deals with card updates.
1203 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1204 card_barrier_c2(masm, addr, tmp);
1205 }
1206 }
1207
1208 void ShenandoahBarrierSetAssembler::card_barrier_c2(MacroAssembler* masm, Address dst, Register tmp) {
1209 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
1210
1211 // TODO: Might be a good place to implement some filters here.
1212 // For example, G1 only flips card marks for stores within a single region.
1213
1214 __ lea(tmp, dst);
1215 __ shrptr(tmp, CardTable::card_shift());
1216 __ addptr(tmp, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
1217 Address card_address(tmp, 0);
1218
1219 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
1220 Label L_done;
1221 if (UseCondCardMark) {
1222 __ cmpb(card_address, 0);
1223 __ jccb(Assembler::equal, L_done);
1224 }
1225 if (UseCompressedOops && CompressedOops::base() == nullptr) {
1226 __ movb(card_address, r12);
1227 } else {
1228 __ movb(card_address, 0);
1229 }
1230 __ bind(L_done);
1231 }
1232
1233 #undef __
1234 #define __ masm.
1235
1236 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state) {
1237 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
1238
1239 if (ShenandoahGCStateCheckRemove) {
1240 // Unrealistic: remove all barrier fastpath checks.
1241 } else if (ShenandoahGCStateCheckHotpatch) {
1242 // Emit the unconditional branch in the first version of the method.
1243 // Let the rest of runtime figure out how to manage it.
1244 __ relocate(barrier_Relocation::spec());
1245 __ jmp(*entry(), /* maybe_short = */ false);
1246
1247 Label L_done;
1248 #ifdef ASSERT
1249 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1250 __ testb(gc_state, 0xFF);
1251 __ jccb(Assembler::zero, L_done);
1252 __ hlt(); // Correctness bug: barrier is NOP-ed, but heap is NOT IDLE
1253 #endif
1254 __ bind(*continuation());
1255 // This is futile to assert, because barriers are turned off asynchronously.
1256 // #ifdef ASSERT
1257 // __ testb(gc_state, 0xFF);
1258 // __ jccb(Assembler::notZero, L_done);
1259 // __ hlt(); // Performance bug: barrier is JMP-ed, but heap is IDLE
1260 // #endif
1261 __ bind(L_done);
1262 } else {
1263 Address gc_state_fast(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_offset()));
1264 __ testb(gc_state_fast, ShenandoahThreadLocalData::gc_state_to_fast(test_state));
1265 __ jcc(Assembler::notZero, *entry());
1266 __ bind(*continuation());
1267 }
1268 }
1269
1270 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
1271 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
1272
1273 __ bind(*entry());
1274
1275 Label L_done, L_barriers;
1276
1277 // If we need to load ourselves, do it here.
1278 if (_do_load) {
1279 if (_narrow) {
1280 __ movl(_obj, _addr);
1281 } else {
1282 __ movq(_obj, _addr);
1283 }
1284 }
1285
1286 // If the object is null, there is no point in applying barriers.
1287 if (_narrow) {
1288 __ testl(_obj, _obj);
1289 } else {
1290 __ testptr(_obj, _obj);
1291 }
1292 __ jccb(Assembler::notZero, L_barriers);
1293
1294 // Exit here.
1295 __ bind(L_done);
1296 __ jmp(*continuation());
1297
1298 // Barriers start here.
1299 __ bind(L_barriers);
1300
1301 // Barriers need temp to work, allocate one now.
1302 bool tmp_live;
1303 Register tmp = select_temp_register(tmp_live, _addr, _obj);
1304 if (tmp_live) {
1305 push_save_register(masm, tmp);
1306 }
1307
1308 // If object is narrow, we need to decode it first: barrier checks need full oops.
1309 if (_narrow) {
1310 __ decode_heap_oop_not_null(_obj);
1311 }
1312
1313 // Go for barriers. If both barriers are required (rare), do a runtime check for enabled barrier.
1314 if (_needs_keep_alive_barrier) {
1315 keepalive(masm, _obj, tmp, noreg);
1316 }
1317 if (_needs_load_ref_barrier) {
1318 lrb(masm, _obj, _addr, tmp);
1319 }
1320
1321 if (tmp_live) {
1322 pop_save_register(masm, tmp);
1323 }
1324
1325 // If object is narrow, we need to encode it before exiting.
1326 // For encoding, dst can only turn null if we are dealing with weak loads.
1327 // Otherwise, we have already null-checked. We can skip all this if we performed
1328 // the load ourselves, which means the value is not used by caller.
1329 if (_narrow && !_do_load) {
1330 if (_needs_load_ref_weak_barrier) {
1331 __ encode_heap_oop(_obj);
1332 } else {
1333 __ encode_heap_oop_not_null(_obj);
1334 }
1335 }
1336 __ jmp(L_done);
1337 }
1338
1339 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Register obj, Register tmp1, Register tmp2) {
1340 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
1341 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
1342
1343 Label L_fast, L_done;
1344
1345 // If another barrier is enabled as well, do a runtime check for a specific barrier.
1346 // Hotpatched GC checks only care about idle/non-idle state, so needs a check anyhow.
1347 if (_needs_load_ref_barrier || ShenandoahGCStateCheckHotpatch) {
1348 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1349 __ testb(gc_state, ShenandoahHeap::MARKING);
1350 __ jccb(Assembler::zero, L_done);
1351 }
1352
1353 // Check if buffer is already full. Go slow, if so.
1354 __ movptr(tmp1, index);
1355 __ testptr(tmp1, tmp1);
1356 __ jccb(Assembler::notZero, L_fast);
1357
1358 // Slow-path: call runtime to handle.
1359 {
1360 // Shuffle in the arguments. The end result should be:
1361 // c_rarg0 <-- obj
1362 //
1363 // Save clobbered registers before overwriting them.
1364 bool clobbered_c_rarg0 = false;
1365 if (c_rarg0 != obj) {
1366 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1367 __ mov(c_rarg0, obj);
1368 }
1369
1370 // Go to runtime stub and handle the rest there.
1371 __ call(RuntimeAddress(keepalive_runtime_entry_addr()));
1372
1373 // Restore the clobbered registers.
1374 if (clobbered_c_rarg0) {
1375 pop_save_register(masm, c_rarg0);
1376 }
1377 __ jmpb(L_done);
1378 }
1379
1380 // Fast-path: put object into buffer.
1381 __ bind(L_fast);
1382 __ subptr(tmp1, wordSize);
1383 __ movptr(index, tmp1);
1384 __ addptr(tmp1, buffer);
1385 __ movptr(Address(tmp1, 0), obj);
1386
1387 __ bind(L_done);
1388 }
1389
1390 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm, Register obj, Address addr, Register tmp) {
1391 Label L_done;
1392
1393 // If another barrier is enabled as well, do a runtime check for a specific barrier.
1394 // Hotpatched GC checks only care about idle/non-idle state, so needs a check anyhow.
1395 if (_needs_keep_alive_barrier || ShenandoahGCStateCheckHotpatch) {
1396 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1397 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0));
1398 __ jccb(Assembler::zero, L_done);
1399 }
1400
1401 // Weak/phantom loads are handled in slow path.
1402 if (!_needs_load_ref_weak_barrier) {
1403 // Compute the cset bitmap index
1404 __ movptr(tmp, obj);
1405 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1406
1407 // If cset address is in good spot to just use it as offset. It almost always is.
1408 Address cset_addr_arg;
1409 intptr_t cset_addr = (intptr_t) ShenandoahHeap::in_cset_fast_test_addr();
1410 if ((cset_addr >> 3) < INT32_MAX) {
1411 assert(is_aligned(cset_addr, 8), "Sanity");
1412 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr >> 3), Address::times_8);
1413 } else {
1414 __ addptr(tmp, cset_addr);
1415 cset_addr_arg = Address(tmp, 0);
1416 }
1417
1418 // Cset-check. Fall-through to slow if in collection set.
1419 __ cmpb(cset_addr_arg, 0);
1420 __ jccb(Assembler::equal, L_done);
1421 }
1422
1423 // Slow path
1424 {
1425 assert_different_registers(rax, c_rarg0, c_rarg1);
1426
1427 // Shuffle in the arguments. The end result should be:
1428 // c_rarg0 <-- obj
1429 // c_rarg1 <-- lea(addr)
1430 //
1431 // Save clobbered registers before overwriting them, unless they
1432 // carry obj, which would be overwritten on return.
1433 bool clobbered_c_rarg0 = false;
1434 bool clobbered_c_rarg1 = false;
1435 bool clobbered_rax = false;
1436
1437 if (obj == c_rarg0) {
1438 clobbered_c_rarg1 = push_save_register_if_live(masm, c_rarg1);
1439 __ lea(c_rarg1, addr);
1440 } else if (obj == c_rarg1) {
1441 // Set up arguments in reverse, and then flip them
1442 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1443 __ lea(c_rarg0, addr);
1444 __ xchgptr(c_rarg0, c_rarg1);
1445 } else {
1446 assert_different_registers(obj, c_rarg0, c_rarg1);
1447 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1448 clobbered_c_rarg1 = push_save_register_if_live(masm, c_rarg1);
1449 __ lea(c_rarg1, addr);
1450 __ movptr(c_rarg0, obj);
1451 }
1452 if (obj != rax) {
1453 clobbered_rax = push_save_register_if_live(masm, rax);
1454 }
1455
1456 // Go to runtime stub and handle the rest there.
1457 __ call(RuntimeAddress(lrb_runtime_entry_addr()));
1458
1459 // Save the result where needed and restore the clobbered registers.
1460 if (obj != rax) {
1461 __ movptr(obj, rax);
1462 }
1463 if (clobbered_rax) {
1464 pop_save_register(masm, rax);
1465 }
1466 if (clobbered_c_rarg1) {
1467 pop_save_register(masm, c_rarg1);
1468 }
1469 if (clobbered_c_rarg0) {
1470 pop_save_register(masm, c_rarg0);
1471 }
1472 }
1473
1474 __ bind(L_done);
1475 }
1476
1477 bool ShenandoahBarrierStubC2::is_live(Register reg) {
1478 // TODO: Precompute the generic register map for faster lookups.
1479 RegMaskIterator rmi(preserve_set());
1480 while (rmi.has_next()) {
1481 const OptoReg::Name opto_reg = rmi.next();
1482 const VMReg vm_reg = OptoReg::as_VMReg(opto_reg);
1483 if (vm_reg->is_Register()) {
1484 if (reg == vm_reg->as_Register()) {
1485 return true;
1486 }
1487 } else if (vm_reg->is_KRegister()) {
1488 // Do not care, skip.
1489 } else if (vm_reg->is_XMMRegister()) {
1490 // Do not care, skip.
1491 } else {
1492 fatal("Unexpected register type");
1493 }
1494 }
1495 return false;
1496 }
1497
1498 bool ShenandoahBarrierStubC2::push_save_register_if_live(MacroAssembler& masm, Register reg) {
1499 if (is_live(reg)) {
1500 push_save_register(masm, reg);
1501 return true;
1502 } else {
1503 return false;
1504 }
1505 }
1506
1507 void ShenandoahBarrierStubC2::push_save_register(MacroAssembler& masm, Register reg) {
1508 __ movptr(Address(rsp, push_save_slot()), reg);
1509 }
1510
1511 void ShenandoahBarrierStubC2::pop_save_register(MacroAssembler& masm, Register reg) {
1512 __ movptr(reg, Address(rsp, pop_save_slot()));
1513 }
1514
1515 Register ShenandoahBarrierStubC2::select_temp_register(bool& selected_live, Address addr, Register reg1) {
1516 Register tmp = noreg;
1517 Register fallback_live = noreg;
1518
1519 // Try to select non-live first:
1520 for (int i = 0; i < Register::available_gp_registers(); i++) {
1521 Register r = as_Register(i);
1522 if (r != rsp && r != rbp && r != r12_heapbase && r != r15_thread &&
1523 r != reg1 && r != addr.base() && r != addr.index()) {
1524 if (!is_live(r)) {
1525 tmp = r;
1526 break;
1527 } else if (fallback_live == noreg) {
1528 fallback_live = r;
1529 }
1530 }
1531 }
1532
1533 // If we could not find a non-live register, select the live fallback:
1534 if (tmp == noreg) {
1535 tmp = fallback_live;
1536 selected_live = true;
1537 } else {
1538 selected_live = false;
1539 }
1540
1541 assert(tmp != noreg, "successfully selected");
1542 assert_different_registers(tmp, reg1);
1543 assert_different_registers(tmp, addr.base());
1544 assert_different_registers(tmp, addr.index());
1545 return tmp;
1546 }
1547
1548 void ShenandoahBarrierStubC2::post_init(int offset) {
1549 // Do nothing.
1550 }
1551 #undef __
1552 #endif