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