cpu: lower XSAVE and XRSTOR state save and restore instructions #1058

Merged
Ammmae0x merged 6 commits from upstream-pr/1065 into main 2026-11-24 02:38:31 +00:00
Ammmae0x commented 2026-11-24 00:04:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T00:04:08Z.


Change

Add a bounded leaf AOT translator ( ools/cpu_xsave_aot.py) and source-built MASM/Python differential fixture ( ests/cpu/xsave_state.asm, ests/cpu/xsave_state.def, ests/cpu/xsave_state_check.py, CMakeLists.txt target cpu_aot_xsave_state, and docs/experiments.md) covering four uncovered x64 XSAVE forms from docs/coverage/x64_zen2.tsv:

  • xsave, xsave64, xrstor, and xrstor64

The translator models the bounded 832-byte standard-format x87|SSE|AVX (XCR0[2:0] = 7) XState locally without mutating host nonvolatile XMM6..15, x87, or MXCSR:

  • Enforces 64-byte alignment and bounded mask contract (EDX == 0 and (EAX & ~7) == 0).
  • xrstor/xrstor64: raises #GP before mutating state on non-zero XCOMP_BV, non-zero reserved header bytes [528..535], (XSTATE_BV & ~7) != 0, or (RFBM & 6) != 0 with invalid MXCSR & ~MXCSR_MASK; restores or initializes requested components and updates XINUSE[2:0].
  • xsave/xsave64: writes requested legacy x87/SSE ([0..415], honoring CPUID.7.0:EBX[13] for FCS/FDS and MXCSR_MASK at [28..31]) and AVX YMM[255:128] ([576..831]), leaves [416..511], XCOMP_BV ([520..527]), and header [528..575] untouched, and updates XSTATE_BV[2:0] = (old & ~RFBM) | (XINUSE & RFBM).
  • Transport and ABI glue covers movdqa/ movdqu (XMM0..5, YMM0..5), zeroupper, movdqa/movdqu, paddd, movq (MM0..7), mms, 32/64-bit volatile GPR mov/xor (with pointer provenance across register saves), and terminal
    et.

