feat(libkernel): implement mlock and munlock #1673

Merged
SleglJan merged 2 commits from upstream-pr/1673 into main 2026-10-09 21:15:20 +00:00
SleglJan commented 2026-10-08 16:11:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T16:11:35Z.


What

Adds the POSIX mlock export to libkernel as a wrapper over the existing sceKernelMlock (#359), beside munmap and mprotect in DirectMemory/PosixMemory.cpp: 0 on success, otherwise -1 with the guest errno taken from the kernel error code the sce form returns (EINVAL for a range that wraps around zero, ENOMEM for an unmapped or reserved-only range, EPERM and EAGAIN when the host refuses the lock), as FreeBSD 11's mlock(2) lists them. The signature is FreeBSD's, int mlock(const void *addr, size_t len); page rounding and a zero length at a page-aligned address returning 0 come from sceKernelMlock. An unexpected host error still throws there.

Second commit, asked for below: munlock (OG4RsDwLguo) and sceKernelMunlock (xQIIfJ860sk, listed in aerolib), the same pair for unlocking. The host unlock is munlock on Linux and VirtualUnlock per committed region on Windows, where pages that are not locked count as unlocked. Results follow FreeBSD 11's sys_munlock and vm_map_unwire (vm_mmap.c, vm_map.c): 0 for a mapped range whether or not it is locked, a reserved-only range included, EINVAL for a range that wraps around zero, ENOMEM for an unmapped range; an unexpected host error throws. sceKernelMlock shares the page rounding with it.

Overlap: #1633 (madvise) and #1857 (msync) add functions at the same place in PosixMemory.cpp; adjacent additions, whichever merges second needs a one-line rebase.

In the Linux audit of the homebrew catalog (#1578), mlock was absent for 14 of the 34 relinked titles. A bundled libChakraCore.prx imports both mlock and munlock (@EdouardSence below).

Tested

  • Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), full build with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON on this branch rebased onto main d70b8998: guest_mlock and guest_memory pass --repeat until-fail:3; tools/check_test_registration.py and tools/check_conventions.py --base upstream/main pass.
  • guest_mlock: the sceKernelMlock cases of guest_memory through the POSIX form (without the Windows working-set check): zero length at an aligned address with errno untouched, an unaligned start inside a flexible mapping (on Linux VmLck grows by the rounded length), locking twice; -1 with errno 22 for a range wrapping around zero, errno 12 for a reserved-only range and for the mapping after sceKernelMunmap. Written before the implementation; it failed on the unresolved export and passes with it.
  • munlock cases in the same test, written first and failing on the unresolved export: zero length with errno untouched; an unaligned start whose rounded range unlocks the whole mapping (VmLck back to its starting value on Linux); unlocking an already unlocked range returns 0 with errno untouched; lock one page, unlock it (VmLck up by one page, then back); -1 with errno 22 for a range wrapping around zero; 0 for a reserved-only range; -1 with errno 12 for the mapping after sceKernelMunmap.
  • The patched libkernel.prx exports the four NIDs (mTBZfEal2Bw, OG4RsDwLguo, 3k6kx-zOOSQ, xQIIfJ860sk).
  • tools/import_audit.py on five of the 14 catalog titles, absent imports on main 4b005b22 → the mlock commit: PPSA17641 SymphonyStation5 35 → 34, PPSA74126 Stremio Plus 49 → 48, PPSA97510 PSXS5 72 → 71, PPSA99000 ProsperoStore 40 → 39, PPSA99001 ProsperoRadio 64 → 63. None of them loads yet; the remaining imports are listed in #1578.
  • Windows, mlock commit: the fork's Build workflow merged onto main 6b56175a, run 37822087281: guest_mlock passes its three repeats, full suite 474/474. hikazey ran it merged onto main 6b56175a on Windows 11 with MinGW-w64 GCC 15.2.0 (the CI toolchain): guest_mlock 5/5, the full suite 474/474, and the test fails 5/5 when SetError is given the sceKernelMlock result without the & 0xffff (comment).
  • Windows, munlock commit: the fork's Build workflow on this head merged onto main d70b8998, run 37935576283: guest_mlock and guest_memory pass, full suite 508/508; Linux and the relinker matrix pass in the same run..

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 in TechnicalDebt
  • 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
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4789628408 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T16:11:35Z. --- ### What Adds the POSIX `mlock` export to libkernel as a wrapper over the existing `sceKernelMlock` (#359), beside `munmap` and `mprotect` in `DirectMemory/PosixMemory.cpp`: 0 on success, otherwise -1 with the guest errno taken from the kernel error code the sce form returns (`EINVAL` for a range that wraps around zero, `ENOMEM` for an unmapped or reserved-only range, `EPERM` and `EAGAIN` when the host refuses the lock), as FreeBSD 11's [`mlock(2)`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libc/sys/mlock.2) lists them. The signature is FreeBSD's, `int mlock(const void *addr, size_t len)`; page rounding and a zero length at a page-aligned address returning 0 come from `sceKernelMlock`. An unexpected host error still throws there. Second commit, asked for below: `munlock` (`OG4RsDwLguo`) and `sceKernelMunlock` (`xQIIfJ860sk`, listed in aerolib), the same pair for unlocking. The host unlock is `munlock` on Linux and `VirtualUnlock` per committed region on Windows, where pages that are not locked count as unlocked. Results follow FreeBSD 11's `sys_munlock` and `vm_map_unwire` ([`vm_mmap.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/vm/vm_mmap.c), [`vm_map.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/vm/vm_map.c)): 0 for a mapped range whether or not it is locked, a reserved-only range included, `EINVAL` for a range that wraps around zero, `ENOMEM` for an unmapped range; an unexpected host error throws. `sceKernelMlock` shares the page rounding with it. Overlap: #1633 (`madvise`) and #1857 (`msync`) add functions at the same place in `PosixMemory.cpp`; adjacent additions, whichever merges second needs a one-line rebase. In the Linux audit of the homebrew catalog (#1578), `mlock` was absent for 14 of the 34 relinked titles. A bundled `libChakraCore.prx` imports both `mlock` and `munlock` (@EdouardSence below). ### Tested - Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), full build with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON` on this branch rebased onto `main` d70b8998: `guest_mlock` and `guest_memory` pass `--repeat until-fail:3`; `tools/check_test_registration.py` and `tools/check_conventions.py --base upstream/main` pass. - `guest_mlock`: the `sceKernelMlock` cases of `guest_memory` through the POSIX form (without the Windows working-set check): zero length at an aligned address with errno untouched, an unaligned start inside a flexible mapping (on Linux `VmLck` grows by the rounded length), locking twice; -1 with errno 22 for a range wrapping around zero, errno 12 for a reserved-only range and for the mapping after `sceKernelMunmap`. Written before the implementation; it failed on the unresolved export and passes with it. - `munlock` cases in the same test, written first and failing on the unresolved export: zero length with errno untouched; an unaligned start whose rounded range unlocks the whole mapping (`VmLck` back to its starting value on Linux); unlocking an already unlocked range returns 0 with errno untouched; lock one page, unlock it (`VmLck` up by one page, then back); -1 with errno 22 for a range wrapping around zero; 0 for a reserved-only range; -1 with errno 12 for the mapping after `sceKernelMunmap`. - The patched `libkernel.prx` exports the four NIDs (`mTBZfEal2Bw`, `OG4RsDwLguo`, `3k6kx-zOOSQ`, `xQIIfJ860sk`). - `tools/import_audit.py` on five of the 14 catalog titles, absent imports on `main` 4b005b22 → the mlock commit: PPSA17641 SymphonyStation5 35 → 34, PPSA74126 Stremio Plus 49 → 48, PPSA97510 PSXS5 72 → 71, PPSA99000 ProsperoStore 40 → 39, PPSA99001 ProsperoRadio 64 → 63. None of them loads yet; the remaining imports are listed in #1578. - Windows, mlock commit: the fork's Build workflow merged onto `main` 6b56175a, [run 37822087281](https://github.com/SleglJan/AnyPS5/actions/runs/37822087281): `guest_mlock` passes its three repeats, full suite 474/474. hikazey ran it merged onto `main` 6b56175a on Windows 11 with MinGW-w64 GCC 15.2.0 (the CI toolchain): `guest_mlock` 5/5, the full suite 474/474, and the test fails 5/5 when `SetError` is given the `sceKernelMlock` result without the `& 0xffff` ([comment](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6066324621)). - Windows, munlock commit: the fork's Build workflow on this head merged onto `main` d70b8998, [run 37935576283](https://github.com/SleglJan/AnyPS5/actions/runs/37935576283): `guest_mlock` and `guest_memory` pass, full suite 508/508; Linux and the relinker matrix pass in the same run.. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-08 17:08:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T17:08:37Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1857 core/libs/prx/libkernel/CMakeLists.txt L438
core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp L7, L99
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065084562 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6065084562). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T17:08:37Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1857 | `core/libs/prx/libkernel/CMakeLists.txt` [L438](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/libs/prx/libkernel/CMakeLists.txt#L438)<br>`core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` [L7](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L7), [L99](https://github.com/boykopovar/AnyPS5/blob/f145139676a48306681d3a4d5fabf83938759daf/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L99) | | |
hikazey commented 2026-10-08 18:21:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T18:21:40Z.


Windows run (the description lists Linux only): Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X. This commit merged onto main 6b56175a, which merges cleanly.

build guest_mlock
this PR 5/5 passed
SetError(result) without & 0xffff, test kept 5/5 failed
full suite with this PR 474/474 passed

So the POSIX form works on top of the Windows sceKernelMlock path (VirtualLock) as well: zero length, the unaligned start inside a flexible mapping, locking twice, and errno 22/12 for the wrapping, reserved-only and unmapped ranges.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6066324621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6066324621). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-08T18:21:40Z. --- Windows run (the description lists Linux only): Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X. This commit merged onto `main` `6b56175a`, which merges cleanly. | build | `guest_mlock` | | --- | --- | | this PR | 5/5 passed | | `SetError(result)` without `& 0xffff`, test kept | 5/5 failed | | full suite with this PR | 474/474 passed | So the POSIX form works on top of the Windows `sceKernelMlock` path (`VirtualLock`) as well: zero length, the unaligned start inside a flexible mapping, locking twice, and errno 22/12 for the wrapping, reserved-only and unmapped ranges. AI-assisted: yes (Claude Code).
SleglJan commented 2026-10-08 18:27:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T18:27:18Z.


