1 /*
2 * Copyright (c) 2018, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
27 #include "gc/shenandoah/mode/shenandoahMode.hpp"
28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
30 #include "gc/shenandoah/shenandoahForwarding.hpp"
31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
32 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
33 #include "gc/shenandoah/shenandoahRuntime.hpp"
34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
35 #include "interpreter/interp_masm.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "runtime/sharedRuntime.hpp"
39 #ifdef COMPILER1
40 #include "c1/c1_LIRAssembler.hpp"
41 #include "c1/c1_MacroAssembler.hpp"
42 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
43 #endif
44 #ifdef COMPILER2
45 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
46 #endif
47
48 #define __ masm->
49
50 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
51 Register src, Register dst, Register count, RegSet saved_regs) {
52 if (is_oop) {
53 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
54 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
55
56 Label done;
57
58 // Avoid calling runtime if count == 0
59 __ cbz(count, done);
60
61 // Is GC active?
62 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
63 __ ldrb(rscratch1, gc_state);
64 if (ShenandoahSATBBarrier && dest_uninitialized) {
65 __ tbz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
66 } else {
67 __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
68 __ tst(rscratch1, rscratch2);
69 __ br(Assembler::EQ, done);
70 }
71
72 __ push(saved_regs, sp);
73 if (UseCompressedOops) {
74 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop), src, dst, count);
75 } else {
76 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop), src, dst, count);
77 }
78 __ pop(saved_regs, sp);
79 __ bind(done);
80 }
81 }
82 }
83
84 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
85 Register start, Register count, Register tmp, RegSet saved_regs) {
86 if (ShenandoahCardBarrier && is_oop) {
87 gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp, saved_regs);
88 }
89 }
90
91 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
92 Register obj,
93 Register pre_val,
94 Register thread,
95 Register tmp,
96 bool tosca_live,
97 bool expand_call) {
98 if (ShenandoahSATBBarrier) {
99 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, rscratch1, tosca_live, expand_call);
100 }
101 }
102
103 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
104 Register obj,
105 Register pre_val,
106 Register thread,
107 Register tmp1,
108 Register tmp2,
109 bool tosca_live,
110 bool expand_call) {
111 // If expand_call is true then we expand the call_VM_leaf macro
112 // directly to skip generating the check by
113 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
114
115 assert(thread == rthread, "must be");
116
117 Label done;
118 Label runtime;
119
120 assert_different_registers(obj, pre_val, tmp1, tmp2);
121 assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register");
122
123 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
124 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
125
126 // Is marking active?
127 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
128 __ ldrb(tmp1, gc_state);
129 __ tbz(tmp1, ShenandoahHeap::MARKING_BITPOS, done);
130
131 // Do we need to load the previous value?
132 if (obj != noreg) {
133 __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
134 }
135
136 // Is the previous value null?
137 __ cbz(pre_val, done);
138
139 // Can we store original value in the thread's buffer?
140 // Is index == 0?
141 // (The index field is typed as size_t.)
142
143 __ ldr(tmp1, index); // tmp := *index_adr
144 __ cbz(tmp1, runtime); // tmp == 0?
145 // If yes, goto runtime
146
147 __ sub(tmp1, tmp1, wordSize); // tmp := tmp - wordSize
148 __ str(tmp1, index); // *index_adr := tmp
149 __ ldr(tmp2, buffer);
150 __ add(tmp1, tmp1, tmp2); // tmp := tmp + *buffer_adr
151
152 // Record the previous value
153 __ str(pre_val, Address(tmp1, 0));
154 __ b(done);
155
156 __ bind(runtime);
157 // save the live input values
158 RegSet saved = RegSet::of(pre_val);
159 if (tosca_live) saved += RegSet::of(r0);
160 if (obj != noreg) saved += RegSet::of(obj);
161
162 __ push(saved, sp);
163
164 // Calling the runtime using the regular call_VM_leaf mechanism generates
165 // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
166 // that checks that the *(rfp+frame::interpreter_frame_last_sp) == nullptr.
167 //
168 // If we care generating the pre-barrier without a frame (e.g. in the
169 // intrinsified Reference.get() routine) then rfp might be pointing to
170 // the caller frame and so this check will most likely fail at runtime.
171 //
172 // Expanding the call directly bypasses the generation of the check.
173 // So when we do not have have a full interpreter frame on the stack
174 // expand_call should be passed true.
175
176 if (expand_call) {
177 assert(pre_val != c_rarg1, "smashed arg");
178 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
179 } else {
180 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
181 }
182
183 __ pop(saved, sp);
184
185 __ bind(done);
186 }
187
188 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) {
189 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
190 Label is_null;
191 __ cbz(dst, is_null);
192 resolve_forward_pointer_not_null(masm, dst, tmp);
193 __ bind(is_null);
194 }
195
196 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitly
197 // passed in.
198 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) {
199 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
200 // The below loads the mark word, checks if the lowest two bits are
201 // set, and if so, clear the lowest two bits and copy the result
202 // to dst. Otherwise it leaves dst alone.
203 // Implementing this is surprisingly awkward. I do it here by:
204 // - Inverting the mark word
205 // - Test lowest two bits == 0
206 // - If so, set the lowest two bits
207 // - Invert the result back, and copy to dst
208
209 bool borrow_reg = (tmp == noreg);
210 if (borrow_reg) {
211 // No free registers available. Make one useful.
212 tmp = rscratch1;
213 if (tmp == dst) {
214 tmp = rscratch2;
215 }
216 __ push(RegSet::of(tmp), sp);
217 }
218
219 assert_different_registers(tmp, dst);
220
221 Label done;
222 __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes()));
223 __ eon(tmp, tmp, zr);
224 __ ands(zr, tmp, markWord::lock_mask_in_place);
225 __ br(Assembler::NE, done);
226 __ orr(tmp, tmp, markWord::marked_value);
227 __ eon(dst, tmp, zr);
228 __ bind(done);
229
230 if (borrow_reg) {
231 __ pop(RegSet::of(tmp), sp);
232 }
233 }
234
235 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address load_addr, DecoratorSet decorators) {
236 assert(ShenandoahLoadRefBarrier, "Should be enabled");
237 assert(dst != rscratch2, "need rscratch2");
238 assert_different_registers(load_addr.base(), load_addr.index(), rscratch1, rscratch2);
239
240 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
241 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
242 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
243 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
244 bool is_narrow = UseCompressedOops && !is_native;
245
246 Label heap_stable, not_cset;
247 __ enter(/*strip_ret_addr*/true);
248 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
249 __ ldrb(rscratch2, gc_state);
250
251 // Check for heap stability
252 if (is_strong) {
253 __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, heap_stable);
254 } else {
255 Label lrb;
256 __ tbnz(rscratch2, ShenandoahHeap::WEAK_ROOTS_BITPOS, lrb);
257 __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, heap_stable);
258 __ bind(lrb);
259 }
260
261 // use r1 for load address
262 Register result_dst = dst;
263 if (dst == r1) {
264 __ mov(rscratch1, dst);
265 dst = rscratch1;
266 }
267
268 // Save r0 and r1, unless it is an output register
269 RegSet to_save = RegSet::of(r0, r1) - result_dst;
270 __ push(to_save, sp);
271 __ lea(r1, load_addr);
272 __ mov(r0, dst);
273
274 // Test for in-cset
275 if (is_strong) {
276 __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
277 __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
278 __ ldrb(rscratch2, Address(rscratch2, rscratch1));
279 __ tbz(rscratch2, 0, not_cset);
280 }
281
282 __ push_call_clobbered_registers();
283 if (is_strong) {
284 if (is_narrow) {
285 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
286 } else {
287 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
288 }
289 } else if (is_weak) {
290 if (is_narrow) {
291 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
292 } else {
293 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
294 }
295 } else {
296 assert(is_phantom, "only remaining strength");
297 assert(!is_narrow, "phantom access cannot be narrow");
298 // AOT saved adapters need relocation for this call.
299 __ lea(lr, RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom)));
300 }
301 __ blr(lr);
302 __ mov(rscratch1, r0);
303 __ pop_call_clobbered_registers();
304 __ mov(r0, rscratch1);
305
306 __ bind(not_cset);
307
308 __ mov(result_dst, r0);
309 __ pop(to_save, sp);
310
311 __ bind(heap_stable);
312 __ leave();
313 }
314
315 //
316 // Arguments:
317 //
318 // Inputs:
319 // src: oop location to load from, might be clobbered
320 //
321 // Output:
322 // dst: oop loaded from src location
323 //
324 // Kill:
325 // rscratch1 (scratch reg)
326 //
327 // Alias:
328 // dst: rscratch1 (might use rscratch1 as temporary output register to avoid clobbering src)
329 //
330 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
331 Register dst, Address src, Register tmp1, Register tmp2) {
332 // 1: non-reference load, no additional barrier is needed
333 if (!is_reference_type(type)) {
334 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
335 return;
336 }
337
338 // 2: load a reference from src location and apply LRB if needed
339 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
340 Register result_dst = dst;
341
342 // Preserve src location for LRB
343 if (dst == src.base() || dst == src.index()) {
344 dst = rscratch1;
345 }
346 assert_different_registers(dst, src.base(), src.index());
347
348 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
349
350 load_reference_barrier(masm, dst, src, decorators);
351
352 if (dst != result_dst) {
353 __ mov(result_dst, dst);
354 dst = result_dst;
355 }
356 } else {
357 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
358 }
359
360 // 3: apply keep-alive barrier if needed
361 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
362 __ enter(/*strip_ret_addr*/true);
363 __ push_call_clobbered_registers();
364 satb_write_barrier_pre(masm /* masm */,
365 noreg /* obj */,
366 dst /* pre_val */,
367 rthread /* thread */,
368 tmp1 /* tmp1 */,
369 tmp2 /* tmp2 */,
370 true /* tosca_live */,
371 true /* expand_call */);
372 __ pop_call_clobbered_registers();
373 __ leave();
374 }
375 }
376
377 void ShenandoahBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj) {
378 assert(ShenandoahCardBarrier, "Should have been checked by caller");
379
380 __ lsr(obj, obj, CardTable::card_shift());
381
382 assert(CardTable::dirty_card_val() == 0, "must be");
383
384 Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
385 __ ldr(rscratch1, curr_ct_holder_addr);
386
387 if (UseCondCardMark) {
388 Label L_already_dirty;
389 __ ldrb(rscratch2, Address(obj, rscratch1));
390 __ cbz(rscratch2, L_already_dirty);
391 __ strb(zr, Address(obj, rscratch1));
392 __ bind(L_already_dirty);
393 } else {
394 __ strb(zr, Address(obj, rscratch1));
395 }
396 }
397
398 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
399 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
400 bool on_oop = is_reference_type(type);
401 if (!on_oop) {
402 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
403 return;
404 }
405
406 // flatten object address if needed
407 if (dst.index() == noreg && dst.offset() == 0) {
408 if (dst.base() != tmp3) {
409 __ mov(tmp3, dst.base());
410 }
411 } else {
412 __ lea(tmp3, dst);
413 }
414
415 shenandoah_write_barrier_pre(masm,
416 tmp3 /* obj */,
417 tmp2 /* pre_val */,
418 rthread /* thread */,
419 tmp1 /* tmp */,
420 val != noreg /* tosca_live */,
421 false /* expand_call */);
422
423 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
424
425 bool in_heap = (decorators & IN_HEAP) != 0;
426 bool needs_post_barrier = (val != noreg) && in_heap && ShenandoahCardBarrier;
427 if (needs_post_barrier) {
428 store_check(masm, tmp3);
429 }
430 }
431
432 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
433 Register obj, Register tmp, Label& slowpath) {
434 Label done;
435 // Resolve jobject
436 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
437
438 // Check for null.
439 __ cbz(obj, done);
440
441 assert(obj != rscratch2, "need rscratch2");
442 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
443 __ lea(rscratch2, gc_state);
444 __ ldrb(rscratch2, Address(rscratch2));
445
446 // Check for heap in evacuation phase
447 __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
448
449 __ bind(done);
450 }
451
452 // Special Shenandoah CAS implementation that handles false negatives due
453 // to concurrent evacuation. The service is more complex than a
454 // traditional CAS operation because the CAS operation is intended to
455 // succeed if the reference at addr exactly matches expected or if the
456 // reference at addr holds a pointer to a from-space object that has
457 // been relocated to the location named by expected. There are two
458 // races that must be addressed:
459 // a) A parallel thread may mutate the contents of addr so that it points
460 // to a different object. In this case, the CAS operation should fail.
461 // b) A parallel thread may heal the contents of addr, replacing a
462 // from-space pointer held in addr with the to-space pointer
463 // representing the new location of the object.
464 // Upon entry to cmpxchg_oop, it is assured that new_val equals null
465 // or it refers to an object that is not being evacuated out of
466 // from-space, or it refers to the to-space version of an object that
467 // is being evacuated out of from-space.
468 //
469 // By default the value held in the result register following execution
470 // of the generated code sequence is 0 to indicate failure of CAS,
471 // non-zero to indicate success. If is_cae, the result is the value most
472 // recently fetched from addr rather than a boolean success indicator.
473 //
474 // Clobbers rscratch1, rscratch2
475 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm,
476 Register addr,
477 Register expected,
478 Register new_val,
479 bool acquire, bool release,
480 bool is_cae,
481 Register result) {
482 Register tmp1 = rscratch1;
483 Register tmp2 = rscratch2;
484 bool is_narrow = UseCompressedOops;
485 Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
486
487 assert_different_registers(addr, expected, tmp1, tmp2);
488 assert_different_registers(addr, new_val, tmp1, tmp2);
489
490 Label step4, done;
491
492 // There are two ways to reach this label. Initial entry into the
493 // cmpxchg_oop code expansion starts at step1 (which is equivalent
494 // to label step4). Additionally, in the rare case that four steps
495 // are required to perform the requested operation, the fourth step
496 // is the same as the first. On a second pass through step 1,
497 // control may flow through step 2 on its way to failure. It will
498 // not flow from step 2 to step 3 since we are assured that the
499 // memory at addr no longer holds a from-space pointer.
500 //
501 // The comments that immediately follow the step4 label apply only
502 // to the case in which control reaches this label by branch from
503 // step 3.
504
505 __ bind (step4);
506
507 // Step 4. CAS has failed because the value most recently fetched
508 // from addr is no longer the from-space pointer held in tmp2. If a
509 // different thread replaced the in-memory value with its equivalent
510 // to-space pointer, then CAS may still be able to succeed. The
511 // value held in the expected register has not changed.
512 //
513 // It is extremely rare we reach this point. For this reason, the
514 // implementation opts for smaller rather than potentially faster
515 // code. Ultimately, smaller code for this rare case most likely
516 // delivers higher overall throughput by enabling improved icache
517 // performance.
518
519 // Step 1. Fast-path.
520 //
521 // Try to CAS with given arguments. If successful, then we are done.
522 //
523 // No label required for step 1.
524
525 __ cmpxchg(addr, expected, new_val, size, acquire, release, false, tmp2);
526 // EQ flag set iff success. tmp2 holds value fetched.
527
528 // If expected equals null but tmp2 does not equal null, the
529 // following branches to done to report failure of CAS. If both
530 // expected and tmp2 equal null, the following branches to done to
531 // report success of CAS. There's no need for a special test of
532 // expected equal to null.
533
534 __ br(Assembler::EQ, done);
535 // if CAS failed, fall through to step 2
536
537 // Step 2. CAS has failed because the value held at addr does not
538 // match expected. This may be a false negative because the value fetched
539 // from addr (now held in tmp2) may be a from-space pointer to the
540 // original copy of same object referenced by to-space pointer expected.
541 //
542 // To resolve this, it suffices to find the forward pointer associated
543 // with fetched value. If this matches expected, retry CAS with new
544 // parameters. If this mismatches, then we have a legitimate
545 // failure, and we're done.
546 //
547 // No need for step2 label.
548
549 // overwrite tmp1 with from-space pointer fetched from memory
550 __ mov(tmp1, tmp2);
551
552 if (is_narrow) {
553 // Decode tmp1 in order to resolve its forward pointer
554 __ decode_heap_oop(tmp1, tmp1);
555 }
556 resolve_forward_pointer(masm, tmp1);
557 // Encode tmp1 to compare against expected.
558 __ encode_heap_oop(tmp1, tmp1);
559
560 // Does forwarded value of fetched from-space pointer match original
561 // value of expected? If tmp1 holds null, this comparison will fail
562 // because we know from step1 that expected is not null. There is
563 // no need for a separate test for tmp1 (the value originally held
564 // in memory) equal to null.
565 __ cmp(tmp1, expected);
566
567 // If not, then the failure was legitimate and we're done.
568 // Branching to done with NE condition denotes failure.
569 __ br(Assembler::NE, done);
570
571 // Fall through to step 3. No need for step3 label.
572
573 // Step 3. We've confirmed that the value originally held in memory
574 // (now held in tmp2) pointed to from-space version of original
575 // expected value. Try the CAS again with the from-space expected
576 // value. If it now succeeds, we're good.
577 //
578 // Note: tmp2 holds encoded from-space pointer that matches to-space
579 // object residing at expected. tmp2 is the new "expected".
580
581 // Note that macro implementation of __cmpxchg cannot use same register
582 // tmp2 for result and expected since it overwrites result before it
583 // compares result with expected.
584 __ cmpxchg(addr, tmp2, new_val, size, acquire, release, false, noreg);
585 // EQ flag set iff success. tmp2 holds value fetched, tmp1 (rscratch1) clobbered.
586
587 // If fetched value did not equal the new expected, this could
588 // still be a false negative because some other thread may have
589 // newly overwritten the memory value with its to-space equivalent.
590 __ br(Assembler::NE, step4);
591
592 if (is_cae) {
593 // We're falling through to done to indicate success. Success
594 // with is_cae is denoted by returning the value of expected as
595 // result.
596 __ mov(tmp2, expected);
597 }
598
599 __ bind(done);
600 // At entry to done, the Z (EQ) flag is on iff if the CAS
601 // operation was successful. Additionally, if is_cae, tmp2 holds
602 // the value most recently fetched from addr. In this case, success
603 // is denoted by tmp2 matching expected.
604
605 if (is_cae) {
606 __ mov(result, tmp2);
607 } else {
608 __ cset(result, Assembler::EQ);
609 }
610 }
611
612 #ifdef COMPILER2
613 void ShenandoahBarrierSetAssembler::load_ref_barrier_c2(const MachNode* node, MacroAssembler* masm, Register obj, Register addr, Register tmp, bool narrow, bool maybe_null) {
614 if (!ShenandoahLoadRefBarrierStubC2::needs_barrier(node)) {
615 return;
616 }
617 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
618 Label done;
619 if (maybe_null) {
620 __ cbz(obj, done);
621 }
622 ShenandoahLoadRefBarrierStubC2* const stub = ShenandoahLoadRefBarrierStubC2::create(node, obj, addr, tmp, noreg, noreg, narrow);
623 // Don't preserve the obj across the runtime call, we override it from the return value anyway.
624 stub->dont_preserve(obj);
625 // Check if GC marking is in progress, otherwise we don't have to do anything.
626 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
627 __ ldrb(rscratch1, gc_state);
628 __ tstw(rscratch1, ShenandoahHeap::HAS_FORWARDED);
629 __ br(Assembler::NE, *stub->entry());
630 __ bind(*stub->continuation());
631 __ bind(done);
632 }
633
634 void ShenandoahBarrierSetAssembler::satb_barrier_c2(const MachNode* node, MacroAssembler* masm, Register addr, Register pre_val) {
635 assert_different_registers(addr, pre_val);
636 if (!ShenandoahSATBBarrierStubC2::needs_barrier(node)) {
637 return;
638 }
639 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
640 ShenandoahSATBBarrierStubC2* const stub = ShenandoahSATBBarrierStubC2::create(node, addr, pre_val);
641
642 // Check if GC marking is in progress, otherwise we don't have to do anything.
643 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
644 __ ldrb(rscratch1, gc_state);
645 __ tstw(rscratch1, ShenandoahHeap::MARKING);
646 __ br(Assembler::NE, *stub->entry());
647 __ bind(*stub->continuation());
648 }
649
650 void ShenandoahBarrierSetAssembler::card_barrier_c2(const MachNode* node, MacroAssembler* masm, Register addr, Register tmp) {
651 if (!ShenandoahCardBarrier ||
652 (node->barrier_data() & (ShenandoahBarrierCardMark | ShenandoahBarrierCardMarkNotNull)) == 0) {
653 return;
654 }
655
656 Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
657 __ lsr(tmp, addr, CardTable::card_shift());
658
659 assert(CardTable::dirty_card_val() == 0, "must be");
660
661 Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
662 __ ldr(rscratch1, curr_ct_holder_addr);
663
664 if (UseCondCardMark) {
665 Label L_already_dirty;
666 __ ldrb(rscratch2, Address(tmp, rscratch1));
667 __ cbz(rscratch2, L_already_dirty);
668 __ strb(zr, Address(tmp, rscratch1));
669 __ bind(L_already_dirty);
670 } else {
671 __ strb(zr, Address(tmp, rscratch1));
672 }
673 }
674
675 void ShenandoahBarrierSetAssembler::cmpxchg_oop_c2(const MachNode* node,
676 MacroAssembler* masm,
677 Register addr,
678 Register expected,
679 Register new_val,
680 Register result,
681 bool acquire, bool release, bool weak,
682 bool is_cae) {
683 Register tmp = rscratch2;
684 Assembler::operand_size size = UseCompressedOops ? Assembler::word : Assembler::xword;
685
686 assert_different_registers(addr, expected, result, tmp);
687 assert_different_registers(addr, new_val, result, tmp);
688
689 ShenandoahCASBarrierSlowStubC2* const slow_stub = ShenandoahCASBarrierSlowStubC2::create(node, addr, expected, new_val, result, tmp, is_cae, acquire, release, weak);
690 ShenandoahCASBarrierMidStubC2* const mid_stub = ShenandoahCASBarrierMidStubC2::create(node, slow_stub, expected, result, tmp, is_cae);
691
692 // Step 1. Fast-path.
693 //
694 // Try to CAS with given arguments. If successful, then we are done.
695 __ cmpxchg(addr, expected, new_val, size, acquire, release, weak, result);
696 // EQ flag set iff success. result holds value fetched.
697
698 __ br(Assembler::NE, *mid_stub->entry());
699
700 // Slow-stub re-enters with condition flags according to CAS, we may need to
701 // set result accordingly.
702 __ bind(*slow_stub->continuation());
703 if (!is_cae) {
704 __ cset(result, Assembler::EQ);
705 }
706
707 // Mid-stub re-enters with result set correctly.
708 __ bind(*mid_stub->continuation());
709 }
710
711 #undef __
712 #define __ masm.
713
714 void ShenandoahLoadRefBarrierStubC2::emit_code(MacroAssembler& masm) {
715 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
716 __ bind(*entry());
717 Register obj = _obj;
718 if (_narrow) {
719 __ decode_heap_oop(_tmp1, _obj);
720 obj = _tmp1;
721 }
722 // Weak/phantom loads always need to go to runtime.
723 if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
724 // Check for object in cset.
725 __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
726 __ lsr(rscratch1, obj, ShenandoahHeapRegion::region_size_bytes_shift_jint());
727 __ ldrb(rscratch2, Address(rscratch2, rscratch1));
728 __ cbz(rscratch2, *continuation());
729 }
730 {
731 SaveLiveRegisters save_registers(&masm, this);
732 if (c_rarg0 != obj) {
733 if (c_rarg0 == _addr) {
734 __ mov(rscratch1, _addr);
735 _addr = rscratch1;
736 }
737 __ mov(c_rarg0, obj);
738 }
739 __ mov(c_rarg1, _addr);
740
741 if (_narrow) {
742 if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
743 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
744 } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
745 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
746 } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
747 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow));
748 }
749 } else {
750 if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
751 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
752 } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
753 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
754 } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
755 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
756 }
757 }
758 __ blr(rscratch1);
759 __ mov(_obj, r0);
760 }
761 if (_narrow) {
762 __ encode_heap_oop(_obj);
763 }
764 __ b(*continuation());
765 }
766
767 void ShenandoahSATBBarrierStubC2::emit_code(MacroAssembler& masm) {
768 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
769 __ bind(*entry());
770 // Do we need to load the previous value?
771 if (_addr != noreg) {
772 __ load_heap_oop(_preval, Address(_addr, 0), noreg, noreg, AS_RAW);
773 }
774 // Is the previous value null?
775 __ cbz(_preval, *continuation());
776
777 Address index(rthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
778 Address buffer(rthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
779 Label runtime;
780 __ ldr(rscratch1, index);
781 // If buffer is full, call into runtime.
782 __ cbz(rscratch1, runtime);
783
784 // The buffer is not full, store value into it.
785 __ sub(rscratch1, rscratch1, wordSize);
786 __ str(rscratch1, index);
787 __ ldr(rscratch2, buffer);
788 __ str(_preval, Address(rscratch2, rscratch1));
789 __ b(*continuation());
790
791 // Runtime call
792 __ bind(runtime);
793 {
794 SaveLiveRegisters save_registers(&masm, this);
795 if (c_rarg0 != _preval) {
796 __ mov(c_rarg0, _preval);
797 }
798 __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre_c2));
799 __ blr(rscratch1);
800 }
801 __ b(*continuation());
802 }
803
804 void ShenandoahCASBarrierMidStubC2::emit_code(MacroAssembler& masm) {
805 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
806 __ bind(*entry());
807
808 // Check if CAS result is null. If it is, then we must have a legitimate failure.
809 // This makes loading the fwdptr in the slow-path simpler.
810 __ tst(_result, _result);
811 // In case of !CAE, this has the correct value for legitimate failure (0/false)
812 // in result register.
813 __ br(Assembler::EQ, *continuation());
814
815 // Check if GC is in progress, otherwise we must have a legitimate failure.
816 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
817 __ ldrb(_tmp, gc_state);
818 __ tstw(_tmp, ShenandoahHeap::HAS_FORWARDED);
819 __ br(Assembler::NE, *_slow_stub->entry());
820
821 if (!_cae) {
822 __ mov(_result, 0); // result = false
823 }
824 __ b(*continuation());
825 }
826
827 void ShenandoahCASBarrierSlowStubC2::emit_code(MacroAssembler& masm) {
828 Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
829 __ bind(*entry());
830 Assembler::operand_size size = UseCompressedOops ? Assembler::word : Assembler::xword;
831
832 // Step 2. CAS has failed because the value held at addr does not
833 // match expected. This may be a false negative because the value fetched
834 // from addr (now held in result) may be a from-space pointer to the
835 // original copy of same object referenced by to-space pointer expected.
836 //
837 // To resolve this, it suffices to find the forward pointer associated
838 // with fetched value. If this matches expected, retry CAS with new
839 // parameters. If this mismatches, then we have a legitimate
840 // failure, and we're done.
841
842 // overwrite tmp with from-space pointer fetched from memory
843 __ mov(_tmp1, _result);
844
845 if (UseCompressedOops) {
846 // Decode tmp in order to resolve its forward pointer
847 __ decode_heap_oop_not_null(_tmp1, _tmp1);
848 }
849
850 // Load/decode forwarding pointer.
851 __ ldr(_tmp1, Address(_tmp1, oopDesc::mark_offset_in_bytes()));
852 // Negate the mark-word. This allows us to test lowest 2 bits easily while preserving the upper bits.
853 __ eon(_tmp1, _tmp1, zr);
854 __ ands(zr, _tmp1, markWord::lock_mask_in_place);
855 // Not forwarded, must have a legit CAS failure.
856 __ br(Assembler::NE, *continuation());
857 // Set the lowest two bits. This is equivalent to clearing the two bits after
858 // the subsequent inversion.
859 __ orr(_tmp1, _tmp1, markWord::marked_value);
860 // And invert back to get the forwardee.
861 __ eon(_tmp1, _tmp1, zr);
862
863 if (UseCompressedOops) {
864 // Encode tmp to compare against expected.
865 __ encode_heap_oop_not_null(_tmp1, _tmp1);
866 }
867
868 // Does forwarded value of fetched from-space pointer match original
869 // value of expected? If result holds null, this comparison will fail
870 // because we know from step1 that expected is not null. There is
871 // no need for a separate test for result (the value originally held
872 // in memory) equal to null.
873 __ cmp(_tmp1, _expected);
874
875 // If not, then the failure was legitimate and we're done.
876 // Branching to continuation with NE condition denotes failure.
877 __ br(Assembler::NE, *continuation());
878
879 // Fall through to step 3.
880
881 // Step 3. We've confirmed that the value originally held in memory
882 // (now held in result) pointed to from-space version of original
883 // expected value. Try the CAS again with the from-space expected
884 // value. If it now succeeds, we're good.
885 //
886 // Note: result holds encoded from-space pointer that matches to-space
887 // object residing at expected. result is the new "expected".
888
889 // Note that macro implementation of __cmpxchg cannot use same register
890 // tmp2 for result and expected since it overwrites result before it
891 // compares result with expected.
892 __ mov(_tmp1, _result);
893 __ cmpxchg(_addr_reg, _tmp1, _new_val, size, _acquire, _release, _weak, _result);
894 // EQ flag set iff success. result holds value fetched, rscratch1 clobbered.
895
896 // If fetched value did not equal the new expected, this could
897 // still be a false negative because some other thread may have
898 // newly overwritten the memory value with its to-space equivalent.
899 __ br(Assembler::EQ, *continuation());
900
901 // Step 4. Retry CAS with original to-space expected.
902 __ cmpxchg(_addr_reg, _expected, _new_val, size, _acquire, _release, _weak, _result);
903
904 __ b(*continuation());
905 }
906 #undef __
907 #define __ masm->
908 #endif // COMPILER2
909
910 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
911 Register start, Register count, Register scratch, RegSet saved_regs) {
912 assert(ShenandoahCardBarrier, "Should have been checked by caller");
913
914 Label L_loop, L_done;
915 const Register end = count;
916
917 // Zero count? Nothing to do.
918 __ cbz(count, L_done);
919
920 // end = start + count << LogBytesPerHeapOop
921 // last element address to make inclusive
922 __ lea(end, Address(start, count, Address::lsl(LogBytesPerHeapOop)));
923 __ sub(end, end, BytesPerHeapOop);
924 __ lsr(start, start, CardTable::card_shift());
925 __ lsr(end, end, CardTable::card_shift());
926
927 // number of bytes to copy
928 __ sub(count, end, start);
929
930 Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
931 __ ldr(scratch, curr_ct_holder_addr);
932 __ add(start, start, scratch);
933 __ bind(L_loop);
934 __ strb(zr, Address(start, count));
935 __ subs(count, count, 1);
936 __ br(Assembler::GE, L_loop);
937 __ bind(L_done);
938 }
939
940 #undef __
941
942 #ifdef COMPILER1
943
944 #define __ ce->masm()->
945
946 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
947 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
948 // At this point we know that marking is in progress.
949 // If do_load() is true then we have to emit the
950 // load of the previous value; otherwise it has already
951 // been loaded into _pre_val.
952
953 __ bind(*stub->entry());
954
955 assert(stub->pre_val()->is_register(), "Precondition.");
956
957 Register pre_val_reg = stub->pre_val()->as_register();
958
959 if (stub->do_load()) {
960 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
961 }
962 __ cbz(pre_val_reg, *stub->continuation());
963 ce->store_parameter(stub->pre_val()->as_register(), 0);
964 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
965 __ b(*stub->continuation());
966 }
967
968 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
969 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
970 __ bind(*stub->entry());
971
972 DecoratorSet decorators = stub->decorators();
973 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
974 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
975 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
976 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
977
978 Register obj = stub->obj()->as_register();
979 Register res = stub->result()->as_register();
980 Register addr = stub->addr()->as_pointer_register();
981 Register tmp1 = stub->tmp1()->as_register();
982 Register tmp2 = stub->tmp2()->as_register();
983
984 assert(res == r0, "result must arrive in r0");
985
986 if (res != obj) {
987 __ mov(res, obj);
988 }
989
990 if (is_strong) {
991 // Check for object in cset.
992 __ mov(tmp2, ShenandoahHeap::in_cset_fast_test_addr());
993 __ lsr(tmp1, res, ShenandoahHeapRegion::region_size_bytes_shift_jint());
994 __ ldrb(tmp2, Address(tmp2, tmp1));
995 __ cbz(tmp2, *stub->continuation());
996 }
997
998 ce->store_parameter(res, 0);
999 ce->store_parameter(addr, 1);
1000 if (is_strong) {
1001 if (is_native) {
1002 __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
1003 } else {
1004 __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
1005 }
1006 } else if (is_weak) {
1007 __ far_call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
1008 } else {
1009 assert(is_phantom, "only remaining strength");
1010 __ far_call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
1011 }
1012
1013 __ b(*stub->continuation());
1014 }
1015
1016 #undef __
1017
1018 #define __ sasm->
1019
1020 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
1021 __ prologue("shenandoah_pre_barrier", false);
1022
1023 // arg0 : previous value of memory
1024
1025 BarrierSet* bs = BarrierSet::barrier_set();
1026
1027 const Register pre_val = r0;
1028 const Register thread = rthread;
1029 const Register tmp = rscratch1;
1030
1031 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
1032 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
1033
1034 Label done;
1035 Label runtime;
1036
1037 // Is marking still active?
1038 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1039 __ ldrb(tmp, gc_state);
1040 __ tbz(tmp, ShenandoahHeap::MARKING_BITPOS, done);
1041
1042 // Can we store original value in the thread's buffer?
1043 __ ldr(tmp, queue_index);
1044 __ cbz(tmp, runtime);
1045
1046 __ sub(tmp, tmp, wordSize);
1047 __ str(tmp, queue_index);
1048 __ ldr(rscratch2, buffer);
1049 __ add(tmp, tmp, rscratch2);
1050 __ load_parameter(0, rscratch2);
1051 __ str(rscratch2, Address(tmp, 0));
1052 __ b(done);
1053
1054 __ bind(runtime);
1055 __ push_call_clobbered_registers();
1056 __ load_parameter(0, pre_val);
1057 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
1058 __ pop_call_clobbered_registers();
1059 __ bind(done);
1060
1061 __ epilogue();
1062 }
1063
1064 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
1065 __ prologue("shenandoah_load_reference_barrier", false);
1066 // arg0 : object to be resolved
1067
1068 __ push_call_clobbered_registers();
1069 __ load_parameter(0, r0);
1070 __ load_parameter(1, r1);
1071
1072 bool is_strong = ShenandoahBarrierSet::is_strong_access(decorators);
1073 bool is_weak = ShenandoahBarrierSet::is_weak_access(decorators);
1074 bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
1075 bool is_native = ShenandoahBarrierSet::is_native_access(decorators);
1076 if (is_strong) {
1077 if (is_native) {
1078 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
1079 } else {
1080 if (UseCompressedOops) {
1081 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
1082 } else {
1083 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
1084 }
1085 }
1086 } else if (is_weak) {
1087 assert(!is_native, "weak must not be called off-heap");
1088 if (UseCompressedOops) {
1089 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
1090 } else {
1091 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
1092 }
1093 } else {
1094 assert(is_phantom, "only remaining strength");
1095 assert(is_native, "phantom must only be called off-heap");
1096 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
1097 }
1098 __ blr(lr);
1099 __ mov(rscratch1, r0);
1100 __ pop_call_clobbered_registers();
1101 __ mov(r0, rscratch1);
1102
1103 __ epilogue();
1104 }
1105
1106 #undef __
1107
1108 #endif // COMPILER1