1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2018 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 // Implementation of patching:
257 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes).
258 // - Replace original code with a call to the stub.
259 // At Runtime:
260 // - call to stub, jump to runtime.
261 // - in runtime: Preserve all registers (especially objects, i.e., source and destination object).
262 // - in runtime: After initializing class, restore original code, reexecute instruction.
263 
264 int PatchingStub::_patch_info_offset = - (12 /* load const */ + 2 /*BASR*/);
265 
266 void PatchingStub::align_patch_site(MacroAssembler* masm) {
267 #ifndef PRODUCT
268   const char* bc;
269   switch (_id) {
270   case access_field_id: bc = "patch site (access_field)"; break;
271   case load_klass_id: bc = "patch site (load_klass)"; break;
272   case load_mirror_id: bc = "patch site (load_mirror)"; break;
273   case load_appendix_id: bc = "patch site (load_appendix)"; break;
274   default: bc = "patch site (unknown patch id)"; break;
275   }
276   masm->block_comment(bc);
277 #endif
278 
279   masm->align(align_up((int)NativeGeneralJump::instruction_size, wordSize));
280 }
281 
282 void PatchingStub::emit_code(LIR_Assembler* ce) {
283   // Copy original code here.
284   assert(NativeGeneralJump::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
285          "not enough room for call, need %d", _bytes_to_copy);
286 
287   NearLabel call_patch;
288 
289   int being_initialized_entry = __ offset();
290 
291   if (_id == load_klass_id) {
292     // Produce a copy of the load klass instruction for use by the case being initialized.
293 #ifdef ASSERT
294     address start = __ pc();
295 #endif
296     AddressLiteral addrlit((intptr_t)0, metadata_Relocation::spec(_index));
297     __ load_const(_obj, addrlit);
298 
299 #ifdef ASSERT
300     for (int i = 0; i < _bytes_to_copy; i++) {
301       address ptr = (address)(_pc_start + i);
302       int a_byte = (*ptr) & 0xFF;
303       assert(a_byte == *start++, "should be the same code");
304     }
305 #endif
306   } else if (_id == load_mirror_id || _id == load_appendix_id) {
307     // Produce a copy of the load mirror instruction for use by the case being initialized.
308 #ifdef ASSERT
309     address start = __ pc();
310 #endif
311     AddressLiteral addrlit((intptr_t)0, oop_Relocation::spec(_index));
312     __ load_const(_obj, addrlit);
313 
314 #ifdef ASSERT
315     for (int i = 0; i < _bytes_to_copy; i++) {
316       address ptr = (address)(_pc_start + i);
317       int a_byte = (*ptr) & 0xFF;
318       assert(a_byte == *start++, "should be the same code");
319     }
320 #endif
321   } else {
322     // Make a copy of the code which is going to be patched.
323     for (int i = 0; i < _bytes_to_copy; i++) {
324       address ptr = (address)(_pc_start + i);
325       int a_byte = (*ptr) & 0xFF;
326       __ emit_int8 (a_byte);
327     }
328   }
329 
330   address end_of_patch = __ pc();
331   int bytes_to_skip = 0;
332   if (_id == load_mirror_id) {
333     int offset = __ offset();
334     if (CommentedAssembly) {
335       __ block_comment(" being_initialized check");
336     }
337 
338     // Static field accesses have special semantics while the class
339     // initializer is being run, so we emit a test which can be used to
340     // check that this code is being executed by the initializing
341     // thread.
342     assert(_obj != noreg, "must be a valid register");
343     assert(_index >= 0, "must have oop index");
344     __ z_lg(Z_R1_scratch, java_lang_Class::klass_offset(), _obj);
345     __ z_cg(Z_thread, Address(Z_R1_scratch, InstanceKlass::init_thread_offset()));
346     __ branch_optimized(Assembler::bcondNotEqual, call_patch);
347 
348     // Load_klass patches may execute the patched code before it's
349     // copied back into place so we need to jump back into the main
350     // code of the nmethod to continue execution.
351     __ branch_optimized(Assembler::bcondAlways, _patch_site_continuation);
352 
353     // Make sure this extra code gets skipped.
354     bytes_to_skip += __ offset() - offset;
355   }
356 
357   // Now emit the patch record telling the runtime how to find the
358   // pieces of the patch. We only need 3 bytes but to help the disassembler
359   // we make the data look like the following add instruction:
360   //   A R1, D2(X2, B2)
361   // which requires 4 bytes.
362   int sizeof_patch_record = 4;
363   bytes_to_skip += sizeof_patch_record;
364 
365   // Emit the offsets needed to find the code to patch.
366   int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record;
367 
368   // Emit the patch record: opcode of the add followed by 3 bytes patch record data.
369   __ emit_int8((int8_t)(A_ZOPC>>24));
370   __ emit_int8(being_initialized_entry_offset);
371   __ emit_int8(bytes_to_skip);
372   __ emit_int8(_bytes_to_copy);
373   address patch_info_pc = __ pc();
374   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
375 
376   address entry = __ pc();
377   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
378   address target = nullptr;
379   relocInfo::relocType reloc_type = relocInfo::none;
380   switch (_id) {
381     case access_field_id:  target = Runtime1::entry_for (Runtime1::access_field_patching_id); break;
382     case load_klass_id:    target = Runtime1::entry_for (Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
383     case load_mirror_id:   target = Runtime1::entry_for (Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
384     case load_appendix_id: target = Runtime1::entry_for (Runtime1::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
385     default: ShouldNotReachHere();
386   }
387   __ bind(call_patch);
388 
389   if (CommentedAssembly) {
390     __ block_comment("patch entry point");
391   }
392   // Cannot use call_c_opt() because its size is not constant.
393   __ load_const(Z_R1_scratch, target); // Must not optimize in order to keep constant _patch_info_offset constant.
394   __ z_basr(Z_R14, Z_R1_scratch);
395   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
396   ce->add_call_info_here(_info);
397   __ z_brcl(Assembler::bcondAlways, _patch_site_entry);
398   if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) {
399     CodeSection* cs = __ code_section();
400     address pc = (address)_pc_start;
401     RelocIterator iter(cs, pc, pc + 1);
402     relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none);
403   }
404 }
405 
406 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
407   __ bind(_entry);
408   __ load_const_optimized(Z_R1_scratch, _trap_request); // Pass trap request in Z_R1_scratch.
409   ce->emit_call_c(Runtime1::entry_for (Runtime1::deoptimize_id));
410   CHECK_BAILOUT();
411   ce->add_call_info_here(_info);
412   DEBUG_ONLY(__ should_not_reach_here());
413 }
414 
415 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
416   // Slow case: call to native.
417   __ bind(_entry);
418   __ lgr_if_needed(Z_ARG1, src()->as_register());
419   __ lgr_if_needed(Z_ARG2, src_pos()->as_register());
420   __ lgr_if_needed(Z_ARG3, dst()->as_register());
421   __ lgr_if_needed(Z_ARG4, dst_pos()->as_register());
422   __ lgr_if_needed(Z_ARG5, length()->as_register());
423 
424   // Must align calls sites, otherwise they can't be updated atomically on MP hardware.
425   ce->align_call(lir_static_call);
426 
427   assert((__ offset() + NativeCall::call_far_pcrelative_displacement_offset) % NativeCall::call_far_pcrelative_displacement_alignment == 0,
428          "must be aligned");
429 
430   ce->emit_static_call_stub();
431 
432   // Prepend each BRASL with a nop.
433   __ relocate(relocInfo::static_call_type);
434   __ z_nop();
435   __ z_brasl(Z_R14, SharedRuntime::get_resolve_static_call_stub());
436   ce->add_call_info_here(info());
437   ce->verify_oop_map(info());
438 
439 #ifndef PRODUCT
440   if (PrintC1Statistics) {
441     __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_slowcase_cnt);
442     __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
443   }
444 #endif
445 
446   __ branch_optimized(Assembler::bcondAlways, _continuation);
447 }
448 
449 #undef __
--- EOF ---