Thanks for the Windows run, especially the mutation check. Added to the Tested section.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6066419084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6066419084). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T18:27:18Z. --- Thanks for the Windows run, especially the mutation check. Added to the Tested section.
EdouardSence commented 2026-10-09 10:37:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-09T10:37:42Z.


Title evidence: Ratchet & Clank: Rift Apart (PPSA01474) needs mlock.

On main 43972e69 the relinked title doesn't start:

./app.elf: symbol lookup error: .../app0/prx/libChakraCore.prx.guest.prx: undefined symbol: mTBZfEal2Bw

mTBZfEal2Bw is mlock, imported by the title's bundled libChakraCore.prx. The same module also imports munlock (OG4RsDwLguo), which nothing exports either, so it fails on that symbol next. With this PR plus a local placeholder for munlock, the title loads and runs until a later, unrelated stop.

Since munlock is the counterpart of what this PR adds, would you add it here? Otherwise I can open a separate PR once this one is merged.

Measured on Linux, RX 9070 XT. Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079214264 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6079214264). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-09T10:37:42Z. --- Title evidence: Ratchet & Clank: Rift Apart (PPSA01474) needs `mlock`. On `main` 43972e69 the relinked title doesn't start: ``` ./app.elf: symbol lookup error: .../app0/prx/libChakraCore.prx.guest.prx: undefined symbol: mTBZfEal2Bw ``` `mTBZfEal2Bw` is `mlock`, imported by the title's bundled `libChakraCore.prx`. The same module also imports `munlock` (`OG4RsDwLguo`), which nothing exports either, so it fails on that symbol next. With this PR plus a local placeholder for `munlock`, the title loads and runs until a later, unrelated stop. Since `munlock` is the counterpart of what this PR adds, would you add it here? Otherwise I can open a separate PR once this one is merged. Measured on Linux, RX 9070 XT. *Tested and written by Claude (AI assistant) on a contributor's machine.*
github-actions[bot] commented 2026-10-09 13:16:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T13:16:00Z.


