fix(libkernel_sys): answer sceKernelGetPhysPageSize for the mapping holding an address #17936

Open
kali113 wants to merge 4 commits from upstream-pr/17936 into main
kali113 commented 2026-11-09 23:56:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kali113 on 2026-11-09T23:56:00Z.


What

sceKernelGetPhysPageSize took no arguments and returned 16 KiB (#10335, which noted that console behaviour was unverified and that the export is a thin wrapper of syscall 0x2c1, 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 holds address. A caller of the old no-argument form read an int that was never what a console returns, and never got its output pointer written.

address console
direct memory, 2 MiB aligned virtual and physical, mapped as a whole 2 MiB granule (4, 6 MiB mappings; 2 MiB + 16 KiB) 0x200000 for each whole granule
the 16 KiB tail after the last whole granule; a mapping smaller than 2 MiB (16 KiB, 64 KiB, 1 MiB); 16 KiB of a 2 MiB allocation 0x1000
a mapping at a virtual address 16 KiB past a granule boundary, physical offset 0 0x1000 everywhere
the same with physical offset 16 KiB: the granule that lines up in both 0x200000 for that granule only, 0x1000 for the rest
virtual aligned, physical offset 16 KiB 0x1000
two adjacent 2 MiB mappings of one physical block 0x200000 each
a 2 MiB granule after sceKernelMprotect of 16 KiB inside it 0x1000 for that granule, 0x200000 for the next
stack, created-thread stack, executable images, globals, libc heap 0x1000
flexible, system flexible, anonymous (private, shared, PROT_NONE), file-backed (shared, private) and reserved ranges 0x80020016, errno 22
NULL, 0x1000, a kernel address 0x80020016, errno 22

Argument order: the address is checked first (0x80020016, errno 22, whatever pageSize is), then pageSize == NULL (0x80020016), then writing it (0x8002000e, errno 14, for 0x10, a kernel address or a write that straddles into unmapped memory). Success returns 0, leaves errno alone and writes 8 bytes. The contents behind address are ignored.

The 2 MiB rule is the one GuestPageTableUsage already applies for sceKernelGetPageTableStats, 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 new DirectMappingPageSize applies it to one address, and GuestMappingPageSize_nid_no_patch classifies the address (reservation, registered flexible range, direct mapping, host memory) so libkernel_sys imports one extern "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 guest malloc blocks (a PS5 answers 0x1000; here they answer EINVAL if the registry holds them as flexible memory; the test cannot create one because the guest heap needs process parameters).

Tested

  • Linux x86-64 (GCC 15.2, Rosetta): new guest_kernel_sys_phys_page_size passes (the direct-mapping scenarios above, the other memory kinds, the argument order); guest_kernel_sys and guest_page_table_stats still pass.
  • PS5 firmware 13.590.001: the measurements above, with a payload (probe source and raw output in the first comment). Not run with a title.
  • python3 tools/check_conventions.py --base origin/main and tools/pr_overlap.py pass.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5233204594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17936). Originally posted by [kali113](https://github.com/kali113) on 2026-11-09T23:56:00Z. --- ### What `sceKernelGetPhysPageSize` took no arguments and returned 16 KiB (#10335, which noted that console behaviour was unverified and that the export is a thin wrapper of syscall `0x2c1`, 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 holds `address`. A caller of the old no-argument form read an `int` that was never what a console returns, and never got its output pointer written. | `address` | console | |---|---| | direct memory, 2 MiB aligned virtual and physical, mapped as a whole 2 MiB granule (4, 6 MiB mappings; 2 MiB + 16 KiB) | `0x200000` for each whole granule | | the 16 KiB tail after the last whole granule; a mapping smaller than 2 MiB (16 KiB, 64 KiB, 1 MiB); 16 KiB of a 2 MiB allocation | `0x1000` | | a mapping at a virtual address 16 KiB past a granule boundary, physical offset 0 | `0x1000` everywhere | | the same with physical offset 16 KiB: the granule that lines up in both | `0x200000` for that granule only, `0x1000` for the rest | | virtual aligned, physical offset 16 KiB | `0x1000` | | two adjacent 2 MiB mappings of one physical block | `0x200000` each | | a 2 MiB granule after `sceKernelMprotect` of 16 KiB inside it | `0x1000` for that granule, `0x200000` for the next | | stack, created-thread stack, executable images, globals, libc heap | `0x1000` | | flexible, system flexible, anonymous (private, shared, `PROT_NONE`), file-backed (shared, private) and reserved ranges | `0x80020016`, errno 22 | | NULL, `0x1000`, a kernel address | `0x80020016`, errno 22 | Argument order: the address is checked first (`0x80020016`, errno 22, whatever `pageSize` is), then `pageSize == NULL` (`0x80020016`), then writing it (`0x8002000e`, errno 14, for `0x10`, a kernel address or a write that straddles into unmapped memory). Success returns 0, leaves errno alone and writes 8 bytes. The contents behind `address` are ignored. The 2 MiB rule is the one `GuestPageTableUsage` already applies for `sceKernelGetPageTableStats`, 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 new `DirectMappingPageSize` applies it to one address, and `GuestMappingPageSize_nid_no_patch` classifies the address (reservation, registered flexible range, direct mapping, host memory) so `libkernel_sys` imports one `extern "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 guest `malloc` blocks (a PS5 answers `0x1000`; here they answer `EINVAL` if the registry holds them as flexible memory; the test cannot create one because the guest heap needs process parameters). ### Tested - Linux x86-64 (GCC 15.2, Rosetta): new `guest_kernel_sys_phys_page_size` passes (the direct-mapping scenarios above, the other memory kinds, the argument order); `guest_kernel_sys` and `guest_page_table_stats` still pass. - PS5 firmware 13.590.001: the measurements above, with a payload (probe source and raw output in the first comment). Not run with a title. - `python3 tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py` pass. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-11-09 23:56:11 +00:00 (Migrated from github.com)

Imported 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5834568838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17936#pullrequestreview-5834568838). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) 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.
MarcZX10 commented 2026-11-17 03:34:11 +00:00 (Migrated from github.com)

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, base main). Read the description, commit list, manifest and diff for all nine files in reviewed; excluded is empty. No build and no test run in this review.

