cpu: lower CMPXCHG8B and CMPXCHG16B without LOCK #628

Manually merged
github merged 3 commits from gh-pr/628/head into gh-pr/628/base 2026-11-26 22:07:03 +00:00
Owner

@⁠danielradosa on GitHub · 2026-11-22 16:21 UTC · original
danielradosa:cpu/cmpxchg8b-16b → main · +276 −0 · 6 files · 3 commits · merged 2026-11-23 05:49 UTC

Change

The shared lowering in tools/ffx_version_query.py now accepts cmpxchg8b and cmpxchg16b with a memory operand. LOCK stays rejected by the prefix guard, as for cmpxchg.

  • Each compares EDX:EAX (RDX:RAX) with the operand. When they are equal it sets ZF and stores ECX:EBX (RCX:RBX), leaving RAX and RDX unchanged. Otherwise it clears ZF and loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit halves for cmpxchg8b. The other flags keep their values.
  • A cmpxchg16b operand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1. INTEGER_HEADER gains raise_cmpxchg16b_alignment, which raises the same exception with the same two parameters.

They add 2 x64 forms to the coverage chart.

Verification

cpu_aot_cmpxchg_wide (tests/cpu/cmpxchg_wide.asm, 2 exports) loads RAX, RDX, RBX and RCX from a block, runs the instruction on an operand in the same block, and stores RAX, RDX, ZF and CF back. It compares the results with the native DLL and a reference model over 120 cases: equal operands, either half differing, register upper halves that cmpxchg8b must ignore on success and clear on failure, and both CF values. It also checks that cmpxchg16b operands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with .savereg unwind data, so the faulting cases still restore the caller's RBX. lock cmpxchg8b and lock cmpxchg16b stay rejected.

Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on main at 9b0df70:

ctest --preset dev --no-tests=error -R '^cpu_aot_cmpxchg_wide$' -V
Build Result
dev and release (/O2) 120 cases matched; offsets 4 and 8 raised exception: access violation reading 0xFFFFFFFFFFFFFFFF in both DLLs; both lock forms rejected
all cpu_ tests, dev 113 of 115 passed; the 2 failures are cpu_native_archive_reader and cpu_native_archive_blast, which need the Zig-built archive_reader.exe (ziglang is not installed on this machine). In one of three parallel (-j 8) runs cpu_native_startup also failed; it passed in the other two and in three runs on its own, and it does not use the shared lowering

Evidence and follow-up

The upper-half behaviour of cmpxchg8b follows the SDM's 32-bit register writes. On success RAX and RDX keep bits 32 to 63; on failure both are zero-extended. The native runs confirm this. A non-locked cmpxchg16b still writes the operand back when the comparison fails, but that write cannot be observed in a single thread, so it is not modelled.

enter and leave would need a stack model that the shared lowering does not have (it has no push or pop), so this PR leaves them out.

