Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ int APS5_VABI sceKernelMemoryPoolExpand(int64_t search_start, int64_t search_end
|| (alignment != 0 && !PageAligned(alignment))) {
return SCE_KERNEL_ERROR_EINVAL;
}
const int ret = DirectMemoryAlloc(search_start, std::min<int64_t>(search_end, static_cast<int64_t>(DirectMemorySize())), len, alignment, -1, phys_addr_out);
if (ret == 0) {
PoolState& pool = Pool();
std::lock_guard<std::mutex> lock(pool.mutex);
pool.expandedBytes += len;
int64_t physical = 0;
const int ret = DirectMemoryAlloc(search_start, std::min<int64_t>(search_end, static_cast<int64_t>(DirectMemorySize())), len, alignment, -1, &physical);
if (ret != 0) return ret;
if (!GuestCopyOut_nid_no_patch(phys_addr_out, &physical, sizeof(physical))) {
DirectMemoryFree(physical, len);
return SCE_KERNEL_ERROR_EFAULT;
}
return ret;
PoolState& pool = Pool();
std::lock_guard<std::mutex> lock(pool.mutex);
pool.expandedBytes += len;
return 0;
}

int APS5_VABI sceKernelMemoryPoolGetBlockStats(KernelMemoryPoolBlockStats* output, size_t output_size) {
Expand Down
32 changes: 32 additions & 0 deletions core/libs/tests/GuestDirectMemoryForApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <cstdlib>
#include <source_location>
#include <stdexcept>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#endif

static void Require(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
Expand Down Expand Up @@ -35,6 +40,33 @@ int main() {
Require(sceKernelAllocateDirectMemoryForApp(freePage, pageEnd, page, 0, 0, &phys) == 0 && phys == freePage);
Require(sceKernelReleaseDirectMemory(phys, page) == 0);

constexpr std::size_t block = 2 * 1024 * 1024;
Require(sceKernelAllocateDirectMemoryForApp(0, size, block, block, 0, &phys) == 0);
const auto freeBlock = phys;
const auto blockEnd = freeBlock + static_cast<int64_t>(block);
Require(sceKernelReleaseDirectMemory(phys, block) == 0);
Require(sceKernelMemoryPoolExpand(freeBlock, blockEnd, block, block, badOutput) == SCE_KERNEL_ERROR_EFAULT);
#ifdef _WIN32
void* const readOnlyPage = VirtualAlloc(nullptr, page, MEM_COMMIT | MEM_RESERVE, PAGE_READONLY);
Require(readOnlyPage != nullptr);
#else
void* const readOnlyPage = mmap(nullptr, page, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Require(readOnlyPage != MAP_FAILED);
#endif
Require(sceKernelMemoryPoolExpand(freeBlock, blockEnd, block, block, static_cast<int64_t*>(readOnlyPage)) == SCE_KERNEL_ERROR_EFAULT);
#ifdef _WIN32
Require(VirtualFree(readOnlyPage, 0, MEM_RELEASE) != 0);
#else
Require(munmap(readOnlyPage, page) == 0);
#endif
KernelMemoryPoolBlockStats stats{};
Require(sceKernelMemoryPoolGetBlockStats(&stats, sizeof(stats)) == 0);
Require(stats.available_flushed_blocks == 0 && stats.allocated_flushed_blocks == 0);
Require(sceKernelMemoryPoolExpand(freeBlock, blockEnd, block, block, &phys) == 0 && phys == freeBlock);
Require(sceKernelMemoryPoolGetBlockStats(&stats, sizeof(stats)) == 0);
Require(stats.available_flushed_blocks == 1 && stats.allocated_flushed_blocks == 0);
Require(sceKernelReleaseDirectMemory(phys, block) == 0);

phys = -1;
Require(sceKernelAllocateDirectMemoryForApp(0, size, page * 4, 0, 0, &phys) == 0);
Require(phys >= 0);
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libkernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@
- [sceKernelGetResidentFmemCount](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) throws. PS5_MihawkOS (PPSA99170) 8.1.0-alpha-8 imports it from `libkernel` and declares `int sceKernelGetResidentFmemCount(int pid, uint64_t *resident, uint64_t *mapped)` in [`src/pw_memory_helper.c`](https://github.com/mihawk-99/PS5_MihawkOS/blob/bc24d44be95f1c26ce297c0463f3bb31a751434a/src/pw_memory_helper.c#L203-L205), where only its memory helper's census calls it. Its [console notes](https://github.com/mihawk-99/PS5_MihawkOS/blob/bc24d44be95f1c26ce297c0463f3bb31a751434a/docs/history/2026-10-07/docs/WINDOWS_PROCESSES.md#L1575-L1583) give two counts in 16 KiB pages for any pid, resident and mapped, and `0x8002000e` with neither written when the second pointer is null. A guest runs as the only process here and no resident count is kept, so nothing is answered until a title reaches it.
- [sceKernelAllocateDirectMemory](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - `search_end` 0 with a `search_start` above 0 and below the direct memory size searches up to that size, because a payload process on a PS5 (firmware 11.60, 2026-11-07) returned 0 and set the output for `(1 MiB, 0, 1 MiB, alignment 0, type 12)`. The physical address it chose was not recorded, so where the console places the block is not confirmed. Any other `search_end` not above `search_start`, including a nonzero one below it and 0 with `search_start` 0, still returns `EINVAL` here; the console's result for them was not measured, and neither was a title process or firmware 13.x.
- [sceKernelAllocateDirectMemoryForApp](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) forwards to `sceKernelAllocateDirectMemory`, sharing its validation and allocator. The same title's pinned [`native/pw_dmem_share.h`](https://github.com/mihawk-99/PS5_MihawkOS/blob/8a9c117cb265d0f54ead53b0aa37cdc8c0dc9c91/native/pw_dmem_share.h#L17) declares `int sceKernelAllocateDirectMemoryForApp(int64_t, int64_t, size_t, size_t, int, int64_t*)`, the six arguments of `sceKernelAllocateDirectMemory`, and its [`docs/PLAN.md`](https://github.com/mihawk-99/PS5_MihawkOS/blob/8a9c117cb265d0f54ead53b0aa37cdc8c0dc9c91/docs/PLAN.md) shows the same allocation reached through both calls. The forward is assumed from that shared signature: `ForApp`'s own argument checks and error codes were not measured, and a guest has one direct memory range, so an app-specific range restriction has nothing separate to observe.
- [sceKernelMemoryPoolExpand](../../../core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp) (libkernel) allocates into a local and copies the physical address out with `GuestCopyOut`: a null output keeps its earlier answer, while an unmapped or read-only output returns `SCE_KERNEL_ERROR_EFAULT` after the allocation is released again and without adding to the pool's expanded size, so a failed call leaves the pool as it was. This is a host-safety fallback; what the console does for a bad output pointer, and whether it keeps the allocation, was not measured.
- [APS5_GAME_MODE](../../../core/libs/prx/libkernel/System/src/HostPerformanceProcess.cpp) (libkernel) is `off` when unset and `auto` is not the default, because no distribution or desktop was measured to say what `gamemoded` or an activity changes for a title (CPU governor, priority, frame pacing, power draw), and a default that changes scheduling for everyone must be measured first. Whether `on` helps or hurts a title is unmeasured.
- [Host game mode on Linux](../../../core/libs/prx/libkernel/System/src/GameModeClient.cpp) (libkernel) - the request ends from the libc shutdown callbacks, which every exit path runs except an immediate `_Exit` and a crash, so those leave it to `gamemoded`. The daemon has a reaper thread for clients that disappear (FeralInteractive/gamemode at `a74b8106a2236d1f2696aa44c93bc4c8ef13b42e`, `daemon/gamemode-context.c`), but how soon it drops a client that never ended its request is unmeasured.
- [Host game mode on Linux](../../../core/libs/prx/libkernel/System/src/GameModeClient.cpp) (libkernel) - the library names (`libgamemode.so.0`, then `libgamemode.so`) and the `real_gamemode_request_start`, `real_gamemode_request_end` and `real_gamemode_error_string` symbols are those of the client header in FeralInteractive/gamemode at `a74b8106a2236d1f2696aa44c93bc4c8ef13b42e` (`lib/gamemode_client.h`); a later ABI change was not checked, and no real `gamemoded` was run against the backend, only fixture libraries exporting those symbols.
Expand Down
Loading