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