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