1 /*
   2  * Copyright (c) 1999, 2017, 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 "interpreter/interpreter.hpp"
  34 #include "nativeInst_aarch64.hpp"
  35 #include "oops/compiledICHolder.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "register_aarch64.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/signature.hpp"
  41 #include "runtime/vframe.hpp"
  42 #include "runtime/vframeArray.hpp"
  43 #include "vmreg_aarch64.inline.hpp"
  44 #if INCLUDE_ALL_GCS
  45 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  46 #endif
  47 
  48 
  49 // Implementation of StubAssembler
  50 
  51 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, int args_size) {
  52   // setup registers
  53   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) || oop_result1 != metadata_result, "registers must be different");
  54   assert(oop_result1 != rthread && metadata_result != rthread, "registers must be different");
  55   assert(args_size >= 0, "illegal args_size");
  56   bool align_stack = false;
  57 
  58   mov(c_rarg0, rthread);
  59   set_num_rt_args(0); // Nothing on stack
  60 
  61   Label retaddr;
  62   set_last_Java_frame(sp, rfp, retaddr, rscratch1);
  63 
  64   // do the call
  65   lea(rscratch1, RuntimeAddress(entry));
  66   blrt(rscratch1, args_size + 1, 8, 1);
  67   bind(retaddr);
  68   int call_offset = offset();
  69   // verify callee-saved register
  70 #ifdef ASSERT
  71   push(r0, sp);
  72   { Label L;
  73     get_thread(r0);
  74     cmp(rthread, r0);
  75     br(Assembler::EQ, L);
  76     stop("StubAssembler::call_RT: rthread not callee saved?");
  77     bind(L);
  78   }
  79   pop(r0, sp);
  80 #endif
  81   reset_last_Java_frame(true);
  82   maybe_isb();
  83 
  84   // check for pending exceptions
  85   { Label L;
  86     // check for pending exceptions (java_thread is set upon return)
  87     ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset())));
  88     cbz(rscratch1, L);
  89     // exception pending => remove activation and forward to exception handler
  90     // make sure that the vm_results are cleared
  91     if (oop_result1->is_valid()) {
  92       str(zr, Address(rthread, JavaThread::vm_result_offset()));
  93     }
  94     if (metadata_result->is_valid()) {
  95       str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
  96     }
  97     if (frame_size() == no_frame_size) {
  98       leave();
  99       far_jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 100     } else if (_stub_id == Runtime1::forward_exception_id) {
 101       should_not_reach_here();
 102     } else {
 103       far_jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 104     }
 105     bind(L);
 106   }
 107   // get oop results if there are any and reset the values in the thread
 108   if (oop_result1->is_valid()) {
 109     get_vm_result(oop_result1, rthread);
 110   }
 111   if (metadata_result->is_valid()) {
 112     get_vm_result_2(metadata_result, rthread);
 113   }
 114   return call_offset;
 115 }
 116 
 117 
 118 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
 119   mov(c_rarg1, arg1);
 120   return call_RT(oop_result1, metadata_result, entry, 1);
 121 }
 122 
 123 
 124 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
 125   if (c_rarg1 == arg2) {
 126     if (c_rarg2 == arg1) {
 127       mov(rscratch1, arg1);
 128       mov(arg1, arg2);
 129       mov(arg2, rscratch1);
 130     } else {
 131       mov(c_rarg2, arg2);
 132       mov(c_rarg1, arg1);
 133     }
 134   } else {
 135     mov(c_rarg1, arg1);
 136     mov(c_rarg2, arg2);
 137   }
 138   return call_RT(oop_result1, metadata_result, entry, 2);
 139 }
 140 
 141 
 142 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
 143   // if there is any conflict use the stack
 144   if (arg1 == c_rarg2 || arg1 == c_rarg3 ||
 145       arg2 == c_rarg1 || arg1 == c_rarg3 ||
 146       arg3 == c_rarg1 || arg1 == c_rarg2) {
 147     stp(arg3, arg2, Address(pre(sp, 2 * wordSize)));
 148     stp(arg1, zr, Address(pre(sp, -2 * wordSize)));
 149     ldp(c_rarg1, zr, Address(post(sp, 2 * wordSize)));
 150     ldp(c_rarg3, c_rarg2, Address(post(sp, 2 * wordSize)));
 151   } else {
 152     mov(c_rarg1, arg1);
 153     mov(c_rarg2, arg2);
 154     mov(c_rarg3, arg3);
 155   }
 156   return call_RT(oop_result1, metadata_result, entry, 3);
 157 }
 158 
 159 // Implementation of StubFrame
 160 
 161 class StubFrame: public StackObj {
 162  private:
 163   StubAssembler* _sasm;
 164 
 165  public:
 166   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments);
 167   void load_argument(int offset_in_words, Register reg);
 168 
 169   ~StubFrame();
 170 };;
 171 
 172 
 173 #define __ _sasm->
 174 
 175 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments) {
 176   _sasm = sasm;
 177   __ set_info(name, must_gc_arguments);
 178   __ enter();
 179 }
 180 
 181 // load parameters that were stored with LIR_Assembler::store_parameter
 182 // Note: offsets for store_parameter and load_argument must match
 183 void StubFrame::load_argument(int offset_in_words, Register reg) {
 184   // rbp, + 0: link
 185   //     + 1: return address
 186   //     + 2: argument with offset 0
 187   //     + 3: argument with offset 1
 188   //     + 4: ...
 189 
 190   __ ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
 191 }
 192 
 193 
 194 StubFrame::~StubFrame() {
 195   __ leave();
 196   __ ret(lr);
 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 an argument (passed in rscratch1)
 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, rscratch1);
 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 && FastTLABRefill) {
 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(r5, r19, 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           // if we got here then the TLAB allocation failed, so try
 720           // refilling the TLAB or allocating directly from eden.
 721           Label retry_tlab, try_eden;
 722           __ tlab_refill(retry_tlab, try_eden, slow_path); // does not destroy r3 (klass), returns r5
 723 
 724           __ bind(retry_tlab);
 725 
 726           // get the instance size (size is postive so movl is fine for 64bit)
 727           __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 728 
 729           __ tlab_allocate(obj, obj_size, 0, t1, t2, slow_path);
 730 
 731           __ initialize_object(obj, klass, obj_size, 0, t1, t2, /* is_tlab_allocated */ true);
 732           __ verify_oop(obj);
 733           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 734           __ ret(lr);
 735 
 736           __ bind(try_eden);
 737           // get the instance size (size is postive so movl is fine for 64bit)
 738           __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 739 
 740           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
 741           __ incr_allocated_bytes(rthread, obj_size, 0, rscratch1);
 742 
 743           __ initialize_object(obj, klass, obj_size, 0, t1, t2, /* is_tlab_allocated */ false);
 744           __ verify_oop(obj);
 745           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 746           __ ret(lr);
 747 
 748           __ bind(slow_path);
 749           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 750         }
 751 
 752         __ enter();
 753         OopMap* map = save_live_registers(sasm);
 754         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
 755         oop_maps = new OopMapSet();
 756         oop_maps->add_gc_map(call_offset, map);
 757         restore_live_registers_except_r0(sasm);
 758         __ verify_oop(obj);
 759         __ leave();
 760         __ ret(lr);
 761 
 762         // r0,: new instance
 763       }
 764 
 765       break;
 766 
 767     case counter_overflow_id:
 768       {
 769         Register bci = r0, method = r1;
 770         __ enter();
 771         OopMap* map = save_live_registers(sasm);
 772         // Retrieve bci
 773         __ ldrw(bci, Address(rfp, 2*BytesPerWord));
 774         // And a pointer to the Method*
 775         __ ldr(method, Address(rfp, 3*BytesPerWord));
 776         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
 777         oop_maps = new OopMapSet();
 778         oop_maps->add_gc_map(call_offset, map);
 779         restore_live_registers(sasm);
 780         __ leave();
 781         __ ret(lr);
 782       }
 783       break;
 784 
 785     case new_type_array_id:
 786     case new_object_array_id:
 787       {
 788         Register length   = r19; // Incoming
 789         Register klass    = r3; // Incoming
 790         Register obj      = r0; // Result
 791 
 792         if (id == new_type_array_id) {
 793           __ set_info("new_type_array", dont_gc_arguments);
 794         } else {
 795           __ set_info("new_object_array", dont_gc_arguments);
 796         }
 797 
 798 #ifdef ASSERT
 799         // assert object type is really an array of the proper kind
 800         {
 801           Label ok;
 802           Register t0 = obj;
 803           __ ldrw(t0, Address(klass, Klass::layout_helper_offset()));
 804           __ asrw(t0, t0, Klass::_lh_array_tag_shift);
 805           int tag = ((id == new_type_array_id)
 806                      ? Klass::_lh_array_tag_type_value
 807                      : Klass::_lh_array_tag_obj_value);
 808           __ mov(rscratch1, tag);
 809           __ cmpw(t0, rscratch1);
 810           __ br(Assembler::EQ, ok);
 811           __ stop("assert(is an array klass)");
 812           __ should_not_reach_here();
 813           __ bind(ok);
 814         }
 815 #endif // ASSERT
 816 
 817         if (UseTLAB && FastTLABRefill) {
 818           Register arr_size = r4;
 819           Register t1       = r2;
 820           Register t2       = r5;
 821           Label slow_path;
 822           assert_different_registers(length, klass, obj, arr_size, t1, t2);
 823 
 824           // check that array length is small enough for fast path.
 825           __ mov(rscratch1, C1_MacroAssembler::max_array_allocation_length);
 826           __ cmpw(length, rscratch1);
 827           __ br(Assembler::HI, slow_path);
 828 
 829           // if we got here then the TLAB allocation failed, so try
 830           // refilling the TLAB or allocating directly from eden.
 831           Label retry_tlab, try_eden;
 832           const Register thread =
 833             __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves r19 & r3, returns rthread
 834 
 835           __ bind(retry_tlab);
 836 
 837           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
 838           // since size is positive ldrw does right thing on 64bit
 839           __ ldrw(t1, Address(klass, Klass::layout_helper_offset()));
 840           __ lslvw(arr_size, length, t1);
 841           __ ubfx(t1, t1, Klass::_lh_header_size_shift,
 842                   exact_log2(Klass::_lh_header_size_mask + 1));
 843           __ add(arr_size, arr_size, t1);
 844           __ add(arr_size, arr_size, MinObjAlignmentInBytesMask); // align up
 845           __ andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 846 
 847           __ tlab_allocate(obj, arr_size, 0, t1, t2, slow_path);  // preserves arr_size
 848 
 849           __ initialize_header(obj, klass, length, t1, t2);
 850           __ ldrb(t1, Address(klass, in_bytes(Klass::layout_helper_offset()) + (Klass::_lh_header_size_shift / BitsPerByte)));
 851           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
 852           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
 853           __ andr(t1, t1, Klass::_lh_header_size_mask);
 854           __ sub(arr_size, arr_size, t1);  // body length
 855           __ add(t1, t1, obj);       // body start
 856           if (!ZeroTLAB) {
 857            __ initialize_body(t1, arr_size, 0, t2);
 858           }
 859           __ verify_oop(obj);
 860 
 861           __ ret(lr);
 862 
 863           __ bind(try_eden);
 864           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
 865           // since size is positive ldrw does right thing on 64bit
 866           __ ldrw(t1, Address(klass, Klass::layout_helper_offset()));
 867           // since size is postive movw does right thing on 64bit
 868           __ movw(arr_size, length);
 869           __ lslvw(arr_size, length, t1);
 870           __ ubfx(t1, t1, Klass::_lh_header_size_shift,
 871                   exact_log2(Klass::_lh_header_size_mask + 1));
 872           __ add(arr_size, arr_size, t1);
 873           __ add(arr_size, arr_size, MinObjAlignmentInBytesMask); // align up
 874           __ andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 875 
 876           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
 877           __ incr_allocated_bytes(thread, arr_size, 0, rscratch1);
 878 
 879           __ initialize_header(obj, klass, length, t1, t2);
 880           __ ldrb(t1, Address(klass, in_bytes(Klass::layout_helper_offset()) + (Klass::_lh_header_size_shift / BitsPerByte)));
 881           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
 882           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
 883           __ andr(t1, t1, Klass::_lh_header_size_mask);
 884           __ sub(arr_size, arr_size, t1);  // body length
 885           __ add(t1, t1, obj);       // body start
 886           __ initialize_body(t1, arr_size, 0, t2);
 887           __ verify_oop(obj);
 888 
 889           __ ret(lr);
 890 
 891           __ bind(slow_path);
 892         }
 893 
 894         __ enter();
 895         OopMap* map = save_live_registers(sasm);
 896         int call_offset;
 897         if (id == new_type_array_id) {
 898           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
 899         } else {
 900           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
 901         }
 902 
 903         oop_maps = new OopMapSet();
 904         oop_maps->add_gc_map(call_offset, map);
 905         restore_live_registers_except_r0(sasm);
 906 
 907         __ verify_oop(obj);
 908         __ leave();
 909         __ ret(lr);
 910 
 911         // r0: new array
 912       }
 913       break;
 914 
 915     case new_multi_array_id:
 916       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
 917         // r0,: klass
 918         // r19,: rank
 919         // r2: address of 1st dimension
 920         OopMap* map = save_live_registers(sasm);
 921         __ mov(c_rarg1, r0);
 922         __ mov(c_rarg3, r2);
 923         __ mov(c_rarg2, r19);
 924         int call_offset = __ call_RT(r0, noreg, CAST_FROM_FN_PTR(address, new_multi_array), r1, r2, r3);
 925 
 926         oop_maps = new OopMapSet();
 927         oop_maps->add_gc_map(call_offset, map);
 928         restore_live_registers_except_r0(sasm);
 929 
 930         // r0,: new multi array
 931         __ verify_oop(r0);
 932       }
 933       break;
 934 
 935     case register_finalizer_id:
 936       {
 937         __ set_info("register_finalizer", dont_gc_arguments);
 938 
 939         // This is called via call_runtime so the arguments
 940         // will be place in C abi locations
 941 
 942         __ verify_oop(c_rarg0);
 943 
 944         // load the klass and check the has finalizer flag
 945         Label register_finalizer;
 946         Register t = r5;
 947         __ load_klass(t, r0);
 948         __ ldrw(t, Address(t, Klass::access_flags_offset()));
 949         __ tbnz(t, exact_log2(JVM_ACC_HAS_FINALIZER), register_finalizer);
 950         __ ret(lr);
 951 
 952         __ bind(register_finalizer);
 953         __ enter();
 954         OopMap* oop_map = save_live_registers(sasm);
 955         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), r0);
 956         oop_maps = new OopMapSet();
 957         oop_maps->add_gc_map(call_offset, oop_map);
 958 
 959         // Now restore all the live registers
 960         restore_live_registers(sasm);
 961 
 962         __ leave();
 963         __ ret(lr);
 964       }
 965       break;
 966 
 967     case throw_class_cast_exception_id:
 968       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments);
 969         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
 970       }
 971       break;
 972 
 973     case throw_incompatible_class_change_error_id:
 974       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments);
 975         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
 976       }
 977       break;
 978 
 979     case slow_subtype_check_id:
 980       {
 981         // Typical calling sequence:
 982         // __ push(klass_RInfo);  // object klass or other subclass
 983         // __ push(sup_k_RInfo);  // array element klass or other superclass
 984         // __ bl(slow_subtype_check);
 985         // Note that the subclass is pushed first, and is therefore deepest.
 986         enum layout {
 987           r0_off, r0_off_hi,
 988           r2_off, r2_off_hi,
 989           r4_off, r4_off_hi,
 990           r5_off, r5_off_hi,
 991           sup_k_off, sup_k_off_hi,
 992           klass_off, klass_off_hi,
 993           framesize,
 994           result_off = sup_k_off
 995         };
 996 
 997         __ set_info("slow_subtype_check", dont_gc_arguments);
 998         __ push(RegSet::of(r0, r2, r4, r5), sp);
 999 
1000         // This is called by pushing args and not with C abi
1001         // __ ldr(r4, Address(sp, (klass_off) * VMRegImpl::stack_slot_size)); // subclass
1002         // __ ldr(r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size)); // superclass
1003 
1004         __ ldp(r4, r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size));
1005 
1006         Label miss;
1007         __ check_klass_subtype_slow_path(r4, r0, r2, r5, NULL, &miss);
1008 
1009         // fallthrough on success:
1010         __ mov(rscratch1, 1);
1011         __ str(rscratch1, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
1012         __ pop(RegSet::of(r0, r2, r4, r5), sp);
1013         __ ret(lr);
1014 
1015         __ bind(miss);
1016         __ str(zr, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
1017         __ pop(RegSet::of(r0, r2, r4, r5), sp);
1018         __ ret(lr);
1019       }
1020       break;
1021 
1022     case monitorenter_nofpu_id:
1023       save_fpu_registers = false;
1024       // fall through
1025     case monitorenter_id:
1026       {
1027         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
1028         OopMap* map = save_live_registers(sasm, save_fpu_registers);
1029 
1030         // Called with store_parameter and not C abi
1031 
1032         f.load_argument(1, r0); // r0,: object
1033         f.load_argument(0, r1); // r1,: lock address
1034 
1035         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), r0, r1);
1036 
1037         oop_maps = new OopMapSet();
1038         oop_maps->add_gc_map(call_offset, map);
1039         restore_live_registers(sasm, save_fpu_registers);
1040       }
1041       break;
1042 
1043     case monitorexit_nofpu_id:
1044       save_fpu_registers = false;
1045       // fall through
1046     case monitorexit_id:
1047       {
1048         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
1049         OopMap* map = save_live_registers(sasm, save_fpu_registers);
1050 
1051         // Called with store_parameter and not C abi
1052 
1053         f.load_argument(0, r0); // r0,: lock address
1054 
1055         // note: really a leaf routine but must setup last java sp
1056         //       => use call_RT for now (speed can be improved by
1057         //       doing last java sp setup manually)
1058         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), r0);
1059 
1060         oop_maps = new OopMapSet();
1061         oop_maps->add_gc_map(call_offset, map);
1062         restore_live_registers(sasm, save_fpu_registers);
1063       }
1064       break;
1065 
1066     case deoptimize_id:
1067       {
1068         StubFrame f(sasm, "deoptimize", dont_gc_arguments);
1069         OopMap* oop_map = save_live_registers(sasm);
1070         f.load_argument(0, c_rarg1);
1071         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), c_rarg1);
1072 
1073         oop_maps = new OopMapSet();
1074         oop_maps->add_gc_map(call_offset, oop_map);
1075         restore_live_registers(sasm);
1076         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1077         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1078         __ leave();
1079         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1080       }
1081       break;
1082 
1083     case throw_range_check_failed_id:
1084       { StubFrame f(sasm, "range_check_failed", dont_gc_arguments);
1085         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
1086       }
1087       break;
1088 
1089     case unwind_exception_id:
1090       { __ set_info("unwind_exception", dont_gc_arguments);
1091         // note: no stubframe since we are about to leave the current
1092         //       activation and we are calling a leaf VM function only.
1093         generate_unwind_exception(sasm);
1094       }
1095       break;
1096 
1097     case access_field_patching_id:
1098       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments);
1099         // we should set up register map
1100         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
1101       }
1102       break;
1103 
1104     case load_klass_patching_id:
1105       { StubFrame f(sasm, "load_klass_patching", dont_gc_arguments);
1106         // we should set up register map
1107         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
1108       }
1109       break;
1110 
1111     case load_mirror_patching_id:
1112       { StubFrame f(sasm, "load_mirror_patching", dont_gc_arguments);
1113         // we should set up register map
1114         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
1115       }
1116       break;
1117 
1118     case load_appendix_patching_id:
1119       { StubFrame f(sasm, "load_appendix_patching", dont_gc_arguments);
1120         // we should set up register map
1121         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
1122       }
1123       break;
1124 
1125     case handle_exception_nofpu_id:
1126     case handle_exception_id:
1127       { StubFrame f(sasm, "handle_exception", dont_gc_arguments);
1128         oop_maps = generate_handle_exception(id, sasm);
1129       }
1130       break;
1131 
1132     case handle_exception_from_callee_id:
1133       { StubFrame f(sasm, "handle_exception_from_callee", dont_gc_arguments);
1134         oop_maps = generate_handle_exception(id, sasm);
1135       }
1136       break;
1137 
1138     case throw_index_exception_id:
1139       { StubFrame f(sasm, "index_range_check_failed", dont_gc_arguments);
1140         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
1141       }
1142       break;
1143 
1144     case throw_array_store_exception_id:
1145       { StubFrame f(sasm, "throw_array_store_exception", dont_gc_arguments);
1146         // tos + 0: link
1147         //     + 1: return address
1148         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
1149       }
1150       break;
1151 
1152 #if INCLUDE_ALL_GCS
1153 
1154     case g1_pre_barrier_slow_id:
1155       {
1156         StubFrame f(sasm, "g1_pre_barrier", dont_gc_arguments);
1157         // arg0 : previous value of memory
1158 
1159         BarrierSet* bs = Universe::heap()->barrier_set();
1160         if (bs->kind() != BarrierSet::G1SATBCTLogging) {
1161           __ mov(r0, (int)id);
1162           __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), r0);
1163           __ should_not_reach_here();
1164           break;
1165         }
1166 
1167         const Register pre_val = r0;
1168         const Register thread = rthread;
1169         const Register tmp = rscratch1;
1170 
1171         Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1172                                              SATBMarkQueue::byte_offset_of_active()));
1173 
1174         Address queue_index(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1175                                              SATBMarkQueue::byte_offset_of_index()));
1176         Address buffer(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1177                                         SATBMarkQueue::byte_offset_of_buf()));
1178 
1179         Label done;
1180         Label runtime;
1181 
1182         // Is marking still active?
1183         if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
1184           __ ldrw(tmp, in_progress);
1185         } else {
1186           assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
1187           __ ldrb(tmp, in_progress);
1188         }
1189         __ cbzw(tmp, done);
1190 
1191         // Can we store original value in the thread's buffer?
1192         __ ldr(tmp, queue_index);
1193         __ cbz(tmp, runtime);
1194 
1195         __ sub(tmp, tmp, wordSize);
1196         __ str(tmp, queue_index);
1197         __ ldr(rscratch2, buffer);
1198         __ add(tmp, tmp, rscratch2);
1199         f.load_argument(0, rscratch2);
1200         __ str(rscratch2, Address(tmp, 0));
1201         __ b(done);
1202 
1203         __ bind(runtime);
1204         __ push_call_clobbered_registers();
1205         f.load_argument(0, pre_val);
1206         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), pre_val, thread);
1207         __ pop_call_clobbered_registers();
1208         __ bind(done);
1209       }
1210       break;
1211     case g1_post_barrier_slow_id:
1212       {
1213         StubFrame f(sasm, "g1_post_barrier", dont_gc_arguments);
1214 
1215         // arg0: store_address
1216         Address store_addr(rfp, 2*BytesPerWord);
1217 
1218         BarrierSet* bs = Universe::heap()->barrier_set();
1219         CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1220         assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1221 
1222         Label done;
1223         Label runtime;
1224 
1225         // At this point we know new_value is non-NULL and the new_value crosses regions.
1226         // Must check to see if card is already dirty
1227 
1228         const Register thread = rthread;
1229 
1230         Address queue_index(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1231                                              DirtyCardQueue::byte_offset_of_index()));
1232         Address buffer(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1233                                         DirtyCardQueue::byte_offset_of_buf()));
1234 
1235         const Register card_offset = rscratch2;
1236         // LR is free here, so we can use it to hold the byte_map_base.
1237         const Register byte_map_base = lr;
1238 
1239         assert_different_registers(card_offset, byte_map_base, rscratch1);
1240 
1241         f.load_argument(0, card_offset);
1242         __ lsr(card_offset, card_offset, CardTableModRefBS::card_shift);
1243         __ load_byte_map_base(byte_map_base);
1244         __ ldrb(rscratch1, Address(byte_map_base, card_offset));
1245         __ cmpw(rscratch1, (int)G1SATBCardTableModRefBS::g1_young_card_val());
1246         __ br(Assembler::EQ, done);
1247 
1248         assert((int)CardTableModRefBS::dirty_card_val() == 0, "must be 0");
1249 
1250         __ membar(Assembler::StoreLoad);
1251         __ ldrb(rscratch1, Address(byte_map_base, card_offset));
1252         __ cbzw(rscratch1, done);
1253 
1254         // storing region crossing non-NULL, card is clean.
1255         // dirty card and log.
1256         __ strb(zr, Address(byte_map_base, card_offset));
1257 
1258         // Convert card offset into an address in card_addr
1259         Register card_addr = card_offset;
1260         __ add(card_addr, byte_map_base, card_addr);
1261 
1262         __ ldr(rscratch1, queue_index);
1263         __ cbz(rscratch1, runtime);
1264         __ sub(rscratch1, rscratch1, wordSize);
1265         __ str(rscratch1, queue_index);
1266 
1267         // Reuse LR to hold buffer_addr
1268         const Register buffer_addr = lr;
1269 
1270         __ ldr(buffer_addr, buffer);
1271         __ str(card_addr, Address(buffer_addr, rscratch1));
1272         __ b(done);
1273 
1274         __ bind(runtime);
1275         __ push_call_clobbered_registers();
1276         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), card_addr, thread);
1277         __ pop_call_clobbered_registers();
1278         __ bind(done);
1279 
1280       }
1281       break;
1282 #endif
1283 
1284     case predicate_failed_trap_id:
1285       {
1286         StubFrame f(sasm, "predicate_failed_trap", dont_gc_arguments);
1287 
1288         OopMap* map = save_live_registers(sasm);
1289 
1290         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
1291         oop_maps = new OopMapSet();
1292         oop_maps->add_gc_map(call_offset, map);
1293         restore_live_registers(sasm);
1294         __ leave();
1295         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1296         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1297 
1298         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1299       }
1300       break;
1301 
1302 
1303     default:
1304       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments);
1305         __ mov(r0, (int)id);
1306         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), r0);
1307         __ should_not_reach_here();
1308       }
1309       break;
1310     }
1311   }
1312   return oop_maps;
1313 }
1314 
1315 #undef __
1316 
1317 const char *Runtime1::pd_name_for_address(address entry) { Unimplemented(); return 0; }