cpu: rewrite instructions the host processor lacks in the startup debugger #1267
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1274"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.pyends 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.CLWB m8CLFLUSHOPT m8(one ModRM bit)MONITORX,MWAITXNOP;PAUSE+NOP(the wait may end at any time)CLZEROrax & ~63INSERTQ xmm, xmm, len, idx,EXTRQ xmm, len, idx(xmm0-7)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 newtools/startup/host_cpu.py;debug_launch.pyonly 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
INSERTQin the debugger on every hit behindX2W_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 oneINSERTQpath is needed; I am happy to dropINSERTQfrom this PR, or rebase on #1229, whichever the CPU reviewer prefers.codex/static-clzero-loopslowers 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
mainat99cde8d6.The check assembles source-built functions byte by byte, patches them the way the debugger does and calls them on the host:
INSERTQ/EXTRQresults over five register pairs, nine fields and four value pairs, against a model of the instruction;CLZEROzeroing exactly its line (neighbours intact), with CF kept, once with a movedleaand once with a moved rip-relative load;CLWB→CLFLUSHOPTfor five addressing forms, decoded before and after with iced-x86 (same length and memory operand), and the two wait rewrites;INSERTQstub checked to keeprcx,rdxand CF.Negative controls: a
CLZEROstub that zeroes 56 bytes, one that zeroes 72, and anINSERTQstub 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:
VirtualAlloc,rcx/rdxarguments). 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) andgit 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 reportedhost-cpu sites rewritten: clwb x5, clzero x5, insertq x2, monitorx x1, mwaitx x1in a 100 s run that was still presenting frames at the timeout. Without the switch the same run stops at the firstCLWB.Not measured
startup_host_cpu, skip code 77) and therun.ps1switch are unbuilt and unrun there; CI is the first Windows run of both.EXTRQhas only run in the check, not in a startup run.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->/7CLFLUSHOPT, memory forms only),wait_rewrite(MONITORX-> 3-byteNOP,MWAITX->PAUSE; NOP),CLZERO_STUB(BMI2shrx/shlxto preserve flags while maskingrax &= ~63and zeroing 64 bytes), andsse4a_stub(immediateINSERTQ/EXTRQforxmm0–xmm7,offset + width <= 64, preservingflags,rax/rcx/rdx, and127:64) are clean and refuse unsupported forms rather than approximating them.plan_replacement: usesiced_x86.BlockEncoderwhenlength < 5to relocate overwritten instructions (including RIP-relative operands) behind the stub with aJMP rel32back.tools/startup/debug_launch.py&tools/startup/run.ps1: Gated behindX2W_HOST_CPU=1(-HostCpu), handles first-chance0xC000001Din the main image ([main_base, main_base + 0x10000000)), allocates stub pages within 256 MiB belowmain_baseviaVirtualAllocEx, and callsFlushInstructionCache.rewrite_for_hostassumesplan_replacementwill not raise fromiced_x86.BlockEncoderon an un-relocatable adjacent instruction; catchingExceptionand returningNonewould 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-byteCLZEROstubs, shifted-offsetINSERTQstub, 6 refused encodings) and native CPU comparison (clzero,sse4a) which executed and passed on both our Linux and Windows Server 2025 AMD hosts.Verification
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)tests/startup_host_cpu_check.py(viax2w_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)--preset dev) & Release/O2(--preset release) CMake configure/build succeeded (3.74s/1.09s).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.56f81dd9282cac2afff15608dd32ef3a8551426626300.945732.0.21045.160103.13.15,iced-x86==1.21.0Command from the PR source directory, with the test virtual environment active:
Expected: exit code
0and aPASScovering 288 INSERTQ/EXTRQ results, two CLZERO relocation forms, five CLWB rewrites, and two wait rewrites.Actual:
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.
Imported from R0Y00/Xbox2Windows. Originally posted by Fr0st0007 on 2026-11-24T07:52:22Z.
Tested
e6133013and the current head9bbc1af7on native Windows with an AMD host: pass at both. This adds the native comparison the description says was not measured.e6133013and one at9bbc1af7, all pass, exit 0;host_cpu.pyand the check are identical between the two.python -B tests/docs_links_check.py(291 links at9bbc1af7),python -B tests/bidi_controls_check.py,git diff --check.tools/startup/run.ps1parses with no errors and lists-HostCpu; parsed only, not run.Not tested: the CMake registration (
startup_host_cpu, skip code 77), thedebug_launch.pypath, and any end-to-end startup run.AI-assisted (Claude Code); run on my own machine.
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.
87088c1e(includes this PR at9bbc1af7) onmainc4966c64,run.ps1 -HostCpu-HostCpu0xc000001dat the first CLWB (66 41 0F AE 31) after 3.1 s-HostCpu66 41 0F AE 39) and raises0xc000001dagain at the same address after 1.8 s: Haswell has neither instruction0F 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.startup_host_cpu_check.pycould cover both choices.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 as77396bf6.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
0x90keeps one instruction boundary where there was one, so a branch target inside the site cannot exist before or after.host_cpu.flush_lengthcomputes the length from ModRM, SIB and displacement, andflush_noppicks the 4- to 10-byte form.Checked (
tests/startup_host_cpu_check.py, pure Python with iced-x86 as the reference decoder):flush_lengthequals 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.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 -HostCpuon the Haswell machine at77396bf6, the expectedevents.txtlines are onehost-cpu clwb, onehost-cpu clflushopt-nopat the same address, thenhost-cpu clwb-nopfor the remaining sites.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.