From 892aa8b5ee3b13e8c70604a1289db31da7f66f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0l=C3=A9gl?= Date: Thu, 8 Oct 2026 18:03:58 +0200 Subject: [PATCH 1/2] feat(libkernel): implement mlock The POSIX form of sceKernelMlock: 0 on success, -1 with errno (EINVAL, ENOMEM, EPERM, EAGAIN) where the sce form returns the kernel error, as munmap and mprotect do beside it. Co-Authored-By: Claude Fable 5.1 --- core/libs/prx/libkernel/CMakeLists.txt | 10 +++ .../libkernel/DirectMemory/PosixMemory.cpp | 8 +++ core/libs/tests/GuestMlock.cpp | 72 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 core/libs/tests/GuestMlock.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 1fe4e1e79..207e7be34 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -446,6 +446,16 @@ if(BUILD_TESTING) set_tests_properties(guest_memory PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_mlock_tests ${LIBS_INCLUDE_DIR}/tests/GuestMlock.cpp) + target_include_directories(guest_mlock_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_mlock_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_mlock_tests) + add_test(NAME guest_mlock COMMAND guest_mlock_tests) + set_tests_properties(guest_mlock PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_mlock PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() if(WIN32) add_test_executable(guest_directory_buffer_tests ${LIBS_INCLUDE_DIR}/tests/GuestDirectoryBuffer.cpp) target_include_directories(guest_directory_buffer_tests PRIVATE ${LIBS_INCLUDE_DIR}) diff --git a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp index bac216bbc..dd0f53b4a 100644 --- a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp @@ -5,6 +5,7 @@ #include extern "C" int* APS5_VABI __error_nid_postfix(); +extern "C" int APS5_VABI sceKernelMlock_nid_postfix(void* address, std::uint64_t length); namespace { // FreeBSD/PS5 ABI values, independent of the host's errno and mmap constants. @@ -97,4 +98,11 @@ int APS5_VABI mprotect_nid_postfix(void* address, std::size_t length, int protec } } +int APS5_VABI mlock_nid_postfix(const void* address, std::size_t length) { + const int result = sceKernelMlock_nid_postfix(const_cast(address), length); + if (result == 0) return 0; + SetError(result & 0xffff); + return -1; +} + } diff --git a/core/libs/tests/GuestMlock.cpp b/core/libs/tests/GuestMlock.cpp new file mode 100644 index 000000000..fb68c505f --- /dev/null +++ b/core/libs/tests/GuestMlock.cpp @@ -0,0 +1,72 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#if defined(__linux__) +#include +#include +#endif + +extern "C" { +int APS5_VABI sceKernelMapFlexibleMemory(void**, std::size_t, int, int); +int APS5_VABI sceKernelReserveVirtualRange(void**, std::size_t, int, std::size_t); +int APS5_VABI sceKernelMunmap(void*, std::size_t); +int APS5_VABI mlock_nid_postfix(const void*, std::size_t); +int* APS5_VABI __error_nid_postfix(); +} + +static void Require(bool value) { if (!value) std::abort(); } + +#if defined(__linux__) +static std::size_t LockedKilobytes() { + std::ifstream status("/proc/self/status"); + std::string line; + while (std::getline(status, line)) { + if (line.rfind("VmLck:", 0) == 0) return std::strtoull(line.c_str() + 6, nullptr, 10); + } + return 0; +} +#endif + +int main() { + constexpr int untouched = 12345; + constexpr std::size_t page = 0x4000; +#ifdef _WIN32 + constexpr std::size_t length = 0x400000; +#else + constexpr std::size_t length = 0x10000; +#endif + void* mapped = nullptr; + Require(sceKernelMapFlexibleMemory(&mapped, length, 3, 0) == 0); + auto* bytes = static_cast(mapped); + + *__error_nid_postfix() = untouched; + Require(mlock_nid_postfix(mapped, 0) == 0); + Require(*__error_nid_postfix() == untouched); +#if defined(__linux__) + const auto lockedBefore = LockedKilobytes(); +#endif + Require(mlock_nid_postfix(bytes + 1, length - page) == 0); +#if defined(__linux__) + Require(LockedKilobytes() - lockedBefore == length / 1024); +#endif + Require(mlock_nid_postfix(mapped, length) == 0); + Require(mlock_nid_postfix(mapped, length) == 0); + bytes[length - 1] = 7; + + *__error_nid_postfix() = 0; + Require(mlock_nid_postfix(reinterpret_cast(std::numeric_limits::max() - page + 1), page * 2) == -1); + Require(*__error_nid_postfix() == 22); + + void* reserved = nullptr; + Require(sceKernelReserveVirtualRange(&reserved, page, 0, 0) == 0); + *__error_nid_postfix() = 0; + Require(mlock_nid_postfix(reserved, page) == -1 && *__error_nid_postfix() == 12); + Require(sceKernelMunmap(reserved, page) == 0); + + Require(sceKernelMunmap(mapped, length) == 0); + *__error_nid_postfix() = 0; + Require(mlock_nid_postfix(mapped, page) == -1 && *__error_nid_postfix() == 12); + return 0; +} From 1078608783fff4e13f0b8e6f54b27774eae9b5d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0l=C3=A9gl?= Date: Fri, 9 Oct 2026 14:49:49 +0200 Subject: [PATCH 2/2] feat(libkernel): implement munlock beside mlock sceKernelMunlock and the POSIX munlock unlock the page-rounded range with FreeBSD's results: 0 for a mapped range whether or not it is locked, EINVAL for a range that wraps around zero, ENOMEM for an unmapped range. On Linux the host munlock gives them. On Windows a reserved placeholder counts as mapped only while the guest reservation or protection registry covers it, and a region VirtualUnlock refuses as not fully locked is unlocked page by page, as VirtualUnlock is all-or-nothing. sceKernelMlock shares the range rounding. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_012GLfKH7Sqe51wX254k1XsV --- .../prx/libkernel/DirectMemory/Export.cpp | 79 +++++++++++++++++-- .../libkernel/DirectMemory/PosixMemory.cpp | 8 ++ core/libs/tests/GuestMlock.cpp | 46 +++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/core/libs/prx/libkernel/DirectMemory/Export.cpp b/core/libs/prx/libkernel/DirectMemory/Export.cpp index 8d893ee6c..d032fc90a 100644 --- a/core/libs/prx/libkernel/DirectMemory/Export.cpp +++ b/core/libs/prx/libkernel/DirectMemory/Export.cpp @@ -445,6 +445,15 @@ int APS5_VABI sceKernelBatchMap2(KernelBatchMapEntry* entries, int num_entries, namespace { +bool PageRange(void* address, std::uint64_t length, std::uintptr_t& start, std::uintptr_t& end) { + constexpr std::uintptr_t pageMask = PS5_PAGE_SIZE - 1; + const auto first = reinterpret_cast(address); + if (length > UINTPTR_MAX - first || first + length > UINTPTR_MAX - pageMask) return false; + start = first & ~pageMask; + end = (first + length + pageMask) & ~pageMask; + return true; +} + #ifdef _WIN32 std::mutex g_workingSetLock; @@ -475,6 +484,53 @@ int LockHostPages(std::uintptr_t start, std::uintptr_t end) { if (GetLastError() != ERROR_WORKING_SET_QUOTA) throw std::system_error(static_cast(GetLastError()), std::system_category(), "VirtualLock failed"); return SCE_KERNEL_ERROR_EAGAIN; } + +std::uintptr_t HostPageSize() { + static const std::uintptr_t size = [] { + SYSTEM_INFO info{}; + GetSystemInfo(&info); + return static_cast(info.dwPageSize); + }(); + return size; +} + +int UnlockHostPages(std::uintptr_t start, std::uintptr_t end) { + for (auto cursor = start; cursor < end;) { + MEMORY_BASIC_INFORMATION region{}; + if (VirtualQuery(reinterpret_cast(cursor), ®ion, sizeof(region)) != sizeof(region) || region.State == MEM_FREE) return SCE_KERNEL_ERROR_ENOMEM; + const auto next = std::min(end, reinterpret_cast(region.BaseAddress) + region.RegionSize); + if (region.State == MEM_RESERVE) { + for (auto reserved = cursor; reserved < next;) { + std::uintptr_t reservationStart = 0; + std::uintptr_t reservationEnd = 0; + int recorded = 0; + if (GuestReservation(reserved, &reservationStart, &reservationEnd)) { + reserved = reservationEnd; + } else if (GuestProtection(reserved, &recorded)) { + reserved = (reserved & ~static_cast(PS5_PAGE_SIZE - 1)) + PS5_PAGE_SIZE; + } else { + return SCE_KERNEL_ERROR_ENOMEM; + } + } + } + cursor = next; + } + for (auto cursor = start; cursor < end;) { + MEMORY_BASIC_INFORMATION region{}; + if (VirtualQuery(reinterpret_cast(cursor), ®ion, sizeof(region)) != sizeof(region)) return SCE_KERNEL_ERROR_ENOMEM; + const auto next = std::min(end, reinterpret_cast(region.BaseAddress) + region.RegionSize); + if (region.State == MEM_COMMIT && (region.Protect & (PAGE_NOACCESS | PAGE_GUARD)) == 0 && !VirtualUnlock(reinterpret_cast(cursor), next - cursor)) { + if (GetLastError() != ERROR_NOT_LOCKED) throw std::system_error(static_cast(GetLastError()), std::system_category(), "VirtualUnlock failed"); + for (auto page = cursor; page < next; page += HostPageSize()) { + if (!VirtualUnlock(reinterpret_cast(page), std::min(HostPageSize(), next - page)) && GetLastError() != ERROR_NOT_LOCKED) { + throw std::system_error(static_cast(GetLastError()), std::system_category(), "VirtualUnlock failed"); + } + } + } + cursor = next; + } + return 0; +} #else int LockHostPages(std::uintptr_t start, std::uintptr_t end) { auto* const address = reinterpret_cast(start); @@ -496,6 +552,13 @@ int LockHostPages(std::uintptr_t start, std::uintptr_t end) { default: throw std::system_error(error, std::generic_category(), "mlock failed"); } } + +int UnlockHostPages(std::uintptr_t start, std::uintptr_t end) { + if (::munlock(reinterpret_cast(start), end - start) == 0) return 0; + const int error = errno; + if (error == ENOMEM) return SCE_KERNEL_ERROR_ENOMEM; + throw std::system_error(error, std::generic_category(), "munlock failed"); +} #endif } @@ -503,13 +566,19 @@ int LockHostPages(std::uintptr_t start, std::uintptr_t end) { extern "C" { int APS5_VABI sceKernelMlock_nid_postfix(void* address, std::uint64_t length) { - constexpr std::uintptr_t pageMask = PS5_PAGE_SIZE - 1; - const auto first = reinterpret_cast(address); - if (length > UINTPTR_MAX - first || first + length > UINTPTR_MAX - pageMask) return SCE_KERNEL_ERROR_EINVAL; - const auto start = first & ~pageMask; - const auto end = (first + length + pageMask) & ~pageMask; + std::uintptr_t start = 0; + std::uintptr_t end = 0; + if (!PageRange(address, length, start, end)) return SCE_KERNEL_ERROR_EINVAL; if (start == end) return 0; return LockHostPages(start, end); } +int APS5_VABI sceKernelMunlock_nid_postfix(void* address, std::uint64_t length) { + std::uintptr_t start = 0; + std::uintptr_t end = 0; + if (!PageRange(address, length, start, end)) return SCE_KERNEL_ERROR_EINVAL; + if (start == end) return 0; + return UnlockHostPages(start, end); +} + } diff --git a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp index dd0f53b4a..ba58cd6ea 100644 --- a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp @@ -6,6 +6,7 @@ extern "C" int* APS5_VABI __error_nid_postfix(); extern "C" int APS5_VABI sceKernelMlock_nid_postfix(void* address, std::uint64_t length); +extern "C" int APS5_VABI sceKernelMunlock_nid_postfix(void* address, std::uint64_t length); namespace { // FreeBSD/PS5 ABI values, independent of the host's errno and mmap constants. @@ -105,4 +106,11 @@ int APS5_VABI mlock_nid_postfix(const void* address, std::size_t length) { return -1; } +int APS5_VABI munlock_nid_postfix(const void* address, std::size_t length) { + const int result = sceKernelMunlock_nid_postfix(const_cast(address), length); + if (result == 0) return 0; + SetError(result & 0xffff); + return -1; +} + } diff --git a/core/libs/tests/GuestMlock.cpp b/core/libs/tests/GuestMlock.cpp index fb68c505f..044cb6828 100644 --- a/core/libs/tests/GuestMlock.cpp +++ b/core/libs/tests/GuestMlock.cpp @@ -3,6 +3,12 @@ #include #include #include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif #if defined(__linux__) #include #include @@ -13,6 +19,7 @@ int APS5_VABI sceKernelMapFlexibleMemory(void**, std::size_t, int, int); int APS5_VABI sceKernelReserveVirtualRange(void**, std::size_t, int, std::size_t); int APS5_VABI sceKernelMunmap(void*, std::size_t); int APS5_VABI mlock_nid_postfix(const void*, std::size_t); +int APS5_VABI munlock_nid_postfix(const void*, std::size_t); int* APS5_VABI __error_nid_postfix(); } @@ -55,18 +62,57 @@ int main() { Require(mlock_nid_postfix(mapped, length) == 0); bytes[length - 1] = 7; + *__error_nid_postfix() = untouched; + Require(munlock_nid_postfix(mapped, 0) == 0); + Require(*__error_nid_postfix() == untouched); + Require(munlock_nid_postfix(bytes + 1, length - page) == 0); +#if defined(__linux__) + Require(LockedKilobytes() == lockedBefore); +#elif defined(_WIN32) + Require(!VirtualUnlock(mapped, length) && GetLastError() == ERROR_NOT_LOCKED); +#endif + *__error_nid_postfix() = untouched; + Require(munlock_nid_postfix(mapped, length) == 0); + Require(*__error_nid_postfix() == untouched); + bytes[0] = 7; + Require(mlock_nid_postfix(mapped, page) == 0); +#if defined(__linux__) + Require(LockedKilobytes() - lockedBefore == page / 1024); +#endif + Require(munlock_nid_postfix(mapped, length) == 0); +#if defined(__linux__) + Require(LockedKilobytes() == lockedBefore); +#elif defined(_WIN32) + Require(!VirtualUnlock(mapped, page) && GetLastError() == ERROR_NOT_LOCKED); +#endif + Require(mlock_nid_postfix(mapped, page) == 0); + Require(munlock_nid_postfix(mapped, page) == 0); +#if defined(__linux__) + Require(LockedKilobytes() == lockedBefore); +#elif defined(_WIN32) + Require(!VirtualUnlock(mapped, page) && GetLastError() == ERROR_NOT_LOCKED); +#endif + *__error_nid_postfix() = 0; Require(mlock_nid_postfix(reinterpret_cast(std::numeric_limits::max() - page + 1), page * 2) == -1); Require(*__error_nid_postfix() == 22); + *__error_nid_postfix() = 0; + Require(munlock_nid_postfix(reinterpret_cast(std::numeric_limits::max() - page + 1), page * 2) == -1); + Require(*__error_nid_postfix() == 22); void* reserved = nullptr; Require(sceKernelReserveVirtualRange(&reserved, page, 0, 0) == 0); *__error_nid_postfix() = 0; Require(mlock_nid_postfix(reserved, page) == -1 && *__error_nid_postfix() == 12); + *__error_nid_postfix() = untouched; + Require(munlock_nid_postfix(reserved, page) == 0); + Require(*__error_nid_postfix() == untouched); Require(sceKernelMunmap(reserved, page) == 0); Require(sceKernelMunmap(mapped, length) == 0); *__error_nid_postfix() = 0; Require(mlock_nid_postfix(mapped, page) == -1 && *__error_nid_postfix() == 12); + *__error_nid_postfix() = 0; + Require(munlock_nid_postfix(mapped, page) == -1 && *__error_nid_postfix() == 12); return 0; }