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

Merged
Kalmarv merged 1 commit from upstream-pr/227 into main 2026-11-23 21:31:53 +00:00
Kalmarv commented 2026-11-23 21:22:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:22:44Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6569103616 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/227). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:22:44Z. --- 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
upstream-sync closed this pull request 2026-11-24 06:57:44 +00:00
ryosen0 commented 2026-11-24 07:04:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ryosen0 on 2026-11-23T21:26:31Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7114465502 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/227#issuecomment-7114465502). Originally posted by [ryosen0](https://github.com/ryosen0) on 2026-11-23T21:26:31Z. --- 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.
Kalmarv commented 2026-11-24 07:04:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:28:21Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7114506050 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/227#issuecomment-7114506050). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:28:21Z. --- 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
ivall commented 2026-11-24 07:04:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:31:55Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7114584217 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/227#issuecomment-7114584217). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:31:55Z. --- 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.
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
xcl-dev/xcl!224
No description provided.