cpu: verify x87 FNSTENV/FNSAVE after FNINIT and wait-prefixed refusal (Part of #870) #2091

Merged
tedin7 merged 2 commits from upstream-pr/2098 into main 2026-11-25 11:56:11 +00:00
tedin7 commented 2026-11-25 00:14:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:14:06Z.


Part of #870

What this unblocks: x87 code that saves the FPU environment (fnstenv, fnsave) right after fninit now translates in the checked profile instead of being refused as environment access.

Summary:

  • tools/x87_lower.py: accept fnstenv (28 bytes) and fnsave (108 bytes) in the 32-bit protected-mode layout. They follow the existing fnstsw m16 rule: only where the whole status word is known, which is right after fninit. There the stack is empty, the status word is 0, the tag word is 0xFFFF and the pointers are zero, so the stored words are exact. fnsave stores ST(0) to ST(7) from the model's slots (the SDM does not define an empty register's bytes), then initializes as fninit does, so the function must restore control word 0x027F. Only the checked entry accepts fnsave.
  • fstenv and fsave (WAIT plus the store) are still refused, with the unknown bits named: the existing rule says WAIT leaves C0 to C3 undefined (cpu_aot_x87_status tests this for fstsw), so the stored word is not known. finit then fnstenv is accepted. I did not change that rule.
  • The 16-bit operand-size forms are refused.
  • tests/cpu/x87_environment_check.py, registered as cpu_x87_environment_controls in CMakeLists.txt: pure Python. It compares the emitted stores with the SDM layout built in the test, and checks the refusals.
  • docs/experiments.md and the module docstring describe the rule.

Open question: if the project wants fstenv/fsave accepted, the WAIT rule has to change for fstsw m16 too (its existing rejection case in tests/cpu/x87_status.py would flip). That is a separate decision.

Verification:

  • Not run on Win2025: the change is Python-only and there is no native fixture for these forms; the check compares against the SDM layout, not the CPU.
  • python3 -B tests/cpu/x87_environment_check.py: fails on main (unsupported x87 operation ... fnstenv), passes with the change.
  • check_rejections() of x87_status.py, x87_fldcw.py, x87_bcd.py, x87_parity.py, x87_examine.py: pass.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594122787 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:14:06Z. --- Part of #870 What this unblocks: x87 code that saves the FPU environment (`fnstenv`, `fnsave`) right after `fninit` now translates in the checked profile instead of being refused as environment access. Summary: - `tools/x87_lower.py`: accept `fnstenv` (28 bytes) and `fnsave` (108 bytes) in the 32-bit protected-mode layout. They follow the existing `fnstsw m16` rule: only where the whole status word is known, which is right after `fninit`. There the stack is empty, the status word is 0, the tag word is 0xFFFF and the pointers are zero, so the stored words are exact. `fnsave` stores ST(0) to ST(7) from the model's slots (the SDM does not define an empty register's bytes), then initializes as `fninit` does, so the function must restore control word 0x027F. Only the checked entry accepts `fnsave`. - `fstenv` and `fsave` (WAIT plus the store) are still refused, with the unknown bits named: the existing rule says WAIT leaves C0 to C3 undefined (`cpu_aot_x87_status` tests this for `fstsw`), so the stored word is not known. `finit` then `fnstenv` is accepted. I did not change that rule. - The 16-bit operand-size forms are refused. - `tests/cpu/x87_environment_check.py`, registered as `cpu_x87_environment_controls` in `CMakeLists.txt`: pure Python. It compares the emitted stores with the SDM layout built in the test, and checks the refusals. - `docs/experiments.md` and the module docstring describe the rule. Open question: if the project wants `fstenv`/`fsave` accepted, the WAIT rule has to change for `fstsw m16` too (its existing rejection case in `tests/cpu/x87_status.py` would flip). That is a separate decision. Verification: - Not run on Win2025: the change is Python-only and there is no native fixture for these forms; the check compares against the SDM layout, not the CPU. - `python3 -B tests/cpu/x87_environment_check.py`: fails on main (`unsupported x87 operation ... fnstenv`), passes with the change. - `check_rejections()` of `x87_status.py`, `x87_fldcw.py`, `x87_bcd.py`, `x87_parity.py`, `x87_examine.py`: pass. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-25 01:01:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:21:46Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f0a5fb87d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049609771 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6049609771). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:21:46Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `f0a5fb87d7` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". 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". </details>
chatgpt-codex-connector[bot commented 2026-11-25 01:02:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:14:16Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T00:21:49.109635Z f0a5fb8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148295861 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7148295861). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:14:16Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"f0a5fb87d75b08ba013fab69b6209d9164975034","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-25T00:21:49.109635Z">2026-11-25T00:21:49.109635Z</relative-time> | `f0a5fb8` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
R0Y00 commented 2026-11-25 01:02:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T00:52:18Z.


