fix(libkernel): move the macOS main stack out of the guest arena #12126

Merged
morten-holm merged 2 commits from upstream-pr/12126 into main 2026-10-29 13:51:58 +00:00
morten-holm commented 2026-10-27 22:11:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by morten-holm on 2026-10-27T22:11:58Z.


What

Under Rosetta 2 the main thread stack lies at about 12 GiB, inside the guest arena. A title can map fixed addresses there, and a fixed mapping over the stack aborts with a host region overlap.

  • Aps5StartGuest moves the main thread onto a 16 MiB stack at 0x600000000000 when the old stack overlaps a host region of the arena. It first copies argv, environ, the program name and the initial block off the old stack. Then it frees the old stack and releases its range to the arena.
  • The guest thread keeps the stack size of the original main thread.
  • The arena gets GuestArenaOverlapsHostRegion_nid_no_patch and GuestArenaReleaseHostRegion_nid_no_patch for POSIX hosts.
  • The move is skipped when the main stack lies outside the arena, when the new stack cannot be placed outside the arena, and on hosts that are not x86-64.
  • The guest thread and the other host threads still take their stacks from the system. docs/dev/debt/libkernel.md records this.

Test guest_main_stack_move (macOS only): it starts the guest, then requires that the main thread is outside the arena, that a fixed guest mapping fits where the old stack was, and that argv and the environment are intact.

Tested

  • macOS 27.0.1, Apple M2, x86-64 under Rosetta 2, on main f759e165d9.
  • libkernel does not build on macOS from main alone (link.h, RUSAGE_THREAD, sched_getcpu, REG_* in Rtld.cpp, Process.cpp, Exception.cpp), and #7631, one of the open macOS PRs that fix it, conflicts with main. So I built libc from this branch and linked MainThreadMacOs.cpp and GuestMainStackMove.cpp by hand against it.
    • With this change: exit code 0 in 3 of 3 runs.
    • With main's MainThreadMacOs.cpp: exit code 1 in 3 of 3 runs, at the main thread stack is outside the guest arena.
  • python3 tools/check_conventions.py --base origin/main passes.
  • ASTRO BOT (PPSA21567), Apple M2, macOS 27.0.1, x86-64 under Rosetta 2, MoltenVK, host imports off. With the stack move disabled, the title aborts at launch (exit 134): fixed guest mapping 0x300000000+0x7e000000 overlaps a host region in the guest arena. With the move active, the same title gets past launch and runs draw packets for the 44 s of the run. This title run used a local equivalent of the move in an older tree, not this branch's MainThreadMacOs.cpp, which could not be linked into a full build (see above). It shows the abort and that moving the stack removes it.
  • Not tested: the CMake test target (libkernel does not build here), Linux and Windows (the code is __APPLE__ only, apart from the two arena functions, which are under #ifndef _WIN32), a title run on this base.

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 (#11048 is merged)
  • Fixes: none
  • Unblocks: PPSA21567 #11917
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5041891527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12126). Originally posted by [morten-holm](https://github.com/morten-holm) on 2026-10-27T22:11:58Z. --- ### What Under Rosetta 2 the main thread stack lies at about 12 GiB, inside the guest arena. A title can map fixed addresses there, and a fixed mapping over the stack aborts with a host region overlap. - `Aps5StartGuest` moves the main thread onto a 16 MiB stack at `0x600000000000` when the old stack overlaps a host region of the arena. It first copies `argv`, `environ`, the program name and the initial block off the old stack. Then it frees the old stack and releases its range to the arena. - The guest thread keeps the stack size of the original main thread. - The arena gets `GuestArenaOverlapsHostRegion_nid_no_patch` and `GuestArenaReleaseHostRegion_nid_no_patch` for POSIX hosts. - The move is skipped when the main stack lies outside the arena, when the new stack cannot be placed outside the arena, and on hosts that are not x86-64. - The guest thread and the other host threads still take their stacks from the system. `docs/dev/debt/libkernel.md` records this. Test `guest_main_stack_move` (macOS only): it starts the guest, then requires that the main thread is outside the arena, that a fixed guest mapping fits where the old stack was, and that `argv` and the environment are intact. ### Tested - macOS 27.0.1, Apple M2, x86-64 under Rosetta 2, on `main` `f759e165d9`. - `libkernel` does not build on macOS from `main` alone (`link.h`, `RUSAGE_THREAD`, `sched_getcpu`, `REG_*` in `Rtld.cpp`, `Process.cpp`, `Exception.cpp`), and #7631, one of the open macOS PRs that fix it, conflicts with `main`. So I built `libc` from this branch and linked `MainThreadMacOs.cpp` and `GuestMainStackMove.cpp` by hand against it. - With this change: exit code 0 in 3 of 3 runs. - With `main`'s `MainThreadMacOs.cpp`: exit code 1 in 3 of 3 runs, at `the main thread stack is outside the guest arena`. - `python3 tools/check_conventions.py --base origin/main` passes. - ASTRO BOT (PPSA21567), Apple M2, macOS 27.0.1, x86-64 under Rosetta 2, MoltenVK, host imports off. With the stack move disabled, the title aborts at launch (exit 134): `fixed guest mapping 0x300000000+0x7e000000 overlaps a host region in the guest arena`. With the move active, the same title gets past launch and runs draw packets for the 44 s of the run. This title run used a local equivalent of the move in an older tree, not this branch's `MainThreadMacOs.cpp`, which could not be linked into a full build (see above). It shows the abort and that moving the stack removes it. - Not tested: the CMake test target (`libkernel` does not build here), Linux and Windows (the code is `__APPLE__` only, apart from the two arena functions, which are under `#ifndef _WIN32`), a title run on this base. ### 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 (#11048 is merged) - [x] Fixes: none - [x] Unblocks: PPSA21567 #11917 - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-28 10:53:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T10:53:29Z.

Review: CHANGES_REQUESTED


Thanks, the approach is sound. One change is needed before merge, the one @oneandonlydean already asked for:

  • MainThreadMacOs.cpp:85-95 (FindHostStack) grows the range downward over every adjacent region tagged VM_MEMORY_STACK, and ServeMainQueue (:48-55) deallocates the whole merged range and releases it to the arena. A thread stack mapped right below the main stack has the same tag and would be freed with it. Bound the range with pthread_get_stackaddr_np / pthread_get_stacksize_np of the main thread, intersected with the region found, so only the main stack is released. Extending the test to put a second VM_MEMORY_STACK region right below the main stack would pin it.

Everything else checked out against the diff (argv/environ/initial block copy, skip conditions, debt entry, non-Apple paths untouched). The macOS build and the new test could not be run on our side.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681473264 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12126#pullrequestreview-5681473264). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T10:53:29Z. **Review:** CHANGES_REQUESTED --- Thanks, the approach is sound. One change is needed before merge, the one @oneandonlydean already asked for: - `MainThreadMacOs.cpp:85-95` (`FindHostStack`) grows the range downward over every adjacent region tagged `VM_MEMORY_STACK`, and `ServeMainQueue` (`:48-55`) deallocates the whole merged range and releases it to the arena. A thread stack mapped right below the main stack has the same tag and would be freed with it. Bound the range with `pthread_get_stackaddr_np` / `pthread_get_stacksize_np` of the main thread, intersected with the region found, so only the main stack is released. Extending the test to put a second `VM_MEMORY_STACK` region right below the main stack would pin it. Everything else checked out against the diff (argv/environ/initial block copy, skip conditions, debt entry, non-Apple paths untouched). The macOS build and the new test could not be run on our side.
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!12126
No description provided.