perf(loader): patch mwaitx/clwb in-place to NOP and accelerate clzero loops #29

Open
github-bot wants to merge 2 commits from github-bot/gh-29 into beta AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 18:03 UTC · original
Vladimir-Human:perf/klbf-intel-mwaitx-clzero → beta · +1913 −303 · 3 files · 2 commits · open

AI-assisted PR.

Summary

Eliminates repeated #UD VEH faults from console synchronization mwaitx (0F 01 FB) / monitorx (0F 01 FA) / clwb waits and tightens clzero (0F 01 FC) cache-line zeroing in crates/victory-loader:

  1. Thread-safe in-place mwaitx/monitorx/clwb NOP patching (console_insn.rs, console_insn_decode.rs):
    • Under PATCH_LOCK, replaces only the single opcode byte (0x01/0xAE -> 0x1F, turning monitorx/mwaitx/clwb into multi-byte 0F 1F NOPs cleanly without modifying surrounding instruction boundaries), brackets VirtualProtect (RX -> RWX -> original protection), flushes the instruction cache (FlushInstructionCache), and retries execution at the exact same Rip once (PatchedInsn) so all subsequent waits incur 0 faults instead of taking a #UD trap per spin iteration.
    • Properly gates p66 Clwb/PatchedInsn decode arms with p66 && rep == 0 so 66 F2/F3 #UD sequences are never rewritten.
    • Respects VICTORY_PATCH_WAIT=0 opt-out cleanly.
  2. Generalized ClzeroLoop emulation & memory-protection hardening (console_insn.rs, console_insn_decode.rs, crash.rs):
    • Emulates contiguous clzero zeroing loops directly inside the handler with checked address math (checked_add), explicit flag semantics, and full PAGE_WRITECOPY / PAGE_EXECUTE_WRITECOPY support (m.Protect & 0xFF) while rejecting PAGE_NOACCESS and PAGE_GUARD pages cleanly without stale WRITABLE caching.
  3. AMD/Intel cross-CPU test safety (console_insn.rs):
    • Adds CPUID 0x8000_0001:ECX[29] (monitorx) and 0x8000_0008:EBX[0] (clzero) detection so native-fault tests fall back cleanly to direct emulation checks on AMD CPUs that support MONITORX/CLZERO natively.

Verification (commit 918d5449, beta base c71c7d6)

  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: 0 warnings (default & --all-features)
  • cargo test -p victory-loader --locked: 37 passed, 0 failed, 1 ignored (unit) + 2/2 (load_probe) + 1/1 (title_dlls) = 40/40
  • cargo test -p victory-loader --all-features --locked: 38 passed, 0 failed, 1 ignored (unit) + 2/2 + 1/1 = 41/41
  • --release bench_wait_patch_before_vs_after (i5-13400F): BEFORE_UNPATCHED 165.64 ms (100,000 faults) -> AFTER_PATCHED 57.20 us (2 faults) (~2,896x faster)
  • git merge-tree --write-tree clean against klbf-upstream/beta (c71c7d6) and PRs #15 & #16.
**@⁠Vladimir-Human** on GitHub · 2026-11-25 18:03 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/29) `Vladimir-Human:perf/klbf-intel-mwaitx-clzero` → `beta` · +1913 −303 · 3 files · 2 commits · open AI-assisted PR. ### Summary Eliminates repeated `#UD` VEH faults from console synchronization `mwaitx` (`0F 01 FB`) / `monitorx` (`0F 01 FA`) / `clwb` waits and tightens `clzero` (`0F 01 FC`) cache-line zeroing in `crates/victory-loader`: 1. **Thread-safe in-place `mwaitx`/`monitorx`/`clwb` NOP patching (`console_insn.rs`, `console_insn_decode.rs`)**: - Under `PATCH_LOCK`, replaces only the single opcode byte (`0x01`/`0xAE -> 0x1F`, turning `monitorx`/`mwaitx`/`clwb` into multi-byte `0F 1F` NOPs cleanly without modifying surrounding instruction boundaries), brackets `VirtualProtect` (`RX -> RWX -> original protection`), flushes the instruction cache (`FlushInstructionCache`), and retries execution at the exact same `Rip` once (`PatchedInsn`) so all subsequent waits incur **0 faults** instead of taking a `#UD` trap per spin iteration. - Properly gates `p66` `Clwb`/`PatchedInsn` decode arms with `p66 && rep == 0` so `66 F2/F3` #UD sequences are never rewritten. - Respects `VICTORY_PATCH_WAIT=0` opt-out cleanly. 2. **Generalized `ClzeroLoop` emulation & memory-protection hardening (`console_insn.rs`, `console_insn_decode.rs`, `crash.rs`)**: - Emulates contiguous `clzero` zeroing loops directly inside the handler with checked address math (`checked_add`), explicit flag semantics, and full `PAGE_WRITECOPY` / `PAGE_EXECUTE_WRITECOPY` support (`m.Protect & 0xFF`) while rejecting `PAGE_NOACCESS` and `PAGE_GUARD` pages cleanly without stale `WRITABLE` caching. 3. **AMD/Intel cross-CPU test safety (`console_insn.rs`)**: - Adds CPUID `0x8000_0001:ECX[29]` (`monitorx`) and `0x8000_0008:EBX[0]` (`clzero`) detection so native-fault tests fall back cleanly to direct emulation checks on AMD CPUs that support `MONITORX`/`CLZERO` natively. ### Verification (commit `918d5449`, `beta` base `c71c7d6`) - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: **0 warnings** (default & `--all-features`) - `cargo test -p victory-loader --locked`: **37 passed, 0 failed, 1 ignored** (unit) + **2/2** (`load_probe`) + **1/1** (`title_dlls`) = **40/40** - `cargo test -p victory-loader --all-features --locked`: **38 passed, 0 failed, 1 ignored** (unit) + **2/2** + **1/1** = **41/41** - `--release` `bench_wait_patch_before_vs_after` (`i5-13400F`): `BEFORE_UNPATCHED 165.64 ms (100,000 faults)` -> `AFTER_PATCHED 57.20 us (2 faults)` (**~2,896x faster**) - `git merge-tree --write-tree` clean against `klbf-upstream/beta` (`c71c7d6`) and PRs `#15` & `#16`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:52:57 +00:00
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 18:38 UTC · original

Thanks a lot @⁠Vladimir-Human! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.

**@⁠PastiiJay** on GitHub · 2026-11-25 18:38 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/29#issuecomment-7171184764) Thanks a lot @⁠Vladimir-Human! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.
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/29/head:github-bot/gh-29'
git switch 'github-bot/gh-29'

Merge

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