2026-01-12 05:18:12 -05:00
|
|
|
// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
|
2024-07-30 13:42:36 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2022-05-18 23:27:23 +10:00
|
|
|
#include "common/Assertions.h"
|
2024-05-07 01:37:56 +10:00
|
|
|
#include "common/BitUtils.h"
|
2022-05-09 20:11:30 +10:00
|
|
|
#include "common/Console.h"
|
2024-05-29 20:30:53 +10:00
|
|
|
#include "common/CrashHandler.h"
|
2024-05-07 01:37:56 +10:00
|
|
|
#include "common/Error.h"
|
2023-12-26 21:25:10 +10:00
|
|
|
#include "common/HostSys.h"
|
2021-09-01 16:31:46 -04:00
|
|
|
|
2024-05-29 20:30:53 +10:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <csignal>
|
|
|
|
|
#include <cerrno>
|
2026-07-12 16:39:22 +02:00
|
|
|
#include <cstdlib>
|
2026-06-10 00:20:33 -04:00
|
|
|
#include <cstring>
|
2024-05-29 20:30:53 +10:00
|
|
|
#include <fcntl.h>
|
2026-07-12 16:39:22 +02:00
|
|
|
#include <limits.h>
|
2024-05-29 20:30:53 +10:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <unistd.h>
|
2026-07-12 16:39:22 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
#include <libkern/OSCacheControl.h>
|
|
|
|
|
#include <TargetConditionals.h>
|
|
|
|
|
#endif
|
2024-08-21 02:11:24 -05:00
|
|
|
#ifndef __APPLE__
|
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
#endif
|
2024-05-29 20:30:53 +10:00
|
|
|
|
2025-01-15 21:26:10 +00:00
|
|
|
#include "fmt/format.h"
|
2015-11-17 19:38:26 +02:00
|
|
|
|
2026-07-08 21:46:23 -04:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
// bionic lacks shm_open until API 30; memfd_create is a libc wrapper only from
|
|
|
|
|
// API 30 too, but the raw syscall works from API 26 (our minSdk).
|
|
|
|
|
static int memfd_create_wrapper(const char* name, unsigned int flags)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<int>(syscall(__NR_memfd_create, name, flags));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-07-11 15:57:28 +01:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
|
#include "cpuinfo.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-12 23:57:53 +10:00
|
|
|
static __ri uint LinuxProt(const PageProtectionMode& mode)
|
2010-11-05 01:33:01 +00:00
|
|
|
{
|
2022-10-12 23:57:53 +10:00
|
|
|
u32 lnxmode = 0;
|
2010-11-05 01:33:01 +00:00
|
|
|
|
2021-09-06 14:28:26 -04:00
|
|
|
if (mode.CanWrite())
|
|
|
|
|
lnxmode |= PROT_WRITE;
|
|
|
|
|
if (mode.CanRead())
|
|
|
|
|
lnxmode |= PROT_READ;
|
|
|
|
|
if (mode.CanExecute())
|
|
|
|
|
lnxmode |= PROT_EXEC | PROT_READ;
|
2010-11-05 01:33:01 +00:00
|
|
|
|
2022-10-12 23:57:53 +10:00
|
|
|
return lnxmode;
|
2010-11-05 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-06 14:28:26 -04:00
|
|
|
void HostSys::MemProtect(void* baseaddr, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2023-12-22 20:30:31 +10:00
|
|
|
pxAssertMsg((size & (__pagesize - 1)) == 0, "Size is page aligned");
|
2022-10-12 23:57:53 +10:00
|
|
|
|
|
|
|
|
const u32 lnxmode = LinuxProt(mode);
|
|
|
|
|
|
|
|
|
|
const int result = mprotect(baseaddr, size, lnxmode);
|
|
|
|
|
if (result != 0)
|
|
|
|
|
pxFail("mprotect() failed");
|
2009-11-16 00:40:09 +00:00
|
|
|
}
|
2022-10-12 23:57:53 +10:00
|
|
|
|
|
|
|
|
std::string HostSys::GetFileMappingName(const char* prefix)
|
|
|
|
|
{
|
|
|
|
|
const unsigned pid = static_cast<unsigned>(getpid());
|
2022-10-27 16:51:37 +02:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
|
// FreeBSD's shm_open(3) requires name to be absolute
|
|
|
|
|
return fmt::format("/tmp/{}_{}", prefix, pid);
|
|
|
|
|
#else
|
2022-10-12 23:57:53 +10:00
|
|
|
return fmt::format("{}_{}", prefix, pid);
|
2022-10-27 16:51:37 +02:00
|
|
|
#endif
|
2022-10-12 23:57:53 +10:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 16:39:22 +02:00
|
|
|
#if defined(__APPLE__) && TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
|
|
|
|
|
static int CreateIOSFileBackedSharedMemory(const char* name, size_t size, int shm_errno)
|
|
|
|
|
{
|
|
|
|
|
const char* tmpdir = std::getenv("TMPDIR");
|
|
|
|
|
if (!tmpdir || tmpdir[0] == '\0')
|
|
|
|
|
tmpdir = "/tmp";
|
|
|
|
|
|
|
|
|
|
for (int attempt = 0; attempt < 16; attempt++)
|
|
|
|
|
{
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
const int written = std::snprintf(path, sizeof(path), "%s/armsx2_%s_%u_%d.mem",
|
|
|
|
|
tmpdir, name, static_cast<unsigned>(getpid()), attempt);
|
|
|
|
|
if (written <= 0 || static_cast<size_t>(written) >= sizeof(path))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
const int fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno == EEXIST)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@IOS_SHM_FILE_FALLBACK_FAIL@@ shm_open_errno=%d open_errno=%d path=\"%s\" size=%zu\n",
|
|
|
|
|
shm_errno, errno, path, size);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
|
|
|
|
|
std::fprintf(stderr, "@@IOS_SHM_CLOEXEC_FAIL@@ errno=%d fd=%d\n", errno, fd);
|
|
|
|
|
|
|
|
|
|
if (ftruncate(fd, static_cast<off_t>(size)) < 0)
|
|
|
|
|
{
|
|
|
|
|
const int truncate_errno = errno;
|
|
|
|
|
unlink(path);
|
|
|
|
|
close(fd);
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@IOS_SHM_FILE_FALLBACK_FAIL@@ shm_open_errno=%d ftruncate_errno=%d path=\"%s\" size=%zu\n",
|
|
|
|
|
shm_errno, truncate_errno, path, size);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int unlink_errno = (unlink(path) != 0) ? errno : 0;
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@IOS_SHM_FILE_FALLBACK@@ shm_open_errno=%d fd=%d size=%zu path=\"%s\" unlink_errno=%d\n",
|
|
|
|
|
shm_errno, fd, size, path, unlink_errno);
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@IOS_SHM_FILE_FALLBACK_FAIL@@ shm_open_errno=%d reason=path_exhausted size=%zu\n",
|
|
|
|
|
shm_errno, size);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-12 23:57:53 +10:00
|
|
|
void* HostSys::CreateSharedMemory(const char* name, size_t size)
|
|
|
|
|
{
|
2026-07-08 21:46:23 -04:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
// Android: memfd_create available since API 26 (our minSdk), no shm_open until API 30.
|
|
|
|
|
const int fd = memfd_create_wrapper(name, 0);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
std::fprintf(stderr, "memfd_create failed: %d\n", errno);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2022-10-12 23:57:53 +10:00
|
|
|
const int fd = shm_open(name, O_CREAT | O_EXCL | O_RDWR, 0600);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
2026-07-12 16:39:22 +02:00
|
|
|
const int shm_errno = errno;
|
|
|
|
|
#if defined(__APPLE__) && TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
|
|
|
|
|
const int file_fd = CreateIOSFileBackedSharedMemory(name, size, shm_errno);
|
|
|
|
|
if (file_fd >= 0)
|
|
|
|
|
return reinterpret_cast<void*>(static_cast<intptr_t>(file_fd));
|
|
|
|
|
#endif
|
|
|
|
|
std::fprintf(stderr, "shm_open failed: %d\n", shm_errno);
|
2022-10-12 23:57:53 +10:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we're not going to be opening this mapping in other processes, so remove the file
|
|
|
|
|
shm_unlink(name);
|
2026-07-08 21:46:23 -04:00
|
|
|
#endif
|
2022-10-12 23:57:53 +10:00
|
|
|
|
|
|
|
|
// ensure it's the correct size
|
|
|
|
|
if (ftruncate(fd, static_cast<off_t>(size)) < 0)
|
|
|
|
|
{
|
2023-09-04 22:31:52 +10:00
|
|
|
std::fprintf(stderr, "ftruncate(%zu) failed: %d\n", size, errno);
|
2022-10-12 23:57:53 +10:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reinterpret_cast<void*>(static_cast<intptr_t>(fd));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HostSys::DestroySharedMemory(void* ptr)
|
|
|
|
|
{
|
|
|
|
|
close(static_cast<int>(reinterpret_cast<intptr_t>(ptr)));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 02:11:24 -05:00
|
|
|
#ifndef __APPLE__
|
|
|
|
|
|
2024-03-21 16:43:50 +10:00
|
|
|
size_t HostSys::GetRuntimePageSize()
|
|
|
|
|
{
|
|
|
|
|
int res = sysconf(_SC_PAGESIZE);
|
|
|
|
|
return (res > 0) ? static_cast<size_t>(res) : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t HostSys::GetRuntimeCacheLineSize()
|
|
|
|
|
{
|
2024-07-11 15:57:28 +01:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
|
if (!cpuinfo_initialize())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
u32 max_line_size = 0;
|
|
|
|
|
for (u32 i = 0; i < cpuinfo_get_processors_count(); i++)
|
|
|
|
|
{
|
|
|
|
|
const u32 l1i = cpuinfo_get_processor(i)->cache.l1i->line_size;
|
|
|
|
|
const u32 l1d = cpuinfo_get_processor(i)->cache.l1d->line_size;
|
|
|
|
|
const u32 res = std::max<u32>(l1i, l1d);
|
|
|
|
|
|
|
|
|
|
max_line_size = std::max<u32>(max_line_size, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static_cast<size_t>(max_line_size);
|
|
|
|
|
#else
|
2024-03-21 16:43:50 +10:00
|
|
|
int l1i = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
|
|
|
|
|
int l1d = sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
|
|
|
|
|
int res = (l1i > l1d) ? l1i : l1d;
|
|
|
|
|
for (int index = 0; index < 16; index++)
|
|
|
|
|
{
|
|
|
|
|
char buf[128];
|
|
|
|
|
snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu0/cache/index%d/coherency_line_size", index);
|
|
|
|
|
std::FILE* fp = std::fopen(buf, "rb");
|
|
|
|
|
if (!fp)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
std::fread(buf, sizeof(buf), 1, fp);
|
|
|
|
|
std::fclose(fp);
|
|
|
|
|
int val = std::atoi(buf);
|
|
|
|
|
res = (val > res) ? val : res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (res > 0) ? static_cast<size_t>(res) : 0;
|
2024-07-11 15:57:28 +01:00
|
|
|
#endif
|
2024-03-21 16:43:50 +10:00
|
|
|
}
|
|
|
|
|
|
2024-08-21 02:11:24 -05:00
|
|
|
#endif
|
|
|
|
|
|
2022-10-29 13:39:19 +10:00
|
|
|
SharedMemoryMappingArea::SharedMemoryMappingArea(u8* base_ptr, size_t size, size_t num_pages)
|
|
|
|
|
: m_base_ptr(base_ptr)
|
|
|
|
|
, m_size(size)
|
|
|
|
|
, m_num_pages(num_pages)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharedMemoryMappingArea::~SharedMemoryMappingArea()
|
|
|
|
|
{
|
|
|
|
|
pxAssertRel(m_num_mappings == 0, "No mappings left");
|
|
|
|
|
|
|
|
|
|
if (munmap(m_base_ptr, m_size) != 0)
|
|
|
|
|
pxFailRel("Failed to release shared memory area");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-06-20 20:27:55 -07:00
|
|
|
std::unique_ptr<SharedMemoryMappingArea> SharedMemoryMappingArea::Create(size_t size, bool jit, uptr fixed_base_hint)
|
2022-10-29 13:39:19 +10:00
|
|
|
{
|
|
|
|
|
pxAssertRel(Common::IsAlignedPow2(size, __pagesize), "Size is page aligned");
|
|
|
|
|
|
2024-08-21 03:02:09 -05:00
|
|
|
uint flags = MAP_ANONYMOUS | MAP_PRIVATE;
|
2026-07-12 16:39:22 +02:00
|
|
|
uint prot = PROT_NONE;
|
2024-08-21 03:02:09 -05:00
|
|
|
#ifdef __APPLE__
|
2026-07-12 16:39:22 +02:00
|
|
|
if (jit) {
|
2024-08-21 03:02:09 -05:00
|
|
|
flags |= MAP_JIT;
|
2026-07-12 16:39:22 +02:00
|
|
|
#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
|
|
|
|
|
// iOS rejects PROT_NONE MAP_JIT reservations on the LiveContainer/JIT path.
|
|
|
|
|
// Code memory must be born as an actual JIT mapping; callers can mprotect it
|
|
|
|
|
// afterward, but we never fall back to a non-MAP_JIT executable region.
|
|
|
|
|
prot = PROT_READ | PROT_WRITE | PROT_EXEC;
|
2024-08-21 03:02:09 -05:00
|
|
|
#endif
|
2026-07-12 16:39:22 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2026-06-20 20:27:55 -07:00
|
|
|
|
|
|
|
|
void* alloc = MAP_FAILED;
|
|
|
|
|
|
|
|
|
|
// Deterministic VA placement for the on-disk VU program cache: when a
|
|
|
|
|
// fixed base hint is given, try it (plus 256MB-stride fallback slots) with a
|
|
|
|
|
// hint-and-verify mmap (no MAP_FIXED, so we never clobber an existing
|
|
|
|
|
// mapping). If none of the slots land exactly, fall through to kernel
|
|
|
|
|
// placement — the program cache records the arena base and simply misses on
|
|
|
|
|
// a mismatch, so this trades determinism for booting, never correctness.
|
|
|
|
|
if (fixed_base_hint != 0)
|
|
|
|
|
{
|
|
|
|
|
for (int slot = 0; slot <= 10; slot++)
|
|
|
|
|
{
|
|
|
|
|
void* const want = reinterpret_cast<void*>(fixed_base_hint + (static_cast<uptr>(slot) << 28));
|
2026-07-19 10:24:29 -07:00
|
|
|
void* const got = mmap(want, size, prot, flags, -1, 0);
|
2026-06-20 20:27:55 -07:00
|
|
|
if (got == MAP_FAILED)
|
|
|
|
|
continue;
|
|
|
|
|
if (got == want)
|
|
|
|
|
{
|
|
|
|
|
alloc = got;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
munmap(got, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (alloc == MAP_FAILED)
|
2026-07-19 10:24:29 -07:00
|
|
|
alloc = mmap(nullptr, size, prot, flags, -1, 0);
|
2022-10-29 13:39:19 +10:00
|
|
|
if (alloc == MAP_FAILED)
|
2026-06-10 00:20:33 -04:00
|
|
|
{
|
|
|
|
|
const int err = errno;
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@HOST_MMAP_FAIL@@ op=reserve size=%zu jit=%d flags=0x%x prot=0x%x err=%d message=\"%s\"\n",
|
2026-07-12 16:39:22 +02:00
|
|
|
size, jit ? 1 : 0, flags, prot, err, std::strerror(err));
|
2022-10-29 13:39:19 +10:00
|
|
|
return nullptr;
|
2026-06-10 00:20:33 -04:00
|
|
|
}
|
2022-10-29 13:39:19 +10:00
|
|
|
|
|
|
|
|
return std::unique_ptr<SharedMemoryMappingArea>(new SharedMemoryMappingArea(static_cast<u8*>(alloc), size, size / __pagesize));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8* SharedMemoryMappingArea::Map(void* file_handle, size_t file_offset, void* map_base, size_t map_size, const PageProtectionMode& mode)
|
|
|
|
|
{
|
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
|
|
const uint lnxmode = LinuxProt(mode);
|
2024-08-21 03:02:09 -05:00
|
|
|
if (file_handle)
|
|
|
|
|
{
|
|
|
|
|
const int fd = static_cast<int>(reinterpret_cast<intptr_t>(file_handle));
|
|
|
|
|
// MAP_FIXED is okay here, since we've reserved the entire region, and *want* to overwrite the mapping.
|
|
|
|
|
void* const ptr = mmap(map_base, map_size, lnxmode, MAP_SHARED | MAP_FIXED, fd, static_cast<off_t>(file_offset));
|
|
|
|
|
if (ptr == MAP_FAILED)
|
2026-06-10 00:20:33 -04:00
|
|
|
{
|
|
|
|
|
const int err = errno;
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@HOST_MMAP_FAIL@@ op=map_shared base=%p size=%zu prot=0x%x err=%d message=\"%s\"\n",
|
|
|
|
|
map_base, map_size, lnxmode, err, std::strerror(err));
|
2024-08-21 03:02:09 -05:00
|
|
|
return nullptr;
|
2026-06-10 00:20:33 -04:00
|
|
|
}
|
2024-08-21 03:02:09 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// macOS doesn't seem to allow MAP_JIT with MAP_FIXED
|
|
|
|
|
// So we do the MAP_JIT in the allocation, and just mprotect here
|
|
|
|
|
// Note that this will only work the first time for a given region
|
|
|
|
|
if (mprotect(map_base, map_size, lnxmode) < 0)
|
2026-06-10 00:20:33 -04:00
|
|
|
{
|
|
|
|
|
const int err = errno;
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"@@HOST_MMAP_FAIL@@ op=mprotect base=%p size=%zu prot=0x%x err=%d message=\"%s\"\n",
|
|
|
|
|
map_base, map_size, lnxmode, err, std::strerror(err));
|
2024-08-21 03:02:09 -05:00
|
|
|
return nullptr;
|
2026-06-10 00:20:33 -04:00
|
|
|
}
|
2024-08-21 03:02:09 -05:00
|
|
|
}
|
2022-10-29 13:39:19 +10:00
|
|
|
|
|
|
|
|
m_num_mappings++;
|
2024-08-21 03:02:09 -05:00
|
|
|
return static_cast<u8*>(map_base);
|
2022-10-29 13:39:19 +10:00
|
|
|
}
|
|
|
|
|
|
2024-08-21 03:02:09 -05:00
|
|
|
bool SharedMemoryMappingArea::Unmap(void* map_base, size_t map_size, bool is_file)
|
2022-10-29 13:39:19 +10:00
|
|
|
{
|
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
|
|
if (mmap(map_base, map_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_num_mappings--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 02:11:24 -05:00
|
|
|
#ifdef ARCH_ARM64
|
|
|
|
|
|
|
|
|
|
void HostSys::FlushInstructionCache(void* address, u32 size)
|
|
|
|
|
{
|
2026-07-12 16:39:22 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
sys_icache_invalidate(address, size);
|
|
|
|
|
#else
|
2024-08-21 02:11:24 -05:00
|
|
|
__builtin___clear_cache(reinterpret_cast<char*>(address), reinterpret_cast<char*>(address) + size);
|
2026-07-12 16:39:22 +02:00
|
|
|
#endif
|
2024-08-21 02:11:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef __APPLE__ // These are done in DarwinMisc
|
|
|
|
|
|
2024-05-29 20:30:53 +10:00
|
|
|
namespace PageFaultHandler
|
|
|
|
|
{
|
|
|
|
|
static std::recursive_mutex s_exception_handler_mutex;
|
|
|
|
|
static bool s_in_exception_handler = false;
|
|
|
|
|
static bool s_installed = false;
|
|
|
|
|
} // namespace PageFaultHandler
|
|
|
|
|
|
2026-03-10 23:40:32 -05:00
|
|
|
#ifdef ARCH_ARM64
|
2024-05-29 20:30:53 +10:00
|
|
|
|
|
|
|
|
[[maybe_unused]] static bool IsStoreInstruction(const void* ptr)
|
|
|
|
|
{
|
|
|
|
|
u32 bits;
|
|
|
|
|
std::memcpy(&bits, ptr, sizeof(bits));
|
|
|
|
|
|
|
|
|
|
// Based on vixl's disassembler Instruction::IsStore().
|
|
|
|
|
// if (Mask(LoadStoreAnyFMask) != LoadStoreAnyFixed)
|
|
|
|
|
if ((bits & 0x0a000000) != 0x08000000)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if (Mask(LoadStorePairAnyFMask) == LoadStorePairAnyFixed)
|
|
|
|
|
if ((bits & 0x3a000000) == 0x28000000)
|
|
|
|
|
{
|
|
|
|
|
// return Mask(LoadStorePairLBit) == 0
|
|
|
|
|
return (bits & (1 << 22)) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (bits & 0xC4C00000)
|
|
|
|
|
{
|
|
|
|
|
case 0x00000000: // STRB_w
|
|
|
|
|
case 0x40000000: // STRH_w
|
|
|
|
|
case 0x80000000: // STR_w
|
|
|
|
|
case 0xC0000000: // STR_x
|
|
|
|
|
case 0x04000000: // STR_b
|
|
|
|
|
case 0x44000000: // STR_h
|
|
|
|
|
case 0x84000000: // STR_s
|
|
|
|
|
case 0xC4000000: // STR_d
|
|
|
|
|
case 0x04800000: // STR_q
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 23:40:32 -05:00
|
|
|
#endif // ARCH_ARM64
|
2024-05-29 20:30:53 +10:00
|
|
|
|
|
|
|
|
namespace PageFaultHandler
|
|
|
|
|
{
|
|
|
|
|
static void SignalHandler(int sig, siginfo_t* info, void* ctx);
|
|
|
|
|
} // namespace PageFaultHandler
|
|
|
|
|
|
|
|
|
|
void PageFaultHandler::SignalHandler(int sig, siginfo_t* info, void* ctx)
|
|
|
|
|
{
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
void* const exception_address = reinterpret_cast<void*>(info->si_addr);
|
|
|
|
|
|
2026-03-10 23:40:32 -05:00
|
|
|
#if defined(ARCH_X86)
|
2024-05-29 20:30:53 +10:00
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext.gregs[REG_RIP]);
|
|
|
|
|
const bool is_write = (static_cast<ucontext_t*>(ctx)->uc_mcontext.gregs[REG_ERR] & 2) != 0;
|
2026-03-10 23:40:32 -05:00
|
|
|
#elif defined(ARCH_ARM64)
|
2024-05-29 20:30:53 +10:00
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext.pc);
|
|
|
|
|
const bool is_write = IsStoreInstruction(exception_pc);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
|
|
2026-03-10 23:40:32 -05:00
|
|
|
#if defined(ARCH_X86)
|
2024-05-29 20:30:53 +10:00
|
|
|
void* const exception_address = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext.mc_addr);
|
|
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext.mc_rip);
|
|
|
|
|
const bool is_write = (static_cast<ucontext_t*>(ctx)->uc_mcontext.mc_err & 2) != 0;
|
2026-03-10 23:40:32 -05:00
|
|
|
#elif defined(ARCH_ARM64)
|
2024-05-29 20:30:53 +10:00
|
|
|
void* const exception_address = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext->__es.__far);
|
|
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext->__ss.__pc);
|
|
|
|
|
const bool is_write = IsStoreInstruction(exception_pc);
|
|
|
|
|
#endif
|
2023-12-26 20:25:26 +10:00
|
|
|
|
|
|
|
|
#endif
|
2024-05-29 20:30:53 +10:00
|
|
|
|
|
|
|
|
// Executing the handler concurrently from multiple threads wouldn't go down well.
|
|
|
|
|
s_exception_handler_mutex.lock();
|
|
|
|
|
|
|
|
|
|
// Prevent recursive exception filtering.
|
|
|
|
|
HandlerResult result = HandlerResult::ExecuteNextHandler;
|
|
|
|
|
if (!s_in_exception_handler)
|
|
|
|
|
{
|
|
|
|
|
s_in_exception_handler = true;
|
|
|
|
|
result = HandlePageFault(exception_pc, exception_address, is_write);
|
|
|
|
|
s_in_exception_handler = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_exception_handler_mutex.unlock();
|
|
|
|
|
|
|
|
|
|
// Resumes execution right where we left off (re-executes instruction that caused the SIGSEGV).
|
|
|
|
|
if (result == HandlerResult::ContinueExecution)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// We couldn't handle it. Pass it off to the crash dumper.
|
|
|
|
|
CrashHandler::CrashSignalHandler(sig, info, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PageFaultHandler::Install(Error* error)
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock lock(s_exception_handler_mutex);
|
2026-07-09 10:22:19 -04:00
|
|
|
// Android keeps one process across game launches, so the VM lifecycle can call
|
|
|
|
|
// Install() more than once. Make it idempotent instead of tripping the release
|
|
|
|
|
// assert below (which aborts the CPU thread on the second game boot).
|
|
|
|
|
if (s_installed)
|
|
|
|
|
return true;
|
2024-05-29 20:30:53 +10:00
|
|
|
pxAssertRel(!s_installed, "Page fault handler has already been installed.");
|
|
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
sa.sa_flags = SA_SIGINFO | SA_NODEFER;
|
|
|
|
|
sa.sa_sigaction = SignalHandler;
|
|
|
|
|
|
|
|
|
|
if (sigaction(SIGSEGV, &sa, nullptr) != 0)
|
|
|
|
|
{
|
|
|
|
|
Error::SetErrno(error, "sigaction() for SIGSEGV failed: ", errno);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 23:40:32 -05:00
|
|
|
#ifdef ARCH_ARM64
|
2024-05-29 20:30:53 +10:00
|
|
|
// We can get SIGBUS on ARM64.
|
|
|
|
|
if (sigaction(SIGBUS, &sa, nullptr) != 0)
|
|
|
|
|
{
|
|
|
|
|
Error::SetErrno(error, "sigaction() for SIGBUS failed: ", errno);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
s_installed = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-03-23 23:01:37 -05:00
|
|
|
|
|
|
|
|
bool PageFaultHandler::InstallSecondaryThread() { return true; }
|
2024-08-21 02:11:24 -05:00
|
|
|
|
|
|
|
|
#endif // __APPLE__
|