Verification

  • Built with 0 errors and 0 warnings on both dev and
    elease (/O2) presets:
    • cmake --build --preset dev --target cpu_xsave_state_original cpu_xsave_state_original_lifted cpu_xsave_state_altered cpu_xsave_state_altered_lifted --clean-first
    • cmake --build --preset release --target cpu_xsave_state_original cpu_xsave_state_original_lifted cpu_xsave_state_altered cpu_xsave_state_altered_lifted --clean-first
  • ctest --preset dev --no-tests=error -R "^cpu_aot_xsave_state$" -V: 1/1 Passed (70 native/lifted/reference cases across 10 exports; mxcsr_mask=0xffff, deprecates_fpu_cs_ds=True; 5 ALTER_OP divergence controls {'op_xrstor64_xsave64': 3, 'op_xrstor_xsave64': 3, 'op_xrstor64_disp': 3, 'op_xrstor64_mask_sse': 7, 'op_pipeline': 7}; 16 misalignment #GP checks; 16 header/MXCSR #GP checks; 5-mode op_discarded_read; host-state preservation; output mutation canary; 4 alternate-encoding lift checks; 32 unit rejection controls; 10 PE-patched DLL rejection controls).
  • ctest --preset release --no-tests=error -R "^cpu_aot_xsave_state$" -V: 1/1 Passed (identical counts).
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: passed; ools/form_coverage.py on cpu_aot_xsave_state JUnit XML confirmed xsave, xsave64, xrstor, xrstor64 covered (x64_zen2 12/948).
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py: passed (15 files, 240 links).
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py: passed (902 files).

Evidence and follow-up

  • All fixtures are source-built MASM ( ests/cpu/xsave_state.asm) and synthetic Python inputs under the MIT License; native execution gates on CPUID.1:ECX[26..28] and XGETBV(0) & 7 == 7 (SKIP_RETURN_CODE 77).
  • Per Intel SDM Vol. 1 Ch. 13.5.1, XSAVE may set XSTATE_BV[1:0] = 1 for initialized legacy x87/SSE components when selected by RFBM (observed directly on hybrid scheduling: XSTATE_BV = 0..7 on P-core CPU 0 vs XSTATE_BV[1:0] = 3 on E-core CPU 15 while XSTATE_BV[2] AVX init-tracking matched uniformly). The lifted translator matches the deterministic XINUSE model bit-for-bit, and the native comparator allows XSTATE_BV[1:0] flex only when RFBM selects an initialized legacy component.
  • Follow-up / out-of-scope: compacted/supervisor formats (xsavec*, xsaves*, xrstors*), xsaveopt*, state components beyond XCR0[2:0], and control flow.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6571474141 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1065). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T00:04:08Z. --- ## Change Add a bounded leaf AOT translator ( ools/cpu_xsave_aot.py) and source-built MASM/Python differential fixture ( ests/cpu/xsave_state.asm, ests/cpu/xsave_state.def, ests/cpu/xsave_state_check.py, CMakeLists.txt target cpu_aot_xsave_state, and docs/experiments.md) covering four uncovered x64 XSAVE forms from docs/coverage/x64_zen2.tsv: - xsave, xsave64, xrstor, and xrstor64 The translator models the bounded 832-byte standard-format x87|SSE|AVX (XCR0[2:0] = 7) XState locally without mutating host nonvolatile XMM6..15, x87, or MXCSR: - Enforces 64-byte alignment and bounded mask contract (EDX == 0 and (EAX & ~7) == 0). - xrstor/xrstor64: raises #GP before mutating state on non-zero XCOMP_BV, non-zero reserved header bytes [528..535], (XSTATE_BV & ~7) != 0, or (RFBM & 6) != 0 with invalid MXCSR & ~MXCSR_MASK; restores or initializes requested components and updates XINUSE[2:0]. - xsave/xsave64: writes requested legacy x87/SSE ([0..415], honoring CPUID.7.0:EBX[13] for FCS/FDS and MXCSR_MASK at [28..31]) and AVX YMM[255:128] ([576..831]), leaves [416..511], XCOMP_BV ([520..527]), and header [528..575] untouched, and updates XSTATE_BV[2:0] = (old & ~RFBM) | (XINUSE & RFBM). - Transport and ABI glue covers movdqa/ movdqu (XMM0..5, YMM0..5), zeroupper, movdqa/movdqu, paddd, movq (MM0..7), mms, 32/64-bit volatile GPR mov/xor (with pointer provenance across register saves), and terminal et. ## Verification - Built with 0 errors and 0 warnings on both dev and elease (/O2) presets: - cmake --build --preset dev --target cpu_xsave_state_original cpu_xsave_state_original_lifted cpu_xsave_state_altered cpu_xsave_state_altered_lifted --clean-first - cmake --build --preset release --target cpu_xsave_state_original cpu_xsave_state_original_lifted cpu_xsave_state_altered cpu_xsave_state_altered_lifted --clean-first - ctest --preset dev --no-tests=error -R "^cpu_aot_xsave_state$" -V: **1/1 Passed** (70 native/lifted/reference cases across 10 exports; mxcsr_mask=0xffff, deprecates_fpu_cs_ds=True; 5 ALTER_OP divergence controls {'op_xrstor64_xsave64': 3, 'op_xrstor_xsave64': 3, 'op_xrstor64_disp': 3, 'op_xrstor64_mask_sse': 7, 'op_pipeline': 7}; 16 misalignment #GP checks; 16 header/MXCSR #GP checks; 5-mode op_discarded_read; host-state preservation; output mutation canary; 4 alternate-encoding lift checks; 32 unit rejection controls; 10 PE-patched DLL rejection controls). - ctest --preset release --no-tests=error -R "^cpu_aot_xsave_state$" -V: **1/1 Passed** (identical counts). - .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: passed; ools/form_coverage.py on cpu_aot_xsave_state JUnit XML confirmed xsave, xsave64, xrstor, xrstor64 covered (x64_zen2 12/948). - .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py: passed (15 files, 240 links). - .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py: passed (902 files). ## Evidence and follow-up - All fixtures are source-built MASM ( ests/cpu/xsave_state.asm) and synthetic Python inputs under the MIT License; native execution gates on CPUID.1:ECX[26..28] and XGETBV(0) & 7 == 7 (SKIP_RETURN_CODE 77). - Per Intel SDM Vol. 1 Ch. 13.5.1, XSAVE may set XSTATE_BV[1:0] = 1 for initialized legacy x87/SSE components when selected by RFBM (observed directly on hybrid scheduling: XSTATE_BV = 0..7 on P-core CPU 0 vs XSTATE_BV[1:0] = 3 on E-core CPU 15 while XSTATE_BV[2] AVX init-tracking matched uniformly). The lifted translator matches the deterministic XINUSE model bit-for-bit, and the native comparator allows XSTATE_BV[1:0] flex only when RFBM selects an initialized legacy component. - Follow-up / out-of-scope: compacted/supervisor formats (xsavec*, xsaves*, xrstors*), xsaveopt*, state components beyond XCR0[2:0], and control flow. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 06:46:06 +00:00
