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 bool in_heap = (decorators & IN_HEAP) != 0; 279 bool as_normal = (decorators & AS_NORMAL) != 0; 280 assert((decorators & IS_DEST_UNINITIALIZED) == 0, "unsupported"); 281 282 bool needs_pre_barrier = as_normal; 283 bool needs_post_barrier = (val != noreg && in_heap); 284 285 286 if (tmp3 == noreg) { 287 tmp3 = rscratch2; 288 } 289 // assert_different_registers(val, tmp1, tmp2, tmp3, rscratch1, rscratch2); 290 assert_different_registers(val, tmp1, tmp2, tmp3); 291 292 // flatten object address if needed 293 if (dst.index() == noreg && dst.offset() == 0) { 294 if (dst.base() != tmp1) { 295 __ mov(tmp1, dst.base()); 296 } 297 } else { 298 __ lea(tmp1, dst); 299 } 300 301 302 if (needs_pre_barrier) { 303 g1_write_barrier_pre(masm, 304 tmp1 /* obj */, 305 tmp2 /* pre_val */, 306 rthread /* thread */, 307 tmp3 /* tmp */, 308 val != noreg /* tosca_live */, 309 false /* expand_call */); 310 } 311 312 if (val == noreg) { 313 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), noreg, noreg, noreg, noreg); 314 } else { 315 // G1 barrier needs uncompressed oop for region cross check. 316 Register new_val = val; 317 if (needs_post_barrier) { 318 if (UseCompressedOops) { 319 // FIXME: Refactor the code to avoid usage of r19 and stay within tmpX 320 new_val = r19; 321 __ mov(new_val, val); 322 } 323 } 324 325 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg, noreg); 326 327 if (needs_post_barrier) { 328 g1_write_barrier_post(masm, 329 tmp1 /* store_adr */, 330 new_val /* new_val */, 331 rthread /* thread */, 332 tmp2 /* tmp */, 333 tmp3 /* tmp2 */); 334 } 335 } 336 337 } 338 339 #ifdef COMPILER1 340 341 #undef __ 342 #define __ ce->masm()-> 343 344 void G1BarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) { 345 G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 346 // At this point we know that marking is in progress. 347 // If do_load() is true then we have to emit the 348 // load of the previous value; otherwise it has already 349 // been loaded into _pre_val. 350 351 __ bind(*stub->entry()); 352 353 assert(stub->pre_val()->is_register(), "Precondition."); 354 355 Register pre_val_reg = stub->pre_val()->as_register(); 356 357 if (stub->do_load()) { 358 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/); 359 } 360 __ cbz(pre_val_reg, *stub->continuation()); 361 ce->store_parameter(stub->pre_val()->as_register(), 0); 362 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin())); 363 __ b(*stub->continuation()); 364 } 365 366 void G1BarrierSetAssembler::gen_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) { 367 G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 368 __ bind(*stub->entry()); 369 assert(stub->addr()->is_register(), "Precondition."); 370 assert(stub->new_val()->is_register(), "Precondition."); 371 Register new_val_reg = stub->new_val()->as_register(); 372 __ cbz(new_val_reg, *stub->continuation()); 373 ce->store_parameter(stub->addr()->as_pointer_register(), 0); 374 __ far_call(RuntimeAddress(bs->post_barrier_c1_runtime_code_blob()->code_begin())); 375 __ b(*stub->continuation()); 376 } 377 378 #undef __ 379 380 #define __ sasm-> 381 382 void G1BarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) { 383 __ prologue("g1_pre_barrier", false); 384 385 // arg0 : previous value of memory 386 387 BarrierSet* bs = BarrierSet::barrier_set(); 388 389 const Register pre_val = r0; 390 const Register thread = rthread; 391 const Register tmp = rscratch1; 392 393 Address in_progress(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset())); 394 Address queue_index(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_index_offset())); 395 Address buffer(thread, in_bytes(G1ThreadLocalData::satb_mark_queue_buffer_offset())); 396 397 Label done; 398 Label runtime; 399 400 // Is marking still active? 401 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 402 __ ldrw(tmp, in_progress); 403 } else { 404 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 405 __ ldrb(tmp, in_progress); 406 } 407 __ cbzw(tmp, done); 408 409 // Can we store original value in the thread's buffer? 410 __ ldr(tmp, queue_index); 411 __ cbz(tmp, runtime); 412 413 __ sub(tmp, tmp, wordSize); 414 __ str(tmp, queue_index); 415 __ ldr(rscratch2, buffer); 416 __ add(tmp, tmp, rscratch2); 417 __ load_parameter(0, rscratch2); 418 __ str(rscratch2, Address(tmp, 0)); 419 __ b(done); 420 421 __ bind(runtime); 422 __ push_call_clobbered_registers(); 423 __ load_parameter(0, pre_val); 424 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_pre_entry), pre_val, thread); 425 __ pop_call_clobbered_registers(); 426 __ bind(done); 427 428 __ epilogue(); 429 } 430 431 void G1BarrierSetAssembler::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) { 432 __ prologue("g1_post_barrier", false); 433 434 // arg0: store_address 435 Address store_addr(rfp, 2*BytesPerWord); 436 437 BarrierSet* bs = BarrierSet::barrier_set(); 438 CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs); 439 CardTable* ct = ctbs->card_table(); 440 441 Label done; 442 Label runtime; 443 444 // At this point we know new_value is non-NULL and the new_value crosses regions. 445 // Must check to see if card is already dirty 446 447 const Register thread = rthread; 448 449 Address queue_index(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_index_offset())); 450 Address buffer(thread, in_bytes(G1ThreadLocalData::dirty_card_queue_buffer_offset())); 451 452 const Register card_offset = rscratch2; 453 // LR is free here, so we can use it to hold the byte_map_base. 454 const Register byte_map_base = lr; 455 456 assert_different_registers(card_offset, byte_map_base, rscratch1); 457 458 __ load_parameter(0, card_offset); 459 __ lsr(card_offset, card_offset, CardTable::card_shift); 460 __ load_byte_map_base(byte_map_base); 461 __ ldrb(rscratch1, Address(byte_map_base, card_offset)); 462 __ cmpw(rscratch1, (int)G1CardTable::g1_young_card_val()); 463 __ br(Assembler::EQ, done); 464 465 assert((int)CardTable::dirty_card_val() == 0, "must be 0"); 466 467 __ membar(Assembler::StoreLoad); 468 __ ldrb(rscratch1, Address(byte_map_base, card_offset)); 469 __ cbzw(rscratch1, done); 470 471 // storing region crossing non-NULL, card is clean. 472 // dirty card and log. 473 __ strb(zr, Address(byte_map_base, card_offset)); 474 475 // Convert card offset into an address in card_addr 476 Register card_addr = card_offset; 477 __ add(card_addr, byte_map_base, card_addr); 478 479 __ ldr(rscratch1, queue_index); 480 __ cbz(rscratch1, runtime); 481 __ sub(rscratch1, rscratch1, wordSize); 482 __ str(rscratch1, queue_index); 483 484 // Reuse LR to hold buffer_addr 485 const Register buffer_addr = lr; 486 487 __ ldr(buffer_addr, buffer); 488 __ str(card_addr, Address(buffer_addr, rscratch1)); 489 __ b(done); 490 491 __ bind(runtime); 492 __ push_call_clobbered_registers(); 493 __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSetRuntime::write_ref_field_post_entry), card_addr, thread); 494 __ pop_call_clobbered_registers(); 495 __ bind(done); 496 __ epilogue(); 497 } 498 499 #undef __ 500 501 #endif // COMPILER1