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