R0Y00 commented 2026-11-24 06:46:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:13:38Z.

Review: CHANGES_REQUESTED


Windows Release passed all 70 native/lifted/reference cases with the fixture's initialized entry state. I then reused its source-built native trampoline with SSE in use, MXCSR 0x3f80 and the low XMM0 lane set to 0x3f800000. Both plain XSAVE exports saved those values natively. Both lifted exports saved MXCSR 0x1f80 and a zero lane. The ordinary three-pointer ABI currently gives the caller no way to supply or check this entry state.

Please make the initialized guest state an explicit opt-in profile at the translation entry and in its generated contract, or reject bodies that can observe state from entry before a preceding definition. Keep the existing initialized-state controls and add the nondefault entry case to check the boundary that you choose. This applies to partially initialized restore/save paths as well as the two entry-only exports.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031620078 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1065#pullrequestreview-6031620078). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:13:38Z. **Review:** CHANGES_REQUESTED --- Windows Release passed all 70 native/lifted/reference cases with the fixture's initialized entry state. I then reused its source-built native trampoline with SSE in use, MXCSR 0x3f80 and the low XMM0 lane set to 0x3f800000. Both plain XSAVE exports saved those values natively. Both lifted exports saved MXCSR 0x1f80 and a zero lane. The ordinary three-pointer ABI currently gives the caller no way to supply or check this entry state. Please make the initialized guest state an explicit opt-in profile at the translation entry and in its generated contract, or reject bodies that can observe state from entry before a preceding definition. Keep the existing initialized-state controls and add the nondefault entry case to check the boundary that you choose. This applies to partially initialized restore/save paths as well as the two entry-only exports.
R0Y00 commented 2026-11-24 06:46:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:45:23Z.

Review: APPROVED


The revised entry contract addresses my previous finding. The default now requires definition before a save can observe the incoming state, and the canonical opt-in names the initialized profile. I reran Windows Release on 60467c27. All 70 differential cases matched, the non-default entry control was detected, and dead-read, fault-ordering and output controls passed. Ready on these conditions once both Windows CI jobs pass.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031885663 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1065#pullrequestreview-6031885663). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:45:23Z. **Review:** APPROVED --- The revised entry contract addresses my previous finding. The default now requires definition before a save can observe the incoming state, and the canonical opt-in names the initialized profile. I reran Windows Release on 60467c27. All 70 differential cases matched, the non-default entry control was detected, and dead-read, fault-ordering and output controls passed. Ready on these conditions once both Windows CI jobs pass.
R0Y00 commented 2026-11-24 06:49:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:58:06Z.


The Windows runner reports heap corruption in cpu_aot_xsave_state. I found that the test harness saves every enabled host component but used fixed-size buffers, including the shorter FIP/FDP probe output. The new candidate sizes each full-state host area from CPUID leaf 0xD subleaf 0 EBX and checks the probe guard bytes. The translated guest profile is still the existing bounded 832-byte profile. Both local Windows presets passed all 70 differential cases and fault and entry controls. This host reports 832 bytes, so the CI rerun is needed to verify the original failure is gone. Intel CPUID reference
Candidate: ee082f52f0

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7118619232 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1065#issuecomment-7118619232). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:58:06Z. --- The Windows runner reports heap corruption in cpu_aot_xsave_state. I found that the test harness saves every enabled host component but used fixed-size buffers, including the shorter FIP/FDP probe output. The new candidate sizes each full-state host area from CPUID leaf 0xD subleaf 0 EBX and checks the probe guard bytes. The translated guest profile is still the existing bounded 832-byte profile. Both local Windows presets passed all 70 differential cases and fault and entry controls. This host reports 832 bytes, so the CI rerun is needed to verify the original failure is gone. [Intel CPUID reference](https://cdrdv2-public.intel.com/865891/319433-059-architecture-instruction-set-extensions-programming-reference_2025-0930.pdf) Candidate: ee082f52f0607c640000828f030474fa3b99efec
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!1058
No description provided.