fix(libkernel): fall back below a mapping hint with no room above it #5218

Merged
YossiYad merged 3 commits from upstream-pr/5218 into main 2026-10-14 19:05:24 +00:00
YossiYad commented 2026-10-13 14:54:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-13T14:54:28Z.


What

A mapping without MAP_FIXED treated its address as the lowest place it could go, and a hint at or above the end of the guest arena threw on the host (mapping address hint is above the guest address space arena). FreeBSD 11.4's vm_mmap_object places such mappings with vm_map_find_min, which takes the address as a hint only: it tries at or above it, then again from its minimum address.

  • GuestArenaAllocateFromHint does the same in the arena: at or above the hint when the hint lies below the arena's end and a range fits there, otherwise from the arena's start. MapPlaced (Linux) and mmap_aligned (Windows) use it for hinted mappings. GuestArenaAllocateAtOrAbove keeps its meaning for the callers that need a range above an address (CheckArenaHonoursHintsWithoutGoingBelowThem still passes).
  • guest_memory reserves with a hint above the arena and with a hint on the arena's last page for two pages; both land inside the arena, the second below its hint.
  • The debt entry records that FreeBSD's minimum is the end of the largest data segment rather than the arena's start.

Title. PS5CEMU-HAR (PPSA99360) reserves its 3DS JIT range with sceKernelReserveVirtualRange and a hint near its own code. With the title's earlier stops applied (#4514, #4469, #4467), launching a 3DS homebrew aborted the process with the throw above. With this PR the reservation succeeds and the title reports its own Azahar did not start: can't alloc and returns to its library instead of terminating. The emulator still cannot start, because the relinked executable lies outside the guest arena; that is #5217 and not changed here.

Refs #5217.

Tested