Progress report (d70b899 → 1078608)

📈 System libraries: 84.71% (+0.02%, +3 implemented functions)

✅ 3 implemented
Library Name
libkernel mlock_nid_postfix
libkernel munlock_nid_postfix
libkernel sceKernelMunlock_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081641300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6081641300). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T13:16:00Z. --- <!-- progress-report --> ### Progress report (d70b899 → 1078608) 📈 **System libraries**: 84.71% (+0.02%, +3 implemented functions) <details> <summary>✅ 3 implemented</summary> | Library | Name | | - | - | | libkernel | `mlock_nid_postfix` | | libkernel | `munlock_nid_postfix` | | libkernel | `sceKernelMunlock_nid_postfix` | </details>
EdouardSence commented 2026-10-09 13:29:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-09T13:29:04Z.


Thanks for adding munlock. Confirmed on Ratchet & Clank: Rift Apart (PPSA01474) with head 10786087, on Linux with an RX 9070 XT.

  • With my local munlock placeholder removed, the relinked title has no undefined imports left, and its bundled libChakraCore.prx loads.
  • The run then continues as before, into rendering, where it stops on unrelated issues.
  • guest_mlock passes here.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081881684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6081881684). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-09T13:29:04Z. --- Thanks for adding `munlock`. Confirmed on Ratchet & Clank: Rift Apart (PPSA01474) with head 10786087, on Linux with an RX 9070 XT. - With my local `munlock` placeholder removed, the relinked title has no undefined imports left, and its bundled `libChakraCore.prx` loads. - The run then continues as before, into rendering, where it stops on unrelated issues. - `guest_mlock` passes here. *Tested and written by Claude (AI assistant) on a contributor's machine.*
