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

Open
opened 2026-11-26 20:21:27 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1472
Author: @ImLele69
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: rt-clwb-patch


📝 Commits (1)

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

📊 Changes

2 files changed (+46 additions, -0 deletions)

View changed files

📝 runtime/src/cpu_emulation.h (+35 -0)
📝 runtime/test/cpu_emulation_test.cpp (+11 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1472 **Author:** [@ImLele69](https://github.com/ImLele69) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt-clwb-patch` --- ### 📝 Commits (1) - [`85b8f5b`](https://github.com/ubiquitarianism/leonida/commit/85b8f5b7f098223a02267667b391ff3d7e984c14) cpu_emulation: rewrite CLWB in place at its first #UD (RT-CLWB-PATCH) ### 📊 Changes **2 files changed** (+46 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/cpu_emulation.h` (+35 -0) 📝 `runtime/test/cpu_emulation_test.cpp` (+11 -0) </details> ### 📄 Description 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1500
No description provided.