Skip to content

cpu_emulation: rewrite CLWB in place at its first #UD (RT-CLWB-PATCH) - #1472

Open
ImLele69 wants to merge 1 commit into
ProjectLeonida:mainfrom
ImLele69:rt-clwb-patch
Open

ImLele69 wants to merge 1 commit into
ProjectLeonida:mainfrom
ImLele69:rt-clwb-patch

Conversation

@ImLele69

Copy link
Copy Markdown
Contributor

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.h emulates it. The retail title's GPU upload worker flushes whole buffers with CLWB in a 64-byte loop (exe+0x532f1b0 on 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 session 0xd. The first-run pages never finished.

The handler now rewrites the instruction at its first #UD:

  • 66 0F AE /6 (CLWB) becomes 66 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);
  • a host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same length that touches no memory.

Each site then faults once. LEONIDA_CLWB_PATCH=0 keeps the per-execution emulation.

Tested

No game data in the change or in this text.

AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc

On a host without CLWB every execution of the instruction raised #UD and the vectored handler emulated it. The
retail title's GPU upload worker flushes whole buffers with CLWB in a 64-byte loop (exe+0x532f1b0 on 1.0.2.1), so a
buffer cost one exception per cache line; under Wine that kept the worker busy for minutes and the main thread
polling its event in session 0xd, so the boot never left the first-run pages.

The handler now rewrites the instruction at its first #UD: 66 0F AE /6 becomes 66 0F AE /7 (CLFLUSHOPT), the same
operand and length, which also writes the line back; a host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same
length. LEONIDA_CLWB_PATCH=0 keeps the old per-execution emulation.

Tested: cpu_emulation_test under Wine 11.21 on an i9-9900KF (no CLWB, has CLFLUSHOPT): both sites rewritten, SIB and
displacement kept, the code runs again. Retail boot on the same machine with ProjectLeonida#984 + ProjectLeonida#968 (RTX 2070 SUPER, Fedora 44):
the worker that sat in the CLWB loop at 420, 540 and 660 s now moves on, and the title gets past the first-run pages
into the story load.

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc
@Gefik23

Gefik23 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested 85b8f5b on 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 reports CLWB 1 (clwb: native), so it covers the native-CLWB host rather than the #UD rewrite path the i9-9900KF exercised.
  • Full 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.

@whoalin1

Copy link
Copy Markdown
Contributor

Independent verification (CPU-only change): built cpu_emulation_test from the PR head — all checks pass; LEONIDA_CLWB_PATCH=0 disable path also verified. A direct harness exercising patch_clwb on RWX memory confirms three CLWB encodings (66 0F AE /6 incl. SIB+disp forms) rewrite to CLFLUSHOPT (/7) with ModRM reg field 6→7 and all operand bytes preserved, and the [rt] cpu: CLWB … rewritten log lines emit correctly. Minimal additive diff (+35 in cpu_emulation.h, +11 test). AI-assisted verification.

@3L1A5-1

3L1A5-1 commented Nov 25, 2026

Copy link
Copy Markdown

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: main 98c2f7777 plus #1756, #1742, #1705 and #1897, without and then with this PR, #1576 and #1499. All three cherry-picked cleanly.

  • Without them: the boot freezes after the story-load prompt. Session state 0xd is reached at about 170 s. From about 180 s to 600 s there are about 2 PresentX per 30 s (0.07/s), and the submit thread is idle at about 2 submissions per 14 s.
  • With them: about 1,050 PresentX per 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.
  • With the first-run pages also driven by the input script: the boot leaves 0xd at about 245 s and starts the streaming load. Registered images go from about 1,420 to about 29,200 within 50 s.
  • The next stop here is host memory. The run was ended at 300 s by my guard (free physical memory below 1.5 GB on this 16 GB box, 13.5 GB private), not by a crash.

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)

@Krashgamer

Copy link
Copy Markdown
Contributor

Tested 85b8f5b on 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 88479ff + #2138 + #1914 + #1886 + #2179), same scripted presses (A at 60/75/90 s), boot.ps1:

  • Without it: from the A on the landing prompt the title showed one frame every 7.0 s (0.14 frames/s) for the rest of the run, in four boots (also with LEONIDA_COMPUTE_WARMUP=0, so not compile load). Thread stacks at 150/180/205 s: one title worker was inside the #UD handler at game+0x532f1b0 in all three samples, a clwb/add 40h/cmp/jbe loop over each decoded movie plane (one #UD per 64-byte line), while the main thread polled that job with Sleep(1) at game+0x23cf729.
  • With it: four sites rewritten (+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.

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.

5 participants