Emulate the console CPU's AMD-only instructions on Intel CPUs #150

Closed
ShyVortex wants to merge 1 commit from upstream-pr/153 into main
ShyVortex commented 2026-11-23 16:52:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ShyVortex on 2026-11-23T16:52:15Z.


Summary

Titles are compiled for the console's Zen 2 and use AMD-only instructions that Intel CPUs don't have. On an Intel CPU each one stops the game with an illegal instruction (0xC000001D). A retail game hit this early in boot: its memset clears large blocks with CLZERO, and its fence waits sleep on MONITORX / MWAITX.

This adds amd_insn.rs: a vectored exception handler, installed only when the CPU lacks one of these instructions (so nothing changes on AMD), that carries the instruction out and resumes after it:

Instruction Emulation
CLZERO zeroes the 64-byte line at rAX. When it is the body of one of the two loops compilers emit around it (clzero; add rax,0x40; cmp rax,LIM; jb and mov rax,P; add P,0x40; clzero; cmp P,LIM; jb), the whole range is cleared at once and execution resumes after the loop: one fault per clear instead of one per 64 bytes
MONITORX no-op
MWAITX yields the thread (it may wake at any time; callers re-check their condition)
RDPRU TSC for MPERF / APERF, CF set

Memory that isn't committed and writable is left to fault as the real instruction would. Decoded sites are cached and logged once. xcl run prints which instructions are emulated.

Testing

On an Intel i7-12700H (no CLZERO / MONITORX / RDPRU):

  • cargo test -p xcl --lib amd_insn: 3 tests. They execute the real instructions from generated code (both loop shapes, a lone CLZERO with an unaligned address, MONITORX + MWAITX) and check the exact bytes cleared. On AMD the same tests run natively.
  • The retail game that stopped on CLZERO now runs past all 5 of its memset loops and its MONITORX fence wait, and goes on to its graphics and audio setup.

Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:6564302349 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/153). Originally posted by [ShyVortex](https://github.com/ShyVortex) on 2026-11-23T16:52:15Z. --- ## Summary Titles are compiled for the console's Zen 2 and use AMD-only instructions that Intel CPUs don't have. On an Intel CPU each one stops the game with an illegal instruction (`0xC000001D`). A retail game hit this early in boot: its `memset` clears large blocks with `CLZERO`, and its fence waits sleep on `MONITORX` / `MWAITX`. This adds `amd_insn.rs`: a vectored exception handler, **installed only when the CPU lacks one of these instructions** (so nothing changes on AMD), that carries the instruction out and resumes after it: | Instruction | Emulation | |---|---| | `CLZERO` | zeroes the 64-byte line at `rAX`. When it is the body of one of the two loops compilers emit around it (`clzero; add rax,0x40; cmp rax,LIM; jb` and `mov rax,P; add P,0x40; clzero; cmp P,LIM; jb`), the whole range is cleared at once and execution resumes after the loop: one fault per clear instead of one per 64 bytes | | `MONITORX` | no-op | | `MWAITX` | yields the thread (it may wake at any time; callers re-check their condition) | | `RDPRU` | TSC for MPERF / APERF, CF set | Memory that isn't committed and writable is left to fault as the real instruction would. Decoded sites are cached and logged once. `xcl run` prints which instructions are emulated. ## Testing On an Intel i7-12700H (no CLZERO / MONITORX / RDPRU): - `cargo test -p xcl --lib amd_insn`: 3 tests. They **execute the real instructions** from generated code (both loop shapes, a lone `CLZERO` with an unaligned address, `MONITORX` + `MWAITX`) and check the exact bytes cleared. On AMD the same tests run natively. - The retail game that stopped on `CLZERO` now runs past all 5 of its `memset` loops and its `MONITORX` fence wait, and goes on to its graphics and audio setup. Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:57:25 +00:00
ShyVortex commented 2026-11-24 07:04:08 +00:00 (Migrated from github.com)

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


Closing in favour of #151, which covers the same instructions and more. I've offered the CLZERO loop fast path and the executing tests from this PR there as a follow-up.

<!-- greyfinch-sync:ivall/xcl:comment:7107894033 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/153#issuecomment-7107894033). Originally posted by [ShyVortex](https://github.com/ShyVortex) on 2026-11-23T16:55:55Z. --- Closing in favour of #151, which covers the same instructions and more. I've offered the CLZERO loop fast path and the executing tests from this PR there as a follow-up.

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
xcl-dev/xcl!150
No description provided.