Console CPU instructions a PC CPU lacks are emulated: clwb, clzero, r… #151

Manually merged
github merged 1 commit from gh-pr/151/head into gh-pr/151/base 2026-11-26 04:12:48 +00:00
Owner

@⁠gbuyssens on GitHub · 2026-11-23 16:39 UTC · original
gbuyssens:pr/console-insn → main · +197 −0 · 4 files · 1 commits · merged 2026-11-23 20:51 UTC

…dpru, monitorx / mwaitx, rdpid

A title compiled for the console's Zen 2 raises STATUS_ILLEGAL_INSTRUCTION on an Intel CPU before Ice Lake / Alder Lake (clwb, sha) or on any Intel CPU (clzero, rdpru, monitorx, mwaitx). insn::decode recognises them at the faulting address; the vectored handler emulates them and steps over. Other illegal instructions and ud2 are logged with their code bytes, in the first-chance handler and in the crash line.

**@⁠gbuyssens** on GitHub · 2026-11-23 16:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/151) `gbuyssens:pr/console-insn` → `main` · +197 −0 · 4 files · 1 commits · merged 2026-11-23 20:51 UTC …dpru, monitorx / mwaitx, rdpid A title compiled for the console's Zen 2 raises STATUS_ILLEGAL_INSTRUCTION on an Intel CPU before Ice Lake / Alder Lake (clwb, sha) or on any Intel CPU (clzero, rdpru, monitorx, mwaitx). insn::decode recognises them at the faulting address; the vectored handler emulates them and steps over. Other illegal instructions and ud2 are logged with their code bytes, in the first-chance handler and in the crash line.
A title compiled for the console's Zen 2 raises STATUS_ILLEGAL_INSTRUCTION on
an Intel CPU before Ice Lake / Alder Lake (clwb, sha) or on any Intel CPU
(clzero, rdpru, monitorx, mwaitx). insn::decode recognises them at the
faulting address; the vectored handler emulates them and steps over. Other
illegal instructions and ud2 are logged with their code bytes, in the
first-chance handler and in the crash line.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:47 +00:00
github manually merged commit 0b28a39139 into gh-pr/151/base 2026-11-26 04:12:48 +00:00
Author
Owner

@⁠ShyVortex on GitHub · 2026-11-23 16:55 UTC · original

I opened #153 for the same problem without seeing this one first. I'm closing it in favour of this PR, which covers more instructions (CLWB, RDPID) and has the prefix-aware decoder.

Two things from #153 that could be useful on top of this, as a follow-up once this is merged, if you want them:

  1. CLZERO loop fast path. A retail game's memset clears large blocks with CLZERO in one of two loop shapes:

    • clzero; add rax, 0x40; cmp rax, LIM; jb clzero
    • mov rax, P; add P, 0x40; clzero; cmp P, LIM; jb mov

    Emulating one line per fault costs a full exception round trip per 64 bytes. When the faulting CLZERO is the body of one of these loops, #153 clears the loop's whole range in one go, sets the registers and flags the loop would leave, and resumes after the jb. That makes it one fault per memset instead of one per cache line. It checks that the range is committed and writable first, and otherwise lets it fault as the real instruction would.

  2. Tests that execute the instructions. They copy small functions (both loop shapes, a lone CLZERO at an unaligned address, MONITORX + MWAITX) into executable memory, call them, and check exactly which bytes were cleared. On an Intel CPU that goes through the handler; on AMD the instructions run natively, so the same test checks the emulation against the hardware.

The code is in crates/xcl/src/amd_insn.rs on #153's branch (ShyVortex:cpu/amd-only-instructions). Tested on an i7-12700H.

