Skip to content

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

Open
kali113 wants to merge 4 commits into
boykopovar:mainfrom
kali113:feat/kernel-sys-phys-page-size
Open

kali113 wants to merge 4 commits into
boykopovar:mainfrom
kali113:feat/kernel-sys-phys-page-size

Conversation

@kali113

@kali113 kali113 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

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

…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>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 23:56
@kali113

kali113 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Probe sources and raw output for the table (payload built with ps5-payload-sdk v0.45, run on firmware 13.590.001). sceKernelGetPhysPageSize is declared with no prototype and called as (address, &out).

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);
}
== getprobe4 start, firmware 13.590.001 ==
  stack                                    addr=7eeffbcb0 -> rax=0x0 errno=0 out=0x1000
  malloc 100                               addr=880009c40 -> rax=0x0 errno=0 out=0x1000
  malloc 4MiB                              addr=880070020 -> rax=0x0 errno=0 out=0x1000
  mmap 1MiB -> 200020000
  mmap anon 1MiB start                     addr=200020000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  mmap anon 1MiB +0x1000                   addr=200021000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  mmap anon 1MiB end-1                     addr=20011ffff -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  just past mmap end                       addr=200120000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  main code                                addr=200000000 -> rax=0x0 errno=0 out=0x1000
  addr 1                                   addr=1 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  addr 0x1000                              addr=1000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  addr 0x7ffffff00000                      addr=7ffffff00000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  kernel addr                              addr=ffffffff80000000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  NULL                                     addr=0 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  AllocateDirectMemory -> 0x0 off=0x2027a00000
  MapDirectMemory -> 0x0 at 200200000
  direct 4MiB start                        addr=200200000 -> rax=0x0 errno=0 out=0x200000
  direct +2MiB                             addr=200400000 -> rax=0x0 errno=0 out=0x200000
  direct +0x1234                           addr=200201234 -> rax=0x0 errno=0 out=0x200000
  MapNamedFlexibleMemory -> 0x0 at 200600000
  flexible start                           addr=200600000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
  flexible +0x4000                         addr=200604000 -> rax=0x80020016 errno=22 out=0xa5a5a5a5a5a5a5a5
== getprobe4 done ==
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);
}
== getprobe5 start, firmware 13.590.001 ==
  [4k-ish 16K: size 0x4000 align 0x4000 type 3] mapped at 200020000
     start                                     200020000 -> 0x1000
     end-1                                     200023fff -> 0x1000
  [64K: size 0x10000 align 0x10000 type 3] mapped at 200020000
     start                                     200020000 -> 0x1000
     end-1                                     20002ffff -> 0x1000
  [1M a64K: size 0x100000 align 0x10000 type 3] mapped at 200020000
     start                                     200020000 -> 0x1000
     end-1                                     20011ffff -> 0x1000
  [2M a2M: size 0x200000 align 0x200000 type 3] mapped at 200200000
     start                                     200200000 -> 0x200000
     end-1                                     2003fffff -> 0x200000
  [2M a2M type0: size 0x200000 align 0x200000 type 0] mapped at 200200000
     start                                     200200000 -> 0x200000
     end-1                                     2003fffff -> 0x200000
  map 2M a2M type10: 0x8002000d
  [6M a2M map 6M: size 0x600000 align 0x200000 type 3] mapped at 200200000
     start                                     200200000 -> 0x200000
     end-1                                     2007fffff -> 0x200000
  [2M a2M mapped 16K of it: size 0x200000 align 0x200000 type 3] mapped at 200200000
     start                                     200200000 -> 0x1000
     end-1                                     200203fff -> 0x1000
  reserve -> 0x0 200200000
  reserved range (unbacked)                    200200000 -> err 0x80020016
  sysflex -> 0x0 200020000
  system flexible                              200020000 -> err 0x80020016
  mmap PROT_NONE                               200120000 -> err 0x80020016
  mmap anon shared                             200124000 -> err 0x80020016
  mmap file shared                             200128000 -> err 0x80020016
  mmap file private                            200130000 -> err 0x80020016
  global data                                  200014020 -> 0x1000
  libc function                                8000bc5d0 -> 0x1000
  libkernel func                               800015e60 -> 0x1000
  created thread stack                         7eede3fb4 -> 0x1000
