cpu_emulation: rewrite bare MONITORX/MWAITX in place at first #UD (RT-MONITORX-PATCH) #1576

Open
github-bot wants to merge 2 commits from github-bot/gh-1576 into main AGit
Owner

@⁠madhyn on GitHub · 2026-11-25 04:52 UTC · original
madhyn:feat/monitorx-mwaitx-inplace-patch → main · +149 −0 · 2 files · 2 commits · open

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 touch MONITORX/MWAITX patching; placed cleanly apart from #1472's CLWB helper so the two do not conflict).

What and why

On Intel hosts (CPUID.80000001H:ECX[29] == 0), AMD-only MONITORX (0F 01 FA) and MWAITX (0F 01 FB) instructions inside spin-wait loops currently raise #UD on every loop iteration, paying full Windows/Wine VEH exception dispatch overhead repeatedly.

This change rewrites bare 3-byte MONITORX / MWAITX sites in place on their first #UD in runtime/src/cpu_emulation.h:

  • MONITORX (0F 01 FA) -> canonical 3-byte NOP (0F 1F 00)
  • MWAITX (0F 01 FB) -> PAUSE; NOP (F3 90 90)

Patching uses a page-bounded atomic 32-bit InterlockedCompareExchange under an SRWLOCK (switching the target page temporarily to PAGE_EXECUTE_READWRITE and restoring its prior protection plus FlushInstructionCache). When a 3-byte site starts at page offset 0xFFD, the 4-byte CAS shifts back 1 byte so the write never crosses a page boundary. Sites starting at 0xFFE/0xFFF or prefixed forms (prefix_len > 0) fall back to existing per-fault emulation. Set LEONIDA_MONITORX_PATCH=0 to disable in-place patching.

Tested

  • Cross-compiled runtime/test/cpu_emulation_test.cpp with clang-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):
    • Default mode: live rewrite of bare MONITORX (0F 1F 00) and MWAITX (F3 90 90), 0 repeat #UD faults on subsequent executions, prefixed fallback, 0xFFD page-end shift-back, and 0xFFE page-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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)
**@⁠madhyn** on GitHub · 2026-11-25 04:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1576) `madhyn:feat/monitorx-mwaitx-inplace-patch` → `main` · +149 −0 · 2 files · 2 commits · open 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 touch `MONITORX`/`MWAITX` patching; placed cleanly apart from #1472's `CLWB` helper so the two do not conflict). ### What and why On Intel hosts (`CPUID.80000001H:ECX[29] == 0`), AMD-only `MONITORX` (`0F 01 FA`) and `MWAITX` (`0F 01 FB`) instructions inside spin-wait loops currently raise `#UD` on every loop iteration, paying full Windows/Wine VEH exception dispatch overhead repeatedly. This change rewrites bare 3-byte `MONITORX` / `MWAITX` sites in place on their first `#UD` in `runtime/src/cpu_emulation.h`: - `MONITORX` (`0F 01 FA`) -> canonical 3-byte `NOP` (`0F 1F 00`) - `MWAITX` (`0F 01 FB`) -> `PAUSE; NOP` (`F3 90 90`) Patching uses a page-bounded atomic 32-bit `InterlockedCompareExchange` under an `SRWLOCK` (switching the target page temporarily to `PAGE_EXECUTE_READWRITE` and restoring its prior protection plus `FlushInstructionCache`). When a 3-byte site starts at page offset `0xFFD`, the 4-byte CAS shifts back 1 byte so the write never crosses a page boundary. Sites starting at `0xFFE`/`0xFFF` or prefixed forms (`prefix_len > 0`) fall back to existing per-fault emulation. Set `LEONIDA_MONITORX_PATCH=0` to disable in-place patching. ### Tested - Cross-compiled `runtime/test/cpu_emulation_test.cpp` with `clang-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`): - Default mode: live rewrite of bare `MONITORX` (`0F 1F 00`) and `MWAITX` (`F3 90 90`), 0 repeat `#UD` faults on subsequent executions, prefixed fallback, `0xFFD` page-end shift-back, and `0xFFE` page-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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:57:03 +00:00
Author
Owner

@⁠madhyn on GitHub · 2026-11-25 13:33 UTC · original

Rebased on main and fixed the Windows CI failure in cpu_emulation_test: the two two_pages boundary cases (0xFFD and 0xFFE) previously entered directly with ECX = lines != 0 and uninitialized EAX/EDX, which triggers #GP(0) instead of #UD on AMD EPYC runners with MONITORX=0 under Hyper-V. Added valid register prologues before the boundary instructions and flushed the instruction cache after VirtualProtect. Re-verified locally under Wine with clang-cl /W4 /WX (default mode and LEONIDA_MONITORX_PATCH=0).

**@⁠madhyn** on GitHub · 2026-11-25 13:33 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1576#issuecomment-7163953374) Rebased on `main` and fixed the Windows CI failure in `cpu_emulation_test`: the two `two_pages` boundary cases (`0xFFD` and `0xFFE`) previously entered directly with `ECX = lines != 0` and uninitialized `EAX`/`EDX`, which triggers `#GP(0)` instead of `#UD` on AMD EPYC runners with `MONITORX=0` under Hyper-V. Added valid register prologues before the boundary instructions and flushed the instruction cache after `VirtualProtect`. Re-verified locally under Wine with `clang-cl /W4 /WX` (default mode and `LEONIDA_MONITORX_PATCH=0`).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1576/head:github-bot/gh-1576'
git switch 'github-bot/gh-1576'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1576'
git switch 'github-bot/gh-1576'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1576'
git switch 'github-bot/gh-1576'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1576'
git switch 'main'
git merge --squash 'github-bot/gh-1576'
git switch 'main'
git merge --ff-only 'github-bot/gh-1576'
git switch 'main'
git merge 'github-bot/gh-1576'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.