mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-14 23:10:33 +02:00
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>
104 lines
2.6 KiB
C++
104 lines
2.6 KiB
C++
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
#include "ppsspp_config.h"
|
|
|
|
#if PPSSPP_PLATFORM(SWITCH)
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
#include "Common/CommonTypes.h"
|
|
#include "Common/Log.h"
|
|
#include "Common/MemoryUtil.h"
|
|
#include "Common/StringUtils.h"
|
|
#include "Common/SysError.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h> // memalign
|
|
|
|
#define MEM_PAGE_SIZE (0x1000)
|
|
#define MEM_PAGE_MASK ((MEM_PAGE_SIZE)-1)
|
|
#define ppsspp_round_page(x) ((((uintptr_t)(x)) + MEM_PAGE_MASK) & ~(MEM_PAGE_MASK))
|
|
|
|
// On Switch we dont support allocating executable memory here
|
|
// See CodeBlock.h
|
|
void *AllocateExecutableMemory(size_t size) {
|
|
return nullptr;
|
|
}
|
|
|
|
void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) {
|
|
void* ptr = nullptr;
|
|
size = ppsspp_round_page(size);
|
|
ptr = memalign(MEM_PAGE_SIZE, size);
|
|
return ptr;
|
|
}
|
|
|
|
// No lazy commit here, the whole reservation is backed (and zeroed) up front.
|
|
void *ReserveMemoryPages(size_t size) {
|
|
size = ppsspp_round_page(size);
|
|
void *ptr = memalign(MEM_PAGE_SIZE, size);
|
|
if (ptr) {
|
|
memset(ptr, 0, size);
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
bool CommitMemoryPages(void *ptr, size_t size) {
|
|
return true;
|
|
}
|
|
|
|
void *AllocateAlignedMemory(size_t size, size_t alignment) {
|
|
void* ptr = memalign(alignment, size);
|
|
|
|
_assert_msg_(ptr != nullptr, "Failed to allocate aligned memory of size %lu", size);
|
|
return ptr;
|
|
}
|
|
|
|
void FreeMemoryPages(void *ptr, size_t size) {
|
|
if (!ptr)
|
|
return;
|
|
|
|
free(ptr);
|
|
return;
|
|
}
|
|
|
|
void FreeExecutableMemory(void *ptr, size_t size) {
|
|
return; // Not supported on Switch
|
|
}
|
|
|
|
void FreeAlignedMemory(void* ptr) {
|
|
if (!ptr)
|
|
return;
|
|
|
|
free(ptr);
|
|
}
|
|
|
|
bool PlatformIsWXExclusive() {
|
|
return false; // Switch technically is W^X but we use dual mappings instead of reprotecting the pages to allow a W and X mapping
|
|
}
|
|
|
|
bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags) {
|
|
return true;
|
|
}
|
|
|
|
int GetMemoryProtectPageSize() {
|
|
return MEM_PAGE_SIZE;
|
|
}
|
|
#endif // PPSSPP_PLATFORM(SWITCH)
|