Independent Windows confirmation of the existing review finding: on Core i9-14900HX, a source-built x64 MASM procedure saved the host FPU state, ran FNINIT plus FNSTENV or FNSAVE, then restored the host state. Both wrote 0xffff at environment halfword offsets 2, 6, 10 and 26; both retained guard bytes outside their respective 28- and 108-byte destinations. The full-layout zero expectation in the Python fixture therefore disagrees on this host too. Please pair the representation change with an actual native/AOT full-environment comparison and retain the existing non-empty-stack refusals. A second CPU family would help establish the supported profile for those reserved words.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148975597 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7148975597). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T00:52:18Z. --- Independent Windows confirmation of the existing review finding: on Core i9-14900HX, a source-built x64 MASM procedure saved the host FPU state, ran FNINIT plus FNSTENV or FNSAVE, then restored the host state. Both wrote 0xffff at environment halfword offsets 2, 6, 10 and 26; both retained guard bytes outside their respective 28- and 108-byte destinations. The full-layout zero expectation in the Python fixture therefore disagrees on this host too. Please pair the representation change with an actual native/AOT full-environment comparison and retain the existing non-empty-stack refusals. A second CPU family would help establish the supported profile for those reserved words.
chatgpt-codex-connector[bot commented 2026-11-25 01:02:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:21:46Z.

Code review location: tools/x87_lower.py; line 665

@@ -637,6 +656,21 @@ def check_flags(body, exits):
                                  f"{i.ip:#x} -> {target:#x}")
 
 
+def _environment(i, address, checked):
+    """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty)
+    and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are
+    the slots 7 down to 0, and initializes as FNINIT does."""
+    word = "0x027f" if checked is None else "x87_cw"
+    stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"]
+    stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)]

P2 Badge Emit the native reserved halfwords in x87 environment saves

When an accepted function reads back the complete saved environment, these dword stores expose different bytes from native execution. On the inspected Intel Xeon Platinum 8370C/Linux x86-64 host, both fninit; fnstenv and fninit; fnsave write 0xffff at halfword offsets 2, 6, 10 and 26, whereas the emitted stores write zero. Consequently, reading the first dword after saving the environment returns 0x0000037f instead of 0xffff037f. The new environment() oracle repeats the zero-padding assumption and marks all 28 bytes as defined, missing all eight differing bytes. Match the native representation, or reject observations of unmodeled reserved fields rather than treating every stored byte as exact.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688461937 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688461937). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:21:46Z. **Code review location:** `tools/x87_lower.py`; line 665 ~~~~diff @@ -637,6 +656,21 @@ def check_flags(body, exits): f"{i.ip:#x} -> {target:#x}") +def _environment(i, address, checked): + """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty) + and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are + the slots 7 down to 0, and initializes as FNINIT does.""" + word = "0x027f" if checked is None else "x87_cw" + stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"] + stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)] ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Emit the native reserved halfwords in x87 environment saves** When an accepted function reads back the complete saved environment, these dword stores expose different bytes from native execution. On the inspected Intel Xeon Platinum 8370C/Linux x86-64 host, both `fninit; fnstenv` and `fninit; fnsave` write `0xffff` at halfword offsets 2, 6, 10 and 26, whereas the emitted stores write zero. Consequently, reading the first dword after saving the environment returns `0x0000037f` instead of `0xffff037f`. The new `environment()` oracle repeats the zero-padding assumption and marks all 28 bytes as defined, missing all eight differing bytes. Match the native representation, or reject observations of unmodeled reserved fields rather than treating every stored byte as exact. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:21:46Z.

Code review location: tools/x87_lower.py; line 252

@@ -233,6 +245,12 @@ def _effect(i, depth, checked=None):
         return depth
     if op == M.FNINIT and checked is not None:  # every register becomes empty
         return 0
+    if op == M.FNSTENV or (op == M.FNSAVE and checked is not None):
+        if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
+            raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
+        if depth:  # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
+            raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")

P2 Badge Update existing FNSTENV refusal checks with the new diagnostic

