1 /*
2 * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2021, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
63 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) {
64 const int aligned_mask = BytesPerWord -1;
65 const int hdr_offset = oopDesc::mark_offset_in_bytes();
66 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2);
67 int null_check_offset = -1;
68
69 verify_oop(obj);
70
71 // save object being locked into the BasicObjectLock
72 str(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
73
74 null_check_offset = offset();
75
76 if (DiagnoseSyncOnValueBasedClasses != 0) {
77 load_klass(hdr, obj);
78 ldrw(hdr, Address(hdr, Klass::access_flags_offset()));
79 tstw(hdr, JVM_ACC_IS_VALUE_BASED_CLASS);
80 br(Assembler::NE, slow_case);
81 }
82
83 // Load object header
84 ldr(hdr, Address(obj, hdr_offset));
85 if (LockingMode == LM_LIGHTWEIGHT) {
86 lightweight_lock(obj, hdr, temp, rscratch2, slow_case);
87 } else if (LockingMode == LM_LEGACY) {
88 Label done;
89 // and mark it as unlocked
90 orr(hdr, hdr, markWord::unlocked_value);
91 // save unlocked object header into the displaced header location on the stack
92 str(hdr, Address(disp_hdr, 0));
93 // test if object header is still the same (i.e. unlocked), and if so, store the
94 // displaced header address in the object header - if it is not the same, get the
95 // object header instead
96 lea(rscratch2, Address(obj, hdr_offset));
97 cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/nullptr);
98 // if the object header was the same, we're done
99 // if the object header was not the same, it is now in the hdr register
100 // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
101 //
102 // 1) (hdr & aligned_mask) == 0
103 // 2) sp <= hdr
104 // 3) hdr <= sp + page_size
105 //
106 // these 3 tests can be done by evaluating the following expression:
107 //
108 // (hdr - sp) & (aligned_mask - page_size)
127
128 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) {
129 const int aligned_mask = BytesPerWord -1;
130 const int hdr_offset = oopDesc::mark_offset_in_bytes();
131 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2);
132 Label done;
133
134 if (LockingMode != LM_LIGHTWEIGHT) {
135 // load displaced header
136 ldr(hdr, Address(disp_hdr, 0));
137 // if the loaded hdr is null we had recursive locking
138 // if we had recursive locking, we are done
139 cbz(hdr, done);
140 }
141
142 // load object
143 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
144 verify_oop(obj);
145
146 if (LockingMode == LM_LIGHTWEIGHT) {
147 ldr(hdr, Address(obj, oopDesc::mark_offset_in_bytes()));
148 // We cannot use tbnz here, the target might be too far away and cannot
149 // be encoded.
150 tst(hdr, markWord::monitor_value);
151 br(Assembler::NE, slow_case);
152 lightweight_unlock(obj, hdr, temp, rscratch2, slow_case);
153 } else if (LockingMode == LM_LEGACY) {
154 // test if object header is pointing to the displaced header, and if so, restore
155 // the displaced header in the object - if the object header is not pointing to
156 // the displaced header, get the object header instead
157 // if the object header was not pointing to the displaced header,
158 // we do unlocking via runtime call
159 if (hdr_offset) {
160 lea(rscratch1, Address(obj, hdr_offset));
161 cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case);
162 } else {
163 cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case);
164 }
165 // done
166 bind(done);
167 }
168 decrement(Address(rthread, JavaThread::held_monitor_count_offset()));
169 }
170
171
172 // Defines obj, preserves var_size_in_bytes
173 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
174 if (UseTLAB) {
175 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
176 } else {
177 b(slow_case);
178 }
179 }
180
181 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
182 assert_different_registers(obj, klass, len);
183 // This assumes that all prototype bits fit in an int32_t
184 mov(t1, (int32_t)(intptr_t)markWord::prototype().value());
185 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
186
187 if (UseCompressedClassPointers) { // Take care not to kill klass
188 encode_klass_not_null(t1, klass);
189 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
190 } else {
191 str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
192 }
193
194 if (len->is_valid()) {
195 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
196 } else if (UseCompressedClassPointers) {
197 store_klass_gap(obj, zr);
198 }
199 }
200
201 // preserves obj, destroys len_in_bytes
202 //
203 // Scratch registers: t1 = r10, t2 = r11
204 //
205 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
206 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
207 assert(t1 == r10 && t2 == r11, "must be");
208
209 Label done;
210
211 // len_in_bytes is positive and ptr sized
212 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
213 br(Assembler::EQ, done);
214
215 // zero_words() takes ptr in r10 and count in words in r11
216 mov(rscratch1, len_in_bytes);
254 } else if (con_size_in_bytes > hdr_size_in_bytes) {
255 con_size_in_bytes -= hdr_size_in_bytes;
256 lea(t1, Address(obj, hdr_size_in_bytes));
257 address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord);
258 if (tpc == nullptr) {
259 Compilation::current()->bailout("no space for trampoline stub");
260 return;
261 }
262 }
263 }
264
265 membar(StoreStore);
266
267 if (CURRENT_ENV->dtrace_alloc_probes()) {
268 assert(obj == r0, "must be");
269 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
270 }
271
272 verify_oop(obj);
273 }
274 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) {
275 assert_different_registers(obj, len, t1, t2, klass);
276
277 // determine alignment mask
278 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
279
280 // check for negative or excessive length
281 mov(rscratch1, (int32_t)max_array_allocation_length);
282 cmp(len, rscratch1);
283 br(Assembler::HS, slow_case);
284
285 const Register arr_size = t2; // okay to be the same
286 // align object end
287 mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask);
288 add(arr_size, arr_size, len, ext::uxtw, f);
289 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
290
291 try_allocate(obj, arr_size, 0, t1, t2, slow_case);
292
293 initialize_header(obj, klass, len, t1, t2);
294
295 // clear rest of allocated space
296 initialize_body(obj, arr_size, header_size * BytesPerWord, t1, t2);
297 if (Compilation::current()->bailed_out()) {
298 return;
299 }
300
301 membar(StoreStore);
302
303 if (CURRENT_ENV->dtrace_alloc_probes()) {
304 assert(obj == r0, "must be");
305 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
306 }
307
308 verify_oop(obj);
309 }
310
311
312 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
313 verify_oop(receiver);
314 // explicit null check not needed since load from [klass_offset] causes a trap
315 // check against inline cache
316 assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");
317
318 cmp_klass(receiver, iCache, rscratch1);
319 }
320
321
322 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) {
323 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect");
324 // Make sure there is enough stack space for this method's activation.
325 // Note that we do this before creating a frame.
326 generate_stack_overflow_check(bang_size_in_bytes);
327 MacroAssembler::build_frame(framesize);
328
329 // Insert nmethod entry barrier into frame.
330 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
331 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
332 }
333
334 void C1_MacroAssembler::remove_frame(int framesize) {
335 MacroAssembler::remove_frame(framesize);
336 }
337
|
1 /*
2 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2021, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
63 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) {
64 const int aligned_mask = BytesPerWord -1;
65 const int hdr_offset = oopDesc::mark_offset_in_bytes();
66 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2);
67 int null_check_offset = -1;
68
69 verify_oop(obj);
70
71 // save object being locked into the BasicObjectLock
72 str(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
73
74 null_check_offset = offset();
75
76 if (DiagnoseSyncOnValueBasedClasses != 0) {
77 load_klass(hdr, obj);
78 ldrw(hdr, Address(hdr, Klass::access_flags_offset()));
79 tstw(hdr, JVM_ACC_IS_VALUE_BASED_CLASS);
80 br(Assembler::NE, slow_case);
81 }
82
83 if (LockingMode == LM_LIGHTWEIGHT) {
84 lightweight_lock(obj, hdr, temp, rscratch2, slow_case);
85 } else if (LockingMode == LM_LEGACY) {
86 Label done;
87 // Load object header
88 ldr(hdr, Address(obj, hdr_offset));
89 // and mark it as unlocked
90 orr(hdr, hdr, markWord::unlocked_value);
91 // save unlocked object header into the displaced header location on the stack
92 str(hdr, Address(disp_hdr, 0));
93 // test if object header is still the same (i.e. unlocked), and if so, store the
94 // displaced header address in the object header - if it is not the same, get the
95 // object header instead
96 lea(rscratch2, Address(obj, hdr_offset));
97 cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/nullptr);
98 // if the object header was the same, we're done
99 // if the object header was not the same, it is now in the hdr register
100 // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
101 //
102 // 1) (hdr & aligned_mask) == 0
103 // 2) sp <= hdr
104 // 3) hdr <= sp + page_size
105 //
106 // these 3 tests can be done by evaluating the following expression:
107 //
108 // (hdr - sp) & (aligned_mask - page_size)
127
128 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) {
129 const int aligned_mask = BytesPerWord -1;
130 const int hdr_offset = oopDesc::mark_offset_in_bytes();
131 assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2);
132 Label done;
133
134 if (LockingMode != LM_LIGHTWEIGHT) {
135 // load displaced header
136 ldr(hdr, Address(disp_hdr, 0));
137 // if the loaded hdr is null we had recursive locking
138 // if we had recursive locking, we are done
139 cbz(hdr, done);
140 }
141
142 // load object
143 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
144 verify_oop(obj);
145
146 if (LockingMode == LM_LIGHTWEIGHT) {
147 lightweight_unlock(obj, hdr, temp, rscratch2, slow_case);
148 } else if (LockingMode == LM_LEGACY) {
149 // test if object header is pointing to the displaced header, and if so, restore
150 // the displaced header in the object - if the object header is not pointing to
151 // the displaced header, get the object header instead
152 // if the object header was not pointing to the displaced header,
153 // we do unlocking via runtime call
154 if (hdr_offset) {
155 lea(rscratch1, Address(obj, hdr_offset));
156 cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case);
157 } else {
158 cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case);
159 }
160 // done
161 bind(done);
162 }
163 decrement(Address(rthread, JavaThread::held_monitor_count_offset()));
164 }
165
166
167 // Defines obj, preserves var_size_in_bytes
168 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
169 if (UseTLAB) {
170 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
171 } else {
172 b(slow_case);
173 }
174 }
175
176 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
177 assert_different_registers(obj, klass, len);
178 if (UseCompactObjectHeaders) {
179 ldr(t1, Address(klass, Klass::prototype_header_offset()));
180 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
181 } else {
182 // This assumes that all prototype bits fit in an int32_t
183 mov(t1, (int32_t)(intptr_t)markWord::prototype().value());
184 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
185
186 if (UseCompressedClassPointers) { // Take care not to kill klass
187 encode_klass_not_null(t1, klass);
188 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
189 } else {
190 str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
191 }
192 }
193
194 if (len->is_valid()) {
195 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
196 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) {
197 store_klass_gap(obj, zr);
198 }
199 }
200
201 // preserves obj, destroys len_in_bytes
202 //
203 // Scratch registers: t1 = r10, t2 = r11
204 //
205 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
206 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
207 assert(t1 == r10 && t2 == r11, "must be");
208
209 Label done;
210
211 // len_in_bytes is positive and ptr sized
212 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
213 br(Assembler::EQ, done);
214
215 // zero_words() takes ptr in r10 and count in words in r11
216 mov(rscratch1, len_in_bytes);
254 } else if (con_size_in_bytes > hdr_size_in_bytes) {
255 con_size_in_bytes -= hdr_size_in_bytes;
256 lea(t1, Address(obj, hdr_size_in_bytes));
257 address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord);
258 if (tpc == nullptr) {
259 Compilation::current()->bailout("no space for trampoline stub");
260 return;
261 }
262 }
263 }
264
265 membar(StoreStore);
266
267 if (CURRENT_ENV->dtrace_alloc_probes()) {
268 assert(obj == r0, "must be");
269 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
270 }
271
272 verify_oop(obj);
273 }
274 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int base_offset_in_bytes, int f, Register klass, Label& slow_case) {
275 assert_different_registers(obj, len, t1, t2, klass);
276
277 // determine alignment mask
278 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
279
280 // check for negative or excessive length
281 mov(rscratch1, (int32_t)max_array_allocation_length);
282 cmp(len, rscratch1);
283 br(Assembler::HS, slow_case);
284
285 const Register arr_size = t2; // okay to be the same
286 // align object end
287 mov(arr_size, (int32_t)base_offset_in_bytes + MinObjAlignmentInBytesMask);
288 add(arr_size, arr_size, len, ext::uxtw, f);
289 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
290
291 try_allocate(obj, arr_size, 0, t1, t2, slow_case);
292
293 initialize_header(obj, klass, len, t1, t2);
294
295 // Clear leading 4 bytes, if necessary.
296 // TODO: This could perhaps go into initialize_body() and also clear the leading 4 bytes
297 // for non-array objects, thereby replacing the klass-gap clearing code in initialize_header().
298 int base_offset = base_offset_in_bytes;
299 if (!is_aligned(base_offset, BytesPerWord)) {
300 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
301 strw(zr, Address(obj, base_offset));
302 base_offset += BytesPerInt;
303 }
304 assert(is_aligned(base_offset, BytesPerWord), "must be word-aligned");
305
306 // clear rest of allocated space
307 initialize_body(obj, arr_size, base_offset, t1, t2);
308 if (Compilation::current()->bailed_out()) {
309 return;
310 }
311
312 membar(StoreStore);
313
314 if (CURRENT_ENV->dtrace_alloc_probes()) {
315 assert(obj == r0, "must be");
316 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
317 }
318
319 verify_oop(obj);
320 }
321
322
323 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
324 verify_oop(receiver);
325 // explicit null check not needed since load from [klass_offset] causes a trap
326 // check against inline cache. This is checked in Universe::genesis()..
327 cmp_klass(receiver, iCache, rscratch1);
328 }
329
330
331 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) {
332 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect");
333 // Make sure there is enough stack space for this method's activation.
334 // Note that we do this before creating a frame.
335 generate_stack_overflow_check(bang_size_in_bytes);
336 MacroAssembler::build_frame(framesize);
337
338 // Insert nmethod entry barrier into frame.
339 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
340 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
341 }
342
343 void C1_MacroAssembler::remove_frame(int framesize) {
344 MacroAssembler::remove_frame(framesize);
345 }
346
|