feat(libkernel): optionally report direct memory as the game budget less the configured flexible memory #15064

Merged
JJenkx merged 4 commits from upstream-pr/15064 into main 2026-11-06 09:16:02 +00:00
JJenkx commented 2026-11-02 13:28:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-02T13:28:30Z.


What

sceKernelGetDirectMemorySize returns 13824 MiB today. #10809 changed it to the 12 GiB measured on a PS5 (inside Gran Turismo 7), and it was reverted in #12192 because ASTRO BOT (PPSA21564) allocates 12404 MiB of direct memory itself and then asserts.

This PR adds that model behind APS5_DIRECT_MEMORY_FROM_FLEXIBLE and keeps the 13824 MiB default (as asked in review: the model is not yet confirmed on a console). With the variable set, sceKernelGetDirectMemorySize reports 12736 MiB less the title's configured flexible-memory size (kernel.flexibleMemorySize in param.json, 448 MiB by default, already read for the flexible budget):

Declared flexible Direct reported (with the variable) Example
none (448 MiB default) 12288 MiB Gran Turismo 7 measured 12 GiB
328 MiB 12408 MiB ASTRO BOT allocates 12404 MiB
224 MiB 12512 MiB ASTRO's PLAYROOM, which needs at least 12318 MiB (#12192)
1024 MiB 11712 MiB Earthion, Blue Prince

With the variable set, sceKernelAllocateDirectMemory, sceKernelAllocateMainDirectMemory, sceKernelAvailableDirectMemorySize and sceKernelMemoryPoolExpand search no higher than that size. Without it nothing changes. The host backing stays at 13824 MiB either way.

Why this split

  • On firmware 13.42, sceKernelGetDirectMemorySize is not a constant: it returns a dmem ioctl's answer after reading the kern.app.memconf sysctl (BunnyFl's disassembly in #10809), so it is per process.
  • One shared 12736 MiB budget fits both known points: GT7 (default flexible) gets 12 GiB, and ASTRO BOT's 328 MiB declaration leaves it 12408 MiB, 4 MiB above the 12404 MiB it allocates on a console.
  • This is an inference, not a per-title measurement, which is why it is opt-in; the debt entry says so. GT7's configured flexible size was not reported, so its 12 GiB fits the model only if it declares the default. Measuring sceKernelGetDirectMemorySize() inside ASTRO BOT on a console (12408 MiB expected) would confirm or refute it.

Tested

Linux (Arch, kernel 7.2.11, GCC 16.2.1, Release), AMD RX 6650 XT with RADV, head 1693c644c4 on main 0278ca9831, where the model was the default, so these runs match the variable being set now (1812afb176 added the sceKernelMemoryPoolExpand check, 6f13e057ab made the model opt-in):

  • Tests: the full suite with the Khronos validation layer: 3490 of 3491 pass, no validation messages. The one failure, agc_idle_publish_wait, also fails on main here. guest_flexible_memory_{declared,limited,none,missing} now check the reported size and that sceKernelAllocateDirectMemory, sceKernelAvailableDirectMemorySize and sceKernelMemoryPoolExpand stop at it (with the sceKernelMemoryPoolExpand clamp removed, all four fail); guest_memory's largest-free-block case uses the reported size as its top.
  • ASTRO BOT (PPSA21564, 01.018.000, 328 MiB flexible), 300 s from boot with Cross presses, APS5_TRACE_MEMORY=1:
