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