diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d866fe92c..28d99b3fb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1740,6 +1740,8 @@ add_library(${CoreLibName} ${CoreLinkType} Core/MIPS/MIPSVFPUUtils.h Core/MIPS/MIPSAsm.cpp Core/MIPS/MIPSAsm.h + Core/MemFault.cpp + Core/MemFault.h Core/MemMap.cpp Core/MemMap.h Core/MemMapFunctions.cpp diff --git a/Common/MachineContext.h b/Common/MachineContext.h index fdd84f26d1..e2cf802888 100644 --- a/Common/MachineContext.h +++ b/Common/MachineContext.h @@ -314,32 +314,30 @@ typedef mcontext_t SContext; #if PPSSPP_ARCH(AMD64) -#include +#include #define CTX_PC CTX_RIP -static inline u64* ContextRN(SContext* ctx, int n) -{ - static const u8 offsets[] = { +static inline uint64_t *ContextRN(SContext* ctx, int n) { + static const uint8_t offsets[] = { offsetof(SContext, CTX_RAX), offsetof(SContext, CTX_RCX), offsetof(SContext, CTX_RDX), offsetof(SContext, CTX_RBX), offsetof(SContext, CTX_RSP), offsetof(SContext, CTX_RBP), offsetof(SContext, CTX_RSI), offsetof(SContext, CTX_RDI), offsetof(SContext, CTX_R8), offsetof(SContext, CTX_R9), offsetof(SContext, CTX_R10), offsetof(SContext, CTX_R11), offsetof(SContext, CTX_R12), offsetof(SContext, CTX_R13), offsetof(SContext, CTX_R14), offsetof(SContext, CTX_R15)}; - return (u64*)((char*)ctx + offsets[n]); + return (uint64_t *)((char *)ctx + offsets[n]); } #elif PPSSPP_ARCH(X86) -#include +#include #define CTX_PC CTX_RIP -static inline u32* ContextRN(SContext* ctx, int n) -{ - static const u8 offsets[] = { +static inline uint32_t *ContextRN(SContext* ctx, int n) { + static const uint8_t offsets[] = { offsetof(SContext, CTX_RAX), offsetof(SContext, CTX_RCX), offsetof(SContext, CTX_RDX), offsetof(SContext, CTX_RBX), offsetof(SContext, CTX_RSP), offsetof(SContext, CTX_RBP), offsetof(SContext, CTX_RSI), offsetof(SContext, CTX_RDI)}; - return (u32*)((char*)ctx + offsets[n]); + return (uint32_t *)((char*)ctx + offsets[n]); } #endif // arch diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 4498efe743..6679ddbf34 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -129,8 +129,7 @@ - + @@ -389,6 +388,7 @@ + @@ -918,6 +918,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index c95b6aa41f..5cfb4de0f5 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -743,6 +743,9 @@ HLE\Kernel + + Core + @@ -1379,10 +1382,13 @@ HLE\Kernel + + Core + - + \ No newline at end of file diff --git a/Core/MemFault.cpp b/Core/MemFault.cpp new file mode 100644 index 0000000000..3e15ecdbb7 --- /dev/null +++ b/Core/MemFault.cpp @@ -0,0 +1,143 @@ +// 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" + +#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/Core.h" +#include "Core/MemFault.h" +#include "Core/MemMap.h" +#include "Core/MIPS/JitCommon/JitCommon.h" + +namespace Memory { + +static int64_t g_numReportedBadAccesses = 0; + +void MemFault_Init() { + g_numReportedBadAccesses = 0; +} + +#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; + + std::string disassembly; + +#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 Memory diff --git a/Core/MemFault.h b/Core/MemFault.h new file mode 100644 index 0000000000..40472a50da --- /dev/null +++ b/Core/MemFault.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace Memory { + +void MemFault_Init(); + +// Called by exception handlers. We simply filter out accesses to PSP RAM and otherwise +// just leave it as-is. +bool HandleFault(uintptr_t hostAddress, void *context); + +} diff --git a/Core/MemMap.cpp b/Core/MemMap.cpp index 730613d5e9..b2fa2d0cd7 100644 --- a/Core/MemMap.cpp +++ b/Core/MemMap.cpp @@ -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 diff --git a/Core/MemMap.h b/Core/MemMap.h index dcbbad5441..515d866f6f 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -148,10 +148,6 @@ Opcode Read_Opcode_JIT(const u32 _Address); // used by JIT. Reads in the "Locked cache" mode void Write_Opcode_JIT(const u32 _Address, const Opcode& _Value); -// Called by exception handlers. We simply filter out accesses to PSP RAM and otherwise -// just leave it as-is. -bool HandleFault(uintptr_t hostAddress, void *context); - // Should be used by analyzers, disassemblers etc. Does resolve replacements. Opcode Read_Instruction(const u32 _Address, bool resolveReplacements = false); Opcode ReadUnchecked_Instruction(const u32 _Address, bool resolveReplacements = false); diff --git a/Core/System.cpp b/Core/System.cpp index c679bb60b9..1f738882c7 100644 --- a/Core/System.cpp +++ b/Core/System.cpp @@ -39,7 +39,7 @@ #include "util/text/utf8.h" #include "Common/GraphicsContext.h" -#include "Core/MemMap.h" +#include "Core/MemFault.h" #include "Core/HDRemaster.h" #include "Core/MIPS/MIPS.h" #include "Core/MIPS/MIPSAnalyst.h" diff --git a/UI/DevScreens.h b/UI/DevScreens.h index 8b98b022c6..7b71ad6f13 100644 --- a/UI/DevScreens.h +++ b/UI/DevScreens.h @@ -182,3 +182,4 @@ private: }; void DrawProfile(UIContext &ui); +const char *GetCompilerABI(); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 7992aeab31..873908586a 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1283,12 +1283,14 @@ static void DrawCrashDump(DrawBuffer *draw2d) { snprintf(statbuf, sizeof(statbuf), R"(%s Game ID (Title): %s (%s) PPSSPP build: %s (%s) +ABI: %s )", ExceptionTypeAsString(info.type), g_paramSFO.GetDiscID().c_str(), g_paramSFO.GetValueString("TITLE").c_str(), versionString, - build + build, + GetCompilerABI() ); draw2d->SetFontScale(.7f, .7f); diff --git a/UWP/CoreUWP/CoreUWP.vcxproj b/UWP/CoreUWP/CoreUWP.vcxproj index 75b8b2c4c5..900df8f61a 100644 --- a/UWP/CoreUWP/CoreUWP.vcxproj +++ b/UWP/CoreUWP/CoreUWP.vcxproj @@ -519,6 +519,7 @@ + @@ -730,6 +731,7 @@ + @@ -947,4 +949,4 @@ - \ No newline at end of file + diff --git a/UWP/CoreUWP/CoreUWP.vcxproj.filters b/UWP/CoreUWP/CoreUWP.vcxproj.filters index 7afcc97e71..8055a958ff 100644 --- a/UWP/CoreUWP/CoreUWP.vcxproj.filters +++ b/UWP/CoreUWP/CoreUWP.vcxproj.filters @@ -82,6 +82,7 @@ + @@ -719,6 +720,7 @@ + diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 44ab4a7332..5a08665464 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -300,6 +300,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/Core/FileLoaders/LocalFileLoader.cpp \ $(SRC)/Core/FileLoaders/RamCachingFileLoader.cpp \ $(SRC)/Core/FileLoaders/RetryingFileLoader.cpp \ + $(SRC)/Core/MemFault.cpp \ $(SRC)/Core/MemMap.cpp \ $(SRC)/Core/MemMapFunctions.cpp \ $(SRC)/Core/Reporting.cpp \ diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 0ac5b33297..3b12a0ba07 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -472,6 +472,7 @@ SOURCES_CXX += $(NATIVEDIR)/math/dataconv.cpp \ $(COREDIR)/MIPS/MIPSIntVFPU.cpp \ $(COREDIR)/MIPS/MIPSTables.cpp \ $(COREDIR)/MIPS/MIPSVFPUUtils.cpp \ + $(COREDIR)/MemFault.cpp \ $(COREDIR)/MemMap.cpp \ $(COREDIR)/MemMapFunctions.cpp \ $(COREDIR)/PSPLoaders.cpp \