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