mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #203 from oneandonlydean/fix/libkernel-direct-memory-memfd
fix(libkernel): back direct memory with a memfd on Linux
This commit is contained in:
@@ -228,11 +228,11 @@ public:
|
||||
section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast<DWORD>(size >> 32), static_cast<DWORD>(size), nullptr);
|
||||
if (!section) throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "create direct memory backing");
|
||||
#else
|
||||
file = std::tmpfile();
|
||||
if (!file) throw std::system_error(errno, std::generic_category(), "create direct memory backing");
|
||||
if (ftruncate(fileno(file), static_cast<off_t>(bytes)) != 0) {
|
||||
file = memfd_create("direct memory", MFD_CLOEXEC);
|
||||
if (file < 0) throw std::system_error(errno, std::generic_category(), "create direct memory backing");
|
||||
if (ftruncate(file, static_cast<off_t>(bytes)) != 0) {
|
||||
const int error = errno;
|
||||
std::fclose(file);
|
||||
::close(file);
|
||||
throw std::system_error(error, std::generic_category(), "size direct memory backing");
|
||||
}
|
||||
#endif
|
||||
@@ -244,7 +244,7 @@ public:
|
||||
#ifdef _WIN32
|
||||
CloseHandle(section);
|
||||
#else
|
||||
std::fclose(file);
|
||||
::close(file);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ public:
|
||||
#ifdef _WIN32
|
||||
GuestArena::GuestArenaMap_nid_postfix(reinterpret_cast<void*>(address), bytes, section, offset, WinProtFromPosix(protection));
|
||||
#else
|
||||
if (::mmap(reinterpret_cast<void*>(address), bytes, protection, MAP_SHARED | MAP_FIXED, fileno(file), static_cast<off_t>(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing");
|
||||
if (::mmap(reinterpret_cast<void*>(address), bytes, protection, MAP_SHARED | MAP_FIXED, file, static_cast<off_t>(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ private:
|
||||
#ifdef _WIN32
|
||||
HANDLE section = nullptr;
|
||||
#else
|
||||
std::FILE* file = nullptr;
|
||||
int file = -1;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#if defined(__linux__)
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@@ -373,6 +375,30 @@ static void CheckWriteWatch() {
|
||||
Require(munmap(raw, spanned + tableSpan) == 0);
|
||||
}
|
||||
|
||||
static std::string BackingOf(const void* address) {
|
||||
std::ifstream maps("/proc/self/maps");
|
||||
std::string line;
|
||||
while (std::getline(maps, line)) {
|
||||
if (std::strtoull(line.c_str(), nullptr, 16) != reinterpret_cast<std::uintptr_t>(address)) continue;
|
||||
const auto path = line.find('/');
|
||||
return path == std::string::npos ? std::string() : line.substr(path);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static void CheckDirectMemoryBackingNeedsNoFilesystem() {
|
||||
constexpr std::size_t page = 0x4000;
|
||||
std::int64_t phys = 0;
|
||||
Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page, 0, 0, &phys) == 0);
|
||||
void* mapped = nullptr;
|
||||
Require(sceKernelMapDirectMemory(&mapped, page, 3, 0, phys, 0) == 0);
|
||||
const auto backing = BackingOf(mapped);
|
||||
if (backing.rfind("/memfd:", 0) != 0) std::fprintf(stderr, "direct memory backing: %s\n", backing.c_str());
|
||||
Require(backing.rfind("/memfd:", 0) == 0);
|
||||
Require(sceKernelMunmap(mapped, page) == 0);
|
||||
Require(sceKernelReleaseDirectMemory(phys, page) == 0);
|
||||
}
|
||||
|
||||
static void CheckDirectMemoryWriteWatch() {
|
||||
if (!GuestWriteWatch::GuestWriteWatchAvailable_nid_postfix()) {
|
||||
std::puts("write watch unavailable: direct memory not tested");
|
||||
@@ -434,6 +460,7 @@ int main() {
|
||||
#if defined(__linux__)
|
||||
CheckWriteWatch();
|
||||
CheckDirectMemoryWriteWatch();
|
||||
CheckDirectMemoryBackingNeedsNoFilesystem();
|
||||
#endif
|
||||
constexpr std::size_t page = 0x4000;
|
||||
const auto failed = reinterpret_cast<void*>(static_cast<std::uintptr_t>(-1));
|
||||
|
||||
Reference in New Issue
Block a user