feat(libkernel): implement mincore #5643

Merged
Amirtheshwaran merged 3 commits from upstream-pr/5643 into main 2026-10-14 14:25:54 +00:00
Amirtheshwaran commented 2026-10-14 02:46:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Amirtheshwaran on 2026-10-14T02:46:45Z.


What

Implement mincore (+hFjyohmOU0) in libkernel.prx following FreeBSD 11's sys_mincore (sys/vm/vm_mmap.c):

  • Address is rounded down to page boundary (trunc_page(address)).
  • Returns 0 immediately if length == 0.
  • Returns EFAULT (14) if vec == nullptr.
  • Checks address range overflow (returns ENOMEM / 12).
  • Verifies memory residency across the whole requested range via GuestRangeMapped (which queries sceKernelVirtualQuery); returns ENOMEM (12) if any page in the range is unmapped.
  • Fills vec with MINCORE_INCORE (0x1) for mapped pages; residency, referenced and modified bits are not modelled.
  • Adds test cases in core/libs/tests/GuestMemory.cpp covering unaligned address rounding, null vector with non-zero length, overflow, partial unmapped ranges, and fully unmapped ranges.
  • Documents technical debt in docs/dev/debt/libkernel.md.

Target context: Java 25 LAN / DNS (PPSA99869, #5501) reaches +hFjyohmOU0 as documented by @karimibrahim07's relink tests on #5563:

  • on main: FAIL: unresolved ELF import UkEV4hvMo1E from libkernel.prx (sysctlnametomib)
  • main + #5502: FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx (fegetenv)
  • main + #5502 + #5563: FAIL: unresolved ELF import +hFjyohmOU0 from libkernel.prx (mincore)

Tested

  • Windows 11 (WinLibs MinGW GCC 15.2.0 r7 / Ninja / CMake).
  • Built and ran guest_memory_tests with CTest: 100% passed (1/1 Test #965: guest_memory ..................... Passed).
  • All conventions checks passed (python tools/check_conventions.py).

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: PPSA99869 #5501
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4852226056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643). Originally posted by [Amirtheshwaran](https://github.com/Amirtheshwaran) on 2026-10-14T02:46:45Z. --- ### What Implement `mincore` (`+hFjyohmOU0`) in `libkernel.prx` following FreeBSD 11's `sys_mincore` (`sys/vm/vm_mmap.c`): - Address is rounded down to page boundary (`trunc_page(address)`). - Returns 0 immediately if `length == 0`. - Returns `EFAULT` (14) if `vec == nullptr`. - Checks address range overflow (returns `ENOMEM` / 12). - Verifies memory residency across the whole requested range via `GuestRangeMapped` (which queries `sceKernelVirtualQuery`); returns `ENOMEM` (12) if any page in the range is unmapped. - Fills `vec` with `MINCORE_INCORE` (0x1) for mapped pages; residency, referenced and modified bits are not modelled. - Adds test cases in `core/libs/tests/GuestMemory.cpp` covering unaligned address rounding, null vector with non-zero length, overflow, partial unmapped ranges, and fully unmapped ranges. - Documents technical debt in `docs/dev/debt/libkernel.md`. Target context: Java 25 LAN / DNS (PPSA99869, #5501) reaches `+hFjyohmOU0` as documented by @karimibrahim07's relink tests on #5563: - on `main`: `FAIL: unresolved ELF import UkEV4hvMo1E from libkernel.prx` (`sysctlnametomib`) - `main` + #5502: `FAIL: unresolved ELF import psx0YiAKm7k from libSceLibcInternal.prx` (`fegetenv`) - `main` + #5502 + #5563: `FAIL: unresolved ELF import +hFjyohmOU0 from libkernel.prx` (`mincore`) ### Tested - Windows 11 (WinLibs MinGW GCC 15.2.0 r7 / Ninja / CMake). - Built and ran `guest_memory_tests` with CTest: 100% passed (`1/1 Test #965: guest_memory ..................... Passed`). - All conventions checks passed (`python tools/check_conventions.py`). ### 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: PPSA99869 #5501 - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-14 02:47:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T02:47:23Z.


Progress report (63fb0cd → 2854d51)

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

✅ 1 implemented
Library Name
libkernel mincore_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159753400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6159753400). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T02:47:23Z. --- <!-- progress-report --> ### Progress report (63fb0cd → 2854d51) ➖ **System libraries**: 88.36% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `mincore_nid_postfix` | </details>
xXPBSXx commented 2026-10-14 03:38:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T03:38:38Z.


Static review (head d4e55f710, read against FreeBSD 11.4 sys/vm/vm_mmap.c, release/11.4.0, kern_mincore lines 750-787); I did not build or run this PR. Two differences from FreeBSD, in the order of the checks:

  1. length == 0: the PR returns 0 at once (if (length == 0) return 0;) and the test pins it (mincore_nid_postfix(memory, 0, nullptr) == 0). kern_mincore still looks up the start address when the length is 0 (end = addr + round_page(len), then vm_map_lookup_entry(map, addr, &entry) fails with ENOMEM, lines 772-787), so mincore on an unmapped address with length 0 answers ENOMEM there.
  2. vec == NULL: the PR answers EFAULT before it checks the range and the mapping (if (vec == nullptr) return failed(GuestFault); comes before GuestRangeMapped). In FreeBSD the range and mapping checks (ENOMEM) come first; vec is only written afterwards, so a NULL vec over an unmapped range gives ENOMEM, not EFAULT.

The residency bits (referenced/modified) that FreeBSD reports are not set; the PR says so in its debt entry.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6160336733 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6160336733). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-14T03:38:38Z. --- Static review (head `d4e55f710`, read against FreeBSD 11.4 `sys/vm/vm_mmap.c`, `release/11.4.0`, `kern_mincore` lines 750-787); I did not build or run this PR. Two differences from FreeBSD, in the order of the checks: 1. `length == 0`: the PR returns 0 at once (`if (length == 0) return 0;`) and the test pins it (`mincore_nid_postfix(memory, 0, nullptr) == 0`). `kern_mincore` still looks up the start address when the length is 0 (`end = addr + round_page(len)`, then `vm_map_lookup_entry(map, addr, &entry)` fails with `ENOMEM`, lines 772-787), so `mincore` on an unmapped address with length 0 answers `ENOMEM` there. 2. `vec == NULL`: the PR answers `EFAULT` before it checks the range and the mapping (`if (vec == nullptr) return failed(GuestFault);` comes before `GuestRangeMapped`). In FreeBSD the range and mapping checks (`ENOMEM`) come first; `vec` is only written afterwards, so a NULL `vec` over an unmapped range gives `ENOMEM`, not `EFAULT`. The residency bits (referenced/modified) that FreeBSD reports are not set; the PR says so in its debt entry. AI-assisted (Claude Code).
MarcZX10 commented 2026-10-14 05:09:16 +00:00 (Migrated from github.com)

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


