Skip to content

runtime: emulate CLWB from the #UD handler (RT-5, #7013) - #7098

Closed
tedin7 wants to merge 15 commits into
ProjectLeonida:mainfrom
tedin7:c2-ud-clwb
Closed

tedin7 wants to merge 15 commits into
ProjectLeonida:mainfrom
tedin7:c2-ud-clwb

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Stacked on the CLZERO dispatcher PR (c2-ud-clzero).

What

Adds a CLWB emulator to the #UD dispatcher in runtime/src/cpu_ud_emulate.cpp: 66 [REX] 0F AE /6 with a memory operand. CLWB writes a cache line back and changes nothing a program can see, so the emulation is a no-op that resumes after the instruction. One function, one line in kTable.

The real work is the exact instruction length: prefix 66, optional REX, 0F AE, ModRM, optional SIB, disp8/disp32, RIP-relative disp32, and a SIB with no base (disp32). An instruction cut off by unreadable memory is not taken.

Also: host_has_clwb() (CPUID 7.0 EBX bit 24), and install() now installs the handler when the host lacks CLWB as well as CLZERO. New test cpu_ud_emulate_clwb_test (separate file and CMake block, to stay clear of the other stacked PRs), and a paragraph in runtime/README.md.

Why

The executable can contain CLWB, which some desktop CPUs lack. On them it raises #UD and the game stops. Not emulated on purpose: 0F AE /7 (CLFLUSH), 66 0F AE /7 (CLFLUSHOPT), 0F AE /6 without 66 (XSAVEOPT), and the register form (mod 11).

@ItsNotRexed: an exe PR, as you asked.

Claim: #6910 (comment)

Tested

On the RTX 4090 pod (AMD EPYC, so CLWB runs natively there), cross-built and run under Wine: cpu_ud_emulate_clwb_test.
The test calls the emulation directly with a synthetic CONTEXT and EXCEPTION_RECORD. Expected lengths are written by hand from the Intel SDM encoding rules, not computed by the code.

  • With the kTable line disabled: 35 checks, 17 failed (all 16 CLWB addressing forms and the end-of-page case report "passed on").
  • With the fix: 30 checks, 0 failed. The 16 forms: [rcx] 4, [rax+rdx*4] 5, [rax+rdx*4+8] 6, [rax+disp32] 8, [rip+disp32] 8, [rsp] 5, [rsp+0x20] 6, [rbp+0] 5, [rdx*4+disp32] 9, [r13+0] 6, [r12] 6, [r12+0x10] 7, [r8+r9*8+disp32] 10, and REX.W / bare REX variants.
  • Negative control (passed on, registers and RIP untouched): CLFLUSH, CLFLUSHOPT (with and without REX), XSAVEOPT, mod 11, /5, /0, 66 0F AF, a NOP.
  • cpu_ud_emulate_test (CLZERO): 19 checks, 0 failed.
  • Under Wine, ReadProcessMemory can read a process's own uncommitted memory, so the "cut off by unreadable memory" checks print SKIP there. They run on Windows. The handler round trip also prints SKIP on this CPU because CLWB runs natively.

Built on #7055 (the CLZERO dispatcher): GitHub shows its commits too until it merges; this PR is the last commit. Known limits (from the review): 67 and segment-override prefixes are not decoded, and unmapped or non-canonical operands are not faulted; the test target sits in the Vulkan-headers block of CMakeLists.txt.

A table-driven dispatcher for STATUS_ILLEGAL_INSTRUCTION (cpu_ud_emulate), installed at startup only when the host CPU has no CLZERO. Unknown instructions and unwritable targets pass on untouched.
@jlric

jlric commented Nov 23, 2026

Copy link
Copy Markdown

Tested 472b0bd99 on CachyOS / Wine 11.22, i7-9700K (CPUID lacks CLWB). The two consecutive CLWB instructions recovered via the installed handler. CLWB suite 35/35, CLZERO suite 20/20. In an A/B with the same local runtime stack, adding this PR and its CLZERO dependency got startup past the illegal-instruction fault to later D3D12 calls. AI-assisted testing.

@dcastell1

Copy link
Copy Markdown
Contributor

Tested e658716 on RTX 5090, 621.24, Windows 11 (CPU-only tests): pass. cpu_ud_emulate and cpu_ud_emulate_clwb → 2/2 passed (0.06 s each).

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested e658716 on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • Built cpu_ud_emulate_test and cpu_ud_emulate_clwb_test with MSVC 14.44, Ninja, RelWithDebInfo. cpu_ud_emulate_clwb_test: 35 checks, 0 failed. cpu_ud_emulate_test (CLZERO): 47 checks, 0 failed.
  • Negative control: the CLWB entry in kTable (runtime/src/cpu_ud_emulate.cpp) disabled, new tests kept: 35 checks, 17 failed. Restored, 35 of 35 pass.
  • Was the handler path really exercised? Only through the synthetic CONTEXT tests. This CPU (AMD Ryzen 7 5800X) has CLWB and CLZERO, so it runs both natively; the real-#UD cases print SKIP ("this CPU has CLWB, it ran natively"; "this CPU has CLZERO"). So the handler was not hit by a real #UD here.
  • Read the diff: emulate_clwb and modrm_length look right for the cases I checked; no defect found.
  • Conflict: runtime: emulate CLWB from the #UD handler (RT-5, #7013) #7098 and runtime: emulate MONITORX/MWAITX from the #UD handler (RT-5, #7013) #7096 both add runtime/src/cpu_ud_emulate.cpp and .h, so they conflict with each other in those two files (each is clean against main).
  • Not done: a run on a CPU without CLWB. Not run against the retail executable.

