Path: blob/master/src/hotspot/cpu/arm/gc/g1/g1BarrierSetAssembler_arm.cpp
40948 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "asm/macroAssembler.inline.hpp"26#include "gc/g1/g1BarrierSet.hpp"27#include "gc/g1/g1BarrierSetAssembler.hpp"28#include "gc/g1/g1BarrierSetRuntime.hpp"29#include "gc/g1/g1ThreadLocalData.hpp"30#include "gc/g1/g1CardTable.hpp"31#include "gc/g1/g1ThreadLocalData.hpp"32#include "gc/g1/heapRegion.hpp"33#include "interpreter/interp_masm.hpp"34#include "runtime/sharedRuntime.hpp"35#include "runtime/thread.hpp"36#include "utilities/macros.hpp"37#ifdef COMPILER138#include "c1/c1_LIRAssembler.hpp"39#include "c1/c1_MacroAssembler.hpp"40#include "gc/g1/c1/g1BarrierSetC1.hpp"41#endif4243#define __ masm->4445#ifdef PRODUCT46#define BLOCK_COMMENT(str) /* nothing */47#else48#define BLOCK_COMMENT(str) __ block_comment(str)49#endif5051#define BIND(label) bind(label); BLOCK_COMMENT(#label ":")5253void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,54Register addr, Register count, int callee_saved_regs) {55bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;56if (!dest_uninitialized) {57assert( addr->encoding() < callee_saved_regs, "addr must be saved");58assert(count->encoding() < callee_saved_regs, "count must be saved");5960BLOCK_COMMENT("PreBarrier");6162RegisterSet saved_regs = RegisterSet(R0, as_Register(callee_saved_regs-1));63__ push(saved_regs | R9ifScratched);6465if (addr != R0) {66assert_different_registers(count, R0);67__ mov(R0, addr);68}69if (count != R1) {70__ mov(R1, count);71}7273if (UseCompressedOops) {74__ call(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry));75} else {76__ call(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry));77}7879__ pop(saved_regs | R9ifScratched);80}81}8283void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,84Register addr, Register count, Register tmp) {8586BLOCK_COMMENT("G1PostBarrier");87if (addr != R0) {88assert_different_registers(count, R0);89__ mov(R0, addr);90}91if (count != R1) {92__ mov(R1, count);93}94#if R9_IS_SCRATCHED95// Safer to save R9 here since callers may have been written96// assuming R9 survives. This is suboptimal but is not in97// general worth optimizing for the few platforms where R998// is scratched. Note that the optimization might not be to99// difficult for this particular call site.100__ push(R9);101#endif // !R9_IS_SCRATCHED102__ call(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_post_entry));103#if R9_IS_SCRATCHED104__ pop(R9);105#endif // !R9_IS_SCRATCHED106}107108// G1 pre-barrier.109// Blows all volatile registers R0-R3, Rtemp, LR).110// If store_addr != noreg, then previous value is loaded from [store_addr];111// in such case store_addr and new_val registers are preserved;112// otherwise pre_val register is preserved.113void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm,114Register store_addr,115Register new_val,116Register pre_val,117Register tmp1,118Register tmp2) {119Label done;120Label runtime;121122if (store_addr != noreg) {123assert_different_registers(store_addr, new_val, pre_val, tmp1, tmp2, noreg);124} else {125assert (new_val == noreg, "should be");126assert_different_registers(pre_val, tmp1, tmp2, noreg);127}128129Address in_progress(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));130Address index(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));131Address buffer(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));132133// Is marking active?134assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "adjust this code");135__ ldrb(tmp1, in_progress);136__ cbz(tmp1, done);137138// Do we need to load the previous value?139if (store_addr != noreg) {140__ load_heap_oop(pre_val, Address(store_addr, 0));141}142143// Is the previous value null?144__ cbz(pre_val, done);145146// Can we store original value in the thread's buffer?147// Is index == 0?148// (The index field is typed as size_t.)149150__ ldr(tmp1, index); // tmp1 := *index_adr151__ ldr(tmp2, buffer);152153__ subs(tmp1, tmp1, wordSize); // tmp1 := tmp1 - wordSize154__ b(runtime, lt); // If negative, goto runtime155156__ str(tmp1, index); // *index_adr := tmp1157158// Record the previous value159__ str(pre_val, Address(tmp2, tmp1));160__ b(done);161162__ bind(runtime);163164// save the live input values165if (store_addr != noreg) {166// avoid raw_push to support any ordering of store_addr and new_val167__ push(RegisterSet(store_addr) | RegisterSet(new_val));168} else {169__ push(pre_val);170}171172if (pre_val != R0) {173__ mov(R0, pre_val);174}175__ mov(R1, Rthread);176177__ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), R0, R1);178179if (store_addr != noreg) {180__ pop(RegisterSet(store_addr) | RegisterSet(new_val));181} else {182__ pop(pre_val);183}184185__ bind(done);186}187188// G1 post-barrier.189// Blows all volatile registers R0-R3, Rtemp, LR).190void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm,191Register store_addr,192Register new_val,193Register tmp1,194Register tmp2,195Register tmp3) {196197Address queue_index(Rthread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));198Address buffer(Rthread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));199200BarrierSet* bs = BarrierSet::barrier_set();201CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);202CardTable* ct = ctbs->card_table();203Label done;204Label runtime;205206// Does store cross heap regions?207208__ eor(tmp1, store_addr, new_val);209__ movs(tmp1, AsmOperand(tmp1, lsr, HeapRegion::LogOfHRGrainBytes));210__ b(done, eq);211212// crosses regions, storing NULL?213214__ cbz(new_val, done);215216// storing region crossing non-NULL, is card already dirty?217const Register card_addr = tmp1;218219__ mov_address(tmp2, (address)ct->byte_map_base());220__ add(card_addr, tmp2, AsmOperand(store_addr, lsr, CardTable::card_shift));221222__ ldrb(tmp2, Address(card_addr));223__ cmp(tmp2, (int)G1CardTable::g1_young_card_val());224__ b(done, eq);225226__ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad), tmp2);227228assert(CardTable::dirty_card_val() == 0, "adjust this code");229__ ldrb(tmp2, Address(card_addr));230__ cbz(tmp2, done);231232// storing a region crossing, non-NULL oop, card is clean.233// dirty card and log.234235__ strb(__ zero_register(tmp2), Address(card_addr));236237__ ldr(tmp2, queue_index);238__ ldr(tmp3, buffer);239240__ subs(tmp2, tmp2, wordSize);241__ b(runtime, lt); // go to runtime if now negative242243__ str(tmp2, queue_index);244245__ str(card_addr, Address(tmp3, tmp2));246__ b(done);247248__ bind(runtime);249250if (card_addr != R0) {251__ mov(R0, card_addr);252}253__ mov(R1, Rthread);254__ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), R0, R1);255256__ bind(done);257}258259void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,260Register dst, Address src, Register tmp1, Register tmp2, Register tmp3) {261bool on_oop = type == T_OBJECT || type == T_ARRAY;262bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;263bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;264bool on_reference = on_weak || on_phantom;265266ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2, tmp3);267if (on_oop && on_reference) {268// Generate the G1 pre-barrier code to log the value of269// the referent field in an SATB buffer.270g1_write_barrier_pre(masm, noreg, noreg, dst, tmp1, tmp2);271}272}273274275void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,276Address obj, Register new_val, Register tmp1, Register tmp2, Register tmp3, bool is_null) {277bool in_heap = (decorators & IN_HEAP) != 0;278bool as_normal = (decorators & AS_NORMAL) != 0;279assert((decorators & IS_DEST_UNINITIALIZED) == 0, "unsupported");280281bool needs_pre_barrier = as_normal;282bool needs_post_barrier = (new_val != noreg) && in_heap;283284// flatten object address if needed285assert (obj.mode() == basic_offset, "pre- or post-indexing is not supported here");286287const Register store_addr = obj.base();288if (obj.index() != noreg) {289assert (obj.disp() == 0, "index or displacement, not both");290assert(obj.offset_op() == add_offset, "addition is expected");291__ add(store_addr, obj.base(), AsmOperand(obj.index(), obj.shift(), obj.shift_imm()));292} else if (obj.disp() != 0) {293__ add(store_addr, obj.base(), obj.disp());294}295296if (needs_pre_barrier) {297g1_write_barrier_pre(masm, store_addr, new_val, tmp1, tmp2, tmp3);298}299300if (is_null) {301BarrierSetAssembler::store_at(masm, decorators, type, Address(store_addr), new_val, tmp1, tmp2, tmp3, true);302} else {303// G1 barrier needs uncompressed oop for region cross check.304Register val_to_store = new_val;305if (UseCompressedOops) {306val_to_store = tmp1;307__ mov(val_to_store, new_val);308}309BarrierSetAssembler::store_at(masm, decorators, type, Address(store_addr), val_to_store, tmp1, tmp2, tmp3, false);310if (needs_post_barrier) {311g1_write_barrier_post(masm, store_addr, new_val, tmp1, tmp2, tmp3);312}313}314}315316#ifdef COMPILER1317318#undef __319#define __ ce->masm()->320321void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {322G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();323// At this point we know that marking is in progress.324// If do_load() is true then we have to emit the325// load of the previous value; otherwise it has already326// been loaded into _pre_val.327328__ bind(*stub->entry());329assert(stub->pre_val()->is_register(), "Precondition.");330331Register pre_val_reg = stub->pre_val()->as_register();332333if (stub->do_load()) {334ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);335}336337__ cbz(pre_val_reg, *stub->continuation());338ce->verify_reserved_argument_area_size(1);339__ str(pre_val_reg, Address(SP));340__ call(bs->pre_barrier_c1_runtime_code_blob()->code_begin(), relocInfo::runtime_call_type);341342__ b(*stub->continuation());343}344345void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {346G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();347__ bind(*stub->entry());348assert(stub->addr()->is_register(), "Precondition.");349assert(stub->new_val()->is_register(), "Precondition.");350Register new_val_reg = stub->new_val()->as_register();351__ cbz(new_val_reg, *stub->continuation());352ce->verify_reserved_argument_area_size(1);353__ str(stub->addr()->as_pointer_register(), Address(SP));354__ call(bs->post_barrier_c1_runtime_code_blob()->code_begin(), relocInfo::runtime_call_type);355__ b(*stub->continuation());356}357358#undef __359#define __ sasm->360361void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {362// Input:363// - pre_val pushed on the stack364365__ set_info("g1_pre_barrier_slow_id", false);366367// save at least the registers that need saving if the runtime is called368const RegisterSet saved_regs = RegisterSet(R0,R3) | RegisterSet(R12) | RegisterSet(LR);369const int nb_saved_regs = 6;370assert(nb_saved_regs == saved_regs.size(), "fix nb_saved_regs");371__ push(saved_regs);372373const Register r_pre_val_0 = R0; // must be R0, to be ready for the runtime call374const Register r_index_1 = R1;375const Register r_buffer_2 = R2;376377Address queue_active(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()));378Address queue_index(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset()));379Address buffer(Rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset()));380381Label done;382Label runtime;383384// Is marking still active?385assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");386__ ldrb(R1, queue_active);387__ cbz(R1, done);388389__ ldr(r_index_1, queue_index);390__ ldr(r_pre_val_0, Address(SP, nb_saved_regs*wordSize));391__ ldr(r_buffer_2, buffer);392393__ subs(r_index_1, r_index_1, wordSize);394__ b(runtime, lt);395396__ str(r_index_1, queue_index);397__ str(r_pre_val_0, Address(r_buffer_2, r_index_1));398399__ bind(done);400401__ pop(saved_regs);402403__ ret();404405__ bind(runtime);406407__ save_live_registers();408409assert(r_pre_val_0 == c_rarg0, "pre_val should be in R0");410__ mov(c_rarg1, Rthread);411__ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), c_rarg0, c_rarg1);412413__ restore_live_registers_without_return();414415__ b(done);416}417418void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {419// Input:420// - store_addr, pushed on the stack421422__ set_info("g1_post_barrier_slow_id", false);423424Label done;425Label recheck;426Label runtime;427428Address queue_index(Rthread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset()));429Address buffer(Rthread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset()));430431AddressLiteral cardtable(ci_card_table_address_as<address>(), relocInfo::none);432433// save at least the registers that need saving if the runtime is called434const RegisterSet saved_regs = RegisterSet(R0,R3) | RegisterSet(R12) | RegisterSet(LR);435const int nb_saved_regs = 6;436assert(nb_saved_regs == saved_regs.size(), "fix nb_saved_regs");437__ push(saved_regs);438439const Register r_card_addr_0 = R0; // must be R0 for the slow case440const Register r_obj_0 = R0;441const Register r_card_base_1 = R1;442const Register r_tmp2 = R2;443const Register r_index_2 = R2;444const Register r_buffer_3 = R3;445const Register tmp1 = Rtemp;446447__ ldr(r_obj_0, Address(SP, nb_saved_regs*wordSize));448// Note: there is a comment in x86 code about not using449// ExternalAddress / lea, due to relocation not working450// properly for that address. Should be OK for arm, where we451// explicitly specify that 'cardtable' has a relocInfo::none452// type.453__ lea(r_card_base_1, cardtable);454__ add(r_card_addr_0, r_card_base_1, AsmOperand(r_obj_0, lsr, CardTable::card_shift));455456// first quick check without barrier457__ ldrb(r_tmp2, Address(r_card_addr_0));458459__ cmp(r_tmp2, (int)G1CardTable::g1_young_card_val());460__ b(recheck, ne);461462__ bind(done);463464__ pop(saved_regs);465466__ ret();467468__ bind(recheck);469470__ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad), tmp1);471472// reload card state after the barrier that ensures the stored oop was visible473__ ldrb(r_tmp2, Address(r_card_addr_0));474475assert(CardTable::dirty_card_val() == 0, "adjust this code");476__ cbz(r_tmp2, done);477478// storing region crossing non-NULL, card is clean.479// dirty card and log.480481assert(0 == (int)CardTable::dirty_card_val(), "adjust this code");482if ((ci_card_table_address_as<intptr_t>() & 0xff) == 0) {483// Card table is aligned so the lowest byte of the table address base is zero.484__ strb(r_card_base_1, Address(r_card_addr_0));485} else {486__ strb(__ zero_register(r_tmp2), Address(r_card_addr_0));487}488489__ ldr(r_index_2, queue_index);490__ ldr(r_buffer_3, buffer);491492__ subs(r_index_2, r_index_2, wordSize);493__ b(runtime, lt); // go to runtime if now negative494495__ str(r_index_2, queue_index);496497__ str(r_card_addr_0, Address(r_buffer_3, r_index_2));498499__ b(done);500501__ bind(runtime);502503__ save_live_registers();504505assert(r_card_addr_0 == c_rarg0, "card_addr should be in R0");506__ mov(c_rarg1, Rthread);507__ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), c_rarg0, c_rarg1);508509__ restore_live_registers_without_return();510511__ b(done);512}513514#undef __515516#endif // COMPILER1517518519