cpu: rewrite instructions the host processor lacks in the startup debugger #1267

Merged
JohnConnorNPC merged 6 commits from upstream-pr/1274 into main 2026-11-24 10:08:26 +00:00
JohnConnorNPC commented 2026-11-24 06:27:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-24T06:27:41Z.


Refs #870.

Problem

A console executable is built for one processor. On a host without CLWB, CLZERO, MONITORX/MWAITX or SSE4a, a startup run under tools/startup/debug_launch.py ends at the first such instruction with an illegal-instruction exception. On the host used here (Intel Core i7-10700KF, which has none of them) that is 1.6 s into the run.

Change

X2W_HOST_CPU=1 (run.ps1 -HostCpu), off by default. When the main image raises a first-chance illegal-instruction exception, the debugger rewrites that instruction in the running process and continues. The file on disk is not touched, and each site costs one debugger round trip.

Instruction Rewritten as
CLWB m8 CLFLUSHOPT m8 (one ModRM bit)
MONITORX, MWAITX 3-byte NOP; PAUSE + NOP (the wait may end at any time)
CLZERO jump to a stub that stores 64 zero bytes at rax & ~63
INSERTQ xmm, xmm, len, idx, EXTRQ xmm, len, idx (xmm0-7) jump to a stub that moves the field in integer registers

The stubs keep the other registers and the flags. Instructions that the 5-byte jump overwrites are re-encoded behind the stub with iced-x86 (already in requirements-dev.txt). The logic is in the new tools/startup/host_cpu.py; debug_launch.py only reads memory, asks it for a plan and writes the bytes.

Refused, not approximated: the register-count forms of INSERTQ/EXTRQ (0F 79), xmm8-15, and fields that end past bit 63.

Relation to other work

  • #1229 emulates the immediate INSERTQ in the debugger on every hit behind X2W_STARTUP_INSERTQ=1. This PR rewrites each site once instead, and covers the other instructions. The two do not touch the same lines, but only one INSERTQ path is needed; I am happy to drop INSERTQ from this PR, or rebase on #1229, whichever the CPU reviewer prefers.
  • codex/static-clzero-loops lowers CLZERO in the AOT translator. That is a different path (translated code); this one is for the untranslated image under the startup debugger.

Evidence

Commit under test: the head of this branch, on main at 99cde8d6.

python -B tests/startup_host_cpu_check.py
startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: none)

The check assembles source-built functions byte by byte, patches them the way the debugger does and calls them on the host:

  • 288 INSERTQ/EXTRQ results over five register pairs, nine fields and four value pairs, against a model of the instruction;
  • CLZERO zeroing exactly its line (neighbours intact), with CF kept, once with a moved lea and once with a moved rip-relative load;
  • CLWB → CLFLUSHOPT for five addressing forms, decoded before and after with iced-x86 (same length and memory operand), and the two wait rewrites;
  • an INSERTQ stub checked to keep rcx, rdx and CF.

Negative controls: a CLZERO stub that zeroes 56 bytes, one that zeroes 72, and an INSERTQ stub for a field 8 bits further along each fail the same comparison. Six encodings outside the supported forms are refused.

On a processor that has SSE4a or CLZERO (CPUID), the unpatched functions run as well and must agree with the model. That did not run here: this host has neither, so "native comparison: none". A run on an AMD host would add that comparison; I can't provide one.

Run on:

  • Linux x86-64, CPython 3.13, iced-x86 1.21.0 (the pinned version): pass.
  • CPython 3.12 for Windows under Wine 11.0 (Proton Experimental), iced-x86 1.22.0: pass. This exercises the Windows branch of the harness (VirtualAlloc, rcx/rdx arguments). It is not a Windows result.

Also ran python -B tests/docs_links_check.py (285 links, pass), python -B tests/bidi_controls_check.py (pass) and git diff --check (clean).

End to end, with a local console executable under the startup debugger and X2W_HOST_CPU=1, on the same host under Wine with vkd3d-proton: the summary reported host-cpu sites rewritten: clwb x5, clzero x5, insertq x2, monitorx x1, mwaitx x1 in a 100 s run that was still presenting frames at the timeout. Without the switch the same run stops at the first CLWB.

