From 6e7bcce51bdd1af15e54f03b2b6c2050e6623a03 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:40:35 +0200 Subject: [PATCH 1/6] Share arm64 EH funclet call helpers and align arm64 funclet ABI with x64 Funclets on arm64 now save only FP and LR, like funclets on x64 do not save any callee-saved registers. The runtime helpers that call funclets save all callee-saved registers instead. - JIT: the arm64 funclet frame is FP/LR plus the outgoing argument space. This removes the five funclet frame types and the padding that kept funclet frames in sync with the main frame. - CallEHFunclet and CallEHFilterFunclet live in runtime/arm64/EHHelpers.{S,asm} and are shared by CoreCLR and NativeAOT, following the x64 template. - NativeAOT pops the ExInfos in arch-neutral code and resumes after a catch through RhpResumeAfterCatch, like x64. - ReadyToRun major version 34, since older runtimes do not preserve the callee-saved registers around filter funclets. Co-authored-by: Cursor --- src/coreclr/inc/readytorun.h | 8 +- src/coreclr/jit/codegen.h | 8 +- src/coreclr/jit/codegenarm64.cpp | 640 +----------------- src/coreclr/jit/codegencommon.cpp | 1 - src/coreclr/jit/jitconfigvalues.h | 2 - src/coreclr/jit/lclvars.cpp | 2 +- .../src/System/Runtime/ExceptionHandling.cs | 8 +- .../src/System/Runtime/InternalCalls.cs | 4 +- src/coreclr/nativeaot/Runtime/CMakeLists.txt | 4 +- .../nativeaot/Runtime/StackFrameIterator.cpp | 30 +- .../Runtime/arm64/AsmMacros_Shared.h | 143 ++++ .../Runtime/arm64/ExceptionHandling.S | 498 ++------------ .../Runtime/arm64/ExceptionHandling.asm | 378 +---------- .../nativeaot/Runtime/inc/ModuleHeaders.h | 4 +- src/coreclr/runtime/arm64/EHHelpers.S | 155 +++++ src/coreclr/runtime/arm64/EHHelpers.asm | 157 +++++ .../Common/Internal/Runtime/ModuleHeaders.cs | 4 +- src/coreclr/vm/CMakeLists.txt | 2 + src/coreclr/vm/arm64/AsmMacros_Shared.h | 32 + src/coreclr/vm/arm64/asmhelpers.S | 76 --- src/coreclr/vm/arm64/asmhelpers.asm | 77 --- src/coreclr/vm/eetwain.cpp | 4 +- 22 files changed, 610 insertions(+), 1627 deletions(-) create mode 100644 src/coreclr/runtime/arm64/EHHelpers.S create mode 100644 src/coreclr/runtime/arm64/EHHelpers.asm diff --git a/src/coreclr/inc/readytorun.h b/src/coreclr/inc/readytorun.h index a1e9b3758edfba..f0f5f4ed0fcb7d 100644 --- a/src/coreclr/inc/readytorun.h +++ b/src/coreclr/inc/readytorun.h @@ -19,10 +19,10 @@ // src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h // If you update this, ensure you run `git grep MINIMUM_READYTORUN_MAJOR_VERSION` // and handle pending work. -#define READYTORUN_MAJOR_VERSION 33 -#define READYTORUN_MINOR_VERSION 0x0001 +#define READYTORUN_MAJOR_VERSION 34 +#define READYTORUN_MINOR_VERSION 0x0000 -#define MINIMUM_READYTORUN_MAJOR_VERSION 33 +#define MINIMUM_READYTORUN_MAJOR_VERSION 34 // R2R Version 2.1 adds the InliningInfo section // R2R Version 2.2 adds the ProfileDataInfo section @@ -86,6 +86,8 @@ // GCRefMaps, moves VASIG_COOKIE from 5 to 7, and prefixes map-bearing import // signatures with their GCRefMaps. // R2R Version 33.1 adds READYTORUN_HELPER_WasmProfSamplepoint (used on WebAssembly) +// R2R Version 34 changes the ABI for funclets on ARM64 to match AMD64: funclets save only FP and LR, and the +// runtime preserves the other callee-saved registers around the funclets that it calls struct READYTORUN_CORE_HEADER { diff --git a/src/coreclr/jit/codegen.h b/src/coreclr/jit/codegen.h index c1625aab3a247c..09caec895eaaf7 100644 --- a/src/coreclr/jit/codegen.h +++ b/src/coreclr/jit/codegen.h @@ -521,12 +521,7 @@ class CodeGen final : public CodeGenInterface // same. struct FuncletFrameInfoDsc { - regMaskTP fiSaveRegs; // Set of callee-saved registers saved in the funclet prolog (includes LR) - int fiSP_to_FPLR_save_delta; // FP/LR register save offset from SP (positive) - int fiSP_to_CalleeSave_delta; // First callee-saved register slot offset from SP (positive) - int fiFrameType; // Funclet frame types are numbered. See genFuncletProlog() for details. - int fiSpDelta1; // Stack pointer delta 1 (negative) - int fiSpDelta2; // Stack pointer delta 2 (negative) + unsigned fiOutgoingArgSpace; // Outgoing argument space allocated below the saved FP/LR, aligned to 16 }; FuncletFrameInfoDsc genFuncletInfo; @@ -666,7 +661,6 @@ class CodeGen final : public CodeGenInterface virtual void SetSaveFpLrWithAllCalleeSavedRegisters(bool value); virtual bool IsSaveFpLrWithAllCalleeSavedRegisters() const; bool genSaveFpLrWithAllCalleeSavedRegisters; - bool genForceFuncletFrameType5; bool genReverseAndPairCalleeSavedRegisters; #endif // TARGET_ARM64 diff --git a/src/coreclr/jit/codegenarm64.cpp b/src/coreclr/jit/codegenarm64.cpp index cd6cd0dc4a5ebf..dd42d1f67c11de 100644 --- a/src/coreclr/jit/codegenarm64.cpp +++ b/src/coreclr/jit/codegenarm64.cpp @@ -1160,42 +1160,17 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * filter: x0 = non-zero if the handler should handle the exception, zero otherwise (see GT_RETFILT) * finally/fault: none * - * The ARM64 funclet prolog sequence is one of the following (Note: #framesz is total funclet frame size, - * including everything; #outsz is outgoing argument space. #framesz must be a multiple of 16): + * The runtime calls funclets with FP set to the FP of the parent function, and the funclet does not change it. + * Funclets access the locals of the parent function relative to FP. Funclets do not save the callee-saved + * registers other than FP and LR: the runtime preserves them around funclets that it calls, and the main function + * saves the ones used by the funclets in its own prolog. Call-to-finally sites do not keep any values in registers + * across the call. All temps are allocated in the parent frame, so the funclet frame only needs to hold FP/LR and + * the outgoing argument space. * - * Frame type 1: - * For #outsz == 0 and #framesz <= 512: - * stp fp,lr,[sp,-#framesz]! ; establish the frame (predecrement by #framesz), save FP/LR - * stp x19,x20,[sp,#xxx] ; save callee-saved registers, as necessary + * The ARM64 funclet prolog sequence is (Note: #outsz is the outgoing argument space, aligned to 16): * - * The funclet frame is thus: - * - * | | - * |-----------------------| - * | incoming arguments | - * +=======================+ <---- Caller's SP - * | OSR padding | // If required - * |-----------------------| - * | Varargs regs space | // Only for varargs main functions; 64 bytes - * |-----------------------| - * |Callee saved registers | // multiple of 8 bytes - * |-----------------------| - * | MonitorAcquired | // 8 bytes; for synchronized methods - * |-----------------------| - * ~ alignment padding ~ // To make the whole frame 16 byte aligned. - * |-----------------------| - * | Saved FP, LR | // 16 bytes - * |-----------------------| <---- Ambient SP - * | | | - * ~ | Stack grows ~ - * | | downward | - * V - * - * Frame type 2: - * For #outsz != 0 and #framesz <= 512: - * sub sp,sp,#framesz ; establish the frame - * stp fp,lr,[sp,#outsz] ; save FP/LR. - * stp x19,x20,[sp,#xxx] ; save callee-saved registers, as necessary + * stp fp,lr,[sp,#-16]! ; save FP/LR + * sub sp,sp,#outsz ; create space for outgoing argument space, if any * * The funclet frame is thus: * @@ -1203,173 +1178,19 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * |-----------------------| * | incoming arguments | * +=======================+ <---- Caller's SP - * | OSR padding | // If required - * |-----------------------| - * | Varargs regs space | // Only for varargs main functions; 64 bytes - * |-----------------------| - * |Callee saved registers | // multiple of 8 bytes - * |-----------------------| - * | MonitorAcquired | // 8 bytes; for synchronized methods - * |-----------------------| - * ~ alignment padding ~ // To make the whole frame 16 byte aligned. - * |-----------------------| * | Saved FP, LR | // 16 bytes * |-----------------------| - * | Outgoing arg space | // multiple of 8 bytes + * | Outgoing arg space | // multiple of 16 bytes (optional; if #outsz > 0) * |-----------------------| <---- Ambient SP * | | | * ~ | Stack grows ~ * | | downward | * V * - * Frame type 3: - * For #framesz > 512: - * stp fp,lr,[sp,- (#framesz - #outsz)]! ; establish the frame, save FP/LR - * ; note that it is guaranteed here that (#framesz - #outsz) <= 240 - * stp x19,x20,[sp,#xxx] ; save callee-saved registers, as necessary - * sub sp,sp,#outsz ; create space for outgoing argument space - * - * The funclet frame is thus: - * - * | | - * |-----------------------| - * | incoming arguments | - * +=======================+ <---- Caller's SP - * | OSR padding | // If required - * |-----------------------| - * | Varargs regs space | // Only for varargs main functions; 64 bytes - * |-----------------------| - * |Callee saved registers | // multiple of 8 bytes - * |-----------------------| - * | MonitorAcquired | // 8 bytes; for synchronized methods - * |-----------------------| - * ~ alignment padding ~ // To make the first SP subtraction 16 byte aligned - * |-----------------------| - * | Saved FP, LR | // 16 bytes <-- SP after first adjustment (points at saved FP) - * |-----------------------| - * ~ alignment padding ~ // To make the whole frame 16 byte aligned (specifically, to 16-byte align the outgoing argument space). - * |-----------------------| - * | Outgoing arg space | // multiple of 8 bytes - * |-----------------------| <---- Ambient SP (SP after second adjustment) - * | | | - * ~ | Stack grows ~ - * | | downward | - * V - * - * Both #1 and #2 only change SP once. That means that there will be a maximum of one alignment slot needed. For the general case, #3, - * it is possible that we will need to add alignment to both changes to SP, leading to 16 bytes of alignment. Remember that the stack - * pointer needs to be 16 byte aligned at all times. The size of the callee-saved registers space is a maximum of 240 bytes: - * - * FP,LR registers - * 10 int callee-saved register x19-x28 - * 8 float callee-saved registers v8-v15 - * 8 saved integer argument registers x0-x7, if varargs function - * 1 monitor acquired slot -* 1 alignment slot - * == 30 slots * 8 bytes = 240 bytes. - * - * The outgoing argument size, however, can be very large, if we call a function that takes a large number of - * arguments (note that we currently use the same outgoing argument space size in the funclet as for the main - * function, even if the funclet doesn't have any calls, or has a much smaller, or larger, maximum number of - * outgoing arguments for any call). In that case, we need to 16-byte align the initial change to SP, before - * saving off the callee-saved registers, so we can use the limited immediate offset encodings we have available, - * before doing another 16-byte aligned SP adjustment to create the outgoing argument space. Both changes to - * SP might need to add alignment padding. - * - * In addition to the above "standard" frames, we also need to support a frame where the saved FP/LR are at the - * highest addresses. This is to match the frame layout (specifically, callee-saved registers including FP/LR) - * that is used in the main function when a GS cookie is required due to the use of localloc. - * (Note that localloc cannot be used in a funclet.) In these variants, not only has the position of FP/LR - * changed, but where the alignment padding is placed has also changed. - * - * Frame type 4 (variant of frame types 1 and 2): - * For #framesz <= 512: - * sub sp,sp,#framesz ; establish the frame - * stp x19,x20,[sp,#xxx] ; save callee-saved registers, as necessary - * stp fp,lr,[sp,#yyy] ; save FP/LR. - * - * The "#framesz <= 512" condition ensures that after we've established the frame, we can use "stp" with its - * maximum allowed offset (504) to save the callee-saved register at the highest address. - * - * We use "sub" instead of folding it into the next instruction as a predecrement as there might also be an - * alignment padding slot. - * - * The funclet frame is thus: - * - * | | - * |-----------------------| - * | incoming arguments | - * +=======================+ <---- Caller's SP - * | OSR padding | // If required - * |-----------------------| - * | Varargs regs space | // Only for varargs main functions; 64 bytes - * |-----------------------| - * | Saved LR | // 8 bytes - * |-----------------------| - * | Saved FP | // 8 bytes - * |-----------------------| - * |Callee saved registers | // multiple of 8 bytes - * |-----------------------| - * | MonitorAcquired | // 8 bytes; for synchronized methods - * |-----------------------| - * ~ alignment padding ~ // To make the whole frame 16 byte aligned. - * |-----------------------| - * | Outgoing arg space | // multiple of 8 bytes (optional; if #outsz > 0) - * |-----------------------| <---- Ambient SP - * | | | - * ~ | Stack grows ~ - * | | downward | - * V - * - * Frame type 5 (variant of frame type 3): - * For #framesz > 512: - * sub sp,sp,(#framesz - #outsz) ; establish part of the frame. Note that it is guaranteed here that (#framesz - #outsz) <= 240 - * stp x19,x20,[sp,#xxx] ; save callee-saved registers, as necessary - * stp fp,lr,[sp,#yyy] ; save FP/LR. - * sub sp,sp,#outsz ; create space for outgoing argument space - * - * For large frames with "#framesz > 512", we must do one SP adjustment first, after which we can save callee-saved - * registers with up to the maximum "stp" offset of 504. Then, we can establish the rest of the frame (namely, the - * space for the outgoing argument space). - * - * The funclet frame is thus: - * - * | | - * |-----------------------| - * | incoming arguments | - * +=======================+ <---- Caller's SP - * | OSR padding | // If required - * |-----------------------| - * | Varargs regs space | // Only for varargs main functions; 64 bytes - * |-----------------------| - * | Saved LR | // 8 bytes - * |-----------------------| - * | Saved FP | // 8 bytes - * |-----------------------| - * |Callee saved registers | // multiple of 8 bytes - * |-----------------------| - * | MonitorAcquired | // 8 bytes; for synchronized methods - * |-----------------------| - * ~ alignment padding ~ // To make the whole frame 16 byte aligned (specifically, to 16-byte align the outgoing argument space). - * |-----------------------| - * | Outgoing arg space | // multiple of 8 bytes - * |-----------------------| <---- Ambient SP (SP after second adjustment) - * | | | - * ~ | Stack grows ~ - * | | downward | - * V - * - * Note that in this case we might have 16 bytes of alignment that is adjacent. This is because we are doing 2 SP - * subtractions, and each one must be aligned up to 16 bytes. - * - * Funclets do not have varargs arguments. - * - * An example epilog sequence is then: + * The epilog sequence is then: * * add sp,sp,#outsz ; if any outgoing argument space - * ... ; restore callee-saved registers - * ldp x19,x20,[sp,#xxx] - * ldp fp,lr,[sp],#framesz + * ldp fp,lr,[sp],#16 * ret lr * * See CodeGen::genPushCalleeSavedRegisters() for a description of the main function frame layout. @@ -1396,141 +1217,14 @@ void CodeGen::genFuncletProlog(BasicBlock* block) GetEmitter()->emitPacInProlog(); } - regMaskTP maskSaveRegsFloat = genFuncletInfo.fiSaveRegs & RBM_ALLFLOAT; - regMaskTP maskSaveRegsInt = genFuncletInfo.fiSaveRegs & ~maskSaveRegsFloat; - - // Funclets must always save LR and FP, since when we have funclets we must have an FP frame. - assert((maskSaveRegsInt & RBM_LR) != 0); - assert((maskSaveRegsInt & RBM_FP) != 0); - - bool isFilter = block->CatchTypeIs(BBCT_FILTER); - - regMaskTP maskArgRegsLiveIn; - if (isFilter) - { - maskArgRegsLiveIn = RBM_R0 | RBM_R1; - } - else if (block->CatchTypeIs(BBCT_FINALLY, BBCT_FAULT)) - { - maskArgRegsLiveIn = RBM_NONE; - } - else - { - maskArgRegsLiveIn = RBM_R0; - } - - if (genFuncletInfo.fiFrameType == 1) - { - if (m_compiler->opts.IsOSR()) - { - // With OSR we may see large values for fiSpDelta1. - // We repurpose genAllocLclFram to do the necessary probing. - bool scratchRegIsZero = false; - genAllocLclFrame(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, &scratchRegIsZero, maskArgRegsLiveIn); - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 0); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, 0); - } - else - { - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, genFuncletInfo.fiSpDelta1, - INS_OPTS_PRE_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, genFuncletInfo.fiSpDelta1); - } - - maskSaveRegsInt &= ~(RBM_LR | RBM_FP); // We've saved these now - - assert(genFuncletInfo.fiSpDelta2 == 0); - assert(genFuncletInfo.fiSP_to_FPLR_save_delta == 0); - } - else if (genFuncletInfo.fiFrameType == 2) - { - // fiFrameType==2 constraints: - assert(genFuncletInfo.fiSpDelta1 < 0); - assert(genFuncletInfo.fiSpDelta1 >= -512); - - // generate sub SP,SP,imm - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); - - assert(genFuncletInfo.fiSpDelta2 == 0); - - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, - genFuncletInfo.fiSP_to_FPLR_save_delta); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, genFuncletInfo.fiSP_to_FPLR_save_delta); - - maskSaveRegsInt &= ~(RBM_LR | RBM_FP); // We've saved these now - } - else if (genFuncletInfo.fiFrameType == 3) - { - if (m_compiler->opts.IsOSR()) - { - // With OSR we may see large values for fiSpDelta1 - // We repurpose genAllocLclFram to do the necessary probing. - bool scratchRegIsZero = false; - genAllocLclFrame(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, &scratchRegIsZero, maskArgRegsLiveIn); - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 0); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, 0); - } - else - { - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, genFuncletInfo.fiSpDelta1, - INS_OPTS_PRE_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, genFuncletInfo.fiSpDelta1); - } - - maskSaveRegsInt &= ~(RBM_LR | RBM_FP); // We've saved these now - } - else if (genFuncletInfo.fiFrameType == 4) - { - // fiFrameType==4 constraints: - assert(genFuncletInfo.fiSpDelta1 < 0); - assert(genFuncletInfo.fiSpDelta1 >= -512); - - // generate sub SP,SP,imm - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); + GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, -2 * REGSIZE_BYTES, + INS_OPTS_PRE_INDEX); + m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, -2 * REGSIZE_BYTES); - assert(genFuncletInfo.fiSpDelta2 == 0); - } - else + if (genFuncletInfo.fiOutgoingArgSpace > 0) { - assert(genFuncletInfo.fiFrameType == 5); - - if (m_compiler->opts.IsOSR()) - { - // With OSR we may see large values for fiSpDelta1. - // We repurpose genAllocLclFram to do the necessary probing. - bool scratchRegIsZero = false; - genAllocLclFrame(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, &scratchRegIsZero, maskArgRegsLiveIn); - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - } - else - { - assert(genFuncletInfo.fiSpDelta1 < 0); - assert(genFuncletInfo.fiSpDelta1 >= -240); - genStackPointerAdjustment(genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); - } - } - - int lowestCalleeSavedOffset = genFuncletInfo.fiSP_to_CalleeSave_delta + - genFuncletInfo.fiSpDelta2; // We haven't done the second adjustment of SP yet (if any) - genSaveCalleeSavedRegistersHelp(maskSaveRegsInt | maskSaveRegsFloat, lowestCalleeSavedOffset, 0); - - if ((genFuncletInfo.fiFrameType == 3) || (genFuncletInfo.fiFrameType == 5)) - { - // Note that genFuncletInfo.fiSpDelta2 is always a non-positive value - assert(genFuncletInfo.fiSpDelta2 <= 0); - - // generate sub SP,SP,imm - if (genFuncletInfo.fiSpDelta2 < 0) - { - genStackPointerAdjustment(genFuncletInfo.fiSpDelta2, REG_R2, nullptr, /* reportUnwindData */ true); - } - else - { - // we will only see fiSpDelta2 == 0 for osr funclets - assert(m_compiler->opts.IsOSR()); - } + genStackPointerAdjustment(-(int)genFuncletInfo.fiOutgoingArgSpace, REG_R2, nullptr, + /* reportUnwindData */ true); } // This is the end of the OS-reported prolog for purposes of unwinding @@ -1541,7 +1235,7 @@ void CodeGen::genFuncletProlog(BasicBlock* block) * * Generates code for an EH funclet epilog. * - * See the description of frame shapes at genFuncletProlog(). + * See the description of the funclet frame at genFuncletProlog(). */ void CodeGen::genFuncletEpilog(BasicBlock* /* block */) @@ -1551,133 +1245,17 @@ void CodeGen::genFuncletEpilog(BasicBlock* /* block */) printf("*************** In genFuncletEpilog()\n"); #endif - bool unwindStarted = false; - - if (!unwindStarted) - { - // We can delay this until we know we'll generate an unwindable instruction, if necessary. - m_compiler->unwindBegEpilog(); - unwindStarted = true; - } - - regMaskTP maskRestoreRegsFloat = genFuncletInfo.fiSaveRegs & RBM_ALLFLOAT; - regMaskTP maskRestoreRegsInt = genFuncletInfo.fiSaveRegs & ~maskRestoreRegsFloat; - - // Funclets must always save LR and FP, since when we have funclets we must have an FP frame. - assert((maskRestoreRegsInt & RBM_LR) != 0); - assert((maskRestoreRegsInt & RBM_FP) != 0); - - if ((genFuncletInfo.fiFrameType == 3) || (genFuncletInfo.fiFrameType == 5)) - { - // Note that genFuncletInfo.fiSpDelta2 is always a non-positive value - assert(genFuncletInfo.fiSpDelta2 <= 0); - - // generate add SP,SP,imm - if (genFuncletInfo.fiSpDelta2 < 0) - { - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta2, REG_R2, nullptr, /* reportUnwindData */ true); - } - else - { - // we should only zee zero SpDelta2 with osr. - assert(m_compiler->opts.IsOSR()); - } - } - - regMaskTP regsToRestoreMask = maskRestoreRegsInt | maskRestoreRegsFloat; - if ((genFuncletInfo.fiFrameType == 1) || (genFuncletInfo.fiFrameType == 2) || (genFuncletInfo.fiFrameType == 3)) - { - regsToRestoreMask &= ~(RBM_LR | RBM_FP); // We restore FP/LR at the end - } - int lowestCalleeSavedOffset = genFuncletInfo.fiSP_to_CalleeSave_delta + genFuncletInfo.fiSpDelta2; - genRestoreCalleeSavedRegistersHelp(regsToRestoreMask, lowestCalleeSavedOffset, 0); - - if (genFuncletInfo.fiFrameType == 1) - { - // With OSR we may see large values for fiSpDelta1 - // - if (m_compiler->opts.IsOSR()) - { - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 0); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, 0); - - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - } - else - { - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, -genFuncletInfo.fiSpDelta1, - INS_OPTS_POST_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, genFuncletInfo.fiSpDelta1); - } - - assert(genFuncletInfo.fiSpDelta2 == 0); - assert(genFuncletInfo.fiSP_to_FPLR_save_delta == 0); - } - else if (genFuncletInfo.fiFrameType == 2) - { - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, - genFuncletInfo.fiSP_to_FPLR_save_delta); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, genFuncletInfo.fiSP_to_FPLR_save_delta); + m_compiler->unwindBegEpilog(); - // fiFrameType==2 constraints: - assert(genFuncletInfo.fiSpDelta1 < 0); - assert(genFuncletInfo.fiSpDelta1 >= -512); - - // generate add SP,SP,imm - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); - - assert(genFuncletInfo.fiSpDelta2 == 0); - } - else if (genFuncletInfo.fiFrameType == 3) - { - // With OSR we may see large values for fiSpDelta1 - // - if (m_compiler->opts.IsOSR()) - { - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 0); - m_compiler->unwindSaveRegPair(REG_FP, REG_LR, 0); - - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - } - else - { - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, -genFuncletInfo.fiSpDelta1, - INS_OPTS_POST_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, genFuncletInfo.fiSpDelta1); - } - } - else if (genFuncletInfo.fiFrameType == 4) + if (genFuncletInfo.fiOutgoingArgSpace > 0) { - // fiFrameType==4 constraints: - assert(genFuncletInfo.fiSpDelta1 < 0); - assert(genFuncletInfo.fiSpDelta1 >= -512); - - // generate add SP,SP,imm - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); - - assert(genFuncletInfo.fiSpDelta2 == 0); + genStackPointerAdjustment((int)genFuncletInfo.fiOutgoingArgSpace, REG_R2, nullptr, + /* reportUnwindData */ true); } - else - { - assert(genFuncletInfo.fiFrameType == 5); - // Same work as fiFrameType==4, but different asserts. - assert(genFuncletInfo.fiSpDelta1 < 0); - - // With OSR we may see large values for fiSpDelta1 as the funclet - // frame currently must pad with the Tier0 frame size. - // - if (m_compiler->opts.IsOSR()) - { - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_SCRATCH, nullptr, /* reportUnwindData */ true); - } - else - { - // generate add SP,SP,imm - assert(genFuncletInfo.fiSpDelta1 >= -240); - genStackPointerAdjustment(-genFuncletInfo.fiSpDelta1, REG_NA, nullptr, /* reportUnwindData */ true); - } - } + GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 2 * REGSIZE_BYTES, + INS_OPTS_POST_INDEX); + m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, -2 * REGSIZE_BYTES); if (JitConfig.JitPacEnabled() != 0) { @@ -1694,8 +1272,7 @@ void CodeGen::genFuncletEpilog(BasicBlock* /* block */) * * Capture the information used to generate the funclet prologs and epilogs. * Note that all funclet prologs are identical, and all funclet epilogs are - * identical (per type: filters are identical, and non-filters are identical). - * Thus, we compute the data used for these just once. + * identical. Thus, we compute the data used for these just once. * * See genFuncletProlog() for more information about the prolog/epilog sequences. */ @@ -1710,162 +1287,16 @@ void CodeGen::genCaptureFuncletPrologEpilogInfo() // The frame size and offsets must be finalized assert(m_compiler->lvaDoneFrameLayout == Compiler::FINAL_FRAME_LAYOUT); - regMaskTP rsMaskSaveRegs = regSet.rsMaskCalleeSaved; - assert((rsMaskSaveRegs & RBM_LR) != 0); - assert((rsMaskSaveRegs & RBM_FP) != 0); - - unsigned saveRegsCount = genCountBits(rsMaskSaveRegs); - unsigned saveRegsSize = saveRegsCount * REGSIZE_BYTES; - if (m_compiler->info.compIsVarArgs) - { - // For varargs we always save all of the integer register arguments - // so that they are contiguous with the incoming stack arguments. - saveRegsSize += MAX_REG_ARG * REGSIZE_BYTES; - } - - if ((m_compiler->lvaMonAcquired != BAD_VAR_NUM) && !m_compiler->opts.IsOSR()) - { - // We furthermore allocate the "monitor acquired" bool between PSP and - // the saved registers because this is part of the EnC header. - // Note that OSR methods reuse the monitor bool created by tier 0. - saveRegsSize += m_compiler->lvaLclStackHomeSize(m_compiler->lvaMonAcquired); - } - - if ((m_compiler->lvaResumedIndicator != BAD_VAR_NUM) && !m_compiler->opts.IsOSR()) - { - saveRegsSize += m_compiler->lvaLclStackHomeSize(m_compiler->lvaResumedIndicator); - } - - if ((m_compiler->lvaAsyncThreadObjectVar != BAD_VAR_NUM) && !m_compiler->opts.IsOSR()) - { - saveRegsSize += m_compiler->lvaLclStackHomeSize(m_compiler->lvaAsyncThreadObjectVar); - } - - if ((m_compiler->lvaAsyncExecutionContextVar != BAD_VAR_NUM) && !m_compiler->opts.IsOSR()) - { - saveRegsSize += m_compiler->lvaLclStackHomeSize(m_compiler->lvaAsyncExecutionContextVar); - } - - if ((m_compiler->lvaAsyncSynchronizationContextVar != BAD_VAR_NUM) && !m_compiler->opts.IsOSR()) - { - saveRegsSize += m_compiler->lvaLclStackHomeSize(m_compiler->lvaAsyncSynchronizationContextVar); - } - - unsigned const saveRegsSizeAligned = roundUp(saveRegsSize, STACK_ALIGN); - assert(m_compiler->lvaOutgoingArgSpaceSize % REGSIZE_BYTES == 0); - unsigned const outgoingArgSpaceAligned = roundUp(m_compiler->lvaOutgoingArgSpaceSize, STACK_ALIGN); - - // If do two SP adjustments, each one must be aligned. This represents the largest possible stack size, if two - // separate alignment slots are required. - unsigned const twoSpAdjustmentFuncletFrameSizeAligned = saveRegsSizeAligned + outgoingArgSpaceAligned; - assert((twoSpAdjustmentFuncletFrameSizeAligned % STACK_ALIGN) == 0); - - int SP_to_FPLR_save_delta; - int SP_to_CalleeSave_delta; - - // Are we stressing frame type 5? Don't do it unless we have non-zero outgoing arg space. - const bool useFrameType5 = genSaveFpLrWithAllCalleeSavedRegisters && genForceFuncletFrameType5 && - (m_compiler->lvaOutgoingArgSpaceSize > 0); - - if ((twoSpAdjustmentFuncletFrameSizeAligned <= 512) && !useFrameType5) - { - unsigned const oneSpAdjustmentFuncletFrameSize = saveRegsSize + m_compiler->lvaOutgoingArgSpaceSize; - unsigned const oneSpAdjustmentFuncletFrameSizeAligned = roundUp(oneSpAdjustmentFuncletFrameSize, STACK_ALIGN); - assert(oneSpAdjustmentFuncletFrameSizeAligned <= twoSpAdjustmentFuncletFrameSizeAligned); - - unsigned const oneSpAdjustmentFuncletFrameSizeAlignmentPad = - oneSpAdjustmentFuncletFrameSizeAligned - oneSpAdjustmentFuncletFrameSize; - assert((oneSpAdjustmentFuncletFrameSizeAlignmentPad == 0) || - (oneSpAdjustmentFuncletFrameSizeAlignmentPad == REGSIZE_BYTES)); - - if (genSaveFpLrWithAllCalleeSavedRegisters) - { - SP_to_FPLR_save_delta = oneSpAdjustmentFuncletFrameSizeAligned - (2 /* FP, LR */ * REGSIZE_BYTES); - if (m_compiler->info.compIsVarArgs) - { - SP_to_FPLR_save_delta -= MAX_REG_ARG * REGSIZE_BYTES; - } - - SP_to_CalleeSave_delta = m_compiler->lvaOutgoingArgSpaceSize + oneSpAdjustmentFuncletFrameSizeAlignmentPad; - - genFuncletInfo.fiFrameType = 4; - } - else - { - SP_to_FPLR_save_delta = m_compiler->lvaOutgoingArgSpaceSize; - SP_to_CalleeSave_delta = - SP_to_FPLR_save_delta + 2 /* FP, LR */ * REGSIZE_BYTES + oneSpAdjustmentFuncletFrameSizeAlignmentPad; - - if (m_compiler->lvaOutgoingArgSpaceSize == 0) - { - genFuncletInfo.fiFrameType = 1; - } - else - { - genFuncletInfo.fiFrameType = 2; - } - } - - genFuncletInfo.fiSpDelta1 = -(int)oneSpAdjustmentFuncletFrameSizeAligned; - genFuncletInfo.fiSpDelta2 = 0; - - assert(genFuncletInfo.fiSpDelta1 + genFuncletInfo.fiSpDelta2 == -(int)oneSpAdjustmentFuncletFrameSizeAligned); - } - else - { - unsigned const saveRegsAlignmentPad = saveRegsSizeAligned - saveRegsSize; - assert((saveRegsAlignmentPad == 0) || (saveRegsAlignmentPad == REGSIZE_BYTES)); - - if (genSaveFpLrWithAllCalleeSavedRegisters) - { - SP_to_FPLR_save_delta = twoSpAdjustmentFuncletFrameSizeAligned - (2 /* FP, LR */ * REGSIZE_BYTES); - if (m_compiler->info.compIsVarArgs) - { - SP_to_FPLR_save_delta -= MAX_REG_ARG * REGSIZE_BYTES; - } - - SP_to_CalleeSave_delta = outgoingArgSpaceAligned + saveRegsAlignmentPad; - - genFuncletInfo.fiFrameType = 5; - } - else - { - SP_to_FPLR_save_delta = outgoingArgSpaceAligned; - SP_to_CalleeSave_delta = SP_to_FPLR_save_delta + 2 /* FP, LR */ * REGSIZE_BYTES + saveRegsAlignmentPad; - - genFuncletInfo.fiFrameType = 3; - } - - genFuncletInfo.fiSpDelta1 = -(int)saveRegsSizeAligned; - genFuncletInfo.fiSpDelta2 = -(int)outgoingArgSpaceAligned; - - assert(genFuncletInfo.fiSpDelta1 + genFuncletInfo.fiSpDelta2 == -(int)twoSpAdjustmentFuncletFrameSizeAligned); - } - - /* Now save it for future use */ - - genFuncletInfo.fiSaveRegs = rsMaskSaveRegs; - genFuncletInfo.fiSP_to_FPLR_save_delta = SP_to_FPLR_save_delta; - genFuncletInfo.fiSP_to_CalleeSave_delta = SP_to_CalleeSave_delta; + genFuncletInfo.fiOutgoingArgSpace = roundUp(m_compiler->lvaOutgoingArgSpaceSize, STACK_ALIGN); #ifdef DEBUG if (verbose) { printf("\n"); printf("Funclet prolog / epilog info\n"); - printf(" Save regs: "); - dspRegMask(genFuncletInfo.fiSaveRegs); - printf("\n"); - printf(" SP to FP/LR save location delta: %d\n", genFuncletInfo.fiSP_to_FPLR_save_delta); - printf(" SP to callee-saved area delta: %d\n", genFuncletInfo.fiSP_to_CalleeSave_delta); - printf(" Frame type: %d\n", genFuncletInfo.fiFrameType); - printf(" SP delta 1: %d\n", genFuncletInfo.fiSpDelta1); - printf(" SP delta 2: %d\n", genFuncletInfo.fiSpDelta2); + printf(" Outgoing arg space: %u\n", genFuncletInfo.fiOutgoingArgSpace); } - - assert(genFuncletInfo.fiSP_to_FPLR_save_delta >= 0); - assert(genFuncletInfo.fiSP_to_CalleeSave_delta >= 0); #endif // DEBUG } @@ -5178,19 +4609,6 @@ void CodeGen::SetSaveFpLrWithAllCalleeSavedRegisters(bool value) { JITDUMP("Setting genSaveFpLrWithAllCalleeSavedRegisters to %s\n", dspBool(value)); genSaveFpLrWithAllCalleeSavedRegisters = value; - - if (genSaveFpLrWithAllCalleeSavedRegisters) - { - // We'll use frame type 4 or 5. Frame type 5 only occurs if there is a very large outgoing argument - // space. This is extremely rare, so under stress force using this frame type. However, frame type 5 - // isn't used if there is no outgoing argument space; this is checked elsewhere. - - if ((m_compiler->opts.compJitSaveFpLrWithCalleeSavedRegisters == 3) || - m_compiler->compStressCompile(Compiler::STRESS_GENERIC_VARN, 50)) - { - genForceFuncletFrameType5 = true; - } - } } //--------------------------------------------------------------------- diff --git a/src/coreclr/jit/codegencommon.cpp b/src/coreclr/jit/codegencommon.cpp index 1defbe62422285..bccd4ce723f6d0 100644 --- a/src/coreclr/jit/codegencommon.cpp +++ b/src/coreclr/jit/codegencommon.cpp @@ -439,7 +439,6 @@ CodeGen::CodeGen(Compiler* theCompiler) #ifdef TARGET_ARM64 genSaveFpLrWithAllCalleeSavedRegisters = false; - genForceFuncletFrameType5 = false; genReverseAndPairCalleeSavedRegisters = false; #endif // TARGET_ARM64 } diff --git a/src/coreclr/jit/jitconfigvalues.h b/src/coreclr/jit/jitconfigvalues.h index 1f24eacabe0ab4..e081c4d037b6aa 100644 --- a/src/coreclr/jit/jitconfigvalues.h +++ b/src/coreclr/jit/jitconfigvalues.h @@ -904,8 +904,6 @@ CONFIG_STRING(JitRawHexCodeFile, "JitRawHexCodeFile") // 1: disable frames that save FP/LR registers with the callee-saved registers (at the top of the frame) // 2: force all frames to use the frame types that save FP/LR registers with the callee-saved registers (at the top // of the frame) -// 3: force all frames to use the frame types that save FP/LR registers with the callee-saved registers (at the top -// of the frame) and also force using the large funclet frame variation (frame 5) if possible. CONFIG_INTEGER(JitSaveFpLrWithCalleeSavedRegisters, "JitSaveFpLrWithCalleeSavedRegisters", 0) // Experimental support for vector length agnostic implementation of Vector diff --git a/src/coreclr/jit/lclvars.cpp b/src/coreclr/jit/lclvars.cpp index 0d4e300fb42ef9..2efa94b3269cb1 100644 --- a/src/coreclr/jit/lclvars.cpp +++ b/src/coreclr/jit/lclvars.cpp @@ -5719,7 +5719,7 @@ void Compiler::lvaAssignVirtualFrameOffsetsToLocals() { codeGen->SetSaveFpLrWithAllCalleeSavedRegisters(false); // Disable using new frames } - else if ((opts.compJitSaveFpLrWithCalleeSavedRegisters == 2) || (opts.compJitSaveFpLrWithCalleeSavedRegisters == 3)) + else if (opts.compJitSaveFpLrWithCalleeSavedRegisters == 2) { codeGen->SetSaveFpLrWithAllCalleeSavedRegisters(true); // Force using new frames } diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs index fbf26dc1667171..e69124c5f8fdb3 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs @@ -942,7 +942,7 @@ private static void DispatchEx(scoped ref StackFrameIterator frameIter, ref ExIn #if FEATURE_OBJCMARSHAL if (pReversePInvokePropagationCallback != IntPtr.Zero) { -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 InternalCalls.RhpPopExInfo(ref exInfo, frameIter.SP); InternalCalls.RhpCallPropagateExceptionCallback( pReversePInvokePropagationContext, pReversePInvokePropagationCallback, frameIter.RegisterSet, frameIter.PreviousTransitionFrame); @@ -962,7 +962,7 @@ private static void DispatchEx(scoped ref StackFrameIterator frameIter, ref ExIn // // ------------------------------------------------ exInfo._idxCurClause = catchingTryRegionIdx; -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 UIntPtr funcletCallerSP; byte* pResumeIP = InternalCalls.CallEHFunclet( exceptionObj, pCatchHandler, frameIter.RegisterSet, &funcletCallerSP); @@ -1106,7 +1106,7 @@ private static bool FindFirstPassHandler(object exception, uint idxStart, #if NATIVEAOT try { -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 UIntPtr funcletCallerSP; shouldInvokeHandler = InternalCalls.CallEHFilterFunclet(exception, frameIter.RegisterSet, pFilterFunclet, &funcletCallerSP) != 0; @@ -1259,7 +1259,7 @@ private static void InvokeSecondPass(ref ExInfo exInfo, uint idxStart, uint idxL byte* pFinallyHandler = ehClause._handlerAddress; exInfo._idxCurClause = curIdx; -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 UIntPtr funcletCallerSP; InternalCalls.CallEHFunclet(null, pFinallyHandler, exInfo._frameIter.RegisterSet, &funcletCallerSP); #else diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs index 17697494d68265..9b04b58d568263 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs @@ -144,7 +144,7 @@ internal static class InternalCalls // Miscellaneous helpers. // -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 [RuntimeImport(RuntimeLibrary, "CallEHFunclet")] [MethodImpl(MethodImplOptions.InternalCall)] internal static extern unsafe byte* CallEHFunclet( @@ -181,7 +181,7 @@ internal static extern unsafe bool RhpCallFilterFunclet( #if FEATURE_OBJCMARSHAL [RuntimeImport(RuntimeLibrary, "RhpCallPropagateExceptionCallback")] [MethodImpl(MethodImplOptions.InternalCall)] -#if TARGET_AMD64 +#if TARGET_AMD64 || TARGET_ARM64 internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( IntPtr callbackContext, IntPtr callback, void* pvRegDisplay, IntPtr pPreviousTransitionFrame); #else diff --git a/src/coreclr/nativeaot/Runtime/CMakeLists.txt b/src/coreclr/nativeaot/Runtime/CMakeLists.txt index 4a5a37b3674856..c7804872f0737c 100644 --- a/src/coreclr/nativeaot/Runtime/CMakeLists.txt +++ b/src/coreclr/nativeaot/Runtime/CMakeLists.txt @@ -323,11 +323,11 @@ if(NOT FEATURE_PORTABLE_HELPERS) endif() endif() -if (CLR_CMAKE_TARGET_ARCH_AMD64) +if (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64) list(APPEND RUNTIME_SOURCES_ARCH_ASM ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/EHHelpers.${ASM_SUFFIX} ) -endif (CLR_CMAKE_TARGET_ARCH_AMD64) +endif (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64) # Add architecture specific folder for looking up headers. convert_to_absolute_path(ARCH_SOURCES_DIR ${ARCH_SOURCES_DIR}) diff --git a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp index a5697ce2adc7cd..66d8a2bc0bafd4 100644 --- a/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp +++ b/src/coreclr/nativeaot/Runtime/StackFrameIterator.cpp @@ -37,7 +37,7 @@ EXTERN_C CODE_LOCATION ReturnFromUniversalTransitionTailCall; EXTERN_C CODE_LOCATION ReturnFromUniversalTransitionGuardedTailCall; #endif -#if defined(TARGET_AMD64) +#if defined(TARGET_AMD64) || defined(TARGET_ARM64) EXTERN_C CODE_LOCATION CallEHFunclet2; EXTERN_C CODE_LOCATION CallEHFilterFunclet2; #else @@ -1086,7 +1086,7 @@ void StackFrameIterator::UnwindFuncletInvokeThunk() #else // FEATURE_PORTABLE_HELPERS PTR_uintptr_t SP; -#if defined(TARGET_AMD64) +#if defined(TARGET_AMD64) || defined(TARGET_ARM64) bool isFilterInvoke = EQUALS_RETURN_ADDRESS(m_ControlPC, CallEHFilterFunclet2); #else bool isFilterInvoke = EQUALS_RETURN_ADDRESS(m_ControlPC, RhpCallFilterFunclet2); @@ -1220,21 +1220,10 @@ void StackFrameIterator::UnwindFuncletInvokeThunk() m_RegDisplay.pR11 = SP++; #elif defined(TARGET_ARM64) - PTR_uint64_t d = (PTR_uint64_t)(m_RegDisplay.SP); - - for (int i = 0; i < 8; i++) - { - m_RegDisplay.D[i] = *d++; - } - - SP = (PTR_uintptr_t)d; + SP = (PTR_uintptr_t)(m_RegDisplay.SP); if (!isFilterInvoke) { - // RhpCallCatchFunclet puts a couple of extra things on the stack that aren't put there by the other two - // thunks, but we don't need to know what they are here, so we just skip them. - SP += EQUALS_RETURN_ADDRESS(m_ControlPC, RhpCallCatchFunclet2) ? 6 : 4; - // Save the preserved regs portion of the REGDISPLAY across the unwind through the C# EH dispatch code. m_funcletPtrs.pX19 = m_RegDisplay.pX19; m_funcletPtrs.pX20 = m_RegDisplay.pX20; @@ -1264,6 +1253,17 @@ void StackFrameIterator::UnwindFuncletInvokeThunk() m_RegDisplay.pX27 = SP++; m_RegDisplay.pX28 = SP++; + PTR_uint64_t d = (PTR_uint64_t)SP; + + for (int i = 0; i < 8; i++) + { + m_RegDisplay.D[i] = *d++; + } + + SP = (PTR_uintptr_t)d; + + SP += 2; // 2 locals + #elif defined(TARGET_LOONGARCH64) PTR_uint64_t f = (PTR_uint64_t)(m_RegDisplay.SP); @@ -2392,7 +2392,7 @@ StackFrameIterator::ReturnAddressCategory StackFrameIterator::CategorizeUnadjust return InThrowSiteThunk; } -#if defined(TARGET_AMD64) +#if defined(TARGET_AMD64) || defined(TARGET_ARM64) if (EQUALS_RETURN_ADDRESS(returnAddress, CallEHFunclet2)) { return InFuncletInvokeThunk; diff --git a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h index 91fef9a06f72bf..f7b3ccfe94eff1 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h +++ b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h @@ -15,6 +15,81 @@ b RhpGcPollRare MEND + +;; CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY + MACRO + LOAD_FUNCLET_REGISTERS $regs, $scratch + + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX19] + ldr x19, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX20] + ldr x20, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX21] + ldr x21, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX22] + ldr x22, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX23] + ldr x23, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX24] + ldr x24, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX25] + ldr x25, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX26] + ldr x26, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX27] + ldr x27, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX28] + ldr x28, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] + ldr fp, [$scratch] + + add $scratch, $regs, #OFFSETOF__REGDISPLAY__D + ldp d8, d9, [$scratch, #0x00] + ldp d10, d11, [$scratch, #0x10] + ldp d12, d13, [$scratch, #0x20] + ldp d14, d15, [$scratch, #0x30] + MEND + + MACRO + STORE_FUNCLET_REGISTERS $regs, $scratch + + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX19] + str x19, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX20] + str x20, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX21] + str x21, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX22] + str x22, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX23] + str x23, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX24] + str x24, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX25] + str x25, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX26] + str x26, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX27] + str x27, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX28] + str x28, [$scratch] + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] + str fp, [$scratch] + + add $scratch, $regs, #OFFSETOF__REGDISPLAY__D + stp d8, d9, [$scratch, #0x00] + stp d10, d11, [$scratch, #0x10] + stp d12, d13, [$scratch, #0x20] + stp d14, d15, [$scratch, #0x30] + MEND + + MACRO + LOAD_FUNCLET_FRAME_POINTER $regs, $scratch + + ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] + ldr fp, [$scratch] + MEND + #else #include #include "AsmOffsets.inc" @@ -22,4 +97,72 @@ .macro TAILCALL_GC_POLL_RARE b C_FUNC(RhpGcPollRare) .endm + +// CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY +.macro LOAD_FUNCLET_REGISTERS regs, scratch + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX19] + ldr x19, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX20] + ldr x20, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX21] + ldr x21, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX22] + ldr x22, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX23] + ldr x23, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX24] + ldr x24, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX25] + ldr x25, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX26] + ldr x26, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX27] + ldr x27, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX28] + ldr x28, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] + ldr fp, [\scratch] + + add \scratch, \regs, #OFFSETOF__REGDISPLAY__D + ldp d8, d9, [\scratch, #0x00] + ldp d10, d11, [\scratch, #0x10] + ldp d12, d13, [\scratch, #0x20] + ldp d14, d15, [\scratch, #0x30] +.endm + +.macro STORE_FUNCLET_REGISTERS regs, scratch + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX19] + str x19, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX20] + str x20, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX21] + str x21, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX22] + str x22, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX23] + str x23, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX24] + str x24, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX25] + str x25, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX26] + str x26, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX27] + str x27, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX28] + str x28, [\scratch] + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] + str fp, [\scratch] + + add \scratch, \regs, #OFFSETOF__REGDISPLAY__D + stp d8, d9, [\scratch, #0x00] + stp d10, d11, [\scratch, #0x10] + stp d12, d13, [\scratch, #0x20] + stp d14, d15, [\scratch, #0x30] +.endm + +.macro LOAD_FUNCLET_FRAME_POINTER regs, scratch + ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] + ldr fp, [\scratch] +.endm #endif diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index 3c97f8138461e3..4ec53f68a294d8 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include -#include "AsmOffsets.inc" +#include "AsmMacros_Shared.h" #define STACKSIZEOF_ExInfo ((SIZEOF__ExInfo + 15)&(~15)) @@ -43,162 +42,6 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo .endm -// ----------------------------------------------------------------------------- -// Macro used to create frame of funclet calling helpers (RhpCallXXXXFunclet) -// extraStackSize - extra stack space that the user of the macro can use to -// store additional registers - .macro ALLOC_CALL_FUNCLET_FRAME extraStackSize - - // Using below prolog instead of PROLOG_SAVE_REG_PAIR fp,lr, #-60! - // is intentional. Above statement would also emit instruction to save - // sp in fp. If sp is saved in fp in prolog then it is not expected that fp can change in the body - // of method. However, this method needs to be able to change fp before calling funclet. - // This is required to access locals in funclet. - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp,lr, -0x60 - PROLOG_SAVE_REG_PAIR x19, x20, 0x10 - PROLOG_SAVE_REG_PAIR x21, x22, 0x20 - PROLOG_SAVE_REG_PAIR x23, x24, 0x30 - PROLOG_SAVE_REG_PAIR x25, x26, 0x40 - PROLOG_SAVE_REG_PAIR x27, x28, 0x50 - mov fp, sp - .cfi_def_cfa_register fp - - .if \extraStackSize != 0 - PROLOG_STACK_ALLOC \extraStackSize - .endif - .endm - -// ----------------------------------------------------------------------------- -// Macro used to free frame of funclet calling helpers (RhpCallXXXXFunclet) -// extraStackSize - extra stack space that the user of the macro can use to -// store additional registers. -// It needs to match the value passed to the corresponding -// ALLOC_CALL_FUNCLET_FRAME. - .macro FREE_CALL_FUNCLET_FRAME extraStackSize - - .if \extraStackSize != 0 - EPILOG_STACK_FREE \extraStackSize - .endif - - EPILOG_RESTORE_REG_PAIR x19, x20, 0x10 - EPILOG_RESTORE_REG_PAIR x21, x22, 0x20 - EPILOG_RESTORE_REG_PAIR x23, x24, 0x30 - EPILOG_RESTORE_REG_PAIR x25, x26, 0x40 - EPILOG_RESTORE_REG_PAIR x27, x28, 0x50 - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 0x60 - .endm - - -// ----------------------------------------------------------------------------- -// Macro used to restore preserved general purpose and FP registers from REGDISPLAY -// regdisplayReg - register pointing to the REGDISPLAY structure - .macro RESTORE_PRESERVED_REGISTERS regdisplayReg - - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - ldr x19, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - ldr x20, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - ldr x21, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - ldr x22, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - ldr x23, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - ldr x24, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - ldr x25, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - ldr x26, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - ldr x27, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - ldr x28, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [x12] - // - // load FP preserved regs - // - add x12, \regdisplayReg, #OFFSETOF__REGDISPLAY__D - ldp d8, d9, [x12, #0x00] - ldp d10, d11, [x12, #0x10] - ldp d12, d13, [x12, #0x20] - ldp d14, d15, [x12, #0x30] - .endm - -// ----------------------------------------------------------------------------- -// Macro used to save preserved general purpose and FP registers to REGDISPLAY -// regdisplayReg - register pointing to the REGDISPLAY structure - .macro SAVE_PRESERVED_REGISTERS regdisplayReg - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - str x19, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - str x20, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - str x21, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - str x22, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - str x23, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - str x24, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - str x25, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - str x26, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - str x27, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - str x28, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - str fp, [x12] - // - // store vfp preserved regs - // - add x12, \regdisplayReg, #OFFSETOF__REGDISPLAY__D - stp d8, d9, [x12, #0x00] - stp d10, d11, [x12, #0x10] - stp d12, d13, [x12, #0x20] - stp d14, d15, [x12, #0x30] - .endm - - -// ----------------------------------------------------------------------------- -// Macro used to thrash preserved general purpose registers in REGDISPLAY -// to make sure nobody uses them -// regdisplayReg - register pointing to the REGDISPLAY structure - .macro TRASH_PRESERVED_REGISTERS_STORAGE regdisplayReg - -#if _DEBUG - movz x3, #0xbaad, LSL #48 - movk x3, #0xdeed, LSL #32 - movk x3, #0xbaad, LSL #16 - movk x3, #0xdeed - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - str x3, [x12] - ldr x12, [\regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - str x3, [x12] -#endif // _DEBUG - .endm - .macro GetThreadX2 stp x0, x1, [sp, -16]! bl C_FUNC(RhpGetThread) @@ -408,275 +251,59 @@ LOCAL_LABEL(NotHijacked): NESTED_END RhpRethrow, _TEXT // -// void* FASTCALL RhpCallCatchFunclet(OBJECTREF exceptionObj, void* pHandlerIP, REGDISPLAY* pRegDisplay, -// ExInfo* pExInfo) +// void FASTCALL RhpResumeAfterCatch(REGDISPLAY* pRegDisplay, void* pResumeIP) // -// INPUT: X0: exception object -// X1: handler funclet address -// X2: REGDISPLAY* -// X3: ExInfo* +// Resumes execution after a catch funclet called by CallEHFunclet has returned and the dispatcher has popped +// the ExInfos below the resume SP. Does not return. // -// OUTPUT: +// INPUT: X0: REGDISPLAY* of the frame that contains the catch +// X1: resume address returned by the catch funclet // - NESTED_ENTRY RhpCallCatchFunclet, _TEXT, NoHandler - - ALLOC_CALL_FUNCLET_FRAME 0x70 // Size needs to be equal with ExceptionHandling.asm variant of this function - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - stp x0, x1, [sp, #0x40] // x0 to x3 are stored to restore them anytime - stp x2, x3, [sp, #0x50] - -#define rsp_offset_x0 0x40 -#define rsp_offset_x1 0x48 -#define rsp_offset_x2 0x50 -#define rsp_offset_x3 0x58 -#define rsp_CatchFunclet_offset_thread 0x60 + NESTED_ENTRY RhpResumeAfterCatch, _TEXT, NoHandler - // - // clear the DoNotTriggerGc flag, trashes x4-x6 - // + PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x20 + stp x0, x1, [sp, #0x10] - bl C_FUNC(RhpGetThread) - str x0, [sp, rsp_CatchFunclet_offset_thread] - mov x5,x0 - ldp x0, x1, [sp, #0x40] - ldp x2, x3, [sp, #0x50] - - add x12, x5, #OFFSETOF__Thread__m_ThreadStateFlags + bl C_FUNC(RhpGetThread) + mov x3, x0 // x3 <- Thread* -LOCAL_LABEL(ClearRetry_Catch): + // Clear the DoNotTriggerGc state before resuming in the managed code after the catch. + add x12, x3, #OFFSETOF__Thread__m_ThreadStateFlags +LOCAL_LABEL(ResumeAfterCatch_ClearRetry): ldxr w4, [x12] bic w4, w4, #TSF_DoNotTriggerGc - stxr w6, w4, [x12] - cbz w6, LOCAL_LABEL(ClearSuccess_Catch) - b LOCAL_LABEL(ClearRetry_Catch) -LOCAL_LABEL(ClearSuccess_Catch): - - // - // set preserved regs to the values expected by the funclet - // - RESTORE_PRESERVED_REGISTERS x2 - // - // trash the values at the old homes to make sure nobody uses them - // - TRASH_PRESERVED_REGISTERS_STORAGE x2 - - // - // call the funclet - // - // x0 still contains the exception object - blr x1 - -ALTERNATE_ENTRY RhpCallCatchFunclet2 - - // x0 contains resume IP - - ldr x2, [sp, #rsp_offset_x2] // x2 <- REGDISPLAY* - -#ifdef _DEBUG - // Call into some C++ code to validate the pop of the ExInfo. We only do this in debug because we - // have to spill all the preserved registers and then refill them after the call. - - str x0, [sp, rsp_offset_x0] - - SAVE_PRESERVED_REGISTERS x2 - - ldr x0, [sp, rsp_CatchFunclet_offset_thread] // x0 <- Thread* - ldr x1, [sp, #rsp_offset_x3] // x1 <- current ExInfo* - ldr x2, [x2, #OFFSETOF__REGDISPLAY__SP] // x2 <- resume SP value - bl C_FUNC(RhpValidateExInfoPop) - - ldr x2, [sp, rsp_offset_x2] // x2 <- REGDISPLAY* - - RESTORE_PRESERVED_REGISTERS x2 - - ldr x0, [sp, rsp_offset_x0] // reload resume IP -#endif - - ldr x1, [sp, rsp_CatchFunclet_offset_thread] + stxr w5, w4, [x12] + cbnz w5, LOCAL_LABEL(ResumeAfterCatch_ClearRetry) // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. - INLINE_THREAD_UNHIJACK x1, x3, x12 // Thread in x1, trashes x3 and x12 + INLINE_THREAD_UNHIJACK x3, x4, x12 // Thread in x3, trashes x4 and x12 - ldr x3, [sp, #rsp_offset_x3] // x3 <- current ExInfo* - ldr x2, [x2, #OFFSETOF__REGDISPLAY__SP] // x2 <- resume SP value + ldp x0, x1, [sp, #0x10] // x0 <- REGDISPLAY*, x1 <- resume IP -LOCAL_LABEL(PopExInfoLoop): - ldr x3, [x3, #OFFSETOF__ExInfo__m_pPrevExInfo] // x3 <- next ExInfo - cbz x3, LOCAL_LABEL(DonePopping) // if (pExInfo == null) { we're done } - cmp x3, x2 - blt LOCAL_LABEL(PopExInfoLoop) // if (pExInfo < resume SP} { keep going } - -LOCAL_LABEL(DonePopping): - str x3, [x1, #OFFSETOF__Thread__m_pExInfoStackHead] // store the new head on the Thread + LOAD_FUNCLET_REGISTERS x0, x12 // reset SP and jump to continuation address + ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] mov sp, x2 - br x0 - -#undef rsp_offset_x0 -#undef rsp_offset_x1 -#undef rsp_offset_x2 -#undef rsp_offset_x3 -#undef rsp_CatchFunclet_offset_thread - - NESTED_END RhpCallCatchFunclet, _Text - -// -// void FASTCALL RhpCallFinallyFunclet(void* pHandlerIP, REGDISPLAY* pRegDisplay) -// -// INPUT: X0: handler funclet address -// X1: REGDISPLAY* -// -// OUTPUT: -// - - NESTED_ENTRY RhpCallFinallyFunclet, _TEXT, NoHandler - - ALLOC_CALL_FUNCLET_FRAME 0x60 // Size needs to be equal with ExceptionHandling.asm variant of this function - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - stp x0, x1, [sp, #0x40] // x0 and x1 are saved so we have them later - -#define rsp_offset_x1 0x48 -#define rsp_FinallyFunclet_offset_thread 0x50 - - - // We want to suppress hijacking between invocations of subsequent finallys. We do this because we - // cannot tolerate a GC after one finally has run (and possibly side-effected the GC state of the - // method) and then been popped off the stack, leaving behind no trace of its effect. - // - // So we clear the state before and set it after invocation of the handler. - // - - // - // clear the DoNotTriggerGc flag, trashes x2-x4 - // - - bl C_FUNC(RhpGetThread) - str x0, [sp, rsp_FinallyFunclet_offset_thread] - mov x2, x0 - ldp x0, x1, [sp, #0x40] - - add x12, x2, #OFFSETOF__Thread__m_ThreadStateFlags - -LOCAL_LABEL(ClearRetry): - ldxr w4, [x12] - bic w4, w4, #TSF_DoNotTriggerGc - stxr w3, w4, [x12] - cbz w3, LOCAL_LABEL(ClearSuccess) - b LOCAL_LABEL(ClearRetry) -LOCAL_LABEL(ClearSuccess): - - // - // set preserved regs to the values expected by the funclet - // - RESTORE_PRESERVED_REGISTERS x1 - // - // trash the values at the old homes to make sure nobody uses them - // - TRASH_PRESERVED_REGISTERS_STORAGE x1 - - // - // call the funclet - // - blr x0 - -ALTERNATE_ENTRY RhpCallFinallyFunclet2 - - ldr x1, [sp, #rsp_offset_x1] // reload REGDISPLAY pointer - - // - // save new values of preserved regs into REGDISPLAY - // - SAVE_PRESERVED_REGISTERS x1 - - // - // set the DoNotTriggerGc flag, trashes x1-x3 - // - - ldr x2, [sp, rsp_FinallyFunclet_offset_thread] - - add x12, x2, #OFFSETOF__Thread__m_ThreadStateFlags -LOCAL_LABEL(SetRetry): - ldxr w1, [x12] - orr w1, w1, #TSF_DoNotTriggerGc - stxr w3, w1, [x12] - cbz w3, LOCAL_LABEL(SetSuccess) - b LOCAL_LABEL(SetRetry) -LOCAL_LABEL(SetSuccess): - - ldp d8, d9, [sp, #0x00] - ldp d10, d11, [sp, #0x10] - ldp d12, d13, [sp, #0x20] - ldp d14, d15, [sp, #0x30] - - FREE_CALL_FUNCLET_FRAME 0x60 - EPILOG_RETURN - -#undef rsp_offset_x1 -#undef rsp_FinallyFunclet_offset_thread - - NESTED_END RhpCallFinallyFunclet, _Text - - -// -// void* FASTCALL RhpCallFilterFunclet(OBJECTREF exceptionObj, void* pFilterIP, REGDISPLAY* pRegDisplay) -// -// INPUT: X0: exception object -// X1: filter funclet address -// X2: REGDISPLAY* -// -// OUTPUT: -// - - NESTED_ENTRY RhpCallFilterFunclet, _TEXT, NoHandler - ALLOC_CALL_FUNCLET_FRAME 0x40 - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - - ldr x12, [x2, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [x12] - - // - // call the funclet - // - // x0 still contains the exception object - blr x1 - -ALTERNATE_ENTRY RhpCallFilterFunclet2 - - ldp d8, d9, [sp, #0x00] - ldp d10, d11, [sp, #0x10] - ldp d12, d13, [sp, #0x20] - ldp d14, d15, [sp, #0x30] - - FREE_CALL_FUNCLET_FRAME 0x40 - EPILOG_RETURN + br x1 - NESTED_END RhpCallFilterFunclet, Text + NESTED_END RhpResumeAfterCatch, _TEXT #ifdef FEATURE_OBJCMARSHAL // // void* FASTCALL RhpCallPropagateExceptionCallback(void* pCallbackContext, void* pCallback, REGDISPLAY* pRegDisplay, -// ExInfo* pExInfo, PInvokeTransitionFrame* pPreviousTransitionFrame) +// PInvokeTransitionFrame* pPreviousTransitionFrame) +// +// Jumps to the propagation callback after the dispatcher has popped the ExInfos below the SP of the frame +// described by pRegDisplay. Does not return. // // INPUT: X0: callback context // X1: callback // X2: REGDISPLAY* -// X3: ExInfo* -// X4: pPreviousTransitionFrame -// -// OUTPUT: +// X3: pPreviousTransitionFrame // NESTED_ENTRY RhpCallPropagateExceptionCallback, _TEXT, NoHandler @@ -685,89 +312,41 @@ ALTERNATE_ENTRY RhpCallFilterFunclet2 #define rsp_offset_x1 0x18 #define rsp_offset_x2 0x20 #define rsp_offset_x3 0x28 -#define rsp_offset_x4 0x30 -#define rsp_CallPropagationCallback_offset_thread 0x38 // Using the NO_FP macro so that the debugger unwinds using SP. - // This makes backtraces work even after using RESTORE_PRESERVED_REGISTERS. - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x40 - mov fp, sp - stp x0, x1, [sp, #rsp_offset_x0] // x0 to x3 are stored to restore them anytime + // This makes backtraces work even after using LOAD_FUNCLET_REGISTERS. + PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x30 + stp x0, x1, [sp, #rsp_offset_x0] stp x2, x3, [sp, #rsp_offset_x2] - stp x4, xzr, [sp, #rsp_offset_x4] - // str xzr, [sp, #rsp_CallPropagationCallback_offset_thread] // xzr makes space to store the thread obj - - // - // clear the DoNotTriggerGc flag, trashes x4-x6 - // bl C_FUNC(RhpGetThread) - str x0, [sp, rsp_CallPropagationCallback_offset_thread] - mov x5,x0 - ldp x0, x1, [sp, #rsp_offset_x0] - ldp x2, x3, [sp, #rsp_offset_x2] + mov x5, x0 // x5 <- Thread* + // Clear the DoNotTriggerGc state before calling out to the propagation callback. add x12, x5, #OFFSETOF__Thread__m_ThreadStateFlags - LOCAL_LABEL(ClearRetry_Propagate): ldxr w4, [x12] bic w4, w4, #TSF_DoNotTriggerGc stxr w6, w4, [x12] - cbz w6, LOCAL_LABEL(ClearSuccess_Propagate) - b LOCAL_LABEL(ClearRetry_Propagate) -LOCAL_LABEL(ClearSuccess_Propagate): + cbnz w6, LOCAL_LABEL(ClearRetry_Propagate) // // set preserved regs to the values expected by the funclet // - RESTORE_PRESERVED_REGISTERS x2 - // - // trash the values at the old homes to make sure nobody uses them - // - TRASH_PRESERVED_REGISTERS_STORAGE x2 - -#ifdef _DEBUG - // Call into some C++ code to validate the pop of the ExInfo. We only do this in debug because we - // have to spill all the preserved registers and then refill them after the call. - - SAVE_PRESERVED_REGISTERS x2 - - ldr x0, [sp, rsp_CallPropagationCallback_offset_thread] // x0 <- Thread* - ldr x1, [sp, #rsp_offset_x3] // x1 <- current ExInfo* - ldr x2, [x2, #OFFSETOF__REGDISPLAY__SP] // x2 <- resume SP value - bl C_FUNC(RhpValidateExInfoPop) - - ldr x2, [sp, rsp_offset_x2] // x2 <- REGDISPLAY* - - RESTORE_PRESERVED_REGISTERS x2 -#endif - - ldr x1, [sp, rsp_CallPropagationCallback_offset_thread] + ldr x2, [sp, #rsp_offset_x2] // x2 <- REGDISPLAY* + LOAD_FUNCLET_REGISTERS x2, x12 // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. - INLINE_THREAD_UNHIJACK x1, x3, x12 // Thread in x1, trashes x3 and x12 - - ldr x3, [sp, #rsp_offset_x3] // x3 <- current ExInfo* - ldr x2, [x2, #OFFSETOF__REGDISPLAY__SP] // x2 <- resume SP value - -LOCAL_LABEL(Propagate_PopExInfoLoop): - ldr x3, [x3, #OFFSETOF__ExInfo__m_pPrevExInfo] // x3 <- next ExInfo - cbz x3, LOCAL_LABEL(Propagate_DonePopping) // if (pExInfo == null) { we're done } - cmp x3, x2 - blt LOCAL_LABEL(Propagate_PopExInfoLoop) // if (pExInfo < resume SP} { keep going } - -LOCAL_LABEL(Propagate_DonePopping): - str x3, [x1, #OFFSETOF__Thread__m_pExInfoStackHead] // store the new head on the Thread + INLINE_THREAD_UNHIJACK x5, x3, x12 // Thread in x5, trashes x3 and x12 // restore preemptive mode - ldr x4, [sp, #rsp_offset_x4] // pPreviousTransitionFrame - str x4, [x1, #OFFSETOF__Thread__m_pTransitionFrame] + ldr x4, [sp, #rsp_offset_x3] // pPreviousTransitionFrame + str x4, [x5, #OFFSETOF__Thread__m_pTransitionFrame] // reset SP and LR and jump to continuation address ldr x0, [sp, #rsp_offset_x0] // callback context ldr x1, [sp, #rsp_offset_x1] // callback - ldr x2, [sp, #rsp_offset_x2] // REGDISPLAY* ldr x3, [x2, #OFFSETOF__REGDISPLAY__pLR] // x3 <- &resume LR value ldr lr, [x3] ldr x3, [x2, #OFFSETOF__REGDISPLAY__SP] // x3 <- resume SP value @@ -778,7 +357,6 @@ LOCAL_LABEL(Propagate_DonePopping): #undef rsp_offset_x1 #undef rsp_offset_x2 #undef rsp_offset_x3 -#undef rsp_CallPropagationCallback_offset_thread NESTED_END RhpCallPropagateExceptionCallback, _Text diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm index da61aa1cfd6e71..be1af48a1314d3 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm @@ -1,7 +1,7 @@ ;; 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" +#include "AsmMacros_Shared.h" TEXTAREA @@ -42,165 +42,6 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo MEND -;; ----------------------------------------------------------------------------- -;; Macro used to create frame of funclet calling helpers (RhpCallXXXXFunclet) -;; $extraStackSize - extra stack space that the user of the macro can use to -;; store additional registers - MACRO - ALLOC_CALL_FUNCLET_FRAME $extraStackSize - - ; Using below prolog instead of PROLOG_SAVE_REG_PAIR fp,lr, #-60! - ; is intentional. Above statement would also emit instruction to save - ; sp in fp. If sp is saved in fp in prolog then it is not expected that fp can change in the body - ; of method. However, this method needs to be able to change fp before calling funclet. - ; This is required to access locals in funclet. - PROLOG_SAVE_REG_PAIR_NO_FP fp,lr, #-0x60! - PROLOG_SAVE_REG_PAIR x19, x20, #0x10 - PROLOG_SAVE_REG_PAIR x21, x22, #0x20 - PROLOG_SAVE_REG_PAIR x23, x24, #0x30 - PROLOG_SAVE_REG_PAIR x25, x26, #0x40 - PROLOG_SAVE_REG_PAIR x27, x28, #0x50 - PROLOG_NOP mov fp, sp - - IF $extraStackSize != 0 - PROLOG_STACK_ALLOC $extraStackSize - ENDIF - MEND - -;; ----------------------------------------------------------------------------- -;; Macro used to free frame of funclet calling helpers (RhpCallXXXXFunclet) -;; $extraStackSize - extra stack space that the user of the macro can use to -;; store additional registers. -;; It needs to match the value passed to the corresponding -;; ALLOC_CALL_FUNCLET_FRAME. - MACRO - FREE_CALL_FUNCLET_FRAME $extraStackSize - - IF $extraStackSize != 0 - EPILOG_STACK_FREE $extraStackSize - ENDIF - - EPILOG_RESTORE_REG_PAIR x19, x20, #0x10 - EPILOG_RESTORE_REG_PAIR x21, x22, #0x20 - EPILOG_RESTORE_REG_PAIR x23, x24, #0x30 - EPILOG_RESTORE_REG_PAIR x25, x26, #0x40 - EPILOG_RESTORE_REG_PAIR x27, x28, #0x50 - EPILOG_RESTORE_REG_PAIR fp, lr, #0x60! - MEND - -;; ----------------------------------------------------------------------------- -;; Macro used to restore preserved general purpose and FP registers from REGDISPLAY -;; $regdisplayReg - register pointing to the REGDISPLAY structure - MACRO - RESTORE_PRESERVED_REGISTERS $regdisplayReg - - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - ldr x19, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - ldr x20, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - ldr x21, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - ldr x22, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - ldr x23, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - ldr x24, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - ldr x25, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - ldr x26, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - ldr x27, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - ldr x28, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [x12] - ;; - ;; load FP preserved regs - ;; - add x12, $regdisplayReg, #OFFSETOF__REGDISPLAY__D - ldp d8, d9, [x12, #0x00] - ldp d10, d11, [x12, #0x10] - ldp d12, d13, [x12, #0x20] - ldp d14, d15, [x12, #0x30] - MEND - -;; ----------------------------------------------------------------------------- -;; Macro used to save preserved general purpose and FP registers to REGDISPLAY -;; $regdisplayReg - register pointing to the REGDISPLAY structure - MACRO - SAVE_PRESERVED_REGISTERS $regdisplayReg - - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - str x19, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - str x20, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - str x21, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - str x22, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - str x23, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - str x24, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - str x25, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - str x26, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - str x27, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - str x28, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - str fp, [x12] - ;; - ;; store vfp preserved regs - ;; - add x12, $regdisplayReg, #OFFSETOF__REGDISPLAY__D - stp d8, d9, [x12, #0x00] - stp d10, d11, [x12, #0x10] - stp d12, d13, [x12, #0x20] - stp d14, d15, [x12, #0x30] - MEND - -;; ----------------------------------------------------------------------------- -;; Macro used to thrash preserved general purpose registers in REGDISPLAY -;; to make sure nobody uses them -;; $regdisplayReg - register pointing to the REGDISPLAY structure - MACRO - TRASH_PRESERVED_REGISTERS_STORAGE $regdisplayReg - -#if 0 // def _DEBUG ;; @TODO: temporarily removed because trashing the frame pointer breaks the debugger - movz x3, #0xbaad, LSL #48 - movk x3, #0xdeed, LSL #32 - movk x3, #0xbaad, LSL #16 - movk x3, #0xdeed - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX19] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX20] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX21] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX22] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX23] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX24] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX25] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX26] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX27] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pX28] - str x3, [x12] - ldr x12, [$regdisplayReg, #OFFSETOF__REGDISPLAY__pFP] - str x3, [x12] -#endif // _DEBUG - MEND - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; RhpThrowHwEx @@ -412,221 +253,38 @@ NotHijacked ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; void* FASTCALL RhpCallCatchFunclet(OBJECTREF exceptionObj, void* pHandlerIP, REGDISPLAY* pRegDisplay, -;; ExInfo* pExInfo) +;; void FASTCALL RhpResumeAfterCatch(REGDISPLAY* pRegDisplay, void* pResumeIP) ;; -;; INPUT: X0: exception object -;; X1: handler funclet address -;; X2: REGDISPLAY* -;; X3: ExInfo* +;; Resumes execution after a catch funclet called by CallEHFunclet has returned and the dispatcher has popped +;; the ExInfos below the resume SP. Does not return. ;; -;; OUTPUT: +;; INPUT: X0: REGDISPLAY* of the frame that contains the catch +;; X1: resume address returned by the catch funclet ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - NESTED_ENTRY RhpCallCatchFunclet - - ALLOC_CALL_FUNCLET_FRAME 0x70 // Size needs to be equal with ExceptionHandling.S variant of this function - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - stp x0, x2, [sp, #0x40] ;; x0, x2 & x3 are saved so we have the exception object, REGDISPLAY and - str x3, [sp, #0x50] ;; ExInfo later - -#define rsp_offset_x2 0x48 -#define rsp_offset_x3 0x50 - - ;; - ;; clear the DoNotTriggerGc flag, trashes x4-x6 - ;; - INLINE_GETTHREAD x5, x6 ;; x5 <- Thread*, x6 <- trashed + LEAF_ENTRY RhpResumeAfterCatch - add x12, x5, #OFFSETOF__Thread__m_ThreadStateFlags + INLINE_GETTHREAD x3, x4 ;; x3 <- Thread*, x4 <- trashed -ClearRetry_Catch + ;; Clear the DoNotTriggerGc state before resuming in the managed code after the catch. + add x12, x3, #OFFSETOF__Thread__m_ThreadStateFlags +ResumeAfterCatch_ClearRetry ldxr w4, [x12] bic w4, w4, #TSF_DoNotTriggerGc - stxr w6, w4, [x12] - cbz w6, ClearSuccess_Catch - b ClearRetry_Catch -ClearSuccess_Catch - - ;; - ;; set preserved regs to the values expected by the funclet - ;; - RESTORE_PRESERVED_REGISTERS x2 - ;; - ;; trash the values at the old homes to make sure nobody uses them - ;; - TRASH_PRESERVED_REGISTERS_STORAGE x2 - - ;; - ;; call the funclet - ;; - ;; x0 still contains the exception object - blr x1 - - ALTERNATE_ENTRY RhpCallCatchFunclet2 - - ;; x0 contains resume IP - - ldr x2, [sp, #rsp_offset_x2] ;; x2 <- REGDISPLAY* - -;; @TODO: add debug-only validation code for ExInfo pop - - INLINE_GETTHREAD x1, x3 ;; x1 <- Thread*, x3 <- trashed + stxr w5, w4, [x12] + cbnz w5, ResumeAfterCatch_ClearRetry ;; We must unhijack the thread at this point because the section of stack where the hijack is applied ;; may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. - INLINE_THREAD_UNHIJACK x1, x3, x12 ;; Thread in x1, trashes x3 and x12 - - ldr x3, [sp, #rsp_offset_x3] ;; x3 <- current ExInfo* - ldr x2, [x2, #OFFSETOF__REGDISPLAY__SP] ;; x2 <- resume SP value + INLINE_THREAD_UNHIJACK x3, x4, x12 ;; Thread in x3, trashes x4 and x12 -PopExInfoLoop - ldr x3, [x3, #OFFSETOF__ExInfo__m_pPrevExInfo] ;; x3 <- next ExInfo - cbz x3, DonePopping ;; if (pExInfo == null) { we're done } - cmp x3, x2 - blt PopExInfoLoop ;; if (pExInfo < resume SP} { keep going } - -DonePopping - str x3, [x1, #OFFSETOF__Thread__m_pExInfoStackHead] ;; store the new head on the Thread + LOAD_FUNCLET_REGISTERS x0, x12 ;; reset SP and jump to continuation address + ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] mov sp, x2 - br x0 - - NESTED_END RhpCallCatchFunclet - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;; void FASTCALL RhpCallFinallyFunclet(void* pHandlerIP, REGDISPLAY* pRegDisplay) -;; -;; INPUT: X0: handler funclet address -;; X1: REGDISPLAY* -;; -;; OUTPUT: -;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - NESTED_ENTRY RhpCallFinallyFunclet - - ALLOC_CALL_FUNCLET_FRAME 0x60 // Size needs to be equal with ExceptionHandling.S variant of this function - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - stp x0, x1, [sp, #0x40] ;; x1 is saved so we have the REGDISPLAY later, x0 is just alignment padding - -#define rsp_offset_x1 0x48 - - ;; - ;; We want to suppress hijacking between invocations of subsequent finallys. We do this because we - ;; cannot tolerate a GC after one finally has run (and possibly side-effected the GC state of the - ;; method) and then been popped off the stack, leaving behind no trace of its effect. - ;; - ;; So we clear the state before and set it after invocation of the handler. - ;; - - ;; - ;; clear the DoNotTriggerGc flag, trashes x2-x4 - ;; - INLINE_GETTHREAD x2, x3 ;; x2 <- Thread*, x3 <- trashed - - add x12, x2, #OFFSETOF__Thread__m_ThreadStateFlags - -ClearRetry - ldxr w4, [x12] - bic w4, w4, #TSF_DoNotTriggerGc - stxr w3, w4, [x12] - cbz w3, ClearSuccess - b ClearRetry -ClearSuccess - - ;; - ;; set preserved regs to the values expected by the funclet - ;; - RESTORE_PRESERVED_REGISTERS x1 - ;; - ;; trash the values at the old homes to make sure nobody uses them - ;; - TRASH_PRESERVED_REGISTERS_STORAGE x1 - - ;; - ;; call the funclet - ;; - blr x0 - - ALTERNATE_ENTRY RhpCallFinallyFunclet2 - - ldr x1, [sp, #rsp_offset_x1] ;; reload REGDISPLAY pointer - - ;; - ;; save new values of preserved regs into REGDISPLAY - ;; - SAVE_PRESERVED_REGISTERS x1 - - ;; - ;; set the DoNotTriggerGc flag, trashes x1-x3 - ;; - INLINE_GETTHREAD x2, x3 ;; x2 <- Thread*, x3 <- trashed - - add x12, x2, #OFFSETOF__Thread__m_ThreadStateFlags -SetRetry - ldxr w1, [x12] - orr w1, w1, #TSF_DoNotTriggerGc - stxr w3, w1, [x12] - cbz w3, SetSuccess - b SetRetry -SetSuccess - - ldp d8, d9, [sp, #0x00] - ldp d10, d11, [sp, #0x10] - ldp d12, d13, [sp, #0x20] - ldp d14, d15, [sp, #0x30] - - FREE_CALL_FUNCLET_FRAME 0x60 - EPILOG_RETURN - - NESTED_END RhpCallFinallyFunclet - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;; void* FASTCALL RhpCallFilterFunclet(OBJECTREF exceptionObj, void* pFilterIP, REGDISPLAY* pRegDisplay) -;; -;; INPUT: X0: exception object -;; X1: filter funclet address -;; X2: REGDISPLAY* -;; -;; OUTPUT: -;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - NESTED_ENTRY RhpCallFilterFunclet - ALLOC_CALL_FUNCLET_FRAME 0x40 - stp d8, d9, [sp, #0x00] - stp d10, d11, [sp, #0x10] - stp d12, d13, [sp, #0x20] - stp d14, d15, [sp, #0x30] - - ldr x12, [x2, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [x12] - - ;; - ;; call the funclet - ;; - ;; x0 still contains the exception object - blr x1 - - ALTERNATE_ENTRY RhpCallFilterFunclet2 - - ldp d8, d9, [sp, #0x00] - ldp d10, d11, [sp, #0x10] - ldp d12, d13, [sp, #0x20] - ldp d14, d15, [sp, #0x30] - - FREE_CALL_FUNCLET_FRAME 0x40 - EPILOG_RETURN + br x1 - NESTED_END RhpCallFilterFunclet + LEAF_END RhpResumeAfterCatch end diff --git a/src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h b/src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h index 88869ab69deb13..c91949a2eb75a0 100644 --- a/src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h +++ b/src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h @@ -11,8 +11,8 @@ struct ReadyToRunHeaderConstants { static const uint32_t Signature = 0x00525452; // 'RTR' - static const uint32_t CurrentMajorVersion = 33; - static const uint32_t CurrentMinorVersion = 1; + static const uint32_t CurrentMajorVersion = 34; + static const uint32_t CurrentMinorVersion = 0; }; struct ReadyToRunHeader diff --git a/src/coreclr/runtime/arm64/EHHelpers.S b/src/coreclr/runtime/arm64/EHHelpers.S new file mode 100644 index 00000000000000..e6ffc6df002760 --- /dev/null +++ b/src/coreclr/runtime/arm64/EHHelpers.S @@ -0,0 +1,155 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "AsmMacros_Shared.h" + +// +// Prologue of the funclet calling helpers. They save all callee-saved registers since the funclets do not +// preserve them. NativeAOT's StackFrameIterator::UnwindFuncletInvokeThunk hard-codes the resulting frame layout. +// +// The helpers set fp to the frame pointer of the parent frame before calling the funclet, so the frame is +// SP based rather than using PROLOG_SAVE_REG_PAIR_INDEXED fp, lr, which would set fp up as the frame pointer. +// +#define FUNCLET_CALL_FRAME_SIZE 0xB0 +#define FUNCLET_CALL_LOCALS 0xA0 + +.macro FUNCLET_CALL_PROLOGUE + PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -FUNCLET_CALL_FRAME_SIZE + PROLOG_SAVE_REG_PAIR x19, x20, 0x10 + PROLOG_SAVE_REG_PAIR x21, x22, 0x20 + PROLOG_SAVE_REG_PAIR x23, x24, 0x30 + PROLOG_SAVE_REG_PAIR x25, x26, 0x40 + PROLOG_SAVE_REG_PAIR x27, x28, 0x50 + PROLOG_SAVE_REG_PAIR d8, d9, 0x60 + PROLOG_SAVE_REG_PAIR d10, d11, 0x70 + PROLOG_SAVE_REG_PAIR d12, d13, 0x80 + PROLOG_SAVE_REG_PAIR d14, d15, 0x90 +.endm + +// +// Epilogue of the funclet calling helpers +// +.macro FUNCLET_CALL_EPILOGUE + EPILOG_RESTORE_REG_PAIR d14, d15, 0x90 + EPILOG_RESTORE_REG_PAIR d12, d13, 0x80 + EPILOG_RESTORE_REG_PAIR d10, d11, 0x70 + EPILOG_RESTORE_REG_PAIR d8, d9, 0x60 + EPILOG_RESTORE_REG_PAIR x27, x28, 0x50 + EPILOG_RESTORE_REG_PAIR x25, x26, 0x40 + EPILOG_RESTORE_REG_PAIR x23, x24, 0x30 + EPILOG_RESTORE_REG_PAIR x21, x22, 0x20 + EPILOG_RESTORE_REG_PAIR x19, x20, 0x10 + EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, FUNCLET_CALL_FRAME_SIZE +.endm + +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// UINT_PTR CallEHFunclet(Object* pThrowable, PCODE pFunclet, void* pRegs, UINT_PTR* pFuncletCallerSP) +// +// Calls a catch or finally funclet with the preserved registers of the parent frame loaded from pRegs +// (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTERS. +// +// On NativeAOT, the funclet runs with TSF_DoNotTriggerGc cleared, and the preserved registers are written back +// through the REGDISPLAY after it returns since a GC during the funclet may have updated them. +// +// INPUT: X0: exception object (null for finally funclets) +// X1: funclet address +// X2: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT +// X3: address where the SP of this helper (the funclet's caller) is saved +// +// OUTPUT: X0: resume address for catch funclets +// +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler + + FUNCLET_CALL_PROLOGUE + +#define locThread (FUNCLET_CALL_LOCALS) +#define locRegs (FUNCLET_CALL_LOCALS + 8) + + mov x4, sp + str x4, [x3] + +#ifdef FEATURE_NATIVEAOT + mov x19, x0 + mov x20, x1 + str x2, [sp, #locRegs] + + bl C_FUNC(RhpGetThread) + str x0, [sp, #locThread] + + // Clear the DoNotTriggerGc state before calling out to the managed funclet. + add x12, x0, #OFFSETOF__Thread__m_ThreadStateFlags +LOCAL_LABEL(CallEHFunclet_ClearRetry): + ldxr w4, [x12] + bic w4, w4, #TSF_DoNotTriggerGc + stxr w5, w4, [x12] + cbnz w5, LOCAL_LABEL(CallEHFunclet_ClearRetry) + + mov x0, x19 + mov x1, x20 + ldr x2, [sp, #locRegs] +#endif + + LOAD_FUNCLET_REGISTERS x2, x12 + + blr x1 + +ALTERNATE_ENTRY CallEHFunclet2 + +#ifdef FEATURE_NATIVEAOT + ldr x2, [sp, #locRegs] + STORE_FUNCLET_REGISTERS x2, x12 + + // Suppress GC again until the dispatcher calls the next funclet or resumes after the catch. + ldr x12, [sp, #locThread] + add x12, x12, #OFFSETOF__Thread__m_ThreadStateFlags +LOCAL_LABEL(CallEHFunclet_SetRetry): + ldxr w4, [x12] + orr w4, w4, #TSF_DoNotTriggerGc + stxr w5, w4, [x12] + cbnz w5, LOCAL_LABEL(CallEHFunclet_SetRetry) +#else + nop +#endif + + FUNCLET_CALL_EPILOGUE + EPILOG_RETURN + +#undef locThread +#undef locRegs + +NESTED_END CallEHFunclet, _TEXT + +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// UINT_PTR CallEHFilterFunclet(Object* pThrowable, void* pRegs, PCODE pFilter, UINT_PTR* pFuncletCallerSP) +// +// Calls a filter funclet with the frame pointer of the parent frame loaded from pRegs (the CONTEXT on CoreCLR, +// the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_FRAME_POINTER. +// +// INPUT: X0: exception object +// X1: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT +// X2: filter funclet address +// X3: address where the SP of this helper (the funclet's caller) is saved +// +// OUTPUT: X0: result of the filter execution +// +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +NESTED_ENTRY CallEHFilterFunclet, _TEXT, NoHandler + + FUNCLET_CALL_PROLOGUE + + mov x4, sp + str x4, [x3] + + LOAD_FUNCLET_FRAME_POINTER x1, x12 + + blr x2 + +ALTERNATE_ENTRY CallEHFilterFunclet2 + + FUNCLET_CALL_EPILOGUE + EPILOG_RETURN + +NESTED_END CallEHFilterFunclet, _TEXT diff --git a/src/coreclr/runtime/arm64/EHHelpers.asm b/src/coreclr/runtime/arm64/EHHelpers.asm new file mode 100644 index 00000000000000..b35e8fa36a76bb --- /dev/null +++ b/src/coreclr/runtime/arm64/EHHelpers.asm @@ -0,0 +1,157 @@ +;; Licensed to the .NET Foundation under one or more agreements. +;; The .NET Foundation licenses this file to you under the MIT license. + +#include "AsmMacros_Shared.h" + + TEXTAREA + +;; +;; Prologue of the funclet calling helpers. They save all callee-saved registers since the funclets do not +;; preserve them. NativeAOT's StackFrameIterator::UnwindFuncletInvokeThunk hard-codes the resulting frame layout. +;; +;; The helpers set fp to the frame pointer of the parent frame before calling the funclet, so the frame is +;; SP based rather than using PROLOG_SAVE_REG_PAIR fp, lr, which would set fp up as the frame pointer. +;; +#define FUNCLET_CALL_FRAME_SIZE 0xB0 +#define FUNCLET_CALL_LOCALS 0xA0 + + MACRO + FUNCLET_CALL_PROLOGUE + + PROLOG_SAVE_REG_PAIR_NO_FP fp, lr, #-FUNCLET_CALL_FRAME_SIZE! + PROLOG_SAVE_REG_PAIR x19, x20, #0x10 + PROLOG_SAVE_REG_PAIR x21, x22, #0x20 + PROLOG_SAVE_REG_PAIR x23, x24, #0x30 + PROLOG_SAVE_REG_PAIR x25, x26, #0x40 + PROLOG_SAVE_REG_PAIR x27, x28, #0x50 + PROLOG_SAVE_REG_PAIR d8, d9, #0x60 + PROLOG_SAVE_REG_PAIR d10, d11, #0x70 + PROLOG_SAVE_REG_PAIR d12, d13, #0x80 + PROLOG_SAVE_REG_PAIR d14, d15, #0x90 + MEND + +;; +;; Epilogue of the funclet calling helpers +;; + MACRO + FUNCLET_CALL_EPILOGUE + + EPILOG_RESTORE_REG_PAIR d14, d15, #0x90 + EPILOG_RESTORE_REG_PAIR d12, d13, #0x80 + EPILOG_RESTORE_REG_PAIR d10, d11, #0x70 + EPILOG_RESTORE_REG_PAIR d8, d9, #0x60 + EPILOG_RESTORE_REG_PAIR x27, x28, #0x50 + EPILOG_RESTORE_REG_PAIR x25, x26, #0x40 + EPILOG_RESTORE_REG_PAIR x23, x24, #0x30 + EPILOG_RESTORE_REG_PAIR x21, x22, #0x20 + EPILOG_RESTORE_REG_PAIR x19, x20, #0x10 + EPILOG_RESTORE_REG_PAIR fp, lr, #FUNCLET_CALL_FRAME_SIZE! + MEND + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; UINT_PTR CallEHFunclet(Object* pThrowable, PCODE pFunclet, void* pRegs, UINT_PTR* pFuncletCallerSP) +;; +;; Calls a catch or finally funclet with the preserved registers of the parent frame loaded from pRegs +;; (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTERS. +;; +;; On NativeAOT, the funclet runs with TSF_DoNotTriggerGc cleared, and the preserved registers are written back +;; through the REGDISPLAY after it returns since a GC during the funclet may have updated them. +;; +;; INPUT: X0: exception object (null for finally funclets) +;; X1: funclet address +;; X2: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT +;; X3: address where the SP of this helper (the funclet's caller) is saved +;; +;; OUTPUT: X0: resume address for catch funclets +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + NESTED_ENTRY CallEHFunclet + + FUNCLET_CALL_PROLOGUE + +#define locThread (FUNCLET_CALL_LOCALS) +#define locRegs (FUNCLET_CALL_LOCALS + 8) + + mov x4, sp + str x4, [x3] + +#ifdef FEATURE_NATIVEAOT + str x2, [sp, #locRegs] + + INLINE_GETTHREAD x4, x5 + str x4, [sp, #locThread] + + ;; Clear the DoNotTriggerGc state before calling out to the managed funclet. + add x12, x4, #OFFSETOF__Thread__m_ThreadStateFlags +CallEHFunclet_ClearRetry + ldxr w4, [x12] + bic w4, w4, #TSF_DoNotTriggerGc + stxr w5, w4, [x12] + cbnz w5, CallEHFunclet_ClearRetry +#endif + + LOAD_FUNCLET_REGISTERS x2, x12 + + blr x1 + + ALTERNATE_ENTRY CallEHFunclet2 + +#ifdef FEATURE_NATIVEAOT + ldr x2, [sp, #locRegs] + STORE_FUNCLET_REGISTERS x2, x12 + + ;; Suppress GC again until the dispatcher calls the next funclet or resumes after the catch. + ldr x12, [sp, #locThread] + add x12, x12, #OFFSETOF__Thread__m_ThreadStateFlags +CallEHFunclet_SetRetry + ldxr w4, [x12] + orr w4, w4, #TSF_DoNotTriggerGc + stxr w5, w4, [x12] + cbnz w5, CallEHFunclet_SetRetry +#else + nop +#endif + + FUNCLET_CALL_EPILOGUE + EPILOG_RETURN + +#undef locThread +#undef locRegs + + NESTED_END CallEHFunclet + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; UINT_PTR CallEHFilterFunclet(Object* pThrowable, void* pRegs, PCODE pFilter, UINT_PTR* pFuncletCallerSP) +;; +;; Calls a filter funclet with the frame pointer of the parent frame loaded from pRegs (the CONTEXT on CoreCLR, +;; the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_FRAME_POINTER. +;; +;; INPUT: X0: exception object +;; X1: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT +;; X2: filter funclet address +;; X3: address where the SP of this helper (the funclet's caller) is saved +;; +;; OUTPUT: X0: result of the filter execution +;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + NESTED_ENTRY CallEHFilterFunclet + + FUNCLET_CALL_PROLOGUE + + mov x4, sp + str x4, [x3] + + LOAD_FUNCLET_FRAME_POINTER x1, x12 + + blr x2 + + ALTERNATE_ENTRY CallEHFilterFunclet2 + + FUNCLET_CALL_EPILOGUE + EPILOG_RETURN + + NESTED_END CallEHFilterFunclet + + END diff --git a/src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs b/src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs index af7028f1b668d2..8f5b952c3a6cdd 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs @@ -15,8 +15,8 @@ internal struct ReadyToRunHeaderConstants { public const uint Signature = 0x00525452; // 'RTR' - public const ushort CurrentMajorVersion = 33; - public const ushort CurrentMinorVersion = 1; + public const ushort CurrentMajorVersion = 34; + public const ushort CurrentMinorVersion = 0; } #if READYTORUN #pragma warning disable 0169 diff --git a/src/coreclr/vm/CMakeLists.txt b/src/coreclr/vm/CMakeLists.txt index 2f10791908bd9e..2af36249a3763f 100644 --- a/src/coreclr/vm/CMakeLists.txt +++ b/src/coreclr/vm/CMakeLists.txt @@ -737,6 +737,7 @@ elseif(CLR_CMAKE_TARGET_WIN32) ${ARCH_SOURCES_DIR}/CachedInterfaceDispatchCoreCLR.asm ${ARCH_SOURCES_DIR}/StubPrecodeDynamicHelpers.asm ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/AllocFast.asm + ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/EHHelpers.asm ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/MiscStubs.asm ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/WriteBarriers.asm ) @@ -798,6 +799,7 @@ else() ${ARCH_SOURCES_DIR}/thunktemplates.S ${ARCH_SOURCES_DIR}/StubPrecodeDynamicHelpers.S ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/AllocFast.S + ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/EHHelpers.S ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/MiscStubs.S ${RUNTIME_DIR}/${ARCH_SOURCES_DIR}/WriteBarriers.S ) diff --git a/src/coreclr/vm/arm64/AsmMacros_Shared.h b/src/coreclr/vm/arm64/AsmMacros_Shared.h index 3092d518e55daf..85225d98e097c6 100644 --- a/src/coreclr/vm/arm64/AsmMacros_Shared.h +++ b/src/coreclr/vm/arm64/AsmMacros_Shared.h @@ -38,6 +38,24 @@ br x9 MEND +;; CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT + MACRO + LOAD_FUNCLET_REGISTERS $regs, $scratch + + ldp x19, x20, [$regs, #OFFSETOF__CONTEXT__X19] + ldp x21, x22, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x10)] + ldp x23, x24, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x20)] + ldp x25, x26, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x30)] + ldp x27, x28, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x40)] + ldr fp, [$regs, #OFFSETOF__CONTEXT__Fp] + MEND + + MACRO + LOAD_FUNCLET_FRAME_POINTER $regs, $scratch + + ldr fp, [$regs, #OFFSETOF__CONTEXT__Fp] + MEND + #else #include "asmconstants.h" #include "unixasmmacros.inc" @@ -47,5 +65,19 @@ br x9 .endm +// CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT +.macro LOAD_FUNCLET_REGISTERS regs, scratch + ldp x19, x20, [\regs, #OFFSETOF__CONTEXT__X19] + ldp x21, x22, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x10)] + ldp x23, x24, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x20)] + ldp x25, x26, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x30)] + ldp x27, x28, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x40)] + ldr fp, [\regs, #OFFSETOF__CONTEXT__Fp] +.endm + +.macro LOAD_FUNCLET_FRAME_POINTER regs, scratch + ldr fp, [\regs, #OFFSETOF__CONTEXT__Fp] +.endm + #endif diff --git a/src/coreclr/vm/arm64/asmhelpers.S b/src/coreclr/vm/arm64/asmhelpers.S index 17c91730f2f0aa..7de0c500db17ac 100644 --- a/src/coreclr/vm/arm64/asmhelpers.S +++ b/src/coreclr/vm/arm64/asmhelpers.S @@ -172,82 +172,6 @@ GenerateRedirectedHandledJITCaseStub GCStress #endif -// ------------------------------------------------------------------ - -// This helper enables us to call into a funclet after restoring Fp register -NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler - // On entry: - // - // X0 = throwable - // X1 = PC to invoke - // X2 = address of CONTEXT record; used to restore the non-volatile registers of CrawlFrame - // X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved. - // - - // Using below prolog instead of PROLOG_SAVE_REG_PAIR_INDEXED fp,lr, -96 - // is intentional. Above statement would also emit instruction to save - // sp in fp. If sp is saved in fp in prolog then it is not expected that fp can change in the body - // of method. However, this method needs to be able to change fp before calling funclet. - // This is required to access locals in funclet. - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED x29, lr, -96 - - // 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 - - // Save the SP of this function - mov x4, sp - str x4, [x3] - - ldp x19, x20, [x2, #OFFSETOF__CONTEXT__X19] - ldp x21, x22, [x2, #(OFFSETOF__CONTEXT__X19 + 16)] - ldp x23, x24, [x2, #(OFFSETOF__CONTEXT__X19 + 32)] - ldp x25, x26, [x2, #(OFFSETOF__CONTEXT__X19 + 48)] - ldp x27, x28, [x2, #(OFFSETOF__CONTEXT__X19 + 64)] - ldr fp, [x2, #OFFSETOF__CONTEXT__Fp] - - // Invoke the funclet - blr x1 - nop - - 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, 96 - EPILOG_RETURN - -NESTED_END CallEHFunclet, _TEXT - -// This helper enables us to call into a filter funclet after restoring Fp register -NESTED_ENTRY CallEHFilterFunclet, _TEXT, NoHandler - - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -16 - - // On entry: - // - // X0 = throwable - // X1 = FP of main function - // X2 = PC to invoke - // X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved. - // - // Save the SP of this function - mov fp, sp - str fp, [x3] - // Restore frame pointer - mov fp, x1 - // Invoke the filter funclet - blr x2 - - EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 16 - EPILOG_RETURN - -NESTED_END CallEHFilterFunclet, _TEXT - #define FaultingExceptionFrame_StackAlloc (SIZEOF__FaultingExceptionFrame) #define FaultingExceptionFrame_FrameOffset 0 diff --git a/src/coreclr/vm/arm64/asmhelpers.asm b/src/coreclr/vm/arm64/asmhelpers.asm index cf2dc0057fe961..d6ea87b3b597b0 100644 --- a/src/coreclr/vm/arm64/asmhelpers.asm +++ b/src/coreclr/vm/arm64/asmhelpers.asm @@ -271,83 +271,6 @@ OnHijackTripThreadReturn #endif -; ------------------------------------------------------------------ - - ; This helper enables us to call into a funclet after restoring Fp register - NESTED_ENTRY CallEHFunclet - ; On entry: - ; - ; X0 = throwable - ; X1 = PC to invoke - ; X2 = address of CONTEXT record; used to restore the non-volatile registers of CrawlFrame - ; X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved. - ; - - ; Using below prolog instead of PROLOG_SAVE_REG_PAIR fp,lr, #-96! - ; is intentional. Above statement would also emit instruction to save - ; sp in fp. If sp is saved in fp in prolog then it is not expected that fp can change in the body - ; of method. However, this method needs to be able to change fp before calling funclet. - ; This is required to access locals in funclet. - PROLOG_SAVE_REG_PAIR_NO_FP fp,lr, #-96! - - ; 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 - - ; Save the SP of this function. We cannot store SP directly. - mov fp, sp - str fp, [x3] - - ldp x19, x20, [x2, #OFFSETOF__CONTEXT__X19] - ldp x21, x22, [x2, #(OFFSETOF__CONTEXT__X19 + 16)] - ldp x23, x24, [x2, #(OFFSETOF__CONTEXT__X19 + 32)] - ldp x25, x26, [x2, #(OFFSETOF__CONTEXT__X19 + 48)] - ldp x27, x28, [x2, #(OFFSETOF__CONTEXT__X19 + 64)] - ldr fp, [x2, #OFFSETOF__CONTEXT__Fp] - - ; Invoke the funclet - blr x1 - nop - - 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, #96! - EPILOG_RETURN - - NESTED_END CallEHFunclet - - ; This helper enables us to call into a filter funclet after restoring Fp register - NESTED_ENTRY CallEHFilterFunclet - - PROLOG_SAVE_REG_PAIR_NO_FP fp, lr, #-16! - - ; On entry: - ; - ; X0 = throwable - ; X1 = FP of the main function - ; X2 = PC to invoke - ; X3 = address of the location where the SP of funclet's caller (i.e. this helper) should be saved. - ; - ; Save the SP of this function - mov fp, sp - str fp, [x3] - ; Restore frame pointer - mov fp, x1 - ; Invoke the filter funclet - blr x2 - - EPILOG_RESTORE_REG_PAIR fp, lr, #16! - EPILOG_RETURN - - NESTED_END CallEHFilterFunclet - - GBLA FaultingExceptionFrame_StackAlloc GBLA FaultingExceptionFrame_FrameOffset diff --git a/src/coreclr/vm/eetwain.cpp b/src/coreclr/vm/eetwain.cpp index c72a68bd96f1c4..528aab834d2306 100644 --- a/src/coreclr/vm/eetwain.cpp +++ b/src/coreclr/vm/eetwain.cpp @@ -1729,7 +1729,7 @@ unsigned int EECodeManager::GetFrameSize(GCInfoToken gcInfoToken) EXTERN_C DWORD_PTR STDCALL 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. -#ifdef TARGET_AMD64 +#if defined(TARGET_AMD64) || defined(TARGET_ARM64) EXTERN_C DWORD_PTR STDCALL CallEHFilterFunclet(Object *pThrowable, CONTEXT *pContext, UINT_PTR pFuncletToInvoke, UINT_PTR *pFuncletCallerSP); #else EXTERN_C DWORD_PTR STDCALL CallEHFilterFunclet(Object *pThrowable, TADDR FP, UINT_PTR pFuncletToInvoke, UINT_PTR *pFuncletCallerSP); @@ -1787,7 +1787,7 @@ DWORD_PTR EECodeManager::CallFunclet(OBJECTREF throwable, void* pHandler, REGDIS // it will retrieve the framepointer for accessing the locals in the parent // method. dwResult = CallEHFilterFunclet(OBJECTREFToObject(throwable), -#ifdef TARGET_AMD64 +#if defined(TARGET_AMD64) || defined(TARGET_ARM64) pRD->pCurrentContext, #else GetFP(pRD->pCurrentContext), From a2843e0e17f0d296c59e3e8b74c9c8c24954a0e1 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:39:21 +0200 Subject: [PATCH 2/6] Save only LR in arm64 funclets and drop redundant ObjC propagation ifdefs Co-authored-by: Cursor --- src/coreclr/inc/readytorun.h | 4 ++-- src/coreclr/jit/codegen.h | 2 +- src/coreclr/jit/codegenarm64.cpp | 24 +++++++++---------- .../src/System/Runtime/ExceptionHandling.cs | 5 ---- .../src/System/Runtime/InternalCalls.cs | 5 ---- 5 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/coreclr/inc/readytorun.h b/src/coreclr/inc/readytorun.h index f0f5f4ed0fcb7d..24d007524d163e 100644 --- a/src/coreclr/inc/readytorun.h +++ b/src/coreclr/inc/readytorun.h @@ -86,8 +86,8 @@ // GCRefMaps, moves VASIG_COOKIE from 5 to 7, and prefixes map-bearing import // signatures with their GCRefMaps. // R2R Version 33.1 adds READYTORUN_HELPER_WasmProfSamplepoint (used on WebAssembly) -// R2R Version 34 changes the ABI for funclets on ARM64 to match AMD64: funclets save only FP and LR, and the -// runtime preserves the other callee-saved registers around the funclets that it calls +// R2R Version 34 changes the ABI for funclets on ARM64 to match AMD64: funclets save only LR, and the +// runtime preserves the callee-saved registers around the funclets that it calls struct READYTORUN_CORE_HEADER { diff --git a/src/coreclr/jit/codegen.h b/src/coreclr/jit/codegen.h index 09caec895eaaf7..4d7712ddd366f8 100644 --- a/src/coreclr/jit/codegen.h +++ b/src/coreclr/jit/codegen.h @@ -521,7 +521,7 @@ class CodeGen final : public CodeGenInterface // same. struct FuncletFrameInfoDsc { - unsigned fiOutgoingArgSpace; // Outgoing argument space allocated below the saved FP/LR, aligned to 16 + unsigned fiOutgoingArgSpace; // Outgoing argument space allocated below the saved LR, aligned to 16 }; FuncletFrameInfoDsc genFuncletInfo; diff --git a/src/coreclr/jit/codegenarm64.cpp b/src/coreclr/jit/codegenarm64.cpp index dd42d1f67c11de..27e54c0e4c075b 100644 --- a/src/coreclr/jit/codegenarm64.cpp +++ b/src/coreclr/jit/codegenarm64.cpp @@ -1162,14 +1162,14 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * * The runtime calls funclets with FP set to the FP of the parent function, and the funclet does not change it. * Funclets access the locals of the parent function relative to FP. Funclets do not save the callee-saved - * registers other than FP and LR: the runtime preserves them around funclets that it calls, and the main function - * saves the ones used by the funclets in its own prolog. Call-to-finally sites do not keep any values in registers - * across the call. All temps are allocated in the parent frame, so the funclet frame only needs to hold FP/LR and - * the outgoing argument space. + * registers: the runtime preserves them around funclets that it calls, and the main function saves the ones used + * by the funclets in its own prolog. Call-to-finally sites do not keep any values in registers across the call. + * All temps are allocated in the parent frame, so the funclet frame only needs to hold LR and the outgoing + * argument space. * * The ARM64 funclet prolog sequence is (Note: #outsz is the outgoing argument space, aligned to 16): * - * stp fp,lr,[sp,#-16]! ; save FP/LR + * str lr,[sp,#-16]! ; save LR * sub sp,sp,#outsz ; create space for outgoing argument space, if any * * The funclet frame is thus: @@ -1178,7 +1178,7 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * |-----------------------| * | incoming arguments | * +=======================+ <---- Caller's SP - * | Saved FP, LR | // 16 bytes + * | Saved LR, padding | // 16 bytes * |-----------------------| * | Outgoing arg space | // multiple of 16 bytes (optional; if #outsz > 0) * |-----------------------| <---- Ambient SP @@ -1190,7 +1190,7 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * The epilog sequence is then: * * add sp,sp,#outsz ; if any outgoing argument space - * ldp fp,lr,[sp],#16 + * ldr lr,[sp],#16 * ret lr * * See CodeGen::genPushCalleeSavedRegisters() for a description of the main function frame layout. @@ -1217,9 +1217,8 @@ void CodeGen::genFuncletProlog(BasicBlock* block) GetEmitter()->emitPacInProlog(); } - GetEmitter()->emitIns_R_R_R_I(INS_stp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, -2 * REGSIZE_BYTES, - INS_OPTS_PRE_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, -2 * REGSIZE_BYTES); + GetEmitter()->emitIns_R_R_I(INS_str, EA_PTRSIZE, REG_LR, REG_SPBASE, -STACK_ALIGN, INS_OPTS_PRE_INDEX); + m_compiler->unwindSaveRegPreindexed(REG_LR, -STACK_ALIGN); if (genFuncletInfo.fiOutgoingArgSpace > 0) { @@ -1253,9 +1252,8 @@ void CodeGen::genFuncletEpilog(BasicBlock* /* block */) /* reportUnwindData */ true); } - GetEmitter()->emitIns_R_R_R_I(INS_ldp, EA_PTRSIZE, REG_FP, REG_LR, REG_SPBASE, 2 * REGSIZE_BYTES, - INS_OPTS_POST_INDEX); - m_compiler->unwindSaveRegPairPreindexed(REG_FP, REG_LR, -2 * REGSIZE_BYTES); + GetEmitter()->emitIns_R_R_I(INS_ldr, EA_PTRSIZE, REG_LR, REG_SPBASE, STACK_ALIGN, INS_OPTS_POST_INDEX); + m_compiler->unwindSaveRegPreindexed(REG_LR, -STACK_ALIGN); if (JitConfig.JitPacEnabled() != 0) { diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs index e69124c5f8fdb3..1db2472f3649c3 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs @@ -942,14 +942,9 @@ private static void DispatchEx(scoped ref StackFrameIterator frameIter, ref ExIn #if FEATURE_OBJCMARSHAL if (pReversePInvokePropagationCallback != IntPtr.Zero) { -#if TARGET_AMD64 || TARGET_ARM64 InternalCalls.RhpPopExInfo(ref exInfo, frameIter.SP); InternalCalls.RhpCallPropagateExceptionCallback( pReversePInvokePropagationContext, pReversePInvokePropagationCallback, frameIter.RegisterSet, frameIter.PreviousTransitionFrame); -#else - InternalCalls.RhpCallPropagateExceptionCallback( - pReversePInvokePropagationContext, pReversePInvokePropagationCallback, frameIter.RegisterSet, ref exInfo, frameIter.PreviousTransitionFrame); -#endif // the helper should jump to propagation handler and not return Debug.Fail("unreachable"); FallbackFailFast(RhFailFastReason.InternalError, null); diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs index 9b04b58d568263..f2c0da386005c0 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs @@ -181,13 +181,8 @@ internal static extern unsafe bool RhpCallFilterFunclet( #if FEATURE_OBJCMARSHAL [RuntimeImport(RuntimeLibrary, "RhpCallPropagateExceptionCallback")] [MethodImpl(MethodImplOptions.InternalCall)] -#if TARGET_AMD64 || TARGET_ARM64 internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( IntPtr callbackContext, IntPtr callback, void* pvRegDisplay, IntPtr pPreviousTransitionFrame); -#else - internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( - IntPtr callbackContext, IntPtr callback, void* pvRegDisplay, ref EH.ExInfo exInfo, IntPtr pPreviousTransitionFrame); -#endif #endif // FEATURE_OBJCMARSHAL [RuntimeImport(RuntimeLibrary, "RhpFallbackFailFast")] From a82448461d8e3f1dde66604e66f0b4783f1e6ddc Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:50:17 +0200 Subject: [PATCH 3/6] Use per-register funclet register macros on arm64 Co-authored-by: Cursor --- src/coreclr/jit/codegenarm64.cpp | 2 +- .../Runtime/arm64/AsmMacros_Shared.h | 158 ++++-------------- .../Runtime/arm64/ExceptionHandling.S | 31 +++- .../Runtime/arm64/ExceptionHandling.asm | 29 +++- src/coreclr/runtime/arm64/EHHelpers.S | 49 +++++- src/coreclr/runtime/arm64/EHHelpers.asm | 49 +++++- src/coreclr/vm/arm64/AsmMacros_Shared.h | 34 ++-- src/coreclr/vm/arm64/asmconstants.h | 3 + 8 files changed, 193 insertions(+), 162 deletions(-) diff --git a/src/coreclr/jit/codegenarm64.cpp b/src/coreclr/jit/codegenarm64.cpp index 27e54c0e4c075b..ce80c656310e58 100644 --- a/src/coreclr/jit/codegenarm64.cpp +++ b/src/coreclr/jit/codegenarm64.cpp @@ -1167,7 +1167,7 @@ void CodeGen::genRestoreCalleeSavedRegistersHelp(regMaskTP regsToRestoreMask, in * All temps are allocated in the parent frame, so the funclet frame only needs to hold LR and the outgoing * argument space. * - * The ARM64 funclet prolog sequence is (Note: #outsz is the outgoing argument space, aligned to 16): + * The funclet prolog sequence is (Note: #outsz is the outgoing argument space, aligned to 16): * * str lr,[sp,#-16]! ; save LR * sub sp,sp,#outsz ; create space for outgoing argument space, if any diff --git a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h index f7b3ccfe94eff1..314737f8a80f6c 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h +++ b/src/coreclr/nativeaot/Runtime/arm64/AsmMacros_Shared.h @@ -16,78 +16,32 @@ b RhpGcPollRare MEND -;; CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY +;; CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY. +;; $reg is the register number: 19-28 for x19-x28, which are followed by fp (29) in the REGDISPLAY. MACRO - LOAD_FUNCLET_REGISTERS $regs, $scratch - - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX19] - ldr x19, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX20] - ldr x20, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX21] - ldr x21, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX22] - ldr x22, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX23] - ldr x23, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX24] - ldr x24, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX25] - ldr x25, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX26] - ldr x26, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX27] - ldr x27, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX28] - ldr x28, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [$scratch] - - add $scratch, $regs, #OFFSETOF__REGDISPLAY__D - ldp d8, d9, [$scratch, #0x00] - ldp d10, d11, [$scratch, #0x10] - ldp d12, d13, [$scratch, #0x20] - ldp d14, d15, [$scratch, #0x30] + LOAD_FUNCLET_REGISTER $base, $reg, $scratch + + ldr $scratch, [$base, #(OFFSETOF__REGDISPLAY__pX19 + ($reg - 19) * 8)] + ldr x$reg, [$scratch] + MEND + + MACRO + STORE_FUNCLET_REGISTER $base, $reg, $scratch + + ldr $scratch, [$base, #(OFFSETOF__REGDISPLAY__pX19 + ($reg - 19) * 8)] + str x$reg, [$scratch] MEND MACRO - STORE_FUNCLET_REGISTERS $regs, $scratch - - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX19] - str x19, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX20] - str x20, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX21] - str x21, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX22] - str x22, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX23] - str x23, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX24] - str x24, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX25] - str x25, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX26] - str x26, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX27] - str x27, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pX28] - str x28, [$scratch] - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] - str fp, [$scratch] - - add $scratch, $regs, #OFFSETOF__REGDISPLAY__D - stp d8, d9, [$scratch, #0x00] - stp d10, d11, [$scratch, #0x10] - stp d12, d13, [$scratch, #0x20] - stp d14, d15, [$scratch, #0x30] + LOAD_FUNCLET_FP_REGISTER $base, $index + + ldr d$index, [$base, #(OFFSETOF__REGDISPLAY__D + ($index - 8) * 8)] MEND MACRO - LOAD_FUNCLET_FRAME_POINTER $regs, $scratch + STORE_FUNCLET_FP_REGISTER $base, $index - ldr $scratch, [$regs, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [$scratch] + str d$index, [$base, #(OFFSETOF__REGDISPLAY__D + ($index - 8) * 8)] MEND #else @@ -98,71 +52,23 @@ b C_FUNC(RhpGcPollRare) .endm -// CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY -.macro LOAD_FUNCLET_REGISTERS regs, scratch - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX19] - ldr x19, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX20] - ldr x20, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX21] - ldr x21, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX22] - ldr x22, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX23] - ldr x23, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX24] - ldr x24, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX25] - ldr x25, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX26] - ldr x26, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX27] - ldr x27, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX28] - ldr x28, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [\scratch] - - add \scratch, \regs, #OFFSETOF__REGDISPLAY__D - ldp d8, d9, [\scratch, #0x00] - ldp d10, d11, [\scratch, #0x10] - ldp d12, d13, [\scratch, #0x20] - ldp d14, d15, [\scratch, #0x30] +// CallEHFunclet loads and writes back the preserved registers of the parent frame through the REGDISPLAY. +// reg is the register number: 19-28 for x19-x28, which are followed by fp (29) in the REGDISPLAY. +.macro LOAD_FUNCLET_REGISTER base, reg, scratch + ldr \scratch, [\base, #(OFFSETOF__REGDISPLAY__pX19 + (\reg - 19) * 8)] + ldr x\reg, [\scratch] +.endm + +.macro STORE_FUNCLET_REGISTER base, reg, scratch + ldr \scratch, [\base, #(OFFSETOF__REGDISPLAY__pX19 + (\reg - 19) * 8)] + str x\reg, [\scratch] .endm -.macro STORE_FUNCLET_REGISTERS regs, scratch - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX19] - str x19, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX20] - str x20, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX21] - str x21, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX22] - str x22, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX23] - str x23, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX24] - str x24, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX25] - str x25, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX26] - str x26, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX27] - str x27, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pX28] - str x28, [\scratch] - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] - str fp, [\scratch] - - add \scratch, \regs, #OFFSETOF__REGDISPLAY__D - stp d8, d9, [\scratch, #0x00] - stp d10, d11, [\scratch, #0x10] - stp d12, d13, [\scratch, #0x20] - stp d14, d15, [\scratch, #0x30] +.macro LOAD_FUNCLET_FP_REGISTER base, index + ldr d\index, [\base, #(OFFSETOF__REGDISPLAY__D + (\index - 8) * 8)] .endm -.macro LOAD_FUNCLET_FRAME_POINTER regs, scratch - ldr \scratch, [\regs, #OFFSETOF__REGDISPLAY__pFP] - ldr fp, [\scratch] +.macro STORE_FUNCLET_FP_REGISTER base, index + str d\index, [\base, #(OFFSETOF__REGDISPLAY__D + (\index - 8) * 8)] .endm #endif diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index 4ec53f68a294d8..2f6bc1e0f279e4 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -42,6 +42,31 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo .endm +// ----------------------------------------------------------------------------- +// Macro used to restore preserved general purpose and FP registers from REGDISPLAY +// regdisplayReg - register pointing to the REGDISPLAY structure +.macro RESTORE_PRESERVED_REGISTERS regdisplayReg + LOAD_FUNCLET_REGISTER \regdisplayReg, 19, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 20, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 21, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 22, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 23, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 24, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 25, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 26, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 27, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 28, x12 + LOAD_FUNCLET_REGISTER \regdisplayReg, 29, x12 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 8 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 9 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 10 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 11 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 12 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 13 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 14 + LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 15 +.endm + .macro GetThreadX2 stp x0, x1, [sp, -16]! bl C_FUNC(RhpGetThread) @@ -282,7 +307,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): ldp x0, x1, [sp, #0x10] // x0 <- REGDISPLAY*, x1 <- resume IP - LOAD_FUNCLET_REGISTERS x0, x12 + RESTORE_PRESERVED_REGISTERS x0 // reset SP and jump to continuation address ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] @@ -314,7 +339,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): #define rsp_offset_x3 0x28 // Using the NO_FP macro so that the debugger unwinds using SP. - // This makes backtraces work even after using LOAD_FUNCLET_REGISTERS. + // This makes backtraces work even after using RESTORE_PRESERVED_REGISTERS. PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x30 stp x0, x1, [sp, #rsp_offset_x0] stp x2, x3, [sp, #rsp_offset_x2] @@ -334,7 +359,7 @@ LOCAL_LABEL(ClearRetry_Propagate): // set preserved regs to the values expected by the funclet // ldr x2, [sp, #rsp_offset_x2] // x2 <- REGDISPLAY* - LOAD_FUNCLET_REGISTERS x2, x12 + RESTORE_PRESERVED_REGISTERS x2 // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm index be1af48a1314d3..4a153ab1db65ab 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm @@ -42,6 +42,33 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo MEND +;; ----------------------------------------------------------------------------- +;; Macro used to restore preserved general purpose and FP registers from REGDISPLAY +;; $regdisplayReg - register pointing to the REGDISPLAY structure + MACRO + RESTORE_PRESERVED_REGISTERS $regdisplayReg + + LOAD_FUNCLET_REGISTER $regdisplayReg, 19, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 20, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 21, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 22, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 23, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 24, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 25, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 26, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 27, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 28, x12 + LOAD_FUNCLET_REGISTER $regdisplayReg, 29, x12 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 8 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 9 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 10 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 11 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 12 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 13 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 14 + LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 15 + MEND + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; RhpThrowHwEx @@ -278,7 +305,7 @@ ResumeAfterCatch_ClearRetry ;; may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. INLINE_THREAD_UNHIJACK x3, x4, x12 ;; Thread in x3, trashes x4 and x12 - LOAD_FUNCLET_REGISTERS x0, x12 + RESTORE_PRESERVED_REGISTERS x0 ;; reset SP and jump to continuation address ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] diff --git a/src/coreclr/runtime/arm64/EHHelpers.S b/src/coreclr/runtime/arm64/EHHelpers.S index e6ffc6df002760..7dc6b7815284f8 100644 --- a/src/coreclr/runtime/arm64/EHHelpers.S +++ b/src/coreclr/runtime/arm64/EHHelpers.S @@ -47,7 +47,8 @@ // UINT_PTR CallEHFunclet(Object* pThrowable, PCODE pFunclet, void* pRegs, UINT_PTR* pFuncletCallerSP) // // Calls a catch or finally funclet with the preserved registers of the parent frame loaded from pRegs -// (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTERS. +// (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTER +// and LOAD_FUNCLET_FP_REGISTER. // // On NativeAOT, the funclet runs with TSF_DoNotTriggerGc cleared, and the preserved registers are written back // through the REGDISPLAY after it returns since a GC during the funclet may have updated them. @@ -91,7 +92,26 @@ LOCAL_LABEL(CallEHFunclet_ClearRetry): ldr x2, [sp, #locRegs] #endif - LOAD_FUNCLET_REGISTERS x2, x12 + LOAD_FUNCLET_REGISTER x2, 19, x12 + LOAD_FUNCLET_REGISTER x2, 20, x12 + LOAD_FUNCLET_REGISTER x2, 21, x12 + LOAD_FUNCLET_REGISTER x2, 22, x12 + LOAD_FUNCLET_REGISTER x2, 23, x12 + LOAD_FUNCLET_REGISTER x2, 24, x12 + LOAD_FUNCLET_REGISTER x2, 25, x12 + LOAD_FUNCLET_REGISTER x2, 26, x12 + LOAD_FUNCLET_REGISTER x2, 27, x12 + LOAD_FUNCLET_REGISTER x2, 28, x12 + LOAD_FUNCLET_REGISTER x2, 29, x12 + + LOAD_FUNCLET_FP_REGISTER x2, 8 + LOAD_FUNCLET_FP_REGISTER x2, 9 + LOAD_FUNCLET_FP_REGISTER x2, 10 + LOAD_FUNCLET_FP_REGISTER x2, 11 + LOAD_FUNCLET_FP_REGISTER x2, 12 + LOAD_FUNCLET_FP_REGISTER x2, 13 + LOAD_FUNCLET_FP_REGISTER x2, 14 + LOAD_FUNCLET_FP_REGISTER x2, 15 blr x1 @@ -99,7 +119,26 @@ ALTERNATE_ENTRY CallEHFunclet2 #ifdef FEATURE_NATIVEAOT ldr x2, [sp, #locRegs] - STORE_FUNCLET_REGISTERS x2, x12 + STORE_FUNCLET_REGISTER x2, 19, x12 + STORE_FUNCLET_REGISTER x2, 20, x12 + STORE_FUNCLET_REGISTER x2, 21, x12 + STORE_FUNCLET_REGISTER x2, 22, x12 + STORE_FUNCLET_REGISTER x2, 23, x12 + STORE_FUNCLET_REGISTER x2, 24, x12 + STORE_FUNCLET_REGISTER x2, 25, x12 + STORE_FUNCLET_REGISTER x2, 26, x12 + STORE_FUNCLET_REGISTER x2, 27, x12 + STORE_FUNCLET_REGISTER x2, 28, x12 + STORE_FUNCLET_REGISTER x2, 29, x12 + + STORE_FUNCLET_FP_REGISTER x2, 8 + STORE_FUNCLET_FP_REGISTER x2, 9 + STORE_FUNCLET_FP_REGISTER x2, 10 + STORE_FUNCLET_FP_REGISTER x2, 11 + STORE_FUNCLET_FP_REGISTER x2, 12 + STORE_FUNCLET_FP_REGISTER x2, 13 + STORE_FUNCLET_FP_REGISTER x2, 14 + STORE_FUNCLET_FP_REGISTER x2, 15 // Suppress GC again until the dispatcher calls the next funclet or resumes after the catch. ldr x12, [sp, #locThread] @@ -126,7 +165,7 @@ NESTED_END CallEHFunclet, _TEXT // UINT_PTR CallEHFilterFunclet(Object* pThrowable, void* pRegs, PCODE pFilter, UINT_PTR* pFuncletCallerSP) // // Calls a filter funclet with the frame pointer of the parent frame loaded from pRegs (the CONTEXT on CoreCLR, -// the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_FRAME_POINTER. +// the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTER. // // INPUT: X0: exception object // X1: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT @@ -143,7 +182,7 @@ NESTED_ENTRY CallEHFilterFunclet, _TEXT, NoHandler mov x4, sp str x4, [x3] - LOAD_FUNCLET_FRAME_POINTER x1, x12 + LOAD_FUNCLET_REGISTER x1, 29, x12 blr x2 diff --git a/src/coreclr/runtime/arm64/EHHelpers.asm b/src/coreclr/runtime/arm64/EHHelpers.asm index b35e8fa36a76bb..285be7cc2c961f 100644 --- a/src/coreclr/runtime/arm64/EHHelpers.asm +++ b/src/coreclr/runtime/arm64/EHHelpers.asm @@ -53,7 +53,8 @@ ;; UINT_PTR CallEHFunclet(Object* pThrowable, PCODE pFunclet, void* pRegs, UINT_PTR* pFuncletCallerSP) ;; ;; Calls a catch or finally funclet with the preserved registers of the parent frame loaded from pRegs -;; (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTERS. +;; (the CONTEXT on CoreCLR, the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTER +;; and LOAD_FUNCLET_FP_REGISTER. ;; ;; On NativeAOT, the funclet runs with TSF_DoNotTriggerGc cleared, and the preserved registers are written back ;; through the REGDISPLAY after it returns since a GC during the funclet may have updated them. @@ -91,7 +92,26 @@ CallEHFunclet_ClearRetry cbnz w5, CallEHFunclet_ClearRetry #endif - LOAD_FUNCLET_REGISTERS x2, x12 + LOAD_FUNCLET_REGISTER x2, 19, x12 + LOAD_FUNCLET_REGISTER x2, 20, x12 + LOAD_FUNCLET_REGISTER x2, 21, x12 + LOAD_FUNCLET_REGISTER x2, 22, x12 + LOAD_FUNCLET_REGISTER x2, 23, x12 + LOAD_FUNCLET_REGISTER x2, 24, x12 + LOAD_FUNCLET_REGISTER x2, 25, x12 + LOAD_FUNCLET_REGISTER x2, 26, x12 + LOAD_FUNCLET_REGISTER x2, 27, x12 + LOAD_FUNCLET_REGISTER x2, 28, x12 + LOAD_FUNCLET_REGISTER x2, 29, x12 + + LOAD_FUNCLET_FP_REGISTER x2, 8 + LOAD_FUNCLET_FP_REGISTER x2, 9 + LOAD_FUNCLET_FP_REGISTER x2, 10 + LOAD_FUNCLET_FP_REGISTER x2, 11 + LOAD_FUNCLET_FP_REGISTER x2, 12 + LOAD_FUNCLET_FP_REGISTER x2, 13 + LOAD_FUNCLET_FP_REGISTER x2, 14 + LOAD_FUNCLET_FP_REGISTER x2, 15 blr x1 @@ -99,7 +119,26 @@ CallEHFunclet_ClearRetry #ifdef FEATURE_NATIVEAOT ldr x2, [sp, #locRegs] - STORE_FUNCLET_REGISTERS x2, x12 + STORE_FUNCLET_REGISTER x2, 19, x12 + STORE_FUNCLET_REGISTER x2, 20, x12 + STORE_FUNCLET_REGISTER x2, 21, x12 + STORE_FUNCLET_REGISTER x2, 22, x12 + STORE_FUNCLET_REGISTER x2, 23, x12 + STORE_FUNCLET_REGISTER x2, 24, x12 + STORE_FUNCLET_REGISTER x2, 25, x12 + STORE_FUNCLET_REGISTER x2, 26, x12 + STORE_FUNCLET_REGISTER x2, 27, x12 + STORE_FUNCLET_REGISTER x2, 28, x12 + STORE_FUNCLET_REGISTER x2, 29, x12 + + STORE_FUNCLET_FP_REGISTER x2, 8 + STORE_FUNCLET_FP_REGISTER x2, 9 + STORE_FUNCLET_FP_REGISTER x2, 10 + STORE_FUNCLET_FP_REGISTER x2, 11 + STORE_FUNCLET_FP_REGISTER x2, 12 + STORE_FUNCLET_FP_REGISTER x2, 13 + STORE_FUNCLET_FP_REGISTER x2, 14 + STORE_FUNCLET_FP_REGISTER x2, 15 ;; Suppress GC again until the dispatcher calls the next funclet or resumes after the catch. ldr x12, [sp, #locThread] @@ -126,7 +165,7 @@ CallEHFunclet_SetRetry ;; UINT_PTR CallEHFilterFunclet(Object* pThrowable, void* pRegs, PCODE pFilter, UINT_PTR* pFuncletCallerSP) ;; ;; Calls a filter funclet with the frame pointer of the parent frame loaded from pRegs (the CONTEXT on CoreCLR, -;; the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_FRAME_POINTER. +;; the REGDISPLAY on NativeAOT) by LOAD_FUNCLET_REGISTER. ;; ;; INPUT: X0: exception object ;; X1: CONTEXT* on CoreCLR, REGDISPLAY* on NativeAOT @@ -143,7 +182,7 @@ CallEHFunclet_SetRetry mov x4, sp str x4, [x3] - LOAD_FUNCLET_FRAME_POINTER x1, x12 + LOAD_FUNCLET_REGISTER x1, 29, x12 blr x2 diff --git a/src/coreclr/vm/arm64/AsmMacros_Shared.h b/src/coreclr/vm/arm64/AsmMacros_Shared.h index 85225d98e097c6..e27c34d5099c77 100644 --- a/src/coreclr/vm/arm64/AsmMacros_Shared.h +++ b/src/coreclr/vm/arm64/AsmMacros_Shared.h @@ -38,22 +38,18 @@ br x9 MEND -;; CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT +;; CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT. +;; $reg is the register number: 19-28 for x19-x28, which are followed by fp (29) in the CONTEXT. MACRO - LOAD_FUNCLET_REGISTERS $regs, $scratch - - ldp x19, x20, [$regs, #OFFSETOF__CONTEXT__X19] - ldp x21, x22, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x10)] - ldp x23, x24, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x20)] - ldp x25, x26, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x30)] - ldp x27, x28, [$regs, #(OFFSETOF__CONTEXT__X19 + 0x40)] - ldr fp, [$regs, #OFFSETOF__CONTEXT__Fp] + LOAD_FUNCLET_REGISTER $base, $reg, $scratch + + ldr x$reg, [$base, #(OFFSETOF__CONTEXT__X19 + ($reg - 19) * 8)] MEND MACRO - LOAD_FUNCLET_FRAME_POINTER $regs, $scratch + LOAD_FUNCLET_FP_REGISTER $base, $index - ldr fp, [$regs, #OFFSETOF__CONTEXT__Fp] + ldr d$index, [$base, #(OFFSETOF__CONTEXT__V + $index * 16)] MEND #else @@ -65,18 +61,14 @@ br x9 .endm -// CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT -.macro LOAD_FUNCLET_REGISTERS regs, scratch - ldp x19, x20, [\regs, #OFFSETOF__CONTEXT__X19] - ldp x21, x22, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x10)] - ldp x23, x24, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x20)] - ldp x25, x26, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x30)] - ldp x27, x28, [\regs, #(OFFSETOF__CONTEXT__X19 + 0x40)] - ldr fp, [\regs, #OFFSETOF__CONTEXT__Fp] +// CallEHFunclet and CallEHFilterFunclet load the preserved registers of the parent frame from the CONTEXT. +// reg is the register number: 19-28 for x19-x28, which are followed by fp (29) in the CONTEXT. +.macro LOAD_FUNCLET_REGISTER base, reg, scratch + ldr x\reg, [\base, #(OFFSETOF__CONTEXT__X19 + (\reg - 19) * 8)] .endm -.macro LOAD_FUNCLET_FRAME_POINTER regs, scratch - ldr fp, [\regs, #OFFSETOF__CONTEXT__Fp] +.macro LOAD_FUNCLET_FP_REGISTER base, index + ldr d\index, [\base, #(OFFSETOF__CONTEXT__V + \index * 16)] .endm #endif diff --git a/src/coreclr/vm/arm64/asmconstants.h b/src/coreclr/vm/arm64/asmconstants.h index 639b537109b164..058fb75a755a71 100644 --- a/src/coreclr/vm/arm64/asmconstants.h +++ b/src/coreclr/vm/arm64/asmconstants.h @@ -91,6 +91,9 @@ ASMCONSTANTS_C_ASSERT(OFFSETOF__CONTEXT__X19 == offsetof(T_CONTEXT, X19)); #define OFFSETOF__CONTEXT__Fp 0xF0 ASMCONSTANTS_C_ASSERT(OFFSETOF__CONTEXT__Fp == offsetof(T_CONTEXT, Fp)); +#define OFFSETOF__CONTEXT__V 0x110 +ASMCONSTANTS_C_ASSERT(OFFSETOF__CONTEXT__V == offsetof(T_CONTEXT, V)); + #define OFFSETOF__DynamicHelperStubArgs__Constant1 0x0 ASMCONSTANTS_C_ASSERT(OFFSETOF__DynamicHelperStubArgs__Constant1 == offsetof(DynamicHelperStubArgs, Constant1)); From 1ae29e2b5b395b77b474c8ea448a4524e4102cbe Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:25:01 +0200 Subject: [PATCH 4/6] Inline the preserved register loads in arm64 NativeAOT helpers like on amd64 Co-authored-by: Cursor --- .../Runtime/arm64/ExceptionHandling.S | 69 +++++++++++-------- .../Runtime/arm64/ExceptionHandling.asm | 48 ++++++------- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index 2f6bc1e0f279e4..25396b40de3f70 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -42,31 +42,6 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo .endm -// ----------------------------------------------------------------------------- -// Macro used to restore preserved general purpose and FP registers from REGDISPLAY -// regdisplayReg - register pointing to the REGDISPLAY structure -.macro RESTORE_PRESERVED_REGISTERS regdisplayReg - LOAD_FUNCLET_REGISTER \regdisplayReg, 19, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 20, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 21, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 22, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 23, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 24, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 25, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 26, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 27, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 28, x12 - LOAD_FUNCLET_REGISTER \regdisplayReg, 29, x12 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 8 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 9 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 10 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 11 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 12 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 13 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 14 - LOAD_FUNCLET_FP_REGISTER \regdisplayReg, 15 -.endm - .macro GetThreadX2 stp x0, x1, [sp, -16]! bl C_FUNC(RhpGetThread) @@ -307,7 +282,26 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): ldp x0, x1, [sp, #0x10] // x0 <- REGDISPLAY*, x1 <- resume IP - RESTORE_PRESERVED_REGISTERS x0 + LOAD_FUNCLET_REGISTER x0, 19, x12 + LOAD_FUNCLET_REGISTER x0, 20, x12 + LOAD_FUNCLET_REGISTER x0, 21, x12 + LOAD_FUNCLET_REGISTER x0, 22, x12 + LOAD_FUNCLET_REGISTER x0, 23, x12 + LOAD_FUNCLET_REGISTER x0, 24, x12 + LOAD_FUNCLET_REGISTER x0, 25, x12 + LOAD_FUNCLET_REGISTER x0, 26, x12 + LOAD_FUNCLET_REGISTER x0, 27, x12 + LOAD_FUNCLET_REGISTER x0, 28, x12 + LOAD_FUNCLET_REGISTER x0, 29, x12 + + LOAD_FUNCLET_FP_REGISTER x0, 8 + LOAD_FUNCLET_FP_REGISTER x0, 9 + LOAD_FUNCLET_FP_REGISTER x0, 10 + LOAD_FUNCLET_FP_REGISTER x0, 11 + LOAD_FUNCLET_FP_REGISTER x0, 12 + LOAD_FUNCLET_FP_REGISTER x0, 13 + LOAD_FUNCLET_FP_REGISTER x0, 14 + LOAD_FUNCLET_FP_REGISTER x0, 15 // reset SP and jump to continuation address ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] @@ -339,7 +333,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): #define rsp_offset_x3 0x28 // Using the NO_FP macro so that the debugger unwinds using SP. - // This makes backtraces work even after using RESTORE_PRESERVED_REGISTERS. + // This makes backtraces work even after loading the preserved registers. PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x30 stp x0, x1, [sp, #rsp_offset_x0] stp x2, x3, [sp, #rsp_offset_x2] @@ -359,7 +353,26 @@ LOCAL_LABEL(ClearRetry_Propagate): // set preserved regs to the values expected by the funclet // ldr x2, [sp, #rsp_offset_x2] // x2 <- REGDISPLAY* - RESTORE_PRESERVED_REGISTERS x2 + LOAD_FUNCLET_REGISTER x2, 19, x12 + LOAD_FUNCLET_REGISTER x2, 20, x12 + LOAD_FUNCLET_REGISTER x2, 21, x12 + LOAD_FUNCLET_REGISTER x2, 22, x12 + LOAD_FUNCLET_REGISTER x2, 23, x12 + LOAD_FUNCLET_REGISTER x2, 24, x12 + LOAD_FUNCLET_REGISTER x2, 25, x12 + LOAD_FUNCLET_REGISTER x2, 26, x12 + LOAD_FUNCLET_REGISTER x2, 27, x12 + LOAD_FUNCLET_REGISTER x2, 28, x12 + LOAD_FUNCLET_REGISTER x2, 29, x12 + + LOAD_FUNCLET_FP_REGISTER x2, 8 + LOAD_FUNCLET_FP_REGISTER x2, 9 + LOAD_FUNCLET_FP_REGISTER x2, 10 + LOAD_FUNCLET_FP_REGISTER x2, 11 + LOAD_FUNCLET_FP_REGISTER x2, 12 + LOAD_FUNCLET_FP_REGISTER x2, 13 + LOAD_FUNCLET_FP_REGISTER x2, 14 + LOAD_FUNCLET_FP_REGISTER x2, 15 // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm index 4a153ab1db65ab..4ad95c9a0ab1e8 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.asm @@ -42,33 +42,6 @@ PROLOG_STACK_ALLOC STACKSIZEOF_ExInfo MEND -;; ----------------------------------------------------------------------------- -;; Macro used to restore preserved general purpose and FP registers from REGDISPLAY -;; $regdisplayReg - register pointing to the REGDISPLAY structure - MACRO - RESTORE_PRESERVED_REGISTERS $regdisplayReg - - LOAD_FUNCLET_REGISTER $regdisplayReg, 19, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 20, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 21, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 22, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 23, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 24, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 25, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 26, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 27, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 28, x12 - LOAD_FUNCLET_REGISTER $regdisplayReg, 29, x12 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 8 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 9 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 10 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 11 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 12 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 13 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 14 - LOAD_FUNCLET_FP_REGISTER $regdisplayReg, 15 - MEND - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; RhpThrowHwEx @@ -305,7 +278,26 @@ ResumeAfterCatch_ClearRetry ;; may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. INLINE_THREAD_UNHIJACK x3, x4, x12 ;; Thread in x3, trashes x4 and x12 - RESTORE_PRESERVED_REGISTERS x0 + LOAD_FUNCLET_REGISTER x0, 19, x12 + LOAD_FUNCLET_REGISTER x0, 20, x12 + LOAD_FUNCLET_REGISTER x0, 21, x12 + LOAD_FUNCLET_REGISTER x0, 22, x12 + LOAD_FUNCLET_REGISTER x0, 23, x12 + LOAD_FUNCLET_REGISTER x0, 24, x12 + LOAD_FUNCLET_REGISTER x0, 25, x12 + LOAD_FUNCLET_REGISTER x0, 26, x12 + LOAD_FUNCLET_REGISTER x0, 27, x12 + LOAD_FUNCLET_REGISTER x0, 28, x12 + LOAD_FUNCLET_REGISTER x0, 29, x12 + + LOAD_FUNCLET_FP_REGISTER x0, 8 + LOAD_FUNCLET_FP_REGISTER x0, 9 + LOAD_FUNCLET_FP_REGISTER x0, 10 + LOAD_FUNCLET_FP_REGISTER x0, 11 + LOAD_FUNCLET_FP_REGISTER x0, 12 + LOAD_FUNCLET_FP_REGISTER x0, 13 + LOAD_FUNCLET_FP_REGISTER x0, 14 + LOAD_FUNCLET_FP_REGISTER x0, 15 ;; reset SP and jump to continuation address ldr x2, [x0, #OFFSETOF__REGDISPLAY__SP] From 2cf904dfd11a65b4d09d1e70de8a0764c0f09c7f Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:45:58 +0200 Subject: [PATCH 5/6] Use the funclet register macros in amd64 NativeAOT helpers and INLINE_GETTHREAD on arm64 Co-authored-by: Cursor --- .../Runtime/amd64/ExceptionHandling.S | 39 +++++---------- .../Runtime/amd64/ExceptionHandling.asm | 48 ++++++++----------- .../Runtime/arm64/ExceptionHandling.S | 15 ++---- src/coreclr/runtime/arm64/EHHelpers.S | 12 ++--- 4 files changed, 40 insertions(+), 74 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S index 13aacf59842701..15f447235382b9 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S @@ -2,8 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. .intel_syntax noprefix -#include // generated by the build from AsmOffsets.cpp -#include +#include "AsmMacros_Shared.h" ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -322,18 +321,12 @@ NESTED_ENTRY RhpResumeAfterCatch, _TEXT, NoHandler mov rax, r13 // rax <- resume IP mov rcx, rbx // rcx <- REGDISPLAY* - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pRbx] - mov rbx, [rsi] - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pRbp] - mov rbp, [rsi] - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pR12] - mov r12, [rsi] - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pR13] - mov r13, [rsi] - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pR14] - mov r14, [rsi] - mov rsi, [rcx + OFFSETOF__REGDISPLAY__pR15] - mov r15, [rsi] + LOAD_FUNCLET_REGISTER(rcx, Rbx, rsi) + LOAD_FUNCLET_REGISTER(rcx, Rbp, rsi) + LOAD_FUNCLET_REGISTER(rcx, R12, rsi) + LOAD_FUNCLET_REGISTER(rcx, R13, rsi) + LOAD_FUNCLET_REGISTER(rcx, R14, rsi) + LOAD_FUNCLET_REGISTER(rcx, R15, rsi) // reset RSP and jump to the continuation address mov rsp, rdx // reset the SP @@ -383,18 +376,12 @@ NESTED_ENTRY RhpCallPropagateExceptionCallback, _TEXT, NoHandler // Clear the DoNotTriggerGc state before calling out to the propagation callback. lock and dword ptr [rax + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc - mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbx] - mov rbx, [rax] - mov rax, [rdx + OFFSETOF__REGDISPLAY__pRbp] - mov rbp, [rax] - mov rax, [rdx + OFFSETOF__REGDISPLAY__pR12] - mov r12, [rax] - mov rax, [rdx + OFFSETOF__REGDISPLAY__pR13] - mov r13, [rax] - mov rax, [rdx + OFFSETOF__REGDISPLAY__pR14] - mov r14, [rax] - mov rax, [rdx + OFFSETOF__REGDISPLAY__pR15] - mov r15, [rax] + LOAD_FUNCLET_REGISTER(rdx, Rbx, rax) + LOAD_FUNCLET_REGISTER(rdx, Rbp, rax) + LOAD_FUNCLET_REGISTER(rdx, R12, rax) + LOAD_FUNCLET_REGISTER(rdx, R13, rax) + LOAD_FUNCLET_REGISTER(rdx, R14, rax) + LOAD_FUNCLET_REGISTER(rdx, R15, rax) mov rsi, [rsp + locThread] // rsi <- Thread* diff --git a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm index 6d4fe5714c8aa8..59d1f8b7b1c8f4 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm +++ b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm @@ -1,7 +1,7 @@ ;; 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 +include AsmMacros_Shared.inc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; @@ -371,33 +371,25 @@ NESTED_ENTRY RhpResumeAfterCatch, _TEXT mov rdx, rbx ;; rdx <- REGDISPLAY* mov r11, [rdx + OFFSETOF__REGDISPLAY__SSP] ;; r11 <- resume SSP value - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pRbx] - mov rbx, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pRbp] - mov rbp, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pRsi] - mov rsi, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pRdi] - mov rdi, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pR12] - mov r12, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pR13] - mov r13, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pR14] - mov r14, [rcx] - mov rcx, [rdx + OFFSETOF__REGDISPLAY__pR15] - mov r15, [rcx] - - movdqa xmm6, [rdx + OFFSETOF__REGDISPLAY__Xmm + 0*10h] - movdqa xmm7, [rdx + OFFSETOF__REGDISPLAY__Xmm + 1*10h] - movdqa xmm8, [rdx + OFFSETOF__REGDISPLAY__Xmm + 2*10h] - movdqa xmm9, [rdx + OFFSETOF__REGDISPLAY__Xmm + 3*10h] - movdqa xmm10,[rdx + OFFSETOF__REGDISPLAY__Xmm + 4*10h] - movdqa xmm11,[rdx + OFFSETOF__REGDISPLAY__Xmm + 5*10h] - movdqa xmm12,[rdx + OFFSETOF__REGDISPLAY__Xmm + 6*10h] - movdqa xmm13,[rdx + OFFSETOF__REGDISPLAY__Xmm + 7*10h] - movdqa xmm14,[rdx + OFFSETOF__REGDISPLAY__Xmm + 8*10h] - movdqa xmm15,[rdx + OFFSETOF__REGDISPLAY__Xmm + 9*10h] + LOAD_FUNCLET_REGISTER rdx, Rbx, rcx + LOAD_FUNCLET_REGISTER rdx, Rbp, rcx + LOAD_FUNCLET_REGISTER rdx, Rsi, rcx + LOAD_FUNCLET_REGISTER rdx, Rdi, rcx + LOAD_FUNCLET_REGISTER rdx, R12, rcx + LOAD_FUNCLET_REGISTER rdx, R13, rcx + LOAD_FUNCLET_REGISTER rdx, R14, rcx + LOAD_FUNCLET_REGISTER rdx, R15, rcx + + LOAD_FUNCLET_XMM_REGISTER rdx, 6 + LOAD_FUNCLET_XMM_REGISTER rdx, 7 + LOAD_FUNCLET_XMM_REGISTER rdx, 8 + LOAD_FUNCLET_XMM_REGISTER rdx, 9 + LOAD_FUNCLET_XMM_REGISTER rdx, 10 + LOAD_FUNCLET_XMM_REGISTER rdx, 11 + LOAD_FUNCLET_XMM_REGISTER rdx, 12 + LOAD_FUNCLET_XMM_REGISTER rdx, 13 + LOAD_FUNCLET_XMM_REGISTER rdx, 14 + LOAD_FUNCLET_XMM_REGISTER rdx, 15 ;; reset RSP and jump to RAX mov rsp, r8 ;; reset the SP to resume SP value diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index 25396b40de3f70..950cbca566c85d 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -260,13 +260,9 @@ LOCAL_LABEL(NotHijacked): // X1: resume address returned by the catch funclet // - NESTED_ENTRY RhpResumeAfterCatch, _TEXT, NoHandler + LEAF_ENTRY RhpResumeAfterCatch, _TEXT - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x20 - stp x0, x1, [sp, #0x10] - - bl C_FUNC(RhpGetThread) - mov x3, x0 // x3 <- Thread* + INLINE_GETTHREAD x3 // x3 <- Thread* // Clear the DoNotTriggerGc state before resuming in the managed code after the catch. add x12, x3, #OFFSETOF__Thread__m_ThreadStateFlags @@ -280,8 +276,6 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. INLINE_THREAD_UNHIJACK x3, x4, x12 // Thread in x3, trashes x4 and x12 - ldp x0, x1, [sp, #0x10] // x0 <- REGDISPLAY*, x1 <- resume IP - LOAD_FUNCLET_REGISTER x0, 19, x12 LOAD_FUNCLET_REGISTER x0, 20, x12 LOAD_FUNCLET_REGISTER x0, 21, x12 @@ -308,7 +302,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): mov sp, x2 br x1 - NESTED_END RhpResumeAfterCatch, _TEXT + LEAF_END RhpResumeAfterCatch, _TEXT #ifdef FEATURE_OBJCMARSHAL @@ -338,8 +332,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): stp x0, x1, [sp, #rsp_offset_x0] stp x2, x3, [sp, #rsp_offset_x2] - bl C_FUNC(RhpGetThread) - mov x5, x0 // x5 <- Thread* + INLINE_GETTHREAD x5 // x5 <- Thread* // Clear the DoNotTriggerGc state before calling out to the propagation callback. add x12, x5, #OFFSETOF__Thread__m_ThreadStateFlags diff --git a/src/coreclr/runtime/arm64/EHHelpers.S b/src/coreclr/runtime/arm64/EHHelpers.S index 7dc6b7815284f8..5840fa01c0265d 100644 --- a/src/coreclr/runtime/arm64/EHHelpers.S +++ b/src/coreclr/runtime/arm64/EHHelpers.S @@ -72,24 +72,18 @@ NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler str x4, [x3] #ifdef FEATURE_NATIVEAOT - mov x19, x0 - mov x20, x1 str x2, [sp, #locRegs] - bl C_FUNC(RhpGetThread) - str x0, [sp, #locThread] + INLINE_GETTHREAD x4 + str x4, [sp, #locThread] // Clear the DoNotTriggerGc state before calling out to the managed funclet. - add x12, x0, #OFFSETOF__Thread__m_ThreadStateFlags + add x12, x4, #OFFSETOF__Thread__m_ThreadStateFlags LOCAL_LABEL(CallEHFunclet_ClearRetry): ldxr w4, [x12] bic w4, w4, #TSF_DoNotTriggerGc stxr w5, w4, [x12] cbnz w5, LOCAL_LABEL(CallEHFunclet_ClearRetry) - - mov x0, x19 - mov x1, x20 - ldr x2, [sp, #locRegs] #endif LOAD_FUNCLET_REGISTER x2, 19, x12 From 5f9e610f1a0f115af3b6ab93fb075ee8ea83e710 Mon Sep 17 00:00:00 2001 From: am11 <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:47:30 +0200 Subject: [PATCH 6/6] Match RhpCallPropagateExceptionCallback to RhpResumeAfterCatch on amd64 and arm64 Co-authored-by: Cursor --- .../Runtime/amd64/ExceptionHandling.S | 88 ++++++------------- .../Runtime/arm64/ExceptionHandling.S | 47 +++------- 2 files changed, 37 insertions(+), 98 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S index 15f447235382b9..5166d17801e32a 100644 --- a/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S @@ -254,39 +254,6 @@ ALTERNATE_ENTRY RhpRethrow2 NESTED_END RhpRethrow, _TEXT -// -// Prologue of all funclet calling helpers (RhpCallXXXXFunclet) -// -.macro FUNCLET_CALL_PROLOGUE localsCount, alignStack - - push_nonvol_reg r15 // save preserved regs for OS stackwalker - push_nonvol_reg r14 // ... - push_nonvol_reg r13 // ... - push_nonvol_reg r12 // ... - push_nonvol_reg rbx // ... - push_nonvol_reg rbp // ... - - stack_alloc_size = \localsCount * 8 + \alignStack * 8 - - alloc_stack stack_alloc_size - - END_PROLOGUE -.endm - -// -// Epilogue of all funclet calling helpers (RhpCallXXXXFunclet) -// -.macro FUNCLET_CALL_EPILOGUE - free_stack stack_alloc_size - - pop_nonvol_reg rbp - pop_nonvol_reg rbx - pop_nonvol_reg r12 - pop_nonvol_reg r13 - pop_nonvol_reg r14 - pop_nonvol_reg r15 -.endm - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // void FASTCALL RhpResumeAfterCatch(REGDISPLAY* pRegDisplay, void* pResumeIP) @@ -355,47 +322,42 @@ NESTED_END RhpResumeAfterCatch, _TEXT ////////////////////////////////////////////////////////////////////////////////////////////////////////////// NESTED_ENTRY RhpCallPropagateExceptionCallback, _TEXT, NoHandler - // We could use a simpler prolog, as we don't have to be unwindable by StackFrameIterator::UnwindFuncletInvokeThunk() - FUNCLET_CALL_PROLOGUE 4, 1 - - locThread = 0 - locArg0 = 0x08 - locArg1 = 0x10 - locArg3 = 0x18 + alloc_stack 8 // align the stack for the calls below + END_PROLOGUE - mov [rsp + locArg0], rdi // save arguments for later - mov [rsp + locArg1], rsi - mov [rsp + locArg3], rcx + // The preserved registers are reloaded from the REGDISPLAY before calling the callback, so they can be used here. + mov rbx, rdi // rbx <- callback context + mov r12, rsi // r12 <- callback + mov r13, rdx // r13 <- REGDISPLAY* + mov r14, rcx // r14 <- pPreviousTransitionFrame - mov rbx, rdx INLINE_GETTHREAD - mov rdx, rbx - - mov [rsp + locThread], rax // save Thread* for later + mov r15, rax // r15 <- Thread* // Clear the DoNotTriggerGc state before calling out to the propagation callback. - lock and dword ptr [rax + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc - - LOAD_FUNCLET_REGISTER(rdx, Rbx, rax) - LOAD_FUNCLET_REGISTER(rdx, Rbp, rax) - LOAD_FUNCLET_REGISTER(rdx, R12, rax) - LOAD_FUNCLET_REGISTER(rdx, R13, rax) - LOAD_FUNCLET_REGISTER(rdx, R14, rax) - LOAD_FUNCLET_REGISTER(rdx, R15, rax) - - mov rsi, [rsp + locThread] // rsi <- Thread* + lock and dword ptr [r15 + OFFSETOF__Thread__m_ThreadStateFlags], ~TSF_DoNotTriggerGc // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. - INLINE_THREAD_UNHIJACK rsi, rdi, rcx // Thread in rsi, trashes rdi and rcx + INLINE_THREAD_UNHIJACK r15, rdi, rcx // Thread in r15, trashes rdi and rcx + + mov rdi, rbx // rdi <- callback context + mov rax, r12 // rax <- callback + mov rdx, r13 // rdx <- REGDISPLAY* + mov r8, r14 // r8 <- pPreviousTransitionFrame + mov r9, r15 // r9 <- Thread* + + LOAD_FUNCLET_REGISTER(rdx, Rbx, rcx) + LOAD_FUNCLET_REGISTER(rdx, Rbp, rcx) + LOAD_FUNCLET_REGISTER(rdx, R12, rcx) + LOAD_FUNCLET_REGISTER(rdx, R13, rcx) + LOAD_FUNCLET_REGISTER(rdx, R14, rcx) + LOAD_FUNCLET_REGISTER(rdx, R15, rcx) // Switch to preemptive mode. - mov r8, [rsp + locArg3] - mov [rsi + OFFSETOF__Thread__m_pTransitionFrame], r8 + mov [r9 + OFFSETOF__Thread__m_pTransitionFrame], r8 - // reset RSP and RDI and jump to the propagation callback - mov rdi, [rsp + locArg0] // rdi <- callback context - mov rax, [rsp + locArg1] // rax <- callback + // reset RSP and jump to the propagation callback mov rsp, [rdx + OFFSETOF__REGDISPLAY__SP] // reset the SP // Don't restore the stack pointer to exact same context. Leave the // return IP on the stack to let the unwinder work if the callback throws diff --git a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S index 950cbca566c85d..df630947b12338 100644 --- a/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S +++ b/src/coreclr/nativeaot/Runtime/arm64/ExceptionHandling.S @@ -319,18 +319,7 @@ LOCAL_LABEL(ResumeAfterCatch_ClearRetry): // X3: pPreviousTransitionFrame // - NESTED_ENTRY RhpCallPropagateExceptionCallback, _TEXT, NoHandler - -#define rsp_offset_x0 0x10 -#define rsp_offset_x1 0x18 -#define rsp_offset_x2 0x20 -#define rsp_offset_x3 0x28 - - // Using the NO_FP macro so that the debugger unwinds using SP. - // This makes backtraces work even after loading the preserved registers. - PROLOG_SAVE_REG_PAIR_NO_FP_INDEXED fp, lr, -0x30 - stp x0, x1, [sp, #rsp_offset_x0] - stp x2, x3, [sp, #rsp_offset_x2] + LEAF_ENTRY RhpCallPropagateExceptionCallback, _TEXT INLINE_GETTHREAD x5 // x5 <- Thread* @@ -342,10 +331,6 @@ LOCAL_LABEL(ClearRetry_Propagate): stxr w6, w4, [x12] cbnz w6, LOCAL_LABEL(ClearRetry_Propagate) - // - // set preserved regs to the values expected by the funclet - // - ldr x2, [sp, #rsp_offset_x2] // x2 <- REGDISPLAY* LOAD_FUNCLET_REGISTER x2, 19, x12 LOAD_FUNCLET_REGISTER x2, 20, x12 LOAD_FUNCLET_REGISTER x2, 21, x12 @@ -369,26 +354,18 @@ LOCAL_LABEL(ClearRetry_Propagate): // We must unhijack the thread at this point because the section of stack where the hijack is applied // may go dead. If it does, then the next time we try to unhijack the thread, it will corrupt the stack. - INLINE_THREAD_UNHIJACK x5, x3, x12 // Thread in x5, trashes x3 and x12 - - // restore preemptive mode - ldr x4, [sp, #rsp_offset_x3] // pPreviousTransitionFrame - str x4, [x5, #OFFSETOF__Thread__m_pTransitionFrame] - - // reset SP and LR and jump to continuation address - ldr x0, [sp, #rsp_offset_x0] // callback context - ldr x1, [sp, #rsp_offset_x1] // callback - ldr x3, [x2, #OFFSETOF__REGDISPLAY__pLR] // x3 <- &resume LR value - ldr lr, [x3] - ldr x3, [x2, #OFFSETOF__REGDISPLAY__SP] // x3 <- resume SP value - mov sp, x3 - br x1 + INLINE_THREAD_UNHIJACK x5, x4, x12 // Thread in x5, trashes x4 and x12 -#undef rsp_offset_x0 -#undef rsp_offset_x1 -#undef rsp_offset_x2 -#undef rsp_offset_x3 + // Switch to preemptive mode. + str x3, [x5, #OFFSETOF__Thread__m_pTransitionFrame] + + // reset SP and LR and jump to the propagation callback + ldr x4, [x2, #OFFSETOF__REGDISPLAY__pLR] // x4 <- &resume LR value + ldr lr, [x4] + ldr x4, [x2, #OFFSETOF__REGDISPLAY__SP] // x4 <- resume SP value + mov sp, x4 + br x1 - NESTED_END RhpCallPropagateExceptionCallback, _Text + LEAF_END RhpCallPropagateExceptionCallback, _TEXT #endif // FEATURE_OBJCMARSHAL