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