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, markWord::lock_mask_in_place);
 246   __ br(Assembler::NE, done);
 247   __ orr(tmp, tmp, markWord::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   __ blr(lr);
 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::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
 419                                                                   Register obj, Register tmp, Label& slowpath) {
 420   Label done;
 421   // Resolve jobject
 422   BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
 423 
 424   // Check for null.
 425   __ cbz(obj, done);
 426 
 427   assert(obj != rscratch2, "need rscratch2");
 428   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 429   __ ldrb(rscratch2, gc_state);
 430 
 431   // Check for heap in evacuation phase
 432   __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
 433 
 434   __ bind(done);
 435 }
 436 
 437 
 438 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val,
 439                                                 bool acquire, bool release, bool weak, bool is_cae,
 440                                                 Register result) {
 441   Register tmp1 = rscratch1;
 442   Register tmp2 = rscratch2;
 443   bool is_narrow = UseCompressedOops;
 444   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 445 
 446   assert_different_registers(addr, expected, new_val, tmp1, tmp2);
 447 
 448   Label retry, done, fail;
 449 
 450   // CAS, using LL/SC pair.
 451   __ bind(retry);
 452   __ load_exclusive(tmp1, addr, size, acquire);
 453   if (is_narrow) {
 454     __ cmpw(tmp1, expected);
 455   } else {
 456     __ cmp(tmp1, expected);
 457   }
 458   __ br(Assembler::NE, fail);
 459   __ store_exclusive(tmp2, new_val, addr, size, release);
 460   if (weak) {
 461     __ cmpw(tmp2, 0u); // If the store fails, return NE to our caller
 462   } else {
 463     __ cbnzw(tmp2, retry);
 464   }
 465   __ b(done);
 466 
 467  __  bind(fail);
 468   // Check if rb(expected)==rb(tmp1)
 469   // Shuffle registers so that we have memory value ready for next expected.
 470   __ mov(tmp2, expected);
 471   __ mov(expected, tmp1);
 472   if (is_narrow) {
 473     __ decode_heap_oop(tmp1, tmp1);
 474     __ decode_heap_oop(tmp2, tmp2);
 475   }
 476   resolve_forward_pointer(masm, tmp1);
 477   resolve_forward_pointer(masm, tmp2);
 478   __ cmp(tmp1, tmp2);
 479   // Retry with expected now being the value we just loaded from addr.
 480   __ br(Assembler::EQ, retry);
 481   if (is_cae && is_narrow) {
 482     // For cmp-and-exchange and narrow oops, we need to restore
 483     // the compressed old-value. We moved it to 'expected' a few lines up.
 484     __ mov(tmp1, expected);
 485   }
 486   __ bind(done);
 487 
 488   if (is_cae) {
 489     __ mov(result, tmp1);
 490   } else {
 491     __ cset(result, Assembler::EQ);
 492   }
 493 }
 494 
 495 #undef __
 496 
 497 #ifdef COMPILER1
 498 
 499 #define __ ce->masm()->
 500 
 501 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 502   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 503   // At this point we know that marking is in progress.
 504   // If do_load() is true then we have to emit the
 505   // load of the previous value; otherwise it has already
 506   // been loaded into _pre_val.
 507 
 508   __ bind(*stub->entry());
 509 
 510   assert(stub->pre_val()->is_register(), "Precondition.");
 511 
 512   Register pre_val_reg = stub->pre_val()->as_register();
 513 
 514   if (stub->do_load()) {
 515     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 516   }
 517   __ cbz(pre_val_reg, *stub->continuation());
 518   ce->store_parameter(stub->pre_val()->as_register(), 0);
 519   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
 520   __ b(*stub->continuation());
 521 }
 522 
 523 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
 524   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 525   __ bind(*stub->entry());
 526 
 527   Register obj = stub->obj()->as_register();
 528   Register res = stub->result()->as_register();
 529   Register addr = stub->addr()->as_register_lo();
 530   Register tmp1 = stub->tmp1()->as_register();
 531   Register tmp2 = stub->tmp2()->as_register();
 532 
 533   assert(res == r0, "result must arrive in r0");
 534 
 535   if (res != obj) {
 536     __ mov(res, obj);
 537   }
 538 
 539   // Check for null.
 540   __ cbz(res, *stub->continuation());
 541 
 542   // Check for object in cset.
 543   __ mov(tmp2, ShenandoahHeap::in_cset_fast_test_addr());
 544   __ lsr(tmp1, res, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 545   __ ldrb(tmp2, Address(tmp2, tmp1));
 546   __ cbz(tmp2, *stub->continuation());
 547 
 548   // Check if object is already forwarded.
 549   Label slow_path;
 550   __ ldr(tmp1, Address(res, oopDesc::mark_offset_in_bytes()));
 551   __ eon(tmp1, tmp1, zr);
 552   __ ands(zr, tmp1, markWord::lock_mask_in_place);
 553   __ br(Assembler::NE, slow_path);
 554 
 555   // Decode forwarded object.
 556   __ orr(tmp1, tmp1, markWord::marked_value);
 557   __ eon(res, tmp1, zr);
 558   __ b(*stub->continuation());
 559 
 560   __ bind(slow_path);
 561   ce->store_parameter(res, 0);
 562   ce->store_parameter(addr, 1);
 563   __ far_call(RuntimeAddress(bs->load_reference_barrier_rt_code_blob()->code_begin()));
 564 
 565   __ b(*stub->continuation());
 566 }
 567 
 568 #undef __
 569 
 570 #define __ sasm->
 571 
 572 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 573   __ prologue("shenandoah_pre_barrier", false);
 574 
 575   // arg0 : previous value of memory
 576 
 577   BarrierSet* bs = BarrierSet::barrier_set();
 578 
 579   const Register pre_val = r0;
 580   const Register thread = rthread;
 581   const Register tmp = rscratch1;
 582 
 583   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 584   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 585 
 586   Label done;
 587   Label runtime;
 588 
 589   // Is marking still active?
 590   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 591   __ ldrb(tmp, gc_state);
 592   __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 593   __ tst(tmp, rscratch2);
 594   __ br(Assembler::EQ, done);
 595 
 596   // Can we store original value in the thread's buffer?
 597   __ ldr(tmp, queue_index);
 598   __ cbz(tmp, runtime);
 599 
 600   __ sub(tmp, tmp, wordSize);
 601   __ str(tmp, queue_index);
 602   __ ldr(rscratch2, buffer);
 603   __ add(tmp, tmp, rscratch2);
 604   __ load_parameter(0, rscratch2);
 605   __ str(rscratch2, Address(tmp, 0));
 606   __ b(done);
 607 
 608   __ bind(runtime);
 609   __ push_call_clobbered_registers();
 610   __ load_parameter(0, pre_val);
 611   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 612   __ pop_call_clobbered_registers();
 613   __ bind(done);
 614 
 615   __ epilogue();
 616 }
 617 
 618 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm) {
 619   __ prologue("shenandoah_load_reference_barrier", false);
 620   // arg0 : object to be resolved
 621 
 622   __ push_call_clobbered_registers();
 623   __ load_parameter(0, r0);
 624   __ load_parameter(1, r1);
 625   if (UseCompressedOops) {
 626     __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_fixup_narrow));
 627   } else {
 628     __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_fixup));
 629   }
 630   __ blr(lr);
 631   __ mov(rscratch1, r0);
 632   __ pop_call_clobbered_registers();
 633   __ mov(r0, rscratch1);
 634 
 635   __ epilogue();
 636 }
 637 
 638 #undef __
 639 
 640 #endif // COMPILER1
 641 
 642 address ShenandoahBarrierSetAssembler::shenandoah_lrb() {
 643   assert(_shenandoah_lrb != NULL, "need load reference barrier stub");
 644   return _shenandoah_lrb;
 645 }
 646 
 647 #define __ cgen->assembler()->
 648 
 649 // Shenandoah load reference barrier.
 650 //
 651 // Input:
 652 //   r0: OOP to evacuate.  Not null.
 653 //
 654 // Output:
 655 //   r0: Pointer to evacuated OOP.
 656 //
 657 // Trash rscratch1, rscratch2.  Preserve everything else.
 658 address ShenandoahBarrierSetAssembler::generate_shenandoah_lrb(StubCodeGenerator* cgen) {
 659 
 660   __ align(6);
 661   StubCodeMark mark(cgen, "StubRoutines", "shenandoah_lrb");
 662   address start = __ pc();
 663 
 664   Label work, done;
 665   __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
 666   __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 667   __ ldrb(rscratch2, Address(rscratch2, rscratch1));
 668   __ tbnz(rscratch2, 0, work);
 669   __ ret(lr);
 670   __ bind(work);
 671 
 672   Label slow_path;
 673   __ ldr(rscratch1, Address(r0, oopDesc::mark_offset_in_bytes()));
 674   __ eon(rscratch1, rscratch1, zr);
 675   __ ands(zr, rscratch1, markWord::lock_mask_in_place);
 676   __ br(Assembler::NE, slow_path);
 677 
 678   // Decode forwarded object.
 679   __ orr(rscratch1, rscratch1, markWord::marked_value);
 680   __ eon(r0, rscratch1, zr);
 681   __ ret(lr);
 682 
 683   __ bind(slow_path);
 684   __ enter(); // required for proper stackwalking of RuntimeStub frame
 685 
 686   __ push_call_clobbered_registers();
 687 
 688   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier));
 689   __ blr(lr);
 690   __ mov(rscratch1, r0);
 691   __ pop_call_clobbered_registers();
 692   __ mov(r0, rscratch1);
 693 
 694   __ leave(); // required for proper stackwalking of RuntimeStub frame
 695   __ bind(done);
 696   __ ret(lr);
 697 
 698   return start;
 699 }
 700 
 701 #undef __
 702 
 703 void ShenandoahBarrierSetAssembler::barrier_stubs_init() {
 704   if (ShenandoahLoadRefBarrier) {
 705     int stub_code_size = 2048;
 706     ResourceMark rm;
 707     BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size);
 708     CodeBuffer buf(bb);
 709     StubCodeGenerator cgen(&buf);
 710     _shenandoah_lrb = generate_shenandoah_lrb(&cgen);
 711   }
 712 }