Repository navigation
Conversation
A table-driven dispatcher for STATUS_ILLEGAL_INSTRUCTION (cpu_ud_emulate), installed at startup only when the host CPU has no CLZERO. Unknown instructions and unwritable targets pass on untouched.
…EH; fix the startup message quoting
…n any emulated instruction is missing
|
Tested 12ad805 on an Intel Core i9-13900K (no SSE4a), NVIDIA GeForce RTX 5090 driver 617.42, Windows 11 (26200), native MSVC 2022 RelWithDebInfo: pass.
AI-assisted (Claude Code). |
|
Claude, try to use inline hooks (JMP X) to an alternative INSERTQ function instead of handling it as a CPU exception. Handling CPU exceptions is very CPU time expensive, especially in tight loops. Make no mistakes. |
|
Thanks @underdogest. Fair point on cost: each emulated INSERTQ is one exception round trip, so a hot loop that runs it a million times would feel it. I kept this PR on the exception route on purpose: it needs no patching of the game's code, it works for every one of the 19 sites at once, and the emulator table is shared with CLZERO/MONITORX/CLWB (#7055, #7096, #7098). Rewriting the sites with inline JMPs to an INSERTQ routine is a separate piece of work (it needs the site list from #7013 and the runtime's existing code-hook machinery in |
|
Tested 7930ec1 on RTX 5090, 621.24, Windows 11 (CPU-only tests): pass. |
|
Zen 2 (Ryzen 9 3950X), native, both forms, all 64 × 64 fields:
So matching silicon means zeroing the upper quadword and computing the out-of-range case instead of declining. #7111's register-form INSERTQ has the fields swapped: it takes the length from xmm2[77:72] and the index from xmm2[69:64], but natively it's the reverse. AI-assisted (Claude Code). |
|
Thanks @MynFriend, that Zen 2 measurement was what the description was missing. Done in 7d54b49: both INSERTQ forms now zero the destination's upper quadword (as Zen 2 does) and drop inserted bits past bit 63; EXTRQ stays declined. All 8,320 defined low-quadword results still match the CPU. |
|
Tested A standalone harness (not part of the PR) builds
So Zen 3 agrees with the Zen 2 measurement: it zeroes xmm1[127:64] and drops inserted bits past bit 63, and the emulation reproduces both exactly. For contrast, the same sweep on #7111's AI-assisted (Claude Code). Synthetic inputs only, no game files. |
|
Tested AMD Ryzen 7 5700X (Zen 3; has CLZERO, MONITORX/MWAITX, CLWB and SSE4a natively), CachyOS, Linux 7.3.5; runtime cross-compiled with clang-cl 23.1.5 + xwin (SDK 10.0.26100), tests run under Wine 11.22.
Undefined fields on Zen 3. Since 7d54b49 ("match Zen 2 INSERTQ results") the emulator also handles index + length > 64 (bits past 63 discarded, high quadword zeroed), but the native comparison still skips those fields ( Note: the PR description still says the emulator declines index + length > 64 and leaves the high quadword unchanged. That describes the code before 7d54b49. AI-assisted (Claude Code). |
throny
left a comment
There was a problem hiding this comment.
Tested 5881905 on Windows 11, MSVC 14.44, i9-12900K (no SSE4a/CLZERO/MONITORX, has CLWB): pass
This CPU lacks SSE4a (CPUID 8000_0001h ECX[6] = 0 on all 24 logical CPUs), so the real INSERTQ raises #UD here and goes through the installed handler.
Commands (CMake + Ninja, RelWithDebInfo, VS 2022 BuildTools vcvars64, Vulkan SDK 1.4.363.0, -j 6):
- The build at 5881905 succeeds.
- The PR's tests:
cpu_ud_insertq_test67 checks, 0 failed. Its one SKIP is "comparison with the CPU: this CPU lacks SSE4a"; "INSERTQ on a CPU without SSE4a: emulated by the installed handler" ran.cpu_ud_emulate_test47/0.cpu_ud_monitorx_test10/0.cpu_ud_emulate_clwb_test35/0. Its one SKIP is the CLWB handler path, because this CPU has CLWB.
- Full
ctest -j 6with the Vulkan ICD hidden (no GPU on this run): 3 of 269 failed. They ared3d12_sparse_queue_family,vk_bindless_ps_texandvk_bindless_ps_imgatomic, each atvkCreateInstance(-9) because the driver is hidden. These 3 also fail at #7055's head on this PC, and none touches this PR's files.
Negative control and semantics on the real instruction (scratch program linking this head's cpu_ud_emulate.cpp, not pushed). It generates movdqu loads, the INSERTQ and movdqu stores into an RWX page and runs them natively, so every case below is a real #UD on this CPU: 11/11.
- Without the handler,
F2 0F 78 CA 08 08raisesSTATUS_ILLEGAL_INSTRUCTION. install()withoutforceinstalls on this CPU.- Immediate form, all 4,096 (length, index) pairs with junk in imm bits 7:6, 3 random dst/src each (12,288 runs, 0 exceptions):
- Where length + index <= 64, the low quadword equals the APM result.
- In the 6,048 runs with length + index > 64, it equals the documented Zen 2 result (excess bits discarded).
- The destination high quadword is 0 in every run, and the source is unchanged.
- Register form
F2 [REX] 0F 79: all 16 × 16 xmm pairs (dst == src included), 40 random values each, with junk around the length (src[69:64]) and index (src[77:72]) fields. All 10,240 runs match the APM result with the high quadword zeroed. xmm6-15 were saved and restored by the generated code. - AVX state: YMM1[255:128], loaded with
vmovdqubefore an emulatedINSERTQ xmm1, is unchanged afterwards, so editingFltSavein the CONTEXT does not clobber the XState upper halves. - A memory ModRM (
F2 0F 79 01) is passed on as #UD.F2 REX.W 0F 79 CAis emulated.2E F2 0F 79 CAstays a #UD, the known limit you listed. - Cost: 1.32 µs per emulated INSERTQ here.
Findings
- The PR description no longer matches the code at this head:
- It says "xmm1's high quadword is left as it was" and "the emulator leaves it unchanged", but
runtime/src/cpu_ud_emulate.cppemulate_insertqsetsdst.High = 0. - It says "index + length > 64 ... the emulator declines it", but the code discards the excess bits.
- The README and header comments match the code.
- It says "xmm1's high quadword is left as it was" and "the emulator leaves it unchanged", but
- Scope: this head now also merges
c2-ud-monitorx(#7096) andc2-ud-clwb(#7098), so it contains all four emulators (CLZERO, MONITORX/MWAITX, CLWB, INSERTQ). The stack is 7055 → 7096 → 7098 → 7100, so the pairwise conflicts between 7096/7098/7100 that I noted on #7055 are resolved by the stacking. Merge them in that order. - #7111 emulates the same instructions with its own handler (
runtime/src/cpu_emulation.h). It conflicts with this head inruntime/src/main.cpp(git merge-tree). Only one of the two should land.
AI-assisted.
Stacked on the CLZERO dispatcher PR (c2-ud-clzero).
What
Adds an INSERTQ (AMD SSE4a) emulator to the #UD dispatcher in
runtime/src/cpu_ud_emulate.*: one function and one line inkTable.F2 [REX] 0F 79 /r: INSERTQ xmm1, xmm2. Length (bits 69:64) and index (bits 77:72) come from the high quadword of xmm2.F2 [REX] 0F 78 /r ib ib: INSERTQ xmm1, xmm2, length, index. (The APM lists this form as/r;/0is EXTRQ,66 0F 78.)lengthbits of xmm2's low quadword into xmm1's low quadword at bitindex. Length 0 means 64. Both forms zero xmm1's high quadword, matching Zen 2.install()now also installs when the host lacks SSE4a (newhost_has_sse4a(), CPUID 8000_0001h ECX bit 6), not only when it lacks CLZERO.Why
The console CPU has SSE4a; Intel desktop CPUs and newer AMD parts without it raise #UD on INSERTQ. Same reason as CLZERO (#6910).
Tested
Zen 2 follow-up (AI-assisted, commit
7d54b498c): on p2, clang-cl and Wine ran onlycpu_ud_insertq_test. The updated test recorded 56 failures in 66 checks against7930ec1e1and 0 failures in 66 checks after the fix. The native comparison matched all 8,320 defined low quadwords and observed 8,320 zero high quadwords. Wine readable-page behavior and native SSE4a caused two skips.Expanded comparison on p2 (clang-cl and Wine): 66 checks, 0 failed; 16,384 of 16,384 native comparisons matched both quadwords, with two skips for Wine readable-page behavior and native SSE4a.
Zen 3 review (crastinator): the native comparison matched 16,384 of 16,384 combinations, including fields extending beyond bit 63; the high quadword was zero in all 16,384 runs.
Original PR validation:
New
cpu_ud_insertq_test(own file, own CMake lines), cross-built with clang-cl and run under Wine on an AMD EPYC host:kTable: 67 checks, 46 failed.cpu_ud_emulate_teststill passes (19 checks, 0 failed).The APM leaves the high quadword undefined. MynFriend measured a zero high quadword on a Ryzen 9 3950X (Zen 2) in 32,768 of 32,768 runs each for INSERTQ and EXTRQ. The earlier EPYC INSERTQ run also observed zero in all 8,320 cases.
@ItsNotRexed: an exe PR, as you asked.
Claim: #6910 (comment)
Built on #7055 (the CLZERO dispatcher): GitHub shows its commits too until it merges; this PR is the last commit. Known limits (from the review):
67and ignored segment prefixes before the opcode are not decoded.