Patch immediate-form SSE4a (insertq/extrq) in-place on Intel CPUs #386

Closed
github-bot wants to merge 0 commits from github-bot/gh-386 into main AGit
Owner

@⁠HDFreitas on GitHub · 2026-12-01 21:13 UTC · original
HDFreitas:perf/sse4a-inplace-patch → main · +200 −15 · 1 files · 72 commits · closed without merging

Summary

Split out from #367 (SSE4a half only, targeting main until the console-instruction rewrite is pushed — happy to rebase once it lands).

On Intel CPUs (which lack AMD's SSE4a extension), immediate-form insertq/extrq instructions (>= 6 bytes, e.g., insertq xmm0, xmm1, 16, 16) currently raise STATUS_ILLEGAL_INSTRUCTION on every execution — reaching 524,288+ kernel exceptions in ~6 minutes (~1,800–2,200 faults/s) during gameplay on an Intel i9-9900K.

Changes (crates/victory-loader/src/console_insn.rs)

  • Mirrors the existing clzero loop trampoline mechanism: replaces the first 5 bytes of faulting immediate-form insertq/extrq instructions (len >= 5, not crossing a 16-byte boundary) atomically via cmpxchg16b with a jmp rel32 to a small native stub in a nearby executable page.
  • Stub preserves all GPRs, other XMM registers, and RFLAGS (pushfq/popfq), and zeroes the destination XMM's high quadword via movq xmm, rax (matching emulation / Zen 5).
  • Extends STUBS to a multi-page pool (Vec<(usize, usize)>) so modules >2 GiB apart can each allocate a reachable stub page.
  • Controlled by VICTORY_PATCH_SSE4A (on by default; set VICTORY_PATCH_SSE4A=0 to fall back to per-fault emulation).

Measurements (Intel i9-9900K @ 5 GHz, Windows 11)

  • Emulated console instructions (console_insn): dropped from 524,288 faults in 6 min to 1 single fault at startup (patched on first hit, 0 subsequent faults).

Verification

  • cargo clippy --locked -p victory-loader --tests -- -D warnings: clean (0 warnings).
  • cargo test --release --locked -p victory-loader console_insn: 6/6 passed, including sse4a_immediate_becomes_native_stub which executes patched insertq and extrq instructions natively and verifies low/high quadwords and RFLAGS preservation.
**@⁠HDFreitas** on GitHub · 2026-12-01 21:13 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/386) `HDFreitas:perf/sse4a-inplace-patch` → `main` · +200 −15 · 1 files · 72 commits · closed without merging ## Summary Split out from #367 (SSE4a half only, targeting `main` until the console-instruction rewrite is pushed — happy to rebase once it lands). On Intel CPUs (which lack AMD's SSE4a extension), immediate-form `insertq`/`extrq` instructions (`>= 6` bytes, e.g., `insertq xmm0, xmm1, 16, 16`) currently raise `STATUS_ILLEGAL_INSTRUCTION` on every execution — reaching **524,288+ kernel exceptions in ~6 minutes (~1,800–2,200 faults/s)** during gameplay on an Intel i9-9900K. ### Changes (`crates/victory-loader/src/console_insn.rs`) - Mirrors the existing `clzero` loop trampoline mechanism: replaces the first 5 bytes of faulting immediate-form `insertq`/`extrq` instructions (`len >= 5`, not crossing a 16-byte boundary) atomically via `cmpxchg16b` with a `jmp rel32` to a small native stub in a nearby executable page. - Stub preserves all GPRs, other XMM registers, and `RFLAGS` (`pushfq`/`popfq`), and zeroes the destination XMM's high quadword via `movq xmm, rax` (matching emulation / Zen 5). - Extends `STUBS` to a multi-page pool (`Vec<(usize, usize)>`) so modules >2 GiB apart can each allocate a reachable stub page. - Controlled by `VICTORY_PATCH_SSE4A` (on by default; set `VICTORY_PATCH_SSE4A=0` to fall back to per-fault emulation). ## Measurements (Intel i9-9900K @ 5 GHz, Windows 11) - **Emulated console instructions (`console_insn`)**: dropped from **524,288 faults in 6 min** to **1 single fault** at startup (patched on first hit, 0 subsequent faults). ## Verification - `cargo clippy --locked -p victory-loader --tests -- -D warnings`: clean (0 warnings). - `cargo test --release --locked -p victory-loader console_insn`: 6/6 passed, including `sse4a_immediate_becomes_native_stub` which executes patched `insertq` and `extrq` instructions natively and verifies low/high quadwords and `RFLAGS` preservation.
Replace faulting immediate-form SSE4a insertq/extrq instructions with a jmp rel32 trampoline to a register- and RFLAGS-preserving native stub in a nearby executable page (controlled by VICTORY_PATCH_SSE4A, on by default).

Extends the stub pool to multiple pages for modules >2 GiB apart and adds unit tests executing patched insertq/extrq instructions natively.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 21:18:36 +00:00
github-bot closed this pull request 2026-12-02 07:54:25 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.