From 7cecb28bdf6443362a6ab20e0042ddb3b407ebec Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 12 Feb 2023 11:07:11 +0100 Subject: DolphinQt: Properly lock CPU before accessing emulated memory This fixes a problem I was having where using frame advance with the debugger open would frequently cause panic alerts about invalid addresses due to the CPU thread changing MSR.DR while the host thread was trying to access memory. To aid in tracking down all the places where we weren't properly locking the CPU, I've created a new type (in Core.h) that you have to pass as a reference or pointer to functions that require running as the CPU thread. --- Source/Core/DolphinQt/Debugger/MemoryWidget.cpp | 27 +++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'Source/Core/DolphinQt/Debugger/MemoryWidget.cpp') diff --git a/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp b/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp index fc6ced4743..ab3e269470 100644 --- a/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp @@ -495,7 +495,9 @@ void MemoryWidget::SetAddress(u32 address) { AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); - good = accessors->IsValidAddress(current_addr); + + Core::CPUThreadGuard guard; + good = accessors->IsValidAddress(guard, current_addr); } if (m_search_address->findText(current_text) == -1 && good) @@ -651,17 +653,20 @@ void MemoryWidget::OnSetValue() return; } + Core::CPUThreadGuard guard; + AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); u32 end_address = target_addr.address + static_cast(bytes.size()) - 1; - if (!accessors->IsValidAddress(target_addr.address) || !accessors->IsValidAddress(end_address)) + if (!accessors->IsValidAddress(guard, target_addr.address) || + !accessors->IsValidAddress(guard, end_address)) { ModalMessageBox::critical(this, tr("Error"), tr("Target address range is invalid.")); return; } for (const char c : bytes) - accessors->WriteU8(target_addr.address++, static_cast(c)); + accessors->WriteU8(guard, target_addr.address++, static_cast(c)); Update(); } @@ -710,8 +715,10 @@ void MemoryWidget::OnSetValueFromFile() AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); + Core::CPUThreadGuard guard; + for (u8 b : file_contents) - accessors->WriteU8(target_addr.address++, b); + accessors->WriteU8(guard, target_addr.address++, b); Update(); } @@ -822,11 +829,15 @@ void MemoryWidget::FindValue(bool next) target_addr.address += next ? 1 : -1; } - AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); + const std::optional found_addr = [&] { + AddressSpace::Accessors* accessors = + AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); - const auto found_addr = - accessors->Search(target_addr.address, reinterpret_cast(search_for.data()), - static_cast(search_for.size()), next); + Core::CPUThreadGuard guard; + return accessors->Search(guard, target_addr.address, + reinterpret_cast(search_for.data()), + static_cast(search_for.size()), next); + }(); if (found_addr.has_value()) { -- cgit v1.2.3