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