**@⁠ShyVortex** on GitHub · 2026-11-23 16:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7107893442) I opened #153 for the same problem without seeing this one first. I'm closing it in favour of this PR, which covers more instructions (CLWB, RDPID) and has the prefix-aware decoder. Two things from #153 that could be useful on top of this, as a follow-up once this is merged, if you want them: 1. **CLZERO loop fast path.** A retail game's `memset` clears large blocks with CLZERO in one of two loop shapes: - `clzero; add rax, 0x40; cmp rax, LIM; jb clzero` - `mov rax, P; add P, 0x40; clzero; cmp P, LIM; jb mov` Emulating one line per fault costs a full exception round trip per 64 bytes. When the faulting CLZERO is the body of one of these loops, #153 clears the loop's whole range in one go, sets the registers and flags the loop would leave, and resumes after the `jb`. That makes it one fault per `memset` instead of one per cache line. It checks that the range is committed and writable first, and otherwise lets it fault as the real instruction would. 2. **Tests that execute the instructions.** They copy small functions (both loop shapes, a lone CLZERO at an unaligned address, MONITORX + MWAITX) into executable memory, call them, and check exactly which bytes were cleared. On an Intel CPU that goes through the handler; on AMD the instructions run natively, so the same test checks the emulation against the hardware. The code is in `crates/xcl/src/amd_insn.rs` on #153's branch (`ShyVortex:cpu/amd-only-instructions`). Tested on an i7-12700H.
Author
Owner

@⁠Vencite on GitHub · 2026-11-23 18:41 UTC · original

tested this on my i9 14900k and rtx 4080 super (driver 32.0.16.2124). both main (956a476bf7) and the pr parent (9add721) crashed with 0xC000001D at the same spot, before any Present calls.

with 236cc749, xcl logged CLZERO emulation at that location. tried two runs with a fresh cache each time, both reached one Present and exited with code 0 at the 25-second limit.

used XCL_NETWORK=0 for all runs. haven't verified the actual frame contents yet, but it definitely gets further than before.

**@⁠Vencite** on GitHub · 2026-11-23 18:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7110522132) tested this on my i9 14900k and rtx 4080 super (driver 32.0.16.2124). both main (956a476bf738) and the pr parent (9add721) crashed with 0xC000001D at the same spot, before any Present calls. with 236cc749, xcl logged CLZERO emulation at that location. tried two runs with a fresh cache each time, both reached one Present and exited with code 0 at the 25-second limit. used XCL_NETWORK=0 for all runs. haven't verified the actual frame contents yet, but it definitely gets further than before.
Author
Owner

@⁠MiguelMachado-dev on GitHub · 2026-11-23 19:52 UTC · original

I tested the CLZERO fallback on a Core i7-14700KF (Windows 11 build 26300, Rust 1.97.1 MSVC). I loaded the same standalone Rust x64 PE with xcl run in both cases.

  • main at 956a476 exited with 0xC000001D at the CLZERO instruction.
  • 956a476 with the source changes from 236cc749 logged CLZERO emulation and exited with code 0. The fixture passed an unaligned address into the middle of three aligned 64-byte lines. It checked that the middle line was zeroed and that both neighboring lines still held 0xa5.
  • cargo test --locked -p xcl --lib insn::tests::decodes_the_console_only_instructions passed on the patched build.

The fixture is synthetic and exercises the exception handler via the normal XCL loader. It covers the CLZERO path reported in #173. AI-assisted with Codex.

**@⁠MiguelMachado-dev** on GitHub · 2026-11-23 19:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7112227266) I tested the CLZERO fallback on a Core i7-14700KF (Windows 11 build 26300, Rust 1.97.1 MSVC). I loaded the same standalone Rust x64 PE with `xcl run` in both cases. - `main` at `956a476` exited with `0xC000001D` at the CLZERO instruction. - `956a476` with the source changes from `236cc749` logged CLZERO emulation and exited with code 0. The fixture passed an unaligned address into the middle of three aligned 64-byte lines. It checked that the middle line was zeroed and that both neighboring lines still held 0xa5. - `cargo test --locked -p xcl --lib insn::tests::decodes_the_console_only_instructions` passed on the patched build. The fixture is synthetic and exercises the exception handler via the normal XCL loader. It covers the CLZERO path reported in #173. AI-assisted with Codex.
Author
Owner