Head context read through the reader:

repo_read.py search sceKernelGetPhysPageSize
repo_read.py search GuestAllocationsLookup_nid_postfix
repo_read.py search g_protections
repo_read.py search GuestThreadStack
repo_read.py search GuestReservation
repo_read.py search releasable
repo_read.py search GuestCopyOut_nid_no_patch
repo_read.py search "sceKernelVirtualQuery("
repo_read.py search SceKernelError core/libs/prx/libkernel_sys
repo_read.py search SCE_KERNEL_ERROR_EFAULT core/libs/prx/libkernel
repo_read.py cat core/libs/prx/libc/src/GuestAllocations.cpp 850 877
repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1080 1100
repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1685 1766
repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1710 1728
repo_read.py cat core/libs/prx/libkernel/DirectMemory/Export.cpp 386 420
repo_read.py cat core/libs/prx/libkernel_sys/include/Api.hpp 1 12

Results

Check Result Note
Changed code, 9 files passed Read as hunks in the diff
Callers and helpers used by the new code passed Names and signatures match
2 MiB rule against GuestPageTableUsage passed Same granule, mapping and protection test, read by hand
Build and guest_kernel_sys_phys_page_size not run Author reports a Linux GCC 15.2 pass
Console answers (firmware 13.590.001) not run Author's payload measurements
check_conventions.py, pr_overlap.py not run Author reports a pass

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1776: DirectMappingPageSize reads the direct mapping under g_directLock, releases it, and then reads the protection table under g_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

Not verified

  • The build and the new test were not run. Several test helpers (sceKernelIsStack, sceKernelMapNamedFlexibleMemory, scePthreadYield, sceKernelReleaseDirectMemory) were not checked against their declarations.
  • The console table, the malloc case and the PS5 payload output are the author's measurements. They were not checked here.
  • EINVAL for flexible, anonymous and file-backed ranges depends on those ranges being registered with releasable set. I read how releasable is used, not how anonymous and file mappings are registered.
  • The branch without a registry range (Export.cpp#L700) never consults QueryDirectMapping. I did not confirm that every direct mapping has a registry entry; if one does not, it reports 4 KiB.
  • tools/check_conventions.py and tools/pr_overlap.py: author-reported, not run.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5932366755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17936#pullrequestreview-5932366755). Originally posted by [MarcZX10](https://github.com/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, base `main`). Read the description, commit list, manifest and diff for all nine files in `reviewed`; `excluded` is empty. No build and no test run in this review. Head context read through the reader: ``` repo_read.py search sceKernelGetPhysPageSize repo_read.py search GuestAllocationsLookup_nid_postfix repo_read.py search g_protections repo_read.py search GuestThreadStack repo_read.py search GuestReservation repo_read.py search releasable repo_read.py search GuestCopyOut_nid_no_patch repo_read.py search "sceKernelVirtualQuery(" repo_read.py search SceKernelError core/libs/prx/libkernel_sys repo_read.py search SCE_KERNEL_ERROR_EFAULT core/libs/prx/libkernel repo_read.py cat core/libs/prx/libc/src/GuestAllocations.cpp 850 877 repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1080 1100 repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1685 1766 repo_read.py cat core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp 1710 1728 repo_read.py cat core/libs/prx/libkernel/DirectMemory/Export.cpp 386 420 repo_read.py cat core/libs/prx/libkernel_sys/include/Api.hpp 1 12 ``` ## Results | Check | Result | Note | |---|---|---| | Changed code, 9 files | passed | Read as hunks in the diff | | Callers and helpers used by the new code | passed | Names and signatures match | | 2 MiB rule against `GuestPageTableUsage` | passed | Same granule, mapping and protection test, read by hand | | Build and `guest_kernel_sys_phys_page_size` | not run | Author reports a Linux GCC 15.2 pass | | Console answers (firmware 13.590.001) | not run | Author's payload measurements | | `check_conventions.py`, `pr_overlap.py` | not run | Author reports a pass | ## Findings ### Blocking None. ### Non-blocking - `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp:1776`: `DirectMappingPageSize` reads the direct mapping under `g_directLock`, releases it, and then reads the protection table under `g_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 - Wrapper: https://github.com/boykopovar/AnyPS5/blob/9b0807b48711d5b681206ea605de8794940a0491/core/libs/prx/libkernel_sys/Export.Discovery.cpp#L886-L892 - `GuestMappingPageSize_nid_no_patch`: https://github.com/boykopovar/AnyPS5/blob/9b0807b48711d5b681206ea605de8794940a0491/core/libs/prx/libkernel/DirectMemory/Export.cpp#L677-L704 - `DirectMappingPageSize`: https://github.com/boykopovar/AnyPS5/blob/9b0807b48711d5b681206ea605de8794940a0491/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp#L1769-L1784 - `GuestPageTableUsage` rule: https://github.com/boykopovar/AnyPS5/blob/9b0807b48711d5b681206ea605de8794940a0491/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp#L1710-L1728 - `sceKernelVirtualQuery`: https://github.com/boykopovar/AnyPS5/blob/9b0807b48711d5b681206ea605de8794940a0491/core/libs/prx/libkernel/DirectMemory/Export.cpp#L389-L420 ## Not verified - The build and the new test were not run. Several test helpers (`sceKernelIsStack`, `sceKernelMapNamedFlexibleMemory`, `scePthreadYield`, `sceKernelReleaseDirectMemory`) were not checked against their declarations. - The console table, the malloc case and the PS5 payload output are the author's measurements. They were not checked here. - `EINVAL` for flexible, anonymous and file-backed ranges depends on those ranges being registered with `releasable` set. I read how `releasable` is used, not how anonymous and file mappings are registered. - The branch without a registry range (`Export.cpp#L700`) never consults `QueryDirectMapping`. I did not confirm that every direct mapping has a registry entry; if one does not, it reports 4 KiB. - `tools/check_conventions.py` and `tools/pr_overlap.py`: author-reported, not run. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 04:52:11 +00:00 (Migrated from github.com)

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 EINVAL for valid addresses inside the guest heap.

What I ran

Reviewed head f4f5ca539042e8e9d630a480b5a581bab9174f81 using repo_read.py. No build or tests were run in this review.

Results

Check Result Note
Source review failed Heap lookup is not handled
Build and tests not run No commands run

Findings

Blocking

  • core/libs/prx/libkernel/DirectMemory/Export.cpp:805 treats any registered guest allocation that is not a direct mapping as eligible for the default EINVAL result, except releasable ranges accepted as thread stacks. The description explicitly says guest malloc blocks should return 0x1000 on console, but that these currently return EINVAL when 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; preserve EINVAL for 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, and docs/dev/debt/libkernel_sys.md. No changed files were excluded from the manifest.

To get this to a good state

  • Handle valid guest heap allocations in GuestMappingPageSize_nid_no_patch so the function returns the measured 4 KiB page size for them; add coverage for that behavior.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002925047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17936#pullrequestreview-6002925047). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:52:11Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: avoid reporting `EINVAL` for valid addresses inside the guest heap. ## What I ran Reviewed head `f4f5ca539042e8e9d630a480b5a581bab9174f81` using `repo_read.py`. No build or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Source review | failed | Heap lookup is not handled | | Build and tests | not run | No commands run | ## Findings ### Blocking - `core/libs/prx/libkernel/DirectMemory/Export.cpp:805` treats any registered guest allocation that is not a direct mapping as eligible for the default `EINVAL` result, except releasable ranges accepted as thread stacks. The description explicitly says guest `malloc` blocks should return `0x1000` on console, but that these currently return `EINVAL` when 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; preserve `EINVAL` for 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`, and `docs/dev/debt/libkernel_sys.md`. No changed files were excluded from the manifest. ## To get this to a good state - Handle valid guest heap allocations in `GuestMappingPageSize_nid_no_patch` so the function returns the measured 4 KiB page size for them; add coverage for that behavior.
ligmaSec commented 2026-12-03 13:36:20 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170835371 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17936#pullrequestreview-6170835371). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:36:20Z. **Review:** COMMENTED --- The Linux integration build passed the registered physical page-size CTest. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/17936:upstream-pr/17936'
git switch 'upstream-pr/17936'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/17936'
git switch 'upstream-pr/17936'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/17936'
git switch 'upstream-pr/17936'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/17936'
git switch 'main'
git merge --squash 'upstream-pr/17936'
git switch 'main'
git merge --ff-only 'upstream-pr/17936'
git switch 'main'
git merge 'upstream-pr/17936'
git push origin 'main'
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!17936
No description provided.