1 /* 2 * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates. 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/brooksPointer.hpp" 26 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp" 27 #include "gc/shenandoah/shenandoahConnectionMatrix.hpp" 28 #include "gc/shenandoah/shenandoahHeap.hpp" 29 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 30 #include "gc/shenandoah/shenandoahHeuristics.hpp" 31 #include "gc/shenandoah/shenandoahRuntime.hpp" 32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 33 #include "interpreter/interpreter.hpp" 34 #include "interpreter/interp_masm.hpp" 35 #include "runtime/sharedRuntime.hpp" 36 #include "runtime/thread.hpp" 37 #include "utilities/macros.hpp" 38 #ifdef COMPILER1 39 #include "c1/c1_LIRAssembler.hpp" 40 #include "c1/c1_MacroAssembler.hpp" 41 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp" 42 #endif 43 44 #define __ masm-> 45 46 address ShenandoahBarrierSetAssembler::_shenandoah_wb = NULL; 47 address ShenandoahBarrierSetAssembler::_shenandoah_wb_C = NULL; 48 49 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 50 Register src, Register dst, Register count) { 51 52 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0; 53 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0; 54 bool obj_int = type == T_OBJECT LP64_ONLY(&& UseCompressedOops); 55 bool dest_uninitialized = (decorators & AS_DEST_NOT_INITIALIZED) != 0; 56 57 if (type == T_OBJECT || type == T_ARRAY) { 58 #ifdef _LP64 59 if (!checkcast && !obj_int) { 60 // Save count for barrier 61 __ movptr(r11, count); 62 } else if (disjoint && obj_int) { 63 // Save dst in r11 in the disjoint case 64 __ movq(r11, dst); 65 } 66 #else 67 if (disjoint) { 68 __ mov(rdx, dst); // save 'to' 69 } 70 #endif 71 72 if (!dest_uninitialized && !ShenandoahHeap::heap()->heuristics()->can_do_traversal_gc()) { 73 Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread); 74 #ifndef _LP64 75 __ push(thread); 76 __ get_thread(thread); 77 #endif 78 79 Label filtered; 80 Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset())); 81 // Is marking active? 82 if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) { 83 __ cmpl(in_progress, 0); 84 } else { 85 assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption"); 86 __ cmpb(in_progress, 0); 87 } 88 89 NOT_LP64(__ pop(thread);) 90 91 __ jcc(Assembler::equal, filtered); 92 93 __ pusha(); // push registers 94 #ifdef _LP64 95 if (count == c_rarg0) { 96 if (dst == c_rarg1) { 97 // exactly backwards!! 98 __ xchgptr(c_rarg1, c_rarg0); 99 } else { 100 __ movptr(c_rarg1, count); 101 __ movptr(c_rarg0, dst); 102 } 103 } else { 104 __ movptr(c_rarg0, dst); 105 __ movptr(c_rarg1, count); 106 } 107 if (UseCompressedOops) { 108 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2); 109 } else { 110 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2); 111 } 112 #else 113 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 114 dst, count); 115 #endif 116 __ popa(); 117 __ bind(filtered); 118 } 119 } 120 121 } 122 123 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 124 Register src, Register dst, Register count) { 125 bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0; 126 bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0; 127 bool obj_int = type == T_OBJECT LP64_ONLY(&& UseCompressedOops); 128 Register tmp = rax; 129 130 if (type == T_OBJECT || type == T_ARRAY) { 131 #ifdef _LP64 132 if (!checkcast && !obj_int) { 133 // Save count for barrier 134 count = r11; 135 } else if (disjoint && obj_int) { 136 // Use the saved dst in the disjoint case 137 dst = r11; 138 } else if (checkcast) { 139 tmp = rscratch1; 140 } 141 #else 142 if (disjoint) { 143 __ mov(dst, rdx); // restore 'to' 144 } 145 #endif 146 147 __ pusha(); // push registers (overkill) 148 #ifdef _LP64 149 if (c_rarg0 == count) { // On win64 c_rarg0 == rcx 150 assert_different_registers(c_rarg1, dst); 151 __ mov(c_rarg1, count); 152 __ mov(c_rarg0, dst); 153 } else { 154 assert_different_registers(c_rarg0, count); 155 __ mov(c_rarg0, dst); 156 __ mov(c_rarg1, count); 157 } 158 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2); 159 #else 160 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 161 dst, count); 162 #endif 163 __ popa(); 164 } 165 } 166 167 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm, 168 Register obj, 169 Register pre_val, 170 Register thread, 171 Register tmp, 172 bool tosca_live, 173 bool expand_call) { 174 175 if (ShenandoahSATBBarrier) { 176 satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call); 177 } 178 } 179 180 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm, 181 Register obj, 182 Register pre_val, 183 Register thread, 184 Register tmp, 185 bool tosca_live, 186 bool expand_call) { 187 // If expand_call is true then we expand the call_VM_leaf macro 188 // directly to skip generating the check by 189 // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp. 190 191 #ifdef _LP64 192 assert(thread == r15_thread, "must be"); 193 #endif // _LP64 194 195 Label done; 196 Label runtime; 197 198 assert(pre_val != noreg, "check this code"); 199 200 if (obj != noreg) { 201 assert_different_registers(obj, pre_val, tmp); 202 assert(pre_val != rax, "check this code"); 203 } 204 205 Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset())); 206 Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset())); 207 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())); 208 209 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 210 __ testb(gc_state, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL); 211 __ jcc(Assembler::zero, done); 212 213 // Do we need to load the previous value? 214 if (obj != noreg) { 215 __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW); 216 } 217 218 // Is the previous value null? 219 __ cmpptr(pre_val, (int32_t) NULL_WORD); 220 __ jcc(Assembler::equal, done); 221 222 // Can we store original value in the thread's buffer? 223 // Is index == 0? 224 // (The index field is typed as size_t.) 225 226 __ movptr(tmp, index); // tmp := *index_adr 227 __ cmpptr(tmp, 0); // tmp == 0? 228 __ jcc(Assembler::equal, runtime); // If yes, goto runtime 229 230 __ subptr(tmp, wordSize); // tmp := tmp - wordSize 231 __ movptr(index, tmp); // *index_adr := tmp 232 __ addptr(tmp, buffer); // tmp := tmp + *buffer_adr 233 234 // Record the previous value 235 __ movptr(Address(tmp, 0), pre_val); 236 __ jmp(done); 237 238 __ bind(runtime); 239 // save the live input values 240 if(tosca_live) __ push(rax); 241 242 if (obj != noreg && obj != rax) 243 __ push(obj); 244 245 if (pre_val != rax) 246 __ push(pre_val); 247 248 // Calling the runtime using the regular call_VM_leaf mechanism generates 249 // code (generated by InterpreterMacroAssember::call_VM_leaf_base) 250 // that checks that the *(ebp+frame::interpreter_frame_last_sp) == NULL. 251 // 252 // If we care generating the pre-barrier without a frame (e.g. in the 253 // intrinsified Reference.get() routine) then ebp might be pointing to 254 // the caller frame and so this check will most likely fail at runtime. 255 // 256 // Expanding the call directly bypasses the generation of the check. 257 // So when we do not have have a full interpreter frame on the stack 258 // expand_call should be passed true. 259 260 NOT_LP64( __ push(thread); ) 261 262 if (expand_call) { 263 LP64_ONLY( assert(pre_val != c_rarg1, "smashed arg"); ) 264 #ifdef _LP64 265 if (c_rarg1 != thread) { 266 __ mov(c_rarg1, thread); 267 } 268 if (c_rarg0 != pre_val) { 269 __ mov(c_rarg0, pre_val); 270 } 271 #else 272 __ push(thread); 273 __ push(pre_val); 274 #endif 275 __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), 2); 276 } else { 277 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread); 278 } 279 280 NOT_LP64( __ pop(thread); ) 281 282 // save the live input values 283 if (pre_val != rax) 284 __ pop(pre_val); 285 286 if (obj != noreg && obj != rax) 287 __ pop(obj); 288 289 if(tosca_live) __ pop(rax); 290 291 __ bind(done); 292 } 293 294 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_post(MacroAssembler* masm, 295 Register store_addr, 296 Register new_val, 297 Register thread, 298 Register tmp, 299 Register tmp2) { 300 assert(UseShenandoahGC, "why else should we be here?"); 301 302 if (! UseShenandoahMatrix) { 303 // No need for that barrier if not using matrix. 304 return; 305 } 306 307 Label done; 308 __ testptr(new_val, new_val); 309 __ jcc(Assembler::zero, done); 310 ShenandoahConnectionMatrix* matrix = ShenandoahHeap::heap()->connection_matrix(); 311 address matrix_addr = matrix->matrix_addr(); 312 __ movptr(rscratch1, (intptr_t) ShenandoahHeap::heap()->base()); 313 // Compute to-region index 314 __ movptr(tmp, new_val); 315 __ subptr(tmp, rscratch1); 316 __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 317 // Compute from-region index 318 __ movptr(tmp2, store_addr); 319 __ subptr(tmp2, rscratch1); 320 __ shrptr(tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 321 // Compute matrix index 322 __ imulptr(tmp, tmp, matrix->stride_jint()); 323 __ addptr(tmp, tmp2); 324 // Address is _matrix[to * stride + from] 325 __ movptr(rscratch1, (intptr_t) matrix_addr); 326 // Test if the element is already set. 327 __ cmpb(Address(rscratch1, tmp, Address::times_1), 0); 328 __ jcc(Assembler::notEqual, done); 329 // Store true, if not yet set. 330 __ movb(Address(rscratch1, tmp, Address::times_1), 1); 331 __ bind(done); 332 } 333 334 void ShenandoahBarrierSetAssembler::read_barrier(MacroAssembler* masm, Register dst) { 335 if (ShenandoahReadBarrier) { 336 read_barrier_impl(masm, dst); 337 } 338 } 339 340 void ShenandoahBarrierSetAssembler::read_barrier_impl(MacroAssembler* masm, Register dst) { 341 assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled"); 342 Label is_null; 343 __ testptr(dst, dst); 344 __ jcc(Assembler::zero, is_null); 345 read_barrier_not_null_impl(masm, dst); 346 __ bind(is_null); 347 } 348 349 void ShenandoahBarrierSetAssembler::read_barrier_not_null(MacroAssembler* masm, Register dst) { 350 if (ShenandoahReadBarrier) { 351 read_barrier_not_null_impl(masm, dst); 352 } 353 } 354 355 void ShenandoahBarrierSetAssembler::read_barrier_not_null_impl(MacroAssembler* masm, Register dst) { 356 assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled"); 357 __ movptr(dst, Address(dst, BrooksPointer::byte_offset())); 358 } 359 360 361 void ShenandoahBarrierSetAssembler::write_barrier(MacroAssembler* masm, Register dst) { 362 if (ShenandoahWriteBarrier) { 363 write_barrier_impl(masm, dst); 364 } 365 } 366 367 void ShenandoahBarrierSetAssembler::write_barrier_impl(MacroAssembler* masm, Register dst) { 368 assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier), "should be enabled"); 369 #ifdef _LP64 370 assert(dst != rscratch1, "different regs"); 371 372 Label done; 373 374 Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 375 __ testb(gc_state, ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL); 376 377 // Now check if evacuation is in progress. 378 read_barrier_not_null(masm, dst); 379 380 __ jcc(Assembler::zero, done); 381 __ push(rscratch1); 382 __ push(rscratch2); 383 384 __ movptr(rscratch1, dst); 385 __ shrptr(rscratch1, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 386 __ movptr(rscratch2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr()); 387 __ movbool(rscratch2, Address(rscratch2, rscratch1, Address::times_1)); 388 __ testb(rscratch2, 0x1); 389 390 __ pop(rscratch2); 391 __ pop(rscratch1); 392 393 __ jcc(Assembler::zero, done); 394 395 __ push(rscratch1); 396 397 // Save possibly live regs. 398 if (dst != rax) { 399 __ push(rax); 400 } 401 if (dst != rbx) { 402 __ push(rbx); 403 } 404 if (dst != rcx) { 405 __ push(rcx); 406 } 407 if (dst != rdx) { 408 __ push(rdx); 409 } 410 if (dst != c_rarg1) { 411 __ push(c_rarg1); 412 } 413 414 __ subptr(rsp, 2 * Interpreter::stackElementSize); 415 __ movdbl(Address(rsp, 0), xmm0); 416 417 // Call into runtime 418 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_IRT), dst); 419 __ mov(rscratch1, rax); 420 421 // Restore possibly live regs. 422 __ movdbl(xmm0, Address(rsp, 0)); 423 __ addptr(rsp, 2 * Interpreter::stackElementSize); 424 425 if (dst != c_rarg1) { 426 __ pop(c_rarg1); 427 } 428 if (dst != rdx) { 429 __ pop(rdx); 430 } 431 if (dst != rcx) { 432 __ pop(rcx); 433 } 434 if (dst != rbx) { 435 __ pop(rbx); 436 } 437 if (dst != rax) { 438 __ pop(rax); 439 } 440 441 // Move result into dst reg. 442 __ mov(dst, rscratch1); 443 444 __ pop(rscratch1); 445 446 __ bind(done); 447 #else 448 Unimplemented(); 449 #endif 450 } 451 452 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) { 453 if (ShenandoahStoreValReadBarrier || ShenandoahStoreValEnqueueBarrier) { 454 storeval_barrier_impl(masm, dst, tmp); 455 } 456 } 457 458 void ShenandoahBarrierSetAssembler::storeval_barrier_impl(MacroAssembler* masm, Register dst, Register tmp) { 459 assert(UseShenandoahGC && (ShenandoahStoreValReadBarrier || ShenandoahStoreValEnqueueBarrier), "should be enabled"); 460 461 if (dst == noreg) return; 462 463 #ifdef _LP64 464 if (ShenandoahStoreValEnqueueBarrier) { 465 Label is_null; 466 __ testptr(dst, dst); 467 __ jcc(Assembler::zero, is_null); 468 write_barrier_impl(masm, dst); 469 __ bind(is_null); 470 471 // The set of registers to be saved+restored is the same as in the write-barrier above. 472 // Those are the commonly used registers in the interpreter. 473 __ pusha(); 474 // __ push_callee_saved_registers(); 475 __ subptr(rsp, 2 * Interpreter::stackElementSize); 476 __ movdbl(Address(rsp, 0), xmm0); 477 478 satb_write_barrier_pre(masm, noreg, dst, r15_thread, tmp, true, false); 479 __ movdbl(xmm0, Address(rsp, 0)); 480 __ addptr(rsp, 2 * Interpreter::stackElementSize); 481 //__ pop_callee_saved_registers(); 482 __ popa(); 483 } 484 if (ShenandoahStoreValReadBarrier) { 485 read_barrier_impl(masm, dst); 486 } 487 #else 488 Unimplemented(); 489 #endif 490 } 491 492 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 493 Register dst, Address src, Register tmp1, Register tmp_thread) { 494 bool on_oop = type == T_OBJECT || type == T_ARRAY; 495 bool in_heap = (decorators & IN_HEAP) != 0; 496 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0; 497 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0; 498 bool on_reference = on_weak || on_phantom; 499 if (in_heap) { 500 read_barrier_not_null(masm, src.base()); 501 } 502 BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread); 503 if (ShenandoahKeepAliveBarrier && on_oop && on_reference) { 504 const Register thread = NOT_LP64(tmp_thread) LP64_ONLY(r15_thread); 505 NOT_LP64(__ get_thread(thread)); 506 507 // Generate the SATB pre-barrier code to log the value of 508 // the referent field in an SATB buffer. 509 shenandoah_write_barrier_pre(masm /* masm */, 510 noreg /* obj */, 511 dst /* pre_val */, 512 thread /* thread */, 513 tmp1 /* tmp */, 514 true /* tosca_live */, 515 true /* expand_call */); 516 } 517 } 518 519 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type, 520 Address dst, Register val, Register tmp1, Register tmp2) { 521 522 bool in_heap = (decorators & IN_HEAP) != 0; 523 bool in_concurrent_root = (decorators & IN_CONCURRENT_ROOT) != 0; 524 if (in_heap) { 525 write_barrier(masm, dst.base()); 526 } 527 if (type == T_OBJECT || type == T_ARRAY) { 528 bool needs_pre_barrier = in_heap || in_concurrent_root; 529 bool needs_post_barrier = val != noreg && in_heap && UseShenandoahMatrix; 530 531 Register tmp3 = LP64_ONLY(r8) NOT_LP64(rsi); 532 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 533 // flatten object address if needed 534 // We do it regardless of precise because we need the registers 535 if (dst.index() == noreg && dst.disp() == 0) { 536 if (dst.base() != tmp1) { 537 __ movptr(tmp1, dst.base()); 538 } 539 } else { 540 __ lea(tmp1, dst); 541 } 542 543 #ifndef _LP64 544 InterpreterMacroAssembler *imasm = static_cast<InterpreterMacroAssembler*>(masm); 545 #endif 546 547 NOT_LP64(__ get_thread(rcx)); 548 NOT_LP64(imasm->save_bcp()); 549 550 if (needs_pre_barrier) { 551 shenandoah_write_barrier_pre(masm /*masm*/, 552 tmp1 /* obj */, 553 tmp2 /* pre_val */, 554 rthread /* thread */, 555 tmp3 /* tmp */, 556 val != noreg /* tosca_live */, 557 false /* expand_call */); 558 } 559 if (val == noreg) { 560 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg); 561 } else { 562 storeval_barrier(masm, val, tmp3); 563 Register new_val = val; 564 if (needs_post_barrier) { 565 if (UseCompressedOops) { 566 new_val = tmp2; 567 __ movptr(new_val, val); 568 } 569 } 570 BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg); 571 572 if (needs_post_barrier) { 573 shenandoah_write_barrier_post(masm /*masm*/, 574 tmp1 /* store_adr */, 575 new_val /* new_val */, 576 rthread /* thread */, 577 tmp3 /* tmp */, 578 tmp2 /* tmp2 */); 579 } 580 } 581 NOT_LP64(imasm->restore_bcp()); 582 583 } else { 584 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2); 585 } 586 } 587 588 void ShenandoahBarrierSetAssembler::obj_equals(MacroAssembler* masm, DecoratorSet decorators, Register op1, Register op2) { 589 __ cmpptr(op1, op2); 590 if (ShenandoahAcmpBarrier) { 591 Label done; 592 __ jccb(Assembler::equal, done); 593 read_barrier(masm, op1); 594 read_barrier(masm, op2); 595 __ cmpptr(op1, op2); 596 __ bind(done); 597 } 598 } 599 600 void ShenandoahBarrierSetAssembler::obj_equals_addr(MacroAssembler* masm, DecoratorSet decorators, Register src1, Address src2) { 601 __ cmpptr(src1, src2); 602 if (ShenandoahAcmpBarrier) { 603 Label done; 604 __ jccb(Assembler::equal, done); 605 __ movptr(rscratch2, src2); 606 read_barrier(masm, src1); 607 read_barrier(masm, rscratch2); 608 __ cmpptr(src1, rscratch2); 609 __ bind(done); 610 } 611 } 612 613 void ShenandoahBarrierSetAssembler::resolve_for_read(MacroAssembler* masm, DecoratorSet decorators, Register obj) { 614 bool oop_not_null = (decorators & OOP_NOT_NULL) != 0; 615 if (oop_not_null) { 616 read_barrier_not_null(masm, obj); 617 } else { 618 read_barrier(masm, obj); 619 } 620 } 621 622 void ShenandoahBarrierSetAssembler::resolve_for_write(MacroAssembler* masm, DecoratorSet decorators, Register obj) { 623 write_barrier(masm, obj); 624 } 625 626 // Special Shenandoah CAS implementation that handles false negatives 627 // due to concurrent evacuation. 628 #ifndef _LP64 629 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, DecoratorSet decorators, 630 Register res, Address addr, Register oldval, Register newval, 631 bool exchange, bool encode, Register tmp1, Register tmp2) { 632 // Shenandoah has no 32-bit version for this. 633 Unimplemented(); 634 } 635 #else 636 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, DecoratorSet decorators, 637 Register res, Address addr, Register oldval, Register newval, 638 bool exchange, bool encode, Register tmp1, Register tmp2) { 639 640 if (!ShenandoahCASBarrier) { 641 BarrierSetAssembler::cmpxchg_oop(masm, decorators, res, addr, oldval, newval, exchange, encode, tmp1, tmp2); 642 return; 643 } 644 645 assert(ShenandoahCASBarrier, "Should only be used when CAS barrier is enabled"); 646 assert(oldval == rax, "must be in rax for implicit use in cmpxchg"); 647 648 Label retry, done; 649 650 // Apply storeval barrier to newval. 651 if (encode) { 652 if (newval == c_rarg1 && ShenandoahStoreValEnqueueBarrier) { 653 __ mov(tmp2, newval); 654 storeval_barrier(masm, tmp2, tmp1); 655 } else { 656 storeval_barrier(masm, newval, tmp1); 657 } 658 } 659 660 if (UseCompressedOops) { 661 if (encode) { 662 __ encode_heap_oop(oldval); 663 __ mov(rscratch1, newval); 664 __ encode_heap_oop(rscratch1); 665 newval = rscratch1; 666 } 667 } 668 669 // Remember oldval for retry logic below 670 if (UseCompressedOops) { 671 __ movl(tmp1, oldval); 672 } else { 673 __ movptr(tmp1, oldval); 674 } 675 676 // Step 1. Try to CAS with given arguments. If successful, then we are done, 677 // and can safely return. 678 if (os::is_MP()) __ lock(); 679 if (UseCompressedOops) { 680 __ cmpxchgl(newval, addr); 681 } else { 682 __ cmpxchgptr(newval, addr); 683 } 684 __ jcc(Assembler::equal, done, true); 685 686 // Step 2. CAS had failed. This may be a false negative. 687 // 688 // The trouble comes when we compare the to-space pointer with the from-space 689 // pointer to the same object. To resolve this, it will suffice to read both 690 // oldval and the value from memory through the read barriers -- this will give 691 // both to-space pointers. If they mismatch, then it was a legitimate failure. 692 // 693 if (UseCompressedOops) { 694 __ decode_heap_oop(tmp1); 695 } 696 __ resolve_for_read(0, tmp1); 697 698 if (UseCompressedOops) { 699 __ movl(tmp2, oldval); 700 __ decode_heap_oop(tmp2); 701 } else { 702 __ movptr(tmp2, oldval); 703 } 704 __ resolve_for_read(0, tmp2); 705 706 __ cmpptr(tmp1, tmp2); 707 __ jcc(Assembler::notEqual, done, true); 708 709 // Step 3. Try to CAS again with resolved to-space pointers. 710 // 711 // Corner case: it may happen that somebody stored the from-space pointer 712 // to memory while we were preparing for retry. Therefore, we can fail again 713 // on retry, and so need to do this in loop, always re-reading the failure 714 // witness through the read barrier. 715 __ bind(retry); 716 if (os::is_MP()) __ lock(); 717 if (UseCompressedOops) { 718 __ cmpxchgl(newval, addr); 719 } else { 720 __ cmpxchgptr(newval, addr); 721 } 722 __ jcc(Assembler::equal, done, true); 723 724 if (UseCompressedOops) { 725 __ movl(tmp2, oldval); 726 __ decode_heap_oop(tmp2); 727 } else { 728 __ movptr(tmp2, oldval); 729 } 730 __ resolve_for_read(0, tmp2); 731 732 __ cmpptr(tmp1, tmp2); 733 __ jcc(Assembler::equal, retry, true); 734 735 // Step 4. If we need a boolean result out of CAS, check the flag again, 736 // and promote the result. Note that we handle the flag from both the CAS 737 // itself and from the retry loop. 738 __ bind(done); 739 if (!exchange) { 740 assert(res != NULL, "need result register"); 741 __ setb(Assembler::equal, res); 742 __ movzbl(res, res); 743 } 744 } 745 #endif // LP64 746 747 void ShenandoahBarrierSetAssembler::xchg_oop(MacroAssembler* masm, DecoratorSet decorators, 748 Register obj, Address addr, Register tmp) { 749 storeval_barrier(masm, obj, tmp); 750 BarrierSetAssembler::xchg_oop(masm, decorators, obj, addr, tmp); 751 } 752 753 #ifdef COMPILER1 754 755 #undef __ 756 #define __ ce->masm()-> 757 758 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) { 759 ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1(); 760 // At this point we know that marking is in progress. 761 // If do_load() is true then we have to emit the 762 // load of the previous value; otherwise it has already 763 // been loaded into _pre_val. 764 765 __ bind(*stub->entry()); 766 assert(stub->pre_val()->is_register(), "Precondition."); 767 768 Register pre_val_reg = stub->pre_val()->as_register(); 769 770 if (stub->do_load()) { 771 ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/); 772 } 773 774 __ cmpptr(pre_val_reg, (int32_t)NULL_WORD); 775 __ jcc(Assembler::equal, *stub->continuation()); 776 ce->store_parameter(stub->pre_val()->as_register(), 0); 777 __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin())); 778 __ jmp(*stub->continuation()); 779 780 } 781 782 #undef __ 783 784 #define __ sasm-> 785 786 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) { 787 __ prologue("shenandoah_pre_barrier", false); 788 // arg0 : previous value of memory 789 790 __ push(rax); 791 __ push(rdx); 792 793 const Register pre_val = rax; 794 const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread); 795 const Register tmp = rdx; 796 797 NOT_LP64(__ get_thread(thread);) 798 799 Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset())); 800 Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset())); 801 802 Label done; 803 Label runtime; 804 805 // Is SATB still active? 806 Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 807 __ testb(gc_state, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL); 808 __ jcc(Assembler::zero, done); 809 810 // Can we store original value in the thread's buffer? 811 812 __ movptr(tmp, queue_index); 813 __ testptr(tmp, tmp); 814 __ jcc(Assembler::zero, runtime); 815 __ subptr(tmp, wordSize); 816 __ movptr(queue_index, tmp); 817 __ addptr(tmp, buffer); 818 819 // prev_val (rax) 820 __ load_parameter(0, pre_val); 821 __ movptr(Address(tmp, 0), pre_val); 822 __ jmp(done); 823 824 __ bind(runtime); 825 826 __ save_live_registers_no_oop_map(true); 827 828 // load the pre-value 829 __ load_parameter(0, rcx); 830 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), rcx, thread); 831 832 __ restore_live_registers(true); 833 834 __ bind(done); 835 836 __ pop(rdx); 837 __ pop(rax); 838 839 __ epilogue(); 840 } 841 842 #undef __ 843 844 #endif // COMPILER1 845 846 address ShenandoahBarrierSetAssembler::shenandoah_wb() { 847 assert(_shenandoah_wb != NULL, "need write barrier stub"); 848 return _shenandoah_wb; 849 } 850 851 address ShenandoahBarrierSetAssembler::shenandoah_wb_C() { 852 assert(_shenandoah_wb_C != NULL, "need write barrier stub"); 853 return _shenandoah_wb_C; 854 } 855 856 #define __ cgen->assembler()-> 857 858 address ShenandoahBarrierSetAssembler::generate_shenandoah_wb(StubCodeGenerator* cgen, bool c_abi, bool do_cset_test) { 859 __ align(CodeEntryAlignment); 860 StubCodeMark mark(cgen, "StubRoutines", "shenandoah_wb"); 861 address start = __ pc(); 862 863 #ifdef _LP64 864 Label not_done; 865 866 // We use RDI, which also serves as argument register for slow call. 867 // RAX always holds the src object ptr, except after the slow call and 868 // the cmpxchg, then it holds the result. 869 // R8 and RCX are used as temporary registers. 870 if (!c_abi) { 871 __ push(rdi); 872 __ push(r8); 873 } 874 875 // Check for object beeing in the collection set. 876 // TODO: Can we use only 1 register here? 877 // The source object arrives here in rax. 878 // live: rax 879 // live: rdi 880 if (!c_abi) { 881 __ mov(rdi, rax); 882 } else { 883 if (rax != c_rarg0) { 884 __ mov(rax, c_rarg0); 885 } 886 } 887 if (do_cset_test) { 888 __ shrptr(rdi, ShenandoahHeapRegion::region_size_bytes_shift_jint()); 889 // live: r8 890 __ movptr(r8, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr()); 891 __ movbool(r8, Address(r8, rdi, Address::times_1)); 892 // unlive: rdi 893 __ testbool(r8); 894 // unlive: r8 895 __ jccb(Assembler::notZero, not_done); 896 897 if (!c_abi) { 898 __ pop(r8); 899 __ pop(rdi); 900 } 901 __ ret(0); 902 903 __ bind(not_done); 904 } 905 906 if (!c_abi) { 907 __ push(rcx); 908 } 909 910 if (!c_abi) { 911 __ push(rdx); 912 __ push(rdi); 913 __ push(rsi); 914 __ push(r8); 915 __ push(r9); 916 __ push(r10); 917 __ push(r11); 918 __ push(r12); 919 __ push(r13); 920 __ push(r14); 921 __ push(r15); 922 } 923 __ save_vector_registers(); 924 __ movptr(rdi, rax); 925 __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_JRT), rdi); 926 __ restore_vector_registers(); 927 if (!c_abi) { 928 __ pop(r15); 929 __ pop(r14); 930 __ pop(r13); 931 __ pop(r12); 932 __ pop(r11); 933 __ pop(r10); 934 __ pop(r9); 935 __ pop(r8); 936 __ pop(rsi); 937 __ pop(rdi); 938 __ pop(rdx); 939 940 __ pop(rcx); 941 __ pop(r8); 942 __ pop(rdi); 943 } 944 __ ret(0); 945 #else 946 ShouldNotReachHere(); 947 #endif 948 return start; 949 } 950 951 #undef __ 952 953 void ShenandoahBarrierSetAssembler::barrier_stubs_init() { 954 if (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier) { 955 int stub_code_size = 4096; 956 ResourceMark rm; 957 BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size); 958 CodeBuffer buf(bb); 959 StubCodeGenerator cgen(&buf); 960 _shenandoah_wb = generate_shenandoah_wb(&cgen, false, true); 961 _shenandoah_wb_C = generate_shenandoah_wb(&cgen, true, !ShenandoahWriteBarrierCsetTestInIR); 962 } 963 }