feat(libkernel): implement sceKernelMlock #359

Merged
DotDebian merged 1 commit from upstream-pr/359 into main 2026-10-05 01:20:14 +00:00
DotDebian commented 2026-10-02 13:28:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T13:28:10Z.


What

Implements sceKernelMlock, which threw NotImplemented.

  • Semantics follow FreeBSD mlock(2): the range is widened to guest page boundaries, a zero length returns 0, a range that wraps returns SCE_KERNEL_ERROR_EINVAL, a range that is not entirely mapped and accessible returns SCE_KERNEL_ERROR_ENOMEM.
  • Windows: VirtualLock. The number of pages a process may lock is bounded by its minimum working set, so on ERROR_WORKING_SET_QUOTA the working set minimum and maximum are both raised by the size of the range (current limits and flags read first, not overwritten) and the lock is retried once. If the limits cannot be raised the call returns SCE_KERNEL_ERROR_EAGAIN. Any other host failure throws.
  • Linux: mlock. On ENOMEM, EPERM or EAGAIN the soft RLIMIT_MEMLOCK is raised to the hard limit and the lock is retried once; the remaining error is returned as the matching SCE_KERNEL_ERROR_*.

Caller found: the libc.prx (SDK 9) shipped with Astro Bot (PPSA21564) imports it (3k6kx-zOOSQ) and calls it as (address, length) on its heap right after sceKernelMapNamedFlexibleMemoryInternal (libc.prx+0xb0e3) when one of its internal flags is set. A nonzero return takes its error path.

Tested

Windows 11, Intel Core Ultra 9 285K, MinGW-w64 GCC 15.2.0.

  • ctest: 107 of 107 pass.
  • New CheckMlock case in guest_memory: locks 4 MiB of flexible memory from an unaligned address (above the default 200 KiB minimum working set, so the quota path runs), checks that the working set minimum now covers it and that VirtualUnlock finds every page locked, locks again, then checks the zero length, the wrapping range, a reserved range (ENOMEM), unmapping a locked range, and the unmapped range (ENOMEM).
  • The Linux branch was not built or run here; CI is its first run. The test locks 64 KiB there and checks VmLck in /proc/self/status.
  • No title was run through it: Astro Bot leaves the flag clear, so its libc does not make the call.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4717007425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/359). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-02T13:28:10Z. --- ### What Implements `sceKernelMlock`, which threw `NotImplemented`. - Semantics follow FreeBSD `mlock(2)`: the range is widened to guest page boundaries, a zero length returns 0, a range that wraps returns `SCE_KERNEL_ERROR_EINVAL`, a range that is not entirely mapped and accessible returns `SCE_KERNEL_ERROR_ENOMEM`. - Windows: `VirtualLock`. The number of pages a process may lock is bounded by its minimum working set, so on `ERROR_WORKING_SET_QUOTA` the working set minimum and maximum are both raised by the size of the range (current limits and flags read first, not overwritten) and the lock is retried once. If the limits cannot be raised the call returns `SCE_KERNEL_ERROR_EAGAIN`. Any other host failure throws. - Linux: `mlock`. On `ENOMEM`, `EPERM` or `EAGAIN` the soft `RLIMIT_MEMLOCK` is raised to the hard limit and the lock is retried once; the remaining error is returned as the matching `SCE_KERNEL_ERROR_*`. Caller found: the `libc.prx` (SDK 9) shipped with Astro Bot (PPSA21564) imports it (`3k6kx-zOOSQ`) and calls it as `(address, length)` on its heap right after `sceKernelMapNamedFlexibleMemoryInternal` (`libc.prx+0xb0e3`) when one of its internal flags is set. A nonzero return takes its error path. ### Tested Windows 11, Intel Core Ultra 9 285K, MinGW-w64 GCC 15.2.0. - `ctest`: 107 of 107 pass. - New `CheckMlock` case in `guest_memory`: locks 4 MiB of flexible memory from an unaligned address (above the default 200 KiB minimum working set, so the quota path runs), checks that the working set minimum now covers it and that `VirtualUnlock` finds every page locked, locks again, then checks the zero length, the wrapping range, a reserved range (`ENOMEM`), unmapping a locked range, and the unmapped range (`ENOMEM`). - The Linux branch was not built or run here; CI is its first run. The test locks 64 KiB there and checks `VmLck` in `/proc/self/status`. - No title was run through it: Astro Bot leaves the flag clear, so its libc does not make the call. ### 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 (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-04 05:33:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T05:33:17Z.


Progress report (8387b4d → 774b349)

📈 System libraries: 79.17% (+0.03%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel sceKernelMlock_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5976962913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/359#issuecomment-5976962913). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-04T05:33:17Z. --- <!-- progress-report --> ### Progress report (8387b4d → 774b349) 📈 **System libraries**: 79.17% (+0.03%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `sceKernelMlock_nid_postfix` | </details>
oneandonlydean commented 2026-10-04 05:43:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:43:05Z.


Thanks, the semantics look right to me (I checked here that Linux mlock on a PROT_NONE reservation fails with ENOMEM, as your test expects). It now conflicts with main in core/libs/tests/GuestMemory.cpp; could you rebase?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977022820 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/359#issuecomment-5977022820). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:43:05Z. --- Thanks, the semantics look right to me (I checked here that Linux `mlock` on a PROT_NONE reservation fails with ENOMEM, as your test expects). It now conflicts with main in `core/libs/tests/GuestMemory.cpp`; could you rebase?
oneandonlydean commented 2026-10-04 22:37:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:55Z.


Thanks for the rebase. It conflicts with main again in core/libs/tests/GuestMemory.cpp, this time with #347's CheckNoOverwriteRejectsHostOccupiedMapping (the _WIN32 include block and the new test function sit where yours go); keeping both sides works. With both kept, guest_memory passes here. The only red CI job on this head is Windows guest_audio_in_no_device (0xc0000409), the flake #532 fixed, so a rebase also clears that. After that it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985218539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/359#issuecomment-5985218539). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:37:55Z. --- Thanks for the rebase. It conflicts with main again in `core/libs/tests/GuestMemory.cpp`, this time with #347's `CheckNoOverwriteRejectsHostOccupiedMapping` (the `_WIN32` include block and the new test function sit where yours go); keeping both sides works. With both kept, `guest_memory` passes here. The only red CI job on this head is Windows `guest_audio_in_no_device` (0xc0000409), the flake #532 fixed, so a rebase also clears that. After that it's ready to merge from my side.
oneandonlydean commented 2026-10-05 01:18:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:03Z.


Thanks for the rebase. CheckMlock now sits after #347's test and reuses main's _WIN32 include block, the code is unchanged, guest_memory passes here, and CI is green on both jobs. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986478667 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/359#issuecomment-5986478667). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T01:18:03Z. --- Thanks for the rebase. `CheckMlock` now sits after #347's test and reuses main's `_WIN32` include block, the code is unchanged, `guest_memory` passes here, and CI is green on both jobs. Ready to merge from my side.
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!359
No description provided.