Split the fault handling from MemMap.cpp to MemFault.cpp.

This commit is contained in:
Henrik Rydgård committed 2020-07-15 12:12:57 +02:00
1 parent 1350c7471c
commit 626d173d10
15 files changed
+191 -131

No files matched your search

+3 -111
View File
@@ -29,17 +29,8 @@
#include "Common/MemArena.h"
#include "Common/ChunkFile.h"
#include "Common/MachineContext.h"
#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86)
#include "Common/x64Analyzer.h"
#elif PPSSPP_ARCH(ARM64)
#include "Core/Util/DisArm64.h"
#elif PPSSPP_ARCH(ARM)
#include "ext/disarm.h"
#endif
#include "Core/MemMap.h"
#include "Core/MemFault.h"
#include "Core/HDRemaster.h"
#include "Core/MIPS/MIPS.h"
#include "Core/HLE/HLE.h"
@@ -57,7 +48,7 @@
namespace Memory {
// The base pointer to the auto-mirrored arena.
u8* base = NULL;
u8* base = nullptr;
// The MemArena class
MemArena g_arena;
@@ -97,8 +88,6 @@ u32 g_PSPModel;
std::recursive_mutex g_shutdownLock;
static int64_t g_numReportedBadAccesses = 0;
// We don't declare the IO region in here since its handled by other means.
static MemoryView views[] =
{
@@ -311,7 +300,7 @@ void Init() {
INFO_LOG(MEMMAP, "Memory system initialized. Base at %p (RAM at @ %p, uncached @ %p)",
base, m_pPhysicalRAM, m_pUncachedRAM);
g_numReportedBadAccesses = 0;
MemFault_Init();
}
void Reinit() {
@@ -479,101 +468,4 @@ void Memset(const u32 _Address, const u8 _iValue, const u32 _iLength) {
CBreakPoints::ExecMemCheck(_Address, true, _iLength, currentMIPS->pc);
}
#ifdef MACHINE_CONTEXT_SUPPORTED
bool HandleFault(uintptr_t hostAddress, void *ctx) {
SContext *context = (SContext *)ctx;
const uint8_t *codePtr = (uint8_t *)(context->CTX_PC);
// TODO: Check that codePtr is within the current JIT space.
bool inJitSpace = MIPSComp::jit && MIPSComp::jit->CodeInRange(codePtr);
if (!inJitSpace) {
// This is a crash in non-jitted code. Not something we want to handle here, ignore.
return false;
}
uintptr_t baseAddress = (uintptr_t)base;
#ifdef MASKED_PSP_MEMORY
const uintptr_t addressSpaceSize = 0x40000000ULL;
#else
const uintptr_t addressSpaceSize = 0x100000000ULL;
#endif
// Check whether hostAddress is within the PSP memory space, which (likely) means it was a guest executable that did the bad access.
if (hostAddress < baseAddress || hostAddress >= baseAddress + addressSpaceSize) {
// Host address outside - this was a different kind of crash.
return false;
}
// OK, a guest executable did a bad access. Take care of it.
uint32_t guestAddress = hostAddress - baseAddress;
// TODO: Share the struct between the various analyzers, that will allow us to share most of
// the implementations here.
bool success = false;
MemoryExceptionType type = MemoryExceptionType::NONE;
#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86)
// X86, X86-64. Variable instruction size so need to analyze the mov instruction in detail.
// To ignore the access, we need to disassemble the instruction and modify context->CTX_PC
LSInstructionInfo info;
success = X86AnalyzeMOV(codePtr, info);
#elif PPSSPP_ARCH(ARM64)
uint32_t word;
memcpy(&word, codePtr, 4);
// To ignore the access, we need to disassemble the instruction and modify context->CTX_PC
Arm64LSInstructionInfo info;
success = Arm64AnalyzeLoadStore((uint64_t)codePtr, word, &info);
#elif PPSSPP_ARCH(ARM)
uint32_t word;
memcpy(&word, codePtr, 4);
// To ignore the access, we need to disassemble the instruction and modify context->CTX_PC
ArmLSInstructionInfo info;
success = ArmAnalyzeLoadStore((uint32_t)codePtr, word, &info);
#endif
if (success) {
if (info.isMemoryWrite) {
type = MemoryExceptionType::WRITE_WORD;
} else {
type = MemoryExceptionType::READ_WORD;
}
} else {
type = MemoryExceptionType::UNKNOWN;
}
if (success && g_Config.bIgnoreBadMemAccess) {
if (!info.isMemoryWrite) {
// It was a read. Fill the destination register with 0.
// TODO
}
// Move on to the next instruction. Note that handling bad accesses like this is pretty slow.
context->CTX_PC += info.instructionSize;
g_numReportedBadAccesses++;
if (g_numReportedBadAccesses < 100) {
ERROR_LOG(MEMMAP, "Bad memory access detected and ignored: %08x (%p)", guestAddress, (void *)hostAddress);
}
} else {
// Either bIgnoreBadMemAccess is off, or we failed recovery analysis.
uint32_t approximatePC = currentMIPS->pc;
Core_MemoryException(guestAddress, currentMIPS->pc, type);
// Redirect execution to a crash handler that will exit the game.
context->CTX_PC = (uintptr_t)MIPSComp::jit->GetCrashHandler();
ERROR_LOG(MEMMAP, "Bad memory access detected! %08x (%p) Stopping emulation.", guestAddress, (void *)hostAddress);
}
return true;
}
#else
bool HandleFault(uintptr_t hostAddress, void *ctx) {
ERROR_LOG(MEMMAP, "Exception handling not supported");
return false;
}
#endif
} // namespace