1 /*
  2  * Copyright (c) 2008, 2023, 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 "c1/c1_MacroAssembler.hpp"
 27 #include "c1/c1_Runtime1.hpp"
 28 #include "gc/shared/barrierSet.hpp"
 29 #include "gc/shared/barrierSetAssembler.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/tlab_globals.hpp"
 32 #include "interpreter/interpreter.hpp"
 33 #include "oops/arrayOop.hpp"
 34 #include "oops/markWord.hpp"
 35 #include "runtime/basicLock.hpp"
 36 #include "runtime/os.hpp"
 37 #include "runtime/sharedRuntime.hpp"
 38 #include "runtime/stubRoutines.hpp"
 39 #include "utilities/powerOfTwo.hpp"
 40 
 41 // Note: Rtemp usage is this file should not impact C2 and should be
 42 // correct as long as it is not implicitly used in lower layers (the
 43 // arm [macro]assembler) and used with care in the other C1 specific
 44 // files.
 45 
 46 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
 47   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 48   assert((frame_size_in_bytes % StackAlignmentInBytes) == 0, "frame size should be aligned");
 49 
 50 
 51   arm_stack_overflow_check(bang_size_in_bytes, Rtemp);
 52 
 53   // FP can no longer be used to memorize SP. It may be modified
 54   // if this method contains a methodHandle call site
 55   raw_push(FP, LR);
 56   sub_slow(SP, SP, frame_size_in_bytes);
 57 
 58   // Insert nmethod entry barrier into frame.
 59   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 60   bs->nmethod_entry_barrier(this);
 61 }
 62 
 63 void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) {
 64   add_slow(SP, SP, frame_size_in_bytes);
 65   raw_pop(FP, LR);
 66 }
 67 
 68 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 69   if (breakAtEntry) {
 70     breakpoint();
 71   }
 72 }
 73 
 74 // Puts address of allocated object into register `obj` and end of allocated object into register `obj_end`.
 75 void C1_MacroAssembler::try_allocate(Register obj, Register obj_end, Register tmp1, Register tmp2,
 76                                      RegisterOrConstant size_expression, Label& slow_case) {
 77   if (UseTLAB) {
 78     tlab_allocate(obj, obj_end, tmp1, size_expression, slow_case);
 79   } else {
 80     b(slow_case);
 81   }
 82 }
 83 
 84 
 85 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register tmp) {
 86   assert_different_registers(obj, klass, len, tmp);
 87 
 88   mov(tmp, (intptr_t)markWord::prototype().value());
 89 
 90   str(tmp, Address(obj, oopDesc::mark_offset_in_bytes()));
 91   str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
 92 
 93   if (len->is_valid()) {
 94     str_32(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
 95   }
 96 }
 97 
 98 
 99 // Cleans object body [base..obj_end]. Clobbers `base` and `tmp` registers.
100 void C1_MacroAssembler::initialize_body(Register base, Register obj_end, Register tmp) {
101   zero_memory(base, obj_end, tmp);
102 }
103 
104 
105 void C1_MacroAssembler::initialize_object(Register obj, Register obj_end, Register klass,
106                                           Register len, Register tmp1, Register tmp2,
107                                           RegisterOrConstant header_size, int obj_size_in_bytes,
108                                           bool is_tlab_allocated)
109 {
110   assert_different_registers(obj, obj_end, klass, len, tmp1, tmp2);
111   initialize_header(obj, klass, len, tmp1);
112 
113   const Register ptr = tmp2;
114 
115   if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
116     if (obj_size_in_bytes >= 0 && obj_size_in_bytes <= 8 * BytesPerWord) {
117       mov(tmp1, 0);
118       const int base = instanceOopDesc::header_size() * HeapWordSize;
119       for (int i = base; i < obj_size_in_bytes; i += wordSize) {
120         str(tmp1, Address(obj, i));
121       }
122     } else {
123       assert(header_size.is_constant() || header_size.as_register() == ptr, "code assumption");
124       add(ptr, obj, header_size);
125       initialize_body(ptr, obj_end, tmp1);
126     }
127   }
128 
129   // StoreStore barrier required after complete initialization
130   // (headers + content zeroing), before the object may escape.
131   membar(MacroAssembler::StoreStore, tmp1);
132 }
133 
134 void C1_MacroAssembler::allocate_object(Register obj, Register tmp1, Register tmp2, Register tmp3,
135                                         int header_size, int object_size,
136                                         Register klass, Label& slow_case) {
137   assert_different_registers(obj, tmp1, tmp2, tmp3, klass, Rtemp);
138   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
139   const int object_size_in_bytes = object_size * BytesPerWord;
140 
141   const Register obj_end = tmp1;
142   const Register len = noreg;
143 
144   if (Assembler::is_arith_imm_in_range(object_size_in_bytes)) {
145     try_allocate(obj, obj_end, tmp2, tmp3, object_size_in_bytes, slow_case);
146   } else {
147     // Rtemp should be free at c1 LIR level
148     mov_slow(Rtemp, object_size_in_bytes);
149     try_allocate(obj, obj_end, tmp2, tmp3, Rtemp, slow_case);
150   }
151   initialize_object(obj, obj_end, klass, len, tmp2, tmp3, instanceOopDesc::header_size() * HeapWordSize, object_size_in_bytes, /* is_tlab_allocated */ UseTLAB);
152 }
153 
154 void C1_MacroAssembler::allocate_array(Register obj, Register len,
155                                        Register tmp1, Register tmp2, Register tmp3,
156                                        int header_size, int element_size,
157                                        Register klass, Label& slow_case) {
158   assert_different_registers(obj, len, tmp1, tmp2, tmp3, klass, Rtemp);
159   const int header_size_in_bytes = header_size * BytesPerWord;
160   const int scale_shift = exact_log2(element_size);
161   const Register obj_size = Rtemp; // Rtemp should be free at c1 LIR level
162 
163   cmp_32(len, max_array_allocation_length);
164   b(slow_case, hs);
165 
166   bool align_header = ((header_size_in_bytes | element_size) & MinObjAlignmentInBytesMask) != 0;
167   assert(align_header || ((header_size_in_bytes & MinObjAlignmentInBytesMask) == 0), "must be");
168   assert(align_header || ((element_size & MinObjAlignmentInBytesMask) == 0), "must be");
169 
170   mov(obj_size, header_size_in_bytes + (align_header ? (MinObjAlignmentInBytes - 1) : 0));
171   add_ptr_scaled_int32(obj_size, obj_size, len, scale_shift);
172 
173   if (align_header) {
174     align_reg(obj_size, obj_size, MinObjAlignmentInBytes);
175   }
176 
177   try_allocate(obj, tmp1, tmp2, tmp3, obj_size, slow_case);
178   initialize_object(obj, tmp1, klass, len, tmp2, tmp3, header_size_in_bytes, -1, /* is_tlab_allocated */ UseTLAB);
179 }
180 
181 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
182   Label done, fast_lock, fast_lock_done;
183   int null_check_offset = 0;
184 
185   const Register tmp2 = Rtemp; // Rtemp should be free at c1 LIR level
186   assert_different_registers(hdr, obj, disp_hdr, tmp2);
187 
188   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
189   const ByteSize obj_offset = BasicObjectLock::obj_offset();
190   const int mark_offset = BasicLock::displaced_header_offset_in_bytes();
191 
192   // save object being locked into the BasicObjectLock
193   str(obj, Address(disp_hdr, obj_offset));
194 
195   null_check_offset = offset();
196 
197   if (DiagnoseSyncOnValueBasedClasses != 0) {
198     load_klass(tmp2, obj);
199     ldr_u32(tmp2, Address(tmp2, Klass::access_flags_offset()));
200     tst(tmp2, JVM_ACC_IS_VALUE_BASED_CLASS);
201     b(slow_case, ne);
202   }
203 
204   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
205 
206   if (LockingMode == LM_LIGHTWEIGHT) {
207 
208     Register t1 = disp_hdr; // Needs saving, probably
209     Register t2 = hdr;      // blow
210     Register t3 = Rtemp;    // blow
211 
212     lightweight_lock(obj /* obj */, t1, t2, t3, 1 /* savemask - save t1 */, slow_case);
213     // Success: fall through
214 
215   } else if (LockingMode == LM_LEGACY) {
216 
217     // On MP platforms the next load could return a 'stale' value if the memory location has been modified by another thread.
218     // That would be acceptable as ether CAS or slow case path is taken in that case.
219 
220     // Must be the first instruction here, because implicit null check relies on it
221     ldr(hdr, Address(obj, oopDesc::mark_offset_in_bytes()));
222 
223     tst(hdr, markWord::unlocked_value);
224     b(fast_lock, ne);
225 
226     // Check for recursive locking
227     // See comments in InterpreterMacroAssembler::lock_object for
228     // explanations on the fast recursive locking check.
229     // -1- test low 2 bits
230     movs(tmp2, AsmOperand(hdr, lsl, 30));
231     // -2- test (hdr - SP) if the low two bits are 0
232     sub(tmp2, hdr, SP, eq);
233     movs(tmp2, AsmOperand(tmp2, lsr, exact_log2(os::vm_page_size())), eq);
234     // If still 'eq' then recursive locking OK
235     // set to zero if recursive lock, set to non zero otherwise (see discussion in JDK-8267042)
236     str(tmp2, Address(disp_hdr, mark_offset));
237     b(fast_lock_done, eq);
238     // else need slow case
239     b(slow_case);
240 
241 
242     bind(fast_lock);
243     // Save previous object header in BasicLock structure and update the header
244     str(hdr, Address(disp_hdr, mark_offset));
245 
246     cas_for_lock_acquire(hdr, disp_hdr, obj, tmp2, slow_case);
247 
248     bind(fast_lock_done);
249   }
250   bind(done);
251 
252   return null_check_offset;
253 }
254 
255 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
256   assert_different_registers(hdr, obj, disp_hdr, Rtemp);
257   Register tmp2 = Rtemp;
258 
259   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
260   const ByteSize obj_offset = BasicObjectLock::obj_offset();
261   const int mark_offset = BasicLock::displaced_header_offset_in_bytes();
262 
263   Label done;
264 
265   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
266 
267   if (LockingMode == LM_LIGHTWEIGHT) {
268 
269     ldr(obj, Address(disp_hdr, obj_offset));
270 
271     Register t1 = disp_hdr; // Needs saving, probably
272     Register t2 = hdr;      // blow
273     Register t3 = Rtemp;    // blow
274 
275     lightweight_unlock(obj /* object */, t1, t2, t3, 1 /* savemask (save t1) */,
276                        slow_case);
277     // Success: Fall through
278 
279   } else if (LockingMode == LM_LEGACY) {
280 
281     // Load displaced header and object from the lock
282     ldr(hdr, Address(disp_hdr, mark_offset));
283     // If hdr is null, we've got recursive locking and there's nothing more to do
284     cbz(hdr, done);
285 
286     // load object
287     ldr(obj, Address(disp_hdr, obj_offset));
288 
289     // Restore the object header
290     cas_for_lock_release(disp_hdr, hdr, obj, tmp2, slow_case);
291   }
292   bind(done);
293 }
294 
295 #ifndef PRODUCT
296 
297 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
298   if (!VerifyOops) return;
299   verify_oop_addr(Address(SP, stack_offset));
300 }
301 
302 void C1_MacroAssembler::verify_not_null_oop(Register r) {
303   Label not_null;
304   cbnz(r, not_null);
305   stop("non-null oop required");
306   bind(not_null);
307   if (!VerifyOops) return;
308   verify_oop(r);
309 }
310 
311 #endif // !PRODUCT