The existing FNSTENV cases in tests/cpu/x87_sub.py, tests/cpu/x87_moves.py, tests/cpu/x87_constants.py and tests/cpu/x87_rndint.py execute the store with a non-empty stack and require a rejection containing unsupported x87 operation. This branch now raises stores the tag word of a non-empty x87 stack, so all four registered CPU tests fail in check_rejections() even if their native/translated execution comparisons succeed. Update those expectations alongside the new diagnostic while retaining the negative cases.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688461943 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688461943). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:21:46Z. **Code review location:** `tools/x87_lower.py`; line 252 ~~~~diff @@ -233,6 +245,12 @@ def _effect(i, depth, checked=None): return depth if op == M.FNINIT and checked is not None: # every register becomes empty return 0 + if op == M.FNSTENV or (op == M.FNSAVE and checked is not None): + if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]: + raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}") + if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags) + raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack") ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Update existing FNSTENV refusal checks with the new diagnostic** The existing FNSTENV cases in `tests/cpu/x87_sub.py`, `tests/cpu/x87_moves.py`, `tests/cpu/x87_constants.py` and `tests/cpu/x87_rndint.py` execute the store with a non-empty stack and require a rejection containing `unsupported x87 operation`. This branch now raises `stores the tag word of a non-empty x87 stack`, so all four registered CPU tests fail in `check_rejections()` even if their native/translated execution comparisons succeed. Update those expectations alongside the new diagnostic while retaining the negative cases. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-25 02:01:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:05:01Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049973231 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6049973231). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:05:01Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:01:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:05:03Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049973468 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6049973468). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:05:03Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:02:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:36:42Z.


@R0Y00 Update for commit 3ee66b25:

  • The reserved halfwords at offsets 2, 6, 10 and 26 of the FNSTENV image now read 0xffff.
  • The four FNSTENV refusal checks in tests/cpu/x87_environment_check.py were updated to match.

Verification on Windows Server 2025 (build 26100, MSVC 14.44.35207), head 3ee66b25a6fbc5b0e97426de91d394a98dfece39, run through the repo Win2025 pipeline with focused targets (cpu_x87_harness plus the sub, moves, constants and rndint fixtures and their variants):

  • Debug (--preset dev): ctest -R 'cpu_(aot_x87_(sub|moves|constants|rndint)|x87_environment_controls)' gave 5/5 passed (7.39s).
  • Release /O2 (--preset release): same regex, 5/5 passed (5.93s).
  • Python check tests/cpu/x87_environment_check.py: ok (0.5s).

Limit: I have not run a second CPU family or a native full-environment comparison locally. The 0xffff values come from the documented FNSTENV layout and the current checks, not from a cross-CPU comparison.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7149748714 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#issuecomment-7149748714). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:36:42Z. --- @R0Y00 Update for commit 3ee66b25: - The reserved halfwords at offsets 2, 6, 10 and 26 of the FNSTENV image now read `0xffff`. - The four FNSTENV refusal checks in `tests/cpu/x87_environment_check.py` were updated to match. Verification on Windows Server 2025 (build 26100, MSVC 14.44.35207), head `3ee66b25a6fbc5b0e97426de91d394a98dfece39`, run through the repo Win2025 pipeline with focused targets (`cpu_x87_harness` plus the `sub`, `moves`, `constants` and `rndint` fixtures and their variants): - Debug (`--preset dev`): `ctest -R 'cpu_(aot_x87_(sub|moves|constants|rndint)|x87_environment_controls)'` gave 5/5 passed (7.39s). - Release `/O2` (`--preset release`): same regex, 5/5 passed (5.93s). - Python check `tests/cpu/x87_environment_check.py`: ok (0.5s). Limit: I have not run a second CPU family or a native full-environment comparison locally. The `0xffff` values come from the documented FNSTENV layout and the current checks, not from a cross-CPU comparison. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 02:02:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:05:01Z.

Code review location: tools/x87_lower.py; line 665

@@ -637,6 +656,21 @@ def check_flags(body, exits):
                                  f"{i.ip:#x} -> {target:#x}")
 
 
+def _environment(i, address, checked):
+    """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty)
+    and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are
+    the slots 7 down to 0, and initializes as FNINIT does."""
+    word = "0x027f" if checked is None else "x87_cw"
+    stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"]
+    stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)]

Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic.

AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688782662 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688782662). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:05:01Z. **Code review location:** `tools/x87_lower.py`; line 665 ~~~~diff @@ -637,6 +656,21 @@ def check_flags(body, exits): f"{i.ip:#x} -> {target:#x}") +def _environment(i, address, checked): + """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty) + and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are + the slots 7 down to 0, and initializes as FNINIT does.""" + word = "0x027f" if checked is None else "x87_cw" + stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"] + stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)] ~~~~ --- Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic. AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.
tedin7 commented 2026-11-25 02:02:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:05:03Z.

Code review location: tools/x87_lower.py; line 252

