d3d12: rewrite unsupported clwb to clflushopt on pre-Ice Lake CPUs #80
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/brcly--TuxBox!80
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/80/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCSummary
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 raisesEXCEPTION_ILLEGAL_INSTRUCTION(0xc000001d) on pre-Ice Lake Intel desktop CPUs.This change adds on-demand
clwbcompatibility insided3d12_xs:runtime/d3d12/src/lib.rs,survey.rs): Registers the existing vectored exception handler whend3d12_xs.dllattaches (via.CRT$XCU, avoiding aDllMainduplicate-symbol collision withlibcmt's stub under MSVClink.exe) and removes it viaatexiton dynamic unload, so titleclwbsites are covered even beforeD3D12XboxCreateDevice.clwbdecoder & zero-overhead fast path (runtime/d3d12/src/clwb.rs):CLWBreturn immediately without locking or touching code pages.#UDinside the title image,read_code_bytessafely reads up to 15 committed, readable, non-guard bytes across page boundaries, anddecode_clwbmatches only validclwb m8encodings (requiring0x66,mod != 3,/6, valid prefixes/REX/SIB/displacement, length ≤ 15; rejectingtpause,xsaveopt,clflushopt,LOCK/F2/F3, and truncated/malformed bytes).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 sameRipso hot loops run at native speed without further traps. Concurrent threads trapping on the sameRipretry cleanly once patched.CLFLUSHOPT(Haswell/Broadwell) or non-writable pages, advancesRippast 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:RX/RWX),PAGE_NOACCESS,PAGE_GUARD, out-of-boundsRip, and non-clwb(ud2,lock clwb) rejection testsPAGE_EXECUTE_READclwbsite, verifying single atomic patch, protection restoration, and live execution of the rewrittenclflushopt!clflushopt)Ripadvancement and single-log behaviorcargo test --locked -p tuxbox-dxbc --features native-bindings— 51/51 passedcargo test --locked -p tuxbox-xg-layout— 14/14 passedcargo test --locked --manifest-path tools/shader-compiler/Cargo.toml— 82/82 passedd3d12_xs.dllwith MSVClink.exe, verifiedAddVectoredExceptionHandlerandRemoveVectoredExceptionHandlerimports, and ran a 5×LoadLibraryW/FreeLibraryattach/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-
CLWBhost (such as the i9-9900 in #71) will need confirmation on that hardware.