From 0be3dc354ff414aa9266db63fd8d55e821e8538c Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sun, 15 Nov 2026 13:20:23 +0530 Subject: [PATCH] series: apply cross-process work inside a syscall callback (0013) --- gamenative/README.md | 1 + ...ss-cross-process-work-inside-a-sysca.patch | 111 ++++++++++++++++++ gamenative/series | 1 + 3 files changed, 113 insertions(+) create mode 100644 gamenative/patches/0013-arm64ec-process-cross-process-work-inside-a-sysca.patch diff --git a/gamenative/README.md b/gamenative/README.md index 3a40861ad8..e511b8f5ff 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -33,6 +33,7 @@ behaviour that is not a plain correctness fix is behind an env var. | 0010 wow64-handle-exceptions-raised-before-the-first-thread | GameNative | The WoW64 exception handler read the thread state without a null check; a fault during `BTCpuProcessInit` (cache validation) killed the process silently. The overcommit path now runs before any thread access | to be sent upstream | | 0011 codecache-keep-the-main-dispatcher-config-when-creating-the-validation-context | GameNative | The validation context's `InitCore` overwrote the shared signal handler config | to be sent upstream | | 0012 windows-log-the-code-cache-load-phases-and-pre-thread-exceptions | GameNative | INFO lines for each phase of an offline cache load (map, copy, relocate, register, finalize, enable), visible with `FEX_SILENTLOG=0` | GameNative only | +| 0013 arm64ec-process-cross-process-work-inside-a-syscall-callback | GameNative | Cross-process work (memory-write, alloc/free/protect and flush entries) is applied with the thread marked as inside a syscall callback. The invalidation it triggers holds `CodeInvalidationMutex` while the allocator frees memory; outside a callback ntdll reported that free back to `NotifyMemoryFree`, which locked the mutex again on the same thread and hung | to be sent upstream | Left out of 0002 on purpose: tarikbc 0002 (hides the emulator from the guest), 0007 (rewrites ntdll export bytes to pass one game's hook check), 0008 (debug tool), 0010 (obsolete on Proton diff --git a/gamenative/patches/0013-arm64ec-process-cross-process-work-inside-a-sysca.patch b/gamenative/patches/0013-arm64ec-process-cross-process-work-inside-a-sysca.patch new file mode 100644 index 0000000000..c2de4e4c24 --- /dev/null +++ b/gamenative/patches/0013-arm64ec-process-cross-process-work-inside-a-sysca.patch @@ -0,0 +1,111 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Sun, 15 Nov 2026 13:19:58 +0530 +Subject: [PATCH] Arm64EC: Process cross-process work inside a syscall callback + +The work list handlers invalidate code with CodeInvalidationMutex held, and the +allocator can free memory while it is held. ntdll only skips its memory +notifications for calls made inside a syscall callback, so the nested +NtFreeVirtualMemory called back into NotifyMemoryFree on the same thread, which +locked CodeInvalidationMutex again and hung. + +Mark the thread as inside a syscall callback while the work is processed, the +same way ResetToConsistentState already does. +--- + Source/Windows/ARM64EC/Module.cpp | 47 ++++++++++++++++++------------- + 1 file changed, 28 insertions(+), 19 deletions(-) + +diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp +index 1fcea15d2..ced7197fb 100644 +--- a/Source/Windows/ARM64EC/Module.cpp ++++ b/Source/Windows/ARM64EC/Module.cpp +@@ -408,6 +408,22 @@ void HandleImageUnmap(uint64_t Address, uint64_t Size) { + } + } // namespace + ++class ScopedCallbackDisable { ++private: ++ bool Prev; ++ ++public: ++ ScopedCallbackDisable() { ++ const auto CPUArea = GetCPUArea(); ++ Prev = CPUArea.Area->InSyscallCallback; ++ CPUArea.Area->InSyscallCallback = true; ++ } ++ ++ ~ScopedCallbackDisable() { ++ GetCPUArea().Area->InSyscallCallback = Prev; ++ } ++}; ++ + namespace Exception { + static std::optional HandlerConfig; + static uintptr_t KiUserExceptionDispatcher; +@@ -884,12 +900,21 @@ static void SynchronousExceptionHandler(FEXCore::Context::Context* CTX, FEXCore: + ERROR_AND_DIE_FMT("This should always noreturn"); + } + ++// Work queued by other processes (or by ntdll on behalf of a system call) is applied with code invalidations that hold ++// CodeInvalidationMutex, and the allocator can free memory while it is held. ntdll only skips its memory notifications ++// for calls made inside a syscall callback, so without this the nested NtFreeVirtualMemory calls back into ++// NotifyMemoryFree on the same thread and locks the mutex a second time. ++static void ProcessPendingCrossProcessWork() { ++ ScopedCallbackDisable Guard; ++ ProcessPendingCrossProcessEmulatorWork(); ++} ++ + class ECSyscallHandler : public FEXCore::HLE::SyscallHandler, public FEXCore::Allocator::FEXAllocOperators { + public: + ECSyscallHandler() = default; + + void HandleSyscall(FEXCore::Core::CpuStateFrame* Frame) override { +- ProcessPendingCrossProcessEmulatorWork(); ++ ProcessPendingCrossProcessWork(); + + // Manually raise an exeption with the current JIT state packed into a native context, ntdll handles this and + // reenters the JIT (see dlls/ntdll/signal_arm64ec.c in wine). +@@ -948,7 +973,7 @@ public: + } + + void PreCompile() override { +- ProcessPendingCrossProcessEmulatorWork(); ++ ProcessPendingCrossProcessWork(); + } + }; + } // namespace Exception +@@ -1145,7 +1170,7 @@ extern "C" NTSTATUS NtProtectVirtualMemoryHooked(HANDLE Process, PVOID* AddressP + } + + extern "C" void SyncThreadContext(CONTEXT* Context) { +- ProcessPendingCrossProcessEmulatorWork(); ++ Exception::ProcessPendingCrossProcessWork(); + auto* Thread = GetCPUArea().ThreadState(); + // All other EFlags bits are lost when converting to/from an ARM64EC context, so merge them in from the current JIT state. + // This is advisable over dropping their values as thread suspend/resume uses this function, and that can happen at any point in guest code. +@@ -1267,22 +1292,6 @@ NTSTATUS ProcessInit() { + + void ProcessTerm(HANDLE Handle, BOOL After, NTSTATUS Status) {} + +-class ScopedCallbackDisable { +-private: +- bool Prev; +- +-public: +- ScopedCallbackDisable() { +- const auto CPUArea = GetCPUArea(); +- Prev = CPUArea.Area->InSyscallCallback; +- CPUArea.Area->InSyscallCallback = true; +- } +- +- ~ScopedCallbackDisable() { +- GetCPUArea().Area->InSyscallCallback = Prev; +- } +-}; +- + // Returns true if exception dispatch should be halted and the execution context restored to NativeContext + bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* Exception, CONTEXT* GuestContext, ARM64_NT_CONTEXT* NativeContext) { + auto Thread = CPUArea.ThreadState(); +-- +2.43.0 + diff --git a/gamenative/series b/gamenative/series index a4a95a30ed..e7005c5926 100644 --- a/gamenative/series +++ b/gamenative/series @@ -10,3 +10,4 @@ 0010-wow64-handle-exceptions-raised-before-the-first-.patch 0011-codecache-keep-the-main-dispatcher-config-when-c.patch 0012-windows-log-the-code-cache-load-phases-and-pre-t.patch +0013-arm64ec-process-cross-process-work-inside-a-sysca.patch