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