AI-assisted (Claude Code).

@crastinator

Copy link
Copy Markdown
Contributor

Tested 63d51edd (which now includes #7096) merged onto main 9ce9a792 (clean merge) on a Zen 3 host under Wine: pass.

AMD Ryzen 7 5700X (Zen 3; has CLZERO, MONITORX/MWAITX, CLWB and SSE4a natively), CachyOS, Linux 7.3.5; runtime cross-compiled with clang-cl 23.1.5 + xwin (SDK 10.0.26100), tests run under Wine 11.22.

  • cpu_ud_emulate_clwb_test: 35 checks, 0 failed (including "two CLWB in a row run to the RET"). The one SKIP is "CLWB through the vectored handler: this CPU has CLWB, it ran natively".
  • cpu_ud_emulate_test (CLZERO): 47 checks, 0 failed; cpu_ud_monitorx_test: 10 checks, 0 failed.
  • No new warnings from cpu_ud_emulate.* or the test.

AI-assisted (Claude Code).

@throny throny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 63d51ed on Windows 11, MSVC 14.44, i9-12900K (no SSE4a/CLZERO/MONITORX, has CLWB): pass, one fidelity gap below

This CPU has CLWB (CPUID 7.0 EBX[24] = 1 on all 24 logical CPUs). The real-#UD path for CLWB therefore cannot run here, and no negative control is possible for it. Instead I used the CPU as an oracle for the emulator's length decoding (below).

Commands (CMake + Ninja, RelWithDebInfo, VS 2022 BuildTools vcvars64, Vulkan SDK 1.4.363.0, -j 6):

  • The build at 63d51ed succeeds.
  • The PR's tests:
    • cpu_ud_emulate_clwb_test 35 checks, 0 failed. Its one SKIP is "CLWB through the vectored handler: this CPU has CLWB, it ran natively".
    • cpu_ud_emulate_test 47/0 and cpu_ud_monitorx_test 10/0, both from the merged #7055/#7096.
  • Full ctest -j 6 with the Vulkan ICD hidden (no GPU on this run): 3 of 268 failed. They are d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, at vkCreateInstance (-9) because I hid the driver. They fail the same way at the other heads tested here and are unrelated to this PR.

Length decoding against the CPU (scratch program linking this head's cpu_ud_emulate.cpp, not pushed). The setup is 20,000 random encodings 66 [REX 40-4F] 0F AE /6 with mod 0-2, random rm/SIB/displacement bytes and 15-byte windows. For each one, emulate() gives a length L. The CPU then executes the bytes placed so they end at a page boundary in front of a no-access page: with L bytes it must not fault fetching at the instruction start, and with L-1 bytes it must. Result: 20,000 of 20,000 lengths agree with the hardware, 0 declined. modrm_length (RIP-relative, SIB, SIB base 101 with mod 00, disp8/disp32) is right.

Finding: an invalid address does not fault (runtime/src/cpu_ud_emulate.cpp:105, emulate_clwb)

  • Native CLWB here raises STATUS_ACCESS_VIOLATION for clwb [rcx] with RCX on a PAGE_NOACCESS page, and also with RCX = 0x10.
  • The emulator, given the same instruction and RCX as a synthetic #UD, returns EXCEPTION_CONTINUE_EXECUTION and skips it.
  • A CPU without CLWB therefore runs past a CLWB that would crash on hardware. #7055's CLZERO path in the same file turns that case into the hardware's access violation.
  • Only buggy code would hit this, so I'm not requesting changes. Matching it would need the effective address (the ModRM/SIB decode is already there) plus a VirtualQuery readability check that returns write_fault()-style AV information.

Nits

  • runtime/CMakeLists.txt:2952: cpu_ud_emulate_clwb_test is registered inside if(TARGET Vulkan::Headers) next to arena_size_test. A Windows build without Vulkan headers therefore drops this CPU-only test, while cpu_ud_emulate_test and cpu_ud_monitorx_test sit in the plain if(BUILD_TESTING) Windows block.
  • Stack: this head merges #7096, and #7100 (5881905) merges this branch, so merge 7055 → 7096 → 7098 → 7100. #7111 covers CLWB with its own handler and conflicts with this branch in runtime/src/main.cpp.

AI-assisted.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @throny for the 20,000-length oracle run: that is the check the decoder needed. Two points taken: the CMake registration of the CLWB test is being moved next to the other two CPU-only tests, and the merge order you list (7055 → 7096 → 7098 → 7100) is the intended one. The missing access violation for an invalid CLWB address stays a known gap: it needs the effective address from the ModRM/SIB decode plus a readability check, and CLWB on a bad address is only reachable by a title that would already crash on hardware.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested c011e263 on Windows 11, MSVC 14.51, Ninja, RelWithDebInfo. CPU: AMD Ryzen 7 7700X (Zen 4), which runs CLZERO, MONITORX/MWAITX, CLWB and SSE4a natively. An AI tool helped me to do these tests. No game files; hand-written operands only. Result: pass. This is the first report for this head (the CMake registration of the CLWB test without the Vulkan headers).

CLWB has no result that a program can see, thus a native comparison of its effect is not possible. I did not repeat the instruction-length oracle of @throny.

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.

8 participants