Build Result Direct memory allocated
main (13824 MiB) exit 0 at 294 s, New Game opening 2016, 3232, 184, 2172, 4608, 128, 64 = 12404 MiB
main with 12288 MiB (as #10809) Can't allocate direct memory at 6 s, exit 139 stops after 4608 (12212 MiB), as in #10809
this PR with the variable set (12408 MiB) exit 0 at 294 s, New Game opening the same seven, 12404 MiB
  • Earthion (PPSA28061, 1024 MiB flexible, so 11712 MiB direct), the same inputs on main and this PR with the variable set: both exit 0 at 213 s with a 59.94 FPS median and allocate 1111 MiB of direct memory, with no allocation failure.

  • Blue Prince (PPSA25009, 1024 MiB flexible): on main and this PR with the variable set alike it stops at 46.4 s on the unimplemented "sample-preserving guest publication of native multisample depth" path, having allocated 3678 and 3692 MiB of direct memory, with no allocation failure.

  • Windows, independent run by @maskym (report): exact head 7d36292121 merged alone into main a4eca2a056, Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 UCRT, Release, build clean. The 42 tests this PR adds or extends (including all eight guest_flexible_memory_* cases, guest_memory and guest_mmap_*) pass in each of three runs, 126 passes. Not run on a PS5.

Not tested: titles that size pools from the reported total.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none (restores the console size that #10809 measured, without its ASTRO BOT regression)
  • GPUs tested: AMD RDNA2 (RADV), Linux
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5121106467 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15064). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-02T13:28:30Z. --- ### What `sceKernelGetDirectMemorySize` returns 13824 MiB today. #10809 changed it to the 12 GiB measured on a PS5 (inside Gran Turismo 7), and it was reverted in #12192 because ASTRO BOT (PPSA21564) allocates 12404 MiB of direct memory itself and then asserts. This PR adds that model **behind `APS5_DIRECT_MEMORY_FROM_FLEXIBLE`** and keeps the 13824 MiB default (as asked in review: the model is not yet confirmed on a console). With the variable set, `sceKernelGetDirectMemorySize` reports **12736 MiB less the title's configured flexible-memory size** (`kernel.flexibleMemorySize` in `param.json`, 448 MiB by default, already read for the flexible budget): | Declared flexible | Direct reported (with the variable) | Example | |---|---|---| | none (448 MiB default) | 12288 MiB | Gran Turismo 7 measured 12 GiB | | 328 MiB | 12408 MiB | ASTRO BOT allocates 12404 MiB | | 224 MiB | 12512 MiB | ASTRO's PLAYROOM, which needs at least 12318 MiB (#12192) | | 1024 MiB | 11712 MiB | Earthion, Blue Prince | With the variable set, `sceKernelAllocateDirectMemory`, `sceKernelAllocateMainDirectMemory`, `sceKernelAvailableDirectMemorySize` and `sceKernelMemoryPoolExpand` search no higher than that size. Without it nothing changes. The host backing stays at 13824 MiB either way. ### Why this split - On firmware 13.42, `sceKernelGetDirectMemorySize` is not a constant: it returns a dmem ioctl's answer after reading the `kern.app.memconf` sysctl (BunnyFl's disassembly in #10809), so it is per process. - One shared 12736 MiB budget fits both known points: GT7 (default flexible) gets 12 GiB, and ASTRO BOT's 328 MiB declaration leaves it 12408 MiB, 4 MiB above the 12404 MiB it allocates on a console. - This is an inference, not a per-title measurement, which is why it is opt-in; the debt entry says so. GT7's configured flexible size was not reported, so its 12 GiB fits the model only if it declares the default. Measuring `sceKernelGetDirectMemorySize()` inside ASTRO BOT on a console (12408 MiB expected) would confirm or refute it. ### Tested Linux (Arch, kernel 7.2.11, GCC 16.2.1, Release), AMD RX 6650 XT with RADV, head `1693c644c4` on `main` `0278ca9831`, where the model was the default, so these runs match the variable being set now (`1812afb176` added the `sceKernelMemoryPoolExpand` check, `6f13e057ab` made the model opt-in): - **Tests:** the full suite with the Khronos validation layer: 3490 of 3491 pass, no validation messages. The one failure, `agc_idle_publish_wait`, also fails on `main` here. `guest_flexible_memory_{declared,limited,none,missing}` now check the reported size and that `sceKernelAllocateDirectMemory`, `sceKernelAvailableDirectMemorySize` and `sceKernelMemoryPoolExpand` stop at it (with the `sceKernelMemoryPoolExpand` clamp removed, all four fail); `guest_memory`'s largest-free-block case uses the reported size as its top. - **ASTRO BOT (PPSA21564, 01.018.000, 328 MiB flexible)**, 300 s from boot with Cross presses, `APS5_TRACE_MEMORY=1`: | Build | Result | Direct memory allocated | |---|---|---| | `main` (13824 MiB) | exit 0 at 294 s, New Game opening | 2016, 3232, 184, 2172, 4608, 128, 64 = 12404 MiB | | `main` with 12288 MiB (as #10809) | `Can't allocate direct memory` at 6 s, exit 139 | stops after 4608 (12212 MiB), as in #10809 | | this PR with the variable set (12408 MiB) | exit 0 at 294 s, New Game opening | the same seven, 12404 MiB | - **Earthion (PPSA28061, 1024 MiB flexible, so 11712 MiB direct)**, the same inputs on `main` and this PR with the variable set: both exit 0 at 213 s with a 59.94 FPS median and allocate 1111 MiB of direct memory, with no allocation failure. - **Blue Prince (PPSA25009, 1024 MiB flexible)**: on `main` and this PR with the variable set alike it stops at 46.4 s on the unimplemented "sample-preserving guest publication of native multisample depth" path, having allocated 3678 and 3692 MiB of direct memory, with no allocation failure. - **Windows, independent run by @maskym** ([report](https://github.com/boykopovar/AnyPS5/pull/15064#issuecomment-6587336553)): exact head `7d36292121` merged alone into `main` `a4eca2a056`, Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 UCRT, Release, build clean. The 42 tests this PR adds or extends (including all eight `guest_flexible_memory_*` cases, `guest_memory` and `guest_mmap_*`) pass in each of three runs, 126 passes. Not run on a PS5. Not tested: titles that size pools from the reported total. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [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 (restores the console size that #10809 measured, without its ASTRO BOT regression) - [x] GPUs tested: AMD RDNA2 (RADV), Linux - [x] AI-assisted: yes
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!15064
No description provided.