fix(relinker): initialize MXCSR in macOS guest entry #7741

Merged
MarcZX10 merged 1 commit from upstream-pr/7741 into main 2026-10-24 10:51:41 +00:00
MarcZX10 commented 2026-10-19 05:26:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T05:26:40Z.


What

The macOS Mach-O LC_MAIN entry stub calls the bound guest-start routine without applying the measured PS5 payload MXCSR to the main guest thread. Save the host MXCSR in aligned stack storage, load guest MXCSR 0x9fc0 immediately before the start call, and restore the host value if the call returns. The guest stack pointer is captured before the temporary slot is allocated. This complements #7594's Linux and Windows main-entry paths and #7565's created-thread path.

Tested

Linux x86_64, GCC 12.2.0, relinker-only Release build. Full CTest: 210/210 passed. The macos_output test checks the emitted setup and restore instructions, the guest entry target, and the bound start slot. python3 tools/check_conventions.py --base upstream/main passed. python3 tools/pr_overlap.py --branch codex/macos-main-thread-mxcsr reported no overlap. No native macOS runtime test was run.

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; follows up on #7548
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4916306045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7741). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T05:26:40Z. --- ### What The macOS Mach-O `LC_MAIN` entry stub calls the bound guest-start routine without applying the measured PS5 payload MXCSR to the main guest thread. Save the host MXCSR in aligned stack storage, load guest MXCSR `0x9fc0` immediately before the start call, and restore the host value if the call returns. The guest stack pointer is captured before the temporary slot is allocated. This complements #7594's Linux and Windows main-entry paths and #7565's created-thread path. ### Tested Linux x86_64, GCC 12.2.0, relinker-only Release build. Full CTest: 210/210 passed. The `macos_output` test checks the emitted setup and restore instructions, the guest entry target, and the bound start slot. `python3 tools/check_conventions.py --base upstream/main` passed. `python3 tools/pr_overlap.py --branch codex/macos-main-thread-mxcsr` reported no overlap. No native macOS runtime test was run. ### Checklist - [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; follows up on #7548 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
jotitadev commented 2026-10-19 05:43:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T05:43:03Z.

Review: COMMENTED


Native macOS run of the new stub, since the description has none. Apple M2 Max, macOS 27.2, Apple clang 21, head b5d55bd80 merged on main 4ffb0cd12.

  • Relinker-only Release, Python 3.9.6: ctest 201/201 on arm64 and on x86_64 under Rosetta 2. On the base relinker, macos_output with the new check fails with entry stub does not save host MXCSR and load guest MXCSR 0x9fc0, and it passes with the fix.

  • Rosetta 2 runtime: an x86_64 probe that runs the exact stub bytes (mov rsi, rsp, sub rsp, 0x10 / stmxcsr / mov dword [rsp+4], 0x9fc0 / ldmxcsr, call [rip+slot], ldmxcsr [rsp] / add rsp, 0x10) around a C callee:

    • the callee reads MXCSR 0x9fc0;
    • DAZ takes effect: a denormal f32 input times 1.0 gives 0, and the host gives 1e-39;
    • FTZ takes effect: 1.5e-38 * 0.5 gives 0;
    • after the call returns, the host MXCSR (0x1f80 plus its sticky flags) is restored.

    So Rosetta 2 accepts the DAZ bit in ldmxcsr without a fault and applies both modes.

Not tested: a full relinked title through LC_MAIN.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5567376433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7741#pullrequestreview-5567376433). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T05:43:03Z. **Review:** COMMENTED --- Native macOS run of the new stub, since the description has none. Apple M2 Max, macOS 27.2, Apple clang 21, head `b5d55bd80` merged on `main` `4ffb0cd12`. - Relinker-only Release, Python 3.9.6: `ctest` 201/201 on arm64 and on x86_64 under Rosetta 2. On the base relinker, `macos_output` with the new check fails with `entry stub does not save host MXCSR and load guest MXCSR 0x9fc0`, and it passes with the fix. - Rosetta 2 runtime: an x86_64 probe that runs the exact stub bytes (`mov rsi, rsp`, `sub rsp, 0x10` / `stmxcsr` / `mov dword [rsp+4], 0x9fc0` / `ldmxcsr`, `call [rip+slot]`, `ldmxcsr [rsp]` / `add rsp, 0x10`) around a C callee: - the callee reads MXCSR `0x9fc0`; - DAZ takes effect: a denormal f32 input times 1.0 gives 0, and the host gives 1e-39; - FTZ takes effect: 1.5e-38 * 0.5 gives 0; - after the call returns, the host MXCSR (`0x1f80` plus its sticky flags) is restored. So Rosetta 2 accepts the DAZ bit in `ldmxcsr` without a fault and applies both modes. Not tested: a full relinked title through `LC_MAIN`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-19 16:52:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T16:52:19Z.


Thanks for the native macOS and Rosetta validation. The relinker-only suite passes 201/201 on both arm64 and x86_64, and the negative control fails with the expected missing guest MXCSR setup. The runtime probe also confirms the stub restores host MXCSR on return. This fills the native macOS evidence gap for the current change; I’ll keep the exact tested head and scope in the PR record.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6272478177 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7741#issuecomment-6272478177). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T16:52:19Z. --- Thanks for the native macOS and Rosetta validation. The relinker-only suite passes 201/201 on both arm64 and x86_64, and the negative control fails with the expected missing guest MXCSR setup. The runtime probe also confirms the stub restores host MXCSR on return. This fills the native macOS evidence gap for the current change; I’ll keep the exact tested head and scope in the PR record.
MarcZX10 commented 2026-10-21 01:22:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T01:22:59Z.


Manual overlap check for #8658 against #7741, using the current GitHub heads (b5d55bd804a9 and fc7d30780fac). The two PR heads merge together without a conflict. In MacOsImage.cpp, #7741 extends the EntryStub instructions and updates EntryStubStartDisplacement; #8658 changes the stubOffset calculation to reserve space after load commands. These hunks are separate and compose cleanly. #8658's current merge conflict with main is in docs/dev/debt/libkernel.md, outside this overlap. I don't see a dependency or code change needed in #7741.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6305670180 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7741#issuecomment-6305670180). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T01:22:59Z. --- Manual overlap check for #8658 against #7741, using the current GitHub heads (`b5d55bd804a9` and `fc7d30780fac`). The two PR heads merge together without a conflict. In `MacOsImage.cpp`, #7741 extends the `EntryStub` instructions and updates `EntryStubStartDisplacement`; #8658 changes the `stubOffset` calculation to reserve space after load commands. These hunks are separate and compose cleanly. #8658's current merge conflict with `main` is in `docs/dev/debt/libkernel.md`, outside this overlap. I don't see a dependency or code change needed in #7741.
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!7741
No description provided.