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 r22 and r23)
 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     call_offset = __ call_RT(noreg, noreg, target, r22, r23);
 336   }
 337   OopMapSet* oop_maps = new OopMapSet();
 338   oop_maps->add_gc_map(call_offset, oop_map);
 339 
 340   __ should_not_reach_here();
 341   return oop_maps;
 342 }
 343 
 344 
 345 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler *sasm) {
 346   __ block_comment("generate_handle_exception");
 347 
 348   // incoming parameters
 349   const Register exception_oop = r0;
 350   const Register exception_pc  = r3;
 351   // other registers used in this stub
 352 
 353   // Save registers, if required.
 354   OopMapSet* oop_maps = new OopMapSet();
 355   OopMap* oop_map = NULL;
 356   switch (id) {
 357   case forward_exception_id:
 358     // We're handling an exception in the context of a compiled frame.
 359     // The registers have been saved in the standard places.  Perform
 360     // an exception lookup in the caller and dispatch to the handler
 361     // if found.  Otherwise unwind and dispatch to the callers
 362     // exception handler.
 363     oop_map = generate_oop_map(sasm, 1 /*thread*/);
 364 
 365     // load and clear pending exception oop into r0
 366     __ ldr(exception_oop, Address(rthread, Thread::pending_exception_offset()));
 367     __ str(zr, Address(rthread, Thread::pending_exception_offset()));
 368 
 369     // load issuing PC (the return address for this stub) into r3
 370     __ ldr(exception_pc, Address(rfp, 1*BytesPerWord));
 371 
 372     // make sure that the vm_results are cleared (may be unnecessary)
 373     __ str(zr, Address(rthread, JavaThread::vm_result_offset()));
 374     __ str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
 375     break;
 376   case handle_exception_nofpu_id:
 377   case handle_exception_id:
 378     // At this point all registers MAY be live.
 379     oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id);
 380     break;
 381   case handle_exception_from_callee_id: {
 382     // At this point all registers except exception oop (r0) and
 383     // exception pc (lr) are dead.
 384     const int frame_size = 2 /*fp, return address*/;
 385     oop_map = new OopMap(frame_size * VMRegImpl::slots_per_word, 0);
 386     sasm->set_frame_size(frame_size);
 387     break;
 388   }
 389   default:
 390     __ should_not_reach_here();
 391     break;
 392   }
 393 
 394   // verify that only r0 and r3 are valid at this time
 395   __ invalidate_registers(false, true, true, false, true, true);
 396   // verify that r0 contains a valid exception
 397   __ verify_not_null_oop(exception_oop);
 398 
 399 #ifdef ASSERT
 400   // check that fields in JavaThread for exception oop and issuing pc are
 401   // empty before writing to them
 402   Label oop_empty;
 403   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 404   __ cbz(rscratch1, oop_empty);
 405   __ stop("exception oop already set");
 406   __ bind(oop_empty);
 407 
 408   Label pc_empty;
 409   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 410   __ cbz(rscratch1, pc_empty);
 411   __ stop("exception pc already set");
 412   __ bind(pc_empty);
 413 #endif
 414 
 415   // save exception oop and issuing pc into JavaThread
 416   // (exception handler will load it from here)
 417   __ str(exception_oop, Address(rthread, JavaThread::exception_oop_offset()));
 418   __ str(exception_pc, Address(rthread, JavaThread::exception_pc_offset()));
 419 
 420   // patch throwing pc into return address (has bci & oop map)
 421   __ str(exception_pc, Address(rfp, 1*BytesPerWord));
 422 
 423   // compute the exception handler.
 424   // the exception oop and the throwing pc are read from the fields in JavaThread
 425   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 426   oop_maps->add_gc_map(call_offset, oop_map);
 427 
 428   // r0: handler address
 429   //      will be the deopt blob if nmethod was deoptimized while we looked up
 430   //      handler regardless of whether handler existed in the nmethod.
 431 
 432   // only r0 is valid at this time, all other registers have been destroyed by the runtime call
 433   __ invalidate_registers(false, true, true, true, true, true);
 434 
 435   // patch the return address, this stub will directly return to the exception handler
 436   __ str(r0, Address(rfp, 1*BytesPerWord));
 437 
 438   switch (id) {
 439   case forward_exception_id:
 440   case handle_exception_nofpu_id:
 441   case handle_exception_id:
 442     // Restore the registers that were saved at the beginning.
 443     restore_live_registers(sasm, id != handle_exception_nofpu_id);
 444     break;
 445   case handle_exception_from_callee_id:
 446     // Pop the return address.
 447     __ leave();
 448     __ ret(lr);  // jump to exception handler
 449     break;
 450   default:  ShouldNotReachHere();
 451   }
 452 
 453   return oop_maps;
 454 }
 455 
 456 
 457 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 458   // incoming parameters
 459   const Register exception_oop = r0;
 460   // callee-saved copy of exception_oop during runtime call
 461   const Register exception_oop_callee_saved = r19;
 462   // other registers used in this stub
 463   const Register exception_pc = r3;
 464   const Register handler_addr = r1;
 465 
 466   // verify that only r0, is valid at this time
 467   __ invalidate_registers(false, true, true, true, true, true);
 468 
 469 #ifdef ASSERT
 470   // check that fields in JavaThread for exception oop and issuing pc are empty
 471   Label oop_empty;
 472   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 473   __ cbz(rscratch1, oop_empty);
 474   __ stop("exception oop must be empty");
 475   __ bind(oop_empty);
 476 
 477   Label pc_empty;
 478   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 479   __ cbz(rscratch1, pc_empty);
 480   __ stop("exception pc must be empty");
 481   __ bind(pc_empty);
 482 #endif
 483 
 484   // Save our return address because
 485   // exception_handler_for_return_address will destroy it.  We also
 486   // save exception_oop
 487   __ stp(lr, exception_oop, Address(__ pre(sp, -2 * wordSize)));
 488 
 489   // search the exception handler address of the caller (using the return address)
 490   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), rthread, lr);
 491   // r0: exception handler address of the caller
 492 
 493   // Only R0 is valid at this time; all other registers have been
 494   // destroyed by the call.
 495   __ invalidate_registers(false, true, true, true, false, true);
 496 
 497   // move result of call into correct register
 498   __ mov(handler_addr, r0);
 499 
 500   // get throwing pc (= return address).
 501   // lr has been destroyed by the call
 502   __ ldp(lr, exception_oop, Address(__ post(sp, 2 * wordSize)));
 503   __ mov(r3, lr);
 504 
 505   __ verify_not_null_oop(exception_oop);
 506 
 507   // continue at exception handler (return address removed)
 508   // note: do *not* remove arguments when unwinding the
 509   //       activation since the caller assumes having
 510   //       all arguments on the stack when entering the
 511   //       runtime to determine the exception handler
 512   //       (GC happens at call site with arguments!)
 513   // r0: exception oop
 514   // r3: throwing pc
 515   // r1: exception handler
 516   __ br(handler_addr);
 517 }
 518 
 519 
 520 
 521 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 522   // use the maximum number of runtime-arguments here because it is difficult to
 523   // distinguish each RT-Call.
 524   // Note: This number affects also the RT-Call in generate_handle_exception because
 525   //       the oop-map is shared for all calls.
 526   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 527   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 528 
 529   OopMap* oop_map = save_live_registers(sasm);
 530 
 531   __ mov(c_rarg0, rthread);
 532   Label retaddr;
 533   __ set_last_Java_frame(sp, rfp, retaddr, rscratch1);
 534   // do the call
 535   __ lea(rscratch1, RuntimeAddress(target));
 536   __ blrt(rscratch1, 1, 0, 1);
 537   __ bind(retaddr);
 538   OopMapSet* oop_maps = new OopMapSet();
 539   oop_maps->add_gc_map(__ offset(), oop_map);
 540   // verify callee-saved register
 541 #ifdef ASSERT
 542   { Label L;
 543     __ get_thread(rscratch1);
 544     __ cmp(rthread, rscratch1);
 545     __ br(Assembler::EQ, L);
 546     __ stop("StubAssembler::call_RT: rthread not callee saved?");
 547     __ bind(L);
 548   }
 549 #endif
 550   __ reset_last_Java_frame(true);
 551   __ maybe_isb();
 552 
 553   // check for pending exceptions
 554   { Label L;
 555     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 556     __ cbz(rscratch1, L);
 557     // exception pending => remove activation and forward to exception handler
 558 
 559     { Label L1;
 560       __ cbnz(r0, L1);                                  // have we deoptimized?
 561       __ far_jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 562       __ bind(L1);
 563     }
 564 
 565     // the deopt blob expects exceptions in the special fields of
 566     // JavaThread, so copy and clear pending exception.
 567 
 568     // load and clear pending exception
 569     __ ldr(r0, Address(rthread, Thread::pending_exception_offset()));
 570     __ str(zr, Address(rthread, Thread::pending_exception_offset()));
 571 
 572     // check that there is really a valid exception
 573     __ verify_not_null_oop(r0);
 574 
 575     // load throwing pc: this is the return address of the stub
 576     __ mov(r3, lr);
 577 
 578 #ifdef ASSERT
 579     // check that fields in JavaThread for exception oop and issuing pc are empty
 580     Label oop_empty;
 581     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 582     __ cbz(rscratch1, oop_empty);
 583     __ stop("exception oop must be empty");
 584     __ bind(oop_empty);
 585 
 586     Label pc_empty;
 587     __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 588     __ cbz(rscratch1, pc_empty);
 589     __ stop("exception pc must be empty");
 590     __ bind(pc_empty);
 591 #endif
 592 
 593     // store exception oop and throwing pc to JavaThread
 594     __ str(r0, Address(rthread, JavaThread::exception_oop_offset()));
 595     __ str(r3, Address(rthread, JavaThread::exception_pc_offset()));
 596 
 597     restore_live_registers(sasm);
 598 
 599     __ leave();
 600 
 601     // Forward the exception directly to deopt blob. We can blow no
 602     // registers and must leave throwing pc on the stack.  A patch may
 603     // have values live in registers so the entry point with the
 604     // exception in tls.
 605     __ far_jump(RuntimeAddress(deopt_blob->unpack_with_exception_in_tls()));
 606 
 607     __ bind(L);
 608   }
 609 
 610 
 611   // Runtime will return true if the nmethod has been deoptimized during
 612   // the patching process. In that case we must do a deopt reexecute instead.
 613 
 614   Label reexecuteEntry, cont;
 615 
 616   __ cbz(r0, cont);                                 // have we deoptimized?
 617 
 618   // Will reexecute. Proper return address is already on the stack we just restore
 619   // registers, pop all of our frame but the return address and jump to the deopt blob
 620   restore_live_registers(sasm);
 621   __ leave();
 622   __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 623 
 624   __ bind(cont);
 625   restore_live_registers(sasm);
 626   __ leave();
 627   __ ret(lr);
 628 
 629   return oop_maps;
 630 }
 631 
 632 
 633 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 634 
 635   const Register exception_oop = r0;
 636   const Register exception_pc  = r3;
 637 
 638   // for better readability
 639   const bool must_gc_arguments = true;
 640   const bool dont_gc_arguments = false;
 641 
 642   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 643   bool save_fpu_registers = true;
 644 
 645   // stub code & info for the different stubs
 646   OopMapSet* oop_maps = NULL;
 647   OopMap* oop_map = NULL;
 648   switch (id) {
 649     {
 650     case forward_exception_id:
 651       {
 652         oop_maps = generate_handle_exception(id, sasm);
 653         __ leave();
 654         __ ret(lr);
 655       }
 656       break;
 657 
 658     case throw_div0_exception_id:
 659       { StubFrame f(sasm, "throw_div0_exception", dont_gc_arguments);
 660         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
 661       }
 662       break;
 663 
 664     case throw_null_pointer_exception_id:
 665       { StubFrame f(sasm, "throw_null_pointer_exception", dont_gc_arguments);
 666         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
 667       }
 668       break;
 669 
 670     case new_instance_id:
 671     case fast_new_instance_id:
 672     case fast_new_instance_init_check_id:
 673       {
 674         Register klass = r3; // Incoming
 675         Register obj   = r0; // Result
 676 
 677         if (id == new_instance_id) {
 678           __ set_info("new_instance", dont_gc_arguments);
 679         } else if (id == fast_new_instance_id) {
 680           __ set_info("fast new_instance", dont_gc_arguments);
 681         } else {
 682           assert(id == fast_new_instance_init_check_id, "bad StubID");
 683           __ set_info("fast new_instance init check", dont_gc_arguments);
 684         }
 685 
 686         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
 687             UseTLAB && Universe::heap()->supports_inline_contig_alloc()) {
 688           Label slow_path;
 689           Register obj_size = r2;
 690           Register t1       = r19;
 691           Register t2       = r4;
 692           assert_different_registers(klass, obj, obj_size, t1, t2);
 693 
 694           __ stp(r19, zr, Address(__ pre(sp, -2 * wordSize)));
 695 
 696           if (id == fast_new_instance_init_check_id) {
 697             // make sure the klass is initialized
 698             __ ldrb(rscratch1, Address(klass, InstanceKlass::init_state_offset()));
 699             __ cmpw(rscratch1, InstanceKlass::fully_initialized);
 700             __ br(Assembler::NE, slow_path);
 701           }
 702 
 703 #ifdef ASSERT
 704           // assert object can be fast path allocated
 705           {
 706             Label ok, not_ok;
 707             __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 708             __ cmp(obj_size, 0u);
 709             __ br(Assembler::LE, not_ok);  // make sure it's an instance (LH > 0)
 710             __ tstw(obj_size, Klass::_lh_instance_slow_path_bit);
 711             __ br(Assembler::EQ, ok);
 712             __ bind(not_ok);
 713             __ stop("assert(can be fast path allocated)");
 714             __ should_not_reach_here();
 715             __ bind(ok);
 716           }
 717 #endif // ASSERT
 718 
 719           // get the instance size (size is postive so movl is fine for 64bit)
 720           __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 721 
 722           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
 723           __ incr_allocated_bytes(rthread, obj_size, 0, rscratch1);
 724 
 725           __ initialize_object(obj, klass, obj_size, 0, t1, t2, /* is_tlab_allocated */ false);
 726           __ verify_oop(obj);
 727           __ ldp(r19, zr, Address(__ post(sp, 2 * wordSize)));
 728           __ ret(lr);
 729 
 730           __ bind(slow_path);
 731           __ ldp(r19, zr, Address(__ post(sp, 2 * wordSize)));
 732         }
 733 
 734         __ enter();
 735         OopMap* map = save_live_registers(sasm);
 736         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
 737         oop_maps = new OopMapSet();
 738         oop_maps->add_gc_map(call_offset, map);
 739         restore_live_registers_except_r0(sasm);
 740         __ verify_oop(obj);
 741         __ leave();
 742         __ ret(lr);
 743 
 744         // r0,: new instance
 745       }
 746 
 747       break;
 748 
 749     case counter_overflow_id:
 750       {
 751         Register bci = r0, method = r1;
 752         __ enter();
 753         OopMap* map = save_live_registers(sasm);
 754         // Retrieve bci
 755         __ ldrw(bci, Address(rfp, 2*BytesPerWord));
 756         // And a pointer to the Method*
 757         __ ldr(method, Address(rfp, 3*BytesPerWord));
 758         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
 759         oop_maps = new OopMapSet();
 760         oop_maps->add_gc_map(call_offset, map);
 761         restore_live_registers(sasm);
 762         __ leave();
 763         __ ret(lr);
 764       }
 765       break;
 766 
 767     case new_type_array_id:
 768     case new_object_array_id:
 769       {
 770         Register length   = r19; // Incoming
 771         Register klass    = r3; // Incoming
 772         Register obj      = r0; // Result
 773 
 774         if (id == new_type_array_id) {
 775           __ set_info("new_type_array", dont_gc_arguments);
 776         } else {
 777           __ set_info("new_object_array", dont_gc_arguments);
 778         }
 779 
 780 #ifdef ASSERT
 781         // assert object type is really an array of the proper kind
 782         {
 783           Label ok;
 784           Register t0 = obj;
 785           __ ldrw(t0, Address(klass, Klass::layout_helper_offset()));
 786           __ asrw(t0, t0, Klass::_lh_array_tag_shift);
 787           int tag = ((id == new_type_array_id)
 788                      ? Klass::_lh_array_tag_type_value
 789                      : Klass::_lh_array_tag_obj_value);
 790           __ mov(rscratch1, tag);
 791           __ cmpw(t0, rscratch1);
 792           __ br(Assembler::EQ, ok);
 793           __ stop("assert(is an array klass)");
 794           __ should_not_reach_here();
 795           __ bind(ok);
 796         }
 797 #endif // ASSERT
 798 
 799         if (UseTLAB && Universe::heap()->supports_inline_contig_alloc()) {
 800           Register arr_size = r4;
 801           Register t1       = r2;
 802           Register t2       = r5;
 803           Label slow_path;
 804           assert_different_registers(length, klass, obj, arr_size, t1, t2);
 805 
 806           // check that array length is small enough for fast path.
 807           __ mov(rscratch1, C1_MacroAssembler::max_array_allocation_length);
 808           __ cmpw(length, rscratch1);
 809           __ br(Assembler::HI, slow_path);
 810 
 811           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
 812           // since size is positive ldrw does right thing on 64bit
 813           __ ldrw(t1, Address(klass, Klass::layout_helper_offset()));
 814           // since size is positive movw does right thing on 64bit
 815           __ movw(arr_size, length);
 816           __ lslvw(arr_size, length, t1);
 817           __ ubfx(t1, t1, Klass::_lh_header_size_shift,
 818                   exact_log2(Klass::_lh_header_size_mask + 1));
 819           __ add(arr_size, arr_size, t1);
 820           __ add(arr_size, arr_size, MinObjAlignmentInBytesMask); // align up
 821           __ andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 822 
 823           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
 824           __ incr_allocated_bytes(rthread, arr_size, 0, rscratch1);
 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; }