fix(relinker): rewrite XSAVEOPT to XSAVE under --to-rosetta #7918

Merged
Kernel-Hunter merged 1 commit from upstream-pr/7918 into main 2026-10-24 12:15:00 +00:00
Kernel-Hunter commented 2026-10-19 13:59:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kernel-Hunter on 2026-10-19T13:59:08Z.


What

XSAVEOPT raises an illegal instruction under Rosetta. The PS5's Zen 2 has it, so code built for the console can use it without a CPUID check. With --to-rosetta it is now rewritten to XSAVE in place: 0F AE /6 with a memory operand becomes 0F AE /4, keeping the prefixes, REX and operand, the same way RDSEED becomes RDRAND.

XSAVEOPT differs from XSAVE only in the init and modified optimizations (Intel SDM volume 1, chapter 13). They let it skip writing state that is in its initial configuration or unchanged since the last XRSTOR from the same area. XSAVE writes the same XSTATE_BV and the full state, so XRSTOR restores the same registers from either area.

Not taken: CLWB (66 0F AE /6, already a NOP), MFENCE, UMONITOR, UMWAIT, XSAVE, XRSTOR, CLFLUSH. The Intel target and a plain relink are unchanged. XSAVEC also faults under Rosetta but is not converted, because it writes the compacted format. The debt entry says so.

Changes:

  • DecodedInstruction::IsXsaveopt, the XSAVEOPT table entry, and the in-place case in _matchRosetta.
  • Amd64OnlyConverterTests: rewrite checks for the plain, REX.W, RSP-relative, r12-based, RIP-relative and FS-relative forms; the neighbouring 0F AE encodings are not taken; not converted for Intel. New xsaveoptExecution runs the rewritten XSAVEOPT64, clears YMM3, runs XRSTOR and checks YMM3, XSTATE_BV and the standard-format offsets. It runs on x86-64 Linux and macOS hosts and is empty elsewhere.
  • test_to_rosetta.py now includes XSAVEOPT in the relinked fixture.
  • docs/user/USAGE.md (--to-rosetta row) and a new entry in docs/dev/debt/relinker.md.

Tested

Apple M5, macOS 27.0.1 (26A434), Apple clang, relinker-only Release build.

Under Rosetta, from a standalone clang -arch x86_64 program (sysctl.proc_translated = 1) that runs the bytes from executable memory:

48 0F AE 37 (XSAVEOPT64)     signal 4 (Illegal instruction: 4)
48 0F AE 27 (XSAVE64)        executed

Same host: XSAVEC also raises SIGILL. XSAVE64 with RFBM 7 followed by XRSTOR64 restores YMM3, XSTATE_BV reads 7, and the AVX high half is at offset 576. XGETBV reads XCR0 = 7 and CPUID (0xD, 0) gives 832 bytes, but CPUID leaf 1 reports neither XSAVE nor OSXSAVE, and leaf (0xD, 1) reports neither XSAVEOPT nor XSAVEC.

  • Native arm64 build: ctest, 207/207 pass.
  • x86-64 build under Rosetta (-DCMAKE_OSX_ARCHITECTURES=x86_64), where xsaveoptExecution actually runs: 207/207 pass, including amd64_only_converter and to_rosetta.
  • With the rewrite disabled, amd64_only_converter fails with XSAVEOPT was not rewritten to XSAVE.
  • python3 tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested on Linux or Windows. The execution test is also built for x86-64 Linux, so the Linux CI job runs it natively. No title was tested, and I don't know of one that uses XSAVEOPT.

