feat(libkernel): direct memory mappings on macOS hosts #7631
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7631"
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 mugurc on 2026-10-18T18:13:16Z.
What
Maps guest memory on macOS hosts. Direct memory, flexible memory and the mmap family used Linux-only calls (
memfd_create,MAP_FIXED_NOREPLACE, a hintedmmapthat searches upward), and placed mappings in the guest arena only on Linux. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).mach_make_memory_entry_64withMAP_MEM_NAMED_CREATE), mapped withmach_vm_map. macOS has nomemfd, and a POSIX shared memory object does not work for guest code: anmprotectthat adds execute to its mapping fails withEACCES, and code mapped executable from it does not run under Rosetta (SIGBUS). The entry is a Mach port, so a pool holds no file descriptor.ArenaAvailable,ArenaCommit,ArenaForget, and theGuestArenaAllocateFromHintandGuestArenaAllocatebranches ofMapPlaced). Without them a mapping with no address came back from a plainmmapoutside the arena: @DavidSamir found that ccleste (PPSA59322) needs its 16 MiB ring fromsceKernelMapDirectMemoryinside 0x2_0000_0000-0x3_0000_0000 and stops before device creation otherwise (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6207674678), and that ten radv-based titles report no Vulkan device for the same reason (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6254181383).mach_vm_mapon macOS: withVM_FLAGS_ANYWHERE, whose search starts at the hint, for a mapping above a hint, and withoutVM_FLAGS_OVERWRITE, which refuses an occupied range, for a fixed mapping that must not replace another.mprotectfinds the guest module that holds a range through the emulateddl_iterate_phdrof macOS, as Linux finds it through the host one. The mappings are registered with the write watch (#3703) and the physical span index, as on Linux.mprotectfails withENOMEM, stays Linux-only (MapUnmappedPagesis): macOS maps the whole stack of a thread. Under Rosetta, the main thread's stack at 8 MiB and at the 64 MiB limit and pthread stacks of 512 KiB and 64 MiB have no unmapped page, andmprotectover each whole stack succeeds.ENOTSUPon macOS. #6542 gave the Windows implementation the branch that every host other than Linux took; it now has its own, and the others keep the refusal.CheckGuestModuleUsesFlexibleMemoryis skipped on macOS: its module is a host-built library, which macOS's emulateddl_iterate_phdr(it lists relinked images only) does not show.Tested
macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2.
main7e2ba73e4with the rest of the macOS series (ci/macos-series-2on my fork, which now includes #7937) and the libSceSsl change of #7332. The libraries do not link on macOS without the series.main7e2ba73e4. The one conflict was the Windows branch of~PhysicalBacking, which #8857 gave a section pool; the macOS branch after it is unchanged.guest_memorywith the two checks this PR adds,guest_mmap_fixed_anonymous,guest_amm_placement,guest_kernel_stack_protectandguest_jit_shared_memorypass, and so doesguest_shm_opennow that the series carries #7937.ninja,ninja libsandctest -j4: 1798 tests, 1123 passed, 25 failed, the rest skipped (no Vulkan loader in the environment). Of the memory tests among them,guest_memory_lifetime,guest_flexible_memory_declaredandguest_kernel_app_archive_mmapfailed the same way before this rebase. The others failed before as well, or are new since my last run onmain3fd1cc393: tests and checksmainadded since then, andagc_driver_graphics, which now times out. I have not looked into the new ones yet.Earlier runs of this pull request:
main218aa8f00, with #6124's arena reserved and the mapping paths of this PR left Linux-only,guest_memory(CheckReservationHintWithoutRoomAbove: a reservation hinted above the arena is not placed inside it),guest_mmap_fixed_anonymous(CheckPosixFixedAnonymousMapping: a fixedmmapinto a reservation fails) andguest_amm_placementfail. With them all three pass, andguest_memoryruns every arena check on macOS.guest_ampr_markers,guest_ampr_threads,guest_arena_available,guest_arena_unmappable,guest_arena_host,guest_arena_commit,guest_mlockandguest_mprotect_unmappedpass either way.guest_memorypass: direct memory mapped read-write, filled with code and made executable withsceKernelMprotectruns it, and 64 rounds of allocating, mapping and releasing direct memory leave no host descriptor open. With ashm_openbacking the first fails withEACCESand the second leaks one descriptor a round.sceKernelMapDirectMemorywithout an address, as ccleste's ring (onmain218aa8f00): in six launches it was placed at 0x2_0000_0000 five times and at 0x2_00CB_0000 once, inside the window ccleste needs.mainc1ca83479,libkerneldoes not compile on macOS with the earlier version of this PR (MapUnmappedPagesis undeclared, and the Windows file mapping code of #6542 is compiled); with this one it does, andguest_kernel_stack_protectpasses.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)Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-24T16:16:12Z.
Review: APPROVED
Tested head
c28643f3e5on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2.Alone on
main3fcb5e4d76:DirectMemory/DirectMemory.cppnow compiles. The other 19 steps that fail onmainon macOS still fail, and there are no new failures.With the other open macOS build PRs (#10562, #10641, #10366, #10419, #7804, #10552, #4630, #10659, #10660; I resolved the conflicts of #10552 and #4630 with
mainlocally),libkernellinks and the guest tests run:guest_mmap_fixed_anonymous,guest_amm_placement,guest_jit_shared_memory,guest_shm_open,guest_arena_available,guest_mprotect_unmapped,guest_memory_lifetimeandguest_flexible_memory_declaredpass.guest_memoryruns the two checks this PR adds without a failure. It then aborts later, inCheckMlock(GuestMemory.cpp:1695):sceKernelMlockon asceKernelReserveVirtualRangerange does not returnENOMEM.guest_mlock,guest_system_configuration,guest_kernel_is_stackandguest_credentialsabort too. Inguest_system_configuration,kern.proc.vmmapleaveserrnoat 2 becausesceKernelVirtualQueryfalls back to/proc/self/maps.All five pass once your
f66a4df741(feat(libkernel): guest memory protection and VirtualQuery on macOS hosts, branchfeat/libkernel-memory-protection-macos) is added on top; it applies cleanly to this stack. Apart from #7201, I couldn't find an open PR that carries it. Do you plan to open it? It seems to be the part of the series these tests still need on macOS.guest_kernel_stack_protectstill aborts with it: the child re-executes/proc/self/exe(GuestKernelStackProtect.cpp:529), which macOS doesn't have.AI-assisted: yes
Pull request closed