1 /*
   2  * Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2021, Red Hat Inc. 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/assembler.hpp"
  28 #include "c1/c1_CodeStubs.hpp"
  29 #include "c1/c1_Defs.hpp"
  30 #include "c1/c1_MacroAssembler.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "compiler/oopMap.hpp"
  34 #include "gc/shared/cardTable.hpp"
  35 #include "gc/shared/cardTableBarrierSet.hpp"
  36 #include "gc/shared/collectedHeap.hpp"
  37 #include "gc/shared/tlab_globals.hpp"
  38 #include "interpreter/interpreter.hpp"
  39 #include "memory/universe.hpp"
  40 #include "nativeInst_aarch64.hpp"
  41 #include "oops/compiledICHolder.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "prims/jvmtiExport.hpp"
  44 #include "register_aarch64.hpp"
  45 #include "runtime/sharedRuntime.hpp"
  46 #include "runtime/signature.hpp"
  47 #include "runtime/stubRoutines.hpp"
  48 #include "runtime/vframe.hpp"
  49 #include "runtime/vframeArray.hpp"
  50 #include "utilities/powerOfTwo.hpp"
  51 #include "vmreg_aarch64.inline.hpp"
  52 
  53 
  54 // Implementation of StubAssembler
  55 
  56 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, int args_size) {
  57   // setup registers
  58   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) || oop_result1 != metadata_result, "registers must be different");
  59   assert(oop_result1 != rthread && metadata_result != rthread, "registers must be different");
  60   assert(args_size >= 0, "illegal args_size");
  61   bool align_stack = false;
  62 
  63   mov(c_rarg0, rthread);
  64   set_num_rt_args(0); // Nothing on stack
  65 
  66   Label retaddr;
  67   set_last_Java_frame(sp, rfp, retaddr, rscratch1);
  68 
  69   // do the call
  70   lea(rscratch1, RuntimeAddress(entry));
  71   blr(rscratch1);
  72   bind(retaddr);
  73   int call_offset = offset();
  74   // verify callee-saved register
  75 #ifdef ASSERT
  76   push(r0, sp);
  77   { Label L;
  78     get_thread(r0);
  79     cmp(rthread, r0);
  80     br(Assembler::EQ, L);
  81     stop("StubAssembler::call_RT: rthread not callee saved?");
  82     bind(L);
  83   }
  84   pop(r0, sp);
  85 #endif
  86   reset_last_Java_frame(true);
  87 
  88   // check for pending exceptions
  89   { Label L;
  90     // check for pending exceptions (java_thread is set upon return)
  91     ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset())));
  92     cbz(rscratch1, L);
  93     // exception pending => remove activation and forward to exception handler
  94     // make sure that the vm_results are cleared
  95     if (oop_result1->is_valid()) {
  96       str(zr, Address(rthread, JavaThread::vm_result_offset()));
  97     }
  98     if (metadata_result->is_valid()) {
  99       str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
 100     }
 101     if (frame_size() == no_frame_size) {
 102       leave();
 103       far_jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 104     } else if (_stub_id == Runtime1::forward_exception_id) {
 105       should_not_reach_here();
 106     } else {
 107       far_jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 108     }
 109     bind(L);
 110   }
 111   // get oop results if there are any and reset the values in the thread
 112   if (oop_result1->is_valid()) {
 113     get_vm_result(oop_result1, rthread);
 114   }
 115   if (metadata_result->is_valid()) {
 116     get_vm_result_2(metadata_result, rthread);
 117   }
 118   return call_offset;
 119 }
 120 
 121 
 122 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
 123   mov(c_rarg1, arg1);
 124   return call_RT(oop_result1, metadata_result, entry, 1);
 125 }
 126 
 127 
 128 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
 129   if (c_rarg1 == arg2) {
 130     if (c_rarg2 == arg1) {
 131       mov(rscratch1, arg1);
 132       mov(arg1, arg2);
 133       mov(arg2, rscratch1);
 134     } else {
 135       mov(c_rarg2, arg2);
 136       mov(c_rarg1, arg1);
 137     }
 138   } else {
 139     mov(c_rarg1, arg1);
 140     mov(c_rarg2, arg2);
 141   }
 142   return call_RT(oop_result1, metadata_result, entry, 2);
 143 }
 144 
 145 
 146 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
 147   // if there is any conflict use the stack
 148   if (arg1 == c_rarg2 || arg1 == c_rarg3 ||
 149       arg2 == c_rarg1 || arg2 == c_rarg3 ||
 150       arg3 == c_rarg1 || arg3 == c_rarg2) {
 151     stp(arg3, arg2, Address(pre(sp, -2 * wordSize)));
 152     stp(arg1, zr, Address(pre(sp, -2 * wordSize)));
 153     ldp(c_rarg1, zr, Address(post(sp, 2 * wordSize)));
 154     ldp(c_rarg3, c_rarg2, Address(post(sp, 2 * wordSize)));
 155   } else {
 156     mov(c_rarg1, arg1);
 157     mov(c_rarg2, arg2);
 158     mov(c_rarg3, arg3);
 159   }
 160   return call_RT(oop_result1, metadata_result, entry, 3);
 161 }
 162 
 163 enum return_state_t {
 164   does_not_return, requires_return
 165 };
 166 
 167 
 168 // Implementation of StubFrame
 169 
 170 class StubFrame: public StackObj {
 171  private:
 172   StubAssembler* _sasm;
 173   bool _return_state;
 174 
 175  public:
 176   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments, return_state_t return_state=requires_return);
 177   void load_argument(int offset_in_words, Register reg);
 178 
 179   ~StubFrame();
 180 };;
 181 
 182 void StubAssembler::prologue(const char* name, bool must_gc_arguments) {
 183   set_info(name, must_gc_arguments);
 184   enter();
 185 }
 186 
 187 void StubAssembler::epilogue() {
 188   leave();
 189   ret(lr);
 190 }
 191 
 192 #define __ _sasm->
 193 
 194 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments, return_state_t return_state) {
 195   _sasm = sasm;
 196   _return_state = return_state;
 197   __ prologue(name, must_gc_arguments);
 198 }
 199 
 200 // load parameters that were stored with LIR_Assembler::store_parameter
 201 // Note: offsets for store_parameter and load_argument must match
 202 void StubFrame::load_argument(int offset_in_words, Register reg) {
 203   __ load_parameter(offset_in_words, reg);
 204 }
 205 
 206 StubFrame::~StubFrame() {
 207   if (_return_state == requires_return) {
 208     __ epilogue();
 209   } else {
 210     __ should_not_reach_here();
 211   }
 212 }
 213 
 214 #undef __
 215 
 216 
 217 // Implementation of Runtime1
 218 
 219 #define __ sasm->
 220 
 221 const int float_regs_as_doubles_size_in_slots = pd_nof_fpu_regs_frame_map * 2;
 222 
 223 // Stack layout for saving/restoring  all the registers needed during a runtime
 224 // call (this includes deoptimization)
 225 // Note: note that users of this frame may well have arguments to some runtime
 226 // while these values are on the stack. These positions neglect those arguments
 227 // but the code in save_live_registers will take the argument count into
 228 // account.
 229 //
 230 
 231 enum reg_save_layout {
 232   reg_save_frame_size = 32 /* float */ + 32 /* integer */
 233 };
 234 
 235 // Save off registers which might be killed by calls into the runtime.
 236 // Tries to smart of about FP registers.  In particular we separate
 237 // saving and describing the FPU registers for deoptimization since we
 238 // have to save the FPU registers twice if we describe them.  The
 239 // deopt blob is the only thing which needs to describe FPU registers.
 240 // In all other cases it should be sufficient to simply save their
 241 // current value.
 242 
 243 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
 244 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
 245 static int reg_save_size_in_words;
 246 static int frame_size_in_bytes = -1;
 247 
 248 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
 249   int frame_size_in_bytes = reg_save_frame_size * BytesPerWord;
 250   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 251   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
 252   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
 253 
 254   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
 255     Register r = as_Register(i);
 256     if (i <= 18 && i != rscratch1->encoding() && i != rscratch2->encoding()) {
 257       int sp_offset = cpu_reg_save_offsets[i];
 258       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 259                                 r->as_VMReg());
 260     }
 261   }
 262 
 263   if (save_fpu_registers) {
 264     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 265       FloatRegister r = as_FloatRegister(i);
 266       {
 267         int sp_offset = fpu_reg_save_offsets[i];
 268         oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 269                                   r->as_VMReg());
 270       }
 271     }
 272   }
 273   return oop_map;
 274 }
 275 
 276 static void save_live_registers_no_oop_map(StubAssembler* sasm,
 277                                               bool save_fpu_registers = true) {
 278   __ block_comment("save_live_registers");
 279 
 280   __ push(RegSet::range(r0, r29), sp);         // integer registers except lr & sp
 281 
 282   if (save_fpu_registers) {
 283     for (int i = 31; i>= 0; i -= 4) {
 284       __ sub(sp, sp, 4 * wordSize); // no pre-increment for st1. Emulate it without modifying other registers
 285       __ st1(as_FloatRegister(i-3), as_FloatRegister(i-2), as_FloatRegister(i-1),
 286           as_FloatRegister(i), __ T1D, Address(sp));
 287     }
 288   } else {
 289     __ add(sp, sp, -32 * wordSize);
 290   }
 291 }
 292 
 293 static OopMap* save_live_registers(StubAssembler* sasm,
 294                                    bool save_fpu_registers = true) {
 295   save_live_registers_no_oop_map(sasm, save_fpu_registers);
 296   return generate_oop_map(sasm, save_fpu_registers);
 297 }
 298 
 299 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 300   if (restore_fpu_registers) {
 301     for (int i = 0; i < 32; i += 4)
 302       __ ld1(as_FloatRegister(i), as_FloatRegister(i+1), as_FloatRegister(i+2),
 303           as_FloatRegister(i+3), __ T1D, Address(__ post(sp, 4 * wordSize)));
 304   } else {
 305     __ add(sp, sp, 32 * wordSize);
 306   }
 307 
 308   __ pop(RegSet::range(r0, r29), sp);
 309 }
 310 
 311 static void restore_live_registers_except_r0(StubAssembler* sasm, bool restore_fpu_registers = true)  {
 312 
 313   if (restore_fpu_registers) {
 314     for (int i = 0; i < 32; i += 4)
 315       __ ld1(as_FloatRegister(i), as_FloatRegister(i+1), as_FloatRegister(i+2),
 316           as_FloatRegister(i+3), __ T1D, Address(__ post(sp, 4 * wordSize)));
 317   } else {
 318     __ add(sp, sp, 32 * wordSize);
 319   }
 320 
 321   __ ldp(zr, r1, Address(__ post(sp, 16)));
 322   __ pop(RegSet::range(r2, r29), sp);
 323 }
 324 
 325 
 326 
 327 void Runtime1::initialize_pd() {
 328   int i;
 329   int sp_offset = 0;
 330 
 331   // all float registers are saved explicitly
 332   assert(FrameMap::nof_fpu_regs == 32, "double registers not handled here");
 333   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 334     fpu_reg_save_offsets[i] = sp_offset;
 335     sp_offset += 2;   // SP offsets are in halfwords
 336   }
 337 
 338   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 339     Register r = as_Register(i);
 340     cpu_reg_save_offsets[i] = sp_offset;
 341     sp_offset += 2;   // SP offsets are in halfwords
 342   }
 343 }
 344 
 345 
 346 // target: the entry point of the method that creates and posts the exception oop
 347 // has_argument: true if the exception needs arguments (passed in rscratch1 and rscratch2)
 348 
 349 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 350   // make a frame and preserve the caller's caller-save registers
 351   OopMap* oop_map = save_live_registers(sasm);
 352   int call_offset;
 353   if (!has_argument) {
 354     call_offset = __ call_RT(noreg, noreg, target);
 355   } else {
 356     __ mov(c_rarg1, rscratch1);
 357     __ mov(c_rarg2, rscratch2);
 358     call_offset = __ call_RT(noreg, noreg, target);
 359   }
 360   OopMapSet* oop_maps = new OopMapSet();
 361   oop_maps->add_gc_map(call_offset, oop_map);
 362   return oop_maps;
 363 }
 364 
 365 
 366 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler *sasm) {
 367   __ block_comment("generate_handle_exception");
 368 
 369   // incoming parameters
 370   const Register exception_oop = r0;
 371   const Register exception_pc  = r3;
 372   // other registers used in this stub
 373 
 374   // Save registers, if required.
 375   OopMapSet* oop_maps = new OopMapSet();
 376   OopMap* oop_map = NULL;
 377   switch (id) {
 378   case forward_exception_id:
 379     // We're handling an exception in the context of a compiled frame.
 380     // The registers have been saved in the standard places.  Perform
 381     // an exception lookup in the caller and dispatch to the handler
 382     // if found.  Otherwise unwind and dispatch to the callers
 383     // exception handler.
 384     oop_map = generate_oop_map(sasm, 1 /*thread*/);
 385 
 386     // load and clear pending exception oop into r0
 387     __ ldr(exception_oop, Address(rthread, Thread::pending_exception_offset()));
 388     __ str(zr, Address(rthread, Thread::pending_exception_offset()));
 389 
 390     // load issuing PC (the return address for this stub) into r3
 391     __ ldr(exception_pc, Address(rfp, 1*BytesPerWord));
 392     __ authenticate_return_address(exception_pc, rscratch1);
 393 
 394     // make sure that the vm_results are cleared (may be unnecessary)
 395     __ str(zr, Address(rthread, JavaThread::vm_result_offset()));
 396     __ str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
 397     break;
 398   case handle_exception_nofpu_id:
 399   case handle_exception_id:
 400     // At this point all registers MAY be live.
 401     oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id);
 402     break;
 403   case handle_exception_from_callee_id: {
 404     // At this point all registers except exception oop (r0) and
 405     // exception pc (lr) are dead.
 406     const int frame_size = 2 /*fp, return address*/;
 407     oop_map = new OopMap(frame_size * VMRegImpl::slots_per_word, 0);
 408     sasm->set_frame_size(frame_size);
 409     break;
 410   }
 411   default: ShouldNotReachHere();
 412   }
 413 
 414   // verify that only r0 and r3 are valid at this time
 415   __ invalidate_registers(false, true, true, false, true, true);
 416   // verify that r0 contains a valid exception
 417   __ verify_not_null_oop(exception_oop);
 418 
 419 #ifdef ASSERT
 420   // check that fields in JavaThread for exception oop and issuing pc are
 421   // empty before writing to them
 422   Label oop_empty;
 423   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 424   __ cbz(rscratch1, oop_empty);
 425   __ stop("exception oop already set");
 426   __ bind(oop_empty);
 427 
 428   Label pc_empty;
 429   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 430   __ cbz(rscratch1, pc_empty);
 431   __ stop("exception pc already set");
 432   __ bind(pc_empty);
 433 #endif
 434 
 435   // save exception oop and issuing pc into JavaThread
 436   // (exception handler will load it from here)
 437   __ str(exception_oop, Address(rthread, JavaThread::exception_oop_offset()));
 438   __ str(exception_pc, Address(rthread, JavaThread::exception_pc_offset()));
 439 
 440   // patch throwing pc into return address (has bci & oop map)
 441   __ protect_return_address(exception_pc, rscratch1);
 442   __ str(exception_pc, Address(rfp, 1*BytesPerWord));
 443 
 444   // compute the exception handler.
 445   // the exception oop and the throwing pc are read from the fields in JavaThread
 446   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 447   oop_maps->add_gc_map(call_offset, oop_map);
 448 
 449   // r0: handler address
 450   //      will be the deopt blob if nmethod was deoptimized while we looked up
 451   //      handler regardless of whether handler existed in the nmethod.
 452 
 453   // only r0 is valid at this time, all other registers have been destroyed by the runtime call
 454   __ invalidate_registers(false, true, true, true, true, true);
 455 
 456   // patch the return address, this stub will directly return to the exception handler
 457   __ protect_return_address(r0, rscratch1);
 458   __ str(r0, Address(rfp, 1*BytesPerWord));
 459 
 460   switch (id) {
 461   case forward_exception_id:
 462   case handle_exception_nofpu_id:
 463   case handle_exception_id:
 464     // Restore the registers that were saved at the beginning.
 465     restore_live_registers(sasm, id != handle_exception_nofpu_id);
 466     break;
 467   case handle_exception_from_callee_id:
 468     break;
 469   default:  ShouldNotReachHere();
 470   }
 471 
 472   return oop_maps;
 473 }
 474 
 475 
 476 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 477   // incoming parameters
 478   const Register exception_oop = r0;
 479   // callee-saved copy of exception_oop during runtime call
 480   const Register exception_oop_callee_saved = r19;
 481   // other registers used in this stub
 482   const Register exception_pc = r3;
 483   const Register handler_addr = r1;
 484 
 485   // verify that only r0, is valid at this time
 486   __ invalidate_registers(false, true, true, true, true, true);
 487 
 488 #ifdef ASSERT
 489   // check that fields in JavaThread for exception oop and issuing pc are empty
 490   Label oop_empty;
 491   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 492   __ cbz(rscratch1, oop_empty);
 493   __ stop("exception oop must be empty");
 494   __ bind(oop_empty);
 495 
 496   Label pc_empty;
 497   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 498   __ cbz(rscratch1, pc_empty);
 499   __ stop("exception pc must be empty");
 500   __ bind(pc_empty);
 501 #endif
 502 
 503   // Save our return address because
 504   // exception_handler_for_return_address will destroy it.  We also
 505   // save exception_oop
 506   __ mov(r3, lr);
 507   __ protect_return_address();
 508   __ stp(lr, exception_oop, Address(__ pre(sp, -2 * wordSize)));
 509 
 510   // search the exception handler address of the caller (using the return address)
 511   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), rthread, r3);
 512   // r0: exception handler address of the caller
 513 
 514   // Only R0 is valid at this time; all other registers have been
 515   // destroyed by the call.
 516   __ invalidate_registers(false, true, true, true, false, true);
 517 
 518   // move result of call into correct register
 519   __ mov(handler_addr, r0);
 520 
 521   // get throwing pc (= return address).
 522   // lr has been destroyed by the call
 523   __ ldp(lr, exception_oop, Address(__ post(sp, 2 * wordSize)));
 524   __ authenticate_return_address();
 525   __ mov(r3, lr);
 526 
 527   __ verify_not_null_oop(exception_oop);
 528 
 529   // continue at exception handler (return address removed)
 530   // note: do *not* remove arguments when unwinding the
 531   //       activation since the caller assumes having
 532   //       all arguments on the stack when entering the
 533   //       runtime to determine the exception handler
 534   //       (GC happens at call site with arguments!)
 535   // r0: exception oop
 536   // r3: throwing pc
 537   // r1: exception handler
 538   __ br(handler_addr);
 539 }
 540 
 541 
 542 
 543 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 544   // use the maximum number of runtime-arguments here because it is difficult to
 545   // distinguish each RT-Call.
 546   // Note: This number affects also the RT-Call in generate_handle_exception because
 547   //       the oop-map is shared for all calls.
 548   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 549   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 550 
 551   OopMap* oop_map = save_live_registers(sasm);
 552 
 553   __ mov(c_rarg0, rthread);
 554   Label retaddr;
 555   __ set_last_Java_frame(sp, rfp, retaddr, rscratch1);
 556   // do the call
 557   __ lea(rscratch1, RuntimeAddress(target));
 558   __ blr(rscratch1);
 559   __ bind(retaddr);
 560   OopMapSet* oop_maps = new OopMapSet();
 561   oop_maps->add_gc_map(__ offset(), oop_map);
 562   // verify callee-saved register
 563 #ifdef ASSERT
 564   { Label L;
 565     __ get_thread(rscratch1);
 566     __ cmp(rthread, rscratch1);
 567     __ br(Assembler::EQ, L);
 568     __ stop("StubAssembler::call_RT: rthread not callee saved?");
 569     __ bind(L);
 570   }
 571 #endif
 572 
 573   __ reset_last_Java_frame(true);
 574 
 575 #ifdef ASSERT
 576   // check that fields in JavaThread for exception oop and issuing pc are empty
 577   Label oop_empty;
 578   __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 579   __ cbz(rscratch1, oop_empty);
 580   __ stop("exception oop must be empty");
 581   __ bind(oop_empty);
 582 
 583   Label pc_empty;
 584   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 585   __ cbz(rscratch1, pc_empty);
 586   __ stop("exception pc must be empty");
 587   __ bind(pc_empty);
 588 #endif
 589 
 590   // Runtime will return true if the nmethod has been deoptimized, this is the
 591   // expected scenario and anything else is  an error. Note that we maintain a
 592   // check on the result purely as a defensive measure.
 593   Label no_deopt;
 594   __ cbz(r0, no_deopt);                                // Have we deoptimized?
 595 
 596   // Perform a re-execute. The proper return  address is already on the stack,
 597   // we just need  to restore registers, pop  all of our frame  but the return
 598   // address and jump to the deopt blob.
 599   restore_live_registers(sasm);
 600   __ leave();
 601   __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 602 
 603   __ bind(no_deopt);
 604   __ stop("deopt not performed");
 605 
 606   return oop_maps;
 607 }
 608 
 609 
 610 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 611 
 612   const Register exception_oop = r0;
 613   const Register exception_pc  = r3;
 614 
 615   // for better readability
 616   const bool must_gc_arguments = true;
 617   const bool dont_gc_arguments = false;
 618 
 619   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 620   bool save_fpu_registers = true;
 621 
 622   // stub code & info for the different stubs
 623   OopMapSet* oop_maps = NULL;
 624   OopMap* oop_map = NULL;
 625   switch (id) {
 626     {
 627     case forward_exception_id:
 628       {
 629         oop_maps = generate_handle_exception(id, sasm);
 630         __ leave();
 631         __ ret(lr);
 632       }
 633       break;
 634 
 635     case throw_div0_exception_id:
 636       { StubFrame f(sasm, "throw_div0_exception", dont_gc_arguments, does_not_return);
 637         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
 638       }
 639       break;
 640 
 641     case throw_null_pointer_exception_id:
 642       { StubFrame f(sasm, "throw_null_pointer_exception", dont_gc_arguments, does_not_return);
 643         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
 644       }
 645       break;
 646 
 647     case new_instance_id:
 648     case fast_new_instance_id:
 649     case fast_new_instance_init_check_id:
 650       {
 651         Register klass = r3; // Incoming
 652         Register obj   = r0; // Result
 653 
 654         if (id == new_instance_id) {
 655           __ set_info("new_instance", dont_gc_arguments);
 656         } else if (id == fast_new_instance_id) {
 657           __ set_info("fast new_instance", dont_gc_arguments);
 658         } else {
 659           assert(id == fast_new_instance_init_check_id, "bad StubID");
 660           __ set_info("fast new_instance init check", dont_gc_arguments);
 661         }
 662 
 663         __ enter();
 664         OopMap* map = save_live_registers(sasm);
 665         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
 666         oop_maps = new OopMapSet();
 667         oop_maps->add_gc_map(call_offset, map);
 668         restore_live_registers_except_r0(sasm);
 669         __ verify_oop(obj);
 670         __ leave();
 671         __ ret(lr);
 672 
 673         // r0,: new instance
 674       }
 675 
 676       break;
 677 
 678     case load_klass_id:
 679       {
 680         StubFrame f(sasm, "load_klass", dont_gc_arguments);
 681         save_live_registers_no_oop_map(sasm, true);
 682         f.load_argument(0, r0); // obj
 683         __ call_VM_leaf(CAST_FROM_FN_PTR(address, oopDesc::load_nklass_runtime), r0);
 684         restore_live_registers_except_r0(sasm, true);
 685       }
 686       break;
 687 
 688     case counter_overflow_id:
 689       {
 690         Register bci = r0, method = r1;
 691         __ enter();
 692         OopMap* map = save_live_registers(sasm);
 693         // Retrieve bci
 694         __ ldrw(bci, Address(rfp, 2*BytesPerWord));
 695         // And a pointer to the Method*
 696         __ ldr(method, Address(rfp, 3*BytesPerWord));
 697         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
 698         oop_maps = new OopMapSet();
 699         oop_maps->add_gc_map(call_offset, map);
 700         restore_live_registers(sasm);
 701         __ leave();
 702         __ ret(lr);
 703       }
 704       break;
 705 
 706     case new_type_array_id:
 707     case new_object_array_id:
 708       {
 709         Register length   = r19; // Incoming
 710         Register klass    = r3; // Incoming
 711         Register obj      = r0; // Result
 712 
 713         if (id == new_type_array_id) {
 714           __ set_info("new_type_array", dont_gc_arguments);
 715         } else {
 716           __ set_info("new_object_array", dont_gc_arguments);
 717         }
 718 
 719 #ifdef ASSERT
 720         // assert object type is really an array of the proper kind
 721         {
 722           Label ok;
 723           Register t0 = obj;
 724           __ ldrw(t0, Address(klass, Klass::layout_helper_offset()));
 725           __ asrw(t0, t0, Klass::_lh_array_tag_shift);
 726           int tag = ((id == new_type_array_id)
 727                      ? Klass::_lh_array_tag_type_value
 728                      : Klass::_lh_array_tag_obj_value);
 729           __ mov(rscratch1, tag);
 730           __ cmpw(t0, rscratch1);
 731           __ br(Assembler::EQ, ok);
 732           __ stop("assert(is an array klass)");
 733           __ should_not_reach_here();
 734           __ bind(ok);
 735         }
 736 #endif // ASSERT
 737 
 738         __ enter();
 739         OopMap* map = save_live_registers(sasm);
 740         int call_offset;
 741         if (id == new_type_array_id) {
 742           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
 743         } else {
 744           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
 745         }
 746 
 747         oop_maps = new OopMapSet();
 748         oop_maps->add_gc_map(call_offset, map);
 749         restore_live_registers_except_r0(sasm);
 750 
 751         __ verify_oop(obj);
 752         __ leave();
 753         __ ret(lr);
 754 
 755         // r0: new array
 756       }
 757       break;
 758 
 759     case new_multi_array_id:
 760       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
 761         // r0,: klass
 762         // r19,: rank
 763         // r2: address of 1st dimension
 764         OopMap* map = save_live_registers(sasm);
 765         __ mov(c_rarg1, r0);
 766         __ mov(c_rarg3, r2);
 767         __ mov(c_rarg2, r19);
 768         int call_offset = __ call_RT(r0, noreg, CAST_FROM_FN_PTR(address, new_multi_array), r1, r2, r3);
 769 
 770         oop_maps = new OopMapSet();
 771         oop_maps->add_gc_map(call_offset, map);
 772         restore_live_registers_except_r0(sasm);
 773 
 774         // r0,: new multi array
 775         __ verify_oop(r0);
 776       }
 777       break;
 778 
 779     case register_finalizer_id:
 780       {
 781         __ set_info("register_finalizer", dont_gc_arguments);
 782 
 783         // This is called via call_runtime so the arguments
 784         // will be place in C abi locations
 785 
 786         __ verify_oop(c_rarg0);
 787 
 788         // load the klass and check the has finalizer flag
 789         Label register_finalizer;
 790         Register t = r5;
 791         __ load_klass(t, r0);
 792         __ ldrw(t, Address(t, Klass::access_flags_offset()));
 793         __ tbnz(t, exact_log2(JVM_ACC_HAS_FINALIZER), register_finalizer);
 794         __ ret(lr);
 795 
 796         __ bind(register_finalizer);
 797         __ enter();
 798         OopMap* oop_map = save_live_registers(sasm);
 799         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), r0);
 800         oop_maps = new OopMapSet();
 801         oop_maps->add_gc_map(call_offset, oop_map);
 802 
 803         // Now restore all the live registers
 804         restore_live_registers(sasm);
 805 
 806         __ leave();
 807         __ ret(lr);
 808       }
 809       break;
 810 
 811     case throw_class_cast_exception_id:
 812       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments, does_not_return);
 813         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
 814       }
 815       break;
 816 
 817     case throw_incompatible_class_change_error_id:
 818       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments, does_not_return);
 819         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
 820       }
 821       break;
 822 
 823     case slow_subtype_check_id:
 824       {
 825         // Typical calling sequence:
 826         // __ push(klass_RInfo);  // object klass or other subclass
 827         // __ push(sup_k_RInfo);  // array element klass or other superclass
 828         // __ bl(slow_subtype_check);
 829         // Note that the subclass is pushed first, and is therefore deepest.
 830         enum layout {
 831           r0_off, r0_off_hi,
 832           r2_off, r2_off_hi,
 833           r4_off, r4_off_hi,
 834           r5_off, r5_off_hi,
 835           sup_k_off, sup_k_off_hi,
 836           klass_off, klass_off_hi,
 837           framesize,
 838           result_off = sup_k_off
 839         };
 840 
 841         __ set_info("slow_subtype_check", dont_gc_arguments);
 842         __ push(RegSet::of(r0, r2, r4, r5), sp);
 843 
 844         // This is called by pushing args and not with C abi
 845         // __ ldr(r4, Address(sp, (klass_off) * VMRegImpl::stack_slot_size)); // subclass
 846         // __ ldr(r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size)); // superclass
 847 
 848         __ ldp(r4, r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size));
 849 
 850         Label miss;
 851         __ check_klass_subtype_slow_path(r4, r0, r2, r5, NULL, &miss);
 852 
 853         // fallthrough on success:
 854         __ mov(rscratch1, 1);
 855         __ str(rscratch1, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
 856         __ pop(RegSet::of(r0, r2, r4, r5), sp);
 857         __ ret(lr);
 858 
 859         __ bind(miss);
 860         __ str(zr, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
 861         __ pop(RegSet::of(r0, r2, r4, r5), sp);
 862         __ ret(lr);
 863       }
 864       break;
 865 
 866     case monitorenter_nofpu_id:
 867       save_fpu_registers = false;
 868       // fall through
 869     case monitorenter_id:
 870       {
 871         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
 872         OopMap* map = save_live_registers(sasm, save_fpu_registers);
 873 
 874         // Called with store_parameter and not C abi
 875 
 876         f.load_argument(0, r0); // r0,: object
 877 
 878         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), r0);
 879 
 880         oop_maps = new OopMapSet();
 881         oop_maps->add_gc_map(call_offset, map);
 882         restore_live_registers(sasm, save_fpu_registers);
 883       }
 884       break;
 885 
 886     case monitorexit_nofpu_id:
 887       save_fpu_registers = false;
 888       // fall through
 889     case monitorexit_id:
 890       {
 891         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
 892         OopMap* map = save_live_registers(sasm, save_fpu_registers);
 893 
 894         // Called with store_parameter and not C abi
 895 
 896         f.load_argument(0, r0); // r0: object
 897 
 898         // note: really a leaf routine but must setup last java sp
 899         //       => use call_RT for now (speed can be improved by
 900         //       doing last java sp setup manually)
 901         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), r0);
 902 
 903         oop_maps = new OopMapSet();
 904         oop_maps->add_gc_map(call_offset, map);
 905         restore_live_registers(sasm, save_fpu_registers);
 906       }
 907       break;
 908 
 909     case deoptimize_id:
 910       {
 911         StubFrame f(sasm, "deoptimize", dont_gc_arguments, does_not_return);
 912         OopMap* oop_map = save_live_registers(sasm);
 913         f.load_argument(0, c_rarg1);
 914         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), c_rarg1);
 915 
 916         oop_maps = new OopMapSet();
 917         oop_maps->add_gc_map(call_offset, oop_map);
 918         restore_live_registers(sasm);
 919         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 920         assert(deopt_blob != NULL, "deoptimization blob must have been created");
 921         __ leave();
 922         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 923       }
 924       break;
 925 
 926     case throw_range_check_failed_id:
 927       { StubFrame f(sasm, "range_check_failed", dont_gc_arguments, does_not_return);
 928         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
 929       }
 930       break;
 931 
 932     case unwind_exception_id:
 933       { __ set_info("unwind_exception", dont_gc_arguments);
 934         // note: no stubframe since we are about to leave the current
 935         //       activation and we are calling a leaf VM function only.
 936         generate_unwind_exception(sasm);
 937       }
 938       break;
 939 
 940     case access_field_patching_id:
 941       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments, does_not_return);
 942         // we should set up register map
 943         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
 944       }
 945       break;
 946 
 947     case load_klass_patching_id:
 948       { StubFrame f(sasm, "load_klass_patching", dont_gc_arguments, does_not_return);
 949         // we should set up register map
 950         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
 951       }
 952       break;
 953 
 954     case load_mirror_patching_id:
 955       { StubFrame f(sasm, "load_mirror_patching", dont_gc_arguments, does_not_return);
 956         // we should set up register map
 957         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
 958       }
 959       break;
 960 
 961     case load_appendix_patching_id:
 962       { StubFrame f(sasm, "load_appendix_patching", dont_gc_arguments, does_not_return);
 963         // we should set up register map
 964         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
 965       }
 966       break;
 967 
 968     case handle_exception_nofpu_id:
 969     case handle_exception_id:
 970       { StubFrame f(sasm, "handle_exception", dont_gc_arguments);
 971         oop_maps = generate_handle_exception(id, sasm);
 972       }
 973       break;
 974 
 975     case handle_exception_from_callee_id:
 976       { StubFrame f(sasm, "handle_exception_from_callee", dont_gc_arguments);
 977         oop_maps = generate_handle_exception(id, sasm);
 978       }
 979       break;
 980 
 981     case throw_index_exception_id:
 982       { StubFrame f(sasm, "index_range_check_failed", dont_gc_arguments, does_not_return);
 983         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
 984       }
 985       break;
 986 
 987     case throw_array_store_exception_id:
 988       { StubFrame f(sasm, "throw_array_store_exception", dont_gc_arguments, does_not_return);
 989         // tos + 0: link
 990         //     + 1: return address
 991         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
 992       }
 993       break;
 994 
 995     case predicate_failed_trap_id:
 996       {
 997         StubFrame f(sasm, "predicate_failed_trap", dont_gc_arguments, does_not_return);
 998 
 999         OopMap* map = save_live_registers(sasm);
1000 
1001         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
1002         oop_maps = new OopMapSet();
1003         oop_maps->add_gc_map(call_offset, map);
1004         restore_live_registers(sasm);
1005         __ leave();
1006         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1007         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1008 
1009         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1010       }
1011       break;
1012 
1013     case dtrace_object_alloc_id:
1014       { // c_rarg0: object
1015         StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
1016         save_live_registers(sasm);
1017 
1018         __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), c_rarg0);
1019 
1020         restore_live_registers(sasm);
1021       }
1022       break;
1023 
1024     default:
1025       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments, does_not_return);
1026         __ mov(r0, (int)id);
1027         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), r0);
1028       }
1029       break;
1030     }
1031   }
1032   return oop_maps;
1033 }
1034 
1035 #undef __
1036 
1037 const char *Runtime1::pd_name_for_address(address entry) { Unimplemented(); return 0; }