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