Skip to content
Merged
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
1 change: 1 addition & 0 deletions core/libs/prx/libc/include/GuestAllocations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void GuestAllocationsRemove_nid_postfix(void* mutation, const void* pointer);
void GuestAllocationsProtect_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, bool readable, bool writable, const std::function<void()>& apply);
void GuestAllocationsUnmap_nid_postfix(void* mutation, const void* pointer, std::size_t bytes, const std::function<void(const void*, std::size_t, const void*, bool)>& apply);
Lease GuestAllocationsAcquire_nid_postfix();
Lease GuestAllocationsAcquireAll_nid_postfix();
// Changes whenever a mutation ends, so callers can cache facts about guest mappings between changes.
std::uint64_t GuestAllocationsGeneration_nid_postfix();
// Precise invalidation for such caches: the callback runs, after the generation changed, for every
Expand Down
10 changes: 10 additions & 0 deletions core/libs/prx/libc/src/GuestAllocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,14 @@ Lease GuestAllocationsAcquire_nid_postfix() {
return result;
}

Lease GuestAllocationsAcquireAll_nid_postfix() {
std::lock_guard lock(registry().mutex);
Lease result;
result.reserve(registry().ranges.size());
for (const auto& [address, range] : registry().ranges) {
if (range->bytes != 0) result.push_back(range);
}
return result;
}

}
17 changes: 8 additions & 9 deletions core/libs/prx/libkernel/DirectMemory/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sys/resource.h>
#endif
#include <algorithm>
#include <cassert>
#include <limits>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -228,9 +229,6 @@ int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInf
if (!info || info_size < sizeof(VirtualQueryInfo)) return SCE_KERNEL_ERROR_EINVAL;
memset(info, 0, sizeof(VirtualQueryInfo));
const auto address = reinterpret_cast<uintptr_t>(addr);
// The mapping that contains the address, or with SCE_KERNEL_VQ_FIND_NEXT (flags bit 0) the first
// mapping at or above it: titles walk their mappings and check that a mapping covers a whole
// allocation, so the answer must be the registered allocation, not a page.
constexpr int findNext = 1;
std::uintptr_t reservedStart = 0;
std::uintptr_t reservedEnd = 0;
Expand All @@ -239,17 +237,18 @@ int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInf
info->end = reservedEnd;
return 0;
}
const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix();
const auto lease = GuestAllocations::GuestAllocationsAcquireAll_nid_postfix();
assert(std::is_sorted(lease.begin(), lease.end(), [](const auto& left, const auto& right) { return left->address < right->address; }));
const GuestAllocations::Range* best = nullptr;
for (const auto& range : lease) {
const auto begin = range->allocationAddress;
const auto end = begin + range->allocationBytes;
const auto begin = range->address;
const auto end = begin + range->bytes;
if (address >= begin && address < end) { best = range.get(); break; }
if ((flags & findNext) != 0 && begin > address && (best == nullptr || begin < best->allocationAddress)) best = range.get();
if ((flags & findNext) != 0 && begin > address && (best == nullptr || begin < best->address)) best = range.get();
}
if (best != nullptr) {
info->start = best->allocationAddress;
info->end = best->allocationAddress + best->allocationBytes;
info->start = best->address;
info->end = best->address + best->bytes;
info->protection = (best->readable ? 1 : 0) | (best->writable ? 2 : 0) | (!best->releasable ? 4 : 0);
int recorded = 0;
if (GuestProtection(std::max<uintptr_t>(address, info->start), &recorded)) info->protection = recorded;
Expand Down
77 changes: 77 additions & 0 deletions core/libs/tests/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,85 @@ static void CheckDirectMemoryWriteWatch() {
}
#endif

static void CheckVirtualQuerySplitFlexibleRanges() {
constexpr std::size_t page = 0x4000;
void* mapped = nullptr;
Require(sceKernelMapFlexibleMemory(&mapped, page * 3, 3, 0) == 0);
auto* bytes = static_cast<unsigned char*>(mapped);
bytes[0] = 17;
bytes[page * 2] = 29;
Require(sceKernelMprotect(bytes + page, page, 1) == 0);
for (std::size_t index = 0; index < 3; ++index) {
VirtualQueryInfo info{};
Require(sceKernelVirtualQuery(bytes + page * index, 0, &info, sizeof(info)) == 0);
Require(info.start == reinterpret_cast<std::uintptr_t>(bytes + page * index));
Require(info.end == info.start + page);
Require(info.protection == (index == 1 ? 1 : 3));
Require(info.is_committed && info.is_flexible && !info.is_direct);
}
Require(sceKernelMunmap(bytes + page, page) == 0);
for (std::size_t index : {std::size_t{0}, std::size_t{2}}) {
VirtualQueryInfo info{};
Require(sceKernelVirtualQuery(bytes + page * index, 0, &info, sizeof(info)) == 0);
Require(info.start == reinterpret_cast<std::uintptr_t>(bytes + page * index));
Require(info.end == info.start + page);
}
VirtualQueryInfo next{};
Require(sceKernelVirtualQuery(bytes + page, 1, &next, sizeof(next)) == 0);
Require(next.start == reinterpret_cast<std::uintptr_t>(bytes + page * 2));
Require(next.end == next.start + page && next.protection == 3);
Require(bytes[0] == 17 && bytes[page * 2] == 29);
Require(sceKernelMunmap(bytes, page) == 0);
Require(sceKernelMunmap(bytes + page * 2, page) == 0);
}

static void CheckVirtualQueryForNonReadableGuestRange() {
constexpr std::size_t page = 0x4000;
auto* mapped = static_cast<unsigned char*>(mmap_nid_postfix(nullptr, page, 3, 0x1002, -1, 0));
const auto failed = reinterpret_cast<void*>(static_cast<std::uintptr_t>(-1));
Require(mapped != failed);
Require(mprotect_nid_postfix(mapped, page, 0) == 0);
VirtualQueryInfo info{};
Require(sceKernelVirtualQuery(mapped, 0, &info, sizeof(info)) == 0);
Require(info.start == reinterpret_cast<std::uintptr_t>(mapped));
Require(info.end == info.start + page && info.protection == 0);
Require(sceKernelMunmap(mapped, page) == 0);
}

static void CheckVirtualQueryPartialMunmap() {
constexpr std::size_t page = 0x4000;
const auto failed = reinterpret_cast<void*>(static_cast<std::uintptr_t>(-1));
void* raw = mmap_nid_postfix(nullptr, page * 3, 3, 0x1002, -1, 0);
Require(raw != failed);
auto* memory = static_cast<unsigned char*>(raw);
VirtualQueryInfo before{};
Require(sceKernelVirtualQuery(memory, 0, &before, sizeof(before)) == 0);
Require(before.start == reinterpret_cast<std::uintptr_t>(memory));
Require(before.end == reinterpret_cast<std::uintptr_t>(memory) + page * 3);
Require(munmap_nid_postfix(memory + page, page) == 0);
VirtualQueryInfo first{};
Require(sceKernelVirtualQuery(memory, 0, &first, sizeof(first)) == 0);
Require(first.start == reinterpret_cast<std::uintptr_t>(memory));
Require(first.end == reinterpret_cast<std::uintptr_t>(memory) + page);
VirtualQueryInfo middle{};
Require(sceKernelVirtualQuery(memory + page, 0, &middle, sizeof(middle)) == SCE_KERNEL_ERROR_EACCES);
VirtualQueryInfo next{};
Require(sceKernelVirtualQuery(memory + page, 1, &next, sizeof(next)) == 0);
Require(next.start == reinterpret_cast<std::uintptr_t>(memory) + page * 2);
Require(next.end == reinterpret_cast<std::uintptr_t>(memory) + page * 3);
VirtualQueryInfo tail{};
Require(sceKernelVirtualQuery(memory + page * 2, 0, &tail, sizeof(tail)) == 0);
Require(tail.start == reinterpret_cast<std::uintptr_t>(memory) + page * 2);
Require(tail.end == reinterpret_cast<std::uintptr_t>(memory) + page * 3);
Require(munmap_nid_postfix(memory, page) == 0);
Require(munmap_nid_postfix(memory + page * 2, page) == 0);
}

int main() {
CheckVirtualQuerySplitFlexibleRanges();
CheckVirtualQueryForNonReadableGuestRange();
CheckReleaseFlexibleMemory();
CheckVirtualQueryPartialMunmap();
CheckNamedAndHintedMappings();
CheckInternalNamedFlexibleMapping();
CheckBatchMapStopsAtInvalidEntry();
Expand Down
Loading