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