feat(libkernel): implement mincore #5643
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5643"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Amirtheshwaran on 2026-10-14T02:46:45Z.
What
Implement
mincore(+hFjyohmOU0) inlibkernel.prxfollowing FreeBSD 11'ssys_mincore(sys/vm/vm_mmap.c):trunc_page(address)).length == 0.EFAULT(14) ifvec == nullptr.ENOMEM/ 12).GuestRangeMapped(which queriessceKernelVirtualQuery); returnsENOMEM(12) if any page in the range is unmapped.vecwithMINCORE_INCORE(0x1) for mapped pages; residency, referenced and modified bits are not modelled.core/libs/tests/GuestMemory.cppcovering unaligned address rounding, null vector with non-zero length, overflow, partial unmapped ranges, and fully unmapped ranges.docs/dev/debt/libkernel.md.Target context: Java 25 LAN / DNS (PPSA99869, #5501) reaches
+hFjyohmOU0as documented by @karimibrahim07's relink tests on #5563: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
guest_memory_testswith CTest: 100% passed (1/1 Test #965: guest_memory ..................... Passed).python tools/check_conventions.py).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
mincore_nid_postfixImported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T03:38:38Z.
Static review (head
d4e55f710, read against FreeBSD 11.4sys/vm/vm_mmap.c,release/11.4.0,kern_mincorelines 750-787); I did not build or run this PR. Two differences from FreeBSD, in the order of the checks: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_mincorestill looks up the start address when the length is 0 (end = addr + round_page(len), thenvm_map_lookup_entry(map, addr, &entry)fails withENOMEM, lines 772-787), somincoreon an unmapped address with length 0 answersENOMEMthere.vec == NULL: the PR answersEFAULTbefore it checks the range and the mapping (if (vec == nullptr) return failed(GuestFault);comes beforeGuestRangeMapped). In FreeBSD the range and mapping checks (ENOMEM) come first;vecis only written afterwards, so a NULLvecover an unmapped range givesENOMEM, notEFAULT.The residency bits (referenced/modified) that FreeBSD reports are not set; the PR says so in its debt entry.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T05:09:16Z.
I independently checked the FreeBSD 11.4
releng/11.4implementation (sys/vm/vm_mmap.c,kern_mincore): it rounds the address and computesendeven whenlen == 0, then performsvm_map_lookup_entrybefore the page loop. Thus zero length succeeds for a mapped starting page but returnsENOMEMwhen the starting page is unmapped. The mapping/contiguity checks also precedesubyte(vec + ...), so a NULL vector over an unmapped range returnsENOMEMbeforeEFAULT. This confirms the two ordering differences called out above.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.
main)docs/dev/debt/libkernel.mdL167-168Imported from boykopovar/AnyPS5. Originally posted by pipe1os on 2026-10-14T05:42:06Z.
Built and ran this on Windows: head
d4e55f710merged ontomainf5424b4db(no conflicts), Windows 11, WinLibs GCC 15.2.0, Ninja Release.guest_memorypasses. WithPosixMemory.cppput back tomainand the test kept, it doesn't link (undefined reference to mincore_nid_postfix).+hFjyohmOU0and stops at the last missing import,KZ-4qlqlpmo(sigsuspend).One more difference on top of the two @xXPBSXx listed, about how many bytes of
vecget written. FreeBSD 11.0 truncates the address first and adds the rounded length to that (vm_mmap.cL762-L763), then writes exactly(end - first_addr) / PAGE_SIZEbytes (L964-L965). The PR roundsaddress + lengthinstead, so with an unaligned address it can write one byte more than a caller sizedvecfor. Local probe on the merge,vecpre-filled with 0x55:FreeBSD only writes
vec[0]there. The test'smincore_nid_postfix(memory + 1, page, mincoreVec)doesn't catch it becausemincoreVechas 4 bytes. Computingendfrom the truncated address, and checking thatvec[1]stays untouched in that case, would cover it.AI-assisted.
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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T07:04:23Z.
One useful implementation detail for the reserved-page case:
VirtualQueryInfoalready exposesis_committed. InsceKernelVirtualQuery, theGuestReservationpath zero-initializes the result and returns reservation bounds, leavingis_committedclear; tracked allocations and committed host regions set it. A per-page query can preserve the currentENOMEMvalidation while filling eachvecbyte fromis_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.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T13:06:04Z.
Checked against FreeBSD 11.4
kern_mincoreand the review points above; pushed one commit on top with the fixes:ENOMEM) and mapping checks now come before the null-vecEFAULT.length == 0returns 0 only when the start page is mapped,ENOMEMotherwise.endis computed from the truncated address, sovecgets exactly(end - first) / pagebytes (test checks the next byte stays untouched); deadend <= firstbranch removed.is_committedclear) report 0, committed onesMINCORE_INCORE; tests cover it. Debt entry updated.guest_memorypasses on the merge with main and fails to link without the change; Windows run by @pipe1os confirms the loader gets past+hFjyohmOU0. Merging.