Commit Graph

9 Commits

Author SHA1 Message Date
3107c8a2a8 Merge pull request #2035 from IlyassRS3/fix/rt-cpu-emulation-clzero-region-and-bounds
runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation
2026-11-28 18:53:57 +01:00
5d677b7ba2 cpu_emulation: a CLWB #UD on a site another thread rewrote continues at it (RT-CLWB-PATCH)
Threads that fault on the same CLWB site together lost the race: a #UD handled
after another thread rewrote the site decoded CLFLUSHOPT (or the NOP), did not
know it and returned EXCEPTION_CONTINUE_SEARCH, which ends the title.

- The handler continues at a site that already holds the form written on this
  host (or the NOP's halfway state): the CPU runs it.
- One lock around unprotect, re-check, write and protect again, so two threads
  never interleave on a page; a site found rewritten under it is not written.
- The NOP is written opcode first (66 0F 1F /6 in between is a NOP too).
- The sites counter counts each site once, by the thread that wrote it.

cpu_emulation_test calls the handler with a made-up #UD, so it runs on hosts
with CLWB: first #UD, a second one on the rewritten site, and 16 threads on
2048 fresh execute-read sites. Before the fix 27958 of 32768 concurrent #UDs
were passed on, 4810 rewrites were counted for 2048 sites and pages were left
writable; with it all checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 05:28:00 +01:00
7f56e6a50b runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation 2026-11-25 19:39:50 +01:00
85b8f5b7f0 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc
2026-11-25 01:58:20 +01:00
3b8c1555d8 cpu_emulation: update CMP flags on partial CLZERO batches and post-prefix faults 2026-11-24 07:25:21 +01:00
361e8455a2 cpu_emulation: run a whole CLZERO zeroing loop in one emulated step 2026-11-24 05:54:10 +01:00
978d525d99 cpu_emulation: INSERTQ register form reads len from xmm2[69:64]; zero xmm1[127:64]; AMD prefix forms; no SEH
- INSERTQ xmm, xmm took the length and index from swapped fields. The APM (vol. 4) has the length in
  xmm2[69:64] and the index in xmm2[77:72]; a Ryzen 9800X3D and a Ryzen 5700X agree (TheHoracio,
  Mayzkl on #7111). The measured 0x0810 case is a unit check now.
- INSERTQ/EXTRQ zero xmm1[127:64], as the Zen 3 measured in the same review does in all four forms.
- Decode REX bytes among the legacy prefixes (a non-final REX is ignored) and accept F2/F3 on CLZERO,
  as Zen 3 runs them (#7055's reviews); F2/F3 stay #UD on MONITORX/MWAITX.
- Check the CLZERO line's page and the instruction bytes' readability with VirtualQuery instead of
  catching a fault in a local __try, which clang-cl without /EHa does not cover for inline stores
  (venetanji on #7111).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:41:46 +01:00
3fe388b39d cpu_emulation: CLZERO on an unwritable line raises the CPU's write fault; accept 66 on 0F 01 FC/FA/FB
A line CLZERO cannot write now reaches the next handlers as the access violation (or guard-page
violation) the instruction raises, with the write flag and the line address, instead of the #UD.
The 66 prefix and segment overrides are accepted on CLZERO/MONITORX/MWAITX; F2/F3 (other
instructions there, F3 0F 01 FA is MCOMMIT) and LOCK stay #UD. Points raised by SirKingg on #7055.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:07:09 +01:00
62b410e756 gta6rt: emulate the Zen 2 instructions a desktop CPU may lack (INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB)
The executable uses SSE4a INSERTQ, CLZERO and MONITORX/MWAITX (AMD only) and CLWB (Intel since Ice Lake)
without asking CPUID (docs/cpu_features.md). A host without one raises #UD there. src/cpu_emulation.h adds
a vectored handler that recognises those encodings at the faulting RIP, does the same work on the CONTEXT
and steps past the instruction; gta6rt installs it before any image code runs, after the startup trace's
handler so it runs first. One [rt] line names what the host lacks, one more per instruction on first use.

cpu_emulation_test checks the decoder on hand-written encodings, the bit-field helpers and apply() on a
synthetic CONTEXT, then runs every form for real from an executable buffer: natively on a Zen host,
through the handler on an Intel one, with the same expected values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 16:57:59 +01:00