feat(libkernel): implement msync #1857

Merged
hikazey merged 1 commit from upstream-pr/1857 into main 2026-10-09 22:12:20 +00:00
hikazey commented 2026-10-08 22:10:21 +00:00 (Migrated from github.com)

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


What

Adds msync to libkernel, next to mmap, munmap and mprotect in DirectMemory/PosixMemory.cpp. It follows FreeBSD 11's kern_msync and vm_map_sync over the guest's mappings as sceKernelVirtualQuery reports them:

  • The address is rounded down to the page and the length rounded up, with the same unsigned arithmetic as kern_msync. A range whose end wraps is EINVAL.
  • MS_ASYNC | MS_INVALIDATE is EINVAL. Other flag bits are not checked, as in FreeBSD.
  • An unmapped start, or any unmapped page in the range, is ENOMEM. Length 0 checks the mapping that holds the address (vm_map_sync widens start == end to that entry). Reservations count as mapped, since FreeBSD has a map entry for them.
  • Success writes nothing back: the guest's mmap only creates private anonymous mappings, so there are no file mappings to sync.
  • MS_INVALIDATE on a mapped range throws. FreeBSD returns EBUSY there when the range holds mlocked pages, and locked pages are not tracked. The ENOMEM check comes first, as in vm_map_sync.

One limitation is recorded in TechnicalDebt with the rest: a page munmaped from inside a larger mapping still counts as mapped, because sceKernelVirtualQuery keeps reporting the whole mapping after a partial unmap (on main too).

EVO-PLAYER PPSA99039 v0.11.0 (sainsaji/EVO-PLAYER-PS5) imports it for a mapped-file benchmark (projects/evoplayer/src/evo_iobench.c, msync(p, len, MS_SYNC)). It's also in the absent lists of other titles in #1578, where it was claimed.

Tested

  • Title. EVO-PLAYER PPSA99039 v0.11.0 (release ZIP sha256 687c0f0f…5349, eboot.bin and the bundled libc.prx unwrapped from their SELFs), relinked with --windows on Windows 11. On main 364a56b6 with #1838, #1634, #1714 and #1641 merged locally (its earlier stops):
    • without this change it stops at FAIL: unresolved ELF import tZY4+SZNFhA from libkernel.prx (msync);
    • with it, it gets past and stops at KiJEPEWRyUY (sigaction, #1431).
  • New guest_msync test (Windows and Linux): success for the whole range, MS_ASYNC, an unaligned address inside the mapping, a range crossing a page boundary, length 0, an unknown flag bit and a reserved range, with errno untouched on success; EINVAL for MS_ASYNC | MS_INVALIDATE and a wrapping range; ENOMEM after munmap for the start, a range, length 0 and MS_INVALIDATE; the MS_INVALIDATE throw on a mapped range.
  • Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, configured like CI: guest_msync 5/5; full suite 482/482. Mutations, each with the test kept, all fail: the mapped check removed, the MS_ASYNC | MS_INVALIDATE check removed, the throw moved before the mapped check, and the wrap check removed.
  • Linux (Ubuntu 24.04 container, GCC 14.2, Release): guest_msync 5/5; full suite 488/488; the mapped-check mutation fails.
  • Rebased onto main ae91d978: guest_msync 3/3 on Windows; tools/check_conventions.py --base origin/main passes.
  • Not measured on a console: the semantics come from FreeBSD's source, as the TechnicalDebt line says.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793389707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1857). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-08T22:10:21Z. --- ### What Adds `msync` to libkernel, next to `mmap`, `munmap` and `mprotect` in `DirectMemory/PosixMemory.cpp`. It follows FreeBSD 11's [`kern_msync`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/vm/vm_mmap.c) and [`vm_map_sync`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/vm/vm_map.c) over the guest's mappings as `sceKernelVirtualQuery` reports them: - The address is rounded down to the page and the length rounded up, with the same unsigned arithmetic as `kern_msync`. A range whose end wraps is `EINVAL`. - `MS_ASYNC | MS_INVALIDATE` is `EINVAL`. Other flag bits are not checked, as in FreeBSD. - An unmapped start, or any unmapped page in the range, is `ENOMEM`. Length 0 checks the mapping that holds the address (`vm_map_sync` widens `start == end` to that entry). Reservations count as mapped, since FreeBSD has a map entry for them. - Success writes nothing back: the guest's `mmap` only creates private anonymous mappings, so there are no file mappings to sync. - `MS_INVALIDATE` on a mapped range throws. FreeBSD returns `EBUSY` there when the range holds `mlock`ed pages, and locked pages are not tracked. The `ENOMEM` check comes first, as in `vm_map_sync`. One limitation is recorded in TechnicalDebt with the rest: a page `munmap`ed from inside a larger mapping still counts as mapped, because `sceKernelVirtualQuery` keeps reporting the whole mapping after a partial unmap (on `main` too). EVO-PLAYER PPSA99039 v0.11.0 ([sainsaji/EVO-PLAYER-PS5](https://github.com/sainsaji/EVO-PLAYER-PS5)) imports it for a mapped-file benchmark (`projects/evoplayer/src/evo_iobench.c`, `msync(p, len, MS_SYNC)`). It's also in the absent lists of other titles in #1578, where it was claimed. ### Tested - **Title.** EVO-PLAYER PPSA99039 v0.11.0 (release ZIP sha256 `687c0f0f…5349`, `eboot.bin` and the bundled `libc.prx` unwrapped from their SELFs), relinked with `--windows` on Windows 11. On `main` 364a56b6 with #1838, #1634, #1714 and #1641 merged locally (its earlier stops): - without this change it stops at `FAIL: unresolved ELF import tZY4+SZNFhA from libkernel.prx` (`msync`); - with it, it gets past and stops at `KiJEPEWRyUY` (`sigaction`, #1431). - **New `guest_msync` test** (Windows and Linux): success for the whole range, `MS_ASYNC`, an unaligned address inside the mapping, a range crossing a page boundary, length 0, an unknown flag bit and a reserved range, with errno untouched on success; `EINVAL` for `MS_ASYNC | MS_INVALIDATE` and a wrapping range; `ENOMEM` after `munmap` for the start, a range, length 0 and `MS_INVALIDATE`; the `MS_INVALIDATE` throw on a mapped range. - **Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, configured like CI:** `guest_msync` 5/5; full suite 482/482. Mutations, each with the test kept, all fail: the mapped check removed, the `MS_ASYNC | MS_INVALIDATE` check removed, the throw moved before the mapped check, and the wrap check removed. - **Linux (Ubuntu 24.04 container, GCC 14.2, Release):** `guest_msync` 5/5; full suite 488/488; the mapped-check mutation fails. - Rebased onto `main` ae91d978: `guest_msync` 3/3 on Windows; `tools/check_conventions.py --base origin/main` passes. - Not measured on a console: the semantics come from FreeBSD's source, as the TechnicalDebt line says. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-08 22:10:43 +00:00 (Migrated from github.com)

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


Progress report (8d47cfb → 0bff6da)

➖ System libraries: 85.46% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel msync_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070031971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1857#issuecomment-6070031971). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T22:10:43Z. --- <!-- progress-report --> ### Progress report (8d47cfb → 0bff6da) ➖ **System libraries**: 85.46% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `msync_nid_postfix` | </details>
github-actions[bot] commented 2026-10-08 22:21:07 +00:00 (Migrated from github.com)

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


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
#1633 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp L1-2, L17, L115
#1909 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp L17
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070202599 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1857#issuecomment-6070202599). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T22:21:07Z. --- <!-- 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 | |---|---|---|---| | #1633 | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` [L1-2](https://github.com/boykopovar/AnyPS5/blob/c2ac5dd93f0b6275634fe57415681f27af95ae7d/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L1-L2), [L17](https://github.com/boykopovar/AnyPS5/blob/c2ac5dd93f0b6275634fe57415681f27af95ae7d/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L17), [L115](https://github.com/boykopovar/AnyPS5/blob/c2ac5dd93f0b6275634fe57415681f27af95ae7d/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L115) | | | | #1909 | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` [L17](https://github.com/boykopovar/AnyPS5/blob/c2ac5dd93f0b6275634fe57415681f27af95ae7d/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp#L17) | | |
adriadam10 commented 2026-10-09 21:53:07 +00:00 (Migrated from github.com)

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


Checked msync against FreeBSD 11 kern_msync/vm_map_sync (page rounding, wrap and MS_ASYNC | MS_INVALIDATE give EINVAL, unmapped range gives ENOMEM, MS_INVALIDATE on a mapped range throws).

The branch conflicted with main only by adjacency (mlock/munlock in PosixMemory.cpp, the test block in libkernel/CMakeLists.txt). I rebased it keeping both sides, no logic changes. Full build and ctest on the rebased head: 525/525, guest_msync passes, and without the change it doesn't link, so it does exercise the code.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089879773 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1857#issuecomment-6089879773). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T21:53:07Z. --- Checked `msync` against FreeBSD 11 `kern_msync`/`vm_map_sync` (page rounding, wrap and `MS_ASYNC | MS_INVALIDATE` give `EINVAL`, unmapped range gives `ENOMEM`, `MS_INVALIDATE` on a mapped range throws). The branch conflicted with `main` only by adjacency (`mlock`/`munlock` in `PosixMemory.cpp`, the test block in `libkernel/CMakeLists.txt`). I rebased it keeping both sides, no logic changes. Full build and ctest on the rebased head: 525/525, `guest_msync` passes, and without the change it doesn't link, so it does exercise the code.
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!1857
No description provided.