Not measured

  • Windows. I have no Windows host. The CMake registration (startup_host_cpu, skip code 77) and the run.ps1 switch are unbuilt and unrun there; CI is the first Windows run of both.
  • EXTRQ has only run in the check, not in a startup run.
  • A thread executing inside the bytes of a jump site while another thread faults on it is not handled. The window is the first fault per site.
  • Bits 127:64 of an SSE4a destination are undefined after the original instruction; the stubs leave them unchanged.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6576037382 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-24T06:27:41Z. --- Refs #870. ## Problem A console executable is built for one processor. On a host without CLWB, CLZERO, MONITORX/MWAITX or SSE4a, a startup run under `tools/startup/debug_launch.py` ends at the first such instruction with an illegal-instruction exception. On the host used here (Intel Core i7-10700KF, which has none of them) that is 1.6 s into the run. ## Change `X2W_HOST_CPU=1` (`run.ps1 -HostCpu`), off by default. When the main image raises a first-chance illegal-instruction exception, the debugger rewrites that instruction in the running process and continues. The file on disk is not touched, and each site costs one debugger round trip. | Instruction | Rewritten as | |---|---| | `CLWB m8` | `CLFLUSHOPT m8` (one ModRM bit) | | `MONITORX`, `MWAITX` | 3-byte `NOP`; `PAUSE` + `NOP` (the wait may end at any time) | | `CLZERO` | jump to a stub that stores 64 zero bytes at `rax & ~63` | | `INSERTQ xmm, xmm, len, idx`, `EXTRQ xmm, len, idx` (xmm0-7) | jump to a stub that moves the field in integer registers | The stubs keep the other registers and the flags. Instructions that the 5-byte jump overwrites are re-encoded behind the stub with iced-x86 (already in `requirements-dev.txt`). The logic is in the new `tools/startup/host_cpu.py`; `debug_launch.py` only reads memory, asks it for a plan and writes the bytes. Refused, not approximated: the register-count forms of `INSERTQ`/`EXTRQ` (`0F 79`), xmm8-15, and fields that end past bit 63. ## Relation to other work - #1229 emulates the immediate `INSERTQ` in the debugger on every hit behind `X2W_STARTUP_INSERTQ=1`. This PR rewrites each site once instead, and covers the other instructions. The two do not touch the same lines, but only one `INSERTQ` path is needed; I am happy to drop `INSERTQ` from this PR, or rebase on #1229, whichever the CPU reviewer prefers. - `codex/static-clzero-loops` lowers CLZERO in the AOT translator. That is a different path (translated code); this one is for the untranslated image under the startup debugger. ## Evidence Commit under test: the head of this branch, on `main` at `99cde8d6`. ``` python -B tests/startup_host_cpu_check.py startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: none) ``` The check assembles source-built functions byte by byte, patches them the way the debugger does and calls them on the host: - 288 `INSERTQ`/`EXTRQ` results over five register pairs, nine fields and four value pairs, against a model of the instruction; - `CLZERO` zeroing exactly its line (neighbours intact), with CF kept, once with a moved `lea` and once with a moved rip-relative load; - `CLWB` → `CLFLUSHOPT` for five addressing forms, decoded before and after with iced-x86 (same length and memory operand), and the two wait rewrites; - an `INSERTQ` stub checked to keep `rcx`, `rdx` and CF. Negative controls: a `CLZERO` stub that zeroes 56 bytes, one that zeroes 72, and an `INSERTQ` stub for a field 8 bits further along each fail the same comparison. Six encodings outside the supported forms are refused. On a processor that has SSE4a or CLZERO (CPUID), the unpatched functions run as well and must agree with the model. That did not run here: this host has neither, so "native comparison: none". **A run on an AMD host would add that comparison**; I can't provide one. Run on: - Linux x86-64, CPython 3.13, iced-x86 1.21.0 (the pinned version): pass. - CPython 3.12 for Windows under Wine 11.0 (Proton Experimental), iced-x86 1.22.0: pass. This exercises the Windows branch of the harness (`VirtualAlloc`, `rcx`/`rdx` arguments). It is not a Windows result. Also ran `python -B tests/docs_links_check.py` (285 links, pass), `python -B tests/bidi_controls_check.py` (pass) and `git diff --check` (clean). End to end, with a local console executable under the startup debugger and `X2W_HOST_CPU=1`, on the same host under Wine with vkd3d-proton: the summary reported `host-cpu sites rewritten: clwb x5, clzero x5, insertq x2, monitorx x1, mwaitx x1` in a 100 s run that was still presenting frames at the timeout. Without the switch the same run stops at the first `CLWB`. ## Not measured - **Windows.** I have no Windows host. The CMake registration (`startup_host_cpu`, skip code 77) and the `run.ps1` switch are unbuilt and unrun there; CI is the first Windows run of both. - `EXTRQ` has only run in the check, not in a startup run. - A thread executing inside the bytes of a jump site while another thread faults on it is not handled. The window is the first fault per site. - Bits 127:64 of an SSE4a destination are undefined after the original instruction; the stubs leave them unchanged.
