1 /*
  2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "asm/macroAssembler.inline.hpp"
 27 #include "gc/g1/g1BarrierSet.hpp"
 28 #include "gc/g1/g1BarrierSetCodeGen.hpp"
 29 #include "gc/g1/g1CardTable.hpp"
 30 #include "gc/g1/heapRegion.hpp"
 31 #include "gc/shared/collectedHeap.hpp"
 32 #include "interpreter/interp_masm.hpp"
 33 #include "runtime/sharedRuntime.hpp"
 34 #include "runtime/thread.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 #define __ masm->
 38 
 39 void G1BarrierSetCodeGen::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators, Register addr, Register count) {
 40   bool dest_uninitialized = (decorators & AS_DEST_NOT_INITIALIZED) != 0;
 41 
 42   if (!dest_uninitialized) {
 43     Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
 44 #ifndef _LP64
 45     __ push(thread);
 46     __ get_thread(thread);
 47 #endif
 48 
 49     Label filtered;
 50     Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 51                                          SATBMarkQueue::byte_offset_of_active()));
 52     // Is marking active?
 53     if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 54       __ cmpl(in_progress, 0);
 55     } else {
 56       assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 57       __ cmpb(in_progress, 0);
 58     }
 59 
 60     NOT_LP64(__ pop(thread);)
 61 
 62     __ jcc(Assembler::equal, filtered);
 63 
 64     __ pusha();                      // push registers
 65 #ifdef _LP64
 66     if (count == c_rarg0) {
 67       if (addr == c_rarg1) {
 68         // exactly backwards!!
 69         __ xchgptr(c_rarg1, c_rarg0);
 70       } else {
 71         __ movptr(c_rarg1, count);
 72         __ movptr(c_rarg0, addr);
 73       }
 74     } else {
 75       __ movptr(c_rarg0, addr);
 76       __ movptr(c_rarg1, count);
 77     }
 78     if (UseCompressedOops) {
 79       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_narrow_oop_entry), 2);
 80     } else {
 81       __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_oop_entry), 2);
 82     }
 83 #else
 84     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_oop_entry),
 85                     addr, count);
 86 #endif
 87     __ popa();
 88 
 89     __ bind(filtered);
 90   }
 91 }
 92 
 93 void G1BarrierSetCodeGen::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators, Register addr, Register count, Register tmp) {
 94   __ pusha();             // push registers (overkill)
 95 #ifdef _LP64
 96   if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
 97     assert_different_registers(c_rarg1, addr);
 98     __ mov(c_rarg1, count);
 99     __ mov(c_rarg0, addr);
100   } else {
101     assert_different_registers(c_rarg0, count);
102     __ mov(c_rarg0, addr);
103     __ mov(c_rarg1, count);
104   }
105   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_post_entry), 2);
106 #else
107   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_post_entry),
108                   addr, count);
109 #endif
110   __ popa();
111 }