SleglJan commented 2026-10-09 13:36:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-09T13:36:45Z.


Thanks for the ChakraCore log line and for confirming the head. munlock (OG4RsDwLguo) and sceKernelMunlock (xQIIfJ860sk, listed in aerolib) are in the second commit, so the pair matches mlock/sceKernelMlock; the POSIX form wraps the sce form as mlock does. The host unlock is munlock on Linux and VirtualUnlock on Windows, where a region that is only partly locked is unlocked page by page and an unmapped guest range is told apart from a reservation through libkernel's own registries, since both read as a reserved placeholder there. Results follow FreeBSD 11's sys_munlock and vm_map_unwire: 0 for a mapped range whether or not it is locked, a reserved-only range included, EINVAL for a range that wraps around zero, ENOMEM for an unmapped range. guest_mlock covers them, on Linux with VmLck returning to its starting value. Windows: the fork's Build workflow on head 10786087 merged onto main d70b8998, run 37935576283: guest_mlock and guest_memory pass, full suite 508/508; Linux and the relinker matrix pass in the same run. Title and description updated.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082027398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6082027398). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-09T13:36:45Z. --- Thanks for the ChakraCore log line and for confirming the head. `munlock` (`OG4RsDwLguo`) and `sceKernelMunlock` (`xQIIfJ860sk`, listed in aerolib) are in the second commit, so the pair matches `mlock`/`sceKernelMlock`; the POSIX form wraps the sce form as `mlock` does. The host unlock is `munlock` on Linux and `VirtualUnlock` on Windows, where a region that is only partly locked is unlocked page by page and an unmapped guest range is told apart from a reservation through libkernel's own registries, since both read as a reserved placeholder there. Results follow FreeBSD 11's `sys_munlock` and `vm_map_unwire`: 0 for a mapped range whether or not it is locked, a reserved-only range included, `EINVAL` for a range that wraps around zero, `ENOMEM` for an unmapped range. `guest_mlock` covers them, on Linux with `VmLck` returning to its starting value. Windows: the fork's Build workflow on head 10786087 merged onto `main` d70b8998, [run 37935576283](https://github.com/SleglJan/AnyPS5/actions/runs/37935576283): `guest_mlock` and `guest_memory` pass, full suite 508/508; Linux and the relinker matrix pass in the same run. Title and description updated. AI-assisted: yes (Claude Code).
adriadam10 commented 2026-10-09 21:15:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T21:15:17Z.


Checked on Linux, main + this head: full build, ctest 521/521 (guest_mlock passes; without the change it doesn't link, so the test needs it). The four NIDs match aerolib, and the errno mapping follows the cited FreeBSD sys_munlock/vm_map_unwire. The Windows path is covered by the fork run and @hikazey's run; the linux/windows jobs are skipped upstream by the current manual-build policy. Merging.

Overlap with #1633 and #1857 in PosixMemory.cpp is adjacent only; whichever lands second needs a one-line rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089382898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1673#issuecomment-6089382898). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T21:15:17Z. --- Checked on Linux, `main` + this head: full build, ctest 521/521 (`guest_mlock` passes; without the change it doesn't link, so the test needs it). The four NIDs match aerolib, and the errno mapping follows the cited FreeBSD `sys_munlock`/`vm_map_unwire`. The Windows path is covered by the fork run and @hikazey's run; the `linux`/`windows` jobs are skipped upstream by the current manual-build policy. Merging. Overlap with #1633 and #1857 in `PosixMemory.cpp` is adjacent only; whichever lands second needs a one-line rebase.
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!1673
No description provided.