2020-07-15 10:10:33 +00:00
|
|
|
// Copyright (C) 2020 PPSSPP 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 git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2020-12-19 19:26:02 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <unordered_set>
|
2021-11-28 00:11:51 +00:00
|
|
|
#include <mutex>
|
2023-03-31 08:08:12 +00:00
|
|
|
#include <sstream>
|
2020-12-19 19:26:02 +00:00
|
|
|
|
2023-03-31 08:08:12 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-07-15 10:10:33 +00:00
|
|
|
#include "Common/MachineContext.h"
|
|
|
|
|
|
|
|
#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86)
|
|
|
|
#include "Common/x64Analyzer.h"
|
2020-07-15 10:38:05 +00:00
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
#elif PPSSPP_ARCH(ARM64)
|
|
|
|
#include "Core/Util/DisArm64.h"
|
|
|
|
#elif PPSSPP_ARCH(ARM)
|
|
|
|
#include "ext/disarm.h"
|
|
|
|
#endif
|
|
|
|
|
2020-10-04 07:29:36 +00:00
|
|
|
#include "Common/Log.h"
|
2022-01-30 23:49:02 +00:00
|
|
|
#include "Core/Config.h"
|
2020-07-15 10:10:33 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/MemFault.h"
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/MIPS/JitCommon/JitCommon.h"
|
2023-03-31 08:08:12 +00:00
|
|
|
#include "Core/Debugger/SymbolMap.h"
|
|
|
|
|
|
|
|
// Stack walking stuff
|
|
|
|
#include "Core/MIPS/MIPSStackWalk.h"
|
|
|
|
#include "Core/MIPS/MIPSDebugInterface.h"
|
|
|
|
#include "Core/HLE/sceKernelThread.h"
|
2020-07-15 10:10:33 +00:00
|
|
|
|
|
|
|
namespace Memory {
|
|
|
|
|
|
|
|
static int64_t g_numReportedBadAccesses = 0;
|
2020-12-19 19:26:02 +00:00
|
|
|
const uint8_t *g_lastCrashAddress;
|
2021-02-09 00:30:31 +00:00
|
|
|
MemoryExceptionType g_lastMemoryExceptionType;
|
2022-12-21 04:18:02 +00:00
|
|
|
static bool inCrashHandler = false;
|
2021-02-09 00:30:31 +00:00
|
|
|
|
2020-12-19 19:26:02 +00:00
|
|
|
std::unordered_set<const uint8_t *> g_ignoredAddresses;
|
2020-07-15 10:10:33 +00:00
|
|
|
|
|
|
|
void MemFault_Init() {
|
|
|
|
g_numReportedBadAccesses = 0;
|
2020-12-19 19:26:02 +00:00
|
|
|
g_lastCrashAddress = nullptr;
|
2021-02-09 00:30:31 +00:00
|
|
|
g_lastMemoryExceptionType = MemoryExceptionType::NONE;
|
2020-12-19 19:26:02 +00:00
|
|
|
g_ignoredAddresses.clear();
|
2020-07-15 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 22:19:42 +00:00
|
|
|
bool MemFault_MayBeResumable() {
|
2021-02-09 08:38:03 +00:00
|
|
|
return g_lastCrashAddress != nullptr;
|
2020-12-19 22:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemFault_IgnoreLastCrash() {
|
|
|
|
g_ignoredAddresses.insert(g_lastCrashAddress);
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
#ifdef MACHINE_CONTEXT_SUPPORTED
|
|
|
|
|
2020-07-15 10:38:05 +00:00
|
|
|
static bool DisassembleNativeAt(const uint8_t *codePtr, int instructionSize, std::string *dest) {
|
|
|
|
#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86)
|
|
|
|
auto lines = DisassembleX86(codePtr, instructionSize);
|
|
|
|
if (!lines.empty()) {
|
|
|
|
*dest = lines[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#elif PPSSPP_ARCH(ARM64)
|
|
|
|
auto lines = DisassembleArm64(codePtr, instructionSize);
|
|
|
|
if (!lines.empty()) {
|
|
|
|
*dest = lines[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#elif PPSSPP_ARCH(ARM)
|
|
|
|
auto lines = DisassembleArm2(codePtr, instructionSize);
|
|
|
|
if (!lines.empty()) {
|
|
|
|
*dest = lines[0];
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-30 19:36:32 +00:00
|
|
|
#elif PPSSPP_ARCH(RISCV64)
|
|
|
|
auto lines = DisassembleRV64(codePtr, instructionSize);
|
|
|
|
if (!lines.empty()) {
|
|
|
|
*dest = lines[0];
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-15 10:38:05 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
bool HandleFault(uintptr_t hostAddress, void *ctx) {
|
2022-12-21 04:18:02 +00:00
|
|
|
if (inCrashHandler)
|
|
|
|
return false;
|
|
|
|
inCrashHandler = true;
|
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
SContext *context = (SContext *)ctx;
|
|
|
|
const uint8_t *codePtr = (uint8_t *)(context->CTX_PC);
|
|
|
|
|
2021-11-28 00:11:51 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
|
|
|
|
|
2020-12-19 19:26:02 +00:00
|
|
|
// We set this later if we think it can be resumed from.
|
|
|
|
g_lastCrashAddress = nullptr;
|
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
// 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.
|
2022-12-21 04:18:02 +00:00
|
|
|
inCrashHandler = false;
|
2020-07-15 10:10:33 +00:00
|
|
|
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.
|
2022-05-21 18:39:26 +00:00
|
|
|
bool invalidHostAddress = hostAddress == (uintptr_t)0xFFFFFFFFFFFFFFFFULL;
|
2020-07-15 10:10:33 +00:00
|
|
|
if (hostAddress < baseAddress || hostAddress >= baseAddress + addressSpaceSize) {
|
|
|
|
// Host address outside - this was a different kind of crash.
|
2022-12-21 04:18:02 +00:00
|
|
|
if (!invalidHostAddress) {
|
|
|
|
inCrashHandler = false;
|
2022-05-21 18:39:26 +00:00
|
|
|
return false;
|
2022-12-21 04:18:02 +00:00
|
|
|
}
|
2020-07-15 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:08:12 +00:00
|
|
|
// OK, a guest executable did a bad access. Let's handle it.
|
2020-07-15 10:10:33 +00:00
|
|
|
|
2022-05-21 18:39:26 +00:00
|
|
|
uint32_t guestAddress = invalidHostAddress ? 0xFFFFFFFFUL : (uint32_t)(hostAddress - baseAddress);
|
2020-07-15 10:10:33 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2020-07-15 10:38:05 +00:00
|
|
|
std::string infoString = "";
|
|
|
|
|
2021-02-09 00:30:31 +00:00
|
|
|
bool isAtDispatch = false;
|
2020-07-15 10:38:05 +00:00
|
|
|
if (MIPSComp::jit) {
|
|
|
|
std::string desc;
|
|
|
|
if (MIPSComp::jit->DescribeCodePtr(codePtr, desc)) {
|
|
|
|
infoString += desc + "\n";
|
|
|
|
}
|
2021-02-09 00:30:31 +00:00
|
|
|
if (MIPSComp::jit->IsAtDispatchFetch(codePtr)) {
|
|
|
|
isAtDispatch = true;
|
|
|
|
}
|
2020-07-15 10:38:05 +00:00
|
|
|
}
|
2020-07-15 10:10:33 +00:00
|
|
|
|
2020-07-15 10:38:05 +00:00
|
|
|
int instructionSize = 4;
|
2020-07-15 10:10:33 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64) || PPSSPP_ARCH(X86)
|
|
|
|
// X86, X86-64. Variable instruction size so need to analyze the mov instruction in detail.
|
2020-12-19 18:16:04 +00:00
|
|
|
instructionSize = 15;
|
2020-07-15 10:10:33 +00:00
|
|
|
|
|
|
|
// To ignore the access, we need to disassemble the instruction and modify context->CTX_PC
|
2020-07-19 15:22:33 +00:00
|
|
|
LSInstructionInfo info{};
|
2020-07-15 10:10:33 +00:00
|
|
|
success = X86AnalyzeMOV(codePtr, info);
|
2020-12-19 18:16:04 +00:00
|
|
|
if (success)
|
|
|
|
instructionSize = info.instructionSize;
|
2020-07-15 10:10:33 +00:00
|
|
|
#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
|
2020-07-19 15:22:33 +00:00
|
|
|
Arm64LSInstructionInfo info{};
|
2020-07-15 10:10:33 +00:00
|
|
|
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
|
2020-07-19 15:22:33 +00:00
|
|
|
ArmLSInstructionInfo info{};
|
2020-07-15 10:10:33 +00:00
|
|
|
success = ArmAnalyzeLoadStore((uint32_t)codePtr, word, &info);
|
2022-12-24 19:08:31 +00:00
|
|
|
#elif PPSSPP_ARCH(RISCV64)
|
|
|
|
// TODO: Put in a disassembler.
|
|
|
|
struct RiscVLSInstructionInfo {
|
|
|
|
int instructionSize;
|
|
|
|
bool isIntegerLoadStore;
|
|
|
|
bool isFPLoadStore;
|
|
|
|
int size;
|
|
|
|
bool isMemoryWrite;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t word;
|
|
|
|
memcpy(&word, codePtr, 4);
|
|
|
|
|
|
|
|
RiscVLSInstructionInfo info{};
|
|
|
|
// Compressed instructions have low bits 00, 01, or 10.
|
|
|
|
info.instructionSize = (word & 3) == 3 ? 4 : 2;
|
|
|
|
instructionSize = info.instructionSize;
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
switch (word & 0x7F) {
|
|
|
|
case 3:
|
|
|
|
info.isIntegerLoadStore = true;
|
|
|
|
info.size = 1 << ((word >> 12) & 3);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
info.isFPLoadStore = true;
|
|
|
|
info.size = 1 << ((word >> 12) & 3);
|
|
|
|
break;
|
|
|
|
case 35:
|
|
|
|
info.isIntegerLoadStore = true;
|
|
|
|
info.isMemoryWrite = true;
|
|
|
|
info.size = 1 << ((word >> 12) & 3);
|
|
|
|
break;
|
|
|
|
case 39:
|
|
|
|
info.isFPLoadStore = true;
|
|
|
|
info.isMemoryWrite = true;
|
|
|
|
info.size = 1 << ((word >> 12) & 3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Compressed instruction.
|
|
|
|
switch (word & 0x6003) {
|
|
|
|
case 0x4000:
|
|
|
|
case 0x4002:
|
|
|
|
case 0x6000:
|
|
|
|
case 0x6002:
|
|
|
|
info.isIntegerLoadStore = true;
|
|
|
|
info.size = (word & 0x2000) != 0 ? 8 : 4;
|
|
|
|
info.isMemoryWrite = (word & 0x8000) != 0;
|
|
|
|
break;
|
|
|
|
case 0x2000:
|
|
|
|
case 0x2002:
|
|
|
|
info.isFPLoadStore = true;
|
|
|
|
info.size = 8;
|
|
|
|
info.isMemoryWrite = (word & 0x8000) != 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Not a read or a write.
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-07-15 10:10:33 +00:00
|
|
|
#endif
|
2020-07-15 10:38:05 +00:00
|
|
|
|
|
|
|
std::string disassembly;
|
2020-12-19 18:16:04 +00:00
|
|
|
if (DisassembleNativeAt(codePtr, instructionSize, &disassembly)) {
|
2020-07-15 10:38:05 +00:00
|
|
|
infoString += disassembly + "\n";
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:30:31 +00:00
|
|
|
if (isAtDispatch) {
|
2021-02-09 08:38:03 +00:00
|
|
|
u32 targetAddr = currentMIPS->pc; // bad approximation
|
2021-02-09 08:48:34 +00:00
|
|
|
// TODO: Do the other archs and platforms.
|
|
|
|
#if PPSSPP_ARCH(AMD64) && PPSSPP_PLATFORM(WINDOWS)
|
2021-02-09 08:38:03 +00:00
|
|
|
// We know which register the address is in, look in Asm.cpp.
|
2022-01-30 23:15:38 +00:00
|
|
|
targetAddr = (uint32_t)context->Rax;
|
2021-02-09 08:38:03 +00:00
|
|
|
#endif
|
|
|
|
Core_ExecException(targetAddr, currentMIPS->pc, ExecExceptionType::JUMP);
|
|
|
|
// Redirect execution to a crash handler that will switch to CoreState::CORE_RUNTIME_ERROR immediately.
|
|
|
|
context->CTX_PC = (uintptr_t)MIPSComp::jit->GetCrashHandler();
|
|
|
|
ERROR_LOG(MEMMAP, "Bad execution access detected, halting: %08x (last known pc %08x, host: %p)", targetAddr, currentMIPS->pc, (void *)hostAddress);
|
2022-12-21 04:18:02 +00:00
|
|
|
inCrashHandler = false;
|
2021-02-09 08:38:03 +00:00
|
|
|
return true;
|
2021-02-09 00:30:31 +00:00
|
|
|
} else if (success) {
|
2020-07-15 10:10:33 +00:00
|
|
|
if (info.isMemoryWrite) {
|
|
|
|
type = MemoryExceptionType::WRITE_WORD;
|
|
|
|
} else {
|
|
|
|
type = MemoryExceptionType::READ_WORD;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
type = MemoryExceptionType::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:30:31 +00:00
|
|
|
g_lastMemoryExceptionType = type;
|
|
|
|
|
2022-12-24 19:08:31 +00:00
|
|
|
bool handled = true;
|
2021-02-09 09:17:15 +00:00
|
|
|
if (success && (g_Config.bIgnoreBadMemAccess || g_ignoredAddresses.find(codePtr) != g_ignoredAddresses.end())) {
|
2020-07-15 10:10:33 +00:00
|
|
|
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.
|
2022-12-30 11:21:05 +00:00
|
|
|
// We can't ignore this memory access.
|
2020-07-15 10:10:33 +00:00
|
|
|
uint32_t approximatePC = currentMIPS->pc;
|
2023-01-01 18:22:41 +00:00
|
|
|
// TODO: Determine access size from the disassembled native instruction. We have some partial info already,
|
|
|
|
// just need to clean it up.
|
|
|
|
Core_MemoryExceptionInfo(guestAddress, 0, approximatePC, type, infoString, true);
|
2020-07-15 10:10:33 +00:00
|
|
|
|
2020-12-19 19:26:02 +00:00
|
|
|
// There's a small chance we can resume from this type of crash.
|
|
|
|
g_lastCrashAddress = codePtr;
|
|
|
|
|
|
|
|
// Redirect execution to a crash handler that will switch to CoreState::CORE_RUNTIME_ERROR immediately.
|
2022-12-24 19:08:31 +00:00
|
|
|
if (MIPSComp::jit)
|
|
|
|
context->CTX_PC = (uintptr_t)MIPSComp::jit->GetCrashHandler();
|
|
|
|
else
|
|
|
|
handled = false;
|
2020-07-15 10:38:05 +00:00
|
|
|
ERROR_LOG(MEMMAP, "Bad memory access detected! %08x (%p) Stopping emulation. Info:\n%s", guestAddress, (void *)hostAddress, infoString.c_str());
|
2020-07-15 10:10:33 +00:00
|
|
|
}
|
2022-12-21 04:18:02 +00:00
|
|
|
|
|
|
|
inCrashHandler = false;
|
2022-12-24 19:08:31 +00:00
|
|
|
return handled;
|
2020-07-15 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
bool HandleFault(uintptr_t hostAddress, void *ctx) {
|
|
|
|
ERROR_LOG(MEMMAP, "Exception handling not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace Memory
|
2023-03-31 08:08:12 +00:00
|
|
|
|
|
|
|
std::vector<MIPSStackWalk::StackFrame> WalkCurrentStack(int threadID) {
|
|
|
|
DebugInterface *cpuDebug = currentDebugMIPS;
|
|
|
|
|
|
|
|
auto threads = GetThreadsInfo();
|
|
|
|
uint32_t entry = cpuDebug->GetPC();
|
|
|
|
uint32_t stackTop = 0;
|
|
|
|
for (const DebugThreadInfo &th : threads) {
|
|
|
|
if ((threadID == -1 && th.isCurrent) || th.id == threadID) {
|
|
|
|
entry = th.entrypoint;
|
|
|
|
stackTop = th.initialStack;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ra = cpuDebug->GetRegValue(0, MIPS_REG_RA);
|
|
|
|
uint32_t sp = cpuDebug->GetRegValue(0, MIPS_REG_SP);
|
|
|
|
return MIPSStackWalk::Walk(cpuDebug->GetPC(), ra, sp, entry, stackTop);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FormatStackTrace(const std::vector<MIPSStackWalk::StackFrame> &frames) {
|
|
|
|
std::stringstream str;
|
|
|
|
for (const auto &frame : frames) {
|
|
|
|
std::string desc = g_symbolMap->GetDescription(frame.entry);
|
|
|
|
str << StringFromFormat("%s (%08x+%03x, pc: %08x sp: %08x)\n", desc.c_str(), frame.entry, frame.pc - frame.entry, frame.pc, frame.sp);
|
|
|
|
}
|
|
|
|
return str.str();
|
|
|
|
}
|