I independently checked the FreeBSD 11.4 releng/11.4 implementation (sys/vm/vm_mmap.c, kern_mincore): it rounds the address and computes end even when len == 0, then performs vm_map_lookup_entry before the page loop. Thus zero length succeeds for a mapped starting page but returns ENOMEM when the starting page is unmapped. The mapping/contiguity checks also precede subyte(vec + ...), so a NULL vector over an unmapped range returns ENOMEM before EFAULT. This confirms the two ordering differences called out above.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161374802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6161374802). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T05:09:16Z. --- I independently checked the FreeBSD 11.4 `releng/11.4` implementation (`sys/vm/vm_mmap.c`, `kern_mincore`): it rounds the address and computes `end` even when `len == 0`, then performs `vm_map_lookup_entry` before the page loop. Thus zero length succeeds for a mapped starting page but returns `ENOMEM` when the starting page is unmapped. The mapping/contiguity checks also precede `subyte(vec + ...)`, so a NULL vector over an unmapped range returns `ENOMEM` before `EFAULT`. This confirms the two ordering differences called out above.
github-actions[bot] commented 2026-10-14 05:21:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T05:21:40Z.


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
#5663 docs/dev/debt/libkernel.md L167-168
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161522121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6161522121). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T05:21:40Z. --- <!-- 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 | |---|---|---|---| | #5663 | `docs/dev/debt/libkernel.md` [L167-168](https://github.com/boykopovar/AnyPS5/blob/4469ea22f6c2982fe7e350985be3a7816790c1b2/docs/dev/debt/libkernel.md#L167-L168) | | |
pipe1os commented 2026-10-14 05:42:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pipe1os on 2026-10-14T05:42:06Z.


