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