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

Closed
Vladimir-Human wants to merge 0 commits from upstream-pr/29 into stable-amd
Vladimir-Human commented 2026-11-25 18:03:27 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-25T18:03:27Z.


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.
<!-- greyfinch-sync:PastiiJay/klbf-lab:issue:5496540959 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/29). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-25T18:03:27Z. --- 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`.
PastiiJay commented 2026-11-25 18:38:16 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T18:38:16Z.


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

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7171184764 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/29#issuecomment-7171184764). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T18:38:16Z. --- Thanks a lot @Vladimir-Human! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.
Vladimir-Human commented 2026-11-28 17:54:04 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-28T17:54:04Z.


Re-ported onto stable-amd (v26.11.6) in #176 (https://github.com/PastiiJay/klbf-lab/pull/176).

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7243374831 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/29#issuecomment-7243374831). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-28T17:54:04Z. --- Re-ported onto `stable-amd` (`v26.11.6`) in #176 (https://github.com/PastiiJay/klbf-lab/pull/176).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
PastiiJay/klbf-lab!29
No description provided.