fix(libkernel_sys): answer sceKernelGetPhysPageSize for the mapping holding an address #17936
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17936"
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 kali113 on 2026-11-09T23:56:00Z.
What
sceKernelGetPhysPageSizetook no arguments and returned 16 KiB (#10335, which noted that console behaviour was unverified and that the export is a thin wrapper of syscall0x2c1, per @BunnyFl's disassembly). On a PS5 payload (firmware 13.590.001) it takes(const void* address, uint64_t* pageSize)and reports the page size of the mapping that holdsaddress. A caller of the old no-argument form read anintthat was never what a console returns, and never got its output pointer written.address0x200000for each whole granule0x10000x1000everywhere0x200000for that granule only,0x1000for the rest0x10000x200000eachsceKernelMprotectof 16 KiB inside it0x1000for that granule,0x200000for the next0x1000PROT_NONE), file-backed (shared, private) and reserved ranges0x80020016, errno 220x1000, a kernel address0x80020016, errno 22Argument order: the address is checked first (
0x80020016, errno 22, whateverpageSizeis), thenpageSize == NULL(0x80020016), then writing it (0x8002000e, errno 14, for0x10, a kernel address or a write that straddles into unmapped memory). Success returns 0, leaves errno alone and writes 8 bytes. The contents behindaddressare ignored.The 2 MiB rule is the one
GuestPageTableUsagealready applies forsceKernelGetPageTableStats, so the two stay in step: a granule counts as a large page when it lies wholly inside one direct mapping, one protection range, and its physical offset is 2 MiB aligned. The newDirectMappingPageSizeapplies it to one address, andGuestMappingPageSize_nid_no_patchclassifies the address (reservation, registered flexible range, direct mapping, host memory) solibkernel_sysimports oneextern "C"function.The debt entry replaces "returns
PS5_PAGE_SIZE, console behaviour is unverified". It records what is not covered: a granule spanned by two adjacent mappings or memory types, and guestmallocblocks (a PS5 answers0x1000; here they answerEINVALif the registry holds them as flexible memory; the test cannot create one because the guest heap needs process parameters).Tested
guest_kernel_sys_phys_page_sizepasses (the direct-mapping scenarios above, the other memory kinds, the argument order);guest_kernel_sysandguest_page_table_statsstill pass.python3 tools/check_conventions.py --base origin/mainandtools/pr_overlap.pypass.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 repositoryImported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-09T23:56:11Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T03:34:11Z.
Review: APPROVED
Reviewed sceKernelGetPhysPageSize fix — looks good
Nothing blocks this; the points below are optional. I found no blocking defect in the code I read; the console and build results are the author's, not mine.
What I ran
Head:
9b0807b48711d5b681206ea605de8794940a0491(PR 17936, basemain). Read the description, commit list, manifest and diff for all nine files inreviewed;excludedis empty. No build and no test run in this review.Head context read through the reader:
Results
GuestPageTableUsageguest_kernel_sys_phys_page_sizecheck_conventions.py,pr_overlap.pyFindings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1776:DirectMappingPageSizereads the direct mapping underg_directLock, releases it, and then reads the protection table underg_protectionLock. A munmap or mprotect between the two reads can give a size that matches neither state. A caller racing with unmap gets a stale answer, not a crash. Holding one lock across both reads would remove it, or the debt entry could say the answer is a snapshot.docs/dev/debt/libkernel_sys.md:367: the new entry ends with "Unmeasured: ..." and no full stop, and its first sentence reads as a list of unrelated facts. It is correct; the wording could be tidier.Sources
github.com/boykopovar/AnyPS5@9b0807b487/core/libs/prx/libkernel_sys/Export.Discovery.cpp (L886-L892)GuestMappingPageSize_nid_no_patch:github.com/boykopovar/AnyPS5@9b0807b487/core/libs/prx/libkernel/DirectMemory/Export.cpp (L677-L704)DirectMappingPageSize:github.com/boykopovar/AnyPS5@9b0807b487/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp (L1769-L1784)GuestPageTableUsagerule:github.com/boykopovar/AnyPS5@9b0807b487/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp (L1710-L1728)sceKernelVirtualQuery:github.com/boykopovar/AnyPS5@9b0807b487/core/libs/prx/libkernel/DirectMemory/Export.cpp (L389-L420)Not verified
sceKernelIsStack,sceKernelMapNamedFlexibleMemory,scePthreadYield,sceKernelReleaseDirectMemory) were not checked against their declarations.EINVALfor flexible, anonymous and file-backed ranges depends on those ranges being registered withreleasableset. I read howreleasableis used, not how anonymous and file mappings are registered.Export.cpp#L700) never consultsQueryDirectMapping. I did not confirm that every direct mapping has a registry entry; if one does not, it reports 4 KiB.tools/check_conventions.pyandtools/pr_overlap.py: author-reported, not run.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:52:11Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: avoid reporting
EINVALfor valid addresses inside the guest heap.What I ran
Reviewed head
f4f5ca539042e8e9d630a480b5a581bab9174f81usingrepo_read.py. No build or tests were run in this review.Results
Findings
Blocking
core/libs/prx/libkernel/DirectMemory/Export.cpp:805treats any registered guest allocation that is not a direct mapping as eligible for the defaultEINVALresult, except releasable ranges accepted as thread stacks. The description explicitly says guestmallocblocks should return0x1000on console, but that these currently returnEINVALwhen registered as flexible memory. The function therefore returns the wrong result for valid heap addresses, a common case for callers querying the size of their own buffers. Classify heap allocations as ordinary mapped memory and return the small page size for them; preserveEINVALfor the memory categories measured to be invalid.Non-blocking
None.
Not verified
No tests, build, console behavior, or CI checks were run by this reviewer. The author reports Linux tests and PS5 firmware measurements in the description; those results were not independently verified. Read all changed files listed in the manifest:
core/libs/CMakeLists.txt,core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp,core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/prx/libkernel_sys/Export.Discovery.cpp,core/libs/prx/libkernel_sys/include/Api.hpp,core/libs/tests/GuestKernelSys.cpp,core/libs/tests/GuestKernelSysPhysPageSize.cpp, anddocs/dev/debt/libkernel_sys.md. No changed files were excluded from the manifest.To get this to a good state
GuestMappingPageSize_nid_no_patchso the function returns the measured 4 KiB page size for them; add coverage for that behavior.Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:36:20Z.
Review: COMMENTED
The Linux integration build passed the registered physical page-size CTest.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.