Built and ran this on Windows: head d4e55f710 merged onto main f5424b4db (no conflicts), Windows 11, WinLibs GCC 15.2.0, Ninja Release.

  • guest_memory passes. With PosixMemory.cpp put back to main and the test kept, it doesn't link (undefined reference to mincore_nid_postfix).
  • Java 25 LAN / DNS (PPSA99869) with #5502 + #5563 + this: the loader gets past +hFjyohmOU0 and stops at the last missing import, KZ-4qlqlpmo (sigsuspend).

One more difference on top of the two @xXPBSXx listed, about how many bytes of vec get written. FreeBSD 11.0 truncates the address first and adds the rounded length to that (vm_mmap.c L762-L763), then writes exactly (end - first_addr) / PAGE_SIZE bytes (L964-L965). The PR rounds address + length instead, so with an unaligned address it can write one byte more than a caller sized vec for. Local probe on the merge, vec pre-filled with 0x55:

mincore(memory + 1, page, vec) = 0, vec = 1 1 85

FreeBSD only writes vec[0] there. The test's mincore_nid_postfix(memory + 1, page, mincoreVec) doesn't catch it because mincoreVec has 4 bytes. Computing end from the truncated address, and checking that vec[1] stays untouched in that case, would cover it.

AI-assisted.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161763943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6161763943). Originally posted by [pipe1os](https://github.com/pipe1os) on 2026-10-14T05:42:06Z. --- Built and ran this on Windows: head `d4e55f710` merged onto `main` `f5424b4db` (no conflicts), Windows 11, WinLibs GCC 15.2.0, Ninja Release. - `guest_memory` passes. With `PosixMemory.cpp` put back to `main` and the test kept, it doesn't link (`undefined reference to mincore_nid_postfix`). - Java 25 LAN / DNS (PPSA99869) with #5502 + #5563 + this: the loader gets past `+hFjyohmOU0` and stops at the last missing import, `KZ-4qlqlpmo` (`sigsuspend`). One more difference on top of the two @xXPBSXx listed, about how many bytes of `vec` get written. FreeBSD 11.0 truncates the address first and adds the rounded length to that ([`vm_mmap.c` L762-L763](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/vm/vm_mmap.c#L762-L763)), then writes exactly `(end - first_addr) / PAGE_SIZE` bytes ([L964-L965](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/vm/vm_mmap.c#L964-L965)). The PR rounds `address + length` instead, so with an unaligned address it can write one byte more than a caller sized `vec` for. Local probe on the merge, `vec` pre-filled with 0x55: ``` mincore(memory + 1, page, vec) = 0, vec = 1 1 85 ``` FreeBSD only writes `vec[0]` there. The test's `mincore_nid_postfix(memory + 1, page, mincoreVec)` doesn't catch it because `mincoreVec` has 4 bytes. Computing `end` from the truncated address, and checking that `vec[1]` stays untouched in that case, would cover it. AI-assisted.
AlexOfTheForest commented 2026-10-14 06:05:29 +00:00 (Migrated from github.com)

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


GuestRangeMapped goes through sceKernelVirtualQuery, which also reports reservations (see the msync debt entry). So a range from sceKernelReserveVirtualRange passes the check and every page gets 0x1 (MINCORE_INCORE), but nothing is resident there. FreeBSD reports 0 for a page with nothing resident. Reserved-but-uncommitted pages should probably get 0, or the debt entry should say they're reported as resident.

The memory + 1, page case rounds to two pages (start rounds down, end rounds up), but the test only checks mincoreVec[0]. Checking mincoreVec[1] too would cover the round-up.

Minor: after the overflow check and the length == 0 return, end <= first can't be true, so that branch is dead.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162050777 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6162050777). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-14T06:05:29Z. --- GuestRangeMapped goes through sceKernelVirtualQuery, which also reports reservations (see the msync debt entry). So a range from sceKernelReserveVirtualRange passes the check and every page gets 0x1 (MINCORE_INCORE), but nothing is resident there. FreeBSD reports 0 for a page with nothing resident. Reserved-but-uncommitted pages should probably get 0, or the debt entry should say they're reported as resident. The memory + 1, page case rounds to two pages (start rounds down, end rounds up), but the test only checks mincoreVec[0]. Checking mincoreVec[1] too would cover the round-up. Minor: after the overflow check and the length == 0 return, end <= first can't be true, so that branch is dead.
MarcZX10 commented 2026-10-14 07:04:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T07:04:23Z.


One useful implementation detail for the reserved-page case: VirtualQueryInfo already exposes is_committed. In sceKernelVirtualQuery, the GuestReservation path zero-initializes the result and returns reservation bounds, leaving is_committed clear; tracked allocations and committed host regions set it. A per-page query can preserve the current ENOMEM validation while filling each vec byte from is_committed (with the debt entry retaining that actual page residency is not modeled). A focused test could reserve one page, confirm it reports 0, then map/commit it and confirm it reports 1.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162857802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6162857802). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T07:04:23Z. --- One useful implementation detail for the reserved-page case: `VirtualQueryInfo` already exposes `is_committed`. In `sceKernelVirtualQuery`, the `GuestReservation` path zero-initializes the result and returns reservation bounds, leaving `is_committed` clear; tracked allocations and committed host regions set it. A per-page query can preserve the current `ENOMEM` validation while filling each `vec` byte from `is_committed` (with the debt entry retaining that actual page residency is not modeled). A focused test could reserve one page, confirm it reports 0, then map/commit it and confirm it reports 1.
adriadam10 commented 2026-10-14 13:06:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T13:06:04Z.


