1 /*
2 * Copyright (c) 2018, 2021, 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/gc_globals.hpp"
32
33 #define __ masm->
34
35 #ifdef PRODUCT
36 #define BLOCK_COMMENT(str) /* nothing */
37 #else
38 #define BLOCK_COMMENT(str) __ block_comment(str)
39 #endif
40
41 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
42
43 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
44
45 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
46 Register addr, Register count, Register tmp) {
47 BarrierSet *bs = BarrierSet::barrier_set();
48 CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
49 CardTable* ct = ctbs->card_table();
50 intptr_t disp = (intptr_t) ct->byte_map_base();
51 SHENANDOAHGC_ONLY(assert(!UseShenandoahGC, "Shenandoah byte_map_base is not constant.");)
52
53 Label L_loop, L_done;
54 const Register end = count;
55 assert_different_registers(addr, end);
56
57 __ testl(count, count);
58 __ jcc(Assembler::zero, L_done); // zero count - nothing to do
59
60
61 #ifdef _LP64
62 __ leaq(end, Address(addr, count, TIMES_OOP, 0)); // end == addr+count*oop_size
63 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
64 __ shrptr(addr, CardTable::card_shift());
65 __ shrptr(end, CardTable::card_shift());
66 __ subptr(end, addr); // end --> cards count
67
68 __ mov64(tmp, disp);
69 __ addptr(addr, tmp);
70 __ BIND(L_loop);
71 __ movb(Address(addr, count, Address::times_1), 0);
72 __ decrement(count);
73 __ jcc(Assembler::greaterEqual, L_loop);
74 #else
75 __ lea(end, Address(addr, count, Address::times_ptr, -wordSize));
76 __ shrptr(addr, CardTable::card_shift());
77 __ shrptr(end, CardTable::card_shift());
78 __ subptr(end, addr); // end --> count
79 __ BIND(L_loop);
80 Address cardtable(addr, count, Address::times_1, disp);
81 __ movb(cardtable, 0);
82 __ decrement(count);
83 __ jcc(Assembler::greaterEqual, L_loop);
84 #endif
85
86 __ BIND(L_done);
87 }
88
89 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj, Address dst) {
90 // Does a store check for the oop in register obj. The content of
91 // register obj is destroyed afterwards.
92 BarrierSet* bs = BarrierSet::barrier_set();
93
94 CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
95 CardTable* ct = ctbs->card_table();
96
97 __ shrptr(obj, CardTable::card_shift());
98
99 Address card_addr;
100
101 // The calculation for byte_map_base is as follows:
102 // byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift);
103 // So this essentially converts an address to a displacement and it will
104 // never need to be relocated. On 64bit however the value may be too
105 // large for a 32bit displacement.
106 intptr_t byte_map_base = (intptr_t)ct->byte_map_base();
107 if (__ is_simm32(byte_map_base)) {
108 card_addr = Address(noreg, obj, Address::times_1, byte_map_base);
109 } else {
110 // By doing it as an ExternalAddress 'byte_map_base' could be converted to a rip-relative
111 // displacement and done in a single instruction given favorable mapping and a
112 // smarter version of as_Address. However, 'ExternalAddress' generates a relocation
113 // entry and that entry is not properly handled by the relocation code.
114 AddressLiteral cardtable((address)byte_map_base, relocInfo::none);
115 Address index(noreg, obj, Address::times_1);
116 card_addr = __ as_Address(ArrayAddress(cardtable, index), rscratch1);
117 }
118
119 int dirty = CardTable::dirty_card_val();
120 if (UseCondCardMark) {
121 Label L_already_dirty;
122 __ cmpb(card_addr, dirty);
123 __ jccb(Assembler::equal, L_already_dirty);
124 __ movb(card_addr, dirty);
125 __ bind(L_already_dirty);
126 } else {
127 __ movb(card_addr, dirty);
128 }
129 }
130
131 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
132 Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
133 bool in_heap = (decorators & IN_HEAP) != 0;
134
135 bool is_array = (decorators & IS_ARRAY) != 0;
136 bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
137 bool precise = is_array || on_anonymous;
138
139 bool needs_post_barrier = val != noreg && in_heap;
140
141 BarrierSetAssembler::store_at(masm, decorators, type, dst, val, noreg, noreg, noreg);
142 if (needs_post_barrier) {
143 // flatten object address if needed
144 if (!precise || (dst.index() == noreg && dst.disp() == 0)) {
145 store_check(masm, dst.base(), dst);
146 } else {
147 __ lea(tmp1, dst);
148 store_check(masm, tmp1, dst);
149 }
150 }
151 }