1 /*
  2  * Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2024 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 "asm/macroAssembler.inline.hpp"
 27 #include "c1/c1_CodeStubs.hpp"
 28 #include "c1/c1_FrameMap.hpp"
 29 #include "c1/c1_LIRAssembler.hpp"
 30 #include "c1/c1_MacroAssembler.hpp"
 31 #include "c1/c1_Runtime1.hpp"
 32 #include "classfile/javaClasses.hpp"
 33 #include "nativeInst_s390.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "utilities/align.hpp"
 36 #include "utilities/macros.hpp"
 37 #include "vmreg_s390.inline.hpp"
 38 
 39 #define __ ce->masm()->
 40 #undef  CHECK_BAILOUT
 41 #define CHECK_BAILOUT() { if (ce->compilation()->bailed_out()) return; }
 42 
 43 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
 44   ShouldNotReachHere();
 45 }
 46 
 47 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
 48   __ bind(_entry);
 49   if (_info->deoptimize_on_exception()) {
 50     address a = Runtime1::entry_for (StubId::c1_predicate_failed_trap_id);
 51     ce->emit_call_c(a);
 52     CHECK_BAILOUT();
 53     ce->add_call_info_here(_info);
 54     ce->verify_oop_map(_info);
 55     DEBUG_ONLY(__ should_not_reach_here());
 56     return;
 57   }
 58 
 59   // Pass the array index in Z_R1_scratch which is not managed by linear scan.
 60   if (_index->is_cpu_register()) {
 61     __ lgr_if_needed(Z_R1_scratch, _index->as_register());
 62   } else {
 63     __ load_const_optimized(Z_R1_scratch, _index->as_jint());
 64   }
 65 
 66   StubId stub_id;
 67   if (_throw_index_out_of_bounds_exception) {
 68     stub_id = StubId::c1_throw_index_exception_id;
 69   } else {
 70     stub_id = StubId::c1_throw_range_check_failed_id;
 71     __ lgr_if_needed(Z_R0_scratch, _array->as_pointer_register());
 72   }
 73   ce->emit_call_c(Runtime1::entry_for (stub_id));
 74   CHECK_BAILOUT();
 75   ce->add_call_info_here(_info);
 76   ce->verify_oop_map(_info);
 77   DEBUG_ONLY(__ should_not_reach_here());
 78 }
 79 
 80 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
 81   _info = new CodeEmitInfo(info);
 82 }
 83 
 84 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
 85   __ bind(_entry);
 86   address a = Runtime1::entry_for (StubId::c1_predicate_failed_trap_id);
 87   ce->emit_call_c(a);
 88   CHECK_BAILOUT();
 89   ce->add_call_info_here(_info);
 90   ce->verify_oop_map(_info);
 91   DEBUG_ONLY(__ should_not_reach_here());
 92 }
 93 
 94 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
 95   __ bind(_entry);
 96   Metadata *m = _method->as_constant_ptr()->as_metadata();
 97   bool success = __ set_metadata_constant(m, Z_R1_scratch);
 98   if (!success) {
 99     ce->compilation()->bailout("const section overflow");
100     return;
101   }
102   ce->store_parameter(/*_method->as_register()*/ Z_R1_scratch, 1);
103   ce->store_parameter(_bci, 0);
104   ce->emit_call_c(Runtime1::entry_for (StubId::c1_counter_overflow_id));
105   CHECK_BAILOUT();
106   ce->add_call_info_here(_info);
107   ce->verify_oop_map(_info);
108   __ branch_optimized(Assembler::bcondAlways, _continuation);
109 }
110 
111 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
112   if (_offset != -1) {
113     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
114   }
115   __ bind(_entry);
116   ce->emit_call_c(Runtime1::entry_for (StubId::c1_throw_div0_exception_id));
117   CHECK_BAILOUT();
118   ce->add_call_info_here(_info);
119   DEBUG_ONLY(__ should_not_reach_here());
120 }
121 
122 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
123   address a;
124   if (_info->deoptimize_on_exception()) {
125     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
126     a = Runtime1::entry_for (StubId::c1_predicate_failed_trap_id);
127   } else {
128     a = Runtime1::entry_for (StubId::c1_throw_null_pointer_exception_id);
129   }
130 
131   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
132   __ bind(_entry);
133   ce->emit_call_c(a);
134   CHECK_BAILOUT();
135   ce->add_call_info_here(_info);
136   ce->verify_oop_map(_info);
137   DEBUG_ONLY(__ should_not_reach_here());
138 }
139 
140 // Note: pass object in Z_R1_scratch
141 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
142   __ bind(_entry);
143   if (_obj->is_valid()) {
144     __ z_lgr(Z_R1_scratch, _obj->as_register()); // _obj contains the optional argument to the stub
145   }
146   address a = Runtime1::entry_for (_stub);
147   ce->emit_call_c(a);
148   CHECK_BAILOUT();
149   ce->add_call_info_here(_info);
150   DEBUG_ONLY(__ should_not_reach_here());
151 }
152 
153 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, StubId stub_id) {
154   _result = result;
155   _klass = klass;
156   _klass_reg = klass_reg;
157   _info = new CodeEmitInfo(info);
158   assert(stub_id == StubId::c1_new_instance_id                 ||
159          stub_id == StubId::c1_fast_new_instance_id            ||
160          stub_id == StubId::c1_fast_new_instance_init_check_id,
161          "need new_instance id");
162   _stub_id = stub_id;
163 }
164 
165 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
166   __ bind(_entry);
167   assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11");
168   address a = Runtime1::entry_for (_stub_id);
169   ce->emit_call_c(a);
170   CHECK_BAILOUT();
171   ce->add_call_info_here(_info);
172   ce->verify_oop_map(_info);
173   assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,");
174   __ z_brul(_continuation);
175 }
176 
177 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
178   _klass_reg = klass_reg;
179   _length = length;
180   _result = result;
181   _info = new CodeEmitInfo(info);
182 }
183 
184 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
185   __ bind(_entry);
186   assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11");
187   __ lgr_if_needed(Z_R13, _length->as_register());
188   address a = Runtime1::entry_for (StubId::c1_new_type_array_id);
189   ce->emit_call_c(a);
190   CHECK_BAILOUT();
191   ce->add_call_info_here(_info);
192   ce->verify_oop_map(_info);
193   assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,");
194   __ z_brul(_continuation);
195 }
196 
197 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
198   _klass_reg = klass_reg;
199   _length = length;
200   _result = result;
201   _info = new CodeEmitInfo(info);
202 }
203 
204 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
205   __ bind(_entry);
206   assert(_klass_reg->as_register() == Z_R11, "call target expects klass in Z_R11");
207   __ lgr_if_needed(Z_R13, _length->as_register());
208   address a = Runtime1::entry_for (StubId::c1_new_object_array_id);
209   ce->emit_call_c(a);
210   CHECK_BAILOUT();
211   ce->add_call_info_here(_info);
212   ce->verify_oop_map(_info);
213   assert(_result->as_register() == Z_R2, "callee returns result in Z_R2,");
214   __ z_brul(_continuation);
215 }
216 
217 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
218   __ bind(_entry);
219   StubId enter_id;
220   if (ce->compilation()->has_fpu_code()) {
221     enter_id = StubId::c1_monitorenter_id;
222   } else {
223     enter_id = StubId::c1_monitorenter_nofpu_id;
224   }
225   __ lgr_if_needed(Z_R1_scratch, _obj_reg->as_register());
226   __ lgr_if_needed(Z_R13, _lock_reg->as_register()); // See LIRGenerator::syncTempOpr().
227   ce->emit_call_c(Runtime1::entry_for (enter_id));
228   CHECK_BAILOUT();
229   ce->add_call_info_here(_info);
230   ce->verify_oop_map(_info);
231   __ branch_optimized(Assembler::bcondAlways, _continuation);
232 }
233 
234 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
235   __ bind(_entry);
236   // Move address of the BasicObjectLock into Z_R1_scratch.
237 
238   // Lock_reg was destroyed by fast unlocking attempt => recompute it.
239   ce->monitor_address(_monitor_ix, FrameMap::as_opr(Z_R1_scratch));
240 
241   // Note: non-blocking leaf routine => no call info needed.
242   StubId exit_id;
243   if (ce->compilation()->has_fpu_code()) {
244     exit_id = StubId::c1_monitorexit_id;
245   } else {
246     exit_id = StubId::c1_monitorexit_nofpu_id;
247   }
248   ce->emit_call_c(Runtime1::entry_for (exit_id));
249   CHECK_BAILOUT();
250   __ branch_optimized(Assembler::bcondAlways, _continuation);
251 }
252 
253 // Implementation of patching:
254 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes).
255 // - Replace original code with a call to the stub.
256 // At Runtime:
257 // - call to stub, jump to runtime.
258 // - in runtime: Preserve all registers (especially objects, i.e., source and destination object).
259 // - in runtime: After initializing class, restore original code, reexecute instruction.
260 
261 int PatchingStub::_patch_info_offset = - (12 /* load const */ + 2 /*BASR*/);
262 
263 void PatchingStub::align_patch_site(MacroAssembler* masm) {
264 #ifndef PRODUCT
265   const char* bc;
266   switch (_id) {
267   case access_field_id: bc = "patch site (access_field)"; break;
268   case load_klass_id: bc = "patch site (load_klass)"; break;
269   case load_mirror_id: bc = "patch site (load_mirror)"; break;
270   case load_appendix_id: bc = "patch site (load_appendix)"; break;
271   default: bc = "patch site (unknown patch id)"; break;
272   }
273   masm->block_comment(bc);
274 #endif
275 
276   masm->align(align_up((int)NativeGeneralJump::instruction_size, wordSize));
277 }
278 
279 void PatchingStub::emit_code(LIR_Assembler* ce) {
280   // Copy original code here.
281   assert(NativeGeneralJump::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
282          "not enough room for call, need %d", _bytes_to_copy);
283 
284   NearLabel call_patch;
285 
286   int being_initialized_entry = __ offset();
287 
288   if (_id == load_klass_id) {
289     // Produce a copy of the load klass instruction for use by the case being initialized.
290 #ifdef ASSERT
291     address start = __ pc();
292 #endif
293     AddressLiteral addrlit((intptr_t)0, metadata_Relocation::spec(_index));
294     __ load_const(_obj, addrlit);
295 
296 #ifdef ASSERT
297     for (int i = 0; i < _bytes_to_copy; i++) {
298       address ptr = (address)(_pc_start + i);
299       int a_byte = (*ptr) & 0xFF;
300       assert(a_byte == *start++, "should be the same code");
301     }
302 #endif
303   } else if (_id == load_mirror_id || _id == load_appendix_id) {
304     // Produce a copy of the load mirror instruction for use by the case being initialized.
305 #ifdef ASSERT
306     address start = __ pc();
307 #endif
308     AddressLiteral addrlit((intptr_t)0, oop_Relocation::spec(_index));
309     __ load_const(_obj, addrlit);
310 
311 #ifdef ASSERT
312     for (int i = 0; i < _bytes_to_copy; i++) {
313       address ptr = (address)(_pc_start + i);
314       int a_byte = (*ptr) & 0xFF;
315       assert(a_byte == *start++, "should be the same code");
316     }
317 #endif
318   } else {
319     // Make a copy of the code which is going to be patched.
320     for (int i = 0; i < _bytes_to_copy; i++) {
321       address ptr = (address)(_pc_start + i);
322       int a_byte = (*ptr) & 0xFF;
323       __ emit_int8 (a_byte);
324     }
325   }
326 
327   address end_of_patch = __ pc();
328   int bytes_to_skip = 0;
329   if (_id == load_mirror_id) {
330     int offset = __ offset();
331     if (CommentedAssembly) {
332       __ block_comment(" being_initialized check");
333     }
334 
335     // Static field accesses have special semantics while the class
336     // initializer is being run, so we emit a test which can be used to
337     // check that this code is being executed by the initializing
338     // thread.
339     assert(_obj != noreg, "must be a valid register");
340     assert(_index >= 0, "must have oop index");
341     __ z_lg(Z_R1_scratch, java_lang_Class::klass_offset(), _obj);
342     __ z_cg(Z_thread, Address(Z_R1_scratch, InstanceKlass::init_thread_offset()));
343     __ branch_optimized(Assembler::bcondNotEqual, call_patch);
344 
345     // Load_klass patches may execute the patched code before it's
346     // copied back into place so we need to jump back into the main
347     // code of the nmethod to continue execution.
348     __ branch_optimized(Assembler::bcondAlways, _patch_site_continuation);
349 
350     // Make sure this extra code gets skipped.
351     bytes_to_skip += __ offset() - offset;
352   }
353 
354   // Now emit the patch record telling the runtime how to find the
355   // pieces of the patch. We only need 3 bytes but to help the disassembler
356   // we make the data look like the following add instruction:
357   //   A R1, D2(X2, B2)
358   // which requires 4 bytes.
359   int sizeof_patch_record = 4;
360   bytes_to_skip += sizeof_patch_record;
361 
362   // Emit the offsets needed to find the code to patch.
363   int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record;
364 
365   // Emit the patch record: opcode of the add followed by 3 bytes patch record data.
366   __ emit_int8((int8_t)(A_ZOPC>>24));
367   __ emit_int8(being_initialized_entry_offset);
368   __ emit_int8(bytes_to_skip);
369   __ emit_int8(_bytes_to_copy);
370   address patch_info_pc = __ pc();
371   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
372 
373   address entry = __ pc();
374   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
375   address target = nullptr;
376   relocInfo::relocType reloc_type = relocInfo::none;
377   switch (_id) {
378     case access_field_id:  target = Runtime1::entry_for (StubId::c1_access_field_patching_id); break;
379     case load_klass_id:    target = Runtime1::entry_for (StubId::c1_load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
380     case load_mirror_id:   target = Runtime1::entry_for (StubId::c1_load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
381     case load_appendix_id: target = Runtime1::entry_for (StubId::c1_load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
382     default: ShouldNotReachHere();
383   }
384   __ bind(call_patch);
385 
386   if (CommentedAssembly) {
387     __ block_comment("patch entry point");
388   }
389   // Cannot use call_c_opt() because its size is not constant.
390   __ load_const(Z_R1_scratch, target); // Must not optimize in order to keep constant _patch_info_offset constant.
391   __ z_basr(Z_R14, Z_R1_scratch);
392   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
393   ce->add_call_info_here(_info);
394   __ z_brcl(Assembler::bcondAlways, _patch_site_entry);
395   if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) {
396     CodeSection* cs = __ code_section();
397     address pc = (address)_pc_start;
398     RelocIterator iter(cs, pc, pc + 1);
399     relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none);
400   }
401 }
402 
403 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
404   __ bind(_entry);
405   __ load_const_optimized(Z_R1_scratch, _trap_request); // Pass trap request in Z_R1_scratch.
406   ce->emit_call_c(Runtime1::entry_for (StubId::c1_deoptimize_id));
407   CHECK_BAILOUT();
408   ce->add_call_info_here(_info);
409   DEBUG_ONLY(__ should_not_reach_here());
410 }
411 
412 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
413   // Slow case: call to native.
414   __ bind(_entry);
415   __ lgr_if_needed(Z_ARG1, src()->as_register());
416   __ lgr_if_needed(Z_ARG2, src_pos()->as_register());
417   __ lgr_if_needed(Z_ARG3, dst()->as_register());
418   __ lgr_if_needed(Z_ARG4, dst_pos()->as_register());
419   __ lgr_if_needed(Z_ARG5, length()->as_register());
420 
421   // Must align calls sites, otherwise they can't be updated atomically on MP hardware.
422   ce->align_call(lir_static_call);
423 
424   assert((__ offset() + NativeCall::call_far_pcrelative_displacement_offset) % NativeCall::call_far_pcrelative_displacement_alignment == 0,
425          "must be aligned");
426 
427   ce->emit_static_call_stub();
428   CHECK_BAILOUT();
429 
430   // Prepend each BRASL with a nop.
431   __ relocate(relocInfo::static_call_type);
432   __ z_nop();
433   __ z_brasl(Z_R14, SharedRuntime::get_resolve_static_call_stub());
434   ce->add_call_info_here(info());
435   ce->verify_oop_map(info());
436 
437 #ifndef PRODUCT
438   if (PrintC1Statistics) {
439     __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_slowcase_cnt);
440     __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
441   }
442 #endif
443 
444   __ branch_optimized(Assembler::bcondAlways, _continuation);
445 }
446 
447 #undef __