diff --git a/gamenative/README.md b/gamenative/README.md index 3a40861ad8..52bd99c6c9 100644 --- a/gamenative/README.md +++ b/gamenative/README.md @@ -21,7 +21,7 @@ behaviour that is not a plain correctness fix is behind an env var. | Patch | From | What | Upstream | |---|---|---|---| -| 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 | +| 0001 windows-fixes-for-protected-launchers | Dmage22/FEX-D2 `d2r/clean` (11 commits squashed, plus 2d8b53d393) | 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, with `InSyscallCallback` set so the invalidation cannot re-enter the invalidation lock through `NotifyMemoryFree`), 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 | @@ -37,7 +37,7 @@ behaviour that is not a plain correctness fix is behind an env var. 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). +Left out of 0001: dmage22's log-level change (cosmetic), the entry-block NoExec log cap (238fe0a6b9), ba8346d046 (superseded by 2d8b53d393), and the next-page probe for full NoExec faults (ba05439d66: it changes the fault address the guest sees, on WoW64 too). 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 diff --git a/gamenative/SERIES_VERSION b/gamenative/SERIES_VERSION index 1e8b314962..7f8f011eb7 100644 --- a/gamenative/SERIES_VERSION +++ b/gamenative/SERIES_VERSION @@ -1 +1 @@ -6 +7 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 e8c2b9980d..6de506a066 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 @@ -19,19 +19,25 @@ op carries the first byte that could not be fetched), so the VirtualQuery heuris 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. + +Later d2r/clean fix for Battle.net launchers (2d8b53d393): SynchronousExceptionHandler sets +InSyscallCallback around the stale NoExec retry and clears it before re-entering the dispatcher +or raising. The fault stub reaches it with the flag clear, so the lookup cache decommit in the +invalidation went to NotifyMemoryFree, which took the code invalidation mutex a second time on +the same thread and hung the process. --- 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 ++++--- + .../Interface/Core/OpcodeDispatcher.cpp | 72 +++--- .../Source/Interface/Core/OpcodeDispatcher.h | 2 + .../FEXCore/Debug/InternalThreadState.h | 13 ++ Source/Windows/ARM64EC/Module.S | 9 + - Source/Windows/ARM64EC/Module.cpp | 204 +++++++++++++++++- + Source/Windows/ARM64EC/Module.cpp | 211 +++++++++++++++++- Source/Windows/Common/Exception.h | 8 +- Source/Windows/Common/InvalidationTracker.cpp | 119 +++++++++- - 11 files changed, 439 insertions(+), 46 deletions(-) + 11 files changed, 446 insertions(+), 46 deletions(-) diff --git a/FEXCore/Source/Interface/Core/Core.cpp b/FEXCore/Source/Interface/Core/Core.cpp index 2ba56b69a..53d81a872 100644 @@ -369,7 +375,7 @@ index ce36c1d68..4d9fd8b7e 100644 + 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 3be7e465a..4b8149bf3 100644 +index 3be7e465a..2af511082 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -50,6 +50,7 @@ $end_info$ @@ -455,7 +461,7 @@ index 3be7e465a..4b8149bf3 100644 ECContext.Fpcr = FPCR; ECContext.Fpsr = FPSR; -@@ -657,12 +688,125 @@ static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, co +@@ -657,12 +688,132 @@ static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, co RaiseException(Thread, Rec, GuestContext); } @@ -573,15 +579,22 @@ index 3be7e465a..4b8149bf3 100644 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(); ++ // The fault stub branches here from JIT code with InSyscallCallback clear. Set it like the dispatcher's guarded ++ // calls and the host exception path do: the invalidation decommits lookup cache pages with the code invalidation ++ // mutex held, and without the flag ntdll reports that to NotifyMemoryFree, which takes the mutex again and hangs. ++ // Clear it again before either exit, since both run guest-visible code: the JIT, or the guest exception dispatch. ++ const auto CPUArea = GetCPUArea(); ++ CPUArea.Area->InSyscallCallback = true; ++ const bool StaleNoExec = DropStaleNoExecFault(Thread); ++ CPUArea.Area->InSyscallCallback = false; ++ if (StaleNoExec) { + 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: +@@ -730,7 +881,24 @@ public: } FEXCore::HLE::ExecutableRangeInfo QueryGuestExecutableRange(FEXCore::Core::InternalThreadState* Thread, uint64_t Address) override { @@ -607,7 +620,7 @@ index 3be7e465a..4b8149bf3 100644 } void PreCompile() override { -@@ -744,9 +905,13 @@ extern "C" void SyncThreadContext(CONTEXT* Context) { +@@ -744,9 +912,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. @@ -624,7 +637,7 @@ index 3be7e465a..4b8149bf3 100644 uint32_t StateEFlags = CTX->ReconstructCompactedEFLAGS(Thread, false, nullptr, 0); Context->EFlags = (Context->EFlags & ECValidEFlagsMask) | (StateEFlags & ~ECValidEFlagsMask); -@@ -777,6 +942,7 @@ NTSTATUS ProcessInit() { +@@ -777,6 +949,7 @@ NTSTATUS ProcessInit() { const auto NtDll = GetModuleHandleW(L"ntdll.dll"); const bool IsWine = !!GetProcAddress(NtDll, "wine_get_version"); @@ -632,7 +645,7 @@ index 3be7e465a..4b8149bf3 100644 OvercommitTracker.emplace(IsWine); FEX::Windows::SetupEnvironmentVariableValues(NtDll); -@@ -876,6 +1042,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -876,6 +1049,13 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -646,7 +659,7 @@ index 3be7e465a..4b8149bf3 100644 std::scoped_lock Lock(ThreadCreationMutex); if (InvalidationTracker && InvalidationTracker->HandleRWXAccessViolation(Thread, NativeContext->Pc, FaultAddress)) { FEXCORE_PROFILE_INSTANT_INCREMENT(Thread, AccumulatedSMCCount, 1); -@@ -919,6 +1092,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E +@@ -919,6 +1099,10 @@ bool ResetToConsistentStateImpl(const ThreadCPUArea CPUArea, EXCEPTION_RECORD* E return true; } @@ -657,7 +670,7 @@ index 3be7e465a..4b8149bf3 100644 if (IsEmulatorStackAddress(CPUArea, reinterpret_cast(__builtin_frame_address(0)))) { Exception::RethrowGuestException(Thread, *Exception, *NativeContext); LogMan::Msg::DFmt("Rethrowing onto guest stack: {:X}", NativeContext->Sp); -@@ -1081,7 +1258,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si +@@ -1081,7 +1265,18 @@ NTSTATUS NotifyMapViewOfSection(void* Unk1, void* Address, void* Unk2, SIZE_T Si { std::scoped_lock Lock(ThreadCreationMutex); diff --git a/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch index c2cfa7d6c3..c541984fcf 100644 --- a/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch +++ b/gamenative/patches/0002-windows-anti-tamper-stepping-and-fault-delivery-.patch @@ -624,7 +624,7 @@ index 4d9fd8b7e..0ceed511b 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 4b8149bf3..8914f2c35 100644 +index 2af511082..b7b236750 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -91,8 +91,27 @@ uint64_t WineNtContinueSyscallId; @@ -719,7 +719,7 @@ index 4b8149bf3..8914f2c35 100644 } static void RethrowGuestException(FEXCore::Core::InternalThreadState* Thread, const EXCEPTION_RECORD& Rec, ARM64_NT_CONTEXT& HostContext) { -@@ -900,6 +953,197 @@ public: +@@ -907,6 +960,197 @@ public: }; } // namespace Exception @@ -917,7 +917,7 @@ index 4b8149bf3..8914f2c35 100644 extern "C" void SyncThreadContext(CONTEXT* Context) { ProcessPendingCrossProcessEmulatorWork(); auto* Thread = GetCPUArea().ThreadState(); -@@ -978,6 +1222,30 @@ NTSTATUS ProcessInit() { +@@ -985,6 +1229,30 @@ NTSTATUS ProcessInit() { PAGE_EXECUTE_READ); *reinterpret_cast(X64ReturnInstr) = 0xc3; diff --git a/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch b/gamenative/patches/0003-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch index 59d2bc48df..e57ce9c8a5 100644 --- a/gamenative/patches/0003-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 8914f2c35..1fcea15d2 100644 +index b7b236750..f604db1a6 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp -@@ -1519,6 +1519,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, +@@ -1526,6 +1526,10 @@ void NotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL After, } }