Companion PRs, independent of this one: fences and cache hints (#626), and the x87 status-word forms (#627). x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three.

AI-assisted.

**@⁠danielradosa** on GitHub · 2026-11-22 16:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628) `danielradosa:cpu/cmpxchg8b-16b` → `main` · +276 −0 · 6 files · 3 commits · merged 2026-11-23 05:49 UTC ## Change The shared lowering in `tools/ffx_version_query.py` now accepts `cmpxchg8b` and `cmpxchg16b` with a memory operand. `LOCK` stays rejected by the prefix guard, as for `cmpxchg`. - Each compares EDX:EAX (RDX:RAX) with the operand. When they are equal it sets ZF and stores ECX:EBX (RCX:RBX), leaving RAX and RDX unchanged. Otherwise it clears ZF and loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit halves for `cmpxchg8b`. The other flags keep their values. - A `cmpxchg16b` operand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1. `INTEGER_HEADER` gains `raise_cmpxchg16b_alignment`, which raises the same exception with the same two parameters. They add 2 x64 forms to the coverage chart. ## Verification `cpu_aot_cmpxchg_wide` (`tests/cpu/cmpxchg_wide.asm`, 2 exports) loads RAX, RDX, RBX and RCX from a block, runs the instruction on an operand in the same block, and stores RAX, RDX, ZF and CF back. It compares the results with the native DLL and a reference model over 120 cases: equal operands, either half differing, register upper halves that `cmpxchg8b` must ignore on success and clear on failure, and both CF values. It also checks that `cmpxchg16b` operands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with `.savereg` unwind data, so the faulting cases still restore the caller's RBX. `lock cmpxchg8b` and `lock cmpxchg16b` stay rejected. Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on `main` at 9b0df70: ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_cmpxchg_wide$' -V ``` | Build | Result | |---|---| | `dev` and `release` (/O2) | 120 cases matched; offsets 4 and 8 raised `exception: access violation reading 0xFFFFFFFFFFFFFFFF` in both DLLs; both `lock` forms rejected | | all `cpu_` tests, `dev` | 113 of 115 passed; the 2 failures are `cpu_native_archive_reader` and `cpu_native_archive_blast`, which need the Zig-built `archive_reader.exe` (`ziglang` is not installed on this machine). In one of three parallel (`-j 8`) runs `cpu_native_startup` also failed; it passed in the other two and in three runs on its own, and it does not use the shared lowering | ## Evidence and follow-up The upper-half behaviour of `cmpxchg8b` follows the SDM's 32-bit register writes. On success RAX and RDX keep bits 32 to 63; on failure both are zero-extended. The native runs confirm this. A non-locked `cmpxchg16b` still writes the operand back when the comparison fails, but that write cannot be observed in a single thread, so it is not modelled. `enter` and `leave` would need a stack model that the shared lowering does not have (it has no `push` or `pop`), so this PR leaves them out. Companion PRs, independent of this one: fences and cache hints (#626), and the x87 status-word forms (#627). x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three. _AI-assisted._
Each compares EDX:EAX (RDX:RAX) with its memory operand. When they are
equal it sets ZF and stores ECX:EBX (RCX:RBX); otherwise it clears ZF and
loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit
halves of CMPXCHG8B. The other flags keep their values, and LOCK stays
rejected by the prefix guard. A CMPXCHG16B operand that is not 16-byte
aligned raises #GP(0), which Windows reports as an access violation
reading address -1; the translation raises the same exception.

cpu_aot_cmpxchg_wide compares memory, RAX, RDX, ZF and CF with the native
DLL and a reference model over 120 cases, and checks that operands at
offsets 4 and 8 raise the same exception in both DLLs. The fixture saves
RBX in the home area with unwind data, so the fault restores it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:07:01 +00:00
github manually merged commit 40ec7cdbc7 into gh-pr/628/base 2026-11-26 22:07:03 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 16:21 UTC · original · edited 2026-11-22 16:24 UTC

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-22T16:24:25.309883Z 79c93c8 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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 16:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628#issuecomment-7082007793) · edited 2026-11-22 16:24 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"79c93c8d71f0f8670cafb82bf5fabcbdaa7007ff","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-22T16:24:25.309883Z">2026-11-22T16:24:25.309883Z</relative-time> | `79c93c8` | 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>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 16:24 UTC · original

P2 Badge Preserve writeback on a compare mismatch

When a CMPXCHG16B comparison fails, the instruction still writes the original operand back, but this arm only reloads RAX/RDX. On a read-only page, native code therefore raises an access violation while the lifted code returns normally; the CMPXCHG8B mismatch arm has the same gap. Write the original value back on mismatch so protected operands preserve this behavior.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 16:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628#discussion_r4658930763) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve writeback on a compare mismatch** When a CMPXCHG16B comparison fails, the instruction still writes the original operand back, but this arm only reloads RAX/RDX. On a read-only page, native code therefore raises an access violation while the lifted code returns normally; the CMPXCHG8B mismatch arm has the same gap. Write the original value back on mismatch so protected operands preserve this behavior. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-22 18:19 UTC · original

Verification of 79c93c8 on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main.

ctest --preset dev --no-tests=error -R "^cpu_" -j 4
100% tests passed, 0 tests failed out of 115
cpu_aot_cmpxchg_wide: 120 CMPXCHG8B and CMPXCHG16B cases matched the reference in native and translated DLLs

