Skip to content

cpu: lower CPUID and XGETBV in an opt-in host-CPU profile - #969

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/lower-cpuid
Nov 23, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:cpu/lower-cpuid

Conversation

@legacy-code-archaeologist

@legacy-code-archaeologist legacy-code-archaeologist commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Change

Lowers CPUID and XGETBV in tools/ffx_version_query.py as the opt-in host-CPU profile agreed in #937. The profile is enabled by lower(..., host_cpu=True); the default profile still rejects both instructions.

They report the host processor, not a console CPU. No guest feature table is applied.

  • CPUID passes the guest EAX leaf and ECX subleaf to __cpuidex and writes EAX, EBX, ECX and EDX zero-extended.
  • XGETBV passes ECX to _xgetbv and writes EDX:EAX zero-extended.
  • Both keep the flags.
  • XGETBV needs CPUID.01H:ECX bit 27 (OSXSAVE). The test checks that bit, not the lowering. An index the host does not support faults as it does natively.
  • /O2: a probe with cl /O2 (MSVC 19.52) showed that a discarded __cpuidex keeps its cpuid, while a discarded _xgetbv is removed. So XGETBV keeps its result in a volatile local, and its plain control repeats the finding inside the test.

Tests

cpu_aot_cpuid (cpuid.asm) and cpu_aot_xgetbv (xgetbv.asm) have separate fixtures. A native harness supplies the expected values; it is never translated.

Pinning. Both checks pin the thread, in its processor group, to the logical processor it is on, and confirm it runs there. Every native and translated read, and the harness reads they are compared with, run there. The previous group affinity is restored in a finally.

CPUID check:

  • Covers leaves 00H, 01H, 04H, 07H, 0BH and 0DH, with subleaves 0 and 1 where the leaf takes one, plus 80000000H and 80000001H, up to the host's maximum leaves.
  • Inputs: EAX and ECX start with their upper halves set, and RBX and RDX with all ones.
  • For four add r8, r8 presets, all four results must equal the host's, with zero upper halves and ZF/CF kept.
  • A CPUID whose result is overwritten must return the overwritten value.
  • The fixture saves RBX in a state slot, because the shared lowering has no PUSH.

XGETBV check:

  • Exits 77 without OSXSAVE before any XGETBV runs.
  • Compares XCR0 only, the supported contract: a caller that checked OSXSAVE and reads ECX = 0. The lowering passes any ECX through; other indices have no supported contract, and XINUSE (ECX = 1) stays outside the profile.
  • A reserved index (0x7fffffff) runs in a child process. Native and translated code must report the same fault. This checks fault passthrough only, not XINUSE.

Controls (mutations of the generated C++, each must break a property; every replacement is anchored on the new statement):

  • noext: keeps an upper half.
  • swap: exchanges EBX and EDX for CPUID, or the two halves for XGETBV.
  • nosubleaf (CPUID): drops ECX. It is measurable only when a covered leaf differs between subleaf 0 and 1; otherwise the test exits 77 after its other checks.
  • constant: a fixed result. XGETBV uses 0, which is never XCR0 because bit 0 is always set.
  • flags: flips ZF and CF.

/O2: the translations are also built with cl /O2. A walk that follows every branch must find each CPUID or XGETBV, including the discarded one, and the rules must still hold. XGETBV's plain control (no volatile) must lose the discarded read.

Verification

At d10c6fe (on main a48c2b9) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ (OSXSAVE set, XCR0 0x1f), MSVC 19.52, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_(cpuid|xgetbv)$' -V: both pass.
    • CPUID: 12 leaf/subleaf pairs match the host. Leaves 04H, 07H, 0BH and 0DH depend on the subleaf, so nosubleaf is measured (CPUID(0x4, 1) gave … 0x1c004121 …, host … 0x1c004122 …).
    • Every control reports its broken property.
    • XGETBV: XCR0 0x1f matches. cpu_xgetbv_plain_o2 reports {'xgetbv_op': 1, 'xgetbv_unused': 0}. The reserved index faults alike in both children: exception: access violation reading 0xFFFFFFFFFFFFFFFF, which is how Windows reports the #GP.
  • A simulated host without OSXSAVE (has_osxsave forced to 0, every XGETBV call raising): cpu_aot_xgetbv exits 77 before any XGETBV.
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 185 of 186 pass, with three capability skips (cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha, cpu_shim_chain_hardware).
  • tools/form_coverage.py on a JUnit run of that suite no longer lists cpuid or xgetbv.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

No README row lists individual lowered instructions (as for #789 and #807), so the README is unchanged.

Limits

  • Only this Intel host was run. Not run on AMD, on a hybrid processor (the docs note the values hold for the one logical processor tested), with more than 64 logical processors, or without OSXSAVE (only simulated).

Refs #937

  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: written with Claude Code; design and diff reviewed by fresh agents; checked and submitted by the account owner.

🤖 Generated with Claude Code

As agreed in R0Y00#937, lower(..., host_cpu=True) lowers CPUID and XGETBV; the
default profile still rejects both. They report the host processor; no
guest feature table is applied. CPUID passes the guest EAX leaf and ECX
subleaf to __cpuidex and writes all four results zero-extended. XGETBV
passes ECX to _xgetbv and writes EDX:EAX zero-extended. Both keep the
flags. A discarded CPUID stays in /O2 code; XGETBV keeps a volatile
result, because /O2 removes a discarded one without it.

cpu_aot_cpuid and cpu_aot_xgetbv pin the thread to its logical processor
and restore the previous group affinity afterwards. They compare native,
translated and harness reads for twelve leaf/subleaf pairs and for XCR0,
with zero-extended results and kept flags. XGETBV exits 77 without
OSXSAVE before any XGETBV runs, reads XCR0 only, and checks a reserved
index in a child process, where native and translated code report the
same fault. Mutated controls must each break a property, and /O2 builds
are walked to confirm every read survives.

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

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

The pinned-thread comparison and discarded-read control match #937. The docs currently say XGETBV reads XCR0 only, while the emitted code forwards any ECX to the host intrinsic. Please distinguish the supported caller contract (OSXSAVE and XCR0) from what the passthrough may execute. An invalid-index child verifies native fault behavior. It does not verify XINUSE on a host where index 1 is valid and observes the current extended-state use. Keep that state outside the first profile unless it has its own native differential.

… executes

The lowering passes any ECX to the host; the supported contract is a caller
that checked OSXSAVE and reads XCR0. The invalid-index child checks fault
passthrough only, and XINUSE stays outside the profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks. b22457d separates the two in the docs, the lowering comment and the PR description:

  • What the lowering executes: XGETBV passes any ECX to _xgetbv unchanged.
  • The supported contract: a caller that checked OSXSAVE (the lowering does not check it) and reads XCR0 (ECX = 0).
  • Other indices are passed through without a supported contract:
    • an index the host rejects faults as natively;
    • where the host supports XINUSE (ECX = 1), the read reports the host's current extended-state use, which this profile does not verify.
  • The invalid-index child is described as checking fault passthrough only, not XINUSE.

The generated C++ is unchanged. cpu_aot_cpuid and cpu_aot_xgetbv still pass, and tests/docs_links_check.py and tests/bidi_controls_check.py pass.

AI-assisted (Claude Code).

@R0Y00
R0Y00 merged commit e58a231 into R0Y00:main Nov 23, 2026
5 checks passed
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.

2 participants