== getprobe5 done ==
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);
}
== getprobe6 start, firmware 13.590.001 ==
alloc 8M a2M -> 0x0 phys=0x2027a00000
reserve 16M -> 0x0 200200000
A: map 2M+16K at base, phys+0
  r=0x0 at 200200000
    granule0 start                     200200000 -> 0x200000
    granule0 last                      2003fffff -> 0x200000
    tail start (+2M)                   200400000 -> 0x1000
    tail last                          200403fff -> 0x1000
B: map 4M at base+16K (virtual unaligned), phys+0
  r=0x0 at 200204000
    first 16K                          200204000 -> 0x1000
    granule1 start (base+2M)           200400000 -> 0x1000
    granule1 last                      2005fffff -> 0x1000
    last                               200603fff -> 0x1000
C: map 4M at base+16K, phys+16K (granule1 phys aligned)
  r=0x0 at 200204000
    first 16K                          200204000 -> 0x1000
    granule0 body                      200300000 -> 0x1000
    granule1 start (base+2M)           200400000 -> 0x200000
    granule1 last                      2005fffff -> 0x200000
    tail                               200603fff -> 0x1000
D: map 2M at base, phys+16K (virtual aligned, phys not)
  r=0x0 at 200200000
    start                              200200000 -> 0x1000
    middle                             200300000 -> 0x1000
E: two adjacent maps of the same 4M phys block, 2M each
  r=0x0 r2=0x0
    first                              200200000 -> 0x200000
    second                             200400000 -> 0x200000
F: protection/type changes
    before                             200200000 -> 0x200000
    after mprotect of 16K inside granule0 200200000 -> 0x1000
    granule1                           200400000 -> 0x200000
== getprobe6 done ==
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);
}
== getprobe7 start, firmware 13.590.001 ==
  valid a, valid b                             a=7eeffbcf4 b=200014020 -> rax=0x0 errno=30583 out=1000,a5a5a5a5a5a5a5a5
  valid a, unaligned b (Out+1 byte)            a=7eeffbcf4 b=200014021 -> rax=0x0 errno=30583 out=1000a5,a5a5a5a5a5a5a500
  valid a, NULL b                              a=7eeffbcf4 b=0 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  valid a, b=0x10                              a=7eeffbcf4 b=10 -> rax=0x8002000e errno=14 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  valid a, b=code (read-only)                  a=7eeffbcf4 b=200000000 -> rax=0x0 errno=30583 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  valid a, b=kernel addr                       a=7eeffbcf4 b=ffffffff80000000 -> rax=0x8002000e errno=14 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  NULL a, valid b                              a=0 b=200014020 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  unmapped a, valid b                          a=1000 b=200014020 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  unmapped a, b=0x10                           a=1000 b=10 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  unmapped a, NULL b                           a=1000 b=0 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  NULL a, b=0x10                               a=0 b=10 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  flexible a, valid b                          a=200020000 b=200014020 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  flexible a, b=0x10                           a=200020000 b=10 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  flexible a, NULL b                           a=200020000 b=0 -> rax=0x80020016 errno=22 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  valid a, b = end of mapping (straddle)       a=7eeffbcf4 b=200023ffc -> rax=0x8002000e errno=14 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
  valid a, b straddles into unmapped           a=7eeffbcf4 b=200023ffc -> rax=0x8002000e errno=14 out=a5a5a5a5a5a5a5a5,a5a5a5a5a5a5a5a5
== getprobe7 done ==

AI-assisted: yes

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JJenkx

