1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  26 #include "gc/shenandoah/shenandoahForwarding.hpp"
  27 #include "gc/shenandoah/shenandoahHeap.inline.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 #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_lrb = NULL;
  45 
  46 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  47                                                        Register src, Register dst, Register count, RegSet saved_regs) {
  48   if (is_oop) {
  49     bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
  50     if (ShenandoahSATBBarrier && !dest_uninitialized) {
  51 
  52       Label done;
  53 
  54       // Avoid calling runtime if count == 0
  55       __ cbz(count, done);
  56 
  57       // Is marking active?
  58       Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
  59       __ ldrb(rscratch1, gc_state);
  60       __ tbz(rscratch1, ShenandoahHeap::MARKING_BITPOS, done);
  61 
  62       __ push(saved_regs, sp);
  63       if (count == c_rarg0) {
  64         if (dst == c_rarg1) {
  65           // exactly backwards!!
  66           __ mov(rscratch1, c_rarg0);
  67           __ mov(c_rarg0, c_rarg1);
  68           __ mov(c_rarg1, rscratch1);
  69         } else {
  70           __ mov(c_rarg1, count);
  71           __ mov(c_rarg0, dst);
  72         }
  73       } else {
  74         __ mov(c_rarg0, dst);
  75         __ mov(c_rarg1, count);
  76       }
  77       if (UseCompressedOops) {
  78         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2);
  79       } else {
  80         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2);
  81       }
  82       __ pop(saved_regs, sp);
  83       __ bind(done);
  84     }
  85   }
  86 }
  87 
  88 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  89                                                        Register start, Register count, Register scratch, RegSet saved_regs) {
  90   if (is_oop) {
  91       Label done;
  92 
  93       // Avoid calling runtime if count == 0
  94       __ cbz(count, done);
  95 
  96       // Is updating references?
  97       Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
  98       __ ldrb(rscratch1, gc_state);
  99       __ tbz(rscratch1, ShenandoahHeap::UPDATEREFS_BITPOS, done);
 100 
 101     __ push(saved_regs, sp);
 102     assert_different_registers(start, count, scratch);
 103     assert_different_registers(c_rarg0, count);
 104     __ mov(c_rarg0, start);
 105     __ mov(c_rarg1, count);
 106     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2);
 107     __ pop(saved_regs, sp);
 108 
 109     __ bind(done);
 110   }
 111 }
 112 
 113 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
 114                                                                  Register obj,
 115                                                                  Register pre_val,
 116                                                                  Register thread,
 117                                                                  Register tmp,
 118                                                                  bool tosca_live,
 119                                                                  bool expand_call) {
 120   if (ShenandoahSATBBarrier) {
 121     satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call);
 122   }
 123 }
 124 
 125 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
 126                                                            Register obj,
 127                                                            Register pre_val,
 128                                                            Register thread,
 129                                                            Register tmp,
 130                                                            bool tosca_live,
 131                                                            bool expand_call) {
 132   // If expand_call is true then we expand the call_VM_leaf macro
 133   // directly to skip generating the check by
 134   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 135 
 136   assert(thread == rthread, "must be");
 137 
 138   Label done;
 139   Label runtime;
 140 
 141   assert_different_registers(obj, pre_val, tmp, rscratch1);
 142   assert(pre_val != noreg &&  tmp != noreg, "expecting a register");
 143 
 144   Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
 145   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 146   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 147 
 148   // Is marking active?
 149   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 150     __ ldrw(tmp, in_progress);
 151   } else {
 152     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 153     __ ldrb(tmp, in_progress);
 154   }
 155   __ cbzw(tmp, done);
 156 
 157   // Do we need to load the previous value?
 158   if (obj != noreg) {
 159     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
 160   }
 161 
 162   // Is the previous value null?
 163   __ cbz(pre_val, done);
 164 
 165   // Can we store original value in the thread's buffer?
 166   // Is index == 0?
 167   // (The index field is typed as size_t.)
 168 
 169   __ ldr(tmp, index);                      // tmp := *index_adr
 170   __ cbz(tmp, runtime);                    // tmp == 0?
 171                                         // If yes, goto runtime
 172 
 173   __ sub(tmp, tmp, wordSize);              // tmp := tmp - wordSize
 174   __ str(tmp, index);                      // *index_adr := tmp
 175   __ ldr(rscratch1, buffer);
 176   __ add(tmp, tmp, rscratch1);             // tmp := tmp + *buffer_adr
 177 
 178   // Record the previous value
 179   __ str(pre_val, Address(tmp, 0));
 180   __ b(done);
 181 
 182   __ bind(runtime);
 183   // save the live input values
 184   RegSet saved = RegSet::of(pre_val);
 185   if (tosca_live) saved += RegSet::of(r0);
 186   if (obj != noreg) saved += RegSet::of(obj);
 187 
 188   __ push(saved, sp);
 189 
 190   // Calling the runtime using the regular call_VM_leaf mechanism generates
 191   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 192   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
 193   //
 194   // If we care generating the pre-barrier without a frame (e.g. in the
 195   // intrinsified Reference.get() routine) then ebp might be pointing to
 196   // the caller frame and so this check will most likely fail at runtime.
 197   //
 198   // Expanding the call directly bypasses the generation of the check.
 199   // So when we do not have have a full interpreter frame on the stack
 200   // expand_call should be passed true.
 201 
 202   if (expand_call) {
 203     assert(pre_val != c_rarg1, "smashed arg");
 204     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 205   } else {
 206     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 207   }
 208 
 209   __ pop(saved, sp);
 210 
 211   __ bind(done);
 212 }
 213 
 214 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) {
 215   assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
 216   Label is_null;
 217   __ cbz(dst, is_null);
 218   resolve_forward_pointer_not_null(masm, dst, tmp);
 219   __ bind(is_null);
 220 }
 221 
 222 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitely
 223 // passed in.
 224 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) {
 225   assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
 226   // The below loads the mark word, checks if the lowest two bits are
 227   // set, and if so, clear the lowest two bits and copy the result
 228   // to dst. Otherwise it leaves dst alone.
 229   // Implementing this is surprisingly awkward. I do it here by:
 230   // - Inverting the mark word
 231   // - Test lowest two bits == 0
 232   // - If so, set the lowest two bits
 233   // - Invert the result back, and copy to dst
 234 
 235   bool borrow_reg = (tmp == noreg);
 236   if (borrow_reg) {
 237     // No free registers available. Make one useful.
 238     tmp = rscratch1;
 239     __ push(RegSet::of(tmp), sp);
 240   }
 241 
 242   Label done;
 243   __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes()));
 244   __ eon(tmp, tmp, zr);
 245   __ ands(zr, tmp, markOopDesc::lock_mask_in_place);
 246   __ br(Assembler::NE, done);
 247   __ orr(tmp, tmp, markOopDesc::marked_value);
 248   __ eon(dst, tmp, zr);
 249   __ bind(done);
 250 
 251   if (borrow_reg) {
 252     __ pop(RegSet::of(tmp), sp);
 253   }
 254 }
 255 
 256 void ShenandoahBarrierSetAssembler::load_reference_barrier_not_null(MacroAssembler* masm, Register dst, Register tmp) {
 257   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 258   assert(dst != rscratch2, "need rscratch2");
 259 
 260   Label done;
 261   __ enter();
 262   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 263   __ ldrb(rscratch2, gc_state);
 264 
 265   // Check for heap stability
 266   __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
 267 
 268   RegSet to_save = RegSet::of(r0);
 269   if (dst != r0) {
 270     __ push(to_save, sp);
 271     __ mov(r0, dst);
 272   }
 273 
 274   __ far_call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahBarrierSetAssembler::shenandoah_lrb())));
 275 
 276   if (dst != r0) {
 277     __ mov(dst, r0);
 278     __ pop(to_save, sp);
 279   }
 280 
 281   __ bind(done);
 282   __ leave();
 283 }
 284 
 285 void ShenandoahBarrierSetAssembler::load_reference_barrier_native(MacroAssembler* masm, Register dst, Register tmp) {
 286   if (!ShenandoahLoadRefBarrier) {
 287     return;
 288   }
 289 
 290   assert(dst != rscratch2, "need rscratch2");
 291 
 292   Label is_null;
 293   Label done;
 294 
 295   __ cbz(dst, is_null);
 296 
 297   __ enter();
 298 
 299   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 300   __ ldrb(rscratch2, gc_state);
 301 
 302   // Check for heap in evacuation phase
 303   __ tbz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, done);
 304 
 305   __ mov(rscratch2, dst);
 306   __ push_call_clobbered_registers();
 307   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_native));
 308   __ mov(r0, rscratch2);
 309   __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral);
 310   __ mov(rscratch2, r0);
 311   __ pop_call_clobbered_registers();
 312   __ mov(dst, rscratch2);
 313 
 314   __ bind(done);
 315   __ leave();
 316   __ bind(is_null);
 317 }
 318 
 319 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 320   if (ShenandoahStoreValEnqueueBarrier) {
 321     // Save possibly live regs.
 322     RegSet live_regs = RegSet::range(r0, r4) - dst;
 323     __ push(live_regs, sp);
 324     __ strd(v0, __ pre(sp, 2 * -wordSize));
 325 
 326     satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false);
 327 
 328     // Restore possibly live regs.
 329     __ ldrd(v0, __ post(sp, 2 * wordSize));
 330     __ pop(live_regs, sp);
 331   }
 332 }
 333 
 334 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 335   if (ShenandoahLoadRefBarrier) {
 336     Label is_null;
 337     __ cbz(dst, is_null);
 338     load_reference_barrier_not_null(masm, dst, tmp);
 339     __ bind(is_null);
 340   }
 341 }
 342 
 343 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 344                                             Register dst, Address src, Register tmp1, Register tmp_thread) {
 345   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 346   bool not_in_heap = (decorators & IN_NATIVE) != 0;
 347   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 348   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 349   bool on_reference = on_weak || on_phantom;
 350   bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 351 
 352   BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
 353   if (on_oop) {
 354      if (not_in_heap) {
 355        if (ShenandoahHeap::heap()->is_traversal_mode()) {
 356          load_reference_barrier(masm, dst, tmp1);
 357          keep_alive = true;
 358        } else {
 359          load_reference_barrier_native(masm, dst, tmp1);
 360        }
 361      } else {
 362        load_reference_barrier(masm, dst, tmp1);
 363      }
 364     if (ShenandoahKeepAliveBarrier && on_reference && keep_alive) {
 365       __ enter();
 366       satb_write_barrier_pre(masm /* masm */,
 367                              noreg /* obj */,
 368                              dst /* pre_val */,
 369                              rthread /* thread */,
 370                              tmp1 /* tmp */,
 371                              true /* tosca_live */,
 372                              true /* expand_call */);
 373       __ leave();
 374     }
 375   }
 376 }
 377 
 378 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 379                                              Address dst, Register val, Register tmp1, Register tmp2) {
 380   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 381   if (!on_oop) {
 382     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2);
 383     return;
 384   }
 385 
 386   // flatten object address if needed
 387   if (dst.index() == noreg && dst.offset() == 0) {
 388     if (dst.base() != r3) {
 389       __ mov(r3, dst.base());
 390     }
 391   } else {
 392     __ lea(r3, dst);
 393   }
 394 
 395   shenandoah_write_barrier_pre(masm,
 396                                r3 /* obj */,
 397                                tmp2 /* pre_val */,
 398                                rthread /* thread */,
 399                                tmp1  /* tmp */,
 400                                val != noreg /* tosca_live */,
 401                                false /* expand_call */);
 402 
 403   if (val == noreg) {
 404     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
 405   } else {
 406     storeval_barrier(masm, val, tmp1);
 407     // G1 barrier needs uncompressed oop for region cross check.
 408     Register new_val = val;
 409     if (UseCompressedOops) {
 410       new_val = rscratch2;
 411       __ mov(new_val, val);
 412     }
 413     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
 414   }
 415 
 416 }
 417 
 418 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val,
 419                                                 bool acquire, bool release, bool weak, bool is_cae,
 420                                                 Register result) {
 421   Register tmp1 = rscratch1;
 422   Register tmp2 = rscratch2;
 423   bool is_narrow = UseCompressedOops;
 424   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 425 
 426   assert_different_registers(addr, expected, new_val, tmp1, tmp2);
 427 
 428   Label retry, done, fail;
 429 
 430   // CAS, using LL/SC pair.
 431   __ bind(retry);
 432   __ load_exclusive(tmp1, addr, size, acquire);
 433   if (is_narrow) {
 434     __ cmpw(tmp1, expected);
 435   } else {
 436     __ cmp(tmp1, expected);
 437   }
 438   __ br(Assembler::NE, fail);
 439   __ store_exclusive(tmp2, new_val, addr, size, release);
 440   if (weak) {
 441     __ cmpw(tmp2, 0u); // If the store fails, return NE to our caller
 442   } else {
 443     __ cbnzw(tmp2, retry);
 444   }
 445   __ b(done);
 446 
 447  __  bind(fail);
 448   // Check if rb(expected)==rb(tmp1)
 449   // Shuffle registers so that we have memory value ready for next expected.
 450   __ mov(tmp2, expected);
 451   __ mov(expected, tmp1);
 452   if (is_narrow) {
 453     __ decode_heap_oop(tmp1, tmp1);
 454     __ decode_heap_oop(tmp2, tmp2);
 455   }
 456   resolve_forward_pointer(masm, tmp1);
 457   resolve_forward_pointer(masm, tmp2);
 458   __ cmp(tmp1, tmp2);
 459   // Retry with expected now being the value we just loaded from addr.
 460   __ br(Assembler::EQ, retry);
 461   if (is_cae && is_narrow) {
 462     // For cmp-and-exchange and narrow oops, we need to restore
 463     // the compressed old-value. We moved it to 'expected' a few lines up.
 464     __ mov(tmp1, expected);
 465   }
 466   __ bind(done);
 467 
 468   if (is_cae) {
 469     __ mov(result, tmp1);
 470   } else {
 471     __ cset(result, Assembler::EQ);
 472   }
 473 }
 474 
 475 // Generate cset check. If obj is in cset, branch to in_cset label, otherwise fall through
 476 // obj: Register holding the oop, preserved
 477 // tmp1, tmp2: temp registers, trashed
 478 void ShenandoahBarrierSetAssembler::gen_cset_check(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& in_cset) {
 479   __ mov(tmp2, ShenandoahHeap::in_cset_fast_test_addr());
 480   __ lsr(tmp1, obj, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 481   __ ldrb(tmp2, Address(tmp2, tmp1));
 482   __ tbnz(tmp2, 0, in_cset);
 483 }
 484 
 485 // Generate check if object is resolved. Branch to not_resolved label, if not. Otherwise return resolved
 486 // object in obj register.
 487 // obj: object, resolved object on normal return
 488 // tmp1, tmp2: temp registers, trashed
 489 void ShenandoahBarrierSetAssembler::gen_resolved_check(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& resolved) {
 490   __ mov(tmp2, obj);
 491   resolve_forward_pointer_not_null(masm, obj, tmp1);
 492   __ cmp(tmp2, obj);
 493   __ br(Assembler::EQ, resolved);
 494 }
 495 
 496 #undef __
 497 
 498 #ifdef COMPILER1
 499 
 500 #define __ ce->masm()->
 501 
 502 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 503   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 504   // At this point we know that marking is in progress.
 505   // If do_load() is true then we have to emit the
 506   // load of the previous value; otherwise it has already
 507   // been loaded into _pre_val.
 508 
 509   __ bind(*stub->entry());
 510 
 511   assert(stub->pre_val()->is_register(), "Precondition.");
 512 
 513   Register pre_val_reg = stub->pre_val()->as_register();
 514 
 515   if (stub->do_load()) {
 516     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 517   }
 518   __ cbz(pre_val_reg, *stub->continuation());
 519   ce->store_parameter(stub->pre_val()->as_register(), 0);
 520   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
 521   __ b(*stub->continuation());
 522 }
 523 
 524 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
 525   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 526   __ bind(*stub->entry());
 527 
 528   Register obj = stub->obj()->as_register();
 529   Register res = stub->result()->as_register();
 530    assert(res == r0, "result must arrive in r0");
 531 
 532   if (res != obj) {
 533     __ mov(res, obj);
 534   }
 535 
 536   // Check for null.
 537   __ cbz(res, *stub->continuation());
 538 
 539   ce->store_parameter(res, 0);
 540   __ far_call(RuntimeAddress(bs->load_reference_barrier_rt_code_blob()->code_begin()));
 541 
 542   __ b(*stub->continuation());
 543 }
 544 
 545 #undef __
 546 
 547 #define __ sasm->
 548 
 549 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 550   __ prologue("shenandoah_pre_barrier", false);
 551 
 552   // arg0 : previous value of memory
 553 
 554   BarrierSet* bs = BarrierSet::barrier_set();
 555 
 556   const Register pre_val = r0;
 557   const Register thread = rthread;
 558   const Register tmp = rscratch1;
 559 
 560   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 561   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 562 
 563   Label done;
 564   Label runtime;
 565 
 566   // Is marking still active?
 567   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 568   __ ldrb(tmp, gc_state);
 569   __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 570   __ tst(tmp, rscratch2);
 571   __ br(Assembler::EQ, done);
 572 
 573   // Can we store original value in the thread's buffer?
 574   __ ldr(tmp, queue_index);
 575   __ cbz(tmp, runtime);
 576 
 577   __ sub(tmp, tmp, wordSize);
 578   __ str(tmp, queue_index);
 579   __ ldr(rscratch2, buffer);
 580   __ add(tmp, tmp, rscratch2);
 581   __ load_parameter(0, rscratch2);
 582   __ str(rscratch2, Address(tmp, 0));
 583   __ b(done);
 584 
 585   __ bind(runtime);
 586   __ push_call_clobbered_registers();
 587   __ load_parameter(0, pre_val);
 588   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 589   __ pop_call_clobbered_registers();
 590   __ bind(done);
 591 
 592   __ epilogue();
 593 }
 594 
 595 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm) {
 596   __ prologue("shenandoah_load_reference_barrier", false);
 597   // arg0 : object to be resolved
 598 
 599   Label work, call_rt;
 600   __ load_parameter(0, r0);
 601   gen_cset_check(sasm, r0, rscratch1, rscratch2, work);
 602   __ epilogue();
 603 
 604   __ bind(work);
 605   gen_resolved_check(sasm, r0, rscratch1, rscratch2, call_rt);
 606   __ epilogue();
 607 
 608   __ bind(call_rt);
 609 
 610   __ push_call_clobbered_registers();
 611 
 612   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier));
 613   __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral);
 614   __ mov(rscratch1, r0);
 615   __ pop_call_clobbered_registers();
 616   __ mov(r0, rscratch1);
 617 
 618   __ epilogue();
 619 }
 620 
 621 #undef __
 622 
 623 #endif // COMPILER1
 624 
 625 address ShenandoahBarrierSetAssembler::shenandoah_lrb() {
 626   assert(_shenandoah_lrb != NULL, "need load reference barrier stub");
 627   return _shenandoah_lrb;
 628 }
 629 
 630 #define __ cgen->assembler()->
 631 
 632 // Shenandoah load reference barrier.
 633 //
 634 // Input:
 635 //   r0: OOP to evacuate.  Not null.
 636 //
 637 // Output:
 638 //   r0: Pointer to evacuated OOP.
 639 //
 640 // Trash rscratch1, rscratch2.  Preserve everything else.
 641 address ShenandoahBarrierSetAssembler::generate_shenandoah_lrb(StubCodeGenerator* cgen) {
 642 
 643   __ align(6);
 644   StubCodeMark mark(cgen, "StubRoutines", "shenandoah_lrb");
 645   address start = __ pc();
 646 
 647   Label work, call_rt;
 648   gen_cset_check(cgen->assembler(), r0, rscratch1, rscratch2, work);
 649   __ ret(lr);
 650 
 651   __ bind(work);
 652   gen_resolved_check(cgen->assembler(), r0, rscratch1, rscratch2, call_rt);
 653   __ ret(lr);
 654 
 655   __ bind(call_rt);
 656   __ enter(); // required for proper stackwalking of RuntimeStub frame
 657 
 658   __ push_call_clobbered_registers();
 659 
 660   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier));
 661   __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral);
 662   __ mov(rscratch1, r0);
 663   __ pop_call_clobbered_registers();
 664   __ mov(r0, rscratch1);
 665 
 666   __ leave(); // required for proper stackwalking of RuntimeStub frame
 667   __ ret(lr);
 668 
 669   return start;
 670 }
 671 
 672 #undef __
 673 
 674 void ShenandoahBarrierSetAssembler::barrier_stubs_init() {
 675   if (ShenandoahLoadRefBarrier) {
 676     int stub_code_size = 2048;
 677     ResourceMark rm;
 678     BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size);
 679     CodeBuffer buf(bb);
 680     StubCodeGenerator cgen(&buf);
 681     _shenandoah_lrb = generate_shenandoah_lrb(&cgen);
 682   }
 683 }