Checked against FreeBSD 11.4 kern_mincore and the review points above; pushed one commit on top with the fixes:

  • Range (ENOMEM) and mapping checks now come before the null-vec EFAULT.
  • length == 0 returns 0 only when the start page is mapped, ENOMEM otherwise.
  • end is computed from the truncated address, so vec gets exactly (end - first) / page bytes (test checks the next byte stays untouched); dead end <= first branch removed.
  • Pages that are only reserved (is_committed clear) report 0, committed ones MINCORE_INCORE; tests cover it. Debt entry updated.
    guest_memory passes on the merge with main and fails to link without the change; Windows run by @pipe1os confirms the loader gets past +hFjyohmOU0. Merging.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6169049594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5643#issuecomment-6169049594). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T13:06:04Z. --- Checked against FreeBSD 11.4 `kern_mincore` and the review points above; pushed one commit on top with the fixes: - Range (`ENOMEM`) and mapping checks now come before the null-`vec` `EFAULT`. - `length == 0` returns 0 only when the start page is mapped, `ENOMEM` otherwise. - `end` is computed from the truncated address, so `vec` gets exactly `(end - first) / page` bytes (test checks the next byte stays untouched); dead `end <= first` branch removed. - Pages that are only reserved (`is_committed` clear) report 0, committed ones `MINCORE_INCORE`; tests cover it. Debt entry updated. `guest_memory` passes on the merge with main and fails to link without the change; Windows run by @pipe1os confirms the loader gets past `+hFjyohmOU0`. Merging.
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!5643
No description provided.