fix(libkernel): move the macOS main stack out of the guest arena #12126
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12126"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
Aps5StartGuestmoves the main thread onto a 16 MiB stack at0x600000000000when the old stack overlaps a host region of the arena. It first copiesargv,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.GuestArenaOverlapsHostRegion_nid_no_patchandGuestArenaReleaseHostRegion_nid_no_patchfor POSIX hosts.docs/dev/debt/libkernel.mdrecords 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 thatargvand the environment are intact.Tested
mainf759e165d9.libkerneldoes not build on macOS frommainalone (link.h,RUSAGE_THREAD,sched_getcpu,REG_*inRtld.cpp,Process.cpp,Exception.cpp), and #7631, one of the open macOS PRs that fix it, conflicts withmain. So I builtlibcfrom this branch and linkedMainThreadMacOs.cppandGuestMainStackMove.cppby hand against it.main'sMainThreadMacOs.cpp: exit code 1 in 3 of 3 runs, atthe main thread stack is outside the guest arena.python3 tools/check_conventions.py --base origin/mainpasses.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'sMainThreadMacOs.cpp, which could not be linked into a full build (see above). It shows the abort and that moving the stack removes it.libkerneldoes 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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 taggedVM_MEMORY_STACK, andServeMainQueue(: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 withpthread_get_stackaddr_np/pthread_get_stacksize_npof the main thread, intersected with the region found, so only the main stack is released. Extending the test to put a secondVM_MEMORY_STACKregion 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.