diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 257f41fba..1edbc7e5b 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -477,6 +477,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/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 bac216bbc..ba58cd6ea 100644 --- a/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp @@ -5,6 +5,8 @@ #include 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. @@ -97,4 +99,18 @@ 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; +} + +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 new file mode 100644 index 000000000..044cb6828 --- /dev/null +++ b/core/libs/tests/GuestMlock.cpp @@ -0,0 +1,118 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif +#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 munlock_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() = 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; +}