From 85b8f5b7f098223a02267667b391ff3d7e984c14 Mon Sep 17 00:00:00 2001 From: ImLele69 Date: Wed, 25 Nov 2026 01:58:20 +0100 Subject: [PATCH] cpu_emulation: rewrite CLWB in place at its first #UD (RT-CLWB-PATCH) 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 Claude-Session: https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc --- runtime/src/cpu_emulation.h | 35 +++++++++++++++++++++++++++++ runtime/test/cpu_emulation_test.cpp | 11 +++++++++ 2 files changed, 46 insertions(+) diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index 8df3b55d0a..777a536bd7 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h @@ -350,6 +350,40 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, return ClzeroLoopResult::ok; } +// CLWB is rewritten in place at its first #UD, so a loop over a buffer faults once instead of once per cache line. +// The retail title's GPU upload worker flushes whole buffers that way (exe+0x532f1b0 on 1.0.2.1); under Wine a #UD +// costs tens of microseconds, which kept that worker busy for minutes per buffer and the boot waiting on it. +// 66 0F AE /6 (CLWB) becomes 66 0F AE /7 (CLFLUSHOPT): the same operand and length, and it writes the line back too +// (then evicts it). A host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same length that touches no memory. +// LEONIDA_CLWB_PATCH=0 keeps emulating every execution instead. +inline bool patch_clwb(uint64_t rip, const uint8_t* code, std::size_t length) { + static const int mode = [] { + char value[4]{}; + if (GetEnvironmentVariableA("LEONIDA_CLWB_PATCH", value, sizeof(value)) > 0 && value[0] == '0') return 0; + int r[4]; + __cpuidex(r, 7, 0); + return ((r[1] >> 23) & 1) ? 1 : 2; // EBX bit 23: CLFLUSHOPT + }(); + if (!mode) return false; + std::size_t at = 0; + while (at + 2 < length && !(code[at] == 0x0F && code[at + 1] == 0xAE)) ++at; + if (at + 2 >= length) return false; + uint8_t patched[3] = {0x0F, 0xAE, uint8_t(code[at + 2] | 0x08)}; // reg field 6 -> 7 + if (mode == 2) patched[1] = 0x1F, patched[2] = uint8_t(code[at + 2] & ~0x38); // reg field 0 + void* const where = reinterpret_cast(rip + at); + DWORD old = 0; + if (!VirtualProtect(where, sizeof patched, PAGE_EXECUTE_READWRITE, &old)) return false; + std::memcpy(where, patched, sizeof patched); + VirtualProtect(where, sizeof patched, old, &old); + FlushInstructionCache(GetCurrentProcess(), where, sizeof patched); + static std::atomic sites{0}; + const unsigned n = ++sites; + if (n <= 4 || !(n & (n - 1))) + std::fprintf(stderr, "[rt] cpu: CLWB at %p rewritten as %s (%u sites so far)\n", reinterpret_cast(rip), + mode == 1 ? "CLFLUSHOPT" : "a NOP", n); + return true; +} + inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; CONTEXT* c = info->ContextRecord; @@ -358,6 +392,7 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { std::memcpy(code, reinterpret_cast(c->Rip), avail); const Decoded d = decode(code, avail); if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; + if (d.op == Op::clwb && patch_clwb(c->Rip, code, d.length)) return EXCEPTION_CONTINUE_EXECUTION; EXCEPTION_RECORD fault; if (d.op == Op::clzero && !d.addr32 && d.length == 3) { const uint64_t fault_rip = c->Rip; diff --git a/runtime/test/cpu_emulation_test.cpp b/runtime/test/cpu_emulation_test.cpp index f94f080443..824c387183 100644 --- a/runtime/test/cpu_emulation_test.cpp +++ b/runtime/test/cpu_emulation_test.cpp @@ -334,6 +334,17 @@ void test_live(const ce::Host& host) { uint32_t marker = 0; check(run(wb, lines, &marker, &ex) && marker == 1, "clwb ran and execution continued after it"); std::printf("clwb: %s\n", path(host.clwb)); + if (!host.clwb) { + // Both sites were rewritten at their first #UD: CLFLUSHOPT (reg field 7) or the same-length NOP. + const auto rewritten = [&](int at) { + return (wb.mem[at] == 0xAE && (wb.mem[at + 1] & 0x38) == 0x38) || + (wb.mem[at] == 0x1F && (wb.mem[at + 1] & 0x38) == 0); + }; + check(rewritten(4) && rewritten(8), "clwb sites rewritten in place"); + check(wb.mem[10] == 0x01 && wb.mem[11] == 0x08, "the rewritten clwb keeps its SIB and displacement"); + marker = 0; + check(run(wb, lines, &marker, &ex) && marker == 1, "the rewritten code runs again"); + } // A #UD the handler does not know stays an illegal-instruction exception: ud2; ret Code ud({0x0F, 0x0B, 0xC3});