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/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 addr, 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 (addr == 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, addr); 72 } 73 } else { 74 __ mov(c_rarg0, addr); 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 Label done; 235 __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes())); 236 __ eon(tmp, tmp, zr); 237 __ ands(zr, tmp, markOopDesc::lock_mask_in_place); 238 __ br(Assembler::NE, done); 239 __ orr(tmp, tmp, markOopDesc::marked_value); 240 __ eon(dst, tmp, zr); 241 __ bind(done); 242 } 243 244 void ShenandoahBarrierSetAssembler::load_reference_barrier_not_null(MacroAssembler* masm, Register dst, Register tmp) { 245 assert(ShenandoahLoadRefBarrier, "Should be enabled"); 246 assert(dst != rscratch2, "need rscratch2"); 247 248 Label done; 249 __ enter(); 250 Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 251 __ ldrb(rscratch2, gc_state); 252 253 // Check for heap stability 254 __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, done); 255 256 RegSet to_save = RegSet::of(r0); 257 if (dst != r0) { 258 __ push(to_save, sp); 259 __ mov(r0, dst); 260 } 261 262 __ far_call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahBarrierSetAssembler::shenandoah_lrb()))); 263 264 if (dst != r0) { 265 __ mov(dst, r0); 266 __ pop(to_save, sp); 267 } 268 269 __ bind(done); 270 __ leave(); 271 } 272 273 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) { 274 if (ShenandoahStoreValEnqueueBarrier) { 275 // Save possibly live regs. 276 RegSet live_regs = RegSet::range(r0, r4) - dst; 277 __ push(live_regs, sp); 278 __ strd(v0, __ pre(sp, 2 * -wordSize)); 279 280 satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false); 281 282 // Restore possibly live regs. 283 __ ldrd(v0, __ post(sp, 2 * wordSize)); 284 __ pop(live_regs, sp); 285 } 286 } 287 288 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Register tmp) { 289 if (ShenandoahLoadRefBarrier) { 290 Label is_null; 291 __ cbz(dst, is_null); 292 load_reference_barrier_not_null(masm, dst, tmp); 293 __ bind(is_null); 294 } 295 } 296 297 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 298 Register dst, Address src, Register tmp1, Register tmp_thread) { 299 bool on_oop = type == T_OBJECT || type == T_ARRAY; 300 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0; 301 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0; 302 bool on_reference = on_weak || on_phantom; 303 304 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread); 305 if (on_oop) { 306 load_reference_barrier(masm, dst, tmp1); 307 308 if (ShenandoahKeepAliveBarrier && on_reference) { 309 __ enter(); 310 satb_write_barrier_pre(masm /* masm */, 311 noreg /* obj */, 312 dst /* pre_val */, 313 rthread /* thread */, 314 tmp1 /* tmp */, 315 true /* tosca_live */, 316 true /* expand_call */); 317 __ leave(); 318 } 319 } 320 } 321 322 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 323 Address dst, Register val, Register tmp1, Register tmp2) { 324 bool on_oop = type == T_OBJECT || type == T_ARRAY; 325 if (!on_oop) { 326 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2); 327 return; 328 } 329 330 // flatten object address if needed 331 if (dst.index() == noreg && dst.offset() == 0) { 332 if (dst.base() != r3) { 333 __ mov(r3, dst.base()); 334 } 335 } else { 336 __ lea(r3, dst); 337 } 338 339 shenandoah_write_barrier_pre(masm, 340 r3 /* obj */, 341 tmp2 /* pre_val */, 342 rthread /* thread */, 343 tmp1 /* tmp */, 344 val != noreg /* tosca_live */, 345 false /* expand_call */); 346 347 if (val == noreg) { 348 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg); 349 } else { 350 storeval_barrier(masm, val, tmp1); 351 // G1 barrier needs uncompressed oop for region cross check. 352 Register new_val = val; 353 if (UseCompressedOops) { 354 new_val = rscratch2; 355 __ mov(new_val, val); 356 } 357 BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg); 358 } 359 360 } 361 362 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val, 363 bool acquire, bool release, bool weak, bool is_cae, 364 Register tmp, Register result) { 365 Register tmp1 = rscratch1; 366 Register tmp2 = rscratch2; 367 bool is_narrow = UseCompressedOops; 368 Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword; 369 370 assert_different_registers(addr, expected, new_val, tmp1, tmp2); 371 372 Label retry, done, fail; 373 374 // CAS, using LL/SC pair. 375 __ bind(retry); 376 __ load_exclusive(tmp1, addr, size, acquire); 377 if (is_narrow) { 378 __ cmpw(tmp1, expected); 379 } else { 380 __ cmp(tmp1, expected); 381 } 382 __ br(Assembler::NE, fail); 383 __ store_exclusive(tmp2, new_val, addr, size, release); 384 if (weak) { 385 __ cmpw(tmp2, 0u); // If the store fails, return NE to our caller 386 } else { 387 __ cbnzw(tmp2, retry); 388 } 389 __ b(done); 390 391 __ bind(fail); 392 // Check if rb(expected)==rb(tmp1) 393 // Shuffle registers so that we have memory value ready for next expected. 394 __ mov(tmp2, expected); 395 __ mov(expected, tmp1); 396 if (is_narrow) { 397 __ decode_heap_oop(tmp1, tmp1); 398 __ decode_heap_oop(tmp2, tmp2); 399 } 400 resolve_forward_pointer(masm, tmp1, tmp); 401 resolve_forward_pointer(masm, tmp2, tmp); 402 __ cmp(tmp1, tmp2); 403 // Retry with expected now being the value we just loaded from addr. 404 __ br(Assembler::EQ, retry); 405 if (is_cae && is_narrow) { 406 // For cmp-and-exchange and narrow oops, we need to restore 407 // the compressed old-value. We moved it to 'expected' a few lines up. 408 __ mov(tmp1, expected); 409 } 410 __ bind(done); 411 412 if (is_cae) { 413 __ mov(result, tmp1); 414 } else { 415 __ cset(result, Assembler::EQ); 416 } 417 } 418 419 #ifdef COMPILER1 420 421 #undef __ 422 #define __ ce->masm()-> 423 424 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) { 425 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 426 // At this point we know that marking is in progress. 427 // If do_load() is true then we have to emit the 428 // load of the previous value; otherwise it has already 429 // been loaded into _pre_val. 430 431 __ bind(*stub->entry()); 432 433 assert(stub->pre_val()->is_register(), "Precondition."); 434 435 Register pre_val_reg = stub->pre_val()->as_register(); 436 437 if (stub->do_load()) { 438 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/); 439 } 440 __ cbz(pre_val_reg, *stub->continuation()); 441 ce->store_parameter(stub->pre_val()->as_register(), 0); 442 __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin())); 443 __ b(*stub->continuation()); 444 } 445 446 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) { 447 448 Register obj = stub->obj()->as_register(); 449 Register res = stub->result()->as_register(); 450 451 Label done; 452 453 __ bind(*stub->entry()); 454 455 if (res != obj) { 456 __ mov(res, obj); 457 } 458 // Check for null. 459 if (stub->needs_null_check()) { 460 __ cbz(res, done); 461 } 462 463 load_reference_barrier_not_null(ce->masm(), res, rscratch1); 464 465 __ bind(done); 466 __ b(*stub->continuation()); 467 } 468 469 #undef __ 470 471 #define __ sasm-> 472 473 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) { 474 __ prologue("shenandoah_pre_barrier", false); 475 476 // arg0 : previous value of memory 477 478 BarrierSet* bs = BarrierSet::barrier_set(); 479 480 const Register pre_val = r0; 481 const Register thread = rthread; 482 const Register tmp = rscratch1; 483 484 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset())); 485 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())); 486 487 Label done; 488 Label runtime; 489 490 // Is marking still active? 491 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 492 __ ldrb(tmp, gc_state); 493 __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL); 494 __ tst(tmp, rscratch2); 495 __ br(Assembler::EQ, done); 496 497 // Can we store original value in the thread's buffer? 498 __ ldr(tmp, queue_index); 499 __ cbz(tmp, runtime); 500 501 __ sub(tmp, tmp, wordSize); 502 __ str(tmp, queue_index); 503 __ ldr(rscratch2, buffer); 504 __ add(tmp, tmp, rscratch2); 505 __ load_parameter(0, rscratch2); 506 __ str(rscratch2, Address(tmp, 0)); 507 __ b(done); 508 509 __ bind(runtime); 510 __ push_call_clobbered_registers(); 511 __ load_parameter(0, pre_val); 512 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread); 513 __ pop_call_clobbered_registers(); 514 __ bind(done); 515 516 __ epilogue(); 517 } 518 519 #undef __ 520 521 #endif // COMPILER1 522 523 address ShenandoahBarrierSetAssembler::shenandoah_lrb() { 524 assert(_shenandoah_lrb != NULL, "need load reference barrier stub"); 525 return _shenandoah_lrb; 526 } 527 528 #define __ cgen->assembler()-> 529 530 // Shenandoah load reference barrier. 531 // 532 // Input: 533 // r0: OOP to evacuate. Not null. 534 // 535 // Output: 536 // r0: Pointer to evacuated OOP. 537 // 538 // Trash rscratch1, rscratch2. Preserve everything else. 539 address ShenandoahBarrierSetAssembler::generate_shenandoah_lrb(StubCodeGenerator* cgen) { 540 541 __ align(6); 542 StubCodeMark mark(cgen, "StubRoutines", "shenandoah_lrb"); 543 address start = __ pc(); 544 545 Label work, done; 546 __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr()); 547 __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 548 __ ldrb(rscratch2, Address(rscratch2, rscratch1)); 549 __ tbnz(rscratch2, 0, work); 550 __ ret(lr); 551 __ bind(work); 552 553 __ mov(rscratch2, r0); 554 resolve_forward_pointer_not_null(cgen->assembler(), r0, rscratch1); 555 __ cmp(rscratch2, r0); 556 __ br(Assembler::NE, done); 557 558 __ enter(); // required for proper stackwalking of RuntimeStub frame 559 560 __ push_call_clobbered_registers(); 561 562 __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_JRT)); 563 __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral); 564 __ mov(rscratch1, r0); 565 __ pop_call_clobbered_registers(); 566 __ mov(r0, rscratch1); 567 568 __ leave(); // required for proper stackwalking of RuntimeStub frame 569 __ bind(done); 570 __ ret(lr); 571 572 return start; 573 } 574 575 #undef __ 576 577 void ShenandoahBarrierSetAssembler::barrier_stubs_init() { 578 if (ShenandoahLoadRefBarrier) { 579 int stub_code_size = 2048; 580 ResourceMark rm; 581 BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size); 582 CodeBuffer buf(bb); 583 StubCodeGenerator cgen(&buf); 584 _shenandoah_lrb = generate_shenandoah_lrb(&cgen); 585 } 586 }