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