fix(libkernel): accept shared anonymous, MAP_ALIGNED and hint flags in mmap, as FreeBSD 11.4 does #27303

Merged
kiansaeidnia merged 1 commit from upstream-pr/27303 into main 2026-11-29 12:30:32 +00:00
kiansaeidnia commented 2026-11-25 19:14:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kiansaeidnia on 2026-11-25T19:14:38Z.


What

mmap failed with EOPNOTSUPP for several flags that FreeBSD 11.4 kern_mmap_fpcheck and vm_mmap_object accept. It now handles them as FreeBSD does:

  • MAP_SHARED | MAP_ANON, with or without MAP_FIXED, maps zero-filled memory through the private anonymous path. fd -1 and offset 0 are still required.
  • MAP_ALIGNED(n) without MAP_FIXED places the map at a 2^n-aligned address, for anonymous and file maps. It fails with EINVAL for n below the 16 KiB page shift or from 64. From n = 47 no user address is that aligned, so it fails with ENOMEM. With MAP_FIXED the alignment is ignored.
  • MAP_HASSEMAPHORE, MAP_NOSYNC, MAP_NOCORE and MAP_PREFAULT_READ are accepted.

A hint inside a sceKernelReserveVirtualRange reservation is now rounded up to the requested alignment before it is placed there, because vm_map_find searches upward from the hint. This applies to mmap, sceKernelMapDirectMemory and sceKernelMapDirectMemory2. MAP_ALIGNED_SUPER aligns only maps of 2 MiB or more to 2 MiB, as pmap_align_superpage does, so a smaller superpage map keeps its hint. The caller's out address is now written only after the map succeeds.

Other flags still fail with EOPNOTSUPP and are recorded under Silent stubs. FreeBSD drops 0x20 and 0x40 and returns EINVAL for its other unknown bits. A new Unknown function info entry records that this behaviour is not measured on a console, and the reservation-hint entry now describes the rounding.

Tested

Windows 11, MinGW-w64 GCC 16.2, Release: guest_memory passes. The tests in GuestMemory.cpp cover:

  • CheckPosixMappingFlags:
    • Shared anonymous maps, including a fixed replace and MAP_FIXED | MAP_EXCL, and their EINVAL cases (fd, offset, alignment, protection).
    • The four hint flags on shared and private maps.
    • MAP_ALIGNED(n) for n = 14, 22 and 30 placed 2^n-aligned; n = 2, 13, 64 and 0xFF giving EINVAL; n = 47, 50 and 63 giving ENOMEM.
    • The alignment ignored under MAP_FIXED.
    • Aligned hints inside and outside a reservation, and the same on the shared-memory file path.
  • CheckAlignedHintInsideReservation: sceKernelMapDirectMemory with a hint at reservation + 16 KiB and 4 MiB alignment lands at reservation + 4 MiB. A hint whose rounded range leaves the reservation stays a plain hint (sceKernelMapDirectMemory2). Flexible, unaligned and top-of-space hints are also checked.
  • CheckSuperpageHintInsideReservation: a 16 KiB MAP_ALIGNED_SUPER map hinted inside a reservation lands at its hint, and a 2 MiB one lands at the next 2 MiB boundary.

PosixMemory.cpp, DirectMemory.cpp and the test were syntax-checked with g++ 11 on Linux (WSL Ubuntu); the Linux and macOS paths have not been run here.

The overlap check reports #24034 (remap alias recheck, a different change) on adjacent lines of DirectMemory.cpp; whichever merges second rebases them.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5497568660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27303). Originally posted by [kiansaeidnia](https://github.com/kiansaeidnia) on 2026-11-25T19:14:38Z. --- ### What `mmap` failed with `EOPNOTSUPP` for several flags that FreeBSD 11.4 `kern_mmap_fpcheck` and `vm_mmap_object` accept. It now handles them as FreeBSD does: - **`MAP_SHARED | MAP_ANON`**, with or without `MAP_FIXED`, maps zero-filled memory through the private anonymous path. `fd` -1 and offset 0 are still required. - **`MAP_ALIGNED(n)`** without `MAP_FIXED` places the map at a 2^n-aligned address, for anonymous and file maps. It fails with `EINVAL` for n below the 16 KiB page shift or from 64. From n = 47 no user address is that aligned, so it fails with `ENOMEM`. With `MAP_FIXED` the alignment is ignored. - **`MAP_HASSEMAPHORE`, `MAP_NOSYNC`, `MAP_NOCORE` and `MAP_PREFAULT_READ`** are accepted. A hint inside a `sceKernelReserveVirtualRange` reservation is now rounded up to the requested alignment before it is placed there, because `vm_map_find` searches upward from the hint. This applies to `mmap`, `sceKernelMapDirectMemory` and `sceKernelMapDirectMemory2`. `MAP_ALIGNED_SUPER` aligns only maps of 2 MiB or more to 2 MiB, as `pmap_align_superpage` does, so a smaller superpage map keeps its hint. The caller's out address is now written only after the map succeeds. Other flags still fail with `EOPNOTSUPP` and are recorded under Silent stubs. FreeBSD drops 0x20 and 0x40 and returns `EINVAL` for its other unknown bits. A new Unknown function info entry records that this behaviour is not measured on a console, and the reservation-hint entry now describes the rounding. ### Tested Windows 11, MinGW-w64 GCC 16.2, Release: `guest_memory` passes. The tests in `GuestMemory.cpp` cover: - **`CheckPosixMappingFlags`**: - Shared anonymous maps, including a fixed replace and `MAP_FIXED | MAP_EXCL`, and their `EINVAL` cases (fd, offset, alignment, protection). - The four hint flags on shared and private maps. - `MAP_ALIGNED(n)` for n = 14, 22 and 30 placed 2^n-aligned; n = 2, 13, 64 and 0xFF giving `EINVAL`; n = 47, 50 and 63 giving `ENOMEM`. - The alignment ignored under `MAP_FIXED`. - Aligned hints inside and outside a reservation, and the same on the shared-memory file path. - **`CheckAlignedHintInsideReservation`**: `sceKernelMapDirectMemory` with a hint at reservation + 16 KiB and 4 MiB alignment lands at reservation + 4 MiB. A hint whose rounded range leaves the reservation stays a plain hint (`sceKernelMapDirectMemory2`). Flexible, unaligned and top-of-space hints are also checked. - **`CheckSuperpageHintInsideReservation`**: a 16 KiB `MAP_ALIGNED_SUPER` map hinted inside a reservation lands at its hint, and a 2 MiB one lands at the next 2 MiB boundary. `PosixMemory.cpp`, `DirectMemory.cpp` and the test were syntax-checked with g++ 11 on Linux (WSL Ubuntu); the Linux and macOS paths have not been run here. The overlap check reports #24034 (remap alias recheck, a different change) on adjacent lines of `DirectMemory.cpp`; whichever merges second rebases them. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-25 19:34:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T19:34:47Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the prepared diff and all four changed files were read, but the Windows test run described by the author was not independently verified.

What I ran

Reviewed head 2de888d9fe2c1be1fa33a9564f8942eb17810515 using the prepared review artifacts and the pinned repository reader. No commands, builds, or tests were run; no merged preview was checked.

Results

Check Result Note
Changed source review passed Four changed files read
Build and tests not run Author reports Windows test run
Console behavior not run Description marks behavior unmeasured

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported Windows 11 / MinGW-w64 GCC 16.2 guest_memory run and Linux syntax checks were not independently run. Linux and macOS paths, physical-console behavior, and compatibility with FreeBSD 11.4 were not verified. All changed files listed in the manifest's reviewed set were read: DirectMemory.cpp, PosixMemory.cpp, GuestMemory.cpp, and fix-libkernel-mmap-freebsd-flags.md.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6063806174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27303#pullrequestreview-6063806174). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T19:34:47Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the prepared diff and all four changed files were read, but the Windows test run described by the author was not independently verified. ## What I ran Reviewed head `2de888d9fe2c1be1fa33a9564f8942eb17810515` using the prepared review artifacts and the pinned repository reader. No commands, builds, or tests were run; no merged preview was checked. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | Four changed files read | | Build and tests | not run | Author reports Windows test run | | Console behavior | not run | Description marks behavior unmeasured | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported Windows 11 / MinGW-w64 GCC 16.2 `guest_memory` run and Linux syntax checks were not independently run. Linux and macOS paths, physical-console behavior, and compatibility with FreeBSD 11.4 were not verified. All changed files listed in the manifest's `reviewed` set were read: `DirectMemory.cpp`, `PosixMemory.cpp`, `GuestMemory.cpp`, and `fix-libkernel-mmap-freebsd-flags.md`. ## To get this to a good state Nothing is required.
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!27303
No description provided.