From 3bea115487b416fdc2d1f663ae24479ec0329a88 Mon Sep 17 00:00:00 2001 From: Pacuka <69674426+Pacuka@users.noreply.github.com> Date: Mon, 5 Oct 2026 22:04:05 +0200 Subject: [PATCH] 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 --- .../Execution/src/GuestMemory.cpp | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp index 8108098fd..48b9f31c4 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp @@ -26,9 +26,43 @@ #endif #include #else +#include +#include +#include #include #include #include +#include +#include +#if __has_include() +#include +#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 @@ -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(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(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(emit(PageRun{cursor, end, false, false})); + return true; + } + } std::ifstream maps("/proc/self/maps"); if (!maps.is_open()) return false; std::string line;