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

Closed
github-bot wants to merge 1 commit from gh-pr/153/head into gh-pr/153/base
Owner

@⁠ShyVortex on GitHub · 2026-11-23 16:52 UTC · original
ShyVortex:cpu/amd-only-instructions → main · +422 −0 · 4 files · 1 commits · closed without merging

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

**@⁠ShyVortex** on GitHub · 2026-11-23 16:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/153) `ShyVortex:cpu/amd-only-instructions` → `main` · +422 −0 · 4 files · 1 commits · closed without merging ## 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)
Titles are compiled for the console's Zen 2 and use instructions Intel CPUs lack: a
retail game's memset clears large blocks with CLZERO and its fence waits sleep on
MONITORX / MWAITX. On an Intel CPU each one stopped the game with an illegal
instruction (0xC000001D).

A vectored handler, installed only when the CPU lacks one of them, carries the
instruction out and resumes after it:

- CLZERO zeroes the 64-byte line at rAX; when it is the body of one of the two
  loops compilers emit around it, the whole range is cleared at once and execution
  resumes after the loop (one fault per clear instead of one per line)
- MONITORX arms nothing, MWAITX yields (it may wake at any time)
- RDPRU returns the TSC for MPERF / APERF

Tested on an i7-12700H: the tests run the real instructions, and the retail game
that faulted on CLZERO now runs past all of its memset loops.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:54 +00:00
github-bot closed this pull request 2026-11-26 04:12:54 +00:00
Author
Owner

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

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.

**@⁠ShyVortex** on GitHub · 2026-11-23 16:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/153#issuecomment-7107894033) 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.