@⁠afpruivo on GitHub · 2026-11-23 19:57 UTC · original

I tested a local CLZERO regression on Windows with an Intel Core i5-13600K. I ran the same synthetic console-title fixture through xcl run on both revisions:

  • main at 956a476bf738: reached CLZERO, then exited with 0xC000001D (STATUS_ILLEGAL_INSTRUCTION).
  • This PR at 236cc7495479: XCL logged that it emulated CLZERO. The fixture verified that exactly one aligned 64-byte cache line was zeroed, both neighboring lines retained their original bytes, and execution continued to exit code 0.

The focused test used synthetic data only and covers CLZERO. In a separate run of the PR's end-to-end suite, this regression passed again. The existing full-title test failed its startup assertion because this machine reported a missing System32\xgameruntime.dll.

Would you like me to submit the regression test? I can send it as a small follow-up PR once this lands, or share the patch for inclusion here.

AI-assisted with Codex.

**@⁠afpruivo** on GitHub · 2026-11-23 19:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7112353495) I tested a local CLZERO regression on Windows with an Intel Core i5-13600K. I ran the same synthetic console-title fixture through `xcl run` on both revisions: - `main` at `956a476bf738`: reached `CLZERO`, then exited with `0xC000001D` (`STATUS_ILLEGAL_INSTRUCTION`). - This PR at `236cc7495479`: XCL logged that it emulated `CLZERO`. The fixture verified that exactly one aligned 64-byte cache line was zeroed, both neighboring lines retained their original bytes, and execution continued to exit code 0. The focused test used synthetic data only and covers `CLZERO`. In a separate run of the PR's end-to-end suite, this regression passed again. The existing full-title test failed its startup assertion because this machine reported a missing `System32\xgameruntime.dll`. Would you like me to submit the regression test? I can send it as a small follow-up PR once this lands, or share the patch for inclusion here. AI-assisted with Codex.
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 20:34 UTC · original

AI-assisted review (Claude Code). Windows 11 (10.0.26200), Ryzen 9 9950X, Rust 1.99.0 MSVC, debug profile. Synthetic inputs only, no game files. No GPU needed.

Tested 0b28a39 (one commit directly on main 956a476, so the PR head is the merge result). The earlier Intel reports above ran 236cc749. I compared the two: 0b28a39 is a rebase of it, insn.rs is byte-identical, and the eh.rs change is the same. So those results carry over to the current head.

Emulation vs. hardware. This is an AMD CPU, so every one of these instructions also runs natively here. That makes the CPU itself the reference.

My probe registers a 64 KiB page as a mapped module, writes each encoding into it, and feeds STATUS_ILLEGAL_INSTRUCTION with a synthetic CONTEXT through cpp_exception_logger. It then compares the result with the same instruction executed for real. The same probe runs on main as the negative control.

case this PR main
clzero (0F 01 FC), rax at line offsets 0x45 / 0x80 / 0xFF / 0x7C1 4/4: same bytes cleared as the hardware on a 4 KiB 0xA5 buffer; rip +3 4/4 not handled
rdpid rax (F3 0F C7 F8) and rdpid r9 (F3 41 0F C7 F9), pinned to each logical CPU 32/32 CPUs equal the hardware's TSC_AUX; rip +4 / +5 not handled
rdpru, ecx = 0 / 1 / 2 handled, rip +3. ecx 0/1 give a QPC value (the hardware gives MPERF / APERF); ecx 2 gives 0, same as the hardware 3/3 not handled
monitorx, mwaitx, clwb [rax], clwb [rsp+8], clwb [r8+disp32], clwb [rip+disp32] 6/6 stepped over by their exact length (3, 3, 4, 6, 9, 8); rax unchanged 6/6 not handled
ud2 left to the title (handler returns 0, rip unchanged) same
67 0F 01 FC (clzero with an address-size prefix) not handled not handled

