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