@@ -233,6 +245,12 @@ def _effect(i, depth, checked=None):
         return depth
     if op == M.FNINIT and checked is not None:  # every register becomes empty
         return 0
+    if op == M.FNSTENV or (op == M.FNSAVE and checked is not None):
+        if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
+            raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
+        if depth:  # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
+            raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")

Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic.

AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688782874 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4688782874). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:05:03Z. **Code review location:** `tools/x87_lower.py`; line 252 ~~~~diff @@ -233,6 +245,12 @@ def _effect(i, depth, checked=None): return depth if op == M.FNINIT and checked is not None: # every register becomes empty return 0 + if op == M.FNSTENV or (op == M.FNSAVE and checked is not None): + if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]: + raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}") + if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags) + raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack") ~~~~ --- Fixed in 65c11183 (rebased on current main, pending push to this branch). The environment store now writes 0xffff in the reserved halfwords at 2, 6, 10 and 26, matching a native fninit; fnstenv and fninit; fnsave on x86-64 Linux (dwords ffff037f ffff0000 ffffffff 0 0 0 ffff0000), and the oracle in tests/cpu/x87_environment_check.py uses the same bytes. The FNSTENV refusal checks in x87_sub, x87_moves, x87_constants and x87_rndint expect the new diagnostic. AI-assisted; commands listed were executed locally: python3 -B tests/cpu/x87_environment_check.py, check_rejections() of x87_sub, x87_moves, x87_rndint and x87_constants. The full x87 CPU runs need Windows and were not run.
tedin7 commented 2026-11-25 11:02:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:02:06Z.

Code review location: tools/x87_lower.py; line 665

@@ -637,6 +656,21 @@ def check_flags(body, exits):
                                  f"{i.ip:#x} -> {target:#x}")
 
 
+def _environment(i, address, checked):
+    """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty)
+    and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are
+    the slots 7 down to 0, and initializes as FNINIT does."""
+    word = "0x027f" if checked is None else "x87_cw"
+    stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"]
+    stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)]

Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4693924690 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4693924690). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:02:06Z. **Code review location:** `tools/x87_lower.py`; line 665 ~~~~diff @@ -637,6 +656,21 @@ def check_flags(body, exits): f"{i.ip:#x} -> {target:#x}") +def _environment(i, address, checked): + """FNSTENV and FNSAVE right after FNINIT: control word, status word 0, tag word 0xFFFF (every register empty) + and the instruction and data pointers and opcode, which FNINIT clears. FNSAVE adds ST(0) to ST(7), which are + the slots 7 down to 0, and initializes as FNINIT does.""" + word = "0x027f" if checked is None else "x87_cw" + stores = [f"store<uint32_t>(a, {word});", "store<uint32_t>(a + 4, 0);", "store<uint32_t>(a + 8, 0xffff);"] + stores += [f"store<uint32_t>(a + {offset}, 0);" for offset in (12, 16, 20, 24)] ~~~~ --- Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 11:02:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:02:09Z.

Code review location: tools/x87_lower.py; line 252

@@ -233,6 +245,12 @@ def _effect(i, depth, checked=None):
         return depth
     if op == M.FNINIT and checked is not None:  # every register becomes empty
         return 0
+    if op == M.FNSTENV or (op == M.FNSAVE and checked is not None):
+        if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]:
+            raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}")
+        if depth:  # the status word is known only after FNINIT, which leaves the stack empty (check_flags)
+            raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack")

Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4693925282 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#discussion_r4693925282). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:02:09Z. **Code review location:** `tools/x87_lower.py`; line 252 ~~~~diff @@ -233,6 +245,12 @@ def _effect(i, depth, checked=None): return depth if op == M.FNINIT and checked is not None: # every register becomes empty return 0 + if op == M.FNSTENV or (op == M.FNSAVE and checked is not None): + if i.op0_kind != K.MEMORY or i.memory_size != ENVIRONMENT[op]: + raise ValueError(f"unsupported x87 operand at {i.ip:#x}: {i}") + if depth: # the status word is known only after FNINIT, which leaves the stack empty (check_flags) + raise ValueError(f"{i} at {i.ip:#x} stores the tag word of a non-empty x87 stack") ~~~~ --- Re-checked on current head 3ee66b25: x87 environment saves write native 0xffff reserved halfwords and the FNSTENV refusal text and checks are updated (3ee66b25). Resolving. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 12:01:33 +00:00
tedin7 commented 2026-11-25 12:01:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:02:06Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6056376174 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6056376174). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:02:06Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 12:01:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:02:09Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6056377001 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2098#pullrequestreview-6056377001). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:02:09Z. **Review:** COMMENTED --- _No description provided._
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!2091
No description provided.