Repository navigation
Conversation
…-MONITORX-PATCH) On Intel hosts, AMD-only MONITORX (0F 01 FA) and MWAITX (0F 01 FB) spin-wait sites raise #UD on every loop iteration. Rewrite bare 3-byte sites in place at their first #UD via a page-bounded atomic 32-bit compare-exchange under SRWLOCK: MONITORX -> 3-byte NOP (0F 1F 00) and MWAITX -> PAUSE; NOP (F3 90 90). Prefixed and page-straddling forms fall back to per-fault emulation. Opt out with LEONIDA_MONITORX_PATCH=0.
Contributor
Author
|
Rebased on |
madhyn
force-pushed
the
feat/monitorx-mwaitx-inplace-patch
branch
from
November 25, 2026 13:33
809512c to
44e437b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claimed in the coordination issue as:
RT-MONITORX-PATCH(new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touchMONITORX/MWAITXpatching; placed cleanly apart from #1472'sCLWBhelper so the two do not conflict).What and why
On Intel hosts (
CPUID.80000001H:ECX[29] == 0), AMD-onlyMONITORX(0F 01 FA) andMWAITX(0F 01 FB) instructions inside spin-wait loops currently raise#UDon every loop iteration, paying full Windows/Wine VEH exception dispatch overhead repeatedly.This change rewrites bare 3-byte
MONITORX/MWAITXsites in place on their first#UDinruntime/src/cpu_emulation.h:MONITORX(0F 01 FA) -> canonical 3-byteNOP(0F 1F 00)MWAITX(0F 01 FB) ->PAUSE; NOP(F3 90 90)Patching uses a page-bounded atomic 32-bit
InterlockedCompareExchangeunder anSRWLOCK(switching the target page temporarily toPAGE_EXECUTE_READWRITEand restoring its prior protection plusFlushInstructionCache). When a 3-byte site starts at page offset0xFFD, the 4-byte CAS shifts back 1 byte so the write never crosses a page boundary. Sites starting at0xFFE/0xFFFor prefixed forms (prefix_len > 0) fall back to existing per-fault emulation. SetLEONIDA_MONITORX_PATCH=0to disable in-place patching.Tested
runtime/test/cpu_emulation_test.cppwithclang-cl --target=x86_64-pc-windows-msvc /std:c++20 /O2 /W4 /WX(0 warnings) and ran under Wine 11.22 on Arch Linux (Intel Core i7-12700,MONITORX 0):MONITORX(0F 1F 00) andMWAITX(F3 90 90), 0 repeat#UDfaults on subsequent executions, prefixed fallback,0xFFDpage-end shift-back, and0xFFEpage-straddling fallback —cpu_emulation: all checks passed.LEONIDA_MONITORX_PATCH=0: verified bytes remain unpatched and per-fault emulation path stays intact —cpu_emulation: all checks passed.python3 scripts/check_no_game_data.py: passed.GPUs tested: n/a (CPU instruction emulation only; tested on Intel Core i7-12700, Arch Linux x86_64 / Wine 11.22).
AI-assisted: yes (Antigravity)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)