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