Totals: the PR passes 14/14 of the cases it targets and main fails 14/14. insn::tests::decodes_the_console_only_instructions passes on the PR.

Against the reports.

  • #184: CLZERO at +0x4e8aa70 and MONITORX at +0x4fb0bd7 (its two crash sites) are covered, and so are MWAITX and RDPRU, which it lists as unhandled. It also lists STGI/CLGI/SKINIT/INVLPGA (0F 01 DC–DF). The PR doesn't cover those. They are SVM and privileged, so ring 3 faults on them on AMD too, and no title should run them.
  • #173: the fault at +0x4e8aa70 is the same clzero loop, so it's covered. z961089510-dev's Intel run with this PR reached presents. Their next stop, 0x80000003 at +0x344196, also happens on AMD without this PR (engme66), so it is a separate issue.

From reading the diff (none of these block the PR):

  • The decoder skips 66 / F2 / F3, segment prefixes and REX, but not 0x67. For clzero, a 67 prefix means "use eax". Compilers don't normally emit it, but if one appears it falls through to the title's handler, as on main.
  • CLZERO checks readable(line, 64) before write_bytes. On a committed read-only line that check passes, and the write then faults inside the VEH, where the real instruction would have faulted at the title's rip. A writability check would match the hardware. I didn't test this.
  • Each emulated CLZERO costs one exception round trip per 64 bytes, and #184 counted 6,960 of them before Game Init. ShyVortex's loop fast path (#153) addresses that. I didn't measure the cost here.
  • mwaitx as a no-op turns the title's wait into a busy spin. #184's patch used _mm_pause(). A spin_loop() hint would be cheap.
  • insn.rs's module doc names SHA among the instructions older Intel CPUs lack, but decode has no SHA-NI case (0F 38 C8–CD, 0F 3A CC). Either the doc or the decoder should change.
  • Zen 2 also has SSE4a (EXTRQ / INSERTQ, 66/F2 0F 78/79; MOVNTSS / MOVNTSD, F3/F2 0F 2B), which no Intel CPU has, and LLVM's znver2 target enables it. That is a possible further #UD source. Whether the title uses any of it is unknown.
  • emulate_console_only only acts when modules::image_base_for_addr(rip) is set, so the same instructions in code outside a mapped module (JIT code, copied thunks) still go to the title. #184's and #173's sites are inside game.exe.
  • The emulation lives in the handler registered with AddVectoredExceptionHandler(0, …), so a title's own first-chance VEH sees the #UD first. Frame-based handlers still come after it.

Not tested:

  • a real #UD on an Intel CPU (the earlier reports cover that on 236cc749);
  • the retail title;
  • RDPID on a machine with more than 64 logical processors (processor groups);
  • the cost of the exception path.
