From 93a214836ffa446564c6fc1216aa8a0f42440c60 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Mon, 28 Sep 2026 13:32:36 +1300 Subject: [PATCH] fix(libkernel): build sceKernelVirtualQuery on linux The host-region fallback added with the Demon's Souls work calls VirtualQuery and includes windows.h unconditionally, so libkernel no longer compiles on Linux. On Linux, read the containing mapping and its permissions from /proc/self/maps. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ --- .../prx/libkernel/DirectMemory/Export.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/core/libs/prx/libkernel/DirectMemory/Export.cpp b/core/libs/prx/libkernel/DirectMemory/Export.cpp index 14d1333d8..b39ce5add 100644 --- a/core/libs/prx/libkernel/DirectMemory/Export.cpp +++ b/core/libs/prx/libkernel/DirectMemory/Export.cpp @@ -5,9 +5,14 @@ #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestAllocations.hpp" #include "DirectMemory.hpp" +#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include +#else +#include +#include +#endif #include #include #include @@ -179,12 +184,33 @@ int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInf } // Memory the registry does not know (the title's own heap blocks, stacks): the host's committed // region around the address is the honest extent; unmapped memory is an error, as on the PS5. + #ifdef _WIN32 MEMORY_BASIC_INFORMATION host{}; if (VirtualQuery(addr, &host, sizeof(host)) == 0 || host.State != MEM_COMMIT) return SCE_KERNEL_ERROR_EACCES; info->start = reinterpret_cast(host.BaseAddress); info->end = info->start + host.RegionSize; const bool writable = (host.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY)) != 0; const bool executable = (host.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0; + #else + std::ifstream maps("/proc/self/maps"); + std::string line; + bool found = false, writable = false, executable = false; + while (std::getline(maps, line)) { + std::istringstream fields(line); + uintptr_t begin = 0, end = 0; + char dash = 0; + std::string perms; + if (!(fields >> std::hex >> begin >> dash >> end >> perms) || address < begin || address >= end) continue; + if (perms.size() < 3 || perms[0] != 'r') return SCE_KERNEL_ERROR_EACCES; + info->start = begin; + info->end = end; + writable = perms[1] == 'w'; + executable = perms[2] == 'x'; + found = true; + break; + } + if (!found) return SCE_KERNEL_ERROR_EACCES; + #endif info->protection = 1 | (writable ? 2 : 0) | (executable ? 4 : 0); info->is_flexible = 1; info->is_committed = 1;