Console CPU instructions: CLZERO with an address-size prefix zeroes the line at EAX #227

Manually merged
github merged 1 commit from gh-pr/227/head into gh-pr/227/base 2026-11-26 04:16:40 +00:00
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:22 UTC · original
Kalmarv:fix/clzero-addr32 → main · +42 −1 · 2 files · 1 commits · merged 2026-11-23 21:31 UTC

Follows #216 / af20b4b.

Since af20b4b, 0x67 is an ignored prefix, so 67 0F 01 FC decodes as CLZERO. With that prefix, the CPU zeroes the line at EAX (zero-extended), but the handler used the full RAX:

  • RAX's upper half set and nothing mapped at that 64-bit address: the handler passes the exception on (a crash), where the CPU zeroes the line at EAX.
  • Something mapped at that 64-bit address: the handler zeroes the wrong line.

This change makes the handler use EAX when 0x67 is among the prefixes before 0F 01 FC. It changes no other instruction.

Tested

  • Ryzen 9 9950X (Zen 5), Windows 11 26200, Rust 1.99.0 MSVC, debug; bc59624 on af20b4b.
  • Measured on the CPU (the probe from #209 / #216): with RAX = (upper bits << 32) | a buffer below 4 GiB, 67 0F 01 FC zeroed the 64-byte line at the 32-bit address. On af20b4b, the handler returned 0 and zeroed nothing, in both cases tried.
  • New test eh::tests::clzero_with_an_address_size_prefix_zeroes_at_eax:
    • Setup: memory is mapped at a 32-bit address and at the same address plus n << 32. RAX points into the upper one.
    • Checks: the line at EAX is zeroed, nothing at RAX is touched, and RIP steps by 4.
    • It passes with this change and fails with af20b4b's handler (its "the line at EAX" assertion).
  • eh:: 3/3 and insn:: 2/2 pass.

Not tested

  • an Intel CPU, where the #UD happens;
  • Zen 2;
  • a title that uses 67 0F 01 FC (none is known);
  • the full workspace suite.

AI use: Claude Code, Claude Opus 5.5

**@⁠Kalmarv** on GitHub · 2026-11-23 21:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/227) `Kalmarv:fix/clzero-addr32` → `main` · +42 −1 · 2 files · 1 commits · merged 2026-11-23 21:31 UTC Follows #216 / af20b4b. Since af20b4b, 0x67 is an ignored prefix, so `67 0F 01 FC` decodes as CLZERO. With that prefix, the CPU zeroes the line at EAX (zero-extended), but the handler used the full RAX: - RAX's upper half set and nothing mapped at that 64-bit address: the handler passes the exception on (a crash), where the CPU zeroes the line at EAX. - Something mapped at that 64-bit address: the handler zeroes the wrong line. This change makes the handler use EAX when 0x67 is among the prefixes before `0F 01 FC`. It changes no other instruction. ### Tested - Ryzen 9 9950X (Zen 5), Windows 11 26200, Rust 1.99.0 MSVC, debug; `bc59624` on `af20b4b`. - Measured on the CPU (the probe from #209 / #216): with RAX = (upper bits << 32) | a buffer below 4 GiB, `67 0F 01 FC` zeroed the 64-byte line at the 32-bit address. On `af20b4b`, the handler returned 0 and zeroed nothing, in both cases tried. - New test `eh::tests::clzero_with_an_address_size_prefix_zeroes_at_eax`: - Setup: memory is mapped at a 32-bit address and at the same address plus n << 32. RAX points into the upper one. - Checks: the line at EAX is zeroed, nothing at RAX is touched, and RIP steps by 4. - It passes with this change and fails with `af20b4b`'s handler (its "the line at EAX" assertion). - `eh::` 3/3 and `insn::` 2/2 pass. ### Not tested - an Intel CPU, where the #UD happens; - Zen 2; - a title that uses `67 0F 01 FC` (none is known); - the full workspace suite. AI use: Claude Code, Claude Opus 5.5
With 0x67 among its prefixes (decoded since af20b4b), CLZERO addresses the line at EAX,
zero-extended, as a Zen 5 runs it; the emulation used RAX. After an 0x67 the CLZERO loop is
not fast-forwarded: its jb goes back to the 0F, past the prefix, so only that first CLZERO is
at EAX. It is stepped alone, and the loop is taken from the next CLZERO. A test maps memory
at both the 32- and the 64-bit address and checks that only the 32-bit one's line is zeroed,
for a lone CLZERO and for the first of a loop.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:39 +00:00
github manually merged commit 6b9f2ba4fa into gh-pr/227/base 2026-11-26 04:16:40 +00:00
Author
Owner

@⁠ryosen0 on GitHub · 2026-11-23 21:26 UTC · original

Independent confirmation on a second Zen 5 part (Ryzen 7 9800X3D, Windows 11, MSVC 14.44):

Raw bytes run natively on a scratch line mapped at 0x3000_0000 (below 4 GiB), a VEH recording the fault code and address:

bytes RAX result
67 0F 01 FC 0x3000_0000 line at EAX zeroed, no fault
67 0F 01 FC 0x1_3000_0000 (upper half set) line at 0x3000_0000 zeroed, no fault
0F 01 FC 0x1_3000_0000 AV at 0x1_3000_0000, line untouched
0F 01 FC 0x3000_0000 line zeroed, no fault

