Repository navigation
Conversation
…olding an address The export took no arguments and returned 16 KiB. A PS5 payload on firmware 13.590.001 shows it takes (const void* address, uint64_t* pageSize) and reports the page size of the mapping holding the address: 2 MiB for a 2 MiB granule wholly inside one direct-memory mapping with an aligned physical offset, 4 KiB otherwise, and EINVAL for flexible, anonymous, file and reserved ranges and for unmapped addresses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe sources and raw output for the table (payload built with ps5-payload-sdk v0.45, run on firmware 13.590.001). main4.c and its output#include "../common/probe.h"
#include <sys/mman.h>
extern long sceKernelGetPhysPageSize();
extern int sceKernelAllocateDirectMemory(long, long, unsigned long, unsigned long, unsigned long, long*);
extern int sceKernelMapDirectMemory(void**, unsigned long, int, int, long, unsigned long, unsigned long);
extern int sceKernelMapNamedFlexibleMemory(void**, unsigned long, int, int, const char*);
static unsigned long Out;
static void q(const char* t, void* addr) {
Out = 0xA5A5A5A5A5A5A5A5ul; errno = 0;
long r = sceKernelGetPhysPageSize(addr, &Out);
logf_(" %-40s addr=%p -> rax=0x%lx errno=%d out=0x%lx", t, addr, r, errno, Out);
}
int main(void) {
probe_start("getprobe4", 30000);
int local; q("stack", &local);
void* h = malloc(100); q("malloc 100", h);
void* hb = malloc(4 << 20); q("malloc 4MiB", hb);
void* m = mmap(0, 1 << 20, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); logf_(" mmap 1MiB -> %p", m);
q("mmap anon 1MiB start", m); q("mmap anon 1MiB +0x1000", (char*)m + 0x1000); q("mmap anon 1MiB end-1", (char*)m + (1 << 20) - 1);
q("just past mmap end", (char*)m + (1 << 20));
q("main code", (void*)main);
q("addr 1", (void*)1); q("addr 0x1000", (void*)0x1000); q("addr 0x7ffffff00000", (void*)0x7ffffff00000); q("kernel addr", (void*)0xffffffff80000000ul); q("NULL", 0);
long dm = -1; int r = sceKernelAllocateDirectMemory(0, 0x600000000ul, 0x400000, 0x200000, 3, &dm);
logf_(" AllocateDirectMemory -> 0x%x off=0x%lx", r, dm);
if (r == 0) {
void* da = 0; r = sceKernelMapDirectMemory(&da, 0x400000, 3, 0, dm, 0x200000, 0);
logf_(" MapDirectMemory -> 0x%x at %p", r, da);
if (r == 0) { q("direct 4MiB start", da); q("direct +2MiB", (char*)da + 0x200000); q("direct +0x1234", (char*)da + 0x1234); }
}
void* fa = 0; r = sceKernelMapNamedFlexibleMemory(&fa, 0x100000, 3, 0, "probe");
logf_(" MapNamedFlexibleMemory -> 0x%x at %p", r, fa);
if (r == 0) { q("flexible start", fa); q("flexible +0x4000", (char*)fa + 0x4000); }
probe_finish("getprobe4");
_exit(0);
}main5.c and its output#include "../common/probe.h"
#include <sys/mman.h>
#include <fcntl.h>
extern long sceKernelGetPhysPageSize();
extern int sceKernelAllocateDirectMemory(long, long, unsigned long, unsigned long, int, long*);
extern int sceKernelMapDirectMemory(void**, unsigned long, int, int, long, unsigned long);
extern int sceKernelMapDirectMemory2(void**, unsigned long, int, int, int, long, unsigned long);
extern int sceKernelReserveVirtualRange(void**, unsigned long, int, unsigned long);
extern int sceKernelMapNamedSystemFlexibleMemory(void**, unsigned long, int, int, const char*);
extern int sceKernelMunmap(void*, unsigned long);
static unsigned long Out;
static void q(const char* t, void* addr) {
Out = 0xA5A5A5A5A5A5A5A5ul; errno = 0;
long r = sceKernelGetPhysPageSize(addr, &Out);
if (r == 0) logf_(" %-44s %p -> 0x%lx", t, addr, Out); else logf_(" %-44s %p -> err 0x%lx", t, addr, r);
}
static void* thr(void* a) { int x; q("created thread stack", &x); return 0; }
static void dm(const char* t, unsigned long size, unsigned long align, int type, unsigned long mapsz, int flags) {
long off = -1; int r = sceKernelAllocateDirectMemory(0, 0x600000000ul, size, align, type, &off);
if (r) { logf_(" alloc %s: 0x%x", t, r); return; }
void* a = 0; r = sceKernelMapDirectMemory(&a, mapsz, 3, flags, off, align);
if (r) { logf_(" map %s: 0x%x", t, r); return; }
logf_(" [%s: size 0x%lx align 0x%lx type %d] mapped at %p", t, size, align, type, a);
q(" start", a); q(" end-1", (char*)a + mapsz - 1);
sceKernelMunmap(a, mapsz);
}
int main(void) {
probe_start("getprobe5", 40000);
dm("4k-ish 16K", 0x4000, 0x4000, 3, 0x4000, 0);
dm("64K", 0x10000, 0x10000, 3, 0x10000, 0);
dm("1M a64K", 0x100000, 0x10000, 3, 0x100000, 0);
dm("2M a2M", 0x200000, 0x200000, 3, 0x200000, 0);
dm("2M a2M type0", 0x200000, 0x200000, 0, 0x200000, 0);
dm("2M a2M type10", 0x200000, 0x200000, 10, 0x200000, 0);
dm("6M a2M map 6M", 0x600000, 0x200000, 3, 0x600000, 0);
dm("2M a2M mapped 16K of it", 0x200000, 0x200000, 3, 0x4000, 0);
void* rv = 0; int r = sceKernelReserveVirtualRange(&rv, 0x400000, 0, 0x200000);
logf_(" reserve -> 0x%x %p", r, rv);
if (r == 0) q("reserved range (unbacked)", rv);
void* sf = 0; r = sceKernelMapNamedSystemFlexibleMemory(&sf, 0x100000, 3, 0, "p");
logf_(" sysflex -> 0x%x %p", r, sf);
if (r == 0) q("system flexible", sf);
void* mp = mmap(0, 0x4000, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); q("mmap PROT_NONE", mp);
void* ms = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); q("mmap anon shared", ms);
int fd = open("/data/kprobe_pp.bin", O_RDWR | O_CREAT | O_TRUNC, 0644); ftruncate(fd, 0x8000);
void* mf = mmap(0, 0x8000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); q("mmap file shared", mf);
void* mfp = mmap(0, 0x8000, PROT_READ, MAP_PRIVATE, fd, 0); q("mmap file private", mfp);
close(fd); unlink("/data/kprobe_pp.bin");
static char gdata[16]; q("global data", gdata);
q("libc function", (void*)printf); q("libkernel func", (void*)sceKernelGetPhysPageSize);
pthread_t t; pthread_create(&t, 0, thr, 0); pthread_join(t, 0);
probe_finish("getprobe5");
_exit(0);
}main6.c and its output#include "../common/probe.h"
#include <sys/mman.h>
extern long sceKernelGetPhysPageSize();
extern int sceKernelAllocateDirectMemory(long, long, unsigned long, unsigned long, int, long*);
extern int sceKernelMapDirectMemory(void**, unsigned long, int, int, long, unsigned long);
extern int sceKernelMunmap(void*, unsigned long);
extern int sceKernelReserveVirtualRange(void**, unsigned long, int, unsigned long);
static unsigned long Out;
static void q(const char* t, void* addr) {
Out = 0; errno = 0;
long r = sceKernelGetPhysPageSize(addr, &Out);
if (r == 0) logf_(" %-34s %p -> 0x%lx", t, addr, Out); else logf_(" %-34s %p -> err 0x%lx", t, addr, r);
}
int main(void) {
probe_start("getprobe6", 40000);
long off = -1;
int r = sceKernelAllocateDirectMemory(0, 0x600000000ul, 0x800000, 0x200000, 3, &off);
logf_("alloc 8M a2M -> 0x%x phys=0x%lx", r, off);
void* rv = 0; r = sceKernelReserveVirtualRange(&rv, 0x1000000, 0, 0x200000); logf_("reserve 16M -> 0x%x %p", r, rv);
char* base = (char*)rv;
void* a;
logf_("A: map 2M+16K at base, phys+0");
a = base; r = sceKernelMapDirectMemory(&a, 0x204000, 3, 0x10, off, 0x4000); logf_(" r=0x%x at %p", r, a);
if (r == 0) { q("granule0 start", base); q("granule0 last", base + 0x1fffff); q("tail start (+2M)", base + 0x200000); q("tail last", base + 0x203fff); sceKernelMunmap(base, 0x204000); }
logf_("B: map 4M at base+16K (virtual unaligned), phys+0");
a = base + 0x4000; r = sceKernelMapDirectMemory(&a, 0x400000, 3, 0x10, off, 0x4000); logf_(" r=0x%x at %p", r, a);
if (r == 0) { q("first 16K", base + 0x4000); q("granule1 start (base+2M)", base + 0x200000); q("granule1 last", base + 0x3fffff); q("last", base + 0x403fff); sceKernelMunmap(base + 0x4000, 0x400000); }
logf_("C: map 4M at base+16K, phys+16K (granule1 phys aligned)");
a = base + 0x4000; r = sceKernelMapDirectMemory(&a, 0x400000, 3, 0x10, off + 0x4000, 0x4000); logf_(" r=0x%x at %p", r, a);
if (r == 0) { q("first 16K", base + 0x4000); q("granule0 body", base + 0x100000); q("granule1 start (base+2M)", base + 0x200000); q("granule1 last", base + 0x3fffff); q("tail", base + 0x403fff); sceKernelMunmap(base + 0x4000, 0x400000); }
logf_("D: map 2M at base, phys+16K (virtual aligned, phys not)");
a = base; r = sceKernelMapDirectMemory(&a, 0x200000, 3, 0x10, off + 0x4000, 0x4000); logf_(" r=0x%x at %p", r, a);
if (r == 0) { q("start", base); q("middle", base + 0x100000); sceKernelMunmap(base, 0x200000); }
logf_("E: two adjacent maps of the same 4M phys block, 2M each");
a = base; r = sceKernelMapDirectMemory(&a, 0x200000, 3, 0x10, off, 0x4000); void* b2 = base + 0x200000; int r2 = sceKernelMapDirectMemory(&b2, 0x200000, 3, 0x10, off + 0x200000, 0x4000); logf_(" r=0x%x r2=0x%x", r, r2);
if (!r && !r2) { q("first", base); q("second", base + 0x200000); sceKernelMunmap(base, 0x400000); }
logf_("F: protection/type changes");
a = base; r = sceKernelMapDirectMemory(&a, 0x400000, 3, 0x10, off, 0x4000);
if (r == 0) { q("before", base); extern int sceKernelMprotect(void*, unsigned long, int); sceKernelMprotect(base + 0x4000, 0x4000, 1); q("after mprotect of 16K inside granule0", base); q("granule1", base + 0x200000); sceKernelMunmap(base, 0x400000); }
probe_finish("getprobe6");
_exit(0);
}main7.c and its output#include "../common/probe.h"
#include <sys/mman.h>
extern long sceKernelGetPhysPageSize();
static unsigned long Out[4];
static void q(const char* t, void* a, void* b) {
Out[0] = Out[1] = 0xA5A5A5A5A5A5A5A5ul; errno = 0x7777;
long r = sceKernelGetPhysPageSize(a, b);
logf_(" %-44s a=%p b=%p -> rax=0x%lx errno=%d out=%lx,%lx", t, a, b, r, errno, Out[0], Out[1]);
}
int main(void) {
probe_start("getprobe7", 20000);
void* flex = mmap(0, 0x4000, 3, MAP_ANON | MAP_PRIVATE, -1, 0);
int local;
q("valid a, valid b", &local, Out);
q("valid a, unaligned b (Out+1 byte)", &local, (char*)Out + 1);
q("valid a, NULL b", &local, 0);
q("valid a, b=0x10", &local, (void*)0x10);
q("valid a, b=code (read-only)", &local, (void*)main);
q("valid a, b=kernel addr", &local, (void*)0xffffffff80000000ul);
q("NULL a, valid b", 0, Out);
q("unmapped a, valid b", (void*)0x1000, Out);
q("unmapped a, b=0x10", (void*)0x1000, (void*)0x10);
q("unmapped a, NULL b", (void*)0x1000, 0);
q("NULL a, b=0x10", 0, (void*)0x10);
q("flexible a, valid b", flex, Out);
q("flexible a, b=0x10", flex, (void*)0x10);
q("flexible a, NULL b", flex, 0);
q("valid a, b = end of mapping (straddle)", &local, (char*)flex + 0x4000 - 4);
q("valid a, b straddles into unmapped", &local, (char*)flex + 0x4000 - 4);
probe_finish("getprobe7");
_exit(0);
}AI-assisted: yes |
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
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.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
@JJenkx the head of this PR is now AI-assisted: yes (Claude Code) |
MarcZX10
left a comment
There was a problem hiding this comment.
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: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
- Wrapper:
AnyPS5/core/libs/prx/libkernel_sys/Export.Discovery.cpp
Lines 886 to 892 in 9b0807b
GuestMappingPageSize_nid_no_patch:AnyPS5/core/libs/prx/libkernel/DirectMemory/Export.cpp
Lines 677 to 704 in 9b0807b
DirectMappingPageSize:AnyPS5/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
Lines 1769 to 1784 in 9b0807b
GuestPageTableUsagerule:AnyPS5/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
Lines 1710 to 1728 in 9b0807b
sceKernelVirtualQuery:AnyPS5/core/libs/prx/libkernel/DirectMemory/Export.cpp
Lines 389 to 420 in 9b0807b
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.
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.- The branch without a registry range (
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.
MarcZX10
left a comment
There was a problem hiding this comment.
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: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, 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_patchso the function returns the measured 4 KiB page size for them; add coverage for that behavior.
|
@kali113 I agree with MarcZX10's point on f4f5ca5. On main, Could heap ranges return |
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
ligmaSec
left a comment
There was a problem hiding this comment.
The Linux integration build passed the registered physical page-size CTest.
AI-assisted: yes.
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 repository