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