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