1 /* 2 * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 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 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp" 26 #include "gc/shenandoah/shenandoahForwarding.hpp" 27 #include "gc/shenandoah/shenandoahHeap.hpp" 28 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 29 #include "gc/shenandoah/shenandoahHeuristics.hpp" 30 #include "gc/shenandoah/shenandoahRuntime.hpp" 31 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "interpreter/interp_masm.hpp" 34 #include "runtime/sharedRuntime.hpp" 35 #include "runtime/thread.hpp" 36 #ifdef COMPILER1 37 #include "c1/c1_LIRAssembler.hpp" 38 #include "c1/c1_MacroAssembler.hpp" 39 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp" 40 #endif 41 42 #define __ masm-> 43 44 address ShenandoahBarrierSetAssembler::_shenandoah_lrb = NULL; 45 46 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop, 47 Register src, Register dst, Register count, RegSet saved_regs) { 48 if (is_oop) { 49 bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0; 50 if (ShenandoahSATBBarrier && !dest_uninitialized && !ShenandoahHeap::heap()->heuristics()->can_do_traversal_gc()) { 51 52 Label done; 53 54 // Avoid calling runtime if count == 0 55 __ cbz(count, done); 56 57 // Is marking active? 58 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 59 __ ldrb(rscratch1, gc_state); 60 __ tbz(rscratch1, ShenandoahHeap::MARKING_BITPOS, done); 61 62 __ push(saved_regs, sp); 63 if (count == c_rarg0) { 64 if (dst == c_rarg1) { 65 // exactly backwards!! 66 __ mov(rscratch1, c_rarg0); 67 __ mov(c_rarg0, c_rarg1); 68 __ mov(c_rarg1, rscratch1); 69 } else { 70 __ mov(c_rarg1, count); 71 __ mov(c_rarg0, dst); 72 } 73 } else { 74 __ mov(c_rarg0, dst); 75 __ mov(c_rarg1, count); 76 } 77 if (UseCompressedOops) { 78 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2); 79 } else { 80 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2); 81 } 82 __ pop(saved_regs, sp); 83 __ bind(done); 84 } 85 } 86 } 87 88 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop, 89 Register start, Register count, Register scratch, RegSet saved_regs) { 90 if (is_oop) { 91 Label done; 92 93 // Avoid calling runtime if count == 0 94 __ cbz(count, done); 95 96 // Is updating references? 97 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 98 __ ldrb(rscratch1, gc_state); 99 __ tbz(rscratch1, ShenandoahHeap::UPDATEREFS_BITPOS, done); 100 101 __ push(saved_regs, sp); 102 assert_different_registers(start, count, scratch); 103 assert_different_registers(c_rarg0, count); 104 __ mov(c_rarg0, start); 105 __ mov(c_rarg1, count); 106 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2); 107 __ pop(saved_regs, sp); 108 109 __ bind(done); 110 } 111 } 112 113 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm, 114 Register obj, 115 Register pre_val, 116 Register thread, 117 Register tmp, 118 bool tosca_live, 119 bool expand_call) { 120 if (ShenandoahSATBBarrier) { 121 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call); 122 } 123 } 124 125 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm, 126 Register obj, 127 Register pre_val, 128 Register thread, 129 Register tmp, 130 bool tosca_live, 131 bool expand_call) { 132 // If expand_call is true then we expand the call_VM_leaf macro 133 // directly to skip generating the check by 134 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp. 135 136 assert(thread == rthread, "must be"); 137 138 Label done; 139 Label runtime; 140 141 assert_different_registers(obj, pre_val, tmp, rscratch1); 142 assert(pre_val != noreg && tmp != noreg, "expecting a register"); 143 144 Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset())); 145 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset())); 146 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())); 147 148 // Is marking active? 149 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 150 __ ldrw(tmp, in_progress); 151 } else { 152 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 153 __ ldrb(tmp, in_progress); 154 } 155 __ cbzw(tmp, done); 156 157 // Do we need to load the previous value? 158 if (obj != noreg) { 159 __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW); 160 } 161 162 // Is the previous value null? 163 __ cbz(pre_val, done); 164 165 // Can we store original value in the thread's buffer? 166 // Is index == 0? 167 // (The index field is typed as size_t.) 168 169 __ ldr(tmp, index); // tmp := *index_adr 170 __ cbz(tmp, runtime); // tmp == 0? 171 // If yes, goto runtime 172 173 __ sub(tmp, tmp, wordSize); // tmp := tmp - wordSize 174 __ str(tmp, index); // *index_adr := tmp 175 __ ldr(rscratch1, buffer); 176 __ add(tmp, tmp, rscratch1); // tmp := tmp + *buffer_adr 177 178 // Record the previous value 179 __ str(pre_val, Address(tmp, 0)); 180 __ b(done); 181 182 __ bind(runtime); 183 // save the live input values 184 RegSet saved = RegSet::of(pre_val); 185 if (tosca_live) saved += RegSet::of(r0); 186 if (obj != noreg) saved += RegSet::of(obj); 187 188 __ push(saved, sp); 189 190 // Calling the runtime using the regular call_VM_leaf mechanism generates 191 // code (generated by InterpreterMacroAssember::call_VM_leaf_base) 192 // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL. 193 // 194 // If we care generating the pre-barrier without a frame (e.g. in the 195 // intrinsified Reference.get() routine) then ebp might be pointing to 196 // the caller frame and so this check will most likely fail at runtime. 197 // 198 // Expanding the call directly bypasses the generation of the check. 199 // So when we do not have have a full interpreter frame on the stack 200 // expand_call should be passed true. 201 202 if (expand_call) { 203 assert(pre_val != c_rarg1, "smashed arg"); 204 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread); 205 } else { 206 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread); 207 } 208 209 __ pop(saved, sp); 210 211 __ bind(done); 212 } 213 214 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) { 215 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled"); 216 Label is_null; 217 __ cbz(dst, is_null); 218 resolve_forward_pointer_not_null(masm, dst, tmp); 219 __ bind(is_null); 220 } 221 222 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitely 223 // passed in. 224 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) { 225 assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled"); 226 // The below loads the mark word, checks if the lowest two bits are 227 // set, and if so, clear the lowest two bits and copy the result 228 // to dst. Otherwise it leaves dst alone. 229 // Implementing this is surprisingly awkward. I do it here by: 230 // - Inverting the mark word 231 // - Test lowest two bits == 0 232 // - If so, set the lowest two bits 233 // - Invert the result back, and copy to dst 234 235 bool borrow_reg = (tmp == noreg); 236 if (borrow_reg) { 237 // No free registers available. Make one useful. 238 tmp = rscratch1; 239 __ push(RegSet::of(tmp), sp); 240 } 241 242 Label done; 243 __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes())); 244 __ eon(tmp, tmp, zr); 245 __ ands(zr, tmp, markOopDesc::lock_mask_in_place); 246 __ br(Assembler::NE, done); 247 __ orr(tmp, tmp, markOopDesc::marked_value); 248 __ eon(dst, tmp, zr); 249 __ bind(done); 250 251 if (borrow_reg) { 252 __ pop(RegSet::of(tmp), sp); 253 } 254 } 255 256 void ShenandoahBarrierSetAssembler::load_reference_barrier_not_null(MacroAssembler* masm, Register dst, Register tmp) { 257 assert(ShenandoahLoadRefBarrier, "Should be enabled"); 258 assert(dst != rscratch2, "need rscratch2"); 259 260 Label done; 261 __ enter(); 262 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 263 __ ldrb(rscratch2, gc_state); 264 265 // Check for heap stability 266 __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, done); 267 268 RegSet to_save = RegSet::of(r0); 269 if (dst != r0) { 270 __ push(to_save, sp); 271 __ mov(r0, dst); 272 } 273 274 __ far_call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahBarrierSetAssembler::shenandoah_lrb()))); 275 276 if (dst != r0) { 277 __ mov(dst, r0); 278 __ pop(to_save, sp); 279 } 280 281 __ bind(done); 282 __ leave(); 283 } 284 285 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) { 286 if (ShenandoahStoreValEnqueueBarrier) { 287 // Save possibly live regs. 288 RegSet live_regs = RegSet::range(r0, r4) - dst; 289 __ push(live_regs, sp); 290 __ strd(v0, __ pre(sp, 2 * -wordSize)); 291 292 satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false); 293 294 // Restore possibly live regs. 295 __ ldrd(v0, __ post(sp, 2 * wordSize)); 296 __ pop(live_regs, sp); 297 } 298 } 299 300 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Register tmp) { 301 if (ShenandoahLoadRefBarrier) { 302 Label is_null; 303 __ cbz(dst, is_null); 304 load_reference_barrier_not_null(masm, dst, tmp); 305 __ bind(is_null); 306 } 307 } 308 309 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 310 Register dst, Address src, Register tmp1, Register tmp_thread) { 311 bool on_oop = type == T_OBJECT || type == T_ARRAY; 312 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0; 313 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0; 314 bool on_reference = on_weak || on_phantom; 315 316 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread); 317 if (on_oop) { 318 load_reference_barrier(masm, dst, tmp1); 319 320 if (ShenandoahKeepAliveBarrier && on_reference) { 321 __ enter(); 322 satb_write_barrier_pre(masm /* masm */, 323 noreg /* obj */, 324 dst /* pre_val */, 325 rthread /* thread */, 326 tmp1 /* tmp */, 327 true /* tosca_live */, 328 true /* expand_call */); 329 __ leave(); 330 } 331 } 332 } 333 334 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 335 Address dst, Register val, Register tmp1, Register tmp2) { 336 bool on_oop = type == T_OBJECT || type == T_ARRAY; 337 if (!on_oop) { 338 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2); 339 return; 340 } 341 342 // flatten object address if needed 343 if (dst.index() == noreg && dst.offset() == 0) { 344 if (dst.base() != r3) { 345 __ mov(r3, dst.base()); 346 } 347 } else { 348 __ lea(r3, dst); 349 } 350 351 shenandoah_write_barrier_pre(masm, 352 r3 /* obj */, 353 tmp2 /* pre_val */, 354 rthread /* thread */, 355 tmp1 /* tmp */, 356 val != noreg /* tosca_live */, 357 false /* expand_call */); 358 359 if (val == noreg) { 360 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg); 361 } else { 362 storeval_barrier(masm, val, tmp1); 363 // G1 barrier needs uncompressed oop for region cross check. 364 Register new_val = val; 365 if (UseCompressedOops) { 366 new_val = rscratch2; 367 __ mov(new_val, val); 368 } 369 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg); 370 } 371 372 } 373 374 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val, 375 bool acquire, bool release, bool weak, bool is_cae, 376 Register result) { 377 Register tmp1 = rscratch1; 378 Register tmp2 = rscratch2; 379 bool is_narrow = UseCompressedOops; 380 Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword; 381 382 assert_different_registers(addr, expected, new_val, tmp1, tmp2); 383 384 Label retry, done, fail; 385 386 // CAS, using LL/SC pair. 387 __ bind(retry); 388 __ load_exclusive(tmp1, addr, size, acquire); 389 if (is_narrow) { 390 __ cmpw(tmp1, expected); 391 } else { 392 __ cmp(tmp1, expected); 393 } 394 __ br(Assembler::NE, fail); 395 __ store_exclusive(tmp2, new_val, addr, size, release); 396 if (weak) { 397 __ cmpw(tmp2, 0u); // If the store fails, return NE to our caller 398 } else { 399 __ cbnzw(tmp2, retry); 400 } 401 __ b(done); 402 403 __ bind(fail); 404 // Check if rb(expected)==rb(tmp1) 405 // Shuffle registers so that we have memory value ready for next expected. 406 __ mov(tmp2, expected); 407 __ mov(expected, tmp1); 408 if (is_narrow) { 409 __ decode_heap_oop(tmp1, tmp1); 410 __ decode_heap_oop(tmp2, tmp2); 411 } 412 resolve_forward_pointer(masm, tmp1); 413 resolve_forward_pointer(masm, tmp2); 414 __ cmp(tmp1, tmp2); 415 // Retry with expected now being the value we just loaded from addr. 416 __ br(Assembler::EQ, retry); 417 if (is_cae && is_narrow) { 418 // For cmp-and-exchange and narrow oops, we need to restore 419 // the compressed old-value. We moved it to 'expected' a few lines up. 420 __ mov(tmp1, expected); 421 } 422 __ bind(done); 423 424 if (is_cae) { 425 __ mov(result, tmp1); 426 } else { 427 __ cset(result, Assembler::EQ); 428 } 429 } 430 431 #undef __ 432 433 #ifdef COMPILER1 434 435 #define __ ce->masm()-> 436 437 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) { 438 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 439 // At this point we know that marking is in progress. 440 // If do_load() is true then we have to emit the 441 // load of the previous value; otherwise it has already 442 // been loaded into _pre_val. 443 444 __ bind(*stub->entry()); 445 446 assert(stub->pre_val()->is_register(), "Precondition."); 447 448 Register pre_val_reg = stub->pre_val()->as_register(); 449 450 if (stub->do_load()) { 451 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/); 452 } 453 __ cbz(pre_val_reg, *stub->continuation()); 454 ce->store_parameter(stub->pre_val()->as_register(), 0); 455 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin())); 456 __ b(*stub->continuation()); 457 } 458 459 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) { 460 461 Register obj = stub->obj()->as_register(); 462 Register res = stub->result()->as_register(); 463 464 Label done; 465 466 __ bind(*stub->entry()); 467 468 if (res != obj) { 469 __ mov(res, obj); 470 } 471 // Check for null. 472 if (stub->needs_null_check()) { 473 __ cbz(res, done); 474 } 475 476 load_reference_barrier_not_null(ce->masm(), res, rscratch1); 477 478 __ bind(done); 479 __ b(*stub->continuation()); 480 } 481 482 #undef __ 483 484 #define __ sasm-> 485 486 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) { 487 __ prologue("shenandoah_pre_barrier", false); 488 489 // arg0 : previous value of memory 490 491 BarrierSet* bs = BarrierSet::barrier_set(); 492 493 const Register pre_val = r0; 494 const Register thread = rthread; 495 const Register tmp = rscratch1; 496 497 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset())); 498 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())); 499 500 Label done; 501 Label runtime; 502 503 // Is marking still active? 504 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 505 __ ldrb(tmp, gc_state); 506 __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL); 507 __ tst(tmp, rscratch2); 508 __ br(Assembler::EQ, done); 509 510 // Can we store original value in the thread's buffer? 511 __ ldr(tmp, queue_index); 512 __ cbz(tmp, runtime); 513 514 __ sub(tmp, tmp, wordSize); 515 __ str(tmp, queue_index); 516 __ ldr(rscratch2, buffer); 517 __ add(tmp, tmp, rscratch2); 518 __ load_parameter(0, rscratch2); 519 __ str(rscratch2, Address(tmp, 0)); 520 __ b(done); 521 522 __ bind(runtime); 523 __ push_call_clobbered_registers(); 524 __ load_parameter(0, pre_val); 525 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread); 526 __ pop_call_clobbered_registers(); 527 __ bind(done); 528 529 __ epilogue(); 530 } 531 532 #undef __ 533 534 #endif // COMPILER1 535 536 address ShenandoahBarrierSetAssembler::shenandoah_lrb() { 537 assert(_shenandoah_lrb != NULL, "need load reference barrier stub"); 538 return _shenandoah_lrb; 539 } 540 541 #define __ cgen->assembler()-> 542 543 // Shenandoah load reference barrier. 544 // 545 // Input: 546 // r0: OOP to evacuate. Not null. 547 // 548 // Output: 549 // r0: Pointer to evacuated OOP. 550 // 551 // Trash rscratch1, rscratch2. Preserve everything else. 552 address ShenandoahBarrierSetAssembler::generate_shenandoah_lrb(StubCodeGenerator* cgen) { 553 554 __ align(6); 555 StubCodeMark mark(cgen, "StubRoutines", "shenandoah_lrb"); 556 address start = __ pc(); 557 558 Label work, done; 559 __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr()); 560 __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 561 __ ldrb(rscratch2, Address(rscratch2, rscratch1)); 562 __ tbnz(rscratch2, 0, work); 563 __ ret(lr); 564 __ bind(work); 565 566 __ mov(rscratch2, r0); 567 resolve_forward_pointer_not_null(cgen->assembler(), r0, rscratch1); 568 __ cmp(rscratch2, r0); 569 __ br(Assembler::NE, done); 570 571 __ enter(); // required for proper stackwalking of RuntimeStub frame 572 573 __ push_call_clobbered_registers(); 574 575 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_JRT)); 576 __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral); 577 __ mov(rscratch1, r0); 578 __ pop_call_clobbered_registers(); 579 __ mov(r0, rscratch1); 580 581 __ leave(); // required for proper stackwalking of RuntimeStub frame 582 __ bind(done); 583 __ ret(lr); 584 585 return start; 586 } 587 588 #undef __ 589 590 void ShenandoahBarrierSetAssembler::barrier_stubs_init() { 591 if (ShenandoahLoadRefBarrier) { 592 int stub_code_size = 2048; 593 ResourceMark rm; 594 BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size); 595 CodeBuffer buf(bb); 596 StubCodeGenerator cgen(&buf); 597 _shenandoah_lrb = generate_shenandoah_lrb(&cgen); 598 } 599 }