1 /* 2 * Copyright (c) 2018, 2023, Oracle and/or its affiliates. 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 "asm/macroAssembler.inline.hpp" 27 #include "gc/g1/g1BarrierSet.hpp" 28 #include "gc/g1/g1BarrierSetAssembler.hpp" 29 #include "gc/g1/g1BarrierSetRuntime.hpp" 30 #include "gc/g1/g1CardTable.hpp" 31 #include "gc/g1/g1HeapRegion.hpp" 32 #include "gc/g1/g1ThreadLocalData.hpp" 33 #include "gc/shared/collectedHeap.hpp" 34 #include "interpreter/interp_masm.hpp" 35 #include "runtime/javaThread.hpp" 36 #include "runtime/sharedRuntime.hpp" 37 #ifdef COMPILER1 38 #include "c1/c1_LIRAssembler.hpp" 39 #include "c1/c1_MacroAssembler.hpp" 40 #include "gc/g1/c1/g1BarrierSetC1.hpp" 41 #endif 42 43 #define __ masm-> 44 45 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators, 46 Register addr, Register count, RegSet saved_regs) { 47 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0; 48 if (!dest_uninitialized) { 49 Label done; 50 Address in_progress(rthread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset())); 51 52 // Is marking active? 53 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 54 __ ldrw(rscratch1, in_progress); 55 } else { 56 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 57 __ ldrb(rscratch1, in_progress); 58 } 59 __ cbzw(rscratch1, done); 60 61 __ push(saved_regs, sp); 62 if (count == c_rarg0) { 63 if (addr == c_rarg1) { 64 // exactly backwards!! 65 __ mov(rscratch1, c_rarg0); 66 __ mov(c_rarg0, c_rarg1); 67 __ mov(c_rarg1, rscratch1); 68 } else { 69 __ mov(c_rarg1, count); 70 __ mov(c_rarg0, addr); 71 } 72 } else { 73 __ mov(c_rarg0, addr); 74 __ mov(c_rarg1, count); 75 } 76 if (UseCompressedOops) { 77 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_narrow_oop_entry), 2); 78 } else { 79 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_pre_oop_entry), 2); 80 } 81 __ pop(saved_regs, sp); 82 83 __ bind(done); 84 } 85 } 86 87 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators, 88 Register start, Register count, Register scratch, RegSet saved_regs) { 89 __ push(saved_regs, sp); 90 assert_different_registers(start, count, scratch); 91 assert_different_registers(c_rarg0, count); 92 __ mov(c_rarg0, start); 93 __ mov(c_rarg1, count); 94 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_array_post_entry), 2); 95 __ pop(saved_regs, sp); 96 } 97 98 void G1BarrierSetAssembler::g1_write_barrier_pre(MacroAssembler* masm, 99 Register obj, 100 Register pre_val, 101 Register thread, 102 Register tmp1, 103 Register tmp2, 104 bool tosca_live, 105 bool expand_call) { 106 // If expand_call is true then we expand the call_VM_leaf macro 107 // directly to skip generating the check by 108 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp. 109 110 assert(thread == rthread, "must be"); 111 112 Label done; 113 Label runtime; 114 115 assert_different_registers(obj, pre_val, tmp1, tmp2); 116 assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register"); 117 118 Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset())); 119 Address index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset())); 120 Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset())); 121 122 // Is marking active? 123 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 124 __ ldrw(tmp1, in_progress); 125 } else { 126 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 127 __ ldrb(tmp1, in_progress); 128 } 129 __ cbzw(tmp1, 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 158 __ push_call_clobbered_registers(); 159 160 // Calling the runtime using the regular call_VM_leaf mechanism generates 161 // code (generated by InterpreterMacroAssember::call_VM_leaf_base) 162 // that checks that the *(rfp+frame::interpreter_frame_last_sp) == nullptr. 163 // 164 // If we care generating the pre-barrier without a frame (e.g. in the 165 // intrinsified Reference.get() routine) then rfp might be pointing to 166 // the caller frame and so this check will most likely fail at runtime. 167 // 168 // Expanding the call directly bypasses the generation of the check. 169 // So when we do not have have a full interpreter frame on the stack 170 // expand_call should be passed true. 171 172 if (expand_call) { 173 assert(pre_val != c_rarg1, "smashed arg"); 174 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread); 175 } else { 176 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread); 177 } 178 179 __ pop_call_clobbered_registers(); 180 181 __ bind(done); 182 183 } 184 185 void G1BarrierSetAssembler::g1_write_barrier_post(MacroAssembler* masm, 186 Register store_addr, 187 Register new_val, 188 Register thread, 189 Register tmp1, 190 Register tmp2) { 191 assert(thread == rthread, "must be"); 192 assert_different_registers(store_addr, new_val, thread, tmp1, tmp2, 193 rscratch1); 194 assert(store_addr != noreg && new_val != noreg && tmp1 != noreg 195 && tmp2 != noreg, "expecting a register"); 196 197 Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset())); 198 Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset())); 199 200 BarrierSet* bs = BarrierSet::barrier_set(); 201 CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs); 202 CardTable* ct = ctbs->card_table(); 203 204 Label done; 205 Label runtime; 206 207 // Does store cross heap regions? 208 209 __ eor(tmp1, store_addr, new_val); 210 __ lsr(tmp1, tmp1, HeapRegion::LogOfHRGrainBytes); 211 __ cbz(tmp1, done); 212 213 // crosses regions, storing null? 214 215 __ cbz(new_val, done); 216 217 // storing region crossing non-null, is card already dirty? 218 219 const Register card_addr = tmp1; 220 221 __ lsr(card_addr, store_addr, CardTable::card_shift()); 222 223 // get the address of the card 224 __ load_byte_map_base(tmp2); 225 __ add(card_addr, card_addr, tmp2); 226 __ ldrb(tmp2, Address(card_addr)); 227 __ cmpw(tmp2, (int)G1CardTable::g1_young_card_val()); 228 __ br(Assembler::EQ, done); 229 230 assert((int)CardTable::dirty_card_val() == 0, "must be 0"); 231 232 __ membar(Assembler::StoreLoad); 233 234 __ ldrb(tmp2, Address(card_addr)); 235 __ cbzw(tmp2, done); 236 237 // storing a region crossing, non-null oop, card is clean. 238 // dirty card and log. 239 240 __ strb(zr, Address(card_addr)); 241 242 __ ldr(rscratch1, queue_index); 243 __ cbz(rscratch1, runtime); 244 __ sub(rscratch1, rscratch1, wordSize); 245 __ str(rscratch1, queue_index); 246 247 __ ldr(tmp2, buffer); 248 __ str(card_addr, Address(tmp2, rscratch1)); 249 __ b(done); 250 251 __ bind(runtime); 252 // save the live input values 253 RegSet saved = RegSet::of(store_addr); 254 __ push(saved, sp); 255 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread); 256 __ pop(saved, sp); 257 258 __ bind(done); 259 } 260 261 void G1BarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 262 Register dst, Address src, Register tmp1, Register tmp2) { 263 bool on_oop = is_reference_type(type); 264 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0; 265 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0; 266 bool on_reference = on_weak || on_phantom; 267 ModRefBarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2); 268 if (on_oop && on_reference) { 269 // LR is live. It must be saved around calls. 270 __ enter(/*strip_ret_addr*/true); // barrier may call runtime 271 // Generate the G1 pre-barrier code to log the value of 272 // the referent field in an SATB buffer. 273 g1_write_barrier_pre(masm /* masm */, 274 noreg /* obj */, 275 dst /* pre_val */, 276 rthread /* thread */, 277 tmp1 /* tmp1 */, 278 tmp2 /* tmp2 */, 279 true /* tosca_live */, 280 true /* expand_call */); 281 __ leave(); 282 } 283 } 284 285 void G1BarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 286 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) { 287 // flatten object address if needed 288 if (dst.index() == noreg && dst.offset() == 0) { 289 if (dst.base() != tmp3) { 290 __ mov(tmp3, dst.base()); 291 } 292 } else { 293 __ lea(tmp3, dst); 294 } 295 296 g1_write_barrier_pre(masm, 297 tmp3 /* obj */, 298 tmp2 /* pre_val */, 299 rthread /* thread */, 300 tmp1 /* tmp1 */, 301 rscratch2 /* tmp2 */, 302 val != noreg /* tosca_live */, 303 false /* expand_call */); 304 305 if (val == noreg) { 306 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), noreg, noreg, noreg, noreg); 307 } else { 308 // G1 barrier needs uncompressed oop for region cross check. 309 Register new_val = val; 310 if (UseCompressedOops) { 311 new_val = rscratch2; 312 __ mov(new_val, val); 313 } 314 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg); 315 g1_write_barrier_post(masm, 316 tmp3 /* store_adr */, 317 new_val /* new_val */, 318 rthread /* thread */, 319 tmp1 /* tmp1 */, 320 tmp2 /* tmp2 */); 321 } 322 323 } 324 325 #ifdef COMPILER1 326 327 #undef __ 328 #define __ ce->masm()-> 329 330 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) { 331 G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 332 // At this point we know that marking is in progress. 333 // If do_load() is true then we have to emit the 334 // load of the previous value; otherwise it has already 335 // been loaded into _pre_val. 336 337 __ bind(*stub->entry()); 338 339 assert(stub->pre_val()->is_register(), "Precondition."); 340 341 Register pre_val_reg = stub->pre_val()->as_register(); 342 343 if (stub->do_load()) { 344 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/); 345 } 346 __ cbz(pre_val_reg, *stub->continuation()); 347 ce->store_parameter(stub->pre_val()->as_register(), 0); 348 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin())); 349 __ b(*stub->continuation()); 350 } 351 352 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) { 353 G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 354 __ bind(*stub->entry()); 355 assert(stub->addr()->is_register(), "Precondition."); 356 assert(stub->new_val()->is_register(), "Precondition."); 357 Register new_val_reg = stub->new_val()->as_register(); 358 __ cbz(new_val_reg, *stub->continuation()); 359 ce->store_parameter(stub->addr()->as_pointer_register(), 0); 360 __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin())); 361 __ b(*stub->continuation()); 362 } 363 364 #undef __ 365 366 #define __ sasm-> 367 368 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) { 369 __ prologue("g1_pre_barrier", false); 370 371 // arg0 : previous value of memory 372 373 BarrierSet* bs = BarrierSet::barrier_set(); 374 375 const Register pre_val = r0; 376 const Register thread = rthread; 377 const Register tmp = rscratch1; 378 379 Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset())); 380 Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset())); 381 Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset())); 382 383 Label done; 384 Label runtime; 385 386 // Is marking still active? 387 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 388 __ ldrw(tmp, in_progress); 389 } else { 390 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 391 __ ldrb(tmp, in_progress); 392 } 393 __ cbzw(tmp, done); 394 395 // Can we store original value in the thread's buffer? 396 __ ldr(tmp, queue_index); 397 __ cbz(tmp, runtime); 398 399 __ sub(tmp, tmp, wordSize); 400 __ str(tmp, queue_index); 401 __ ldr(rscratch2, buffer); 402 __ add(tmp, tmp, rscratch2); 403 __ load_parameter(0, rscratch2); 404 __ str(rscratch2, Address(tmp, 0)); 405 __ b(done); 406 407 __ bind(runtime); 408 __ push_call_clobbered_registers(); 409 __ load_parameter(0, pre_val); 410 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread); 411 __ pop_call_clobbered_registers(); 412 __ bind(done); 413 414 __ epilogue(); 415 } 416 417 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) { 418 __ prologue("g1_post_barrier", false); 419 420 // arg0: store_address 421 Address store_addr(rfp, 2*BytesPerWord); 422 423 BarrierSet* bs = BarrierSet::barrier_set(); 424 CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs); 425 CardTable* ct = ctbs->card_table(); 426 427 Label done; 428 Label runtime; 429 430 // At this point we know new_value is non-null and the new_value crosses regions. 431 // Must check to see if card is already dirty 432 433 const Register thread = rthread; 434 435 Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset())); 436 Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset())); 437 438 const Register card_offset = rscratch2; 439 // LR is free here, so we can use it to hold the byte_map_base. 440 const Register byte_map_base = lr; 441 442 assert_different_registers(card_offset, byte_map_base, rscratch1); 443 444 __ load_parameter(0, card_offset); 445 __ lsr(card_offset, card_offset, CardTable::card_shift()); 446 __ load_byte_map_base(byte_map_base); 447 __ ldrb(rscratch1, Address(byte_map_base, card_offset)); 448 __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val()); 449 __ br(Assembler::EQ, done); 450 451 assert((int)CardTable::dirty_card_val() == 0, "must be 0"); 452 453 __ membar(Assembler::StoreLoad); 454 __ ldrb(rscratch1, Address(byte_map_base, card_offset)); 455 __ cbzw(rscratch1, done); 456 457 // storing region crossing non-null, card is clean. 458 // dirty card and log. 459 __ strb(zr, Address(byte_map_base, card_offset)); 460 461 // Convert card offset into an address in card_addr 462 Register card_addr = card_offset; 463 __ add(card_addr, byte_map_base, card_addr); 464 465 __ ldr(rscratch1, queue_index); 466 __ cbz(rscratch1, runtime); 467 __ sub(rscratch1, rscratch1, wordSize); 468 __ str(rscratch1, queue_index); 469 470 // Reuse LR to hold buffer_addr 471 const Register buffer_addr = lr; 472 473 __ ldr(buffer_addr, buffer); 474 __ str(card_addr, Address(buffer_addr, rscratch1)); 475 __ b(done); 476 477 __ bind(runtime); 478 __ push_call_clobbered_registers(); 479 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread); 480 __ pop_call_clobbered_registers(); 481 __ bind(done); 482 __ epilogue(); 483 } 484 485 #undef __ 486 487 #endif // COMPILER1