diff --git a/core/libs/prx/libc/include/GuestArena.hpp b/core/libs/prx/libc/include/GuestArena.hpp index 8d7972549..fa3f3c3f3 100644 --- a/core/libs/prx/libc/include/GuestArena.hpp +++ b/core/libs/prx/libc/include/GuestArena.hpp @@ -13,6 +13,7 @@ extern "C" { bool GuestArenaAvailable_nid_postfix(); bool GuestArenaContains_nid_postfix(const void* pointer, std::size_t bytes); void* GuestArenaAllocate_nid_postfix(std::size_t bytes, std::size_t alignment); +void* GuestArenaAllocateAtOrAbove_nid_postfix(std::uintptr_t hint, std::size_t bytes, std::size_t alignment); void GuestArenaMarkUsed_nid_postfix(const void* pointer, std::size_t bytes); void GuestArenaRelease_nid_postfix(const void* pointer, std::size_t bytes); // The reserved range, and whether it was reserved with page write watching (Windows MEM_WRITE_WATCH). diff --git a/core/libs/prx/libc/src/GuestArena.cpp b/core/libs/prx/libc/src/GuestArena.cpp index dc87e282e..fa25fc7c9 100644 --- a/core/libs/prx/libc/src/GuestArena.cpp +++ b/core/libs/prx/libc/src/GuestArena.cpp @@ -44,15 +44,23 @@ public: } void* Allocate(std::size_t bytes, std::size_t alignment) { + return AllocateAtOrAbove(0, bytes, alignment); + } + + void* AllocateAtOrAbove(std::uintptr_t hint, std::size_t bytes, std::size_t alignment) { if (_base == 0) throw std::runtime_error("guest address space arena is unavailable"); if (alignment == 0 || (alignment & (alignment - 1)) != 0) throw std::invalid_argument("invalid guest arena alignment"); std::lock_guard lock(_lock); - std::uintptr_t candidate = alignUp(_base, alignment); - for (const auto& [start, end] : _used) { - if (candidate + bytes <= start) break; + if (hint >= _end) throw std::runtime_error("mapping address hint is above the guest address space arena"); + std::uintptr_t candidate = alignUp(std::max(_base, hint), alignment); + auto it = _used.upper_bound(candidate); + if (it != _used.begin() && std::prev(it)->second > candidate) --it; + for (; it != _used.end(); ++it) { + const auto& [start, end] = *it; + if (candidate <= _end && bytes <= _end - candidate && candidate + bytes <= start) break; candidate = std::max(candidate, alignUp(end, alignment)); } - if (bytes > _end - candidate) throw std::runtime_error("guest address space arena exhausted"); + if (candidate > _end || bytes > _end - candidate) throw std::runtime_error("guest address space arena exhausted"); _used.emplace(candidate, candidate + bytes); return reinterpret_cast(candidate); } @@ -133,6 +141,10 @@ void* GuestArenaAllocate_nid_postfix(std::size_t bytes, std::size_t alignment) { return Arena::Get().Allocate(bytes, alignment); } +void* GuestArenaAllocateAtOrAbove_nid_postfix(std::uintptr_t hint, std::size_t bytes, std::size_t alignment) { + return Arena::Get().AllocateAtOrAbove(hint, bytes, alignment); +} + void GuestArenaMarkUsed_nid_postfix(const void* pointer, std::size_t bytes) { Arena::Get().MarkUsed(pointer, bytes); } diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp index 6e8fc15b4..6187e79d0 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp @@ -61,9 +61,9 @@ static void CommitArenaRange(void* addr, size_t len, DWORD winProt) { GuestArena::GuestArenaCommit_nid_postfix(addr, len, winProt, PS5_PAGE_SIZE); } -static void* mmap_aligned(size_t len, int prot, size_t alignment) { +static void* mmap_aligned(size_t len, int prot, size_t alignment, std::uintptr_t hint = 0) { auto& arena = KernelArena::Get(); - void* result = arena.Allocate(len, alignment); + void* result = GuestArena::GuestArenaAllocateAtOrAbove_nid_postfix(hint, len, alignment); if (prot != PROT_NONE) { try { CommitArenaRange(result, len, WinProtFromPosix(prot)); @@ -389,11 +389,7 @@ void* MapPlaced(void* addr, size_t len, int prot, int flags, size_t alignment) { constexpr int GuestMapFixed = 0x10; constexpr int GuestMapNoOverwrite = 0x80; constexpr int GuestMapNoCoalesce = 0x400000; -#if defined(__linux__) constexpr int SupportedFlags = GuestMapFixed | GuestMapNoOverwrite | GuestMapNoCoalesce; -#else - constexpr int SupportedFlags = GuestMapFixed | GuestMapNoCoalesce; -#endif if ((flags & ~SupportedFlags) != 0) { char message[64]; std::snprintf(message, sizeof(message), "Unsupported memory mapping flags 0x%x", flags); @@ -417,7 +413,7 @@ void* MapPlaced(void* addr, size_t len, int prot, int flags, size_t alignment) { #if defined(__linux__) return MapAtOrAbove(reinterpret_cast(addr), len, prot, alignment); #else - throw std::invalid_argument("Non-fixed mapping address hints are not implemented"); + return mmap_aligned(len, prot, alignment, reinterpret_cast(addr)); #endif } #ifdef _WIN32 diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index 94fd6eb02..b84537b50 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -65,16 +65,20 @@ static void CheckNamedAndHintedMappings() { Require(sceKernelClearVirtualRangeName(first, length) == 0); Require(NameAt(middle)[0] == '\0'); Require(sceKernelSetVirtualRangeName(nullptr, length, "x") != 0); -#if defined(__linux__) void* hinted = first; Require(sceKernelMapFlexibleMemory(&hinted, length, 3, 0) == 0); Require(hinted > first && (reinterpret_cast(hinted) & 0x3fff) == 0); + static_cast(hinted)[length - 1] = 1; bool rejected = false; void* overwrite = first; try { sceKernelMapFlexibleMemory(&overwrite, 0x4000, 3, 0x90); } catch (const std::exception&) { rejected = true; } Require(rejected && overwrite == first); Require(sceKernelMunmap(hinted, length) == 0); -#endif + void* exclusive = hinted; + Require(sceKernelMapFlexibleMemory(&exclusive, length, 3, 0x90) == 0); + Require(exclusive == hinted); + static_cast(exclusive)[0] = 1; + Require(sceKernelMunmap(exclusive, length) == 0); Require(sceKernelMunmap(first, length) == 0); }