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 #ifdef COMPILER1
  36 #include "c1/c1_LIRAssembler.hpp"
  37 #include "c1/c1_MacroAssembler.hpp"
  38 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  39 #endif
  40 
  41 #define __ masm->
  42 
  43 address ShenandoahBarrierSetAssembler::_shenandoah_wb = NULL;
  44 
  45 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  46                                                        Register addr, Register count, RegSet saved_regs) {
  47   if (is_oop) {
  48     bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
  49     if (!dest_uninitialized && !ShenandoahHeap::heap()->heuristics()->can_do_traversal_gc()) {
  50       __ push(saved_regs, sp);
  51       if (count == c_rarg0) {
  52         if (addr == c_rarg1) {
  53           // exactly backwards!!
  54           __ mov(rscratch1, c_rarg0);
  55           __ mov(c_rarg0, c_rarg1);
  56           __ mov(c_rarg1, rscratch1);
  57         } else {
  58           __ mov(c_rarg1, count);
  59           __ mov(c_rarg0, addr);
  60         }
  61       } else {
  62         __ mov(c_rarg0, addr);
  63         __ mov(c_rarg1, count);
  64       }
  65       if (UseCompressedOops) {
  66         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2);
  67       } else {
  68         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2);
  69       }
  70       __ pop(saved_regs, sp);
  71     }
  72   }
  73 }
  74 
  75 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  76                                                        Register start, Register end, Register scratch, RegSet saved_regs) {
  77   if (is_oop) {
  78     __ push(saved_regs, sp);
  79     // must compute element count unless barrier set interface is changed (other platforms supply count)
  80     assert_different_registers(start, end, scratch);
  81     __ lea(scratch, Address(end, BytesPerHeapOop));
  82     __ sub(scratch, scratch, start);               // subtract start to get #bytes
  83     __ lsr(scratch, scratch, LogBytesPerHeapOop);  // convert to element count
  84     __ mov(c_rarg0, start);
  85     __ mov(c_rarg1, scratch);
  86     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2);
  87     __ pop(saved_regs, sp);
  88   }
  89 }
  90 
  91 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
  92                                                                  Register obj,
  93                                                                  Register pre_val,
  94                                                                  Register thread,
  95                                                                  Register tmp,
  96                                                                  bool tosca_live,
  97                                                                  bool expand_call) {
  98   if (ShenandoahSATBBarrier) {
  99     satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call);
 100   }
 101 }
 102 
 103 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
 104                                                            Register obj,
 105                                                            Register pre_val,
 106                                                            Register thread,
 107                                                            Register tmp,
 108                                                            bool tosca_live,
 109                                                            bool expand_call) {
 110   // If expand_call is true then we expand the call_VM_leaf macro
 111   // directly to skip generating the check by
 112   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 113 
 114   assert(thread == rthread, "must be");
 115 
 116   Label done;
 117   Label runtime;
 118 
 119   assert_different_registers(obj, pre_val, tmp, rscratch1);
 120   assert(pre_val != noreg &&  tmp != noreg, "expecting a register");
 121 
 122   Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
 123   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 124   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 125 
 126   // Is marking active?
 127   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 128     __ ldrw(tmp, in_progress);
 129   } else {
 130     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 131     __ ldrb(tmp, in_progress);
 132   }
 133   __ cbzw(tmp, done);
 134 
 135   // Do we need to load the previous value?
 136   if (obj != noreg) {
 137     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
 138   }
 139 
 140   // Is the previous value null?
 141   __ cbz(pre_val, done);
 142 
 143   // Can we store original value in the thread's buffer?
 144   // Is index == 0?
 145   // (The index field is typed as size_t.)
 146 
 147   __ ldr(tmp, index);                      // tmp := *index_adr
 148   __ cbz(tmp, runtime);                    // tmp == 0?
 149                                         // If yes, goto runtime
 150 
 151   __ sub(tmp, tmp, wordSize);              // tmp := tmp - wordSize
 152   __ str(tmp, index);                      // *index_adr := tmp
 153   __ ldr(rscratch1, buffer);
 154   __ add(tmp, tmp, rscratch1);             // tmp := tmp + *buffer_adr
 155 
 156   // Record the previous value
 157   __ str(pre_val, Address(tmp, 0));
 158   __ b(done);
 159 
 160   __ bind(runtime);
 161   // save the live input values
 162   RegSet saved = RegSet::of(pre_val);
 163   if (tosca_live) saved += RegSet::of(r0);
 164   if (obj != noreg) saved += RegSet::of(obj);
 165 
 166   __ push(saved, sp);
 167 
 168   // Calling the runtime using the regular call_VM_leaf mechanism generates
 169   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 170   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
 171   //
 172   // If we care generating the pre-barrier without a frame (e.g. in the
 173   // intrinsified Reference.get() routine) then ebp might be pointing to
 174   // the caller frame and so this check will most likely fail at runtime.
 175   //
 176   // Expanding the call directly bypasses the generation of the check.
 177   // So when we do not have have a full interpreter frame on the stack
 178   // expand_call should be passed true.
 179 
 180   if (expand_call) {
 181     assert(pre_val != c_rarg1, "smashed arg");
 182     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 183   } else {
 184     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 185   }
 186 
 187   __ pop(saved, sp);
 188 
 189   __ bind(done);
 190 }
 191 
 192 void ShenandoahBarrierSetAssembler::read_barrier(MacroAssembler* masm, Register dst) {
 193   if (ShenandoahReadBarrier) {
 194     read_barrier_impl(masm, dst);
 195   }
 196 }
 197 
 198 void ShenandoahBarrierSetAssembler::read_barrier_impl(MacroAssembler* masm, Register dst) {
 199   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier || ShenandoahCASBarrier), "should be enabled");
 200   Label is_null;
 201   __ cbz(dst, is_null);
 202   read_barrier_not_null_impl(masm, dst);
 203   __ bind(is_null);
 204 }
 205 
 206 void ShenandoahBarrierSetAssembler::read_barrier_not_null(MacroAssembler* masm, Register dst) {
 207   if (ShenandoahReadBarrier) {
 208     read_barrier_not_null_impl(masm, dst);
 209   }
 210 }
 211 
 212 
 213 void ShenandoahBarrierSetAssembler::read_barrier_not_null_impl(MacroAssembler* masm, Register dst) {
 214   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier || ShenandoahCASBarrier), "should be enabled");
 215   __ ldr(dst, Address(dst, ShenandoahBrooksPointer::byte_offset()));
 216 }
 217 
 218 void ShenandoahBarrierSetAssembler::write_barrier(MacroAssembler* masm, Register dst) {
 219   if (ShenandoahWriteBarrier) {
 220     write_barrier_impl(masm, dst);
 221   }
 222 }
 223 
 224 void ShenandoahBarrierSetAssembler::write_barrier_impl(MacroAssembler* masm, Register dst) {
 225   assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier), "Should be enabled");
 226   assert(dst != rscratch1, "need rscratch1");
 227   assert(dst != rscratch2, "need rscratch2");
 228 
 229   Label done;
 230 
 231   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 232   __ ldrb(rscratch1, gc_state);
 233 
 234   // Check for heap stability
 235   __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 236   __ tst(rscratch1, rscratch2);
 237   __ br(Assembler::EQ, done);
 238 
 239   // Heap is unstable, need to perform the read-barrier even if WB is inactive
 240   __ ldr(dst, Address(dst, ShenandoahBrooksPointer::byte_offset()));
 241 
 242   // Check for evacuation-in-progress and jump to WB slow-path if needed
 243   __ mov(rscratch2, ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 244   __ tst(rscratch1, rscratch2);
 245   __ br(Assembler::EQ, done);
 246 
 247   RegSet to_save = RegSet::of(r0);
 248   if (dst != r0) {
 249     __ push(to_save, sp);
 250     __ mov(r0, dst);
 251   }
 252 
 253   __ far_call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahBarrierSetAssembler::shenandoah_wb())));
 254 
 255   if (dst != r0) {
 256     __ mov(dst, r0);
 257     __ pop(to_save, sp);
 258   }
 259 
 260   __ bind(done);
 261 }
 262 
 263 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 264   if (ShenandoahStoreValEnqueueBarrier) {
 265     Label is_null;
 266     __ cbz(dst, is_null);
 267     write_barrier_impl(masm, dst);
 268     __ bind(is_null);
 269     // Save possibly live regs.
 270     RegSet live_regs = RegSet::range(r0, r4) - dst;
 271     __ push(live_regs, sp);
 272     __ strd(v0, __ pre(sp, 2 * -wordSize));
 273 
 274     satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false);
 275 
 276     // Restore possibly live regs.
 277     __ ldrd(v0, __ post(sp, 2 * wordSize));
 278     __ pop(live_regs, sp);
 279   }
 280   if (ShenandoahStoreValReadBarrier) {
 281     read_barrier_impl(masm, dst);
 282   }
 283 }
 284 
 285 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 286                                             Register dst, Address src, Register tmp1, Register tmp_thread) {
 287   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 288   bool in_heap = (decorators & IN_HEAP) != 0;
 289   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 290   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 291   bool on_reference = on_weak || on_phantom;
 292 
 293   if (in_heap) {
 294     read_barrier_not_null(masm, src.base());
 295   }
 296 
 297   BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
 298   if (ShenandoahKeepAliveBarrier && on_oop && on_reference) {
 299     __ enter();
 300     satb_write_barrier_pre(masm /* masm */,
 301                            noreg /* obj */,
 302                            dst /* pre_val */,
 303                            rthread /* thread */,
 304                            tmp1 /* tmp */,
 305                            true /* tosca_live */,
 306                            true /* expand_call */);
 307     __ leave();
 308   }
 309 }
 310 
 311 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 312                                              Address dst, Register val, Register tmp1, Register tmp2) {
 313   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 314   bool in_heap = (decorators & IN_HEAP) != 0;
 315   if (in_heap) {
 316     write_barrier(masm, dst.base());
 317   }
 318   if (!on_oop) {
 319     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2);
 320     return;
 321   }
 322 
 323   // flatten object address if needed
 324   if (dst.index() == noreg && dst.offset() == 0) {
 325     if (dst.base() != r3) {
 326       __ mov(r3, dst.base());
 327     }
 328   } else {
 329     __ lea(r3, dst);
 330   }
 331 
 332   shenandoah_write_barrier_pre(masm,
 333                                r3 /* obj */,
 334                                tmp2 /* pre_val */,
 335                                rthread /* thread */,
 336                                tmp1  /* tmp */,
 337                                val != noreg /* tosca_live */,
 338                                false /* expand_call */);
 339 
 340   if (val == noreg) {
 341     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
 342   } else {
 343     storeval_barrier(masm, val, tmp1);
 344     // G1 barrier needs uncompressed oop for region cross check.
 345     Register new_val = val;
 346     if (UseCompressedOops) {
 347       new_val = rscratch2;
 348       __ mov(new_val, val);
 349     }
 350     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
 351   }
 352 
 353 }
 354 
 355 void ShenandoahBarrierSetAssembler::obj_equals(MacroAssembler* masm, Register op1, Register op2) {
 356   __ cmp(op1, op2);
 357   if (ShenandoahAcmpBarrier) {
 358     Label done;
 359     __ br(Assembler::EQ, done);
 360     // The object may have been evacuated, but we won't see it without a
 361     // membar here.
 362     __ membar(Assembler::LoadStore| Assembler::LoadLoad);
 363     read_barrier(masm, op1);
 364     read_barrier(masm, op2);
 365     __ cmp(op1, op2);
 366     __ bind(done);
 367   }
 368 }
 369 
 370 void ShenandoahBarrierSetAssembler::tlab_allocate(MacroAssembler* masm, Register obj,
 371                                                   Register var_size_in_bytes,
 372                                                   int con_size_in_bytes,
 373                                                   Register t1,
 374                                                   Register t2,
 375                                                   Label& slow_case) {
 376 
 377   assert_different_registers(obj, t2);
 378   assert_different_registers(obj, var_size_in_bytes);
 379   Register end = t2;
 380 
 381   __ ldr(obj, Address(rthread, JavaThread::tlab_top_offset()));
 382   if (var_size_in_bytes == noreg) {
 383     __ lea(end, Address(obj, (int) (con_size_in_bytes + ShenandoahBrooksPointer::byte_size())));
 384   } else {
 385     __ add(var_size_in_bytes, var_size_in_bytes, ShenandoahBrooksPointer::byte_size());
 386     __ lea(end, Address(obj, var_size_in_bytes));
 387   }
 388   __ ldr(rscratch1, Address(rthread, JavaThread::tlab_end_offset()));
 389   __ cmp(end, rscratch1);
 390   __ br(Assembler::HI, slow_case);
 391 
 392   // update the tlab top pointer
 393   __ str(end, Address(rthread, JavaThread::tlab_top_offset()));
 394 
 395   __ add(obj, obj, ShenandoahBrooksPointer::byte_size());
 396   __ str(obj, Address(obj, ShenandoahBrooksPointer::byte_offset()));
 397 
 398   // recover var_size_in_bytes if necessary
 399   if (var_size_in_bytes == end) {
 400     __ sub(var_size_in_bytes, var_size_in_bytes, obj);
 401   }
 402 }
 403 
 404 void ShenandoahBarrierSetAssembler::resolve(MacroAssembler* masm, DecoratorSet decorators, Register obj) {
 405   bool oop_not_null = (decorators & IS_NOT_NULL) != 0;
 406   bool is_write = (decorators & ACCESS_WRITE) != 0;
 407   if (is_write) {
 408     if (oop_not_null) {
 409       write_barrier(masm, obj);
 410     } else {
 411       Label done;
 412       __ cbz(obj, done);
 413       write_barrier(masm, obj);
 414       __ bind(done);
 415     }
 416   } else {
 417     if (oop_not_null) {
 418       read_barrier_not_null(masm, obj);
 419     } else {
 420       read_barrier(masm, obj);
 421     }
 422   }
 423 }
 424 
 425 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val,
 426                                                 bool acquire, bool release, bool weak, bool is_cae,
 427                                                 Register result) {
 428 
 429   Register tmp = rscratch2;
 430   bool is_narrow = UseCompressedOops;
 431   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 432 
 433   assert_different_registers(addr, expected, new_val, result, tmp);
 434 
 435   Label retry, done, fail;
 436 
 437   // CAS, using LL/SC pair.
 438   __ bind(retry);
 439   __ load_exclusive(result, addr, size, acquire);
 440   if (is_narrow) {
 441     __ cmpw(result, expected);
 442   } else {
 443     __ cmp(result, expected);
 444   }
 445   __ br(Assembler::NE, fail);
 446   __ store_exclusive(tmp, new_val, addr, size, release);
 447   if (weak) {
 448     __ cmpw(tmp, 0u); // If the store fails, return NE to our caller
 449   } else {
 450     __ cbnzw(tmp, retry);
 451   }
 452   __ b(done);
 453 
 454  __  bind(fail);
 455   // Check if rb(expected)==rb(result)
 456   // Shuffle registers so that we have memory value ready for next expected.
 457   __ mov(tmp, expected);
 458   __ mov(expected, result);
 459   if (is_narrow) {
 460     __ decode_heap_oop(result, result);
 461     __ decode_heap_oop(tmp, tmp);
 462   }
 463   read_barrier_impl(masm, result);
 464   read_barrier_impl(masm, tmp);
 465   __ cmp(result, tmp);
 466   // Retry with expected now being the value we just loaded from addr.
 467   __ br(Assembler::EQ, retry);
 468   if (is_cae && is_narrow) {
 469     // For cmp-and-exchange and narrow oops, we need to restore
 470     // the compressed old-value. We moved it to 'expected' a few lines up.
 471     __ mov(result, expected);
 472   }
 473   __ bind(done);
 474 
 475   if (!is_cae) {
 476     __ cset(result, Assembler::EQ);
 477   }
 478 }
 479 
 480 #ifdef COMPILER1
 481 
 482 #undef __
 483 #define __ ce->masm()->
 484 
 485 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 486   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 487   // At this point we know that marking is in progress.
 488   // If do_load() is true then we have to emit the
 489   // load of the previous value; otherwise it has already
 490   // been loaded into _pre_val.
 491 
 492   __ bind(*stub->entry());
 493 
 494   assert(stub->pre_val()->is_register(), "Precondition.");
 495 
 496   Register pre_val_reg = stub->pre_val()->as_register();
 497 
 498   if (stub->do_load()) {
 499     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 500   }
 501   __ cbz(pre_val_reg, *stub->continuation());
 502   ce->store_parameter(stub->pre_val()->as_register(), 0);
 503   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
 504   __ b(*stub->continuation());
 505 }
 506 
 507 void ShenandoahBarrierSetAssembler::gen_write_barrier_stub(LIR_Assembler* ce, ShenandoahWriteBarrierStub* stub) {
 508 
 509   Register obj = stub->obj()->as_register();
 510   Register res = stub->result()->as_register();
 511 
 512   Label done;
 513 
 514   __ bind(*stub->entry());
 515 
 516   if (res != obj) {
 517     __ mov(res, obj);
 518   }
 519   // Check for null.
 520   if (stub->needs_null_check()) {
 521     __ cbz(res, done);
 522   }
 523 
 524   write_barrier(ce->masm(), res);
 525 
 526   __ bind(done);
 527   __ b(*stub->continuation());
 528 }
 529 
 530 #undef __
 531 
 532 #define __ sasm->
 533 
 534 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 535   __ prologue("shenandoah_pre_barrier", false);
 536 
 537   // arg0 : previous value of memory
 538 
 539   BarrierSet* bs = BarrierSet::barrier_set();
 540 
 541   const Register pre_val = r0;
 542   const Register thread = rthread;
 543   const Register tmp = rscratch1;
 544 
 545   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 546   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 547 
 548   Label done;
 549   Label runtime;
 550 
 551   // Is marking still active?
 552   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 553   __ ldrb(tmp, gc_state);
 554   __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 555   __ tst(tmp, rscratch2);
 556   __ br(Assembler::EQ, done);
 557 
 558   // Can we store original value in the thread's buffer?
 559   __ ldr(tmp, queue_index);
 560   __ cbz(tmp, runtime);
 561 
 562   __ sub(tmp, tmp, wordSize);
 563   __ str(tmp, queue_index);
 564   __ ldr(rscratch2, buffer);
 565   __ add(tmp, tmp, rscratch2);
 566   __ load_parameter(0, rscratch2);
 567   __ str(rscratch2, Address(tmp, 0));
 568   __ b(done);
 569 
 570   __ bind(runtime);
 571   __ push_call_clobbered_registers();
 572   __ load_parameter(0, pre_val);
 573   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 574   __ pop_call_clobbered_registers();
 575   __ bind(done);
 576 
 577   __ epilogue();
 578 }
 579 
 580 #undef __
 581 
 582 #endif // COMPILER1
 583 
 584 address ShenandoahBarrierSetAssembler::shenandoah_wb() {
 585   assert(_shenandoah_wb != NULL, "need write barrier stub");
 586   return _shenandoah_wb;
 587 }
 588 
 589 #define __ cgen->assembler()->
 590 
 591 // Shenandoah write barrier.
 592 //
 593 // Input:
 594 //   r0: OOP to evacuate.  Not null.
 595 //
 596 // Output:
 597 //   r0: Pointer to evacuated OOP.
 598 //
 599 // Trash rscratch1, rscratch2.  Preserve everything else.
 600 address ShenandoahBarrierSetAssembler::generate_shenandoah_wb(StubCodeGenerator* cgen) {
 601 
 602   __ align(6);
 603   StubCodeMark mark(cgen, "StubRoutines", "shenandoah_wb");
 604   address start = __ pc();
 605 
 606   Label work;
 607   __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
 608   __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 609   __ ldrb(rscratch2, Address(rscratch2, rscratch1));
 610   __ tbnz(rscratch2, 0, work);
 611   __ ret(lr);
 612   __ bind(work);
 613 
 614   Register obj = r0;
 615 
 616   __ enter(); // required for proper stackwalking of RuntimeStub frame
 617 
 618   __ push_call_clobbered_registers();
 619 
 620   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_JRT));
 621   __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral);
 622   __ mov(rscratch1, obj);
 623   __ pop_call_clobbered_registers();
 624   __ mov(obj, rscratch1);
 625 
 626   __ leave(); // required for proper stackwalking of RuntimeStub frame
 627   __ ret(lr);
 628 
 629   return start;
 630 }
 631 
 632 #undef __
 633 
 634 void ShenandoahBarrierSetAssembler::barrier_stubs_init() {
 635   if (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier) {
 636     int stub_code_size = 2048;
 637     ResourceMark rm;
 638     BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size);
 639     CodeBuffer buf(bb);
 640     StubCodeGenerator cgen(&buf);
 641     _shenandoah_wb = generate_shenandoah_wb(&cgen);
 642   }
 643 }