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_implementation/shenandoah/brooksPointer.hpp"
  26 #include "gc_implementation/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 
  38     Label is_null;
  39     __ testptr(dst, dst);
  40     __ jcc(Assembler::zero, is_null);
  41     interpreter_read_barrier_not_null(masm, dst);
  42     __ bind(is_null);
  43   }
  44 }
  45 
  46 void ShenandoahBarrierSet::interpreter_read_barrier_not_null(MacroAssembler* masm, Register dst) {
  47   if (ShenandoahReadBarrier) {
  48     __ movptr(dst, Address(dst, BrooksPointer::byte_offset()));
  49   }
  50 }
  51 
  52 void ShenandoahBarrierSet::interpreter_write_barrier(MacroAssembler* masm, Register dst) {
  53 
  54   if (! ShenandoahWriteBarrier) {
  55     return interpreter_read_barrier(masm, dst);
  56   }
  57 
  58 #ifdef _LP64
  59   assert(dst != rscratch1, "different regs");
  60 
  61   Label done;
  62 
  63   Address gc_state(r15_thread, in_bytes(JavaThread::gc_state_offset()));
  64 
  65   // Now check if evacuation is in progress.
  66   __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION);
  67   __ jcc(Assembler::zero, done);
  68 
  69   // Heap is unstable, need to perform the read-barrier even if WB is inactive
  70   interpreter_read_barrier_not_null(masm, dst);
  71 
  72   __ testb(gc_state, ShenandoahHeap::EVACUATION);
  73   __ jcc(Assembler::zero, done);
  74   __ push(rscratch1);
  75   __ push(rscratch2);
  76 
  77   __ movptr(rscratch1, dst);
  78   __ shrptr(rscratch1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
  79   __ movptr(rscratch2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
  80   __ movbool(rscratch2, Address(rscratch2, rscratch1, Address::times_1));
  81   __ testb(rscratch2, 0x1);
  82 
  83   __ pop(rscratch2);
  84   __ pop(rscratch1);
  85 
  86   __ jcc(Assembler::zero, done);
  87 
  88   __ push(rscratch1);
  89 
  90   // Save possibly live regs.
  91   if (dst != rax) {
  92     __ push(rax);
  93   }
  94   if (dst != rbx) {
  95     __ push(rbx);
  96   }
  97   if (dst != rcx) {
  98     __ push(rcx);
  99   }
 100   if (dst != rdx) {
 101     __ push(rdx);
 102   }
 103   if (dst != c_rarg1) {
 104     __ push(c_rarg1);
 105   }
 106 
 107   __ subptr(rsp, 2 * wordSize);
 108   __ movdbl(Address(rsp, 0), xmm0);
 109 
 110   // Call into runtime
 111   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahBarrierSet::write_barrier_IRT), dst);
 112   __ mov(rscratch1, rax);
 113 
 114   // Restore possibly live regs.
 115   __ movdbl(xmm0, Address(rsp, 0));
 116   __ addptr(rsp, 2 * Interpreter::stackElementSize);
 117 
 118   if (dst != c_rarg1) {
 119     __ pop(c_rarg1);
 120   }
 121   if (dst != rdx) {
 122     __ pop(rdx);
 123   }
 124   if (dst != rcx) {
 125     __ pop(rcx);
 126   }
 127   if (dst != rbx) {
 128     __ pop(rbx);
 129   }
 130   if (dst != rax) {
 131     __ pop(rax);
 132   }
 133 
 134   // Move result into dst reg.
 135   __ mov(dst, rscratch1);
 136 
 137   __ pop(rscratch1);
 138 
 139   __ bind(done);
 140 #else
 141   Unimplemented();
 142 #endif
 143 }
 144 
 145 void ShenandoahBarrierSet::asm_acmp_barrier(MacroAssembler* masm, Register op1, Register op2) {
 146   assert (UseShenandoahGC, "Should be enabled");
 147   if (ShenandoahAcmpBarrier) {
 148     Label done;
 149     __ jccb(Assembler::equal, done);
 150     interpreter_read_barrier(masm, op1);
 151     interpreter_read_barrier(masm, op2);
 152     __ cmpptr(op1, op2);
 153     __ bind(done);
 154   }
 155 }
 156 
 157 void ShenandoahHeap::compile_prepare_oop(MacroAssembler* masm, Register obj) {
 158 #ifdef _LP64
 159   __ incrementq(obj, BrooksPointer::byte_size());
 160 #else
 161   __ incrementl(obj, BrooksPointer::byte_size());
 162 #endif
 163   __ movptr(Address(obj, BrooksPointer::byte_offset()), obj);
 164 }
 165 #endif