mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #286 from SP4C3B4R-8/feat/windows-mapping-hints
feat(libkernel): support mapping address hints and no-overwrite fixed mappings on Windows
This commit is contained in:
@@ -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).
|
||||
|
||||
@@ -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<void*>(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);
|
||||
}
|
||||
|
||||
@@ -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<std::uintptr_t>(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<std::uintptr_t>(addr));
|
||||
#endif
|
||||
}
|
||||
#ifdef _WIN32
|
||||
|
||||
@@ -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<std::uintptr_t>(hinted) & 0x3fff) == 0);
|
||||
static_cast<volatile unsigned char*>(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<volatile unsigned char*>(exclusive)[0] = 1;
|
||||
Require(sceKernelMunmap(exclusive, length) == 0);
|
||||
Require(sceKernelMunmap(first, length) == 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user