cpu_emulation: rewrite CLWB in place at its first #UD (RT-CLWB-PATCH) #1472
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1472"
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?
Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T00:58:36Z.
Claimed in #14 as
RT-CLWB-PATCH.What and why
On a CPU without CLWB (Intel desktop before Ice Lake, AMD before Zen 2) every execution of CLWB raises #UD and the vectored handler in
cpu_emulation.hemulates it. The retail title's GPU upload worker flushes whole buffers with CLWB in a 64-byte loop (exe+0x532f1b0on 1.0.2.1), so each buffer costs one exception per cache line. Under Wine an exception costs tens of microseconds, and in thread snapshots (LEONIDA_STACKS) that worker sat in the same loop at 420, 540 and 660 s while the main thread polled its event (WaitForSingleObject(h, 0)in a 1 ms loop) in session0xd. The first-run pages never finished.The handler now rewrites the instruction at its first #UD:
66 0F AE /6(CLWB) becomes66 0F AE /7(CLFLUSHOPT): the same operand and length (only the ModRM reg field changes), and it also writes the line back (and then evicts it);66 0F 1F /0, a NOP of the same length that touches no memory.Each site then faults once.
LEONIDA_CLWB_PATCH=0keeps the per-execution emulation.Tested
cpu_emulation_testunder Wine 11.21 (Staging) on an i9-9900KF (no CLWB, has CLFLUSHOPT): all checks pass, including the new ones (both sites rewritten, SIB and displacement kept, the rewritten code runs again).0xdto0) into the story load. Without it, the same setup stays in0xd.No game data in the change or in this text.
AI-assisted: yes
🤖 Generated with Claude Code
https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc
Imported from ubiquitarianism/leonida. Originally posted by Gefik23 on 2026-11-25T07:04:12Z.
Tested
85b8f5b7fon AMD Ryzen 5 7500F (Zen 4) + NVIDIA GeForce RTX 4060, driver 617.42, Windows 11 Pro build 26200, native Windows (not Wine): pass.Built at this head with
scripts\build_runtime.ps1(MSVC 19.44, VS 2022 Build Tools, Ninja, RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0).ctest -R cpu_emulation -V: passed. This CPU reportsCLWB 1(clwb: native), so it covers the native-CLWB host rather than the #UD rewrite path the i9-9900KF exercised.ctest -C RelWithDebInfo: 308/308 passed (game-data tests skipped, LEONIDA_GAME_DIR unset).AI-assisted (Claude Code ran the build and tests). No game files.
Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-25T07:53:09Z.
Independent verification (CPU-only change): built
cpu_emulation_testfrom the PR head — all checks pass;LEONIDA_CLWB_PATCH=0disable path also verified. A direct harness exercisingpatch_clwbon RWX memory confirms three CLWB encodings (66 0F AE /6incl. SIB+disp forms) rewrite to CLFLUSHOPT (/7) with ModRM reg field 6→7 and all operand bytes preserved, and the[rt] cpu: CLWB … rewrittenlog lines emit correctly. Minimal additive diff (+35 incpu_emulation.h, +11 test). AI-assisted verification.Imported from ubiquitarianism/leonida. Originally posted by 3L1A5-1 on 2026-11-25T15:25:47Z.
Confirmed on another Intel CPU without CLWB, on native Windows: i5-9400F (6 threads; no SSE4a, CLZERO, MONITORX or CLWB), RTX 2060 SUPER, NVIDIA 617.14, Windows 11 26300, retail 1.0.2.1. I compared two 600 s boots of the same tree:
main98c2f7777plus #1756, #1742, #1705 and #1897, without and then with this PR, #1576 and #1499. All three cherry-picked cleanly.0xdis reached at about 170 s. From about 180 s to 600 s there are about 2PresentXper 30 s (0.07/s), and the submit thread is idle at about 2 submissions per 14 s.PresentXper 30 s (35/s), held from 180 s to 600 s (16,089 after 170 s, against 404). The log shows CLWB rewritten as CLFLUSHOPT at 4 sites, and the MONITORX/MWAITX pair rewritten.0xdat about 245 s and starts the streaming load. Registered images go from about 1,420 to about 29,200 within 50 s.So on this CPU the "freeze at 0xd" was the per-cache-line #UD of the CLWB flush loops, plus first-run input.
AI-assisted: yes (Claude Code, Opus 5.5)
Imported from ubiquitarianism/leonida. Originally posted by Krashgamer on 2026-11-26T03:32:10Z.
Tested
85b8f5b7fon an Intel i7-10700K (no SSE4a, CLZERO, MONITORX or CLWB), RTX 3080 10 GB, NVIDIA 610.88, Windows 11 Pro 26200, retail 1.0.2.1, native Windows. AI-assisted (Claude Code, Claude Opus 5.5).On this CPU it is the difference between a stuck and a working loading screen. Same tree (main
88479ff65+ #2138 + #1914 + #1886 + #2179), same scripted presses (A at 60/75/90 s),boot.ps1:LEONIDA_COMPUTE_WARMUP=0, so not compile load). Thread stacks at 150/180/205 s: one title worker was inside the #UD handler atgame+0x532f1b0in all three samples, aclwb/add 40h/cmp/jbeloop over each decoded movie plane (one #UD per 64-byte line), while the main thread polled that job withSleep(1)atgame+0x23cf729.+0x4ecc84a,+0x4e4b680,+0x532f1b0,+0x532f1f0), 16–23 frames/s for the whole 600 s boot (10,502 presents against 1,151 in 900 s before), and the title went on past the loading screen to the first-run settings pages. No faults, no device loss.Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-28T04:44:12Z.
Merged into
mainthrough #2559 (merge commitb1619e0a3), approved by DadaCap. Thanks for the in-place CLWB rewrite!Review of this pull request found one concurrency bug: when a thread's CLWB #UD landed on a site another thread had already rewritten, the handler returned
EXCEPTION_CONTINUE_SEARCH, which crashed the title. OreoFR's follow-up #2559 keeps your rewrite and adds the fix:That test fails on this pull request alone and passes with the fix. It was run on an Intel Core Ultra 7 155H (13 runs out of 13) and on a Ryzen 9 9900X (3 out of 3).
AI-assisted (Claude Code), posted for DadaCap.