tedin7 commented 2026-11-24 06:47:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T06:45:17Z.

Review: COMMENTED


Peer review at commit 56f81dd9282cac2afff15608dd32ef3a85514266:

Findings

  • tools/startup/host_cpu.py:
    • clwb_rewrite (66 [REX] 0F AE /6 -> /7 CLFLUSHOPT, memory forms only), wait_rewrite (MONITORX -> 3-byte NOP, MWAITX -> PAUSE; NOP), CLZERO_STUB (BMI2 shrx/shlx to preserve flags while masking rax &= ~63 and zeroing 64 bytes), and sse4a_stub (immediate INSERTQ/EXTRQ for xmm0–xmm7, offset + width <= 64, preserving flags, rax/rcx/rdx, and 127:64) are clean and refuse unsupported forms rather than approximating them.
    • plan_replacement: uses iced_x86.BlockEncoder when length < 5 to relocate overwritten instructions (including RIP-relative operands) behind the stub with a JMP rel32 back.
  • tools/startup/debug_launch.py & tools/startup/run.ps1: Gated behind X2W_HOST_CPU=1 (-HostCpu), handles first-chance 0xC000001D in the main image ([main_base, main_base + 0x10000000)), allocates stub pages within 256 MiB below main_base via VirtualAllocEx, and calls FlushInstructionCache.
    • Minor note: rewrite_for_host assumes plan_replacement will not raise from iced_x86.BlockEncoder on an un-relocatable adjacent instruction; catching Exception and returning None would let the debugger fall through to normal exception logging if that ever occurred.
  • tests/startup_host_cpu_check.py: Comprehensive self-contained check with negative controls (56-byte/72-byte CLZERO stubs, shifted-offset INSERTQ stub, 6 refused encodings) and native CPU comparison (clzero, sse4a) which executed and passed on both our Linux and Windows Server 2025 AMD hosts.

Verification

  • Linux:
    • git diff --check origin/main...HEAD: clean
    • /root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: passed (Checked 16 Markdown files and 285 links)
    • /root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: passed (Checked 1069 tracked source, script and build/CI files: no bidirectional controls)
    • /root/.local/share/x2w-rev-venv/bin/python -B tests/startup_host_cpu_check.py: startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a)
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • tests/startup_host_cpu_check.py (via x2w_pipe.py win2025 1274 --py-checks tests/startup_host_cpu_check.py --regex ""): startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a) (rc=0, 0.14s)
    • Debug (--preset dev) & Release /O2 (--preset release) CMake configure/build succeeded (3.74s / 1.09s).
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6034816421 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#pullrequestreview-6034816421). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T06:45:17Z. **Review:** COMMENTED --- Peer review at commit `56f81dd9282cac2afff15608dd32ef3a85514266`: ### Findings - `tools/startup/host_cpu.py`: - `clwb_rewrite` (`66 [REX] 0F AE /6` -> `/7` `CLFLUSHOPT`, memory forms only), `wait_rewrite` (`MONITORX` -> 3-byte `NOP`, `MWAITX` -> `PAUSE; NOP`), `CLZERO_STUB` (BMI2 `shrx`/`shlx` to preserve flags while masking `rax &= ~63` and zeroing 64 bytes), and `sse4a_stub` (immediate `INSERTQ`/`EXTRQ` for `xmm0`–`xmm7`, `offset + width <= 64`, preserving `flags`, `rax`/`rcx`/`rdx`, and `127:64`) are clean and refuse unsupported forms rather than approximating them. - `plan_replacement`: uses `iced_x86.BlockEncoder` when `length < 5` to relocate overwritten instructions (including RIP-relative operands) behind the stub with a `JMP rel32` back. - `tools/startup/debug_launch.py` & `tools/startup/run.ps1`: Gated behind `X2W_HOST_CPU=1` (`-HostCpu`), handles first-chance `0xC000001D` in the main image (`[main_base, main_base + 0x10000000)`), allocates stub pages within 256 MiB below `main_base` via `VirtualAllocEx`, and calls `FlushInstructionCache`. - Minor note: `rewrite_for_host` assumes `plan_replacement` will not raise from `iced_x86.BlockEncoder` on an un-relocatable adjacent instruction; catching `Exception` and returning `None` would let the debugger fall through to normal exception logging if that ever occurred. - `tests/startup_host_cpu_check.py`: Comprehensive self-contained check with negative controls (56-byte/72-byte `CLZERO` stubs, shifted-offset `INSERTQ` stub, 6 refused encodings) and **native CPU comparison** (`clzero`, `sse4a`) which executed and passed on both our Linux and Windows Server 2025 AMD hosts. ### Verification - **Linux**: - `git diff --check origin/main...HEAD`: clean - `/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py`: passed (`Checked 16 Markdown files and 285 links`) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py`: passed (`Checked 1069 tracked source, script and build/CI files: no bidirectional controls`) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/startup_host_cpu_check.py`: `startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a)` - **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)**: - `tests/startup_host_cpu_check.py` (via `x2w_pipe.py win2025 1274 --py-checks tests/startup_host_cpu_check.py --regex ""`): `startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a)` (`rc=0`, `0.14s`) - Debug (`--preset dev`) & Release `/O2` (`--preset release`) CMake configure/build succeeded (`3.74s` / `1.09s`).
xiongzhangchi-ui commented 2026-11-24 08:01:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by xiongzhangchi-ui on 2026-11-24T07:19:13Z.


