Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gamenative/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Utkarsh Dalal <utkarsh.dalal@toptal.com>
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<FEX::Windows::TSOHandlerConfig> 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

1 change: 1 addition & 0 deletions gamenative/series
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading