feat(libkernel): add munlockall export #5663

Merged
al3w0f205 merged 1 commit from upstream-pr/5663 into main 2026-10-15 15:03:54 +00:00
al3w0f205 commented 2026-10-14 05:16:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by al3w0f205 on 2026-10-14T05:16:20Z.


What

Adds the munlockall export (NpLBpgVV7PU, aerolib line 79270) following merged mlockall (#4386) for XPSemu (PPSA97358, QEMU os-posix.c), which imports both functions. Predicted stop on main:

FAIL: unresolved ELF import NpLBpgVV7PU from libkernel.prx

Implementation follows FreeBSD 11.4 sys/vm/vm_mmap.c sys_munlockall (commit 8c639945a44588f6cfdddfb057bb5aad88735da0): checks PRIV_VM_MUNLOCK, clears wire state and returns 0. To match, munlockall unlocks ranges locked by mlock (VirtualUnlock on Windows, tolerating ERROR_NOT_LOCKED for pages never locked, and munlockall on Linux).

Tested

  • Windows 11 MinGW GCC 15.2 Ninja build: guest_mlock_tests passed (1/1). Verified that munlockall() returns 0 and leaves *__error_nid_postfix() untouched, and that mlock(mapped, page) followed by munlockall() restores the host pages to unlocked state (verified with VirtualUnlock returning ERROR_NOT_LOCKED).
  • ools/check_conventions.py --base origin/main passed with 0 errors.

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
  • Unblocks: XPSemu (PPSA97358)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4853311389 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663). Originally posted by [al3w0f205](https://github.com/al3w0f205) on 2026-10-14T05:16:20Z. --- ### What Adds the munlockall export (NpLBpgVV7PU, aerolib line 79270) following merged mlockall (#4386) for XPSemu (PPSA97358, QEMU os-posix.c), which imports both functions. Predicted stop on main: ` FAIL: unresolved ELF import NpLBpgVV7PU from libkernel.prx ` Implementation follows FreeBSD 11.4 sys/vm/vm_mmap.c sys_munlockall (commit 8c639945a44588f6cfdddfb057bb5aad88735da0): checks PRIV_VM_MUNLOCK, clears wire state and returns 0. To match, munlockall unlocks ranges locked by mlock (VirtualUnlock on Windows, tolerating ERROR_NOT_LOCKED for pages never locked, and munlockall on Linux). ### Tested - Windows 11 MinGW GCC 15.2 Ninja build: guest_mlock_tests passed (1/1). Verified that munlockall() returns 0 and leaves *__error_nid_postfix() untouched, and that mlock(mapped, page) followed by munlockall() restores the host pages to unlocked state (verified with VirtualUnlock returning ERROR_NOT_LOCKED). - ools/check_conventions.py --base origin/main passed with 0 errors. ### 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 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] Unblocks: XPSemu (PPSA97358) - [x] GPUs tested: n/a - [x] AI-assisted: yes
AlexOfTheForest commented 2026-10-14 06:05:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-14T06:05:08Z.


munlockall returns 0 without touching memory, and the debt entry says that's fine because mlockall keeps no locked state. But mlock does lock pages: sceKernelMlock_nid_postfix calls LockHostPages (host mlock / VirtualLock), and guest_mlock checks VmLck after it. So after mlock(range) followed by munlockall(), the range stays locked on the host.

FreeBSD 11.4 sys_munlockall unwires the whole user map (vm_map_unwire over vm_map_min..vm_map_max with VM_MAP_WIRE_HOLESOK), which includes ranges wired by mlock. To match, munlockall would need to unlock what mlock locked, either by tracking those ranges or by walking the guest mappings and unlocking each one (Windows VirtualUnlock fails with ERROR_NOT_LOCKED on pages that were never locked, so that error has to be tolerated there).

A test that does mlock → munlockall → LockedKilobytes() == lockedBefore would catch this. Also, sys_munlockall checks PRIV_VM_MUNLOCK first; returning 0 assumes a privileged process, like the madvise MADV_PROTECT entry says. Worth stating that in the debt entry too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162046101 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#issuecomment-6162046101). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-14T06:05:08Z. --- munlockall returns 0 without touching memory, and the debt entry says that's fine because mlockall keeps no locked state. But mlock does lock pages: sceKernelMlock_nid_postfix calls LockHostPages (host mlock / VirtualLock), and guest_mlock checks VmLck after it. So after mlock(range) followed by munlockall(), the range stays locked on the host. FreeBSD 11.4 sys_munlockall unwires the whole user map (vm_map_unwire over vm_map_min..vm_map_max with VM_MAP_WIRE_HOLESOK), which includes ranges wired by mlock. To match, munlockall would need to unlock what mlock locked, either by tracking those ranges or by walking the guest mappings and unlocking each one (Windows VirtualUnlock fails with ERROR_NOT_LOCKED on pages that were never locked, so that error has to be tolerated there). A test that does mlock → munlockall → LockedKilobytes() == lockedBefore would catch this. Also, sys_munlockall checks PRIV_VM_MUNLOCK first; returning 0 assumes a privileged process, like the madvise MADV_PROTECT entry says. Worth stating that in the debt entry too.
github-actions[bot] commented 2026-10-14 12:43:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T12:43:36Z.


Progress report (6239cef → e03e6f7)

📈 System libraries: 88.7% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel munlockall_nid_postfix
libkernel sceKernelMunlockall_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6168617169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#issuecomment-6168617169). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T12:43:36Z. --- <!-- progress-report --> ### Progress report (6239cef → e03e6f7) 📈 **System libraries**: 88.7% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `munlockall_nid_postfix` | | libkernel | `sceKernelMunlockall_nid_postfix` | </details>
adriadam10 commented 2026-10-14 12:45:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T12:45:30Z.

Review: CHANGES_REQUESTED


Thanks for the PR. One correctness problem, also raised by @AlexOfTheForest and still unanswered:

  • core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp:227: munlockall returns 0 without touching anything, on the premise that mlockall keeps no locked state. But mlock does lock host pages (sceKernelMlock_nid_postfix → LockHostPages), so mlock(range); munlockall(); leaves the range locked. FreeBSD 11.4 sys_munlockall unwires the whole user map, including ranges wired by mlock. It needs to unlock what mlock locked, either by tracking those ranges or by walking the guest mappings (on Windows VirtualUnlock fails with ERROR_NOT_LOCKED for pages never locked, so tolerate that).
  • core/libs/tests/GuestMlock.cpp:143: the test only calls munlockall with nothing locked, so it passes with a no-op. Add mlock → munlockall → check the locked amount (as the file already does after mlock) is back to what it was before.
  • docs/dev/debt/libkernel.md:165: sys_munlockall checks PRIV_VM_MUNLOCK first; returning 0 assumes a privileged process, like the madvise MADV_PROTECT entry says. State that, and drop "since mlockall keeps no locked state" once the above is fixed.

Also, the overlap bot lists #5643 touching the same debt lines; check it conflicts only by adjacency after you rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5518078927 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#pullrequestreview-5518078927). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T12:45:30Z. **Review:** CHANGES_REQUESTED --- Thanks for the PR. One correctness problem, also raised by @AlexOfTheForest and still unanswered: - `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp:227`: `munlockall` returns 0 without touching anything, on the premise that `mlockall` keeps no locked state. But `mlock` does lock host pages (`sceKernelMlock_nid_postfix` → `LockHostPages`), so `mlock(range); munlockall();` leaves the range locked. FreeBSD 11.4 `sys_munlockall` unwires the whole user map, including ranges wired by `mlock`. It needs to unlock what `mlock` locked, either by tracking those ranges or by walking the guest mappings (on Windows `VirtualUnlock` fails with `ERROR_NOT_LOCKED` for pages never locked, so tolerate that). - `core/libs/tests/GuestMlock.cpp:143`: the test only calls `munlockall` with nothing locked, so it passes with a no-op. Add `mlock` → `munlockall` → check the locked amount (as the file already does after `mlock`) is back to what it was before. - `docs/dev/debt/libkernel.md:165`: `sys_munlockall` checks `PRIV_VM_MUNLOCK` first; returning 0 assumes a privileged process, like the `madvise` `MADV_PROTECT` entry says. State that, and drop "since `mlockall` keeps no locked state" once the above is fixed. Also, the overlap bot lists #5643 touching the same debt lines; check it conflicts only by adjacency after you rebase.
al3w0f205 commented 2026-10-14 23:08:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by al3w0f205 on 2026-10-14T23:08:59Z.


Thanks @AlexOfTheForest and @adriadam10 for the thorough review and guidance!

All points addressed:

  1. Unlocking locked memory: Implemented locked range tracking across LockHostPages / UnlockHostPages / sceKernelMunmap, and sceKernelMunlockall_nid_postfix / munlockall_nid_postfix now unlocks those host pages (walking the tracked ranges with VirtualUnlock, tolerating ERROR_NOT_LOCKED on Windows, and ::munlockall() on Linux).
  2. Unit tests updated: Added the mlock -> munlockall -> verify unlocked amount test in GuestMlock.cpp (!VirtualUnlock returning ERROR_NOT_LOCKED on Windows, and LockedKilobytes() == lockedBefore on Linux), along with the no-op call.
  3. Debt entry updated: Updated docs/dev/debt/libkernel.md to document the PRIV_VM_MUNLOCK check and that returning 0 assumes a privileged process, removing the previous note.
  4. Rebased: Rebased onto origin/main after #5643 was merged, resolving the adjacent debt lines conflict.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6180493839 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#issuecomment-6180493839). Originally posted by [al3w0f205](https://github.com/al3w0f205) on 2026-10-14T23:08:59Z. --- Thanks @AlexOfTheForest and @adriadam10 for the thorough review and guidance! All points addressed: 1. **Unlocking locked memory**: Implemented locked range tracking across LockHostPages / UnlockHostPages / sceKernelMunmap, and sceKernelMunlockall_nid_postfix / munlockall_nid_postfix now unlocks those host pages (walking the tracked ranges with VirtualUnlock, tolerating ERROR_NOT_LOCKED on Windows, and ::munlockall() on Linux). 2. **Unit tests updated**: Added the mlock -> munlockall -> verify unlocked amount test in GuestMlock.cpp (!VirtualUnlock returning ERROR_NOT_LOCKED on Windows, and LockedKilobytes() == lockedBefore on Linux), along with the no-op call. 3. **Debt entry updated**: Updated docs/dev/debt/libkernel.md to document the PRIV_VM_MUNLOCK check and that returning 0 assumes a privileged process, removing the previous note. 4. **Rebased**: Rebased onto origin/main after #5643 was merged, resolving the adjacent debt lines conflict.
adriadam10 commented 2026-10-15 11:51:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:51:13Z.

Review: APPROVED


Re-checked the three points against the new head: munlockall now unlocks the ranges mlock locked (tracked across LockHostPages/UnlockHostPages/sceKernelMunmap; ::munlockall() on Linux, VirtualUnlock tolerating ERROR_NOT_LOCKED on Windows), GuestMlock covers mlock → munlockall → locked amount back to the baseline, and the debt entry states the PRIV_VM_MUNLOCK assumption. Build and ctest (172 tests in libs/libkernel) pass, guest_mlock included; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5532629930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#pullrequestreview-5532629930). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:51:13Z. **Review:** APPROVED --- Re-checked the three points against the new head: `munlockall` now unlocks the ranges `mlock` locked (tracked across `LockHostPages`/`UnlockHostPages`/`sceKernelMunmap`; `::munlockall()` on Linux, `VirtualUnlock` tolerating `ERROR_NOT_LOCKED` on Windows), `GuestMlock` covers `mlock` → `munlockall` → locked amount back to the baseline, and the debt entry states the `PRIV_VM_MUNLOCK` assumption. Build and ctest (172 tests in libs/libkernel) pass, `guest_mlock` included; merging.
github-actions[bot] commented 2026-10-15 12:08:27 +00:00 (Migrated from github.com)

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


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3891 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4156 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4396 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4397 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4422 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4423 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4424 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4520 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4521 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4522 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4523 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4566 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4599 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4718 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4740 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4749 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4760 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 4813 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/DirectMemory/Export.cpp
docs/dev/debt/libkernel.md
PR 5835 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestMlock.cpp
PR 6222 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191683388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5663#issuecomment-6191683388). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:08:27Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 3891](https://www.github.com/boykopovar/AnyPS5/pull/3891) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4156](https://www.github.com/boykopovar/AnyPS5/pull/4156) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4396](https://www.github.com/boykopovar/AnyPS5/pull/4396) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4397](https://www.github.com/boykopovar/AnyPS5/pull/4397) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4422](https://www.github.com/boykopovar/AnyPS5/pull/4422) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4423](https://www.github.com/boykopovar/AnyPS5/pull/4423) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4424](https://www.github.com/boykopovar/AnyPS5/pull/4424) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4520](https://www.github.com/boykopovar/AnyPS5/pull/4520) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4521](https://www.github.com/boykopovar/AnyPS5/pull/4521) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4522](https://www.github.com/boykopovar/AnyPS5/pull/4522) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4523](https://www.github.com/boykopovar/AnyPS5/pull/4523) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4566](https://www.github.com/boykopovar/AnyPS5/pull/4566) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4599](https://www.github.com/boykopovar/AnyPS5/pull/4599) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4718](https://www.github.com/boykopovar/AnyPS5/pull/4718) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4740](https://www.github.com/boykopovar/AnyPS5/pull/4740) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4749](https://www.github.com/boykopovar/AnyPS5/pull/4749) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4760](https://www.github.com/boykopovar/AnyPS5/pull/4760) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 4813](https://www.github.com/boykopovar/AnyPS5/pull/4813) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 5835](https://www.github.com/boykopovar/AnyPS5/pull/5835) | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestMlock.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`<br>`docs/dev/debt/libkernel.md` |
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!5663
No description provided.