So the addr32 form truncates to EAX and zero-extends even with RAX's upper half set, matching the PR's description; the plain form faults on the full 64-bit address. With the scratch in the process image (above 4 GiB) the addr32 form faults at the truncated address - the EAX address must be mapped, which is the case the handler's readable check already covers.

Also ran the SSE4a formulas (insn::extrq / insn::insertq semantics) against the CPU here: 32,776 cases (imm forms, every len 0..63 x idx 0..63 on 4 patterns each, reg-form control words with garbage outside the 6-bit fields) - 0 mismatches, high quadword zero in every case, agreeing with the 9950X numbers above.

**@⁠ryosen0** on GitHub · 2026-11-23 21:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/227#issuecomment-7114465502) Independent confirmation on a second Zen 5 part (Ryzen 7 9800X3D, Windows 11, MSVC 14.44): Raw bytes run natively on a scratch line mapped at `0x3000_0000` (below 4 GiB), a VEH recording the fault code and address: | bytes | RAX | result | |---|---|---| | `67 0F 01 FC` | `0x3000_0000` | line at EAX zeroed, no fault | | `67 0F 01 FC` | `0x1_3000_0000` (upper half set) | **line at `0x3000_0000` zeroed, no fault** | | `0F 01 FC` | `0x1_3000_0000` | AV at `0x1_3000_0000`, line untouched | | `0F 01 FC` | `0x3000_0000` | line zeroed, no fault | So the addr32 form truncates to EAX and zero-extends even with RAX's upper half set, matching the PR's description; the plain form faults on the full 64-bit address. With the scratch in the process image (above 4 GiB) the addr32 form faults at the truncated address - the EAX address must be mapped, which is the case the handler's `readable` check already covers. Also ran the SSE4a formulas (`insn::extrq` / `insn::insertq` semantics) against the CPU here: 32,776 cases (imm forms, every len 0..63 x idx 0..63 on 4 patterns each, reg-form control words with garbage outside the 6-bit fields) - 0 mismatches, high quadword zero in every case, agreeing with the 9950X numbers above.
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:28 UTC · original

Rebased on main d4f17ca (6b9f2ba). #217 reworked the CLZERO handler (is_writable, the ClzeroLoop fast-forward), so the EAX rule now sits on the new lone-CLZERO path.

The fast-forward must not run under 0x67: the loop's jb -12 jumps back to the 0F, past the prefix, so on the CPU only the first CLZERO is at EAX and the rest of the loop is at RAX. The decoder therefore leaves a 0x67-prefixed CLZERO unfused and steps it alone (length 4); the next trap, at the 0F, takes the fast-forward as on main.

  • The eh test covers the lone 67 0F 01 FC and the first CLZERO of a 0x67-prefixed loop: only the line at EAX is zeroed, nothing at RAX, and RIP moves by 4. The insn decode test has a case for the prefixed loop.
  • With main's handler and decoder put back, both fail; with this change, eh:: 3/3 and insn:: 2/2 pass (Ryzen 9 9950X, debug).
  • Not measured on the CPU: the loop case, which follows from the encoding. 2 files, +42/−1.

Thanks @⁠ryosen0 for the second Zen 5 run.

🤖 Claude Opus 5.5

**@⁠Kalmarv** on GitHub · 2026-11-23 21:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/227#issuecomment-7114506050) Rebased on main d4f17ca (6b9f2ba). #217 reworked the CLZERO handler (`is_writable`, the `ClzeroLoop` fast-forward), so the EAX rule now sits on the new lone-CLZERO path. The fast-forward must not run under 0x67: the loop's `jb -12` jumps back to the `0F`, past the prefix, so on the CPU only the first CLZERO is at EAX and the rest of the loop is at RAX. The decoder therefore leaves a 0x67-prefixed CLZERO unfused and steps it alone (length 4); the next trap, at the `0F`, takes the fast-forward as on main. - The `eh` test covers the lone `67 0F 01 FC` and the first CLZERO of a 0x67-prefixed loop: only the line at EAX is zeroed, nothing at RAX, and RIP moves by 4. The `insn` decode test has a case for the prefixed loop. - With main's handler and decoder put back, both fail; with this change, `eh::` 3/3 and `insn::` 2/2 pass (Ryzen 9 9950X, debug). - Not measured on the CPU: the loop case, which follows from the encoding. 2 files, +42/−1. Thanks @⁠ryosen0 for the second Zen 5 run. 🤖 Claude Opus 5.5
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:31 UTC · original

Merged (main 60565c4), your rebased head with the 0x67 / CLZERO-loop fix. Checked on an RX 9070 (RDNA4): cargo build --workspace && cargo test --workspace -- --test-threads=4 - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.

**@⁠ivall** on GitHub · 2026-11-23 21:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/227#issuecomment-7114584217) Merged (main 60565c4), your rebased head with the 0x67 / CLZERO-loop fix. Checked on an RX 9070 (RDNA4): `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.
This discussion has been locked. Commenting is limited to contributors.
No description provided.