1 /* 2 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved. 4 * Copyright (c) 2020, 2023, 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 "asm/macroAssembler.hpp" 28 #include "asm/macroAssembler.inline.hpp" 29 #include "code/compiledIC.hpp" 30 #include "code/debugInfoRec.hpp" 31 #include "code/vtableStubs.hpp" 32 #include "compiler/oopMap.hpp" 33 #include "gc/shared/barrierSetAssembler.hpp" 34 #include "interpreter/interp_masm.hpp" 35 #include "interpreter/interpreter.hpp" 36 #include "logging/log.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "nativeInst_riscv.hpp" 39 #include "oops/klass.inline.hpp" 40 #include "oops/method.inline.hpp" 41 #include "prims/methodHandles.hpp" 42 #include "runtime/continuation.hpp" 43 #include "runtime/continuationEntry.inline.hpp" 44 #include "runtime/globals.hpp" 45 #include "runtime/jniHandles.hpp" 46 #include "runtime/safepointMechanism.hpp" 47 #include "runtime/sharedRuntime.hpp" 48 #include "runtime/signature.hpp" 49 #include "runtime/stubRoutines.hpp" 50 #include "runtime/timerTrace.hpp" 51 #include "runtime/vframeArray.hpp" 52 #include "utilities/align.hpp" 53 #include "utilities/formatBuffer.hpp" 54 #include "vmreg_riscv.inline.hpp" 55 #ifdef COMPILER1 56 #include "c1/c1_Runtime1.hpp" 57 #endif 58 #ifdef COMPILER2 59 #include "adfiles/ad_riscv.hpp" 60 #include "opto/runtime.hpp" 61 #endif 62 #if INCLUDE_JVMCI 63 #include "jvmci/jvmciJavaClasses.hpp" 64 #endif 65 66 #define __ masm-> 67 68 #ifdef PRODUCT 69 #define BLOCK_COMMENT(str) /* nothing */ 70 #else 71 #define BLOCK_COMMENT(str) __ block_comment(str) 72 #endif 73 74 const int StackAlignmentInSlots = StackAlignmentInBytes / VMRegImpl::stack_slot_size; 75 76 class RegisterSaver { 77 const bool _save_vectors; 78 public: 79 RegisterSaver(bool save_vectors) : _save_vectors(UseRVV && save_vectors) {} 80 ~RegisterSaver() {} 81 OopMap* save_live_registers(MacroAssembler* masm, int additional_frame_words, int* total_frame_words); 82 void restore_live_registers(MacroAssembler* masm); 83 84 // Offsets into the register save area 85 // Used by deoptimization when it is managing result register 86 // values on its own 87 // gregs:28, float_register:32; except: x1(ra) & x2(sp) & gp(x3) & tp(x4) 88 // |---v0---|<---SP 89 // |---v1---|save vectors only in generate_handler_blob 90 // |-- .. --| 91 // |---v31--|----- 92 // |---f0---| 93 // |---f1---| 94 // | .. | 95 // |---f31--| 96 // |---reserved slot for stack alignment---| 97 // |---x5---| 98 // | x6 | 99 // |---.. --| 100 // |---x31--| 101 // |---fp---| 102 // |---ra---| 103 int v0_offset_in_bytes(void) { return 0; } 104 int f0_offset_in_bytes(void) { 105 int f0_offset = 0; 106 #ifdef COMPILER2 107 if (_save_vectors) { 108 f0_offset += Matcher::scalable_vector_reg_size(T_INT) * VectorRegister::number_of_registers * 109 BytesPerInt; 110 } 111 #endif 112 return f0_offset; 113 } 114 int reserved_slot_offset_in_bytes(void) { 115 return f0_offset_in_bytes() + 116 FloatRegister::max_slots_per_register * 117 FloatRegister::number_of_registers * 118 BytesPerInt; 119 } 120 121 int reg_offset_in_bytes(Register r) { 122 assert (r->encoding() > 4, "ra, sp, gp and tp not saved"); 123 return reserved_slot_offset_in_bytes() + (r->encoding() - 4 /* x1, x2, x3, x4 */) * wordSize; 124 } 125 126 int freg_offset_in_bytes(FloatRegister f) { 127 return f0_offset_in_bytes() + f->encoding() * wordSize; 128 } 129 130 int ra_offset_in_bytes(void) { 131 return reserved_slot_offset_in_bytes() + 132 (Register::number_of_registers - 3) * 133 Register::max_slots_per_register * 134 BytesPerInt; 135 } 136 }; 137 138 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm, int additional_frame_words, int* total_frame_words) { 139 int vector_size_in_bytes = 0; 140 int vector_size_in_slots = 0; 141 #ifdef COMPILER2 142 if (_save_vectors) { 143 vector_size_in_bytes += Matcher::scalable_vector_reg_size(T_BYTE); 144 vector_size_in_slots += Matcher::scalable_vector_reg_size(T_INT); 145 } 146 #endif 147 148 int frame_size_in_bytes = align_up(additional_frame_words * wordSize + ra_offset_in_bytes() + wordSize, 16); 149 // OopMap frame size is in compiler stack slots (jint's) not bytes or words 150 int frame_size_in_slots = frame_size_in_bytes / BytesPerInt; 151 // The caller will allocate additional_frame_words 152 int additional_frame_slots = additional_frame_words * wordSize / BytesPerInt; 153 // CodeBlob frame size is in words. 154 int frame_size_in_words = frame_size_in_bytes / wordSize; 155 *total_frame_words = frame_size_in_words; 156 157 // Save Integer, Float and Vector registers. 158 __ enter(); 159 __ push_CPU_state(_save_vectors, vector_size_in_bytes); 160 161 // Set an oopmap for the call site. This oopmap will map all 162 // oop-registers and debug-info registers as callee-saved. This 163 // will allow deoptimization at this safepoint to find all possible 164 // debug-info recordings, as well as let GC find all oops. 165 166 OopMapSet *oop_maps = new OopMapSet(); 167 OopMap* oop_map = new OopMap(frame_size_in_slots, 0); 168 assert_cond(oop_maps != nullptr && oop_map != nullptr); 169 170 int sp_offset_in_slots = 0; 171 int step_in_slots = 0; 172 if (_save_vectors) { 173 step_in_slots = vector_size_in_slots; 174 for (int i = 0; i < VectorRegister::number_of_registers; i++, sp_offset_in_slots += step_in_slots) { 175 VectorRegister r = as_VectorRegister(i); 176 oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset_in_slots), r->as_VMReg()); 177 } 178 } 179 180 step_in_slots = FloatRegister::max_slots_per_register; 181 for (int i = 0; i < FloatRegister::number_of_registers; i++, sp_offset_in_slots += step_in_slots) { 182 FloatRegister r = as_FloatRegister(i); 183 oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset_in_slots), r->as_VMReg()); 184 } 185 186 step_in_slots = Register::max_slots_per_register; 187 // skip the slot reserved for alignment, see MacroAssembler::push_reg; 188 // also skip x5 ~ x6 on the stack because they are caller-saved registers. 189 sp_offset_in_slots += Register::max_slots_per_register * 3; 190 // besides, we ignore x0 ~ x4 because push_CPU_state won't push them on the stack. 191 for (int i = 7; i < Register::number_of_registers; i++, sp_offset_in_slots += step_in_slots) { 192 Register r = as_Register(i); 193 if (r != xthread) { 194 oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset_in_slots + additional_frame_slots), r->as_VMReg()); 195 } 196 } 197 198 return oop_map; 199 } 200 201 void RegisterSaver::restore_live_registers(MacroAssembler* masm) { 202 #ifdef COMPILER2 203 __ pop_CPU_state(_save_vectors, Matcher::scalable_vector_reg_size(T_BYTE)); 204 #else 205 #if !INCLUDE_JVMCI 206 assert(!_save_vectors, "vectors are generated only by C2 and JVMCI"); 207 #endif 208 __ pop_CPU_state(_save_vectors); 209 #endif 210 __ leave(); 211 } 212 213 // Is vector's size (in bytes) bigger than a size saved by default? 214 // riscv does not ovlerlay the floating-point registers on vector registers like aarch64. 215 bool SharedRuntime::is_wide_vector(int size) { 216 return UseRVV; 217 } 218 219 // --------------------------------------------------------------------------- 220 // Read the array of BasicTypes from a signature, and compute where the 221 // arguments should go. Values in the VMRegPair regs array refer to 4-byte 222 // quantities. Values less than VMRegImpl::stack0 are registers, those above 223 // refer to 4-byte stack slots. All stack slots are based off of the stack pointer 224 // as framesizes are fixed. 225 // VMRegImpl::stack0 refers to the first slot 0(sp). 226 // and VMRegImpl::stack0+1 refers to the memory word 4-byes higher. 227 // Register up to Register::number_of_registers) are the 64-bit 228 // integer registers. 229 230 // Note: the INPUTS in sig_bt are in units of Java argument words, 231 // which are 64-bit. The OUTPUTS are in 32-bit units. 232 233 // The Java calling convention is a "shifted" version of the C ABI. 234 // By skipping the first C ABI register we can call non-static jni 235 // methods with small numbers of arguments without having to shuffle 236 // the arguments at all. Since we control the java ABI we ought to at 237 // least get some advantage out of it. 238 239 int SharedRuntime::java_calling_convention(const BasicType *sig_bt, 240 VMRegPair *regs, 241 int total_args_passed) { 242 // Create the mapping between argument positions and 243 // registers. 244 static const Register INT_ArgReg[Argument::n_int_register_parameters_j] = { 245 j_rarg0, j_rarg1, j_rarg2, j_rarg3, 246 j_rarg4, j_rarg5, j_rarg6, j_rarg7 247 }; 248 static const FloatRegister FP_ArgReg[Argument::n_float_register_parameters_j] = { 249 j_farg0, j_farg1, j_farg2, j_farg3, 250 j_farg4, j_farg5, j_farg6, j_farg7 251 }; 252 253 uint int_args = 0; 254 uint fp_args = 0; 255 uint stk_args = 0; 256 257 for (int i = 0; i < total_args_passed; i++) { 258 switch (sig_bt[i]) { 259 case T_BOOLEAN: // fall through 260 case T_CHAR: // fall through 261 case T_BYTE: // fall through 262 case T_SHORT: // fall through 263 case T_INT: 264 if (int_args < Argument::n_int_register_parameters_j) { 265 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg()); 266 } else { 267 stk_args = align_up(stk_args, 2); 268 regs[i].set1(VMRegImpl::stack2reg(stk_args)); 269 stk_args += 1; 270 } 271 break; 272 case T_VOID: 273 // halves of T_LONG or T_DOUBLE 274 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half"); 275 regs[i].set_bad(); 276 break; 277 case T_LONG: // fall through 278 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half"); 279 case T_OBJECT: // fall through 280 case T_ARRAY: // fall through 281 case T_ADDRESS: 282 if (int_args < Argument::n_int_register_parameters_j) { 283 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg()); 284 } else { 285 stk_args = align_up(stk_args, 2); 286 regs[i].set2(VMRegImpl::stack2reg(stk_args)); 287 stk_args += 2; 288 } 289 break; 290 case T_FLOAT: 291 if (fp_args < Argument::n_float_register_parameters_j) { 292 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg()); 293 } else { 294 stk_args = align_up(stk_args, 2); 295 regs[i].set1(VMRegImpl::stack2reg(stk_args)); 296 stk_args += 1; 297 } 298 break; 299 case T_DOUBLE: 300 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half"); 301 if (fp_args < Argument::n_float_register_parameters_j) { 302 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg()); 303 } else { 304 stk_args = align_up(stk_args, 2); 305 regs[i].set2(VMRegImpl::stack2reg(stk_args)); 306 stk_args += 2; 307 } 308 break; 309 default: 310 ShouldNotReachHere(); 311 } 312 } 313 314 return stk_args; 315 } 316 317 // Patch the callers callsite with entry to compiled code if it exists. 318 static void patch_callers_callsite(MacroAssembler *masm) { 319 Label L; 320 __ ld(t0, Address(xmethod, in_bytes(Method::code_offset()))); 321 __ beqz(t0, L); 322 323 __ enter(); 324 __ push_CPU_state(); 325 326 // VM needs caller's callsite 327 // VM needs target method 328 // This needs to be a long call since we will relocate this adapter to 329 // the codeBuffer and it may not reach 330 331 #ifndef PRODUCT 332 assert(frame::arg_reg_save_area_bytes == 0, "not expecting frame reg save area"); 333 #endif 334 335 __ mv(c_rarg0, xmethod); 336 __ mv(c_rarg1, ra); 337 __ rt_call(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)); 338 339 __ pop_CPU_state(); 340 // restore sp 341 __ leave(); 342 __ bind(L); 343 } 344 345 static void gen_c2i_adapter(MacroAssembler *masm, 346 int total_args_passed, 347 int comp_args_on_stack, 348 const BasicType *sig_bt, 349 const VMRegPair *regs, 350 Label& skip_fixup) { 351 // Before we get into the guts of the C2I adapter, see if we should be here 352 // at all. We've come from compiled code and are attempting to jump to the 353 // interpreter, which means the caller made a static call to get here 354 // (vcalls always get a compiled target if there is one). Check for a 355 // compiled target. If there is one, we need to patch the caller's call. 356 patch_callers_callsite(masm); 357 358 __ bind(skip_fixup); 359 360 int words_pushed = 0; 361 362 // Since all args are passed on the stack, total_args_passed * 363 // Interpreter::stackElementSize is the space we need. 364 365 int extraspace = total_args_passed * Interpreter::stackElementSize; 366 367 __ mv(x19_sender_sp, sp); 368 369 // stack is aligned, keep it that way 370 extraspace = align_up(extraspace, 2 * wordSize); 371 372 if (extraspace) { 373 __ sub(sp, sp, extraspace); 374 } 375 376 // Now write the args into the outgoing interpreter space 377 for (int i = 0; i < total_args_passed; i++) { 378 if (sig_bt[i] == T_VOID) { 379 assert(i > 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "missing half"); 380 continue; 381 } 382 383 // offset to start parameters 384 int st_off = (total_args_passed - i - 1) * Interpreter::stackElementSize; 385 int next_off = st_off - Interpreter::stackElementSize; 386 387 // Say 4 args: 388 // i st_off 389 // 0 32 T_LONG 390 // 1 24 T_VOID 391 // 2 16 T_OBJECT 392 // 3 8 T_BOOL 393 // - 0 return address 394 // 395 // However to make thing extra confusing. Because we can fit a Java long/double in 396 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter 397 // leaves one slot empty and only stores to a single slot. In this case the 398 // slot that is occupied is the T_VOID slot. See I said it was confusing. 399 400 VMReg r_1 = regs[i].first(); 401 VMReg r_2 = regs[i].second(); 402 if (!r_1->is_valid()) { 403 assert(!r_2->is_valid(), ""); 404 continue; 405 } 406 if (r_1->is_stack()) { 407 // memory to memory use t0 408 int ld_off = (r_1->reg2stack() * VMRegImpl::stack_slot_size 409 + extraspace 410 + words_pushed * wordSize); 411 if (!r_2->is_valid()) { 412 __ lwu(t0, Address(sp, ld_off)); 413 __ sd(t0, Address(sp, st_off), /*temp register*/esp); 414 } else { 415 __ ld(t0, Address(sp, ld_off), /*temp register*/esp); 416 417 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG 418 // T_DOUBLE and T_LONG use two slots in the interpreter 419 if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) { 420 // ld_off == LSW, ld_off+wordSize == MSW 421 // st_off == MSW, next_off == LSW 422 __ sd(t0, Address(sp, next_off), /*temp register*/esp); 423 #ifdef ASSERT 424 // Overwrite the unused slot with known junk 425 __ mv(t0, 0xdeadffffdeadaaaaul); 426 __ sd(t0, Address(sp, st_off), /*temp register*/esp); 427 #endif /* ASSERT */ 428 } else { 429 __ sd(t0, Address(sp, st_off), /*temp register*/esp); 430 } 431 } 432 } else if (r_1->is_Register()) { 433 Register r = r_1->as_Register(); 434 if (!r_2->is_valid()) { 435 // must be only an int (or less ) so move only 32bits to slot 436 __ sd(r, Address(sp, st_off)); 437 } else { 438 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG 439 // T_DOUBLE and T_LONG use two slots in the interpreter 440 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) { 441 // long/double in gpr 442 #ifdef ASSERT 443 // Overwrite the unused slot with known junk 444 __ mv(t0, 0xdeadffffdeadaaabul); 445 __ sd(t0, Address(sp, st_off), /*temp register*/esp); 446 #endif /* ASSERT */ 447 __ sd(r, Address(sp, next_off)); 448 } else { 449 __ sd(r, Address(sp, st_off)); 450 } 451 } 452 } else { 453 assert(r_1->is_FloatRegister(), ""); 454 if (!r_2->is_valid()) { 455 // only a float use just part of the slot 456 __ fsw(r_1->as_FloatRegister(), Address(sp, st_off)); 457 } else { 458 #ifdef ASSERT 459 // Overwrite the unused slot with known junk 460 __ mv(t0, 0xdeadffffdeadaaacul); 461 __ sd(t0, Address(sp, st_off), /*temp register*/esp); 462 #endif /* ASSERT */ 463 __ fsd(r_1->as_FloatRegister(), Address(sp, next_off)); 464 } 465 } 466 } 467 468 __ mv(esp, sp); // Interp expects args on caller's expression stack 469 470 __ ld(t1, Address(xmethod, in_bytes(Method::interpreter_entry_offset()))); 471 __ jr(t1); 472 } 473 474 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm, 475 int total_args_passed, 476 int comp_args_on_stack, 477 const BasicType *sig_bt, 478 const VMRegPair *regs) { 479 // Note: x19_sender_sp contains the senderSP on entry. We must 480 // preserve it since we may do a i2c -> c2i transition if we lose a 481 // race where compiled code goes non-entrant while we get args 482 // ready. 483 484 // Cut-out for having no stack args. 485 int comp_words_on_stack = align_up(comp_args_on_stack * VMRegImpl::stack_slot_size, wordSize) >> LogBytesPerWord; 486 if (comp_args_on_stack != 0) { 487 __ sub(t0, sp, comp_words_on_stack * wordSize); 488 __ andi(sp, t0, -16); 489 } 490 491 // Will jump to the compiled code just as if compiled code was doing it. 492 // Pre-load the register-jump target early, to schedule it better. 493 __ ld(t1, Address(xmethod, in_bytes(Method::from_compiled_offset()))); 494 495 #if INCLUDE_JVMCI 496 if (EnableJVMCI) { 497 // check if this call should be routed towards a specific entry point 498 __ ld(t0, Address(xthread, in_bytes(JavaThread::jvmci_alternate_call_target_offset()))); 499 Label no_alternative_target; 500 __ beqz(t0, no_alternative_target); 501 __ mv(t1, t0); 502 __ sd(zr, Address(xthread, in_bytes(JavaThread::jvmci_alternate_call_target_offset()))); 503 __ bind(no_alternative_target); 504 } 505 #endif // INCLUDE_JVMCI 506 507 // Now generate the shuffle code. 508 for (int i = 0; i < total_args_passed; i++) { 509 if (sig_bt[i] == T_VOID) { 510 assert(i > 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "missing half"); 511 continue; 512 } 513 514 // Pick up 0, 1 or 2 words from SP+offset. 515 516 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(), 517 "scrambled load targets?"); 518 // Load in argument order going down. 519 int ld_off = (total_args_passed - i - 1) * Interpreter::stackElementSize; 520 // Point to interpreter value (vs. tag) 521 int next_off = ld_off - Interpreter::stackElementSize; 522 523 VMReg r_1 = regs[i].first(); 524 VMReg r_2 = regs[i].second(); 525 if (!r_1->is_valid()) { 526 assert(!r_2->is_valid(), ""); 527 continue; 528 } 529 if (r_1->is_stack()) { 530 // Convert stack slot to an SP offset (+ wordSize to account for return address ) 531 int st_off = regs[i].first()->reg2stack() * VMRegImpl::stack_slot_size; 532 if (!r_2->is_valid()) { 533 __ lw(t0, Address(esp, ld_off)); 534 __ sd(t0, Address(sp, st_off), /*temp register*/t2); 535 } else { 536 // 537 // We are using two optoregs. This can be either T_OBJECT, 538 // T_ADDRESS, T_LONG, or T_DOUBLE the interpreter allocates 539 // two slots but only uses one for thr T_LONG or T_DOUBLE case 540 // So we must adjust where to pick up the data to match the 541 // interpreter. 542 // 543 // Interpreter local[n] == MSW, local[n+1] == LSW however locals 544 // are accessed as negative so LSW is at LOW address 545 546 // ld_off is MSW so get LSW 547 const int offset = (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) ? 548 next_off : ld_off; 549 __ ld(t0, Address(esp, offset)); 550 // st_off is LSW (i.e. reg.first()) 551 __ sd(t0, Address(sp, st_off), /*temp register*/t2); 552 } 553 } else if (r_1->is_Register()) { // Register argument 554 Register r = r_1->as_Register(); 555 if (r_2->is_valid()) { 556 // 557 // We are using two VMRegs. This can be either T_OBJECT, 558 // T_ADDRESS, T_LONG, or T_DOUBLE the interpreter allocates 559 // two slots but only uses one for thr T_LONG or T_DOUBLE case 560 // So we must adjust where to pick up the data to match the 561 // interpreter. 562 563 const int offset = (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) ? 564 next_off : ld_off; 565 566 // this can be a misaligned move 567 __ ld(r, Address(esp, offset)); 568 } else { 569 // sign extend and use a full word? 570 __ lw(r, Address(esp, ld_off)); 571 } 572 } else { 573 if (!r_2->is_valid()) { 574 __ flw(r_1->as_FloatRegister(), Address(esp, ld_off)); 575 } else { 576 __ fld(r_1->as_FloatRegister(), Address(esp, next_off)); 577 } 578 } 579 } 580 581 __ push_cont_fastpath(xthread); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about 582 583 // 6243940 We might end up in handle_wrong_method if 584 // the callee is deoptimized as we race thru here. If that 585 // happens we don't want to take a safepoint because the 586 // caller frame will look interpreted and arguments are now 587 // "compiled" so it is much better to make this transition 588 // invisible to the stack walking code. Unfortunately if 589 // we try and find the callee by normal means a safepoint 590 // is possible. So we stash the desired callee in the thread 591 // and the vm will find there should this case occur. 592 593 __ sd(xmethod, Address(xthread, JavaThread::callee_target_offset())); 594 595 __ jr(t1); 596 } 597 598 // --------------------------------------------------------------- 599 600 void SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm, 601 int total_args_passed, 602 int comp_args_on_stack, 603 const BasicType *sig_bt, 604 const VMRegPair *regs, 605 address entry_address[AdapterBlob::ENTRY_COUNT]) { 606 entry_address[AdapterBlob::I2C] = __ pc(); 607 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs); 608 609 entry_address[AdapterBlob::C2I_Unverified] = __ pc(); 610 Label skip_fixup; 611 612 const Register receiver = j_rarg0; 613 const Register data = t0; 614 615 // ------------------------------------------------------------------------- 616 // Generate a C2I adapter. On entry we know xmethod holds the Method* during calls 617 // to the interpreter. The args start out packed in the compiled layout. They 618 // need to be unpacked into the interpreter layout. This will almost always 619 // require some stack space. We grow the current (compiled) stack, then repack 620 // the args. We finally end in a jump to the generic interpreter entry point. 621 // On exit from the interpreter, the interpreter will restore our SP (lest the 622 // compiled code, which relies solely on SP and not FP, get sick). 623 624 { 625 __ block_comment("c2i_unverified_entry {"); 626 627 __ ic_check(); 628 __ ld(xmethod, Address(data, CompiledICData::speculated_method_offset())); 629 630 __ ld(t0, Address(xmethod, in_bytes(Method::code_offset()))); 631 __ beqz(t0, skip_fixup); 632 __ far_jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub())); 633 __ block_comment("} c2i_unverified_entry"); 634 } 635 636 entry_address[AdapterBlob::C2I] = __ pc(); 637 638 // Class initialization barrier for static methods 639 entry_address[AdapterBlob::C2I_No_Clinit_Check] = nullptr; 640 if (VM_Version::supports_fast_class_init_checks()) { 641 Label L_skip_barrier; 642 643 { // Bypass the barrier for non-static methods 644 __ load_unsigned_short(t0, Address(xmethod, Method::access_flags_offset())); 645 __ test_bit(t1, t0, exact_log2(JVM_ACC_STATIC)); 646 __ beqz(t1, L_skip_barrier); // non-static 647 } 648 649 __ load_method_holder(t1, xmethod); 650 __ clinit_barrier(t1, t0, &L_skip_barrier); 651 __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); 652 653 __ bind(L_skip_barrier); 654 entry_address[AdapterBlob::C2I_No_Clinit_Check] = __ pc(); 655 } 656 657 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 658 bs->c2i_entry_barrier(masm); 659 660 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup); 661 return; 662 } 663 664 int SharedRuntime::vector_calling_convention(VMRegPair *regs, 665 uint num_bits, 666 uint total_args_passed) { 667 assert(total_args_passed <= Argument::n_vector_register_parameters_c, "unsupported"); 668 assert(num_bits >= 64 && num_bits <= 2048 && is_power_of_2(num_bits), "unsupported"); 669 670 // check more info at https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc 671 static const VectorRegister VEC_ArgReg[Argument::n_vector_register_parameters_c] = { 672 v8, v9, v10, v11, v12, v13, v14, v15, 673 v16, v17, v18, v19, v20, v21, v22, v23 674 }; 675 676 const int next_reg_val = 3; 677 for (uint i = 0; i < total_args_passed; i++) { 678 VMReg vmreg = VEC_ArgReg[i]->as_VMReg(); 679 regs[i].set_pair(vmreg->next(next_reg_val), vmreg); 680 } 681 return 0; 682 } 683 684 int SharedRuntime::c_calling_convention(const BasicType *sig_bt, 685 VMRegPair *regs, 686 int total_args_passed) { 687 688 // We return the amount of VMRegImpl stack slots we need to reserve for all 689 // the arguments NOT counting out_preserve_stack_slots. 690 691 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = { 692 c_rarg0, c_rarg1, c_rarg2, c_rarg3, 693 c_rarg4, c_rarg5, c_rarg6, c_rarg7 694 }; 695 static const FloatRegister FP_ArgReg[Argument::n_float_register_parameters_c] = { 696 c_farg0, c_farg1, c_farg2, c_farg3, 697 c_farg4, c_farg5, c_farg6, c_farg7 698 }; 699 700 uint int_args = 0; 701 uint fp_args = 0; 702 uint stk_args = 0; // inc by 2 each time 703 704 for (int i = 0; i < total_args_passed; i++) { 705 switch (sig_bt[i]) { 706 case T_BOOLEAN: // fall through 707 case T_CHAR: // fall through 708 case T_BYTE: // fall through 709 case T_SHORT: // fall through 710 case T_INT: 711 if (int_args < Argument::n_int_register_parameters_c) { 712 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg()); 713 } else { 714 regs[i].set1(VMRegImpl::stack2reg(stk_args)); 715 stk_args += 2; 716 } 717 break; 718 case T_LONG: // fall through 719 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half"); 720 case T_OBJECT: // fall through 721 case T_ARRAY: // fall through 722 case T_ADDRESS: // fall through 723 case T_METADATA: 724 if (int_args < Argument::n_int_register_parameters_c) { 725 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg()); 726 } else { 727 regs[i].set2(VMRegImpl::stack2reg(stk_args)); 728 stk_args += 2; 729 } 730 break; 731 case T_FLOAT: 732 if (fp_args < Argument::n_float_register_parameters_c) { 733 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg()); 734 } else if (int_args < Argument::n_int_register_parameters_c) { 735 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg()); 736 } else { 737 regs[i].set1(VMRegImpl::stack2reg(stk_args)); 738 stk_args += 2; 739 } 740 break; 741 case T_DOUBLE: 742 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half"); 743 if (fp_args < Argument::n_float_register_parameters_c) { 744 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg()); 745 } else if (int_args < Argument::n_int_register_parameters_c) { 746 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg()); 747 } else { 748 regs[i].set2(VMRegImpl::stack2reg(stk_args)); 749 stk_args += 2; 750 } 751 break; 752 case T_VOID: // Halves of longs and doubles 753 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half"); 754 regs[i].set_bad(); 755 break; 756 default: 757 ShouldNotReachHere(); 758 } 759 } 760 761 return stk_args; 762 } 763 764 void SharedRuntime::save_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) { 765 // We always ignore the frame_slots arg and just use the space just below frame pointer 766 // which by this time is free to use 767 switch (ret_type) { 768 case T_FLOAT: 769 __ fsw(f10, Address(fp, -3 * wordSize)); 770 break; 771 case T_DOUBLE: 772 __ fsd(f10, Address(fp, -3 * wordSize)); 773 break; 774 case T_VOID: break; 775 default: { 776 __ sd(x10, Address(fp, -3 * wordSize)); 777 } 778 } 779 } 780 781 void SharedRuntime::restore_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) { 782 // We always ignore the frame_slots arg and just use the space just below frame pointer 783 // which by this time is free to use 784 switch (ret_type) { 785 case T_FLOAT: 786 __ flw(f10, Address(fp, -3 * wordSize)); 787 break; 788 case T_DOUBLE: 789 __ fld(f10, Address(fp, -3 * wordSize)); 790 break; 791 case T_VOID: break; 792 default: { 793 __ ld(x10, Address(fp, -3 * wordSize)); 794 } 795 } 796 } 797 798 static void save_args(MacroAssembler *masm, int arg_count, int first_arg, VMRegPair *args) { 799 RegSet x; 800 for ( int i = first_arg ; i < arg_count ; i++ ) { 801 if (args[i].first()->is_Register()) { 802 x = x + args[i].first()->as_Register(); 803 } else if (args[i].first()->is_FloatRegister()) { 804 __ subi(sp, sp, 2 * wordSize); 805 __ fsd(args[i].first()->as_FloatRegister(), Address(sp, 0)); 806 } 807 } 808 __ push_reg(x, sp); 809 } 810 811 static void restore_args(MacroAssembler *masm, int arg_count, int first_arg, VMRegPair *args) { 812 RegSet x; 813 for ( int i = first_arg ; i < arg_count ; i++ ) { 814 if (args[i].first()->is_Register()) { 815 x = x + args[i].first()->as_Register(); 816 } else { 817 ; 818 } 819 } 820 __ pop_reg(x, sp); 821 for ( int i = arg_count - 1 ; i >= first_arg ; i-- ) { 822 if (args[i].first()->is_Register()) { 823 ; 824 } else if (args[i].first()->is_FloatRegister()) { 825 __ fld(args[i].first()->as_FloatRegister(), Address(sp, 0)); 826 __ addi(sp, sp, 2 * wordSize); 827 } 828 } 829 } 830 831 static void verify_oop_args(MacroAssembler* masm, 832 const methodHandle& method, 833 const BasicType* sig_bt, 834 const VMRegPair* regs) { 835 const Register temp_reg = x9; // not part of any compiled calling seq 836 if (VerifyOops) { 837 for (int i = 0; i < method->size_of_parameters(); i++) { 838 if (sig_bt[i] == T_OBJECT || 839 sig_bt[i] == T_ARRAY) { 840 VMReg r = regs[i].first(); 841 assert(r->is_valid(), "bad oop arg"); 842 if (r->is_stack()) { 843 __ ld(temp_reg, Address(sp, r->reg2stack() * VMRegImpl::stack_slot_size)); 844 __ verify_oop(temp_reg); 845 } else { 846 __ verify_oop(r->as_Register()); 847 } 848 } 849 } 850 } 851 } 852 853 // on exit, sp points to the ContinuationEntry 854 static OopMap* continuation_enter_setup(MacroAssembler* masm, int& stack_slots) { 855 assert(ContinuationEntry::size() % VMRegImpl::stack_slot_size == 0, ""); 856 assert(in_bytes(ContinuationEntry::cont_offset()) % VMRegImpl::stack_slot_size == 0, ""); 857 assert(in_bytes(ContinuationEntry::chunk_offset()) % VMRegImpl::stack_slot_size == 0, ""); 858 859 stack_slots += (int)ContinuationEntry::size() / wordSize; 860 __ sub(sp, sp, (int)ContinuationEntry::size()); // place Continuation metadata 861 862 OopMap* map = new OopMap(((int)ContinuationEntry::size() + wordSize) / VMRegImpl::stack_slot_size, 0 /* arg_slots*/); 863 864 __ ld(t0, Address(xthread, JavaThread::cont_entry_offset())); 865 __ sd(t0, Address(sp, ContinuationEntry::parent_offset())); 866 __ sd(sp, Address(xthread, JavaThread::cont_entry_offset())); 867 868 return map; 869 } 870 871 // on entry c_rarg1 points to the continuation 872 // sp points to ContinuationEntry 873 // c_rarg3 -- isVirtualThread 874 static void fill_continuation_entry(MacroAssembler* masm) { 875 #ifdef ASSERT 876 __ mv(t0, ContinuationEntry::cookie_value()); 877 __ sw(t0, Address(sp, ContinuationEntry::cookie_offset())); 878 #endif 879 880 __ sd(c_rarg1, Address(sp, ContinuationEntry::cont_offset())); 881 __ sw(c_rarg3, Address(sp, ContinuationEntry::flags_offset())); 882 __ sd(zr, Address(sp, ContinuationEntry::chunk_offset())); 883 __ sw(zr, Address(sp, ContinuationEntry::argsize_offset())); 884 __ sw(zr, Address(sp, ContinuationEntry::pin_count_offset())); 885 886 __ ld(t0, Address(xthread, JavaThread::cont_fastpath_offset())); 887 __ sd(t0, Address(sp, ContinuationEntry::parent_cont_fastpath_offset())); 888 __ ld(t0, Address(xthread, JavaThread::held_monitor_count_offset())); 889 __ sd(t0, Address(sp, ContinuationEntry::parent_held_monitor_count_offset())); 890 891 __ sd(zr, Address(xthread, JavaThread::cont_fastpath_offset())); 892 __ sd(zr, Address(xthread, JavaThread::held_monitor_count_offset())); 893 } 894 895 // on entry, sp points to the ContinuationEntry 896 // on exit, fp points to the spilled fp + 2 * wordSize in the entry frame 897 static void continuation_enter_cleanup(MacroAssembler* masm) { 898 #ifndef PRODUCT 899 Label OK; 900 __ ld(t0, Address(xthread, JavaThread::cont_entry_offset())); 901 __ beq(sp, t0, OK); 902 __ stop("incorrect sp"); 903 __ bind(OK); 904 #endif 905 906 __ ld(t0, Address(sp, ContinuationEntry::parent_cont_fastpath_offset())); 907 __ sd(t0, Address(xthread, JavaThread::cont_fastpath_offset())); 908 909 if (CheckJNICalls) { 910 // Check if this is a virtual thread continuation 911 Label L_skip_vthread_code; 912 __ lwu(t0, Address(sp, ContinuationEntry::flags_offset())); 913 __ beqz(t0, L_skip_vthread_code); 914 915 // If the held monitor count is > 0 and this vthread is terminating then 916 // it failed to release a JNI monitor. So we issue the same log message 917 // that JavaThread::exit does. 918 __ ld(t0, Address(xthread, JavaThread::jni_monitor_count_offset())); 919 __ beqz(t0, L_skip_vthread_code); 920 921 // Save return value potentially containing the exception oop in callee-saved x9 922 __ mv(x9, x10); 923 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::log_jni_monitor_still_held)); 924 // Restore potential return value 925 __ mv(x10, x9); 926 927 // For vthreads we have to explicitly zero the JNI monitor count of the carrier 928 // on termination. The held count is implicitly zeroed below when we restore from 929 // the parent held count (which has to be zero). 930 __ sd(zr, Address(xthread, JavaThread::jni_monitor_count_offset())); 931 932 __ bind(L_skip_vthread_code); 933 } 934 #ifdef ASSERT 935 else { 936 // Check if this is a virtual thread continuation 937 Label L_skip_vthread_code; 938 __ lwu(t0, Address(sp, ContinuationEntry::flags_offset())); 939 __ beqz(t0, L_skip_vthread_code); 940 941 // See comment just above. If not checking JNI calls the JNI count is only 942 // needed for assertion checking. 943 __ sd(zr, Address(xthread, JavaThread::jni_monitor_count_offset())); 944 945 __ bind(L_skip_vthread_code); 946 } 947 #endif 948 949 __ ld(t0, Address(sp, ContinuationEntry::parent_held_monitor_count_offset())); 950 __ sd(t0, Address(xthread, JavaThread::held_monitor_count_offset())); 951 952 __ ld(t0, Address(sp, ContinuationEntry::parent_offset())); 953 __ sd(t0, Address(xthread, JavaThread::cont_entry_offset())); 954 __ add(fp, sp, (int)ContinuationEntry::size() + 2 * wordSize /* 2 extra words to match up with leave() */); 955 } 956 957 // enterSpecial(Continuation c, boolean isContinue, boolean isVirtualThread) 958 // On entry: c_rarg1 -- the continuation object 959 // c_rarg2 -- isContinue 960 // c_rarg3 -- isVirtualThread 961 static void gen_continuation_enter(MacroAssembler* masm, 962 const methodHandle& method, 963 const BasicType* sig_bt, 964 const VMRegPair* regs, 965 int& exception_offset, 966 OopMapSet*oop_maps, 967 int& frame_complete, 968 int& stack_slots, 969 int& interpreted_entry_offset, 970 int& compiled_entry_offset) { 971 // verify_oop_args(masm, method, sig_bt, regs); 972 Address resolve(SharedRuntime::get_resolve_static_call_stub(), relocInfo::static_call_type); 973 974 address start = __ pc(); 975 976 Label call_thaw, exit; 977 978 // i2i entry used at interp_only_mode only 979 interpreted_entry_offset = __ pc() - start; 980 { 981 #ifdef ASSERT 982 Label is_interp_only; 983 __ lw(t0, Address(xthread, JavaThread::interp_only_mode_offset())); 984 __ bnez(t0, is_interp_only); 985 __ stop("enterSpecial interpreter entry called when not in interp_only_mode"); 986 __ bind(is_interp_only); 987 #endif 988 989 // Read interpreter arguments into registers (this is an ad-hoc i2c adapter) 990 __ ld(c_rarg1, Address(esp, Interpreter::stackElementSize * 2)); 991 __ ld(c_rarg2, Address(esp, Interpreter::stackElementSize * 1)); 992 __ ld(c_rarg3, Address(esp, Interpreter::stackElementSize * 0)); 993 __ push_cont_fastpath(xthread); 994 995 __ enter(); 996 stack_slots = 2; // will be adjusted in setup 997 OopMap* map = continuation_enter_setup(masm, stack_slots); 998 // The frame is complete here, but we only record it for the compiled entry, so the frame would appear unsafe, 999 // but that's okay because at the very worst we'll miss an async sample, but we're in interp_only_mode anyway. 1000 1001 fill_continuation_entry(masm); 1002 1003 __ bnez(c_rarg2, call_thaw); 1004 1005 address call_pc; 1006 { 1007 Assembler::IncompressibleScope scope(masm); 1008 // Make sure the call is patchable 1009 __ align(NativeInstruction::instruction_size); 1010 1011 call_pc = __ reloc_call(resolve); 1012 if (call_pc == nullptr) { 1013 fatal("CodeCache is full at gen_continuation_enter"); 1014 } 1015 1016 oop_maps->add_gc_map(__ pc() - start, map); 1017 __ post_call_nop(); 1018 } 1019 __ j(exit); 1020 1021 address stub = CompiledDirectCall::emit_to_interp_stub(masm, call_pc); 1022 if (stub == nullptr) { 1023 fatal("CodeCache is full at gen_continuation_enter"); 1024 } 1025 } 1026 1027 // compiled entry 1028 __ align(CodeEntryAlignment); 1029 compiled_entry_offset = __ pc() - start; 1030 1031 __ enter(); 1032 stack_slots = 2; // will be adjusted in setup 1033 OopMap* map = continuation_enter_setup(masm, stack_slots); 1034 frame_complete = __ pc() - start; 1035 1036 fill_continuation_entry(masm); 1037 1038 __ bnez(c_rarg2, call_thaw); 1039 1040 address call_pc; 1041 { 1042 Assembler::IncompressibleScope scope(masm); 1043 // Make sure the call is patchable 1044 __ align(NativeInstruction::instruction_size); 1045 1046 call_pc = __ reloc_call(resolve); 1047 if (call_pc == nullptr) { 1048 fatal("CodeCache is full at gen_continuation_enter"); 1049 } 1050 1051 oop_maps->add_gc_map(__ pc() - start, map); 1052 __ post_call_nop(); 1053 } 1054 1055 __ j(exit); 1056 1057 __ bind(call_thaw); 1058 1059 // Post call nops must be natural aligned due to cmodx rules. 1060 { 1061 Assembler::IncompressibleScope scope(masm); 1062 __ align(NativeInstruction::instruction_size); 1063 1064 ContinuationEntry::_thaw_call_pc_offset = __ pc() - start; 1065 __ rt_call(CAST_FROM_FN_PTR(address, StubRoutines::cont_thaw())); 1066 oop_maps->add_gc_map(__ pc() - start, map->deep_copy()); 1067 ContinuationEntry::_return_pc_offset = __ pc() - start; 1068 __ post_call_nop(); 1069 } 1070 1071 __ bind(exit); 1072 ContinuationEntry::_cleanup_offset = __ pc() - start; 1073 continuation_enter_cleanup(masm); 1074 __ leave(); 1075 __ ret(); 1076 1077 // exception handling 1078 exception_offset = __ pc() - start; 1079 { 1080 __ mv(x9, x10); // save return value contaning the exception oop in callee-saved x9 1081 1082 continuation_enter_cleanup(masm); 1083 1084 __ ld(c_rarg1, Address(fp, -1 * wordSize)); // return address 1085 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), xthread, c_rarg1); 1086 1087 // see OptoRuntime::generate_exception_blob: x10 -- exception oop, x13 -- exception pc 1088 1089 __ mv(x11, x10); // the exception handler 1090 __ mv(x10, x9); // restore return value contaning the exception oop 1091 __ verify_oop(x10); 1092 1093 __ leave(); 1094 __ mv(x13, ra); 1095 __ jr(x11); // the exception handler 1096 } 1097 1098 address stub = CompiledDirectCall::emit_to_interp_stub(masm, call_pc); 1099 if (stub == nullptr) { 1100 fatal("CodeCache is full at gen_continuation_enter"); 1101 } 1102 } 1103 1104 static void gen_continuation_yield(MacroAssembler* masm, 1105 const methodHandle& method, 1106 const BasicType* sig_bt, 1107 const VMRegPair* regs, 1108 OopMapSet* oop_maps, 1109 int& frame_complete, 1110 int& stack_slots, 1111 int& compiled_entry_offset) { 1112 enum layout { 1113 fp_off, 1114 fp_off2, 1115 return_off, 1116 return_off2, 1117 framesize // inclusive of return address 1118 }; 1119 // assert(is_even(framesize/2), "sp not 16-byte aligned"); 1120 1121 stack_slots = framesize / VMRegImpl::slots_per_word; 1122 assert(stack_slots == 2, "recheck layout"); 1123 1124 address start = __ pc(); 1125 1126 compiled_entry_offset = __ pc() - start; 1127 __ enter(); 1128 1129 __ mv(c_rarg1, sp); 1130 1131 // Post call nops must be natural aligned due to cmodx rules. 1132 __ align(NativeInstruction::instruction_size); 1133 1134 frame_complete = __ pc() - start; 1135 address the_pc = __ pc(); 1136 1137 { 1138 Assembler::IncompressibleScope scope(masm); 1139 __ post_call_nop(); // this must be exactly after the pc value that is pushed into the frame info, we use this nop for fast CodeBlob lookup 1140 } 1141 1142 __ mv(c_rarg0, xthread); 1143 __ set_last_Java_frame(sp, fp, the_pc, t0); 1144 __ call_VM_leaf(Continuation::freeze_entry(), 2); 1145 __ reset_last_Java_frame(true); 1146 1147 Label pinned; 1148 1149 __ bnez(x10, pinned); 1150 1151 // We've succeeded, set sp to the ContinuationEntry 1152 __ ld(sp, Address(xthread, JavaThread::cont_entry_offset())); 1153 continuation_enter_cleanup(masm); 1154 1155 __ bind(pinned); // pinned -- return to caller 1156 1157 // handle pending exception thrown by freeze 1158 __ ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1159 Label ok; 1160 __ beqz(t0, ok); 1161 __ leave(); 1162 __ j(RuntimeAddress(StubRoutines::forward_exception_entry())); 1163 __ bind(ok); 1164 1165 __ leave(); 1166 __ ret(); 1167 1168 OopMap* map = new OopMap(framesize, 1); 1169 oop_maps->add_gc_map(the_pc - start, map); 1170 } 1171 1172 void SharedRuntime::continuation_enter_cleanup(MacroAssembler* masm) { 1173 ::continuation_enter_cleanup(masm); 1174 } 1175 1176 static void gen_special_dispatch(MacroAssembler* masm, 1177 const methodHandle& method, 1178 const BasicType* sig_bt, 1179 const VMRegPair* regs) { 1180 verify_oop_args(masm, method, sig_bt, regs); 1181 vmIntrinsics::ID iid = method->intrinsic_id(); 1182 1183 // Now write the args into the outgoing interpreter space 1184 bool has_receiver = false; 1185 Register receiver_reg = noreg; 1186 int member_arg_pos = -1; 1187 Register member_reg = noreg; 1188 int ref_kind = MethodHandles::signature_polymorphic_intrinsic_ref_kind(iid); 1189 if (ref_kind != 0) { 1190 member_arg_pos = method->size_of_parameters() - 1; // trailing MemberName argument 1191 member_reg = x9; // known to be free at this point 1192 has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind); 1193 } else if (iid == vmIntrinsics::_invokeBasic) { 1194 has_receiver = true; 1195 } else if (iid == vmIntrinsics::_linkToNative) { 1196 member_arg_pos = method->size_of_parameters() - 1; // trailing NativeEntryPoint argument 1197 member_reg = x9; // known to be free at this point 1198 } else { 1199 fatal("unexpected intrinsic id %d", vmIntrinsics::as_int(iid)); 1200 } 1201 1202 if (member_reg != noreg) { 1203 // Load the member_arg into register, if necessary. 1204 SharedRuntime::check_member_name_argument_is_last_argument(method, sig_bt, regs); 1205 VMReg r = regs[member_arg_pos].first(); 1206 if (r->is_stack()) { 1207 __ ld(member_reg, Address(sp, r->reg2stack() * VMRegImpl::stack_slot_size)); 1208 } else { 1209 // no data motion is needed 1210 member_reg = r->as_Register(); 1211 } 1212 } 1213 1214 if (has_receiver) { 1215 // Make sure the receiver is loaded into a register. 1216 assert(method->size_of_parameters() > 0, "oob"); 1217 assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object"); 1218 VMReg r = regs[0].first(); 1219 assert(r->is_valid(), "bad receiver arg"); 1220 if (r->is_stack()) { 1221 // Porting note: This assumes that compiled calling conventions always 1222 // pass the receiver oop in a register. If this is not true on some 1223 // platform, pick a temp and load the receiver from stack. 1224 fatal("receiver always in a register"); 1225 receiver_reg = x12; // known to be free at this point 1226 __ ld(receiver_reg, Address(sp, r->reg2stack() * VMRegImpl::stack_slot_size)); 1227 } else { 1228 // no data motion is needed 1229 receiver_reg = r->as_Register(); 1230 } 1231 } 1232 1233 // Figure out which address we are really jumping to: 1234 MethodHandles::generate_method_handle_dispatch(masm, iid, 1235 receiver_reg, member_reg, /*for_compiler_entry:*/ true); 1236 } 1237 1238 // --------------------------------------------------------------------------- 1239 // Generate a native wrapper for a given method. The method takes arguments 1240 // in the Java compiled code convention, marshals them to the native 1241 // convention (handlizes oops, etc), transitions to native, makes the call, 1242 // returns to java state (possibly blocking), unhandlizes any result and 1243 // returns. 1244 // 1245 // Critical native functions are a shorthand for the use of 1246 // GetPrimtiveArrayCritical and disallow the use of any other JNI 1247 // functions. The wrapper is expected to unpack the arguments before 1248 // passing them to the callee and perform checks before and after the 1249 // native call to ensure that they GCLocker 1250 // lock_critical/unlock_critical semantics are followed. Some other 1251 // parts of JNI setup are skipped like the tear down of the JNI handle 1252 // block and the check for pending exceptions it's impossible for them 1253 // to be thrown. 1254 // 1255 // They are roughly structured like this: 1256 // if (GCLocker::needs_gc()) SharedRuntime::block_for_jni_critical() 1257 // tranistion to thread_in_native 1258 // unpack array arguments and call native entry point 1259 // check for safepoint in progress 1260 // check if any thread suspend flags are set 1261 // call into JVM and possible unlock the JNI critical 1262 // if a GC was suppressed while in the critical native. 1263 // transition back to thread_in_Java 1264 // return to caller 1265 // 1266 nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm, 1267 const methodHandle& method, 1268 int compile_id, 1269 BasicType* in_sig_bt, 1270 VMRegPair* in_regs, 1271 BasicType ret_type) { 1272 if (method->is_continuation_native_intrinsic()) { 1273 int exception_offset = -1; 1274 OopMapSet* oop_maps = new OopMapSet(); 1275 int frame_complete = -1; 1276 int stack_slots = -1; 1277 int interpreted_entry_offset = -1; 1278 int vep_offset = -1; 1279 if (method->is_continuation_enter_intrinsic()) { 1280 gen_continuation_enter(masm, 1281 method, 1282 in_sig_bt, 1283 in_regs, 1284 exception_offset, 1285 oop_maps, 1286 frame_complete, 1287 stack_slots, 1288 interpreted_entry_offset, 1289 vep_offset); 1290 } else if (method->is_continuation_yield_intrinsic()) { 1291 gen_continuation_yield(masm, 1292 method, 1293 in_sig_bt, 1294 in_regs, 1295 oop_maps, 1296 frame_complete, 1297 stack_slots, 1298 vep_offset); 1299 } else { 1300 guarantee(false, "Unknown Continuation native intrinsic"); 1301 } 1302 1303 #ifdef ASSERT 1304 if (method->is_continuation_enter_intrinsic()) { 1305 assert(interpreted_entry_offset != -1, "Must be set"); 1306 assert(exception_offset != -1, "Must be set"); 1307 } else { 1308 assert(interpreted_entry_offset == -1, "Must be unset"); 1309 assert(exception_offset == -1, "Must be unset"); 1310 } 1311 assert(frame_complete != -1, "Must be set"); 1312 assert(stack_slots != -1, "Must be set"); 1313 assert(vep_offset != -1, "Must be set"); 1314 #endif 1315 1316 __ flush(); 1317 nmethod* nm = nmethod::new_native_nmethod(method, 1318 compile_id, 1319 masm->code(), 1320 vep_offset, 1321 frame_complete, 1322 stack_slots, 1323 in_ByteSize(-1), 1324 in_ByteSize(-1), 1325 oop_maps, 1326 exception_offset); 1327 if (nm == nullptr) return nm; 1328 if (method->is_continuation_enter_intrinsic()) { 1329 ContinuationEntry::set_enter_code(nm, interpreted_entry_offset); 1330 } else if (method->is_continuation_yield_intrinsic()) { 1331 ContinuationEntry::set_yield_code(nm); 1332 } else { 1333 guarantee(false, "Unknown Continuation native intrinsic"); 1334 } 1335 return nm; 1336 } 1337 1338 if (method->is_method_handle_intrinsic()) { 1339 vmIntrinsics::ID iid = method->intrinsic_id(); 1340 intptr_t start = (intptr_t)__ pc(); 1341 int vep_offset = ((intptr_t)__ pc()) - start; 1342 1343 // First instruction must be a nop as it may need to be patched on deoptimisation 1344 { 1345 Assembler::IncompressibleScope scope(masm); // keep the nop as 4 bytes for patching. 1346 MacroAssembler::assert_alignment(__ pc()); 1347 __ nop(); // 4 bytes 1348 } 1349 gen_special_dispatch(masm, 1350 method, 1351 in_sig_bt, 1352 in_regs); 1353 int frame_complete = ((intptr_t)__ pc()) - start; // not complete, period 1354 __ flush(); 1355 int stack_slots = SharedRuntime::out_preserve_stack_slots(); // no out slots at all, actually 1356 return nmethod::new_native_nmethod(method, 1357 compile_id, 1358 masm->code(), 1359 vep_offset, 1360 frame_complete, 1361 stack_slots / VMRegImpl::slots_per_word, 1362 in_ByteSize(-1), 1363 in_ByteSize(-1), 1364 (OopMapSet*)nullptr); 1365 } 1366 address native_func = method->native_function(); 1367 assert(native_func != nullptr, "must have function"); 1368 1369 // An OopMap for lock (and class if static) 1370 OopMapSet *oop_maps = new OopMapSet(); 1371 assert_cond(oop_maps != nullptr); 1372 intptr_t start = (intptr_t)__ pc(); 1373 1374 // We have received a description of where all the java arg are located 1375 // on entry to the wrapper. We need to convert these args to where 1376 // the jni function will expect them. To figure out where they go 1377 // we convert the java signature to a C signature by inserting 1378 // the hidden arguments as arg[0] and possibly arg[1] (static method) 1379 1380 const int total_in_args = method->size_of_parameters(); 1381 int total_c_args = total_in_args + (method->is_static() ? 2 : 1); 1382 1383 BasicType* out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args); 1384 VMRegPair* out_regs = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args); 1385 1386 int argc = 0; 1387 out_sig_bt[argc++] = T_ADDRESS; 1388 if (method->is_static()) { 1389 out_sig_bt[argc++] = T_OBJECT; 1390 } 1391 1392 for (int i = 0; i < total_in_args ; i++) { 1393 out_sig_bt[argc++] = in_sig_bt[i]; 1394 } 1395 1396 // Now figure out where the args must be stored and how much stack space 1397 // they require. 1398 int out_arg_slots = c_calling_convention(out_sig_bt, out_regs, total_c_args); 1399 1400 // Compute framesize for the wrapper. We need to handlize all oops in 1401 // incoming registers 1402 1403 // Calculate the total number of stack slots we will need. 1404 1405 // First count the abi requirement plus all of the outgoing args 1406 int stack_slots = SharedRuntime::out_preserve_stack_slots() + out_arg_slots; 1407 1408 // Now the space for the inbound oop handle area 1409 int total_save_slots = 8 * VMRegImpl::slots_per_word; // 8 arguments passed in registers 1410 1411 int oop_handle_offset = stack_slots; 1412 stack_slots += total_save_slots; 1413 1414 // Now any space we need for handlizing a klass if static method 1415 1416 int klass_slot_offset = 0; 1417 int klass_offset = -1; 1418 int lock_slot_offset = 0; 1419 bool is_static = false; 1420 1421 if (method->is_static()) { 1422 klass_slot_offset = stack_slots; 1423 stack_slots += VMRegImpl::slots_per_word; 1424 klass_offset = klass_slot_offset * VMRegImpl::stack_slot_size; 1425 is_static = true; 1426 } 1427 1428 // Plus a lock if needed 1429 1430 if (method->is_synchronized()) { 1431 lock_slot_offset = stack_slots; 1432 stack_slots += VMRegImpl::slots_per_word; 1433 } 1434 1435 // Now a place (+2) to save return values or temp during shuffling 1436 // + 4 for return address (which we own) and saved fp 1437 stack_slots += 6; 1438 1439 // Ok The space we have allocated will look like: 1440 // 1441 // 1442 // FP-> | | 1443 // | 2 slots (ra) | 1444 // | 2 slots (fp) | 1445 // |---------------------| 1446 // | 2 slots for moves | 1447 // |---------------------| 1448 // | lock box (if sync) | 1449 // |---------------------| <- lock_slot_offset 1450 // | klass (if static) | 1451 // |---------------------| <- klass_slot_offset 1452 // | oopHandle area | 1453 // |---------------------| <- oop_handle_offset (8 java arg registers) 1454 // | outbound memory | 1455 // | based arguments | 1456 // | | 1457 // |---------------------| 1458 // | | 1459 // SP-> | out_preserved_slots | 1460 // 1461 // 1462 1463 1464 // Now compute actual number of stack words we need rounding to make 1465 // stack properly aligned. 1466 stack_slots = align_up(stack_slots, StackAlignmentInSlots); 1467 1468 int stack_size = stack_slots * VMRegImpl::stack_slot_size; 1469 1470 // First thing make an ic check to see if we should even be here 1471 1472 // We are free to use all registers as temps without saving them and 1473 // restoring them except fp. fp is the only callee save register 1474 // as far as the interpreter and the compiler(s) are concerned. 1475 1476 const Register receiver = j_rarg0; 1477 1478 __ verify_oop(receiver); 1479 assert_different_registers(receiver, t0, t1); 1480 1481 __ ic_check(); 1482 1483 int vep_offset = ((intptr_t)__ pc()) - start; 1484 1485 // If we have to make this method not-entrant we'll overwrite its 1486 // first instruction with a jump. 1487 { 1488 Assembler::IncompressibleScope scope(masm); // keep the nop as 4 bytes for patching. 1489 MacroAssembler::assert_alignment(__ pc()); 1490 __ nop(); // 4 bytes 1491 } 1492 1493 if (VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) { 1494 Label L_skip_barrier; 1495 __ mov_metadata(t1, method->method_holder()); // InstanceKlass* 1496 __ clinit_barrier(t1, t0, &L_skip_barrier); 1497 __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); 1498 1499 __ bind(L_skip_barrier); 1500 } 1501 1502 // Generate stack overflow check 1503 __ bang_stack_with_offset(checked_cast<int>(StackOverflow::stack_shadow_zone_size())); 1504 1505 // Generate a new frame for the wrapper. 1506 __ enter(); 1507 // -2 because return address is already present and so is saved fp 1508 __ sub(sp, sp, stack_size - 2 * wordSize); 1509 1510 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 1511 assert_cond(bs != nullptr); 1512 bs->nmethod_entry_barrier(masm, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */); 1513 1514 // Frame is now completed as far as size and linkage. 1515 int frame_complete = ((intptr_t)__ pc()) - start; 1516 1517 // We use x18 as the oop handle for the receiver/klass 1518 // It is callee save so it survives the call to native 1519 1520 const Register oop_handle_reg = x18; 1521 1522 // 1523 // We immediately shuffle the arguments so that any vm call we have to 1524 // make from here on out (sync slow path, jvmti, etc.) we will have 1525 // captured the oops from our caller and have a valid oopMap for 1526 // them. 1527 1528 // ----------------- 1529 // The Grand Shuffle 1530 1531 // The Java calling convention is either equal (linux) or denser (win64) than the 1532 // c calling convention. However the because of the jni_env argument the c calling 1533 // convention always has at least one more (and two for static) arguments than Java. 1534 // Therefore if we move the args from java -> c backwards then we will never have 1535 // a register->register conflict and we don't have to build a dependency graph 1536 // and figure out how to break any cycles. 1537 // 1538 1539 // Record esp-based slot for receiver on stack for non-static methods 1540 int receiver_offset = -1; 1541 1542 // This is a trick. We double the stack slots so we can claim 1543 // the oops in the caller's frame. Since we are sure to have 1544 // more args than the caller doubling is enough to make 1545 // sure we can capture all the incoming oop args from the 1546 // caller. 1547 // 1548 OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/); 1549 assert_cond(map != nullptr); 1550 1551 int float_args = 0; 1552 int int_args = 0; 1553 1554 #ifdef ASSERT 1555 bool reg_destroyed[Register::number_of_registers]; 1556 bool freg_destroyed[FloatRegister::number_of_registers]; 1557 for ( int r = 0 ; r < Register::number_of_registers ; r++ ) { 1558 reg_destroyed[r] = false; 1559 } 1560 for ( int f = 0 ; f < FloatRegister::number_of_registers ; f++ ) { 1561 freg_destroyed[f] = false; 1562 } 1563 1564 #endif /* ASSERT */ 1565 1566 // For JNI natives the incoming and outgoing registers are offset upwards. 1567 GrowableArray<int> arg_order(2 * total_in_args); 1568 1569 for (int i = total_in_args - 1, c_arg = total_c_args - 1; i >= 0; i--, c_arg--) { 1570 arg_order.push(i); 1571 arg_order.push(c_arg); 1572 } 1573 1574 for (int ai = 0; ai < arg_order.length(); ai += 2) { 1575 int i = arg_order.at(ai); 1576 int c_arg = arg_order.at(ai + 1); 1577 __ block_comment(err_msg("mv %d -> %d", i, c_arg)); 1578 assert(c_arg != -1 && i != -1, "wrong order"); 1579 #ifdef ASSERT 1580 if (in_regs[i].first()->is_Register()) { 1581 assert(!reg_destroyed[in_regs[i].first()->as_Register()->encoding()], "destroyed reg!"); 1582 } else if (in_regs[i].first()->is_FloatRegister()) { 1583 assert(!freg_destroyed[in_regs[i].first()->as_FloatRegister()->encoding()], "destroyed reg!"); 1584 } 1585 if (out_regs[c_arg].first()->is_Register()) { 1586 reg_destroyed[out_regs[c_arg].first()->as_Register()->encoding()] = true; 1587 } else if (out_regs[c_arg].first()->is_FloatRegister()) { 1588 freg_destroyed[out_regs[c_arg].first()->as_FloatRegister()->encoding()] = true; 1589 } 1590 #endif /* ASSERT */ 1591 switch (in_sig_bt[i]) { 1592 case T_ARRAY: 1593 case T_OBJECT: 1594 __ object_move(map, oop_handle_offset, stack_slots, in_regs[i], out_regs[c_arg], 1595 ((i == 0) && (!is_static)), 1596 &receiver_offset); 1597 int_args++; 1598 break; 1599 case T_VOID: 1600 break; 1601 1602 case T_FLOAT: 1603 __ float_move(in_regs[i], out_regs[c_arg]); 1604 float_args++; 1605 break; 1606 1607 case T_DOUBLE: 1608 assert( i + 1 < total_in_args && 1609 in_sig_bt[i + 1] == T_VOID && 1610 out_sig_bt[c_arg + 1] == T_VOID, "bad arg list"); 1611 __ double_move(in_regs[i], out_regs[c_arg]); 1612 float_args++; 1613 break; 1614 1615 case T_LONG : 1616 __ long_move(in_regs[i], out_regs[c_arg]); 1617 int_args++; 1618 break; 1619 1620 case T_ADDRESS: 1621 assert(false, "found T_ADDRESS in java args"); 1622 break; 1623 1624 default: 1625 __ move32_64(in_regs[i], out_regs[c_arg]); 1626 int_args++; 1627 } 1628 } 1629 1630 // point c_arg at the first arg that is already loaded in case we 1631 // need to spill before we call out 1632 int c_arg = total_c_args - total_in_args; 1633 1634 // Pre-load a static method's oop into c_rarg1. 1635 if (method->is_static()) { 1636 1637 // load oop into a register 1638 __ movoop(c_rarg1, 1639 JNIHandles::make_local(method->method_holder()->java_mirror())); 1640 1641 // Now handlize the static class mirror it's known not-null. 1642 __ sd(c_rarg1, Address(sp, klass_offset)); 1643 map->set_oop(VMRegImpl::stack2reg(klass_slot_offset)); 1644 1645 // Now get the handle 1646 __ la(c_rarg1, Address(sp, klass_offset)); 1647 // and protect the arg if we must spill 1648 c_arg--; 1649 } 1650 1651 // Change state to native (we save the return address in the thread, since it might not 1652 // be pushed on the stack when we do a stack traversal). It is enough that the pc() 1653 // points into the right code segment. It does not have to be the correct return pc. 1654 // We use the same pc/oopMap repeatedly when we call out. 1655 1656 Label native_return; 1657 if (method->is_object_wait0()) { 1658 // For convenience we use the pc we want to resume to in case of preemption on Object.wait. 1659 __ set_last_Java_frame(sp, noreg, native_return, t0); 1660 } else { 1661 intptr_t the_pc = (intptr_t) __ pc(); 1662 oop_maps->add_gc_map(the_pc - start, map); 1663 1664 __ set_last_Java_frame(sp, noreg, __ pc(), t0); 1665 } 1666 1667 Label dtrace_method_entry, dtrace_method_entry_done; 1668 if (DTraceMethodProbes) { 1669 __ j(dtrace_method_entry); 1670 __ bind(dtrace_method_entry_done); 1671 } 1672 1673 // RedefineClasses() tracing support for obsolete method entry 1674 if (log_is_enabled(Trace, redefine, class, obsolete)) { 1675 // protect the args we've loaded 1676 save_args(masm, total_c_args, c_arg, out_regs); 1677 __ mov_metadata(c_rarg1, method()); 1678 __ call_VM_leaf( 1679 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry), 1680 xthread, c_rarg1); 1681 restore_args(masm, total_c_args, c_arg, out_regs); 1682 } 1683 1684 // Lock a synchronized method 1685 1686 // Register definitions used by locking and unlocking 1687 1688 const Register swap_reg = x10; 1689 const Register obj_reg = x9; // Will contain the oop 1690 const Register lock_reg = x30; // Address of compiler lock object (BasicLock) 1691 const Register old_hdr = x30; // value of old header at unlock time 1692 const Register lock_tmp = x31; // Temporary used by lightweight_lock/unlock 1693 const Register tmp = ra; 1694 1695 Label slow_path_lock; 1696 Label lock_done; 1697 1698 if (method->is_synchronized()) { 1699 // Get the handle (the 2nd argument) 1700 __ mv(oop_handle_reg, c_rarg1); 1701 1702 // Get address of the box 1703 1704 __ la(lock_reg, Address(sp, lock_slot_offset * VMRegImpl::stack_slot_size)); 1705 1706 // Load the oop from the handle 1707 __ ld(obj_reg, Address(oop_handle_reg, 0)); 1708 1709 __ lightweight_lock(lock_reg, obj_reg, swap_reg, tmp, lock_tmp, slow_path_lock); 1710 1711 // Slow path will re-enter here 1712 __ bind(lock_done); 1713 } 1714 1715 1716 // Finally just about ready to make the JNI call 1717 1718 // get JNIEnv* which is first argument to native 1719 __ la(c_rarg0, Address(xthread, in_bytes(JavaThread::jni_environment_offset()))); 1720 1721 // Now set thread in native 1722 __ la(t1, Address(xthread, JavaThread::thread_state_offset())); 1723 __ mv(t0, _thread_in_native); 1724 __ membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore); 1725 __ sw(t0, Address(t1)); 1726 1727 // Clobbers t1 1728 __ rt_call(native_func); 1729 1730 // Verify or restore cpu control state after JNI call 1731 __ restore_cpu_control_state_after_jni(t0); 1732 1733 // Unpack native results. 1734 if (ret_type != T_OBJECT && ret_type != T_ARRAY) { 1735 __ cast_primitive_type(ret_type, x10); 1736 } 1737 1738 Label safepoint_in_progress, safepoint_in_progress_done; 1739 1740 // Switch thread to "native transition" state before reading the synchronization state. 1741 // This additional state is necessary because reading and testing the synchronization 1742 // state is not atomic w.r.t. GC, as this scenario demonstrates: 1743 // Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted. 1744 // VM thread changes sync state to synchronizing and suspends threads for GC. 1745 // Thread A is resumed to finish this native method, but doesn't block here since it 1746 // didn't see any synchronization is progress, and escapes. 1747 __ mv(t0, _thread_in_native_trans); 1748 1749 __ sw(t0, Address(xthread, JavaThread::thread_state_offset())); 1750 1751 // Force this write out before the read below 1752 if (!UseSystemMemoryBarrier) { 1753 __ membar(MacroAssembler::AnyAny); 1754 } 1755 1756 // check for safepoint operation in progress and/or pending suspend requests 1757 { 1758 __ safepoint_poll(safepoint_in_progress, true /* at_return */, false /* in_nmethod */); 1759 __ lwu(t0, Address(xthread, JavaThread::suspend_flags_offset())); 1760 __ bnez(t0, safepoint_in_progress); 1761 __ bind(safepoint_in_progress_done); 1762 } 1763 1764 // change thread state 1765 __ la(t1, Address(xthread, JavaThread::thread_state_offset())); 1766 __ mv(t0, _thread_in_Java); 1767 __ membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore); 1768 __ sw(t0, Address(t1)); 1769 1770 if (method->is_object_wait0()) { 1771 // Check preemption for Object.wait() 1772 __ ld(t1, Address(xthread, JavaThread::preempt_alternate_return_offset())); 1773 __ beqz(t1, native_return); 1774 __ sd(zr, Address(xthread, JavaThread::preempt_alternate_return_offset())); 1775 __ jr(t1); 1776 __ bind(native_return); 1777 1778 intptr_t the_pc = (intptr_t) __ pc(); 1779 oop_maps->add_gc_map(the_pc - start, map); 1780 } 1781 1782 Label reguard; 1783 Label reguard_done; 1784 __ lbu(t0, Address(xthread, JavaThread::stack_guard_state_offset())); 1785 __ mv(t1, StackOverflow::stack_guard_yellow_reserved_disabled); 1786 __ beq(t0, t1, reguard); 1787 __ bind(reguard_done); 1788 1789 // native result if any is live 1790 1791 // Unlock 1792 Label unlock_done; 1793 Label slow_path_unlock; 1794 if (method->is_synchronized()) { 1795 1796 // Get locked oop from the handle we passed to jni 1797 __ ld(obj_reg, Address(oop_handle_reg, 0)); 1798 1799 // Must save x10 if if it is live now because cmpxchg must use it 1800 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) { 1801 save_native_result(masm, ret_type, stack_slots); 1802 } 1803 1804 __ lightweight_unlock(obj_reg, old_hdr, swap_reg, lock_tmp, slow_path_unlock); 1805 1806 // slow path re-enters here 1807 __ bind(unlock_done); 1808 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) { 1809 restore_native_result(masm, ret_type, stack_slots); 1810 } 1811 } 1812 1813 Label dtrace_method_exit, dtrace_method_exit_done; 1814 if (DTraceMethodProbes) { 1815 __ j(dtrace_method_exit); 1816 __ bind(dtrace_method_exit_done); 1817 } 1818 1819 __ reset_last_Java_frame(false); 1820 1821 // Unbox oop result, e.g. JNIHandles::resolve result. 1822 if (is_reference_type(ret_type)) { 1823 __ resolve_jobject(x10, x11, x12); 1824 } 1825 1826 if (CheckJNICalls) { 1827 // clear_pending_jni_exception_check 1828 __ sd(zr, Address(xthread, JavaThread::pending_jni_exception_check_fn_offset())); 1829 } 1830 1831 // reset handle block 1832 __ ld(x12, Address(xthread, JavaThread::active_handles_offset())); 1833 __ sd(zr, Address(x12, JNIHandleBlock::top_offset())); 1834 1835 __ leave(); 1836 1837 #if INCLUDE_JFR 1838 // We need to do a poll test after unwind in case the sampler 1839 // managed to sample the native frame after returning to Java. 1840 Label L_return; 1841 __ ld(t0, Address(xthread, JavaThread::polling_word_offset())); 1842 address poll_test_pc = __ pc(); 1843 __ relocate(relocInfo::poll_return_type); 1844 __ test_bit(t0, t0, log2i_exact(SafepointMechanism::poll_bit())); 1845 __ beqz(t0, L_return); 1846 assert(SharedRuntime::polling_page_return_handler_blob() != nullptr, 1847 "polling page return stub not created yet"); 1848 address stub = SharedRuntime::polling_page_return_handler_blob()->entry_point(); 1849 __ la(t0, InternalAddress(poll_test_pc)); 1850 __ sd(t0, Address(xthread, JavaThread::saved_exception_pc_offset())); 1851 __ far_jump(RuntimeAddress(stub)); 1852 __ bind(L_return); 1853 #endif // INCLUDE_JFR 1854 1855 // Any exception pending? 1856 Label exception_pending; 1857 __ ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1858 __ bnez(t0, exception_pending); 1859 1860 // We're done 1861 __ ret(); 1862 1863 // Unexpected paths are out of line and go here 1864 1865 // forward the exception 1866 __ bind(exception_pending); 1867 1868 // and forward the exception 1869 __ far_jump(RuntimeAddress(StubRoutines::forward_exception_entry())); 1870 1871 // Slow path locking & unlocking 1872 if (method->is_synchronized()) { 1873 1874 __ block_comment("Slow path lock {"); 1875 __ bind(slow_path_lock); 1876 1877 // has last_Java_frame setup. No exceptions so do vanilla call not call_VM 1878 // args are (oop obj, BasicLock* lock, JavaThread* thread) 1879 1880 // protect the args we've loaded 1881 save_args(masm, total_c_args, c_arg, out_regs); 1882 1883 __ mv(c_rarg0, obj_reg); 1884 __ mv(c_rarg1, lock_reg); 1885 __ mv(c_rarg2, xthread); 1886 1887 // Not a leaf but we have last_Java_frame setup as we want. 1888 // We don't want to unmount in case of contention since that would complicate preserving 1889 // the arguments that had already been marshalled into the native convention. So we force 1890 // the freeze slow path to find this native wrapper frame (see recurse_freeze_native_frame()) 1891 // and pin the vthread. Otherwise the fast path won't find it since we don't walk the stack. 1892 __ push_cont_fastpath(); 1893 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C), 3); 1894 __ pop_cont_fastpath(); 1895 restore_args(masm, total_c_args, c_arg, out_regs); 1896 1897 #ifdef ASSERT 1898 { Label L; 1899 __ ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1900 __ beqz(t0, L); 1901 __ stop("no pending exception allowed on exit from monitorenter"); 1902 __ bind(L); 1903 } 1904 #endif 1905 __ j(lock_done); 1906 1907 __ block_comment("} Slow path lock"); 1908 1909 __ block_comment("Slow path unlock {"); 1910 __ bind(slow_path_unlock); 1911 1912 if (ret_type == T_FLOAT || ret_type == T_DOUBLE) { 1913 save_native_result(masm, ret_type, stack_slots); 1914 } 1915 1916 __ mv(c_rarg2, xthread); 1917 __ la(c_rarg1, Address(sp, lock_slot_offset * VMRegImpl::stack_slot_size)); 1918 __ mv(c_rarg0, obj_reg); 1919 1920 // Save pending exception around call to VM (which contains an EXCEPTION_MARK) 1921 // NOTE that obj_reg == x9 currently 1922 __ ld(x9, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1923 __ sd(zr, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1924 1925 __ rt_call(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C)); 1926 1927 #ifdef ASSERT 1928 { 1929 Label L; 1930 __ ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1931 __ beqz(t0, L); 1932 __ stop("no pending exception allowed on exit complete_monitor_unlocking_C"); 1933 __ bind(L); 1934 } 1935 #endif /* ASSERT */ 1936 1937 __ sd(x9, Address(xthread, in_bytes(Thread::pending_exception_offset()))); 1938 1939 if (ret_type == T_FLOAT || ret_type == T_DOUBLE) { 1940 restore_native_result(masm, ret_type, stack_slots); 1941 } 1942 __ j(unlock_done); 1943 1944 __ block_comment("} Slow path unlock"); 1945 1946 } // synchronized 1947 1948 // SLOW PATH Reguard the stack if needed 1949 1950 __ bind(reguard); 1951 save_native_result(masm, ret_type, stack_slots); 1952 __ rt_call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)); 1953 restore_native_result(masm, ret_type, stack_slots); 1954 // and continue 1955 __ j(reguard_done); 1956 1957 // SLOW PATH safepoint 1958 { 1959 __ block_comment("safepoint {"); 1960 __ bind(safepoint_in_progress); 1961 1962 // Don't use call_VM as it will see a possible pending exception and forward it 1963 // and never return here preventing us from clearing _last_native_pc down below. 1964 // 1965 save_native_result(masm, ret_type, stack_slots); 1966 __ mv(c_rarg0, xthread); 1967 #ifndef PRODUCT 1968 assert(frame::arg_reg_save_area_bytes == 0, "not expecting frame reg save area"); 1969 #endif 1970 __ rt_call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)); 1971 1972 // Restore any method result value 1973 restore_native_result(masm, ret_type, stack_slots); 1974 1975 __ j(safepoint_in_progress_done); 1976 __ block_comment("} safepoint"); 1977 } 1978 1979 // SLOW PATH dtrace support 1980 if (DTraceMethodProbes) { 1981 { 1982 __ block_comment("dtrace entry {"); 1983 __ bind(dtrace_method_entry); 1984 1985 // We have all of the arguments setup at this point. We must not touch any register 1986 // argument registers at this point (what if we save/restore them there are no oop? 1987 1988 save_args(masm, total_c_args, c_arg, out_regs); 1989 __ mov_metadata(c_rarg1, method()); 1990 __ call_VM_leaf( 1991 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), 1992 xthread, c_rarg1); 1993 restore_args(masm, total_c_args, c_arg, out_regs); 1994 __ j(dtrace_method_entry_done); 1995 __ block_comment("} dtrace entry"); 1996 } 1997 1998 { 1999 __ block_comment("dtrace exit {"); 2000 __ bind(dtrace_method_exit); 2001 save_native_result(masm, ret_type, stack_slots); 2002 __ mov_metadata(c_rarg1, method()); 2003 __ call_VM_leaf( 2004 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), 2005 xthread, c_rarg1); 2006 restore_native_result(masm, ret_type, stack_slots); 2007 __ j(dtrace_method_exit_done); 2008 __ block_comment("} dtrace exit"); 2009 } 2010 } 2011 2012 __ flush(); 2013 2014 nmethod *nm = nmethod::new_native_nmethod(method, 2015 compile_id, 2016 masm->code(), 2017 vep_offset, 2018 frame_complete, 2019 stack_slots / VMRegImpl::slots_per_word, 2020 (is_static ? in_ByteSize(klass_offset) : in_ByteSize(receiver_offset)), 2021 in_ByteSize(lock_slot_offset*VMRegImpl::stack_slot_size), 2022 oop_maps); 2023 assert(nm != nullptr, "create native nmethod fail!"); 2024 return nm; 2025 } 2026 2027 // this function returns the adjust size (in number of words) to a c2i adapter 2028 // activation for use during deoptimization 2029 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals) { 2030 assert(callee_locals >= callee_parameters, 2031 "test and remove; got more parms than locals"); 2032 if (callee_locals < callee_parameters) { 2033 return 0; // No adjustment for negative locals 2034 } 2035 int diff = (callee_locals - callee_parameters) * Interpreter::stackElementWords; 2036 // diff is counted in stack words 2037 return align_up(diff, 2); 2038 } 2039 2040 //------------------------------generate_deopt_blob---------------------------- 2041 void SharedRuntime::generate_deopt_blob() { 2042 // Allocate space for the code 2043 ResourceMark rm; 2044 // Setup code generation tools 2045 int pad = 0; 2046 #if INCLUDE_JVMCI 2047 if (EnableJVMCI) { 2048 pad += 512; // Increase the buffer size when compiling for JVMCI 2049 } 2050 #endif 2051 const char* name = SharedRuntime::stub_name(StubId::shared_deopt_id); 2052 CodeBuffer buffer(name, 2048 + pad, 1024); 2053 MacroAssembler* masm = new MacroAssembler(&buffer); 2054 int frame_size_in_words = -1; 2055 OopMap* map = nullptr; 2056 OopMapSet *oop_maps = new OopMapSet(); 2057 assert_cond(masm != nullptr && oop_maps != nullptr); 2058 RegisterSaver reg_saver(COMPILER2_OR_JVMCI != 0); 2059 2060 // ------------- 2061 // This code enters when returning to a de-optimized nmethod. A return 2062 // address has been pushed on the stack, and return values are in 2063 // registers. 2064 // If we are doing a normal deopt then we were called from the patched 2065 // nmethod from the point we returned to the nmethod. So the return 2066 // address on the stack is wrong by NativeCall::instruction_size 2067 // We will adjust the value so it looks like we have the original return 2068 // address on the stack (like when we eagerly deoptimized). 2069 // In the case of an exception pending when deoptimizing, we enter 2070 // with a return address on the stack that points after the call we patched 2071 // into the exception handler. We have the following register state from, 2072 // e.g., the forward exception stub (see stubGenerator_riscv.cpp). 2073 // x10: exception oop 2074 // x9: exception handler 2075 // x13: throwing pc 2076 // So in this case we simply jam x13 into the useless return address and 2077 // the stack looks just like we want. 2078 // 2079 // At this point we need to de-opt. We save the argument return 2080 // registers. We call the first C routine, fetch_unroll_info(). This 2081 // routine captures the return values and returns a structure which 2082 // describes the current frame size and the sizes of all replacement frames. 2083 // The current frame is compiled code and may contain many inlined 2084 // functions, each with their own JVM state. We pop the current frame, then 2085 // push all the new frames. Then we call the C routine unpack_frames() to 2086 // populate these frames. Finally unpack_frames() returns us the new target 2087 // address. Notice that callee-save registers are BLOWN here; they have 2088 // already been captured in the vframeArray at the time the return PC was 2089 // patched. 2090 address start = __ pc(); 2091 Label cont; 2092 2093 // Prolog for non exception case! 2094 2095 // Save everything in sight. 2096 map = reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2097 2098 // Normal deoptimization. Save exec mode for unpack_frames. 2099 __ mv(xcpool, Deoptimization::Unpack_deopt); // callee-saved 2100 __ j(cont); 2101 2102 int reexecute_offset = __ pc() - start; 2103 #if INCLUDE_JVMCI && !defined(COMPILER1) 2104 if (UseJVMCICompiler) { 2105 // JVMCI does not use this kind of deoptimization 2106 __ should_not_reach_here(); 2107 } 2108 #endif 2109 2110 // Reexecute case 2111 // return address is the pc describes what bci to do re-execute at 2112 2113 // No need to update map as each call to save_live_registers will produce identical oopmap 2114 (void) reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2115 2116 __ mv(xcpool, Deoptimization::Unpack_reexecute); // callee-saved 2117 __ j(cont); 2118 2119 #if INCLUDE_JVMCI 2120 Label after_fetch_unroll_info_call; 2121 int implicit_exception_uncommon_trap_offset = 0; 2122 int uncommon_trap_offset = 0; 2123 2124 if (EnableJVMCI) { 2125 implicit_exception_uncommon_trap_offset = __ pc() - start; 2126 2127 __ ld(ra, Address(xthread, in_bytes(JavaThread::jvmci_implicit_exception_pc_offset()))); 2128 __ sd(zr, Address(xthread, in_bytes(JavaThread::jvmci_implicit_exception_pc_offset()))); 2129 2130 uncommon_trap_offset = __ pc() - start; 2131 2132 // Save everything in sight. 2133 reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2134 // fetch_unroll_info needs to call last_java_frame() 2135 Label retaddr; 2136 __ set_last_Java_frame(sp, noreg, retaddr, t0); 2137 2138 __ lw(c_rarg1, Address(xthread, in_bytes(JavaThread::pending_deoptimization_offset()))); 2139 __ mv(t0, -1); 2140 __ sw(t0, Address(xthread, in_bytes(JavaThread::pending_deoptimization_offset()))); 2141 2142 __ mv(xcpool, Deoptimization::Unpack_reexecute); 2143 __ mv(c_rarg0, xthread); 2144 __ orrw(c_rarg2, zr, xcpool); // exec mode 2145 __ rt_call(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap)); 2146 __ bind(retaddr); 2147 oop_maps->add_gc_map( __ pc()-start, map->deep_copy()); 2148 2149 __ reset_last_Java_frame(false); 2150 2151 __ j(after_fetch_unroll_info_call); 2152 } // EnableJVMCI 2153 #endif // INCLUDE_JVMCI 2154 2155 int exception_offset = __ pc() - start; 2156 2157 // Prolog for exception case 2158 2159 // all registers are dead at this entry point, except for x10, and 2160 // x13 which contain the exception oop and exception pc 2161 // respectively. Set them in TLS and fall thru to the 2162 // unpack_with_exception_in_tls entry point. 2163 2164 __ sd(x13, Address(xthread, JavaThread::exception_pc_offset())); 2165 __ sd(x10, Address(xthread, JavaThread::exception_oop_offset())); 2166 2167 int exception_in_tls_offset = __ pc() - start; 2168 2169 // new implementation because exception oop is now passed in JavaThread 2170 2171 // Prolog for exception case 2172 // All registers must be preserved because they might be used by LinearScan 2173 // Exceptiop oop and throwing PC are passed in JavaThread 2174 // tos: stack at point of call to method that threw the exception (i.e. only 2175 // args are on the stack, no return address) 2176 2177 // The return address pushed by save_live_registers will be patched 2178 // later with the throwing pc. The correct value is not available 2179 // now because loading it from memory would destroy registers. 2180 2181 // NB: The SP at this point must be the SP of the method that is 2182 // being deoptimized. Deoptimization assumes that the frame created 2183 // here by save_live_registers is immediately below the method's SP. 2184 // This is a somewhat fragile mechanism. 2185 2186 // Save everything in sight. 2187 map = reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2188 2189 // Now it is safe to overwrite any register 2190 2191 // Deopt during an exception. Save exec mode for unpack_frames. 2192 __ mv(xcpool, Deoptimization::Unpack_exception); // callee-saved 2193 2194 // load throwing pc from JavaThread and patch it as the return address 2195 // of the current frame. Then clear the field in JavaThread 2196 2197 __ ld(x13, Address(xthread, JavaThread::exception_pc_offset())); 2198 __ sd(x13, Address(fp, frame::return_addr_offset * wordSize)); 2199 __ sd(zr, Address(xthread, JavaThread::exception_pc_offset())); 2200 2201 #ifdef ASSERT 2202 // verify that there is really an exception oop in JavaThread 2203 __ ld(x10, Address(xthread, JavaThread::exception_oop_offset())); 2204 __ verify_oop(x10); 2205 2206 // verify that there is no pending exception 2207 Label no_pending_exception; 2208 __ ld(t0, Address(xthread, Thread::pending_exception_offset())); 2209 __ beqz(t0, no_pending_exception); 2210 __ stop("must not have pending exception here"); 2211 __ bind(no_pending_exception); 2212 #endif 2213 2214 __ bind(cont); 2215 2216 // Call C code. Need thread and this frame, but NOT official VM entry 2217 // crud. We cannot block on this call, no GC can happen. 2218 // 2219 // UnrollBlock* fetch_unroll_info(JavaThread* thread) 2220 2221 // fetch_unroll_info needs to call last_java_frame(). 2222 2223 Label retaddr; 2224 __ set_last_Java_frame(sp, noreg, retaddr, t0); 2225 #ifdef ASSERT 2226 { 2227 Label L; 2228 __ ld(t0, Address(xthread, 2229 JavaThread::last_Java_fp_offset())); 2230 __ beqz(t0, L); 2231 __ stop("SharedRuntime::generate_deopt_blob: last_Java_fp not cleared"); 2232 __ bind(L); 2233 } 2234 #endif // ASSERT 2235 __ mv(c_rarg0, xthread); 2236 __ mv(c_rarg1, xcpool); 2237 __ rt_call(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info)); 2238 __ bind(retaddr); 2239 2240 // Need to have an oopmap that tells fetch_unroll_info where to 2241 // find any register it might need. 2242 oop_maps->add_gc_map(__ pc() - start, map); 2243 2244 __ reset_last_Java_frame(false); 2245 2246 #if INCLUDE_JVMCI 2247 if (EnableJVMCI) { 2248 __ bind(after_fetch_unroll_info_call); 2249 } 2250 #endif 2251 2252 // Load UnrollBlock* into x15 2253 __ mv(x15, x10); 2254 2255 __ lwu(xcpool, Address(x15, Deoptimization::UnrollBlock::unpack_kind_offset())); 2256 Label noException; 2257 __ mv(t0, Deoptimization::Unpack_exception); 2258 __ bne(xcpool, t0, noException); // Was exception pending? 2259 __ ld(x10, Address(xthread, JavaThread::exception_oop_offset())); 2260 __ ld(x13, Address(xthread, JavaThread::exception_pc_offset())); 2261 __ sd(zr, Address(xthread, JavaThread::exception_oop_offset())); 2262 __ sd(zr, Address(xthread, JavaThread::exception_pc_offset())); 2263 2264 __ verify_oop(x10); 2265 2266 // Overwrite the result registers with the exception results. 2267 __ sd(x10, Address(sp, reg_saver.reg_offset_in_bytes(x10))); 2268 2269 __ bind(noException); 2270 2271 // Only register save data is on the stack. 2272 // Now restore the result registers. Everything else is either dead 2273 // or captured in the vframeArray. 2274 2275 // Restore fp result register 2276 __ fld(f10, Address(sp, reg_saver.freg_offset_in_bytes(f10))); 2277 // Restore integer result register 2278 __ ld(x10, Address(sp, reg_saver.reg_offset_in_bytes(x10))); 2279 2280 // Pop all of the register save area off the stack 2281 __ add(sp, sp, frame_size_in_words * wordSize); 2282 2283 // All of the register save area has been popped of the stack. Only the 2284 // return address remains. 2285 2286 // Pop all the frames we must move/replace. 2287 // 2288 // Frame picture (youngest to oldest) 2289 // 1: self-frame (no frame link) 2290 // 2: deopting frame (no frame link) 2291 // 3: caller of deopting frame (could be compiled/interpreted). 2292 // 2293 // Note: by leaving the return address of self-frame on the stack 2294 // and using the size of frame 2 to adjust the stack 2295 // when we are done the return to frame 3 will still be on the stack. 2296 2297 // Pop deoptimized frame 2298 __ lwu(x12, Address(x15, Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset())); 2299 __ subi(x12, x12, 2 * wordSize); 2300 __ add(sp, sp, x12); 2301 __ ld(fp, Address(sp, 0)); 2302 __ ld(ra, Address(sp, wordSize)); 2303 __ addi(sp, sp, 2 * wordSize); 2304 // RA should now be the return address to the caller (3) 2305 2306 #ifdef ASSERT 2307 // Compilers generate code that bang the stack by as much as the 2308 // interpreter would need. So this stack banging should never 2309 // trigger a fault. Verify that it does not on non product builds. 2310 __ lwu(x9, Address(x15, Deoptimization::UnrollBlock::total_frame_sizes_offset())); 2311 __ bang_stack_size(x9, x12); 2312 #endif 2313 // Load address of array of frame pcs into x12 2314 __ ld(x12, Address(x15, Deoptimization::UnrollBlock::frame_pcs_offset())); 2315 2316 // Load address of array of frame sizes into x14 2317 __ ld(x14, Address(x15, Deoptimization::UnrollBlock::frame_sizes_offset())); 2318 2319 // Load counter into x13 2320 __ lwu(x13, Address(x15, Deoptimization::UnrollBlock::number_of_frames_offset())); 2321 2322 // Now adjust the caller's stack to make up for the extra locals 2323 // but record the original sp so that we can save it in the skeletal interpreter 2324 // frame and the stack walking of interpreter_sender will get the unextended sp 2325 // value and not the "real" sp value. 2326 2327 const Register sender_sp = x16; 2328 2329 __ mv(sender_sp, sp); 2330 __ lwu(x9, Address(x15, 2331 Deoptimization::UnrollBlock:: 2332 caller_adjustment_offset())); 2333 __ sub(sp, sp, x9); 2334 2335 // Push interpreter frames in a loop 2336 __ mv(t0, 0xDEADDEAD); // Make a recognizable pattern 2337 __ mv(t1, t0); 2338 Label loop; 2339 __ bind(loop); 2340 __ ld(x9, Address(x14, 0)); // Load frame size 2341 __ addi(x14, x14, wordSize); 2342 __ subi(x9, x9, 2 * wordSize); // We'll push pc and fp by hand 2343 __ ld(ra, Address(x12, 0)); // Load pc 2344 __ addi(x12, x12, wordSize); 2345 __ enter(); // Save old & set new fp 2346 __ sub(sp, sp, x9); // Prolog 2347 // This value is corrected by layout_activation_impl 2348 __ sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize)); 2349 __ sd(sender_sp, Address(fp, frame::interpreter_frame_sender_sp_offset * wordSize)); // Make it walkable 2350 __ mv(sender_sp, sp); // Pass sender_sp to next frame 2351 __ subi(x13, x13, 1); // Decrement counter 2352 __ bnez(x13, loop); 2353 2354 // Re-push self-frame 2355 __ ld(ra, Address(x12)); 2356 __ enter(); 2357 2358 // Allocate a full sized register save area. We subtract 2 because 2359 // enter() just pushed 2 words 2360 __ sub(sp, sp, (frame_size_in_words - 2) * wordSize); 2361 2362 // Restore frame locals after moving the frame 2363 __ fsd(f10, Address(sp, reg_saver.freg_offset_in_bytes(f10))); 2364 __ sd(x10, Address(sp, reg_saver.reg_offset_in_bytes(x10))); 2365 2366 // Call C code. Need thread but NOT official VM entry 2367 // crud. We cannot block on this call, no GC can happen. Call should 2368 // restore return values to their stack-slots with the new SP. 2369 // 2370 // void Deoptimization::unpack_frames(JavaThread* thread, int exec_mode) 2371 2372 // Use fp because the frames look interpreted now 2373 // Don't need the precise return PC here, just precise enough to point into this code blob. 2374 address the_pc = __ pc(); 2375 __ set_last_Java_frame(sp, fp, the_pc, t0); 2376 2377 __ mv(c_rarg0, xthread); 2378 __ mv(c_rarg1, xcpool); // second arg: exec_mode 2379 __ rt_call(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)); 2380 2381 // Set an oopmap for the call site 2382 // Use the same PC we used for the last java frame 2383 oop_maps->add_gc_map(the_pc - start, 2384 new OopMap(frame_size_in_words, 0)); 2385 2386 // Clear fp AND pc 2387 __ reset_last_Java_frame(true); 2388 2389 // Collect return values 2390 __ fld(f10, Address(sp, reg_saver.freg_offset_in_bytes(f10))); 2391 __ ld(x10, Address(sp, reg_saver.reg_offset_in_bytes(x10))); 2392 2393 // Pop self-frame. 2394 __ leave(); // Epilog 2395 2396 // Jump to interpreter 2397 __ ret(); 2398 2399 // Make sure all code is generated 2400 masm->flush(); 2401 2402 _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset, reexecute_offset, frame_size_in_words); 2403 assert(_deopt_blob != nullptr, "create deoptimization blob fail!"); 2404 _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset); 2405 #if INCLUDE_JVMCI 2406 if (EnableJVMCI) { 2407 _deopt_blob->set_uncommon_trap_offset(uncommon_trap_offset); 2408 _deopt_blob->set_implicit_exception_uncommon_trap_offset(implicit_exception_uncommon_trap_offset); 2409 } 2410 #endif 2411 } 2412 2413 // Number of stack slots between incoming argument block and the start of 2414 // a new frame. The PROLOG must add this many slots to the stack. The 2415 // EPILOG must remove this many slots. 2416 // RISCV needs two words for RA (return address) and FP (frame pointer). 2417 uint SharedRuntime::in_preserve_stack_slots() { 2418 return 2 * VMRegImpl::slots_per_word; 2419 } 2420 2421 uint SharedRuntime::out_preserve_stack_slots() { 2422 return 0; 2423 } 2424 2425 VMReg SharedRuntime::thread_register() { 2426 return xthread->as_VMReg(); 2427 } 2428 2429 //------------------------------generate_handler_blob------ 2430 // 2431 // Generate a special Compile2Runtime blob that saves all registers, 2432 // and setup oopmap. 2433 // 2434 SafepointBlob* SharedRuntime::generate_handler_blob(StubId id, address call_ptr) { 2435 assert(is_polling_page_id(id), "expected a polling page stub id"); 2436 2437 ResourceMark rm; 2438 OopMapSet *oop_maps = new OopMapSet(); 2439 assert_cond(oop_maps != nullptr); 2440 OopMap* map = nullptr; 2441 2442 // Allocate space for the code. Setup code generation tools. 2443 const char* name = SharedRuntime::stub_name(id); 2444 CodeBuffer buffer(name, 2048, 1024); 2445 MacroAssembler* masm = new MacroAssembler(&buffer); 2446 assert_cond(masm != nullptr); 2447 2448 address start = __ pc(); 2449 address call_pc = nullptr; 2450 int frame_size_in_words = -1; 2451 bool cause_return = (id == StubId::shared_polling_page_return_handler_id); 2452 RegisterSaver reg_saver(id == StubId::shared_polling_page_vectors_safepoint_handler_id /* save_vectors */); 2453 2454 // Save Integer and Float registers. 2455 map = reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2456 2457 // The following is basically a call_VM. However, we need the precise 2458 // address of the call in order to generate an oopmap. Hence, we do all the 2459 // work ourselves. 2460 2461 Label retaddr; 2462 __ set_last_Java_frame(sp, noreg, retaddr, t0); 2463 2464 // The return address must always be correct so that frame constructor never 2465 // sees an invalid pc. 2466 2467 if (!cause_return) { 2468 // overwrite the return address pushed by save_live_registers 2469 // Additionally, x18 is a callee-saved register so we can look at 2470 // it later to determine if someone changed the return address for 2471 // us! 2472 __ ld(x18, Address(xthread, JavaThread::saved_exception_pc_offset())); 2473 __ sd(x18, Address(fp, frame::return_addr_offset * wordSize)); 2474 } 2475 2476 // Do the call 2477 __ mv(c_rarg0, xthread); 2478 __ rt_call(call_ptr); 2479 __ bind(retaddr); 2480 2481 // Set an oopmap for the call site. This oopmap will map all 2482 // oop-registers and debug-info registers as callee-saved. This 2483 // will allow deoptimization at this safepoint to find all possible 2484 // debug-info recordings, as well as let GC find all oops. 2485 2486 oop_maps->add_gc_map( __ pc() - start, map); 2487 2488 Label noException; 2489 2490 __ reset_last_Java_frame(false); 2491 2492 __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore); 2493 2494 __ ld(t0, Address(xthread, Thread::pending_exception_offset())); 2495 __ beqz(t0, noException); 2496 2497 // Exception pending 2498 2499 reg_saver.restore_live_registers(masm); 2500 2501 __ far_jump(RuntimeAddress(StubRoutines::forward_exception_entry())); 2502 2503 // No exception case 2504 __ bind(noException); 2505 2506 Label no_adjust, bail; 2507 if (!cause_return) { 2508 // If our stashed return pc was modified by the runtime we avoid touching it 2509 __ ld(t0, Address(fp, frame::return_addr_offset * wordSize)); 2510 __ bne(x18, t0, no_adjust); 2511 2512 #ifdef ASSERT 2513 // Verify the correct encoding of the poll we're about to skip. 2514 // See NativeInstruction::is_lwu_to_zr() 2515 __ lwu(t0, Address(x18)); 2516 __ andi(t1, t0, 0b1111111); 2517 __ mv(t2, 0b0000011); 2518 __ bne(t1, t2, bail); // 0-6:0b0000011 2519 __ srli(t1, t0, 7); 2520 __ andi(t1, t1, 0b11111); 2521 __ bnez(t1, bail); // 7-11:0b00000 2522 __ srli(t1, t0, 12); 2523 __ andi(t1, t1, 0b111); 2524 __ mv(t2, 0b110); 2525 __ bne(t1, t2, bail); // 12-14:0b110 2526 #endif 2527 2528 // Adjust return pc forward to step over the safepoint poll instruction 2529 __ addi(x18, x18, NativeInstruction::instruction_size); 2530 __ sd(x18, Address(fp, frame::return_addr_offset * wordSize)); 2531 } 2532 2533 __ bind(no_adjust); 2534 // Normal exit, restore registers and exit. 2535 2536 reg_saver.restore_live_registers(masm); 2537 __ ret(); 2538 2539 #ifdef ASSERT 2540 __ bind(bail); 2541 __ stop("Attempting to adjust pc to skip safepoint poll but the return point is not what we expected"); 2542 #endif 2543 2544 // Make sure all code is generated 2545 masm->flush(); 2546 2547 // Fill-out other meta info 2548 return SafepointBlob::create(&buffer, oop_maps, frame_size_in_words); 2549 } 2550 2551 // 2552 // generate_resolve_blob - call resolution (static/virtual/opt-virtual/ic-miss 2553 // 2554 // Generate a stub that calls into vm to find out the proper destination 2555 // of a java call. All the argument registers are live at this point 2556 // but since this is generic code we don't know what they are and the caller 2557 // must do any gc of the args. 2558 // 2559 RuntimeStub* SharedRuntime::generate_resolve_blob(StubId id, address destination) { 2560 assert(StubRoutines::forward_exception_entry() != nullptr, "must be generated before"); 2561 assert(is_resolve_id(id), "expected a resolve stub id"); 2562 2563 // allocate space for the code 2564 ResourceMark rm; 2565 2566 const char* name = SharedRuntime::stub_name(id); 2567 CodeBuffer buffer(name, 1000, 512); 2568 MacroAssembler* masm = new MacroAssembler(&buffer); 2569 assert_cond(masm != nullptr); 2570 2571 int frame_size_in_words = -1; 2572 RegisterSaver reg_saver(false /* save_vectors */); 2573 2574 OopMapSet *oop_maps = new OopMapSet(); 2575 assert_cond(oop_maps != nullptr); 2576 OopMap* map = nullptr; 2577 2578 int start = __ offset(); 2579 2580 map = reg_saver.save_live_registers(masm, 0, &frame_size_in_words); 2581 2582 int frame_complete = __ offset(); 2583 2584 { 2585 Label retaddr; 2586 __ set_last_Java_frame(sp, noreg, retaddr, t0); 2587 2588 __ mv(c_rarg0, xthread); 2589 __ rt_call(destination); 2590 __ bind(retaddr); 2591 } 2592 2593 // Set an oopmap for the call site. 2594 // We need this not only for callee-saved registers, but also for volatile 2595 // registers that the compiler might be keeping live across a safepoint. 2596 2597 oop_maps->add_gc_map( __ offset() - start, map); 2598 2599 // x10 contains the address we are going to jump to assuming no exception got installed 2600 2601 // clear last_Java_sp 2602 __ reset_last_Java_frame(false); 2603 // check for pending exceptions 2604 Label pending; 2605 __ ld(t1, Address(xthread, Thread::pending_exception_offset())); 2606 __ bnez(t1, pending); 2607 2608 // get the returned Method* 2609 __ get_vm_result_metadata(xmethod, xthread); 2610 __ sd(xmethod, Address(sp, reg_saver.reg_offset_in_bytes(xmethod))); 2611 2612 // x10 is where we want to jump, overwrite t1 which is saved and temporary 2613 __ sd(x10, Address(sp, reg_saver.reg_offset_in_bytes(t1))); 2614 reg_saver.restore_live_registers(masm); 2615 2616 // We are back to the original state on entry and ready to go. 2617 __ jr(t1); 2618 2619 // Pending exception after the safepoint 2620 2621 __ bind(pending); 2622 2623 reg_saver.restore_live_registers(masm); 2624 2625 // exception pending => remove activation and forward to exception handler 2626 2627 __ sd(zr, Address(xthread, JavaThread::vm_result_oop_offset())); 2628 2629 __ ld(x10, Address(xthread, Thread::pending_exception_offset())); 2630 __ far_jump(RuntimeAddress(StubRoutines::forward_exception_entry())); 2631 2632 // ------------- 2633 // make sure all code is generated 2634 masm->flush(); 2635 2636 // return the blob 2637 return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_words, oop_maps, true); 2638 } 2639 2640 // Continuation point for throwing of implicit exceptions that are 2641 // not handled in the current activation. Fabricates an exception 2642 // oop and initiates normal exception dispatching in this 2643 // frame. Since we need to preserve callee-saved values (currently 2644 // only for C2, but done for C1 as well) we need a callee-saved oop 2645 // map and therefore have to make these stubs into RuntimeStubs 2646 // rather than BufferBlobs. If the compiler needs all registers to 2647 // be preserved between the fault point and the exception handler 2648 // then it must assume responsibility for that in 2649 // AbstractCompiler::continuation_for_implicit_null_exception or 2650 // continuation_for_implicit_division_by_zero_exception. All other 2651 // implicit exceptions (e.g., NullPointerException or 2652 // AbstractMethodError on entry) are either at call sites or 2653 // otherwise assume that stack unwinding will be initiated, so 2654 // caller saved registers were assumed volatile in the compiler. 2655 2656 RuntimeStub* SharedRuntime::generate_throw_exception(StubId id, address runtime_entry) { 2657 assert(is_throw_id(id), "expected a throw stub id"); 2658 2659 const char* name = SharedRuntime::stub_name(id); 2660 2661 // Information about frame layout at time of blocking runtime call. 2662 // Note that we only have to preserve callee-saved registers since 2663 // the compilers are responsible for supplying a continuation point 2664 // if they expect all registers to be preserved. 2665 // n.b. riscv asserts that frame::arg_reg_save_area_bytes == 0 2666 assert_cond(runtime_entry != nullptr); 2667 enum layout { 2668 fp_off = 0, 2669 fp_off2, 2670 return_off, 2671 return_off2, 2672 framesize // inclusive of return address 2673 }; 2674 2675 const int insts_size = 1024; 2676 const int locs_size = 64; 2677 2678 ResourceMark rm; 2679 const char* timer_msg = "SharedRuntime generate_throw_exception"; 2680 TraceTime timer(timer_msg, TRACETIME_LOG(Info, startuptime)); 2681 2682 CodeBuffer code(name, insts_size, locs_size); 2683 OopMapSet* oop_maps = new OopMapSet(); 2684 MacroAssembler* masm = new MacroAssembler(&code); 2685 assert_cond(oop_maps != nullptr && masm != nullptr); 2686 2687 address start = __ pc(); 2688 2689 // This is an inlined and slightly modified version of call_VM 2690 // which has the ability to fetch the return PC out of 2691 // thread-local storage and also sets up last_Java_sp slightly 2692 // differently than the real call_VM 2693 2694 __ enter(); // Save FP and RA before call 2695 2696 assert(is_even(framesize / 2), "sp not 16-byte aligned"); 2697 2698 // ra and fp are already in place 2699 __ subi(sp, fp, (unsigned)framesize << LogBytesPerInt); // prolog 2700 2701 int frame_complete = __ pc() - start; 2702 2703 // Set up last_Java_sp and last_Java_fp 2704 address the_pc = __ pc(); 2705 __ set_last_Java_frame(sp, fp, the_pc, t0); 2706 2707 // Call runtime 2708 __ mv(c_rarg0, xthread); 2709 BLOCK_COMMENT("call runtime_entry"); 2710 __ rt_call(runtime_entry); 2711 2712 // Generate oop map 2713 OopMap* map = new OopMap(framesize, 0); 2714 assert_cond(map != nullptr); 2715 2716 oop_maps->add_gc_map(the_pc - start, map); 2717 2718 __ reset_last_Java_frame(true); 2719 2720 __ leave(); 2721 2722 // check for pending exceptions 2723 #ifdef ASSERT 2724 Label L; 2725 __ ld(t0, Address(xthread, Thread::pending_exception_offset())); 2726 __ bnez(t0, L); 2727 __ should_not_reach_here(); 2728 __ bind(L); 2729 #endif // ASSERT 2730 __ far_jump(RuntimeAddress(StubRoutines::forward_exception_entry())); 2731 2732 // codeBlob framesize is in words (not VMRegImpl::slot_size) 2733 RuntimeStub* stub = 2734 RuntimeStub::new_runtime_stub(name, 2735 &code, 2736 frame_complete, 2737 (framesize >> (LogBytesPerWord - LogBytesPerInt)), 2738 oop_maps, false); 2739 assert(stub != nullptr, "create runtime stub fail!"); 2740 return stub; 2741 } 2742 2743 #if INCLUDE_JFR 2744 2745 static void jfr_prologue(address the_pc, MacroAssembler* masm, Register thread) { 2746 __ set_last_Java_frame(sp, fp, the_pc, t0); 2747 __ mv(c_rarg0, thread); 2748 } 2749 2750 static void jfr_epilogue(MacroAssembler* masm) { 2751 __ reset_last_Java_frame(true); 2752 } 2753 // For c2: c_rarg0 is junk, call to runtime to write a checkpoint. 2754 // It returns a jobject handle to the event writer. 2755 // The handle is dereferenced and the return value is the event writer oop. 2756 RuntimeStub* SharedRuntime::generate_jfr_write_checkpoint() { 2757 enum layout { 2758 fp_off, 2759 fp_off2, 2760 return_off, 2761 return_off2, 2762 framesize // inclusive of return address 2763 }; 2764 2765 int insts_size = 1024; 2766 int locs_size = 64; 2767 const char* name = SharedRuntime::stub_name(StubId::shared_jfr_write_checkpoint_id); 2768 CodeBuffer code(name, insts_size, locs_size); 2769 OopMapSet* oop_maps = new OopMapSet(); 2770 MacroAssembler* masm = new MacroAssembler(&code); 2771 2772 address start = __ pc(); 2773 __ enter(); 2774 int frame_complete = __ pc() - start; 2775 address the_pc = __ pc(); 2776 jfr_prologue(the_pc, masm, xthread); 2777 __ call_VM_leaf(CAST_FROM_FN_PTR(address, JfrIntrinsicSupport::write_checkpoint), 1); 2778 2779 jfr_epilogue(masm); 2780 __ resolve_global_jobject(x10, t0, t1); 2781 __ leave(); 2782 __ ret(); 2783 2784 OopMap* map = new OopMap(framesize, 1); 2785 oop_maps->add_gc_map(the_pc - start, map); 2786 2787 RuntimeStub* stub = // codeBlob framesize is in words (not VMRegImpl::slot_size) 2788 RuntimeStub::new_runtime_stub(name, &code, frame_complete, 2789 (framesize >> (LogBytesPerWord - LogBytesPerInt)), 2790 oop_maps, false); 2791 return stub; 2792 } 2793 2794 // For c2: call to return a leased buffer. 2795 RuntimeStub* SharedRuntime::generate_jfr_return_lease() { 2796 enum layout { 2797 fp_off, 2798 fp_off2, 2799 return_off, 2800 return_off2, 2801 framesize // inclusive of return address 2802 }; 2803 2804 int insts_size = 1024; 2805 int locs_size = 64; 2806 const char* name = SharedRuntime::stub_name(StubId::shared_jfr_return_lease_id); 2807 CodeBuffer code(name, insts_size, locs_size); 2808 OopMapSet* oop_maps = new OopMapSet(); 2809 MacroAssembler* masm = new MacroAssembler(&code); 2810 2811 address start = __ pc(); 2812 __ enter(); 2813 int frame_complete = __ pc() - start; 2814 address the_pc = __ pc(); 2815 jfr_prologue(the_pc, masm, xthread); 2816 __ call_VM_leaf(CAST_FROM_FN_PTR(address, JfrIntrinsicSupport::return_lease), 1); 2817 2818 jfr_epilogue(masm); 2819 __ leave(); 2820 __ ret(); 2821 2822 OopMap* map = new OopMap(framesize, 1); 2823 oop_maps->add_gc_map(the_pc - start, map); 2824 2825 RuntimeStub* stub = // codeBlob framesize is in words (not VMRegImpl::slot_size) 2826 RuntimeStub::new_runtime_stub(name, &code, frame_complete, 2827 (framesize >> (LogBytesPerWord - LogBytesPerInt)), 2828 oop_maps, false); 2829 return stub; 2830 } 2831 2832 #endif // INCLUDE_JFR