Linux (Bazzite 44, GCC 16.2.1, Ninja, Release, -DBUILD_TESTING=ON), based on main 0f2683cb:

  • Full build and ctest: 1054 of 1059 pass. The five others fail identically on unchanged main here: agc_driver_tessellation (segfault), guest_kernel_stack_protect, guest_module_directories, and guest_ampr_cpp_error / video_out_mouse (not built: the first does not link with this machine's non-static toolchain).
  • guest_memory fails with the previous call in MapPlaced, and with the fallback to the arena's start removed.
  • Title run (distrobox, RX 7700 XT, the three PRs above applied): before, what(): mapping address hint is above the guest address space arena and SIGABRT; after, the title's own error message and a running launcher.
  • tools/check_conventions.py --base upstream/main, git diff --check and tools/pr_overlap.py (no overlap) pass. Windows was not built here.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4845114957 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5218). Originally posted by [YossiYad](https://github.com/YossiYad) on 2026-10-13T14:54:28Z. --- ### What A mapping without `MAP_FIXED` treated its address as the lowest place it could go, and a hint at or above the end of the guest arena threw on the host (`mapping address hint is above the guest address space arena`). FreeBSD 11.4's [`vm_mmap_object`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/vm/vm_mmap.c#L1569) places such mappings with [`vm_map_find_min`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/vm/vm_map.c#L1555-L1584), which takes the address as a hint only: it tries at or above it, then again from its minimum address. - `GuestArenaAllocateFromHint` does the same in the arena: at or above the hint when the hint lies below the arena's end and a range fits there, otherwise from the arena's start. `MapPlaced` (Linux) and `mmap_aligned` (Windows) use it for hinted mappings. `GuestArenaAllocateAtOrAbove` keeps its meaning for the callers that need a range above an address (`CheckArenaHonoursHintsWithoutGoingBelowThem` still passes). - `guest_memory` reserves with a hint above the arena and with a hint on the arena's last page for two pages; both land inside the arena, the second below its hint. - The debt entry records that FreeBSD's minimum is the end of the largest data segment rather than the arena's start. **Title.** PS5CEMU-HAR (PPSA99360) reserves its 3DS JIT range with `sceKernelReserveVirtualRange` and a hint near its own code. With the title's earlier stops applied (#4514, #4469, #4467), launching a 3DS homebrew aborted the process with the throw above. With this PR the reservation succeeds and the title reports its own `Azahar did not start: can't alloc` and returns to its library instead of terminating. The emulator still cannot start, because the relinked executable lies outside the guest arena; that is #5217 and not changed here. Refs #5217. ### Tested Linux (Bazzite 44, GCC 16.2.1, Ninja, Release, `-DBUILD_TESTING=ON`), based on `main` `0f2683cb`: - Full build and `ctest`: 1054 of 1059 pass. The five others fail identically on unchanged `main` here: `agc_driver_tessellation` (segfault), `guest_kernel_stack_protect`, `guest_module_directories`, and `guest_ampr_cpp_error` / `video_out_mouse` (not built: the first does not link with this machine's non-static toolchain). - `guest_memory` fails with the previous call in `MapPlaced`, and with the fallback to the arena's start removed. - Title run (distrobox, RX 7700 XT, the three PRs above applied): before, `what(): mapping address hint is above the guest address space arena` and SIGABRT; after, the title's own error message and a running launcher. - `tools/check_conventions.py --base upstream/main`, `git diff --check` and `tools/pr_overlap.py` (no overlap) pass. Windows was not built here. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
simonbronk commented 2026-10-13 21:19:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-13T21:19:40Z.


Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, full build, this PR (3fbf4fb82) merged onto main 0aa4cb1c2 (AI-assisted: Claude Code). This exercises the Windows call site (mmap_aligned in DirectMemory.cpp), which the Linux run does not reach.

  • With main's DirectMemory.cpp and the rest of the PR kept: guest_memory aborts with what(): mapping address hint is above the guest address space arena (exit 0xc0000409).
  • With this PR: guest_memory passes 10/10 (--repeat until-fail:10), and the full ctest passes 1098/1098.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6155421893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5218#issuecomment-6155421893). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-13T21:19:40Z. --- Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, full build, this PR (`3fbf4fb82`) merged onto `main` `0aa4cb1c2` (AI-assisted: Claude Code). This exercises the Windows call site (`mmap_aligned` in `DirectMemory.cpp`), which the Linux run does not reach. - With `main`'s `DirectMemory.cpp` and the rest of the PR kept: `guest_memory` aborts with `what(): mapping address hint is above the guest address space arena` (exit 0xc0000409). - With this PR: `guest_memory` passes 10/10 (`--repeat until-fail:10`), and the full `ctest` passes 1098/1098.
adriadam10 commented 2026-10-14 18:05:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:05:53Z.


Checked the arena fallback against FreeBSD's vm_map_find_min retry from the minimum address, built and ran the libc/libkernel ctest scope (240 tests, all pass), and confirmed guest_memory fails with the code change reverted. Windows call site covered by @simonbronk's run above; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175001204 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5218#issuecomment-6175001204). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T18:05:53Z. --- Checked the arena fallback against FreeBSD's `vm_map_find_min` retry from the minimum address, built and ran the libc/libkernel ctest scope (240 tests, all pass), and confirmed `guest_memory` fails with the code change reverted. Windows call site covered by @simonbronk's run above; merging.
YossiYad commented 2026-10-14 18:50:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-14T18:50:19Z.


@adriadam10 thanks for checking it against vm_map_find_min, and @simonbronk for the Windows run. The branch still merges into current main (2b7e3ce69) without conflicts, so it can go in as it is.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175835553 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5218#issuecomment-6175835553). Originally posted by [YossiYad](https://github.com/YossiYad) on 2026-10-14T18:50:19Z. --- @adriadam10 thanks for checking it against `vm_map_find_min`, and @simonbronk for the Windows run. The branch still merges into current `main` (`2b7e3ce69`) without conflicts, so it can go in as it is.
adriadam10 commented 2026-10-14 19:05:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T19:05:26Z.


Partly fixed by #5218: mapping hints with no room above them fall back to the arena start (covered by guest_memory); the relinked executable still lies outside the arena.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6176124753 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5218#issuecomment-6176124753). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T19:05:26Z. --- Partly fixed by #5218: mapping hints with no room above them fall back to the arena start (covered by `guest_memory`); the relinked executable still lies outside the arena.
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!5218
No description provided.