Keep JIT block entries and replacement hooks out of PSP memory

The JITs used to find a block by overwriting its first instruction with an "emuhack" holding
the offset into the code space, and replacement functions installed their own emuhack the same
way. Anything that read code (savestates, the debugger, memory dumps, cheats, the function
hasher, games reading their own code) had to translate them back.

Blocks now live in a block shadow: one u32 per guest instruction, indexed by the PC masked with
BLOCK_SHADOW_MASK (1GB on 64-bit, folding the kernel and uncached mirrors; 256MB on 32-bit, iOS
and Switch, where address space is short), with the low bits cleared so a misaligned PC can't
read across two entries. Only the ranges under memory views are committed, so a bad PC still
faults in the dispatcher, and the shadow is reserved before the memory map so it can't land in
a gap of the 4GB window. Entries hold full 32-bit offsets, lifting the 16MB limit. The
dispatchers on every backend, and the IR interpreter's loop, look blocks up there.

A hooked instruction without a compiled block gets the JIT's hook value in its entry: the offset
of the dispatcher's compile path (1 for the interpreters, the IR arena keeping that offset
empty), so dispatching there compiles a block, and the frontends substitute the hook. Compiling
replaces the entry with the block's, and destroying the block puts the hook value back. The hook
value is rewritten when the JIT changes or regenerates its dispatcher.

Hooks live in a map keyed by the masked address, and are dropped when the game overwrites the
hooked instruction, or when an icache invalidation finds the function's hash (immediates masked,
as when matching it) changed. Those checks run when queued invalidations are processed, since
cheats and the debugger queue them before writing. Installing and removing hooks invalidates
deferred, as it happens in HLE calls that can run inside a block. The cache instruction goes
through MIPSState, so it drops changed hooks too.

With clean memory, the save/clear/restore around savestates, memory reads and dumps goes away,
as do the source-range invalidations in the memcpy replacements, sceDmac and the cheats.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-11 10:33:30 +02:00
1 parent 0a8ec4d8f1
commit 6d122f416e
57 files changed
+876 -759

No files matched your search

+54
View File
@@ -44,6 +44,12 @@
#ifndef _WIN32
#include <unistd.h>
#endif
#ifndef MAP_NORESERVE
// Not implemented on BSDs
#define MAP_NORESERVE 0
#endif
static int hint_location;
#ifdef __APPLE__
#define MEM_PAGE_SIZE (PAGE_SIZE)
@@ -254,6 +260,54 @@ void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) {
return ptr;
}
void *ReserveMemoryPages(size_t size) {
#ifdef _WIN32
if (sys_info.dwPageSize == 0)
GetSystemInfo(&sys_info);
size = ppsspp_round_page(size);
#if PPSSPP_PLATFORM(UWP)
void *ptr = VirtualAllocFromApp(0, size, MEM_RESERVE, PAGE_NOACCESS);
#else
void *ptr = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS);
#endif
if (!ptr) {
ERROR_LOG(Log::MemMap, "Failed to reserve %d bytes of address space", (int)size);
return nullptr;
}
#else
size = ppsspp_round_page(size);
void *ptr = mmap(0, size, PROT_NONE, MAP_ANON | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
if (ptr == MAP_FAILED) {
ERROR_LOG(Log::MemMap, "Failed to reserve %d bytes of address space: errno=%d", (int)size, errno);
return nullptr;
}
#endif
return ptr;
}
bool CommitMemoryPages(void *ptr, size_t size) {
uintptr_t page_size = GetMemoryProtectPageSize();
uintptr_t start = (uintptr_t)ptr & ~(page_size - 1);
uintptr_t end = ((uintptr_t)ptr + size + page_size - 1) & ~(page_size - 1);
#ifdef _WIN32
#if PPSSPP_PLATFORM(UWP)
void *result = VirtualAllocFromApp((void *)start, end - start, MEM_COMMIT, PAGE_READWRITE);
#else
void *result = VirtualAlloc((void *)start, end - start, MEM_COMMIT, PAGE_READWRITE);
#endif
if (!result) {
ERROR_LOG(Log::MemMap, "CommitMemoryPages failed!\n%s", GetLastErrorMsg().c_str());
return false;
}
#else
if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE) != 0) {
ERROR_LOG(Log::MemMap, "CommitMemoryPages failed (%p)! errno=%d (%s)", (void *)start, errno, strerror(errno));
return false;
}
#endif
return true;
}
void *AllocateAlignedMemory(size_t size, size_t alignment) {
#ifdef _WIN32
void* ptr = _aligned_malloc(size, alignment);