1 /*
  2  * Copyright (c) 1999, 2022, 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/barrierSet.hpp"
 32 #include "gc/shared/barrierSetAssembler.hpp"
 33 #include "gc/shared/tlab_globals.hpp"
 34 #include "interpreter/interpreter.hpp"
 35 #include "oops/arrayOop.hpp"
 36 #include "oops/markWord.hpp"
 37 #include "runtime/basicLock.hpp"
 38 #include "runtime/os.hpp"
 39 #include "runtime/sharedRuntime.hpp"
 40 #include "runtime/stubRoutines.hpp"
 41 
 42 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result,
 43                                   FloatRegister f0, FloatRegister f1,
 44                                   Register result)
 45 {
 46   Label done;
 47   if (is_float) {
 48     fcmps(f0, f1);
 49   } else {
 50     fcmpd(f0, f1);
 51   }
 52   if (unordered_result < 0) {
 53     // we want -1 for unordered or less than, 0 for equal and 1 for
 54     // greater than.
 55     cset(result, NE);  // Not equal or unordered
 56     cneg(result, result, LT);  // Less than or unordered
 57   } else {
 58     // we want -1 for less than, 0 for equal and 1 for unordered or
 59     // greater than.
 60     cset(result, NE);  // Not equal or unordered
 61     cneg(result, result, LO);  // Less than
 62   }
 63 }
 64 
 65 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
 66   const int aligned_mask = BytesPerWord -1;
 67   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 68   assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
 69   Label done;
 70   int null_check_offset = -1;
 71 
 72   verify_oop(obj);
 73 
 74   // save object being locked into the BasicObjectLock
 75   str(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
 76 
 77   null_check_offset = offset();
 78 
 79   if (DiagnoseSyncOnValueBasedClasses != 0) {
 80     load_klass(hdr, obj);
 81     ldrw(hdr, Address(hdr, Klass::access_flags_offset()));
 82     tstw(hdr, JVM_ACC_IS_VALUE_BASED_CLASS);
 83     br(Assembler::NE, slow_case);
 84   }
 85 
 86   // Load object header
 87   ldr(hdr, Address(obj, hdr_offset));
 88   // and mark it as unlocked
 89   orr(hdr, hdr, markWord::unlocked_value);
 90 
 91   if (EnableValhalla) {
 92     // Mask always_locked bit such that we go to the slow path if object is an inline type
 93     andr(hdr, hdr, ~markWord::inline_type_bit_in_place);
 94   }
 95 
 96   // save unlocked object header into the displaced header location on the stack
 97   str(hdr, Address(disp_hdr, 0));
 98   // test if object header is still the same (i.e. unlocked), and if so, store the
 99   // displaced header address in the object header - if it is not the same, get the
100   // object header instead
101   lea(rscratch2, Address(obj, hdr_offset));
102   cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/NULL);
103   // if the object header was the same, we're done
104   // if the object header was not the same, it is now in the hdr register
105   // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
106   //
107   // 1) (hdr & aligned_mask) == 0
108   // 2) sp <= hdr
109   // 3) hdr <= sp + page_size
110   //
111   // these 3 tests can be done by evaluating the following expression:
112   //
113   // (hdr - sp) & (aligned_mask - page_size)
114   //
115   // assuming both the stack pointer and page_size have their least
116   // significant 2 bits cleared and page_size is a power of 2
117   mov(rscratch1, sp);
118   sub(hdr, hdr, rscratch1);
119   ands(hdr, hdr, aligned_mask - os::vm_page_size());
120   // for recursive locking, the result is zero => save it in the displaced header
121   // location (NULL in the displaced hdr location indicates recursive locking)
122   str(hdr, Address(disp_hdr, 0));
123   // otherwise we don't care about the result and handle locking via runtime call
124   cbnz(hdr, slow_case);
125   // done
126   bind(done);
127   increment(Address(rthread, JavaThread::held_monitor_count_offset()));
128   return null_check_offset;
129 }
130 
131 
132 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
133   const int aligned_mask = BytesPerWord -1;
134   const int hdr_offset = oopDesc::mark_offset_in_bytes();
135   assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
136   Label done;
137 
138   // load displaced header
139   ldr(hdr, Address(disp_hdr, 0));
140   // if the loaded hdr is NULL we had recursive locking
141   // if we had recursive locking, we are done
142   cbz(hdr, done);
143   // load object
144   ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
145   verify_oop(obj);
146   // test if object header is pointing to the displaced header, and if so, restore
147   // the displaced header in the object - if the object header is not pointing to
148   // the displaced header, get the object header instead
149   // if the object header was not pointing to the displaced header,
150   // we do unlocking via runtime call
151   if (hdr_offset) {
152     lea(rscratch1, Address(obj, hdr_offset));
153     cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case);
154   } else {
155     cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case);
156   }
157   // done
158   bind(done);
159   decrement(Address(rthread, JavaThread::held_monitor_count_offset()));
160 }
161 
162 
163 // Defines obj, preserves var_size_in_bytes
164 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
165   if (UseTLAB) {
166     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
167   } else {
168     b(slow_case);
169   }
170 }
171 
172 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
173   assert_different_registers(obj, klass, len);
174   if (EnableValhalla) {
175     // Need to copy markWord::prototype header for klass
176     assert_different_registers(obj, klass, len, t1, t2);
177     ldr(t1, Address(klass, Klass::prototype_header_offset()));
178   } else {
179     // This assumes that all prototype bits fit in an int32_t
180     mov(t1, (int32_t)(intptr_t)markWord::prototype().value());
181   }
182   str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
183 
184   if (UseCompressedClassPointers) { // Take care not to kill klass
185     encode_klass_not_null(t1, klass);
186     strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
187   } else {
188     str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
189   }
190 
191   if (len->is_valid()) {
192     strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
193   } else if (UseCompressedClassPointers) {
194     store_klass_gap(obj, zr);
195   }
196 }
197 
198 // preserves obj, destroys len_in_bytes
199 //
200 // Scratch registers: t1 = r10, t2 = r11
201 //
202 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
203   assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
204   assert(t1 == r10 && t2 == r11, "must be");
205 
206   Label done;
207 
208   // len_in_bytes is positive and ptr sized
209   subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
210   br(Assembler::EQ, done);
211 
212   // zero_words() takes ptr in r10 and count in words in r11
213   mov(rscratch1, len_in_bytes);
214   lea(t1, Address(obj, hdr_size_in_bytes));
215   lsr(t2, rscratch1, LogBytesPerWord);
216   zero_words(t1, t2);
217 
218   bind(done);
219 }
220 
221 
222 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
223   assert_different_registers(obj, t1, t2); // XXX really?
224   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
225 
226   try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
227 
228   initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB);
229 }
230 
231 // Scratch registers: t1 = r10, t2 = r11
232 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) {
233   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
234          "con_size_in_bytes is not multiple of alignment");
235   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
236 
237   initialize_header(obj, klass, noreg, t1, t2);
238 
239   if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
240      // clear rest of allocated space
241      const Register index = t2;
242      if (var_size_in_bytes != noreg) {
243        mov(index, var_size_in_bytes);
244        initialize_body(obj, index, hdr_size_in_bytes, t1, t2);
245      } else if (con_size_in_bytes > hdr_size_in_bytes) {
246        con_size_in_bytes -= hdr_size_in_bytes;
247        lea(t1, Address(obj, hdr_size_in_bytes));
248        zero_words(t1, con_size_in_bytes / BytesPerWord);
249      }
250   }
251 
252   membar(StoreStore);
253 
254   if (CURRENT_ENV->dtrace_alloc_probes()) {
255     assert(obj == r0, "must be");
256     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
257   }
258 
259   verify_oop(obj);
260 }
261 
262 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) {
263   assert_different_registers(obj, len, t1, t2, klass);
264 
265   // determine alignment mask
266   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
267 
268   // check for negative or excessive length
269   mov(rscratch1, (int32_t)max_array_allocation_length);
270   cmp(len, rscratch1);
271   br(Assembler::HS, slow_case);
272 
273   const Register arr_size = t2; // okay to be the same
274   // align object end
275   mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask);
276   add(arr_size, arr_size, len, ext::uxtw, f);
277   andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
278 
279   try_allocate(obj, arr_size, 0, t1, t2, slow_case);
280 
281   initialize_header(obj, klass, len, t1, t2);
282 
283   // clear rest of allocated space
284   initialize_body(obj, arr_size, header_size * BytesPerWord, t1, t2);
285 
286   membar(StoreStore);
287 
288   if (CURRENT_ENV->dtrace_alloc_probes()) {
289     assert(obj == r0, "must be");
290     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
291   }
292 
293   verify_oop(obj);
294 }
295 
296 
297 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
298   verify_oop(receiver);
299   // explicit NULL check not needed since load from [klass_offset] causes a trap
300   // check against inline cache
301   assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");
302 
303   cmp_klass(receiver, iCache, rscratch1);
304 }
305 
306 void C1_MacroAssembler::build_frame_helper(int frame_size_in_bytes, int sp_offset_for_orig_pc, int sp_inc, bool reset_orig_pc, bool needs_stack_repair) {
307   MacroAssembler::build_frame(frame_size_in_bytes);
308 
309   if (needs_stack_repair) {
310     save_stack_increment(sp_inc, frame_size_in_bytes);
311   }
312   if (reset_orig_pc) {
313     // Zero orig_pc to detect deoptimization during buffering in the entry points
314     str(zr, Address(sp, sp_offset_for_orig_pc));
315   }
316 }
317 
318 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, bool needs_stack_repair, bool has_scalarized_args, Label* verified_inline_entry_label) {
319   // Make sure there is enough stack space for this method's activation.
320   // Note that we do this before creating a frame.
321   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
322   generate_stack_overflow_check(bang_size_in_bytes);
323 
324   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, has_scalarized_args, needs_stack_repair);
325 
326   // Insert nmethod entry barrier into frame.
327   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
328   bs->nmethod_entry_barrier(this, NULL /* slow_path */, NULL /* continuation */, NULL /* guard */);
329 
330   if (verified_inline_entry_label != NULL) {
331     // Jump here from the scalarized entry points that already created the frame.
332     bind(*verified_inline_entry_label);
333   }
334 }
335 
336 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
337   // If we have to make this method not-entrant we'll overwrite its
338   // first instruction with a jump.  For this action to be legal we
339   // must ensure that this first instruction is a B, BL, NOP, BKPT,
340   // SVC, HVC, or SMC.  Make it a NOP.
341   nop();
342   if (C1Breakpoint) brk(1);
343 }
344 
345 int C1_MacroAssembler::scalarized_entry(const CompiledEntrySignature* ces, int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, Label& verified_inline_entry_label, bool is_inline_ro_entry) {
346   assert(InlineTypePassFieldsAsArgs, "sanity");
347   // Make sure there is enough stack space for this method's activation.
348   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
349   generate_stack_overflow_check(bang_size_in_bytes);
350 
351   GrowableArray<SigEntry>* sig    = &ces->sig();
352   GrowableArray<SigEntry>* sig_cc = is_inline_ro_entry ? &ces->sig_cc_ro() : &ces->sig_cc();
353   VMRegPair* regs      = ces->regs();
354   VMRegPair* regs_cc   = is_inline_ro_entry ? ces->regs_cc_ro() : ces->regs_cc();
355   int args_on_stack    = ces->args_on_stack();
356   int args_on_stack_cc = is_inline_ro_entry ? ces->args_on_stack_cc_ro() : ces->args_on_stack_cc();
357 
358   assert(sig->length() <= sig_cc->length(), "Zero-sized inline class not allowed!");
359   BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig_cc->length());
360   int args_passed = sig->length();
361   int args_passed_cc = SigEntry::fill_sig_bt(sig_cc, sig_bt);
362 
363   // Create a temp frame so we can call into the runtime. It must be properly set up to accommodate GC.
364   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, true, ces->c1_needs_stack_repair());
365 
366   // The runtime call might safepoint, make sure nmethod entry barrier is executed
367   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
368   // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
369   bs->nmethod_entry_barrier(this, NULL /* slow_path */, NULL /* continuation */, NULL /* guard */);
370 
371   // FIXME -- call runtime only if we cannot in-line allocate all the incoming inline type args.
372   mov(r19, (intptr_t) ces->method());
373   if (is_inline_ro_entry) {
374     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::buffer_inline_args_no_receiver_id)));
375   } else {
376     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::buffer_inline_args_id)));
377   }
378   int rt_call_offset = offset();
379 
380   // The runtime call returns the new array in r20 instead of the usual r0
381   // because r0 is also j_rarg7 which may be holding a live argument here.
382   Register val_array = r20;
383 
384   // Remove the temp frame
385   MacroAssembler::remove_frame(frame_size_in_bytes);
386 
387   // Check if we need to extend the stack for packing
388   int sp_inc = 0;
389   if (args_on_stack > args_on_stack_cc) {
390     sp_inc = extend_stack_for_inline_args(args_on_stack);
391   }
392 
393   shuffle_inline_args(true, is_inline_ro_entry, sig_cc,
394                       args_passed_cc, args_on_stack_cc, regs_cc, // from
395                       args_passed, args_on_stack, regs,          // to
396                       sp_inc, val_array);
397 
398   // Create the real frame. Below jump will then skip over the stack banging and frame
399   // setup code in the verified_inline_entry (which has a different real_frame_size).
400   build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, sp_inc, false, ces->c1_needs_stack_repair());
401 
402   b(verified_inline_entry_label);
403   return rt_call_offset;
404 }
405 
406 
407 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
408   // rfp, + 0: link
409   //     + 1: return address
410   //     + 2: argument with offset 0
411   //     + 3: argument with offset 1
412   //     + 4: ...
413 
414   ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
415 }
416 
417 #ifndef PRODUCT
418 
419 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
420   if (!VerifyOops) return;
421   verify_oop_addr(Address(sp, stack_offset));
422 }
423 
424 void C1_MacroAssembler::verify_not_null_oop(Register r) {
425   if (!VerifyOops) return;
426   Label not_null;
427   cbnz(r, not_null);
428   stop("non-null oop required");
429   bind(not_null);
430   verify_oop(r);
431 }
432 
433 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) {
434 #ifdef ASSERT
435   static int nn;
436   if (inv_r0) mov(r0, 0xDEAD);
437   if (inv_r19) mov(r19, 0xDEAD);
438   if (inv_r2) mov(r2, nn++);
439   if (inv_r3) mov(r3, 0xDEAD);
440   if (inv_r4) mov(r4, 0xDEAD);
441   if (inv_r5) mov(r5, 0xDEAD);
442 #endif
443 }
444 #endif // ifndef PRODUCT