From 0c6b84889ef4dc71806e9e34b2b61cc72338bb66 Mon Sep 17 00:00:00 2001 From: Justin Miller Date: Sat, 7 Dec 2024 19:27:10 -0800 Subject: [PATCH] weird jank go brr --- hal/hal.spec | 2 +- hal/halx86/apic/apicsmp.c | 2 +- hal/halx86/generic/up.c | 5 ++--- hal/halx86/smp/ipi.c | 8 +++++--- ntoskrnl/ke/i386/ipi.c | 20 ++++++++++++++------ ntoskrnl/ke/i386/traphdlr.c | 13 ++++++++++++- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/hal/hal.spec b/hal/hal.spec index 2a3285e1c81b8..583ea7632134f 100644 --- a/hal/hal.spec +++ b/hal/hal.spec @@ -41,7 +41,7 @@ @ stdcall HalRequestIpi(long) @ fastcall HalRequestSoftwareInterrupt(long) @ stdcall HalReturnToFirmware(long) -@ stdcall -arch=x86_64 HalSendNMI(int64) +@ stdcall HalSendNMI(long) @ stdcall -arch=x86_64 HalSendSoftwareInterrupt(int64 long) @ stdcall HalSetBusData(long long long ptr long) @ stdcall HalSetBusDataByOffset(long long long ptr long long) diff --git a/hal/halx86/apic/apicsmp.c b/hal/halx86/apic/apicsmp.c index 80ba649a86a43..d95730af61c8e 100644 --- a/hal/halx86/apic/apicsmp.c +++ b/hal/halx86/apic/apicsmp.c @@ -261,6 +261,7 @@ HalpSendSoftwareInterrupt( /* Request the IPI with the specified vector */ HalRequestIpiSpecifyVector(TargetSet, Vector); } +#endif // _M_AMD64 /*! * \brief Requests an NMI interrupt on the specified processors. @@ -298,4 +299,3 @@ HalpSendNMI( } } -#endif // _M_AMD64 diff --git a/hal/halx86/generic/up.c b/hal/halx86/generic/up.c index 6ea91e32eddea..4ad3b71b19737 100644 --- a/hal/halx86/generic/up.c +++ b/hal/halx86/generic/up.c @@ -47,9 +47,6 @@ HalpBroadcastClockIpi( { NOTHING; } - -#ifdef _M_AMD64 - VOID NTAPI HalSendNMI( @@ -58,6 +55,8 @@ HalSendNMI( NOTHING; } +#ifdef _M_AMD64 + VOID NTAPI HalSendSoftwareInterrupt( diff --git a/hal/halx86/smp/ipi.c b/hal/halx86/smp/ipi.c index c6925d86a44e8..ed098fc694595 100644 --- a/hal/halx86/smp/ipi.c +++ b/hal/halx86/smp/ipi.c @@ -22,9 +22,10 @@ HalRequestIpi( { HalpRequestIpi(TargetProcessors); } - -#ifdef _M_AMD64 - +VOID +NTAPI +HalpSendNMI( + _In_ KAFFINITY TargetSet); VOID NTAPI HalSendNMI( @@ -32,6 +33,7 @@ HalSendNMI( { HalpSendNMI(TargetSet); } +#ifdef _M_AMD64 // See: // - https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Runtime/r0drv/nt/internal-r0drv-nt.h#L53 diff --git a/ntoskrnl/ke/i386/ipi.c b/ntoskrnl/ke/i386/ipi.c index eaa1ab0f3d8bb..1ca1e6452a5a0 100644 --- a/ntoskrnl/ke/i386/ipi.c +++ b/ntoskrnl/ke/i386/ipi.c @@ -11,7 +11,10 @@ BOOLEAN KiProcessorFreezeHandler( _In_ PKTRAP_FRAME TrapFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame); - +VOID +NTAPI +HalSendNMI( + _In_ KAFFINITY TargetSet); VOID NTAPI KiIpiGenericCallTarget(_Inout_ PKIPI_CONTEXT PacketContext, @@ -161,11 +164,7 @@ KiIpiServiceRoutine(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFra { HalRequestSoftwareInterrupt(DISPATCH_LEVEL); } - /* Freeze level! Trigger a FREEZE interrupt */ - if (InterlockedBitTestAndReset((PLONG)&Prcb->RequestSummary, IPI_FREEZE)) - { - KiProcessorFreezeHandler(TrapFrame, ExceptionFrame); - } + /* SYNCH_REQUEST we have a function pointer to execute! */ if (InterlockedBitTestAndReset((PLONG)&Prcb->RequestSummary, IPI_SYNCH_REQUEST)) @@ -293,7 +292,16 @@ VOID FASTCALL KiIpiSend(IN KAFFINITY TargetProcessors, IN ULONG IpiRequest) { + if (IpiRequest == IPI_FREEZE) + { + /* On ? the freeze IPI is an NMI */ + HalSendNMI(TargetProcessors); + } + else + { + /* Call private function */ KiIpiSendRequest(TargetProcessors, IpiRequest); + } } diff --git a/ntoskrnl/ke/i386/traphdlr.c b/ntoskrnl/ke/i386/traphdlr.c index 852a70c6ffe66..90337c13a33f4 100644 --- a/ntoskrnl/ke/i386/traphdlr.c +++ b/ntoskrnl/ke/i386/traphdlr.c @@ -454,7 +454,10 @@ KiTrap01Handler(IN PKTRAP_FRAME TrapFrame) TrapFrame->Eip, TrapFrame); } - +BOOLEAN +KiProcessorFreezeHandler( + _In_ PKTRAP_FRAME TrapFrame, + _In_ PKEXCEPTION_FRAME ExceptionFrame); VOID __cdecl KiTrap02Handler(VOID) @@ -537,6 +540,13 @@ KiTrap02Handler(VOID) /* Store the trap frame in the KPRCB */ KiSaveProcessorState(&TrapFrame, NULL); + /* Check if this is a freeze */ + if (KiProcessorFreezeHandler(&TrapFrame, NULL)) + { + /* NMI was handled */ + goto Exit; + } + /* Call any registered NMI handlers and see if they handled it or not */ if (!KiHandleNmi()) { @@ -566,6 +576,7 @@ KiTrap02Handler(VOID) KiSystemFatalException(EXCEPTION_NMI, NULL); } +Exit: /* Restore original TSS */ KeGetPcr()->TSS = Tss;