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 #undef __
1024 #define __ masm->
1025
1026 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool narrow) {
1027 // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
1028 if (narrow) {
1029 __ movl(dst, src);
1030 } else {
1031 __ movq(dst, src);
1032 }
1033
1034 // Post-barrier: LRB
1035 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1036 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, dst, src, narrow, false);
1037 char check = 0;
1038 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1039 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1040 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node) ? ShenandoahHeap::WEAK_ROOTS : 0;
1041 stub->enter_if_gc_state(*masm, check);
1042 }
1043 }
1044
1045 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm,
1046 Address dst, bool dst_narrow,
1047 Register src, bool src_narrow,
1048 Register tmp) {
1049
1050 // Pre-barrier: SATB, keep-alive the current memory value.
1051 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1052 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier(node), "Should not be required for stores");
1053 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, dst, dst_narrow, true);
1054 stub->enter_if_gc_state(*masm, ShenandoahHeap::MARKING);
1055 }
1056
1057 // Need to encode into tmp, because we cannot clobber src.
1058 // TODO: Maybe there is a matcher way to test that src is unused after this?
1059 if (dst_narrow && !src_narrow) {
1060 __ movq(tmp, src);
1061 if (ShenandoahBarrierStubC2::maybe_null(node)) {
1062 __ encode_heap_oop(tmp);
1063 } else {
1064 __ encode_heap_oop_not_null(tmp);
1065 }
1066 src = tmp;
1067 }
1068
1069 // Do the actual store
1070 if (dst_narrow) {
1071 __ movl(dst, src);
1072 } else {
1073 __ movq(dst, src);
1074 }
1075
1076 // Post-barrier: card updates.
1077 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1078 card_barrier_c2(masm, dst, tmp);
1079 }
1080 }
1081
1082 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm,
1083 Register res, Address addr,
1084 Register oldval, Register newval, Register tmp,
1085 bool narrow) {
1086
1087 assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
1088
1089 // Oldval and newval can be in the same register, but all other registers should be
1090 // distinct for extra safety, as we shuffle register values around.
1091 assert_different_registers(oldval, tmp, addr.base(), addr.index());
1092 assert_different_registers(newval, tmp, addr.base(), addr.index());
1093
1094 // Pre-barrier covers several things:
1095 // a. Avoids false positives from CAS encountering to-space memory values.
1096 // b. Satisfies the need for LRB for the CAE result.
1097 // c. Records old value for the sake of SATB.
1098 //
1099 // (a) and (b) are covered because load barrier does memory location fixup.
1100 // (c) is covered by KA on the current memory value.
1101 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1102 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, addr, narrow, true);
1103 char check = 0;
1104 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1105 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1106 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node), "Not supported for CAS");
1107 stub->enter_if_gc_state(*masm, check);
1108 }
1109
1110 // CAS!
1111 __ lock();
1112 if (narrow) {
1113 __ cmpxchgl(newval, addr);
1114 } else {
1115 __ cmpxchgptr(newval, addr);
1116 }
1117
1118 // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
1119 if (res != noreg) {
1120 __ setcc(Assembler::equal, res);
1121 }
1122
1123 // Post-barrier deals with card updates.
1124 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1125 card_barrier_c2(masm, addr, tmp);
1126 }
1127 }
1128
1129 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register newval, Address addr, Register tmp, bool narrow) {
1130 assert_different_registers(newval, tmp, addr.base(), addr.index());
1131
1132 // Pre-barrier covers several things:
1133 // a. Satisfies the need for LRB for the GAS result.
1134 // b. Records old value for the sake of SATB.
1135 //
1136 // (a) is covered because load barrier does memory location fixup.
1137 // (b) is covered by KA on the current memory value.
1138 if (ShenandoahBarrierStubC2::needs_slow_barrier(node)) {
1139 ShenandoahBarrierStubC2* const stub = ShenandoahBarrierStubC2::create(node, tmp, addr, narrow, true);
1140 char check = 0;
1141 check |= ShenandoahBarrierStubC2::needs_keep_alive_barrier(node) ? ShenandoahHeap::MARKING : 0;
1142 check |= ShenandoahBarrierStubC2::needs_load_ref_barrier(node) ? ShenandoahHeap::HAS_FORWARDED : 0;
1143 assert(!ShenandoahBarrierStubC2::needs_load_ref_barrier_weak(node), "Not supported for GAS");
1144 stub->enter_if_gc_state(*masm, check);
1145 }
1146
1147 if (narrow) {
1148 __ xchgl(newval, addr);
1149 } else {
1150 __ xchgq(newval, addr);
1151 }
1152
1153 // Post-barrier deals with card updates.
1154 if (ShenandoahBarrierStubC2::needs_card_barrier(node)) {
1155 card_barrier_c2(masm, addr, tmp);
1156 }
1157 }
1158
1159 void ShenandoahBarrierSetAssembler::card_barrier_c2(MacroAssembler* masm, Address dst, Register tmp) {
1160 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
1161
1162 // TODO: Might be a good place to implement some filters here.
1163 // For example, G1 only flips card marks for stores within a single region.
1164
1165 __ lea(tmp, dst);
1166 __ shrptr(tmp, CardTable::card_shift());
1167 __ addptr(tmp, Address(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset())));
1168 Address card_address(tmp, 0);
1169
1170 assert(CardTable::dirty_card_val() == 0, "Encoding assumption");
1171 Label L_done;
1172 if (UseCondCardMark) {
1173 __ cmpb(card_address, 0);
1174 __ jccb(Assembler::equal, L_done);
1175 }
1176 if (UseCompressedOops && CompressedOops::base() == nullptr) {
1177 __ movb(card_address, r12);
1178 } else {
1179 __ movb(card_address, 0);
1180 }
1181 __ bind(L_done);
1182 }
1183
1184 #undef __
1185 #define __ masm.
1186
1187 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state) {
1188 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
1189
1190 // Emit the unconditional branch in the first version of the method.
1191 // Let the rest of runtime figure out how to manage it.
1192 __ relocate(barrier_Relocation::spec());
1193 __ jmp(*entry(), /* maybe_short = */ false);
1194
1195 #ifdef ASSERT
1196 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1197 __ testb(gc_state, 0xFF);
1198 __ jccb(Assembler::zero, *continuation());
1199 __ hlt(); // Correctness bug: barrier is NOP-ed, but heap is NOT IDLE
1200 #endif
1201 // TODO: When barriers are consistently turned off at the end of the cycle, assert that barrier is NOP-ed.
1202
1203 __ bind(*continuation());
1204 }
1205
1206 address ShenandoahBarrierSetAssembler::parse_stub_address(address pc) {
1207 NativeInstruction* ni = nativeInstruction_at(pc);
1208 assert(ni->is_jump(), "Initial code version: GC barrier fastpath must be a jump");
1209 NativeJump* jmp = nativeJump_at(pc);
1210 return jmp->jump_destination();
1211 }
1212
1213 void insert_5_byte_nop(address pc) {
1214 *(pc + 0) = 0x0F;
1215 *(pc + 1) = 0x1F;
1216 *(pc + 2) = 0x44;
1217 *(pc + 3) = 0x00;
1218 *(pc + 4) = 0x00;
1219 ICache::invalidate_range(pc, 5);
1220 }
1221
1222 bool is_5_byte_nop(address pc) {
1223 if (*(pc + 0) != 0x0F) return false;
1224 if (*(pc + 1) != 0x1F) return false;
1225 if (*(pc + 2) != 0x44) return false;
1226 if (*(pc + 3) != 0x00) return false;
1227 if (*(pc + 4) != 0x00) return false;
1228 return true;
1229 }
1230
1231 void check_at(bool cond, address pc, const char* msg) {
1232 assert(cond, "%s: at PC " PTR_FORMAT ": %02x%02x%02x%02x%02x",
1233 msg, p2i(pc), *(pc + 0), *(pc + 1), *(pc + 2), *(pc + 3), *(pc + 4));
1234 }
1235
1236 void ShenandoahBarrierSetAssembler::patch_branch_to_nop(address pc) {
1237 NativeInstruction* ni = nativeInstruction_at(pc);
1238 if (ni->is_jump()) {
1239 insert_5_byte_nop(pc);
1240 } else {
1241 check_at(is_5_byte_nop(pc), pc, "Should already be nop");
1242 }
1243 }
1244
1245 void ShenandoahBarrierSetAssembler::patch_nop_to_branch(address pc, address stub_addr) {
1246 NativeInstruction* ni = nativeInstruction_at(pc);
1247 if (is_5_byte_nop(pc)) {
1248 NativeJump::insert(pc, stub_addr);
1249 } else {
1250 check_at(ni->is_jump(), pc, "Should already be jump");
1251 check_at(nativeJump_at(pc)->jump_destination() == stub_addr, pc, "Jump should be to the same address");
1252 }
1253 }
1254
1255 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
1256 assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
1257
1258 __ bind(*entry());
1259
1260 Label L_done, L_barriers;
1261
1262 // If we need to load ourselves, do it here.
1263 if (_do_load) {
1264 if (_narrow) {
1265 __ movl(_obj, _addr);
1266 } else {
1267 __ movq(_obj, _addr);
1268 }
1269 }
1270
1271 // If the object is null, there is no point in applying barriers.
1272 if (_narrow) {
1273 __ testl(_obj, _obj);
1274 } else {
1275 __ testptr(_obj, _obj);
1276 }
1277 __ jccb(Assembler::notZero, L_barriers);
1278
1279 // Exit here.
1280 __ bind(L_done);
1281 __ jmp(*continuation());
1282
1283 // Barriers start here.
1284 __ bind(L_barriers);
1285
1286 // Barriers need temp to work, allocate one now.
1287 bool tmp_live;
1288 Register tmp = select_temp_register(tmp_live, _addr, _obj);
1289 if (tmp_live) {
1290 push_save_register(masm, tmp);
1291 }
1292
1293 // If object is narrow, we need to decode it first: barrier checks need full oops.
1294 if (_narrow) {
1295 __ decode_heap_oop_not_null(_obj);
1296 }
1297
1298 // Go for barriers. If both barriers are required (rare), do a runtime check for enabled barrier.
1299 if (_needs_keep_alive_barrier) {
1300 keepalive(masm, _obj, tmp);
1301 }
1302 if (_needs_load_ref_barrier) {
1303 lrb(masm, _obj, _addr, tmp);
1304 }
1305
1306 if (tmp_live) {
1307 pop_save_register(masm, tmp);
1308 }
1309
1310 // If object is narrow, we need to encode it before exiting.
1311 // For encoding, dst can only turn null if we are dealing with weak loads.
1312 // Otherwise, we have already null-checked. We can skip all this if we performed
1313 // the load ourselves, which means the value is not used by caller.
1314 if (_narrow && !_do_load) {
1315 if (_needs_load_ref_weak_barrier) {
1316 __ encode_heap_oop(_obj);
1317 } else {
1318 __ encode_heap_oop_not_null(_obj);
1319 }
1320 }
1321 __ jmp(L_done);
1322 }
1323
1324 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Register obj, Register tmp1) {
1325 Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
1326 Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
1327
1328 Label L_fast, L_done;
1329
1330 // Hotpatched GC checks only care about idle/non-idle state, so we need to check again here.
1331 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1332 __ testb(gc_state, ShenandoahHeap::MARKING);
1333 __ jccb(Assembler::zero, L_done);
1334
1335 // Check if buffer is already full. Go slow, if so.
1336 __ movptr(tmp1, index);
1337 __ testptr(tmp1, tmp1);
1338 __ jccb(Assembler::notZero, L_fast);
1339
1340 // Slow-path: call runtime to handle.
1341 {
1342 // Shuffle in the arguments. The end result should be:
1343 // c_rarg0 <-- obj
1344 //
1345 // Save clobbered registers before overwriting them.
1346 bool clobbered_c_rarg0 = false;
1347 if (c_rarg0 != obj) {
1348 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1349 __ mov(c_rarg0, obj);
1350 }
1351
1352 // Go to runtime stub and handle the rest there.
1353 __ call(RuntimeAddress(keepalive_runtime_entry_addr()));
1354
1355 // Restore the clobbered registers.
1356 if (clobbered_c_rarg0) {
1357 pop_save_register(masm, c_rarg0);
1358 }
1359 __ jmpb(L_done);
1360 }
1361
1362 // Fast-path: put object into buffer.
1363 __ bind(L_fast);
1364 __ subptr(tmp1, wordSize);
1365 __ movptr(index, tmp1);
1366 __ addptr(tmp1, buffer);
1367 __ movptr(Address(tmp1, 0), obj);
1368
1369 __ bind(L_done);
1370 }
1371
1372 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm, Register obj, Address addr, Register tmp) {
1373 Label L_done, L_slow;
1374
1375 // Hotpatched GC checks only care about idle/non-idle state, so we need a check here.
1376 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1377 __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0));
1378 __ jccb(Assembler::zero, L_done);
1379
1380 // If weak references are being processed, weak/phantom loads need to go slow,
1381 // regadless of their cset status.
1382 if (_needs_load_ref_weak_barrier) {
1383 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1384 __ testb(gc_state, ShenandoahHeap::WEAK_ROOTS);
1385 __ jccb(Assembler::notZero, L_slow);
1386 }
1387
1388 // Compute the cset bitmap index
1389 __ movptr(tmp, obj);
1390 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1391
1392 // If cset address is in good spot to just use it as offset. It almost always is.
1393 Address cset_addr_arg;
1394 intptr_t cset_addr = (intptr_t) ShenandoahHeap::in_cset_fast_test_addr();
1395 if ((cset_addr >> 3) < INT32_MAX) {
1396 assert(is_aligned(cset_addr, 8), "Sanity");
1397 cset_addr_arg = Address(tmp, checked_cast<int>(cset_addr >> 3), Address::times_8);
1398 } else {
1399 __ addptr(tmp, cset_addr);
1400 cset_addr_arg = Address(tmp, 0);
1401 }
1402
1403 // Cset-check. Fall-through to slow if in collection set.
1404 __ cmpb(cset_addr_arg, 0);
1405 __ jccb(Assembler::equal, L_done);
1406
1407 // Slow path
1408 __ bind(L_slow);
1409 {
1410 assert_different_registers(rax, c_rarg0, c_rarg1);
1411
1412 // Shuffle in the arguments. The end result should be:
1413 // c_rarg0 <-- obj
1414 // c_rarg1 <-- lea(addr)
1415 //
1416 // Save clobbered registers before overwriting them, unless they
1417 // carry obj, which would be overwritten on return.
1418 bool clobbered_c_rarg0 = false;
1419 bool clobbered_c_rarg1 = false;
1420 bool clobbered_rax = false;
1421
1422 if (obj == c_rarg0) {
1423 clobbered_c_rarg1 = push_save_register_if_live(masm, c_rarg1);
1424 __ lea(c_rarg1, addr);
1425 } else if (obj == c_rarg1) {
1426 // Set up arguments in reverse, and then flip them
1427 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1428 __ lea(c_rarg0, addr);
1429 __ xchgptr(c_rarg0, c_rarg1);
1430 } else {
1431 assert_different_registers(obj, c_rarg0, c_rarg1);
1432 clobbered_c_rarg0 = push_save_register_if_live(masm, c_rarg0);
1433 clobbered_c_rarg1 = push_save_register_if_live(masm, c_rarg1);
1434 __ lea(c_rarg1, addr);
1435 __ movptr(c_rarg0, obj);
1436 }
1437 if (obj != rax) {
1438 clobbered_rax = push_save_register_if_live(masm, rax);
1439 }
1440
1441 // Go to runtime stub and handle the rest there.
1442 __ call(RuntimeAddress(lrb_runtime_entry_addr()));
1443
1444 // Save the result where needed and restore the clobbered registers.
1445 if (obj != rax) {
1446 __ movptr(obj, rax);
1447 }
1448 if (clobbered_rax) {
1449 pop_save_register(masm, rax);
1450 }
1451 if (clobbered_c_rarg1) {
1452 pop_save_register(masm, c_rarg1);
1453 }
1454 if (clobbered_c_rarg0) {
1455 pop_save_register(masm, c_rarg0);
1456 }
1457 }
1458
1459 __ bind(L_done);
1460 }
1461
1462 bool ShenandoahBarrierStubC2::has_live_vector_registers() {
1463 RegMaskIterator rmi(preserve_set());
1464 while (rmi.has_next()) {
1465 const OptoReg::Name opto_reg = rmi.next();
1466 const VMReg vm_reg = OptoReg::as_VMReg(opto_reg);
1467 if (vm_reg->is_Register()) {
1468 // Not a vector.
1469 } else if (vm_reg->is_KRegister()) {
1470 // Definitely vector.
1471 return true;
1472 } else if (vm_reg->is_XMMRegister()) {
1473 // Maybe vector, assume the worst right now.
1474 return true;
1475 } else {
1476 fatal("Unexpected register type");
1477 }
1478 }
1479 return false;
1480 }
1481
1482 bool ShenandoahBarrierStubC2::is_live(Register reg) {
1483 // TODO: Precompute the generic register map for faster lookups.
1484 RegMaskIterator rmi(preserve_set());
1485 while (rmi.has_next()) {
1486 const OptoReg::Name opto_reg = rmi.next();
1487 const VMReg vm_reg = OptoReg::as_VMReg(opto_reg);
1488 if (vm_reg->is_Register()) {
1489 if (reg == vm_reg->as_Register()) {
1490 return true;
1491 }
1492 } else if (vm_reg->is_KRegister()) {
1493 // Do not care, skip.
1494 } else if (vm_reg->is_XMMRegister()) {
1495 // Do not care, skip.
1496 } else {
1497 fatal("Unexpected register type");
1498 }
1499 }
1500 return false;
1501 }
1502
1503 bool ShenandoahBarrierStubC2::push_save_register_if_live(MacroAssembler& masm, Register reg) {
1504 if (is_live(reg)) {
1505 push_save_register(masm, reg);
1506 return true;
1507 } else {
1508 return false;
1509 }
1510 }
1511
1512 void ShenandoahBarrierStubC2::push_save_register(MacroAssembler& masm, Register reg) {
1513 __ movptr(Address(rsp, push_save_slot()), reg);
1514 }
1515
1516 void ShenandoahBarrierStubC2::pop_save_register(MacroAssembler& masm, Register reg) {
1517 __ movptr(reg, Address(rsp, pop_save_slot()));
1518 }
1519
1520 Register ShenandoahBarrierStubC2::select_temp_register(bool& selected_live, Address addr, Register reg1) {
1521 Register tmp = noreg;
1522 Register fallback_live = noreg;
1523
1524 // Try to select non-live first:
1525 for (int i = 0; i < Register::available_gp_registers(); i++) {
1526 Register r = as_Register(i);
1527 if (r != rsp && r != rbp && r != r12_heapbase && r != r15_thread &&
1528 r != reg1 && r != addr.base() && r != addr.index()) {
1529 if (!is_live(r)) {
1530 tmp = r;
1531 break;
1532 } else if (fallback_live == noreg) {
1533 fallback_live = r;
1534 }
1535 }
1536 }
1537
1538 // If we could not find a non-live register, select the live fallback:
1539 if (tmp == noreg) {
1540 tmp = fallback_live;
1541 selected_live = true;
1542 } else {
1543 selected_live = false;
1544 }
1545
1546 assert(tmp != noreg, "successfully selected");
1547 assert_different_registers(tmp, reg1);
1548 assert_different_registers(tmp, addr.base());
1549 assert_different_registers(tmp, addr.index());
1550 return tmp;
1551 }
1552
1553 #undef __
1554 #endif