1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016 SAP SE. 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 "asm/macroAssembler.inline.hpp"
 28 #include "c1/c1_MacroAssembler.hpp"
 29 #include "c1/c1_Runtime1.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "gc/shared/barrierSetAssembler.hpp"
 32 #include "gc/shared/collectedHeap.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 #include "utilities/macros.hpp"
 42 
 43 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
 44   Label ic_miss, ic_hit;
 45   verify_oop(receiver, FILE_AND_LINE);
 46   int klass_offset = oopDesc::klass_offset_in_bytes();
 47 
 48   if (!ImplicitNullChecks || MacroAssembler::needs_explicit_null_check(klass_offset)) {
 49     if (VM_Version::has_CompareBranch()) {
 50       z_cgij(receiver, 0, Assembler::bcondEqual, ic_miss);
 51     } else {
 52       z_ltgr(receiver, receiver);
 53       z_bre(ic_miss);
 54     }
 55   }
 56 
 57   compare_klass_ptr(iCache, klass_offset, receiver, false);
 58   z_bre(ic_hit);
 59 
 60   // If icache check fails, then jump to runtime routine.
 61   // Note: RECEIVER must still contain the receiver!
 62   load_const_optimized(Z_R1_scratch, AddressLiteral(SharedRuntime::get_ic_miss_stub()));
 63   z_br(Z_R1_scratch);
 64   align(CodeEntryAlignment);
 65   bind(ic_hit);
 66 }
 67 
 68 void C1_MacroAssembler::explicit_null_check(Register base) {
 69   ShouldNotCallThis(); // unused
 70 }
 71 
 72 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
 73   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 74   generate_stack_overflow_check(bang_size_in_bytes);
 75   save_return_pc();
 76   push_frame(frame_size_in_bytes);
 77 
 78   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 79   bs->nmethod_entry_barrier(this);
 80 }
 81 
 82 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 83   if (breakAtEntry) z_illtrap(0xC1);
 84 }
 85 
 86 void C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
 87   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 88   assert_different_registers(hdr, obj, disp_hdr);
 89 
 90   verify_oop(obj, FILE_AND_LINE);
 91 
 92   // Load object header.
 93   z_lg(hdr, Address(obj, hdr_offset));
 94 
 95   // Save object being locked into the BasicObjectLock...
 96   z_stg(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
 97 
 98   if (DiagnoseSyncOnValueBasedClasses != 0) {
 99     load_klass(Z_R1_scratch, obj);
100     testbit(Address(Z_R1_scratch, Klass::access_flags_offset()), exact_log2(JVM_ACC_IS_VALUE_BASED_CLASS));
101     z_btrue(slow_case);
102   }
103 
104   assert(LockingMode != LM_MONITOR, "LM_MONITOR is already handled, by emit_lock()");
105 
106   if (LockingMode == LM_LIGHTWEIGHT) {
107     Unimplemented();
108   } else if (LockingMode == LM_LEGACY) {
109     NearLabel done;
110     // and mark it as unlocked.
111     z_oill(hdr, markWord::unlocked_value);
112     // Save unlocked object header into the displaced header location on the stack.
113     z_stg(hdr, Address(disp_hdr, (intptr_t) 0));
114     // Test if object header is still the same (i.e. unlocked), and if so, store the
115     // displaced header address in the object header. If it is not the same, get the
116     // object header instead.
117     z_csg(hdr, disp_hdr, hdr_offset, obj);
118     // If the object header was the same, we're done.
119     branch_optimized(Assembler::bcondEqual, done);
120     // If the object header was not the same, it is now in the hdr register.
121     // => Test if it is a stack pointer into the same stack (recursive locking), i.e.:
122     //
123     // 1) (hdr & markWord::lock_mask_in_place) == 0
124     // 2) rsp <= hdr
125     // 3) hdr <= rsp + page_size
126     //
127     // These 3 tests can be done by evaluating the following expression:
128     //
129     // (hdr - Z_SP) & (~(page_size-1) | markWord::lock_mask_in_place)
130     //
131     // assuming both the stack pointer and page_size have their least
132     // significant 2 bits cleared and page_size is a power of 2
133     z_sgr(hdr, Z_SP);
134 
135     load_const_optimized(Z_R0_scratch, (~(os::vm_page_size() - 1) | markWord::lock_mask_in_place));
136     z_ngr(hdr, Z_R0_scratch); // AND sets CC (result eq/ne 0).
137     // For recursive locking, the result is zero. => Save it in the displaced header
138     // location (null in the displaced hdr location indicates recursive locking).
139     z_stg(hdr, Address(disp_hdr, (intptr_t) 0));
140     // Otherwise we don't care about the result and handle locking via runtime call.
141     branch_optimized(Assembler::bcondNotZero, slow_case);
142     // done
143     bind(done);
144   }
145 }
146 
147 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
148   const int aligned_mask = BytesPerWord -1;
149   const int hdr_offset = oopDesc::mark_offset_in_bytes();
150   assert_different_registers(hdr, obj, disp_hdr);
151   NearLabel done;
152 
153   if (LockingMode != LM_LIGHTWEIGHT) {
154     // Load displaced header.
155     z_ltg(hdr, Address(disp_hdr, (intptr_t) 0));
156     // If the loaded hdr is null we had recursive locking, and we are done.
157     z_bre(done);
158   }
159 
160   // Load object.
161   z_lg(obj, Address(disp_hdr, BasicObjectLock::obj_offset()));
162   verify_oop(obj, FILE_AND_LINE);
163 
164   if (LockingMode == LM_LIGHTWEIGHT) {
165     Unimplemented();
166   } else {
167     // Test if object header is pointing to the displaced header, and if so, restore
168     // the displaced header in the object. If the object header is not pointing to
169     // the displaced header, get the object header instead.
170     z_csg(disp_hdr, hdr, hdr_offset, obj);
171     // If the object header was not pointing to the displaced header,
172     // we do unlocking via runtime call.
173     branch_optimized(Assembler::bcondNotEqual, slow_case);
174     // done
175   }
176   bind(done);
177 }
178 
179 void C1_MacroAssembler::try_allocate(
180   Register obj,                        // result: Pointer to object after successful allocation.
181   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
182   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
183   Register t1,                         // Temp register: Must be global register for incr_allocated_bytes.
184   Label&   slow_case                   // Continuation point if fast allocation fails.
185 ) {
186   if (UseTLAB) {
187     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
188   } else {
189     // Allocation in shared Eden not implemented, because sapjvm allocation trace does not allow it.
190     z_brul(slow_case);
191   }
192 }
193 
194 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register Rzero, Register t1) {
195   assert_different_registers(obj, klass, len, t1, Rzero);
196   // This assumes that all prototype bits fit in an int32_t.
197   load_const_optimized(t1, (intx)markWord::prototype().value());
198   z_stg(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
199 
200   if (len->is_valid()) {
201     // Length will be in the klass gap, if one exists.
202     z_st(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
203   } else if (UseCompressedClassPointers) {
204     store_klass_gap(Rzero, obj);  // Zero klass gap for compressed oops.
205   }
206   store_klass(klass, obj, t1);
207 }
208 
209 void C1_MacroAssembler::initialize_body(Register objectFields, Register len_in_bytes, Register Rzero) {
210   Label done;
211   assert_different_registers(objectFields, len_in_bytes, Rzero);
212 
213   // Initialize object fields.
214   // See documentation for MVCLE instruction!!!
215   assert(objectFields->encoding()%2==0, "objectFields must be an even register");
216   assert(len_in_bytes->encoding() == (objectFields->encoding()+1), "objectFields and len_in_bytes must be a register pair");
217   assert(Rzero->encoding()%2==1, "Rzero must be an odd register");
218 
219   // Use Rzero as src length, then mvcle will copy nothing
220   // and fill the object with the padding value 0.
221   move_long_ext(objectFields, as_Register(Rzero->encoding()-1), 0);
222   bind(done);
223 }
224 
225 void C1_MacroAssembler::allocate_object(
226   Register obj,                        // Result: pointer to object after successful allocation.
227   Register t1,                         // temp register
228   Register t2,                         // temp register: Must be a global register for try_allocate.
229   int      hdr_size,                   // object header size in words
230   int      obj_size,                   // object size in words
231   Register klass,                      // object klass
232   Label&   slow_case                   // Continuation point if fast allocation fails.
233 ) {
234   assert_different_registers(obj, t1, t2, klass);
235 
236   // Allocate space and initialize header.
237   try_allocate(obj, noreg, obj_size * wordSize, t1, slow_case);
238 
239   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
240 }
241 
242 void C1_MacroAssembler::initialize_object(
243   Register obj,                        // result: Pointer to object after successful allocation.
244   Register klass,                      // object klass
245   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
246   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
247   Register t1,                         // temp register
248   Register t2                          // temp register
249  ) {
250   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
251          "con_size_in_bytes is not multiple of alignment");
252   assert(var_size_in_bytes == noreg, "not implemented");
253   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
254 
255   const Register Rzero = t2;
256 
257   z_xgr(Rzero, Rzero);
258   initialize_header(obj, klass, noreg, Rzero, t1);
259 
260   // Clear rest of allocated space.
261   const int threshold = 4 * BytesPerWord;
262   if (con_size_in_bytes <= threshold) {
263     // Use explicit null stores.
264     // code size = 6*n bytes (n = number of fields to clear)
265     for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)
266       z_stg(Rzero, Address(obj, i));
267   } else {
268     // Code size generated by initialize_body() is 16.
269     Register object_fields = Z_R0_scratch;
270     Register len_in_bytes  = Z_R1_scratch;
271     z_la(object_fields, hdr_size_in_bytes, obj);
272     load_const_optimized(len_in_bytes, con_size_in_bytes - hdr_size_in_bytes);
273     initialize_body(object_fields, len_in_bytes, Rzero);
274   }
275 
276   // Dtrace support is unimplemented.
277   //  if (CURRENT_ENV->dtrace_alloc_probes()) {
278   //    assert(obj == rax, "must be");
279   //    call(RuntimeAddress(Runtime1::entry_for (Runtime1::dtrace_object_alloc_id)));
280   //  }
281 
282   verify_oop(obj, FILE_AND_LINE);
283 }
284 
285 void C1_MacroAssembler::allocate_array(
286   Register obj,                        // result: Pointer to array after successful allocation.
287   Register len,                        // array length
288   Register t1,                         // temp register
289   Register t2,                         // temp register
290   int      hdr_size,                   // object header size in words
291   int      elt_size,                   // element size in bytes
292   Register klass,                      // object klass
293   Label&   slow_case                   // Continuation point if fast allocation fails.
294 ) {
295   assert_different_registers(obj, len, t1, t2, klass);
296 
297   // Determine alignment mask.
298   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
299 
300   // Check for negative or excessive length.
301   compareU64_and_branch(len, (int32_t)max_array_allocation_length, bcondHigh, slow_case);
302 
303   // Compute array size.
304   // Note: If 0 <= len <= max_length, len*elt_size + header + alignment is
305   // smaller or equal to the largest integer. Also, since top is always
306   // aligned, we can do the alignment here instead of at the end address
307   // computation.
308   const Register arr_size = t2;
309   switch (elt_size) {
310     case  1: lgr_if_needed(arr_size, len); break;
311     case  2: z_sllg(arr_size, len, 1); break;
312     case  4: z_sllg(arr_size, len, 2); break;
313     case  8: z_sllg(arr_size, len, 3); break;
314     default: ShouldNotReachHere();
315   }
316   add2reg(arr_size, hdr_size * wordSize + MinObjAlignmentInBytesMask); // Add space for header & alignment.
317   z_nill(arr_size, (~MinObjAlignmentInBytesMask) & 0xffff);            // Align array size.
318 
319   try_allocate(obj, arr_size, 0, t1, slow_case);
320 
321   initialize_header(obj, klass, len, noreg, t1);
322 
323   // Clear rest of allocated space.
324   Label done;
325   Register object_fields = t1;
326   Register Rzero = Z_R1_scratch;
327   z_aghi(arr_size, -(hdr_size * BytesPerWord));
328   z_bre(done); // Jump if size of fields is zero.
329   z_la(object_fields, hdr_size * BytesPerWord, obj);
330   z_xgr(Rzero, Rzero);
331   initialize_body(object_fields, arr_size, Rzero);
332   bind(done);
333 
334   // Dtrace support is unimplemented.
335   // if (CURRENT_ENV->dtrace_alloc_probes()) {
336   //   assert(obj == rax, "must be");
337   //   call(RuntimeAddress(Runtime1::entry_for (Runtime1::dtrace_object_alloc_id)));
338   // }
339 
340   verify_oop(obj, FILE_AND_LINE);
341 }
342 
343 
344 #ifndef PRODUCT
345 
346 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
347   if (!VerifyOops) return;
348   verify_oop_addr(Address(Z_SP, stack_offset), FILE_AND_LINE);
349 }
350 
351 void C1_MacroAssembler::verify_not_null_oop(Register r) {
352   if (!VerifyOops) return;
353   NearLabel not_null;
354   compareU64_and_branch(r, (intptr_t)0, bcondNotEqual, not_null);
355   stop("non-null oop required");
356   bind(not_null);
357   verify_oop(r, FILE_AND_LINE);
358 }
359 
360 void C1_MacroAssembler::invalidate_registers(Register preserve1,
361                                              Register preserve2,
362                                              Register preserve3) {
363   Register dead_value = noreg;
364   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
365     Register r = as_Register(i);
366     if (r != preserve1 && r != preserve2 && r != preserve3 && r != Z_SP && r != Z_thread) {
367       if (dead_value == noreg) {
368         load_const_optimized(r, 0xc1dead);
369         dead_value = r;
370       } else {
371         z_lgr(r, dead_value);
372       }
373     }
374   }
375 }
376 
377 #endif // !PRODUCT