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