From a277d66c50e78311874047a700db89b466104289 Mon Sep 17 00:00:00 2001 From: "Jared M. White" Date: Sun, 7 Jul 2024 20:39:50 -0500 Subject: [PATCH] Fix executable booting not functioning, move dirty page initialization to emu thread initialization to fix MMU bug, general code cleanliness improvements --- Source/Core/Core/Core.cpp | 4 +++- Source/Core/Core/HW/Memmap.cpp | 24 +++++++----------------- Source/Core/Core/HW/Memmap.h | 1 - 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index 683fa9cf6f..11494c4cb5 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -383,7 +383,6 @@ static void CpuThread(Core::System& system, const std::optional& sa if (exception_handler) { EMM::InstallExceptionHandler(); - system.GetMemory().InitDirtyPages(); } #ifdef USE_MEMORYWATCHER s_memory_watcher = std::make_unique(); @@ -689,6 +688,9 @@ static void EmuThread(Core::System& system, std::unique_ptr boot cpuThreadFunc(system, savestate_path, delete_savestate); } + if (cpuThreadFunc == CpuThread) + system.GetMemory().InitDirtyPages(); + INFO_LOG_FMT(CONSOLE, "{}", StopMessage(true, "Stopping GDB ...")); GDBStub::Deinit(); INFO_LOG_FMT(CONSOLE, "{}", StopMessage(true, "GDB stopped.")); diff --git a/Source/Core/Core/HW/Memmap.cpp b/Source/Core/Core/HW/Memmap.cpp index a1ac3d4b28..1e87f414b0 100644 --- a/Source/Core/Core/HW/Memmap.cpp +++ b/Source/Core/Core/HW/Memmap.cpp @@ -90,7 +90,8 @@ void MemoryManager::WriteProtectPhysicalMemoryRegions() reinterpret_cast(*region.out_pointer)); } const size_t page_size = Common::PageSize(); - for (size_t i = reinterpret_cast(*region.out_pointer); i < region.size; i += page_size) + const intptr_t out_pointer = reinterpret_cast(*region.out_pointer); + for (size_t i = out_pointer; i < region.size; i += page_size) { m_dirty_pages[i] = false; } @@ -154,7 +155,8 @@ void MemoryManager::Init() m_physical_regions[1] = PhysicalMemoryRegion{ &m_l1_cache, 0xE0000000, GetL1CacheSize(), PhysicalMemoryRegion::ALWAYS, 0, false, false}; m_physical_regions[2] = PhysicalMemoryRegion{ - &m_fake_vmem, 0x7E000000, GetFakeVMemSize(), PhysicalMemoryRegion::FAKE_VMEM, 0, false, false}; + &m_fake_vmem, 0x7E000000, GetFakeVMemSize(), PhysicalMemoryRegion::FAKE_VMEM, 0, + false, false}; m_physical_regions[3] = PhysicalMemoryRegion{ &m_exram, 0x10000000, GetExRamSize(), PhysicalMemoryRegion::WII_ONLY, 0, false, true}; @@ -408,23 +410,11 @@ void MemoryManager::DoState(PointerWrap& p, bool delta) p.DoArray(m_ram + i, page_size); } } - for (size_t i = 0; i < current_l1_cache_size; i += page_size) - { - if (IsPageDirty(reinterpret_cast(&m_l1_cache[i]))) - { - p.DoArray(m_l1_cache + i, page_size); - } - } + p.DoArray(m_l1_cache, current_l1_cache_size); p.DoMarker("Memory RAM"); if (current_have_fake_vmem) { - for (size_t i = 0; i < current_fake_vmem_size; i += page_size) - { - if (IsPageDirty(reinterpret_cast(&m_fake_vmem[i]))) - { - p.DoArray(m_fake_vmem + i, page_size); - } - } + p.DoArray(m_fake_vmem, current_fake_vmem_size); } p.DoMarker("Memory FakeVMEM"); if (current_have_exram) @@ -456,13 +446,13 @@ void MemoryManager::DoState(PointerWrap& p, bool delta) void MemoryManager::Shutdown() { ShutdownFastmemArena(); + m_dirty_pages.clear(); m_is_initialized = false; for (const PhysicalMemoryRegion& region : m_physical_regions) { if (!region.active) continue; - m_arena.ReleaseView(*region.out_pointer, region.size); *region.out_pointer = nullptr; } diff --git a/Source/Core/Core/HW/Memmap.h b/Source/Core/Core/HW/Memmap.h index 834525b9f8..a9b6e52531 100644 --- a/Source/Core/Core/HW/Memmap.h +++ b/Source/Core/Core/HW/Memmap.h @@ -137,7 +137,6 @@ public: bool IsPageDirty(uintptr_t address); void SetPageDirtyBit(uintptr_t address, size_t size, bool dirty); void ResetDirtyPages(); - bool VirtualProtectMemory(u8* data, size_t size, u64 flag); bool HandleFault(uintptr_t fault_address); std::map& GetDirtyPages() { return m_dirty_pages; }