cpu: restore the compacted XSAVE format in XRSTOR #1394
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1401"
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 tedin7 on 2026-11-24T10:38:09Z.
What
op_xrstorintools/cpu_xsave_aot.pynow accepts a header withXCOMP_BV[63]set, the layoutxsavecwrites. Before, any non-zeroXCOMP_BVraised#GPin the lifted code, so a buffer filled byxsaveccould not be restored.XCOMP_BV[62:0]may list only x87, SSE and AVX;XSTATE_BVmust be a subset of it; every header byte[528..575]must be zero. Any violation raises#GPbefore state changes. A requested component restores from its slot (x87 and SSE in the legacy area, AVX at 576, the first extended slot) whenXSTATE_BVandXCOMP_BVboth list it, and is initialized otherwise, including a componentXCOMP_BVleaves out.MXCSR_MASK) whenRFBM[1]andXSTATE_BV[1]are set, reset to0x1F80whenRFBM[1]is set andXSTATE_BV[1]is clear, untouched whenRFBM[1]is clear (an AVX-only restore keeps it). The standard format is unchanged: translated C++ of the 20 existing exports is identical before and after except theop_xrstorhelper.tests/cpu/xsave_state.{asm,def}: six new exports (fivexsavectoxrstorround trips of the same buffer with a different live state restored in between, one that restores a hand-built compacted image with its request mask read from the image).CMakeLists.txtregisters them, with twoALTER_OPvariants.tests/cpu/xsave_state_check.py: reference model for the compacted restore, 50 round trips, 216 header/RFBM combinations plus 4 MXCSR cases, 25#GPcontrols, 2ALTER_OPdivergences, 11 mutation controls, one new lift-acceptance case. Native runs are gated onCPUID.(0DH,1):EAX[1]like thexsavecfixtures (a compacted restore needs it).docs/experiments.mdand thetools/cpu_xsave_aot.pydocstring: the compacted rules, the table below, and the removal of "compacted XRSTOR" from the unsupported list.store64helper. The lowering of a register store to memory (mov [r8+disp], r64,movq [m64], mm) already emittedstore64(...), but nothing defined it, so such an export did not compile. The new fixtures need it to zero the header bytes beforexsavec.Why
xsavecwas lowered earlier, but a program that saves with it then restores the same buffer raised#GPin the lifted code, while the native instruction restores it. This closes that gap for the x87|SSE|AVX state the translator models. Hardware was measured first because the MXCSR rule differs from the standard format and a model written from the standard-format rule would have been wrong.Measured on one host (below):
xrstorof a compacted header, each case in a child process after a restore of a different state so restored, initialized and untouched are distinguishable.XCOMP_BV[62:0]has any of bits 3 to 9 or 62#GPXSTATE_BVhas a bitXCOMP_BV[62:0]lacks (bit 3, bit 63, x87 or SSE or AVX alone)#GP[528..575](all 48 bytes tried)#GP#GPXCOMP_BV[63]clear, otherXCOMP_BVbits set#GP(standard format)XCOMP_BV = bit 63only,RFBM = 7RFBM, listed byXSTATE_BVandXCOMP_BVRFBM, not listed byXSTATE_BV, or left out ofXCOMP_BVRFBMRFBM[1]andXSTATE_BV[1]set#GPRFBM[1]set,XSTATE_BV[1]clear0x1F80, image word not readRFBM[1]clear (RFBM = 4too)#GPRFBMbit outsideXCR0#GP), lifted onlyAll 216 combinations of
XCOMP_BV[2:0],XSTATE_BVandRFBMfollowed these rules, andXGETBV(1)after each restore matched the model'sXINUSE.xsavecleaves[528..575]untouched on this host, so the round-trip fixtures zero them first. The standard format on the same host loads MXCSR whenRFBM[2:1] != 0and checks only[528..535], as before.Measuring host:
AuthenticAMD, a KVM guest reporting "QEMU Virtual CPU version 2.5+" (family 15, model 107),XCR0 = 7, 832-byte area,CPUID.(0DH,1):EAX = 7(XSAVEOPT, XSAVEC, XGETBV(1); no XSAVES). Only this vendor was measured. An Intel host could differ in the MXCSR rule; the rule used matches whatxsavecwrites (MXCSR is stored with the SSE component only). A Windows run on an Intel machine is the thing to look at.Tested
Windows (not run locally):
(The registered test is
cpu_aot_xsave_state; a filter^cpu_xsave_stateselects no test.)Ran on Linux:
XCOMP_BV[63]set raised#GPin the lifted code; no other translator lowersxrstor(cpu_sse_fxsave_aot.pyonly lists the encodings as rejected); no open PR touches these files.llvm-ml-19and linked withlld-link-19(validating the MASM syntax). The realtranslate()ran on it. The C++ was built with MinGW GCC at-O0and-O2, and the native and lifted DLLs ran under Wine through a throwaway loader that stands in for the ctypes harness. The realxsave_state_check.pyfunctions ran on top of it (check_compacted_xrstor,check_fault_controls, the 190-case export loop with the native comparison, the altered loop, the dynamic-mask, MXCSR-liveness andxsaveoptlayout and control checks): all passed at both levels, including the compacted native and lifted comparisons and the#GPobservations (Wine reports the#GPas an access violation at -1). The native canonical entry exports, the entry-state contract checks, the dead-read check and the host-state check need the real Windows harness and were not run.XCOMP_BVbit check, the standard MXCSR rule, a compacted header treated as standard) each failed in the compacted checks with the expected message.check_alt_encodings_and_rejectionspassed on the LLVM-built PE (10 lift checks, 41 unit rejection controls, 10 PE-patched controls).tests/form_coverage_check.py,tests/docs_links_check.py,tests/bidi_controls_check.py,py_compileandgit diff --checkpass; no local variable in the edited Python shadows a module function.x2w_pipe.py preflight: WARN for diff size only (the diff is mostly the check and the fixture).MSVC/ML64 was NOT run locally, and the check script itself was not run end to end (it loads the DLLs with ctypes, which needs Windows).
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T10:53:21Z.
Review: COMMENTED
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:
06395b06b9ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:55:15Z.
Review: CHANGES_REQUESTED
The compacted restore correctly leaves MXCSR untouched when the mask selects AVX alone. The lifter still marks MXCSR defined for a constant mask containing AVX or SSE, so in the default defined profile a compacted AVX-only restore can let a later XSAVEOPT expose the translator's reset MXCSR value. The equivalent native sequence can retain the caller's non-default MXCSR. Please preserve the prior standard-format behavior and prevent this new compacted path from observing entry state under the default profile. Add a non-default entry-MXCSR control where a valid AVX-only compacted restore is followed by a save that observes MXCSR, together with the standard-format or SSE-defined control that still works.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:57:40Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:45:54Z.
Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit
06395b06:--preset dev):ctest --preset dev -R '^cpu_aot_xsave_state$' --no-tests=error --output-on-failure— 1/1 passed (0.42s)/O2(--preset release):ctest --preset release -R '^cpu_aot_xsave_state$' --no-tests=error --output-on-failure— 1/1 passed (0.43s)Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T10:46:06Z.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
06395b0ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T10:53:21Z.
Code review location:
tools/cpu_xsave_aot.py; line 526When the first restore uses a compacted image and RFBM=4, this path leaves MXCSR unchanged, but
lift_codestill marks it defined wheneverrax_val & 6is nonzero. Consequently,XRSTOR(4) -> XSAVEOPT(4)is accepted in the defaultdefinedprofile even though the save depends on incoming MXCSR. With entry MXCSR0x3F80, native code stores0x3F80at output offset 24, while the lifted code stores the local state's reset value0x1F80—confirmed using native instructions and the extracted helpers on x64 Linux. Only mark MXCSR defined when the restore guarantees its definition for both formats, or require the explicit canonical profile for such paths; a non-default-entry control would catch this.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:57:40Z.
Code review location:
tools/cpu_xsave_aot.py; line 526Fixed in
639ef5a1:lift_codenow setsdefined_mxcsronXRSTOR/XRSTOR64only whenrax_val & 2is set (since compactedXRSTORwithRFBM=4leaves MXCSR unchanged), andtests/cpu/xsave_state_check.pynow rejectsXRSTOR64(4) -> XSAVEOPT64(4)under the defaultdefinedprofile while accepting it undercanonical. Verifiedcpu_aot_xsave_state1/1 PASS on Win2025 Debug & Release/O2.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T11:08:49Z.
Review: CHANGES_REQUESTED
The new controls catch the compacted case. There is still a compatibility change: under the default defined profile, this AVX-only restore/save sequence was previously accepted for standard-format data and did not require a canonical caller. The new static check rejects that same sequence even when it only receives a standard header. Please keep the existing standard-only path available under the existing default contract, and use an explicit opt-in for an input that may be compacted where the formats cannot be distinguished at lift time. Verify the same default-profile entry with standard input and a non-default caller MXCSR, along with the new compacted refusal and the SSE-defined positive.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T11:57:00Z.
Review: APPROVED
Re-reviewed the standard-only default and the opt-in compacted restore. Windows Release native/lifted XSAVE state tests passed, including the AVX-only default path, the compacted MXCSR controls, and the format/header controls.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:03:56Z.
@R0Y00 Addressed your review feedback across
639ef5a1..bb3d5bd4:tools/cpu_xsave_aot.py):XRSTOR/XRSTOR64now marksdefined_mxcsr = Trueonly whenRFBM[1](SSE) is set (rax_val & 2), so an AVX-only restore followed byXSAVEOPTis rejected under the defaultdefinedprofile (preventing a compacted AVX-only restore from observing entry MXCSR) and requires--entry-state canonical. Prior standard-format behavior is preserved at runtime and undercanonical.tests/cpu/xsave_state.asm,tests/cpu/xsave_state_check.py):op_xrstor64_avx_xsaveopt64_avx(verified rejected underdefined, accepted undercanonical): with non-default entry MXCSR (0x3F80) and a valid AVX-only compacted image, native retains0x3F80whilecanonical-lifted writes0x1F80; with a standard-format image (MXCSR=0x5F80), both native andcanonical-lifted load and save0x5F80.op_xrstor64_sse_compacted_avx_xsaveopt64_avxunder thedefinedprofile (XRSTOR64(2)followed by compactedXRSTOR64(4)andXSAVEOPT64(4)): both native anddefined-lifted observe0x5F80and never observe caller's0x3F80.XRSTOR64(4) -> XSAVEOPT64(4)andXRSTOR64(4) -> VZEROUPPER -> XSAVEOPT64(4).Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit
bb3d5bd47c8e5da31aaeb545d29fe3a9f051148f:--preset dev):cpu_aot_xsave_state1/1 passed (0.42s)/O2(--preset release):cpu_aot_xsave_state1/1 passed (0.42s)Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:28:48Z.
@R0Y00 Addressed your feedback in
98c1b943..f4442ee8:tools/cpu_xsave_aot.py): The default contract (--entry-state defined, without--compacted-xrstor) keeps the standard-onlyop_xrstorpath (XCOMP_BV != 0raises#GP,RFBM & 6defines MXCSR), soop_xrstor64_avx_xsaveopt64_avxandxrstor_avx_vzeroupper_xsaveopt64_avxcontinue to lift under the defaultdefinedprofile without requiringcanonical.--compacted-xrstoropt-in: Passing--compacted-xrstor(compacted_xrstor=True) enables compactedXRSTORheaders (XCOMP_BV[63]) and requiresRFBM[1](SSE) to define MXCSR underdefined, rejecting AVX-onlyXRSTOR(4) -> XSAVEOPT(4)unless--entry-state canonicalor a prior SSE restore is present.f4442ee8(cpu_aot_xsave_stateDebug0.47s, Release/O20.44s, 1/1 passed): default-profileop_xrstor64_avx_xsaveopt64_avxwithstandard_avxand non-default caller MXCSR (0x3F80) loads0x5F80and matches native (while#GPing oncompacted_avx),--compacted-xrstorrefuses AVX-only underdefined,--entry-state canonical --compacted-xrstorhandles both, and SSE-definedop_xrstor64_sse_compacted_avx_xsaveopt64_avxpasses.