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