Skip to content

Console CPU instructions a PC CPU lacks are emulated: clwb, clzero, r… - #151

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
gbuyssens:pr/console-insn
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
gbuyssens:pr/console-insn

Conversation

@gbuyssens

Copy link
Copy Markdown
Contributor

…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

Copy link
Copy Markdown

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:

  1. CLZERO loop fast path. A retail game's memset clears large blocks with CLZERO in one of two loop shapes:

    • clzero; add rax, 0x40; cmp rax, LIM; jb clzero
    • mov rax, P; add P, 0x40; clzero; cmp P, LIM; jb mov

    Emulating 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, Emulate the console CPU's AMD-only instructions on Intel CPUs #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 per memset instead 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.

  2. 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.rs on #153's branch (ShyVortex:cpu/amd-only-instructions). Tested on an i7-12700H.

@Vencite

Vencite commented Nov 23, 2026

Copy link
Copy Markdown

tested this on my i9 14900k and rtx 4080 super (driver 32.0.16.2124). both main (956a476) and the pr parent (9add721) crashed with 0xC000001D at the same spot, before any Present calls.

with 236cc74, 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.

theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
… render-target and texture-address fixes

Open upstream pull requests applied as they are (their authors' work):
- xcl-dev#151 (gbuyssens): clzero / clwb / rdpru / monitorx / mwaitx / rdpid emulated
  on CPUs that lack them (HANDOFF.md hunk left out, it did not apply).
- xcl-dev#163, xcl-dev#170 (Sr-0w): Xbox D16S8 depth formats, depth + UAV buffers, console
  mesh pipeline streams, null stand-in shaders.
- xcl-dev#168 (ShyVortex): GetGPUVirtualAddress of a placed texture.

Local changes:
- xg: XGTextureAddressComputer xcl-dev#10 as GetResourceTiling, from the layout model
  (not checked against the console's library).
- d3d12x: a UAV of a texture made without ALLOW_UNORDERED_ACCESS is a null UAV
  (the real call removed the device); a committed texture gets memory when the
  title asks for its address; clear values and view descriptions get pc_format;
  formats 86 / 115 -> R8G8B8A8, 116 / 117 -> R16G16B16A16_FLOAT.
- Debug layer log: 8 messages per id instead of 200 in all.
- The earlier "AS slot ignored" workaround stays but is no longer reached (xcl-dev#170).
- CHANGES.txt, README status and installer version for build 3.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@MiguelMachado-dev

Copy link
Copy Markdown
Contributor

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 run in both cases.

  • main at 956a476 exited with 0xC000001D at the CLZERO instruction.
  • 956a476 with the source changes from 236cc749 logged 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_instructions passed 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.

…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.
@afpruivo

Copy link
Copy Markdown

I tested a local CLZERO regression on Windows with an Intel Core i5-13600K. I ran the same synthetic console-title fixture through xcl run on both revisions:

  • main at 956a476bf738: reached CLZERO, then exited with 0xC000001D (STATUS_ILLEGAL_INSTRUCTION).
  • This PR at 236cc7495479: XCL logged that it emulated CLZERO. 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 missing System32\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

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 on main 956a476, so the PR head is the merge result). The earlier Intel reports above ran 236cc749. I compared the two: 0b28a39 is a rebase of it, insn.rs is byte-identical, and the eh.rs change 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_INSTRUCTION with a synthetic CONTEXT through cpp_exception_logger. It then compares the result with the same instruction executed for real. The same probe runs on main as the negative control.

case this PR main
clzero (0F 01 FC), rax at line offsets 0x45 / 0x80 / 0xFF / 0x7C1 4/4: same bytes cleared as the hardware on a 4 KiB 0xA5 buffer; rip +3 4/4 not handled
rdpid rax (F3 0F C7 F8) and rdpid r9 (F3 41 0F C7 F9), pinned to each logical CPU 32/32 CPUs equal the hardware's TSC_AUX; rip +4 / +5 not handled
rdpru, ecx = 0 / 1 / 2 handled, rip +3. ecx 0/1 give a QPC value (the hardware gives MPERF / APERF); ecx 2 gives 0, same as the hardware 3/3 not handled
monitorx, mwaitx, clwb [rax], clwb [rsp+8], clwb [r8+disp32], clwb [rip+disp32] 6/6 stepped over by their exact length (3, 3, 4, 6, 9, 8); rax unchanged 6/6 not handled
ud2 left to the title (handler returns 0, rip unchanged) same
67 0F 01 FC (clzero with an address-size prefix) not handled not handled

Totals: the PR passes 14/14 of the cases it targets and main fails 14/14. insn::tests::decodes_the_console_only_instructions passes on the PR.

Against the reports.

From reading the diff (none of these block the PR):

Not tested:

  • a real #UD on an Intel CPU (the earlier reports cover that on 236cc749);
  • the retail title;
  • RDPID on a machine with more than 64 logical processors (processor groups);
  • the cost of the exception path.

@Jefter5549

Copy link
Copy Markdown
Contributor

Tested 0b28a39 on 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 (+0x4ecc84a for clwb, +0x4e8aa70 for clzero, and +0x4fb0bd7 for 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) with write_bytes and skipping to rip + 12 brought 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) via VirtualProtect avoids 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) before write_bytes avoids 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

Copy link
Copy Markdown

Can confirm it runs with this patch on my Intel CPU where it previously crashed

@ivall
ivall merged commit 0636e7a into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants