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