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/g1BarrierSetAssembler.hpp"
 29 #include "gc/g1/g1CardTable.hpp"
 30 #include "gc/g1/heapRegion.hpp"
 31 #include "interpreter/interp_masm.hpp"
 32 #include "runtime/sharedRuntime.hpp"
 33 #include "runtime/thread.hpp"
 34 #include "utilities/macros.hpp"
 35 
 36 #define __ masm->
 37 
 38 #ifdef PRODUCT
 39 #define BLOCK_COMMENT(str) /* nothing */
 40 #else
 41 #define BLOCK_COMMENT(str) __ block_comment(str)
 42 #endif
 43 
 44 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
 45 
 46 void G1BarrierSetAssembler::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators,
 47                                                             Register addr, Register count, int callee_saved_regs) {
 48   bool dest_uninitialized = (decorators & AS_DEST_NOT_INITIALIZED) != 0;
 49   if (!dest_uninitialized) {
 50     assert( addr->encoding() < callee_saved_regs, "addr must be saved");
 51     assert(count->encoding() < callee_saved_regs, "count must be saved");
 52 
 53     BLOCK_COMMENT("PreBarrier");
 54 
 55 #ifdef AARCH64
 56     callee_saved_regs = align_up(callee_saved_regs, 2);
 57     for (int i = 0; i < callee_saved_regs; i += 2) {
 58       __ raw_push(as_Register(i), as_Register(i+1));
 59     }
 60 #else
 61     RegisterSet saved_regs = RegisterSet(R0, as_Register(callee_saved_regs-1));
 62     __ push(saved_regs | R9ifScratched);
 63 #endif // AARCH64
 64 
 65     if (addr != R0) {
 66       assert_different_registers(count, R0);
 67       __ mov(R0, addr);
 68     }
 69 #ifdef AARCH64
 70     __ zero_extend(R1, count, 32); // G1BarrierSet::write_ref_array_pre_*_entry takes size_t
 71 #else
 72     if (count != R1) {
 73       __ mov(R1, count);
 74     }
 75 #endif // AARCH64
 76 
 77     if (UseCompressedOops) {
 78       __ call(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_narrow_oop_entry));
 79     } else {
 80       __ call(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_pre_oop_entry));
 81     }
 82 
 83 #ifdef AARCH64
 84     for (int i = callee_saved_regs - 2; i >= 0; i -= 2) {
 85       __ raw_pop(as_Register(i), as_Register(i+1));
 86     }
 87 #else
 88     __ pop(saved_regs | R9ifScratched);
 89 #endif // AARCH64
 90   }
 91 }
 92 
 93 void G1BarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 94                                                              Register addr, Register count, Register tmp) {
 95 
 96   BLOCK_COMMENT("G1PostBarrier");
 97   if (addr != R0) {
 98     assert_different_registers(count, R0);
 99     __ mov(R0, addr);
100   }
101 #ifdef AARCH64
102   __ zero_extend(R1, count, 32); // G1BarrierSet::write_ref_array_post_entry takes size_t
103 #else
104   if (count != R1) {
105     __ mov(R1, count);
106   }
107 #if R9_IS_SCRATCHED
108   // Safer to save R9 here since callers may have been written
109   // assuming R9 survives. This is suboptimal but is not in
110   // general worth optimizing for the few platforms where R9
111   // is scratched. Note that the optimization might not be to
112   // difficult for this particular call site.
113   __ push(R9);
114 #endif // !R9_IS_SCRATCHED
115 #endif // !AARCH64
116   __ call(CAST_FROM_FN_PTR(address, G1BarrierSet::write_ref_array_post_entry));
117 #ifndef AARCH64
118 #if R9_IS_SCRATCHED
119   __ pop(R9);
120 #endif // !R9_IS_SCRATCHED
121 #endif // !AARCH64
122 }