I measured the open Codex finding (writeback on a compare mismatch) on hardware, and it reproduces.

Setup:

  • I used the built cpu_cmpxchg_wide.dll and cpu_cmpxchg_wide_lifted.dll unchanged, one child process per call.
  • The block sits on a read-write page. RDX (the operand offset) is 0x1000, so the 16-byte operand is the first bytes of the next page, set to PAGE_READONLY.
  • "Mismatch" means RDX:RAX differs from the operand. "Equal" means it matches.
Export Case Native Translated
cmpxchg8b_op mismatch access violation writing the operand returns normally, ZF=0, RDX:RAX loaded
cmpxchg16b_op mismatch access violation writing the operand returns normally, ZF=0, RDX:RAX loaded
cmpxchg8b_op equal access violation writing access violation writing
cmpxchg16b_op equal access violation writing access violation writing

The mismatch rows are a real native/translated divergence: the hardware performs the write cycle even when the comparison fails, and the translation does not. Writing the original operand back in the mismatch arm, as the review suggests, would close it. The same read-only-page setup would work as a regression case in tests/cpu/cmpxchg_wide.py, because RDX already selects the operand offset.

AI-assisted: measured and summarized with Claude Code; outputs copied from the local runs.

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-22 18:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628#issuecomment-7083512828) Verification of `79c93c8` on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, `dev` preset. It merges cleanly with `main`. ```text ctest --preset dev --no-tests=error -R "^cpu_" -j 4 100% tests passed, 0 tests failed out of 115 cpu_aot_cmpxchg_wide: 120 CMPXCHG8B and CMPXCHG16B cases matched the reference in native and translated DLLs ``` I measured the open Codex finding (writeback on a compare mismatch) on hardware, and it reproduces. Setup: - I used the built `cpu_cmpxchg_wide.dll` and `cpu_cmpxchg_wide_lifted.dll` unchanged, one child process per call. - The block sits on a read-write page. RDX (the operand offset) is `0x1000`, so the 16-byte operand is the first bytes of the next page, set to `PAGE_READONLY`. - "Mismatch" means RDX:RAX differs from the operand. "Equal" means it matches. | Export | Case | Native | Translated | |---|---|---|---| | `cmpxchg8b_op` | mismatch | access violation **writing** the operand | returns normally, ZF=0, RDX:RAX loaded | | `cmpxchg16b_op` | mismatch | access violation **writing** the operand | returns normally, ZF=0, RDX:RAX loaded | | `cmpxchg8b_op` | equal | access violation writing | access violation writing | | `cmpxchg16b_op` | equal | access violation writing | access violation writing | The mismatch rows are a real native/translated divergence: the hardware performs the write cycle even when the comparison fails, and the translation does not. Writing the original operand back in the mismatch arm, as the review suggests, would close it. The same read-only-page setup would work as a regression case in `tests/cpu/cmpxchg_wide.py`, because RDX already selects the operand offset. AI-assisted: measured and summarized with Claude Code; outputs copied from the local runs.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 05:34 UTC · original

I pushed 40ec7cd so both wide forms preserve the destination write on a compare mismatch, including under /O2. The Windows Release differential passed all 120 value cases, both original alignment and LOCK controls, and the added read-only cases. On each form, both the equal and unequal compare recorded a write access violation at the operand and left the result slots untouched. Waiting for the refreshed CI on this commit.

**@⁠R0Y00** on GitHub · 2026-11-23 05:34 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628#issuecomment-7092519966) I pushed `40ec7cd` so both wide forms preserve the destination write on a compare mismatch, including under `/O2`. The Windows Release differential passed all 120 value cases, both original alignment and LOCK controls, and the added read-only cases. On each form, both the equal and unequal compare recorded a write access violation at the operand and left the result slots untouched. Waiting for the refreshed CI on this commit.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-22 16:24 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79c93c8d71

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

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-22 16:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/628#pullrequestreview-6013251444) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `79c93c8d71` <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>
This discussion has been locked. Commenting is limited to contributors.
No description provided.