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