cpu: handle immediate INSERTQ in the startup debugger #1222

Closed
R0Y00 wants to merge 1 commit from upstream-pr/1229 into main
R0Y00 commented 2026-11-24 04:26:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T04:26:10Z.


Refs #870.

An opt-in startup debugger fallback executes the defined low-quadword result of immediate SSE4a INSERTQ on an x64 host that raises an illegal-instruction exception. It only handles six-byte XMM0-XMM7 register encodings in the mapped main executable. Native support needs no exception handling. Unsupported forms/ranges and stop-first-chance diagnostics retain normal exception processing.

Windows Release on Intel Core i9-14900HX, MSVC 19.51, checked with the source-built MASM fixture:

. ./tools/dev.ps1
cmake --preset release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release
cmake --build --preset release --target startup_insertq_fixture
ctest --preset release --no-tests=error -R '^startup_insertq$' -V

1/1 passed, zero skips. The baseline stops on 0xc000001d. With X2W_STARTUP_INSERTQ=1 the same fixture checks neighboring bits, source contents and flags, exits 0, and records one emulation. Independent context controls cover length 0 / 64, bit 63, unchanged state on rejection, and bounded decoding. The upper 64 bits of the hardware result are unspecified. No private samples or logs are included.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6574599234 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1229). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T04:26:10Z. --- Refs #870. An opt-in startup debugger fallback executes the defined low-quadword result of immediate SSE4a INSERTQ on an x64 host that raises an illegal-instruction exception. It only handles six-byte XMM0-XMM7 register encodings in the mapped main executable. Native support needs no exception handling. Unsupported forms/ranges and stop-first-chance diagnostics retain normal exception processing. Windows Release on Intel Core i9-14900HX, MSVC 19.51, checked with the source-built MASM fixture: ```powershell . ./tools/dev.ps1 cmake --preset release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release cmake --build --preset release --target startup_insertq_fixture ctest --preset release --no-tests=error -R '^startup_insertq$' -V ``` 1/1 passed, zero skips. The baseline stops on 0xc000001d. With X2W_STARTUP_INSERTQ=1 the same fixture checks neighboring bits, source contents and flags, exits 0, and records one emulation. Independent context controls cover length 0 / 64, bit 63, unchanged state on rejection, and bounded decoding. The upper 64 bits of the hardware result are unspecified. No private samples or logs are included.
tedin7 commented 2026-11-24 06:47:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:38:46Z.

Review: COMMENTED


Reviewed commit 7bb1ab4f on Linux (x86_64, Python 3.12 with iced-x86 and pefile). Windows build/tests (startup_insertq_fixture, ctest --preset release --no-tests=error -R '^startup_insertq$' -V) were not run locally.

Code & Spec Audit

  • tools/startup/insertq.py: apply_insertq matches the 6-byte SSE4a immediate INSERTQ xmm1, xmm2, imm8, imm8 (F2 0F 78 /r ib ib, ModR/M >= 0xc0, XMM0-XMM7), maps length == 0 to 64 ((code[4] & 63) or 64), masks offset = code[5] & 63, rejects offset + width > 64 (undefined AMD64 range), checks CONTEXT.Rip (0xf8) against rip, updates only the low 64 bits of dest in FltSave.XmmRegisters (0x1a0 + 16 * reg), preserves EFLAGS, and advances Rip by 6.
  • tools/startup/debug_launch.py: gates on first and ex_code == 0xc000001d and allow_insertq and not stop_first, uses K32GetModuleInformation to bound address to [base, base + size - 6], and reads/writes CONTEXT with ContextFlags = 0x10000B (includes CONTEXT_FLOATING_POINT).
  • README.md and docs/experiments.md are title-neutral and match the implementation.

Checks Run Locally (Linux)

  • /root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: passed (self-test passed; 16 Markdown files, 277 links)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: passed (self-test passed; 1037 tracked files)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py: passed
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/hardware_reports_check.py: 12/12 passed, 0 skipped, 0 failed
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/gpu_report_format_check.py: 10/10 passed, 0 skipped, 0 failed
  • Inline tools/startup/insertq.py apply_insertq check (3 valid boundary cases: 16/16, 0/0 -> 64, 1/63; 6 invalid/rejected cases): passed
  • git diff --check HEAD~1..HEAD: passed
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033725199 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1229#pullrequestreview-6033725199). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T04:38:46Z. **Review:** COMMENTED --- Reviewed commit `7bb1ab4f` on Linux (`x86_64`, Python 3.12 with `iced-x86` and `pefile`). Windows build/tests (`startup_insertq_fixture`, `ctest --preset release --no-tests=error -R '^startup_insertq$' -V`) were not run locally. ### Code & Spec Audit - `tools/startup/insertq.py`: `apply_insertq` matches the 6-byte SSE4a immediate `INSERTQ xmm1, xmm2, imm8, imm8` (`F2 0F 78 /r ib ib`, `ModR/M >= 0xc0`, XMM0-XMM7), maps `length == 0` to `64` (`(code[4] & 63) or 64`), masks `offset = code[5] & 63`, rejects `offset + width > 64` (undefined AMD64 range), checks `CONTEXT.Rip` (`0xf8`) against `rip`, updates only the low 64 bits of `dest` in `FltSave.XmmRegisters` (`0x1a0 + 16 * reg`), preserves `EFLAGS`, and advances `Rip` by 6. - `tools/startup/debug_launch.py`: gates on `first and ex_code == 0xc000001d and allow_insertq and not stop_first`, uses `K32GetModuleInformation` to bound `address` to `[base, base + size - 6]`, and reads/writes `CONTEXT` with `ContextFlags = 0x10000B` (includes `CONTEXT_FLOATING_POINT`). - `README.md` and `docs/experiments.md` are title-neutral and match the implementation. ### Checks Run Locally (Linux) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py`: passed (self-test passed; 16 Markdown files, 277 links) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py`: passed (self-test passed; 1037 tracked files) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py`: passed - `/root/.local/share/x2w-rev-venv/bin/python -B tests/hardware_reports_check.py`: 12/12 passed, 0 skipped, 0 failed - `/root/.local/share/x2w-rev-venv/bin/python -B tests/gpu_report_format_check.py`: 10/10 passed, 0 skipped, 0 failed - Inline `tools/startup/insertq.py` `apply_insertq` check (3 valid boundary cases: `16/16`, `0/0 -> 64`, `1/63`; 6 invalid/rejected cases): passed - `git diff --check HEAD~1..HEAD`: passed
upstream-sync closed this pull request 2026-11-25 01:00:43 +00:00
R0Y00 commented 2026-11-25 01:02:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T00:56:43Z.


Superseded by #1274 and the wider SSE4a support in #1846. I rebuilt the original MASM fixture from this PR and ran it under the merged debugger on a Core i9-14900HX. With X2W_HOST_CPU off it stopped at 0xc000001d. With X2W_HOST_CPU=1 it rewrote one INSERTQ and exited 0, including the fixture destination, source and flag checks. The source-built fixture remains available here for an exact debugger-path regression.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7149049558 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1229#issuecomment-7149049558). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T00:56:43Z. --- Superseded by #1274 and the wider SSE4a support in #1846. I rebuilt the original MASM fixture from this PR and ran it under the merged debugger on a Core i9-14900HX. With X2W_HOST_CPU off it stopped at 0xc000001d. With X2W_HOST_CPU=1 it rewrote one INSERTQ and exited 0, including the fixture destination, source and flag checks. The source-built fixture remains available here for an exact debugger-path regression.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1222
No description provided.