658 // Allocate argument register save area
659 if (frame::arg_reg_save_area_bytes != 0) {
660 __ subptr(rsp, frame::arg_reg_save_area_bytes);
661 }
662 __ mov(c_rarg0, rbx);
663 __ mov(c_rarg1, rax);
664 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
665
666 // De-allocate argument register save area
667 if (frame::arg_reg_save_area_bytes != 0) {
668 __ addptr(rsp, frame::arg_reg_save_area_bytes);
669 }
670
671 __ vzeroupper();
672 __ pop_CPU_state();
673 // restore sp
674 __ mov(rsp, r13);
675 __ bind(L);
676 }
677
678
679 static void gen_c2i_adapter(MacroAssembler *masm,
680 int total_args_passed,
681 int comp_args_on_stack,
682 const BasicType *sig_bt,
683 const VMRegPair *regs,
684 Label& skip_fixup) {
685 // Before we get into the guts of the C2I adapter, see if we should be here
686 // at all. We've come from compiled code and are attempting to jump to the
687 // interpreter, which means the caller made a static call to get here
688 // (vcalls always get a compiled target if there is one). Check for a
689 // compiled target. If there is one, we need to patch the caller's call.
690 patch_callers_callsite(masm);
691
692 __ bind(skip_fixup);
693
694 // Since all args are passed on the stack, total_args_passed *
695 // Interpreter::stackElementSize is the space we need.
696
697 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
698
809 assert(r_1->is_XMMRegister(), "");
810 if (!r_2->is_valid()) {
811 // only a float use just part of the slot
812 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
813 } else {
814 #ifdef ASSERT
815 // Overwrite the unused slot with known junk
816 __ mov64(rax, CONST64(0xdeadffffdeadaaac));
817 __ movptr(Address(rsp, st_off), rax);
818 #endif /* ASSERT */
819 __ movdbl(Address(rsp, next_off), r_1->as_XMMRegister());
820 }
821 }
822 }
823
824 // Schedule the branch target address early.
825 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
826 __ jmp(rcx);
827 }
828
829 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
830 address code_start, address code_end,
831 Label& L_ok) {
832 Label L_fail;
833 __ lea(temp_reg, AddressLiteral(code_start, relocInfo::none));
834 __ cmpptr(pc_reg, temp_reg);
835 __ jcc(Assembler::belowEqual, L_fail);
836 __ lea(temp_reg, AddressLiteral(code_end, relocInfo::none));
837 __ cmpptr(pc_reg, temp_reg);
838 __ jcc(Assembler::below, L_ok);
839 __ bind(L_fail);
840 }
841
842 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
843 int total_args_passed,
844 int comp_args_on_stack,
845 const BasicType *sig_bt,
846 const VMRegPair *regs) {
847
848 // Note: r13 contains the senderSP on entry. We must preserve it since
849 // we may do a i2c -> c2i transition if we lose a race where compiled
850 // code goes non-entrant while we get args ready.
851 // In addition we use r13 to locate all the interpreter args as
852 // we must align the stack to 16 bytes on an i2c entry else we
853 // lose alignment we expect in all compiled code and register
854 // save code can segv when fxsave instructions find improperly
855 // aligned stack pointer.
856
857 // Adapters can be frameless because they do not require the caller
858 // to perform additional cleanup work, such as correcting the stack pointer.
859 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
860 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
861 // even if a callee has modified the stack pointer.
862 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
863 // routinely repairs its caller's stack pointer (from sender_sp, which is set
864 // up via the senderSP register).
865 // In other words, if *either* the caller or callee is interpreted, we can
866 // get the stack pointer repaired after a call.
867 // This is why c2i and i2c adapters cannot be indefinitely composed.
868 // In particular, if a c2i adapter were to somehow call an i2c adapter,
869 // both caller and callee would be compiled methods, and neither would
870 // clean up the stack pointer changes performed by the two adapters.
871 // If this happens, control eventually transfers back to the compiled
872 // caller, but with an uncorrected stack, causing delayed havoc.
873
874 if (VerifyAdapterCalls &&
875 (Interpreter::code() != nullptr || StubRoutines::final_stubs_code() != nullptr)) {
876 // So, let's test for cascading c2i/i2c adapters right now.
877 // assert(Interpreter::contains($return_addr) ||
878 // StubRoutines::contains($return_addr),
879 // "i2c adapter must return to an interpreter frame");
880 __ block_comment("verify_i2c { ");
881 // Pick up the return address
882 __ movptr(rax, Address(rsp, 0));
883 Label L_ok;
884 if (Interpreter::code() != nullptr) {
885 range_check(masm, rax, r11,
886 Interpreter::code()->code_start(),
887 Interpreter::code()->code_end(),
888 L_ok);
889 }
890 if (StubRoutines::initial_stubs_code() != nullptr) {
891 range_check(masm, rax, r11,
892 StubRoutines::initial_stubs_code()->code_begin(),
893 StubRoutines::initial_stubs_code()->code_end(),
894 L_ok);
895 }
896 if (StubRoutines::final_stubs_code() != nullptr) {
897 range_check(masm, rax, r11,
898 StubRoutines::final_stubs_code()->code_begin(),
899 StubRoutines::final_stubs_code()->code_end(),
900 L_ok);
901 }
902 const char* msg = "i2c adapter must return to an interpreter frame";
903 __ block_comment(msg);
904 __ stop(msg);
905 __ bind(L_ok);
906 __ block_comment("} verify_i2ce ");
907 }
908
909 // Must preserve original SP for loading incoming arguments because
910 // we need to align the outgoing SP for compiled code.
911 __ movptr(r11, rsp);
912
913 // Pick up the return address
914 __ pop(rax);
915
916 // Convert 4-byte c2 stack slots to words.
917 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
918
919 if (comp_args_on_stack) {
920 __ subptr(rsp, comp_words_on_stack * wordSize);
921 }
922
923 // Ensure compiled code always sees stack at proper alignment
924 __ andptr(rsp, -16);
925
926 // push the return address and misalign the stack that youngest frame always sees
927 // as far as the placement of the call instruction
928 __ push(rax);
1033 // 6243940 We might end up in handle_wrong_method if
1034 // the callee is deoptimized as we race thru here. If that
1035 // happens we don't want to take a safepoint because the
1036 // caller frame will look interpreted and arguments are now
1037 // "compiled" so it is much better to make this transition
1038 // invisible to the stack walking code. Unfortunately if
1039 // we try and find the callee by normal means a safepoint
1040 // is possible. So we stash the desired callee in the thread
1041 // and the vm will find there should this case occur.
1042
1043 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1044
1045 // put Method* where a c2i would expect should we end up there
1046 // only needed because eof c2 resolve stubs return Method* as a result in
1047 // rax
1048 __ mov(rax, rbx);
1049 __ jmp(r11);
1050 }
1051
1052 // ---------------------------------------------------------------
1053 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1054 int total_args_passed,
1055 int comp_args_on_stack,
1056 const BasicType *sig_bt,
1057 const VMRegPair *regs,
1058 AdapterFingerPrint* fingerprint) {
1059 address i2c_entry = __ pc();
1060
1061 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
1062
1063 // -------------------------------------------------------------------------
1064 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
1065 // to the interpreter. The args start out packed in the compiled layout. They
1066 // need to be unpacked into the interpreter layout. This will almost always
1067 // require some stack space. We grow the current (compiled) stack, then repack
1068 // the args. We finally end in a jump to the generic interpreter entry point.
1069 // On exit from the interpreter, the interpreter will restore our SP (lest the
1070 // compiled code, which relies solely on SP and not RBP, get sick).
1071
1072 address c2i_unverified_entry = __ pc();
1073 Label skip_fixup;
1074
1075 Register data = rax;
1076 Register receiver = j_rarg0;
1077 Register temp = rbx;
1078
1100 __ load_unsigned_short(flags, Address(method, Method::access_flags_offset()));
1101 __ testl(flags, JVM_ACC_STATIC);
1102 __ jcc(Assembler::zero, L_skip_barrier); // non-static
1103 }
1104
1105 Register klass = rscratch1;
1106 __ load_method_holder(klass, method);
1107 __ clinit_barrier(klass, &L_skip_barrier /*L_fast_path*/);
1108
1109 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
1110
1111 __ bind(L_skip_barrier);
1112 c2i_no_clinit_check_entry = __ pc();
1113 }
1114
1115 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
1116 bs->c2i_entry_barrier(masm);
1117
1118 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
1119
1120 return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, c2i_no_clinit_check_entry);
1121 }
1122
1123 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1124 VMRegPair *regs,
1125 int total_args_passed) {
1126
1127 // We return the amount of VMRegImpl stack slots we need to reserve for all
1128 // the arguments NOT counting out_preserve_stack_slots.
1129
1130 // NOTE: These arrays will have to change when c1 is ported
1131 #ifdef _WIN64
1132 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1133 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1134 };
1135 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1136 c_farg0, c_farg1, c_farg2, c_farg3
1137 };
1138 #else
1139 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1140 c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5
|
658 // Allocate argument register save area
659 if (frame::arg_reg_save_area_bytes != 0) {
660 __ subptr(rsp, frame::arg_reg_save_area_bytes);
661 }
662 __ mov(c_rarg0, rbx);
663 __ mov(c_rarg1, rax);
664 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
665
666 // De-allocate argument register save area
667 if (frame::arg_reg_save_area_bytes != 0) {
668 __ addptr(rsp, frame::arg_reg_save_area_bytes);
669 }
670
671 __ vzeroupper();
672 __ pop_CPU_state();
673 // restore sp
674 __ mov(rsp, r13);
675 __ bind(L);
676 }
677
678 static void gen_c2i_adapter(MacroAssembler *masm,
679 int total_args_passed,
680 int comp_args_on_stack,
681 const BasicType *sig_bt,
682 const VMRegPair *regs,
683 Label& skip_fixup) {
684 // Before we get into the guts of the C2I adapter, see if we should be here
685 // at all. We've come from compiled code and are attempting to jump to the
686 // interpreter, which means the caller made a static call to get here
687 // (vcalls always get a compiled target if there is one). Check for a
688 // compiled target. If there is one, we need to patch the caller's call.
689 patch_callers_callsite(masm);
690
691 __ bind(skip_fixup);
692
693 // Since all args are passed on the stack, total_args_passed *
694 // Interpreter::stackElementSize is the space we need.
695
696 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
697
808 assert(r_1->is_XMMRegister(), "");
809 if (!r_2->is_valid()) {
810 // only a float use just part of the slot
811 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
812 } else {
813 #ifdef ASSERT
814 // Overwrite the unused slot with known junk
815 __ mov64(rax, CONST64(0xdeadffffdeadaaac));
816 __ movptr(Address(rsp, st_off), rax);
817 #endif /* ASSERT */
818 __ movdbl(Address(rsp, next_off), r_1->as_XMMRegister());
819 }
820 }
821 }
822
823 // Schedule the branch target address early.
824 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
825 __ jmp(rcx);
826 }
827
828 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
829 int total_args_passed,
830 int comp_args_on_stack,
831 const BasicType *sig_bt,
832 const VMRegPair *regs) {
833
834 // Note: r13 contains the senderSP on entry. We must preserve it since
835 // we may do a i2c -> c2i transition if we lose a race where compiled
836 // code goes non-entrant while we get args ready.
837 // In addition we use r13 to locate all the interpreter args as
838 // we must align the stack to 16 bytes on an i2c entry else we
839 // lose alignment we expect in all compiled code and register
840 // save code can segv when fxsave instructions find improperly
841 // aligned stack pointer.
842
843 // Adapters can be frameless because they do not require the caller
844 // to perform additional cleanup work, such as correcting the stack pointer.
845 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
846 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
847 // even if a callee has modified the stack pointer.
848 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
849 // routinely repairs its caller's stack pointer (from sender_sp, which is set
850 // up via the senderSP register).
851 // In other words, if *either* the caller or callee is interpreted, we can
852 // get the stack pointer repaired after a call.
853 // This is why c2i and i2c adapters cannot be indefinitely composed.
854 // In particular, if a c2i adapter were to somehow call an i2c adapter,
855 // both caller and callee would be compiled methods, and neither would
856 // clean up the stack pointer changes performed by the two adapters.
857 // If this happens, control eventually transfers back to the compiled
858 // caller, but with an uncorrected stack, causing delayed havoc.
859
860 // Must preserve original SP for loading incoming arguments because
861 // we need to align the outgoing SP for compiled code.
862 __ movptr(r11, rsp);
863
864 // Pick up the return address
865 __ pop(rax);
866
867 // Convert 4-byte c2 stack slots to words.
868 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
869
870 if (comp_args_on_stack) {
871 __ subptr(rsp, comp_words_on_stack * wordSize);
872 }
873
874 // Ensure compiled code always sees stack at proper alignment
875 __ andptr(rsp, -16);
876
877 // push the return address and misalign the stack that youngest frame always sees
878 // as far as the placement of the call instruction
879 __ push(rax);
984 // 6243940 We might end up in handle_wrong_method if
985 // the callee is deoptimized as we race thru here. If that
986 // happens we don't want to take a safepoint because the
987 // caller frame will look interpreted and arguments are now
988 // "compiled" so it is much better to make this transition
989 // invisible to the stack walking code. Unfortunately if
990 // we try and find the callee by normal means a safepoint
991 // is possible. So we stash the desired callee in the thread
992 // and the vm will find there should this case occur.
993
994 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
995
996 // put Method* where a c2i would expect should we end up there
997 // only needed because eof c2 resolve stubs return Method* as a result in
998 // rax
999 __ mov(rax, rbx);
1000 __ jmp(r11);
1001 }
1002
1003 // ---------------------------------------------------------------
1004 void SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1005 int total_args_passed,
1006 int comp_args_on_stack,
1007 const BasicType *sig_bt,
1008 const VMRegPair *regs,
1009 AdapterHandlerEntry* handler) {
1010 address i2c_entry = __ pc();
1011
1012 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
1013
1014 // -------------------------------------------------------------------------
1015 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
1016 // to the interpreter. The args start out packed in the compiled layout. They
1017 // need to be unpacked into the interpreter layout. This will almost always
1018 // require some stack space. We grow the current (compiled) stack, then repack
1019 // the args. We finally end in a jump to the generic interpreter entry point.
1020 // On exit from the interpreter, the interpreter will restore our SP (lest the
1021 // compiled code, which relies solely on SP and not RBP, get sick).
1022
1023 address c2i_unverified_entry = __ pc();
1024 Label skip_fixup;
1025
1026 Register data = rax;
1027 Register receiver = j_rarg0;
1028 Register temp = rbx;
1029
1051 __ load_unsigned_short(flags, Address(method, Method::access_flags_offset()));
1052 __ testl(flags, JVM_ACC_STATIC);
1053 __ jcc(Assembler::zero, L_skip_barrier); // non-static
1054 }
1055
1056 Register klass = rscratch1;
1057 __ load_method_holder(klass, method);
1058 __ clinit_barrier(klass, &L_skip_barrier /*L_fast_path*/);
1059
1060 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
1061
1062 __ bind(L_skip_barrier);
1063 c2i_no_clinit_check_entry = __ pc();
1064 }
1065
1066 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
1067 bs->c2i_entry_barrier(masm);
1068
1069 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
1070
1071 handler->set_entry_points(i2c_entry, c2i_entry, c2i_unverified_entry, c2i_no_clinit_check_entry);
1072 return;
1073 }
1074
1075 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1076 VMRegPair *regs,
1077 int total_args_passed) {
1078
1079 // We return the amount of VMRegImpl stack slots we need to reserve for all
1080 // the arguments NOT counting out_preserve_stack_slots.
1081
1082 // NOTE: These arrays will have to change when c1 is ported
1083 #ifdef _WIN64
1084 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1085 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1086 };
1087 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1088 c_farg0, c_farg1, c_farg2, c_farg3
1089 };
1090 #else
1091 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1092 c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5
|