Skip to content
Open
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
3 changes: 2 additions & 1 deletion core/libs/prx/libkernel/DirectMemory/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,11 @@ int APS5_VABI sceKernelReleaseFlexibleMemory(void* addr, size_t len) {
}

int APS5_VABI sceKernelReleaseDirectMemory(int64_t start, size_t len) {
if (start < 0 || len == 0) return SCE_KERNEL_ERROR_EINVAL;
if (start < 0) return SCE_KERNEL_ERROR_EINVAL;
if ((static_cast<std::uint64_t>(start) & (PS5_PAGE_SIZE - 1)) != 0 || (len & (PS5_PAGE_SIZE - 1)) != 0 ||
len > DIRECT_MEMORY_SIZE || static_cast<std::uint64_t>(start) > DIRECT_MEMORY_SIZE - len)
return SCE_KERNEL_ERROR_EINVAL;
if (len == 0) return 0;
if (!DirectMemoryClaimRelease(start, len, false)) return 0;
ReleaseClaimedDirectMemory(start, len);
return 0;
Expand Down
16 changes: 14 additions & 2 deletions core/libs/tests/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ static void CheckDirectMemoryFollowsPhysicalPages() {
Require(sceKernelAllocateDirectMemory(0, 0x10000, page, 0, 0, nullptr) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelAllocateDirectMemory(0, 0x10000, page, page - 1, 0, &invalidPhys) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(-1, page) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, 0) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, 0) == 0);
Require(sceKernelReleaseDirectMemory(1, page) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, page + 1) == SCE_KERNEL_ERROR_EINVAL);
std::int64_t phys = 0;
Expand Down Expand Up @@ -1083,10 +1083,21 @@ static void CheckReleaseDirectMemoryClearsMappings() {
}
}

static void CheckReleaseDirectMemoryZeroLength() {
constexpr std::size_t page = 0x4000;
std::int64_t phys = 0;
Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page * 2, 0, 0, &phys) == 0);
Require(sceKernelReleaseDirectMemory(phys, 0) == 0);
Require(sceKernelReleaseDirectMemory(phys, 1) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(phys + 1, page) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(phys, page * 2) == 0);
Require(sceKernelReleaseDirectMemory(phys, page * 2) == 0);
}

static void CheckReleaseDirectMemoryRejectsInvalidRanges() {
constexpr std::size_t page = 0x4000;
Require(sceKernelReleaseDirectMemory(-1, page) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, 0) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, 0) == 0);
Require(sceKernelReleaseDirectMemory(0, 1) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(0, page - 1) == SCE_KERNEL_ERROR_EINVAL);
Require(sceKernelReleaseDirectMemory(1, page) == SCE_KERNEL_ERROR_EINVAL);
Expand Down Expand Up @@ -4911,6 +4922,7 @@ int main(int argc, char** argv) {
CheckAllocateDirectMemoryRejectsInvalidArguments();
CheckReleaseDirectMemoryClearsMappings();
CheckReleaseDirectMemoryRejectsInvalidRanges();
CheckReleaseDirectMemoryZeroLength();
CheckFixedMappingReplacesPartialOverlap();
CheckFixedDirectMapRejectsAlignmentBeforeReplacing();
CheckFixedMappingOverHoleInsideAllocation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libkernel

## change [sceKernelReleaseDirectMemory](

- [sceKernelReleaseDirectMemory](../../../core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp) (libkernel) - releases the allocated pages of the range and skips pages that are not allocated, returning 0 (also for a length of 0 at an aligned start, as a PS5 payload process (firmware 11.60) did on 2026-11-07; a title process, a misaligned start with a length of 0 and a start at the end of direct memory were not measured), while `sceKernelCheckedReleaseDirectMemory` returns `ENOENT` for them, as [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/c206f35a546230d9c9808b823c0f36f5850cd4af/src/core/memory.cpp#L295-L313) does; not measured on a console. Absolum (PPSA28311) releases such a range when it disposes of its video logos
Loading