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.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "c1/c1_MacroAssembler.hpp"
 28 #include "c1/c1_Runtime1.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 
 40 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result,
 41                                   FloatRegister f0, FloatRegister f1,
 42                                   Register result)
 43 {
 44   Label done;
 45   if (is_float) {
 46     fcmps(f0, f1);
 47   } else {
 48     fcmpd(f0, f1);
 49   }
 50   if (unordered_result < 0) {
 51     // we want -1 for unordered or less than, 0 for equal and 1 for
 52     // greater than.
 53     cset(result, NE);  // Not equal or unordered
 54     cneg(result, result, LT);  // Less than or unordered
 55   } else {
 56     // we want -1 for less than, 0 for equal and 1 for unordered or
 57     // greater than.
 58     cset(result, NE);  // Not equal or unordered
 59     cneg(result, result, LO);  // Less than
 60   }
 61 }
 62 
 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   Label count_locking, done;
 69 
 70   verify_oop(obj);
 71 
 72   // save object being locked into the BasicObjectLock
 73   str(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
 74 
 75   null_check_offset = offset();
 76 
 77   if (DiagnoseSyncOnValueBasedClasses != 0) {
 78     load_klass(hdr, obj);
 79     ldrw(hdr, Address(hdr, Klass::access_flags_offset()));
 80     tstw(hdr, JVM_ACC_IS_VALUE_BASED_CLASS);
 81     br(Assembler::NE, slow_case);
 82   }
 83 
 84   if (LockingMode == LM_LIGHTWEIGHT) {
 85     lightweight_lock(obj, hdr, temp, rscratch2, slow_case);
 86     b(done);
 87   } else if (LockingMode == LM_LEGACY) {
 88     // Load object header
 89     ldr(hdr, Address(obj, hdr_offset));
 90     // and mark it as unlocked
 91     orr(hdr, hdr, markWord::unlocked_value);
 92     // save unlocked object header into the displaced header location on the stack
 93     str(hdr, Address(disp_hdr, 0));
 94     // test if object header is still the same (i.e. unlocked), and if so, store the
 95     // displaced header address in the object header - if it is not the same, get the
 96     // object header instead
 97     lea(rscratch2, Address(obj, hdr_offset));
 98     // if the object header was the same, we're done
 99     cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, count_locking, /*fallthough*/nullptr);
100     // if the object header was the same, we're done
101     // if the object header was not the same, it is now in the hdr register
102     // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
103     //
104     // 1) (hdr & aligned_mask) == 0
105     // 2) sp <= hdr
106     // 3) hdr <= sp + page_size
107     //
108     // these 3 tests can be done by evaluating the following expression:
109     //
110     // (hdr - sp) & (aligned_mask - page_size)
111     //
112     // assuming both the stack pointer and page_size have their least
113     // significant 2 bits cleared and page_size is a power of 2
114     mov(rscratch1, sp);
115     sub(hdr, hdr, rscratch1);
116     ands(hdr, hdr, aligned_mask - (int)os::vm_page_size());
117     // for recursive locking, the result is zero => save it in the displaced header
118     // location (null in the displaced hdr location indicates recursive locking)
119     str(hdr, Address(disp_hdr, 0));
120     // otherwise we don't care about the result and handle locking via runtime call
121     cbnz(hdr, slow_case);
122     // done
123     b(done);
124   }
125   bind(count_locking);
126   inc_held_monitor_count();
127   bind(done);
128   return null_check_offset;
129 }
130 
131 
132 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Register temp, Label& slow_case) {
133   const int aligned_mask = BytesPerWord -1;
134   const int hdr_offset = oopDesc::mark_offset_in_bytes();
135   assert_different_registers(hdr, obj, disp_hdr, temp, rscratch2);
136   Label count_locking, done;
137 
138   if (LockingMode != LM_LIGHTWEIGHT) {
139     // load displaced header
140     ldr(hdr, Address(disp_hdr, 0));
141     // if the loaded hdr is null we had recursive locking
142     // if we had recursive locking, we are done
143     cbz(hdr, done);
144   }
145 
146   // load object
147   ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
148   verify_oop(obj);
149 
150   if (LockingMode == LM_LIGHTWEIGHT) {
151     lightweight_unlock(obj, hdr, temp, rscratch2, slow_case);
152     b(done);
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, count_locking, &slow_case);
162     } else {
163       cmpxchgptr(disp_hdr, hdr, obj, rscratch2, count_locking, &slow_case);
164     }
165     // done
166     bind(count_locking);
167     dec_held_monitor_count();
168   }
169   bind(done);
170 }
171 
172 
173 // Defines obj, preserves var_size_in_bytes
174 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
175   if (UseTLAB) {
176     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
177   } else {
178     b(slow_case);
179   }
180 }
181 
182 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
183   assert_different_registers(obj, klass, len);
184   // This assumes that all prototype bits fit in an int32_t
185   mov(t1, (int32_t)(intptr_t)markWord::prototype().value());
186   str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
187 
188   if (UseCompressedClassPointers) { // Take care not to kill klass
189     encode_klass_not_null(t1, klass);
190     strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
191   } else {
192     str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
193   }
194 
195   if (len->is_valid()) {
196     strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
197   } else if (UseCompressedClassPointers) {
198     store_klass_gap(obj, zr);
199   }
200 }
201 
202 // preserves obj, destroys len_in_bytes
203 //
204 // Scratch registers: t1 = r10, t2 = r11
205 //
206 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
207   assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
208   assert(t1 == r10 && t2 == r11, "must be");
209 
210   Label done;
211 
212   // len_in_bytes is positive and ptr sized
213   subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
214   br(Assembler::EQ, done);
215 
216   // zero_words() takes ptr in r10 and count in words in r11
217   mov(rscratch1, len_in_bytes);
218   lea(t1, Address(obj, hdr_size_in_bytes));
219   lsr(t2, rscratch1, LogBytesPerWord);
220   address tpc = zero_words(t1, t2);
221 
222   bind(done);
223   if (tpc == nullptr) {
224     Compilation::current()->bailout("no space for trampoline stub");
225   }
226 }
227 
228 
229 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
230   assert_different_registers(obj, t1, t2); // XXX really?
231   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
232 
233   try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
234 
235   initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB);
236 }
237 
238 // Scratch registers: t1 = r10, t2 = r11
239 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, bool is_tlab_allocated) {
240   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
241          "con_size_in_bytes is not multiple of alignment");
242   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
243 
244   initialize_header(obj, klass, noreg, t1, t2);
245 
246   if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
247      // clear rest of allocated space
248      const Register index = t2;
249      if (var_size_in_bytes != noreg) {
250        mov(index, var_size_in_bytes);
251        initialize_body(obj, index, hdr_size_in_bytes, t1, t2);
252        if (Compilation::current()->bailed_out()) {
253          return;
254        }
255      } else if (con_size_in_bytes > hdr_size_in_bytes) {
256        con_size_in_bytes -= hdr_size_in_bytes;
257        lea(t1, Address(obj, hdr_size_in_bytes));
258        address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord);
259        if (tpc == nullptr) {
260          Compilation::current()->bailout("no space for trampoline stub");
261          return;
262        }
263      }
264   }
265 
266   membar(StoreStore);
267 
268   if (CURRENT_ENV->dtrace_alloc_probes()) {
269     assert(obj == r0, "must be");
270     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
271   }
272 
273   verify_oop(obj);
274 }
275 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) {
276   assert_different_registers(obj, len, t1, t2, klass);
277 
278   // determine alignment mask
279   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
280 
281   // check for negative or excessive length
282   mov(rscratch1, (int32_t)max_array_allocation_length);
283   cmp(len, rscratch1);
284   br(Assembler::HS, slow_case);
285 
286   const Register arr_size = t2; // okay to be the same
287   // align object end
288   mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask);
289   add(arr_size, arr_size, len, ext::uxtw, f);
290   andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
291 
292   try_allocate(obj, arr_size, 0, t1, t2, slow_case);
293 
294   initialize_header(obj, klass, len, t1, t2);
295 
296   // clear rest of allocated space
297   initialize_body(obj, arr_size, header_size * BytesPerWord, t1, t2);
298   if (Compilation::current()->bailed_out()) {
299     return;
300   }
301 
302   membar(StoreStore);
303 
304   if (CURRENT_ENV->dtrace_alloc_probes()) {
305     assert(obj == r0, "must be");
306     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
307   }
308 
309   verify_oop(obj);
310 }
311 
312 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) {
313   assert(bang_size_in_bytes >= framesize, "stack bang size incorrect");
314   // Make sure there is enough stack space for this method's activation.
315   // Note that we do this before creating a frame.
316   generate_stack_overflow_check(bang_size_in_bytes);
317   MacroAssembler::build_frame(framesize);
318 
319   // Insert nmethod entry barrier into frame.
320   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
321   bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
322 }
323 
324 void C1_MacroAssembler::remove_frame(int framesize) {
325   MacroAssembler::remove_frame(framesize);
326 }
327 
328 
329 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
330   // If we have to make this method not-entrant we'll overwrite its
331   // first instruction with a jump.  For this action to be legal we
332   // must ensure that this first instruction is a B, BL, NOP, BKPT,
333   // SVC, HVC, or SMC.  Make it a NOP.
334   nop();
335 }
336 
337 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
338   // rfp, + 0: link
339   //     + 1: return address
340   //     + 2: argument with offset 0
341   //     + 3: argument with offset 1
342   //     + 4: ...
343 
344   ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
345 }
346 
347 #ifndef PRODUCT
348 
349 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
350   if (!VerifyOops) return;
351   verify_oop_addr(Address(sp, stack_offset));
352 }
353 
354 void C1_MacroAssembler::verify_not_null_oop(Register r) {
355   if (!VerifyOops) return;
356   Label not_null;
357   cbnz(r, not_null);
358   stop("non-null oop required");
359   bind(not_null);
360   verify_oop(r);
361 }
362 
363 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) {
364 #ifdef ASSERT
365   static int nn;
366   if (inv_r0) mov(r0, 0xDEAD);
367   if (inv_r19) mov(r19, 0xDEAD);
368   if (inv_r2) mov(r2, nn++);
369   if (inv_r3) mov(r3, 0xDEAD);
370   if (inv_r4) mov(r4, 0xDEAD);
371   if (inv_r5) mov(r5, 0xDEAD);
372 #endif
373 }
374 #endif // ifndef PRODUCT