1 /*
2 * Copyright (c) 2018, 2022, Red Hat, Inc. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
28 #include "gc/shenandoah/shenandoahForwarding.hpp"
29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
30 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
31 #include "gc/shenandoah/shenandoahRuntime.hpp"
32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
34 #include "interpreter/interpreter.hpp"
35 #include "interpreter/interp_masm.hpp"
36 #include "runtime/javaThread.hpp"
37 #include "runtime/sharedRuntime.hpp"
38 #ifdef COMPILER1
39 #include "c1/c1_LIRAssembler.hpp"
40 #include "c1/c1_MacroAssembler.hpp"
41 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
42 #endif
43
44 #define __ masm->
45
46 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
47 Register src, Register dst, Register count, RegSet saved_regs) {
48 if (is_oop) {
49 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
50 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
51
52 Label done;
53
60 if (ShenandoahSATBBarrier && dest_uninitialized) {
61 __ tbz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
62 } else {
63 __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
64 __ tst(rscratch1, rscratch2);
65 __ br(Assembler::EQ, done);
66 }
67
68 __ push(saved_regs, sp);
69 if (UseCompressedOops) {
70 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop), src, dst, count);
71 } else {
72 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop), src, dst, count);
73 }
74 __ pop(saved_regs, sp);
75 __ bind(done);
76 }
77 }
78 }
79
80 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
81 Register obj,
82 Register pre_val,
83 Register thread,
84 Register tmp,
85 bool tosca_live,
86 bool expand_call) {
87 if (ShenandoahSATBBarrier) {
88 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, rscratch1, tosca_live, expand_call);
89 }
90 }
91
92 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
93 Register obj,
94 Register pre_val,
95 Register thread,
96 Register tmp1,
97 Register tmp2,
98 bool tosca_live,
99 bool expand_call) {
345 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
346 }
347
348 // 3: apply keep-alive barrier if needed
349 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
350 __ enter(/*strip_ret_addr*/true);
351 __ push_call_clobbered_registers();
352 satb_write_barrier_pre(masm /* masm */,
353 noreg /* obj */,
354 dst /* pre_val */,
355 rthread /* thread */,
356 tmp1 /* tmp1 */,
357 tmp2 /* tmp2 */,
358 true /* tosca_live */,
359 true /* expand_call */);
360 __ pop_call_clobbered_registers();
361 __ leave();
362 }
363 }
364
365 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
366 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
367 bool on_oop = is_reference_type(type);
368 if (!on_oop) {
369 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
370 return;
371 }
372
373 // flatten object address if needed
374 if (dst.index() == noreg && dst.offset() == 0) {
375 if (dst.base() != tmp3) {
376 __ mov(tmp3, dst.base());
377 }
378 } else {
379 __ lea(tmp3, dst);
380 }
381
382 shenandoah_write_barrier_pre(masm,
383 tmp3 /* obj */,
384 tmp2 /* pre_val */,
385 rthread /* thread */,
386 tmp1 /* tmp */,
387 val != noreg /* tosca_live */,
388 false /* expand_call */);
389
390 if (val == noreg) {
391 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), noreg, noreg, noreg, noreg);
392 } else {
393 // Barrier needs uncompressed oop for region cross check.
394 Register new_val = val;
395 if (UseCompressedOops) {
396 new_val = rscratch2;
397 __ mov(new_val, val);
398 }
399 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
400 }
401
402 }
403
404 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
405 Register obj, Register tmp, Label& slowpath) {
406 Label done;
407 // Resolve jobject
408 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
409
410 // Check for null.
411 __ cbz(obj, done);
412
413 assert(obj != rscratch2, "need rscratch2");
414 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
415 __ lea(rscratch2, gc_state);
416 __ ldrb(rscratch2, Address(rscratch2));
417
418 // Check for heap in evacuation phase
419 __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
420
421 __ bind(done);
564 if (is_cae) {
565 // We're falling through to done to indicate success. Success
566 // with is_cae is denoted by returning the value of expected as
567 // result.
568 __ mov(tmp2, expected);
569 }
570
571 __ bind(done);
572 // At entry to done, the Z (EQ) flag is on iff if the CAS
573 // operation was successful. Additionally, if is_cae, tmp2 holds
574 // the value most recently fetched from addr. In this case, success
575 // is denoted by tmp2 matching expected.
576
577 if (is_cae) {
578 __ mov(result, tmp2);
579 } else {
580 __ cset(result, Assembler::EQ);
581 }
582 }
583
584 #undef __
585
586 #ifdef COMPILER1
587
588 #define __ ce->masm()->
589
590 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
591 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
592 // At this point we know that marking is in progress.
593 // If do_load() is true then we have to emit the
594 // load of the previous value; otherwise it has already
595 // been loaded into _pre_val.
596
597 __ bind(*stub->entry());
598
599 assert(stub->pre_val()->is_register(), "Precondition.");
600
601 Register pre_val_reg = stub->pre_val()->as_register();
602
603 if (stub->do_load()) {
|
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 "precompiled.hpp"
27 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
28 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
29 #include "gc/shenandoah/shenandoahForwarding.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
32 #include "gc/shenandoah/shenandoahRuntime.hpp"
33 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
34 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
35 #include "gc/shenandoah/mode/shenandoahMode.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "interpreter/interp_masm.hpp"
38 #include "runtime/javaThread.hpp"
39 #include "runtime/sharedRuntime.hpp"
40 #ifdef COMPILER1
41 #include "c1/c1_LIRAssembler.hpp"
42 #include "c1/c1_MacroAssembler.hpp"
43 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
44 #endif
45
46 #define __ masm->
47
48 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
49 Register src, Register dst, Register count, RegSet saved_regs) {
50 if (is_oop) {
51 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
52 if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
53
54 Label done;
55
62 if (ShenandoahSATBBarrier && dest_uninitialized) {
63 __ tbz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
64 } else {
65 __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
66 __ tst(rscratch1, rscratch2);
67 __ br(Assembler::EQ, done);
68 }
69
70 __ push(saved_regs, sp);
71 if (UseCompressedOops) {
72 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop), src, dst, count);
73 } else {
74 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop), src, dst, count);
75 }
76 __ pop(saved_regs, sp);
77 __ bind(done);
78 }
79 }
80 }
81
82 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
83 Register start, Register count, Register tmp, RegSet saved_regs) {
84 if (ShenandoahCardBarrier && is_oop) {
85 gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp, saved_regs);
86 }
87 }
88
89 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
90 Register obj,
91 Register pre_val,
92 Register thread,
93 Register tmp,
94 bool tosca_live,
95 bool expand_call) {
96 if (ShenandoahSATBBarrier) {
97 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, rscratch1, tosca_live, expand_call);
98 }
99 }
100
101 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
102 Register obj,
103 Register pre_val,
104 Register thread,
105 Register tmp1,
106 Register tmp2,
107 bool tosca_live,
108 bool expand_call) {
354 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
355 }
356
357 // 3: apply keep-alive barrier if needed
358 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
359 __ enter(/*strip_ret_addr*/true);
360 __ push_call_clobbered_registers();
361 satb_write_barrier_pre(masm /* masm */,
362 noreg /* obj */,
363 dst /* pre_val */,
364 rthread /* thread */,
365 tmp1 /* tmp1 */,
366 tmp2 /* tmp2 */,
367 true /* tosca_live */,
368 true /* expand_call */);
369 __ pop_call_clobbered_registers();
370 __ leave();
371 }
372 }
373
374 void ShenandoahBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj) {
375 assert(ShenandoahCardBarrier, "Should have been checked by caller");
376
377 __ lsr(obj, obj, CardTable::card_shift());
378
379 assert(CardTable::dirty_card_val() == 0, "must be");
380
381 __ load_byte_map_base(rscratch1);
382
383 if (UseCondCardMark) {
384 Label L_already_dirty;
385 __ ldrb(rscratch2, Address(obj, rscratch1));
386 __ cbz(rscratch2, L_already_dirty);
387 __ strb(zr, Address(obj, rscratch1));
388 __ bind(L_already_dirty);
389 } else {
390 __ strb(zr, Address(obj, rscratch1));
391 }
392 }
393
394 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
395 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
396 bool on_oop = is_reference_type(type);
397 if (!on_oop) {
398 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
399 return;
400 }
401
402 // flatten object address if needed
403 if (dst.index() == noreg && dst.offset() == 0) {
404 if (dst.base() != tmp3) {
405 __ mov(tmp3, dst.base());
406 }
407 } else {
408 __ lea(tmp3, dst);
409 }
410
411 shenandoah_write_barrier_pre(masm,
412 tmp3 /* obj */,
413 tmp2 /* pre_val */,
414 rthread /* thread */,
415 tmp1 /* tmp */,
416 val != noreg /* tosca_live */,
417 false /* expand_call */);
418
419 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
420
421 bool in_heap = (decorators & IN_HEAP) != 0;
422 bool needs_post_barrier = (val != noreg) && in_heap && ShenandoahCardBarrier;
423 if (needs_post_barrier) {
424 store_check(masm, tmp3);
425 }
426 }
427
428 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
429 Register obj, Register tmp, Label& slowpath) {
430 Label done;
431 // Resolve jobject
432 BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
433
434 // Check for null.
435 __ cbz(obj, done);
436
437 assert(obj != rscratch2, "need rscratch2");
438 Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
439 __ lea(rscratch2, gc_state);
440 __ ldrb(rscratch2, Address(rscratch2));
441
442 // Check for heap in evacuation phase
443 __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
444
445 __ bind(done);
588 if (is_cae) {
589 // We're falling through to done to indicate success. Success
590 // with is_cae is denoted by returning the value of expected as
591 // result.
592 __ mov(tmp2, expected);
593 }
594
595 __ bind(done);
596 // At entry to done, the Z (EQ) flag is on iff if the CAS
597 // operation was successful. Additionally, if is_cae, tmp2 holds
598 // the value most recently fetched from addr. In this case, success
599 // is denoted by tmp2 matching expected.
600
601 if (is_cae) {
602 __ mov(result, tmp2);
603 } else {
604 __ cset(result, Assembler::EQ);
605 }
606 }
607
608 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
609 Register start, Register count, Register scratch, RegSet saved_regs) {
610 assert(ShenandoahCardBarrier, "Should have been checked by caller");
611
612 Label L_loop, L_done;
613 const Register end = count;
614
615 // Zero count? Nothing to do.
616 __ cbz(count, L_done);
617
618 // end = start + count << LogBytesPerHeapOop
619 // last element address to make inclusive
620 __ lea(end, Address(start, count, Address::lsl(LogBytesPerHeapOop)));
621 __ sub(end, end, BytesPerHeapOop);
622 __ lsr(start, start, CardTable::card_shift());
623 __ lsr(end, end, CardTable::card_shift());
624
625 // number of bytes to copy
626 __ sub(count, end, start);
627
628 __ load_byte_map_base(scratch);
629 __ add(start, start, scratch);
630 __ bind(L_loop);
631 __ strb(zr, Address(start, count));
632 __ subs(count, count, 1);
633 __ br(Assembler::GE, L_loop);
634 __ bind(L_done);
635 }
636
637 #undef __
638
639 #ifdef COMPILER1
640
641 #define __ ce->masm()->
642
643 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
644 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
645 // At this point we know that marking is in progress.
646 // If do_load() is true then we have to emit the
647 // load of the previous value; otherwise it has already
648 // been loaded into _pre_val.
649
650 __ bind(*stub->entry());
651
652 assert(stub->pre_val()->is_register(), "Precondition.");
653
654 Register pre_val_reg = stub->pre_val()->as_register();
655
656 if (stub->do_load()) {
|