I ran the standalone CPU check twice on native Windows. Both runs passed with exit code 0.

  • PR head: 56f81dd9282cac2afff15608dd32ef3a85514266
  • OS: Windows 26H2, build 26300.9457
  • CPU: Intel Core i7-12700KF
  • GPU: AMD Radeon RX 6900 XT, driver 32.0.21045.16010
  • Runtime: Python 3.13.15, iced-x86==1.21.0

Command from the PR source directory, with the test virtual environment active:

python -E -s -B tests/startup_host_cpu_check.py

Expected: exit code 0 and a PASS covering 288 INSERTQ/EXTRQ results, two CLZERO relocation forms, five CLWB rewrites, and two wait rewrites.

Actual:

startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: none)

The run used only the public, source-generated fixtures. It covered the standalone CPU check. Native SSE4a/CLZERO comparison, CMake/CTest registration, and the startup debugger path were outside this run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7124859074 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#issuecomment-7124859074). Originally posted by [xiongzhangchi-ui](https://github.com/xiongzhangchi-ui) on 2026-11-24T07:19:13Z. --- I ran the standalone CPU check twice on native Windows. Both runs passed with exit code `0`. - PR head: `56f81dd9282cac2afff15608dd32ef3a85514266` - OS: Windows 26H2, build `26300.9457` - CPU: Intel Core i7-12700KF - GPU: AMD Radeon RX 6900 XT, driver `32.0.21045.16010` - Runtime: Python `3.13.15`, `iced-x86==1.21.0` Command from the PR source directory, with the test virtual environment active: ```powershell python -E -s -B tests/startup_host_cpu_check.py ``` Expected: exit code `0` and a `PASS` covering 288 INSERTQ/EXTRQ results, two CLZERO relocation forms, five CLWB rewrites, and two wait rewrites. Actual: ```text startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: none) ``` The run used only the public, source-generated fixtures. It covered the standalone CPU check. Native SSE4a/CLZERO comparison, CMake/CTest registration, and the startup debugger path were outside this run.
Fr0st0007 commented 2026-11-24 08:01:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Fr0st0007 on 2026-11-24T07:52:22Z.


Tested e6133013 and the current head 9bbc1af7 on native Windows with an AMD host: pass at both. This adds the native comparison the description says was not measured.

python -B tests/startup_host_cpu_check.py
startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a)
  • Host: AMD Ryzen 5 3600 (Zen 2; has SSE4a and CLZERO), Windows 11 build 26300, CPython 3.13.15, iced-x86 1.21.0. Three runs at e6133013 and one at 9bbc1af7, all pass, exit 0; host_cpu.py and the check are identical between the two.
  • Also pass: python -B tests/docs_links_check.py (291 links at 9bbc1af7), python -B tests/bidi_controls_check.py, git diff --check.
  • tools/startup/run.ps1 parses with no errors and lists -HostCpu; parsed only, not run.

Not tested: the CMake registration (startup_host_cpu, skip code 77), the debug_launch.py path, and any end-to-end startup run.

