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/shared/barrierSet.hpp"
28 #include "gc/shared/cardTable.hpp"
29 #include "gc/shared/cardTableBarrierSet.hpp"
30 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
31 #include "gc/shared/collectedHeap.hpp"
32 #include "runtime/globals.hpp"
33 
34 #define __ masm->
35 
36 #ifdef PRODUCT
37 #define BLOCK_COMMENT(str) /* nothing */
38 #else
39 #define BLOCK_COMMENT(str) __ block_comment(str)
40 #endif
41 
42 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
43 
gen_write_ref_array_post_barrier(MacroAssembler * masm,DecoratorSet decorators,Register addr,Register count,Register tmp)44 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
45                                                                     Register addr, Register count, Register tmp) {
46   BLOCK_COMMENT("CardTablePostBarrier");
47   BarrierSet* bs = BarrierSet::barrier_set();
48   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
49   CardTable* ct = ctbs->card_table();
50 
51   Label L_cardtable_loop, L_done;
52 
53   __ cbz_32(count, L_done); // zero count - nothing to do
54 
55   __ add_ptr_scaled_int32(count, addr, count, LogBytesPerHeapOop);
56   __ sub(count, count, BytesPerHeapOop);                            // last addr
57 
58   __ logical_shift_right(addr, addr, CardTable::card_shift);
59   __ logical_shift_right(count, count, CardTable::card_shift);
60   __ sub(count, count, addr); // nb of cards
61 
62   // warning: Rthread has not been preserved
63   __ mov_address(tmp, (address) ct->byte_map_base());
64   __ add(addr,tmp, addr);
65 
66   Register zero = __ zero_register(tmp);
67 
68   __ BIND(L_cardtable_loop);
69   __ strb(zero, Address(addr, 1, post_indexed));
70   __ subs(count, count, 1);
71   __ b(L_cardtable_loop, ge);
72   __ BIND(L_done);
73 }
74 
oop_store_at(MacroAssembler * masm,DecoratorSet decorators,BasicType type,Address obj,Register new_val,Register tmp1,Register tmp2,Register tmp3,bool is_null)75 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
76                                              Address obj, Register new_val, Register tmp1, Register tmp2, Register tmp3, bool is_null) {
77   bool is_array = (decorators & IS_ARRAY) != 0;
78   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
79   bool precise = is_array || on_anonymous;
80 
81   if (is_null) {
82     BarrierSetAssembler::store_at(masm, decorators, type, obj, new_val, tmp1, tmp2, tmp3, true);
83   } else {
84     assert (!precise || (obj.index() == noreg && obj.disp() == 0),
85             "store check address should be calculated beforehand");
86 
87     store_check_part1(masm, tmp1);
88     BarrierSetAssembler::store_at(masm, decorators, type, obj, new_val, tmp1, tmp2, tmp3, false);
89     new_val = noreg;
90     store_check_part2(masm, obj.base(), tmp1, tmp2);
91   }
92 }
93 
94 // The 1st part of the store check.
95 // Sets card_table_base register.
store_check_part1(MacroAssembler * masm,Register card_table_base)96 void CardTableBarrierSetAssembler::store_check_part1(MacroAssembler* masm, Register card_table_base) {
97   // Check barrier set type (should be card table) and element size
98   BarrierSet* bs = BarrierSet::barrier_set();
99   assert(bs->kind() == BarrierSet::CardTableBarrierSet,
100          "Wrong barrier set kind");
101 
102   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
103   CardTable* ct = ctbs->card_table();
104 
105   // Load card table base address.
106 
107   /* Performance note.
108 
109      There is an alternative way of loading card table base address
110      from thread descriptor, which may look more efficient:
111 
112      ldr(card_table_base, Address(Rthread, JavaThread::card_table_base_offset()));
113 
114      However, performance measurements of micro benchmarks and specJVM98
115      showed that loading of card table base from thread descriptor is
116      7-18% slower compared to loading of literal embedded into the code.
117      Possible cause is a cache miss (card table base address resides in a
118      rarely accessed area of thread descriptor).
119   */
120   __ mov_address(card_table_base, (address)ct->byte_map_base());
121 }
122 
123 // The 2nd part of the store check.
store_check_part2(MacroAssembler * masm,Register obj,Register card_table_base,Register tmp)124 void CardTableBarrierSetAssembler::store_check_part2(MacroAssembler* masm, Register obj, Register card_table_base, Register tmp) {
125   assert_different_registers(obj, card_table_base, tmp);
126 
127   BarrierSet* bs = BarrierSet::barrier_set();
128   assert(bs->kind() == BarrierSet::CardTableBarrierSet,
129          "Wrong barrier set kind");
130 
131   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
132   CardTable* ct = ctbs->card_table();
133 
134   assert(CardTable::dirty_card_val() == 0, "Dirty card value must be 0 due to optimizations.");
135   Address card_table_addr(card_table_base, obj, lsr, CardTable::card_shift);
136 
137   if (UseCondCardMark) {
138     if (ct->scanned_concurrently()) {
139       __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad), noreg);
140     }
141     Label already_dirty;
142 
143     __ ldrb(tmp, card_table_addr);
144     __ cbz(tmp, already_dirty);
145 
146     set_card(masm, card_table_base, card_table_addr, tmp);
147     __ bind(already_dirty);
148 
149   } else {
150     if (ct->scanned_concurrently()) {
151       __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreStore), noreg);
152     }
153     set_card(masm, card_table_base, card_table_addr, tmp);
154   }
155 }
156 
set_card(MacroAssembler * masm,Register card_table_base,Address card_table_addr,Register tmp)157 void CardTableBarrierSetAssembler::set_card(MacroAssembler* masm, Register card_table_base, Address card_table_addr, Register tmp) {
158   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(BarrierSet::barrier_set());
159   CardTable* ct = ctbs->card_table();
160   if ((((uintptr_t)ct->byte_map_base() & 0xff) == 0)) {
161     // Card table is aligned so the lowest byte of the table address base is zero.
162     // This works only if the code is not saved for later use, possibly
163     // in a context where the base would no longer be aligned.
164     __ strb(card_table_base, card_table_addr);
165   } else {
166     __ mov(tmp, 0);
167     __ strb(tmp, card_table_addr);
168   }
169 }
170