From de0d73201cbefd00ed25ae333c1ff7706517d92d Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Fri, 6 Nov 2026 20:01:53 +0530 Subject: [PATCH] series: rebase onto FEX-2611 and take the offline code cache fixes (gn-6) Upstream merged the NtDllRedirectionLUTSize load (FEX#6021) and the kept guest loads (FEX#6069), so 0002 and 0007 are dropped. FEX-2611 also delivers JIT-raised guest faults on arm64ec without a host trap and passes the partial-fault address itself, so 0001 loses its VirtualQuery heuristic and hooks its stale NoExec retry into the new synchronous handler, and 0003 keeps only the fast raise (FEX_EXP_FASTTRAP is gone). The seven code cache patches from #7 follow as 0006-0012, and the package ships FEXOfflineCompiler32/64.exe in system32. --- .github/workflows/gamenative-fexcore.yml | 17 +- gamenative/README.md | 37 ++-- gamenative/SERIES_VERSION | 2 +- ...s-for-protected-launchers-that-decry.patch | 190 +++++++++++------- ...-ntdllredirectionlutsize-as-32-bits-.patch | 33 --- ...tamper-stepping-and-fault-delivery-.patch} | 185 +++-------------- ...t-btcpu64notifynonimageviews-so-ntd.patch} | 4 +- ...s-make-volatile-code-regions-opt-in.patch} | 0 ...-plain-loads-before-volatile-metada.patch} | 16 +- ...mpiler-load-the-config-before-initia.patch | 29 +++ ...base-relocation-offsets-to-the-code-.patch | 128 ++++++++++++ ...ep-guest-loads-whose-value-is-unused.patch | 96 --------- ...mpiler-quote-process-all-child-argum.patch | 71 +++++++ ...py-cached-code-to-a-writable-buffer-.patch | 62 ++++++ ...-exceptions-raised-before-the-first-.patch | 45 +++++ ...ep-the-main-dispatcher-config-when-c.patch | 35 ++++ ...the-code-cache-load-phases-and-pre-t.patch | 162 +++++++++++++++ gamenative/series | 17 +- 18 files changed, 736 insertions(+), 393 deletions(-) delete mode 100644 gamenative/patches/0002-arm64ec-load-ntdllredirectionlutsize-as-32-bits-.patch rename gamenative/patches/{0003-windows-anti-tamper-stepping-and-fault-delivery-.patch => 0002-windows-anti-tamper-stepping-and-fault-delivery-.patch} (87%) rename gamenative/patches/{0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch => 0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch} (96%) rename gamenative/patches/{0005-windows-make-volatile-code-regions-opt-in.patch => 0004-windows-make-volatile-code-regions-opt-in.patch} (100%) rename gamenative/patches/{0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch => 0005-fexcore-fence-plain-loads-before-volatile-metada.patch} (94%) create mode 100644 gamenative/patches/0006-fexofflinecompiler-load-the-config-before-initia.patch create mode 100644 gamenative/patches/0007-codecache-rebase-relocation-offsets-to-the-code-.patch delete mode 100644 gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch create mode 100644 gamenative/patches/0008-fexofflinecompiler-quote-process-all-child-argum.patch create mode 100644 gamenative/patches/0009-codecache-copy-cached-code-to-a-writable-buffer-.patch create mode 100644 gamenative/patches/0010-wow64-handle-exceptions-raised-before-the-first-.patch create mode 100644 gamenative/patches/0011-codecache-keep-the-main-dispatcher-config-when-c.patch create mode 100644 gamenative/patches/0012-windows-log-the-code-cache-load-phases-and-pre-t.patch diff --git a/.github/workflows/gamenative-fexcore.yml b/.github/workflows/gamenative-fexcore.yml index b4937e9a3c..2d537eec54 100644 --- a/.github/workflows/gamenative-fexcore.yml +++ b/.github/workflows/gamenative-fexcore.yml @@ -146,7 +146,9 @@ jobs: - name: Install working-directory: fex - run: DESTDIR="$PWD"/install cmake --build build -t install + run: | + DESTDIR="$PWD"/install cmake --build build -t install + cp build/Bin/FEXOfflineCompiler*.exe install/usr/lib/wine/aarch64-windows/ - name: ccache stats run: ccache -s @@ -154,7 +156,9 @@ jobs: - uses: actions/upload-artifact@v6 with: name: dll-${{ matrix.target }} - path: fex/install/usr/lib/wine/aarch64-windows/lib*.dll + path: | + fex/install/usr/lib/wine/aarch64-windows/lib*.dll + fex/install/usr/lib/wine/aarch64-windows/FEXOfflineCompiler*.exe if-no-files-found: error retention-days: 7 @@ -199,6 +203,7 @@ jobs: run: | mkdir -p pkg/system32 cp dlls/libarm64ecfex.dll dlls/libwow64fex.dll pkg/system32/ + cp dlls/FEXOfflineCompiler64.exe dlls/FEXOfflineCompiler32.exe pkg/system32/ patches=$(grep -c -v -e '^#' -e '^$' gamenative/series) cat > pkg/profile.json <next and the tail sequence drift past its sequence re-check and crashed reading NULL+8. Outside metadata blocks nothing changes | GameNative only for now | -| 0007 fexcore-keep-guest-loads-whose-value-is-unused | GameNative | The dead-flag pass dropped a guest load whose only consumer was a flag write that a later instruction overwrote. HotSpot C1 null checks are `cmp rax,[reg]` with dead flags and rely on the fault, so a null receiver reached the next field load and the VM aborted (Necesse, 13 s after start). Guest load ops now declare side effects, as LoadMemPair already did | GameNative only for now | - -Left out of 0003 on purpose: tarikbc 0002 (hides the emulator from the guest), 0007 (rewrites +| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed) | Non-image section views, OS query for unknown executable pages, fault raise with `hlt` and the right exception code, stale NoExec retry (on the host exception path and in the FEX-2611 synchronous fault handler), PF/AF through context conversion (needs the ntdll export), disk-cache extents, compiler restart at inaccessible pages, `0x67` MOVS/STOS and FS/GS decode | analysis in FEX-Emu/FEX#5328; no PR yet | +| 0002 windows-anti-tamper-stepping-and-fault-delivery | tarikbc 0004, 0006, 0009, 0012, 0013, 0014, 0017 (v1.6.0, f4a686a0) squashed | Guest-visible protection of trapped pages, raw syscall return registers, volatile code regions, 512 MB code buffer, translation reuse, guest exceptions delivered straight into `KiUserExceptionDispatcher` on the guest stack (fast raise). Env gates: `FEX_EXP_SKIP_CALLRET_RESET=1` opts in; `FEX_EXP_FASTRAISE=0` opts out. The trapless fault path of tarikbc 0017 (`FEX_EXP_FASTTRAP`) is upstream since FEX-2611 and was dropped here. Volatile code regions were on by default here; 0004 makes them opt-in | FEX-Emu/FEX#6022, #6023; Wine bug 60461 | +| 0003 arm64ec-export-btcpu64notifynonimageviews | GameNative | Exports `BTCpu64NotifyNonImageViews` so ntdll only reports non-image views to a FEX that can take them | pairs with GameNative/proton-wine `dlls_arm64ec_emulator_notifications.patch` | +| 0004 windows-make-volatile-code-regions-opt-in | GameNative | Volatile code region detection (from 0003) runs only with `FEX_EXP_VOLATILE=1`; any other value or unset is off. On by default it misfires on JIT runtimes: TMNT: Shredder's Revenge (FNA/.NET) recompiles on its main thread, runs about 10x slower and never creates a D3D device. Age of Empires IV needs it and sets it in its overlay. Env gate: `FEX_EXP_VOLATILE=1` opts in | GameNative only | +| 0005 fexcore-fence-plain-loads-before-volatile-metadata-tso-loads | GameNative | With PE volatile metadata, a listed (TSO) load gets a `dmb ishld` in front when a plain load may precede it in the same metadata block, restoring x86 load order. Counter-Strike 2's CTSQueue pop let node->next and the tail sequence drift past its sequence re-check and crashed reading NULL+8. Outside metadata blocks nothing changes | GameNative only for now | +| 0006 fexofflinecompiler-load-the-config-before-initializing-logging | GameNative | The Windows offline compiler read `SilentLog` before any config was loaded and crashed at startup | to be sent upstream | +| 0007 codecache-rebase-relocation-offsets-to-the-code-buffer | GameNative | Since upstream #5840 relocation offsets are per compiled unit; the cache writer still stored them as code-buffer offsets, so every offline cache carried stale host pointers and un-rebased guest addresses. Offsets are rebased when a cache is generated or validated, and sorted on save | to be sent upstream | +| 0008 fexofflinecompiler-quote-process-all-child-arguments | GameNative | `process-all` passed child arguments unquoted, so exe names with spaces broke | to be sent upstream | +| 0009 codecache-copy-cached-code-to-a-writable-buffer-on-wow64 | GameNative | The WoW64 loader patched relocations into the read-only cache file mapping and killed the guest at start-up; it now gets a writable copy like ARM64EC (upstream #5658 covered ARM64EC only) | to be sent upstream | +| 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 | + +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 11.0-2), 0016 (hooks `powrprof` from the exit thunk; the fix belongs in Wine, bug 60462). Left out of 0001: dmage22's log-level change (cosmetic). -Patch 0003 was rebased onto 0001 by hand in three places: the volatile-code and translation-reuse -hooks in `Core.cpp` sit after 0001's compile restart point, and the fast-raise call in -`RethrowGuestException`. The two fault paths compose: 0003's trapless branch runs first, and -0001's `hlt` is the fallback when `FEX_EXP_FASTTRAP=0`. +Patch 0002 was rebased onto 0001 by hand: the volatile-code and translation-reuse hooks in +`Core.cpp` sit after 0001's compile restart point, and the fast raise replaces the final +`NtRaiseException` in `RaiseException`. FEX-2611 delivers JIT-raised guest faults on arm64ec +without a host trap itself, so 0002's own trapless path went at the 2611 rebase, and 0001's stale +NoExec retry hooks upstream's synchronous handler as well as the host exception path. -Applies on `FEX-2610`. On upstream `main` after 2026-10-12 it applies without 0002. +Dropped at FEX-2611 because upstream merged them: the `NtDllRedirectionLUTSize` 32-bit load +(FEX-Emu/FEX#6021) and the kept-guest-loads IR change (FEX-Emu/FEX#6069). + +Applies on `FEX-2611`. ## When upstream changes diff --git a/gamenative/SERIES_VERSION b/gamenative/SERIES_VERSION index 7ed6ff82de..1e8b314962 100644 --- a/gamenative/SERIES_VERSION +++ b/gamenative/SERIES_VERSION @@ -1 +1 @@ -5 +6 diff --git a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch index 7ef87daef9..e8c2b9980d 100644 --- a/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch +++ b/gamenative/patches/0001-windows-fixes-for-protected-launchers-that-decry.patch @@ -13,24 +13,31 @@ compiler at inaccessible pages instead of deadlocking on the invalidation lock, 0x67 MOVS/STOS and FS/GS selector loads in 64-bit mode. Upstream: analysis in FEX-Emu/FEX#5328; no PR yet. + +Rebased onto FEX-2611: the partial-fault address now comes from upstream (511f58356, the Break +op carries the first byte that could not be fetched), so the VirtualQuery heuristic in +HandleGuestException is gone. FEX-2611 also delivers JIT-raised guest faults on arm64ec straight +to SynchronousExceptionHandler (27cfd41bf, 07abec317), so the stale NoExec retry runs there too +(DropStaleNoExecFault + EnterDispatcherFillSRA), not only on the host exception path. --- - FEXCore/Source/Interface/Core/Core.cpp | 34 ++++ + FEXCore/Source/Interface/Core/Core.cpp | 34 +++ FEXCore/Source/Interface/Core/DiskCache.cpp | 10 +- .../Interface/Core/Dispatcher/Dispatcher.cpp | 9 + FEXCore/Source/Interface/Core/Frontend.cpp | 5 + .../Interface/Core/OpcodeDispatcher.cpp | 72 ++++--- .../Source/Interface/Core/OpcodeDispatcher.h | 2 + .../FEXCore/Debug/InternalThreadState.h | 13 ++ - Source/Windows/ARM64EC/Module.cpp | 177 +++++++++++++++++- - Source/Windows/Common/Exception.h | 28 ++- - Source/Windows/Common/InvalidationTracker.cpp | 119 +++++++++++- - 10 files changed, 422 insertions(+), 47 deletions(-) + Source/Windows/ARM64EC/Module.S | 9 + + Source/Windows/ARM64EC/Module.cpp | 204 +++++++++++++++++- + Source/Windows/Common/Exception.h | 8 +- + Source/Windows/Common/InvalidationTracker.cpp | 119 +++++++++- + 11 files changed, 439 insertions(+), 46 deletions(-) diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp -index 63280a42a..b11fae62d 100644 +index 2ba56b69a..53d81a872 100644 --- a/FEXCore/Source/Interface/Core/Core.cpp +++ b/FEXCore/Source/Interface/Core/Core.cpp -@@ -848,6 +848,24 @@ ContextImpl::CompileCodeResult ContextImpl::CompileCode(FEXCore::Core::InternalT +@@ -854,6 +854,24 @@ ContextImpl::CompileCodeResult ContextImpl::CompileCode(FEXCore::Core::InternalT }; } @@ -55,7 +62,7 @@ index 63280a42a..b11fae62d 100644 uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_t GuestRIP, uint64_t MaxInst) { if constexpr (BLOCK_DEBUGGING) { // Block debugging logic is hand-written and needs to be handled with care. -@@ -875,6 +893,16 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ +@@ -881,6 +899,16 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ return HostCode; } @@ -72,7 +79,7 @@ index 63280a42a..b11fae62d 100644 Thread->FrontendDecoder->SetupDecodeInstructionsAtEntry(Thread, GuestRIP, MaxInst); std::optional Region = SyscallHandler->LookupExecutableFileSection(Thread, GuestRIP); -@@ -1032,6 +1060,12 @@ uintptr_t ContextImpl::CompileSingleStep(FEXCore::Core::CpuStateFrame* Frame, ui +@@ -1038,6 +1066,12 @@ uintptr_t ContextImpl::CompileSingleStep(FEXCore::Core::CpuStateFrame* Frame, ui // Invalidate might take a unique lock on this, to guarantee that during invalidation no code gets compiled auto lk = GuardSignalDeferringSection(CodeInvalidationMutex, Thread); @@ -86,10 +93,10 @@ index 63280a42a..b11fae62d 100644 auto [CompiledCode, DebugData, StartAddr, Length, _] = CompileCode(Thread, GuestRIP, 1); auto CodePtr = CompiledCode.EntryPoints[GuestRIP]; diff --git a/FEXCore/Source/Interface/Core/DiskCache.cpp b/FEXCore/Source/Interface/Core/DiskCache.cpp -index cf341913b..90aca77d5 100644 +index ed63ddb62..fd59ac06d 100644 --- a/FEXCore/Source/Interface/Core/DiskCache.cpp +++ b/FEXCore/Source/Interface/Core/DiskCache.cpp -@@ -1216,7 +1216,15 @@ namespace DiskCache { +@@ -1215,7 +1215,15 @@ namespace DiskCache { } } @@ -107,7 +114,7 @@ index cf341913b..90aca77d5 100644 MesaFOZ::foz_payload_key Key = {}; { diff --git a/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp b/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp -index 2f4babd70..df48ca153 100644 +index 5637b8cef..6ed397b4f 100644 --- a/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp +++ b/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp @@ -434,8 +434,17 @@ void Dispatcher::EmitDispatcher() { @@ -129,10 +136,10 @@ index 2f4babd70..df48ca153 100644 } diff --git a/FEXCore/Source/Interface/Core/Frontend.cpp b/FEXCore/Source/Interface/Core/Frontend.cpp -index 78d5f1a2e..cdd8894fd 100644 +index 7b939c0b3..1934b8bf5 100644 --- a/FEXCore/Source/Interface/Core/Frontend.cpp +++ b/FEXCore/Source/Interface/Core/Frontend.cpp -@@ -1765,6 +1765,11 @@ void Decoder::SetupDecodeInstructionsAtEntry(FEXCore::Core::InternalThreadState* +@@ -1795,6 +1795,11 @@ void Decoder::SetupDecodeInstructionsAtEntry(FEXCore::Core::InternalThreadState* TotalInstructions = 0; @@ -145,10 +152,10 @@ index 78d5f1a2e..cdd8894fd 100644 SectionMaxAddress = ~0ULL; Relocations = nullptr; diff --git a/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp b/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp -index 8dc605ca7..11505f20d 100644 +index 1d8701612..cbb8e72f8 100644 --- a/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp +++ b/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp -@@ -452,6 +452,11 @@ void OpDispatchBuilder::POPSegmentOp(OpcodeArgs, uint32_t SegmentReg) { +@@ -457,6 +457,11 @@ void OpDispatchBuilder::POPSegmentOp(OpcodeArgs, uint32_t SegmentReg) { default: break; // Do nothing } @@ -160,7 +167,7 @@ index 8dc605ca7..11505f20d 100644 UpdatePrefixFromSegment(NewSegment, SegmentReg); } -@@ -1275,8 +1280,10 @@ void OpDispatchBuilder::MOVSegOp(OpcodeArgs, bool ToSeg) { +@@ -1281,8 +1286,10 @@ void OpDispatchBuilder::MOVSegOp(OpcodeArgs, bool ToSeg) { _StoreContextGPR(OpSize::i16Bit, Src, offsetof(FEXCore::Core::CPUState, gs_idx)); UpdatePrefixFromSegment(Src, FEXCore::X86Tables::DecodeFlags::FLAG_GS_PREFIX); } else { @@ -173,7 +180,7 @@ index 8dc605ca7..11505f20d 100644 } break; case FEXCore::X86State::REG_RSP: // FS -@@ -1285,8 +1292,8 @@ void OpDispatchBuilder::MOVSegOp(OpcodeArgs, bool ToSeg) { +@@ -1291,8 +1298,8 @@ void OpDispatchBuilder::MOVSegOp(OpcodeArgs, bool ToSeg) { _StoreContextGPR(OpSize::i16Bit, Src, offsetof(FEXCore::Core::CPUState, fs_idx)); UpdatePrefixFromSegment(Src, FEXCore::X86Tables::DecodeFlags::FLAG_FS_PREFIX); } else { @@ -184,7 +191,7 @@ index 8dc605ca7..11505f20d 100644 } break; default: UnimplementedOp(Op); return; -@@ -3213,14 +3220,25 @@ void OpDispatchBuilder::DECOp(OpcodeArgs) { +@@ -3298,14 +3305,25 @@ void OpDispatchBuilder::DECOp(OpcodeArgs) { } } @@ -212,7 +219,7 @@ index 8dc605ca7..11505f20d 100644 const bool Repeat = (Op->Flags & (FEXCore::X86Tables::DecodeFlags::FLAG_REP_PREFIX | FEXCore::X86Tables::DecodeFlags::FLAG_REPNE_PREFIX)) != 0; if (!Repeat) { -@@ -3228,7 +3246,8 @@ void OpDispatchBuilder::STOSOp(OpcodeArgs) { +@@ -3313,7 +3331,8 @@ void OpDispatchBuilder::STOSOp(OpcodeArgs) { Ref Src = LoadGPRRegister(X86State::REG_RAX, Size, 0, true); // Only ES prefix @@ -222,7 +229,7 @@ index 8dc605ca7..11505f20d 100644 // Store to memory where RDI points if (CTX->IsMemcpyAtomicTSOEnabled()) { -@@ -3238,40 +3257,41 @@ void OpDispatchBuilder::STOSOp(OpcodeArgs) { +@@ -3323,40 +3342,41 @@ void OpDispatchBuilder::STOSOp(OpcodeArgs) { } // Offset the pointer @@ -274,7 +281,7 @@ index 8dc605ca7..11505f20d 100644 auto DstSegment = GetSegment(0, FEXCore::X86Tables::DecodeFlags::FLAG_ES_PREFIX, true); auto SrcSegment = GetSegment(Op->Flags, FEXCore::X86Tables::DecodeFlags::FLAG_DS_PREFIX); -@@ -3297,11 +3317,13 @@ void OpDispatchBuilder::MOVSOp(OpcodeArgs) { +@@ -3382,11 +3402,13 @@ void OpDispatchBuilder::MOVSOp(OpcodeArgs) { } StoreGPRRegister(X86State::REG_RCX, Constant(0)); @@ -292,7 +299,7 @@ index 8dc605ca7..11505f20d 100644 if (CTX->IsMemcpyAtomicTSOEnabled()) { auto Src = _LoadMemGPRAutoTSO(Size, RSI, Size); -@@ -3313,11 +3335,9 @@ void OpDispatchBuilder::MOVSOp(OpcodeArgs) { +@@ -3398,11 +3420,9 @@ void OpDispatchBuilder::MOVSOp(OpcodeArgs) { _StoreMem(RegClass::GPR, Size, Src, RDI, Invalid(), OpSize::i8Bit, MemOffsetType::SXTX, 1); } @@ -308,10 +315,10 @@ index 8dc605ca7..11505f20d 100644 } diff --git a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h -index 71766e010..85919f294 100644 +index b2eb381e0..03c584d3e 100644 --- a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h +++ b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h -@@ -1516,6 +1516,8 @@ private: +@@ -1547,6 +1547,8 @@ private: #undef OpcodeArgs Ref AppendSegmentOffset(Ref Value, uint32_t Flags, uint32_t DefaultPrefix = 0, bool Override = false); @@ -344,8 +351,25 @@ index 42feabb1c..7d6cb2018 100644 // BaseFrameState should always be at the end, directly before the interrupt fault page FEXCore::Core::CpuStateFrame BaseFrameState {}; +diff --git a/Source/Windows/ARM64EC/Module.S b/Source/Windows/ARM64EC/Module.S +index ce36c1d68..4d9fd8b7e 100644 +--- a/Source/Windows/ARM64EC/Module.S ++++ b/Source/Windows/ARM64EC/Module.S +@@ -173,3 +173,12 @@ end: + "#JumpSetStack": + mov sp, x1 + br x0 ++ ++ // Re-enters the dispatcher at DispatcherLoopTopEnterECFillSRA for a normal block. ++ // Expects the CPU area in x0, the entry address in x1 and the SP to set in x2. ++.global "#EnterDispatcherFillSRA" ++"#EnterDispatcherFillSRA": ++ mov x17, x0 // EC_ENTRY_CPUAREA_REG ++ mov sp, x2 ++ mov x11, #0 // ENTRY_FILL_SRA_SINGLE_INST_REG: normal block ++ br x1 diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index 633f252c3..ab908b0fa 100644 +index 3be7e465a..4b8149bf3 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -50,6 +50,7 @@ $end_info$ @@ -356,7 +380,16 @@ index 633f252c3..ab908b0fa 100644 #include #include #include -@@ -147,6 +148,7 @@ fextl::unique_ptr SignalDelegator; +@@ -99,6 +100,8 @@ static fextl::string AppConfigName {}; + + [[noreturn]] + void JumpSetStack(uintptr_t PC, uintptr_t SP); ++[[noreturn]] ++void EnterDispatcherFillSRA(CHPE_V2_CPU_AREA_INFO* Area, uintptr_t PC, uintptr_t SP); + } + + struct ThreadCPUArea { +@@ -147,6 +150,7 @@ fextl::unique_ptr SignalDelegator; fextl::unique_ptr SyscallHandler; fextl::unique_ptr StatAllocHandler; std::optional InvalidationTracker; @@ -364,7 +397,7 @@ index 633f252c3..ab908b0fa 100644 std::optional CPUFeatures; std::optional OvercommitTracker; std::optional ImageTracker; -@@ -347,11 +349,34 @@ bool InitSyscalls() { +@@ -347,11 +351,34 @@ bool InitSyscalls() { return true; } @@ -400,7 +433,7 @@ index 633f252c3..ab908b0fa 100644 } void HandleImageUnmap(uint64_t Address, uint64_t Size) { -@@ -526,8 +551,10 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea +@@ -597,8 +624,10 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea ECContext.X24 = 0; ECContext.X28 = 0; @@ -413,7 +446,7 @@ index 633f252c3..ab908b0fa 100644 uint32_t EFlags = CTX->ReconstructCompactedEFLAGS(Thread, false, nullptr, 0); ECContext.Cpsr = 0; ECContext.Cpsr |= (EFlags & (1U << FEXCore::X86State::RFLAG_TF_RAW_LOC)) ? (1U << 21) : 0; -@@ -535,6 +562,8 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea +@@ -606,6 +635,8 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea ECContext.Cpsr |= (EFlags & (1U << FEXCore::X86State::RFLAG_CF_RAW_LOC)) ? (1U << 29) : 0; ECContext.Cpsr |= (EFlags & (1U << FEXCore::X86State::RFLAG_ZF_RAW_LOC)) ? (1U << 30) : 0; ECContext.Cpsr |= (EFlags & (1U << FEXCore::X86State::RFLAG_SF_RAW_LOC)) ? (1U << 31) : 0; @@ -422,8 +455,8 @@ index 633f252c3..ab908b0fa 100644 ECContext.Fpcr = FPCR; ECContext.Fpsr = FPSR; -@@ -572,6 +601,94 @@ static void RethrowGuestException(const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& - NtRaiseExceptionNative(&GuestRec, &GuestContext, FirstChance); +@@ -657,12 +688,125 @@ static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, co + RaiseException(Thread, Rec, GuestContext); } +// A NoExec stub is compiled when a block entry is not executable at compile time. If another thread makes the page @@ -431,17 +464,16 @@ index 633f252c3..ab908b0fa 100644 +// is published, leaving it cached for good: every later run raises an execute fault for a page that is executable. +// Decrypt-on-demand protectors (Overwatch_loader.dll) then spin forever, since their handler has nothing left to +// unlock. Real hardware would just run the code, so when the fault is stale drop the cached code and resume at RIP. -+static bool RetryStaleNoExecFault(const ThreadCPUArea CPUArea, ARM64_NT_CONTEXT& Context) { -+ auto* Thread = CPUArea.ThreadState(); ++// ++// Expects the guest state to be synchronised already. Returns true when the fault was dropped and the caller must ++// re-enter the dispatcher at the current RIP. ++static bool DropStaleNoExecFault(FEXCore::Core::InternalThreadState* Thread) { + auto& Fault = Thread->CurrentFrame->SynchronousFaultData; + if (!Fault.FaultToTopAndGeneratedException || Fault.Signal != FEXCore::Core::FAULT_SIGSEGV || + Fault.TrapNo != FEXCore::X86State::X86_TRAPNO_PF || !InvalidationTracker) { + return false; + } + -+ if (!IsDispatcherAddress(Context.Pc)) { -+ ReconstructThreadState(Thread, Context); -+ } + const uint64_t Rip = Thread->CurrentFrame->State.rip; + + // Bound the retries so a disagreement between the tracker and the compiler can never become a hang. @@ -471,6 +503,26 @@ index 633f252c3..ab908b0fa 100644 + InvalidationTracker->InvalidateAlignedInterval(FirstPage, LastPage - FirstPage + FEXCore::Utils::FEX_PAGE_SIZE, false); + + Fault.FaultToTopAndGeneratedException = false; ++ return true; ++} ++ ++// Host-exception flavour: the fault trapped out of JIT code (WoW64, or arm64ec without fault-free synchronous faults). ++static bool RetryStaleNoExecFault(const ThreadCPUArea CPUArea, ARM64_NT_CONTEXT& Context) { ++ auto* Thread = CPUArea.ThreadState(); ++ auto& Fault = Thread->CurrentFrame->SynchronousFaultData; ++ if (!Fault.FaultToTopAndGeneratedException || Fault.Signal != FEXCore::Core::FAULT_SIGSEGV || ++ Fault.TrapNo != FEXCore::X86State::X86_TRAPNO_PF) { ++ return false; ++ } ++ ++ if (!IsDispatcherAddress(Context.Pc)) { ++ ReconstructThreadState(Thread, Context); ++ } ++ ++ if (!DropStaleNoExecFault(Thread)) { ++ return false; ++ } ++ + Context.Pc = CPUArea.DispatcherLoopTopEnterECFillSRA(); + Context.Sp = CPUArea.EmulatorStackBase(); + Context.X11 = 0; // ENTRY_FILL_SRA_SINGLE_INST_REG: normal block @@ -514,10 +566,22 @@ index 633f252c3..ab908b0fa 100644 + return true; +} + - class ECSyscallHandler : public FEXCore::HLE::SyscallHandler, public FEXCore::Allocator::FEXAllocOperators { - public: - ECSyscallHandler() = default; -@@ -615,7 +732,24 @@ public: + [[noreturn]] + static void SynchronousExceptionHandler(FEXCore::Context::Context* CTX, FEXCore::Core::CpuStateFrame* Frame) { + // Coming in to this function, the guest state is synchronized and the host thread is in a safe location. + auto Thread = Frame->Thread; + auto& Fault = Thread->CurrentFrame->SynchronousFaultData; + ++ // Stale NoExec faults never reach the host exception handler on this path, so drop them here and resume at RIP. ++ if (DropStaleNoExecFault(Thread)) { ++ const auto CPUArea = GetCPUArea(); ++ EnterDispatcherFillSRA(CPUArea.Area, CPUArea.DispatcherLoopTopEnterECFillSRA(), CPUArea.EmulatorStackBase()); ++ } ++ + uint64_t FPCR, FPSR; + __asm volatile("mrs %[fpcr], fpcr" : [fpcr] "=r"(FPCR)); + __asm volatile("mrs %[fpsr], fpsr" : [fpsr] "=r"(FPSR)); +@@ -730,7 +874,24 @@ public: } FEXCore::HLE::ExecutableRangeInfo QueryGuestExecutableRange(FEXCore::Core::InternalThreadState* Thread, uint64_t Address) override { @@ -543,7 +607,7 @@ index 633f252c3..ab908b0fa 100644 } void PreCompile() override { -@@ -629,9 +763,13 @@ extern "C" void SyncThreadContext(CONTEXT* Context) { +@@ -744,9 +905,13 @@ extern "C" void SyncThreadContext(CONTEXT* Context) { 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. @@ -560,7 +624,7 @@ index 633f252c3..ab908b0fa 100644 uint32_t StateEFlags = CTX->ReconstructCompactedEFLAGS(Thread, false, nullptr, 0); Context->EFlags = (Context->EFlags & ECValidEFlagsMask) | (StateEFlags & ~ECValidEFlagsMask); -@@ -662,6 +800,7 @@ NTSTATUS ProcessInit() { +@@ -777,6 +942,7 @@ NTSTATUS ProcessInit() { const auto NtDll = GetModuleHandleW(L"ntdll.dll"); const bool IsWine = !!GetProcAddress(NtDll, "wine_get_version"); @@ -568,7 +632,7 @@ index 633f252c3..ab908b0fa 100644 OvercommitTracker.emplace(IsWine); FEX::Windows::SetupEnvironmentVariableValues(NtDll); -@@ -761,6 +900,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -876,6 +1042,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -582,7 +646,7 @@ index 633f252c3..ab908b0fa 100644 std::scoped_lock Lock(ThreadCreationMutex); if (InvalidationTracker && InvalidationTracker->HandleRWXAccessViolation(Thread, NativeContext->Pc, FaultAddress)) { FEXCORE_PROFILE_INSTANT_INCREMENT(Thread, AccumulatedSMCCount, 1); -@@ -804,6 +950,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -919,6 +1092,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -591,9 +655,9 @@ index 633f252c3..ab908b0fa 100644 + } + if (IsEmulatorStackAddress(CPUArea, reinterpret_cast(__builtin_frame_address(0)))) { - Exception::RethrowGuestException(*Exception, *NativeContext); + Exception::RethrowGuestException(Thread, *Exception, *NativeContext); LogMan::Msg::DFmt("Rethrowing onto guest stack: {:X}", NativeContext->Sp); -@@ -902,7 +1052,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si +@@ -1081,7 +1258,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si { std::scoped_lock Lock(ThreadCreationMutex); @@ -614,18 +678,10 @@ index 633f252c3..ab908b0fa 100644 diff --git a/Source/Windows/Common/Exception.h b/Source/Windows/Common/Exception.h -index 12162233c..6b8528967 100644 +index b41a5e64a..83814521d 100644 --- a/Source/Windows/Common/Exception.h +++ b/Source/Windows/Common/Exception.h -@@ -6,6 +6,7 @@ - #include - - #include -+#include - #include - - namespace FEX::Windows { -@@ -74,12 +75,33 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame +@@ -74,12 +74,16 @@ static inline EXCEPTION_RECORD HandleGuestException(FEXCore::Core::CpuStateFrame } break; case FEXCore::X86State::X86_TRAPNO_OF: Dst.ExceptionCode = EXCEPTION_INT_OVERFLOW; return Dst; @@ -633,30 +689,12 @@ index 12162233c..6b8528967 100644 - // A page-fault raised by an explicit break in JIT code is always an execute fault + case FEXCore::X86State::X86_TRAPNO_PF: { + // A page-fault raised by an explicit break in JIT code is always an execute fault. -+ // -+ // The reported address has to be the first byte that could not be fetched, which is not always -+ // the start of the instruction: an instruction sitting at the tail of an executable page and -+ // running into the next one faults on that next page. Reporting the instruction start instead -+ // makes decrypt-on-demand protectors spin forever - their handler looks at -+ // ExceptionInformation[1], sees a page it has already unlocked, decides there is nothing to do, -+ // resumes, and faults identically. An x86 instruction is at most 15 bytes, so it can only ever -+ // cross a single page boundary. -+ uint64_t FaultAddress = Rip; -+ const uint64_t RipPage = Rip & FEXCore::Utils::FEX_PAGE_MASK; -+ constexpr ULONG ExecProt = PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; -+ MEMORY_BASIC_INFORMATION Info; -+ if (VirtualQuery(reinterpret_cast(RipPage), &Info, sizeof(Info)) && Info.State == MEM_COMMIT && -+ (Info.Protect & ExecProt)) { -+ FaultAddress = RipPage + FEXCore::Utils::FEX_PAGE_SIZE; -+ } -+ + // Set the code explicitly like every other case: the host trap that got us here is not necessarily an + // access violation (on Windows the SIGSEGV trampoline raises it with hlt). + Dst.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; Dst.NumberParameters = 2; Dst.ExceptionInformation[0] = EXCEPTION_EXECUTE_FAULT; -- Dst.ExceptionInformation[1] = Rip; -+ Dst.ExceptionInformation[1] = FaultAddress; + Dst.ExceptionInformation[1] = Fault.FaultAddress ? Fault.FaultAddress : Rip; return Dst; + } default: LogMan::Msg::EFmt("Unknown SIGSEGV trap: {}", Fault.TrapNo); break; diff --git a/gamenative/patches/0002-arm64ec-load-ntdllredirectionlutsize-as-32-bits-.patch b/gamenative/patches/0002-arm64ec-load-ntdllredirectionlutsize-as-32-bits-.patch deleted file mode 100644 index 97955fb3dc..0000000000 --- a/gamenative/patches/0002-arm64ec-load-ntdllredirectionlutsize-as-32-bits-.patch +++ /dev/null @@ -1,33 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: tarikbc -Date: Tue, 13 Oct 2026 17:07:39 +0530 -Subject: [PATCH] ARM64EC: fix the NtDllRedirectionLUT size load and bounds check in - CheckCall - -NtDllRedirectionLUTSize is a uint32_t; a 64-bit ldr only links while it happens to be -8-byte aligned, and the globals added by the next patch move it. The bounds check used -b.hi, which let an offset equal to the size through and read one entry past the end of -the LUT. - -Upstream: both commits of FEX-Emu/FEX#6021 (4dbb046d62, 3a2b1c0b43; merged as 94ff2b0c6, -2026-10-12). Drop this patch at FEX-2611. ---- - Source/Windows/ARM64EC/Module.S | 4 ++-- - 1 file changed, 2 insertions(+), 2 deletions(-) - -diff --git a/Source/Windows/ARM64EC/Module.S b/Source/Windows/ARM64EC/Module.S -index 2489e653c..ce36c1d68 100644 ---- a/Source/Windows/ARM64EC/Module.S -+++ b/Source/Windows/ARM64EC/Module.S -@@ -158,9 +158,9 @@ DIRECT_SYSCALL_WRAPPER("#NtProtectVirtualMemoryNative", WineNtProtectVirtualMemo - subs x16, x11, x9 - b.lo end - adrp x17, NtDllRedirectionLUTSize -- ldr x17, [x17, #:lo12:NtDllRedirectionLUTSize] -+ ldr w17, [x17, #:lo12:NtDllRedirectionLUTSize] - cmp x16, x17 -- b.hi end -+ b.hs end - adrp x17, NtDllRedirectionLUT - ldr x17, [x17, #:lo12:NtDllRedirectionLUT] - ldr w11, [x17, x16, lsl #2] diff --git a/gamenative/patches/0003-windows-anti-tamper-stepping-and-fault-delivery-.patch b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch similarity index 87% rename from gamenative/patches/0003-windows-anti-tamper-stepping-and-fault-delivery-.patch rename to gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch index 60fe747b73..c2cfa7d6c3 100644 --- a/gamenative/patches/0003-windows-anti-tamper-stepping-and-fault-delivery-.patch +++ b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch @@ -14,25 +14,27 @@ Left out: 0002 (hides the emulator), 0007 (rewrites ntdll export bytes), 0008 (d 0010 (obsolete on Proton 11.0-2), 0016 (hooks powrprof; belongs in Wine, bug 60462). Upstream: FEX-Emu/FEX#6022 (0012/0014), #6023 (0004); 0006 reported as Wine bug 60461. + +Rebased onto FEX-2611: trapless fault delivery (0017, FEX_EXP_FASTTRAP) is upstream now +(27cfd41bf, 07abec317: SupportsFaultFreeSynchronousFaults), so that half is dropped along with +the env var; only the fast raise into KiUserExceptionDispatcher stays, hooked into RaiseException. --- FEXCore/Source/Interface/Context/Context.h | 30 ++ FEXCore/Source/Interface/Core/CPUBackend.h | 7 + - FEXCore/Source/Interface/Core/Core.cpp | 320 ++++++++++++++++- - .../Interface/Core/Dispatcher/Dispatcher.cpp | 21 ++ + FEXCore/Source/Interface/Core/Core.cpp | 320 +++++++++++++++++- FEXCore/Source/Interface/Core/JIT/JIT.cpp | 4 + .../Core/SharedCodeBufferManager.cpp | 6 +- .../Interface/Core/SharedCodeBufferManager.h | 2 + FEXCore/include/FEXCore/Core/Context.h | 5 + - FEXCore/include/FEXCore/Core/CoreState.h | 1 + .../FEXCore/Debug/InternalThreadState.h | 5 + Source/Windows/ARM64EC/Module.S | 26 ++ - Source/Windows/ARM64EC/Module.cpp | 331 +++++++++++++++++- + Source/Windows/ARM64EC/Module.cpp | 270 ++++++++++++++- Source/Windows/Common/InvalidationTracker.cpp | 140 +++++++- Source/Windows/Common/InvalidationTracker.h | 18 + - 14 files changed, 908 insertions(+), 8 deletions(-) + 12 files changed, 825 insertions(+), 8 deletions(-) diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h -index 9f427da20..a8890860c 100644 +index 9b91055bd..01b1f8133 100644 --- a/FEXCore/Source/Interface/Context/Context.h +++ b/FEXCore/Source/Interface/Context/Context.h @@ -152,6 +152,10 @@ public: @@ -46,7 +48,7 @@ index 9f427da20..a8890860c 100644 uint32_t ReconstructCompactedEFLAGS(FEXCore::Core::InternalThreadState* Thread, bool WasInJIT, const uint64_t* HostGPRs, uint64_t PSTATE) override; void SetFlagsFromCompactedEFLAGS(FEXCore::Core::InternalThreadState* Thread, uint32_t EFLAGS) override; -@@ -515,6 +519,32 @@ private: +@@ -516,6 +520,32 @@ private: bool MonoDetected = false; std::atomic MonoBackpatcherBlock; @@ -80,7 +82,7 @@ index 9f427da20..a8890860c 100644 fextl::vector> CodeBufferList; }; diff --git a/FEXCore/Source/Interface/Core/CPUBackend.h b/FEXCore/Source/Interface/Core/CPUBackend.h -index 0183694cd..cb08a295e 100644 +index 1d19bc123..211933e82 100644 --- a/FEXCore/Source/Interface/Core/CPUBackend.h +++ b/FEXCore/Source/Interface/Core/CPUBackend.h @@ -138,6 +138,13 @@ namespace CPU { @@ -98,7 +100,7 @@ index 0183694cd..cb08a295e 100644 // Max spill slot size in bytes. We need at most 32 bytes // to be able to handle a 256-bit vector store to a slot. diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp -index b11fae62d..4802fdd50 100644 +index 53d81a872..3c6101205 100644 --- a/FEXCore/Source/Interface/Core/Core.cpp +++ b/FEXCore/Source/Interface/Core/Core.cpp @@ -9,6 +9,7 @@ $end_info$ @@ -224,7 +226,7 @@ index b11fae62d..4802fdd50 100644 const auto& CodeBlocks = BlockInfo->Blocks; Thread->OpDispatcher->BeginFunction(GuestRIP, &CodeBlocks, BlockInfo->TotalInstructionCount, BlockInfo->Is64BitMode, -@@ -866,6 +947,212 @@ struct CompileDepthScope { +@@ -872,6 +953,212 @@ struct CompileDepthScope { }; } // namespace @@ -437,15 +439,14 @@ index b11fae62d..4802fdd50 100644 uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_t GuestRIP, uint64_t MaxInst) { if constexpr (BLOCK_DEBUGGING) { // Block debugging logic is hand-written and needs to be handled with care. -@@ -902,9 +1189,20 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ - if (FEXCore::UncheckedLongJump::SetJump(Thread->CompileRestartJump)) { +@@ -909,8 +1196,19 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ Thread->FrontendDecoder->ResetExecutableRangeCache(); } -+ + + if (IsVolatileCode(GuestRIP)) { + return CompileVolatileBlock(Thread, GuestRIP); + } - ++ Thread->FrontendDecoder->SetupDecodeInstructionsAtEntry(Thread, GuestRIP, MaxInst); + @@ -458,7 +459,7 @@ index b11fae62d..4802fdd50 100644 std::optional Region = SyscallHandler->LookupExecutableFileSection(Thread, GuestRIP); std::optional Hit; std::optional DiskCacheGuestCodeKey; -@@ -1040,6 +1338,19 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ +@@ -1046,6 +1344,19 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_ Thread->LookupCache->AddBlockMapping(Thread, GuestAddr, CodePages, HostAddr); } @@ -478,7 +479,7 @@ index b11fae62d..4802fdd50 100644 // Clear any relocations that might have been generated if (!CodeCache.IsGeneratingCache) { Thread->CPUBackend->ClearRelocations(); -@@ -1102,7 +1413,14 @@ void ContextImpl::InvalidateThreadCachedCodeRange(FEXCore::Core::InternalThreadS +@@ -1108,7 +1419,14 @@ void ContextImpl::InvalidateThreadCachedCodeRange(FEXCore::Core::InternalThreadS // Accessing FrontendDecoder is safe as the thread's code invalidation mutex must be locked here. Thread->FrontendDecoder->ResetExecutableRangeCache(); @@ -494,57 +495,11 @@ index b11fae62d..4802fdd50 100644 FEXCORE_PROFILE_SCOPED("InvalidateCallRet"); // This may cause access violations in the thread on Windows as zeroing is not atomic, this is handled by the frontend -diff --git a/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp b/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp -index df48ca153..aa03d362e 100644 ---- a/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp -+++ b/FEXCore/Source/Interface/Core/Dispatcher/Dispatcher.cpp -@@ -404,6 +404,13 @@ void Dispatcher::EmitDispatcher() { - - SpillStaticRegs(TMP1); - -+ // Patch 0017: a frontend may take this guest fault directly (no host trap) through a per-thread pointer. -+ ldr(TMP1, STATE, offsetof(FEXCore::Core::CpuStateFrame, Pointers.GuestSignalTrapNative)); -+ ARMEmitter::ForwardLabel NoNativeSIGILL; -+ (void)cbz(ARMEmitter::Size::i64Bit, TMP1, &NoNativeSIGILL); -+ br(TMP1); -+ (void)Bind(&NoNativeSIGILL); -+ - hlt(0); - } - -@@ -414,6 +421,13 @@ void Dispatcher::EmitDispatcher() { - - SpillStaticRegs(TMP1); - -+ // Patch 0017: a frontend may take this guest fault directly (no host trap) through a per-thread pointer. -+ ldr(TMP1, STATE, offsetof(FEXCore::Core::CpuStateFrame, Pointers.GuestSignalTrapNative)); -+ ARMEmitter::ForwardLabel NoNativeSIGTRAP; -+ (void)cbz(ARMEmitter::Size::i64Bit, TMP1, &NoNativeSIGTRAP); -+ br(TMP1); -+ (void)Bind(&NoNativeSIGTRAP); -+ - brk(0); - } - -@@ -424,6 +438,13 @@ void Dispatcher::EmitDispatcher() { - - SpillStaticRegs(TMP1); - -+ // Patch 0017: a frontend may take this guest fault directly (no host trap) through a per-thread pointer. -+ ldr(TMP1, STATE, offsetof(FEXCore::Core::CpuStateFrame, Pointers.GuestSignalTrapNative)); -+ ARMEmitter::ForwardLabel NoNativeSIGSEGV; -+ (void)cbz(ARMEmitter::Size::i64Bit, TMP1, &NoNativeSIGSEGV); -+ br(TMP1); -+ (void)Bind(&NoNativeSIGSEGV); -+ - // hlt/udf = SIGILL - // brk = SIGTRAP - // ??? = SIGSEGV diff --git a/FEXCore/Source/Interface/Core/JIT/JIT.cpp b/FEXCore/Source/Interface/Core/JIT/JIT.cpp -index ef7e33299..58b2fe461 100644 +index 551dc1a08..dd647cfa9 100644 --- a/FEXCore/Source/Interface/Core/JIT/JIT.cpp +++ b/FEXCore/Source/Interface/Core/JIT/JIT.cpp -@@ -584,6 +584,10 @@ uint64_t Arm64JITCore::ExitFunctionLink(FEXCore::Core::CpuStateFrame* Frame, FEX +@@ -558,6 +558,10 @@ uint64_t Arm64JITCore::ExitFunctionLink(FEXCore::Core::CpuStateFrame* Frame, FEX // Hold a reference to the code buffer, to avoid linking unmapped code if compilation triggers a recreation. auto CodeBuffer = static_cast(Thread->CPUBackend.get())->CurrentCodeBuffer; HostCode = static_cast(Thread->CTX)->CompileBlock(Frame, GuestRip, 0); @@ -615,18 +570,6 @@ index 8aa448e2e..3524bcede 100644 ///< State reconstruction helpers ///< Reconstructs the guest RIP from the passed in thread context and related Host PC. FEX_DEFAULT_VISIBILITY virtual uint64_t RestoreRIPFromHostPC(FEXCore::Core::InternalThreadState* Thread, uint64_t HostPC) = 0; -diff --git a/FEXCore/include/FEXCore/Core/CoreState.h b/FEXCore/include/FEXCore/Core/CoreState.h -index 80514bfdc..e27507efe 100644 ---- a/FEXCore/include/FEXCore/Core/CoreState.h -+++ b/FEXCore/include/FEXCore/Core/CoreState.h -@@ -375,6 +375,7 @@ struct JITPointers { - uint64_t ThreadDispatchRemoveCodeEntry {}; - uint64_t GuestSignal_SIGILL {}; - uint64_t GuestSignal_SIGTRAP {}; -+ uint64_t GuestSignalTrapNative {}; // Patch 0017: when set, the guest-signal stubs branch here instead of trapping - uint64_t GuestSignal_SIGSEGV {}; - uint64_t SignalReturnHandler {}; - uint64_t SignalReturnHandlerRT {}; diff --git a/FEXCore/include/FEXCore/Debug/InternalThreadState.h b/FEXCore/include/FEXCore/Debug/InternalThreadState.h index 7d6cb2018..935c7cb73 100644 --- a/FEXCore/include/FEXCore/Debug/InternalThreadState.h @@ -644,7 +587,7 @@ index 7d6cb2018..935c7cb73 100644 uint64_t JITGuardOverflowArgument {}; FEXCore::UncheckedLongJump::JumpBuf RestartJump; diff --git a/Source/Windows/ARM64EC/Module.S b/Source/Windows/ARM64EC/Module.S -index ce36c1d68..bf5ee5b34 100644 +index 4d9fd8b7e..0ceed511b 100644 --- a/Source/Windows/ARM64EC/Module.S +++ b/Source/Windows/ARM64EC/Module.S @@ -147,6 +147,32 @@ DIRECT_SYSCALL_WRAPPER("#NtRaiseExceptionNative", WineNtRaiseExceptionSyscallId) @@ -681,7 +624,7 @@ index ce36c1d68..bf5ee5b34 100644 // A replacement for the standard ARM64EC call checker that ignores any FFS patches and always redirects to a function's // native implementation. As the only library FEX calls into is NTDLL, this is done using a LUT generated at init time. diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index ab908b0fa..3e7744cce 100644 +index 4b8149bf3..8914f2c35 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -91,8 +91,27 @@ uint64_t WineNtContinueSyscallId; @@ -712,7 +655,7 @@ index ab908b0fa..3e7744cce 100644 NTSTATUS NtAllocateVirtualMemoryNative(HANDLE, PVOID*, ULONG_PTR, SIZE_T*, ULONG, ULONG); NTSTATUS NtProtectVirtualMemoryNative(HANDLE, PVOID*, SIZE_T*, ULONG, ULONG*); NTSTATUS NtRaiseExceptionNative(EXCEPTION_RECORD*, ARM64_NT_CONTEXT*, BOOL); -@@ -268,6 +287,8 @@ void ParseWineSyscallNumbers(HMODULE NtDll) { +@@ -270,6 +289,8 @@ void ParseWineSyscallNumbers(HMODULE NtDll) { WineNtProtectVirtualMemorySyscallId = CurSyscallId; } else if (strcmp(it->Name, "NtRaiseException") == 0) { WineNtRaiseExceptionSyscallId = CurSyscallId; @@ -721,7 +664,7 @@ index ab908b0fa..3e7744cce 100644 } } } -@@ -324,6 +345,7 @@ bool InitSyscalls() { +@@ -326,6 +347,7 @@ bool InitSyscalls() { const auto WineSyscallDispatcherPtr = reinterpret_cast(GetProcAddress(NtDll, "__wine_syscall_dispatcher")); if (WineSyscallDispatcherPtr) { @@ -729,7 +672,7 @@ index ab908b0fa..3e7744cce 100644 WineSyscallDispatcher = *WineSyscallDispatcherPtr; ParseWineSyscallNumbers(NtDll); } else { -@@ -571,6 +593,89 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea +@@ -655,6 +677,33 @@ static ARM64_NT_CONTEXT StoreStateToPackedECContext(FEXCore::Core::InternalThrea return ECContext; } @@ -760,66 +703,10 @@ index ab908b0fa..3e7744cce 100644 + NtRaiseExceptionNative(&GuestRec, &GuestContext, FirstChance); +} + -+struct FastTrapStatsType { -+ uint64_t Marker; // "FASTTRP1" -+ uint64_t Taken; -+ uint64_t Fallback; -+ uint64_t Ill, Trap, Segv; -+}; -+FastTrapStatsType FastTrapStats = {0x3150525454534146ULL, 0, 0, 0, 0, 0}; // "FASTTRP1" -+static bool FastTrapEnabled = [] { const char* E = getenv("FEX_EXP_FASTTRAP"); return !(E && E[0] == '0'); }(); -+static void FastRaiseOrNative(EXCEPTION_RECORD& GuestRec, ARM64_NT_CONTEXT& GuestContext, BOOL FirstChance); -+ -+// Patch 0017: entered from FEXCore's guest-signal stubs (SIGILL/SIGTRAP/SIGSEGV breaks of JIT code: ud2, int3, int n, -+// trap-flag steps) with the static registers spilled, on the emulator stack, instead of a hlt/brk host trap. Builds the -+// guest exception as RethrowGuestException does after that trap, without the trap, the signal handler and the first -+// KiUserExceptionDispatcher pass. FEX_EXP_FASTTRAP=0 turns it off. Never returns. -+extern "C" __attribute__((noreturn)) void FastTrapEntry() { -+ uint64_t FPCR, FPSR; -+ __asm volatile("mrs %[fpcr], fpcr" : [fpcr] "=r"(FPCR)); -+ __asm volatile("mrs %[fpsr], fpsr" : [fpsr] "=r"(FPSR)); -+ auto* Thread = GetCPUArea().ThreadState(); -+ auto& Fault = Thread->CurrentFrame->SynchronousFaultData; -+ if (Fault.Signal == FEXCore::Core::FAULT_SIGILL) { -+ __atomic_fetch_add(&FastTrapStats.Ill, 1, __ATOMIC_RELAXED); -+ } else if (Fault.Signal == FEXCore::Core::FAULT_SIGTRAP) { -+ __atomic_fetch_add(&FastTrapStats.Trap, 1, __ATOMIC_RELAXED); -+ } else { -+ __atomic_fetch_add(&FastTrapStats.Segv, 1, __ATOMIC_RELAXED); -+ } -+ ARM64_NT_CONTEXT GuestContext = StoreStateToPackedECContext(Thread, static_cast(FPCR), static_cast(FPSR)); -+ -+ // As RethrowGuestException: x64 clears TF when an exception is delivered. -+ uint32_t EFlags = CTX->ReconstructCompactedEFLAGS(Thread, false, nullptr, 0); -+ EFlags &= ~(1 << FEXCore::X86State::RFLAG_TF_RAW_LOC); -+ CTX->SetFlagsFromCompactedEFLAGS(Thread, EFlags); -+ -+ // The exception the stub's host trap would have raised (hlt: SIGILL, brk: SIGTRAP, a null load: SIGSEGV). -+ // HandleGuestException keeps this code for execute faults, so it must match the trap. -+ EXCEPTION_RECORD HostRec {}; -+ if (Fault.Signal == FEXCore::Core::FAULT_SIGILL) { -+ HostRec.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION; -+ } else if (Fault.Signal == FEXCore::Core::FAULT_SIGTRAP) { -+ HostRec.ExceptionCode = EXCEPTION_BREAKPOINT; -+ } else { -+ HostRec.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; -+ } -+ BOOL FirstChance = TRUE; -+ EXCEPTION_RECORD GuestRec = FEX::Windows::HandleGuestException(Fault, HostRec, GuestContext.Pc, GuestContext.X8, GuestContext.X0, FirstChance); -+ if (GuestRec.ExceptionCode == EXCEPTION_SINGLE_STEP) { -+ GuestContext.Cpsr &= ~(1 << 21); // PSTATE.SS -+ } else if (GuestRec.ExceptionCode == EXCEPTION_BREAKPOINT) { -+ GuestContext.Pc -= 1; -+ } -+ __atomic_fetch_add(&FastTrapStats.Taken, 1, __ATOMIC_RELAXED); -+ FastRaiseOrNative(GuestRec, GuestContext, FirstChance); -+ __builtin_unreachable(); -+} -+ - static void RethrowGuestException(const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& Context) { - auto* Thread = GetCPUArea().ThreadState(); + static void RaiseException(FEXCore::Core::InternalThreadState* Thread, const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& GuestContext) { auto& Fault = Thread->CurrentFrame->SynchronousFaultData; -@@ -598,7 +703,15 @@ static void RethrowGuestException(const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& + +@@ -674,7 +723,11 @@ static void RaiseException(FEXCore::Core::InternalThreadState* Thread, const EXC GuestContext.Pc -= 1; } @@ -828,15 +715,11 @@ index ab908b0fa..3e7744cce 100644 + // KiUserExceptionDispatcher directly, as HandleSyscall does, instead of the unix NtRaiseException. A debugger does + // not see the first-chance event; the old path stays as the fallback when the guest stack cannot take the frame. + // FEX_EXP_FASTRAISE=0 turns it off. -+ FastRaiseTo(GuestRec, GuestContext, FirstChance); -+} -+ -+static void FastRaiseOrNative(EXCEPTION_RECORD& GuestRec, ARM64_NT_CONTEXT& GuestContext, BOOL FirstChance) { + FastRaiseTo(GuestRec, GuestContext, FirstChance); } - // A NoExec stub is compiled when a block entry is not executable at compile time. If another thread makes the page -@@ -758,6 +871,197 @@ public: + static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& HostContext) { +@@ -900,6 +953,197 @@ public: }; } // namespace Exception @@ -1034,7 +917,7 @@ index ab908b0fa..3e7744cce 100644 extern "C" void SyncThreadContext(CONTEXT* Context) { ProcessPendingCrossProcessEmulatorWork(); auto* Thread = GetCPUArea().ThreadState(); -@@ -836,6 +1140,30 @@ NTSTATUS ProcessInit() { +@@ -978,6 +1222,30 @@ NTSTATUS ProcessInit() { PAGE_EXECUTE_READ); *reinterpret_cast(X64ReturnInstr) = 0xc3; @@ -1065,14 +948,6 @@ index ab908b0fa..3e7744cce 100644 const uintptr_t KiUserExceptionDispatcherFFS = reinterpret_cast(GetProcAddress(NtDll, "KiUserExceptionDispatcher")); Exception::KiUserExceptionDispatcher = NtDllRedirectionLUT[KiUserExceptionDispatcherFFS - NtDllBase] + NtDllBase; -@@ -1199,6 +1527,7 @@ NTSTATUS ThreadInit() { - - FEX::Windows::CallRetStack::InitializeThread(Thread); - Thread->CurrentFrame->Pointers.ExitFunctionEC = reinterpret_cast(&ExitFunctionEC); -+ Thread->CurrentFrame->Pointers.GuestSignalTrapNative = Exception::FastTrapEnabled ? reinterpret_cast(&Exception::FastTrapEntry) : 0; - CPUArea.StateFrame() = Thread->CurrentFrame; - - uint64_t EnterEC = Thread->CurrentFrame->Pointers.DispatcherLoopTopEnterEC; diff --git a/Source/Windows/Common/InvalidationTracker.cpp b/Source/Windows/Common/InvalidationTracker.cpp index b9cfb25ff..304f29e8e 100644 --- a/Source/Windows/Common/InvalidationTracker.cpp diff --git a/gamenative/patches/0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch b/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch similarity index 96% rename from gamenative/patches/0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch rename to gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch index 413495fee9..59d2bc48df 100644 --- a/gamenative/patches/0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch +++ b/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch @@ -26,10 +26,10 @@ index d57603079..67f351139 100644 void STDMETHODCALLTYPE FlushInstructionCacheHeavy(const void* Address, SIZE_T Size); void STDMETHODCALLTYPE BTCpu64FlushInstructionCache(const void* Address, SIZE_T Size); diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp -index 56379dcb3..a221b737f 100644 +index 8914f2c35..1fcea15d2 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp -@@ -1365,6 +1365,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, +@@ -1519,6 +1519,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, } } diff --git a/gamenative/patches/0005-windows-make-volatile-code-regions-opt-in.patch b/gamenative/patches/0004-windows-make-volatile-code-regions-opt-in.patch similarity index 100% rename from gamenative/patches/0005-windows-make-volatile-code-regions-opt-in.patch rename to gamenative/patches/0004-windows-make-volatile-code-regions-opt-in.patch diff --git a/gamenative/patches/0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch b/gamenative/patches/0005-fexcore-fence-plain-loads-before-volatile-metada.patch similarity index 94% rename from gamenative/patches/0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch rename to gamenative/patches/0005-fexcore-fence-plain-loads-before-volatile-metada.patch index 0a16bf8c70..e5daa2a73f 100644 --- a/gamenative/patches/0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch +++ b/gamenative/patches/0005-fexcore-fence-plain-loads-before-volatile-metada.patch @@ -24,7 +24,7 @@ same code under Rosetta runs clean. 2 files changed, 19 insertions(+) diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp -index b0b4ad35b..5c79b2460 100644 +index 3c6101205..65c21f887 100644 --- a/FEXCore/Source/Interface/Core/Core.cpp +++ b/FEXCore/Source/Interface/Core/Core.cpp @@ -669,6 +669,8 @@ ContextImpl::GenerateIR(FEXCore::Core::InternalThreadState* Thread, uint64_t Gue @@ -37,10 +37,10 @@ index b0b4ad35b..5c79b2460 100644 Thread->OpDispatcher->SetNewBlockIfChanged(Block.Entry); diff --git a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h -index 85919f294..d973a24c8 100644 +index 03c584d3e..ae44ecd40 100644 --- a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h +++ b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h -@@ -332,6 +332,9 @@ public: +@@ -337,6 +337,9 @@ public: ForceTSOMode GetForceTSO() const { return ForceTSO; } @@ -50,7 +50,7 @@ index 85919f294..d973a24c8 100644 void SetDumpIR(bool DumpIR) { ShouldDump = DumpIR; -@@ -1362,6 +1365,7 @@ private: +@@ -1393,6 +1396,7 @@ private: bool DecodeFailure {false}; bool NeedsBlockEnd {false}; ForceTSOMode ForceTSO {ForceTSOMode::NoOverride}; @@ -58,7 +58,7 @@ index 85919f294..d973a24c8 100644 // Used during new op bringup bool ShouldDump {false}; -@@ -2482,6 +2486,16 @@ private: +@@ -2578,6 +2582,16 @@ private: bool IsMonoBackpatcherBlock {false}; IROp_IRHeader* CurrentHeader {}; @@ -75,7 +75,7 @@ index 85919f294..d973a24c8 100644 [[nodiscard]] bool IsTSOEnabled(RegClass Class) const { if (ForceTSO == ForceTSOMode::ForceEnabled) { -@@ -2510,6 +2524,7 @@ private: +@@ -2606,6 +2620,7 @@ private: } Ref _LoadMemAutoTSO(RegClass Class, OpSize Size, Ref ssa0, OpSize Align = OpSize::i8Bit) { @@ -83,7 +83,7 @@ index 85919f294..d973a24c8 100644 if (IsTSOEnabled(Class)) { return _LoadMemTSO(Class, Size, ssa0, Invalid(), Align, MemOffsetType::SXTX, 1); } else { -@@ -2525,6 +2540,7 @@ private: +@@ -2621,6 +2636,7 @@ private: Ref _LoadMemAutoTSO(RegClass Class, OpSize Size, const AddressMode& A, OpSize Align = OpSize::i8Bit) { const bool AtomicTSO = IsTSOEnabled(Class) && !A.NonTSO; @@ -91,7 +91,7 @@ index 85919f294..d973a24c8 100644 const auto B = SelectAddressMode(this, A, GetGPROpSize(), CTX->HostFeatures.SupportsTSOImm9, AtomicTSO, Class != RegClass::GPR, Size); if (AtomicTSO) { -@@ -2570,6 +2586,7 @@ private: +@@ -2666,6 +2682,7 @@ private: // Use ldp if possible, otherwise fallback on two loads. if (!AtomicTSO && !A.Segment && Size >= OpSize::i32Bit && Size <= OpSize::i128Bit) { diff --git a/gamenative/patches/0006-fexofflinecompiler-load-the-config-before-initia.patch b/gamenative/patches/0006-fexofflinecompiler-load-the-config-before-initia.patch new file mode 100644 index 0000000000..f9a081ae20 --- /dev/null +++ b/gamenative/patches/0006-fexofflinecompiler-load-the-config-before-initia.patch @@ -0,0 +1,29 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:27:57 +0530 +Subject: [PATCH] FEXOfflineCompiler: load the config before initializing + logging on Windows + +FEX::Windows::Logging::Init reads SilentLog and OutputLog from the +config layers. The offline compiler initialized logging before any +config was loaded, so all log output was dropped regardless of the +user configuration. +--- + Source/Tools/FEXOfflineCompiler/Main.cpp | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/Source/Tools/FEXOfflineCompiler/Main.cpp b/Source/Tools/FEXOfflineCompiler/Main.cpp +index e28a295d0..0cbb97cea 100644 +--- a/Source/Tools/FEXOfflineCompiler/Main.cpp ++++ b/Source/Tools/FEXOfflineCompiler/Main.cpp +@@ -844,6 +844,10 @@ int main(int argc, char** argv) { + LogMan::Throw::InstallHandler(AssertHandler); + LogMan::Msg::InstallHandler(MsgHandler); + #else ++ { ++ char* envp[] = {nullptr}; ++ FEX::Config::LoadConfig("", envp, FEX::ReadPortabilityInformation()); ++ } + FEX::Windows::Logging::Init(); + #endif + diff --git a/gamenative/patches/0007-codecache-rebase-relocation-offsets-to-the-code-.patch b/gamenative/patches/0007-codecache-rebase-relocation-offsets-to-the-code-.patch new file mode 100644 index 0000000000..d7cf96d33c --- /dev/null +++ b/gamenative/patches/0007-codecache-rebase-relocation-offsets-to-the-code-.patch @@ -0,0 +1,128 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:28:42 +0530 +Subject: [PATCH] CodeCache: rebase relocation offsets to the code buffer when + serializing + +Since ad3939a44 ("Avoid double offset relocations") relocation offsets +are relative to the start of their compile unit. That works for the +disk cache, which stores one unit at a time, but code cache generation +and validation accumulate relocations across all units of a binary +and apply them to the whole code buffer. + +The result was that every unit's relocations were applied to the first +few pages of the buffer: generated caches kept the host address of the +ExitFunctionLinker and unrelocated guest addresses in all but the first +unit, the relocation list was no longer sorted (breaking the per-page +grouping in LoadCache), and loading patched garbage into the first +pages. + +Rebase the offsets by the unit's code buffer offset when the JIT +compiles for cache generation and when Validate compiles reference +code, sort the serialized list by offset, and let the lazy finalization +path on Linux pass the offset of its staging window. +--- + FEXCore/Source/Interface/Context/Context.h | 4 +++- + FEXCore/Source/Interface/Core/CodeCache.cpp | 19 ++++++++++++++----- + FEXCore/Source/Interface/Core/JIT/JIT.cpp | 7 +++++++ + 3 files changed, 24 insertions(+), 6 deletions(-) + +diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h +index 01b1f8133..d8834c6ef 100644 +--- a/FEXCore/Source/Interface/Context/Context.h ++++ b/FEXCore/Source/Interface/Context/Context.h +@@ -122,11 +122,13 @@ public: + * + * @param GuestDelta Guest address offset to apply to RIP-relative data + * @param ForStorage True for serializing data (producing deterministic output); false for de-serializing it (resolving dynamic symbols) ++ * @param CodeOffset Offset of Code within the buffer that relocation offsets are relative to + * + * @return Returns true on success + */ + [[nodiscard]] +- bool ApplyCodeRelocations(uint64_t GuestDelta, std::span Code, std::span Relocations, bool ForStorage); ++ bool ApplyCodeRelocations(uint64_t GuestDelta, std::span Code, std::span Relocations, bool ForStorage, ++ uint64_t CodeOffset = 0); + + // Same but on disk cache packed relocations + [[nodiscard]] +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 87a110c84..7ddcf1899 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -298,6 +298,8 @@ bool CodeCache::SaveData(Core::InternalThreadState& Thread, int fd, const Execut + auto CodeBuffer = CTX.GetLatest(); + auto& LookupCache = *Thread.LookupCache->Shared; + auto Relocations = Thread.CPUBackend->TakeRelocations(SourceBinary.FileStartVA); ++ // Loading groups relocations by code page, which requires them to be sorted by offset ++ std::ranges::stable_sort(Relocations, std::less {}, [](const CPU::Relocation& Reloc) { return Reloc.Header.Offset; }); + + // Write file header + CodeCacheHeader header {}; +@@ -425,15 +427,21 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setGetBufferBase(), NewCodeBuffer->GetBufferBase() + NewCodeBuffer->UsableSize()}) + .subspan(0, CachedCode.size_bytes()); + ++ fextl::vector NewRelocations; + while (!GuestBlocks.empty()) { + auto [CompiledBlocks, _, _2, _3, _4] = ValidationCTX->CompileCode(ValidationThread.get(), *GuestBlocks.begin(), 0 /* TODO: Set MaxInst? */); + for (auto& Entry : CompiledBlocks.EntryPoints) { + GuestBlocks.erase(Entry.first); + } ++ ++ // Relocation offsets are relative to their compile unit; rebase them to the CodeBuffer start ++ for (auto& Reloc : ValidationThread->CPUBackend->TakeRelocations(Section.FileStartVA)) { ++ Reloc.Header.Offset += CompiledBlocks.HostCodeOffset; ++ NewRelocations.push_back(Reloc); ++ } + } + + // Patch FEX-internal function addresses with values from the main Context to ensure the code blocks are comparable +- auto NewRelocations = ValidationThread->CPUBackend->TakeRelocations(Section.FileStartVA); + NewRelocations.erase(std::remove_if(NewRelocations.begin(), NewRelocations.end(), [](const CPU::Relocation& Reloc) { + return Reloc.Header.Type != CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL && Reloc.Header.Type != CPU::RelocationTypes::RELOC_NAMED_THUNK_MOVE; + })); +@@ -616,12 +624,13 @@ bool CodeCache::ApplyPackedCodeRelocations(uint64_t GuestEntry, std::span Code, +- std::span EntryRelocations, bool ForStorage) { ++ std::span EntryRelocations, bool ForStorage, uint64_t CodeOffset) { + CPU::Arm64Emitter Emitter(&CTX, Code.data(), Code.size_bytes()); + for (size_t j = 0; j < EntryRelocations.size(); ++j) { + const FEXCore::CPU::Relocation& Reloc = EntryRelocations[j]; +- LOGMAN_THROW_A_FMT(Reloc.Header.Offset < Code.size_bytes(), "Invalid relocation offset"); +- Emitter.SetCursorOffset(Reloc.Header.Offset); ++ LOGMAN_THROW_A_FMT(Reloc.Header.Offset >= CodeOffset, "Invalid relocation offset"); ++ LOGMAN_THROW_A_FMT(Reloc.Header.Offset - CodeOffset < Code.size_bytes(), "Invalid relocation offset"); ++ Emitter.SetCursorOffset(Reloc.Header.Offset - CodeOffset); + + switch (Reloc.Header.Type) { + case FEXCore::CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL: { +@@ -970,7 +979,7 @@ void CodeCache::FinalizeCodePages(MappedCodeCacheFile& Code, std::spanGetCodeCache().IsGeneratingCache) { ++ for (auto& Relocation : std::span {Relocations}.subspan(PrevNumAllocations)) { ++ Relocation.Header.Offset += CodeData.HostCodeOffset; ++ } ++ } ++ + TempCodeBufferAllocator.DelayedDisownBuffer(); + + ClearICache(CodeBegin, CodeOnlySize); diff --git a/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch b/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch deleted file mode 100644 index 16e58bfc79..0000000000 --- a/gamenative/patches/0007-fexcore-keep-guest-loads-whose-value-is-unused.patch +++ /dev/null @@ -1,96 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Utkarsh Dalal -Date: Thu, 29 Oct 2026 17:55:38 +0530 -Subject: [PATCH] FEXCore: keep guest loads whose value is unused - -The dead-flag pass removes any node with no uses and no declared side -effects. A plain LoadMem has none declared, so a guest load that only -fed a flag calculation goes away with the flags: once `add [rsi+0x128],1` -overwrites every flag `cmp rax,[rdx]` produced, the compare is removed, -and then the load behind it. - -HotSpot relies on that load faulting. C1's explicit null check is -`cmp rax,[reg]` with the flags dead; the access violation at that PC is -what the JVM turns into a NullPointerException. With the load gone the -null receiver reaches the next field load, which faults at a PC that is -not in the nmethod's null-check table, and the VM aborts with -EXCEPTION_ACCESS_VIOLATION reading 0xc. Necesse (Temurin 17 x86-64) -dies this way 13 s after start in -necesse.engine.save.LoadData.getInt/getBoolean, an 8-byte method whose -inlined invokevirtual is guarded by exactly that probe; every hs_err -has the same registers (rdx = 0 from the interpreted caller, -rsi = 0x46e, the live profile counter) and the event log records -"Implicit null exception at to 0x0", the lookup that found no -continuation. C2 safepoint polls (`test rax,[poll]`) are the same shape. - -Declare the guest load ops as having side effects so the pass keeps -them; LoadMemPair already is. Loads that are truly dead stay in the -block, which compiled x86 rarely produces except as a probe. - -A stand-alone probe (static method calling a virtual on its argument, -null every 5000th call, -XX:TieredStopAtLevel=3) reproduces the abort -on the stock build and passes with the loads kept. ---- - FEXCore/Source/Interface/IR/IR.json | 7 +++++++ - 1 file changed, 7 insertions(+) - -diff --git a/FEXCore/Source/Interface/IR/IR.json b/FEXCore/Source/Interface/IR/IR.json -index 880520077..99cd57273 100644 ---- a/FEXCore/Source/Interface/IR/IR.json -+++ b/FEXCore/Source/Interface/IR/IR.json -@@ -530,6 +530,7 @@ - }, - - "SSA = LoadMem RegisterClass:$Class, OpSize:#Size, GPR:$Addr, GPR:$Offset, OpSize:$Align, MemOffsetType:$OffsetType, u8:$OffsetScale": { -+ "HasSideEffects": true, - "Inline": ["", "Mem"], - "DestSize": "Size" - }, -@@ -575,6 +576,7 @@ - }, - - "SSA = LoadMemTSO RegisterClass:$Class, OpSize:#Size, GPR:$Addr, GPR:$Offset, OpSize:$Align, MemOffsetType:$OffsetType, u8:$OffsetScale": { -+ "HasSideEffects": true, - "Desc": ["Does a x86 TSO compatible load from memory. Offset must be Invalid()." - ], - "Inline": ["", "Memtso"], -@@ -590,6 +592,7 @@ - }, - - "FPR = VLoadVectorMasked OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Mask, GPR:$Addr, GPR:$Offset, MemOffsetType:$OffsetType, u8:$OffsetScale": { -+ "HasSideEffects": true, - "Desc": ["Does a masked load similar to VPMASKMOV/VMASKMOV where the upper bit of each element", - "determines whether or not that element will be loaded from memory"], - "ImplicitFlagClobber": true, -@@ -605,6 +608,7 @@ - "ElementSize": "ElementSize" - }, - "FPR = VLoadVectorGatherMasked OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Incoming, FPR:$Mask, GPR:$AddrBase, FPR:$VectorIndexLow, FPR:$VectorIndexHigh, OpSize:$VectorIndexElementSize, u8:$OffsetScale, u8:$DataElementOffsetStart, u8:$IndexElementOffsetStart, OpSize:$AddrSize, i1:$MaskAllOnes{false}": { -+ "HasSideEffects": true, - "Desc": [ - "Does a masked load similar to VPGATHERD* where the upper bit of each element", - "determines whether or not that element will be loaded from memory.", -@@ -620,6 +624,7 @@ - ] - }, - "FPR = VLoadVectorGatherMaskedQPS OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$Incoming, FPR:$MaskReg, GPR:$AddrBase, FPR:$VectorIndexLow, FPR:$VectorIndexHigh, u8:$OffsetScale, OpSize:$AddrSize, i1:$MaskAllOnes{false}": { -+ "HasSideEffects": true, - "Desc": [ - "Does a masked load similar to VPGATHERQPS where the upper bit of each element", - "determines whether or not that element will be loaded from memory.", -@@ -637,6 +642,7 @@ - ] - }, - "FPR = VLoadVectorElement OpSize:#RegisterSize, OpSize:#ElementSize, FPR:$DstSrc, u8:$Index, GPR:$Addr": { -+ "HasSideEffects": true, - "Desc": ["Does a memory load to a single element of a vector.", - "Leaves the rest of the vector's data intact.", - "Matches arm64 ld1 semantics"], -@@ -776,6 +782,7 @@ - ] - }, - "FPR = VLoadNonTemporal OpSize:#RegisterSize, GPR:$Addr, i8:$Offset": { -+ "HasSideEffects": true, - "Desc": ["Does a non-temporal memory load of a vector.", - "Matches arm64 SVE ldnt1b semantics.", - "Specifically weak-memory model ordered to match x86 non-temporal stores." diff --git a/gamenative/patches/0008-fexofflinecompiler-quote-process-all-child-argum.patch b/gamenative/patches/0008-fexofflinecompiler-quote-process-all-child-argum.patch new file mode 100644 index 0000000000..8b42e4b8b5 --- /dev/null +++ b/gamenative/patches/0008-fexofflinecompiler-quote-process-all-child-argum.patch @@ -0,0 +1,71 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:29:24 +0530 +Subject: [PATCH] FEXOfflineCompiler: quote process-all child arguments on + Windows + +_spawnv joins argv with spaces and does not quote the elements, so the +generate subprocess split paths containing spaces (for example a code +map named after "Blazing Chrome.exe" or a cache directory under a +profile path with spaces) into several arguments and failed. + +Quote each argument following the CommandLineToArgvW rules before +spawning. +--- + Source/Tools/FEXOfflineCompiler/Main.cpp | 36 ++++++++++++++++++++++-- + 1 file changed, 34 insertions(+), 2 deletions(-) + +diff --git a/Source/Tools/FEXOfflineCompiler/Main.cpp b/Source/Tools/FEXOfflineCompiler/Main.cpp +index 0cbb97cea..5fe5b88ff 100644 +--- a/Source/Tools/FEXOfflineCompiler/Main.cpp ++++ b/Source/Tools/FEXOfflineCompiler/Main.cpp +@@ -387,6 +387,28 @@ static LONG ExceptionHandler(_EXCEPTION_POINTERS* ExceptionInfo) { + return EXCEPTION_CONTINUE_SEARCH; + } + ++// _spawnv joins its arguments with spaces without quoting them ++static std::string QuoteCommandLineArgument(std::string_view Arg) { ++ if (!Arg.empty() && Arg.find_first_of(" \t\n\v\"") == Arg.npos) { ++ return std::string {Arg}; ++ } ++ ++ std::string Quoted = "\""; ++ size_t NumBackslashes = 0; ++ for (char C : Arg) { ++ if (C == '\\') { ++ ++NumBackslashes; ++ continue; ++ } ++ Quoted.append(C == '"' ? NumBackslashes * 2 + 1 : NumBackslashes, '\\'); ++ Quoted += C; ++ NumBackslashes = 0; ++ } ++ Quoted.append(NumBackslashes * 2, '\\'); ++ Quoted += '"'; ++ return Quoted; ++} ++ + struct winsize { + int ws_col; + }; +@@ -827,8 +849,18 @@ static int ProcessAll() { + } + #else + GenerateArgs.insert(GenerateArgs.begin(), SelfPath.c_str()); +- GenerateArgs.push_back(nullptr); +- auto Status = _spawnv(_P_WAIT, SelfPath.c_str(), GenerateArgs.data()); ++ std::vector QuotedArgs; ++ QuotedArgs.reserve(GenerateArgs.size()); ++ for (auto* Arg : GenerateArgs) { ++ QuotedArgs.push_back(QuoteCommandLineArgument(Arg)); ++ } ++ std::vector SpawnArgs; ++ SpawnArgs.reserve(QuotedArgs.size() + 1); ++ for (auto& Arg : QuotedArgs) { ++ SpawnArgs.push_back(Arg.c_str()); ++ } ++ SpawnArgs.push_back(nullptr); ++ auto Status = _spawnv(_P_WAIT, SelfPath.c_str(), SpawnArgs.data()); + if (Status) { + fmt::println("ERROR: Cache generation failed for {}", BinaryName); + } diff --git a/gamenative/patches/0009-codecache-copy-cached-code-to-a-writable-buffer-.patch b/gamenative/patches/0009-codecache-copy-cached-code-to-a-writable-buffer-.patch new file mode 100644 index 0000000000..a8c3ac3ebb --- /dev/null +++ b/gamenative/patches/0009-codecache-copy-cached-code-to-a-writable-buffer-.patch @@ -0,0 +1,62 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:29:44 +0530 +Subject: [PATCH] CodeCache: copy cached code to a writable buffer on WoW64 + +ImageTracker maps code cache files with PAGE_EXECUTE_READ. Since +0ac6b3e8f only ARM64EC copies the cached code into a dedicated +allocation before applying relocations; WoW64 kept using the file view +as the execution buffer and FinalizeCodePages wrote relocations into +it, which raises an access violation in BTCpuProcessInit before the +first guest instruction whenever a cache is loaded. + +Use the VirtualAlloc'd copy on all Windows targets. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 12 ++++-------- + 1 file changed, 4 insertions(+), 8 deletions(-) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 7ddcf1899..67bdb8961 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -51,7 +51,7 @@ MappedCodeCacheFile::~MappedCodeCacheFile() { + if (!CodeBuffer.empty()) { + FEXCore::Allocator::munmap(CodeBuffer.data(), CodeBuffer.size_bytes()); + } +-#elif defined(_M_ARM64EC) ++#else + if (!CodeBuffer.empty()) { + FEXCore::Allocator::VirtualFree(CodeBuffer.data(), CodeBuffer.size_bytes()); + } +@@ -718,18 +718,16 @@ CodeCache::LoadCache(std::span CacheFile, const ExecutableFileInfo& F + return nullptr; + } + auto CodeBuffer = std::span {static_cast(CodeBufferAllocation), header.CodeBufferSize}; +-#elif defined(_M_ARM64EC) ++#else + // TODO: Implement lazy mapping on Windows +- // NOTE: The executed code must have MEM_EXTENDED_PARAMETER_EC_CODE set, so we can't operate on the mapped cache file directly ++ // NOTE: The cache file is mapped read-only, so relocations must be applied to a separate buffer. ++ // On ARM64EC, the executed code must also have MEM_EXTENDED_PARAMETER_EC_CODE set. + void* CodeBufferAllocation = Allocator::VirtualAlloc(header.CodeBufferSize, true); + if (!CodeBufferAllocation) { + LogMan::Msg::EFmt("Failed to allocate code cache memory"); + return nullptr; + } + auto CodeBuffer = std::span {reinterpret_cast(CodeBufferAllocation), header.CodeBufferSize}; +-#else // WoW64 +- // TODO: Implement lazy mapping on Windows +- auto CodeBuffer = CodeDataInFile; + #endif + + // Group relocations by page +@@ -997,9 +995,7 @@ void CodeCache::FinalizeCodePages(MappedCodeCacheFile& Code, std::span +Date: Wed, 28 Oct 2026 19:14:50 +0530 +Subject: [PATCH] WOW64: handle exceptions raised before the first thread is + set up + +Code caches for the main executable are loaded from BTCpuProcessInit, +before BTCpuThreadInit has stored a thread state in the TEB. With +FEX_ENABLECODECACHEVALIDATION set, Validate compiles the cached blocks +on a separate context, and CompileCode's FindBlock reads that context's +LookupCache, which is reserved without being committed. The resulting +access violation is meant to be resolved by the OvercommitTracker. + +BTCpuResetToConsistentStateImpl dereferences the thread state in +FEXCORE_PROFILE_ACCUMULATION before checking it, so the handler itself +faults and the process terminates without any log output. Since the +dereference comes first, the compiler also drops the later null checks. + +Handle the case without a thread state before touching it: resolve +overcommit faults and leave everything else to the regular dispatch. +--- + Source/Windows/WOW64/Module.cpp | 10 ++++++++++ + 1 file changed, 10 insertions(+) + +diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp +index 5660dcf39..d633c196a 100644 +--- a/Source/Windows/WOW64/Module.cpp ++++ b/Source/Windows/WOW64/Module.cpp +@@ -793,6 +793,16 @@ bool BTCpuResetToConsistentStateImpl(EXCEPTION_POINTERS* Ptrs) { + auto* Context = Ptrs->ContextRecord; + auto* Exception = Ptrs->ExceptionRecord; + auto Thread = GetTLS().ThreadState(); ++ ++ if (!Thread) { ++ // BTCpuProcessInit runs before any thread state exists, and loading code caches there can touch overcommitted memory ++ if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && OvercommitTracker && ++ OvercommitTracker->HandleAccessViolation(static_cast(Exception->ExceptionInformation[1]))) { ++ return true; ++ } ++ return false; ++ } ++ + FEXCORE_PROFILE_ACCUMULATION(Thread, AccumulatedSignalTime); + + if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { diff --git a/gamenative/patches/0011-codecache-keep-the-main-dispatcher-config-when-c.patch b/gamenative/patches/0011-codecache-keep-the-main-dispatcher-config-when-c.patch new file mode 100644 index 0000000000..b2e4ff0094 --- /dev/null +++ b/gamenative/patches/0011-codecache-keep-the-main-dispatcher-config-when-c.patch @@ -0,0 +1,35 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 19:10:47 +0530 +Subject: [PATCH] CodeCache: keep the main dispatcher config when creating the + validation context + +ValidationCTX shares the main context's SignalDelegator, and InitCore +replaces the delegator config with one describing the validation +context's dispatcher. Every later exception or suspend handled for the +main context then checks dispatcher ranges and resume addresses of a +dispatcher that never runs guest code. + +Save the config before initializing the validation context and restore +it afterwards. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 67bdb8961..d9ee36b44 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -395,9 +395,12 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setSetSignalDelegator(CTX.SignalDelegation); + ValidationCTX->SetSyscallHandler(CTX.SyscallHandler); + ValidationCTX->SetThunkHandler(CTX.ThunkHandler); ++ // InitCore points the shared SignalDelegator at the validation context's dispatcher; keep the main one ++ const auto SignalConfig = CTX.SignalDelegation->GetConfig(); + if (!ValidationCTX->InitCore()) { + ERROR_AND_DIE_FMT("Failed to create cache load validation context"); + } ++ CTX.SignalDelegation->SetConfig(SignalConfig); + + ValidationThread.reset(ValidationCTX->CreateThread(nullptr)); + diff --git a/gamenative/patches/0012-windows-log-the-code-cache-load-phases-and-pre-t.patch b/gamenative/patches/0012-windows-log-the-code-cache-load-phases-and-pre-t.patch new file mode 100644 index 0000000000..8830f1a512 --- /dev/null +++ b/gamenative/patches/0012-windows-log-the-code-cache-load-phases-and-pre-t.patch @@ -0,0 +1,162 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 19:15:01 +0530 +Subject: [PATCH] Windows: log the code cache load phases and pre-thread + exceptions + +A failure while loading a code cache from BTCpuProcessInit used to end +the process without output. Report each step of the load at info level +so a single run shows how far it got: + +- the mapped cache view, or the NTSTATUS when the section or view + cannot be created +- the writable copy of the cached code with its protection +- block and code page registration, finalization and validation +- completion of the load for each image + +On WOW64, also report exceptions taken before BTCpuThreadInit has set +up a thread state, and overcommit ranges committed in that window. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 14 ++++++ + Source/Windows/Common/ImageTracker.cpp | 53 ++++++++++++--------- + Source/Windows/WOW64/Module.cpp | 4 ++ + 3 files changed, 48 insertions(+), 23 deletions(-) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index d9ee36b44..1fbaa12c9 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -401,6 +401,7 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setSetConfig(SignalConfig); ++ LogMan::Msg::IFmt("Cache validation: context ready"); + + ValidationThread.reset(ValidationCTX->CreateThread(nullptr)); + +@@ -731,6 +732,13 @@ CodeCache::LoadCache(std::span CacheFile, const ExecutableFileInfo& F + return nullptr; + } + auto CodeBuffer = std::span {reinterpret_cast(CodeBufferAllocation), header.CodeBufferSize}; ++ { ++ MEMORY_BASIC_INFORMATION Info {}; ++ ::VirtualQuery(CodeBufferAllocation, &Info, sizeof(Info)); ++ LogMan::Msg::IFmt("Cache map: file {} ({:#x} bytes); code {:#x} bytes at file offset {:#x} copied to {} (protect {:#x}); {} relocations", ++ fmt::ptr(CacheFile.data()), CacheFile.size_bytes(), header.CodeBufferSize, CodeDataInFile.data() - CacheFile.data(), ++ CodeBufferAllocation, Info.Protect, header.NumRelocations); ++ } + #endif + + // Group relocations by page +@@ -846,6 +854,8 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + } + } + } ++ LogMan::Msg::IFmt("Cache load: registered {} blocks and {} code pages; host code {}-{}", BlockList.size(), Code.NumCodePages, ++ fmt::ptr(Code.CodeBuffer.data()), fmt::ptr(Code.CodeBuffer.data() + Code.CodeBuffer.size_bytes())); + + #ifndef _WIN32 + if (!EnableLazyCodeCaching || EnableCodeCacheValidation) { +@@ -854,7 +864,9 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + if (true) { + #endif + auto Range = SelectCodeRangeToFinalize(Code, 0, Code.CodeBuffer.size_bytes() / Utils::FEX_PAGE_SIZE); ++ LogMan::Msg::IFmt("Cache load: finalizing {} pages at {}", Range.size_bytes() / Utils::FEX_PAGE_SIZE, fmt::ptr(Range.data())); + FinalizeCodePages(Code, Range); ++ LogMan::Msg::IFmt("Cache load: finalized; guest base {:#x}", Code.GuestBase); + } + + if (EnableCodeCacheValidation) { +@@ -864,9 +876,11 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + HostBlocks.insert(Host.HostCode); + } + ++ LogMan::Msg::IFmt("Cache load: validating {} guest blocks", GuestBlocks.size()); + Validate(BinarySection, std::move(GuestBlocks), HostBlocks, Code.CodeBuffer); + } + ++ LogMan::Msg::IFmt("Cache load: enabled {}", BinarySection.FileInfo.Filename); + return true; + } + +diff --git a/Source/Windows/Common/ImageTracker.cpp b/Source/Windows/Common/ImageTracker.cpp +index c459c446f..fbe74eef2 100644 +--- a/Source/Windows/Common/ImageTracker.cpp ++++ b/Source/Windows/Common/ImageTracker.cpp +@@ -246,29 +246,36 @@ void ImageTracker::LoadAOTImages(MappedImageInfo& ImageInfo) { + ScopedHandle FileHandle; + if (NT_SUCCESS(NtOpenFile(&*FileHandle, GENERIC_READ | SYNCHRONIZE, &FileAttr, &IOSB, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT))) { + ScopedHandle SectionHandle; +- if (NT_SUCCESS(NtCreateSection(&*SectionHandle, SECTION_MAP_EXECUTE | SECTION_MAP_READ, nullptr, nullptr, PAGE_EXECUTE_READ, SEC_COMMIT, +- *FileHandle))) { +- void* LoadAddress = nullptr; +- SIZE_T MappedSize = 0; +- // TODO: Consider NtMapViewOfSectionEx with MEM_EXTENDED_PARAMETER_EC_CODE; +- // instead, we're eagerly copying all cache data to a dedicated buffer currently +- // MEM_EXTENDED_PARAMETER ECParam {}; +- // ECParam.Type = MemExtendedParameterAttributeFlags; +- // ECParam.ULong64 = MEM_EXTENDED_PARAMETER_EC_CODE; +- if (NT_SUCCESS(NtMapViewOfSection(*SectionHandle, NtCurrentProcess(), &LoadAddress, 0, 0, nullptr, &MappedSize, ViewUnmap, +- MEM_RESERVE | MEM_TOP_DOWN, PAGE_EXECUTE_READ))) { +- +- auto CacheSpan = std::span {static_cast(LoadAddress), MappedSize}; +- auto MappedCache = CTX.GetCodeCache().LoadCache(CacheSpan, ImageInfo.Info, ImageInfo.SectionInfo.FileStartVA); +- if (MappedCache) { +- CTX.GetCodeCache().RegisterMappedCodeBuffer(*MappedCache); +- AOTImages[ID] = {.CacheFile = std::move(MappedCache)}; +- LogMan::Msg::IFmt("Loaded cache: {}", ID); +- } else { +- NtUnmapViewOfSection(NtCurrentProcess(), LoadAddress); +- LogMan::Msg::EFmt("Failed to load cache: {}", ID); +- } +- } ++ NTSTATUS Status = NtCreateSection(&*SectionHandle, SECTION_MAP_EXECUTE | SECTION_MAP_READ, nullptr, nullptr, PAGE_EXECUTE_READ, SEC_COMMIT, ++ *FileHandle); ++ if (!NT_SUCCESS(Status)) { ++ LogMan::Msg::EFmt("Failed to create cache section for {}: {:#x}", ID, static_cast(Status)); ++ return; ++ } ++ void* LoadAddress = nullptr; ++ SIZE_T MappedSize = 0; ++ // TODO: Consider NtMapViewOfSectionEx with MEM_EXTENDED_PARAMETER_EC_CODE; ++ // instead, we're eagerly copying all cache data to a dedicated buffer currently ++ // MEM_EXTENDED_PARAMETER ECParam {}; ++ // ECParam.Type = MemExtendedParameterAttributeFlags; ++ // ECParam.ULong64 = MEM_EXTENDED_PARAMETER_EC_CODE; ++ Status = NtMapViewOfSection(*SectionHandle, NtCurrentProcess(), &LoadAddress, 0, 0, nullptr, &MappedSize, ViewUnmap, ++ MEM_RESERVE | MEM_TOP_DOWN, PAGE_EXECUTE_READ); ++ if (!NT_SUCCESS(Status)) { ++ LogMan::Msg::EFmt("Failed to map cache view for {}: {:#x}", ID, static_cast(Status)); ++ return; ++ } ++ LogMan::Msg::IFmt("Mapped cache view: {} at {} ({:#x} bytes)", ID, LoadAddress, MappedSize); ++ ++ auto CacheSpan = std::span {static_cast(LoadAddress), MappedSize}; ++ auto MappedCache = CTX.GetCodeCache().LoadCache(CacheSpan, ImageInfo.Info, ImageInfo.SectionInfo.FileStartVA); ++ if (MappedCache) { ++ CTX.GetCodeCache().RegisterMappedCodeBuffer(*MappedCache); ++ AOTImages[ID] = {.CacheFile = std::move(MappedCache)}; ++ LogMan::Msg::IFmt("Loaded cache: {}", ID); ++ } else { ++ NtUnmapViewOfSection(NtCurrentProcess(), LoadAddress); ++ LogMan::Msg::EFmt("Failed to load cache: {}", ID); + } + } + } +diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp +index d633c196a..57beb255e 100644 +--- a/Source/Windows/WOW64/Module.cpp ++++ b/Source/Windows/WOW64/Module.cpp +@@ -798,8 +798,12 @@ bool BTCpuResetToConsistentStateImpl(EXCEPTION_POINTERS* Ptrs) { + // BTCpuProcessInit runs before any thread state exists, and loading code caches there can touch overcommitted memory + if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && OvercommitTracker && + OvercommitTracker->HandleAccessViolation(static_cast(Exception->ExceptionInformation[1]))) { ++ LogMan::Msg::IFmt("Committed overcommit range at {:X} before thread setup", Exception->ExceptionInformation[1]); + return true; + } ++ LogMan::Msg::EFmt("Unhandled exception {:X} before thread setup: pc {:X}, info {:X} {:X}", Exception->ExceptionCode, Context->Pc, ++ Exception->NumberParameters > 0 ? Exception->ExceptionInformation[0] : 0, ++ Exception->NumberParameters > 1 ? Exception->ExceptionInformation[1] : 0); + return false; + } + diff --git a/gamenative/series b/gamenative/series index f6b75f7051..a4a95a30ed 100644 --- a/gamenative/series +++ b/gamenative/series @@ -1,7 +1,12 @@ 0001-windows-fixes-for-protected-launchers-that-decry.patch -0002-arm64ec-load-ntdllredirectionlutsize-as-32-bits-.patch -0003-windows-anti-tamper-stepping-and-fault-delivery-.patch -0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch -0005-windows-make-volatile-code-regions-opt-in.patch -0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch -0007-fexcore-keep-guest-loads-whose-value-is-unused.patch +0002-windows-anti-tamper-stepping-and-fault-delivery-.patch +0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch +0004-windows-make-volatile-code-regions-opt-in.patch +0005-fexcore-fence-plain-loads-before-volatile-metada.patch +0006-fexofflinecompiler-load-the-config-before-initia.patch +0007-codecache-rebase-relocation-offsets-to-the-code-.patch +0008-fexofflinecompiler-quote-process-all-child-argum.patch +0009-codecache-copy-cached-code-to-a-writable-buffer-.patch +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