d3d12: handle AMD-only clzero, monitorx and mwaitx on Intel CPUs #146

Manually merged
github merged 1 commit from gh-pr/146/head into gh-pr/146/base 2026-11-26 06:34:26 +00:00
Owner

@⁠JeffreyBytes on GitHub · 2026-11-25 05:23 UTC · original
JeffreyBytes:fix/amd-only-cpu-instructions → main · +685 −5 · 5 files · 1 commits · merged 2026-11-25 07:28 UTC

Summary

Fixes #145.

ProjectScarlett 1.0.2.1 uses three AMD-only instructions that raise EXCEPTION_ILLEGAL_INSTRUCTION on Intel CPUs (exit code 29). This adds handlers for them next to the clwb one from #80, called from the vectored exception handler in survey.rs:

  • clzero (runtime/d3d12/src/clzero.rs): zero the 64-byte line at RAX and resume after the instruction. When the title's clzero; add rax,0x40; cmp rax,rN; jb loop follows the fault, one trap clears the whole range and execution resumes at the cmp. Memory that isn't writable is left to fault, as it would on hardware.
  • monitorx / mwaitx (runtime/d3d12/src/mwaitx.rs): on first fault, rewrite the second opcode byte 01 → 1F, giving the same-length reserved NOP 0F 1F /r. It's a single atomic byte store, so later iterations of the wait loop run natively. Neither instruction has architectural side effects, and mwaitx may return at any time, so a NOP is valid. If the page can't be made writable, the handler steps over the instruction instead.

Both act only when CPUID reports the instruction missing and the fault is inside the title image. clwb.rs only changes read_code_bytes to pub(crate) so the new handlers can share it.

On an i5-12600K, every AMD-only site the title reaches is now handled. After the rewrite, each wait site traps once and the rest of the loop runs natively. What remains is one clzero trap per clearing loop (about 250 a second). This fixes the CPU crashes only: on RADV the window stays black at a low frame rate for a separate GPU-side reason, which I'll report on its own.

Checks

All commands from CONTRIBUTING.md pass:

  • python -m unittest discover -s tests -q
  • cargo test for tools/shader-compiler, tuxbox-dxbc (with native-bindings) and tuxbox-xg-layout
  • cargo check --target x86_64-pc-windows-msvc -p tuxbox-d3d12

The new unit tests are Windows-only, like the existing clwb ones. I ran the full tuxbox-d3d12 test binary under Proton's Wine: 86/86 passed. One test executes the rewritten 0F 1F FA / 0F 1F FB natively on the Intel host.

No evidence JSON changed.

**@⁠JeffreyBytes** on GitHub · 2026-11-25 05:23 UTC · [original](https://github.com/brcly/TuxBox/pull/146) `JeffreyBytes:fix/amd-only-cpu-instructions` → `main` · +685 −5 · 5 files · 1 commits · merged 2026-11-25 07:28 UTC ## Summary Fixes #145. ProjectScarlett 1.0.2.1 uses three AMD-only instructions that raise `EXCEPTION_ILLEGAL_INSTRUCTION` on Intel CPUs (exit code 29). This adds handlers for them next to the `clwb` one from #80, called from the vectored exception handler in `survey.rs`: - **`clzero`** (`runtime/d3d12/src/clzero.rs`): zero the 64-byte line at `RAX` and resume after the instruction. When the title's `clzero; add rax,0x40; cmp rax,rN; jb` loop follows the fault, one trap clears the whole range and execution resumes at the `cmp`. Memory that isn't writable is left to fault, as it would on hardware. - **`monitorx` / `mwaitx`** (`runtime/d3d12/src/mwaitx.rs`): on first fault, rewrite the second opcode byte `01` → `1F`, giving the same-length reserved NOP `0F 1F /r`. It's a single atomic byte store, so later iterations of the wait loop run natively. Neither instruction has architectural side effects, and `mwaitx` may return at any time, so a NOP is valid. If the page can't be made writable, the handler steps over the instruction instead. Both act only when CPUID reports the instruction missing and the fault is inside the title image. `clwb.rs` only changes `read_code_bytes` to `pub(crate)` so the new handlers can share it. On an i5-12600K, every AMD-only site the title reaches is now handled. After the rewrite, each wait site traps once and the rest of the loop runs natively. What remains is one `clzero` trap per clearing loop (about 250 a second). This fixes the CPU crashes only: on RADV the window stays black at a low frame rate for a separate GPU-side reason, which I'll report on its own. ## Checks All commands from CONTRIBUTING.md pass: - `python -m unittest discover -s tests -q` - `cargo test` for `tools/shader-compiler`, `tuxbox-dxbc` (with `native-bindings`) and `tuxbox-xg-layout` - `cargo check --target x86_64-pc-windows-msvc -p tuxbox-d3d12` The new unit tests are Windows-only, like the existing `clwb` ones. I ran the full `tuxbox-d3d12` test binary under Proton's Wine: 86/86 passed. One test executes the rewritten `0F 1F FA` / `0F 1F FB` natively on the Intel host. No evidence JSON changed.
ProjectScarlett 1.0.2.1 uses three instructions that Zen 2 implements
and Intel CPUs do not. Each raises EXCEPTION_ILLEGAL_INSTRUCTION on
Intel hosts, so the title exits with code 29.

clzero (0F 01 FC) zeroes the 64-byte line at RAX. No same-length
instruction can replace it, so the vectored handler clears the line and
resumes after it. When the title's clearing loop
(clzero; add rax,0x40; cmp rax,rN; jb) follows the fault, one trap
clears the whole range and resumes at the cmp. Unwritable memory is
left to fault as it would on hardware.

monitorx (0F 01 FA) and mwaitx (0F 01 FB) arm and wait on an address.
Neither changes registers, flags or memory, and callers re-check their
condition, so a NOP is a valid implementation. The handler rewrites the
second opcode byte to 1F on first fault (0F 1F /r, same length, one
atomic byte store) so the wait loop then runs natively, and steps over
the instruction if the page cannot be made writable.

Both act only when CPUID reports the instruction missing and the fault
is inside the title image.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:34:24 +00:00
github manually merged commit e1262aca82 into gh-pr/146/base 2026-11-26 06:34:26 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.