fix(libkernel): report 12 GiB of direct memory, as a PS5 does #10809

Merged
frankea merged 2 commits from upstream-pr/10809 into main 2026-10-28 00:49:58 +00:00
frankea commented 2026-10-25 02:14:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-25T02:14:36Z.


What

sceKernelGetDirectMemorySize returns DIRECT_MEMORY_SIZE, which has been 13824 MiB (13.5 GiB, 0x360000000) since the first direct-memory commit (6ecfc3555). On a PS5 it returns 0x300000000, 12 GiB. DIRECT_MEMORY_SIZE is now 12288 MiB, so the size a title reads, the physical range sceKernelAllocateDirectMemory and sceKernelAllocateMainDirectMemory allocate from, the range checks of the release calls and the host pool behind them all shrink by 1.5 GiB.

Titles print this value: Demon's Souls logs Total Direct Memory: 13824.00 MiB - 0x0000000360000000 Bytes in #7203; on a PS5 that line would read 12288 MiB. Titles also size their pools from it: in @thuanquang's Persona 5 Royal (PPSA05110) run in the comments, the title maps all but 31 MiB of whatever size it reads, so with this PR it maps 12257 MiB instead of 13793 MiB (peak private bytes 19.90 instead of 21.79 GiB on that route) and still reaches New Game.

Measurement: PS5 on firmware 13.40, 2026-10-24, inside Gran Turismo 7 (PPSA01317) shortly after it started. A payload attached to the game with ptrace and called the game's own libkernel export sceKernelGetDirectMemorySize() twice: both calls returned 0x300000000. In the same session sceKernelIsTrinityMode() returned 0 and sceKernelGetAvailableCpumask() returned 0x1fff, the values AnyPS5 already returns.

Not measured: other titles, a PS5 Pro, and how much of the 12 GiB a title can allocate at start-up (sceKernelAvailableDirectMemorySize on the console).

Also changed: the 13824ll << 20 search end in GuestAprRead.cpp (the pool clamps it, so the test passed either way), the 13824 MiB in USAGE.md's Windows commit-limit note, and the 13.5 GiB in the sceKernelAvailableFlexibleMemorySize debt entry. guest_memory now checks that the size is 0x300000000.

Tested

Windows 11 (build 26200), MinGW GCC (WinLibs 15.2) Release build with BUILD_TESTING=ON, each test run three times (ctest --repeat until-fail:3).

  • The 16 ctest entries of the test executables that use the direct-memory size (guest_memory_tests, guest_apr_read_tests, guest_ampr_tests, guest_flexible_memory_budget_tests, MemoryPoolTest):
    • This PR (b5c6010f8, ad9a0c8ef with main 97d33464c merged in): 16/16.
    • Control: main 97d33464c with only this PR's GuestMemory.cpp: guest_memory stops on the new check (Guest memory check failed at .../GuestMemory.cpp:709; main returns 0x360000000); the other 15 pass.
    • main 97d33464c: 16/16.
  • Linux: @wachi35676's run in the comments (WSL2 Ubuntu 24.04, GCC 13.3) of b5c6010f8 merged onto main 9c435c281: guest_memory, guest_protection_report and the guest_apr_read_* tests pass 3 of 3 (7 tests match there, 9 on Windows), and with DirectMemory.hpp from main guest_memory fails on both hosts. tools/check_conventions.py --base origin/main reports no errors; its one warning is the length of the sceKernelAvailableFlexibleMemorySize debt entry, which was already over the limit (this PR changes one number in it).

Overlap

