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