fix(libkernel): report the largest free direct memory block from sceKernelAvailableDirectMemorySize #3292

Merged
reyani-dev merged 1 commit from upstream-pr/3292 into main 2026-10-11 07:27:09 +00:00
reyani-dev commented 2026-10-11 05:42:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T05:42:52Z.


What

Carries the first of @xXPBSXx's two fixes from #3143, which was shared as a branch while intake was paused. The commit is theirs, unchanged (aace6a3f), and merges cleanly onto current main. I left out the second fix (sceKernelMprotect on host thread-stack memory) because it overlaps #2982.

sceKernelAvailableDirectMemorySize allocated one page, freed it, and reported the free run that starts at that first fitting page. So it returned the first gap in the search range, not the largest free block. In Resident Evil 4 (PPSA07411) on Windows, it reported about 1.9 MiB and the title's 96 MiB allocation then failed (#3143). The function now scans the range for the largest free page run, starting at the requested alignment, and reports that block. When nothing fits, it still returns SCE_KERNEL_ERROR_EAGAIN with both outputs zeroed, the same code the old path got from the allocator. The scan walks the same per-page bitmap that Alloc already scans linearly.

guest_memory gains CheckAvailableDirectMemoryIsLargestFreeBlock. It leaves a 4 MiB hole between two 2 MiB blocks and checks:

  • the largest block in the whole window;
  • the hole when the window ends before the larger block;
  • an aligned start;
  • EAGAIN with zeroed outputs when nothing is free;
  • EINVAL for a null output.

Closes the first item of #3143.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, full build with BUILD_TESTING=ON, the commit merged onto main 961b25fe:

  • guest_memory passes 3/3 (--repeat until-fail:3).
  • With DirectMemory/Export.cpp reverted to main, it fails at the first largest-block check (GuestMemory.cpp:1640).
  • ctest -E "^agc_|video_out": 411/412. The one failure is guest_filesystem's symlink-creation check, because this account can't create symbolic links (#3169). It fails the same way on main.
  • check_conventions.py --base upstream/main passes, and pr_overlap.py reports no overlap.

The RE4 result is xXPBSXx's runtime finding in #3143; I don't have the title.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4816884919 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3292). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T05:42:52Z. --- ### What Carries the first of @xXPBSXx's two fixes from #3143, which was shared as a branch while intake was paused. The commit is theirs, unchanged (`aace6a3f`), and merges cleanly onto current `main`. I left out the second fix (`sceKernelMprotect` on host thread-stack memory) because it overlaps #2982. `sceKernelAvailableDirectMemorySize` allocated one page, freed it, and reported the free run that starts at that first fitting page. So it returned the *first* gap in the search range, not the largest free block. In Resident Evil 4 (PPSA07411) on Windows, it reported about 1.9 MiB and the title's 96 MiB allocation then failed (#3143). The function now scans the range for the largest free page run, starting at the requested alignment, and reports that block. When nothing fits, it still returns `SCE_KERNEL_ERROR_EAGAIN` with both outputs zeroed, the same code the old path got from the allocator. The scan walks the same per-page bitmap that `Alloc` already scans linearly. `guest_memory` gains `CheckAvailableDirectMemoryIsLargestFreeBlock`. It leaves a 4 MiB hole between two 2 MiB blocks and checks: - the largest block in the whole window; - the hole when the window ends before the larger block; - an aligned start; - `EAGAIN` with zeroed outputs when nothing is free; - `EINVAL` for a null output. Closes the first item of #3143. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, full build with `BUILD_TESTING=ON`, the commit merged onto `main` `961b25fe`: - `guest_memory` passes 3/3 (`--repeat until-fail:3`). - With `DirectMemory/Export.cpp` reverted to `main`, it fails at the first largest-block check (`GuestMemory.cpp:1640`). - `ctest -E "^agc_|video_out"`: 411/412. The one failure is `guest_filesystem`'s symlink-creation check, because this account can't create symbolic links (#3169). It fails the same way on `main`. - `check_conventions.py --base upstream/main` passes, and `pr_overlap.py` reports no overlap. The RE4 result is xXPBSXx's runtime finding in #3143; I don't have the title. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Mouhand-Kaddo commented 2026-10-11 07:25:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-11T07:25:49Z.


Additional platform validation of standalone PR head aace6a3f38f91ea9e6c9239efdf54e1ae9a2a4bd (2026-10-11):

Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, BUILD_TESTING=ON.

1 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run.

Checks: guest_memory.

Command after building: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_memory$".

No title/gameplay validation. These results cover the standalone head, not a merge with current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106659164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3292#issuecomment-6106659164). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-11T07:25:49Z. --- <!-- anyps5-platform-validation:aace6a3f38f91ea9e6c9239efdf54e1ae9a2a4bd --> Additional platform validation of standalone PR head `aace6a3f38f91ea9e6c9239efdf54e1ae9a2a4bd` (2026-10-11): **Linux: passed** — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, `BUILD_TESTING=ON`. 1 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run. Checks: `guest_memory`. Command after building: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_memory$"`. No title/gameplay validation. These results cover the standalone head, not a merge with current main.
adriadam10 commented 2026-10-11 07:26:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:26:58Z.


Checked the diff against current main, built it and ran guest_memory (passes; it fails with the three non-test files reverted). sceKernelAvailableDirectMemorySize now reports the largest aligned free run clipped to the search window, which matches shadPS4's DirectQueryAvailable; the EAGAIN path with zeroed outputs is unchanged from before. Merging.

Not covered here: the RE4 behaviour on Windows (from #3143). Small nit for later: a doubled blank line before CheckAvailableDirectMemoryIsLargestFreeBlock in GuestMemory.cpp.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106668150 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3292#issuecomment-6106668150). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:26:58Z. --- Checked the diff against current `main`, built it and ran `guest_memory` (passes; it fails with the three non-test files reverted). `sceKernelAvailableDirectMemorySize` now reports the largest aligned free run clipped to the search window, which matches shadPS4's `DirectQueryAvailable`; the `EAGAIN` path with zeroed outputs is unchanged from before. Merging. Not covered here: the RE4 behaviour on Windows (from #3143). Small nit for later: a doubled blank line before `CheckAvailableDirectMemoryIsLargestFreeBlock` in `GuestMemory.cpp`.
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!3292
No description provided.