JJenkx commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Windows check of head daf771f459, merged onto main 5e04f680ad (2527f2b852, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_kernel_sys, guest_kernel_sys_phys_page_size pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

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
PR 18112 sceKernelGetPhysPageSize

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.

Pull request Shared files not compared
PR 7275 core/libs/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
docs/dev/debt/libkernel_sys.md
PR 12130 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 12642 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
docs/dev/debt/libkernel_sys.md
PR 15037 core/libs/prx/libkernel_sys/Export.Discovery.cpp
core/libs/prx/libkernel_sys/include/Api.hpp
core/libs/tests/GuestKernelSys.cpp
docs/dev/debt/libkernel_sys.md
PR 15040 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 16793 core/libs/CMakeLists.txt
PR 23395 core/libs/CMakeLists.txt
PR 23975 core/libs/CMakeLists.txt
PR 28264 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
core/libs/prx/libkernel/DirectMemory/Export.cpp

@kali113

kali113 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@JJenkx the head of this PR is now 9b0807b487, after changes since your last Windows check. When you have time, could you re-run guest_kernel_sys and guest_kernel_sys_phys_page_size on it?

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
    extern "C" int GuestMappingPageSize_nid_no_patch(const void* address, std::uint64_t* pageSize);
    int APS5_VABI sceKernelGetPhysPageSize(const void* address, std::uint64_t* pageSize) {
    const int error = GuestMappingPageSize_nid_no_patch(address, pageSize);
    if (error == 0) return 0;
    *__error_nid_postfix() = error;
    return SceKernelError(error);
  • GuestMappingPageSize_nid_no_patch:
    extern "C" int GuestMappingPageSize_nid_no_patch(const void* address, std::uint64_t* pageSize) {
    constexpr int GuestEfault = 14;
    constexpr int GuestEinval = 22;
    constexpr std::uint64_t SmallPage = 0x1000;
    const auto location = reinterpret_cast<std::uintptr_t>(address);
    std::uintptr_t reservedStart = 0;
    std::uintptr_t reservedEnd = 0;
    if (GuestReservation(location, &reservedStart, &reservedEnd)) return GuestEinval;
    std::uint64_t size = SmallPage;
    const auto range = GuestAllocations::GuestAllocationsLookup_nid_postfix(location, false);
    if (range != nullptr) {
    std::uintptr_t directStart = 0;
    std::uintptr_t directEnd = 0;
    std::uint64_t physicalOffset = 0;
    int memoryType = 0;
    if (QueryDirectMapping(location, &directStart, &directEnd, &physicalOffset, &memoryType)) size = DirectMappingPageSize(location);
    else if (range->releasable) {
    std::uintptr_t stackStart = 0;
    std::uintptr_t stackEnd = 0;
    if (!GuestThreadStack(location, &stackStart, &stackEnd)) return GuestEinval;
    }
    } else {
    VirtualQueryInfo info{};
    if (sceKernelVirtualQuery(address, 0, &info, sizeof(info)) != 0) return GuestEinval;
    }
    if (pageSize == nullptr) return GuestEinval;
    return GuestCopyOut_nid_no_patch(pageSize, &size, sizeof(size)) ? 0 : GuestEfault;
    }
  • DirectMappingPageSize:
    std::uint64_t DirectMappingPageSize(std::uintptr_t address) {
    constexpr std::uint64_t SmallPage = 0x1000;
    constexpr std::uint64_t LargePage = 0x200000;
    std::uintptr_t start = 0;
    std::uintptr_t end = 0;
    std::uint64_t phys = 0;
    int memoryType = 0;
    if (!QueryDirectMapping(address, &start, &end, &phys, &memoryType)) return SmallPage;
    const auto granule = address & ~static_cast<std::uintptr_t>(LargePage - 1);
    if (granule < start || granule + LargePage > end || (phys + (granule - start)) % LargePage != 0) return SmallPage;
    std::lock_guard lock(g_protectionLock);
    auto next = g_protections.upper_bound(granule);
    if (next == g_protections.begin()) return SmallPage;
    const auto containing = std::prev(next);
    return granule + LargePage <= containing->second.end ? LargePage : SmallPage;
    }
  • GuestPageTableUsage rule:
    void GuestPageTableUsage(std::uint64_t granularity, std::uint64_t* cpuEntries, std::uint64_t* gpuEntries) {
    constexpr std::uint64_t GpuDirectorySpan = 1ULL << 30;
    struct DirectSpan {
    std::uintptr_t start;
    std::uintptr_t end;
    std::uint64_t phys;
    };
    std::vector<DirectSpan> direct;
    {
    std::lock_guard lock(g_directLock);
    direct.reserve(g_directMappings.size());
    for (const auto& [start, mapping] : g_directMappings) direct.push_back({start, mapping.end, mapping.phys});
    }
    const auto largePage = [&](std::uintptr_t granule) {
    const auto next = std::upper_bound(direct.begin(), direct.end(), granule, [](std::uintptr_t address, const DirectSpan& span) { return address < span.start; });
    if (next == direct.begin()) return false;
    const auto& span = *std::prev(next);
    return granule + granularity <= span.end && (span.phys + (granule - span.start)) % granularity == 0;
    };
  • sceKernelVirtualQuery:
    int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInfo* info, uint64_t info_size) {
    if (!info || info_size < sizeof(VirtualQueryInfo)) return SCE_KERNEL_ERROR_EINVAL;
    memset(info, 0, sizeof(VirtualQueryInfo));
    const auto address = reinterpret_cast<uintptr_t>(addr);
    constexpr int findNext = 1;
    std::uintptr_t reservedStart = 0;
    std::uintptr_t reservedEnd = 0;
    if (GuestReservation(address, &reservedStart, &reservedEnd)) {
    info->start = reservedStart;
    info->end = reservedEnd;
    return 0;
    }
    const auto best = GuestAllocations::GuestAllocationsLookup_nid_postfix(address, (flags & findNext) != 0);
    if (best != nullptr) {
    info->start = best->address;
    info->end = best->address + best->bytes;
    info->protection = (best->readable ? 1 : 0) | (best->writable ? 2 : 0) | (!best->releasable ? 4 : 0);
    int recorded = 0;
    if (GuestProtection(std::max<uintptr_t>(address, info->start), &recorded)) info->protection = recorded;
    std::uintptr_t directStart = 0;
    std::uintptr_t directEnd = 0;
    std::uint64_t physicalOffset = 0;
    int memoryType = 0;
    const bool direct = QueryDirectMapping(std::max<uintptr_t>(address, info->start), &directStart, &directEnd, &physicalOffset, &memoryType);
    info->is_direct = direct ? 1u : 0u;
    info->is_flexible = !direct && best->releasable ? 1u : 0u;
    if (direct) {
    info->start = std::max(info->start, directStart);
    info->end = std::min(info->end, directEnd);
    info->memory_type = memoryType;
    }
    info->is_committed = 1;

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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@kali113 I agree with MarcZX10's point on f4f5ca5. On main, sceKernelGetPhysPageSize answers a page size for every address, so a title that queries its own malloc buffer gets a positive result. With this change, a heap block registered as flexible memory reaches the range->releasable branch in GuestMappingPageSize_nid_no_patch and gets EINVAL, while your debt note says the console answers 4 KiB there.

Could heap ranges return SmallPage before that branch, keeping EINVAL only for the flexible mappings you measured as invalid? If the registry cannot tell the libc heap apart from other flexible memory yet, the safer default for this PR is SmallPage for releasable ranges, with the EINVAL cases listed as debt.

@ycbuzi

ycbuzi commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Tested f4f5ca5390 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 3 tests failed out of 7483; 99% tests passed, 2 tests failed out of 7483. 3 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 7483; 99% tests passed, 1 tests failed out of 7483. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Linux integration build passed the registered physical page-size CTest.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants