feat(libkernel): implement sceKernelMapNamedSystemFlexibleMemory #11190

Merged
zaryabzip merged 1 commit from upstream-pr/11190 into main 2026-10-26 22:02:22 +00:00
zaryabzip commented 2026-10-25 21:14:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-25T21:14:39Z.


What

Implements sceKernelMapNamedSystemFlexibleMemory in libkernel, beside its twin sceKernelMapNamedFlexibleMemory. ps5-payload-dev/sdk's sce_stubs/libkernel.c and libkernel_sys.c both export it. The Windows import builder falls back from libkernel_sys to libkernel but never the other way, so the throwing libkernel_sys stub and its (void) declaration are removed and the full signature goes in prx/libkernel/include/Api.hpp.

The only sources are two PS4 emulators: fpPS4 (export) and shadPS4. No PS5 measurement exists.

  • The length, flag, protection and alignment checks and the default placement are the twin's (DoMapAnon). Flags other than MAP_FIXED, MAP_NO_OVERWRITE and MAP_NO_COALESCE throw, the same set fpPS4's mask 0xffbfff6f allows. Flag 0x8000 throws, as in sceKernelMapNamedFlexibleMemory (#10305 measured it rejected there on a PS5; the system variant was not probed). fpPS4's 0x880000000 default and both emulators' SDK 1.70 MAP_FIXED-at-null gate are not imported, because the twin has neither.
  • It is not charged to the game flexible budget. There is no separate system budget here. shadPS4 charges only its flexible VMA type (memory.cpp L524). fpPS4 passes MAP_SYSTEM to a virtual manager that ignores it, so it does charge it there (mm_adr_virtual.pas L655-L660, ps4_map_mm.pas L429-L457). A MAP_FIXED map over game flexible memory releases that charge, as direct maps do.
  • A length of 0, or one that is not a multiple of 16 KiB, returns EINVAL first, as in both emulators. A null name, or a name of 32 bytes or more, then throws before anything is mapped. shadPS4 returns EFAULT or ENAMETOOLONG before mapping. fpPS4 maps the range and leaves it unnamed. The twin skips a null name and truncates a long one.
  • sceKernelVirtualQuery reports the mapping as flexible, as fpPS4 does (L1096). shadPS4 reports it as not flexible (memory.cpp L1209).

Unverified on a PS5: the system budget and whether this map is charged to the game budget, the sceKernelVirtualQuery flags of a system map, the null and over-long name results, flag 0x8000 and the other flag and protection bits for this function, default placement, and MAP_FIXED with a null address. All of this is recorded in docs/dev/debt/libkernel.md.

Tested

Windows 11 Home 25H2 (i7-11800H, WinLibs MinGW-w64 GCC 15.2.0 UCRT r7, CMake 4.3.1, Ninja 1.13.2, Release, BUILD_TESTING=ON) at b8ab1b1a5c, and Ubuntu 24.04.5 on WSL2 (GCC 13.3.0, CMake 3.28.3) with the PR merged into main 4e43321bd6 as the Linux CI job does:

  • No warnings in the changed files on either host.
  • ctest -R "guest_system_flexible_memory|guest_memory|guest_flexible_memory|guest_kernel_sys|windows_kernel_variant_sys": 100% tests passed, 0 tests failed out of 31 on Windows and out of 34 on Linux. Those include guest_system_flexible_memory, guest_memory, the five guest_flexible_memory_* tests, the guest_kernel_sys* tests and, on Windows only, windows_kernel_variant_sys.
  • guest_system_flexible_memory with --repeat until-fail:3: 3 of 3 on both hosts.
  • Baseline: on main with only the new test file and its add_test block, the test builds and then aborts with sceKernelMapNamedSystemFlexibleMemory is not exported by libkernel on both hosts (Windows exit 0xc0000409).
  • The earlier head e107d81094 also passed this set on both hosts. On Linux, every library and test target built from a source that includes libkernel/include/Api.hpp or libkernel_sys/include/Api.hpp compiled and linked.

Earlier checks on an arm64 Mac:

  • x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -Wall -Wextra on every changed .cpp reports no diagnostics, the same as on main.
  • tools/check_conventions.py passes.
  • git merge-tree against #11005, #10889, #10898, #9882, #7105, #11042, #10305 and each of the 44 PRs tools/pr_overlap.py could not compare: no changed file has more conflict regions than the same merge with main, where #10898, #7105 and others already conflict. None of their diffs mention sceKernelMapNamedSystemFlexibleMemory.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008783337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11190). Originally posted by [zaryabzip](https://github.com/zaryabzip) on 2026-10-25T21:14:39Z. --- ### What Implements `sceKernelMapNamedSystemFlexibleMemory` in libkernel, beside its twin `sceKernelMapNamedFlexibleMemory`. ps5-payload-dev/sdk's `sce_stubs/libkernel.c` and `libkernel_sys.c` both export it. The Windows import builder falls back from libkernel_sys to libkernel but never the other way, so the throwing libkernel_sys stub and its `(void)` declaration are removed and the full signature goes in `prx/libkernel/include/Api.hpp`. The only sources are two PS4 emulators: [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_map_mm.pas#L1121-L1209) ([export](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_map_mm.pas#L1588-L1599)) and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/30f4170896ce974353a6f6486d0ecec179aec54b/src/core/libraries/kernel/memory.cpp#L354-L391). No PS5 measurement exists. - The length, flag, protection and alignment checks and the default placement are the twin's (`DoMapAnon`). Flags other than `MAP_FIXED`, `MAP_NO_OVERWRITE` and `MAP_NO_COALESCE` throw, the same set fpPS4's mask `0xffbfff6f` allows. Flag 0x8000 throws, as in `sceKernelMapNamedFlexibleMemory` (#10305 measured it rejected there on a PS5; the system variant was not probed). fpPS4's 0x880000000 default and both emulators' SDK 1.70 `MAP_FIXED`-at-null gate are not imported, because the twin has neither. - It is not charged to the game flexible budget. There is no separate system budget here. shadPS4 charges only its flexible VMA type ([memory.cpp L524](https://github.com/shadps4-emu/shadPS4/blob/30f4170896ce974353a6f6486d0ecec179aec54b/src/core/memory.cpp#L524)). fpPS4 passes `MAP_SYSTEM` to a virtual manager that ignores it, so it does charge it there ([mm_adr_virtual.pas L655-L660](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/mm_adr_virtual.pas#L655-L660), [ps4_map_mm.pas L429-L457](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_map_mm.pas#L429-L457)). A `MAP_FIXED` map over game flexible memory releases that charge, as direct maps do. - A length of 0, or one that is not a multiple of 16 KiB, returns `EINVAL` first, as in both emulators. A null name, or a name of 32 bytes or more, then throws before anything is mapped. shadPS4 returns `EFAULT` or `ENAMETOOLONG` before mapping. fpPS4 maps the range and leaves it unnamed. The twin skips a null name and truncates a long one. - `sceKernelVirtualQuery` reports the mapping as flexible, as fpPS4 does ([L1096](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/kernel/ps4_map_mm.pas#L1096)). shadPS4 reports it as not flexible ([memory.cpp L1209](https://github.com/shadps4-emu/shadPS4/blob/30f4170896ce974353a6f6486d0ecec179aec54b/src/core/memory.cpp#L1209)). Unverified on a PS5: the system budget and whether this map is charged to the game budget, the `sceKernelVirtualQuery` flags of a system map, the null and over-long name results, flag 0x8000 and the other flag and protection bits for this function, default placement, and `MAP_FIXED` with a null address. All of this is recorded in `docs/dev/debt/libkernel.md`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Home 25H2 (i7-11800H, WinLibs MinGW-w64 GCC 15.2.0 UCRT r7, CMake 4.3.1, Ninja 1.13.2, Release, `BUILD_TESTING=ON`) at b8ab1b1a5c, and Ubuntu 24.04.5 on WSL2 (GCC 13.3.0, CMake 3.28.3) with the PR merged into `main` 4e43321bd6 as the Linux CI job does: - No warnings in the changed files on either host. - `ctest -R "guest_system_flexible_memory|guest_memory|guest_flexible_memory|guest_kernel_sys|windows_kernel_variant_sys"`: `100% tests passed, 0 tests failed out of 31` on Windows and `out of 34` on Linux. Those include `guest_system_flexible_memory`, `guest_memory`, the five `guest_flexible_memory_*` tests, the `guest_kernel_sys*` tests and, on Windows only, `windows_kernel_variant_sys`. - `guest_system_flexible_memory` with `--repeat until-fail:3`: 3 of 3 on both hosts. - Baseline: on `main` with only the new test file and its `add_test` block, the test builds and then aborts with `sceKernelMapNamedSystemFlexibleMemory is not exported by libkernel` on both hosts (Windows exit 0xc0000409). - The earlier head e107d81094 also passed this set on both hosts. On Linux, every library and test target built from a source that includes `libkernel/include/Api.hpp` or `libkernel_sys/include/Api.hpp` compiled and linked. Earlier checks on an arm64 Mac: - `x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -Wall -Wextra` on every changed `.cpp` reports no diagnostics, the same as on `main`. - `tools/check_conventions.py` passes. - `git merge-tree` against #11005, #10889, #10898, #9882, #7105, #11042, #10305 and each of the 44 PRs `tools/pr_overlap.py` could not compare: no changed file has more conflict regions than the same merge with `main`, where #10898, #7105 and others already conflict. None of their diffs mention `sceKernelMapNamedSystemFlexibleMemory`. ### 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) - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-10-25 21:27:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T21:27:09Z.

Review: COMMENTED


Reviewed exact head bda99550443b634a492ebc6c839e8261c668a276. The new export delegates mapping validation and placement to DoMapAnon, releases the existing game-flexible accounting only after a successful map, and applies the name after mapping; the documented differences from the twin (including unverified system-map behavior) are called out rather than presented as console facts. I found no functional defect in the diff. Eligibility, conventions, intake, labels, and the progress report pass; the Linux, Windows, and relinker product jobs are skipped by the current PR gate. The PR reports focused tests on Windows and WSL, but I did not independently rerun them locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645399873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11190#pullrequestreview-5645399873). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T21:27:09Z. **Review:** COMMENTED --- Reviewed exact head `bda99550443b634a492ebc6c839e8261c668a276`. The new export delegates mapping validation and placement to `DoMapAnon`, releases the existing game-flexible accounting only after a successful map, and applies the name after mapping; the documented differences from the twin (including unverified system-map behavior) are called out rather than presented as console facts. I found no functional defect in the diff. Eligibility, conventions, intake, labels, and the progress report pass; the Linux, Windows, and relinker product jobs are skipped by the current PR gate. The PR reports focused tests on Windows and WSL, but I did not independently rerun them locally.
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!11190
No description provided.