From 7b5cac08922b256c08f56ebefc3efdc901d18a04 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:17:38 +0200 Subject: [PATCH 01/14] Converge NativeAOT transition and throw helpers - RhpUniversalTransitionTailCall becomes RhpInterfaceDispatchSlow, like in CoreCLR. It is built with the PROLOG_WITH_TRANSITION_BLOCK and EPILOG_WITH_TRANSITION_BLOCK_TAILCALL macros, which move from CoreCLR to the shared runtime/ macro files, and calls RhpCidResolve directly. The arguments are still reported conservatively. - The throw helpers set up and link their ExInfo with RhpPushExInfo instead of doing it in assembly. The unhijack in RhpThrowEx is removed, since it is not tail called. Also removes leftovers of the old funclet ABI: DWORD_PTR in EECodeManager::CallFunclet and a stale PSPSym comment. Co-authored-by: Cursor --- src/coreclr/inc/eetwain.h | 6 +- src/coreclr/jit/codegenarmarch.cpp | 2 +- .../src/Internal/Runtime/TransitionBlock.cs | 611 +----------------- src/coreclr/nativeaot/Runtime/CMakeLists.txt | 2 +- src/coreclr/nativeaot/Runtime/EHHelpers.cpp | 21 + .../nativeaot/Runtime/StackFrameIterator.cpp | 231 ++++--- .../nativeaot/Runtime/StackFrameIterator.h | 4 +- .../Runtime/UniversalTransitionHelpers.cpp | 2 +- .../nativeaot/Runtime/amd64/AsmMacros.inc | 4 + .../nativeaot/Runtime/amd64/DispatchResolve.S | 3 +- .../Runtime/amd64/DispatchResolve.asm | 9 +- .../Runtime/amd64/ExceptionHandling.S | 88 +-- .../Runtime/amd64/ExceptionHandling.asm | 83 +-- .../Runtime/amd64/InterfaceDispatchSlow.S | 56 ++ .../Runtime/amd64/InterfaceDispatchSlow.asm | 81 +++ .../Runtime/amd64/UniversalTransition.S | 156 ----- .../Runtime/amd64/UniversalTransition.asm | 166 ----- .../nativeaot/Runtime/arm/DispatchResolve.S | 10 +- .../nativeaot/Runtime/arm/ExceptionHandling.S | 135 +--- .../Runtime/arm/InterfaceDispatchSlow.S | 70 ++ .../Runtime/arm/UniversalTransition.S | 144 ----- .../nativeaot/Runtime/arm64/AsmMacros.h | 4 + .../nativeaot/Runtime/arm64/DispatchResolve.S | 5 +- .../Runtime/arm64/DispatchResolve.asm | 10 +- .../Runtime/arm64/ExceptionHandling.S | 129 +--- .../Runtime/arm64/ExceptionHandling.asm | 125 +--- .../Runtime/arm64/InterfaceDispatchSlow.S | 57 ++ .../Runtime/arm64/InterfaceDispatchSlow.asm | 59 ++ .../Runtime/arm64/UniversalTransition.S | 156 ----- .../Runtime/arm64/UniversalTransition.asm | 166 ----- .../nativeaot/Runtime/i386/AsmMacros.inc | 2 + .../Runtime/i386/DispatchResolve.asm | 11 +- .../Runtime/i386/ExceptionHandling.asm | 93 +-- .../Runtime/i386/InterfaceDispatchSlow.asm | 75 +++ .../Runtime/i386/UniversalTransition.asm | 89 --- .../Runtime/loongarch64/DispatchResolve.S | 8 +- .../Runtime/loongarch64/ExceptionHandling.S | 131 +--- .../loongarch64/InterfaceDispatchSlow.S | 67 ++ .../Runtime/loongarch64/UniversalTransition.S | 162 ----- src/coreclr/nativeaot/Runtime/portable.cpp | 6 +- ...alTransition.S => InterfaceDispatchSlow.S} | 0 .../Runtime/riscv64/DispatchResolve.S | 12 +- .../Runtime/riscv64/ExceptionHandling.S | 133 +--- .../Runtime/riscv64/InterfaceDispatchSlow.S | 67 ++ .../Runtime/riscv64/UniversalTransition.S | 149 ----- ...alTransition.S => InterfaceDispatchSlow.S} | 0 .../Runtime/unix/unixasmmacrosarm64.inc | 3 + .../Runtime/unix/unixasmmacrosloongarch64.inc | 3 + .../Runtime/unix/unixasmmacrosriscv64.inc | 3 + src/coreclr/pal/inc/unixasmmacrosamd64.inc | 112 ---- src/coreclr/pal/inc/unixasmmacrosarm.inc | 122 ---- src/coreclr/pal/inc/unixasmmacrosarm64.inc | 163 ----- .../pal/inc/unixasmmacrosloongarch64.inc | 110 ---- src/coreclr/pal/inc/unixasmmacrosriscv64.inc | 141 ---- src/coreclr/runtime/amd64/AsmMacros_Unix.inc | 112 ++++ .../runtime/amd64/AsmMacros_Windows.inc | 136 ++++ src/coreclr/runtime/arm/AsmMacros_Unix.inc | 122 ++++ src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 162 +++++ src/coreclr/runtime/arm64/AsmMacros_Windows.h | 133 ++++ .../runtime/loongarch64/AsmMacros_Unix.inc | 110 ++++ .../runtime/riscv64/AsmMacros_Unix.inc | 141 ++++ src/coreclr/vm/amd64/AsmMacros.inc | 136 ---- src/coreclr/vm/arm64/asmmacros.h | 133 ---- src/coreclr/vm/eetwain.cpp | 12 +- src/coreclr/vm/exceptionhandling.cpp | 4 +- 65 files changed, 1842 insertions(+), 3616 deletions(-) create mode 100644 src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S create mode 100644 src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.S delete mode 100644 src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.asm create mode 100644 src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/arm/UniversalTransition.S create mode 100644 src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S create mode 100644 src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.S delete mode 100644 src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.asm create mode 100644 src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/i386/UniversalTransition.asm create mode 100644 src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/loongarch64/UniversalTransition.S rename src/coreclr/nativeaot/Runtime/ppc64le/{UniversalTransition.S => InterfaceDispatchSlow.S} (100%) create mode 100644 src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/riscv64/UniversalTransition.S rename src/coreclr/nativeaot/Runtime/s390x/{UniversalTransition.S => InterfaceDispatchSlow.S} (100%) diff --git a/src/coreclr/inc/eetwain.h b/src/coreclr/inc/eetwain.h index 27fb800306ed25..92c9fc6be40db2 100644 --- a/src/coreclr/inc/eetwain.h +++ b/src/coreclr/inc/eetwain.h @@ -280,7 +280,7 @@ virtual unsigned int GetFrameSize(GCInfoToken gcInfoToken) = 0; /* Debugger API */ -virtual DWORD_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter) = 0; +virtual UINT_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter) = 0; virtual void ResumeAfterCatch(CONTEXT *pContext, size_t targetSSP, bool fIntercepted) = 0; #if defined(HOST_AMD64) && defined(HOST_WINDOWS) virtual void UpdateSSP(PREGDISPLAY pRD) = 0; @@ -493,7 +493,7 @@ unsigned int GetFrameSize(GCInfoToken gcInfoToken); #ifndef DACCESS_COMPILE -virtual DWORD_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter); +virtual UINT_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter); virtual void ResumeAfterCatch(CONTEXT *pContext, size_t targetSSP, bool fIntercepted); #if defined(HOST_AMD64) && defined(HOST_WINDOWS) @@ -651,7 +651,7 @@ unsigned int GetFrameSize(GCInfoToken gcInfoToken) #ifndef DACCESS_COMPILE -virtual DWORD_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter); +virtual UINT_PTR CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter); virtual void ResumeAfterCatch(CONTEXT *pContext, size_t targetSSP, bool fIntercepted); #if defined(HOST_AMD64) && defined(HOST_WINDOWS) virtual void UpdateSSP(PREGDISPLAY pRD); diff --git a/src/coreclr/jit/codegenarmarch.cpp b/src/coreclr/jit/codegenarmarch.cpp index 0e7e0520c10b0e..c06897ba075c48 100644 --- a/src/coreclr/jit/codegenarmarch.cpp +++ b/src/coreclr/jit/codegenarmarch.cpp @@ -3921,7 +3921,7 @@ void CodeGen::genCreateAndStoreGCInfo(unsigned codeSize, if (m_compiler->info.compIsVarArgs) { - // Varargs save all int registers between saved registers and PSP. + // Varargs save all integer argument registers. preservedAreaSize += MAX_REG_ARG * REGSIZE_BYTES; } diff --git a/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs b/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs index 2bce373796905b..a3395fd99cc55c 100644 --- a/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs +++ b/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs @@ -1,603 +1,44 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// -// This file is a line by line port of callingconvention.h from the CLR with the intention that we may wish to merge -// changes from the CLR in at a later time. As such, the normal coding conventions are ignored. -// - -// -#if TARGET_ARM -#define HAS_FLOAT_ARGUMENT_REGISTERS // Calling convention has floating-point argument registers -#define ENREGISTERED_RETURNTYPE_MAXSIZE -#define ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE -#define FEATURE_HFA -#elif TARGET_ARM64 -#define HAS_FLOAT_ARGUMENT_REGISTERS // Calling convention has floating-point argument registers -#define ENREGISTERED_RETURNTYPE_MAXSIZE -#define ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE -#define ENREGISTERED_PARAMTYPE_MAXSIZE -#define FEATURE_HFA -#elif TARGET_X86 -#define ENREGISTERED_RETURNTYPE_MAXSIZE -#define ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE -#elif TARGET_AMD64 -#if TARGET_UNIX -#define UNIX_AMD64_ABI -#else -#endif -#define HAS_FLOAT_ARGUMENT_REGISTERS // Calling convention has floating-point argument registers -#define ENREGISTERED_RETURNTYPE_MAXSIZE -#define ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE -#define ENREGISTERED_PARAMTYPE_MAXSIZE -#elif TARGET_WASM -#elif TARGET_LOONGARCH64 || TARGET_RISCV64 -#define HAS_FLOAT_ARGUMENT_REGISTERS // Calling convention has floating-point argument registers -#define ENREGISTERED_RETURNTYPE_MAXSIZE -#define ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE -#define ENREGISTERED_PARAMTYPE_MAXSIZE -#else -#error Unknown architecture! -#endif - -// Provides an abstraction over platform specific calling conventions (specifically, the calling convention -// utilized by the JIT on that platform). The caller enumerates each argument of a signature in turn, and is -// provided with information mapping that argument into registers and/or stack locations. - -using System; -using System.Runtime.InteropServices; - namespace Internal.Runtime { -#if TARGET_AMD64 -#if UNIX_AMD64_ABI - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - private IntPtr returnValue2; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr rdi; - private IntPtr rsi; - private IntPtr rdx; - private IntPtr rcx; - private IntPtr r8; - private IntPtr r9; - } -#else // UNIX_AMD64_ABI - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr rdx; - private IntPtr rcx; - private IntPtr r8; - private IntPtr r9; - } -#endif // UNIX_AMD64_ABI - [StructLayout(LayoutKind.Sequential)] - internal struct M128A - { - private IntPtr a; - private IntPtr b; - } - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - private M128A d0; - private M128A d1; - private M128A d2; - private M128A d3; -#if UNIX_AMD64_ABI - private M128A d4; - private M128A d5; - private M128A d6; - private M128A d7; -#endif - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - -#if UNIX_AMD64_ABI - public const int NUM_ARGUMENT_REGISTERS = 6; -#else - public const int NUM_ARGUMENT_REGISTERS = 4; -#endif - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 8; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 8; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 8; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int ENREGISTERED_PARAMTYPE_MAXSIZE = 8; - public const int STACK_ELEM_SIZE = 8; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#elif TARGET_ARM64 - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - private IntPtr returnValue2; - private IntPtr returnValue3; - private IntPtr returnValue4; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr x0; - private IntPtr x1; - private IntPtr x2; - private IntPtr x3; - private IntPtr x4; - private IntPtr x5; - private IntPtr x6; - private IntPtr x7; - private IntPtr x8; - public static unsafe int GetOffsetOfx8() - { - return sizeof(IntPtr) * 8; - } - } - - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - private double d0; - private double d1; - private double d2; - private double d3; - private double d4; - private double d5; - private double d6; - private double d7; - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 8; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 8; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 32; // bytes (four FP registers: d0,d1,d2 and d3) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 16; // bytes (two int registers: x0 and x1) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int ENREGISTERED_PARAMTYPE_MAXSIZE = 16; // bytes (max value type size that can be passed by value) - public const int STACK_ELEM_SIZE = 8; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#elif TARGET_X86 - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - public IntPtr returnValue; - public IntPtr returnValue2; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - public IntPtr edx; - public static unsafe int GetOffsetOfEdx() - { - return 0; - } - public IntPtr ecx; - public static unsafe int GetOffsetOfEcx() - { - return sizeof(IntPtr); - } - } - // Floating-point arguments are stack-passed on x86; keep an empty placeholder for the shared ABI abstraction. - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 2; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 4; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 8; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 4; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 4; - public const int STACK_ELEM_SIZE = 4; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#elif TARGET_ARM - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - private IntPtr returnValue2; - private IntPtr returnValue3; - private IntPtr returnValue4; - private IntPtr returnValue5; - private IntPtr returnValue6; - private IntPtr returnValue7; - private IntPtr returnValue8; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr r0; - private IntPtr r1; - private IntPtr r2; - private IntPtr r3; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - private double d0; - private double d1; - private double d2; - private double d3; - private double d4; - private double d5; - private double d6; - private double d7; - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 4; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 4; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 32; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 4; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int STACK_ELEM_SIZE = 4; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } - -#elif TARGET_WASM - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - // No registers on WASM - } - - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - // No registers on WASM - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 0; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 4; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 32; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 4; - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int STACK_ELEM_SIZE = 4; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#elif TARGET_LOONGARCH64 - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - private IntPtr returnValue2; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr r4; - private IntPtr r5; - private IntPtr r6; - private IntPtr r7; - private IntPtr r8; - private IntPtr r9; - private IntPtr r10; - private IntPtr r11; - public static unsafe int GetOffsetOfr11() - { - return sizeof(IntPtr) * 7; - } - } - - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - private double f0; - private double f1; - private double f2; - private double f3; - private double f4; - private double f5; - private double f6; - private double f7; - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 8; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 8; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 16; // bytes (two FP registers: f0, f1) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 16; // bytes (two int registers: a0, a1) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int ENREGISTERED_PARAMTYPE_MAXSIZE = 16; // bytes (max value type size that can be passed by value) - public const int STACK_ELEM_SIZE = 8; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#elif TARGET_RISCV64 - [StructLayout(LayoutKind.Sequential)] - internal struct ReturnBlock - { - private IntPtr returnValue; - private IntPtr returnValue2; - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ArgumentRegisters - { - private IntPtr a0; - private IntPtr a1; - private IntPtr a2; - private IntPtr a3; - private IntPtr a4; - private IntPtr a5; - private IntPtr a6; - private IntPtr a7; - public static unsafe int GetOffsetOfa7() - { - return sizeof(IntPtr) * 7; - } - } - - [StructLayout(LayoutKind.Sequential)] - internal struct FloatArgumentRegisters - { - private double fa0; - private double fa1; - private double fa2; - private double fa3; - private double fa4; - private double fa5; - private double fa6; - private double fa7; - } - - internal struct ArchitectureConstants - { - // To avoid corner case bugs, limit maximum size of the arguments with sufficient margin - public const int MAX_ARG_SIZE = 0xFFFFFF; - - public const int NUM_ARGUMENT_REGISTERS = 8; - public const int ARGUMENTREGISTERS_SIZE = NUM_ARGUMENT_REGISTERS * 8; - public const int ENREGISTERED_RETURNTYPE_MAXSIZE = 32; // bytes (four FP registers: fa0, fa1, fa2, and fa3) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE = 16; // bytes (two int registers: a0 and a1) - public const int ENREGISTERED_RETURNTYPE_INTEGER_MAXSIZE_PRIMITIVE = 8; - public const int ENREGISTERED_PARAMTYPE_MAXSIZE = 16; // bytes (max value type size that can be passed by value) - public const int STACK_ELEM_SIZE = 8; - public static int StackElemSize(int size) { return (((size) + STACK_ELEM_SIZE - 1) & ~(STACK_ELEM_SIZE - 1)); } - } -#endif - // - // TransitionBlock is layout of stack frame of method call, saved argument registers and saved callee saved registers. Even though not - // all fields are used all the time, we use uniform form for simplicity. + // The TransitionBlock that RhpInterfaceDispatchSlow passes to RhpCidResolve. Except on x86 and WASM, it is + // created by PROLOG_WITH_TRANSITION_BLOCK and has the same layout as the TransitionBlock in CoreCLR's + // callingconvention.h. // - [StructLayout(LayoutKind.Sequential)] - internal struct TransitionBlock + internal static class TransitionBlock { -#if TARGET_X86 - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() + // Offset of the 'this' argument from the TransitionBlock pointer + public static int GetThisOffset() { +#if TARGET_AMD64 && TARGET_UNIX + // rdi is the first of the argument registers at the base of the TransitionBlock return 0; - } - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return sizeof(ArgumentRegisters); - } - private IntPtr m_ebp; - private IntPtr m_ReturnAddress; #elif TARGET_AMD64 - -#if UNIX_AMD64_ABI - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } - - private IntPtr m_alignmentPadding; - private IntPtr m_ReturnAddress; -#else // UNIX_AMD64_ABI - private IntPtr m_returnBlockPadding; - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return sizeof(IntPtr); - } - private IntPtr m_alignmentPadding; - private IntPtr m_ReturnAddress; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(TransitionBlock); - } -#endif // UNIX_AMD64_ABI - -#elif TARGET_ARM - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } + // rcx is homed above the callee-saved registers (rdi, rsi, rbx, rbp, r12-r15) and the return address + return 9 * 8; #elif TARGET_ARM64 - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } - - public IntPtr m_alignmentPad; -#elif TARGET_WASM - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } + // x0 follows the callee-saved registers (fp, lr, x19-x28), the padding and x8 + return 14 * 8; +#elif TARGET_ARM + // r0 follows the callee-saved registers (r4-r11) and lr + return 9 * 4; #elif TARGET_LOONGARCH64 - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } - - public IntPtr m_alignmentPad; + // a0 follows the callee-saved registers (fp, ra, s0-s8) and the padding + return 12 * 8; #elif TARGET_RISCV64 - public ReturnBlock m_returnBlock; - public static unsafe int GetOffsetOfReturnValuesBlock() - { - return 0; - } - - public ArgumentRegisters m_argumentRegisters; - public static unsafe int GetOffsetOfArgumentRegisters() - { - return sizeof(ReturnBlock); - } - - public IntPtr m_alignmentPad; + // a0 follows the callee-saved registers (fp, ra, s1-s11, tp, gp) and the padding + return 16 * 8; +#elif TARGET_X86 + // ecx follows edx at the base of the transition frame + return 4; +#elif TARGET_WASM + return 4; #else -#error Portability problem -#endif - - // The transition block should define everything pushed by callee. The code assumes in number of places that - // end of the transition block is caller's stack pointer. - - public static unsafe byte GetOffsetOfArgs() - { - return (byte)sizeof(TransitionBlock); - } - - - public static bool IsStackArgumentOffset(int offset) - { - int ofsArgRegs = GetOffsetOfArgumentRegisters(); - - return offset >= (int)(ofsArgRegs + ArchitectureConstants.ARGUMENTREGISTERS_SIZE); - } - - public static bool IsArgumentRegisterOffset(int offset) - { - int ofsArgRegs = GetOffsetOfArgumentRegisters(); - - return offset >= ofsArgRegs && offset < (int)(ofsArgRegs + ArchitectureConstants.ARGUMENTREGISTERS_SIZE); - } - -#if !TARGET_X86 - public static unsafe int GetArgumentIndexFromOffset(int offset) - { - return ((offset - GetOffsetOfArgumentRegisters()) / IntPtr.Size); - } - - public static int GetStackArgumentIndexFromOffset(int offset) - { - return (offset - GetOffsetOfArgs()) / ArchitectureConstants.STACK_ELEM_SIZE; - } -#endif - -#if HAS_FLOAT_ARGUMENT_REGISTERS - public static bool IsFloatArgumentRegisterOffset(int offset) - { - return offset < 0; - } - - public static int GetOffsetOfFloatArgumentRegisters() - { - return -GetNegSpaceSize(); - } -#endif - - public static unsafe int GetNegSpaceSize() - { - int negSpaceSize = 0; -#if HAS_FLOAT_ARGUMENT_REGISTERS - negSpaceSize += sizeof(FloatArgumentRegisters); -#endif - return negSpaceSize; - } - - public static int GetThisOffset() - { - // This pointer is in the first argument register by default - int ret = TransitionBlock.GetOffsetOfArgumentRegisters(); - -#if TARGET_X86 - // x86 is special as always - ret += ArgumentRegisters.GetOffsetOfEcx(); +#error Unknown architecture! #endif - - return ret; } - - public const int InvalidOffset = -1; - }; + } } diff --git a/src/coreclr/nativeaot/Runtime/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/CMakeLists.txt index 9cbc88f1f7b408..14c1f7d15777b4 100644 --- a/src/coreclr/nativeaot/Runtime/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/CMakeLists.txt @@ -312,7 +312,7 @@ if(NOT FEATURE_PORTABLE_HELPERS) ${ARCH_SOURCES_DIR}/GcProbe.${ASM_SUFFIX} ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/MiscStubs.${ASM_SUFFIX} ${ARCH_SOURCES_DIR}/PInvoke.${ASM_SUFFIX} - ${ARCH_SOURCES_DIR}/UniversalTransition.${ASM_SUFFIX} + ${ARCH_SOURCES_DIR}/InterfaceDispatchSlow.${ASM_SUFFIX} ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/WriteBarriers.${ASM_SUFFIX} ) diff --git a/src/coreclr/nativeaot/Runtime/EHHelpers.cpp b/src/coreclr/nativeaot/Runtime/EHHelpers.cpp index 479cdea4c78f73..52eb50cc4a56ae 100644 --- a/src/coreclr/nativeaot/Runtime/EHHelpers.cpp +++ b/src/coreclr/nativeaot/Runtime/EHHelpers.cpp @@ -619,4 +619,25 @@ FCIMPL0(void, RhpFallbackFailFast) } FCIMPLEND +#ifndef FEATURE_PORTABLE_HELPERS + +// Called by the throw helpers (RhpThrowEx, RhpThrowExact, RhpRethrow and RhpThrowHwEx) to initialize the ExInfo +// in their frame and link it into the ExInfo chain of the thread. Returns the previously active ExInfo. +EXTERN_C ExInfo* FASTCALL RhpPushExInfo(ExInfo* pExInfo, PAL_LIMITED_CONTEXT* pContext, uint32_t kind) +{ + Thread* pThread = ThreadStore::GetCurrentThread(); + ExInfo* pActiveExInfo = pThread->GetCurExInfo(); + + pExInfo->m_pExContext = pContext; + pExInfo->m_exception = nullptr; + pExInfo->m_kind = (ExKind)kind; + pExInfo->m_passNumber = 1; + pExInfo->m_idxCurClause = UINT32_MAX; + pThread->PushExInfo(pExInfo); + + return pActiveExInfo; +} + +#endif // !FEATURE_PORTABLE_HELPERS + #endif // !DACCESS_COMPILE diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp index 01a02a503a875e..c1017a3a0f9040 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp @@ -32,9 +32,9 @@ #pragma warning(disable:4061) #if !defined(FEATURE_PORTABLE_HELPERS) // @TODO: these are (currently) only implemented in assembly helpers -EXTERN_C CODE_LOCATION ReturnFromUniversalTransitionTailCall; +EXTERN_C CODE_LOCATION ReturnFromInterfaceDispatchSlow; #if defined(TARGET_WINDOWS) && (defined(TARGET_AMD64) || defined(TARGET_ARM64)) -EXTERN_C CODE_LOCATION ReturnFromUniversalTransitionGuardedTailCall; +EXTERN_C CODE_LOCATION ReturnFromInterfaceDispatchSlowGuarded; #endif EXTERN_C CODE_LOCATION CallEHFunclet2; @@ -1366,70 +1366,81 @@ void StackFrameIterator::UnwindFuncletInvokeThunk() } // For a given target architecture, the layout of this structure must precisely match the -// stack frame layout used by the associated architecture-specific RhpUniversalTransition +// stack frame layout used by the associated architecture-specific RhpInterfaceDispatchSlow // implementation. -struct UniversalTransitionStackFrame +struct InterfaceDispatchSlowStackFrame { // In DAC builds, the "this" pointer refers to an object in the DAC host. #define GET_POINTER_TO_FIELD(_FieldName) \ - (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(UniversalTransitionStackFrame, this, _FieldName) + (PTR_uintptr_t)PTR_HOST_MEMBER_TADDR(InterfaceDispatchSlowStackFrame, this, _FieldName) #if defined(UNIX_AMD64_ABI) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the base of the TransitionBlock and the top of the StackPassedArgs. private: - Fp128 m_fpArgRegs[8]; // ChildSP+000 CallerSP-0D0 (0x80 bytes) (xmm0-xmm7) - uintptr_t m_returnBlock[2]; // ChildSP+080 CallerSP-050 (0x10 bytes) - uintptr_t m_intArgRegs[6]; // ChildSP+090 CallerSP-040 (0x30 bytes) (rdi,rsi,rcx,rdx,r8,r9) - uintptr_t m_alignmentPad; // ChildSP+0C0 CallerSP-010 (0x8 bytes) - uintptr_t m_callerRetaddr; // ChildSP+0C8 CallerSP-008 (0x8 bytes) - uintptr_t m_stackPassedArgs[1]; // ChildSP+0D0 CallerSP+000 (unknown size) + Fp128 m_fpArgRegs[8]; // ChildSP+000 CallerSP-0F0 (0x80 bytes) (xmm0-xmm7) + uintptr_t m_alignmentPad; // ChildSP+080 CallerSP-070 (0x8 bytes) + uintptr_t m_intArgRegs[6]; // ChildSP+088 CallerSP-068 (0x30 bytes) (rdi,rsi,rdx,rcx,r8,r9) + uintptr_t m_calleeSavedRegs[6]; // ChildSP+0B8 CallerSP-038 (0x30 bytes) (r12,r13,r14,r15,rbx,rbp) + uintptr_t m_callerRetaddr; // ChildSP+0E8 CallerSP-008 (0x8 bytes) + uintptr_t m_stackPassedArgs[1]; // ChildSP+0F0 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_callerRetaddr); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_intArgRegs[0]); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - // RhpUniversalTransition does not touch any non-volatile state on amd64. - UNREFERENCED_PARAMETER(pRegisterSet); + pRegisterSet->pR12 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pR13 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pR14 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pR15 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pRbx = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pRbp = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) { pRegisterSet->pRdi = GET_POINTER_TO_FIELD(m_intArgRegs[0]); pRegisterSet->pRsi = GET_POINTER_TO_FIELD(m_intArgRegs[1]); - pRegisterSet->pRcx = GET_POINTER_TO_FIELD(m_intArgRegs[2]); - pRegisterSet->pRdx = GET_POINTER_TO_FIELD(m_intArgRegs[3]); + pRegisterSet->pRdx = GET_POINTER_TO_FIELD(m_intArgRegs[2]); + pRegisterSet->pRcx = GET_POINTER_TO_FIELD(m_intArgRegs[3]); pRegisterSet->pR8 = GET_POINTER_TO_FIELD(m_intArgRegs[4]); pRegisterSet->pR9 = GET_POINTER_TO_FIELD(m_intArgRegs[5]); } #elif defined(TARGET_AMD64) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the homed argument registers and the top of the StackPassedArgs. private: - uintptr_t m_calleeArgumentHomes[4]; // ChildSP+000 CallerSP-080 (0x20 bytes) - Fp128 m_fpArgRegs[4]; // ChildSP+020 CallerSP-060 (0x40 bytes) (xmm0-xmm3) - uintptr_t m_returnBlock[2]; // ChildSP+060 CallerSP-020 (0x10 bytes) - uintptr_t m_alignmentPad; // ChildSP+070 CallerSP-010 (0x8 bytes) - uintptr_t m_callerRetaddr; // ChildSP+078 CallerSP-008 (0x8 bytes) - uintptr_t m_intArgRegs[4]; // ChildSP+080 CallerSP+000 (0x20 bytes) (rcx,rdx,r8,r9) - uintptr_t m_stackPassedArgs[1]; // ChildSP+0a0 CallerSP+020 (unknown size) + uintptr_t m_calleeArgumentHomes[4]; // ChildSP+000 CallerSP-0B0 (0x20 bytes) + Fp128 m_fpArgRegs[4]; // ChildSP+020 CallerSP-090 (0x40 bytes) (xmm0-xmm3) + uintptr_t m_alignmentPad; // ChildSP+060 CallerSP-050 (0x8 bytes) + uintptr_t m_calleeSavedRegs[8]; // ChildSP+068 CallerSP-048 (0x40 bytes) (rdi,rsi,rbx,rbp,r12-r15) + uintptr_t m_callerRetaddr; // ChildSP+0A8 CallerSP-008 (0x8 bytes) + uintptr_t m_intArgRegs[4]; // ChildSP+0B0 CallerSP+000 (0x20 bytes) (rcx,rdx,r8,r9) + uintptr_t m_stackPassedArgs[1]; // ChildSP+0D0 CallerSP+020 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_intArgRegs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_callerRetaddr); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + // The lower bound must be below CallerSP, where the argument registers are homed. + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_callerRetaddr); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - // RhpUniversalTransition does not touch any non-volatile state on amd64. - UNREFERENCED_PARAMETER(pRegisterSet); + pRegisterSet->pRdi = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pRsi = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pRbx = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pRbp = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pR12 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pR13 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); + pRegisterSet->pR14 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[6]); + pRegisterSet->pR15 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[7]); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) @@ -1442,24 +1453,31 @@ struct UniversalTransitionStackFrame #elif defined(TARGET_ARM) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the base of the TransitionBlock and the top of the StackPassedArgs. private: - uintptr_t m_pushedR11; // ChildSP+000 CallerSP-078 (0x4 bytes) (r11) - uintptr_t m_pushedLR; // ChildSP+004 CallerSP-074 (0x4 bytes) (lr) - uint64_t m_fpArgRegs[8]; // ChildSP+008 CallerSP-070 (0x40 bytes) (d0-d7) - uint64_t m_returnBlock[4]; // ChildSP+048 CallerSP-030 (0x20 bytes) + uint64_t m_fpArgRegs[8]; // ChildSP+000 CallerSP-078 (0x40 bytes) (d0-d7) + uintptr_t m_alignmentPad; // ChildSP+040 CallerSP-038 (0x4 bytes) + uintptr_t m_calleeSavedRegs[8]; // ChildSP+044 CallerSP-034 (0x20 bytes) (r4-r11) + uintptr_t m_pushedLR; // ChildSP+064 CallerSP-014 (0x4 bytes) (lr) uintptr_t m_intArgRegs[4]; // ChildSP+068 CallerSP-010 (0x10 bytes) (r0-r3) uintptr_t m_stackPassedArgs[1]; // ChildSP+078 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_pushedLR); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - pRegisterSet->pR11 = GET_POINTER_TO_FIELD(m_pushedR11); + pRegisterSet->pR4 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pR5 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pR6 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pR7 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pR8 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pR9 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); + pRegisterSet->pR10 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[6]); + pRegisterSet->pR11 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[7]); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) @@ -1475,11 +1493,11 @@ struct UniversalTransitionStackFrame // Conservative GC reporting must be applied to everything between the base of the // IntArgRegs and the top of the StackPassedArgs. private: - uintptr_t m_intArgRegs[2]; // ChildSP+000 CallerSP-018 (0x8 bytes) (edx,ecx) - uintptr_t m_returnBlock[2]; // ChildSP+008 CallerSP-010 (0x8 bytes) - uintptr_t m_pushedEBP; // ChildSP+010 CallerSP-008 (0x4 bytes) - uintptr_t m_callerRetaddr; // ChildSP+014 CallerSP-004 (0x4 bytes) - uintptr_t m_stackPassedArgs[1]; // ChildSP+018 CallerSP+000 (unknown size) + uintptr_t m_intArgRegs[2]; // ChildSP+000 CallerSP-014 (0x8 bytes) (edx,ecx) + uintptr_t m_dispatchCell; // ChildSP+008 CallerSP-00C (0x4 bytes) + uintptr_t m_pushedEBP; // ChildSP+00C CallerSP-008 (0x4 bytes) + uintptr_t m_callerRetaddr; // ChildSP+010 CallerSP-004 (0x4 bytes) + uintptr_t m_stackPassedArgs[1]; // ChildSP+014 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } @@ -1499,25 +1517,36 @@ struct UniversalTransitionStackFrame #elif defined(TARGET_ARM64) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the base of the TransitionBlock and the top of the StackPassedArgs. private: - uintptr_t m_pushedFP; // ChildSP+000 CallerSP-100 (0x08 bytes) (fp) - uintptr_t m_pushedLR; // ChildSP+008 CallerSP-0F8 (0x08 bytes) (lr) - Fp128 m_fpArgRegs[8]; // ChildSP+010 CallerSP-0F0 (0x80 bytes) (q0-q7) - uintptr_t m_returnBlock[4]; // ChildSP+090 CallerSP-070 (0x20 bytes) - uintptr_t m_intArgRegs[9]; // ChildSP+0B0 CallerSP-050 (0x48 bytes) (x0-x8) - uintptr_t m_alignmentPad; // ChildSP+0F8 CallerSP-008 (0x08 bytes) - uintptr_t m_stackPassedArgs[1]; // ChildSP+100 CallerSP+000 (unknown size) + Fp128 m_fpArgRegs[8]; // ChildSP+000 CallerSP-130 (0x80 bytes) (q0-q7) + uintptr_t m_pushedFP; // ChildSP+080 CallerSP-0B0 (0x08 bytes) (fp) + uintptr_t m_pushedLR; // ChildSP+088 CallerSP-0A8 (0x08 bytes) (lr) + uintptr_t m_calleeSavedRegs[10]; // ChildSP+090 CallerSP-0A0 (0x50 bytes) (x19-x28) + uintptr_t m_alignmentPad; // ChildSP+0E0 CallerSP-050 (0x08 bytes) + uintptr_t m_x8; // ChildSP+0E8 CallerSP-048 (0x08 bytes) (x8) + uintptr_t m_intArgRegs[8]; // ChildSP+0F0 CallerSP-040 (0x40 bytes) (x0-x7) + uintptr_t m_stackPassedArgs[1]; // ChildSP+130 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_pushedLR); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_pushedFP); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pX19 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pX20 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pX21 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pX22 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pX23 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pX24 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); + pRegisterSet->pX25 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[6]); + pRegisterSet->pX26 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[7]); + pRegisterSet->pX27 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[8]); + pRegisterSet->pX28 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[9]); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) @@ -1530,29 +1559,39 @@ struct UniversalTransitionStackFrame pRegisterSet->pX5 = GET_POINTER_TO_FIELD(m_intArgRegs[5]); pRegisterSet->pX6 = GET_POINTER_TO_FIELD(m_intArgRegs[6]); pRegisterSet->pX7 = GET_POINTER_TO_FIELD(m_intArgRegs[7]); - pRegisterSet->pX8 = GET_POINTER_TO_FIELD(m_intArgRegs[8]); + pRegisterSet->pX8 = GET_POINTER_TO_FIELD(m_x8); } #elif defined(TARGET_LOONGARCH64) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the base of the TransitionBlock and the top of the StackPassedArgs. private: - uintptr_t m_pushedFP; // ChildSP+000 CallerSP-0A0 (0x08 bytes) (fp) - uintptr_t m_pushedRA; // ChildSP+008 CallerSP-098 (0x08 bytes) (ra) - uint64_t m_fpArgRegs[8]; // ChildSP+010 CallerSP-090 (0x40 bytes) (fa0-fa7) - uintptr_t m_returnBlock[2]; // ChildSP+050 CallerSP-050 (0x10 bytes) - uintptr_t m_intArgRegs[8]; // ChildSP+060 CallerSP-040 (0x40 bytes) (a0-a7) - uintptr_t m_stackPassedArgs[1]; // ChildSP+0A0 CallerSP+000 (unknown size) + uint64_t m_fpArgRegs[8]; // ChildSP+000 CallerSP-0E0 (0x40 bytes) (fa0-fa7) + uintptr_t m_pushedFP; // ChildSP+040 CallerSP-0A0 (0x08 bytes) (fp) + uintptr_t m_pushedRA; // ChildSP+048 CallerSP-098 (0x08 bytes) (ra) + uintptr_t m_calleeSavedRegs[9]; // ChildSP+050 CallerSP-090 (0x48 bytes) (s0-s8) + uintptr_t m_alignmentPad; // ChildSP+098 CallerSP-048 (0x08 bytes) + uintptr_t m_intArgRegs[8]; // ChildSP+0A0 CallerSP-040 (0x40 bytes) (a0-a7) + uintptr_t m_stackPassedArgs[1]; // ChildSP+0E0 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_pushedRA); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_pushedFP); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pR23 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pR24 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pR25 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pR26 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pR27 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pR28 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); + pRegisterSet->pR29 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[6]); + pRegisterSet->pR30 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[7]); + pRegisterSet->pR31 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[8]); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) @@ -1569,24 +1608,40 @@ struct UniversalTransitionStackFrame #elif defined(TARGET_RISCV64) - // Conservative GC reporting must be applied to everything between the base of the - // ReturnBlock and the top of the StackPassedArgs. + // The frame created by PROLOG_WITH_TRANSITION_BLOCK. Conservative GC reporting must be applied to everything + // between the base of the TransitionBlock and the top of the StackPassedArgs. private: - uintptr_t m_pushedFP; // ChildSP+000 CallerSP-0A0 (0x08 bytes) (fp) - uintptr_t m_pushedRA; // ChildSP+008 CallerSP-098 (0x08 bytes) (ra) - uint64_t m_fpArgRegs[8]; // ChildSP+010 CallerSP-090 (0x40 bytes) (fa0-fa7) - uintptr_t m_returnBlock[2]; // ChildSP+050 CallerSP-050 (0x10 bytes) - uintptr_t m_intArgRegs[8]; // ChildSP+060 CallerSP-040 (0x40 bytes) (a0-a7) - uintptr_t m_stackPassedArgs[1]; // ChildSP+0A0 CallerSP+000 (unknown size) + uint64_t m_fpArgRegs[8]; // ChildSP+000 CallerSP-100 (0x40 bytes) (fa0-fa7) + uintptr_t m_pushedFP; // ChildSP+040 CallerSP-0C0 (0x08 bytes) (fp) + uintptr_t m_pushedRA; // ChildSP+048 CallerSP-0B8 (0x08 bytes) (ra) + uintptr_t m_calleeSavedRegs[11]; // ChildSP+050 CallerSP-0B0 (0x58 bytes) (s1-s11) + uintptr_t m_pushedTP; // ChildSP+0A8 CallerSP-058 (0x08 bytes) (tp) + uintptr_t m_pushedGP; // ChildSP+0B0 CallerSP-050 (0x08 bytes) (gp) + uintptr_t m_alignmentPad; // ChildSP+0B8 CallerSP-048 (0x08 bytes) + uintptr_t m_intArgRegs[8]; // ChildSP+0C0 CallerSP-040 (0x40 bytes) (a0-a7) + uintptr_t m_stackPassedArgs[1]; // ChildSP+100 CallerSP+000 (unknown size) public: PTR_uintptr_t get_CallerSP() { return GET_POINTER_TO_FIELD(m_stackPassedArgs[0]); } PTR_uintptr_t get_AddressOfPushedCallerIP() { return GET_POINTER_TO_FIELD(m_pushedRA); } - PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_returnBlock[0]); } + PTR_uintptr_t get_LowerBoundForConservativeReporting() { return GET_POINTER_TO_FIELD(m_pushedFP); } void UnwindNonVolatileRegisters(REGDISPLAY * pRegisterSet) { - pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pFP = GET_POINTER_TO_FIELD(m_pushedFP); + pRegisterSet->pS1 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[0]); + pRegisterSet->pS2 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[1]); + pRegisterSet->pS3 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[2]); + pRegisterSet->pS4 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[3]); + pRegisterSet->pS5 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[4]); + pRegisterSet->pS6 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[5]); + pRegisterSet->pS7 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[6]); + pRegisterSet->pS8 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[7]); + pRegisterSet->pS9 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[8]); + pRegisterSet->pS10 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[9]); + pRegisterSet->pS11 = GET_POINTER_TO_FIELD(m_calleeSavedRegs[10]); + pRegisterSet->pTP = GET_POINTER_TO_FIELD(m_pushedTP); + pRegisterSet->pGP = GET_POINTER_TO_FIELD(m_pushedGP); } void UnwindVolatileArgRegisters(REGDISPLAY * pRegisterSet) @@ -1629,7 +1684,7 @@ struct UniversalTransitionStackFrame }; -typedef DPTR(UniversalTransitionStackFrame) PTR_UniversalTransitionStackFrame; +typedef DPTR(InterfaceDispatchSlowStackFrame) PTR_InterfaceDispatchSlowStackFrame; // NOTE: This function always publishes a non-NULL conservative stack range lower bound. // @@ -1637,21 +1692,21 @@ typedef DPTR(UniversalTransitionStackFrame) PTR_UniversalTransitionStackFrame; // of stack-passed argument space to be callee-popped before control returns (or unwinds) to the // callsite. Since the callsite signature (and thus the amount of callee-popped space) is unknown, // the recovered SP does not account for the callee-popped space is therefore "wrong" for the -// purposes of unwind. This implies that any x86 function which calls into RhpUniversalTransition +// purposes of unwind. This implies that any x86 function which calls into RhpInterfaceDispatchSlow // must have a frame pointer to ensure that the incorrect SP value is ignored and does not break the // unwind. -void StackFrameIterator::UnwindUniversalTransitionThunk() +void StackFrameIterator::UnwindInterfaceDispatchSlowThunk() { ASSERT((m_dwFlags & MethodStateCalculated) == 0); #if defined(FEATURE_PORTABLE_HELPERS) // @TODO: Corresponding helper code is only defined in assembly code return; #else // defined(FEATURE_PORTABLE_HELPERS) - ASSERT(CategorizeUnadjustedReturnAddress(m_ControlPC) == InUniversalTransitionThunk); + ASSERT(CategorizeUnadjustedReturnAddress(m_ControlPC) == InInterfaceDispatchSlowThunk); - // The current PC is within RhpUniversalTransition, so establish a view of the surrounding stack frame. + // The current PC is within RhpInterfaceDispatchSlow, so establish a view of the surrounding stack frame. // NOTE: In DAC builds, the pointer will refer to a newly constructed object in the DAC host. - UniversalTransitionStackFrame * stackFrame = (PTR_UniversalTransitionStackFrame)m_RegDisplay.SP; + InterfaceDispatchSlowStackFrame * stackFrame = (PTR_InterfaceDispatchSlowStackFrame)m_RegDisplay.SP; stackFrame->UnwindNonVolatileRegisters(&m_RegDisplay); stackFrame->UnwindVolatileArgRegisters(&m_RegDisplay); @@ -2073,9 +2128,9 @@ void StackFrameIterator::UnwindNonEHThunkSequence() { ASSERT(m_pConservativeStackRangeLowerBound == NULL); - if (category == InUniversalTransitionThunk) + if (category == InInterfaceDispatchSlowThunk) { - UnwindUniversalTransitionThunk(); + UnwindInterfaceDispatchSlowThunk(); ASSERT(m_pConservativeStackRangeLowerBound != NULL); } else @@ -2150,7 +2205,7 @@ void StackFrameIterator::PrepareToYieldFrame() // flowed into a managed callsite which called into the runtime. The runtime has no // knowledge of the callsite signature in these cases, and unwind through these callsites // is only possible via the associated assembly thunk (e.g., the ManagedCalloutThunk or - // UniversalTransitionThunk). + // RhpInterfaceDispatchSlow). // // The iterator is currently positioned on the managed frame which contains the callsite of // interest. The lower bound of the argument set was already computed while unwinding @@ -2292,7 +2347,7 @@ bool StackFrameIterator::IsNonEHThunk(ReturnAddressCategory category) { default: return false; - case InUniversalTransitionThunk: + case InInterfaceDispatchSlowThunk: return true; } } @@ -2369,13 +2424,13 @@ StackFrameIterator::ReturnAddressCategory StackFrameIterator::CategorizeUnadjust return InManagedCode; #else // defined(FEATURE_PORTABLE_HELPERS) - if (EQUALS_RETURN_ADDRESS(returnAddress, ReturnFromUniversalTransitionTailCall) + if (EQUALS_RETURN_ADDRESS(returnAddress, ReturnFromInterfaceDispatchSlow) #if defined(TARGET_WINDOWS) && (defined(TARGET_AMD64) || defined(TARGET_ARM64)) - || EQUALS_RETURN_ADDRESS(returnAddress, ReturnFromUniversalTransitionGuardedTailCall) + || EQUALS_RETURN_ADDRESS(returnAddress, ReturnFromInterfaceDispatchSlowGuarded) #endif ) { - return InUniversalTransitionThunk; + return InInterfaceDispatchSlowThunk; } if (EQUALS_RETURN_ADDRESS(returnAddress, RhpThrowImpl2) || diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.h b/src/coreclr/nativeaot/Runtime/StackFrameIterator.h index 3f124feb7b4455..9c27e689a2d718 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.h +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.h @@ -95,7 +95,7 @@ class StackFrameIterator // If our control PC indicates that we're in the universal transition thunk that we use to generically // dispatch arbitrary managed calls, then handle the stack walk specially. // NOTE: This function always publishes a non-NULL conservative stack range lower bound. - void UnwindUniversalTransitionThunk(); + void UnwindInterfaceDispatchSlowThunk(); void EnterInitialInvalidState(Thread * pThreadToWalk); @@ -130,7 +130,7 @@ class StackFrameIterator InThrowSiteThunk, InFuncletInvokeThunk, InFilterFuncletInvokeThunk, - InUniversalTransitionThunk, + InInterfaceDispatchSlowThunk, }; static ReturnAddressCategory CategorizeUnadjustedReturnAddress(PTR_VOID returnAddress); diff --git a/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp b/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp index aabf45392313ae..1bd326ecd62352 100644 --- a/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp +++ b/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp @@ -14,7 +14,7 @@ #ifdef TRASH_SAVED_ARGUMENT_REGISTERS // -// Define tables of predictable distinguished values that RhpUniversalTransition can use to +// Define tables of predictable distinguished values that RhpInterfaceDispatchSlow can use to // trash argument registers after they have been saved into the transition frame. // // Trashing these registers is a testability aid that makes it easier to detect bugs where diff --git a/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc b/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc index 8427928cddb273..81dd61235d9cb5 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc +++ b/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc @@ -11,6 +11,9 @@ include AsmOffsets.inc ; generated by the build from AsmOffsets.cpp include amd64\AsmMacros_Windows.inc +;; Size of the argument home area that PROLOG_WITH_TRANSITION_BLOCK reserves for its callees +SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES equ 20h + ;; ;; __declspec(thread) tls_CurrentThread ;; @@ -141,6 +144,7 @@ EXTERN RhExceptionHandling_FailedAllocation : PROC EXTERN RhThrowHwEx : PROC EXTERN RhThrowEx : PROC EXTERN RhRethrow : PROC +EXTERN RhpPushExInfo : PROC EXTERN RhpGcPoll2 : PROC EXTERN RhpTrapThreads : DWORD diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S index 0f4d768c5b48fc..087a87709b3fa1 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S @@ -122,8 +122,7 @@ LOCAL_LABEL(CacheMiss\@): mov rcx, [rsp - 8] // DispatchCellReg contains indirection cell address - lea r10, [C_VAR(RhpCidResolve)] - jmp C_FUNC(RhpUniversalTransitionTailCall) + jmp C_FUNC(RhpInterfaceDispatchSlow) .endif LEAF_END \DispatchName, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm index 722e67b3e2b128..0f1a57731fa521 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm @@ -6,8 +6,8 @@ include AsmMacros.inc EXTERN RhpCidResolve : PROC EXTERN RhpCidResolve_Worker : PROC -EXTERN RhpUniversalTransitionTailCall : PROC -EXTERN RhpUniversalTransitionGuardedTailCall : PROC +EXTERN RhpInterfaceDispatchSlow : PROC +EXTERN RhpInterfaceDispatchSlowGuarded : PROC EXTERN g_pDispatchCache : QWORD @@ -153,11 +153,10 @@ else SlowPath: ;; DispatchCellReg contains indirection cell address - lea r10, RhpCidResolve if Guarded ne 0 - jmp RhpUniversalTransitionGuardedTailCall + jmp RhpInterfaceDispatchSlowGuarded else - jmp RhpUniversalTransitionTailCall + jmp RhpInterfaceDispatchSlow endif endif diff --git a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S index 5166d17801e32a..d0a491b07dcb61 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S @@ -44,29 +44,15 @@ NESTED_ENTRY RhpThrowHwEx, _TEXT, NoHandler END_PROLOGUE - mov rbx, rdi - INLINE_GETTHREAD - mov rdi, rbx + mov ebx, edi // ebx <- exception code - mov rsi, rsp // rsi <- ExInfo* + mov rdi, rsp // rdi <- ExInfo* + lea rsi, [rsp + rsp_offsetof_Context] // rsi <- PAL_LIMITED_CONTEXT* + mov edx, 2 // edx <- ExKind.HardwareFault + call C_FUNC(RhpPushExInfo) - xor rdx, rdx - mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null - mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass - mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF - mov byte ptr [rsi + OFFSETOF__ExInfo__m_kind], 2 // ExKind.HardwareFault - - // link the ExInfo into the thread's ExInfo chain - mov rdx, [rax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdx // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT* - mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext - - // rdi still contains the exception code - // rsi contains the address of the ExInfo + mov edi, ebx // edi <- exception code + mov rsi, rsp // rsi <- ExInfo* call EXTERNAL_C_FUNC(RhThrowHwEx) ALTERNATE_ENTRY RhpThrowHwEx2 @@ -141,39 +127,15 @@ NESTED_ENTRY RhpThrowImpl, _TEXT, NoHandler END_PROLOGUE - mov rbx, rdi - mov r12, r8 // Save ExKind - INLINE_GETTHREAD - mov rdi, rbx - - lea rbx, [rsp + rsp_offsetof_Context + SIZEOF__PAL_LIMITED_CONTEXT + 0x8] // rbx <- addr of return address + mov rbx, rdi // rbx <- exception object - // There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - // address could have been hijacked when we were in that C# code and we must remove the hijack and - // reflect the correct return address in our exception context record. The other throw helpers don't - // need this because they cannot be tail-called from C#. - INLINE_THREAD_UNHIJACK rax, rcx, rsi // trashes RCX, RSI - mov rsi, [rbx] // rdx <- return address - mov [rsp + rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP], rsi // set 'faulting' IP after unhijack + mov rdi, rsp // rdi <- ExInfo* + lea rsi, [rsp + rsp_offsetof_Context] // rsi <- PAL_LIMITED_CONTEXT* + mov edx, r8d // edx <- ExKind + call C_FUNC(RhpPushExInfo) + mov rdi, rbx // rdi <- exception object mov rsi, rsp // rsi <- ExInfo* - - mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null - mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass - mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF - mov byte ptr [rsi + OFFSETOF__ExInfo__m_kind], r12b // ExKind - - // link the ExInfo into the thread's ExInfo chain - mov rdx, [rax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdx // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT* - mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext - - // rdi still contains the exception object - // rsi contains the address of the ExInfo call EXTERNAL_C_FUNC(RhThrowEx) ALTERNATE_ENTRY RhpThrowImpl2 @@ -224,27 +186,13 @@ NESTED_ENTRY RhpRethrow, _TEXT, NoHandler END_PROLOGUE - INLINE_GETTHREAD + mov rdi, rsp // rdi <- ExInfo* + lea rsi, [rsp + rsp_offsetof_Context] // rsi <- PAL_LIMITED_CONTEXT* + xor edx, edx // edx <- ExKind.None + call C_FUNC(RhpPushExInfo) + mov rdi, rax // rdi <- currently active ExInfo mov rsi, rsp // rsi <- ExInfo* - - mov [rsi + OFFSETOF__ExInfo__m_exception], rdx // init the exception object to null - mov byte ptr [rsi + OFFSETOF__ExInfo__m_passNumber], 1 // init to the first pass - mov dword ptr [rsi + OFFSETOF__ExInfo__m_idxCurClause], 0xFFFFFFFF - mov byte ptr [rsi + OFFSETOF__ExInfo__m_kind], 0 // init to a deterministic value (ExKind.None) - - - // link the ExInfo into the thread's ExInfo chain - mov rdi, [rax + OFFSETOF__Thread__m_pExInfoStackHead] // rdi <- currently active ExInfo - mov [rsi + OFFSETOF__ExInfo__m_pPrevExInfo], rdi // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rsi // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - lea rdx, [rsp + rsp_offsetof_Context] // rdx <- PAL_LIMITED_CONTEXT* - mov [rsi + OFFSETOF__ExInfo__m_pExContext], rdx // init ExInfo.m_pExContext - - // rdi contains the currently active ExInfo - // rsi contains the address of the new ExInfo call EXTERNAL_C_FUNC(RhRethrow) ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm index 59d1f8b7b1c8f4..a635ade026bc5e 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm @@ -75,27 +75,15 @@ ALTERNATE_ENTRY RhpThrowHwExGEHCONT ; this needs to be an EHCONT target since we END_PROLOGUE - INLINE_GETTHREAD rax, rbx ;; rax <- Thread*, rbx is trashed + mov ebx, ecx ;; ebx <- exception code - lea rdx, [rsp + rsp_offsetof_ExInfo] ;; rdx <- ExInfo* + lea rcx, [rsp + rsp_offsetof_ExInfo] ;; rcx <- ExInfo* + lea rdx, [rsp + rsp_offsetof_Context] ;; rdx <- PAL_LIMITED_CONTEXT* + mov r8d, 2 ;; r8d <- ExKind.HardwareFault + call RhpPushExInfo - xor r8, r8 - mov [rdx + OFFSETOF__ExInfo__m_exception], r8 ;; init the exception object to null - mov byte ptr [rdx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [rdx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [rdx + OFFSETOF__ExInfo__m_kind], 2 ;; ExKind.HardwareFault - - ;; link the ExInfo into the thread's ExInfo chain - mov r8, [rax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [rdx + OFFSETOF__ExInfo__m_pPrevExInfo], r8 ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rdx ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - lea r8, [rsp + rsp_offsetof_Context] ;; r8 <- PAL_LIMITED_CONTEXT* - mov [rdx + OFFSETOF__ExInfo__m_pExContext], r8 ;; init ExInfo.m_pExContext - - ;; rcx still contains the exception code - ;; rdx contains the address of the ExInfo + mov ecx, ebx ;; ecx <- exception code + lea rdx, [rsp + rsp_offsetof_ExInfo] ;; rdx <- ExInfo* call RhThrowHwEx ALTERNATE_ENTRY RhpThrowHwEx2 @@ -189,36 +177,15 @@ NESTED_ENTRY RhpThrowImpl, _TEXT END_PROLOGUE - INLINE_GETTHREAD rax, rbx ;; rax <- Thread*, rbx is trashed - - lea rbx, [rsp + rsp_offsetof_Context + SIZEOF__PAL_LIMITED_CONTEXT + 8h] ;; rbx <- addr of return address + mov rbx, rcx ;; rbx <- exception object - ;; There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - ;; address could have been hijacked when we were in that C# code and we must remove the hijack and - ;; reflect the correct return address in our exception context record. The other throw helpers don't - ;; need this because they cannot be tail-called from C#. - INLINE_THREAD_UNHIJACK rax, r10, rdx ;; trashes R10, RDX - mov rdx, [rbx] ;; rdx <- return address - mov [rsp + rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP], rdx ;; set 'faulting' IP after unhijack + lea rcx, [rsp + rsp_offsetof_ExInfo] ;; rcx <- ExInfo* + lea rdx, [rsp + rsp_offsetof_Context] ;; rdx <- PAL_LIMITED_CONTEXT* + mov r8d, r9d ;; r8d <- ExKind + call RhpPushExInfo + mov rcx, rbx ;; rcx <- exception object lea rdx, [rsp + rsp_offsetof_ExInfo] ;; rdx <- ExInfo* - - mov [rdx + OFFSETOF__ExInfo__m_exception], r8 ;; init the exception object to null - mov byte ptr [rdx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [rdx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [rdx + OFFSETOF__ExInfo__m_kind], r9b ;; ExKind (from r9b) - - ;; link the ExInfo into the thread's ExInfo chain - mov r8, [rax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [rdx + OFFSETOF__ExInfo__m_pPrevExInfo], r8 ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rdx ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - lea r8, [rsp + rsp_offsetof_Context] ;; r8 <- PAL_LIMITED_CONTEXT* - mov [rdx + OFFSETOF__ExInfo__m_pExContext], r8 ;; init ExInfo.m_pExContext - - ;; rcx still contains the exception object - ;; rdx contains the address of the ExInfo call RhThrowEx ALTERNATE_ENTRY RhpThrowImpl2 @@ -289,27 +256,13 @@ NESTED_ENTRY RhpRethrow, _TEXT END_PROLOGUE - INLINE_GETTHREAD rax, rbx ;; rax <- Thread*, rbx is trashed + lea rcx, [rsp + rsp_offsetof_ExInfo] ;; rcx <- ExInfo* + lea rdx, [rsp + rsp_offsetof_Context] ;; rdx <- PAL_LIMITED_CONTEXT* + xor r8d, r8d ;; r8d <- ExKind.None + call RhpPushExInfo + mov rcx, rax ;; rcx <- currently active ExInfo lea rdx, [rsp + rsp_offsetof_ExInfo] ;; rdx <- ExInfo* - - mov [rdx + OFFSETOF__ExInfo__m_exception], r8 ;; init the exception object to null - mov byte ptr [rdx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [rdx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [rdx + OFFSETOF__ExInfo__m_kind], 0 ;; init to a deterministic value (ExKind.None) - - - ;; link the ExInfo into the thread's ExInfo chain - mov rcx, [rax + OFFSETOF__Thread__m_pExInfoStackHead] ;; rcx <- currently active ExInfo - mov [rdx + OFFSETOF__ExInfo__m_pPrevExInfo], rcx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [rax + OFFSETOF__Thread__m_pExInfoStackHead], rdx ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - lea r8, [rsp + rsp_offsetof_Context] ;; r8 <- PAL_LIMITED_CONTEXT* - mov [rdx + OFFSETOF__ExInfo__m_pExContext], r8 ;; init ExInfo.m_pExContext - - ;; rcx contains the currently active ExInfo - ;; rdx contains the address of the new ExInfo call RhRethrow ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S new file mode 100644 index 00000000000000..54e6f9491337cd --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.intel_syntax noprefix +#include + +#ifdef _DEBUG +#define TRASH_SAVED_ARGUMENT_REGISTERS 1 +#endif + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +// interface dispatch cell and then jumps to the resolved target with the original arguments. +// +// Inputs: +// rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7, stack space: arguments as normal +// r11: The interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// +NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + // Before calling out, trash all of the argument registers except the ones (rdi, rsi) that + // hold outgoing arguments. All of these registers have been saved to the transition + // frame, and the code at the call target is required to use only the transition frame + // copies when dispatching this call to the eventual callee. + movsd xmm0, [C_VAR(RhpFpTrashValues) + 0x0] + movsd xmm1, [C_VAR(RhpFpTrashValues) + 0x8] + movsd xmm2, [C_VAR(RhpFpTrashValues) + 0x10] + movsd xmm3, [C_VAR(RhpFpTrashValues) + 0x18] + movsd xmm4, [C_VAR(RhpFpTrashValues) + 0x20] + movsd xmm5, [C_VAR(RhpFpTrashValues) + 0x28] + movsd xmm6, [C_VAR(RhpFpTrashValues) + 0x30] + movsd xmm7, [C_VAR(RhpFpTrashValues) + 0x38] + mov rcx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x10] + mov rdx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x18] + mov r8, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x20] + mov r9, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x28] +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + + lea rdi, [rsp + __PWTB_TransitionBlock] + mov rsi, r11 + call C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + TAILJMP_RAX + +NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm new file mode 100644 index 00000000000000..de0e12e640e726 --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm @@ -0,0 +1,81 @@ +;; Licensed to the .NET Foundation under one or more agreements. +;; The .NET Foundation licenses this file to you under the MIT license. + +include AsmMacros.inc + +ifdef _DEBUG +TRASH_SAVED_ARGUMENT_REGISTERS equ 1 +else +TRASH_SAVED_ARGUMENT_REGISTERS equ 0 +endif + +if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 +EXTERN RhpIntegerTrashValues : QWORD +EXTERN RhpFpTrashValues : QWORD +endif ;; TRASH_SAVED_ARGUMENT_REGISTERS + +EXTERN RhpCidResolve : PROC +EXTERN __guard_check_icall_fptr : QWORD + +; +; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded +; +; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +; interface dispatch cell and then jumps to the resolved target with the original arguments. The guarded variant +; validates the target with Control Flow Guard first. +; +; Inputs: +; rcx, rdx, r8, r9, xmm0-xmm3, stack space: arguments as normal +; r11: The interface dispatch cell +; +; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +; the argument registers homed by the TransitionBlock prolog and the stack passed arguments conservatively. +; +INTERFACE_DISPATCH_SLOW macro FunctionName, ValidateTarget + +NESTED_ENTRY Rhp&FunctionName, _TEXT + + PROLOG_WITH_TRANSITION_BLOCK + +if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 + + ; Before calling out, trash all of the argument registers except the ones (rcx, rdx) that + ; hold outgoing arguments. All of these registers have been saved to the transition + ; frame, and the code at the call target is required to use only the transition frame + ; copies when dispatching this call to the eventual callee. + + movsd xmm0, mmword ptr [RhpFpTrashValues + 0h] + movsd xmm1, mmword ptr [RhpFpTrashValues + 8h] + movsd xmm2, mmword ptr [RhpFpTrashValues + 10h] + movsd xmm3, mmword ptr [RhpFpTrashValues + 18h] + + mov r8, qword ptr [RhpIntegerTrashValues + 10h] + mov r9, qword ptr [RhpIntegerTrashValues + 18h] + +endif ; TRASH_SAVED_ARGUMENT_REGISTERS + + lea rcx, [rsp + __PWTB_TransitionBlock] + mov rdx, r11 + call RhpCidResolve + +ALTERNATE_ENTRY ReturnFrom&FunctionName + +if ValidateTarget ne 0 + ; Validate the target address using Control Flow Guard before tail-calling it. + ; The validator takes the target in rcx and preserves rcx, r10, and all float registers. + mov rcx, rax + call [__guard_check_icall_fptr] + mov rax, rcx +endif ; ValidateTarget + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + TAILJMP_RAX + +NESTED_END Rhp&FunctionName, _TEXT + + endm + + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow, 0 + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, 1 + +end diff --git a/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.S deleted file mode 100644 index f4d9de929f50b8..00000000000000 --- a/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.S +++ /dev/null @@ -1,156 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -.intel_syntax noprefix -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS 1 -#endif - -#define SIZEOF_RETADDR 8 - -#define SIZEOF_RETURN_BLOCK 0x10 // for 16 bytes of conservatively reported space that the callee can - // use to manage the return value that the call eventually generates - -#define SIZEOF_ARGUMENT_REGISTERS 0x30 // Callee register spill - -#define SIZEOF_FP_REGS 0x80 // xmm0-7 - -// -// From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -// -// SIZEOF_RETADDR -// SIZEOF_ARGUMENT_REGISTERS -// SIZEOF_RETURN_BLOCK -// SIZEOF_FP_REGS -// - -#define DISTANCE_FROM_CHILDSP_TO_FP_REGS 0 - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK SIZEOF_FP_REGS - -#define DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS (SIZEOF_FP_REGS + SIZEOF_RETURN_BLOCK) - -#define DISTANCE_FROM_CHILDSP_TO_RETADDR (SIZEOF_FP_REGS + SIZEOF_RETURN_BLOCK + SIZEOF_ARGUMENT_REGISTERS + 8) - -// -// Defines an assembly thunk used to make a transition from managed code to a callee, -// then (based on the return value from the callee), either returning or jumping to -// a new location while preserving the input arguments. The usage of this thunk also -// ensures arguments passed are properly reported. -// -// TODO: This code currently only tailcalls, and does not return. -// -// Inputs: -// rdi, esi, rcx, rdx, r8, r9, stack space: arguments as normal -// r10: The location of the target code the UniversalTransition thunk will call -// r11: The only parameter to the target function (passed in rdx to callee) -// - -// -// Frame layout is: -// -// {StackPassedArgs} ChildSP+0D0 CallerSP+000 -// {CallerRetaddr} ChildSP+0C8 CallerSP-008 -// {AlignmentPad (0x8 bytes)} ChildSP+0C0 CallerSP-010 -// {IntArgRegs (0x30 bytes)} ChildSP+090 CallerSP-040 -// {ReturnBlock (0x10 bytes)} ChildSP+080 CallerSP-050 -// {FpArgRegs (xmm0-xmm7) (0x80 bytes)} ChildSP+000 CallerSP-0D0 -// {CalleeRetaddr} ChildSP-008 CallerSP-0D8 -// -// NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -// must be updated as well. -// -// NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -// knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -// FpArgRegs. -// -// NOTE: The stack walker guarantees that conservative GC reporting will be applied to -// everything between the base of the ReturnBlock and the top of the StackPassedArgs. -// - -.macro UNIVERSAL_TRANSITION FunctionName - -NESTED_ENTRY Rhp\FunctionName, _TEXT, NoHandler - - alloc_stack DISTANCE_FROM_CHILDSP_TO_RETADDR - - // save integer argument registers - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x00], rdi - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x08], rsi - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x10], rcx - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x18], rdx - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x20], r8 - mov [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x28], r9 - - // save fp argument registers - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x00], xmm0 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x10], xmm1 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x20], xmm2 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x30], xmm3 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x40], xmm4 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x50], xmm5 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x60], xmm6 - movdqa [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x70], xmm7 - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - - // Before calling out, trash all of the argument registers except the ones (rdi, rsi) that - // hold outgoing arguments. All of these registers have been saved to the transition - // frame, and the code at the call target is required to use only the transition frame - // copies when dispatching this call to the eventual callee. - - movsd xmm0, [C_VAR(RhpFpTrashValues) + 0x0] - movsd xmm1, [C_VAR(RhpFpTrashValues) + 0x8] - movsd xmm2, [C_VAR(RhpFpTrashValues) + 0x10] - movsd xmm3, [C_VAR(RhpFpTrashValues) + 0x18] - movsd xmm4, [C_VAR(RhpFpTrashValues) + 0x20] - movsd xmm5, [C_VAR(RhpFpTrashValues) + 0x28] - movsd xmm6, [C_VAR(RhpFpTrashValues) + 0x30] - movsd xmm7, [C_VAR(RhpFpTrashValues) + 0x38] - - mov rcx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x10] - mov rdx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x18] - mov r8, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x20] - mov r9, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x28] - -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - // - // Call out to the target, while storing and reporting arguments to the GC. - // - mov rsi, r11 - lea rdi, [rsp + DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK] - call r10 - -ALTERNATE_ENTRY ReturnFrom\FunctionName - - // restore fp argument registers - movdqa xmm0, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x00] - movdqa xmm1, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x10] - movdqa xmm2, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x20] - movdqa xmm3, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x30] - movdqa xmm4, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x40] - movdqa xmm5, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x50] - movdqa xmm6, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x60] - movdqa xmm7, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 0x70] - - // restore integer argument registers - mov rdi, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x00] - mov rsi, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x08] - mov rcx, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x10] - mov rdx, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x18] - mov r8, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x20] - mov r9, [rsp + DISTANCE_FROM_CHILDSP_TO_ARGUMENT_REGISTERS + 0x28] - - // Pop the space that was allocated between the ChildSP and the caller return address. - free_stack DISTANCE_FROM_CHILDSP_TO_RETADDR - - jmp rax - -NESTED_END Rhp\FunctionName, _TEXT - -.endm // UNIVERSAL_TRANSITION - - UNIVERSAL_TRANSITION UniversalTransitionTailCall diff --git a/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.asm b/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.asm deleted file mode 100644 index 5bee48744ca349..00000000000000 --- a/src/coreclr/nativeaot/Runtime/amd64/UniversalTransition.asm +++ /dev/null @@ -1,166 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - -include AsmMacros.inc - -ifdef _DEBUG -TRASH_SAVED_ARGUMENT_REGISTERS equ 1 -else -TRASH_SAVED_ARGUMENT_REGISTERS equ 0 -endif - -if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 -EXTERN RhpIntegerTrashValues : QWORD -EXTERN RhpFpTrashValues : QWORD -endif ;; TRASH_SAVED_ARGUMENT_REGISTERS - -SIZEOF_RETADDR equ 8h - -SIZEOF_ALIGNMENT_PADDING equ 8h - -SIZEOF_RETURN_BLOCK equ 10h ; for 16 bytes of conservatively reported space that the callee can - ; use to manage the return value that the call eventually generates - -SIZEOF_FP_REGS equ 40h ; xmm0-3 - -SIZEOF_OUT_REG_HOMES equ 20h ; Callee register spill - -; -; From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -; -; SIZEOF_RETADDR -; SIZEOF_ALIGNMENT_PADDING -; SIZEOF_RETURN_BLOCK -; SIZEOF_FP_REGS -; SIZEOF_OUT_REG_HOMES -; - -DISTANCE_FROM_CHILDSP_TO_FP_REGS equ SIZEOF_OUT_REG_HOMES - -DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK equ DISTANCE_FROM_CHILDSP_TO_FP_REGS + SIZEOF_FP_REGS - -DISTANCE_FROM_CHILDSP_TO_RETADDR equ DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK + SIZEOF_RETURN_BLOCK + SIZEOF_ALIGNMENT_PADDING - -DISTANCE_FROM_CHILDSP_TO_CALLERSP equ DISTANCE_FROM_CHILDSP_TO_RETADDR + SIZEOF_RETADDR - -.errnz DISTANCE_FROM_CHILDSP_TO_CALLERSP mod 16 - -;; -;; Defines an assembly thunk used to make a transition from managed code to a callee, -;; then (based on the return value from the callee), either returning or jumping to -;; a new location while preserving the input arguments. The usage of this thunk also -;; ensures arguments passed are properly reported. -;; -;; TODO: This code currently only tailcalls, and does not return. -;; -;; Inputs: -;; rcx, rdx, r8, r9, stack space: arguments as normal -;; r10: The location of the target code the UniversalTransition thunk will call -;; r11: The only parameter to the target function (passed in rdx to callee) -;; - -; -; Frame layout is: -; -; {StackPassedArgs} ChildSP+0a0 CallerSP+020 -; {IntArgRegs (rcx,rdx,r8,r9) (0x20 bytes)} ChildSP+080 CallerSP+000 -; {CallerRetaddr} ChildSP+078 CallerSP-008 -; {AlignmentPad (0x8 bytes)} ChildSP+070 CallerSP-010 -; {ReturnBlock (0x10 bytes)} ChildSP+060 CallerSP-020 -; {FpArgRegs (xmm0-xmm3) (0x40 bytes)} ChildSP+020 CallerSP-060 -; {CalleeArgumentHomes (0x20 bytes)} ChildSP+000 CallerSP-080 -; {CalleeRetaddr} ChildSP-008 CallerSP-088 -; -; NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -; must be updated as well. -; -; NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -; knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -; FpArgRegs. -; -; NOTE: The stack walker guarantees that conservative GC reporting will be applied to -; everything between the base of the ReturnBlock and the top of the StackPassedArgs. -; - -EXTERN __guard_check_icall_fptr : QWORD - -UNIVERSAL_TRANSITION macro FunctionName, ValidateTarget - -NESTED_ENTRY Rhp&FunctionName, _TEXT - - alloc_stack DISTANCE_FROM_CHILDSP_TO_RETADDR - - save_reg_postrsp rcx, 0h + DISTANCE_FROM_CHILDSP_TO_CALLERSP - save_reg_postrsp rdx, 8h + DISTANCE_FROM_CHILDSP_TO_CALLERSP - save_reg_postrsp r8, 10h + DISTANCE_FROM_CHILDSP_TO_CALLERSP - save_reg_postrsp r9, 18h + DISTANCE_FROM_CHILDSP_TO_CALLERSP - - save_xmm128_postrsp xmm0, DISTANCE_FROM_CHILDSP_TO_FP_REGS - save_xmm128_postrsp xmm1, DISTANCE_FROM_CHILDSP_TO_FP_REGS + 10h - save_xmm128_postrsp xmm2, DISTANCE_FROM_CHILDSP_TO_FP_REGS + 20h - save_xmm128_postrsp xmm3, DISTANCE_FROM_CHILDSP_TO_FP_REGS + 30h - - END_PROLOGUE - -if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 - - ; Before calling out, trash all of the argument registers except the ones (rcx, rdx) that - ; hold outgoing arguments. All of these registers have been saved to the transition - ; frame, and the code at the call target is required to use only the transition frame - ; copies when dispatching this call to the eventual callee. - - movsd xmm0, mmword ptr [RhpFpTrashValues + 0h] - movsd xmm1, mmword ptr [RhpFpTrashValues + 8h] - movsd xmm2, mmword ptr [RhpFpTrashValues + 10h] - movsd xmm3, mmword ptr [RhpFpTrashValues + 18h] - - mov r8, qword ptr [RhpIntegerTrashValues + 10h] - mov r9, qword ptr [RhpIntegerTrashValues + 18h] - -endif ; TRASH_SAVED_ARGUMENT_REGISTERS - - ; - ; Call out to the target, while storing and reporting arguments to the GC. - ; - mov rdx, r11 - lea rcx, [rsp + DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK] - call r10 - -ALTERNATE_ENTRY ReturnFrom&FunctionName - -if ValidateTarget ne 0 - ; Validate the target address using Control Flow Guard before tail-calling it. - ; The validator takes the target in rcx and preserves rcx, r10, and all float registers. - mov rcx, rax - call [__guard_check_icall_fptr] - mov rax, rcx -endif ; ValidateTarget - - ; restore fp argument registers - movdqa xmm0, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS ] - movdqa xmm1, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 10h] - movdqa xmm2, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 20h] - movdqa xmm3, [rsp + DISTANCE_FROM_CHILDSP_TO_FP_REGS + 30h] - - ; restore integer argument registers - mov rcx, [rsp + 0h + DISTANCE_FROM_CHILDSP_TO_CALLERSP] - mov rdx, [rsp + 8h + DISTANCE_FROM_CHILDSP_TO_CALLERSP] - mov r8, [rsp + 10h + DISTANCE_FROM_CHILDSP_TO_CALLERSP] - mov r9, [rsp + 18h + DISTANCE_FROM_CHILDSP_TO_CALLERSP] - - ; epilog - nop - - ; Pop the space that was allocated between the ChildSP and the caller return address. - add rsp, DISTANCE_FROM_CHILDSP_TO_RETADDR - - TAILJMP_RAX - -NESTED_END Rhp&FunctionName, _TEXT - - endm - - UNIVERSAL_TRANSITION UniversalTransitionTailCall, 0 - UNIVERSAL_TRANSITION UniversalTransitionGuardedTailCall, 1 - -end diff --git a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S index d64a9ac06a8e09..59397f1066ed63 100644 --- a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S @@ -114,14 +114,8 @@ LOCAL_LABEL(CacheMiss): EPILOG_POP "{r3,r4,r5,r6,r8}" EPILOG_POP "{r1,r2}" - // Push args for RhpUniversalTransitionTailCall: - // [sp+0] = extra arg (indirection cell), [sp+4] = target fn (RhpCidResolve) - PROLOG_STACK_ALLOC 8 - str r12, [sp, #0] - PREPARE_EXTERNAL_VAR RhpCidResolve, r12 - str r12, [sp, #4] - - b C_FUNC(RhpUniversalTransitionTailCall) + // r12 contains the indirection cell address + b C_FUNC(RhpInterfaceDispatchSlow) NESTED_END RhpInterfaceDispatch, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm/ExceptionHandling.S index b20377007568fc..47bc4acdb491a6 100644 --- a/src/coreclr/nativeaot/Runtime/arm/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm/ExceptionHandling.S @@ -42,32 +42,13 @@ NESTED_ENTRY RhpThrowHwEx, _TEXT, NoHandler // lr: IP of fault site (as a 'return address') mov r4, r2 // save exception code of fault - // r0 = GetThread() - INLINE_GETTHREAD - - // r1 <- ExInfo* - add r1, sp, #rsp_offsetof_ExInfo - mov r3, #0 - str r3, [r1, #OFFSETOF__ExInfo__m_exception] // pExInfo->m_exception = null - mov r3, #1 - strb r3, [r1, #OFFSETOF__ExInfo__m_passNumber] // pExInfo->m_passNumber = 1 - mov r3, #0xFFFFFFFF - str r3, [r1, #OFFSETOF__ExInfo__m_idxCurClause] // pExInfo->m_idxCurClause = MaxTryRegionIdx - mov r3, #2 - strb r3, [r1, #OFFSETOF__ExInfo__m_kind] // pExInfo->m_kind = ExKind.HardwareFault - - // link the ExInfo into the thread's ExInfo chain - ldr r3, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] - str r3, [r1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str r1, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add r2, sp, #rsp_offsetof_Context // r2 <- PAL_LIMITED_CONTEXT* - str r2, [r1, #OFFSETOF__ExInfo__m_pExContext] // pExInfo->m_pExContext = pContext - - mov r0, r4 // restore the exception code - // r0 contains the exception code - // r1 contains the address of the ExInfo + add r0, sp, #rsp_offsetof_ExInfo // r0 <- ExInfo* + add r1, sp, #rsp_offsetof_Context // r1 <- PAL_LIMITED_CONTEXT* + mov r2, #2 // r2 <- ExKind.HardwareFault + bl C_FUNC(RhpPushExInfo) + + mov r0, r4 // r0 <- exception code + add r1, sp, #rsp_offsetof_ExInfo // r1 <- ExInfo* bl C_FUNC(RhThrowHwEx) GLOBAL_LABEL RhpThrowHwEx2 @@ -126,77 +107,14 @@ NESTED_ENTRY RhpThrowImpl, _TEXT, NoHandler str r4, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__SP)] mov r4, r0 // Save exception object - mov r5, r2 // Save ExKind - // r0 = GetThread() - INLINE_GETTHREAD - - // There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - // address could have been hijacked when we were in that C# code and we must remove the hijack and - // reflect the correct return address in our exception context record. The other throw helpers don't - // need this because they cannot be tail-called from C#. - // NOTE: we cannot use INLINE_THREAD_UNHIJACK because it will write into the stack at the location - // where the tail-calling thread had saved LR, which may not match where we have saved LR. - - ldr r1, [r0, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - cbz r1, LOCAL_LABEL(NotHiJacked) - - ldr r3, [r0, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - - // r4: exception object - // r5: ExKind - // r1: hijacked return address - // r0: pThread - // r3: hijacked return address location - - add r12, sp, #(STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) // re-compute SP at callsite - cmp r3, r12 // if (m_ppvHijackedReturnAddressLocation < SP at callsite) - blo LOCAL_LABEL(TailCallWasHijacked) - - // normal case where a valid return address location is hijacked - str r1, [r3] - b LOCAL_LABEL(ClearThreadState) - -LOCAL_LABEL(TailCallWasHijacked): - // Abnormal case where the return address location is now invalid because we ended up here via a tail - // call. In this case, our hijacked return address should be the correct caller of this method. - // - - // stick the previous return address in LR as well as in the right spots in our PAL_LIMITED_CONTEXT. - mov lr, r1 - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__LR)] - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP)] - -LOCAL_LABEL(ClearThreadState): - - // clear the Thread's hijack state - mov r3, #0 - str r3, [r0, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - str r3, [r0, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - -LOCAL_LABEL(NotHiJacked): + add r0, sp, #rsp_offsetof_ExInfo // r0 <- ExInfo* + add r1, sp, #rsp_offsetof_Context // r1 <- PAL_LIMITED_CONTEXT* + // r2 contains the ExKind + bl C_FUNC(RhpPushExInfo) + mov r0, r4 // r0 <- exception object add r1, sp, #rsp_offsetof_ExInfo // r1 <- ExInfo* - mov r3, #0 - str r3, [r1, #OFFSETOF__ExInfo__m_exception] // init the exception object to null - mov r3, #1 - strb r3, [r1, #OFFSETOF__ExInfo__m_passNumber] // init to the first pass - strb r5, [r1, #OFFSETOF__ExInfo__m_kind] // ExKind - mov r3, #0xFFFFFFFF - str r3, [r1, #OFFSETOF__ExInfo__m_idxCurClause] - - // link the ExInfo into the thread's ExInfo chain - ldr r3, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] - str r3, [r1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str r1, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add r3, sp, #rsp_offsetof_Context // r3 <- PAL_LIMITED_CONTEXT* - str r3, [r1, #OFFSETOF__ExInfo__m_pExContext] // init ExInfo.m_pExContext - - mov r0, r4 // Restore exception object - // r0 contains the exception object - // r1 contains the address of the new ExInfo bl C_FUNC(RhThrowEx) GLOBAL_LABEL RhpThrowImpl2 @@ -232,32 +150,13 @@ NESTED_ENTRY RhpRethrow, _TEXT, NoHandler add r1, sp, #(STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) str r1, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__SP)] - // r0 = GetThread(); - INLINE_GETTHREAD - - // r1 <- ExInfo* - add r1, sp, #rsp_offsetof_ExInfo + add r0, sp, #rsp_offsetof_ExInfo // r0 <- ExInfo* + add r1, sp, #rsp_offsetof_Context // r1 <- PAL_LIMITED_CONTEXT* + mov r2, #0 // r2 <- ExKind.None + bl C_FUNC(RhpPushExInfo) - mov r3, #0 - str r3, [r1, #OFFSETOF__ExInfo__m_exception] // init the exception object to null - strb r3, [r1, #OFFSETOF__ExInfo__m_kind] // init to a deterministic value (ExKind.None) - mov r3, #1 - strb r3, [r1, #OFFSETOF__ExInfo__m_passNumber] // pExInfo->m_passNumber = 1 - mov r3, #0xFFFFFFFF - str r3, [r1, #OFFSETOF__ExInfo__m_idxCurClause] - - // link the ExInfo into the thread's ExInfo chain - ldr r3, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] // r3 <- currently active ExInfo - str r3, [r1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str r1, [r0, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add r2, sp, #rsp_offsetof_Context // r2 <- PAL_LIMITED_CONTEXT* - str r2, [r1, #OFFSETOF__ExInfo__m_pExContext] // init ExInfo.m_pExContext - - mov r0, r3 // r0 contains the currently active ExInfo - // r1 contains the address of the new ExInfo + add r1, sp, #rsp_offsetof_ExInfo // r1 <- ExInfo* blx C_FUNC(RhRethrow) GLOBAL_LABEL RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S new file mode 100644 index 00000000000000..7be647ea76279b --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S @@ -0,0 +1,70 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.syntax unified +.thumb + +#include + +#ifdef _DEBUG +#define TRASH_SAVED_ARGUMENT_REGISTERS +#endif + +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +// interface dispatch cell and then jumps to the resolved target with the original arguments. +// +// At input to this function, r0-3, d0-7 and the stack may contain any number of arguments, and r12 contains the +// interface dispatch cell. +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + + // Before calling out, trash all of the argument registers except the ones (r0, r1) that + // hold outgoing arguments. All of these registers have been saved to the transition + // frame, and the code at the call target is required to use only the transition frame + // copies when dispatching this call to the eventual callee. + + PREPARE_EXTERNAL_VAR RhpFpTrashValues, r3 + vldr d0, [r3, #(0 * 8)] + vldr d1, [r3, #(1 * 8)] + vldr d2, [r3, #(2 * 8)] + vldr d3, [r3, #(3 * 8)] + vldr d4, [r3, #(4 * 8)] + vldr d5, [r3, #(5 * 8)] + vldr d6, [r3, #(6 * 8)] + vldr d7, [r3, #(7 * 8)] + + PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, r3 + ldr r2, [r3, #(2 * 4)] + ldr r3, [r3, #(3 * 4)] + +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + + add r0, sp, #__PWTB_TransitionBlock + mov r1, r12 + + // Make the ReturnFromInterfaceDispatchSlow label 4 byte aligned + .balign 4 + bl C_FUNC(RhpCidResolve) + +GLOBAL_LABEL ReturnFromInterfaceDispatchSlow + + // Move the result (the target address) to r12 so it doesn't get overridden when we restore the + // argument registers. Additionally make sure the thumb2 bit is set. + orr r12, r0, #1 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG r12 + +NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/arm/UniversalTransition.S deleted file mode 100644 index 8d942868421597..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm/UniversalTransition.S +++ /dev/null @@ -1,144 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -.syntax unified -.thumb - -#include // generated by the build from AsmOffsets.cpp -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#define COUNT_ARG_REGISTERS (4) -#define INTEGER_REGISTER_SIZE (4) -#define ARGUMENT_REGISTERS_SIZE (COUNT_ARG_REGISTERS * INTEGER_REGISTER_SIZE) - -// Largest return block is 4 doubles -#define RETURN_BLOCK_SIZE (32) - -#define COUNT_FLOAT_ARG_REGISTERS (8) -#define FLOAT_REGISTER_SIZE (8) -#define FLOAT_ARG_REGISTERS_SIZE (COUNT_FLOAT_ARG_REGISTERS * FLOAT_REGISTER_SIZE) - -#define PUSHED_LR_SIZE (4) -#define PUSHED_R11_SIZE (4) - -// -// From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -// -// ARGUMENT_REGISTERS_SIZE -// RETURN_BLOCK_SIZE -// FLOAT_ARG_REGISTERS_SIZE -// PUSHED_LR -// PUSHED_R11 - - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK (PUSHED_R11_SIZE + PUSHED_LR_SIZE + FLOAT_ARG_REGISTERS_SIZE) - -////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// -// RhpUniversalTransition -// -// At input to this function, r0-3, d0-7 and the stack may contain any number of arguments. -// -// In addition, there are 2 extra arguments passed on the stack. The caller pushes them -// (sp adjusted by -8 before branching here): -// [sp+4] = managed function to be called -// [sp+0] = pointer sized extra argument -// -// When invoking the callee: -// -// r0 shall contain a pointer to the TransitionBlock -// r1 shall contain the value that was in [sp+0] at entry to this function -// -// Frame layout is: -// -// {StackPassedArgs} ChildSP+078 CallerSP+000 -// {IntArgRegs (r0-r3) (0x10 bytes)} ChildSP+068 CallerSP-010 -// {ReturnBlock (0x20 bytes)} ChildSP+048 CallerSP-030 -// -- The base address of the Return block is the TransitionBlock pointer, the floating point args are -// in the neg space of the TransitionBlock pointer. Note that the callee has knowledge of the exact -// layout of all pieces of the frame that lie at or above the pushed floating point registers. -// {FpArgRegs (d0-d7) (0x40 bytes)} ChildSP+008 CallerSP-070 -// {PushedLR} ChildSP+004 CallerSP-074 -// {PushedR11} ChildSP+000 CallerSP-078 -// -// NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -// must be updated as well. -// -// NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -// knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -// FpArgRegs. -// -// NOTE: The stack walker guarantees that conservative GC reporting will be applied to -// everything between the base of the ReturnBlock and the top of the StackPassedArgs. -// -////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -.macro UNIVERSAL_TRANSITION FunctionName - -NESTED_ENTRY Rhp\FunctionName, _TEXT, NoHandler - // Save argument registers (including floating point) and the return address. - // Caller pushed 8 bytes: [sp]=extra arg, [sp+4]=target fn - .pad #8 - PROLOG_PUSH "{r0-r1}" - ldr r12, [sp, #12] // Capture target function (caller's [sp+4], now at sp+8+4) - ldr r1, [sp, #8] // Capture extra arg (caller's [sp], now at sp+8) - str r3, [sp, #12] // Store remaining arg registers into the space used for the hidden args - str r2, [sp, #8] - PROLOG_STACK_ALLOC RETURN_BLOCK_SIZE // Save space a buffer to be used to hold return buffer data. - PROLOG_VPUSH {d0-d7} // Capture the floating point argument registers - PROLOG_PUSH "{r11,lr}" // Save caller's frame chain pointer and PC - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - - // Before calling out, trash all of the argument registers except the ones (r0, r1) that - // hold outgoing arguments. All of these registers have been saved to the transition - // frame, and the code at the call target is required to use only the transition frame - // copies when dispatching this call to the eventual callee. - - PREPARE_EXTERNAL_VAR RhpFpTrashValues, r3 - vldr d0, [r3, #(0 * 8)] - vldr d1, [r3, #(1 * 8)] - vldr d2, [r3, #(2 * 8)] - vldr d3, [r3, #(3 * 8)] - vldr d4, [r3, #(4 * 8)] - vldr d5, [r3, #(5 * 8)] - vldr d6, [r3, #(6 * 8)] - vldr d7, [r3, #(7 * 8)] - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, r3 - ldr r2, [r3, #(2 * 4)] - ldr r3, [r3, #(3 * 4)] - -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - // Make the ReturnFromUniversalTransition alternate entry 4 byte aligned - .balign 4 - add r0, sp, #DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK // First parameter to target function is a pointer to the return block - blx r12 - -GLOBAL_LABEL ReturnFrom\FunctionName - - // Move the result (the target address) to r12 so it doesn't get overridden when we restore the - // argument registers. Additionally make sure the thumb2 bit is set. - orr r12, r0, #1 - - // Restore caller's frame chain pointer and PC. - EPILOG_POP "{r11,lr}" - - // Restore the argument registers. - EPILOG_VPOP {d0-d7} - EPILOG_STACK_FREE RETURN_BLOCK_SIZE // pop return block conservatively reported area - EPILOG_POP "{r0-r3}" - - // Tailcall to the target address. - EPILOG_BRANCH_REG r12 - -NESTED_END Rhp\FunctionName, _TEXT - -.endm - -UNIVERSAL_TRANSITION UniversalTransitionTailCall diff --git a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros.h b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros.h index e608af355c3b7b..9b10bd86a5f6f9 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros.h +++ b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros.h @@ -6,6 +6,9 @@ ;; generated by the build from AsmOffsets.cpp #include "AsmOffsets.inc" +;; Size of the q0-q7 save area of PROLOG_WITH_TRANSITION_BLOCK +#define SIZEOF__FloatArgumentRegisters 0x80 + #include "arm64/AsmMacros_Windows.h" ;; @@ -77,6 +80,7 @@ OFFSETOF__ee_alloc_context equ OFFSETOF__Thread__m_eeAllocCont EXTERN RhThrowHwEx EXTERN RhThrowEx EXTERN RhRethrow + EXTERN RhpPushExInfo EXTERN RhpTrapThreads EXTERN g_lowest_address diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S index 653de0d7613a16..ca859b42860b38 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S @@ -116,9 +116,8 @@ LOCAL_LABEL(CacheMiss\@): .if \ReturnTarget b C_FUNC(RhpCidResolve_Worker) .else - PREPARE_EXTERNAL_VAR RhpCidResolve, xip0 - mov xip1, \DispatchCellReg - b C_FUNC(RhpUniversalTransitionTailCall) + // DispatchCellReg (x11) contains the indirection cell address + b C_FUNC(RhpInterfaceDispatchSlow) .endif LEAF_END \DispatchName, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm index 5ae00eca70b1ab..3f07ef5c83ab1c 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm @@ -7,8 +7,8 @@ EXTERN RhpCidResolve EXTERN RhpCidResolve_Worker - EXTERN RhpUniversalTransitionTailCall - EXTERN RhpUniversalTransitionGuardedTailCall + EXTERN RhpInterfaceDispatchSlow + EXTERN RhpInterfaceDispatchSlowGuarded EXTERN __guard_dispatch_icall_fptr EXTERN g_pDispatchCache @@ -144,12 +144,10 @@ IF "$ReturnTarget" != "0" b RhpCidResolve_Worker ELSE - ldr xip0, =RhpCidResolve - mov xip1, $DispatchCellReg IF "$Guarded" != "0" - b RhpUniversalTransitionGuardedTailCall + b RhpInterfaceDispatchSlowGuarded ELSE - b RhpUniversalTransitionTailCall + b RhpInterfaceDispatchSlow ENDIF ENDIF diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index df630947b12338..b8193878dedb15 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -42,13 +42,6 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo .endm -.macro GetThreadX2 - stp x0, x1, [sp, -16]! - bl C_FUNC(RhpGetThread) - mov x2, x0 - ldp x0, x1, [sp], 16 -.endm - #define rsp_offsetof_ExInfo 0 #define rsp_offsetof_Context STACKSIZEOF_ExInfo @@ -64,28 +57,15 @@ ALLOC_THROW_FRAME HARDWARE_EXCEPTION - GetThreadX2 + mov w19, w0 // w19 <- exception code + + add x0, sp, #rsp_offsetof_ExInfo // x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context // x1 <- PAL_LIMITED_CONTEXT* + mov w2, #2 // w2 <- ExKind.HardwareFault + bl C_FUNC(RhpPushExInfo) + mov w0, w19 // w0 <- exception code add x1, sp, #rsp_offsetof_ExInfo // x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] // pExInfo->m_exception = null - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] // pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] // pExInfo->m_idxCurClause = MaxTryRegionIdx - mov w3, #2 - strb w3, [x1, #OFFSETOF__ExInfo__m_kind] // pExInfo->m_kind = ExKind.HardwareFault - - // link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context // x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] // pExInfo->m_pExContext = pContext - - // w0: exception code - // x1: ExInfo* bl C_FUNC(RhThrowHwEx) ALTERNATE_ENTRY RhpThrowHwEx2 @@ -130,74 +110,16 @@ NESTED_ENTRY RhpThrowImpl, _TEXT, NoHandler ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - mov x19, x4 // Save ExKind - - GetThreadX2 - - // There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - // address could have been hijacked when we were in that C# code and we must remove the hijack and - // reflect the correct return address in our exception context record. The other throw helpers don't - // need this because they cannot be tail-called from C#. - - // NOTE: we cannot use INLINE_THREAD_UNHIJACK because it will write into the stack at the location - // where the tail-calling thread had saved LR, which may not match where we have saved LR. - - ldr x1, [x2, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - cbz x1, LOCAL_LABEL(NotHijacked) - - ldr x3, [x2, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - - // x0: exception object - // x1: hijacked return address - // x2: pThread - // x3: hijacked return address location - - add x12, sp, #(STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) // re-compute SP at callsite - cmp x3, x12 // if (m_ppvHijackedReturnAddressLocation < SP at callsite) - blo LOCAL_LABEL(TailCallWasHijacked) - // normal case where a valid return address location is hijacked - str x1, [x3] - b LOCAL_LABEL(ClearThreadState) + mov x19, x0 // x19 <- exception object -LOCAL_LABEL(TailCallWasHijacked): - - // Abnormal case where the return address location is now invalid because we ended up here via a tail - // call. In this case, our hijacked return address should be the correct caller of this method. - - // stick the previous return address in LR as well as in the right spots in our PAL_LIMITED_CONTEXT. - mov lr, x1 - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__LR)] - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP)] - -LOCAL_LABEL(ClearThreadState): - - // clear the Thread's hijack state - str xzr, [x2, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - str xzr, [x2, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - str xzr, [x2, #OFFSETOF__Thread__m_pSpForPacSign] - -LOCAL_LABEL(NotHijacked): + add x0, sp, #rsp_offsetof_ExInfo // x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context // x1 <- PAL_LIMITED_CONTEXT* + mov w2, w4 // w2 <- ExKind + bl C_FUNC(RhpPushExInfo) + mov x0, x19 // x0 <- exception object add x1, sp, #rsp_offsetof_ExInfo // x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] // pExInfo->m_exception = null - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] // pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] // pExInfo->m_idxCurClause = MaxTryRegionIdx - strb w19, [x1, #OFFSETOF__ExInfo__m_kind] // pExInfo->m_kind = ExKind - - // link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context // x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] // pExInfo->m_pExContext = pContext - - // x0: exception object - // x1: ExInfo* bl C_FUNC(RhThrowEx) ALTERNATE_ENTRY RhpThrowImpl2 @@ -220,28 +142,13 @@ LOCAL_LABEL(NotHijacked): ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - GetThreadX2 - - add x1, sp, #rsp_offsetof_ExInfo // x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] // pExInfo->m_exception = null - strb wzr, [x1, #OFFSETOF__ExInfo__m_kind] // init to a deterministic value (ExKind.None) - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] // pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] // pExInfo->m_idxCurClause = MaxTryRegionIdx - - // link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - mov x0, x3 // x0 <- current ExInfo - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context // x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] // pExInfo->m_pExContext = pContext + add x0, sp, #rsp_offsetof_ExInfo // x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context // x1 <- PAL_LIMITED_CONTEXT* + mov w2, #0 // w2 <- ExKind.None + bl C_FUNC(RhpPushExInfo) // x0 contains the currently active ExInfo - // x1 contains the address of the new ExInfo + add x1, sp, #rsp_offsetof_ExInfo // x1 <- ExInfo* bl C_FUNC(RhRethrow) ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm index 4ad95c9a0ab1e8..a7e74f4aa48971 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm @@ -59,29 +59,15 @@ ALLOC_THROW_FRAME HARDWARE_EXCEPTION - ;; x2 = GetThread(), TRASHES x1 - INLINE_GETTHREAD x2, x1 + mov w19, w0 ;; w19 <- exception code + add x0, sp, #rsp_offsetof_ExInfo ;; x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context ;; x1 <- PAL_LIMITED_CONTEXT* + mov w2, #2 ;; w2 <- ExKind.HardwareFault + bl RhpPushExInfo + + mov w0, w19 ;; w0 <- exception code add x1, sp, #rsp_offsetof_ExInfo ;; x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] ;; pExInfo->m_exception = null - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] ;; pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] ;; pExInfo->m_idxCurClause = MaxTryRegionIdx - mov w3, #2 - strb w3, [x1, #OFFSETOF__ExInfo__m_kind] ;; pExInfo->m_kind = ExKind.HardwareFault - - ;; link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context ;; x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] ;; pExInfo->m_pExContext = pContext - - ;; w0: exception code - ;; x1: ExInfo* bl RhThrowHwEx ALTERNATE_ENTRY RhpThrowHwEx2 @@ -129,74 +115,15 @@ ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - ;; x2 = GetThread(), TRASHES x1 - INLINE_GETTHREAD x2, x1 - - ;; There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - ;; address could have been hijacked when we were in that C# code and we must remove the hijack and - ;; reflect the correct return address in our exception context record. The other throw helpers don't - ;; need this because they cannot be tail-called from C#. - - ;; NOTE: we cannot use INLINE_THREAD_UNHIJACK because it will write into the stack at the location - ;; where the tail-calling thread had saved LR, which may not match where we have saved LR. - - ldr x1, [x2, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - cbz x1, NotHijacked - - ldr x3, [x2, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - - ;; x0: exception object - ;; x1: hijacked return address - ;; x2: pThread - ;; x3: hijacked return address location - - add x12, sp, #(STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) ;; re-compute SP at callsite - cmp x3, x12 ;; if (m_ppvHijackedReturnAddressLocation < SP at callsite) - blo TailCallWasHijacked - - ;; normal case where a valid return address location is hijacked - str x1, [x3] - b ClearThreadState + mov x19, x0 ;; x19 <- exception object -TailCallWasHijacked - - ;; Abnormal case where the return address location is now invalid because we ended up here via a tail - ;; call. In this case, our hijacked return address should be the correct caller of this method. - ;; - - ;; stick the previous return address in LR as well as in the right spots in our PAL_LIMITED_CONTEXT. - mov lr, x1 - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__LR)] - str lr, [sp, #(rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP)] - -ClearThreadState - - ;; clear the Thread's hijack state - str xzr, [x2, #OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation] - str xzr, [x2, #OFFSETOF__Thread__m_pvHijackedReturnAddress] - str xzr, [x2, #OFFSETOF__Thread__m_pSpForPacSign] - -NotHijacked + add x0, sp, #rsp_offsetof_ExInfo ;; x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context ;; x1 <- PAL_LIMITED_CONTEXT* + mov w2, w4 ;; w2 <- ExKind + bl RhpPushExInfo + mov x0, x19 ;; x0 <- exception object add x1, sp, #rsp_offsetof_ExInfo ;; x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] ;; pExInfo->m_exception = null - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] ;; pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] ;; pExInfo->m_idxCurClause = MaxTryRegionIdx - strb w4, [x1, #OFFSETOF__ExInfo__m_kind] ;; pExInfo->m_kind = ExKind (from w4) - - ;; link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context ;; x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] ;; pExInfo->m_pExContext = pContext - - ;; x0: exception object - ;; x1: ExInfo* bl RhThrowEx ALTERNATE_ENTRY RhpThrowImpl2 @@ -220,29 +147,13 @@ NotHijacked ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - ;; x2 = GetThread(), TRASHES x1 - INLINE_GETTHREAD x2, x1 - - add x1, sp, #rsp_offsetof_ExInfo ;; x1 <- ExInfo* - str xzr, [x1, #OFFSETOF__ExInfo__m_exception] ;; pExInfo->m_exception = null - strb wzr, [x1, #OFFSETOF__ExInfo__m_kind] ;; init to a deterministic value (ExKind.None) - mov w3, #1 - strb w3, [x1, #OFFSETOF__ExInfo__m_passNumber] ;; pExInfo->m_passNumber = 1 - mov w3, #0xFFFFFFFF - str w3, [x1, #OFFSETOF__ExInfo__m_idxCurClause] ;; pExInfo->m_idxCurClause = MaxTryRegionIdx - - ;; link the ExInfo into the thread's ExInfo chain - ldr x3, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] - mov x0, x3 ;; x0 <- current ExInfo - str x3, [x1, #OFFSETOF__ExInfo__m_pPrevExInfo] ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - str x1, [x2, #OFFSETOF__Thread__m_pExInfoStackHead] ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - add x2, sp, #rsp_offsetof_Context ;; x2 <- PAL_LIMITED_CONTEXT* - str x2, [x1, #OFFSETOF__ExInfo__m_pExContext] ;; pExInfo->m_pExContext = pContext + add x0, sp, #rsp_offsetof_ExInfo ;; x0 <- ExInfo* + add x1, sp, #rsp_offsetof_Context ;; x1 <- PAL_LIMITED_CONTEXT* + mov w2, #0 ;; w2 <- ExKind.None + bl RhpPushExInfo ;; x0 contains the currently active ExInfo - ;; x1 contains the address of the new ExInfo + add x1, sp, #rsp_offsetof_ExInfo ;; x1 <- ExInfo* bl RhRethrow ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S new file mode 100644 index 00000000000000..bb47d4c58fd2f0 --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +#ifdef _DEBUG +#define TRASH_SAVED_ARGUMENT_REGISTERS +#endif + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + .global RhpIntegerTrashValues + .global RhpFpTrashValues +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +// interface dispatch cell and then jumps to the resolved target with the original arguments. +// +// At input to this function, x0-8, q0-7 and the stack may contain any number of arguments, and x11 contains the +// interface dispatch cell. +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + PREPARE_EXTERNAL_VAR RhpFpTrashValues, x1 + + ldp q0,q1, [x1, 0x0] + ldp q2,q3, [x1, 0x20] + ldp q4,q5, [x1, 0x40] + ldp q6,q7, [x1, 0x60] + + PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, x1 + + ldp x2,x3, [x1, 0x10] + ldp x4,x5, [x1, 0x20] + ldp x6,x7, [x1, 0x30] +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + + add x0, sp, #__PWTB_TransitionBlock + mov x1, x11 + bl C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + mov x9, x0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG x9 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm new file mode 100644 index 00000000000000..317507cdf06e3b --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm @@ -0,0 +1,59 @@ +;; Licensed to the .NET Foundation under one or more agreements. +;; The .NET Foundation licenses this file to you under the MIT license. + +#include "AsmMacros.h" + + EXTERN RhpCidResolve + EXTERN __guard_check_icall_fptr + + TEXTAREA + +;; +;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded +;; +;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +;; interface dispatch cell and then jumps to the resolved target with the original arguments. The guarded variant +;; validates the target with Control Flow Guard first. +;; +;; At input to this function, x0-8, q0-7 and the stack may contain any number of arguments, and x11 contains the +;; interface dispatch cell. +;; +;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +;; everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +;; + MACRO + INTERFACE_DISPATCH_SLOW $FunctionName, $ValidateTarget + + NESTED_ENTRY Rhp$FunctionName + + PROLOG_WITH_TRANSITION_BLOCK + + add x0, sp, #__PWTB_TransitionBlock + mov x1, x11 + bl RhpCidResolve + + ALTERNATE_ENTRY ReturnFrom$FunctionName + + IF "$ValidateTarget" != "" + ;; Validate the target address using Control Flow Guard before tail-calling it. + ;; The validator takes the target in x15 and preserves x0-x8, x15, and all float registers. + mov x15, x0 + adrp x16, __guard_check_icall_fptr + ldr x16, [x16, __guard_check_icall_fptr] + blr x16 + mov x0, x15 + ENDIF + + mov x9, x0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG x9 + + NESTED_END Rhp$FunctionName + + MEND + + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, ValidateTarget + + END diff --git a/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.S deleted file mode 100644 index 9dcd758059d44b..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.S +++ /dev/null @@ -1,156 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -// Padding to account for the odd number of saved integer registers -#define ALIGNMENT_PADDING_SIZE (8) - -#define COUNT_ARG_REGISTERS (9) -#define INTEGER_REGISTER_SIZE (8) -#define ARGUMENT_REGISTERS_SIZE (COUNT_ARG_REGISTERS * INTEGER_REGISTER_SIZE) - -// Largest return block is 4 doubles -#define RETURN_BLOCK_SIZE (32) - -#define COUNT_FLOAT_ARG_REGISTERS (8) -#define FLOAT_REGISTER_SIZE (16) -#define FLOAT_ARG_REGISTERS_SIZE (COUNT_FLOAT_ARG_REGISTERS * FLOAT_REGISTER_SIZE) - -#define PUSHED_LR_SIZE (8) -#define PUSHED_FP_SIZE (8) - -// -// From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -// -// ALIGNMENT_PADDING_SIZE -// ARGUMENT_REGISTERS_SIZE -// RETURN_BLOCK_SIZE -// FLOAT_ARG_REGISTERS_SIZE -// PUSHED_LR_SIZE -// PUSHED_FP_SIZE -// - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK (PUSHED_FP_SIZE + PUSHED_LR_SIZE + FLOAT_ARG_REGISTERS_SIZE) - -#define STACK_SIZE (ALIGNMENT_PADDING_SIZE + ARGUMENT_REGISTERS_SIZE + RETURN_BLOCK_SIZE + FLOAT_ARG_REGISTERS_SIZE + PUSHED_LR_SIZE + PUSHED_FP_SIZE) - -#define FLOAT_ARG_OFFSET (PUSHED_FP_SIZE + PUSHED_LR_SIZE) -#define ARGUMENT_REGISTERS_OFFSET (FLOAT_ARG_OFFSET + FLOAT_ARG_REGISTERS_SIZE + RETURN_BLOCK_SIZE) - -// -// RhpUniversalTransition -// -// At input to this function, x0-8, q0-7 and the stack may contain any number of arguments. -// -// In addition, there are 2 extra arguments passed in the intra-procedure-call scratch register: -// xip0 will contain the managed function that is to be called by this transition function -// xip1 will contain the pointer sized extra argument to the managed function -// -// When invoking the callee: -// -// x0 shall contain a pointer to the TransitionBlock -// x1 shall contain the value that was in xip1 at entry to this function -// -// Frame layout is: -// -// {StackPassedArgs} ChildSP+100 CallerSP+000 -// {AlignmentPad (0x8 bytes)} ChildSP+0F8 CallerSP-008 -// {IntArgRegs (x0-x8) (0x48 bytes)} ChildSP+0B0 CallerSP-050 -// {ReturnBlock (0x20 bytes)} ChildSP+090 CallerSP-070 -// -- The base address of the Return block is the TransitionBlock pointer, the floating point args are -// in the neg space of the TransitionBlock pointer. Note that the callee has knowledge of the exact -// layout of all pieces of the frame that lie at or above the pushed floating point registers. -// {FpArgRegs (q0-q7) (0x80 bytes)} ChildSP+010 CallerSP-0F0 -// {PushedLR} ChildSP+008 CallerSP-0F8 -// {PushedFP} ChildSP+000 CallerSP-100 -// -// NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -// must be updated as well. -// -// NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -// knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -// FpArgRegs. -// -// NOTE: The stack walker guarantees that conservative GC reporting will be applied to -// everything between the base of the ReturnBlock and the top of the StackPassedArgs. -// - - .text - - .macro UNIVERSAL_TRANSITION FunctionName - - NESTED_ENTRY Rhp\FunctionName, _TEXT, NoHandler - - // FP and LR registers - PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -STACK_SIZE // ;; Push down stack pointer and store FP and LR - - // Floating point registers - SAVE_FLOAT_ARGUMENT_REGISTERS sp, FLOAT_ARG_OFFSET - - // Space for return buffer data (0x40 bytes) - - // Save argument registers - stp x0, x1, [sp, #(ARGUMENT_REGISTERS_OFFSET )] - stp x2, x3, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x10)] - stp x4, x5, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x20)] - stp x6, x7, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x30)] - stp x8, xzr, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x40)] - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, x1 - - ldp q0,q1, [x1, 0x0] - ldp q2,q3, [x1, 0x20] - ldp q4,q5, [x1, 0x40] - ldp q6,q7, [x1, 0x60] - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, x1 - - ldp x2,x3, [x1, 0x10] - ldp x4,x5, [x1, 0x20] - ldp x6,x7, [x1, 0x30] -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - add x0, sp, #DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK // First parameter to target function is a pointer to the return block - mov x8, x0 // Arm64 calling convention: Address of return block shall be passed in x8 - mov x1, xip1 // Second parameter to target function - blr xip0 - -ALTERNATE_ENTRY ReturnFrom\FunctionName - - // Move the result (the target address) to x12 so it doesn't get overridden when we restore the - // argument registers. - mov x12, x0 - - // Restore floating point registers - RESTORE_FLOAT_ARGUMENT_REGISTERS sp, FLOAT_ARG_OFFSET - - // Restore the argument registers - ldp x0, x1, [sp, #(ARGUMENT_REGISTERS_OFFSET )] - ldp x2, x3, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x10)] - ldp x4, x5, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x20)] - ldp x6, x7, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x30)] - ldr x8, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x40)] - - // Restore FP and LR registers, and free the allocated stack block - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, STACK_SIZE - - // Tailcall to the target address. - br x12 - - NESTED_END Rhp\FunctionName, _TEXT - - .endm - - UNIVERSAL_TRANSITION UniversalTransitionTailCall - diff --git a/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.asm b/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.asm deleted file mode 100644 index a5fc214995799c..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm64/UniversalTransition.asm +++ /dev/null @@ -1,166 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - -#include "AsmMacros.h" - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - EXTERN RhpIntegerTrashValues - EXTERN RhpFpTrashValues -#endif ;; TRASH_SAVED_ARGUMENT_REGISTERS - -;; Padding to account for the odd number of saved integer registers -#define ALIGNMENT_PADDING_SIZE (8) - -#define COUNT_ARG_REGISTERS (9) -#define INTEGER_REGISTER_SIZE (8) -#define ARGUMENT_REGISTERS_SIZE (COUNT_ARG_REGISTERS * INTEGER_REGISTER_SIZE) - -;; Largest return block is 4 doubles -#define RETURN_BLOCK_SIZE (32) - -#define COUNT_FLOAT_ARG_REGISTERS (8) -#define FLOAT_REGISTER_SIZE (16) -#define FLOAT_ARG_REGISTERS_SIZE (COUNT_FLOAT_ARG_REGISTERS * FLOAT_REGISTER_SIZE) - -#define PUSHED_LR_SIZE (8) -#define PUSHED_FP_SIZE (8) - -;; -;; From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -;; -;; ALIGNMENT_PADDING_SIZE -;; ARGUMENT_REGISTERS_SIZE -;; RETURN_BLOCK_SIZE -;; FLOAT_ARG_REGISTERS_SIZE -;; PUSHED_LR_SIZE -;; PUSHED_FP_SIZE -;; - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK (PUSHED_FP_SIZE + PUSHED_LR_SIZE + FLOAT_ARG_REGISTERS_SIZE) - -#define STACK_SIZE (ALIGNMENT_PADDING_SIZE + ARGUMENT_REGISTERS_SIZE + RETURN_BLOCK_SIZE + FLOAT_ARG_REGISTERS_SIZE + \ - PUSHED_LR_SIZE + PUSHED_FP_SIZE) - -#define FLOAT_ARG_OFFSET (PUSHED_FP_SIZE + PUSHED_LR_SIZE) -#define ARGUMENT_REGISTERS_OFFSET (FLOAT_ARG_OFFSET + FLOAT_ARG_REGISTERS_SIZE + RETURN_BLOCK_SIZE) - -;; -;; RhpUniversalTransition -;; -;; At input to this function, x0-8, q0-7 and the stack may contain any number of arguments. -;; -;; In addition, there are 2 extra arguments passed in the intra-procedure-call scratch register: -;; xip0 will contain the managed function that is to be called by this transition function -;; xip1 will contain the pointer sized extra argument to the managed function -;; -;; When invoking the callee: -;; -;; x0 shall contain a pointer to the TransitionBlock -;; x1 shall contain the value that was in xip1 at entry to this function -;; -;; Frame layout is: -;; -;; {StackPassedArgs} ChildSP+100 CallerSP+000 -;; {AlignmentPad (0x8 bytes)} ChildSP+0F8 CallerSP-008 -;; {IntArgRegs (x0-x8) (0x48 bytes)} ChildSP+0A0 CallerSP-050 -;; {ReturnBlock (0x20 bytes)} ChildSP+090 CallerSP-070 -;; -- The base address of the Return block is the TransitionBlock pointer, the floating point args are -;; in the neg space of the TransitionBlock pointer. Note that the callee has knowledge of the exact -;; layout of all pieces of the frame that lie at or above the pushed floating point registers. -;; {FpArgRegs (q0-q7) (0x80 bytes)} ChildSP+010 CallerSP-0F0 -;; {PushedLR} ChildSP+008 CallerSP-0F8 -;; {PushedFP} ChildSP+000 CallerSP-100 -;; -;; NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -;; must be updated as well. -;; -;; NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -;; knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -;; FpArgRegs. -;; -;; NOTE: The stack walker guarantees that conservative GC reporting will be applied to -;; everything between the base of the ReturnBlock and the top of the StackPassedArgs. -;; - - EXTERN __guard_check_icall_fptr - - TEXTAREA - - MACRO - UNIVERSAL_TRANSITION $FunctionName, $ValidateTarget - - NESTED_ENTRY Rhp$FunctionName - - ;; FP and LR registers - PROLOG_SAVE_REG_PAIR fp, lr, #-STACK_SIZE! ;; Push down stack pointer and store FP and LR - - ;; Floating point registers - stp q0, q1, [sp, #(FLOAT_ARG_OFFSET )] - stp q2, q3, [sp, #(FLOAT_ARG_OFFSET + 0x20)] - stp q4, q5, [sp, #(FLOAT_ARG_OFFSET + 0x40)] - stp q6, q7, [sp, #(FLOAT_ARG_OFFSET + 0x60)] - - ;; Space for return buffer data (0x40 bytes) - - ;; Save argument registers - stp x0, x1, [sp, #(ARGUMENT_REGISTERS_OFFSET )] - stp x2, x3, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x10)] - stp x4, x5, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x20)] - stp x6, x7, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x30)] - stp x8, xzr, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x40)] - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - ;; ARM64TODO -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - add x0, sp, #DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK ;; First parameter to target function is a pointer to the return block - mov x8, x0 ;; Arm64 calling convention: Address of return block shall be passed in x8 - mov x1, xip1 ;; Second parameter to target function - blr xip0 - - ALTERNATE_ENTRY ReturnFrom$FunctionName - - IF "$ValidateTarget" != "" - ;; Validate the target address using Control Flow Guard before tail-calling it. - ;; The validator takes the target in x15 and preserves x0-x8, x15, and all float registers. - mov x15, x0 - adrp x16, __guard_check_icall_fptr - ldr x16, [x16, __guard_check_icall_fptr] - blr x16 - ENDIF - - ;; Move the result (the target address) to x12 so it doesn't get overridden when we restore the - ;; argument registers. - mov x12, x0 - - ;; Restore floating point registers - ldp q0, q1, [sp, #(FLOAT_ARG_OFFSET )] - ldp q2, q3, [sp, #(FLOAT_ARG_OFFSET + 0x20)] - ldp q4, q5, [sp, #(FLOAT_ARG_OFFSET + 0x40)] - ldp q6, q7, [sp, #(FLOAT_ARG_OFFSET + 0x60)] - - ;; Restore the argument registers - ldp x0, x1, [sp, #(ARGUMENT_REGISTERS_OFFSET )] - ldp x2, x3, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x10)] - ldp x4, x5, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x20)] - ldp x6, x7, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x30)] - ldr x8, [sp, #(ARGUMENT_REGISTERS_OFFSET + 0x40)] - - ;; Restore FP and LR registers, and free the allocated stack block - EPILOG_RESTORE_REG_PAIR fp, lr, #STACK_SIZE! - - ;; Tailcall to the target address. - EPILOG_NOP br x12 - - NESTED_END Rhp$FunctionName - - MEND - - UNIVERSAL_TRANSITION UniversalTransitionTailCall - UNIVERSAL_TRANSITION UniversalTransitionGuardedTailCall, ValidateTarget - - END diff --git a/src/coreclr/nativeaot/Runtime/i386/AsmMacros.inc b/src/coreclr/nativeaot/Runtime/i386/AsmMacros.inc index 8f11e67a9557bb..097891ae694dad 100644 --- a/src/coreclr/nativeaot/Runtime/i386/AsmMacros.inc +++ b/src/coreclr/nativeaot/Runtime/i386/AsmMacros.inc @@ -142,6 +142,7 @@ G_EPHEMERAL_LOW equ _g_ephemeral_low G_EPHEMERAL_HIGH equ _g_ephemeral_high G_CARD_TABLE equ _g_card_table RhpWaitForGC2 equ @RhpWaitForGC2@4 +RhpPushExInfo equ @RhpPushExInfo@12 RhpTrapThreads equ _RhpTrapThreads RhpGcPoll2 equ @RhpGcPoll2@4 RhHandleGet equ @RhHandleGet@4 @@ -159,6 +160,7 @@ EXTERN RhExceptionHandling_FailedAllocation : PROC EXTERN RhThrowHwEx : PROC EXTERN RhThrowEx : PROC EXTERN RhRethrow : PROC +EXTERN RhpPushExInfo : PROC EXTERN RhpGcPoll2 : PROC ;; The following imports are not used in the assembly helpers. Due to the diff --git a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm index 2e05141e65816d..5c47174dd1e045 100644 --- a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm @@ -8,9 +8,8 @@ include AsmMacros.inc -EXTERN RhpCidResolve : PROC EXTERN RhpCidResolve_Worker : PROC -EXTERN _RhpUniversalTransitionTailCall@0 : PROC +EXTERN _RhpInterfaceDispatchSlow@0 : PROC EXTERN _g_pDispatchCache : DWORD @@ -130,14 +129,12 @@ ALTERNATE_ENTRY _RhpInterfaceDispatch pop esi pop ebx - ;; Setup call to Universal Transition thunk + ;; Setup call to RhpInterfaceDispatchSlow push ebp mov ebp, esp - push eax - lea eax, [RhpCidResolve] - push eax + push eax ;; interface dispatch cell - jmp _RhpUniversalTransitionTailCall@0 + jmp _RhpInterfaceDispatchSlow@0 FASTCALL_ENDFUNC diff --git a/src/coreclr/nativeaot/Runtime/i386/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/i386/ExceptionHandling.asm index 1bc2bc60dd2415..310c9780e169fb 100644 --- a/src/coreclr/nativeaot/Runtime/i386/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/i386/ExceptionHandling.asm @@ -46,27 +46,15 @@ FASTCALL_FUNC RhpThrowHwEx, 8 sub esp, SIZEOF__ExInfo - INLINE_GETTHREAD eax, edx ;; eax <- thread, edx <- trashed + mov ebx, ecx ;; ebx <- exception code - lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo* - - xor esi, esi - mov [edx + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null - mov byte ptr [edx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [edx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [edx + OFFSETOF__ExInfo__m_kind], 2 ;; ExKind.HardwareFault - - ;; link the ExInfo into the thread's ExInfo chain - mov ebx, [eax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [edx + OFFSETOF__ExInfo__m_pPrevExInfo], ebx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edx ;; m_pExInfoStackHead = pExInfo + lea ecx, [esp + esp_offsetof_ExInfo] ;; ecx <- ExInfo* + lea edx, [esp + esp_offsetof_Context] ;; edx <- PAL_LIMITED_CONTEXT* + push 2 ;; ExKind.HardwareFault + call RhpPushExInfo - ;; set the exception context field on the ExInfo - lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT* - mov [edx + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext - - ;; ecx still contains the exception code - ;; edx contains the address of the ExInfo + mov ecx, ebx ;; ecx <- exception code + lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo* call RhThrowHwEx ALTERNATE_ENTRY _RhpThrowHwEx2 @@ -134,41 +122,15 @@ FASTCALL_FUNC RhpThrowImpl, 8 sub esp, SIZEOF__ExInfo - ;; ------------------------- - - lea ebx, [ebp+4] ;; ebx <- addr of return address - INLINE_GETTHREAD eax, edi ;; eax <- thread, edi <- trashed - - ;; There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - ;; address could have been hijacked when we were in that C# code and we must remove the hijack and - ;; reflect the correct return address in our exception context record. The other throw helpers don't - ;; need this because they cannot be tail-called from C#. - - INLINE_THREAD_UNHIJACK eax, esi, edi ;; trashes esi, edi - - mov edi, [ebx] ;; edi <- return address - mov [esp + esp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP], edi ;; set 'faulting' IP after unhijack + mov ebx, ecx ;; ebx <- exception object - lea edi, [esp + esp_offsetof_ExInfo] ;; edi <- ExInfo* + lea ecx, [esp + esp_offsetof_ExInfo] ;; ecx <- ExInfo* + push edx ;; ExKind + lea edx, [esp + 4 + esp_offsetof_Context] ;; edx <- PAL_LIMITED_CONTEXT* + call RhpPushExInfo - xor esi, esi - mov [edi + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null - mov byte ptr [edi + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [edi + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [edi + OFFSETOF__ExInfo__m_kind], dl ;; ExKind - - ;; link the ExInfo into the thread's ExInfo chain - mov ebx, [eax + OFFSETOF__Thread__m_pExInfoStackHead] - mov [edi + OFFSETOF__ExInfo__m_pPrevExInfo], ebx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edi ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT* - mov [edi + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext - mov edx, edi - - ;; ecx still contains the exception object - ;; edx contains the address of the ExInfo + mov ecx, ebx ;; ecx <- exception object + lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo* call RhThrowEx ALTERNATE_ENTRY _RhpThrowImpl2 @@ -216,28 +178,13 @@ FASTCALL_FUNC RhpRethrow, 0 ;; ------------------------- - lea ebx, [eax-4] ;; ebx <- addr of return address - INLINE_GETTHREAD eax, edx ;; eax <- thread, edx <- trashed - - lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo* + lea ecx, [esp + esp_offsetof_ExInfo] ;; ecx <- ExInfo* + lea edx, [esp + esp_offsetof_Context] ;; edx <- PAL_LIMITED_CONTEXT* + push 0 ;; ExKind.None + call RhpPushExInfo - xor esi, esi - mov [edx + OFFSETOF__ExInfo__m_exception], esi ;; init the exception object to null - mov byte ptr [edx + OFFSETOF__ExInfo__m_passNumber], 1 ;; init to the first pass - mov dword ptr [edx + OFFSETOF__ExInfo__m_idxCurClause], 0FFFFFFFFh - mov byte ptr [edx + OFFSETOF__ExInfo__m_kind], 0 ;; init to a deterministic value (ExKind.None) - - ;; link the ExInfo into the thread's ExInfo chain - mov ecx, [eax + OFFSETOF__Thread__m_pExInfoStackHead] ;; ecx <- currently active ExInfo - mov [edx + OFFSETOF__ExInfo__m_pPrevExInfo], ecx ;; pExInfo->m_pPrevExInfo = m_pExInfoStackHead - mov [eax + OFFSETOF__Thread__m_pExInfoStackHead], edx ;; m_pExInfoStackHead = pExInfo - - ;; set the exception context field on the ExInfo - lea ebx, [esp + esp_offsetof_Context] ;; ebx <- PAL_LIMITED_CONTEXT* - mov [edx + OFFSETOF__ExInfo__m_pExContext], ebx ;; init ExInfo.m_pExContext - - ;; ecx contains the currently active ExInfo - ;; edx contains the address of the new ExInfo + mov ecx, eax ;; ecx <- currently active ExInfo + lea edx, [esp + esp_offsetof_ExInfo] ;; edx <- ExInfo* call RhRethrow ALTERNATE_ENTRY _RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm new file mode 100644 index 00000000000000..6b71c32c185de5 --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm @@ -0,0 +1,75 @@ +;; Licensed to the .NET Foundation under one or more agreements. +;; The .NET Foundation licenses this file to you under the MIT license. + + .586 + .model flat + option casemap:none + .code + +include AsmMacros.inc + +EXTERN RhpCidResolve : PROC + +;; +;; RhpInterfaceDispatchSlow +;; +;; Saves the argument registers, calls RhpCidResolve to resolve the interface dispatch cell and then jumps to the +;; resolved target with the original arguments. +;; +;; Inputs: +;; ecx, edx, stack space two pops down: arguments as normal +;; first register sized field on the stack is the interface dispatch cell, +;; followed by the caller's EBP and the return address of the whole method. (This method cannot be called +;; via a call instruction, it must be jumped to.) The fake entrypoint is in place to +;; convince the stack walker this is a normal framed function. +;; +;; NOTE! FOR CORRECTNESS THIS FUNCTION REQUIRES THAT ALL NON-LEAF MANAGED FUNCTIONS HAVE +;; FRAME POINTERS, OR THE STACK WALKER CAN'T STACKWALK OUT OF HERE +;; + +; +; Frame layout is: +; +; {StackPassedArgs} ChildSP+014 CallerSP+000 +; {CallerRetaddr} ChildSP+010 CallerSP-004 +; {CallerEBP} ChildSP+00C CallerSP-008 +; {DispatchCell} ChildSP+008 CallerSP-00C +; {IntArgRegs (edx,ecx) (0x8 bytes)} ChildSP+000 CallerSP-014 +; {CalleeRetaddr} ChildSP-004 CallerSP-018 +; +; NOTE: If the frame layout ever changes, the C++ InterfaceDispatchSlowStackFrame structure +; must be updated as well. +; +; NOTE: RhpCidResolve receives a pointer to the base of the pushed IntArgRegs. +; +; NOTE: The stack walker guarantees that conservative GC reporting will be applied to +; everything between the base of the IntArgRegs and the top of the StackPassedArgs. +; + +FASTCALL_FUNC RhpInterfaceDispatchSlow_FAKE_ENTRY, 0 + ; Set up an ebp frame + push ebp + mov ebp, esp + push eax +ALTERNATE_ENTRY _RhpInterfaceDispatchSlow@0 + push ecx + push edx + + ; + ; Call out to RhpCidResolve, while storing and reporting arguments to the GC. + ; + mov edx, [ebp-4] ; Get the interface dispatch cell + lea ecx, [ebp-0Ch] ; Get pointer to edx value pushed above + call RhpCidResolve + +ALTERNATE_ENTRY _ReturnFromInterfaceDispatchSlow + + pop edx + pop ecx + add esp, 4 + pop ebp + jmp eax + +FASTCALL_ENDFUNC + +end diff --git a/src/coreclr/nativeaot/Runtime/i386/UniversalTransition.asm b/src/coreclr/nativeaot/Runtime/i386/UniversalTransition.asm deleted file mode 100644 index 7e780380bdc737..00000000000000 --- a/src/coreclr/nativeaot/Runtime/i386/UniversalTransition.asm +++ /dev/null @@ -1,89 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - - .586 - .model flat - option casemap:none - .code - -include AsmMacros.inc - -;; -;; Defines an assembly thunk used to make a transition from managed code to a callee, -;; then (based on the return value from the callee), either returning or jumping to -;; a new location while preserving the input arguments. The usage of this thunk also -;; ensures arguments passed are properly reported. -;; -;; TODO: This code currently only tailcalls, and does not return. -;; -;; Inputs: -;; ecx, edx, stack space three pops down: arguments as normal -;; first register sized fields on the stack is the location of the target code -;; the UniversalTransitionThunk will call -;; second register sized field on the stack is the parameter to the target function -;; followed by the return address of the whole method. (This method cannot be called -;; via a call instruction, it must be jumped to.) The fake entrypoint is in place to -;; convince the stack walker this is a normal framed function. -;; -;; NOTE! FOR CORRECTNESS THIS FUNCTION REQUIRES THAT ALL NON-LEAF MANAGED FUNCTIONS HAVE -;; FRAME POINTERS, OR THE STACK WALKER CAN'T STACKWALK OUT OF HERE -;; - -; -; Frame layout is: -; -; {StackPassedArgs} ChildSP+018 CallerSP+000 -; {CallerRetaddr} ChildSP+014 CallerSP-004 -; {CallerEBP} ChildSP+010 CallerSP-008 -; {ReturnBlock (0x8 bytes)} ChildSP+008 CallerSP-010 -; -- On input (i.e., when control jumps to RhpUniversalTransition), the low 4 bytes of -; the ReturnBlock area holds the address of the callee and the high 4 bytes holds the -; extra argument to pass to the callee. -; {IntArgRegs (edx,ecx) (0x8 bytes)} ChildSP+000 CallerSP-018 -; {CalleeRetaddr} ChildSP-004 CallerSP-01c -; -; NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -; must be updated as well. -; -; NOTE: The callee receives a pointer to the base of the pushed IntArgRegs, and the callee -; has knowledge of the exact layout of the entire frame. -; -; NOTE: The stack walker guarantees that conservative GC reporting will be applied to -; everything between the base of the IntArgRegs and the top of the StackPassedArgs. -; - -UNIVERSAL_TRANSITION macro FunctionName - -FASTCALL_FUNC Rhp&FunctionName&_FAKE_ENTRY, 0 - ; Set up an ebp frame - push ebp - mov ebp, esp - push eax - push eax -ALTERNATE_ENTRY _Rhp&FunctionName&@0 - push ecx - push edx - - ; - ; Call out to the target, while storing and reporting arguments to the GC. - ; - mov eax, [ebp-8] ; Get the address of the callee - mov edx, [ebp-4] ; Get the extra argument to pass to the callee - lea ecx, [ebp-10h] ; Get pointer to edx value pushed above - call eax - -ALTERNATE_ENTRY _ReturnFrom&FunctionName - - pop edx - pop ecx - add esp, 8 - pop ebp - jmp eax - -FASTCALL_ENDFUNC - - endm - - UNIVERSAL_TRANSITION UniversalTransitionTailCall - -end diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S index b352e3371e11a9..be885a8fdf2279 100644 --- a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S @@ -13,12 +13,8 @@ LEAF_ENTRY RhpInterfaceDispatch, _TEXT // to a NullReferenceException at the callsite. ld.d $zero, $a0, 0 - // $t8 currently contains the indirection cell address. - // Calling convention of the universal thunk is: - // $t7: target address for the thunk to call - // $t8: parameter of the thunk's target - PREPARE_EXTERNAL_VAR RhpCidResolve, $t7 - b C_FUNC(RhpUniversalTransitionTailCall) + // $t8 contains the indirection cell address + b C_FUNC(RhpInterfaceDispatchSlow) LEAF_END RhpInterfaceDispatch, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/loongarch64/ExceptionHandling.S index d33d5926f3c5bc..636f55d84729cc 100644 --- a/src/coreclr/nativeaot/Runtime/loongarch64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/loongarch64/ExceptionHandling.S @@ -52,17 +52,6 @@ addi.d $sp, $sp, -STACKSIZEOF_ExInfo .endm -.macro GetThreadA2 - addi.d $sp, $sp, -16 - st.d $a0, $sp, 0 - st.d $a1, $sp, 8 - bl C_FUNC(RhpGetThread) - ori $a2, $a0, 0 - ld.d $a0, $sp, 0 - ld.d $a1, $sp, 8 - addi.d $sp, $sp, 16 -.endm - #define rsp_offsetof_ExInfo 0 #define rsp_offsetof_Context STACKSIZEOF_ExInfo @@ -78,28 +67,15 @@ ALLOC_THROW_FRAME HARDWARE_EXCEPTION - GetThreadA2 + ori $r23, $a0, 0 // r23 <- exception code + + addi.d $a0, $sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi.d $a1, $sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + ori $a2, $zero, 2 // a2 <- ExKind.HardwareFault + bl C_FUNC(RhpPushExInfo) + ori $a0, $r23, 0 // a0 <- exception code addi.d $a1, $sp, rsp_offsetof_ExInfo // a1 <- ExInfo* - st.d $zero, $a1, OFFSETOF__ExInfo__m_exception // pExInfo->m_exception = null - ori $a3, $zero, 1 - st.b $a3, $a1, OFFSETOF__ExInfo__m_passNumber // pExInfo->m_passNumber = 1 - addi.w $a3, $zero, -1 - st.w $a3, $a1, OFFSETOF__ExInfo__m_idxCurClause // pExInfo->m_idxCurClause = MaxTryRegionIdx - ori $a3, $zero, 2 - st.b $a3, $a1, OFFSETOF__ExInfo__m_kind // pExInfo->m_kind = ExKind.HardwareFault - - // link the ExInfo into the thread's ExInfo chain - ld.d $a3, $a2, OFFSETOF__Thread__m_pExInfoStackHead - st.d $a3, $a1, OFFSETOF__ExInfo__m_pPrevExInfo // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - st.d $a1, $a2, OFFSETOF__Thread__m_pExInfoStackHead // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - addi.d $a2, $sp, rsp_offsetof_Context // a2 <- PAL_LIMITED_CONTEXT* - st.d $a2, $a1, OFFSETOF__ExInfo__m_pExContext // pExInfo->m_pExContext = pContext - - // a0[31:0]: exception code - // a1: ExInfo* bl C_FUNC(RhThrowHwEx) ALTERNATE_ENTRY RhpThrowHwEx2 @@ -144,72 +120,16 @@ NESTED_ENTRY RhpThrowImpl, _TEXT, NoHandler ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - ori $r23, $a4, 0 // Save ExKind - - GetThreadA2 - - // There is runtime C# code that can tail call to RhpThrowEx using a binder intrinsic. So the return - // address could have been hijacked when we were in that C# code and we must remove the hijack and - // reflect the correct return address in our exception context record. The other throw helpers don't - // need this because they cannot be tail-called from C#. - - // NOTE: we cannot use INLINE_THREAD_UNHIJACK because it will write into the stack at the location - // where the tail-calling thread had saved RA, which may not match where we have saved RA. - - ld.d $a1, $a2, OFFSETOF__Thread__m_pvHijackedReturnAddress - beqz $a1, LOCAL_LABEL(NotHijacked) - - ld.d $a3, $a2, OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation - - // a0: exception object - // a1: hijacked return address - // a2: pThread - // a3: hijacked return address location - - addi.d $t3, $sp, (STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) // re-compute SP at callsite - bltu $a3, $t3, LOCAL_LABEL(TailCallWasHijacked) // if (m_ppvHijackedReturnAddressLocation < SP at callsite) - // normal case where a valid return address location is hijacked - st.d $a1, $a3, 0 - b LOCAL_LABEL(ClearThreadState) + ori $r23, $a0, 0 // r23 <- exception object -LOCAL_LABEL(TailCallWasHijacked): - - // Abnormal case where the return address location is now invalid because we ended up here via a tail - // call. In this case, our hijacked return address should be the correct caller of this method. - - // stick the previous return address in RA as well as in the right spots in our PAL_LIMITED_CONTEXT. - ori $ra, $a1, 0 - st.d $ra, $sp, (rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__RA) - st.d $ra, $sp, (rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP) - -LOCAL_LABEL(ClearThreadState): - - // clear the Thread's hijack state - st.d $zero, $a2, OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation - st.d $zero, $a2, OFFSETOF__Thread__m_pvHijackedReturnAddress - -LOCAL_LABEL(NotHijacked): + addi.d $a0, $sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi.d $a1, $sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + ori $a2, $a4, 0 // a2 <- ExKind + bl C_FUNC(RhpPushExInfo) + ori $a0, $r23, 0 // a0 <- exception object addi.d $a1, $sp, rsp_offsetof_ExInfo // a1 <- ExInfo* - st.d $zero, $a1, OFFSETOF__ExInfo__m_exception // pExInfo->m_exception = null - ori $a3, $zero, 1 - st.b $a3, $a1, OFFSETOF__ExInfo__m_passNumber // pExInfo->m_passNumber = 1 - addi.w $a3, $zero, -1 - st.w $a3, $a1, OFFSETOF__ExInfo__m_idxCurClause // pExInfo->m_idxCurClause = MaxTryRegionIdx - st.b $r23, $a1, OFFSETOF__ExInfo__m_kind // pExInfo->m_kind = ExKind - - // link the ExInfo into the thread's ExInfo chain - ld.d $a3, $a2, OFFSETOF__Thread__m_pExInfoStackHead - st.d $a3, $a1, OFFSETOF__ExInfo__m_pPrevExInfo // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - st.d $a1, $a2, OFFSETOF__Thread__m_pExInfoStackHead // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - addi.d $a2, $sp, rsp_offsetof_Context // a2 <- PAL_LIMITED_CONTEXT* - st.d $a2, $a1, OFFSETOF__ExInfo__m_pExContext // pExInfo->m_pExContext = pContext - - // a0: exception object - // a1: ExInfo* bl C_FUNC(RhThrowEx) ALTERNATE_ENTRY RhpThrowImpl2 @@ -232,28 +152,13 @@ LOCAL_LABEL(NotHijacked): ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - GetThreadA2 - - addi.d $a1, $sp, rsp_offsetof_ExInfo // a1 <- ExInfo* - st.d $zero, $a1, OFFSETOF__ExInfo__m_exception // pExInfo->m_exception = null - st.b $zero, $a1, OFFSETOF__ExInfo__m_kind // init to a deterministic value (ExKind.None) - ori $a3, $zero, 1 - st.b $a3, $a1, OFFSETOF__ExInfo__m_passNumber // pExInfo->m_passNumber = 1 - addi.w $a3, $zero, -1 - st.w $a3, $a1, OFFSETOF__ExInfo__m_idxCurClause // pExInfo->m_idxCurClause = MaxTryRegionIdx - - // link the ExInfo into the thread's ExInfo chain - ld.d $a3, $a2, OFFSETOF__Thread__m_pExInfoStackHead - ori $a0, $a3, 0 // a0 <- current ExInfo - st.d $a3, $a1, OFFSETOF__ExInfo__m_pPrevExInfo // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - st.d $a1, $a2, OFFSETOF__Thread__m_pExInfoStackHead // m_pExInfoStackHead = pExInfo - - // set the exception context field on the ExInfo - addi.d $a2, $sp, rsp_offsetof_Context // a2 <- PAL_LIMITED_CONTEXT* - st.d $a2, $a1, OFFSETOF__ExInfo__m_pExContext // pExInfo->m_pExContext = pContext + addi.d $a0, $sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi.d $a1, $sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + ori $a2, $zero, 0 // a2 <- ExKind.None + bl C_FUNC(RhpPushExInfo) // a0 contains the currently active ExInfo - // a1 contains the address of the new ExInfo + addi.d $a1, $sp, rsp_offsetof_ExInfo // a1 <- ExInfo* bl C_FUNC(RhRethrow) ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S new file mode 100644 index 00000000000000..55687140465b72 --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S @@ -0,0 +1,67 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +#ifdef _DEBUG +#define TRASH_SAVED_ARGUMENT_REGISTERS +#endif + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + .global RhpIntegerTrashValues + .global RhpFpTrashValues +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +// interface dispatch cell and then jumps to the resolved target with the original arguments. +// +// At input to this function, a0-a7, f0-f7 and the stack may contain any number of arguments, and t8 contains the +// interface dispatch cell. +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + PREPARE_EXTERNAL_VAR RhpFpTrashValues, $a1 + + fld.d $f0, $a1, 0 + fld.d $f1, $a1, 0x08 + fld.d $f2, $a1, 0x10 + fld.d $f3, $a1, 0x18 + fld.d $f4, $a1, 0x20 + fld.d $f5, $a1, 0x28 + fld.d $f6, $a1, 0x30 + fld.d $f7, $a1, 0x38 + + PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, $a1 + + ld.d $a2, $a1, 0x10 + ld.d $a3, $a1, 0x18 + ld.d $a4, $a1, 0x20 + ld.d $a5, $a1, 0x28 + ld.d $a6, $a1, 0x30 + ld.d $a7, $a1, 0x38 +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + + addi.d $a0, $sp, __PWTB_TransitionBlock + ori $a1, $t8, 0 + bl C_FUNC(RhpCidResolve) + + ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + // Move the result (the target address) to t3 so it doesn't get overridden when we restore the + // argument registers. + ori $t3, $a0, 0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + jirl $r0, $t3, 0 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/loongarch64/UniversalTransition.S deleted file mode 100644 index 92b7c2131f218d..00000000000000 --- a/src/coreclr/nativeaot/Runtime/loongarch64/UniversalTransition.S +++ /dev/null @@ -1,162 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -#define COUNT_ARG_REGISTERS (8) -#define INTEGER_REGISTER_SIZE (8) -#define ARGUMENT_REGISTERS_SIZE (COUNT_ARG_REGISTERS * INTEGER_REGISTER_SIZE) - -// Largest return block is 2 doubles -#define RETURN_BLOCK_SIZE (16) - -#define COUNT_FLOAT_ARG_REGISTERS (8) -#define FLOAT_REGISTER_SIZE (8) -#define FLOAT_ARG_REGISTERS_SIZE (COUNT_FLOAT_ARG_REGISTERS * FLOAT_REGISTER_SIZE) - -#define PUSHED_RA_SIZE (8) -#define PUSHED_FP_SIZE (8) - -// -// From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -// -// ARGUMENT_REGISTERS_SIZE -// RETURN_BLOCK_SIZE -// FLOAT_ARG_REGISTERS_SIZE -// PUSHED_RA_SIZE -// PUSHED_FP_SIZE -// - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK (PUSHED_FP_SIZE + PUSHED_RA_SIZE + FLOAT_ARG_REGISTERS_SIZE) - -#define STACK_SIZE (ARGUMENT_REGISTERS_SIZE + RETURN_BLOCK_SIZE + FLOAT_ARG_REGISTERS_SIZE + PUSHED_RA_SIZE + PUSHED_FP_SIZE) - -#define FLOAT_ARG_OFFSET (PUSHED_FP_SIZE + PUSHED_RA_SIZE) -#define ARGUMENT_REGISTERS_OFFSET (FLOAT_ARG_OFFSET + FLOAT_ARG_REGISTERS_SIZE + RETURN_BLOCK_SIZE) - -// -// RhpUniversalTransition -// -// At input to this function, a0-a7, f0-f7 and the stack may contain any number of arguments. -// -// In addition, there are 2 extra arguments passed in the intra-procedure-call scratch register: -// t7 will contain the managed function that is to be called by this transition function -// t8 will contain the pointer sized extra argument to the managed function -// -// When invoking the callee: -// -// a0 shall contain a pointer to the TransitionBlock -// a1 shall contain the value that was in t8 at entry to this function -// -// Frame layout is: -// -// {StackPassedArgs} ChildSP+0A0 CallerSP+000 -// {IntArgRegs (a0-a7) (0x40 bytes)} ChildSP+060 CallerSP-040 -// {ReturnBlock (0x10 bytes)} ChildSP+050 CallerSP-050 -// -- The base address of the Return block is the TransitionBlock pointer, the floating point args are -// in the neg space of the TransitionBlock pointer. Note that the callee has knowledge of the exact -// layout of all pieces of the frame that lie at or above the pushed floating point registers. -// {FpArgRegs (f0-f7) (0x40 bytes)} ChildSP+010 CallerSP-090 -// {PushedRA} ChildSP+008 CallerSP-098 -// {PushedFP} ChildSP+000 CallerSP-0A0 -// -// NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -// must be updated as well. -// -// NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -// knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -// FpArgRegs. -// -// NOTE: The stack walker guarantees that conservative GC reporting will be applied to -// everything between the base of the ReturnBlock and the top of the StackPassedArgs. -// - - .text - - .macro UNIVERSAL_TRANSITION FunctionName - - NESTED_ENTRY Rhp\FunctionName, _TEXT, NoHandler - - // FP and RA registers - PROLOG_SAVE_REG_PAIR_INDEXED 22, 1, STACK_SIZE // ;; Push down stack pointer and store FP and RA - - // Floating point registers - SAVE_FLOAT_ARGUMENT_REGISTERS $sp, FLOAT_ARG_OFFSET - - // Space for return buffer data (0x10 bytes) - - // Save argument registers - SAVE_ARGUMENT_REGISTERS $sp, ARGUMENT_REGISTERS_OFFSET - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, $a1 - - fld.d $f0, $a1, 0 - fld.d $f1, $a1, 0x08 - fld.d $f2, $a1, 0x10 - fld.d $f3, $a1, 0x18 - fld.d $f4, $a1, 0x20 - fld.d $f5, $a1, 0x28 - fld.d $f6, $a1, 0x30 - fld.d $f7, $a1, 0x38 - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, $a1 - - ld.d $a2, $a1, 0x10 - ld.d $a3, $a1, 0x18 - ld.d $a4, $a1, 0x20 - ld.d $a5, $a1, 0x28 - ld.d $a6, $a1, 0x30 - ld.d $a7, $a1, 0x38 -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - addi.d $a0, $sp, DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK // First parameter to target function is a pointer to the return block - ori $a1, $t8, 0 // Second parameter to target function - jirl $ra, $t7, 0 - - ALTERNATE_ENTRY ReturnFrom\FunctionName - - // Move the result (the target address) to t3 so it doesn't get overridden when we restore the - // argument registers. - ori $t3, $a0, 0 - - // Restore floating point registers - fld.d $f0, $sp, FLOAT_ARG_OFFSET - fld.d $f1, $sp, FLOAT_ARG_OFFSET + 0x08 - fld.d $f2, $sp, FLOAT_ARG_OFFSET + 0x10 - fld.d $f3, $sp, FLOAT_ARG_OFFSET + 0x18 - fld.d $f4, $sp, FLOAT_ARG_OFFSET + 0x20 - fld.d $f5, $sp, FLOAT_ARG_OFFSET + 0x28 - fld.d $f6, $sp, FLOAT_ARG_OFFSET + 0x30 - fld.d $f7, $sp, FLOAT_ARG_OFFSET + 0x38 - - // Restore the argument registers - ld.d $a0, $sp, ARGUMENT_REGISTERS_OFFSET - ld.d $a1, $sp, ARGUMENT_REGISTERS_OFFSET + 0x08 - ld.d $a2, $sp, ARGUMENT_REGISTERS_OFFSET + 0x10 - ld.d $a3, $sp, ARGUMENT_REGISTERS_OFFSET + 0x18 - ld.d $a4, $sp, ARGUMENT_REGISTERS_OFFSET + 0x20 - ld.d $a5, $sp, ARGUMENT_REGISTERS_OFFSET + 0x28 - ld.d $a6, $sp, ARGUMENT_REGISTERS_OFFSET + 0x30 - ld.d $a7, $sp, ARGUMENT_REGISTERS_OFFSET + 0x38 - - // Restore FP and RA registers, and free the allocated stack block - EPILOG_RESTORE_REG_PAIR_INDEXED 22, 1, STACK_SIZE - - // Tailcall to the target address. - jirl $r0, $t3, 0 - - NESTED_END Rhp\FunctionName, _TEXT - - .endm - - UNIVERSAL_TRANSITION UniversalTransitionTailCall diff --git a/src/coreclr/nativeaot/Runtime/portable.cpp b/src/coreclr/nativeaot/Runtime/portable.cpp index 1c74ec61f26b61..3e7e6c418aeb48 100644 --- a/src/coreclr/nativeaot/Runtime/portable.cpp +++ b/src/coreclr/nativeaot/Runtime/portable.cpp @@ -33,9 +33,9 @@ #include "GCMemoryHelpers.inl" #if defined(FEATURE_PORTABLE_HELPERS) -// @TODO Implement UniversalTransitionTailCall -EXTERN_C void * ReturnFromUniversalTransitionTailCall; -void * ReturnFromUniversalTransitionTailCall; +// @TODO Implement RhpInterfaceDispatchSlow +EXTERN_C void * ReturnFromInterfaceDispatchSlow; +void * ReturnFromInterfaceDispatchSlow; #if !defined(HOST_WASM) FCIMPL2_RAW(void, RhpAssignRef, Object ** dst, Object * ref) diff --git a/src/coreclr/nativeaot/Runtime/ppc64le/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S similarity index 100% rename from src/coreclr/nativeaot/Runtime/ppc64le/UniversalTransition.S rename to src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S diff --git a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S index 3c7a07e1078938..9470d9a1ba2dd9 100644 --- a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S @@ -136,16 +136,8 @@ LOCAL_LABEL(CacheMiss\@): .if \ReturnTarget tail C_FUNC(RhpCidResolve_Worker) .else - // Calling convention of the universal thunk is: - // t0: target address for the thunk to call - // t1: parameter of the thunk's target - PREPARE_EXTERNAL_VAR RhpCidResolve, t0 - mv t1, \DispatchCellReg - // The tail call is written out by hand because the "tail" pseudo-instruction - // expands through t1, which is carrying the thunk parameter set just above. -1: - auipc t2, %pcrel_hi(C_FUNC(RhpUniversalTransitionTailCall)) - jalr x0, t2, %pcrel_lo(1b) + // DispatchCellReg (t5) contains the indirection cell address + tail C_FUNC(RhpInterfaceDispatchSlow) .endif LEAF_END \DispatchName, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/riscv64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/riscv64/ExceptionHandling.S index fcc19b5a5206a7..a18790f32be13a 100644 --- a/src/coreclr/nativeaot/Runtime/riscv64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/riscv64/ExceptionHandling.S @@ -58,17 +58,6 @@ addi sp, sp, -STACKSIZEOF_ExInfo .endm -.macro GetThreadA2 - addi sp, sp, -16 - sd a0, 0(sp) - sd a1, 8(sp) - call C_FUNC(RhpGetThread) - mv a2, a0 - ld a0, 0(sp) - ld a1, 8(sp) - addi sp, sp, 16 -.endm - #define rsp_offsetof_ExInfo 0 #define rsp_offsetof_Context STACKSIZEOF_ExInfo @@ -85,29 +74,15 @@ ALLOC_THROW_FRAME HARDWARE_EXCEPTION - GetThreadA2 + mv s1, a0 // s1 <- exception code - // Compute address for ExInfo* + addi a0, sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi a1, sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + li a2, 2 // a2 <- ExKind.HardwareFault + call C_FUNC(RhpPushExInfo) + + mv a0, s1 // a0 <- exception code addi a1, sp, rsp_offsetof_ExInfo // a1 <- ExInfo* - sd zero, OFFSETOF__ExInfo__m_exception(a1) // pExInfo->m_exception = null - li a3, 1 - sb a3, OFFSETOF__ExInfo__m_passNumber(a1) // pExInfo->m_passNumber = 1 - addiw a3, zero, -1 - sw a3, OFFSETOF__ExInfo__m_idxCurClause(a1) // pExInfo->m_idxCurClause = MaxTryRegionIdx - li a3, 2 - sb a3, OFFSETOF__ExInfo__m_kind(a1) // pExInfo->m_kind = ExKind.HardwareFault - - // Link the ExInfo into the thread's ExInfo chain - ld a3, OFFSETOF__Thread__m_pExInfoStackHead(a2) - sd a3, OFFSETOF__ExInfo__m_pPrevExInfo(a1) // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - sd a1, OFFSETOF__Thread__m_pExInfoStackHead(a2) // m_pExInfoStackHead = pExInfo - - // Set the exception context field on the ExInfo - addi a2, sp, rsp_offsetof_Context // a2 <- PAL_LIMITED_CONTEXT* - sd a2, OFFSETOF__ExInfo__m_pExContext(a1) // pExInfo->m_pExContext = pContext - - // a0[31:0]: exception code - // a1: ExInfo* call C_FUNC(RhThrowHwEx) ALTERNATE_ENTRY RhpThrowHwEx2 @@ -152,63 +127,16 @@ NESTED_ENTRY RhpThrowImpl, _TEXT, NoHandler ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - mv s1, a4 // Save ExKind - - GetThreadA2 - - ld a1, OFFSETOF__Thread__m_pvHijackedReturnAddress(a2) - beq a1, zero, LOCAL_LABEL(NotHijacked) - - ld a3, OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation(a2) - - // Recompute SP at callsite - addi t3, sp, (STACKSIZEOF_ExInfo + SIZEOF__PAL_LIMITED_CONTEXT) - bltu a3, t3, LOCAL_LABEL(TailCallWasHijacked) // if (m_ppvHijackedReturnAddressLocation < SP at callsite) - // Normal case where a valid return address location is hijacked - sd a1, 0(a3) - j LOCAL_LABEL(ClearThreadState) + mv s1, a0 // s1 <- exception object -LOCAL_LABEL(TailCallWasHijacked): + addi a0, sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi a1, sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + mv a2, a4 // a2 <- ExKind + call C_FUNC(RhpPushExInfo) - // Abnormal case where the return address location is now invalid because we ended up here via a tail - // call. In this case, our hijacked return address should be the correct caller of this method. - - // Stick the previous return address in RA as well as in the right spots in our PAL_LIMITED_CONTEXT. - mv ra, a1 - - // Compute offsets for PAL_LIMITED_CONTEXT - sd ra, (rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__RA)(sp) - sd ra, (rsp_offsetof_Context + OFFSETOF__PAL_LIMITED_CONTEXT__IP)(sp) - -LOCAL_LABEL(ClearThreadState): - - // Clear the Thread's hijack state - sd zero, OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation(a2) - sd zero, OFFSETOF__Thread__m_pvHijackedReturnAddress(a2) - -LOCAL_LABEL(NotHijacked): - - // Compute the offset for ExInfo + mv a0, s1 // a0 <- exception object addi a1, sp, rsp_offsetof_ExInfo // a1 <- ExInfo* - sd zero, OFFSETOF__ExInfo__m_exception(a1) // pExInfo->m_exception = null - li a3, 1 - sb a3, OFFSETOF__ExInfo__m_passNumber(a1) // pExInfo->m_passNumber = 1 - addiw a3, zero, -1 - sw a3, OFFSETOF__ExInfo__m_idxCurClause(a1) // pExInfo->m_idxCurClause = MaxTryRegionIdx - sb s1, OFFSETOF__ExInfo__m_kind(a1) // pExInfo->m_kind = ExKind - - // Link the ExInfo into the thread's ExInfo chain - ld a3, OFFSETOF__Thread__m_pExInfoStackHead(a2) - sd a3, OFFSETOF__ExInfo__m_pPrevExInfo(a1) // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - sd a1, OFFSETOF__Thread__m_pExInfoStackHead(a2) // m_pExInfoStackHead = pExInfo - - // Set the exception context field on the ExInfo - addi a2, sp, rsp_offsetof_Context // a2 <- PAL_LIMITED_CONTEXT* - sd a2, OFFSETOF__ExInfo__m_pExContext(a1) // pExInfo->m_pExContext = pContext - - // a0: exception object - // a1: ExInfo* call C_FUNC(RhThrowEx) ALTERNATE_ENTRY RhpThrowImpl2 @@ -230,38 +158,13 @@ LOCAL_LABEL(NotHijacked): ALLOC_THROW_FRAME SOFTWARE_EXCEPTION - GetThreadA2 - - // a1 <- ExInfo* - addi a1, sp, rsp_offsetof_ExInfo - // pExInfo->m_exception = null - sd zero, OFFSETOF__ExInfo__m_exception(a1) - // init to a deterministic value (ExKind.None) - sb zero, OFFSETOF__ExInfo__m_kind(a1) - // pExInfo->m_passNumber = 1 - li a3, 1 - sb a3, OFFSETOF__ExInfo__m_passNumber(a1) - // pExInfo->m_idxCurClause = MaxTryRegionIdx - addiw a3, zero, -1 - sw a3, OFFSETOF__ExInfo__m_idxCurClause(a1) - - // link the ExInfo into the thread's ExInfo chain - ld a3, OFFSETOF__Thread__m_pExInfoStackHead(a2) - // a0 <- current ExInfo - mv a0, a3 - // pExInfo->m_pPrevExInfo = m_pExInfoStackHead - sd a3, OFFSETOF__ExInfo__m_pPrevExInfo(a1) - // m_pExInfoStackHead = pExInfo - sd a1, OFFSETOF__Thread__m_pExInfoStackHead(a2) - - // set the exception context field on the ExInfo - // a2 <- PAL_LIMITED_CONTEXT* - addi a2, sp, rsp_offsetof_Context - // pExInfo->m_pExContext = pContext - sd a2, OFFSETOF__ExInfo__m_pExContext(a1) + addi a0, sp, rsp_offsetof_ExInfo // a0 <- ExInfo* + addi a1, sp, rsp_offsetof_Context // a1 <- PAL_LIMITED_CONTEXT* + li a2, 0 // a2 <- ExKind.None + call C_FUNC(RhpPushExInfo) // a0 contains the currently active ExInfo - // a1 contains the address of the new ExInfo + addi a1, sp, rsp_offsetof_ExInfo // a1 <- ExInfo* call C_FUNC(RhRethrow) ALTERNATE_ENTRY RhpRethrow2 diff --git a/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S new file mode 100644 index 00000000000000..ad20e3016959d4 --- /dev/null +++ b/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S @@ -0,0 +1,67 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +#ifdef _DEBUG +#define TRASH_SAVED_ARGUMENT_REGISTERS +#endif + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + .global RhpIntegerTrashValues + .global RhpFpTrashValues +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the +// interface dispatch cell and then jumps to the resolved target with the original arguments. +// +// At input to this function, a0-a7, fa0-fa7 and the stack may contain any number of arguments, and t5 contains the +// interface dispatch cell. +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + +#ifdef TRASH_SAVED_ARGUMENT_REGISTERS + PREPARE_EXTERNAL_VAR RhpFpTrashValues, a1 + + fld fa0, 0x00(a1) + fld fa1, 0x08(a1) + fld fa2, 0x10(a1) + fld fa3, 0x18(a1) + fld fa4, 0x20(a1) + fld fa5, 0x28(a1) + fld fa6, 0x30(a1) + fld fa7, 0x38(a1) + + PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, a1 + + ld a2, 0x10(a1) + ld a3, 0x18(a1) + ld a4, 0x20(a1) + ld a5, 0x28(a1) + ld a6, 0x30(a1) + ld a7, 0x38(a1) +#endif // TRASH_SAVED_ARGUMENT_REGISTERS + + addi a0, sp, __PWTB_TransitionBlock + mv a1, t5 + call C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + # Move the result (the target address) to t2 so it doesn't get overridden when we restore the + # argument registers. + mv t2, a0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + jr t2 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/riscv64/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/riscv64/UniversalTransition.S deleted file mode 100644 index db695646a127e9..00000000000000 --- a/src/coreclr/nativeaot/Runtime/riscv64/UniversalTransition.S +++ /dev/null @@ -1,149 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -#define COUNT_ARG_REGISTERS (8) -#define INTEGER_REGISTER_SIZE (8) -#define ARGUMENT_REGISTERS_SIZE (COUNT_ARG_REGISTERS * INTEGER_REGISTER_SIZE) - -// Largest return block is 4 doubles -#define RETURN_BLOCK_SIZE 16 - -#define COUNT_FLOAT_ARG_REGISTERS (8) -#define FLOAT_REGISTER_SIZE (8) -#define FLOAT_ARG_REGISTERS_SIZE (COUNT_FLOAT_ARG_REGISTERS * FLOAT_REGISTER_SIZE) - -#define PUSHED_RA_SIZE (8) -#define PUSHED_FP_SIZE (8) - -// From CallerSP to ChildSP, the stack frame is composed of the following adjacent regions: -// -// ARGUMENT_REGISTERS_SIZE -// RETURN_BLOCK_SIZE -// FLOAT_ARG_REGISTERS_SIZE -// PUSHED_RA_SIZE -// PUSHED_FP_SIZE -// - -#define DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK (PUSHED_FP_SIZE + PUSHED_RA_SIZE + FLOAT_ARG_REGISTERS_SIZE) - -#define STACK_SIZE (ARGUMENT_REGISTERS_SIZE + RETURN_BLOCK_SIZE + FLOAT_ARG_REGISTERS_SIZE + PUSHED_RA_SIZE + PUSHED_FP_SIZE) - -#define FLOAT_ARG_OFFSET (PUSHED_FP_SIZE + PUSHED_RA_SIZE) -#define ARGUMENT_REGISTERS_OFFSET (FLOAT_ARG_OFFSET + FLOAT_ARG_REGISTERS_SIZE + RETURN_BLOCK_SIZE) - -// -// RhpUniversalTransition -// -// At input to this function, a0-a7, fa0-fa7, and the stack may contain any number of arguments. -// -// In addition, there are 2 extra arguments passed in the intra-procedure-call scratch register: -// t0 will contain the managed function that is to be called by this transition function -// t1 will contain the pointer-sized extra argument to the managed function -// -// When invoking the callee: -// -// a0 shall contain a pointer to the TransitionBlock -// a1 shall contain the value that was in t1 at entry to this function -// -// Frame layout is: -// -// {StackPassedArgs} ChildSP+100 CallerSP+000 -// {IntArgRegs (a0-a7) (0x40 bytes)} ChildSP+0B8 CallerSP-048 -// {ReturnBlock (0x10 bytes)} ChildSP+098 CallerSP-068 -// -- The base address of the Return block is the TransitionBlock pointer, the floating point args are -// in the neg space of the TransitionBlock pointer. Note that the callee has knowledge of the exact -// layout of all pieces of the frame that lie at or above the pushed floating point registers. -// {FpArgRegs (fa0-fa7) (0x40 bytes)} ChildSP+058 CallerSP-0A8 -// {PushedRA} ChildSP+008 CallerSP-0F8 -// {PushedFP} ChildSP+000 CallerSP-100 -// -// NOTE: If the frame layout ever changes, the C++ UniversalTransitionStackFrame structure -// must be updated as well. -// -// NOTE: The callee receives a pointer to the base of the ReturnBlock, and the callee has -// knowledge of the exact layout of all pieces of the frame that lie at or above the pushed -// FpArgRegs. -// -// NOTE: The stack walker guarantees that conservative GC reporting will be applied to -// everything between the base of the ReturnBlock and the top of the StackPassedArgs. -// - - .text - - .macro UNIVERSAL_TRANSITION FunctionName - - NESTED_ENTRY Rhp\FunctionName, _TEXT, NoHandler - - # FP and RA registers - PROLOG_SAVE_REG_PAIR_INDEXED fp, ra, STACK_SIZE - - # Floating point registers - SAVE_FLOAT_ARGUMENT_REGISTERS sp, FLOAT_ARG_OFFSET - - # Space for return block data (0x10 bytes) - - # Save argument registers - SAVE_ARGUMENT_REGISTERS sp, ARGUMENT_REGISTERS_OFFSET - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, a1 - - fld fa0, 0x00(a1) - fld fa1, 0x08(a1) - fld fa2, 0x10(a1) - fld fa3, 0x18(a1) - fld fa4, 0x20(a1) - fld fa5, 0x28(a1) - fld fa6, 0x30(a1) - fld fa7, 0x38(a1) - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, a1 - - ld a2, 0x10(a1) - ld a3, 0x18(a1) - ld a4, 0x20(a1) - ld a5, 0x28(a1) - ld a6, 0x30(a1) - ld a7, 0x38(a1) -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - add a0, sp, DISTANCE_FROM_CHILDSP_TO_RETURN_BLOCK # First parameter to target function is a pointer to the return block - mv a1, t1 # Second parameter to target function - jalr ra, t0, 0 - -ALTERNATE_ENTRY ReturnFrom\FunctionName - - # Move the result (the target address) to a2 so it doesn't get overridden when we restore the - # argument registers. - mv t2, a0 - - # Restore floating point registers - RESTORE_FLOAT_ARGUMENT_REGISTERS sp, FLOAT_ARG_OFFSET - - # Restore the argument registers - RESTORE_ARGUMENT_REGISTERS sp, ARGUMENT_REGISTERS_OFFSET - - # Restore FP and RA registers, and free the allocated stack block - ld ra, 0x08(sp) - ld s0, 0x0(sp) - addi sp, sp, STACK_SIZE - - # Tailcall to the target address. - jr t2 - - NESTED_END Rhp\FunctionName, _TEXT - - .endm - - UNIVERSAL_TRANSITION UniversalTransitionTailCall diff --git a/src/coreclr/nativeaot/Runtime/s390x/UniversalTransition.S b/src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S similarity index 100% rename from src/coreclr/nativeaot/Runtime/s390x/UniversalTransition.S rename to src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S diff --git a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc index bff826b2cac976..c3f266550fe993 100644 --- a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc +++ b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc @@ -3,6 +3,9 @@ #include "AsmOffsets.inc" +// Size of the q0-q7 save area of PROLOG_WITH_TRANSITION_BLOCK +#define SIZEOF__FloatArgumentRegisters 0x80 + #include // Inlined version of RhpGetThread. Target cannot be x0. diff --git a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosloongarch64.inc b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosloongarch64.inc index 7e5ec9c20b1934..2b309278f28e9f 100644 --- a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosloongarch64.inc +++ b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosloongarch64.inc @@ -3,6 +3,9 @@ #include "AsmOffsets.inc" +// Size of the float argument register save area of PROLOG_WITH_TRANSITION_BLOCK +#define SIZEOF__FloatArgumentRegisters 0x40 + #include // Inlined version of RhpGetThread. Target cannot be a0. diff --git a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosriscv64.inc b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosriscv64.inc index 0d82343f9e027c..d3b2febfcf2555 100644 --- a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosriscv64.inc +++ b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosriscv64.inc @@ -3,6 +3,9 @@ #include "AsmOffsets.inc" +// Size of the float argument register save area of PROLOG_WITH_TRANSITION_BLOCK +#define SIZEOF__FloatArgumentRegisters 0x40 + #include // Inlined version of RhpGetThread. Target cannot be x0. diff --git a/src/coreclr/pal/inc/unixasmmacrosamd64.inc b/src/coreclr/pal/inc/unixasmmacrosamd64.inc index 6dc262ae0ecff2..4528875fd1f839 100644 --- a/src/coreclr/pal/inc/unixasmmacrosamd64.inc +++ b/src/coreclr/pal/inc/unixasmmacrosamd64.inc @@ -3,118 +3,6 @@ #include -// Stack layout: -// -// (stack parameters) -// ... -// return address -// CalleeSavedRegisters::rbp -// CalleeSavedRegisters::rbx -// CalleeSavedRegisters::r15 -// CalleeSavedRegisters::r14 -// CalleeSavedRegisters::r13 -// CalleeSavedRegisters::r12 -// ArgumentRegisters::r9 -// ArgumentRegisters::r8 -// ArgumentRegisters::rcx -// ArgumentRegisters::rdx -// ArgumentRegisters::rsi -// ArgumentRegisters::rdi <- __PWTB_StackAlloc, __PWTB_TransitionBlock -// padding to align xmm save area -// xmm7 -// xmm6 -// xmm5 -// xmm4 -// xmm3 -// xmm2 -// xmm1 -// xmm0 <- __PWTB_FloatArgumentRegisters -// extra locals + padding to qword align -.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, stackAllocOnEntry = 0, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 - - set_cfa_register rsp, 8 - - __PWTB_FloatArgumentRegisters = \extraLocals - - .if ((__PWTB_FloatArgumentRegisters % 16) != 0) - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 - .endif - - __PWTB_StackAlloc = __PWTB_FloatArgumentRegisters + 8 * 16 + 8 // 8 floating point registers - __PWTB_TransitionBlock = __PWTB_StackAlloc - __PWTB_ArgumentRegisters = __PWTB_TransitionBlock - - .if \stackAllocOnEntry >= 4*8 - .error "Max supported stackAllocOnEntry is 3*8" - .endif - - .if \stackAllocOnEntry > 0 - .cfi_adjust_cfa_offset \stackAllocOnEntry - .endif - - // PUSH_CALLEE_SAVED_REGISTERS expanded here - - .if \stackAllocOnEntry < 8 - push_nonvol_reg rbp - mov rbp, rsp - .endif - - .if \stackAllocOnEntry < 2*8 - push_nonvol_reg rbx - .endif - - .if \stackAllocOnEntry < 3*8 - push_nonvol_reg r15 - .endif - - push_nonvol_reg r14 - push_nonvol_reg r13 - push_nonvol_reg r12 - - // ArgumentRegisters - PUSH_ARGUMENT_REGISTERS - - .if \stackAllocOnEntry >= 3*8 - mov \stackAllocSpill3, [rsp + 0x48] - save_reg_postrsp r15, 0x48 - .endif - - .if \stackAllocOnEntry >= 2*8 - mov \stackAllocSpill2, [rsp + 0x50] - save_reg_postrsp rbx, 0x50 - .endif - - .if \stackAllocOnEntry >= 8 - mov \stackAllocSpill1, [rsp + 0x58] - save_reg_postrsp rbp, 0x58 - lea rbp, [rsp + 0x58] - .endif - - alloc_stack __PWTB_StackAlloc - SAVE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters - - END_PROLOGUE - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - - free_stack __PWTB_StackAlloc - SKIP_ARGUMENT_REGISTERS - POP_CALLEE_SAVED_REGISTERS - ret - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters - free_stack __PWTB_StackAlloc - POP_ARGUMENT_REGISTERS - POP_CALLEE_SAVED_REGISTERS - -.endm - // Inlined version of GetThreadEEAllocContext. Trashes volatile registers. .macro INLINE_GET_ALLOC_CONTEXT_BASE #ifdef FEATURE_EMULATED_TLS diff --git a/src/coreclr/pal/inc/unixasmmacrosarm.inc b/src/coreclr/pal/inc/unixasmmacrosarm.inc index 97476a130da971..93507015ad752e 100644 --- a/src/coreclr/pal/inc/unixasmmacrosarm.inc +++ b/src/coreclr/pal/inc/unixasmmacrosarm.inc @@ -3,128 +3,6 @@ #include -// Stack layout: -// -// (stack parameters) -// ... -// ArgumentRegisters::r3 -// ArgumentRegisters::r2 -// ArgumentRegisters::r1 -// ArgumentRegisters::r0 -// CalleeSavedRegisters::lr -// CalleeSavedRegisters::r11 -// CalleeSavedRegisters::r10 -// CalleeSavedRegisters::r9 -// CalleeSavedRegisters::r8 -// CalleeSavedRegisters::r7 <- r7 -// CalleeSavedRegisters::r6 -// CalleeSavedRegisters::r5 -// CalleeSavedRegisters::r4 <- __PWTB_StackAlloc, __PWTB_TransitionBlock -// padding to align float save area -// d7 -// d6 -// d5 -// d4 -// d3 -// d2 -// d1 -// d0 <- __PWTB_FloatArgumentRegisters -// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) -// d15 -// d14 -// d13 -// d12 -// d11 -// d10 -// d9 -// d8 <- __PWTB_FloatCalleeSavedRegisters -// -// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save -// the callee-saved floating point registers (d8-d15) to the stack. -// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. -// -.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, saveFpArgs = 1, pushArgRegs = 0, pushCalleeSavedFloatRegs = 0 - - __PWTB_FloatCalleeSavedRegisters = \extraLocals - __PWTB_SaveFPArgs = \saveFpArgs - __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs - - // If pushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - .if ((__PWTB_FloatCalleeSavedRegisters % 8) != 0) - __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 4 - .endif - __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 64 - .else - __PWTB_FloatArgumentRegisters = \extraLocals - .endif - - .if (__PWTB_SaveFPArgs == 1) - .if ((__PWTB_FloatArgumentRegisters % 8) != 0) - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 4 - .endif - - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + 8 * 8 + 4 // 8 floating point registers + padding - .else - .if ((__PWTB_FloatArgumentRegisters % 8) == 0) - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 4 - .endif - - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters - .endif - - __PWTB_StackAlloc = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 36 - - .ifnc \pushArgRegs, DoNotPushArgRegs - PUSH_ARGUMENT_REGISTERS - .endif - - PUSH_CALLEE_SAVED_REGISTERS - PROLOG_STACK_SAVE_OFFSET r7, #12 - // let r7 point the saved r7 in the stack (clang FP style) - - alloc_stack __PWTB_StackAlloc - - .if (__PWTB_SaveFPArgs == 1) - add r6, sp, #(__PWTB_FloatArgumentRegisters) - vstm r6, {d0-d7} - .endif - - // Save callee-saved floating point registers if requested - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - add r6, sp, #(__PWTB_FloatCalleeSavedRegisters) - vstm r6, {d8-d15} - .endif - - CHECK_STACK_ALIGNMENT - - END_PROLOGUE - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - - free_stack __PWTB_StackAlloc - POP_CALLEE_SAVED_REGISTERS - free_stack 16 - bx lr - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - .if (__PWTB_SaveFPArgs == 1) - add r6, sp, #(__PWTB_FloatArgumentRegisters) - vldm r6, {d0-d7} - .endif - - free_stack __PWTB_StackAlloc - POP_CALLEE_SAVED_REGISTERS - POP_ARGUMENT_REGISTERS - -.endm - .macro INLINE_GET_ALLOC_CONTEXT_BASE bl C_FUNC(GetThreadEEAllocContext) .endm diff --git a/src/coreclr/pal/inc/unixasmmacrosarm64.inc b/src/coreclr/pal/inc/unixasmmacrosarm64.inc index 04c9e2ac8dc147..86eda4ad24c2f5 100644 --- a/src/coreclr/pal/inc/unixasmmacrosarm64.inc +++ b/src/coreclr/pal/inc/unixasmmacrosarm64.inc @@ -3,169 +3,6 @@ #include -//----------------------------------------------------------------------------- -// Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and -// comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned -// -// Stack layout: -// -// (stack parameters) -// ... -// fp -// lr -// CalleeSavedRegisters::x28 -// CalleeSavedRegisters::x27 -// CalleeSavedRegisters::x26 -// CalleeSavedRegisters::x25 -// CalleeSavedRegisters::x24 -// CalleeSavedRegisters::x23 -// CalleeSavedRegisters::x22 -// CalleeSavedRegisters::x21 -// CalleeSavedRegisters::x20 -// CalleeSavedRegisters::x19 -// CalleeSavedRegisters::x30 (Lr) -// CalleeSavedRegisters::x29 (Fp) -// ArgumentRegisters::x7 -// ArgumentRegisters::x6 -// ArgumentRegisters::x5 -// ArgumentRegisters::x4 -// ArgumentRegisters::x3 -// ArgumentRegisters::x2 -// ArgumentRegisters::x1 -// ArgumentRegisters::x0 -// ArgumentRegisters::x8 -// FloatRegisters::q7 -// FloatRegisters::q6 -// FloatRegisters::q5 -// FloatRegisters::q4 -// FloatRegisters::q3 -// FloatRegisters::q2 -// FloatRegisters::q1 -// FloatRegisters::q0 -// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) -// d15 -// d14 -// d13 -// d12 -// d11 -// d10 -// d9 -// d8 -// -// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save -// the callee-saved floating point registers (d8-d15) to the stack. -// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. -// -.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, SaveFPArgs = 1, SaveGPArgs = 1, pushCalleeSavedFloatRegs = 0 - - __PWTB_FloatCalleeSavedRegisters = \extraLocals - __PWTB_SaveFPArgs = \SaveFPArgs - __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs - - .if ((__PWTB_FloatCalleeSavedRegisters % 16) != 0) - __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 8 - .endif - - // If pushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 64 - .else - __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters - .endif - - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters - - // Always reserve space for the FP arg regs, even if they are not saved in the PROLOG_WITH_TRANSITION_BLOCK - // Some code using this macro saves them optionally - __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters - - __PWTB_StackAlloc = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 104 - __PWTB_ArgumentRegister_FirstArg = __PWTB_ArgumentRegisters + 8 - - PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -176 - // Spill callee saved registers - PROLOG_SAVE_REG_PAIR x19, x20, 16 - PROLOG_SAVE_REG_PAIR x21, x22, 32 - PROLOG_SAVE_REG_PAIR x23, x24, 48 - PROLOG_SAVE_REG_PAIR x25, x26, 64 - PROLOG_SAVE_REG_PAIR x27, x28, 80 - - // Allocate space for the rest of the frame - PROLOG_STACK_ALLOC __PWTB_StackAlloc - - .if (\SaveGPArgs == 1) - // Spill argument registers. - SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - .endif - - .if (__PWTB_SaveFPArgs == 1) - SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - .endif - - // Save callee-saved floating point registers if requested - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - PROLOG_SAVE_REG_PAIR d8, d9, __PWTB_FloatCalleeSavedRegisters - PROLOG_SAVE_REG_PAIR d10, d11, __PWTB_FloatCalleeSavedRegisters + 16 - PROLOG_SAVE_REG_PAIR d12, d13, __PWTB_FloatCalleeSavedRegisters + 32 - PROLOG_SAVE_REG_PAIR d14, d15, __PWTB_FloatCalleeSavedRegisters + 48 - .endif - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - - EPILOG_STACK_FREE __PWTB_StackAlloc - - EPILOG_RESTORE_REG_PAIR x19, x20, 16 - EPILOG_RESTORE_REG_PAIR x21, x22, 32 - EPILOG_RESTORE_REG_PAIR x23, x24, 48 - EPILOG_RESTORE_REG_PAIR x25, x26, 64 - EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 - ret - -.endm - -// Same as EPILOG_WITH_TRANSITION_BLOCK_RETURN but skips restoring x21 (SwiftError) -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN_SKIP_SWIFT_ERROR - - EPILOG_STACK_FREE __PWTB_StackAlloc - - EPILOG_RESTORE_REG_PAIR x19, x20, 16 - EPILOG_RESTORE_REG x22, 40 - EPILOG_RESTORE_REG_PAIR x23, x24, 48 - EPILOG_RESTORE_REG_PAIR x25, x26, 64 - EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 - ret - -.endm - -//----------------------------------------------------------------------------- -// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. -// Since this is a tail call argument registers are restored. -// -.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - .if (__PWTB_SaveFPArgs == 1) - RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - .endif - - RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - - EPILOG_STACK_FREE __PWTB_StackAlloc - - EPILOG_RESTORE_REG_PAIR x19, x20, 16 - EPILOG_RESTORE_REG_PAIR x21, x22, 32 - EPILOG_RESTORE_REG_PAIR x23, x24, 48 - EPILOG_RESTORE_REG_PAIR x25, x26, 64 - EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 - -.endm - - // Inlined version of GetThreadEEAllocContext. Target cannot be x0 or x1. .macro INLINE_GET_ALLOC_CONTEXT_BASE target .ifc \target, x0 diff --git a/src/coreclr/pal/inc/unixasmmacrosloongarch64.inc b/src/coreclr/pal/inc/unixasmmacrosloongarch64.inc index 3160785ccbd6f9..ccb9cc91b1ed15 100644 --- a/src/coreclr/pal/inc/unixasmmacrosloongarch64.inc +++ b/src/coreclr/pal/inc/unixasmmacrosloongarch64.inc @@ -3,116 +3,6 @@ #include -//----------------------------------------------------------------------------- -// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and -// comprises the entire prolog.The locals must be 8 byte aligned -// -// Save_argument_registers: -// GPR_a7 -// GPR_a6 -// GPR_a5 -// GPR_a4 -// GPR_a3 -// GPR_a2 -// GPR_a1 -// GPR_a0 -// -// General Registers: -// GPR_s8 -// GPR_s7 -// GPR_s6 -// GPR_s5 -// GPR_s4 -// GPR_s3 -// GPR_s2 -// GPR_s1 -// GPR_s0 -// GPR_ra -// GPR_fp -// -// Float Point: -// FPR_f31 / fs7 -// FPR_f30 / fs6 -// FPR_f29 / fs5 -// FPR_f28 / fs4 -// FPR_f27 / fs3 -// FPR_f26 / fs2 -// FPR_f25 / fs1 -// FPR_f24 / fs0 -// Extra: -// -.macro PROLOG_WITH_TRANSITION_BLOCK extraParameters = 0, extraLocals = 0, SaveFPRegs = 1 - - __PWTB_SaveFPArgs = \SaveFPRegs - - __PWTB_FloatArgumentRegisters = \extraLocals - - .if ((__PWTB_FloatArgumentRegisters % 16) != 0) - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 - .endif - - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters - - .if (__PWTB_SaveFPArgs == 1) - __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters - .endif - - - __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 96 - - // Including fp, ra, s0-s8, padding, and (a0-a7)arguments. (1+1+9+1)*8 + 8*8. - __PWTB_StackAlloc = __PWTB_TransitionBlock + 96 + 64 - PROLOG_STACK_ALLOC __PWTB_StackAlloc - // $fp,$ra - PROLOG_SAVE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters, 1 - - // First, Spill argument registers. - SAVE_ARGUMENT_REGISTERS $sp, __PWTB_ArgumentRegisters - - // Then, Spill callee saved registers. $sp=$r3. - SAVE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters - - // saving is f12-19. - .if (__PWTB_SaveFPArgs == 1) - SAVE_FLOAT_ARGUMENT_REGISTERS $sp, __PWTB_FloatArgumentRegisters - .endif - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - - RESTORE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters - - EPILOG_RESTORE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters - - EPILOG_STACK_FREE __PWTB_StackAlloc - - jirl $r0, $ra, 0 - -.endm - - -//----------------------------------------------------------------------------- -// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. -// Since this is a tail call argument registers are restored. -// -.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - .if (__PWTB_SaveFPArgs == 1) - RESTORE_FLOAT_ARGUMENT_REGISTERS $sp, __PWTB_FloatArgumentRegisters - .endif - - RESTORE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters - - RESTORE_ARGUMENT_REGISTERS $sp, __PWTB_ArgumentRegisters - - EPILOG_RESTORE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters - - EPILOG_STACK_FREE __PWTB_StackAlloc - -.endm - // Inlined version of GetThreadEEAllocContext. Target cannot be a0. .macro INLINE_GET_ALLOC_CONTEXT_BASE target .ifc \target, $a0 diff --git a/src/coreclr/pal/inc/unixasmmacrosriscv64.inc b/src/coreclr/pal/inc/unixasmmacrosriscv64.inc index 0449565f00958f..ae418806ff6ea6 100644 --- a/src/coreclr/pal/inc/unixasmmacrosriscv64.inc +++ b/src/coreclr/pal/inc/unixasmmacrosriscv64.inc @@ -3,147 +3,6 @@ #include -//----------------------------------------------------------------------------- -// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and -// comprises the entire prolog.The locals must be 8 byte aligned -// -// Save_argument_registers: -// GPR_a7 -// GPR_a6 -// GPR_a5 -// GPR_a4 -// GPR_a3 -// GPR_a2 -// GPR_a1 -// GPR_a0 -// -// Callee Saved Registers: -// GPR_gp -// GPR_tp -// GPR_s11 -// GPR_s10 -// GPR_s9 -// GPR_s8 -// GPR_s7 -// GPR_s6 -// GPR_s5 -// GPR_s4 -// GPR_s3 -// GPR_s2 -// GPR_s1 -// GPR_ra -// GPR_fp -// -// Float Point: -// FPR_f27 / fs11 -// FPR_f26 / fs10 -// FPR_f25 / fs9 -// FPR_f24 / fs8 -// FPR_f23 / fs7 -// FPR_f22 / fs6 -// FPR_f21 / fs5 -// FPR_f20 / fs4 -// FPR_f19 / fs3 -// FPR_f18 / fs2 -// FPR_f9 / fs1 -// FPR_f8 / fs0 -// Extra: -// -// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save -// the callee-saved floating point registers (fs0-fs11) to the stack. -// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. -// -.macro PROLOG_WITH_TRANSITION_BLOCK extraParameters = 0, extraLocals = 0, SaveFPRegs = 1, pushCalleeSavedFloatRegs = 0 - __PWTB_SaveFPArgs = \SaveFPRegs - __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs - - __PWTB_FloatCalleeSavedRegisters = \extraLocals - - // Note, stack (see __PWTB_StackAlloc variable) must be 16 byte aligned. - .if ((__PWTB_FloatCalleeSavedRegisters % 16) != 0) - __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 8 - .endif - - // If pushCalleeSavedFloatRegs is specified, reserve space for fs0-fs11 (12 registers * 8 bytes = 96 bytes) - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 96 - .else - __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters - .endif - - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters - - .if (__PWTB_SaveFPArgs == 1) - __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters - .endif - - - __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 120 + 8 - - // Including fp, ra, s1-s11, tp, gp, padding and (a0-a7)arguments. (1+1+11+1+1)*8 + 8 + 8*8. - __PWTB_StackAlloc = __PWTB_TransitionBlock + 120 + 8 + 64 - PROLOG_STACK_ALLOC __PWTB_StackAlloc - PROLOG_SAVE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters, 1 - - // First, Spill argument registers. - SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - - // Then, Spill callee saved registers. sp=r2. - SAVE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters - - // saving is f10-17. - .if (__PWTB_SaveFPArgs == 1) - SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - .endif - - // Save callee-saved floating point registers if requested (fs0-fs11) - .if (__PWTB_PushCalleeSavedFloatRegs == 1) - fsd fs0, (__PWTB_FloatCalleeSavedRegisters)(sp) - fsd fs1, (__PWTB_FloatCalleeSavedRegisters + 8)(sp) - fsd fs2, (__PWTB_FloatCalleeSavedRegisters + 16)(sp) - fsd fs3, (__PWTB_FloatCalleeSavedRegisters + 24)(sp) - fsd fs4, (__PWTB_FloatCalleeSavedRegisters + 32)(sp) - fsd fs5, (__PWTB_FloatCalleeSavedRegisters + 40)(sp) - fsd fs6, (__PWTB_FloatCalleeSavedRegisters + 48)(sp) - fsd fs7, (__PWTB_FloatCalleeSavedRegisters + 56)(sp) - fsd fs8, (__PWTB_FloatCalleeSavedRegisters + 64)(sp) - fsd fs9, (__PWTB_FloatCalleeSavedRegisters + 72)(sp) - fsd fs10, (__PWTB_FloatCalleeSavedRegisters + 80)(sp) - fsd fs11, (__PWTB_FloatCalleeSavedRegisters + 88)(sp) - .endif - -.endm - -.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - RESTORE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters - - EPILOG_RESTORE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters - - EPILOG_STACK_FREE __PWTB_StackAlloc - - ret -.endm - - -//----------------------------------------------------------------------------- -// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. -// Since this is a tail call argument registers are restored. -// -.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - .if (__PWTB_SaveFPArgs == 1) - RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - .endif - - RESTORE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters - - RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - - EPILOG_RESTORE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters - - EPILOG_STACK_FREE __PWTB_StackAlloc -.endm - // Inlined version of GetThreadEEAllocContext .macro INLINE_GET_ALLOC_CONTEXT_BASE call C_FUNC(GetThreadEEAllocContext) diff --git a/src/coreclr/runtime/amd64/AsmMacros_Unix.inc b/src/coreclr/runtime/amd64/AsmMacros_Unix.inc index 418bae14c03614..b5805268708e74 100644 --- a/src/coreclr/runtime/amd64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/amd64/AsmMacros_Unix.inc @@ -290,3 +290,115 @@ C_FUNC(\Name\()_End): .byte 0xFF .byte 0xE0 .endm + +// Stack layout: +// +// (stack parameters) +// ... +// return address +// CalleeSavedRegisters::rbp +// CalleeSavedRegisters::rbx +// CalleeSavedRegisters::r15 +// CalleeSavedRegisters::r14 +// CalleeSavedRegisters::r13 +// CalleeSavedRegisters::r12 +// ArgumentRegisters::r9 +// ArgumentRegisters::r8 +// ArgumentRegisters::rcx +// ArgumentRegisters::rdx +// ArgumentRegisters::rsi +// ArgumentRegisters::rdi <- __PWTB_StackAlloc, __PWTB_TransitionBlock +// padding to align xmm save area +// xmm7 +// xmm6 +// xmm5 +// xmm4 +// xmm3 +// xmm2 +// xmm1 +// xmm0 <- __PWTB_FloatArgumentRegisters +// extra locals + padding to qword align +.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, stackAllocOnEntry = 0, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 + + set_cfa_register rsp, 8 + + __PWTB_FloatArgumentRegisters = \extraLocals + + .if ((__PWTB_FloatArgumentRegisters % 16) != 0) + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 + .endif + + __PWTB_StackAlloc = __PWTB_FloatArgumentRegisters + 8 * 16 + 8 // 8 floating point registers + __PWTB_TransitionBlock = __PWTB_StackAlloc + __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + + .if \stackAllocOnEntry >= 4*8 + .error "Max supported stackAllocOnEntry is 3*8" + .endif + + .if \stackAllocOnEntry > 0 + .cfi_adjust_cfa_offset \stackAllocOnEntry + .endif + + // PUSH_CALLEE_SAVED_REGISTERS expanded here + + .if \stackAllocOnEntry < 8 + push_nonvol_reg rbp + mov rbp, rsp + .endif + + .if \stackAllocOnEntry < 2*8 + push_nonvol_reg rbx + .endif + + .if \stackAllocOnEntry < 3*8 + push_nonvol_reg r15 + .endif + + push_nonvol_reg r14 + push_nonvol_reg r13 + push_nonvol_reg r12 + + // ArgumentRegisters + PUSH_ARGUMENT_REGISTERS + + .if \stackAllocOnEntry >= 3*8 + mov \stackAllocSpill3, [rsp + 0x48] + save_reg_postrsp r15, 0x48 + .endif + + .if \stackAllocOnEntry >= 2*8 + mov \stackAllocSpill2, [rsp + 0x50] + save_reg_postrsp rbx, 0x50 + .endif + + .if \stackAllocOnEntry >= 8 + mov \stackAllocSpill1, [rsp + 0x58] + save_reg_postrsp rbp, 0x58 + lea rbp, [rsp + 0x58] + .endif + + alloc_stack __PWTB_StackAlloc + SAVE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters + + END_PROLOGUE + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN + + free_stack __PWTB_StackAlloc + SKIP_ARGUMENT_REGISTERS + POP_CALLEE_SAVED_REGISTERS + ret + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters + free_stack __PWTB_StackAlloc + POP_ARGUMENT_REGISTERS + POP_CALLEE_SAVED_REGISTERS + +.endm diff --git a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc index a7a9fb910ef52b..42210db52957c7 100644 --- a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc +++ b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc @@ -343,3 +343,139 @@ endm ;; GC type flags GC_ALLOC_FINALIZE equ 1 + +; Stack layout: +; +; (stack parameters) +; ... +; r9 +; r8 +; rdx +; rcx <- __PWTB_ArgumentRegisters +; return address +; CalleeSavedRegisters::r15 +; CalleeSavedRegisters::r14 +; CalleeSavedRegisters::r13 +; CalleeSavedRegisters::r12 +; CalleeSavedRegisters::rbp +; CalleeSavedRegisters::rbx +; CalleeSavedRegisters::rsi +; CalleeSavedRegisters::rdi <- __PWTB_StackAlloc +; padding to align xmm save area +; xmm3 +; xmm2 +; xmm1 +; xmm0 <- __PWTB_FloatArgumentRegisters +; Optional: Callee saved floating point registers +; xmm15 +; . +; . +; xmm6 +; extra locals + padding to qword align +; callee's r9 +; callee's r8 +; callee's rdx +; callee's rcx + +PROLOG_WITH_TRANSITION_BLOCK macro extraLocals := <0>, calleeSavedFloatRegs := , stackAllocOnEntry := <0>, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 + + __PWTB_FloatArgumentRegisters = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES + extraLocals + + if (__PWTB_FloatArgumentRegisters mod 16) ne 0 + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 + endif + + ifidn , + __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatArgumentRegisters + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 10 * 16 + endif + + __PWTB_StackAlloc = __PWTB_FloatArgumentRegisters + 4 * 16 + 8 + __PWTB_TransitionBlock = __PWTB_StackAlloc + __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 9 * 8 + + .errnz stackAllocOnEntry ge 4*8, Max supported stackAllocOnEntry is 3*8 + + if stackAllocOnEntry gt 0 + .allocstack stackAllocOnEntry + endif + + ; PUSH_CALLEE_SAVED_REGISTERS expanded here + + if stackAllocOnEntry lt 8 + push_nonvol_reg r15 + endif + + if stackAllocOnEntry lt 2*8 + push_nonvol_reg r14 + endif + + if stackAllocOnEntry lt 3*8 + push_nonvol_reg r13 + endif + + push_nonvol_reg r12 + push_nonvol_reg rbp + push_nonvol_reg rbx + push_nonvol_reg rsi + push_nonvol_reg rdi + + alloc_stack __PWTB_StackAlloc + SAVE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters + SAVE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters + + ifidn , + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters], xmm6 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 10h], xmm7 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 20h], xmm8 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 30h], xmm9 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 40h], xmm10 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 50h], xmm11 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 60h], xmm12 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 70h], xmm13 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 80h], xmm14 + movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 90h], xmm15 + endif + + if stackAllocOnEntry ge 3*8 + mov stackAllocSpill3, [rsp + __PWTB_StackAlloc + 28h] + save_reg_postrsp r13, __PWTB_StackAlloc + 28h + endif + + if stackAllocOnEntry ge 2*8 + mov stackAllocSpill2, [rsp + __PWTB_StackAlloc + 30h] + save_reg_postrsp r14, __PWTB_StackAlloc + 30h + endif + + if stackAllocOnEntry ge 8 + mov stackAllocSpill1, [rsp + __PWTB_StackAlloc + 38h] + save_reg_postrsp r15, __PWTB_StackAlloc + 38h + endif + + END_PROLOGUE + + endm + +EPILOG_WITH_TRANSITION_BLOCK macro + + add rsp, __PWTB_StackAlloc + POP_CALLEE_SAVED_REGISTERS + + endm + +EPILOG_WITH_TRANSITION_BLOCK_RETURN macro + + add rsp, __PWTB_StackAlloc + POP_CALLEE_SAVED_REGISTERS + ret + + endm + +EPILOG_WITH_TRANSITION_BLOCK_TAILCALL macro + + RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters + RESTORE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters + add rsp, __PWTB_StackAlloc + POP_CALLEE_SAVED_REGISTERS + + endm diff --git a/src/coreclr/runtime/arm/AsmMacros_Unix.inc b/src/coreclr/runtime/arm/AsmMacros_Unix.inc index 4291e92e462d16..db58c924b666c0 100644 --- a/src/coreclr/runtime/arm/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm/AsmMacros_Unix.inc @@ -232,3 +232,125 @@ C_FUNC(\Name): .long \Var(TLSGD) + (2b - 1b - 4) 3: .endm + +// Stack layout: +// +// (stack parameters) +// ... +// ArgumentRegisters::r3 +// ArgumentRegisters::r2 +// ArgumentRegisters::r1 +// ArgumentRegisters::r0 +// CalleeSavedRegisters::lr +// CalleeSavedRegisters::r11 +// CalleeSavedRegisters::r10 +// CalleeSavedRegisters::r9 +// CalleeSavedRegisters::r8 +// CalleeSavedRegisters::r7 <- r7 +// CalleeSavedRegisters::r6 +// CalleeSavedRegisters::r5 +// CalleeSavedRegisters::r4 <- __PWTB_StackAlloc, __PWTB_TransitionBlock +// padding to align float save area +// d7 +// d6 +// d5 +// d4 +// d3 +// d2 +// d1 +// d0 <- __PWTB_FloatArgumentRegisters +// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) +// d15 +// d14 +// d13 +// d12 +// d11 +// d10 +// d9 +// d8 <- __PWTB_FloatCalleeSavedRegisters +// +// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save +// the callee-saved floating point registers (d8-d15) to the stack. +// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. +// +.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, saveFpArgs = 1, pushArgRegs = 0, pushCalleeSavedFloatRegs = 0 + + __PWTB_FloatCalleeSavedRegisters = \extraLocals + __PWTB_SaveFPArgs = \saveFpArgs + __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs + + // If pushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + .if ((__PWTB_FloatCalleeSavedRegisters % 8) != 0) + __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 4 + .endif + __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 64 + .else + __PWTB_FloatArgumentRegisters = \extraLocals + .endif + + .if (__PWTB_SaveFPArgs == 1) + .if ((__PWTB_FloatArgumentRegisters % 8) != 0) + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 4 + .endif + + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + 8 * 8 + 4 // 8 floating point registers + padding + .else + .if ((__PWTB_FloatArgumentRegisters % 8) == 0) + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 4 + .endif + + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + .endif + + __PWTB_StackAlloc = __PWTB_TransitionBlock + __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 36 + + .ifnc \pushArgRegs, DoNotPushArgRegs + PUSH_ARGUMENT_REGISTERS + .endif + + PUSH_CALLEE_SAVED_REGISTERS + PROLOG_STACK_SAVE_OFFSET r7, #12 + // let r7 point the saved r7 in the stack (clang FP style) + + alloc_stack __PWTB_StackAlloc + + .if (__PWTB_SaveFPArgs == 1) + add r6, sp, #(__PWTB_FloatArgumentRegisters) + vstm r6, {d0-d7} + .endif + + // Save callee-saved floating point registers if requested + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + add r6, sp, #(__PWTB_FloatCalleeSavedRegisters) + vstm r6, {d8-d15} + .endif + + CHECK_STACK_ALIGNMENT + + END_PROLOGUE + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN + + free_stack __PWTB_StackAlloc + POP_CALLEE_SAVED_REGISTERS + free_stack 16 + bx lr + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + .if (__PWTB_SaveFPArgs == 1) + add r6, sp, #(__PWTB_FloatArgumentRegisters) + vldm r6, {d0-d7} + .endif + + free_stack __PWTB_StackAlloc + POP_CALLEE_SAVED_REGISTERS + POP_ARGUMENT_REGISTERS + +.endm diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index 299062b619f573..a69b86a1f4993b 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -305,3 +305,165 @@ C_FUNC(\Name): #endif .endm + +//----------------------------------------------------------------------------- +// Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and +// comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned +// +// Stack layout: +// +// (stack parameters) +// ... +// fp +// lr +// CalleeSavedRegisters::x28 +// CalleeSavedRegisters::x27 +// CalleeSavedRegisters::x26 +// CalleeSavedRegisters::x25 +// CalleeSavedRegisters::x24 +// CalleeSavedRegisters::x23 +// CalleeSavedRegisters::x22 +// CalleeSavedRegisters::x21 +// CalleeSavedRegisters::x20 +// CalleeSavedRegisters::x19 +// CalleeSavedRegisters::x30 (Lr) +// CalleeSavedRegisters::x29 (Fp) +// ArgumentRegisters::x7 +// ArgumentRegisters::x6 +// ArgumentRegisters::x5 +// ArgumentRegisters::x4 +// ArgumentRegisters::x3 +// ArgumentRegisters::x2 +// ArgumentRegisters::x1 +// ArgumentRegisters::x0 +// ArgumentRegisters::x8 +// FloatRegisters::q7 +// FloatRegisters::q6 +// FloatRegisters::q5 +// FloatRegisters::q4 +// FloatRegisters::q3 +// FloatRegisters::q2 +// FloatRegisters::q1 +// FloatRegisters::q0 +// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) +// d15 +// d14 +// d13 +// d12 +// d11 +// d10 +// d9 +// d8 +// +// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save +// the callee-saved floating point registers (d8-d15) to the stack. +// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. +// +.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, SaveFPArgs = 1, SaveGPArgs = 1, pushCalleeSavedFloatRegs = 0 + + __PWTB_FloatCalleeSavedRegisters = \extraLocals + __PWTB_SaveFPArgs = \SaveFPArgs + __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs + + .if ((__PWTB_FloatCalleeSavedRegisters % 16) != 0) + __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 8 + .endif + + // If pushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 64 + .else + __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + .endif + + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + + // Always reserve space for the FP arg regs, even if they are not saved in the PROLOG_WITH_TRANSITION_BLOCK + // Some code using this macro saves them optionally + __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters + + __PWTB_StackAlloc = __PWTB_TransitionBlock + __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 104 + __PWTB_ArgumentRegister_FirstArg = __PWTB_ArgumentRegisters + 8 + + PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -176 + // Spill callee saved registers + PROLOG_SAVE_REG_PAIR x19, x20, 16 + PROLOG_SAVE_REG_PAIR x21, x22, 32 + PROLOG_SAVE_REG_PAIR x23, x24, 48 + PROLOG_SAVE_REG_PAIR x25, x26, 64 + PROLOG_SAVE_REG_PAIR x27, x28, 80 + + // Allocate space for the rest of the frame + PROLOG_STACK_ALLOC __PWTB_StackAlloc + + .if (\SaveGPArgs == 1) + // Spill argument registers. + SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + .endif + + .if (__PWTB_SaveFPArgs == 1) + SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + .endif + + // Save callee-saved floating point registers if requested + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + PROLOG_SAVE_REG_PAIR d8, d9, __PWTB_FloatCalleeSavedRegisters + PROLOG_SAVE_REG_PAIR d10, d11, __PWTB_FloatCalleeSavedRegisters + 16 + PROLOG_SAVE_REG_PAIR d12, d13, __PWTB_FloatCalleeSavedRegisters + 32 + PROLOG_SAVE_REG_PAIR d14, d15, __PWTB_FloatCalleeSavedRegisters + 48 + .endif + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN + + EPILOG_STACK_FREE __PWTB_StackAlloc + + EPILOG_RESTORE_REG_PAIR x19, x20, 16 + EPILOG_RESTORE_REG_PAIR x21, x22, 32 + EPILOG_RESTORE_REG_PAIR x23, x24, 48 + EPILOG_RESTORE_REG_PAIR x25, x26, 64 + EPILOG_RESTORE_REG_PAIR x27, x28, 80 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + ret + +.endm + +// Same as EPILOG_WITH_TRANSITION_BLOCK_RETURN but skips restoring x21 (SwiftError) +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN_SKIP_SWIFT_ERROR + + EPILOG_STACK_FREE __PWTB_StackAlloc + + EPILOG_RESTORE_REG_PAIR x19, x20, 16 + EPILOG_RESTORE_REG x22, 40 + EPILOG_RESTORE_REG_PAIR x23, x24, 48 + EPILOG_RESTORE_REG_PAIR x25, x26, 64 + EPILOG_RESTORE_REG_PAIR x27, x28, 80 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + ret + +.endm + +//----------------------------------------------------------------------------- +// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. +// Since this is a tail call argument registers are restored. +// +.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + .if (__PWTB_SaveFPArgs == 1) + RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + .endif + + RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + + EPILOG_STACK_FREE __PWTB_StackAlloc + + EPILOG_RESTORE_REG_PAIR x19, x20, 16 + EPILOG_RESTORE_REG_PAIR x21, x22, 32 + EPILOG_RESTORE_REG_PAIR x23, x24, 48 + EPILOG_RESTORE_REG_PAIR x25, x26, 64 + EPILOG_RESTORE_REG_PAIR x27, x28, 80 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + +.endm diff --git a/src/coreclr/runtime/arm64/AsmMacros_Windows.h b/src/coreclr/runtime/arm64/AsmMacros_Windows.h index 9482e60f200c36..842c19196f2330 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Windows.h +++ b/src/coreclr/runtime/arm64/AsmMacros_Windows.h @@ -255,3 +255,136 @@ __PWTB_RESTORE_FLOAT_ARGUMENT_REGISTERS_OFFSET SETA 0 GC_ALLOC_FINALIZE equ 1 GC_ALLOC_ALIGN8_BIAS equ 4 GC_ALLOC_ALIGN8 equ 8 + +;----------------------------------------------------------------------------- +; The following group of macros assist in implementing prologs and epilogs for methods that set up some +; subclass of TransitionFrame. They ensure that the SP is 16-byte aligned at the conclusion of the prolog + +;----------------------------------------------------------------------------- +; Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and +; comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned +; +; $pushCalleeSavedFloatRegs - Optional parameter. If set to PushCalleeSavedFloatRegs, the macro will also save +; the callee-saved floating point registers (d8-d15) to the stack. These registers +; are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. +; + MACRO + PROLOG_WITH_TRANSITION_BLOCK $extraLocals, $SaveFPArgs, $pushCalleeSavedFloatRegs + + GBLA __PWTB_FloatArgumentRegisters + GBLA __PWTB_FloatCalleeSavedRegisters + GBLA __PWTB_ArgumentRegisters + GBLA __PWTB_ArgumentRegister_FirstArg ; We save the x8 register ahead of the first argument, so this + ; is different from the start of the argument register save area. + GBLA __PWTB_StackAlloc + GBLA __PWTB_TransitionBlock + GBLL __PWTB_SaveFPArgs + + IF "$SaveFPArgs" != "" +__PWTB_SaveFPArgs SETL $SaveFPArgs + ELSE +__PWTB_SaveFPArgs SETL {true} + ENDIF + + IF "$extraLocals" != "" +__PWTB_FloatCalleeSavedRegisters SETA $extraLocals + ELSE +__PWTB_FloatCalleeSavedRegisters SETA 0 + ENDIF + + IF __PWTB_FloatCalleeSavedRegisters:MOD:16 != 0 +__PWTB_FloatCalleeSavedRegisters SETA __PWTB_FloatCalleeSavedRegisters + 8 + ENDIF + + ; If PushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) + IF "$pushCalleeSavedFloatRegs" == "PushCalleeSavedFloatRegs" +__PWTB_FloatArgumentRegisters SETA __PWTB_FloatCalleeSavedRegisters + 64 + ELSE +__PWTB_FloatArgumentRegisters SETA __PWTB_FloatCalleeSavedRegisters + ENDIF + + IF __PWTB_SaveFPArgs +__PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters + SIZEOF__FloatArgumentRegisters + ELSE +__PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters + ENDIF + +__PWTB_StackAlloc SETA __PWTB_TransitionBlock +__PWTB_ArgumentRegisters SETA __PWTB_StackAlloc + 104 +__PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 + + PROLOG_SAVE_REG_PAIR fp, lr, #-176! + ; Spill callee saved registers + PROLOG_SAVE_REG_PAIR x19, x20, #16 + PROLOG_SAVE_REG_PAIR x21, x22, #32 + PROLOG_SAVE_REG_PAIR x23, x24, #48 + PROLOG_SAVE_REG_PAIR x25, x26, #64 + PROLOG_SAVE_REG_PAIR x27, x28, #80 + + ; Allocate space for the rest of the frame + PROLOG_STACK_ALLOC __PWTB_StackAlloc + + ; Spill argument registers. + SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + + IF __PWTB_SaveFPArgs + SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + ENDIF + + ; Save callee-saved floating point registers if requested + IF "$pushCalleeSavedFloatRegs" == "PushCalleeSavedFloatRegs" + stp d8, d9, [sp, #__PWTB_FloatCalleeSavedRegisters] + stp d10, d11, [sp, #(__PWTB_FloatCalleeSavedRegisters + 16)] + stp d12, d13, [sp, #(__PWTB_FloatCalleeSavedRegisters + 32)] + stp d14, d15, [sp, #(__PWTB_FloatCalleeSavedRegisters + 48)] + ENDIF + + MEND + +;----------------------------------------------------------------------------- +; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK. +; + MACRO + EPILOG_WITH_TRANSITION_BLOCK + + EPILOG_STACK_FREE __PWTB_StackAlloc + + EPILOG_RESTORE_REG_PAIR x19, x20, #16 + EPILOG_RESTORE_REG_PAIR x21, x22, #32 + EPILOG_RESTORE_REG_PAIR x23, x24, #48 + EPILOG_RESTORE_REG_PAIR x25, x26, #64 + EPILOG_RESTORE_REG_PAIR x27, x28, #80 + EPILOG_RESTORE_REG_PAIR fp, lr, #176! + MEND + +;----------------------------------------------------------------------------- +; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and returns to caller. +; + MACRO + EPILOG_WITH_TRANSITION_BLOCK_RETURN + + EPILOG_WITH_TRANSITION_BLOCK + EPILOG_RETURN + MEND + +;----------------------------------------------------------------------------- +; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. +; Since this is a tail call argument registers are restored. +; + MACRO + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + IF __PWTB_SaveFPArgs + RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + ENDIF + + RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + EPILOG_STACK_FREE __PWTB_StackAlloc + + EPILOG_RESTORE_REG_PAIR x19, x20, #16 + EPILOG_RESTORE_REG_PAIR x21, x22, #32 + EPILOG_RESTORE_REG_PAIR x23, x24, #48 + EPILOG_RESTORE_REG_PAIR x25, x26, #64 + EPILOG_RESTORE_REG_PAIR x27, x28, #80 + EPILOG_RESTORE_REG_PAIR fp, lr, #176! + MEND diff --git a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc index 405f2337f6eef0..3b2d583e1d0e06 100644 --- a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc @@ -308,3 +308,113 @@ C_FUNC(\Name\()_End): ld.d $ra, $sp, 8 addi.d $sp, $sp, 16 .endm + +//----------------------------------------------------------------------------- +// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and +// comprises the entire prolog.The locals must be 8 byte aligned +// +// Save_argument_registers: +// GPR_a7 +// GPR_a6 +// GPR_a5 +// GPR_a4 +// GPR_a3 +// GPR_a2 +// GPR_a1 +// GPR_a0 +// +// General Registers: +// GPR_s8 +// GPR_s7 +// GPR_s6 +// GPR_s5 +// GPR_s4 +// GPR_s3 +// GPR_s2 +// GPR_s1 +// GPR_s0 +// GPR_ra +// GPR_fp +// +// Float Point: +// FPR_f31 / fs7 +// FPR_f30 / fs6 +// FPR_f29 / fs5 +// FPR_f28 / fs4 +// FPR_f27 / fs3 +// FPR_f26 / fs2 +// FPR_f25 / fs1 +// FPR_f24 / fs0 +// Extra: +// +.macro PROLOG_WITH_TRANSITION_BLOCK extraParameters = 0, extraLocals = 0, SaveFPRegs = 1 + + __PWTB_SaveFPArgs = \SaveFPRegs + + __PWTB_FloatArgumentRegisters = \extraLocals + + .if ((__PWTB_FloatArgumentRegisters % 16) != 0) + __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 + .endif + + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + + .if (__PWTB_SaveFPArgs == 1) + __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters + .endif + + + __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock + __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 96 + + // Including fp, ra, s0-s8, padding, and (a0-a7)arguments. (1+1+9+1)*8 + 8*8. + __PWTB_StackAlloc = __PWTB_TransitionBlock + 96 + 64 + PROLOG_STACK_ALLOC __PWTB_StackAlloc + // $fp,$ra + PROLOG_SAVE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters, 1 + + // First, Spill argument registers. + SAVE_ARGUMENT_REGISTERS $sp, __PWTB_ArgumentRegisters + + // Then, Spill callee saved registers. $sp=$r3. + SAVE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters + + // saving is f12-19. + .if (__PWTB_SaveFPArgs == 1) + SAVE_FLOAT_ARGUMENT_REGISTERS $sp, __PWTB_FloatArgumentRegisters + .endif + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN + + RESTORE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters + + EPILOG_RESTORE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters + + EPILOG_STACK_FREE __PWTB_StackAlloc + + jirl $r0, $ra, 0 + +.endm + + +//----------------------------------------------------------------------------- +// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. +// Since this is a tail call argument registers are restored. +// +.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + .if (__PWTB_SaveFPArgs == 1) + RESTORE_FLOAT_ARGUMENT_REGISTERS $sp, __PWTB_FloatArgumentRegisters + .endif + + RESTORE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters + + RESTORE_ARGUMENT_REGISTERS $sp, __PWTB_ArgumentRegisters + + EPILOG_RESTORE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters + + EPILOG_STACK_FREE __PWTB_StackAlloc + +.endm diff --git a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc index c8152f22cf72f7..a8ca29dddad2c1 100644 --- a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc @@ -340,3 +340,144 @@ C_FUNC(\Name): */ .endm + +//----------------------------------------------------------------------------- +// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and +// comprises the entire prolog.The locals must be 8 byte aligned +// +// Save_argument_registers: +// GPR_a7 +// GPR_a6 +// GPR_a5 +// GPR_a4 +// GPR_a3 +// GPR_a2 +// GPR_a1 +// GPR_a0 +// +// Callee Saved Registers: +// GPR_gp +// GPR_tp +// GPR_s11 +// GPR_s10 +// GPR_s9 +// GPR_s8 +// GPR_s7 +// GPR_s6 +// GPR_s5 +// GPR_s4 +// GPR_s3 +// GPR_s2 +// GPR_s1 +// GPR_ra +// GPR_fp +// +// Float Point: +// FPR_f27 / fs11 +// FPR_f26 / fs10 +// FPR_f25 / fs9 +// FPR_f24 / fs8 +// FPR_f23 / fs7 +// FPR_f22 / fs6 +// FPR_f21 / fs5 +// FPR_f20 / fs4 +// FPR_f19 / fs3 +// FPR_f18 / fs2 +// FPR_f9 / fs1 +// FPR_f8 / fs0 +// Extra: +// +// pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save +// the callee-saved floating point registers (fs0-fs11) to the stack. +// These registers are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. +// +.macro PROLOG_WITH_TRANSITION_BLOCK extraParameters = 0, extraLocals = 0, SaveFPRegs = 1, pushCalleeSavedFloatRegs = 0 + __PWTB_SaveFPArgs = \SaveFPRegs + __PWTB_PushCalleeSavedFloatRegs = \pushCalleeSavedFloatRegs + + __PWTB_FloatCalleeSavedRegisters = \extraLocals + + // Note, stack (see __PWTB_StackAlloc variable) must be 16 byte aligned. + .if ((__PWTB_FloatCalleeSavedRegisters % 16) != 0) + __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatCalleeSavedRegisters + 8 + .endif + + // If pushCalleeSavedFloatRegs is specified, reserve space for fs0-fs11 (12 registers * 8 bytes = 96 bytes) + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + 96 + .else + __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters + .endif + + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + + .if (__PWTB_SaveFPArgs == 1) + __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters + .endif + + + __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock + __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 120 + 8 + + // Including fp, ra, s1-s11, tp, gp, padding and (a0-a7)arguments. (1+1+11+1+1)*8 + 8 + 8*8. + __PWTB_StackAlloc = __PWTB_TransitionBlock + 120 + 8 + 64 + PROLOG_STACK_ALLOC __PWTB_StackAlloc + PROLOG_SAVE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters, 1 + + // First, Spill argument registers. + SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + + // Then, Spill callee saved registers. sp=r2. + SAVE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters + + // saving is f10-17. + .if (__PWTB_SaveFPArgs == 1) + SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + .endif + + // Save callee-saved floating point registers if requested (fs0-fs11) + .if (__PWTB_PushCalleeSavedFloatRegs == 1) + fsd fs0, (__PWTB_FloatCalleeSavedRegisters)(sp) + fsd fs1, (__PWTB_FloatCalleeSavedRegisters + 8)(sp) + fsd fs2, (__PWTB_FloatCalleeSavedRegisters + 16)(sp) + fsd fs3, (__PWTB_FloatCalleeSavedRegisters + 24)(sp) + fsd fs4, (__PWTB_FloatCalleeSavedRegisters + 32)(sp) + fsd fs5, (__PWTB_FloatCalleeSavedRegisters + 40)(sp) + fsd fs6, (__PWTB_FloatCalleeSavedRegisters + 48)(sp) + fsd fs7, (__PWTB_FloatCalleeSavedRegisters + 56)(sp) + fsd fs8, (__PWTB_FloatCalleeSavedRegisters + 64)(sp) + fsd fs9, (__PWTB_FloatCalleeSavedRegisters + 72)(sp) + fsd fs10, (__PWTB_FloatCalleeSavedRegisters + 80)(sp) + fsd fs11, (__PWTB_FloatCalleeSavedRegisters + 88)(sp) + .endif + +.endm + +.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN + RESTORE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters + + EPILOG_RESTORE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters + + EPILOG_STACK_FREE __PWTB_StackAlloc + + ret +.endm + + +//----------------------------------------------------------------------------- +// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. +// Since this is a tail call argument registers are restored. +// +.macro EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + .if (__PWTB_SaveFPArgs == 1) + RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters + .endif + + RESTORE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters + + RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters + + EPILOG_RESTORE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters + + EPILOG_STACK_FREE __PWTB_StackAlloc +.endm diff --git a/src/coreclr/vm/amd64/AsmMacros.inc b/src/coreclr/vm/amd64/AsmMacros.inc index 97225eff78ea90..823a5c20fe3f23 100644 --- a/src/coreclr/vm/amd64/AsmMacros.inc +++ b/src/coreclr/vm/amd64/AsmMacros.inc @@ -36,142 +36,6 @@ Section ends endm -; Stack layout: -; -; (stack parameters) -; ... -; r9 -; r8 -; rdx -; rcx <- __PWTB_ArgumentRegisters -; return address -; CalleeSavedRegisters::r15 -; CalleeSavedRegisters::r14 -; CalleeSavedRegisters::r13 -; CalleeSavedRegisters::r12 -; CalleeSavedRegisters::rbp -; CalleeSavedRegisters::rbx -; CalleeSavedRegisters::rsi -; CalleeSavedRegisters::rdi <- __PWTB_StackAlloc -; padding to align xmm save area -; xmm3 -; xmm2 -; xmm1 -; xmm0 <- __PWTB_FloatArgumentRegisters -; Optional: Callee saved floating point registers -; xmm15 -; . -; . -; xmm6 -; extra locals + padding to qword align -; callee's r9 -; callee's r8 -; callee's rdx -; callee's rcx - -PROLOG_WITH_TRANSITION_BLOCK macro extraLocals := <0>, calleeSavedFloatRegs := , stackAllocOnEntry := <0>, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 - - __PWTB_FloatArgumentRegisters = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES + extraLocals - - if (__PWTB_FloatArgumentRegisters mod 16) ne 0 - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 - endif - - ifidn , - __PWTB_FloatCalleeSavedRegisters = __PWTB_FloatArgumentRegisters - __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 10 * 16 - endif - - __PWTB_StackAlloc = __PWTB_FloatArgumentRegisters + 4 * 16 + 8 - __PWTB_TransitionBlock = __PWTB_StackAlloc - __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 9 * 8 - - .errnz stackAllocOnEntry ge 4*8, Max supported stackAllocOnEntry is 3*8 - - if stackAllocOnEntry gt 0 - .allocstack stackAllocOnEntry - endif - - ; PUSH_CALLEE_SAVED_REGISTERS expanded here - - if stackAllocOnEntry lt 8 - push_nonvol_reg r15 - endif - - if stackAllocOnEntry lt 2*8 - push_nonvol_reg r14 - endif - - if stackAllocOnEntry lt 3*8 - push_nonvol_reg r13 - endif - - push_nonvol_reg r12 - push_nonvol_reg rbp - push_nonvol_reg rbx - push_nonvol_reg rsi - push_nonvol_reg rdi - - alloc_stack __PWTB_StackAlloc - SAVE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters - SAVE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters - - ifidn , - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters], xmm6 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 10h], xmm7 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 20h], xmm8 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 30h], xmm9 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 40h], xmm10 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 50h], xmm11 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 60h], xmm12 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 70h], xmm13 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 80h], xmm14 - movdqa [rsp + __PWTB_FloatCalleeSavedRegisters + 90h], xmm15 - endif - - if stackAllocOnEntry ge 3*8 - mov stackAllocSpill3, [rsp + __PWTB_StackAlloc + 28h] - save_reg_postrsp r13, __PWTB_StackAlloc + 28h - endif - - if stackAllocOnEntry ge 2*8 - mov stackAllocSpill2, [rsp + __PWTB_StackAlloc + 30h] - save_reg_postrsp r14, __PWTB_StackAlloc + 30h - endif - - if stackAllocOnEntry ge 8 - mov stackAllocSpill1, [rsp + __PWTB_StackAlloc + 38h] - save_reg_postrsp r15, __PWTB_StackAlloc + 38h - endif - - END_PROLOGUE - - endm - -EPILOG_WITH_TRANSITION_BLOCK macro - - add rsp, __PWTB_StackAlloc - POP_CALLEE_SAVED_REGISTERS - - endm - -EPILOG_WITH_TRANSITION_BLOCK_RETURN macro - - add rsp, __PWTB_StackAlloc - POP_CALLEE_SAVED_REGISTERS - ret - - endm - -EPILOG_WITH_TRANSITION_BLOCK_TAILCALL macro - - RESTORE_FLOAT_ARGUMENT_REGISTERS __PWTB_FloatArgumentRegisters - RESTORE_ARGUMENT_REGISTERS __PWTB_ArgumentRegisters - add rsp, __PWTB_StackAlloc - POP_CALLEE_SAVED_REGISTERS - - endm - t_runtime_thread_locals TEXTEQU ; Inlined version of GetThreadEEAllocContext. Trashes RegTrash, r11 diff --git a/src/coreclr/vm/arm64/asmmacros.h b/src/coreclr/vm/arm64/asmmacros.h index 43e94127c09999..d3b1b92f76d36e 100644 --- a/src/coreclr/vm/arm64/asmmacros.h +++ b/src/coreclr/vm/arm64/asmmacros.h @@ -3,139 +3,6 @@ #include "arm64/AsmMacros_Windows.h" -;----------------------------------------------------------------------------- -; The following group of macros assist in implementing prologs and epilogs for methods that set up some -; subclass of TransitionFrame. They ensure that the SP is 16-byte aligned at the conclusion of the prolog - -;----------------------------------------------------------------------------- -; Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and -; comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned -; -; $pushCalleeSavedFloatRegs - Optional parameter. If set to PushCalleeSavedFloatRegs, the macro will also save -; the callee-saved floating point registers (d8-d15) to the stack. These registers -; are NOT restored by the EPILOG_WITH_TRANSITION_BLOCK variants. -; - MACRO - PROLOG_WITH_TRANSITION_BLOCK $extraLocals, $SaveFPArgs, $pushCalleeSavedFloatRegs - - GBLA __PWTB_FloatArgumentRegisters - GBLA __PWTB_FloatCalleeSavedRegisters - GBLA __PWTB_ArgumentRegisters - GBLA __PWTB_ArgumentRegister_FirstArg ; We save the x8 register ahead of the first argument, so this - ; is different from the start of the argument register save area. - GBLA __PWTB_StackAlloc - GBLA __PWTB_TransitionBlock - GBLL __PWTB_SaveFPArgs - - IF "$SaveFPArgs" != "" -__PWTB_SaveFPArgs SETL $SaveFPArgs - ELSE -__PWTB_SaveFPArgs SETL {true} - ENDIF - - IF "$extraLocals" != "" -__PWTB_FloatCalleeSavedRegisters SETA $extraLocals - ELSE -__PWTB_FloatCalleeSavedRegisters SETA 0 - ENDIF - - IF __PWTB_FloatCalleeSavedRegisters:MOD:16 != 0 -__PWTB_FloatCalleeSavedRegisters SETA __PWTB_FloatCalleeSavedRegisters + 8 - ENDIF - - ; If PushCalleeSavedFloatRegs is specified, reserve space for d8-d15 (8 registers * 8 bytes = 64 bytes) - IF "$pushCalleeSavedFloatRegs" == "PushCalleeSavedFloatRegs" -__PWTB_FloatArgumentRegisters SETA __PWTB_FloatCalleeSavedRegisters + 64 - ELSE -__PWTB_FloatArgumentRegisters SETA __PWTB_FloatCalleeSavedRegisters - ENDIF - - IF __PWTB_SaveFPArgs -__PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters + SIZEOF__FloatArgumentRegisters - ELSE -__PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters - ENDIF - -__PWTB_StackAlloc SETA __PWTB_TransitionBlock -__PWTB_ArgumentRegisters SETA __PWTB_StackAlloc + 104 -__PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 - - PROLOG_SAVE_REG_PAIR fp, lr, #-176! - ; Spill callee saved registers - PROLOG_SAVE_REG_PAIR x19, x20, #16 - PROLOG_SAVE_REG_PAIR x21, x22, #32 - PROLOG_SAVE_REG_PAIR x23, x24, #48 - PROLOG_SAVE_REG_PAIR x25, x26, #64 - PROLOG_SAVE_REG_PAIR x27, x28, #80 - - ; Allocate space for the rest of the frame - PROLOG_STACK_ALLOC __PWTB_StackAlloc - - ; Spill argument registers. - SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - - IF __PWTB_SaveFPArgs - SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - ENDIF - - ; Save callee-saved floating point registers if requested - IF "$pushCalleeSavedFloatRegs" == "PushCalleeSavedFloatRegs" - stp d8, d9, [sp, #__PWTB_FloatCalleeSavedRegisters] - stp d10, d11, [sp, #(__PWTB_FloatCalleeSavedRegisters + 16)] - stp d12, d13, [sp, #(__PWTB_FloatCalleeSavedRegisters + 32)] - stp d14, d15, [sp, #(__PWTB_FloatCalleeSavedRegisters + 48)] - ENDIF - - MEND - -;----------------------------------------------------------------------------- -; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK. -; - MACRO - EPILOG_WITH_TRANSITION_BLOCK - - EPILOG_STACK_FREE __PWTB_StackAlloc - - EPILOG_RESTORE_REG_PAIR x19, x20, #16 - EPILOG_RESTORE_REG_PAIR x21, x22, #32 - EPILOG_RESTORE_REG_PAIR x23, x24, #48 - EPILOG_RESTORE_REG_PAIR x25, x26, #64 - EPILOG_RESTORE_REG_PAIR x27, x28, #80 - EPILOG_RESTORE_REG_PAIR fp, lr, #176! - MEND - -;----------------------------------------------------------------------------- -; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and returns to caller. -; - MACRO - EPILOG_WITH_TRANSITION_BLOCK_RETURN - - EPILOG_WITH_TRANSITION_BLOCK - EPILOG_RETURN - MEND - -;----------------------------------------------------------------------------- -; Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling. -; Since this is a tail call argument registers are restored. -; - MACRO - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - IF __PWTB_SaveFPArgs - RESTORE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters - ENDIF - - RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - EPILOG_STACK_FREE __PWTB_StackAlloc - - EPILOG_RESTORE_REG_PAIR x19, x20, #16 - EPILOG_RESTORE_REG_PAIR x21, x22, #32 - EPILOG_RESTORE_REG_PAIR x23, x24, #48 - EPILOG_RESTORE_REG_PAIR x25, x26, #64 - EPILOG_RESTORE_REG_PAIR x27, x28, #80 - EPILOG_RESTORE_REG_PAIR fp, lr, #176! - MEND - ;----------------------------------------------------------------------------- ; Macros used for shared allocation helpers diff --git a/src/coreclr/vm/eetwain.cpp b/src/coreclr/vm/eetwain.cpp index 14776c3e8f21b7..a077070c00e569 100644 --- a/src/coreclr/vm/eetwain.cpp +++ b/src/coreclr/vm/eetwain.cpp @@ -1817,14 +1817,14 @@ unsigned int EECodeManager::GetFrameSize(GCInfoToken gcInfoToken) /*****************************************************************************/ -// This is an assembly helper that enables us to call into EH funclets. +// Calls a catch or finally funclet. EXTERN_C UINT_PTR F_CALL_CONV CallEHFunclet(Object *pThrowable, UINT_PTR pFuncletToInvoke, CONTEXT *pContext, UINT_PTR *pFuncletCallerSP); -// This is an assembly helper that enables us to call into EH filter funclets. +// Calls a filter funclet. EXTERN_C int F_CALL_CONV CallEHFilterFunclet(Object *pThrowable, CONTEXT *pContext, UINT_PTR pFuncletToInvoke, UINT_PTR *pFuncletCallerSP); #ifndef TARGET_WASM -typedef DWORD_PTR (HandlerFn)(UINT_PTR uStackFrame, Object* pExceptionObj); +typedef UINT_PTR (HandlerFn)(UINT_PTR uStackFrame, Object* pExceptionObj); #else typedef TADDR HandlerFn; #endif // TARGET_WASM @@ -1846,9 +1846,9 @@ static inline UINT_PTR CastHandlerFn(HandlerFn *pfnHandler) // * Catch funclet: address to resume at after the catch returns // * Finally funclet: unused // * Filter funclet: result of the filter funclet (EXCEPTION_CONTINUE_SEARCH (0) or EXCEPTION_EXECUTE_HANDLER (1)) -DWORD_PTR EECodeManager::CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilterFunclet) +UINT_PTR EECodeManager::CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilterFunclet) { - DWORD_PTR dwResult = 0; + UINT_PTR dwResult = 0; HandlerFn* pfnHandler = (HandlerFn*)pHandler; // Since the actual caller of the funclet is the assembly helper, pass the reference @@ -2007,7 +2007,7 @@ extern "C" DWORD_PTR STDCALL CallInterpreterFuncletWorker(OBJECTREF throwable, v } } -DWORD_PTR InterpreterCodeManager::CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter) +UINT_PTR InterpreterCodeManager::CallFunclet(OBJECTREF throwable, void* pHandler, REGDISPLAY *pRD, ExInfo *pExInfo, bool isFilter) { #if !defined(TARGET_WASM) // Call the ASM helper which creates a real TransitionBlock diff --git a/src/coreclr/vm/exceptionhandling.cpp b/src/coreclr/vm/exceptionhandling.cpp index d875af4e66ea63..3ad83a114a4653 100644 --- a/src/coreclr/vm/exceptionhandling.cpp +++ b/src/coreclr/vm/exceptionhandling.cpp @@ -3045,7 +3045,7 @@ void CallCatchFunclet(BYTE* pHandlerIP, REGDISPLAY* pvRegDisplay, ExInfo* exInfo pThread->DecPreventAbort(); exInfo->m_ScannedStackRange.ExtendUpperBound(exInfo->m_frameIter.m_crawl.GetRegisterSet()->SP); - DWORD_PTR dwResumePC = 0; + UINT_PTR dwResumePC = 0; UINT_PTR callerTargetSp = 0; #if defined(HOST_AMD64) && defined(HOST_WINDOWS) @@ -3333,7 +3333,7 @@ extern "C" CLR_BOOL QCALLTYPE CallFilterFunclet(QCall::ObjectHandleOnStack excep { QCALL_CONTRACT; - DWORD_PTR dwResult = 0; + UINT_PTR dwResult = 0; BEGIN_QCALL; From c7820d3ffb41f4c2cb01b6fa2c377dbc294e15d0 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:46:47 +0200 Subject: [PATCH 02/14] Strip the Thumb bit in IsValidReturnAddress The hijack code passes the return address from the stack, which has the Thumb bit set on arm, so the return address of a thunk such as RhpInterfaceDispatchSlow did not match its label. Co-authored-by: Cursor --- src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp index c1017a3a0f9040..fdf5ab843ee74d 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp @@ -2354,6 +2354,8 @@ bool StackFrameIterator::IsNonEHThunk(ReturnAddressCategory category) bool StackFrameIterator::IsValidReturnAddress(PTR_VOID pvAddress) { + pvAddress = dac_cast(ReturnAddressToCanonicalPC(dac_cast(pvAddress))); + // These are return addresses into functions that call into managed (non-funclet) code, so we might see // them as hijacked return addresses. ReturnAddressCategory category = CategorizeUnadjustedReturnAddress(pvAddress); From 72b465cd58b29375fc3064baf29843f084d06fee Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:06:37 +0200 Subject: [PATCH 03/14] Fix lr unwind rule after INLINE_GETTHREAD on arm64 `INLINE_GETTHREAD` saves `x0` and `lr` around the TLS lookup, and the `EPILOG_RESTORE_REG_PAIR_INDEXED` that pops them emits `.cfi_restore` for both. That resets `lr` to its rule at function entry, which is wrong in functions that saved `lr` in their prologue before using the macro, such as `CallEHFunclet`. For the rest of such a function the CFI claims that `lr` holds the caller's return address, so after the call into a funclet lldb computes `CallEHFunclet` as its own caller and stops unwinding. Wrap the macro in `.cfi_remember_state` / `.cfi_restore_state` so the caller's unwind rules are restored as they were. Co-authored-by: Cursor --- src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc index c3f266550fe993..aef852cc5a5c07 100644 --- a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc +++ b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc @@ -13,9 +13,13 @@ // Can trash volatile FP/SIMD registers. // Only for functions whose CFA is sp-based (no fp frame): the x0/lr save adjusts the CFA offset. .macro INLINE_GETTHREAD target + // The caller may have saved lr already, so restore its unwind rules instead of resetting + // them to the state at function entry. + .cfi_remember_state PROLOG_SAVE_REG_PAIR_INDEXED x0, lr, -0x10 INLINE_GET_TLS_VAR \target, C_FUNC(tls_CurrentThread) EPILOG_RESTORE_REG_PAIR_INDEXED x0, lr, 0x10 + .cfi_restore_state .endm // Target cannot be x0 or x2. Preserves x0-x2, which the allocation helpers keep live across it. From 0625694c38fde9deb9ca1b12e8b09ad3fed6b7d1 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:13:43 +0200 Subject: [PATCH 04/14] Strip the Thumb bit at the arm hijack call site `ReturnAddressToCanonicalPC` in `IsValidReturnAddress` also strips the PAC signature on arm64, but its callers pass either an already stripped return address or an unsigned context IP. Only arm needed a change, so clear the Thumb bit at the hijack call site instead, the same way arm64 calls `PacStripPtr` there. Co-authored-by: Cursor --- src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp | 2 -- src/coreclr/nativeaot/Runtime/thread.cpp | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp index fdf5ab843ee74d..c1017a3a0f9040 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp @@ -2354,8 +2354,6 @@ bool StackFrameIterator::IsNonEHThunk(ReturnAddressCategory category) bool StackFrameIterator::IsValidReturnAddress(PTR_VOID pvAddress) { - pvAddress = dac_cast(ReturnAddressToCanonicalPC(dac_cast(pvAddress))); - // These are return addresses into functions that call into managed (non-funclet) code, so we might see // them as hijacked return addresses. ReturnAddressCategory category = CategorizeUnadjustedReturnAddress(pvAddress); diff --git a/src/coreclr/nativeaot/Runtime/thread.cpp b/src/coreclr/nativeaot/Runtime/thread.cpp index cdd81aadc8d860..6de0b28adbcf31 100644 --- a/src/coreclr/nativeaot/Runtime/thread.cpp +++ b/src/coreclr/nativeaot/Runtime/thread.cpp @@ -21,6 +21,7 @@ #include "threadstore.h" #include "threadstore.inl" #include "thread.inl" +#include "CommonMacros.inl" #include "RuntimeInstance.h" #include "shash.h" #include "rhbinder.h" @@ -681,9 +682,11 @@ void Thread::HijackReturnAddressWorker(StackFrameIterator* frameIterator, Hijack #if defined(TARGET_ARM64) ASSERT(StackFrameIterator::IsValidReturnAddress(PacStripPtr(pvRetAddr))); +#elif defined(TARGET_ARM) + ASSERT(StackFrameIterator::IsValidReturnAddress((void*)PCODEToPINSTR((PCODE)pvRetAddr))); #else ASSERT(StackFrameIterator::IsValidReturnAddress(pvRetAddr)); -#endif // TARGET_ARM64 +#endif m_ppvHijackedReturnAddressLocation = ppvRetAddrLocation; m_pvHijackedReturnAddress = pvRetAddr; From c9e5649336a87ed18e847057bf0b7123c58babda Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:17:28 +0200 Subject: [PATCH 05/14] Pass isCanonicalPC to IsValidReturnAddress The hijack code passes the return address from the stack, so let IsValidReturnAddress canonicalize it instead of duplicating ReturnAddressToCanonicalPC at the call site. Co-authored-by: Cursor --- src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp | 7 +++++-- src/coreclr/nativeaot/Runtime/StackFrameIterator.h | 2 +- src/coreclr/nativeaot/Runtime/thread.cpp | 9 +-------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp index c1017a3a0f9040..89f1eabccf09b3 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp @@ -658,7 +658,7 @@ void StackFrameIterator::InternalInit(Thread * pThreadToWalk, NATIVE_CONTEXT* pC // This codepath is used by the hijack stackwalk. The IP must be in managed code // or in a conservatively reported assembly thunk. - ASSERT(IsValidReturnAddress((void*)pCtx->GetIp())); + ASSERT(IsValidReturnAddress((void*)pCtx->GetIp(), true)); // // control state @@ -2352,8 +2352,11 @@ bool StackFrameIterator::IsNonEHThunk(ReturnAddressCategory category) } } -bool StackFrameIterator::IsValidReturnAddress(PTR_VOID pvAddress) +bool StackFrameIterator::IsValidReturnAddress(PTR_VOID pvAddress, bool isCanonicalPC) { + if (!isCanonicalPC) + pvAddress = dac_cast(ReturnAddressToCanonicalPC(dac_cast(pvAddress))); + // These are return addresses into functions that call into managed (non-funclet) code, so we might see // them as hijacked return addresses. ReturnAddressCategory category = CategorizeUnadjustedReturnAddress(pvAddress); diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.h b/src/coreclr/nativeaot/Runtime/StackFrameIterator.h index 9c27e689a2d718..8651665dddd40b 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.h +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.h @@ -68,7 +68,7 @@ class StackFrameIterator void SetControlPC(PTR_VOID controlPC); PTR_VOID GetControlPC() { return m_ControlPC; } - static bool IsValidReturnAddress(PTR_VOID pvAddress); + static bool IsValidReturnAddress(PTR_VOID pvAddress, bool isCanonicalPC); // Support for conservatively reporting GC references in a stack range. This is used when managed methods // with an unknown signature potentially including GC references call into the runtime and we need to let diff --git a/src/coreclr/nativeaot/Runtime/thread.cpp b/src/coreclr/nativeaot/Runtime/thread.cpp index 6de0b28adbcf31..5d8fb32bc2d7dd 100644 --- a/src/coreclr/nativeaot/Runtime/thread.cpp +++ b/src/coreclr/nativeaot/Runtime/thread.cpp @@ -21,7 +21,6 @@ #include "threadstore.h" #include "threadstore.inl" #include "thread.inl" -#include "CommonMacros.inl" #include "RuntimeInstance.h" #include "shash.h" #include "rhbinder.h" @@ -680,13 +679,7 @@ void Thread::HijackReturnAddressWorker(StackFrameIterator* frameIterator, Hijack ASSERT(pvRetAddr != NULL); -#if defined(TARGET_ARM64) - ASSERT(StackFrameIterator::IsValidReturnAddress(PacStripPtr(pvRetAddr))); -#elif defined(TARGET_ARM) - ASSERT(StackFrameIterator::IsValidReturnAddress((void*)PCODEToPINSTR((PCODE)pvRetAddr))); -#else - ASSERT(StackFrameIterator::IsValidReturnAddress(pvRetAddr)); -#endif + ASSERT(StackFrameIterator::IsValidReturnAddress(pvRetAddr, false)); m_ppvHijackedReturnAddressLocation = ppvRetAddrLocation; m_pvHijackedReturnAddress = pvRetAddr; From d9d026bbed0e7b428031e65b6614c835c9e82231 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:40:13 +0200 Subject: [PATCH 06/14] Fold InterfaceDispatchSlow into DispatchResolve Also removes the TRASH_SAVED_ARGUMENT_REGISTERS debugging aid and passes the location of the 'this' argument to RhpCidResolve, so TransitionBlock.cs no longer needs to be kept in sync with the asm. Co-authored-by: Cursor --- .../src/Internal/Runtime/TransitionBlock.cs | 44 ---------- .../System/Runtime/CachedInterfaceDispatch.cs | 3 +- src/coreclr/nativeaot/Runtime/CMakeLists.txt | 2 - .../Runtime/UniversalTransitionHelpers.cpp | 68 ---------------- .../nativeaot/Runtime/amd64/DispatchResolve.S | 28 +++++++ .../Runtime/amd64/DispatchResolve.asm | 47 ++++++++++- .../Runtime/amd64/InterfaceDispatchSlow.S | 56 ------------- .../Runtime/amd64/InterfaceDispatchSlow.asm | 81 ------------------- .../nativeaot/Runtime/arm/DispatchResolve.S | 37 +++++++++ .../Runtime/arm/InterfaceDispatchSlow.S | 70 ---------------- .../nativeaot/Runtime/arm64/DispatchResolve.S | 30 +++++++ .../Runtime/arm64/DispatchResolve.asm | 52 +++++++++++- .../Runtime/arm64/InterfaceDispatchSlow.S | 57 ------------- .../Runtime/arm64/InterfaceDispatchSlow.asm | 59 -------------- .../Runtime/i386/DispatchResolve.asm | 58 ++++++++++++- .../Runtime/i386/InterfaceDispatchSlow.asm | 75 ----------------- .../Runtime/loongarch64/DispatchResolve.S | 33 ++++++++ .../loongarch64/InterfaceDispatchSlow.S | 67 --------------- .../Runtime/ppc64le/InterfaceDispatchSlow.S | 4 - .../Runtime/riscv64/DispatchResolve.S | 33 ++++++++ .../Runtime/riscv64/InterfaceDispatchSlow.S | 67 --------------- .../Runtime/s390x/InterfaceDispatchSlow.S | 4 - .../src/System.Private.CoreLib.csproj | 3 - .../src/System.Private.TypeLoader.csproj | 3 - .../Test.CoreLib/src/Test.CoreLib.csproj | 3 - 25 files changed, 314 insertions(+), 670 deletions(-) delete mode 100644 src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs delete mode 100644 src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp delete mode 100644 src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm delete mode 100644 src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S delete mode 100644 src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S diff --git a/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs b/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs deleted file mode 100644 index 282517bfc8573a..00000000000000 --- a/src/coreclr/nativeaot/Common/src/Internal/Runtime/TransitionBlock.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace Internal.Runtime -{ - // - // The TransitionBlock that RhpInterfaceDispatchSlow passes to RhpCidResolve. Except on x86 and WASM, it is - // created by PROLOG_WITH_TRANSITION_BLOCK and has the same layout as the TransitionBlock in CoreCLR's - // callingconvention.h. - // - internal static class TransitionBlock - { - // Offset of the 'this' argument from the TransitionBlock pointer - public static int GetThisOffset() - { -#if TARGET_AMD64 && TARGET_UNIX - // rdi is the first of the argument registers at the base of the TransitionBlock - return 0; -#elif TARGET_AMD64 - // rcx is homed above the callee-saved registers (rdi, rsi, rbx, rbp, r12-r15) and the return address - return 9 * 8; -#elif TARGET_ARM64 - // x0 follows the callee-saved registers (fp, lr, x19-x28), the padding and x8 - return 14 * 8; -#elif TARGET_ARM - // r0 follows the callee-saved registers (r4-r11) and lr - return 9 * 4; -#elif TARGET_LOONGARCH64 - // a0 follows the callee-saved registers (fp, ra, s0-s8) and the padding - return 12 * 8; -#elif TARGET_RISCV64 - // a0 follows the callee-saved registers (fp, ra, s1-s11, tp, gp) and the padding - return 16 * 8; -#elif TARGET_X86 - // ecx follows edx at the base of the transition frame - return 4; -#elif TARGET_WASMARCH - return 4; -#else -#error Unknown architecture! -#endif - } - } -} diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs index 7c52e60ea1035f..a1b423cff18cc1 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs @@ -63,9 +63,8 @@ public override bool Equals(object obj) #endif [RuntimeExport("RhpCidResolve")] - private static IntPtr RhpCidResolve(IntPtr callerTransitionBlockParam, IntPtr pCell) + private static IntPtr RhpCidResolve(IntPtr locationOfThisPointer, IntPtr pCell) { - IntPtr locationOfThisPointer = callerTransitionBlockParam + TransitionBlock.GetThisOffset(); object pObject = *(object*)locationOfThisPointer; IntPtr dispatchResolveTarget = RhpCidResolve_Worker(pObject, pCell); return dispatchResolveTarget; diff --git a/src/coreclr/nativeaot/Runtime/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/CMakeLists.txt index 191896d5ceb691..86e42539222762 100644 --- a/src/coreclr/nativeaot/Runtime/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/CMakeLists.txt @@ -63,7 +63,6 @@ set(COMMON_RUNTIME_SOURCES SyncClean.cpp thread.cpp threadstore.cpp - UniversalTransitionHelpers.cpp yieldprocessornormalized.cpp ${GC_DIR}/gceventstatus.cpp @@ -311,7 +310,6 @@ if(NOT FEATURE_PORTABLE_HELPERS) ${ARCH_SOURCES_DIR}/GcProbe.${ASM_SUFFIX} ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/MiscStubs.${ASM_SUFFIX} ${ARCH_SOURCES_DIR}/PInvoke.${ASM_SUFFIX} - ${ARCH_SOURCES_DIR}/InterfaceDispatchSlow.${ASM_SUFFIX} ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/WriteBarriers.${ASM_SUFFIX} ) diff --git a/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp b/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp deleted file mode 100644 index 1bd326ecd62352..00000000000000 --- a/src/coreclr/nativeaot/Runtime/UniversalTransitionHelpers.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include "common.h" -#include "CommonTypes.h" -#include "CommonMacros.h" -#include "PalLimitedContext.h" -#include "Pal.h" - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - -// -// Define tables of predictable distinguished values that RhpInterfaceDispatchSlow can use to -// trash argument registers after they have been saved into the transition frame. -// -// Trashing these registers is a testability aid that makes it easier to detect bugs where -// the transition frame content is not correctly propagated to the eventual callee. -// -// In the absence of trashing, such bugs can become undetectable if the code that -// dispatches the call happens to never touch the impacted argument register (e.g., xmm3 on -// amd64 or q5 on arm64). In such a case, the original enregistered argument will flow -// unmodified into the eventual callee, obscuring the fact that the dispatcher failed to -// propagate the transition frame copy of this register. -// -// These tables are manually aligned as a conservative safeguard to ensure that the -// consumers can use arbitrary access widths without ever needing to worry about alignment. -// The comments in each table show the %d/%f renderings of each 32-bit value, plus the -// %I64d/%f rendering of the combined 64-bit value of each aligned pair of 32-bit values. -// - -#define TRASH_VALUE_ALIGNMENT 16 - -EXTERN_C -DECLSPEC_ALIGN(TRASH_VALUE_ALIGNMENT) -const uint32_t RhpIntegerTrashValues[] = { - // Lo32 Hi32 Lo32 Hi32 Hi32:Lo32 - // ----------- ----------- --------- --------- ------------------ - 0x07801001U, 0x07802002U, // (125833217, 125837314) (540467148372316161) - 0x07803003U, 0x07804004U, // (125841411, 125845508) (540502341334347779) - 0x07805005U, 0x07806006U, // (125849605, 125853702) (540537534296379397) - 0x07807007U, 0x07808008U, // (125857799, 125861896) (540572727258411015) - 0x07809009U, 0x0780a00aU, // (125865993, 125870090) (540607920220442633) - 0x0780b00bU, 0x0780c00cU, // (125874187, 125878284) (540643113182474251) - 0x0780d00dU, 0x0780e00eU, // (125882381, 125886478) (540678306144505869) - 0x0780f00fU, 0x07810010U, // (125890575, 125894672) (540713499106537487) -}; - -EXTERN_C -DECLSPEC_ALIGN(TRASH_VALUE_ALIGNMENT) -const uint32_t RhpFpTrashValues[] = { - // Lo32 Hi32 Lo32 Hi32 Hi32:Lo32 - // ----------- ----------- ------------------- ------------------- ------------------- - 0x42001001U, 0x42002002U, // (32.0156288146972660, 32.0312576293945310) (8657061952.00781440) - 0x42003003U, 0x42004004U, // (32.0468864440917970, 32.0625152587890630) (8724187200.02344320) - 0x42005005U, 0x42006006U, // (32.0781440734863280, 32.0937728881835940) (8791312448.03907200) - 0x42007007U, 0x42008008U, // (32.1094017028808590, 32.1250305175781250) (8858437696.05470090) - 0x42009009U, 0x4200a00aU, // (32.1406593322753910, 32.1562881469726560) (8925562944.07032970) - 0x4200b00bU, 0x4200c00cU, // (32.1719169616699220, 32.1875457763671880) (8992688192.08595850) - 0x4200d00dU, 0x4200e00eU, // (32.2031745910644530, 32.2188034057617190) (9059813440.10158730) - 0x4200f00fU, 0x42010010U, // (32.2344322204589840, 32.2500610351562500) (9126938688.11721610) -}; - -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S index 087a87709b3fa1..0e512801fce1dd 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S @@ -131,3 +131,31 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, r11 INTERFACE_DISPATCH RhpDispatchResolve, 1, rsi + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +// the original arguments. +// +// INPUT: rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7, stack space: arguments as normal +// r11: interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// +NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + + lea rdi, [rsp + __PWTB_ArgumentRegisters] + mov rsi, r11 + call C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + TAILJMP_RAX + +NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm index 0f1a57731fa521..c64c2ba4e0e038 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm @@ -6,12 +6,11 @@ include AsmMacros.inc EXTERN RhpCidResolve : PROC EXTERN RhpCidResolve_Worker : PROC -EXTERN RhpInterfaceDispatchSlow : PROC -EXTERN RhpInterfaceDispatchSlowGuarded : PROC EXTERN g_pDispatchCache : QWORD EXTERN __guard_dispatch_icall_fptr : QWORD +EXTERN __guard_check_icall_fptr : QWORD ;; Macro that generates an interface dispatch or dispatch resolve stub. ;; DispatchName: the name of the dispatch entry point @@ -168,4 +167,48 @@ LEAF_END DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatchGuarded, 1, 0, r11, edx INTERFACE_DISPATCH RhpDispatchResolve, 0, 1, rdx, r11d +;; +;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded +;; +;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +;; location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +;; the original arguments. The guarded variant validates the target with Control Flow Guard first. +;; +;; INPUT: rcx, rdx, r8, r9, xmm0-xmm3, stack space: arguments as normal +;; r11: interface dispatch cell +;; +;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +;; the argument registers homed by the TransitionBlock prolog and the stack passed arguments conservatively. +;; +INTERFACE_DISPATCH_SLOW macro FunctionName, ValidateTarget + +NESTED_ENTRY Rhp&FunctionName, _TEXT + + PROLOG_WITH_TRANSITION_BLOCK + + lea rcx, [rsp + __PWTB_ArgumentRegisters] + mov rdx, r11 + call RhpCidResolve + +ALTERNATE_ENTRY ReturnFrom&FunctionName + +if ValidateTarget ne 0 + ;; Validate the target address using Control Flow Guard before tail-calling it. + ;; The validator takes the target in rcx and preserves rcx, r10, and all float registers. + mov rcx, rax + call [__guard_check_icall_fptr] + mov rax, rcx +endif ; ValidateTarget + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + TAILJMP_RAX + +NESTED_END Rhp&FunctionName, _TEXT + + endm + + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow, 0 + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, 1 + + end diff --git a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S deleted file mode 100644 index 54e6f9491337cd..00000000000000 --- a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.S +++ /dev/null @@ -1,56 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -.intel_syntax noprefix -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS 1 -#endif - -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -// interface dispatch cell and then jumps to the resolved target with the original arguments. -// -// Inputs: -// rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7, stack space: arguments as normal -// r11: The interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// -NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler - - PROLOG_WITH_TRANSITION_BLOCK - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - // Before calling out, trash all of the argument registers except the ones (rdi, rsi) that - // hold outgoing arguments. All of these registers have been saved to the transition - // frame, and the code at the call target is required to use only the transition frame - // copies when dispatching this call to the eventual callee. - movsd xmm0, [C_VAR(RhpFpTrashValues) + 0x0] - movsd xmm1, [C_VAR(RhpFpTrashValues) + 0x8] - movsd xmm2, [C_VAR(RhpFpTrashValues) + 0x10] - movsd xmm3, [C_VAR(RhpFpTrashValues) + 0x18] - movsd xmm4, [C_VAR(RhpFpTrashValues) + 0x20] - movsd xmm5, [C_VAR(RhpFpTrashValues) + 0x28] - movsd xmm6, [C_VAR(RhpFpTrashValues) + 0x30] - movsd xmm7, [C_VAR(RhpFpTrashValues) + 0x38] - mov rcx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x10] - mov rdx, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x18] - mov r8, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x20] - mov r9, qword ptr [C_VAR(RhpIntegerTrashValues) + 0x28] -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - lea rdi, [rsp + __PWTB_TransitionBlock] - mov rsi, r11 - call C_FUNC(RhpCidResolve) - -ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - TAILJMP_RAX - -NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm deleted file mode 100644 index de0e12e640e726..00000000000000 --- a/src/coreclr/nativeaot/Runtime/amd64/InterfaceDispatchSlow.asm +++ /dev/null @@ -1,81 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - -include AsmMacros.inc - -ifdef _DEBUG -TRASH_SAVED_ARGUMENT_REGISTERS equ 1 -else -TRASH_SAVED_ARGUMENT_REGISTERS equ 0 -endif - -if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 -EXTERN RhpIntegerTrashValues : QWORD -EXTERN RhpFpTrashValues : QWORD -endif ;; TRASH_SAVED_ARGUMENT_REGISTERS - -EXTERN RhpCidResolve : PROC -EXTERN __guard_check_icall_fptr : QWORD - -; -; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded -; -; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -; interface dispatch cell and then jumps to the resolved target with the original arguments. The guarded variant -; validates the target with Control Flow Guard first. -; -; Inputs: -; rcx, rdx, r8, r9, xmm0-xmm3, stack space: arguments as normal -; r11: The interface dispatch cell -; -; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -; the argument registers homed by the TransitionBlock prolog and the stack passed arguments conservatively. -; -INTERFACE_DISPATCH_SLOW macro FunctionName, ValidateTarget - -NESTED_ENTRY Rhp&FunctionName, _TEXT - - PROLOG_WITH_TRANSITION_BLOCK - -if TRASH_SAVED_ARGUMENT_REGISTERS ne 0 - - ; Before calling out, trash all of the argument registers except the ones (rcx, rdx) that - ; hold outgoing arguments. All of these registers have been saved to the transition - ; frame, and the code at the call target is required to use only the transition frame - ; copies when dispatching this call to the eventual callee. - - movsd xmm0, mmword ptr [RhpFpTrashValues + 0h] - movsd xmm1, mmword ptr [RhpFpTrashValues + 8h] - movsd xmm2, mmword ptr [RhpFpTrashValues + 10h] - movsd xmm3, mmword ptr [RhpFpTrashValues + 18h] - - mov r8, qword ptr [RhpIntegerTrashValues + 10h] - mov r9, qword ptr [RhpIntegerTrashValues + 18h] - -endif ; TRASH_SAVED_ARGUMENT_REGISTERS - - lea rcx, [rsp + __PWTB_TransitionBlock] - mov rdx, r11 - call RhpCidResolve - -ALTERNATE_ENTRY ReturnFrom&FunctionName - -if ValidateTarget ne 0 - ; Validate the target address using Control Flow Guard before tail-calling it. - ; The validator takes the target in rcx and preserves rcx, r10, and all float registers. - mov rcx, rax - call [__guard_check_icall_fptr] - mov rax, rcx -endif ; ValidateTarget - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - TAILJMP_RAX - -NESTED_END Rhp&FunctionName, _TEXT - - endm - - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow, 0 - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, 1 - -end diff --git a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S index 59397f1066ed63..895d617ce0cece 100644 --- a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S @@ -142,3 +142,40 @@ LOCAL_LABEL(ResolveInterfaceMethod): b C_FUNC(RhpResolveInterfaceMethod) LEAF_END RhpDispatchResolve, _TEXT + +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +// the original arguments. +// +// INPUT: r0-r3, d0-d7, stack space: arguments as normal +// r12: interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + + add r0, sp, #__PWTB_ArgumentRegisters + mov r1, r12 + + // Make the ReturnFromInterfaceDispatchSlow label 4 byte aligned + .balign 4 + bl C_FUNC(RhpCidResolve) + +GLOBAL_LABEL ReturnFromInterfaceDispatchSlow + + // Move the result (the target address) to r12 so it doesn't get overridden when we restore the + // argument registers. Additionally make sure the thumb2 bit is set. + orr r12, r0, #1 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG r12 + +NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S deleted file mode 100644 index 7be647ea76279b..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm/InterfaceDispatchSlow.S +++ /dev/null @@ -1,70 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -.syntax unified -.thumb - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -// interface dispatch cell and then jumps to the resolved target with the original arguments. -// -// At input to this function, r0-3, d0-7 and the stack may contain any number of arguments, and r12 contains the -// interface dispatch cell. -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// -////////////////////////////////////////////////////////////////////////////////////////////////////////////// -NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler - - PROLOG_WITH_TRANSITION_BLOCK - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - - // Before calling out, trash all of the argument registers except the ones (r0, r1) that - // hold outgoing arguments. All of these registers have been saved to the transition - // frame, and the code at the call target is required to use only the transition frame - // copies when dispatching this call to the eventual callee. - - PREPARE_EXTERNAL_VAR RhpFpTrashValues, r3 - vldr d0, [r3, #(0 * 8)] - vldr d1, [r3, #(1 * 8)] - vldr d2, [r3, #(2 * 8)] - vldr d3, [r3, #(3 * 8)] - vldr d4, [r3, #(4 * 8)] - vldr d5, [r3, #(5 * 8)] - vldr d6, [r3, #(6 * 8)] - vldr d7, [r3, #(7 * 8)] - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, r3 - ldr r2, [r3, #(2 * 4)] - ldr r3, [r3, #(3 * 4)] - -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - add r0, sp, #__PWTB_TransitionBlock - mov r1, r12 - - // Make the ReturnFromInterfaceDispatchSlow label 4 byte aligned - .balign 4 - bl C_FUNC(RhpCidResolve) - -GLOBAL_LABEL ReturnFromInterfaceDispatchSlow - - // Move the result (the target address) to r12 so it doesn't get overridden when we restore the - // argument registers. Additionally make sure the thumb2 bit is set. - orr r12, r0, #1 - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - EPILOG_BRANCH_REG r12 - -NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S index ca859b42860b38..5f668e57349b8f 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S @@ -126,3 +126,33 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, x11 INTERFACE_DISPATCH RhpDispatchResolve, 1, x1 + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +// the original arguments. +// +// INPUT: x0-x8, q0-q7, stack space: arguments as normal +// x11: interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + + add x0, sp, #__PWTB_ArgumentRegister_FirstArg + mov x1, x11 + bl C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + mov x9, x0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG x9 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm index 3f07ef5c83ab1c..390e8459179839 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm @@ -7,10 +7,9 @@ EXTERN RhpCidResolve EXTERN RhpCidResolve_Worker - EXTERN RhpInterfaceDispatchSlow - EXTERN RhpInterfaceDispatchSlowGuarded EXTERN __guard_dispatch_icall_fptr + EXTERN __guard_check_icall_fptr EXTERN g_pDispatchCache ;; Macro that generates an interface dispatch or dispatch resolve stub. @@ -159,4 +158,53 @@ INTERFACE_DISPATCH RhpInterfaceDispatchGuarded, 1, 0, x11 INTERFACE_DISPATCH RhpDispatchResolve, 0, 1, x1 +;; +;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded +;; +;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +;; location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +;; the original arguments. The guarded variant validates the target with Control Flow Guard first. +;; +;; INPUT: x0-x8, q0-q7, stack space: arguments as normal +;; x11: interface dispatch cell +;; +;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +;; everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +;; + MACRO + INTERFACE_DISPATCH_SLOW $FunctionName, $ValidateTarget + + NESTED_ENTRY Rhp$FunctionName + + PROLOG_WITH_TRANSITION_BLOCK + + add x0, sp, #__PWTB_ArgumentRegister_FirstArg + mov x1, x11 + bl RhpCidResolve + + ALTERNATE_ENTRY ReturnFrom$FunctionName + + IF "$ValidateTarget" != "" + ;; Validate the target address using Control Flow Guard before tail-calling it. + ;; The validator takes the target in x15 and preserves x0-x8, x15, and all float registers. + mov x15, x0 + adrp x16, __guard_check_icall_fptr + ldr x16, [x16, __guard_check_icall_fptr] + blr x16 + mov x0, x15 + ENDIF + + mov x9, x0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + EPILOG_BRANCH_REG x9 + + NESTED_END Rhp$FunctionName + + MEND + + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, ValidateTarget + + END diff --git a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S deleted file mode 100644 index bb47d4c58fd2f0..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.S +++ /dev/null @@ -1,57 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -// interface dispatch cell and then jumps to the resolved target with the original arguments. -// -// At input to this function, x0-8, q0-7 and the stack may contain any number of arguments, and x11 contains the -// interface dispatch cell. -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// - NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler - - PROLOG_WITH_TRANSITION_BLOCK - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, x1 - - ldp q0,q1, [x1, 0x0] - ldp q2,q3, [x1, 0x20] - ldp q4,q5, [x1, 0x40] - ldp q6,q7, [x1, 0x60] - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, x1 - - ldp x2,x3, [x1, 0x10] - ldp x4,x5, [x1, 0x20] - ldp x6,x7, [x1, 0x30] -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - add x0, sp, #__PWTB_TransitionBlock - mov x1, x11 - bl C_FUNC(RhpCidResolve) - -ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow - - mov x9, x0 - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - EPILOG_BRANCH_REG x9 - - NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm deleted file mode 100644 index 317507cdf06e3b..00000000000000 --- a/src/coreclr/nativeaot/Runtime/arm64/InterfaceDispatchSlow.asm +++ /dev/null @@ -1,59 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - -#include "AsmMacros.h" - - EXTERN RhpCidResolve - EXTERN __guard_check_icall_fptr - - TEXTAREA - -;; -;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded -;; -;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -;; interface dispatch cell and then jumps to the resolved target with the original arguments. The guarded variant -;; validates the target with Control Flow Guard first. -;; -;; At input to this function, x0-8, q0-7 and the stack may contain any number of arguments, and x11 contains the -;; interface dispatch cell. -;; -;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -;; everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -;; - MACRO - INTERFACE_DISPATCH_SLOW $FunctionName, $ValidateTarget - - NESTED_ENTRY Rhp$FunctionName - - PROLOG_WITH_TRANSITION_BLOCK - - add x0, sp, #__PWTB_TransitionBlock - mov x1, x11 - bl RhpCidResolve - - ALTERNATE_ENTRY ReturnFrom$FunctionName - - IF "$ValidateTarget" != "" - ;; Validate the target address using Control Flow Guard before tail-calling it. - ;; The validator takes the target in x15 and preserves x0-x8, x15, and all float registers. - mov x15, x0 - adrp x16, __guard_check_icall_fptr - ldr x16, [x16, __guard_check_icall_fptr] - blr x16 - mov x0, x15 - ENDIF - - mov x9, x0 - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - EPILOG_BRANCH_REG x9 - - NESTED_END Rhp$FunctionName - - MEND - - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, ValidateTarget - - END diff --git a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm index 5c47174dd1e045..ee5be5615d49ab 100644 --- a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm @@ -9,7 +9,7 @@ include AsmMacros.inc EXTERN RhpCidResolve_Worker : PROC -EXTERN _RhpInterfaceDispatchSlow@0 : PROC +EXTERN RhpCidResolve : PROC EXTERN _g_pDispatchCache : DWORD @@ -250,4 +250,60 @@ ALTERNATE_ENTRY _RhpDispatchResolve RhpDispatchResolve ENDP +;; +;; RhpInterfaceDispatchSlow +;; +;; Saves the argument registers, calls RhpCidResolve with the location of the 'this' argument to resolve the +;; interface dispatch cell and then jumps to the resolved target with the original arguments. +;; +;; INPUT: ecx, edx, stack space two pops down: arguments as normal +;; first register sized field on the stack is the interface dispatch cell, followed by the caller's EBP and +;; the return address of the whole method. (This method cannot be called via a call instruction, it must be +;; jumped to.) The fake entrypoint is in place to convince the stack walker this is a normal framed function. +;; +;; NOTE! FOR CORRECTNESS THIS FUNCTION REQUIRES THAT ALL NON-LEAF MANAGED FUNCTIONS HAVE +;; FRAME POINTERS, OR THE STACK WALKER CAN'T STACKWALK OUT OF HERE +;; +;; Frame layout is: +;; +;; {StackPassedArgs} ChildSP+014 CallerSP+000 +;; {CallerRetaddr} ChildSP+010 CallerSP-004 +;; {CallerEBP} ChildSP+00C CallerSP-008 +;; {DispatchCell} ChildSP+008 CallerSP-00C +;; {IntArgRegs (edx,ecx) (0x8 bytes)} ChildSP+000 CallerSP-014 +;; {CalleeRetaddr} ChildSP-004 CallerSP-018 +;; +;; NOTE: If the frame layout ever changes, the C++ InterfaceDispatchSlowStackFrame structure +;; must be updated as well. +;; +;; NOTE: The stack walker guarantees that conservative GC reporting will be applied to +;; everything between the base of the IntArgRegs and the top of the StackPassedArgs. +;; +FASTCALL_FUNC RhpInterfaceDispatchSlow_FAKE_ENTRY, 0 + ; Set up an ebp frame + push ebp + mov ebp, esp + push eax +ALTERNATE_ENTRY _RhpInterfaceDispatchSlow@0 + push ecx + push edx + + ; + ; Call out to RhpCidResolve, while storing and reporting arguments to the GC. + ; + mov edx, [ebp-4] ; Get the interface dispatch cell + lea ecx, [ebp-8] ; Get the location of the ecx ('this') value pushed above + call RhpCidResolve + +ALTERNATE_ENTRY _ReturnFromInterfaceDispatchSlow + + pop edx + pop ecx + add esp, 4 + pop ebp + jmp eax + +FASTCALL_ENDFUNC + + end diff --git a/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm b/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm deleted file mode 100644 index 6b71c32c185de5..00000000000000 --- a/src/coreclr/nativeaot/Runtime/i386/InterfaceDispatchSlow.asm +++ /dev/null @@ -1,75 +0,0 @@ -;; Licensed to the .NET Foundation under one or more agreements. -;; The .NET Foundation licenses this file to you under the MIT license. - - .586 - .model flat - option casemap:none - .code - -include AsmMacros.inc - -EXTERN RhpCidResolve : PROC - -;; -;; RhpInterfaceDispatchSlow -;; -;; Saves the argument registers, calls RhpCidResolve to resolve the interface dispatch cell and then jumps to the -;; resolved target with the original arguments. -;; -;; Inputs: -;; ecx, edx, stack space two pops down: arguments as normal -;; first register sized field on the stack is the interface dispatch cell, -;; followed by the caller's EBP and the return address of the whole method. (This method cannot be called -;; via a call instruction, it must be jumped to.) The fake entrypoint is in place to -;; convince the stack walker this is a normal framed function. -;; -;; NOTE! FOR CORRECTNESS THIS FUNCTION REQUIRES THAT ALL NON-LEAF MANAGED FUNCTIONS HAVE -;; FRAME POINTERS, OR THE STACK WALKER CAN'T STACKWALK OUT OF HERE -;; - -; -; Frame layout is: -; -; {StackPassedArgs} ChildSP+014 CallerSP+000 -; {CallerRetaddr} ChildSP+010 CallerSP-004 -; {CallerEBP} ChildSP+00C CallerSP-008 -; {DispatchCell} ChildSP+008 CallerSP-00C -; {IntArgRegs (edx,ecx) (0x8 bytes)} ChildSP+000 CallerSP-014 -; {CalleeRetaddr} ChildSP-004 CallerSP-018 -; -; NOTE: If the frame layout ever changes, the C++ InterfaceDispatchSlowStackFrame structure -; must be updated as well. -; -; NOTE: RhpCidResolve receives a pointer to the base of the pushed IntArgRegs. -; -; NOTE: The stack walker guarantees that conservative GC reporting will be applied to -; everything between the base of the IntArgRegs and the top of the StackPassedArgs. -; - -FASTCALL_FUNC RhpInterfaceDispatchSlow_FAKE_ENTRY, 0 - ; Set up an ebp frame - push ebp - mov ebp, esp - push eax -ALTERNATE_ENTRY _RhpInterfaceDispatchSlow@0 - push ecx - push edx - - ; - ; Call out to RhpCidResolve, while storing and reporting arguments to the GC. - ; - mov edx, [ebp-4] ; Get the interface dispatch cell - lea ecx, [ebp-0Ch] ; Get pointer to edx value pushed above - call RhpCidResolve - -ALTERNATE_ENTRY _ReturnFromInterfaceDispatchSlow - - pop edx - pop ecx - add esp, 4 - pop ebp - jmp eax - -FASTCALL_ENDFUNC - -end diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S index be885a8fdf2279..d38e815e9d5fb6 100644 --- a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S @@ -29,3 +29,36 @@ LEAF_ENTRY RhpDispatchResolve, _TEXT b C_FUNC(RhpResolveInterfaceMethod) LEAF_END RhpDispatchResolve, _TEXT + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +// the original arguments. +// +// INPUT: a0-a7, f0-f7, stack space: arguments as normal +// t8: interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + + addi.d $a0, $sp, __PWTB_ArgumentRegisters + ori $a1, $t8, 0 + bl C_FUNC(RhpCidResolve) + + ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + // Move the result (the target address) to t3 so it doesn't get overridden when we restore the + // argument registers. + ori $t3, $a0, 0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + jirl $r0, $t3, 0 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S deleted file mode 100644 index 55687140465b72..00000000000000 --- a/src/coreclr/nativeaot/Runtime/loongarch64/InterfaceDispatchSlow.S +++ /dev/null @@ -1,67 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -// interface dispatch cell and then jumps to the resolved target with the original arguments. -// -// At input to this function, a0-a7, f0-f7 and the stack may contain any number of arguments, and t8 contains the -// interface dispatch cell. -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// - NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler - - PROLOG_WITH_TRANSITION_BLOCK - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, $a1 - - fld.d $f0, $a1, 0 - fld.d $f1, $a1, 0x08 - fld.d $f2, $a1, 0x10 - fld.d $f3, $a1, 0x18 - fld.d $f4, $a1, 0x20 - fld.d $f5, $a1, 0x28 - fld.d $f6, $a1, 0x30 - fld.d $f7, $a1, 0x38 - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, $a1 - - ld.d $a2, $a1, 0x10 - ld.d $a3, $a1, 0x18 - ld.d $a4, $a1, 0x20 - ld.d $a5, $a1, 0x28 - ld.d $a6, $a1, 0x30 - ld.d $a7, $a1, 0x38 -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - addi.d $a0, $sp, __PWTB_TransitionBlock - ori $a1, $t8, 0 - bl C_FUNC(RhpCidResolve) - - ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow - - // Move the result (the target address) to t3 so it doesn't get overridden when we restore the - // argument registers. - ori $t3, $a0, 0 - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - jirl $r0, $t3, 0 - - NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S deleted file mode 100644 index 8ccdf87b55e2b2..00000000000000 --- a/src/coreclr/nativeaot/Runtime/ppc64le/InterfaceDispatchSlow.S +++ /dev/null @@ -1,4 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include diff --git a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S index 9470d9a1ba2dd9..cba6fab8556fe4 100644 --- a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S @@ -146,3 +146,36 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, t5 INTERFACE_DISPATCH RhpDispatchResolve, 1, a1 + +// +// RhpInterfaceDispatchSlow +// +// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the +// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with +// the original arguments. +// +// INPUT: a0-a7, fa0-fa7, stack space: arguments as normal +// t5: interface dispatch cell +// +// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports +// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. +// + NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + + PROLOG_WITH_TRANSITION_BLOCK + + addi a0, sp, __PWTB_ArgumentRegisters + mv a1, t5 + call C_FUNC(RhpCidResolve) + +ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow + + // Move the result (the target address) to t2 so it doesn't get overridden when we restore the + // argument registers. + mv t2, a0 + + EPILOG_WITH_TRANSITION_BLOCK_TAILCALL + + jr t2 + + NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S deleted file mode 100644 index ad20e3016959d4..00000000000000 --- a/src/coreclr/nativeaot/Runtime/riscv64/InterfaceDispatchSlow.S +++ /dev/null @@ -1,67 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include - -#ifdef _DEBUG -#define TRASH_SAVED_ARGUMENT_REGISTERS -#endif - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - .global RhpIntegerTrashValues - .global RhpFpTrashValues -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve to resolve the -// interface dispatch cell and then jumps to the resolved target with the original arguments. -// -// At input to this function, a0-a7, fa0-fa7 and the stack may contain any number of arguments, and t5 contains the -// interface dispatch cell. -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// - NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler - - PROLOG_WITH_TRANSITION_BLOCK - -#ifdef TRASH_SAVED_ARGUMENT_REGISTERS - PREPARE_EXTERNAL_VAR RhpFpTrashValues, a1 - - fld fa0, 0x00(a1) - fld fa1, 0x08(a1) - fld fa2, 0x10(a1) - fld fa3, 0x18(a1) - fld fa4, 0x20(a1) - fld fa5, 0x28(a1) - fld fa6, 0x30(a1) - fld fa7, 0x38(a1) - - PREPARE_EXTERNAL_VAR RhpIntegerTrashValues, a1 - - ld a2, 0x10(a1) - ld a3, 0x18(a1) - ld a4, 0x20(a1) - ld a5, 0x28(a1) - ld a6, 0x30(a1) - ld a7, 0x38(a1) -#endif // TRASH_SAVED_ARGUMENT_REGISTERS - - addi a0, sp, __PWTB_TransitionBlock - mv a1, t5 - call C_FUNC(RhpCidResolve) - -ALTERNATE_ENTRY ReturnFromInterfaceDispatchSlow - - # Move the result (the target address) to t2 so it doesn't get overridden when we restore the - # argument registers. - mv t2, a0 - - EPILOG_WITH_TRANSITION_BLOCK_TAILCALL - - jr t2 - - NESTED_END RhpInterfaceDispatchSlow, _TEXT diff --git a/src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S b/src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S deleted file mode 100644 index 8ccdf87b55e2b2..00000000000000 --- a/src/coreclr/nativeaot/Runtime/s390x/InterfaceDispatchSlow.S +++ /dev/null @@ -1,4 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 25a34a192b5740..0516dc2cbf9199 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -532,9 +532,6 @@ Runtime.Base\src\System\Runtime\TypeCast.cs - - Common\TransitionBlock.cs - diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj b/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj index ca850192b0e69a..75f792d044f856 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj @@ -34,9 +34,6 @@ RuntimeConstants.cs - - TransitionBlock.cs - MetadataBlob.cs diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj b/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj index acccde8d8fd8f2..3cfe6a5ff8946b 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj +++ b/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj @@ -54,9 +54,6 @@ Runtime.Base\src\System\Runtime\TypeCast.cs - - Common\TransitionBlock.cs - Common\Interop\Windows\Interop.BOOL.cs From 5c300490afcb4557ab326982bc081e3161a7f517 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:35:29 +0200 Subject: [PATCH 07/14] Pass 'this' to RhpCidResolve and merge its worker The dispatch stubs now pass the 'this' object to RhpCidResolve, so RhpCidResolve_Worker is folded into it. Co-authored-by: Cursor --- .../src/System/Runtime/CachedInterfaceDispatch.cs | 12 ++---------- .../nativeaot/Runtime/amd64/DispatchResolve.S | 7 +++---- .../nativeaot/Runtime/amd64/DispatchResolve.asm | 8 +++----- src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S | 5 ++--- .../nativeaot/Runtime/arm64/DispatchResolve.S | 7 +++---- .../nativeaot/Runtime/arm64/DispatchResolve.asm | 8 +++----- .../nativeaot/Runtime/i386/DispatchResolve.asm | 8 +++----- .../nativeaot/Runtime/loongarch64/DispatchResolve.S | 5 ++--- .../nativeaot/Runtime/riscv64/DispatchResolve.S | 7 +++---- 9 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs index a1b423cff18cc1..dbbbe7b24d40b8 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs @@ -63,15 +63,7 @@ public override bool Equals(object obj) #endif [RuntimeExport("RhpCidResolve")] - private static IntPtr RhpCidResolve(IntPtr locationOfThisPointer, IntPtr pCell) - { - object pObject = *(object*)locationOfThisPointer; - IntPtr dispatchResolveTarget = RhpCidResolve_Worker(pObject, pCell); - return dispatchResolveTarget; - } - - [RuntimeExport("RhpCidResolve_Worker")] - private static IntPtr RhpCidResolve_Worker(object pObject, IntPtr pCell) + private static IntPtr RhpCidResolve(object pObject, IntPtr pCell) { var resolver = (delegate*)InternalCalls.RhpGetClasslibFunctionFromEEType(pObject.GetMethodTable(), ClassLibFunctionId.ResolveDispatch); IntPtr pTargetCode = resolver(pObject, pCell); @@ -156,7 +148,7 @@ private static IntPtr RhpResolveInterfaceMethod(object pObject, IntPtr pCell) if (pTargetCode == IntPtr.Zero) { // Otherwise call the version of this method that knows how to resolve the method manually. - pTargetCode = RhpCidResolve_Worker(pObject, pCell); + pTargetCode = RhpCidResolve(pObject, pCell); } return pTargetCode; diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S index 0e512801fce1dd..cee476c28f44aa 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S @@ -114,7 +114,7 @@ LOCAL_LABEL(ProbeMiss\@): LOCAL_LABEL(CacheMiss\@): .if \ReturnTarget - jmp C_FUNC(RhpCidResolve_Worker) + jmp C_FUNC(RhpCidResolve) .else mov r9, [rsp - 32] mov r8, [rsp - 24] @@ -136,8 +136,8 @@ LEAF_END \DispatchName, _TEXT // RhpInterfaceDispatchSlow // // Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -// the original arguments. +// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +// arguments. // // INPUT: rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7, stack space: arguments as normal // r11: interface dispatch cell @@ -149,7 +149,6 @@ NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler PROLOG_WITH_TRANSITION_BLOCK - lea rdi, [rsp + __PWTB_ArgumentRegisters] mov rsi, r11 call C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm index c64c2ba4e0e038..d4215e02d1da02 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm @@ -5,7 +5,6 @@ include AsmMacros.inc EXTERN RhpCidResolve : PROC -EXTERN RhpCidResolve_Worker : PROC EXTERN g_pDispatchCache : QWORD @@ -143,7 +142,7 @@ endif if ReturnTarget ne 0 mov rcx, [rsp + 8h] mov rdx, DispatchCellReg - jmp RhpCidResolve_Worker + jmp RhpCidResolve else mov r9, [rsp + 20h] mov r8, [rsp + 18h] @@ -171,8 +170,8 @@ LEAF_END DispatchName, _TEXT ;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded ;; ;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -;; location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -;; the original arguments. The guarded variant validates the target with Control Flow Guard first. +;; 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +;; arguments. The guarded variant validates the target with Control Flow Guard first. ;; ;; INPUT: rcx, rdx, r8, r9, xmm0-xmm3, stack space: arguments as normal ;; r11: interface dispatch cell @@ -186,7 +185,6 @@ NESTED_ENTRY Rhp&FunctionName, _TEXT PROLOG_WITH_TRANSITION_BLOCK - lea rcx, [rsp + __PWTB_ArgumentRegisters] mov rdx, r11 call RhpCidResolve diff --git a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S index 895d617ce0cece..26f38c23fac50e 100644 --- a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S @@ -148,8 +148,8 @@ LEAF_END RhpDispatchResolve, _TEXT // RhpInterfaceDispatchSlow // // Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -// the original arguments. +// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +// arguments. // // INPUT: r0-r3, d0-d7, stack space: arguments as normal // r12: interface dispatch cell @@ -162,7 +162,6 @@ NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler PROLOG_WITH_TRANSITION_BLOCK - add r0, sp, #__PWTB_ArgumentRegisters mov r1, r12 // Make the ReturnFromInterfaceDispatchSlow label 4 byte aligned diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S index 5f668e57349b8f..f8d02004e444fb 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S @@ -114,7 +114,7 @@ LOCAL_LABEL(ProbeMiss\@): LOCAL_LABEL(CacheMiss\@): .if \ReturnTarget - b C_FUNC(RhpCidResolve_Worker) + b C_FUNC(RhpCidResolve) .else // DispatchCellReg (x11) contains the indirection cell address b C_FUNC(RhpInterfaceDispatchSlow) @@ -131,8 +131,8 @@ LEAF_END \DispatchName, _TEXT // RhpInterfaceDispatchSlow // // Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -// the original arguments. +// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +// arguments. // // INPUT: x0-x8, q0-q7, stack space: arguments as normal // x11: interface dispatch cell @@ -144,7 +144,6 @@ LEAF_END \DispatchName, _TEXT PROLOG_WITH_TRANSITION_BLOCK - add x0, sp, #__PWTB_ArgumentRegister_FirstArg mov x1, x11 bl C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm index 390e8459179839..74d0de868fd255 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm @@ -6,7 +6,6 @@ TEXTAREA EXTERN RhpCidResolve - EXTERN RhpCidResolve_Worker EXTERN __guard_dispatch_icall_fptr EXTERN __guard_check_icall_fptr @@ -141,7 +140,7 @@ 4 ;; CacheMiss / SlowPath IF "$ReturnTarget" != "0" - b RhpCidResolve_Worker + b RhpCidResolve ELSE IF "$Guarded" != "0" b RhpInterfaceDispatchSlowGuarded @@ -162,8 +161,8 @@ ;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded ;; ;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -;; location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -;; the original arguments. The guarded variant validates the target with Control Flow Guard first. +;; 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +;; arguments. The guarded variant validates the target with Control Flow Guard first. ;; ;; INPUT: x0-x8, q0-q7, stack space: arguments as normal ;; x11: interface dispatch cell @@ -178,7 +177,6 @@ PROLOG_WITH_TRANSITION_BLOCK - add x0, sp, #__PWTB_ArgumentRegister_FirstArg mov x1, x11 bl RhpCidResolve diff --git a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm index ee5be5615d49ab..99dcc9826616e8 100644 --- a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm @@ -8,7 +8,6 @@ include AsmMacros.inc -EXTERN RhpCidResolve_Worker : PROC EXTERN RhpCidResolve : PROC EXTERN _g_pDispatchCache : DWORD @@ -246,15 +245,15 @@ ALTERNATE_ENTRY _RhpDispatchResolve pop edi pop esi pop ebx - jmp RhpCidResolve_Worker + jmp RhpCidResolve RhpDispatchResolve ENDP ;; ;; RhpInterfaceDispatchSlow ;; -;; Saves the argument registers, calls RhpCidResolve with the location of the 'this' argument to resolve the -;; interface dispatch cell and then jumps to the resolved target with the original arguments. +;; Saves the argument registers, calls RhpCidResolve with the 'this' argument to resolve the interface dispatch +;; cell and then jumps to the resolved target with the original arguments. ;; ;; INPUT: ecx, edx, stack space two pops down: arguments as normal ;; first register sized field on the stack is the interface dispatch cell, followed by the caller's EBP and @@ -292,7 +291,6 @@ ALTERNATE_ENTRY _RhpInterfaceDispatchSlow@0 ; Call out to RhpCidResolve, while storing and reporting arguments to the GC. ; mov edx, [ebp-4] ; Get the interface dispatch cell - lea ecx, [ebp-8] ; Get the location of the ecx ('this') value pushed above call RhpCidResolve ALTERNATE_ENTRY _ReturnFromInterfaceDispatchSlow diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S index d38e815e9d5fb6..c4476b6f1aa853 100644 --- a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S @@ -34,8 +34,8 @@ LEAF_END RhpDispatchResolve, _TEXT // RhpInterfaceDispatchSlow // // Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -// the original arguments. +// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +// arguments. // // INPUT: a0-a7, f0-f7, stack space: arguments as normal // t8: interface dispatch cell @@ -47,7 +47,6 @@ LEAF_END RhpDispatchResolve, _TEXT PROLOG_WITH_TRANSITION_BLOCK - addi.d $a0, $sp, __PWTB_ArgumentRegisters ori $a1, $t8, 0 bl C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S index cba6fab8556fe4..e016b920410b54 100644 --- a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S @@ -134,7 +134,7 @@ LOCAL_LABEL(ProbeMiss\@): LOCAL_LABEL(CacheMiss\@): .if \ReturnTarget - tail C_FUNC(RhpCidResolve_Worker) + tail C_FUNC(RhpCidResolve) .else // DispatchCellReg (t5) contains the indirection cell address tail C_FUNC(RhpInterfaceDispatchSlow) @@ -151,8 +151,8 @@ LEAF_END \DispatchName, _TEXT // RhpInterfaceDispatchSlow // // Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// location of the 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with -// the original arguments. +// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original +// arguments. // // INPUT: a0-a7, fa0-fa7, stack space: arguments as normal // t5: interface dispatch cell @@ -164,7 +164,6 @@ LEAF_END \DispatchName, _TEXT PROLOG_WITH_TRANSITION_BLOCK - addi a0, sp, __PWTB_ArgumentRegisters mv a1, t5 call C_FUNC(RhpCidResolve) From 7664edf4b7ce17d41a7b1aa64747aad269a57807 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Sun, 29 Nov 2026 21:58:05 +0200 Subject: [PATCH 08/14] Address feedback Rename SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES to SIZEOF_ARGUMENT_SHADOW_SPACE and define it in AsmMacros_Windows.inc, keep an SP-based CFA in the arm64 transition block epilogs and trim the dispatch stub comments. Co-authored-by: Cursor --- .../nativeaot/Runtime/amd64/AsmMacros.inc | 3 -- .../nativeaot/Runtime/amd64/DispatchResolve.S | 16 ++------ .../Runtime/amd64/DispatchResolve.asm | 27 +++++-------- .../nativeaot/Runtime/arm/DispatchResolve.S | 18 ++------- .../nativeaot/Runtime/arm64/DispatchResolve.S | 16 ++------ .../Runtime/arm64/DispatchResolve.asm | 29 +++++--------- .../Runtime/i386/DispatchResolve.asm | 38 +++---------------- .../Runtime/loongarch64/DispatchResolve.S | 16 ++------ .../Runtime/riscv64/DispatchResolve.S | 16 ++------ .../runtime/amd64/AsmMacros_Windows.inc | 5 ++- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 9 +++-- src/coreclr/vm/amd64/AsmMacros.inc | 2 +- src/coreclr/vm/amd64/ComCallPreStub.asm | 2 +- .../vm/amd64/ExternalMethodFixupThunk.asm | 2 +- .../vm/amd64/RedirectedHandledJITCase.asm | 4 +- src/coreclr/vm/amd64/UMThunkStub.asm | 2 +- src/coreclr/vm/amd64/asmconstants.h | 4 -- 17 files changed, 55 insertions(+), 154 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc b/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc index 81dd61235d9cb5..8066e3a9a4d621 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc +++ b/src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc @@ -11,9 +11,6 @@ include AsmOffsets.inc ; generated by the build from AsmOffsets.cpp include amd64\AsmMacros_Windows.inc -;; Size of the argument home area that PROLOG_WITH_TRANSITION_BLOCK reserves for its callees -SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES equ 20h - ;; ;; __declspec(thread) tls_CurrentThread ;; diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S index cee476c28f44aa..540e9c65ab558e 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.S @@ -132,23 +132,13 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, r11 INTERFACE_DISPATCH RhpDispatchResolve, 1, rsi -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -// arguments. -// -// INPUT: rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7, stack space: arguments as normal -// r11: interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// +// r11: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + // NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + // rdi still contains 'this' mov rsi, r11 call C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm index d4215e02d1da02..e8c61240278497 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/DispatchResolve.asm @@ -166,37 +166,29 @@ LEAF_END DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatchGuarded, 1, 0, r11, edx INTERFACE_DISPATCH RhpDispatchResolve, 0, 1, rdx, r11d -;; -;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded -;; -;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -;; 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -;; arguments. The guarded variant validates the target with Control Flow Guard first. -;; -;; INPUT: rcx, rdx, r8, r9, xmm0-xmm3, stack space: arguments as normal -;; r11: interface dispatch cell -;; -;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -;; the argument registers homed by the TransitionBlock prolog and the stack passed arguments conservatively. -;; -INTERFACE_DISPATCH_SLOW macro FunctionName, ValidateTarget +;; Macro that generates slow path for an interface dispatch. +;; FunctionName: the name of the dispatch entry point +;; Guarded: if non-zero, validate indirect call targets using Control Flow Guard +INTERFACE_DISPATCH_SLOW macro FunctionName, Guarded +;; r11: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY Rhp&FunctionName, _TEXT + ;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + ;; rcx still contains 'this' mov rdx, r11 call RhpCidResolve ALTERNATE_ENTRY ReturnFrom&FunctionName -if ValidateTarget ne 0 - ;; Validate the target address using Control Flow Guard before tail-calling it. +if Guarded ne 0 ;; The validator takes the target in rcx and preserves rcx, r10, and all float registers. mov rcx, rax call [__guard_check_icall_fptr] mov rax, rcx -endif ; ValidateTarget +endif EPILOG_WITH_TRANSITION_BLOCK_TAILCALL TAILJMP_RAX @@ -208,5 +200,4 @@ NESTED_END Rhp&FunctionName, _TEXT INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow, 0 INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, 1 - end diff --git a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S index 26f38c23fac50e..d907ea70e5dc20 100644 --- a/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm/DispatchResolve.S @@ -143,25 +143,13 @@ LOCAL_LABEL(ResolveInterfaceMethod): LEAF_END RhpDispatchResolve, _TEXT -////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -// arguments. -// -// INPUT: r0-r3, d0-d7, stack space: arguments as normal -// r12: interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// -////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// r12: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + // NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + // r0 still contains 'this' mov r1, r12 // Make the ReturnFromInterfaceDispatchSlow label 4 byte aligned diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S index f8d02004e444fb..774ab503d1332d 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.S @@ -127,23 +127,13 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, x11 INTERFACE_DISPATCH RhpDispatchResolve, 1, x1 -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -// arguments. -// -// INPUT: x0-x8, q0-q7, stack space: arguments as normal -// x11: interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// +// x11: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + // NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + // x0 still contains 'this' mov x1, x11 bl C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm index 74d0de868fd255..7f366398262139 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/DispatchResolve.asm @@ -157,33 +157,25 @@ INTERFACE_DISPATCH RhpInterfaceDispatchGuarded, 1, 0, x11 INTERFACE_DISPATCH RhpDispatchResolve, 0, 1, x1 -;; -;; RhpInterfaceDispatchSlow / RhpInterfaceDispatchSlowGuarded -;; -;; Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -;; 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -;; arguments. The guarded variant validates the target with Control Flow Guard first. -;; -;; INPUT: x0-x8, q0-q7, stack space: arguments as normal -;; x11: interface dispatch cell -;; -;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -;; everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -;; +;; Macro that generates slow path for an interface dispatch. +;; FunctionName: the name of the dispatch entry point +;; Guarded: if non-zero, validate indirect call targets using Control Flow Guard MACRO - INTERFACE_DISPATCH_SLOW $FunctionName, $ValidateTarget + INTERFACE_DISPATCH_SLOW $FunctionName, $Guarded +;; x11: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY Rhp$FunctionName + ;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + ;; x0 still contains 'this' mov x1, x11 bl RhpCidResolve ALTERNATE_ENTRY ReturnFrom$FunctionName - IF "$ValidateTarget" != "" - ;; Validate the target address using Control Flow Guard before tail-calling it. + IF "$Guarded" != "0" ;; The validator takes the target in x15 and preserves x0-x8, x15, and all float registers. mov x15, x0 adrp x16, __guard_check_icall_fptr @@ -201,8 +193,7 @@ MEND - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow - INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, ValidateTarget - + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlow, 0 + INTERFACE_DISPATCH_SLOW InterfaceDispatchSlowGuarded, 1 END diff --git a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm index 99dcc9826616e8..773f11d67fca6d 100644 --- a/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm +++ b/src/coreclr/nativeaot/Runtime/i386/DispatchResolve.asm @@ -249,37 +249,12 @@ ALTERNATE_ENTRY _RhpDispatchResolve RhpDispatchResolve ENDP +;; [esp]: interface dispatch cell, [esp+4]: caller's ebp, followed by the return address. Arguments are passed in +;; ecx, edx and stack space as normal. This must be jumped to, the fake entrypoint makes the stack walker see a +;; normal framed function. ;; -;; RhpInterfaceDispatchSlow -;; -;; Saves the argument registers, calls RhpCidResolve with the 'this' argument to resolve the interface dispatch -;; cell and then jumps to the resolved target with the original arguments. -;; -;; INPUT: ecx, edx, stack space two pops down: arguments as normal -;; first register sized field on the stack is the interface dispatch cell, followed by the caller's EBP and -;; the return address of the whole method. (This method cannot be called via a call instruction, it must be -;; jumped to.) The fake entrypoint is in place to convince the stack walker this is a normal framed function. -;; -;; NOTE! FOR CORRECTNESS THIS FUNCTION REQUIRES THAT ALL NON-LEAF MANAGED FUNCTIONS HAVE -;; FRAME POINTERS, OR THE STACK WALKER CAN'T STACKWALK OUT OF HERE -;; -;; Frame layout is: -;; -;; {StackPassedArgs} ChildSP+014 CallerSP+000 -;; {CallerRetaddr} ChildSP+010 CallerSP-004 -;; {CallerEBP} ChildSP+00C CallerSP-008 -;; {DispatchCell} ChildSP+008 CallerSP-00C -;; {IntArgRegs (edx,ecx) (0x8 bytes)} ChildSP+000 CallerSP-014 -;; {CalleeRetaddr} ChildSP-004 CallerSP-018 -;; -;; NOTE: If the frame layout ever changes, the C++ InterfaceDispatchSlowStackFrame structure -;; must be updated as well. -;; -;; NOTE: The stack walker guarantees that conservative GC reporting will be applied to -;; everything between the base of the IntArgRegs and the top of the StackPassedArgs. -;; +;; NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout FASTCALL_FUNC RhpInterfaceDispatchSlow_FAKE_ENTRY, 0 - ; Set up an ebp frame push ebp mov ebp, esp push eax @@ -287,9 +262,7 @@ ALTERNATE_ENTRY _RhpInterfaceDispatchSlow@0 push ecx push edx - ; - ; Call out to RhpCidResolve, while storing and reporting arguments to the GC. - ; + ;; ecx still contains 'this' mov edx, [ebp-4] ; Get the interface dispatch cell call RhpCidResolve @@ -303,5 +276,4 @@ ALTERNATE_ENTRY _ReturnFromInterfaceDispatchSlow FASTCALL_ENDFUNC - end diff --git a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S index c4476b6f1aa853..60588f033ed49a 100644 --- a/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/loongarch64/DispatchResolve.S @@ -30,23 +30,13 @@ LEAF_ENTRY RhpDispatchResolve, _TEXT LEAF_END RhpDispatchResolve, _TEXT -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -// arguments. -// -// INPUT: a0-a7, f0-f7, stack space: arguments as normal -// t8: interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// +// t8: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + // NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + // a0 still contains 'this' ori $a1, $t8, 0 bl C_FUNC(RhpCidResolve) diff --git a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S index e016b920410b54..65297850b2a570 100644 --- a/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S +++ b/src/coreclr/nativeaot/Runtime/riscv64/DispatchResolve.S @@ -147,23 +147,13 @@ LEAF_END \DispatchName, _TEXT INTERFACE_DISPATCH RhpInterfaceDispatch, 0, t5 INTERFACE_DISPATCH RhpDispatchResolve, 1, a1 -// -// RhpInterfaceDispatchSlow -// -// Saves the arguments in a TransitionBlock (see PROLOG_WITH_TRANSITION_BLOCK), calls RhpCidResolve with the -// 'this' argument to resolve the interface dispatch cell and then jumps to the resolved target with the original -// arguments. -// -// INPUT: a0-a7, fa0-fa7, stack space: arguments as normal -// t5: interface dispatch cell -// -// NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout. The stack walker reports -// everything between the base of the TransitionBlock and the top of the stack passed arguments conservatively. -// +// t5: interface dispatch cell, arguments passed in registers and stack space as normal NESTED_ENTRY RhpInterfaceDispatchSlow, _TEXT, NoHandler + // NOTE: StackFrameIterator::UnwindInterfaceDispatchSlowThunk hard-codes the frame layout PROLOG_WITH_TRANSITION_BLOCK + // a0 still contains 'this' mv a1, t5 call C_FUNC(RhpCidResolve) diff --git a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc index 42210db52957c7..71255c2214cf33 100644 --- a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc +++ b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc @@ -1,6 +1,9 @@ ; Licensed to the .NET Foundation under one or more agreements. ; The .NET Foundation licenses this file to you under the MIT license. +; Size of the home area for the register arguments that the Windows x64 ABI requires callers to allocate +SIZEOF_ARGUMENT_SHADOW_SPACE equ 20h + ; ; Define macros to build unwind data for prologues. ; @@ -379,7 +382,7 @@ GC_ALLOC_FINALIZE equ 1 PROLOG_WITH_TRANSITION_BLOCK macro extraLocals := <0>, calleeSavedFloatRegs := , stackAllocOnEntry := <0>, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 - __PWTB_FloatArgumentRegisters = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES + extraLocals + __PWTB_FloatArgumentRegisters = SIZEOF_ARGUMENT_SHADOW_SPACE + extraLocals if (__PWTB_FloatArgumentRegisters mod 16) ne 0 __PWTB_FloatArgumentRegisters = __PWTB_FloatArgumentRegisters + 8 diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index a69b86a1f4993b..f6958b3f1c806c 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -418,7 +418,8 @@ C_FUNC(\Name): .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN - EPILOG_STACK_FREE __PWTB_StackAlloc + add sp, sp, __PWTB_StackAlloc + .cfi_def_cfa sp, 176 EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 @@ -433,7 +434,8 @@ C_FUNC(\Name): // Same as EPILOG_WITH_TRANSITION_BLOCK_RETURN but skips restoring x21 (SwiftError) .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN_SKIP_SWIFT_ERROR - EPILOG_STACK_FREE __PWTB_StackAlloc + add sp, sp, __PWTB_StackAlloc + .cfi_def_cfa sp, 176 EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG x22, 40 @@ -457,7 +459,8 @@ C_FUNC(\Name): RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - EPILOG_STACK_FREE __PWTB_StackAlloc + add sp, sp, __PWTB_StackAlloc + .cfi_def_cfa sp, 176 EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 diff --git a/src/coreclr/vm/amd64/AsmMacros.inc b/src/coreclr/vm/amd64/AsmMacros.inc index 823a5c20fe3f23..8fc1ef4ec66109 100644 --- a/src/coreclr/vm/amd64/AsmMacros.inc +++ b/src/coreclr/vm/amd64/AsmMacros.inc @@ -58,7 +58,7 @@ OFFSETOF__ee_alloc_context EQU OFFSETOF__RuntimeThreadLocals__ee_alloc_context ; the PROLOG_WITH_TRANSITION_BLOCK macro for the stack layout. PUSH_COOP_PINVOKE_FRAME macro target - __PWTB_StackAlloc = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES + 8 ; alignment to make the stack 16b aligned + __PWTB_StackAlloc = SIZEOF_ARGUMENT_SHADOW_SPACE + 8 ; alignment to make the stack 16b aligned __PWTB_TransitionBlock = __PWTB_StackAlloc PUSH_CALLEE_SAVED_REGISTERS diff --git a/src/coreclr/vm/amd64/ComCallPreStub.asm b/src/coreclr/vm/amd64/ComCallPreStub.asm index b17b5a7ff016e8..ce6f0051d25370 100644 --- a/src/coreclr/vm/amd64/ComCallPreStub.asm +++ b/src/coreclr/vm/amd64/ComCallPreStub.asm @@ -12,7 +12,7 @@ extern UMEntryPrestubUnwindFrameChainHandler:proc NESTED_ENTRY ComCallPreStub, _TEXT, UMEntryPrestubUnwindFrameChainHandler -ComCallPreStub_STACK_FRAME_SIZE = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES +ComCallPreStub_STACK_FRAME_SIZE = SIZEOF_ARGUMENT_SHADOW_SPACE ; XMM save area ComCallPreStub_XMM_SAVE_OFFSET = ComCallPreStub_STACK_FRAME_SIZE diff --git a/src/coreclr/vm/amd64/ExternalMethodFixupThunk.asm b/src/coreclr/vm/amd64/ExternalMethodFixupThunk.asm index fcdac50f939199..d8c438fa15323a 100644 --- a/src/coreclr/vm/amd64/ExternalMethodFixupThunk.asm +++ b/src/coreclr/vm/amd64/ExternalMethodFixupThunk.asm @@ -35,7 +35,7 @@ NESTED_ENTRY DelayLoad_Helper&suffix, _TEXT PROLOG_WITH_TRANSITION_BLOCK 8h, DoNotPushCalleeSavedFloatRegs, 10h, r8, r9 - mov qword ptr [rsp + SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES], frameFlags + mov qword ptr [rsp + SIZEOF_ARGUMENT_SHADOW_SPACE], frameFlags lea rcx, [rsp + __PWTB_TransitionBlock] ; pTransitionBlock mov rdx, rax ; pIndirection diff --git a/src/coreclr/vm/amd64/RedirectedHandledJITCase.asm b/src/coreclr/vm/amd64/RedirectedHandledJITCase.asm index 15a33bdce0bc18..86efa0d2d3579e 100644 --- a/src/coreclr/vm/amd64/RedirectedHandledJITCase.asm +++ b/src/coreclr/vm/amd64/RedirectedHandledJITCase.asm @@ -98,7 +98,7 @@ endif ; scratch area -OFFSET_OF_FRAME = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES +OFFSET_OF_FRAME = SIZEOF_ARGUMENT_SHADOW_SPACE ; force evaluation to avoid "expression is too complex errors" SIZEOF__FaultingExceptionFrame = SIZEOF__FaultingExceptionFrame @@ -173,7 +173,7 @@ LEAF_END STUB&_FixRsp, _TEXT endm -REDIRECT_FOR_THROW_CONTROL_FRAME_SIZE = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES + 8 +REDIRECT_FOR_THROW_CONTROL_FRAME_SIZE = SIZEOF_ARGUMENT_SHADOW_SPACE + 8 NESTED_ENTRY RedirectForThrowControl2, _TEXT diff --git a/src/coreclr/vm/amd64/UMThunkStub.asm b/src/coreclr/vm/amd64/UMThunkStub.asm index b58808c291a1b1..e1b2ba0092d878 100644 --- a/src/coreclr/vm/amd64/UMThunkStub.asm +++ b/src/coreclr/vm/amd64/UMThunkStub.asm @@ -12,7 +12,7 @@ extern UMEntryPrestubUnwindFrameChainHandler:proc ; NESTED_ENTRY TheUMEntryPrestub, _TEXT, UMEntryPrestubUnwindFrameChainHandler -TheUMEntryPrestub_STACK_FRAME_SIZE = SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES +TheUMEntryPrestub_STACK_FRAME_SIZE = SIZEOF_ARGUMENT_SHADOW_SPACE ; XMM save area TheUMEntryPrestub_XMM_SAVE_OFFSET = TheUMEntryPrestub_STACK_FRAME_SIZE diff --git a/src/coreclr/vm/amd64/asmconstants.h b/src/coreclr/vm/amd64/asmconstants.h index 2614442b3a8588..0ab88714e8661e 100644 --- a/src/coreclr/vm/amd64/asmconstants.h +++ b/src/coreclr/vm/amd64/asmconstants.h @@ -59,13 +59,9 @@ ASMCONSTANTS_C_ASSERT(ASM_ELEMENT_TYPE_R8 == ELEMENT_TYPE_R8); #endif // FEATURE_VARARGS #ifdef UNIX_AMD64_ABI -// rdi, rsi, rdx, rcx, r8, r9 -#define SIZEOF_MAX_INT_ARG_SPILL 0x30 // xmm0...xmm7 #define SIZEOF_MAX_FP_ARG_SPILL 0x80 #else -// rcx, rdx, r8, r9 -#define SIZEOF_MAX_OUTGOING_ARGUMENT_HOMES 0x20 // xmm0...xmm3 #define SIZEOF_MAX_FP_ARG_SPILL 0x40 #endif From 49fc11c7d973a689764f80d2a7ef1b3c684fed24 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:07:06 +0200 Subject: [PATCH 09/14] Use a symbolic constant for the arm64 TransitionBlock size --- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index f6958b3f1c806c..46028a5d7bc26b 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -385,8 +385,9 @@ C_FUNC(\Name): __PWTB_StackAlloc = __PWTB_TransitionBlock __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 104 __PWTB_ArgumentRegister_FirstArg = __PWTB_ArgumentRegisters + 8 + __PWTB_TransitionBlockSize = 176 - PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -176 + PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -__PWTB_TransitionBlockSize // Spill callee saved registers PROLOG_SAVE_REG_PAIR x19, x20, 16 PROLOG_SAVE_REG_PAIR x21, x22, 32 @@ -419,14 +420,14 @@ C_FUNC(\Name): .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, 176 + .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 EPILOG_RESTORE_REG_PAIR x23, x24, 48 EPILOG_RESTORE_REG_PAIR x25, x26, 64 EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, __PWTB_TransitionBlockSize ret .endm @@ -435,14 +436,14 @@ C_FUNC(\Name): .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN_SKIP_SWIFT_ERROR add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, 176 + .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG x22, 40 EPILOG_RESTORE_REG_PAIR x23, x24, 48 EPILOG_RESTORE_REG_PAIR x25, x26, 64 EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, __PWTB_TransitionBlockSize ret .endm @@ -460,13 +461,13 @@ C_FUNC(\Name): RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, 176 + .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 EPILOG_RESTORE_REG_PAIR x23, x24, 48 EPILOG_RESTORE_REG_PAIR x25, x26, 64 EPILOG_RESTORE_REG_PAIR x27, x28, 80 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 176 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, __PWTB_TransitionBlockSize .endm From 012ace7bace219feeb2d31166060ece7cc64957d Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:08:52 +0200 Subject: [PATCH 10/14] Make arm64 PROLOG_STACK_ALLOC emit CFI like EPILOG_STACK_FREE `PROLOG_STACK_ALLOC` now emits `.cfi_adjust_cfa_offset`, matching `EPILOG_STACK_FREE` and the loongarch64/riscv64 macros. Manual CFI after it is removed. Frames whose CFA is fp-based use plain `sub`/`add`. This also fixes the bogus `CFA=fp+0` rule in `FuncEvalHijack`. Co-authored-by: Cursor --- src/coreclr/debug/ee/arm64/dbghelpers.S | 2 +- src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S | 3 +-- src/coreclr/pal/inc/unixasmmacrosarm64.inc | 2 +- src/coreclr/pal/src/arch/arm64/callsignalhandlerwrapper.S | 1 - src/coreclr/pal/src/arch/arm64/context2.S | 2 -- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 3 ++- src/coreclr/vm/arm64/asmhelpers.S | 2 +- 7 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/coreclr/debug/ee/arm64/dbghelpers.S b/src/coreclr/debug/ee/arm64/dbghelpers.S index 4b9504efec859d..0a52a085a675e8 100644 --- a/src/coreclr/debug/ee/arm64/dbghelpers.S +++ b/src/coreclr/debug/ee/arm64/dbghelpers.S @@ -24,7 +24,7 @@ NESTED_ENTRY FuncEvalHijack, _TEXT, UnhandledExceptionHandlerUnix // FuncEvalHijackWorker returns the address we should jump to. bl C_FUNC(FuncEvalHijackWorker) - EPILOG_STACK_FREE 32 + add sp, sp, 32 EPILOG_BRANCH_REG x0 NESTED_END FuncEvalHijack diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index b1f7f5019cbbed..ed99093d4698e8 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -23,7 +23,6 @@ stp x3, x1, [sp] // x3 is the SP and x1 is the IP of the fault site .else PROLOG_STACK_ALLOC 0x50 - .cfi_adjust_cfa_offset 0x50 stp x3, lr, [sp] // x3 is the SP and lr is the IP of the fault site .endif stp d8, d9, [sp, #0x10] @@ -39,7 +38,7 @@ PROLOG_SAVE_REG_PAIR x27, x28, 0x60 // } end PAL_LIMITED_CONTEXT - PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo + sub sp, sp, STACKSIZEOF_ExInfo .endm #define rsp_offsetof_ExInfo 0 diff --git a/src/coreclr/pal/inc/unixasmmacrosarm64.inc b/src/coreclr/pal/inc/unixasmmacrosarm64.inc index 86eda4ad24c2f5..e4070c3c804f00 100644 --- a/src/coreclr/pal/inc/unixasmmacrosarm64.inc +++ b/src/coreclr/pal/inc/unixasmmacrosarm64.inc @@ -87,7 +87,7 @@ PROLOG_SAVE_REG_PAIR x27, x28, 80 // Allocate space for FloatArgumentRegisters (128) + FP callee-saved (64) = 192 bytes - PROLOG_STACK_ALLOC 192 + sub sp, sp, 192 // Save argument registers (x8, x0-x7) at offset 296 from sp (192 + 104) SAVE_ARGUMENT_REGISTERS sp, 296 diff --git a/src/coreclr/pal/src/arch/arm64/callsignalhandlerwrapper.S b/src/coreclr/pal/src/arch/arm64/callsignalhandlerwrapper.S index 67a88fbb2e6fbb..b0080bae2552f1 100644 --- a/src/coreclr/pal/src/arch/arm64/callsignalhandlerwrapper.S +++ b/src/coreclr/pal/src/arch/arm64/callsignalhandlerwrapper.S @@ -16,7 +16,6 @@ C_FUNC(SignalHandlerWorkerReturnOffset\Alignment): NESTED_ENTRY CallSignalHandlerWrapper\Alignment, _TEXT, NoHandler __StackAllocationSize = (128 + 8 + 8 + \Alignment) // red zone + fp + lr + alignment PROLOG_STACK_ALLOC __StackAllocationSize - .cfi_adjust_cfa_offset __StackAllocationSize PROLOG_SAVE_REG_PAIR fp, lr, 0 bl EXTERNAL_C_FUNC(signal_handler_worker) LOCAL_LABEL(SignalHandlerWorkerReturn\Alignment): diff --git a/src/coreclr/pal/src/arch/arm64/context2.S b/src/coreclr/pal/src/arch/arm64/context2.S index 65ace16e47a98b..38cd7bc60939db 100644 --- a/src/coreclr/pal/src/arch/arm64/context2.S +++ b/src/coreclr/pal/src/arch/arm64/context2.S @@ -15,7 +15,6 @@ .arch_extension sve LEAF_ENTRY CONTEXT_CaptureContext, _TEXT PROLOG_STACK_ALLOC 32 - .cfi_adjust_cfa_offset 32 // save x1, x2 and x3 on stack so we can use them as scratch stp x1, x2, [sp] @@ -157,7 +156,6 @@ LEAF_END CONTEXT_CaptureContext, _TEXT LEAF_ENTRY RtlCaptureContext, _TEXT PROLOG_STACK_ALLOC 16 - .cfi_adjust_cfa_offset 16 str x1, [sp] // same as above, clang doesn't like mov with #imm32 // keep this in sync if CONTEXT_FULL changes diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index 46028a5d7bc26b..52ee2d1d707ac3 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -130,6 +130,7 @@ C_FUNC(\Name): .macro PROLOG_STACK_ALLOC Size sub sp, sp, \Size + .cfi_adjust_cfa_offset \Size .endm .macro EPILOG_STACK_FREE Size @@ -396,7 +397,7 @@ C_FUNC(\Name): PROLOG_SAVE_REG_PAIR x27, x28, 80 // Allocate space for the rest of the frame - PROLOG_STACK_ALLOC __PWTB_StackAlloc + sub sp, sp, __PWTB_StackAlloc .if (\SaveGPArgs == 1) // Spill argument registers. diff --git a/src/coreclr/vm/arm64/asmhelpers.S b/src/coreclr/vm/arm64/asmhelpers.S index 5769c4f2f2b6a5..d468b521b060be 100644 --- a/src/coreclr/vm/arm64/asmhelpers.S +++ b/src/coreclr/vm/arm64/asmhelpers.S @@ -191,7 +191,7 @@ GenerateRedirectedHandledJITCaseStub GCStress // PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -16 - PROLOG_STACK_ALLOC FaultingExceptionFrame_StackAlloc + sub sp, sp, FaultingExceptionFrame_StackAlloc // At this point, the stack maybe misaligned if the thread abort was asynchronously // triggered in the prolog or epilog of the managed method. For such a case, we must From c8d2e71c8741a184f644d8c54ad2253645fd00ec Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 30 Nov 2026 05:36:24 +0200 Subject: [PATCH 11/14] Switch arm64 CFA back to sp when restoring fp `EPILOG_RESTORE_REG_PAIR_INDEXED` now emits `.cfi_def_cfa_register sp` before reloading fp, matching loongarch64/riscv64, so the transition block epilogs no longer need explicit `.cfi_def_cfa`. `DelayLoad_Helper` keeps the frame's unwind state across its mid-function epilog. Co-authored-by: Cursor --- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 6 +++--- src/coreclr/vm/arm64/asmhelpers.S | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index 52ee2d1d707ac3..9e0db84db6ffc9 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -188,6 +188,9 @@ C_FUNC(\Name): .endm .macro EPILOG_RESTORE_REG_PAIR_INDEXED reg1, reg2, ofs + .ifc \reg1, fp + .cfi_def_cfa_register sp + .endif ldp \reg1, \reg2, [sp], \ofs .cfi_restore \reg1 .cfi_restore \reg2 @@ -421,7 +424,6 @@ C_FUNC(\Name): .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 @@ -437,7 +439,6 @@ C_FUNC(\Name): .macro EPILOG_WITH_TRANSITION_BLOCK_RETURN_SKIP_SWIFT_ERROR add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG x22, 40 @@ -462,7 +463,6 @@ C_FUNC(\Name): RESTORE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters add sp, sp, __PWTB_StackAlloc - .cfi_def_cfa sp, __PWTB_TransitionBlockSize EPILOG_RESTORE_REG_PAIR x19, x20, 16 EPILOG_RESTORE_REG_PAIR x21, x22, 32 diff --git a/src/coreclr/vm/arm64/asmhelpers.S b/src/coreclr/vm/arm64/asmhelpers.S index d468b521b060be..23d35bbb128d18 100644 --- a/src/coreclr/vm/arm64/asmhelpers.S +++ b/src/coreclr/vm/arm64/asmhelpers.S @@ -380,7 +380,10 @@ C_FUNC(DelayLoad_Helper\suffix): bl C_FUNC(DynamicHelperWorker) cbnz x0, LOCAL_LABEL(FakeProlog\suffix\()_0) ldr x0, [sp, #__PWTB_ArgumentRegister_FirstArg] + .cfi_remember_state EPILOG_WITH_TRANSITION_BLOCK_RETURN + .cfi_restore_state + .cfi_def_cfa_offset __PWTB_TransitionBlockSize // workaround cfi_restore_state bug LOCAL_LABEL(FakeProlog\suffix\()_0): mov x12, x0 EPILOG_WITH_TRANSITION_BLOCK_TAILCALL From 455670ef6d5ecd352b9594c5d3ef1d3a4272ba34 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:07:48 +0200 Subject: [PATCH 12/14] Unify the transition block layout comments Use the same header and compact stack layout comment for `PROLOG_WITH_TRANSITION_BLOCK` on all architectures, fold the two arm64 `__PWTB_TransitionBlock` assignments, and spell out the arm64 argument register offset. Co-authored-by: Cursor --- src/coreclr/runtime/amd64/AsmMacros_Unix.inc | 42 +++++------- .../runtime/amd64/AsmMacros_Windows.inc | 50 ++++++-------- src/coreclr/runtime/arm/AsmMacros_Unix.inc | 51 +++++---------- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 65 +++++-------------- src/coreclr/runtime/arm64/AsmMacros_Windows.h | 26 ++++++-- .../runtime/loongarch64/AsmMacros_Unix.inc | 46 ++++--------- .../runtime/riscv64/AsmMacros_Unix.inc | 55 ++++------------ 7 files changed, 117 insertions(+), 218 deletions(-) diff --git a/src/coreclr/runtime/amd64/AsmMacros_Unix.inc b/src/coreclr/runtime/amd64/AsmMacros_Unix.inc index b5805268708e74..3f923e317e37c4 100644 --- a/src/coreclr/runtime/amd64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/amd64/AsmMacros_Unix.inc @@ -291,33 +291,25 @@ C_FUNC(\Name\()_End): .byte 0xE0 .endm +//----------------------------------------------------------------------------- +// The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. +// +// PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +// (i.e. don't modify RSP after calling this). The locals must be 8 byte aligned. +// // Stack layout: // -// (stack parameters) -// ... -// return address -// CalleeSavedRegisters::rbp -// CalleeSavedRegisters::rbx -// CalleeSavedRegisters::r15 -// CalleeSavedRegisters::r14 -// CalleeSavedRegisters::r13 -// CalleeSavedRegisters::r12 -// ArgumentRegisters::r9 -// ArgumentRegisters::r8 -// ArgumentRegisters::rcx -// ArgumentRegisters::rdx -// ArgumentRegisters::rsi -// ArgumentRegisters::rdi <- __PWTB_StackAlloc, __PWTB_TransitionBlock -// padding to align xmm save area -// xmm7 -// xmm6 -// xmm5 -// xmm4 -// xmm3 -// xmm2 -// xmm1 -// xmm0 <- __PWTB_FloatArgumentRegisters -// extra locals + padding to qword align +// extra locals +// FloatArgumentRegisters (xmm0 - xmm7) +// padding +// struct TransitionBlock +// { +// ArgumentRegisters (rdi, rsi, rdx, rcx, r8, r9) +// CalleeSavedRegisters (r12 - r15, rbx, rbp) +// return address +// } +// Stack arguments +// .macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, stackAllocOnEntry = 0, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 set_cfa_register rsp, 8 diff --git a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc index 71255c2214cf33..bf7c8d9e9148bd 100644 --- a/src/coreclr/runtime/amd64/AsmMacros_Windows.inc +++ b/src/coreclr/runtime/amd64/AsmMacros_Windows.inc @@ -347,39 +347,27 @@ endm ;; GC type flags GC_ALLOC_FINALIZE equ 1 +;----------------------------------------------------------------------------- +; The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. +; +; PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +; (i.e. don't modify RSP after calling this). The locals must be 8 byte aligned. +; ; Stack layout: ; -; (stack parameters) -; ... -; r9 -; r8 -; rdx -; rcx <- __PWTB_ArgumentRegisters -; return address -; CalleeSavedRegisters::r15 -; CalleeSavedRegisters::r14 -; CalleeSavedRegisters::r13 -; CalleeSavedRegisters::r12 -; CalleeSavedRegisters::rbp -; CalleeSavedRegisters::rbx -; CalleeSavedRegisters::rsi -; CalleeSavedRegisters::rdi <- __PWTB_StackAlloc -; padding to align xmm save area -; xmm3 -; xmm2 -; xmm1 -; xmm0 <- __PWTB_FloatArgumentRegisters -; Optional: Callee saved floating point registers -; xmm15 -; . -; . -; xmm6 -; extra locals + padding to qword align -; callee's r9 -; callee's r8 -; callee's rdx -; callee's rcx - +; outgoing argument shadow space (rcx, rdx, r8, r9) +; extra locals +; FloatCalleeSavedRegisters (xmm6 - xmm15) if calleeSavedFloatRegs=PushCalleeSavedFloatRegs +; FloatArgumentRegisters (xmm0 - xmm3) +; padding +; struct TransitionBlock +; { +; CalleeSavedRegisters (rdi, rsi, rbx, rbp, r12 - r15) +; return address +; } +; ArgumentRegisters (rcx, rdx, r8, r9) in the caller's shadow space +; Stack arguments +; PROLOG_WITH_TRANSITION_BLOCK macro extraLocals := <0>, calleeSavedFloatRegs := , stackAllocOnEntry := <0>, stackAllocSpill1, stackAllocSpill2, stackAllocSpill3 __PWTB_FloatArgumentRegisters = SIZEOF_ARGUMENT_SHADOW_SPACE + extraLocals diff --git a/src/coreclr/runtime/arm/AsmMacros_Unix.inc b/src/coreclr/runtime/arm/AsmMacros_Unix.inc index db58c924b666c0..b038086c4a1691 100644 --- a/src/coreclr/runtime/arm/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm/AsmMacros_Unix.inc @@ -233,41 +233,26 @@ C_FUNC(\Name): 3: .endm +//----------------------------------------------------------------------------- +// The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. +// +// PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +// (i.e. don't modify SP after calling this). +// // Stack layout: // -// (stack parameters) -// ... -// ArgumentRegisters::r3 -// ArgumentRegisters::r2 -// ArgumentRegisters::r1 -// ArgumentRegisters::r0 -// CalleeSavedRegisters::lr -// CalleeSavedRegisters::r11 -// CalleeSavedRegisters::r10 -// CalleeSavedRegisters::r9 -// CalleeSavedRegisters::r8 -// CalleeSavedRegisters::r7 <- r7 -// CalleeSavedRegisters::r6 -// CalleeSavedRegisters::r5 -// CalleeSavedRegisters::r4 <- __PWTB_StackAlloc, __PWTB_TransitionBlock -// padding to align float save area -// d7 -// d6 -// d5 -// d4 -// d3 -// d2 -// d1 -// d0 <- __PWTB_FloatArgumentRegisters -// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) -// d15 -// d14 -// d13 -// d12 -// d11 -// d10 -// d9 -// d8 <- __PWTB_FloatCalleeSavedRegisters +// extra locals +// FloatCalleeSavedRegisters (d8 - d15) if pushCalleeSavedFloatRegs=1 +// FloatArgumentRegisters (d0 - d7) if saveFpArgs=1 +// padding +// struct TransitionBlock +// { +// CalleeSavedRegisters (r4 - r11, lr) +// ArgumentRegisters (r0 - r3) +// } +// Stack arguments +// +// r7 points at the saved r7 in CalleeSavedRegisters. // // pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save // the callee-saved floating point registers (d8-d15) to the stack. diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index 9e0db84db6ffc9..69218daef1292f 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -311,53 +311,24 @@ C_FUNC(\Name): .endm //----------------------------------------------------------------------------- -// Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and -// comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned +// The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. +// +// PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +// (i.e. don't modify SP after calling this). The locals must be 8 byte aligned. // // Stack layout: // -// (stack parameters) -// ... -// fp -// lr -// CalleeSavedRegisters::x28 -// CalleeSavedRegisters::x27 -// CalleeSavedRegisters::x26 -// CalleeSavedRegisters::x25 -// CalleeSavedRegisters::x24 -// CalleeSavedRegisters::x23 -// CalleeSavedRegisters::x22 -// CalleeSavedRegisters::x21 -// CalleeSavedRegisters::x20 -// CalleeSavedRegisters::x19 -// CalleeSavedRegisters::x30 (Lr) -// CalleeSavedRegisters::x29 (Fp) -// ArgumentRegisters::x7 -// ArgumentRegisters::x6 -// ArgumentRegisters::x5 -// ArgumentRegisters::x4 -// ArgumentRegisters::x3 -// ArgumentRegisters::x2 -// ArgumentRegisters::x1 -// ArgumentRegisters::x0 -// ArgumentRegisters::x8 -// FloatRegisters::q7 -// FloatRegisters::q6 -// FloatRegisters::q5 -// FloatRegisters::q4 -// FloatRegisters::q3 -// FloatRegisters::q2 -// FloatRegisters::q1 -// FloatRegisters::q0 -// Optional: Callee saved floating point registers (if pushCalleeSavedFloatRegs=1) -// d15 -// d14 -// d13 -// d12 -// d11 -// d10 -// d9 -// d8 +// extra locals +// FloatCalleeSavedRegisters (d8 - d15) if pushCalleeSavedFloatRegs=1 +// FloatArgumentRegisters (q0 - q7) +// struct TransitionBlock +// { +// CalleeSavedRegisters (fp, lr, x19 - x28) +// padding +// RetBuffReg (x8) +// ArgumentRegisters (x0 - x7) +// } +// Stack arguments // // pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save // the callee-saved floating point registers (d8-d15) to the stack. @@ -380,14 +351,12 @@ C_FUNC(\Name): __PWTB_FloatArgumentRegisters = __PWTB_FloatCalleeSavedRegisters .endif - __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters - // Always reserve space for the FP arg regs, even if they are not saved in the PROLOG_WITH_TRANSITION_BLOCK // Some code using this macro saves them optionally - __PWTB_TransitionBlock = __PWTB_TransitionBlock + SIZEOF__FloatArgumentRegisters + __PWTB_TransitionBlock = __PWTB_FloatArgumentRegisters + SIZEOF__FloatArgumentRegisters __PWTB_StackAlloc = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 104 + __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 12 * 8 + 8 // CalleeSavedRegisters + padding __PWTB_ArgumentRegister_FirstArg = __PWTB_ArgumentRegisters + 8 __PWTB_TransitionBlockSize = 176 diff --git a/src/coreclr/runtime/arm64/AsmMacros_Windows.h b/src/coreclr/runtime/arm64/AsmMacros_Windows.h index 842c19196f2330..7c08199769bd4d 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Windows.h +++ b/src/coreclr/runtime/arm64/AsmMacros_Windows.h @@ -257,12 +257,24 @@ GC_ALLOC_ALIGN8_BIAS equ 4 GC_ALLOC_ALIGN8 equ 8 ;----------------------------------------------------------------------------- -; The following group of macros assist in implementing prologs and epilogs for methods that set up some -; subclass of TransitionFrame. They ensure that the SP is 16-byte aligned at the conclusion of the prolog - -;----------------------------------------------------------------------------- -; Define the prolog for a TransitionFrame-based method. This macro should be called first in the method and -; comprises the entire prolog (i.e. don't modify SP after calling this).The locals must be 8 byte aligned +; The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. +; +; PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +; (i.e. don't modify SP after calling this). The locals must be 8 byte aligned. +; +; Stack layout: +; +; extra locals +; FloatCalleeSavedRegisters (d8 - d15) if $pushCalleeSavedFloatRegs=PushCalleeSavedFloatRegs +; FloatArgumentRegisters (q0 - q7) if $SaveFPArgs +; struct TransitionBlock +; { +; CalleeSavedRegisters (fp, lr, x19 - x28) +; padding +; RetBuffReg (x8) +; ArgumentRegisters (x0 - x7) +; } +; Stack arguments ; ; $pushCalleeSavedFloatRegs - Optional parameter. If set to PushCalleeSavedFloatRegs, the macro will also save ; the callee-saved floating point registers (d8-d15) to the stack. These registers @@ -310,7 +322,7 @@ __PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters ENDIF __PWTB_StackAlloc SETA __PWTB_TransitionBlock -__PWTB_ArgumentRegisters SETA __PWTB_StackAlloc + 104 +__PWTB_ArgumentRegisters SETA __PWTB_StackAlloc + 12 * 8 + 8 ; CalleeSavedRegisters + padding __PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 PROLOG_SAVE_REG_PAIR fp, lr, #-176! diff --git a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc index 3b2d583e1d0e06..9fabda6296af9e 100644 --- a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc @@ -310,42 +310,22 @@ C_FUNC(\Name\()_End): .endm //----------------------------------------------------------------------------- -// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and -// comprises the entire prolog.The locals must be 8 byte aligned +// The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. // -// Save_argument_registers: -// GPR_a7 -// GPR_a6 -// GPR_a5 -// GPR_a4 -// GPR_a3 -// GPR_a2 -// GPR_a1 -// GPR_a0 +// PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +// (i.e. don't modify SP after calling this). The locals must be 8 byte aligned. // -// General Registers: -// GPR_s8 -// GPR_s7 -// GPR_s6 -// GPR_s5 -// GPR_s4 -// GPR_s3 -// GPR_s2 -// GPR_s1 -// GPR_s0 -// GPR_ra -// GPR_fp +// Stack layout: // -// Float Point: -// FPR_f31 / fs7 -// FPR_f30 / fs6 -// FPR_f29 / fs5 -// FPR_f28 / fs4 -// FPR_f27 / fs3 -// FPR_f26 / fs2 -// FPR_f25 / fs1 -// FPR_f24 / fs0 -// Extra: +// extra locals +// FloatArgumentRegisters (fa0 - fa7) if SaveFPRegs=1 +// struct TransitionBlock +// { +// CalleeSavedRegisters (fp, ra, s0 - s8) +// padding +// ArgumentRegisters (a0 - a7) +// } +// Stack arguments // .macro PROLOG_WITH_TRANSITION_BLOCK extraParameters = 0, extraLocals = 0, SaveFPRegs = 1 diff --git a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc index a8ca29dddad2c1..3177c3ebc26a80 100644 --- a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc @@ -342,50 +342,23 @@ C_FUNC(\Name): .endm //----------------------------------------------------------------------------- -// Define the prolog for a TransitionBlock-based method. This macro should be called first in the method and -// comprises the entire prolog.The locals must be 8 byte aligned +// The following group of macros assist in implementing prologs and epilogs for methods that set up TransitionFrame. // -// Save_argument_registers: -// GPR_a7 -// GPR_a6 -// GPR_a5 -// GPR_a4 -// GPR_a3 -// GPR_a2 -// GPR_a1 -// GPR_a0 +// PROLOG_WITH_TRANSITION_BLOCK should be called first in the method and comprises the entire prolog +// (i.e. don't modify SP after calling this). The locals must be 8 byte aligned. // -// Callee Saved Registers: -// GPR_gp -// GPR_tp -// GPR_s11 -// GPR_s10 -// GPR_s9 -// GPR_s8 -// GPR_s7 -// GPR_s6 -// GPR_s5 -// GPR_s4 -// GPR_s3 -// GPR_s2 -// GPR_s1 -// GPR_ra -// GPR_fp +// Stack layout: // -// Float Point: -// FPR_f27 / fs11 -// FPR_f26 / fs10 -// FPR_f25 / fs9 -// FPR_f24 / fs8 -// FPR_f23 / fs7 -// FPR_f22 / fs6 -// FPR_f21 / fs5 -// FPR_f20 / fs4 -// FPR_f19 / fs3 -// FPR_f18 / fs2 -// FPR_f9 / fs1 -// FPR_f8 / fs0 -// Extra: +// extra locals +// FloatCalleeSavedRegisters (fs0 - fs11) if pushCalleeSavedFloatRegs=1 +// FloatArgumentRegisters (fa0 - fa7) if SaveFPRegs=1 +// struct TransitionBlock +// { +// CalleeSavedRegisters (fp, ra, s1 - s11, tp, gp) +// padding +// ArgumentRegisters (a0 - a7) +// } +// Stack arguments // // pushCalleeSavedFloatRegs - Optional parameter. If set to 1, the macro will also save // the callee-saved floating point registers (fs0-fs11) to the stack. From 1db25163b8d9cb099db9167ae97060ac6432789f Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:54:21 +0200 Subject: [PATCH 13/14] Fix arm64 float callee-saved CFI in transition blocks The d8-d15 saves of `PROLOG_WITH_TRANSITION_BLOCK` happen after sp has moved below fp, so their unwind offsets are now rebased onto fp. Also use `PROLOG_STACK_ALLOC` for both `ALLOC_THROW_FRAME` paths and reword the `INLINE_GETTHREAD` comment: the requirement is an sp-based CFA, not the absence of saved fp/lr. --- .../Runtime/arm64/ExceptionHandling.S | 4 +--- .../Runtime/unix/unixasmmacrosarm64.inc | 5 +++-- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 20 ++++++++++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index ed99093d4698e8..b74ad73d524b6d 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -17,12 +17,10 @@ mov x3, sp // Setup a PAL_LIMITED_CONTEXT on the stack { + PROLOG_STACK_ALLOC 0x50 .if \exceptionType == HARDWARE_EXCEPTION - sub sp,sp,#0x50 - .cfi_adjust_cfa_offset 0x50 stp x3, x1, [sp] // x3 is the SP and x1 is the IP of the fault site .else - PROLOG_STACK_ALLOC 0x50 stp x3, lr, [sp] // x3 is the SP and lr is the IP of the fault site .endif stp d8, d9, [sp, #0x10] diff --git a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc index 6169e901efc7d6..cb92152c7de283 100644 --- a/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc +++ b/src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.inc @@ -11,7 +11,8 @@ // Inlined version of RhpGetThread. Target cannot be x0. // Preserves all other general-purpose registers, except x16 and x17 on Apple. // Can trash volatile FP/SIMD registers. -// Only for functions whose CFA is sp-based (no fp frame): the x0/lr save adjusts the CFA offset. +// Only for functions whose CFA is sp-based since the x0/lr save adjusts the CFA offset. The function may have +// saved fp and lr itself (e.g. CallEHFunclet), as long as fp is not the CFA register. .macro INLINE_GETTHREAD target // The caller may have saved lr already, so restore its unwind rules instead of resetting // them to the state at function entry. @@ -23,7 +24,7 @@ .endm // Target cannot be x0 or x2. Preserves x0-x2, which the allocation helpers keep live across it. -// Only for functions whose CFA is sp-based (no fp frame), see INLINE_GETTHREAD. +// Only for functions whose CFA is sp-based, see INLINE_GETTHREAD. .macro INLINE_GET_ALLOC_CONTEXT_BASE target .ifc \target, x0 .error "target cannot be x0" diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index 69218daef1292f..d2cde74a887dad 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -380,12 +380,22 @@ C_FUNC(\Name): SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters .endif - // Save callee-saved floating point registers if requested + // Save callee-saved floating point registers if requested. sp is __PWTB_StackAlloc below the CFA + // register fp here, so the unwind offsets are rebased onto fp. .if (__PWTB_PushCalleeSavedFloatRegs == 1) - PROLOG_SAVE_REG_PAIR d8, d9, __PWTB_FloatCalleeSavedRegisters - PROLOG_SAVE_REG_PAIR d10, d11, __PWTB_FloatCalleeSavedRegisters + 16 - PROLOG_SAVE_REG_PAIR d12, d13, __PWTB_FloatCalleeSavedRegisters + 32 - PROLOG_SAVE_REG_PAIR d14, d15, __PWTB_FloatCalleeSavedRegisters + 48 + __PWTB_FloatCalleeSavedRegistersFromFp = __PWTB_FloatCalleeSavedRegisters - __PWTB_StackAlloc + stp d8, d9, [sp, __PWTB_FloatCalleeSavedRegisters] + .cfi_rel_offset d8, __PWTB_FloatCalleeSavedRegistersFromFp + .cfi_rel_offset d9, __PWTB_FloatCalleeSavedRegistersFromFp + 8 + stp d10, d11, [sp, __PWTB_FloatCalleeSavedRegisters + 16] + .cfi_rel_offset d10, __PWTB_FloatCalleeSavedRegistersFromFp + 16 + .cfi_rel_offset d11, __PWTB_FloatCalleeSavedRegistersFromFp + 24 + stp d12, d13, [sp, __PWTB_FloatCalleeSavedRegisters + 32] + .cfi_rel_offset d12, __PWTB_FloatCalleeSavedRegistersFromFp + 32 + .cfi_rel_offset d13, __PWTB_FloatCalleeSavedRegistersFromFp + 40 + stp d14, d15, [sp, __PWTB_FloatCalleeSavedRegisters + 48] + .cfi_rel_offset d14, __PWTB_FloatCalleeSavedRegistersFromFp + 48 + .cfi_rel_offset d15, __PWTB_FloatCalleeSavedRegistersFromFp + 56 .endif .endm From 9a153c88ae129b6656a6e51b9ef371f4392f3fda Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:01:00 +0200 Subject: [PATCH 14/14] Derive transition block offsets from their components Compute `__PWTB_TransitionBlockSize` on arm64 and use it on Windows too, spell out the riscv64 and loongarch64 argument register offsets, and drop comments that restate the code. The copy of d8-d15 saved for seeding the context does not participate in unwinding, so save it without unwind info like on the other architectures. Co-authored-by: Cursor --- src/coreclr/runtime/arm64/AsmMacros_Unix.inc | 14 ++------------ src/coreclr/runtime/arm64/AsmMacros_Windows.h | 8 +++++--- src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc | 9 +++------ src/coreclr/runtime/riscv64/AsmMacros_Unix.inc | 10 +++------- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc index d2cde74a887dad..d3492fe16039d2 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/arm64/AsmMacros_Unix.inc @@ -358,7 +358,7 @@ C_FUNC(\Name): __PWTB_StackAlloc = __PWTB_TransitionBlock __PWTB_ArgumentRegisters = __PWTB_StackAlloc + 12 * 8 + 8 // CalleeSavedRegisters + padding __PWTB_ArgumentRegister_FirstArg = __PWTB_ArgumentRegisters + 8 - __PWTB_TransitionBlockSize = 176 + __PWTB_TransitionBlockSize = __PWTB_ArgumentRegister_FirstArg + 8 * 8 - __PWTB_StackAlloc PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, -__PWTB_TransitionBlockSize // Spill callee saved registers @@ -380,22 +380,12 @@ C_FUNC(\Name): SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters .endif - // Save callee-saved floating point registers if requested. sp is __PWTB_StackAlloc below the CFA - // register fp here, so the unwind offsets are rebased onto fp. + // Save callee-saved floating point registers if requested .if (__PWTB_PushCalleeSavedFloatRegs == 1) - __PWTB_FloatCalleeSavedRegistersFromFp = __PWTB_FloatCalleeSavedRegisters - __PWTB_StackAlloc stp d8, d9, [sp, __PWTB_FloatCalleeSavedRegisters] - .cfi_rel_offset d8, __PWTB_FloatCalleeSavedRegistersFromFp - .cfi_rel_offset d9, __PWTB_FloatCalleeSavedRegistersFromFp + 8 stp d10, d11, [sp, __PWTB_FloatCalleeSavedRegisters + 16] - .cfi_rel_offset d10, __PWTB_FloatCalleeSavedRegistersFromFp + 16 - .cfi_rel_offset d11, __PWTB_FloatCalleeSavedRegistersFromFp + 24 stp d12, d13, [sp, __PWTB_FloatCalleeSavedRegisters + 32] - .cfi_rel_offset d12, __PWTB_FloatCalleeSavedRegistersFromFp + 32 - .cfi_rel_offset d13, __PWTB_FloatCalleeSavedRegistersFromFp + 40 stp d14, d15, [sp, __PWTB_FloatCalleeSavedRegisters + 48] - .cfi_rel_offset d14, __PWTB_FloatCalleeSavedRegistersFromFp + 48 - .cfi_rel_offset d15, __PWTB_FloatCalleeSavedRegistersFromFp + 56 .endif .endm diff --git a/src/coreclr/runtime/arm64/AsmMacros_Windows.h b/src/coreclr/runtime/arm64/AsmMacros_Windows.h index 7c08199769bd4d..ed6b60e3807fdd 100644 --- a/src/coreclr/runtime/arm64/AsmMacros_Windows.h +++ b/src/coreclr/runtime/arm64/AsmMacros_Windows.h @@ -290,6 +290,7 @@ GC_ALLOC_ALIGN8 equ 8 ; is different from the start of the argument register save area. GBLA __PWTB_StackAlloc GBLA __PWTB_TransitionBlock + GBLA __PWTB_TransitionBlockSize GBLL __PWTB_SaveFPArgs IF "$SaveFPArgs" != "" @@ -324,8 +325,9 @@ __PWTB_TransitionBlock SETA __PWTB_FloatArgumentRegisters __PWTB_StackAlloc SETA __PWTB_TransitionBlock __PWTB_ArgumentRegisters SETA __PWTB_StackAlloc + 12 * 8 + 8 ; CalleeSavedRegisters + padding __PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 +__PWTB_TransitionBlockSize SETA __PWTB_ArgumentRegister_FirstArg + 8 * 8 - __PWTB_StackAlloc - PROLOG_SAVE_REG_PAIR fp, lr, #-176! + PROLOG_SAVE_REG_PAIR fp, lr, #-(__PWTB_TransitionBlockSize)! ; Spill callee saved registers PROLOG_SAVE_REG_PAIR x19, x20, #16 PROLOG_SAVE_REG_PAIR x21, x22, #32 @@ -366,7 +368,7 @@ __PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 EPILOG_RESTORE_REG_PAIR x23, x24, #48 EPILOG_RESTORE_REG_PAIR x25, x26, #64 EPILOG_RESTORE_REG_PAIR x27, x28, #80 - EPILOG_RESTORE_REG_PAIR fp, lr, #176! + EPILOG_RESTORE_REG_PAIR fp, lr, #(__PWTB_TransitionBlockSize)! MEND ;----------------------------------------------------------------------------- @@ -398,5 +400,5 @@ __PWTB_ArgumentRegister_FirstArg SETA __PWTB_ArgumentRegisters + 8 EPILOG_RESTORE_REG_PAIR x23, x24, #48 EPILOG_RESTORE_REG_PAIR x25, x26, #64 EPILOG_RESTORE_REG_PAIR x27, x28, #80 - EPILOG_RESTORE_REG_PAIR fp, lr, #176! + EPILOG_RESTORE_REG_PAIR fp, lr, #(__PWTB_TransitionBlockSize)! MEND diff --git a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc index 9fabda6296af9e..a7d33a6b410747 100644 --- a/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/loongarch64/AsmMacros_Unix.inc @@ -345,21 +345,18 @@ C_FUNC(\Name\()_End): __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 96 + __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 11 * 8 + 8 // CalleeSavedRegisters + padding + __PWTB_StackAlloc = __PWTB_ArgumentRegisters + 8 * 8 - // Including fp, ra, s0-s8, padding, and (a0-a7)arguments. (1+1+9+1)*8 + 8*8. - __PWTB_StackAlloc = __PWTB_TransitionBlock + 96 + 64 PROLOG_STACK_ALLOC __PWTB_StackAlloc // $fp,$ra PROLOG_SAVE_REG_PAIR 22, 1, __PWTB_CalleeSavedRegisters, 1 - // First, Spill argument registers. SAVE_ARGUMENT_REGISTERS $sp, __PWTB_ArgumentRegisters - // Then, Spill callee saved registers. $sp=$r3. + // $sp SAVE_CALLEESAVED_REGISTERS 3, __PWTB_CalleeSavedRegisters - // saving is f12-19. .if (__PWTB_SaveFPArgs == 1) SAVE_FLOAT_ARGUMENT_REGISTERS $sp, __PWTB_FloatArgumentRegisters .endif diff --git a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc index 3177c3ebc26a80..1fbf9fbb834766 100644 --- a/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc +++ b/src/coreclr/runtime/riscv64/AsmMacros_Unix.inc @@ -390,25 +390,21 @@ C_FUNC(\Name): __PWTB_CalleeSavedRegisters = __PWTB_TransitionBlock - __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 120 + 8 + __PWTB_ArgumentRegisters = __PWTB_TransitionBlock + 15 * 8 + 8 // CalleeSavedRegisters + padding + __PWTB_StackAlloc = __PWTB_ArgumentRegisters + 8 * 8 - // Including fp, ra, s1-s11, tp, gp, padding and (a0-a7)arguments. (1+1+11+1+1)*8 + 8 + 8*8. - __PWTB_StackAlloc = __PWTB_TransitionBlock + 120 + 8 + 64 PROLOG_STACK_ALLOC __PWTB_StackAlloc PROLOG_SAVE_REG_PAIR fp, ra, __PWTB_CalleeSavedRegisters, 1 - // First, Spill argument registers. SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters - // Then, Spill callee saved registers. sp=r2. SAVE_CALLEESAVED_REGISTERS sp, __PWTB_CalleeSavedRegisters - // saving is f10-17. .if (__PWTB_SaveFPArgs == 1) SAVE_FLOAT_ARGUMENT_REGISTERS sp, __PWTB_FloatArgumentRegisters .endif - // Save callee-saved floating point registers if requested (fs0-fs11) + // Save callee-saved floating point registers if requested .if (__PWTB_PushCalleeSavedFloatRegs == 1) fsd fs0, (__PWTB_FloatCalleeSavedRegisters)(sp) fsd fs1, (__PWTB_FloatCalleeSavedRegisters + 8)(sp)