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