**@⁠Kalmarv** on GitHub · 2026-11-23 20:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7113259214) AI-assisted review (Claude Code). Windows 11 (10.0.26200), Ryzen 9 9950X, Rust 1.99.0 MSVC, debug profile. Synthetic inputs only, no game files. No GPU needed. Tested `0b28a39` (one commit directly on `main` `956a476`, so the PR head is the merge result). The earlier Intel reports above ran `236cc749`. I compared the two: `0b28a39` is a rebase of it, `insn.rs` is byte-identical, and the `eh.rs` change is the same. So those results carry over to the current head. **Emulation vs. hardware.** This is an AMD CPU, so every one of these instructions also runs natively here. That makes the CPU itself the reference. My probe registers a 64 KiB page as a mapped module, writes each encoding into it, and feeds `STATUS_ILLEGAL_INSTRUCTION` with a synthetic `CONTEXT` through `cpp_exception_logger`. It then compares the result with the same instruction executed for real. The same probe runs on `main` as the negative control. | case | this PR | `main` | |---|---|---| | `clzero` (`0F 01 FC`), rax at line offsets 0x45 / 0x80 / 0xFF / 0x7C1 | 4/4: same bytes cleared as the hardware on a 4 KiB 0xA5 buffer; rip +3 | 4/4 not handled | | `rdpid rax` (`F3 0F C7 F8`) and `rdpid r9` (`F3 41 0F C7 F9`), pinned to each logical CPU | 32/32 CPUs equal the hardware's TSC_AUX; rip +4 / +5 | not handled | | `rdpru`, ecx = 0 / 1 / 2 | handled, rip +3. ecx 0/1 give a QPC value (the hardware gives MPERF / APERF); ecx 2 gives 0, same as the hardware | 3/3 not handled | | `monitorx`, `mwaitx`, `clwb [rax]`, `clwb [rsp+8]`, `clwb [r8+disp32]`, `clwb [rip+disp32]` | 6/6 stepped over by their exact length (3, 3, 4, 6, 9, 8); rax unchanged | 6/6 not handled | | `ud2` | left to the title (handler returns 0, rip unchanged) | same | | `67 0F 01 FC` (clzero with an address-size prefix) | **not handled** | not handled | Totals: the PR passes 14/14 of the cases it targets and `main` fails 14/14. `insn::tests::decodes_the_console_only_instructions` passes on the PR. **Against the reports.** - #184: CLZERO at `+0x4e8aa70` and MONITORX at `+0x4fb0bd7` (its two crash sites) are covered, and so are MWAITX and RDPRU, which it lists as unhandled. It also lists STGI/CLGI/SKINIT/INVLPGA (`0F 01 DC–DF`). The PR doesn't cover those. They are SVM and privileged, so ring 3 faults on them on AMD too, and no title should run them. - #173: the fault at `+0x4e8aa70` is the same `clzero` loop, so it's covered. z961089510-dev's Intel run with this PR reached presents. Their next stop, `0x80000003` at `+0x344196`, also happens on AMD without this PR (engme66), so it is a separate issue. **From reading the diff** (none of these block the PR): - The decoder skips 66 / F2 / F3, segment prefixes and REX, but not `0x67`. For `clzero`, a 67 prefix means "use eax". Compilers don't normally emit it, but if one appears it falls through to the title's handler, as on `main`. - CLZERO checks `readable(line, 64)` before `write_bytes`. On a committed read-only line that check passes, and the write then faults inside the VEH, where the real instruction would have faulted at the title's rip. A writability check would match the hardware. I didn't test this. - Each emulated CLZERO costs one exception round trip per 64 bytes, and #184 counted 6,960 of them before `Game Init`. ShyVortex's loop fast path (#153) addresses that. I didn't measure the cost here. - `mwaitx` as a no-op turns the title's wait into a busy spin. #184's patch used `_mm_pause()`. A `spin_loop()` hint would be cheap. - `insn.rs`'s module doc names SHA among the instructions older Intel CPUs lack, but `decode` has no SHA-NI case (`0F 38 C8–CD`, `0F 3A CC`). Either the doc or the decoder should change. - Zen 2 also has SSE4a (`EXTRQ` / `INSERTQ`, `66`/`F2 0F 78/79`; `MOVNTSS` / `MOVNTSD`, `F3`/`F2 0F 2B`), which no Intel CPU has, and LLVM's `znver2` target enables it. That is a possible further #UD source. Whether the title uses any of it is unknown. - `emulate_console_only` only acts when `modules::image_base_for_addr(rip)` is set, so the same instructions in code outside a mapped module (JIT code, copied thunks) still go to the title. #184's and #173's sites are inside `game.exe`. - The emulation lives in the handler registered with `AddVectoredExceptionHandler(0, …)`, so a title's own first-chance VEH sees the #UD first. Frame-based handlers still come after it. Not tested: - a real #UD on an Intel CPU (the earlier reports cover that on `236cc749`); - the retail title; - RDPID on a machine with more than 64 logical processors (processor groups); - the cost of the exception path.
Author
Owner