AI-assisted (Claude Code); run on my own machine.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7125465456 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#issuecomment-7125465456). Originally posted by [Fr0st0007](https://github.com/Fr0st0007) on 2026-11-24T07:52:22Z. --- Tested `e6133013` and the current head `9bbc1af7` on native Windows with an AMD host: pass at both. This adds the native comparison the description says was not measured. ``` python -B tests/startup_host_cpu_check.py startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites; native comparison: clzero, sse4a) ``` - Host: AMD Ryzen 5 3600 (Zen 2; has SSE4a and CLZERO), Windows 11 build 26300, CPython 3.13.15, iced-x86 1.21.0. Three runs at `e6133013` and one at `9bbc1af7`, all pass, exit 0; `host_cpu.py` and the check are identical between the two. - Also pass: `python -B tests/docs_links_check.py` (291 links at `9bbc1af7`), `python -B tests/bidi_controls_check.py`, `git diff --check`. - `tools/startup/run.ps1` parses with no errors and lists `-HostCpu`; parsed only, not run. Not tested: the CMake registration (`startup_host_cpu`, skip code 77), the `debug_launch.py` path, and any end-to-end startup run. AI-assisted (Claude Code); run on my own machine.
lukaslw-lab commented 2026-11-24 10:02:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-24T09:24:31Z.


AI-assisted (Claude Code).

Tested on a host older than the ones above: CLWB → CLFLUSHOPT still faults on Haswell.

Host Intel Core i7-4790K (Haswell), Windows 10 22H2 (19045)
Tested #1312 head 87088c1e (includes this PR at 9bbc1af7) on main c4966c64, run.ps1 -HostCpu
Without -HostCpu 0xc000001d at the first CLWB (66 41 0F AE 31) after 3.1 s
With -HostCpu the site is rewritten to CLFLUSHOPT (66 41 0F AE 39) and raises 0xc000001d again at the same address after 1.8 s: Haswell has neither instruction
  • CLFLUSHOPT arrived with Skylake, so the rewrite only helps hosts that have CLFLUSHOPT but lack CLWB.
  • Locally I replaced the whole CLWB with NOPs of the same length (prefix, REX, 0F AE, ModRM, SIB, displacement). A write-back hint has no effect a single process can observe, as the PR already argues for the eviction. With that, the run continued through the same rewrite sites (CLWB ×5, CLZERO ×5, INSERTQ ×2, MONITORX/MWAITX ×1) to 420 s with no fault.
  • Suggestion: pick the replacement from CPUID — CLFLUSHOPT when present, otherwise a same-length NOP. startup_host_cpu_check.py could cover both choices.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127372778 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#issuecomment-7127372778). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-24T09:24:31Z. --- _AI-assisted (Claude Code)._ Tested on a host older than the ones above: **CLWB → CLFLUSHOPT still faults on Haswell**. | | | |---|---| | Host | Intel Core i7-4790K (Haswell), Windows 10 22H2 (19045) | | Tested | #1312 head `87088c1e` (includes this PR at `9bbc1af7`) on `main` `c4966c64`, `run.ps1 -HostCpu` | | Without `-HostCpu` | `0xc000001d` at the first CLWB (`66 41 0F AE 31`) after 3.1 s | | With `-HostCpu` | the site is rewritten to CLFLUSHOPT (`66 41 0F AE 39`) and raises `0xc000001d` again at the same address after 1.8 s: Haswell has neither instruction | - CLFLUSHOPT arrived with Skylake, so the rewrite only helps hosts that have CLFLUSHOPT but lack CLWB. - Locally I replaced the whole CLWB with NOPs of the same length (prefix, REX, `0F AE`, ModRM, SIB, displacement). A write-back hint has no effect a single process can observe, as the PR already argues for the eviction. With that, the run continued through the same rewrite sites (CLWB ×5, CLZERO ×5, INSERTQ ×2, MONITORX/MWAITX ×1) to 420 s with no fault. - Suggestion: pick the replacement from CPUID — CLFLUSHOPT when present, otherwise a same-length NOP. `startup_host_cpu_check.py` could cover both choices.
JohnConnorNPC commented 2026-11-24 10:02:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-24T09:38:27Z.


Thanks for the Haswell run; that is a host class I could not test. Fixed in aa529520, and merged into #1312 as 77396bf6.

Change. I did not add a CPUID query to the debugger; the fault already answers the question. When a CLFLUSHOPT raises the illegal-instruction exception (the rewritten site on your host), the site becomes one NOP of the same length, and from then on every CLWB goes straight to the NOP. Hosts that have CLFLUSHOPT keep the one-bit rewrite and never take that path. On your host the first CLWB site therefore costs two debugger round trips and each later one costs one. The summary counts them separately: clwb, clflushopt-nop, clwb-nop.

