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