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