cpu: lower LDMXCSR and STMXCSR in an opt-in host-MXCSR profile #1002

Merged
legacy-code-archaeologist merged 2 commits from upstream-pr/1009 into main 2026-11-23 21:39:34 +00:00
legacy-code-archaeologist commented 2026-11-23 21:09:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T21:09:50Z.


Change

Lowers the legacy m32 forms of LDMXCSR and STMXCSR in tools/ffx_version_query.py as the opt-in host-MXCSR profile agreed in #995. The profile is enabled by lower(..., host_mxcsr=Mxcsr(reject)). The default profile still rejects both instructions, the VEX forms stay rejected, and existing fixed-profile generation is unchanged.

LDMXCSR

  • Reads the requested word as a guest operand (load_always), so an inaccessible operand faults before anything is loaded.
  • Checks the word before it reaches the host register. These words leave the function through reject with a distinct status from MXCSR_STATUS, and no later guest instruction runs:
    • a reserved bit (16–31): status 1;
    • an unmasked exception (one of bits 7–12 clear): status 2;
    • FTZ (bit 15) or DAZ (bit 6): status 3.
  • A word that passes, with any rounding control and any sticky flags, is loaded with _mm_setcsr.

STMXCSR stores _mm_getcsr().

Checked boundary (mxcsr_entry)

  • It saves the caller's whole MXCSR and restores it in a __finally: on a normal return, after a refusal, and when a structured exception such as a guest memory fault leaves the lowered body.
  • <name>_status() returns the refusal status of the calling thread's last call; the status is thread_local.
  • A file that uses the profile starts with #pragma fenv_access(on) (MXCSR_HEADER).

Tests

cpu_aot_mxcsr (mxcsr.asm, native harness for reading and writing the host MXCSR).

Rounding. From a non-default caller word (0x7FA0: round toward zero with the precision flag set), every rounding control is loaded with and without the six sticky flags. Operands the test writes at run time:

  • ADDPS of 1 + 0.75 ulp and its negation;
  • two halfway cases, 1 + 0.5 ulp and (1 + 1 ulp) + 0.5 ulp, which to-nearest-even rounds in opposite directions;
  • CVTSS2SI of 2.75 and −2.75.

Together they give a distinct result in each mode. All four lanes and both conversions must match native execution and fixed expectations.

Read-back and memory. STMXCSR must read back the loaded control bits and the requested sticky flags. The bytes next to every stored word must stay unchanged, and the caller's word must be back afterwards.

Boundary. The translated entry must restore the caller's word in each of these cases:

  • the guest returns without restoring it (mxcsr_leak);
  • after each refused word;
  • when a guest load faults, both at the LDMXCSR operand (mxcsr_badload) and after a valid load (mxcsr_fault).

Refusals. The refused words 0x11F80, 0x1D80, 0x9F80 and 0x1FC0 must return statuses 1, 2, 3 and 3. Each must stop the guest before its next store, with MXCSR unchanged. A refusal status stays with its thread while another thread calls the same entry successfully.

Default profile. It must refuse each LDMXCSR and STMXCSR of the fixture on its own.

Controls (both must fail):

  • noload: the word is checked but never loaded.
  • fixed: STMXCSR stores 0x1F80.

/O2. The translation is also built with cl /O2 and must keep every rule. Every export must still reach at least its source's LDMXCSR and STMXCSR.

Native reference. In a child process, a native LDMXCSR of a reserved bit raises a #GP, which Windows reports as access violation reading 0xFFFFFFFFFFFFFFFF. This is recorded as a reference observation, not as the promised status of the checked entry.

Not tested: fenv_access itself has no separate negative control. MSVC's own documentation is the basis for it, and the rounding comparisons pass in both the normal and the /O2 build.

Verification

At 2fca753 (on main 8084aee) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ, MSVC 19.52, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_mxcsr$' -V: passes. The output includes:
    • the default profile refuses each of the 8 MXCSR instructions on its own: 6 LDMXCSR, 2 STMXCSR;
    • the noload control: rounding control 0: lanes/conversions (… 1065353216, 1065353217), (2, -2), expected (… 1065353216, 1065353218, 3, -3);
    • the fixed control: entry STMXCSR stored 0x1f80, caller 0x7fa0.
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 191 of 192 pass, with three capability skips (cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha, cpu_shim_chain_hardware).
    • The failure is cpu_shim_shadow_hardware in its native run, independent of this change and reported in #799.
  • tools/form_coverage.py on a JUnit run of that suite no longer lists ldmxcsr or stmxcsr.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

No README row lists individual lowered instructions, so the README is unchanged.

Out of scope

FTZ and DAZ (refused with status 3), the VEX forms, unmasked exceptions, and AMD hosts (not run).

Refs #995

  • 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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6568893964 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1009). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T21:09:50Z. --- ## Change Lowers the legacy `m32` forms of LDMXCSR and STMXCSR in `tools/ffx_version_query.py` as the opt-in host-MXCSR profile agreed in #995. The profile is enabled by `lower(..., host_mxcsr=Mxcsr(reject))`. The default profile still rejects both instructions, the VEX forms stay rejected, and existing fixed-profile generation is unchanged. **LDMXCSR** - Reads the requested word as a guest operand (`load_always`), so an inaccessible operand faults before anything is loaded. - Checks the word before it reaches the host register. These words leave the function through `reject` with a distinct status from `MXCSR_STATUS`, and no later guest instruction runs: - a reserved bit (16–31): status 1; - an unmasked exception (one of bits 7–12 clear): status 2; - FTZ (bit 15) or DAZ (bit 6): status 3. - A word that passes, with any rounding control and any sticky flags, is loaded with `_mm_setcsr`. **STMXCSR** stores `_mm_getcsr()`. **Checked boundary (`mxcsr_entry`)** - It saves the caller's whole MXCSR and restores it in a `__finally`: on a normal return, after a refusal, and when a structured exception such as a guest memory fault leaves the lowered body. - `<name>_status()` returns the refusal status of the calling thread's last call; the status is `thread_local`. - A file that uses the profile starts with `#pragma fenv_access(on)` (`MXCSR_HEADER`). ## Tests `cpu_aot_mxcsr` ([`mxcsr.asm`](tests/cpu/mxcsr.asm), native [harness](tests/cpu/mxcsr_harness.asm) for reading and writing the host MXCSR). **Rounding.** From a non-default caller word (`0x7FA0`: round toward zero with the precision flag set), every rounding control is loaded with and without the six sticky flags. Operands the test writes at run time: - ADDPS of 1 + 0.75 ulp and its negation; - two halfway cases, 1 + 0.5 ulp and (1 + 1 ulp) + 0.5 ulp, which to-nearest-even rounds in opposite directions; - CVTSS2SI of 2.75 and −2.75. Together they give a distinct result in each mode. All four lanes and both conversions must match native execution and fixed expectations. **Read-back and memory.** STMXCSR must read back the loaded control bits and the requested sticky flags. The bytes next to every stored word must stay unchanged, and the caller's word must be back afterwards. **Boundary.** The translated entry must restore the caller's word in each of these cases: - the guest returns without restoring it (`mxcsr_leak`); - after each refused word; - when a guest load faults, both at the LDMXCSR operand (`mxcsr_badload`) and after a valid load (`mxcsr_fault`). **Refusals.** The refused words `0x11F80`, `0x1D80`, `0x9F80` and `0x1FC0` must return statuses 1, 2, 3 and 3. Each must stop the guest before its next store, with MXCSR unchanged. A refusal status stays with its thread while another thread calls the same entry successfully. **Default profile.** It must refuse each LDMXCSR and STMXCSR of the fixture on its own. **Controls** (both must fail): - `noload`: the word is checked but never loaded. - `fixed`: STMXCSR stores `0x1F80`. **`/O2`.** The translation is also built with `cl /O2` and must keep every rule. Every export must still reach at least its source's LDMXCSR and STMXCSR. **Native reference.** In a child process, a native LDMXCSR of a reserved bit raises a #GP, which Windows reports as `access violation reading 0xFFFFFFFFFFFFFFFF`. This is recorded as a reference observation, not as the promised status of the checked entry. **Not tested:** `fenv_access` itself has no separate negative control. MSVC's own documentation is the basis for it, and the rounding comparisons pass in both the normal and the `/O2` build. ## Verification At `2fca753` (on `main` `8084aee`) on Windows 11 Pro (build 26100), Intel Core i7-6820HQ, MSVC 19.52, Python 3.12: - `ctest --preset dev --no-tests=error -R '^cpu_aot_mxcsr$' -V`: passes. The output includes: - `the default profile refuses each of the 8 MXCSR instructions on its own: 6 LDMXCSR, 2 STMXCSR`; - the `noload` control: `rounding control 0: lanes/conversions (… 1065353216, 1065353217), (2, -2), expected (… 1065353216, 1065353218, 3, -3)`; - the `fixed` control: `entry STMXCSR stored 0x1f80, caller 0x7fa0`. - `ctest --preset dev --no-tests=error -R '^cpu_' -j 4`: 191 of 192 pass, with three capability skips (`cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha`, `cpu_shim_chain_hardware`). - The failure is `cpu_shim_shadow_hardware` in its native run, independent of this change and reported in #799. - `tools/form_coverage.py` on a JUnit run of that suite no longer lists `ldmxcsr` or `stmxcsr`. - `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `tests/form_coverage_check.py` pass. No README row lists individual lowered instructions, so the README is unchanged. ## Out of scope FTZ and DAZ (refused with status 3), the VEX forms, unmasked exceptions, and AMD hosts (not run). Refs #995 - [x] 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](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:49 +00:00