No open PR or issue covers XSAVEOPT or XSAVEC. This branch is rebased on main after #6111.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes. The tests above were run on the hardware listed.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4922121841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7918). Originally posted by [Kernel-Hunter](https://github.com/Kernel-Hunter) on 2026-10-19T13:59:08Z. --- ### What XSAVEOPT raises an illegal instruction under Rosetta. The PS5's Zen 2 has it, so code built for the console can use it without a CPUID check. With `--to-rosetta` it is now rewritten to XSAVE in place: `0F AE /6` with a memory operand becomes `0F AE /4`, keeping the prefixes, REX and operand, the same way RDSEED becomes RDRAND. XSAVEOPT differs from XSAVE only in the init and modified optimizations (Intel SDM volume 1, chapter 13). They let it skip writing state that is in its initial configuration or unchanged since the last XRSTOR from the same area. XSAVE writes the same XSTATE_BV and the full state, so XRSTOR restores the same registers from either area. Not taken: CLWB (`66 0F AE /6`, already a NOP), MFENCE, UMONITOR, UMWAIT, XSAVE, XRSTOR, CLFLUSH. The Intel target and a plain relink are unchanged. XSAVEC also faults under Rosetta but is not converted, because it writes the compacted format. The debt entry says so. Changes: - `DecodedInstruction::IsXsaveopt`, the `XSAVEOPT` table entry, and the in-place case in `_matchRosetta`. - `Amd64OnlyConverterTests`: rewrite checks for the plain, REX.W, RSP-relative, r12-based, RIP-relative and FS-relative forms; the neighbouring `0F AE` encodings are not taken; not converted for Intel. New `xsaveoptExecution` runs the rewritten XSAVEOPT64, clears YMM3, runs XRSTOR and checks YMM3, XSTATE_BV and the standard-format offsets. It runs on x86-64 Linux and macOS hosts and is empty elsewhere. - `test_to_rosetta.py` now includes XSAVEOPT in the relinked fixture. - `docs/user/USAGE.md` (`--to-rosetta` row) and a new entry in `docs/dev/debt/relinker.md`. ### Tested Apple M5, macOS 27.0.1 (26A434), Apple clang, relinker-only Release build. Under Rosetta, from a standalone `clang -arch x86_64` program (`sysctl.proc_translated` = 1) that runs the bytes from executable memory: ``` 48 0F AE 37 (XSAVEOPT64) signal 4 (Illegal instruction: 4) 48 0F AE 27 (XSAVE64) executed ``` Same host: XSAVEC also raises SIGILL. XSAVE64 with RFBM 7 followed by XRSTOR64 restores YMM3, XSTATE_BV reads 7, and the AVX high half is at offset 576. XGETBV reads XCR0 = 7 and CPUID (0xD, 0) gives 832 bytes, but CPUID leaf 1 reports neither XSAVE nor OSXSAVE, and leaf (0xD, 1) reports neither XSAVEOPT nor XSAVEC. - Native arm64 build: `ctest`, 207/207 pass. - x86-64 build under Rosetta (`-DCMAKE_OSX_ARCHITECTURES=x86_64`), where `xsaveoptExecution` actually runs: 207/207 pass, including `amd64_only_converter` and `to_rosetta`. - With the rewrite disabled, `amd64_only_converter` fails with `XSAVEOPT was not rewritten to XSAVE`. - `python3 tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not tested on Linux or Windows. The execution test is also built for x86-64 Linux, so the Linux CI job runs it natively. No title was tested, and I don't know of one that uses XSAVEOPT. No open PR or issue covers XSAVEOPT or XSAVEC. This branch is rebased on main after #6111. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes. The tests above were run on the hardware listed.
blawesom commented 2026-10-20 19:05:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-20T19:05:57Z.


Rebase handoff — rebased onto current main (a9cfea22f, clean, authorship unchanged).

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; 1717/1720 — green apart from the three known current-main failures on this stack (#7784 guest-memory, tessellation pending Mesa 26.2.6, and image_array_window_past_layer_limit — newly-arrived red flagged on #6772), none from this PR; conventions pass. The Rosetta XSAVE rewrite itself is macOS-gated, not exercised here.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7918
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is baafb462a.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298933617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7918#issuecomment-6298933617). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-20T19:05:57Z. --- **Rebase handoff** — rebased onto current `main` (`a9cfea22f`, clean, authorship unchanged). Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; **1717/1720 — green apart from the three known current-`main` failures on this stack** (#7784 guest-memory, tessellation pending Mesa 26.2.6, and `image_array_window_past_layer_limit` — newly-arrived red flagged on #6772), none from this PR; conventions pass. The Rosetta XSAVE rewrite itself is macOS-gated, not exercised here. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7918 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `baafb462a`.
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
boykopovar/AnyPS5!7918
No description provided.