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 "c1/c1_MacroAssembler.hpp"
  26 #include "c1/c1_LIRAssembler.hpp"
  27 #include "macroAssembler_aarch64.hpp"
  28 #include "shenandoahBarrierSetAssembler_aarch64.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahBarrierSet.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahBarrierSetC1.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahForwarding.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  33 #include "gc_implementation/shenandoah/shenandoahRuntime.hpp"
  34 #include "runtime/stubCodeGenerator.hpp"
  35 #include "runtime/thread.hpp"
  36 
  37 ShenandoahBarrierSetAssembler* ShenandoahBarrierSetAssembler::bsasm() {
  38   return ShenandoahBarrierSet::barrier_set()->bsasm();
  39 }
  40 
  41 #define __ masm->
  42 
  43 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) {
  44   assert(ShenandoahCASBarrier, "should be enabled");
  45   Label is_null;
  46   __ cbz(dst, is_null);
  47   resolve_forward_pointer_not_null(masm, dst, tmp);
  48   __ bind(is_null);
  49 }
  50 
  51 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitely
  52 // passed in.
  53 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) {
  54   assert(ShenandoahCASBarrier || ShenandoahLoadRefBarrier, "should be enabled");
  55   // The below loads the mark word, checks if the lowest two bits are
  56   // set, and if so, clear the lowest two bits and copy the result
  57   // to dst. Otherwise it leaves dst alone.
  58   // Implementing this is surprisingly awkward. I do it here by:
  59   // - Inverting the mark word
  60   // - Test lowest two bits == 0
  61   // - If so, set the lowest two bits
  62   // - Invert the result back, and copy to dst
  63 
  64   bool borrow_reg = (tmp == noreg);
  65   if (borrow_reg) {
  66     // No free registers available. Make one useful.
  67     tmp = rscratch1;
  68     if (tmp == dst) {
  69       tmp = rscratch2;
  70     }
  71     __ push(RegSet::of(tmp), sp);
  72   }
  73 
  74   assert_different_registers(tmp, dst);
  75 
  76   Label done;
  77   __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes()));
  78   __ eon(tmp, tmp, zr);
  79   __ ands(zr, tmp, markOopDesc::lock_mask_in_place);
  80   __ br(Assembler::NE, done);
  81   __ orr(tmp, tmp, markOopDesc::marked_value);
  82   __ eon(dst, tmp, zr);
  83   __ bind(done);
  84 
  85   if (borrow_reg) {
  86     __ pop(RegSet::of(tmp), sp);
  87   }
  88 }
  89 
  90 void ShenandoahBarrierSetAssembler::load_reference_barrier_not_null(MacroAssembler* masm, Register dst) {
  91   assert(ShenandoahLoadRefBarrier, "Should be enabled");
  92   assert(dst != rscratch2, "need rscratch2");
  93 
  94   Label done;
  95   __ enter();
  96   Address gc_state(rthread, in_bytes(JavaThread::gc_state_offset()));
  97   __ ldrb(rscratch2, gc_state);
  98 
  99   // Check for heap stability
 100   __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
 101 
 102   RegSet to_save = RegSet::of(r0);
 103   if (dst != r0) {
 104     __ push(to_save, sp);
 105     __ mov(r0, dst);
 106   }
 107 
 108   __ push_call_clobbered_registers();
 109   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_interpreter), r0);
 110   __ mov(rscratch1, r0);
 111   __ pop_call_clobbered_registers();
 112   __ mov(r0, rscratch1);
 113 
 114   if (dst != r0) {
 115     __ mov(dst, r0);
 116     __ pop(to_save, sp);
 117   }
 118 
 119   __ bind(done);
 120   __ leave();
 121 }
 122 
 123 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst) {
 124   if (ShenandoahLoadRefBarrier) {
 125     Label is_null;
 126     __ cbz(dst, is_null);
 127     load_reference_barrier_not_null(masm, dst);
 128     __ bind(is_null);
 129   }
 130 }
 131 
 132 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val,
 133                                                 bool acquire, bool release, bool weak, bool is_cae,
 134                                                 Register result) {
 135 
 136   Register tmp1 = rscratch1;
 137   Register tmp2 = rscratch2;
 138   bool is_narrow = UseCompressedOops;
 139   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 140 
 141   assert_different_registers(addr, expected, new_val, tmp1, tmp2);
 142 
 143   Label retry, done, fail;
 144 
 145   // CAS, using LL/SC pair.
 146   __ bind(retry);
 147   __ load_exclusive(tmp1, addr, size, acquire);
 148   if (is_narrow) {
 149     __ cmpw(tmp1, expected);
 150   } else {
 151     __ cmp(tmp1, expected);
 152   }
 153   __ br(Assembler::NE, fail);
 154   __ store_exclusive(tmp2, new_val, addr, size, release);
 155   if (weak) {
 156     __ cmpw(tmp2, 0u); // If the store fails, return NE to our caller
 157   } else {
 158     __ cbnzw(tmp2, retry);
 159   }
 160   __ b(done);
 161 
 162   __ bind(fail);
 163   // Check if rb(expected)==rb(tmp1)
 164   // Shuffle registers so that we have memory value ready for next expected.
 165   __ mov(tmp2, expected);
 166   __ mov(expected, tmp1);
 167   if (is_narrow) {
 168     __ decode_heap_oop(tmp1, tmp1);
 169     __ decode_heap_oop(tmp2, tmp2);
 170   }
 171   resolve_forward_pointer(masm, tmp1);
 172   resolve_forward_pointer(masm, tmp2);
 173   __ cmp(tmp1, tmp2);
 174   // Retry with expected now being the value we just loaded from addr.
 175   __ br(Assembler::EQ, retry);
 176   if (is_cae && is_narrow) {
 177     // For cmp-and-exchange and narrow oops, we need to restore
 178     // the compressed old-value. We moved it to 'expected' a few lines up.
 179     __ mov(result, expected);
 180   }
 181   __ bind(done);
 182 
 183   if (is_cae) {
 184     __ mov(result, tmp1);
 185   } else {
 186     __ cset(result, Assembler::EQ);
 187   }
 188 }
 189 
 190 #undef __
 191 
 192 #ifdef COMPILER1
 193 
 194 #define __ ce->masm()->
 195 
 196 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
 197 
 198   Register obj = stub->obj()->as_register();
 199   Register res = stub->result()->as_register();
 200 
 201   Label done;
 202 
 203   __ bind(*stub->entry());
 204 
 205   if (res != obj) {
 206     __ mov(res, obj);
 207   }
 208   // Check for null.
 209   if (stub->needs_null_check()) {
 210     __ cbz(res, done);
 211   }
 212 
 213   load_reference_barrier_not_null(ce->masm(), res);
 214 
 215   __ bind(done);
 216   __ b(*stub->continuation());
 217 }
 218 
 219 #undef __
 220 
 221 #endif // COMPILER1