d3d12: rewrite unsupported clwb to clflushopt on pre-Ice Lake CPUs #80

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

@⁠chunfeng11221 on GitHub · 2026-11-24 19:28 UTC · original
chunfeng11221:d3d12/clwb-fallback → main · +806 −5 · 3 files · 1 commits · merged 2026-11-24 19:54 UTC

Summary

Fixes #71.

Xbox Series X|S titles compiled for Zen 2 can execute clwb (66 [REX] 0F AE /6, CPUID leaf 7 EBX bit 24) in cache-line write-back loops, which raises EXCEPTION_ILLEGAL_INSTRUCTION (0xc000001d) on pre-Ice Lake Intel desktop CPUs.

This change adds on-demand clwb compatibility inside d3d12_xs:

  • Early VEH attach/detach (runtime/d3d12/src/lib.rs, survey.rs): Registers the existing vectored exception handler when d3d12_xs.dll attaches (via .CRT$XCU, avoiding a DllMain duplicate-symbol collision with libcmt's stub under MSVC link.exe) and removes it via atexit on dynamic unload, so title clwb sites are covered even before D3D12XboxCreateDevice.
  • Strict clwb decoder & zero-overhead fast path (runtime/d3d12/src/clwb.rs):
    • CPUs advertising CLWB return immediately without locking or touching code pages.
    • On first #UD inside the title image, read_code_bytes safely reads up to 15 committed, readable, non-guard bytes across page boundaries, and decode_clwb matches only valid clwb m8 encodings (requiring 0x66, mod != 3, /6, valid prefixes/REX/SIB/displacement, length ≤ 15; rejecting tpause, xsaveopt, clflushopt, LOCK/F2/F3, and truncated/malformed bytes).
    • On hosts with CLFLUSHOPT (Skylake through Coffee Lake / Comet Lake, CPUID leaf 7 EBX bit 23), atomically sets bit 3 of the ModRM byte (/6 → /7), flushes the instruction cache, restores page protection, and resumes at the same Rip so hot loops run at native speed without further traps. Concurrent threads trapping on the same Rip retry cleanly once patched.
    • On older hosts without CLFLUSHOPT (Haswell/Broadwell) or non-writable pages, advances Rip past the instruction and logs once per RVA.

Checks

Ran on Windows x64 (rustc 1.97.1, MSVC 14.44):

  • cargo check --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 — passed (0 warnings)
  • cargo test --locked -p tuxbox-d3d12 --lib — 33/33 passed, including:
    • exact byte encodings from #71 and all ModRM/SIB/displacement/RIP-relative addressing forms
    • exhaustive 256-value ModRM, 256-value leading-byte prefix, and 768-case SIB decoder sweeps
    • straddling cross-page (RX / RWX), PAGE_NOACCESS, PAGE_GUARD, out-of-bounds Rip, and non-clwb (ud2, lock clwb) rejection tests
    • 8-thread concurrent race on a single PAGE_EXECUTE_READ clwb site, verifying single atomic patch, protection restoration, and live execution of the rewritten clflushopt
    • Haswell fallback (!clflushopt) Rip advancement and single-log behavior
  • cargo test --locked -p tuxbox-dxbc --features native-bindings — 51/51 passed
  • cargo test --locked -p tuxbox-xg-layout — 14/14 passed
  • cargo test --locked --manifest-path tools/shader-compiler/Cargo.toml — 82/82 passed
  • Linked release d3d12_xs.dll with MSVC link.exe, verified AddVectoredExceptionHandler and RemoveVectoredExceptionHandler imports, and ran a 5× LoadLibraryW / FreeLibrary attach/detach cycle.

Note: I do not have a pre-Ice Lake Intel CPU or the retail executable locally, so end-to-end verification on a non-CLWB host (such as the i9-9900 in #71) will need confirmation on that hardware.

**@⁠chunfeng11221** on GitHub · 2026-11-24 19:28 UTC · [original](https://github.com/brcly/TuxBox/pull/80) `chunfeng11221:d3d12/clwb-fallback` → `main` · +806 −5 · 3 files · 1 commits · merged 2026-11-24 19:54 UTC ## Summary Fixes #71. Xbox Series X|S titles compiled for Zen 2 can execute `clwb` (`66 [REX] 0F AE /6`, CPUID leaf 7 EBX bit 24) in cache-line write-back loops, which raises `EXCEPTION_ILLEGAL_INSTRUCTION` (`0xc000001d`) on pre-Ice Lake Intel desktop CPUs. This change adds on-demand `clwb` compatibility inside `d3d12_xs`: - **Early VEH attach/detach (`runtime/d3d12/src/lib.rs`, `survey.rs`)**: Registers the existing vectored exception handler when `d3d12_xs.dll` attaches (via `.CRT$XCU`, avoiding a `DllMain` duplicate-symbol collision with `libcmt`'s stub under MSVC `link.exe`) and removes it via `atexit` on dynamic unload, so title `clwb` sites are covered even before `D3D12XboxCreateDevice`. - **Strict `clwb` decoder & zero-overhead fast path (`runtime/d3d12/src/clwb.rs`)**: - CPUs advertising `CLWB` return immediately without locking or touching code pages. - On first `#UD` inside the title image, `read_code_bytes` safely reads up to 15 committed, readable, non-guard bytes across page boundaries, and `decode_clwb` matches only valid `clwb m8` encodings (requiring `0x66`, `mod != 3`, `/6`, valid prefixes/REX/SIB/displacement, length ≤ 15; rejecting `tpause`, `xsaveopt`, `clflushopt`, `LOCK`/`F2`/`F3`, and truncated/malformed bytes). - On hosts with `CLFLUSHOPT` (Skylake through Coffee Lake / Comet Lake, CPUID leaf 7 EBX bit 23), atomically sets bit 3 of the ModRM byte (`/6` → `/7`), flushes the instruction cache, restores page protection, and resumes at the same `Rip` so hot loops run at native speed without further traps. Concurrent threads trapping on the same `Rip` retry cleanly once patched. - On older hosts without `CLFLUSHOPT` (Haswell/Broadwell) or non-writable pages, advances `Rip` past the instruction and logs once per RVA. ## Checks Ran on Windows x64 (`rustc 1.97.1`, MSVC 14.44): - `cargo check --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12` — passed (0 warnings) - `cargo test --locked -p tuxbox-d3d12 --lib` — 33/33 passed, including: - exact byte encodings from #71 and all ModRM/SIB/displacement/RIP-relative addressing forms - exhaustive 256-value ModRM, 256-value leading-byte prefix, and 768-case SIB decoder sweeps - straddling cross-page (`RX` / `RWX`), `PAGE_NOACCESS`, `PAGE_GUARD`, out-of-bounds `Rip`, and non-`clwb` (`ud2`, `lock clwb`) rejection tests - 8-thread concurrent race on a single `PAGE_EXECUTE_READ` `clwb` site, verifying single atomic patch, protection restoration, and live execution of the rewritten `clflushopt` - Haswell fallback (`!clflushopt`) `Rip` advancement and single-log behavior - `cargo test --locked -p tuxbox-dxbc --features native-bindings` — 51/51 passed - `cargo test --locked -p tuxbox-xg-layout` — 14/14 passed - `cargo test --locked --manifest-path tools/shader-compiler/Cargo.toml` — 82/82 passed - Linked release `d3d12_xs.dll` with MSVC `link.exe`, verified `AddVectoredExceptionHandler` and `RemoveVectoredExceptionHandler` imports, and ran a 5× `LoadLibraryW` / `FreeLibrary` attach/detach cycle. Note: I do not have a pre-Ice Lake Intel CPU or the retail executable locally, so end-to-end verification on a non-`CLWB` host (such as the i9-9900 in #71) will need confirmation on that hardware.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:29:38 +00:00
github manually merged commit 371c839ce0 into gh-pr/80/base 2026-11-26 06:29:40 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.