@⁠Jefter5549 on GitHub · 2026-11-23 20:48 UTC · original

Tested 0b28a39 on a Xeon E5-2666 v3 (Haswell-EP) with an RTX 2060 Super (driver 572.70), Windows 11.

Can confirm this gets past the 0xC000001D crashes on Haswell (+0x4ecc84a for clwb, +0x4e8aa70 for clzero, and +0x4fb0bd7 for monitorx/mcommit). The title initializes, streams assets and starts presenting frames.

A few notes from running it on older hardware:

• CLZERO loop overhead: stepping one 64-byte line per exception causes an exception storm during memory allocation. On this machine it took over 18,000 VEH dispatches in a few seconds, dropping presentation to ~0.2 fps (36 frames in 180s). Catching the 12-byte loop (clzero; add rax, 0x40; cmp rax, REG; jb -12), clearing [rax..REG) with write_bytes and skipping to rip + 12 brought it up to 15-20 fps (1,153 frames in the same 180s).
• CLWB in tight loops: hot-patching the instruction bytes with NOPs (0x90) via VirtualProtect avoids repeated VEH round trips on subsequent frames/iterations. Host memory is cache-coherent anyway.
• Memory check: checking is_writable (VirtualQuery for MEM_COMMIT + PAGE_READWRITE) before write_bytes avoids taking an unhandled AV inside the VEH if the destination is read-only.

Can submit the loop fast-path and NOP-patching as a follow-up PR once this gets merged.

**@⁠Jefter5549** on GitHub · 2026-11-23 20:48 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7113576566) Tested 0b28a39 on a Xeon E5-2666 v3 (Haswell-EP) with an RTX 2060 Super (driver 572.70), Windows 11. Can confirm this gets past the 0xC000001D crashes on Haswell (`+0x4ecc84a` for clwb, `+0x4e8aa70` for clzero, and `+0x4fb0bd7` for monitorx/mcommit). The title initializes, streams assets and starts presenting frames. A few notes from running it on older hardware: • CLZERO loop overhead: stepping one 64-byte line per exception causes an exception storm during memory allocation. On this machine it took over 18,000 VEH dispatches in a few seconds, dropping presentation to ~0.2 fps (36 frames in 180s). Catching the 12-byte loop (`clzero; add rax, 0x40; cmp rax, REG; jb -12`), clearing `[rax..REG)` with `write_bytes` and skipping to `rip + 12` brought it up to 15-20 fps (1,153 frames in the same 180s). • CLWB in tight loops: hot-patching the instruction bytes with NOPs (`0x90`) via `VirtualProtect` avoids repeated VEH round trips on subsequent frames/iterations. Host memory is cache-coherent anyway. • Memory check: checking `is_writable` (VirtualQuery for MEM_COMMIT + PAGE_READWRITE) before `write_bytes` avoids taking an unhandled AV inside the VEH if the destination is read-only. Can submit the loop fast-path and NOP-patching as a follow-up PR once this gets merged.
Author
Owner

@⁠mosin311aa on GitHub · 2026-11-23 20:51 UTC · original

Can confirm it runs with this patch on my Intel CPU where it previously crashed

**@⁠mosin311aa** on GitHub · 2026-11-23 20:51 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7113652825) Can confirm it runs with this patch on my Intel CPU where it previously crashed
Author
Owner

@⁠ivall on GitHub · 2026-11-23 20:52 UTC · original

Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.

**@⁠ivall** on GitHub · 2026-11-23 20:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/151#issuecomment-7113682829) Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.
This discussion has been locked. Commenting is limited to contributors.
No description provided.