1 /*
  2  * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2021 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_ppc.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "utilities/macros.hpp"
 37 #include "vmreg_ppc.inline.hpp"
 38 
 39 #define __ ce->masm()->
 40 
 41 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
 42   if (UseSIGTRAP) {
 43     DEBUG_ONLY( __ should_not_reach_here("C1SafepointPollStub::emit_code"); )
 44   } else {
 45     assert(SharedRuntime::polling_page_return_handler_blob() != nullptr,
 46            "polling page return stub not created yet");
 47     address stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
 48 
 49     __ bind(_entry);
 50     // Using pc relative address computation.
 51     {
 52       Label next_pc;
 53       __ bl(next_pc);
 54       __ bind(next_pc);
 55     }
 56     int current_offset = __ offset();
 57     __ mflr(R12);
 58     __ add_const_optimized(R12, R12, safepoint_offset() - current_offset);
 59     __ std(R12, in_bytes(JavaThread::saved_exception_pc_offset()), R16_thread);
 60 
 61     __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
 62     __ mtctr(R0);
 63     __ bctr();
 64   }
 65 }
 66 
 67 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
 68   __ bind(_entry);
 69 
 70   if (_info->deoptimize_on_exception()) {
 71     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 72     //__ load_const_optimized(R0, a);
 73     __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a));
 74     __ mtctr(R0);
 75     __ bctrl();
 76     ce->add_call_info_here(_info);
 77     ce->verify_oop_map(_info);
 78     debug_only(__ illtrap());
 79     return;
 80   }
 81 
 82   address stub = _throw_index_out_of_bounds_exception ? Runtime1::entry_for(Runtime1::throw_index_exception_id)
 83                                                       : Runtime1::entry_for(Runtime1::throw_range_check_failed_id);
 84   //__ load_const_optimized(R0, stub);
 85   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
 86   __ mtctr(R0);
 87 
 88   Register index = R0;
 89   if (_index->is_register()) {
 90     __ extsw(index, _index->as_register());
 91   } else {
 92     __ load_const_optimized(index, _index->as_jint());
 93   }
 94   if (_array) {
 95     __ std(_array->as_pointer_register(), -8, R1_SP);
 96   }
 97   __ std(index, -16, R1_SP);
 98 
 99   __ bctrl();
100   ce->add_call_info_here(_info);
101   ce->verify_oop_map(_info);
102   debug_only(__ illtrap());
103 }
104 
105 
106 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
107   _info = new CodeEmitInfo(info);
108 }
109 
110 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
111   __ bind(_entry);
112   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
113   //__ load_const_optimized(R0, a);
114   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a));
115   __ mtctr(R0);
116   __ bctrl();
117   ce->add_call_info_here(_info);
118   ce->verify_oop_map(_info);
119   debug_only(__ illtrap());
120 }
121 
122 
123 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
124   __ bind(_entry);
125 
126   // Parameter 1: bci
127   __ load_const_optimized(R0, _bci);
128   __ std(R0, -16, R1_SP);
129 
130   // Parameter 2: Method*
131   Metadata *m = _method->as_constant_ptr()->as_metadata();
132   AddressLiteral md = __ constant_metadata_address(m); // Notify OOP recorder (don't need the relocation).
133   __ load_const_optimized(R0, md.value());
134   __ std(R0, -8, R1_SP);
135 
136   address a = Runtime1::entry_for(Runtime1::counter_overflow_id);
137   //__ load_const_optimized(R0, a);
138   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a));
139   __ mtctr(R0);
140   __ bctrl();
141   ce->add_call_info_here(_info);
142   ce->verify_oop_map(_info);
143 
144   __ b(_continuation);
145 }
146 
147 
148 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
149   if (_offset != -1) {
150     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
151   }
152   __ bind(_entry);
153   address stub = Runtime1::entry_for(Runtime1::throw_div0_exception_id);
154   //__ load_const_optimized(R0, stub);
155   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
156   __ mtctr(R0);
157   __ bctrl();
158   ce->add_call_info_here(_info);
159   ce->verify_oop_map(_info);
160   debug_only(__ illtrap());
161 }
162 
163 
164 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
165   address a;
166   if (_info->deoptimize_on_exception()) {
167     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
168     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
169   } else {
170     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
171   }
172 
173   if (ImplicitNullChecks || TrapBasedNullChecks) {
174     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
175   }
176   __ bind(_entry);
177   //__ load_const_optimized(R0, a);
178   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a));
179   __ mtctr(R0);
180   __ bctrl();
181   ce->add_call_info_here(_info);
182   ce->verify_oop_map(_info);
183   debug_only(__ illtrap());
184 }
185 
186 
187 // Implementation of SimpleExceptionStub
188 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
189   __ bind(_entry);
190   address stub = Runtime1::entry_for(_stub);
191   //__ load_const_optimized(R0, stub);
192   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
193   if (_obj->is_valid()) { __ mr_if_needed(/*tmp1 in do_CheckCast*/ R4_ARG2, _obj->as_register()); }
194   __ mtctr(R0);
195   __ bctrl();
196   ce->add_call_info_here(_info);
197   debug_only( __ illtrap(); )
198 }
199 
200 
201 // Implementation of NewInstanceStub
202 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
203   _result = result;
204   _klass = klass;
205   _klass_reg = klass_reg;
206   _info = new CodeEmitInfo(info);
207   assert(stub_id == Runtime1::new_instance_id                 ||
208          stub_id == Runtime1::fast_new_instance_id            ||
209          stub_id == Runtime1::fast_new_instance_init_check_id,
210          "need new_instance id");
211   _stub_id = stub_id;
212 }
213 
214 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
215   __ bind(_entry);
216 
217   address entry = Runtime1::entry_for(_stub_id);
218   //__ load_const_optimized(R0, entry);
219   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry));
220   __ mtctr(R0);
221   __ bctrl();
222   ce->add_call_info_here(_info);
223   ce->verify_oop_map(_info);
224   __ b(_continuation);
225 }
226 
227 
228 // Implementation of NewTypeArrayStub
229 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
230   _klass_reg = klass_reg;
231   _length = length;
232   _result = result;
233   _info = new CodeEmitInfo(info);
234 }
235 
236 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
237   __ bind(_entry);
238 
239   address entry = Runtime1::entry_for(Runtime1::new_type_array_id);
240   //__ load_const_optimized(R0, entry);
241   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry));
242   __ mr_if_needed(/*op->tmp1()->as_register()*/ R5_ARG3, _length->as_register()); // already sign-extended
243   __ mtctr(R0);
244   __ bctrl();
245   ce->add_call_info_here(_info);
246   ce->verify_oop_map(_info);
247   __ b(_continuation);
248 }
249 
250 
251 // Implementation of NewObjectArrayStub
252 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
253   _klass_reg = klass_reg;
254   _length = length;
255   _result = result;
256   _info = new CodeEmitInfo(info);
257 }
258 
259 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
260   __ bind(_entry);
261 
262   address entry = Runtime1::entry_for(Runtime1::new_object_array_id);
263   //__ load_const_optimized(R0, entry);
264   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry));
265   __ mr_if_needed(/*op->tmp1()->as_register()*/ R5_ARG3, _length->as_register()); // already sign-extended
266   __ mtctr(R0);
267   __ bctrl();
268   ce->add_call_info_here(_info);
269   ce->verify_oop_map(_info);
270   __ b(_continuation);
271 }
272 
273 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
274   __ bind(_entry);
275   address stub = Runtime1::entry_for(ce->compilation()->has_fpu_code() ? Runtime1::monitorenter_id : Runtime1::monitorenter_nofpu_id);
276   //__ load_const_optimized(R0, stub);
277   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
278   __ mr_if_needed(/*scratch_opr()->as_register()*/ R4_ARG2, _obj_reg->as_register());
279   assert(_lock_reg->as_register() == R5_ARG3, "");
280   __ mtctr(R0);
281   __ bctrl();
282   ce->add_call_info_here(_info);
283   ce->verify_oop_map(_info);
284   __ b(_continuation);
285 }
286 
287 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
288   __ bind(_entry);
289   if (_compute_lock) {
290     ce->monitor_address(_monitor_ix, _lock_reg);
291   }
292   address stub = Runtime1::entry_for(ce->compilation()->has_fpu_code() ? Runtime1::monitorexit_id : Runtime1::monitorexit_nofpu_id);
293   //__ load_const_optimized(R0, stub);
294   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
295   assert(_lock_reg->as_register() == R4_ARG2, "");
296   __ mtctr(R0);
297   __ bctrl();
298   __ b(_continuation);
299 }
300 
301 void LoadKlassStub::emit_code(LIR_Assembler* ce) {
302   // Currently not needed.
303   Unimplemented();
304 }
305 
306 // Implementation of patching:
307 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes).
308 // - Replace original code with a call to the stub.
309 // At Runtime:
310 // - call to stub, jump to runtime
311 // - in runtime: preserve all registers (especially objects, i.e., source and destination object)
312 // - in runtime: after initializing class, restore original code, reexecute instruction
313 
314 int PatchingStub::_patch_info_offset = -(5 * BytesPerInstWord);
315 
316 void PatchingStub::align_patch_site(MacroAssembler* ) {
317   // Patch sites on ppc are always properly aligned.
318 }
319 
320 #ifdef ASSERT
321 inline void compare_with_patch_site(address template_start, address pc_start, int bytes_to_copy) {
322   address start = template_start;
323   for (int i = 0; i < bytes_to_copy; i++) {
324     address ptr = (address)(pc_start + i);
325     int a_byte = (*ptr) & 0xFF;
326     assert(a_byte == *start++, "should be the same code");
327   }
328 }
329 #endif
330 
331 void PatchingStub::emit_code(LIR_Assembler* ce) {
332   // copy original code here
333   assert(NativeGeneralJump::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
334          "not enough room for call, need %d", _bytes_to_copy);
335   assert((_bytes_to_copy & 0x3) == 0, "must copy a multiple of four bytes");
336 
337   Label call_patch;
338 
339   int being_initialized_entry = __ offset();
340 
341   if (_id == load_klass_id) {
342     // Produce a copy of the load klass instruction for use by the being initialized case.
343     AddressLiteral addrlit((address)nullptr, metadata_Relocation::spec(_index));
344     __ load_const(_obj, addrlit, R0);
345     DEBUG_ONLY( compare_with_patch_site(__ code_section()->start() + being_initialized_entry, _pc_start, _bytes_to_copy); )
346   } else if (_id == load_mirror_id || _id == load_appendix_id) {
347     // Produce a copy of the load mirror instruction for use by the being initialized case.
348     AddressLiteral addrlit((address)nullptr, oop_Relocation::spec(_index));
349     __ load_const(_obj, addrlit, R0);
350     DEBUG_ONLY( compare_with_patch_site(__ code_section()->start() + being_initialized_entry, _pc_start, _bytes_to_copy); )
351   } else {
352     // Make a copy of the code which is going to be patched.
353     for (int i = 0; i < _bytes_to_copy; i++) {
354       address ptr = (address)(_pc_start + i);
355       int a_byte = (*ptr) & 0xFF;
356       __ emit_int8 (a_byte);
357     }
358   }
359 
360   address end_of_patch = __ pc();
361   int bytes_to_skip = 0;
362   if (_id == load_mirror_id) {
363     int offset = __ offset();
364     __ block_comment(" being_initialized check");
365 
366     // Static field accesses have special semantics while the class
367     // initializer is being run so we emit a test which can be used to
368     // check that this code is being executed by the initializing
369     // thread.
370     assert(_obj != noreg, "must be a valid register");
371     assert(_index >= 0, "must have oop index");
372     __ mr(R0, _obj); // spill
373     __ ld(_obj, java_lang_Class::klass_offset(), _obj);
374     __ ld(_obj, in_bytes(InstanceKlass::init_thread_offset()), _obj);
375     __ cmpd(CCR0, _obj, R16_thread);
376     __ mr(_obj, R0); // restore
377     __ bne(CCR0, call_patch);
378 
379     // Load_klass patches may execute the patched code before it's
380     // copied back into place so we need to jump back into the main
381     // code of the nmethod to continue execution.
382     __ b(_patch_site_continuation);
383 
384     // Make sure this extra code gets skipped.
385     bytes_to_skip += __ offset() - offset;
386   }
387 
388   // Now emit the patch record telling the runtime how to find the
389   // pieces of the patch.  We only need 3 bytes but it has to be
390   // aligned as an instruction so emit 4 bytes.
391   int sizeof_patch_record = 4;
392   bytes_to_skip += sizeof_patch_record;
393 
394   // Emit the offsets needed to find the code to patch.
395   int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record;
396 
397   // Emit the patch record.  We need to emit a full word, so emit an extra empty byte.
398   __ emit_int8(0);
399   __ emit_int8(being_initialized_entry_offset);
400   __ emit_int8(bytes_to_skip);
401   __ emit_int8(_bytes_to_copy);
402   address patch_info_pc = __ pc();
403   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
404 
405   address entry = __ pc();
406   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
407   address target = nullptr;
408   relocInfo::relocType reloc_type = relocInfo::none;
409   switch (_id) {
410     case access_field_id:  target = Runtime1::entry_for(Runtime1::access_field_patching_id); break;
411     case load_klass_id:    target = Runtime1::entry_for(Runtime1::load_klass_patching_id);
412                            reloc_type = relocInfo::metadata_type; break;
413     case load_mirror_id:   target = Runtime1::entry_for(Runtime1::load_mirror_patching_id);
414                            reloc_type = relocInfo::oop_type; break;
415     case load_appendix_id: target = Runtime1::entry_for(Runtime1::load_appendix_patching_id);
416                            reloc_type = relocInfo::oop_type; break;
417     default: ShouldNotReachHere();
418   }
419   __ bind(call_patch);
420 
421   __ block_comment("patch entry point");
422   //__ load_const(R0, target); + mtctr + bctrl must have size -_patch_info_offset
423   __ load_const32(R0, MacroAssembler::offset_to_global_toc(target));
424   __ add(R0, R29_TOC, R0);
425   __ mtctr(R0);
426   __ bctrl();
427   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
428   ce->add_call_info_here(_info);
429   __ b(_patch_site_entry);
430   if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) {
431     CodeSection* cs = __ code_section();
432     address pc = (address)_pc_start;
433     RelocIterator iter(cs, pc, pc + 1);
434     relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none);
435   }
436 }
437 
438 
439 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
440   __ bind(_entry);
441   address stub = Runtime1::entry_for(Runtime1::deoptimize_id);
442   //__ load_const_optimized(R0, stub);
443   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
444   __ mtctr(R0);
445 
446   __ load_const_optimized(R0, _trap_request); // Pass trap request in R0.
447   __ bctrl();
448   ce->add_call_info_here(_info);
449   debug_only(__ illtrap());
450 }
451 
452 
453 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
454   //---------------slow case: call to native-----------------
455   __ bind(_entry);
456   __ mr(R3_ARG1, src()->as_register());
457   __ extsw(R4_ARG2, src_pos()->as_register());
458   __ mr(R5_ARG3, dst()->as_register());
459   __ extsw(R6_ARG4, dst_pos()->as_register());
460   __ extsw(R7_ARG5, length()->as_register());
461 
462   ce->emit_static_call_stub();
463   if (ce->compilation()->bailed_out()) {
464     return; // CodeCache is full
465   }
466 
467   bool success = ce->emit_trampoline_stub_for_call(SharedRuntime::get_resolve_static_call_stub());
468   if (!success) { return; }
469 
470   __ relocate(relocInfo::static_call_type);
471   // Note: At this point we do not have the address of the trampoline
472   // stub, and the entry point might be too far away for bl, so __ pc()
473   // serves as dummy and the bl will be patched later.
474   __ code()->set_insts_mark();
475   __ bl(__ pc());
476   ce->add_call_info_here(info());
477   ce->verify_oop_map(info());
478 
479 #ifndef PRODUCT
480   if (PrintC1Statistics) {
481     const address counter = (address)&Runtime1::_arraycopy_slowcase_cnt;
482     const Register tmp = R3, tmp2 = R4;
483     int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true);
484     __ lwz(tmp2, simm16_offs, tmp);
485     __ addi(tmp2, tmp2, 1);
486     __ stw(tmp2, simm16_offs, tmp);
487   }
488 #endif
489 
490   __ b(_continuation);
491 }
492 
493 #undef __