Skip to content

Console CPU instructions: SSE4a INSERTQ / EXTRQ are emulated - #209

Closed
ShyVortex wants to merge 1 commit into
ivall:mainfrom
ShyVortex:cpu/sse4a-insertq
Closed

ShyVortex wants to merge 1 commit into
ivall:mainfrom
ShyVortex:cpu/sse4a-insertq

Conversation

@ShyVortex

Copy link
Copy Markdown

Summary

Zen 2 has SSE4a, which no Intel CPU has (the possible further #UD source noted in the review of #151). A retail game uses INSERTQ: 19 of them, all the immediate form, in its float-to-half packing. On an Intel CPU, with #151 in place, it ran for about two minutes and presented 900+ frames. Then, when its first menu reacted to input, it stopped with STATUS_ILLEGAL_INSTRUCTION at insertq xmm0, xmm1, 16, 16 (F2 0F 78 C1 10 10).

This adds SSE4a's immediate forms to #151's decoder and emulation:

  • Decoding (insn::decode): now tracks the F2 prefix. It decodes F2 0F 78 /r ib ib (insertq xmm, xmm, len, idx) and 66 0F 78 /0 ib ib (extrq xmm, len, idx), with REX.R / REX.B, register operands only (the encoding has no memory form). 0F 78 without a prefix (vmread) is left alone.
  • Emulation (eh::emulate_console_only): works on the low quadword of the XMM registers in the exception's context. It inserts or extracts a field of len bits at idx, length 0 meaning 64, as AMD's manual (APM vol. 4) defines them. The upper quadword, undefined on the hardware, is left as it was.

A scan of the game's code sections found no EXTRQ and no SSE4a register forms (0F 79) or MOVNTSS / MOVNTSD. EXTRQ's immediate form is included since it shares the opcode.

Testing

  • cargo test --release --workspace: all pass. New: insn::tests::decodes_sse4a (the game's encodings, with REX.R and REX.R + REX.B, plus what must not decode) and insn::tests::sse4a_semantics.
  • i7-12700H + RTX 3080 Ti Laptop GPU, main + this: the game runs past the INSERTQ and shows its loading screen with text.

🤖 Generated with Claude Code

Zen 2 has SSE4a, which no Intel CPU has. A retail game uses INSERTQ (19 of
them, immediate form, in its float-to-half packing): on an Intel CPU it ran
for two minutes, then stopped with STATUS_ILLEGAL_INSTRUCTION at
`insertq xmm0, xmm1, 16, 16` when its first menu reacted to input.

insn::decode takes the F2 prefix into account and decodes the immediate
forms with register operands: F2 0F 78 /r ib ib (insertq) and
66 0F 78 /0 ib ib (extrq). The emulation works on the low quadword of the
XMM registers in the exception's context, as AMD's manual defines it (a
field of `len` bits at `idx`, length 0 meaning 64); the upper quadword,
undefined on the hardware, is left as it was.

Tests: the game's encodings (REX.R / REX.B included) and the bit-field
semantics.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ethandepstein-del

Copy link
Copy Markdown

Independent static code & AMD64 ISA audit of 08c81c629 (code inspection only — cargo test not run here; no game files):

Summary: Looks solid for immediate SSE4a INSERTQ / EXTRQ

  • Bit math (insn.rs:32-49): field_mask(len) handles len & 63 == 0 → u64::MAX (64 bits), and masking idx & 63 prevents any Rust shift-by-64 overflow panic even on undefined len + idx > 64 encodings. insertq / extrq match AMD64 APM Vol. 4 on valid fields, and leaving the upper quadword of XmmRegisters[dst & 15] untouched matches the hardware-undefined upper half.
  • Encoding & context wiring (insn.rs:108-119, eh.rs:304-311): REX.R ((rex & 4) << 1) and REX.B ((rex & 1) << 3) decode xmm0..xmm15 accurately, m >> 6 == 3 properly rejects memory operands, (m >> 3) & 7 == 0 enforces /0 for EXTRQ, and ctx.Rip += len as u64 advances by the full decoded length (6 or 7 bytes; the match-arm len binding doesn't shadow it).

Minor non-blocking notes

  1. Conflicting legacy prefixes (insn.rs:108): (0x78, Some(m)) if m >> 6 == 3 && (pf2 || p66) accepts F3 66 0F 78, F3 F2 0F 78, and 66 F2 0F 78, which are #UD on x86-64 rather than valid INSERTQ/EXTRQ. Tightening the guard to !pf3 && (pf2 ^ p66) keeps invalid prefix combinations rejected.
  2. Scope note: Register-operand forms (F2 0F 79 /r, 66 0F 79 /r) are not decoded here (expected since only immediate 0F 78 forms were seen, just noting in case a title uses 0F 79 later).

(AI-assisted)

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Review of 08c81c6, merged onto main 6da33fa (merge 51ac82a, no conflicts). Tested on a Ryzen 9 9950X (Zen 5, Windows 11 26200), which runs SSE4a natively. That allows a direct comparison between the hardware and the emulation. Synthetic data only.

Result: on every valid encoding, the emulated low quadword equals the CPU's, including insertq xmm0, xmm1, 16, 16 (F2 0F 78 C1 10 10, the instruction in #216). Three differences, none in the instruction the game uses:

  1. The CPU zeroes the upper quadword of the destination; the emulation leaves it unchanged. This held for all 32,779 instructions run, INSERTQ and EXTRQ alike. AMD's manual calls it undefined. Setting .High = 0 would match the hardware measured here. I can't run a Zen 2, so I can't say it matches the console, but nothing points the other way.

  2. The F3 prefix combinations are decoded wrong. On the CPU, the last of F2/F3 decides, and 66 is ignored next to them:

    • 66 F2, F2 66 and F3 F2 run as INSERTQ, and the PR also emulates them correctly. So the earlier suggestion here to reject them would turn valid instructions into crashes.
    • F2 F3, F3 66 and 66 F3 raise #UD on the CPU, but the PR emulates the first as INSERTQ and the other two as EXTRQ.

    A fix: track which of F2/F3 came last. Return None when it is F3; decode INSERTQ when it is F2, and EXTRQ when there is neither and 66 is present. Compilers don't emit these combinations, so this is for correctness only.

  3. Encodings the CPU runs but the PR leaves undecoded, so they still crash:

Method. A test-only probe, not part of the PR.

  • Hardware run: a thunk on an executable page, registered as a mapped module, loads all 16 XMM registers from a block, runs the instruction bytes, and stores the registers back.
  • Emulation run: the same bytes go through the real handler, cpp_exception_logger, with a STATUS_ILLEGAL_INSTRUCTION whose CONTEXT holds the same XMM values.
  • Coverage:
    • every len × idx (0–63 × 0–63) with random data. INSERTQ used three register pairs: xmm0/xmm1, xmm9/xmm7 (REX.R) and xmm11/xmm12 (REX.R+B), with and without REX.W. EXTRQ used xmm2 and xmm10.
    • immediates with bits 7:6 set, and the prefix and ModRM edge cases.
  • A real #UD: for encodings that raise #UD on the CPU but that the PR decodes, the real exception went through the handler. The thread's XMM registers on resuming equal the synthetic emulation's, so the kernel does restore what the handler writes to FltSave.XmmRegisters.
encodings n handled low quadword = CPU other registers untouched CPU upper quadword
F2 0F 78 C1 10 10 (#216) 1 1 1 1 zeroed
INSERTQ, len+idx ≤ 64 6,240 6,240 6,240 6,240 zeroed (all)
INSERTQ, len+idx > 64 6,048 6,048 6,048 6,048 zeroed (all)
INSERTQ REX.W, ≤ 64 / > 64 6,240 / 6,048 all all all zeroed (all)
EXTRQ, ≤ 64 / > 64 4,160 / 4,032 all all all zeroed (all)
immediates with bits 7:6 set 10 10 10 10 zeroed

When len+idx > 64 (undefined per AMD), the CPU drops the bits past 63, exactly like the PR's mask << idx. Length 0 means 64 on the CPU too.

Edge encodings: the CPU's behaviour, and what the PR's handler does (✓ means they agree):

bytes CPU PR
66 F2, F2 66, F3 F2 0F 78 C1 10 10 INSERTQ INSERTQ ✓
F2 F3 0F 78 C1 … #UD INSERTQ ✗
F3 66 0F 78 C1 …, 66 F3 0F 78 C0 … #UD EXTRQ ✗
F3 0F 78; 66 0F 78 /1, /2, /7; memory operand #UD not handled ✓
2E F2 …, F2 48 …, 66 48 … (REX.W) runs emulated ✓
67 F2 …, F2 67 …, 67 66 … runs not handled ✗
41 F2 0F 78 C1 … (REX before F2) runs, REX ignored not handled ✗
F2 0F 79 C1, 66 0F 79 C1 run not handled ✗

Control: the same probe on main 6da33fa. The handler emulates none of these (handled = 0 in every row), the probe's assertion fails, and the low quadword matches only where the instruction happens not to change it. The edge rows give the same CPU results.

The #151 gaps besides 0x67 are untouched by this PR, as expected: CLZERO still checks that the line is readable (not writable), and MWAITX does not pause.

Tested

  • Ryzen 9 9950X, Windows 11 26200, Rust 1.99.0 MSVC, debug.
  • 08c81c6 merged onto 6da33fa (merge 51ac82a): insn:: 3/3 pass (including decodes_sse4a, sse4a_semantics); eh:: 2/2 pass (1 ignored, the probe).
  • The probe: 32,779 hardware-vs-handler comparisons, all matching in the low quadword. The 20 edge encodings are in the table.
  • Control 6da33fa: insn:: 1/1 and eh:: 2/2 pass; the probe fails as expected.

Not tested

  • an Intel CPU (where the #UD actually happens);
  • Zen 2;
  • the game;
  • the full workspace suite;
  • the register forms' semantics in the emulation (not implemented).

AI use: Claude Code, Claude Opus 5.5

ivall added a commit that referenced this pull request Nov 23, 2026
…ecides, 0x67 and early REX ignored

From Kalmarv's hardware comparison on PR #209 (32 779 cases, a Ryzen 9 9950X running the
instructions natively against the handler): the CPU zeroes the destination's high quadword;
the last of F2 / F3 decides and 66 next to them does not (F2 F3 / F3 66 / 66 F3 raise #UD,
66 F2 / F2 66 / F3 F2 are INSERTQ); a 0x67 prefix and a REX before a legacy prefix are
ignored – also for CLZERO and the other console-only instructions.
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks - this crossed with the same fix on main for #216 (0cd0a53, written before I saw this PR), so the branch now conflicts; closing it as superseded rather than merging a second copy. Your Intel run (i7-12700H: past the INSERTQ, loading screen with text) is the confirmation main was missing, and @Kalmarv's hardware comparison is now on main too (af20b4b): the destination's high quadword is zeroed as the Zen 5 does in all 32 779 cases, the last of F2 / F3 decides (F2 F3, F3 66, 66 F3 stay #UD; 66 F2, F2 66, F3 F2 are INSERTQ), 0x67 and a REX before a legacy prefix are ignored (for CLZERO and the rest too), and the register forms 0F 79 are emulated. Checked on an RX 9070 host: cargo test --workspace green (583 unit tests); the emulation path itself cannot fire on an AMD CPU, so a run of current main on your Intel machine would be welcome.

@tactileteamrob

Copy link
Copy Markdown

I tested the experimental SSE4a build again.

The new log confirms that INSERTQ at game_scarlett_master.exe+0x2e7ff1 is now being emulated successfully.

The previous illegal-instruction crash no longer appears in the log. The game continues running and records over 1,000 translated draws, but I still get extremely stuttery narration audio, followed by a green screen and severely distorted audio.

I've attached the new log and report.json.

System: Intel i9-12900K, RTX 5070, 32GB RAM, Windows 11 25H2.
report.json
xcl.log

@ShyVortex

Copy link
Copy Markdown
Author

Ran current main (af20b4b) on the Intel machine as asked: i7-12700H + RTX 3080 Ti Laptop GPU (16 GB), Windows 11 26300, XCL_NETWORK=0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants