Skip to content

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

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

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

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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).
  • 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

As agreed in R0Y00#995, lower(..., host_mxcsr=Mxcsr(reject)) lowers the
legacy m32 forms of LDMXCSR and STMXCSR; the default profile still
rejects both, and the VEX forms stay rejected. LDMXCSR reads the word
as a guest operand and checks it before the host register changes. A
reserved bit, an unmasked exception, or FTZ or DAZ leaves the function
through reject with a distinct status, and no later guest instruction
runs. Any rounding control and the sticky flags are loaded with
_mm_setcsr. STMXCSR stores _mm_getcsr().

mxcsr_entry is the checked boundary. It saves the caller's whole MXCSR
and restores it in a __finally on return, after a refusal and when a
guest memory fault leaves the body, and it keeps the refusal status per
thread. Files that use the profile start with #pragma fenv_access(on).

cpu_aot_mxcsr round-trips every rounding control with and without sticky
flags from a non-default caller word. It compares ADDPS cases, including
two halfway cases, and CVTSS2SI of runtime operands with native
execution and fixed expectations. It checks adjacent bytes, the caller's
word after return, refusal and both fault sites, the per-thread status,
and that refused words stop the guest. The noload and fixed controls
must fail, and the /O2 build keeps every rule.

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

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

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

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Contributor Author

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 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