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

Open
opened 2026-11-26 20:21:48 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1576
Author: @madhyn
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: feat/monitorx-mwaitx-inplace-patch


📝 Commits (2)

  • ea2f435 cpu_emulation: rewrite bare MONITORX/MWAITX in place at first #UD (RT-MONITORX-PATCH)
  • 44e437b cpu_emulation_test: initialize EAX/ECX/EDX before page-boundary MONITORX/MWAITX

📊 Changes

2 files changed (+149 additions, -0 deletions)

View changed files

📝 runtime/src/cpu_emulation.h (+63 -0)
📝 runtime/test/cpu_emulation_test.cpp (+86 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1576 **Author:** [@madhyn](https://github.com/madhyn) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/monitorx-mwaitx-inplace-patch` --- ### 📝 Commits (2) - [`ea2f435`](https://github.com/ubiquitarianism/leonida/commit/ea2f4356e1d63808cb0ecb94ccda13509bb3e1d5) cpu_emulation: rewrite bare MONITORX/MWAITX in place at first #UD (RT-MONITORX-PATCH) - [`44e437b`](https://github.com/ubiquitarianism/leonida/commit/44e437bda46bbb426c88c41c5ead6247f0cd7819) cpu_emulation_test: initialize EAX/ECX/EDX before page-boundary MONITORX/MWAITX ### 📊 Changes **2 files changed** (+149 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/cpu_emulation.h` (+63 -0) 📝 `runtime/test/cpu_emulation_test.cpp` (+86 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1595
No description provided.