1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. and/or its affiliates.
   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/brooksPointer.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  27 
  28 #include "asm/macroAssembler.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 
  31 #define __ masm->
  32 
  33 #ifndef CC_INTERP
  34 
  35 void ShenandoahBarrierSet::interpreter_read_barrier(MacroAssembler* masm, Register dst) {
  36   if (ShenandoahReadBarrier) {
  37     interpreter_read_barrier_impl(masm, dst);
  38   }
  39 }
  40 
  41 void ShenandoahBarrierSet::interpreter_read_barrier_impl(MacroAssembler* masm, Register dst) {
  42   Label is_null;
  43   __ testptr(dst, dst);
  44   __ jcc(Assembler::zero, is_null);
  45   interpreter_read_barrier_not_null_impl(masm, dst);
  46   __ bind(is_null);
  47 }
  48 
  49 void ShenandoahBarrierSet::interpreter_read_barrier_not_null(MacroAssembler* masm, Register dst) {
  50   if (ShenandoahReadBarrier) {
  51     interpreter_read_barrier_not_null_impl(masm, dst);
  52   }
  53 }
  54 
  55 void ShenandoahBarrierSet::interpreter_read_barrier_not_null_impl(MacroAssembler* masm, Register dst) {
  56   __ movptr(dst, Address(dst, BrooksPointer::byte_offset()));
  57 }
  58 
  59 void ShenandoahBarrierSet::interpreter_write_barrier(MacroAssembler* masm, Register dst) {
  60   if (ShenandoahWriteBarrier) {
  61     interpreter_write_barrier_impl(masm, dst);
  62   }
  63 }
  64 
  65 void ShenandoahBarrierSet::interpreter_write_barrier_impl(MacroAssembler* masm, Register dst) {
  66 
  67 #ifdef _LP64
  68   assert(dst != rscratch1, "different regs");
  69 
  70   Label done;
  71 
  72   Address evacuation_in_progress = Address(r15_thread, in_bytes(JavaThread::evacuation_in_progress_offset()));
  73 
  74   __ cmpb(evacuation_in_progress, 0);
  75 
  76   // Now check if evacuation is in progress.
  77   interpreter_read_barrier_not_null(masm, dst);
  78 
  79   __ jcc(Assembler::equal, done);
  80   __ push(rscratch1);
  81   __ push(rscratch2);
  82 
  83   __ movptr(rscratch1, dst);
  84   __ shrptr(rscratch1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
  85   __ movptr(rscratch2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
  86   __ movbool(rscratch2, Address(rscratch2, rscratch1, Address::times_1));
  87   __ testb(rscratch2, 0x1);
  88 
  89   __ pop(rscratch2);
  90   __ pop(rscratch1);
  91 
  92   __ jcc(Assembler::zero, done);
  93 
  94   __ push(rscratch1);
  95 
  96   // Save possibly live regs.
  97   if (dst != rax) {
  98     __ push(rax);
  99   }
 100   if (dst != rbx) {
 101     __ push(rbx);
 102   }
 103   if (dst != rcx) {
 104     __ push(rcx);
 105   }
 106   if (dst != rdx) {
 107     __ push(rdx);
 108   }
 109   if (dst != c_rarg1) {
 110     __ push(c_rarg1);
 111   }
 112 
 113   __ subptr(rsp, 2 * wordSize);
 114   __ movdbl(Address(rsp, 0), xmm0);
 115 
 116   // Call into runtime
 117   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahBarrierSet::write_barrier_IRT), dst);
 118   __ mov(rscratch1, rax);
 119 
 120   // Restore possibly live regs.
 121   __ movdbl(xmm0, Address(rsp, 0));
 122   __ addptr(rsp, 2 * Interpreter::stackElementSize);
 123 
 124   if (dst != c_rarg1) {
 125     __ pop(c_rarg1);
 126   }
 127   if (dst != rdx) {
 128     __ pop(rdx);
 129   }
 130   if (dst != rcx) {
 131     __ pop(rcx);
 132   }
 133   if (dst != rbx) {
 134     __ pop(rbx);
 135   }
 136   if (dst != rax) {
 137     __ pop(rax);
 138   }
 139 
 140   // Move result into dst reg.
 141   __ mov(dst, rscratch1);
 142 
 143   __ pop(rscratch1);
 144 
 145   __ bind(done);
 146 #else
 147   Unimplemented();
 148 #endif
 149 }
 150 
 151 void ShenandoahBarrierSet::interpreter_storeval_barrier(MacroAssembler* masm, Register dst) {
 152   if (ShenandoahStoreValWriteBarrier) {
 153     Label is_null;
 154     __ testptr(dst, dst);
 155     __ jcc(Assembler::zero, is_null);
 156     interpreter_write_barrier_impl(masm, dst);
 157     __ bind(is_null);
 158   }
 159   if (ShenandoahStoreValReadBarrier) {
 160     interpreter_read_barrier_impl(masm, dst);
 161   }
 162 }
 163 
 164 void ShenandoahBarrierSet::asm_acmp_barrier(MacroAssembler* masm, Register op1, Register op2) {
 165   if (ShenandoahAcmpBarrier) {
 166     Label done;
 167     __ jccb(Assembler::equal, done);
 168     interpreter_read_barrier(masm, op1);
 169     interpreter_read_barrier(masm, op2);
 170     __ cmpptr(op1, op2);
 171     __ bind(done);
 172   }
 173 }
 174 
 175 void ShenandoahHeap::compile_prepare_oop(MacroAssembler* masm, Register obj) {
 176 #ifdef _LP64
 177   __ incrementq(obj, BrooksPointer::byte_size());
 178 #else
 179   __ incrementl(obj, BrooksPointer::byte_size());
 180 #endif
 181   __ movptr(Address(obj, BrooksPointer::byte_offset()), obj);
 182 }
 183 #endif