mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
perf(agc): query guest page access with PROCMAP_QUERY on Linux
describePages parsed the whole /proc/self/maps for every run it reported. On kernels with the PROCMAP_QUERY ioctl (Linux 6.11+) each run is now one ioctl on an open /proc/self/maps fd with PROCMAP_QUERY_COVERING_OR_NEXT_VMA, which returns the covering or next VMA and its read/write flags straight from the kernel, so the answer is exact for host mappings too. A VMA starting above the cursor is a gap up to its start and ENOENT is a gap to the end. Kernels without the ioctl keep the line scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -26,9 +26,43 @@
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <pthread.h>
|
||||
#include <sstream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#if __has_include(<linux/fs.h>)
|
||||
#include <linux/fs.h>
|
||||
#endif
|
||||
#if !defined(PROCMAP_QUERY)
|
||||
struct procmap_query {
|
||||
std::uint64_t size;
|
||||
std::uint64_t query_flags;
|
||||
std::uint64_t query_addr;
|
||||
std::uint64_t vma_start;
|
||||
std::uint64_t vma_end;
|
||||
std::uint64_t vma_flags;
|
||||
std::uint64_t vma_page_size;
|
||||
std::uint64_t vma_offset;
|
||||
std::uint64_t inode;
|
||||
std::uint32_t dev_major;
|
||||
std::uint32_t dev_minor;
|
||||
std::uint32_t vma_name_size;
|
||||
std::uint32_t build_id_size;
|
||||
std::uint64_t vma_name_addr;
|
||||
std::uint64_t build_id_addr;
|
||||
};
|
||||
static_assert(sizeof(procmap_query) == 104);
|
||||
enum : std::uint64_t {
|
||||
PROCMAP_QUERY_VMA_READABLE = 0x01,
|
||||
PROCMAP_QUERY_VMA_WRITABLE = 0x02,
|
||||
PROCMAP_QUERY_COVERING_OR_NEXT_VMA = 0x10,
|
||||
};
|
||||
#define PROCMAP_QUERY _IOWR('f', 17, struct procmap_query)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace AgcDriver::GuestMemory {
|
||||
@@ -478,6 +512,23 @@ struct PageRun {
|
||||
bool writable;
|
||||
};
|
||||
|
||||
#if !defined(_WIN32)
|
||||
int ProcMapsQueryFd() {
|
||||
static const int fd = [] {
|
||||
if (std::getenv("APS5_NO_PROCMAP_QUERY") != nullptr) return -1;
|
||||
const int opened = open("/proc/self/maps", O_RDONLY | O_CLOEXEC);
|
||||
if (opened < 0) return -1;
|
||||
procmap_query probe{};
|
||||
probe.size = sizeof(probe);
|
||||
probe.query_flags = PROCMAP_QUERY_COVERING_OR_NEXT_VMA;
|
||||
if (ioctl(opened, PROCMAP_QUERY, &probe) == 0 || errno == ENOENT) return opened;
|
||||
close(opened);
|
||||
return -1;
|
||||
}();
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Calls `emit` with consecutive runs of uniform accessibility covering [address, address + bytes) in
|
||||
// order, stopping early when it returns false. Returns false when the address space cannot be queried.
|
||||
template <class Emit>
|
||||
@@ -535,6 +586,30 @@ bool describePages(std::uintptr_t address, std::size_t bytes, Emit&& emit) {
|
||||
if (!emit(PageRun{cursor, next, readable, writable})) return true;
|
||||
cursor = next;
|
||||
#else
|
||||
if (const int fd = ProcMapsQueryFd(); fd >= 0) {
|
||||
procmap_query query{};
|
||||
query.size = sizeof(query);
|
||||
query.query_flags = PROCMAP_QUERY_COVERING_OR_NEXT_VMA;
|
||||
query.query_addr = cursor;
|
||||
if (ioctl(fd, PROCMAP_QUERY, &query) == 0) {
|
||||
if (query.vma_end <= cursor || query.vma_start >= query.vma_end) return false;
|
||||
if (query.vma_start > cursor) {
|
||||
const auto gapEnd = std::min<std::uintptr_t>(end, query.vma_start);
|
||||
if (!emit(PageRun{cursor, gapEnd, false, false})) return true;
|
||||
cursor = gapEnd;
|
||||
continue;
|
||||
}
|
||||
const bool readable = (query.vma_flags & PROCMAP_QUERY_VMA_READABLE) != 0;
|
||||
const auto next = std::min<std::uintptr_t>(end, query.vma_end);
|
||||
if (!emit(PageRun{cursor, next, readable, readable && (query.vma_flags & PROCMAP_QUERY_VMA_WRITABLE) != 0})) return true;
|
||||
cursor = next;
|
||||
continue;
|
||||
}
|
||||
if (errno == ENOENT) {
|
||||
static_cast<void>(emit(PageRun{cursor, end, false, false}));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
std::ifstream maps("/proc/self/maps");
|
||||
if (!maps.is_open()) return false;
|
||||
std::string line;
|
||||
|
||||
Reference in New Issue
Block a user