A single NOP instruction rather than a run of 0x90 keeps one instruction boundary where there was one, so a branch target inside the site cannot exist before or after. host_cpu.flush_length computes the length from ModRM, SIB and displacement, and flush_nop picks the 4- to 10-byte form.

Checked (tests/startup_host_cpu_check.py, pure Python with iced-x86 as the reference decoder):

  • for the five CLWB encodings already in the test, and for their CLFLUSHOPT rewrites, the NOP decodes as one NOP of the original length;
  • for every ModRM value with /6 or /7 and a memory operand, with no REX and three REX values, and four SIB bytes where one follows (including the no-base and rip-relative forms), flush_length equals the decoder's length; all seven lengths 4 to 10 occur;
  • /5, the register form, a missing 66 prefix and two truncated encodings are refused.
startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites, clwb/clflushopt NOPs for every addressing form; native comparison: none)

On #1312's head the debugger check still passes (startup_host_cpu_debug: PASS (performed by the debugger: rdpid x1, sha (per hit) x56)).

Not verified: the fall-back path inside the debugger. My host has CLFLUSHOPT, so the second fault never happens here; only the byte-level pieces are tested. If you can rerun run.ps1 -HostCpu on the Haswell machine at 77396bf6, the expected events.txt lines are one host-cpu clwb, one host-cpu clflushopt-nop at the same address, then host-cpu clwb-nop for the remaining sites.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127688247 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#issuecomment-7127688247). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-24T09:38:27Z. --- Thanks for the Haswell run; that is a host class I could not test. Fixed in `aa529520`, and merged into #1312 as `77396bf6`. **Change.** I did not add a CPUID query to the debugger; the fault already answers the question. When a CLFLUSHOPT raises the illegal-instruction exception (the rewritten site on your host), the site becomes **one NOP of the same length**, and from then on every CLWB goes straight to the NOP. Hosts that have CLFLUSHOPT keep the one-bit rewrite and never take that path. On your host the first CLWB site therefore costs two debugger round trips and each later one costs one. The summary counts them separately: `clwb`, `clflushopt-nop`, `clwb-nop`. A single NOP instruction rather than a run of `0x90` keeps one instruction boundary where there was one, so a branch target inside the site cannot exist before or after. `host_cpu.flush_length` computes the length from ModRM, SIB and displacement, and `flush_nop` picks the 4- to 10-byte form. **Checked** (`tests/startup_host_cpu_check.py`, pure Python with iced-x86 as the reference decoder): - for the five CLWB encodings already in the test, and for their CLFLUSHOPT rewrites, the NOP decodes as one NOP of the original length; - for every ModRM value with /6 or /7 and a memory operand, with no REX and three REX values, and four SIB bytes where one follows (including the no-base and rip-relative forms), `flush_length` equals the decoder's length; all seven lengths 4 to 10 occur; - `/5`, the register form, a missing 66 prefix and two truncated encodings are refused. ``` startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites, clwb/clflushopt NOPs for every addressing form; native comparison: none) ``` On #1312's head the debugger check still passes (`startup_host_cpu_debug: PASS (performed by the debugger: rdpid x1, sha (per hit) x56)`). **Not verified:** the fall-back path inside the debugger. My host has CLFLUSHOPT, so the second fault never happens here; only the byte-level pieces are tested. If you can rerun `run.ps1 -HostCpu` on the Haswell machine at `77396bf6`, the expected `events.txt` lines are one `host-cpu clwb`, one `host-cpu clflushopt-nop` at the same address, then `host-cpu clwb-nop` for the remaining sites.
upstream-sync closed this pull request 2026-11-24 11:02:33 +00:00
R0Y00 commented 2026-11-24 11:02:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:08:05Z.

Review: APPROVED


Verified the opt-in host CPU fixture on Windows x64 at this head: 288 SSE4a comparisons, both CLZERO relocation forms, and the CLWB and wait controls passed. The documented experimental scope applies.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6037261372 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1274#pullrequestreview-6037261372). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T10:08:05Z. **Review:** APPROVED --- Verified the opt-in host CPU fixture on Windows x64 at this head: 288 SSE4a comparisons, both CLZERO relocation forms, and the CLWB and wait controls passed. The documented experimental scope applies.
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!1267
No description provided.