From 825450a373e3f09a0a4cce32921ddd82b137eafd Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 9 Oct 2022 21:26:13 -0700 Subject: [PATCH] jit: Defer invalidations made while running. Previously, invalidating icache could happen while running, which might cause the CPU to return into outer space. This runs such invalidations after letting the CPU exit. It was easy to trigger this with the debugger: step using the GE debugger, add a CPU memory breakpoint, then resume from the GE debugger. However, cheats and the like could cause similar issues. --- Core/Debugger/Breakpoints.cpp | 15 +++++-------- Core/MIPS/MIPS.cpp | 42 +++++++++++++++++++++++++++++++---- Core/MIPS/MIPS.h | 7 ++++++ Core/MIPS/MIPSAsm.cpp | 4 +--- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Core/Debugger/Breakpoints.cpp b/Core/Debugger/Breakpoints.cpp index 715f18cbf1..ad1d37e41d 100644 --- a/Core/Debugger/Breakpoints.cpp +++ b/Core/Debugger/Breakpoints.cpp @@ -648,16 +648,11 @@ void CBreakPoints::Update(u32 addr) { resume = true; } - { - std::lock_guard guard(MIPSComp::jitLock); - if (MIPSComp::jit) { - // In case this is a delay slot, clear the previous instruction too. - if (addr != 0) - MIPSComp::jit->InvalidateCacheAt(addr - 4, 8); - else - MIPSComp::jit->ClearCache(); - } - } + // In case this is a delay slot, clear the previous instruction too. + if (addr != 0) + mipsr4k.InvalidateICache(addr - 4, 8); + else + mipsr4k.ClearJitCache(); if (resume) Core_EnableStepping(false); diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index 08958c14c6..5aeeacbbf8 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "Common/Math/math_util.h" @@ -334,7 +335,11 @@ int MIPSState::RunLoopUntil(u64 globalTicks) { // We must get out of the delay slot before going into jit. SingleStep(); } + insideJit = true; + if (hasPendingClears) + ProcessPendingClears(); MIPSComp::jit->RunLoopUntil(globalTicks); + insideJit = false; break; case CPUCore::INTERPRETER: @@ -343,15 +348,44 @@ int MIPSState::RunLoopUntil(u64 globalTicks) { return 1; } +// Kept outside MIPSState to avoid header pollution (MIPS.h doesn't even have vector, and is used widely.) +static std::vector> pendingClears; + +void MIPSState::ProcessPendingClears() { + std::lock_guard guard(MIPSComp::jitLock); + for (auto &p : pendingClears) { + if (p.first == 0 && p.second == 0) + MIPSComp::jit->ClearCache(); + else + MIPSComp::jit->InvalidateCacheAt(p.first, p.second); + } + pendingClears.clear(); + hasPendingClears = false; +} + void MIPSState::InvalidateICache(u32 address, int length) { // Only really applies to jit. std::lock_guard guard(MIPSComp::jitLock); - if (MIPSComp::jit) - MIPSComp::jit->InvalidateCacheAt(address, length); + if (MIPSComp::jit) { + if (coreState == CORE_RUNNING || insideJit) { + pendingClears.emplace_back(address, length); + hasPendingClears = true; + CoreTiming::ForceCheck(); + } else { + MIPSComp::jit->InvalidateCacheAt(address, length); + } + } } void MIPSState::ClearJitCache() { std::lock_guard guard(MIPSComp::jitLock); - if (MIPSComp::jit) - MIPSComp::jit->ClearCache(); + if (MIPSComp::jit) { + if (coreState == CORE_RUNNING || insideJit) { + pendingClears.emplace_back(0, 0); + hasPendingClears = true; + CoreTiming::ForceCheck(); + } else { + MIPSComp::jit->ClearCache(); + } + } } diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index 1bab17031f..d2f4a4ba54 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -266,6 +266,13 @@ public: void InvalidateICache(u32 address, int length = 4); void ClearJitCache(); + +protected: + void ProcessPendingClears(); + + // Doesn't need save stating. + volatile bool insideJit = false; + volatile bool hasPendingClears = false; }; diff --git a/Core/MIPS/MIPSAsm.cpp b/Core/MIPS/MIPSAsm.cpp index 1c45f14ac5..efecdac2e6 100644 --- a/Core/MIPS/MIPSAsm.cpp +++ b/Core/MIPS/MIPSAsm.cpp @@ -40,9 +40,7 @@ public: Memory::Memcpy((u32)address, data, (u32)length, "Debugger"); // In case this is a delay slot or combined instruction, clear cache above it too. - std::lock_guard guard(MIPSComp::jitLock); - if (MIPSComp::jit) - MIPSComp::jit->InvalidateCacheAt((u32)(address - 4),(int)length+4); + mipsr4k.InvalidateICache((u32)(address - 4), (int)length + 4); address += length; return true;