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