#5021 (yabanana) rewrites the same sceKernelAvailableFlexibleMemorySize debt line (it adds the macOS accounting), and #11130 (imellag) inserts its mimalloc lines right before the USAGE.md line this PR changes; each conflicts with this PR on that one line. This PR goes first: it changes one number in each line, so the other PR keeps that number when it rebases. #9882 and #10025 change other parts of DirectMemory.hpp and the libkernel debt file and merge with this branch without conflicts. No open PR changes DIRECT_MEMORY_SIZE.

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 known
  • GPUs tested: n/a (no GPU code)
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5001063713 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10809). Originally posted by [frankea](https://github.com/frankea) on 2026-10-25T02:14:36Z. --- ### What `sceKernelGetDirectMemorySize` returns `DIRECT_MEMORY_SIZE`, which has been 13824 MiB (13.5 GiB, `0x360000000`) since the first direct-memory commit (6ecfc3555). On a PS5 it returns `0x300000000`, 12 GiB. `DIRECT_MEMORY_SIZE` is now 12288 MiB, so the size a title reads, the physical range `sceKernelAllocateDirectMemory` and `sceKernelAllocateMainDirectMemory` allocate from, the range checks of the release calls and the host pool behind them all shrink by 1.5 GiB. Titles print this value: Demon's Souls logs `Total Direct Memory: 13824.00 MiB - 0x0000000360000000 Bytes` in #7203; on a PS5 that line would read 12288 MiB. Titles also size their pools from it: in @thuanquang's Persona 5 Royal (PPSA05110) run in the comments, the title maps all but 31 MiB of whatever size it reads, so with this PR it maps 12257 MiB instead of 13793 MiB (peak private bytes 19.90 instead of 21.79 GiB on that route) and still reaches New Game. Measurement: PS5 on firmware 13.40, 2026-10-24, inside Gran Turismo 7 (PPSA01317) shortly after it started. A payload attached to the game with ptrace and called the game's own `libkernel` export `sceKernelGetDirectMemorySize()` twice: both calls returned `0x300000000`. In the same session `sceKernelIsTrinityMode()` returned 0 and `sceKernelGetAvailableCpumask()` returned `0x1fff`, the values AnyPS5 already returns. Not measured: other titles, a PS5 Pro, and how much of the 12 GiB a title can allocate at start-up (`sceKernelAvailableDirectMemorySize` on the console). Also changed: the `13824ll << 20` search end in `GuestAprRead.cpp` (the pool clamps it, so the test passed either way), the 13824 MiB in `USAGE.md`'s Windows commit-limit note, and the 13.5 GiB in the `sceKernelAvailableFlexibleMemorySize` debt entry. `guest_memory` now checks that the size is `0x300000000`. ### Tested Windows 11 (build 26200), MinGW GCC (WinLibs 15.2) Release build with `BUILD_TESTING=ON`, each test run three times (`ctest --repeat until-fail:3`). - The 16 ctest entries of the test executables that use the direct-memory size (`guest_memory_tests`, `guest_apr_read_tests`, `guest_ampr_tests`, `guest_flexible_memory_budget_tests`, `MemoryPoolTest`): - This PR (b5c6010f8, ad9a0c8ef with `main` 97d33464c merged in): 16/16. - Control: `main` 97d33464c with only this PR's `GuestMemory.cpp`: `guest_memory` stops on the new check (`Guest memory check failed at .../GuestMemory.cpp:709`; `main` returns `0x360000000`); the other 15 pass. - `main` 97d33464c: 16/16. - Linux: @wachi35676's run in the comments (WSL2 Ubuntu 24.04, GCC 13.3) of `b5c6010f8` merged onto `main` `9c435c281`: `guest_memory`, `guest_protection_report` and the `guest_apr_read_*` tests pass 3 of 3 (7 tests match there, 9 on Windows), and with `DirectMemory.hpp` from `main` `guest_memory` fails on both hosts. `tools/check_conventions.py --base origin/main` reports no errors; its one warning is the length of the `sceKernelAvailableFlexibleMemorySize` debt entry, which was already over the limit (this PR changes one number in it). ### Overlap #5021 (yabanana) rewrites the same `sceKernelAvailableFlexibleMemorySize` debt line (it adds the macOS accounting), and #11130 (imellag) inserts its mimalloc lines right before the `USAGE.md` line this PR changes; each conflicts with this PR on that one line. This PR goes first: it changes one number in each line, so the other PR keeps that number when it rebases. #9882 and #10025 change other parts of `DirectMemory.hpp` and the libkernel debt file and merge with this branch without conflicts. No open PR changes `DIRECT_MEMORY_SIZE`. ### 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](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 known - [x] GPUs tested: n/a (no GPU code) - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-25 02:21:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:21:07Z.

Review: COMMENTED


I reviewed the full diff and the consumers of DIRECT_MEMORY_SIZE. The 12 GiB value is supported by the author's reported measurement of sceKernelGetDirectMemorySize() on PS5 firmware 13.40, and the constant consistently governs the reported size, allocation pool, and bounds checks. The focused regression asserts 0x300000000; I found no overlapping open PR for this constant. git diff --check passes and hosted eligibility, intake, conventions, labels, and report checks pass. Product build/test jobs are skipped by the path filters. I attempted a local full-build configure, but it stops because 3rdparty/SDL2 is uninitialized in this checkout (CMakeLists.txt is missing); I did not modify submodules or claim a local test run.

I did not find a blocking issue in this revision.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641150108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10809#pullrequestreview-5641150108). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:21:07Z. **Review:** COMMENTED --- I reviewed the full diff and the consumers of `DIRECT_MEMORY_SIZE`. The 12 GiB value is supported by the author's reported measurement of `sceKernelGetDirectMemorySize()` on PS5 firmware 13.40, and the constant consistently governs the reported size, allocation pool, and bounds checks. The focused regression asserts `0x300000000`; I found no overlapping open PR for this constant. `git diff --check` passes and hosted eligibility, intake, conventions, labels, and report checks pass. Product build/test jobs are skipped by the path filters. I attempted a local full-build configure, but it stops because `3rdparty/SDL2` is uninitialized in this checkout (`CMakeLists.txt` is missing); I did not modify submodules or claim a local test run. I did not find a blocking issue in this revision.
MarcZX10 commented 2026-10-25 02:59:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:59:02Z.

Review: CHANGES_REQUESTED


I found one checklist omission during the required manual overlap review. PR #5021 is still open and also edits the sceKernelAvailableFlexibleMemorySize debt bullet in docs/dev/debt/libkernel.md. Both PRs replace the same physical Markdown line: #5021 adds the macOS accounting clause and this PR changes its direct-memory-size statement from 13.5 GiB to 12 GiB. Please coordinate with #5021 on merge order and record it as Depends on: #N in both PR checklists (or none only if the maintainers confirm the overlap rule does not apply here). I have not modified either branch. The direct-memory implementation and the author-reported focused test results otherwise remain consistent with my prior review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641255457 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10809#pullrequestreview-5641255457). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:59:02Z. **Review:** CHANGES_REQUESTED --- I found one checklist omission during the required manual overlap review. PR #5021 is still open and also edits the `sceKernelAvailableFlexibleMemorySize` debt bullet in `docs/dev/debt/libkernel.md`. Both PRs replace the same physical Markdown line: #5021 adds the macOS accounting clause and this PR changes its direct-memory-size statement from 13.5 GiB to 12 GiB. Please coordinate with #5021 on merge order and record it as `Depends on: #N` in both PR checklists (or `none` only if the maintainers confirm the overlap rule does not apply here). I have not modified either branch. The direct-memory implementation and the author-reported focused test results otherwise remain consistent with my prior review.
MarcZX10 commented 2026-10-25 06:08:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T06:08:23Z.

Review: COMMENTED


A base refresh is now also needed: this head contains one commit over a base 70 commits behind current main (61c55ed466). git merge-tree --write-tree currently applies it to main without conflicts, so this is not a claim of a present main-branch conflict. Please rebase before final review as required by the current-main checklist and rerun the focused direct-memory tests against the rebased tree; the reported Windows runs used main 941ace5f3. The same-line overlap with #5021 from my existing change request remains unresolved, so please coordinate and record the agreed order in the affected checklist(s) as well.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641805840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10809#pullrequestreview-5641805840). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T06:08:23Z. **Review:** COMMENTED --- A base refresh is now also needed: this head contains one commit over a base 70 commits behind current `main` (`61c55ed466`). `git merge-tree --write-tree` currently applies it to `main` without conflicts, so this is not a claim of a present main-branch conflict. Please rebase before final review as required by the current-main checklist and rerun the focused direct-memory tests against the rebased tree; the reported Windows runs used `main` `941ace5f3`. The same-line overlap with #5021 from my existing change request remains unresolved, so please coordinate and record the agreed order in the affected checklist(s) as well.
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!10809
No description provided.