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

Open
github-bot wants to merge 1 commit from github-bot/gh-1472 into main AGit
Owner

@⁠ImLele69 on GitHub · 2026-11-25 00:58 UTC · original
ImLele69:rt-clwb-patch → main · +46 −0 · 2 files · 1 commits · open

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

  • cpu_emulation_test under 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).
  • Retail 1.0.2.1 boot on the same machine (RTX 2070 SUPER, NVIDIA 620.70.06, Fedora 44, 16 GB RAM) on #984 + #968 with this change: 4 CLWB sites rewritten, the worker leaves the loop, and with an input script that ticks the privacy/terms boxes the title gets past the first-run pages (session 0xd to 0) into the story load. Without it, the same setup stays in 0xd.
  • Not tested on a host that has CLWB: the handler never sees the instruction there, so nothing changes.

No game data in the change or in this text.

AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc

**@⁠ImLele69** on GitHub · 2026-11-25 00:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1472) `ImLele69:rt-clwb-patch` → `main` · +46 −0 · 2 files · 1 commits · open 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 - `cpu_emulation_test` under 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). - Retail 1.0.2.1 boot on the same machine (RTX 2070 SUPER, NVIDIA 620.70.06, Fedora 44, 16 GB RAM) on #984 + #968 with this change: 4 CLWB sites rewritten, the worker leaves the loop, and with an input script that ticks the privacy/terms boxes the title gets past the first-run pages (session `0xd` to `0`) into the story load. Without it, the same setup stays in `0xd`. - Not tested on a host that has CLWB: the handler never sees the instruction there, so nothing changes. No game data in the change or in this text. AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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 #984 + #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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:53:02 +00:00
Author
Owner

@⁠Gefik23 on GitHub · 2026-11-25 07:04 UTC · original

Tested 85b8f5b7f 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.

**@⁠Gefik23** on GitHub · 2026-11-25 07:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1472#issuecomment-7155453986) Tested 85b8f5b7f 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.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 07:53 UTC · original

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.

**@⁠whoalin1** on GitHub · 2026-11-25 07:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1472#issuecomment-7156361253) 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.
Author
Owner

@⁠3L1A5-1 on GitHub · 2026-11-25 15:25 UTC · original

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)

**@⁠3L1A5-1** on GitHub · 2026-11-25 15:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1472#issuecomment-7166828512) 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)
Author
Owner

@⁠Krashgamer on GitHub · 2026-11-26 03:32 UTC · original

Tested 85b8f5b7f 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 88479ff65 + #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.
**@⁠Krashgamer** on GitHub · 2026-11-26 03:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1472#issuecomment-7181804098) Tested 85b8f5b7f 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 88479ff65 + #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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1472/head:github-bot/gh-1472'
git switch 'github-bot/gh-1472'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1472'
git switch 'github-bot/gh-1472'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1472'
git switch 'github-bot/gh-1472'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1472'
git switch 'main'
git merge --squash 'github-bot/gh-1472'
git switch 'main'
git merge --ff-only 'github-bot/gh-1472'
git switch 'main'
git merge 'github-bot/gh-1472'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.