Repository navigation
RT-500: gta6rt emulates the Zen 2 instructions an Intel CPU lacks (INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB) - #7111
Krashgamer wants to merge 5 commits into
Conversation
… desktop CPU may lack The runtime runs the Xbox Series (Zen 2) executable natively, so an instruction the host CPU lacks raises #UD where it is reached. tools/cpu_feature_scan.py streams dumpbin /disasm like inline_scan.py, maps each instruction to its CPUID feature or privilege class (AMD-only, newer-Intel-only, not on Zen 2, privileged, kernel call, host query, baseline), and splits the counts by .pdata range. docs/cpu_features.md records the 1.0.2.1 result (counts only): 19 SSE4a, 10 CLZERO and 4 MONITORX/MWAITX sites (AMD only) and 8 CLWB sites, all real code; the privileged hits are jump-table data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the first Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Q/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>
8f3da11 to
62b410e
Compare
|
Hi @Krashgamer, a heads-up about overlap, and sorry for it: I did not see your "RT-500 part 2" claim on #6750 (I only looked at #6910 before claiming), so I opened four small PRs for the same instructions before your PR landed: #7055 (CLZERO and the |
…ult; 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 ItsNotRexed#7055. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@tedin7 thanks, no harm done, and thanks for the pointers. Both are in #7111 now (3fe388b): a |
devnull34
left a comment
There was a problem hiding this comment.
Verified on Windows 11 (MSVC RelWithDebInfo):
- Built
cpu_emulation_testcleanly. cpu_emulation_test.exepassed all checks:- Validated CLZERO write-fault translation on unwritable lines (
EXCEPTION_ACCESS_VIOLATIONwith write flag and faulting line address). - Validated prefix handling: 0x66 prefix and segment overrides accepted on
0F 01 FC/FA/FB(CLZERO/MONITORX/MWAITX), while F2/F3 and LOCK properly trigger#UD. - SSE4a (INSERTQ/EXTRQ) and MONITORX/MWAITX emulation verified.
- Validated CLZERO write-fault translation on unwritable lines (
- CTest target
48: cpu_emulationpassed 100% (0.01s). - Whitespace clean (
git diff HEAD~1..HEAD --check).
|
Tested Without this PR: in a local combined build that gets the title as far as descriptor-heap setup, the title raises With this PR (and #7013) merged into the same build:
So the Tested and written by Claude (Claude Code). |
|
Tested
I didn't test tedin7's per-instruction PRs (#7055, #7096, #7098, #7100) separately; they cover the same stop. AI-assisted (Claude Code). |
|
Independent Linux/Intel verification of #7111 at Verification item TEST-1685, claim: #7231 (comment), carried into #7333. This verifies the existing RT-500 implementation; implementation ownership and the #7111/#7055 selection remain with their authors/maintainer. Depends on: #7013.
The passing alternate build uses the unmodified published test and header, configured with CPUID from the test reports SSE4a=0, MONITORX=0, CLZERO=0, CLWB=0. The passing companion's actual handler counters are INSERTQ=3, EXTRQ=2, CLZERO=2, MONITORX=1, MWAITX=1, CLWB=2. This covers the real unsupported-instruction path, both INSERTQ/EXTRQ forms, AVX upper-register preservation, CLZERO/read-only CLZERO, MONITORX/MWAITX, CLWB continuation, and unrelated UD2 refusal. A matching Environment: Intel Core i9-10900K, Arch Linux kernel Revisions: published head above; PR base ref at inspection |
|
INSERTQ register form: length and index are swapped (AI-assisted review; no game files).
lo(c, d.dst) = insert_bits(lo(c, d.dst), lo(c, d.src), unsigned(ctl >> 8) & 63, unsigned(ctl) & 63);
Hardware check: on a Ryzen 7 9800X3D (Zen 5, SSE4a present, so Fix that passes on Zen 5 (native) here: lo(c, d.dst) = insert_bits(lo(c, d.dst), lo(c, d.src), unsigned(ctl) & 63, unsigned(ctl >> 8) & 63);plus |
|
Hardware confirmation of @TheHoracio's INSERTQ finding on a second Zen generation, with a full sweep instead of one case: the register form is the only one that differs, and his one-line fix makes all four forms match the CPU exactly. AMD Ryzen 7 5700X (Zen 3, SSE4a native), Windows 11, MSVC 14.44,
So with One more data point, not a bug: in all four forms this Zen 3 zeroes xmm1[127:64] (destination high qword AI-assisted (Claude Code). Synthetic inputs only, no game files. |
… 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 ItsNotRexed#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 (ItsNotRexed#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 ItsNotRexed#7111). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks all, fixed in 978d525 (all checks pass on an i7-10700K, emulated path):
|
|
Data point for RT-500 on a hybrid Intel CPU: i9-12900K (Alder Lake, 8 P + 8 E cores, 24 logical), Windows 11 26300. CPUID read on every logical CPU, each pinned with
So on this host 33 of the 41 sites #7013 flags (insertq 19, clzero 10, monitorx/mwaitx 4) need the emulation this PR adds, and the 8 clwb sites run natively, with no difference between P and E cores. Static read and CPUID only; nothing of the game was run. AI-assisted (Claude Code). |
|
Retested the updated published head This follows my earlier report on
The detached published-head worktree has no tracked changes. Dependency remains #7013 ( Verification follow-up for TEST-1685. AI-assisted with Codex. |
RT-500 part 2 (RT-5 / EXE), claimed in the coordination issue: #6750 (comment)
Depends on: #7013 (this branch sits on it; the change of this PR is the last commit,
gta6rt: emulate ...).What
#7013 measured that the retail executable uses four Zen 2 instructions without asking CPUID: SSE4a
insertq(19 sites),clzero(10),monitorx/mwaitx(4) — no Intel CPU has these — andclwb(8, Intel only since Ice Lake). All are real code. On a host without one, the CPU raises#UDthere and the game stops.runtime/src/cpu_emulation.h: a vectored exception handler forSTATUS_ILLEGAL_INSTRUCTION. It decodes those encodings at the faulting RIP (prefixes, REX, ModRM/SIB/displacement forclwb), does the same work on the thread'sCONTEXTand steps past the instruction.insertq/extrq(immediate and register forms) work on the low quadword of the XMM register;clzerozeroes the 64-byte line rAX points into;monitorxandclwbneed nothing on coherent write-back memory;mwaitxreturns at once, an exit the manual allows. Anything else, and aclzeroon unwritable memory, is left to the next handler unchanged. Semantics are from the AMD64 APM (vol. 3 and 4) and the Intel SDM vol. 2; the sources are named in the header.runtime/src/main.cpp:gta6rtinstalls it before any image code runs, after the startup trace's handler so it runs first and an emulated instruction isn't logged as an exception. One[rt] cpu:line names what the host lacks; one more line the first time each instruction is emulated.runtime/README.md("PE loader") anddocs/cpu_features.mddescribe it. On a host that has the features the CPU runs them and the handler never sees them.Not done here: each emulated instruction costs an exception round trip, so a hot loop of them is slow; patching such sites in place would be a later step if a trace shows one is hot.
Tests
cpu_emulation_test(new, CTestcpu_emulation; no game files):67forclzero, allclwbaddressing forms (SIB, disp8/disp32, RIP-relative), and the near misses it must leave alone (0F 78without a prefix is VMREAD,0F AE /6without66is XSAVEOPT,66 0F AE /7is CLFLUSHOPT, a cut-off instruction,ud2);applyon a syntheticCONTEXT(results, RIP, the kept upper quadword,clzeroon an unwritable line refused);insertqbetween AVX code and checksymm0[255:128]survives; another checks execution continues at the right byte after twoclwbs;ud2still faults.On this machine (Intel Core i7-10700K, which lacks all four features, so every form took the emulated path):
cpu_emulation_testall checks pass. Fullctest(RelWithDebInfo, MSVC 14.51, Ninja): 262 of 266 pass, includingloader,pe_loaderand everyrun_tiny_*test that startsgta6rt. The 4 failures (d3d12_vulkan_device,d3d12_indirect_dispatch_count,d3d12_indirect_rt,d3d12_bindless_churn_validated) are standalone D3D12/Vulkan tests that don't include or link any file this PR changes. Not run on an AMD host, so the native path of the live test is untested here; the retail executable was not run.Windows 11, Intel Core i7-10700K, NVIDIA RTX 3080 (driver 610.88), MSVC 14.51.
AI-assisted (Claude Code).
🤖 Generated with Claude Code