1 /*
  2  * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2023 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_Defs.hpp"
 29 #include "c1/c1_MacroAssembler.hpp"
 30 #include "c1/c1_Runtime1.hpp"
 31 #include "ci/ciUtilities.hpp"
 32 #include "compiler/oopMap.hpp"
 33 #include "gc/shared/cardTable.hpp"
 34 #include "gc/shared/cardTableBarrierSet.hpp"
 35 #include "interpreter/interpreter.hpp"
 36 #include "nativeInst_ppc.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "prims/jvmtiExport.hpp"
 39 #include "register_ppc.hpp"
 40 #include "runtime/sharedRuntime.hpp"
 41 #include "runtime/signature.hpp"
 42 #include "runtime/vframeArray.hpp"
 43 #include "utilities/align.hpp"
 44 #include "utilities/macros.hpp"
 45 #include "utilities/powerOfTwo.hpp"
 46 #include "vmreg_ppc.inline.hpp"
 47 
 48 // Implementation of StubAssembler
 49 
 50 int StubAssembler::call_RT(Register oop_result1, Register metadata_result,
 51                            address entry_point, int number_of_arguments) {
 52   set_num_rt_args(0); // Nothing on stack
 53   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) ||
 54          oop_result1 != metadata_result, "registers must be different");
 55 
 56   // Currently no stack banging. We assume that there are enough
 57   // StackShadowPages (which have been banged in generate_stack_overflow_check)
 58   // for the stub frame and the runtime frames.
 59 
 60   set_last_Java_frame(R1_SP, noreg);
 61 
 62   // ARG1 must hold thread address.
 63   mr(R3_ARG1, R16_thread);
 64 
 65   address return_pc = call_c_with_frame_resize(entry_point, /*No resize, we have a C compatible frame.*/0);
 66 
 67   reset_last_Java_frame();
 68 
 69   // Check for pending exceptions.
 70   {
 71     ld(R0, in_bytes(Thread::pending_exception_offset()), R16_thread);
 72     cmpdi(CCR0, R0, 0);
 73 
 74     // This used to conditionally jump to forward_exception however it is
 75     // possible if we relocate that the branch will not reach. So we must jump
 76     // around so we can always reach.
 77 
 78     Label ok;
 79     beq(CCR0, ok);
 80 
 81     // Make sure that the vm_results are cleared.
 82     if (oop_result1->is_valid() || metadata_result->is_valid()) {
 83       li(R0, 0);
 84       if (oop_result1->is_valid()) {
 85         std(R0, in_bytes(JavaThread::vm_result_offset()), R16_thread);
 86       }
 87       if (metadata_result->is_valid()) {
 88         std(R0, in_bytes(JavaThread::vm_result_2_offset()), R16_thread);
 89       }
 90     }
 91 
 92     if (frame_size() == no_frame_size) {
 93       ShouldNotReachHere(); // We always have a frame size.
 94       //pop_frame(); // pop the stub frame
 95       //ld(R0, _abi0(lr), R1_SP);
 96       //mtlr(R0);
 97       //load_const_optimized(R0, StubRoutines::forward_exception_entry());
 98       //mtctr(R0);
 99       //bctr();
100     } else if (_stub_id == Runtime1::forward_exception_id) {
101       should_not_reach_here();
102     } else {
103       // keep stub frame for next call_RT
104       //load_const_optimized(R0, Runtime1::entry_for(Runtime1::forward_exception_id));
105       add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(Runtime1::entry_for(Runtime1::forward_exception_id)));
106       mtctr(R0);
107       bctr();
108     }
109 
110     bind(ok);
111   }
112 
113   // Get oop results if there are any and reset the values in the thread.
114   if (oop_result1->is_valid()) {
115     get_vm_result(oop_result1);
116   }
117   if (metadata_result->is_valid()) {
118     get_vm_result_2(metadata_result);
119   }
120 
121   return (int)(return_pc - code_section()->start());
122 }
123 
124 
125 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
126   mr_if_needed(R4_ARG2, arg1);
127   return call_RT(oop_result1, metadata_result, entry, 1);
128 }
129 
130 
131 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
132   mr_if_needed(R4_ARG2, arg1);
133   mr_if_needed(R5_ARG3, arg2); assert(arg2 != R4_ARG2, "smashed argument");
134   return call_RT(oop_result1, metadata_result, entry, 2);
135 }
136 
137 
138 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
139   mr_if_needed(R4_ARG2, arg1);
140   mr_if_needed(R5_ARG3, arg2); assert(arg2 != R4_ARG2, "smashed argument");
141   mr_if_needed(R6_ARG4, arg3); assert(arg3 != R4_ARG2 && arg3 != R5_ARG3, "smashed argument");
142   return call_RT(oop_result1, metadata_result, entry, 3);
143 }
144 
145 
146 // Implementation of Runtime1
147 
148 #define __ sasm->
149 
150 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
151 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
152 static int frame_size_in_bytes = -1;
153 
154 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
155   assert(frame_size_in_bytes > frame::native_abi_reg_args_size, "init");
156   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
157   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
158   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
159 
160   int i;
161   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
162     Register r = as_Register(i);
163     if (FrameMap::reg_needs_save(r)) {
164       int sp_offset = cpu_reg_save_offsets[i];
165       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset>>2), r->as_VMReg());
166       oop_map->set_callee_saved(VMRegImpl::stack2reg((sp_offset>>2) + 1), r->as_VMReg()->next());
167     }
168   }
169 
170   if (save_fpu_registers) {
171     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
172       FloatRegister r = as_FloatRegister(i);
173       int sp_offset = fpu_reg_save_offsets[i];
174       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset>>2), r->as_VMReg());
175       oop_map->set_callee_saved(VMRegImpl::stack2reg((sp_offset>>2) + 1), r->as_VMReg()->next());
176     }
177   }
178 
179   return oop_map;
180 }
181 
182 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true,
183                                    Register ret_pc = noreg, int stack_preserve = 0) {
184   if (ret_pc == noreg) {
185     ret_pc = R0;
186     __ mflr(ret_pc);
187   }
188   __ std(ret_pc, _abi0(lr), R1_SP); // C code needs pc in C1 method.
189   __ push_frame(frame_size_in_bytes + stack_preserve, R0);
190 
191   // Record volatile registers as callee-save values in an OopMap so
192   // their save locations will be propagated to the caller frame's
193   // RegisterMap during StackFrameStream construction (needed for
194   // deoptimization; see compiledVFrame::create_stack_value).
195   // OopMap frame sizes are in c2 stack slot sizes (sizeof(jint)).
196 
197   int i;
198   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
199     Register r = as_Register(i);
200     if (FrameMap::reg_needs_save(r)) {
201       int sp_offset = cpu_reg_save_offsets[i];
202       __ std(r, sp_offset, R1_SP);
203     }
204   }
205 
206   if (save_fpu_registers) {
207     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
208       FloatRegister r = as_FloatRegister(i);
209       int sp_offset = fpu_reg_save_offsets[i];
210       __ stfd(r, sp_offset, R1_SP);
211     }
212   }
213 
214   return generate_oop_map(sasm, save_fpu_registers);
215 }
216 
217 static void restore_live_registers(StubAssembler* sasm, Register result1, Register result2,
218                                    bool restore_fpu_registers = true) {
219   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
220     Register r = as_Register(i);
221     if (FrameMap::reg_needs_save(r) && r != result1 && r != result2) {
222       int sp_offset = cpu_reg_save_offsets[i];
223       __ ld(r, sp_offset, R1_SP);
224     }
225   }
226 
227   if (restore_fpu_registers) {
228     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
229       FloatRegister r = as_FloatRegister(i);
230       int sp_offset = fpu_reg_save_offsets[i];
231       __ lfd(r, sp_offset, R1_SP);
232     }
233   }
234 
235   __ pop_frame();
236   __ ld(R0, _abi0(lr), R1_SP);
237   __ mtlr(R0);
238 }
239 
240 
241 void Runtime1::initialize_pd() {
242   int i;
243   int sp_offset = frame::native_abi_reg_args_size;
244 
245   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
246     Register r = as_Register(i);
247     if (FrameMap::reg_needs_save(r)) {
248       cpu_reg_save_offsets[i] = sp_offset;
249       sp_offset += BytesPerWord;
250     }
251   }
252 
253   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
254     fpu_reg_save_offsets[i] = sp_offset;
255     sp_offset += BytesPerWord;
256   }
257   frame_size_in_bytes = align_up(sp_offset, frame::alignment_in_bytes);
258 }
259 
260 uint Runtime1::runtime_blob_current_thread_offset(frame f) {
261   Unimplemented();
262   return 0;
263 }
264 
265 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
266   // Make a frame and preserve the caller's caller-save registers.
267   OopMap* oop_map = save_live_registers(sasm);
268 
269   int call_offset;
270   if (!has_argument) {
271     call_offset = __ call_RT(noreg, noreg, target);
272   } else {
273     call_offset = __ call_RT(noreg, noreg, target, R4_ARG2);
274   }
275   OopMapSet* oop_maps = new OopMapSet();
276   oop_maps->add_gc_map(call_offset, oop_map);
277 
278   __ should_not_reach_here();
279   return oop_maps;
280 }
281 
282 static OopMapSet* generate_exception_throw_with_stack_parms(StubAssembler* sasm, address target,
283                                                             int stack_parms) {
284   // Make a frame and preserve the caller's caller-save registers.
285   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
286   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
287   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
288 
289   int call_offset = 0;
290   switch (stack_parms) {
291     case 3:
292     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
293     case 2:
294     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
295     case 1:
296     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
297     case 0:
298     call_offset = __ call_RT(noreg, noreg, target);
299     break;
300     default: Unimplemented(); break;
301   }
302   OopMapSet* oop_maps = new OopMapSet();
303   oop_maps->add_gc_map(call_offset, oop_map);
304 
305   __ should_not_reach_here();
306   return oop_maps;
307 }
308 
309 
310 OopMapSet* Runtime1::generate_stub_call(StubAssembler* sasm, Register result, address target,
311                                         Register arg1, Register arg2, Register arg3) {
312   // Make a frame and preserve the caller's caller-save registers.
313   OopMap* oop_map = save_live_registers(sasm);
314 
315   int call_offset;
316   if (arg1 == noreg) {
317     call_offset = __ call_RT(result, noreg, target);
318   } else if (arg2 == noreg) {
319     call_offset = __ call_RT(result, noreg, target, arg1);
320   } else if (arg3 == noreg) {
321     call_offset = __ call_RT(result, noreg, target, arg1, arg2);
322   } else {
323     call_offset = __ call_RT(result, noreg, target, arg1, arg2, arg3);
324   }
325   OopMapSet* oop_maps = new OopMapSet();
326   oop_maps->add_gc_map(call_offset, oop_map);
327 
328   restore_live_registers(sasm, result, noreg);
329   __ blr();
330   return oop_maps;
331 }
332 
333 static OopMapSet* stub_call_with_stack_parms(StubAssembler* sasm, Register result, address target,
334                                              int stack_parms, bool do_return = true) {
335   // Make a frame and preserve the caller's caller-save registers.
336   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
337   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
338   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
339 
340   int call_offset = 0;
341   switch (stack_parms) {
342     case 3:
343     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
344     case 2:
345     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
346     case 1:
347     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
348     case 0:
349     call_offset = __ call_RT(result, noreg, target);
350     break;
351     default: Unimplemented(); break;
352   }
353   OopMapSet* oop_maps = new OopMapSet();
354   oop_maps->add_gc_map(call_offset, oop_map);
355 
356   restore_live_registers(sasm, result, noreg);
357   if (do_return) __ blr();
358   return oop_maps;
359 }
360 
361 
362 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
363   // Make a frame and preserve the caller's caller-save registers.
364   OopMap* oop_map = save_live_registers(sasm);
365 
366   // Call the runtime patching routine, returns non-zero if nmethod got deopted.
367   int call_offset = __ call_RT(noreg, noreg, target);
368   OopMapSet* oop_maps = new OopMapSet();
369   oop_maps->add_gc_map(call_offset, oop_map);
370   __ cmpdi(CCR0, R3_RET, 0);
371 
372   // Re-execute the patched instruction or, if the nmethod was deoptmized,
373   // return to the deoptimization handler entry that will cause re-execution
374   // of the current bytecode.
375   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
376   assert(deopt_blob != nullptr, "deoptimization blob must have been created");
377 
378   // Return to the deoptimization handler entry for unpacking and rexecute.
379   // If we simply returned the we'd deopt as if any call we patched had just
380   // returned.
381 
382   restore_live_registers(sasm, noreg, noreg);
383   // Return if patching routine returned 0.
384   __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CCR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
385 
386   address stub = deopt_blob->unpack_with_reexecution();
387   //__ load_const_optimized(R0, stub);
388   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
389   __ mtctr(R0);
390   __ bctr();
391 
392   return oop_maps;
393 }
394 
395 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
396   OopMapSet* oop_maps = nullptr;
397 
398   // For better readability.
399   const bool must_gc_arguments = true;
400   const bool dont_gc_arguments = false;
401 
402   // Stub code & info for the different stubs.
403   switch (id) {
404     case forward_exception_id:
405       {
406         oop_maps = generate_handle_exception(id, sasm);
407       }
408       break;
409 
410     case new_instance_id:
411     case fast_new_instance_id:
412     case fast_new_instance_init_check_id:
413       {
414         if (id == new_instance_id) {
415           __ set_info("new_instance", dont_gc_arguments);
416         } else if (id == fast_new_instance_id) {
417           __ set_info("fast new_instance", dont_gc_arguments);
418         } else {
419           assert(id == fast_new_instance_init_check_id, "bad StubID");
420           __ set_info("fast new_instance init check", dont_gc_arguments);
421         }
422 
423         // We don't support eden allocation.
424 
425         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_instance), R4_ARG2);
426       }
427       break;
428 
429     case counter_overflow_id:
430         // Bci and method are on stack.
431         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, counter_overflow), 2);
432       break;
433 
434     case new_type_array_id:
435     case new_object_array_id:
436       {
437         if (id == new_type_array_id) {
438           __ set_info("new_type_array", dont_gc_arguments);
439         } else {
440           __ set_info("new_object_array", dont_gc_arguments);
441         }
442 
443 #ifdef ASSERT
444         // Assert object type is really an array of the proper kind.
445         {
446           int tag = (id == new_type_array_id) ? Klass::_lh_array_tag_type_value : Klass::_lh_array_tag_obj_value;
447           Label ok;
448           __ lwz(R0, in_bytes(Klass::layout_helper_offset()), R4_ARG2);
449           __ srawi(R0, R0, Klass::_lh_array_tag_shift);
450           __ cmpwi(CCR0, R0, tag);
451           __ beq(CCR0, ok);
452           __ stop("assert(is an array klass)");
453           __ should_not_reach_here();
454           __ bind(ok);
455         }
456 #endif // ASSERT
457 
458         // We don't support eden allocation.
459 
460         if (id == new_type_array_id) {
461           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_type_array), R4_ARG2, R5_ARG3);
462         } else {
463           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_object_array), R4_ARG2, R5_ARG3);
464         }
465       }
466       break;
467 
468     case new_multi_array_id:
469       {
470         // R4: klass
471         // R5: rank
472         // R6: address of 1st dimension
473         __ set_info("new_multi_array", dont_gc_arguments);
474         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_multi_array), R4_ARG2, R5_ARG3, R6_ARG4);
475       }
476       break;
477 
478     case register_finalizer_id:
479       {
480         __ set_info("register_finalizer", dont_gc_arguments);
481         // This code is called via rt_call. Hence, caller-save registers have been saved.
482         Register t = R11_scratch1;
483 
484         // Load the klass and check the has finalizer flag.
485         __ load_klass(t, R3_ARG1);
486         __ lwz(t, in_bytes(Klass::access_flags_offset()), t);
487         __ testbitdi(CCR0, R0, t, exact_log2(JVM_ACC_HAS_FINALIZER));
488         // Return if has_finalizer bit == 0 (CR0.eq).
489         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CCR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
490 
491         __ mflr(R0);
492         __ std(R0, _abi0(lr), R1_SP);
493         __ push_frame(frame::native_abi_reg_args_size, R0); // Empty dummy frame (no callee-save regs).
494         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
495         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
496         int call_offset = __ call_RT(noreg, noreg,
497                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), R3_ARG1);
498         oop_maps = new OopMapSet();
499         oop_maps->add_gc_map(call_offset, oop_map);
500 
501         __ pop_frame();
502         __ ld(R0, _abi0(lr), R1_SP);
503         __ mtlr(R0);
504         __ blr();
505       }
506       break;
507 
508     case throw_range_check_failed_id:
509       {
510         __ set_info("range_check_failed", dont_gc_arguments); // Arguments will be discarded.
511         oop_maps = generate_exception_throw_with_stack_parms(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), 2);
512       }
513       break;
514 
515     case throw_index_exception_id:
516       {
517         __ set_info("index_range_check_failed", dont_gc_arguments); // Arguments will be discarded.
518         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
519       }
520       break;
521 
522     case throw_div0_exception_id:
523       {
524         __ set_info("throw_div0_exception", dont_gc_arguments);
525         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
526       }
527       break;
528 
529     case throw_null_pointer_exception_id:
530       {
531         __ set_info("throw_null_pointer_exception", dont_gc_arguments);
532         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
533       }
534       break;
535 
536     case handle_exception_nofpu_id:
537     case handle_exception_id:
538       {
539         __ set_info("handle_exception", dont_gc_arguments);
540         oop_maps = generate_handle_exception(id, sasm);
541       }
542       break;
543 
544     case handle_exception_from_callee_id:
545       {
546         __ set_info("handle_exception_from_callee", dont_gc_arguments);
547         oop_maps = generate_handle_exception(id, sasm);
548       }
549       break;
550 
551     case unwind_exception_id:
552       {
553         const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
554                        Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/,
555                        Rexception_save = R31, Rcaller_sp = R30;
556         __ set_info("unwind_exception", dont_gc_arguments);
557 
558         if (AbortVMOnException) {
559           save_live_registers(sasm);
560           __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Rexception);
561           restore_live_registers(sasm, noreg, noreg);
562         }
563 
564         __ ld(Rcaller_sp, 0, R1_SP);
565         __ push_frame_reg_args(0, R0); // dummy frame for C call
566         __ mr(Rexception_save, Rexception); // save over C call
567         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
568         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, Rexception_pc);
569         __ verify_not_null_oop(Rexception_save);
570         __ mtctr(R3_RET);
571         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
572         __ mr(R1_SP, Rcaller_sp); // Pop both frames at once.
573         __ mr(Rexception, Rexception_save); // restore
574         __ mtlr(Rexception_pc);
575         __ bctr();
576       }
577       break;
578 
579     case throw_array_store_exception_id:
580       {
581         __ set_info("throw_array_store_exception", dont_gc_arguments);
582         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
583       }
584       break;
585 
586     case throw_class_cast_exception_id:
587       {
588         __ set_info("throw_class_cast_exception", dont_gc_arguments);
589         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
590       }
591       break;
592 
593     case throw_incompatible_class_change_error_id:
594       {
595         __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
596         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
597       }
598       break;
599 
600     case slow_subtype_check_id:
601       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
602         const Register sub_klass = R5,
603                        super_klass = R4,
604                        temp1_reg = R6,
605                        temp2_reg = R0;
606         __ check_klass_subtype_slow_path(sub_klass, super_klass, temp1_reg, temp2_reg); // returns with CR0.eq if successful
607         __ crandc(CCR0, Assembler::equal, CCR0, Assembler::equal); // failed: CR0.ne
608         __ blr();
609       }
610       break;
611 
612     case monitorenter_nofpu_id:
613     case monitorenter_id:
614       {
615         __ set_info("monitorenter", dont_gc_arguments);
616 
617         int save_fpu_registers = (id == monitorenter_id);
618         // Make a frame and preserve the caller's caller-save registers.
619         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
620 
621         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), R4_ARG2, R5_ARG3);
622 
623         oop_maps = new OopMapSet();
624         oop_maps->add_gc_map(call_offset, oop_map);
625 
626         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
627         __ blr();
628       }
629       break;
630 
631     case monitorexit_nofpu_id:
632     case monitorexit_id:
633       {
634         // note: Really a leaf routine but must setup last java sp
635         //       => use call_RT for now (speed can be improved by
636         //       doing last java sp setup manually).
637         __ set_info("monitorexit", dont_gc_arguments);
638 
639         int save_fpu_registers = (id == monitorexit_id);
640         // Make a frame and preserve the caller's caller-save registers.
641         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
642 
643         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), R4_ARG2);
644 
645         oop_maps = new OopMapSet();
646         oop_maps->add_gc_map(call_offset, oop_map);
647 
648         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
649         __ blr();
650       }
651       break;
652 
653     case deoptimize_id:
654       {
655         __ set_info("deoptimize", dont_gc_arguments);
656         __ std(R0, -8, R1_SP); // Pass trap_request on stack.
657         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, deoptimize), 1, /*do_return*/ false);
658 
659         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
660         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
661         address stub = deopt_blob->unpack_with_reexecution();
662         //__ load_const_optimized(R0, stub);
663         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
664         __ mtctr(R0);
665         __ bctr();
666       }
667       break;
668 
669     case access_field_patching_id:
670       {
671         __ set_info("access_field_patching", dont_gc_arguments);
672         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
673       }
674       break;
675 
676     case load_klass_patching_id:
677       {
678         __ set_info("load_klass_patching", dont_gc_arguments);
679         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
680       }
681       break;
682 
683     case load_mirror_patching_id:
684       {
685         __ set_info("load_mirror_patching", dont_gc_arguments);
686         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
687       }
688       break;
689 
690     case load_appendix_patching_id:
691       {
692         __ set_info("load_appendix_patching", dont_gc_arguments);
693         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
694       }
695       break;
696 
697     case dtrace_object_alloc_id:
698       { // O0: object
699         __ unimplemented("stub dtrace_object_alloc_id");
700         __ set_info("dtrace_object_alloc", dont_gc_arguments);
701 //        // We can't gc here so skip the oopmap but make sure that all
702 //        // the live registers get saved.
703 //        save_live_registers(sasm);
704 //
705 //        __ save_thread(L7_thread_cache);
706 //        __ call(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)),
707 //                relocInfo::runtime_call_type);
708 //        __ delayed()->mov(I0, O0);
709 //        __ restore_thread(L7_thread_cache);
710 //
711 //        restore_live_registers(sasm);
712 //        __ ret();
713 //        __ delayed()->restore();
714       }
715       break;
716 
717     case predicate_failed_trap_id:
718       {
719         __ set_info("predicate_failed_trap", dont_gc_arguments);
720         OopMap* oop_map = save_live_registers(sasm);
721 
722         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
723 
724         oop_maps = new OopMapSet();
725         oop_maps->add_gc_map(call_offset, oop_map);
726 
727         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
728         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
729         restore_live_registers(sasm, noreg, noreg);
730 
731         address stub = deopt_blob->unpack_with_reexecution();
732         //__ load_const_optimized(R0, stub);
733         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
734         __ mtctr(R0);
735         __ bctr();
736       }
737       break;
738 
739   default:
740       {
741         __ set_info("unimplemented entry", dont_gc_arguments);
742         __ mflr(R0);
743         __ std(R0, _abi0(lr), R1_SP);
744         __ push_frame(frame::native_abi_reg_args_size, R0); // empty dummy frame
745         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
746         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
747 
748         __ load_const_optimized(R4_ARG2, (int)id);
749         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), R4_ARG2);
750 
751         oop_maps = new OopMapSet();
752         oop_maps->add_gc_map(call_offset, oop_map);
753         __ should_not_reach_here();
754       }
755       break;
756   }
757   return oop_maps;
758 }
759 
760 
761 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler* sasm) {
762   __ block_comment("generate_handle_exception");
763 
764   // Save registers, if required.
765   OopMapSet* oop_maps = new OopMapSet();
766   OopMap* oop_map = nullptr;
767   const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
768                  Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/;
769 
770   switch (id) {
771   case forward_exception_id:
772     // We're handling an exception in the context of a compiled frame.
773     // The registers have been saved in the standard places. Perform
774     // an exception lookup in the caller and dispatch to the handler
775     // if found. Otherwise unwind and dispatch to the callers
776     // exception handler.
777     oop_map = generate_oop_map(sasm, true);
778     // Transfer the pending exception to the exception_oop.
779     // Also load the PC which is typically at SP + frame_size_in_bytes +_abi0(lr),
780     // but we support additional slots in the frame for parameter passing.
781     __ ld(Rexception_pc, 0, R1_SP);
782     __ ld(Rexception, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
783     __ li(R0, 0);
784     __ ld(Rexception_pc, _abi0(lr), Rexception_pc);
785     __ std(R0, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
786     break;
787   case handle_exception_nofpu_id:
788   case handle_exception_id:
789     // At this point all registers MAY be live.
790     oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id, Rexception_pc);
791     break;
792   case handle_exception_from_callee_id:
793     // At this point all registers except exception oop and exception pc are dead.
794     oop_map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
795     sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
796     __ std(Rexception_pc, _abi0(lr), R1_SP);
797     __ push_frame(frame_size_in_bytes, R0);
798     break;
799   default:  ShouldNotReachHere();
800   }
801 
802   __ verify_not_null_oop(Rexception);
803 
804 #ifdef ASSERT
805   // Check that fields in JavaThread for exception oop and issuing pc are
806   // empty before writing to them.
807   __ ld(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
808   __ cmpdi(CCR0, R0, 0);
809   __ asm_assert_eq("exception oop already set");
810   __ ld(R0, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
811   __ cmpdi(CCR0, R0, 0);
812   __ asm_assert_eq("exception pc already set");
813 #endif
814 
815   // Save the exception and issuing pc in the thread.
816   __ std(Rexception,    in_bytes(JavaThread::exception_oop_offset()), R16_thread);
817   __ std(Rexception_pc, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
818 
819   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
820   oop_maps->add_gc_map(call_offset, oop_map);
821 
822   __ mtctr(R3_RET);
823 
824   // Note: if nmethod has been deoptimized then regardless of
825   // whether it had a handler or not we will deoptimize
826   // by entering the deopt blob with a pending exception.
827 
828   // Restore the registers that were saved at the beginning, remove
829   // the frame and jump to the exception handler.
830   switch (id) {
831   case forward_exception_id:
832   case handle_exception_nofpu_id:
833   case handle_exception_id:
834     restore_live_registers(sasm, noreg, noreg, id != handle_exception_nofpu_id);
835     __ bctr();
836     break;
837   case handle_exception_from_callee_id: {
838     __ pop_frame();
839     __ ld(Rexception_pc, _abi0(lr), R1_SP);
840     __ mtlr(Rexception_pc);
841     __ bctr();
842     break;
843   }
844   default:  ShouldNotReachHere();
845   }
846 
847   return oop_maps;
848 }
849 
850 const char *Runtime1::pd_name_for_address(address entry) {
851   return "<unknown function>";
852 }
853 
854 #undef __