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 
261 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
262   // Make a frame and preserve the caller's caller-save registers.
263   OopMap* oop_map = save_live_registers(sasm);
264 
265   int call_offset;
266   if (!has_argument) {
267     call_offset = __ call_RT(noreg, noreg, target);
268   } else {
269     call_offset = __ call_RT(noreg, noreg, target, R4_ARG2);
270   }
271   OopMapSet* oop_maps = new OopMapSet();
272   oop_maps->add_gc_map(call_offset, oop_map);
273 
274   __ should_not_reach_here();
275   return oop_maps;
276 }
277 
278 static OopMapSet* generate_exception_throw_with_stack_parms(StubAssembler* sasm, address target,
279                                                             int stack_parms) {
280   // Make a frame and preserve the caller's caller-save registers.
281   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
282   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
283   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
284 
285   int call_offset = 0;
286   switch (stack_parms) {
287     case 3:
288     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
289     case 2:
290     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
291     case 1:
292     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
293     case 0:
294     call_offset = __ call_RT(noreg, noreg, target);
295     break;
296     default: Unimplemented(); break;
297   }
298   OopMapSet* oop_maps = new OopMapSet();
299   oop_maps->add_gc_map(call_offset, oop_map);
300 
301   __ should_not_reach_here();
302   return oop_maps;
303 }
304 
305 
306 OopMapSet* Runtime1::generate_stub_call(StubAssembler* sasm, Register result, address target,
307                                         Register arg1, Register arg2, Register arg3) {
308   // Make a frame and preserve the caller's caller-save registers.
309   OopMap* oop_map = save_live_registers(sasm);
310 
311   int call_offset;
312   if (arg1 == noreg) {
313     call_offset = __ call_RT(result, noreg, target);
314   } else if (arg2 == noreg) {
315     call_offset = __ call_RT(result, noreg, target, arg1);
316   } else if (arg3 == noreg) {
317     call_offset = __ call_RT(result, noreg, target, arg1, arg2);
318   } else {
319     call_offset = __ call_RT(result, noreg, target, arg1, arg2, arg3);
320   }
321   OopMapSet* oop_maps = new OopMapSet();
322   oop_maps->add_gc_map(call_offset, oop_map);
323 
324   restore_live_registers(sasm, result, noreg);
325   __ blr();
326   return oop_maps;
327 }
328 
329 static OopMapSet* stub_call_with_stack_parms(StubAssembler* sasm, Register result, address target,
330                                              int stack_parms, bool do_return = true) {
331   // Make a frame and preserve the caller's caller-save registers.
332   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
333   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
334   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
335 
336   int call_offset = 0;
337   switch (stack_parms) {
338     case 3:
339     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
340     case 2:
341     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
342     case 1:
343     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
344     case 0:
345     call_offset = __ call_RT(result, noreg, target);
346     break;
347     default: Unimplemented(); break;
348   }
349   OopMapSet* oop_maps = new OopMapSet();
350   oop_maps->add_gc_map(call_offset, oop_map);
351 
352   restore_live_registers(sasm, result, noreg);
353   if (do_return) __ blr();
354   return oop_maps;
355 }
356 
357 
358 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
359   // Make a frame and preserve the caller's caller-save registers.
360   OopMap* oop_map = save_live_registers(sasm);
361 
362   // Call the runtime patching routine, returns non-zero if nmethod got deopted.
363   int call_offset = __ call_RT(noreg, noreg, target);
364   OopMapSet* oop_maps = new OopMapSet();
365   oop_maps->add_gc_map(call_offset, oop_map);
366   __ cmpdi(CCR0, R3_RET, 0);
367 
368   // Re-execute the patched instruction or, if the nmethod was deoptmized,
369   // return to the deoptimization handler entry that will cause re-execution
370   // of the current bytecode.
371   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
372   assert(deopt_blob != nullptr, "deoptimization blob must have been created");
373 
374   // Return to the deoptimization handler entry for unpacking and rexecute.
375   // If we simply returned the we'd deopt as if any call we patched had just
376   // returned.
377 
378   restore_live_registers(sasm, noreg, noreg);
379   // Return if patching routine returned 0.
380   __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CCR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
381 
382   address stub = deopt_blob->unpack_with_reexecution();
383   //__ load_const_optimized(R0, stub);
384   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
385   __ mtctr(R0);
386   __ bctr();
387 
388   return oop_maps;
389 }
390 
391 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
392   OopMapSet* oop_maps = nullptr;
393 
394   // For better readability.
395   const bool must_gc_arguments = true;
396   const bool dont_gc_arguments = false;
397 
398   // Stub code & info for the different stubs.
399   switch (id) {
400     case forward_exception_id:
401       {
402         oop_maps = generate_handle_exception(id, sasm);
403       }
404       break;
405 
406     case new_instance_id:
407     case fast_new_instance_id:
408     case fast_new_instance_init_check_id:
409       {
410         if (id == new_instance_id) {
411           __ set_info("new_instance", dont_gc_arguments);
412         } else if (id == fast_new_instance_id) {
413           __ set_info("fast new_instance", dont_gc_arguments);
414         } else {
415           assert(id == fast_new_instance_init_check_id, "bad StubID");
416           __ set_info("fast new_instance init check", dont_gc_arguments);
417         }
418 
419         // We don't support eden allocation.
420 
421         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_instance), R4_ARG2);
422       }
423       break;
424 
425     case counter_overflow_id:
426         // Bci and method are on stack.
427         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, counter_overflow), 2);
428       break;
429 
430     case new_type_array_id:
431     case new_object_array_id:
432       {
433         if (id == new_type_array_id) {
434           __ set_info("new_type_array", dont_gc_arguments);
435         } else {
436           __ set_info("new_object_array", dont_gc_arguments);
437         }
438 
439 #ifdef ASSERT
440         // Assert object type is really an array of the proper kind.
441         {
442           int tag = (id == new_type_array_id) ? Klass::_lh_array_tag_type_value : Klass::_lh_array_tag_obj_value;
443           Label ok;
444           __ lwz(R0, in_bytes(Klass::layout_helper_offset()), R4_ARG2);
445           __ srawi(R0, R0, Klass::_lh_array_tag_shift);
446           __ cmpwi(CCR0, R0, tag);
447           __ beq(CCR0, ok);
448           __ stop("assert(is an array klass)");
449           __ should_not_reach_here();
450           __ bind(ok);
451         }
452 #endif // ASSERT
453 
454         // We don't support eden allocation.
455 
456         if (id == new_type_array_id) {
457           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_type_array), R4_ARG2, R5_ARG3);
458         } else {
459           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_object_array), R4_ARG2, R5_ARG3);
460         }
461       }
462       break;
463 
464     case new_multi_array_id:
465       {
466         // R4: klass
467         // R5: rank
468         // R6: address of 1st dimension
469         __ set_info("new_multi_array", dont_gc_arguments);
470         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_multi_array), R4_ARG2, R5_ARG3, R6_ARG4);
471       }
472       break;
473 
474     case register_finalizer_id:
475       {
476         __ set_info("register_finalizer", dont_gc_arguments);
477         // This code is called via rt_call. Hence, caller-save registers have been saved.
478         Register t = R11_scratch1;
479 
480         // Load the klass and check the has finalizer flag.
481         __ load_klass(t, R3_ARG1);
482         __ lwz(t, in_bytes(Klass::access_flags_offset()), t);
483         __ testbitdi(CCR0, R0, t, exact_log2(JVM_ACC_HAS_FINALIZER));
484         // Return if has_finalizer bit == 0 (CR0.eq).
485         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CCR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
486 
487         __ mflr(R0);
488         __ std(R0, _abi0(lr), R1_SP);
489         __ push_frame(frame::native_abi_reg_args_size, R0); // Empty dummy frame (no callee-save regs).
490         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
491         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
492         int call_offset = __ call_RT(noreg, noreg,
493                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), R3_ARG1);
494         oop_maps = new OopMapSet();
495         oop_maps->add_gc_map(call_offset, oop_map);
496 
497         __ pop_frame();
498         __ ld(R0, _abi0(lr), R1_SP);
499         __ mtlr(R0);
500         __ blr();
501       }
502       break;
503 
504     case throw_range_check_failed_id:
505       {
506         __ set_info("range_check_failed", dont_gc_arguments); // Arguments will be discarded.
507         oop_maps = generate_exception_throw_with_stack_parms(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), 2);
508       }
509       break;
510 
511     case throw_index_exception_id:
512       {
513         __ set_info("index_range_check_failed", dont_gc_arguments); // Arguments will be discarded.
514         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
515       }
516       break;
517 
518     case throw_div0_exception_id:
519       {
520         __ set_info("throw_div0_exception", dont_gc_arguments);
521         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
522       }
523       break;
524 
525     case throw_null_pointer_exception_id:
526       {
527         __ set_info("throw_null_pointer_exception", dont_gc_arguments);
528         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
529       }
530       break;
531 
532     case handle_exception_nofpu_id:
533     case handle_exception_id:
534       {
535         __ set_info("handle_exception", dont_gc_arguments);
536         oop_maps = generate_handle_exception(id, sasm);
537       }
538       break;
539 
540     case handle_exception_from_callee_id:
541       {
542         __ set_info("handle_exception_from_callee", dont_gc_arguments);
543         oop_maps = generate_handle_exception(id, sasm);
544       }
545       break;
546 
547     case unwind_exception_id:
548       {
549         const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
550                        Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/,
551                        Rexception_save = R31, Rcaller_sp = R30;
552         __ set_info("unwind_exception", dont_gc_arguments);
553 
554         if (AbortVMOnException) {
555           save_live_registers(sasm);
556           __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Rexception);
557           restore_live_registers(sasm, noreg, noreg);
558         }
559 
560         __ ld(Rcaller_sp, 0, R1_SP);
561         __ push_frame_reg_args(0, R0); // dummy frame for C call
562         __ mr(Rexception_save, Rexception); // save over C call
563         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
564         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, Rexception_pc);
565         __ verify_not_null_oop(Rexception_save);
566         __ mtctr(R3_RET);
567         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
568         __ mr(R1_SP, Rcaller_sp); // Pop both frames at once.
569         __ mr(Rexception, Rexception_save); // restore
570         __ mtlr(Rexception_pc);
571         __ bctr();
572       }
573       break;
574 
575     case throw_array_store_exception_id:
576       {
577         __ set_info("throw_array_store_exception", dont_gc_arguments);
578         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
579       }
580       break;
581 
582     case throw_class_cast_exception_id:
583       {
584         __ set_info("throw_class_cast_exception", dont_gc_arguments);
585         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
586       }
587       break;
588 
589     case throw_incompatible_class_change_error_id:
590       {
591         __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
592         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
593       }
594       break;
595 
596     case slow_subtype_check_id:
597       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
598         const Register sub_klass = R5,
599                        super_klass = R4,
600                        temp1_reg = R6,
601                        temp2_reg = R0;
602         __ check_klass_subtype_slow_path(sub_klass, super_klass, temp1_reg, temp2_reg); // returns with CR0.eq if successful
603         __ crandc(CCR0, Assembler::equal, CCR0, Assembler::equal); // failed: CR0.ne
604         __ blr();
605       }
606       break;
607 
608     case monitorenter_nofpu_id:
609     case monitorenter_id:
610       {
611         __ set_info("monitorenter", dont_gc_arguments);
612 
613         int save_fpu_registers = (id == monitorenter_id);
614         // Make a frame and preserve the caller's caller-save registers.
615         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
616 
617         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), R4_ARG2, R5_ARG3);
618 
619         oop_maps = new OopMapSet();
620         oop_maps->add_gc_map(call_offset, oop_map);
621 
622         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
623         __ blr();
624       }
625       break;
626 
627     case monitorexit_nofpu_id:
628     case monitorexit_id:
629       {
630         // note: Really a leaf routine but must setup last java sp
631         //       => use call_RT for now (speed can be improved by
632         //       doing last java sp setup manually).
633         __ set_info("monitorexit", dont_gc_arguments);
634 
635         int save_fpu_registers = (id == monitorexit_id);
636         // Make a frame and preserve the caller's caller-save registers.
637         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
638 
639         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), R4_ARG2);
640 
641         oop_maps = new OopMapSet();
642         oop_maps->add_gc_map(call_offset, oop_map);
643 
644         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
645         __ blr();
646       }
647       break;
648 
649     case deoptimize_id:
650       {
651         __ set_info("deoptimize", dont_gc_arguments);
652         __ std(R0, -8, R1_SP); // Pass trap_request on stack.
653         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, deoptimize), 1, /*do_return*/ false);
654 
655         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
656         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
657         address stub = deopt_blob->unpack_with_reexecution();
658         //__ load_const_optimized(R0, stub);
659         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
660         __ mtctr(R0);
661         __ bctr();
662       }
663       break;
664 
665     case access_field_patching_id:
666       {
667         __ set_info("access_field_patching", dont_gc_arguments);
668         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
669       }
670       break;
671 
672     case load_klass_patching_id:
673       {
674         __ set_info("load_klass_patching", dont_gc_arguments);
675         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
676       }
677       break;
678 
679     case load_mirror_patching_id:
680       {
681         __ set_info("load_mirror_patching", dont_gc_arguments);
682         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
683       }
684       break;
685 
686     case load_appendix_patching_id:
687       {
688         __ set_info("load_appendix_patching", dont_gc_arguments);
689         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
690       }
691       break;
692 
693     case dtrace_object_alloc_id:
694       { // O0: object
695         __ unimplemented("stub dtrace_object_alloc_id");
696         __ set_info("dtrace_object_alloc", dont_gc_arguments);
697 //        // We can't gc here so skip the oopmap but make sure that all
698 //        // the live registers get saved.
699 //        save_live_registers(sasm);
700 //
701 //        __ save_thread(L7_thread_cache);
702 //        __ call(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)),
703 //                relocInfo::runtime_call_type);
704 //        __ delayed()->mov(I0, O0);
705 //        __ restore_thread(L7_thread_cache);
706 //
707 //        restore_live_registers(sasm);
708 //        __ ret();
709 //        __ delayed()->restore();
710       }
711       break;
712 
713     case predicate_failed_trap_id:
714       {
715         __ set_info("predicate_failed_trap", dont_gc_arguments);
716         OopMap* oop_map = save_live_registers(sasm);
717 
718         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
719 
720         oop_maps = new OopMapSet();
721         oop_maps->add_gc_map(call_offset, oop_map);
722 
723         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
724         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
725         restore_live_registers(sasm, noreg, noreg);
726 
727         address stub = deopt_blob->unpack_with_reexecution();
728         //__ load_const_optimized(R0, stub);
729         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
730         __ mtctr(R0);
731         __ bctr();
732       }
733       break;
734 
735   default:
736       {
737         __ set_info("unimplemented entry", dont_gc_arguments);
738         __ mflr(R0);
739         __ std(R0, _abi0(lr), R1_SP);
740         __ push_frame(frame::native_abi_reg_args_size, R0); // empty dummy frame
741         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
742         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
743 
744         __ load_const_optimized(R4_ARG2, (int)id);
745         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), R4_ARG2);
746 
747         oop_maps = new OopMapSet();
748         oop_maps->add_gc_map(call_offset, oop_map);
749         __ should_not_reach_here();
750       }
751       break;
752   }
753   return oop_maps;
754 }
755 
756 
757 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler* sasm) {
758   __ block_comment("generate_handle_exception");
759 
760   // Save registers, if required.
761   OopMapSet* oop_maps = new OopMapSet();
762   OopMap* oop_map = nullptr;
763   const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
764                  Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/;
765 
766   switch (id) {
767   case forward_exception_id:
768     // We're handling an exception in the context of a compiled frame.
769     // The registers have been saved in the standard places. Perform
770     // an exception lookup in the caller and dispatch to the handler
771     // if found. Otherwise unwind and dispatch to the callers
772     // exception handler.
773     oop_map = generate_oop_map(sasm, true);
774     // Transfer the pending exception to the exception_oop.
775     // Also load the PC which is typically at SP + frame_size_in_bytes +_abi0(lr),
776     // but we support additional slots in the frame for parameter passing.
777     __ ld(Rexception_pc, 0, R1_SP);
778     __ ld(Rexception, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
779     __ li(R0, 0);
780     __ ld(Rexception_pc, _abi0(lr), Rexception_pc);
781     __ std(R0, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
782     break;
783   case handle_exception_nofpu_id:
784   case handle_exception_id:
785     // At this point all registers MAY be live.
786     oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id, Rexception_pc);
787     break;
788   case handle_exception_from_callee_id:
789     // At this point all registers except exception oop and exception pc are dead.
790     oop_map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
791     sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
792     __ std(Rexception_pc, _abi0(lr), R1_SP);
793     __ push_frame(frame_size_in_bytes, R0);
794     break;
795   default:  ShouldNotReachHere();
796   }
797 
798   __ verify_not_null_oop(Rexception);
799 
800 #ifdef ASSERT
801   // Check that fields in JavaThread for exception oop and issuing pc are
802   // empty before writing to them.
803   __ ld(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
804   __ cmpdi(CCR0, R0, 0);
805   __ asm_assert_eq("exception oop already set");
806   __ ld(R0, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
807   __ cmpdi(CCR0, R0, 0);
808   __ asm_assert_eq("exception pc already set");
809 #endif
810 
811   // Save the exception and issuing pc in the thread.
812   __ std(Rexception,    in_bytes(JavaThread::exception_oop_offset()), R16_thread);
813   __ std(Rexception_pc, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
814 
815   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
816   oop_maps->add_gc_map(call_offset, oop_map);
817 
818   __ mtctr(R3_RET);
819 
820   // Note: if nmethod has been deoptimized then regardless of
821   // whether it had a handler or not we will deoptimize
822   // by entering the deopt blob with a pending exception.
823 
824   // Restore the registers that were saved at the beginning, remove
825   // the frame and jump to the exception handler.
826   switch (id) {
827   case forward_exception_id:
828   case handle_exception_nofpu_id:
829   case handle_exception_id:
830     restore_live_registers(sasm, noreg, noreg, id != handle_exception_nofpu_id);
831     __ bctr();
832     break;
833   case handle_exception_from_callee_id: {
834     __ pop_frame();
835     __ ld(Rexception_pc, _abi0(lr), R1_SP);
836     __ mtlr(Rexception_pc);
837     __ bctr();
838     break;
839   }
840   default:  ShouldNotReachHere();
841   }
842 
843   return oop_maps;
844 }
845 
846 const char *Runtime1::pd_name_for_address(address entry) {
847   return "<unknown function>";
848 }
849 
850 #undef __