Console CPU instructions a PC CPU lacks are emulated: clwb, clzero, r… #151
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!151
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/151/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?
@gbuyssens on GitHub · 2026-11-23 16:39 UTC · original
gbuyssens:pr/console-insn→main· +197 −0 · 4 files · 1 commits · merged 2026-11-23 20:51 UTC…dpru, monitorx / mwaitx, rdpid
A title compiled for the console's Zen 2 raises STATUS_ILLEGAL_INSTRUCTION on an Intel CPU before Ice Lake / Alder Lake (clwb, sha) or on any Intel CPU (clzero, rdpru, monitorx, mwaitx). insn::decode recognises them at the faulting address; the vectored handler emulates them and steps over. Other illegal instructions and ud2 are logged with their code bytes, in the first-chance handler and in the crash line.
@ShyVortex on GitHub · 2026-11-23 16:55 UTC · original
I opened #153 for the same problem without seeing this one first. I'm closing it in favour of this PR, which covers more instructions (CLWB, RDPID) and has the prefix-aware decoder.
Two things from #153 that could be useful on top of this, as a follow-up once this is merged, if you want them:
CLZERO loop fast path. A retail game's
memsetclears large blocks with CLZERO in one of two loop shapes:clzero; add rax, 0x40; cmp rax, LIM; jb clzeromov rax, P; add P, 0x40; clzero; cmp P, LIM; jb movEmulating one line per fault costs a full exception round trip per 64 bytes. When the faulting CLZERO is the body of one of these loops, #153 clears the loop's whole range in one go, sets the registers and flags the loop would leave, and resumes after the
jb. That makes it one fault permemsetinstead of one per cache line. It checks that the range is committed and writable first, and otherwise lets it fault as the real instruction would.Tests that execute the instructions. They copy small functions (both loop shapes, a lone CLZERO at an unaligned address, MONITORX + MWAITX) into executable memory, call them, and check exactly which bytes were cleared. On an Intel CPU that goes through the handler; on AMD the instructions run natively, so the same test checks the emulation against the hardware.
The code is in
crates/xcl/src/amd_insn.rson #153's branch (ShyVortex:cpu/amd-only-instructions). Tested on an i7-12700H.@Vencite on GitHub · 2026-11-23 18:41 UTC · original
tested this on my i9 14900k and rtx 4080 super (driver 32.0.16.2124). both main (
956a476bf7) and the pr parent (9add721) crashed with 0xC000001D at the same spot, before any Present calls.with 236cc749, xcl logged CLZERO emulation at that location. tried two runs with a fresh cache each time, both reached one Present and exited with code 0 at the 25-second limit.
used XCL_NETWORK=0 for all runs. haven't verified the actual frame contents yet, but it definitely gets further than before.
@MiguelMachado-dev on GitHub · 2026-11-23 19:52 UTC · original
I tested the CLZERO fallback on a Core i7-14700KF (Windows 11 build 26300, Rust 1.97.1 MSVC). I loaded the same standalone Rust x64 PE with
xcl runin both cases.mainat956a476exited with0xC000001Dat the CLZERO instruction.956a476with the source changes from236cc749logged CLZERO emulation and exited with code 0. The fixture passed an unaligned address into the middle of three aligned 64-byte lines. It checked that the middle line was zeroed and that both neighboring lines still held 0xa5.cargo test --locked -p xcl --lib insn::tests::decodes_the_console_only_instructionspassed on the patched build.The fixture is synthetic and exercises the exception handler via the normal XCL loader. It covers the CLZERO path reported in #173. AI-assisted with Codex.
@afpruivo on GitHub · 2026-11-23 19:57 UTC · original
I tested a local CLZERO regression on Windows with an Intel Core i5-13600K. I ran the same synthetic console-title fixture through
xcl runon both revisions:mainat956a476bf738: reachedCLZERO, then exited with0xC000001D(STATUS_ILLEGAL_INSTRUCTION).236cc7495479: XCL logged that it emulatedCLZERO. The fixture verified that exactly one aligned 64-byte cache line was zeroed, both neighboring lines retained their original bytes, and execution continued to exit code 0.The focused test used synthetic data only and covers
CLZERO. In a separate run of the PR's end-to-end suite, this regression passed again. The existing full-title test failed its startup assertion because this machine reported a missingSystem32\xgameruntime.dll.Would you like me to submit the regression test? I can send it as a small follow-up PR once this lands, or share the patch for inclusion here.
AI-assisted with Codex.
@Kalmarv on GitHub · 2026-11-23 20:34 UTC · original
AI-assisted review (Claude Code). Windows 11 (10.0.26200), Ryzen 9 9950X, Rust 1.99.0 MSVC, debug profile. Synthetic inputs only, no game files. No GPU needed.
Tested
0b28a39(one commit directly onmain956a476, so the PR head is the merge result). The earlier Intel reports above ran236cc749. I compared the two:0b28a39is a rebase of it,insn.rsis byte-identical, and theeh.rschange is the same. So those results carry over to the current head.Emulation vs. hardware. This is an AMD CPU, so every one of these instructions also runs natively here. That makes the CPU itself the reference.
My probe registers a 64 KiB page as a mapped module, writes each encoding into it, and feeds
STATUS_ILLEGAL_INSTRUCTIONwith a syntheticCONTEXTthroughcpp_exception_logger. It then compares the result with the same instruction executed for real. The same probe runs onmainas the negative control.mainclzero(0F 01 FC), rax at line offsets 0x45 / 0x80 / 0xFF / 0x7C1rdpid rax(F3 0F C7 F8) andrdpid r9(F3 41 0F C7 F9), pinned to each logical CPUrdpru, ecx = 0 / 1 / 2monitorx,mwaitx,clwb [rax],clwb [rsp+8],clwb [r8+disp32],clwb [rip+disp32]ud267 0F 01 FC(clzero with an address-size prefix)Totals: the PR passes 14/14 of the cases it targets and
mainfails 14/14.insn::tests::decodes_the_console_only_instructionspasses on the PR.Against the reports.
+0x4e8aa70and MONITORX at+0x4fb0bd7(its two crash sites) are covered, and so are MWAITX and RDPRU, which it lists as unhandled. It also lists STGI/CLGI/SKINIT/INVLPGA (0F 01 DC–DF). The PR doesn't cover those. They are SVM and privileged, so ring 3 faults on them on AMD too, and no title should run them.+0x4e8aa70is the sameclzeroloop, so it's covered. z961089510-dev's Intel run with this PR reached presents. Their next stop,0x80000003at+0x344196, also happens on AMD without this PR (engme66), so it is a separate issue.From reading the diff (none of these block the PR):
0x67. Forclzero, a 67 prefix means "use eax". Compilers don't normally emit it, but if one appears it falls through to the title's handler, as onmain.readable(line, 64)beforewrite_bytes. On a committed read-only line that check passes, and the write then faults inside the VEH, where the real instruction would have faulted at the title's rip. A writability check would match the hardware. I didn't test this.Game Init. ShyVortex's loop fast path (#153) addresses that. I didn't measure the cost here.mwaitxas a no-op turns the title's wait into a busy spin. #184's patch used_mm_pause(). Aspin_loop()hint would be cheap.insn.rs's module doc names SHA among the instructions older Intel CPUs lack, butdecodehas no SHA-NI case (0F 38 C8–CD,0F 3A CC). Either the doc or the decoder should change.EXTRQ/INSERTQ,66/F2 0F 78/79;MOVNTSS/MOVNTSD,F3/F2 0F 2B), which no Intel CPU has, and LLVM'sznver2target enables it. That is a possible further #UD source. Whether the title uses any of it is unknown.emulate_console_onlyonly acts whenmodules::image_base_for_addr(rip)is set, so the same instructions in code outside a mapped module (JIT code, copied thunks) still go to the title. #184's and #173's sites are insidegame.exe.AddVectoredExceptionHandler(0, …), so a title's own first-chance VEH sees the #UD first. Frame-based handlers still come after it.Not tested:
236cc749);@Jefter5549 on GitHub · 2026-11-23 20:48 UTC · original
Tested
0b28a39on a Xeon E5-2666 v3 (Haswell-EP) with an RTX 2060 Super (driver 572.70), Windows 11.Can confirm this gets past the 0xC000001D crashes on Haswell (
+0x4ecc84afor clwb,+0x4e8aa70for clzero, and+0x4fb0bd7for monitorx/mcommit). The title initializes, streams assets and starts presenting frames.A few notes from running it on older hardware:
• CLZERO loop overhead: stepping one 64-byte line per exception causes an exception storm during memory allocation. On this machine it took over 18,000 VEH dispatches in a few seconds, dropping presentation to ~0.2 fps (36 frames in 180s). Catching the 12-byte loop (
clzero; add rax, 0x40; cmp rax, REG; jb -12), clearing[rax..REG)withwrite_bytesand skipping torip + 12brought it up to 15-20 fps (1,153 frames in the same 180s).• CLWB in tight loops: hot-patching the instruction bytes with NOPs (
0x90) viaVirtualProtectavoids repeated VEH round trips on subsequent frames/iterations. Host memory is cache-coherent anyway.• Memory check: checking
is_writable(VirtualQuery for MEM_COMMIT + PAGE_READWRITE) beforewrite_bytesavoids taking an unhandled AV inside the VEH if the destination is read-only.Can submit the loop fast-path and NOP-patching as a follow-up PR once this gets merged.
@mosin311aa on GitHub · 2026-11-23 20:51 UTC · original
Can confirm it runs with this patch on my Intel CPU where it previously crashed
@ivall on GitHub · 2026-11-23 20:52 UTC · original
Merged (main
2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main38b05b7:cargo build --workspace && cargo test --workspace -- --test-threads=4- 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.