R0Y00 commented 2026-11-24 06:49:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T21:12:33Z.


The /O2 rounding fixtures and the SEH restoration match the agreed first slice. One edge is missing from the status contract: mxcsr_entry currently writes last_status after the __finally. If this export first returns a refusal and later faults, the second call restores MXCSR but leaves the first call's refusal visible. Please clear the per-export status at entry or publish the current status in the finally block, and check refusal followed by a fault on the same export/thread. The exception remains the fault result.

Please also state the entry environment before the guest's first LDMXCSR (masked exceptions and DAZ/FTZ off, any allowed rounding and sticky flags) and keep the initial opt-in boundary to the tested leaf profile. Use the first-line source coverage marker for the translated fixture, leaving the native-only MXCSR harness out of the coverage count.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114151306 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1009#issuecomment-7114151306). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:12:33Z. --- The /O2 rounding fixtures and the SEH restoration match the agreed first slice. One edge is missing from the status contract: `mxcsr_entry` currently writes `last_status` after the `__finally`. If this export first returns a refusal and later faults, the second call restores MXCSR but leaves the first call's refusal visible. Please clear the per-export status at entry or publish the current status in the finally block, and check refusal followed by a fault on the same export/thread. The exception remains the fault result. Please also state the entry environment before the guest's first LDMXCSR (masked exceptions and DAZ/FTZ off, any allowed rounding and sticky flags) and keep the initial opt-in boundary to the tested leaf profile. Use the first-line source coverage marker for the translated fixture, leaving the native-only MXCSR harness out of the coverage count.
R0Y00 commented 2026-11-24 06:49:41 +00:00 (Migrated from github.com)

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


I pushed 6832a9dd to update the refusal status in the same __finally that restores MXCSR. The new same-thread, same-export control first records a reserved-bit refusal, then takes a guest access violation. It failed on the previous head due to stale status and passes with the fix, including the explicit /O2 translation. ctest --preset release --no-tests=error -R '^cpu_aot_mxcsr$' -V passed 1/1. The notes now state the starting MXCSR requirements. GitHub CI is running on the updated head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114427375 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1009#issuecomment-7114427375). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:24:50Z. --- I pushed `6832a9dd` to update the refusal status in the same `__finally` that restores MXCSR. The new same-thread, same-export control first records a reserved-bit refusal, then takes a guest access violation. It failed on the previous head due to stale status and passes with the fix, including the explicit `/O2` translation. `ctest --preset release --no-tests=error -R '^cpu_aot_mxcsr$' -V` passed 1/1. The notes now state the starting MXCSR requirements. GitHub CI is running on the updated head.
legacy-code-archaeologist commented 2026-11-24 06:49:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T21:28:18Z.


Thanks for 6832a9dd. With it on the Skylake host (Windows 11 26100, MSVC 19.52, dev preset), ctest --preset dev --no-tests=error -R '^cpu_aot_mxcsr$' -V passes 1/1, including your refusal-then-fault control and the /O2 translation. tests/form_coverage_check.py passes with only the first-line marker counting the translated fixture and mxcsr_harness left as native-only.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114504835 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1009#issuecomment-7114504835). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T21:28:18Z. --- Thanks for `6832a9dd`. With it on the Skylake host (Windows 11 26100, MSVC 19.52, `dev` preset), `ctest --preset dev --no-tests=error -R '^cpu_aot_mxcsr$' -V` passes 1/1, including your refusal-then-fault control and the `/O2` translation. `tests/form_coverage_check.py` passes with only the first-line marker counting the translated fixture and `mxcsr_harness` left as native-only. AI-assisted (Claude Code).
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!1002
No description provided.