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