diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 97d8ad8f14..ccc3f92ac7 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -99,6 +99,8 @@ add_library(common JsonUtil.h JsonUtil.cpp Lazy.h + LazyMemoryRegionBitSet.cpp + LazyMemoryRegionBitSet.h LinearDiskCache.h UnixUtil.h Logging/ConsoleListener.h diff --git a/Source/Core/Common/LazyMemoryRegionBitSet.cpp b/Source/Core/Common/LazyMemoryRegionBitSet.cpp new file mode 100644 index 0000000000..af9d79c8df --- /dev/null +++ b/Source/Core/Common/LazyMemoryRegionBitSet.cpp @@ -0,0 +1,96 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Common/LazyMemoryRegionBitSet.h" + +#include + +#include "Common/Align.h" +#include "Common/MsgHandler.h" + +namespace Common +{ +LazyMemoryRegionBitSet::LazyMemoryRegionBitSet(size_t bits) : m_bits(bits), m_valid(Create(bits)) +{ +} + +bool LazyMemoryRegionBitSet::Create(size_t bits) +{ + m_pointer = reinterpret_cast(m_region.Create(Common::AlignUp(bits, 32) / 8)); + if (!m_pointer) + { + PanicAlertFmt("LazyMemoryRegionBitSet failed to create backing region"); + return false; + } + + return true; +} + +void LazyMemoryRegionBitSet::Clear() +{ + m_region.Clear(); +} + +void LazyMemoryRegionBitSet::CheckBitIndicesWriteable(size_t bits_start, size_t bits_end) +{ + CheckBitIndexReadable(bits_end - 1); + const size_t start_byte = Common::AlignDown(bits_start, 32) / 8; + const size_t end_byte = Common::AlignUp(bits_end, 32) / 8; + m_region.EnsureMemoryPagesWritable(start_byte, end_byte - start_byte); +} + +void LazyMemoryRegionBitSet::SetBits(size_t bits_start, size_t bits_end) +{ + if (bits_start >= bits_end) + return; + + CheckBitIndicesWriteable(bits_start, bits_end); + + const u32 extra_first_word = 0xFFFFFFFF << bits_start % 32; + const u32 extra_last_word = ~(0xFFFFFFFF << bits_end % 32); + + if (bits_start / 32 == bits_end / 32) + { + m_pointer[bits_start / 32] |= extra_first_word & extra_last_word; + return; + } + + if (bits_start % 32 != 0) + m_pointer[bits_start / 32] |= extra_first_word; + + const size_t start_index = Common::AlignUp(bits_start, 32) / 32; + const size_t end_index = bits_end / 32; + std::memset(m_pointer + start_index, 0xFF, (end_index - start_index) * 4); + + if (bits_end % 32 != 0) + m_pointer[bits_end / 32] |= extra_last_word; +} + +void LazyMemoryRegionBitSet::ClearBits(size_t bits_start, size_t bits_end) +{ + if (bits_start >= bits_end) + return; + + CheckBitIndicesWriteable(bits_start, bits_end); + + const u32 extra_first_word = ~(0xFFFFFFFF << bits_start % 32); + const u32 extra_last_word = 0xFFFFFFFF << bits_end % 32; + + if (bits_start / 32 == bits_end / 32) + { + m_pointer[bits_start / 32] &= extra_first_word | extra_last_word; + return; + } + + if (bits_start % 32 != 0) + m_pointer[bits_start / 32] &= extra_first_word; + + const size_t start_index = Common::AlignUp(bits_start, 32) / 32; + const size_t end_index = bits_end / 32; + std::memset(m_pointer + start_index, 0x00, (end_index - start_index) * sizeof(u32)); + + if (bits_end % 32 != 0) + m_pointer[bits_end / 32] &= extra_last_word; +} + +} // namespace Common diff --git a/Source/Core/Common/LazyMemoryRegionBitSet.h b/Source/Core/Common/LazyMemoryRegionBitSet.h new file mode 100644 index 0000000000..f075f12c7a --- /dev/null +++ b/Source/Core/Common/LazyMemoryRegionBitSet.h @@ -0,0 +1,76 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include "Common/Assert.h" +#include "Common/MemArena.h" + +namespace Common +{ +// A bit set backed by a LazyMemoryRegion. +class LazyMemoryRegionBitSet final +{ +public: + // Allocates backing memory and creates a bit set. + // Allocating backing memory may fail; use IsValid to check for success. + LazyMemoryRegionBitSet(size_t bits); + + bool IsValid() const { return m_valid; } + + void Clear(); + + bool TestBit(size_t bit) const + { + CheckBitIndexReadable(bit); + return (m_pointer[bit / 32] & (1 << (bit % 32))) != 0; + } + + void SetBit(size_t bit) + { + CheckBitIndexWriteable(bit); + m_region.EnsureMemoryPageWritable(bit / 8); + m_pointer[bit / 32] |= (1 << (bit % 32)); + } + + void ClearBit(size_t bit) + { + CheckBitIndexWriteable(bit); + m_region.EnsureMemoryPageWritable(bit / 8); + m_pointer[bit / 32] &= ~(1 << (bit % 32)); + } + + void SetBits(size_t bits_start, size_t bits_end); + + void ClearBits(size_t bits_start, size_t bits_end); + + const u32* GetRawMemory() const { return m_pointer; } + +private: + LazyMemoryRegion m_region; + u32* m_pointer; + size_t m_bits; + bool m_valid; + + bool Create(size_t bits); + + void CheckBitIndexReadable(size_t bit) const + { + DEBUG_ASSERT_MSG(COMMON, m_valid, "LazyMemoryRegionBitSet isn't valid"); + DEBUG_ASSERT_MSG(COMMON, bit < m_bits, + "Tried to access bit {} in a LazyMemoryRegionBitSet with only {} bits", bit, + m_bits); + } + + void CheckBitIndexWriteable(size_t bit) + { + CheckBitIndexReadable(bit); + m_region.EnsureMemoryPageWritable(bit / 8); + } + + void CheckBitIndicesWriteable(size_t bits_start, size_t bits_end); +}; + +} // namespace Common diff --git a/Source/Core/Common/MemArena.h b/Source/Core/Common/MemArena.h index 654b13b9a1..4ddc3eb8a8 100644 --- a/Source/Core/Common/MemArena.h +++ b/Source/Core/Common/MemArena.h @@ -161,6 +161,8 @@ private: class LazyMemoryRegion final { public: + constexpr static size_t WINDOWS_BLOCK_SIZE = 8 * 1024 * 1024; // size of allocated memory blocks + LazyMemoryRegion(); ~LazyMemoryRegion(); LazyMemoryRegion(const LazyMemoryRegion&) = delete; @@ -199,7 +201,7 @@ public: void EnsureMemoryPageWritable(size_t offset) { #ifdef _WIN32 - const size_t block_index = offset / BLOCK_SIZE; + const size_t block_index = offset / WINDOWS_BLOCK_SIZE; if (m_writable_block_handles[block_index] == nullptr) MakeMemoryBlockWritable(block_index); #endif @@ -209,8 +211,11 @@ public: { #ifdef _WIN32 const size_t end_offset = offset + size; - for (size_t i = Common::AlignDown(offset, BLOCK_SIZE); i < end_offset; i += BLOCK_SIZE) + for (size_t i = Common::AlignDown(offset, WINDOWS_BLOCK_SIZE); i < end_offset; + i += WINDOWS_BLOCK_SIZE) + { EnsureMemoryPageWritable(i); + } #endif } @@ -220,7 +225,6 @@ private: #ifdef _WIN32 void* m_zero_block = nullptr; - constexpr static size_t BLOCK_SIZE = 8 * 1024 * 1024; // size of allocated memory blocks WindowsMemoryFunctions m_memory_functions; std::vector m_writable_block_handles; diff --git a/Source/Core/Common/MemArenaWin.cpp b/Source/Core/Common/MemArenaWin.cpp index 4c296da631..c1e8369271 100644 --- a/Source/Core/Common/MemArenaWin.cpp +++ b/Source/Core/Common/MemArenaWin.cpp @@ -504,8 +504,8 @@ void* LazyMemoryRegion::Create(size_t size) return nullptr; // reserve block of memory - const size_t memory_size = Common::AlignUp(size, BLOCK_SIZE); - const size_t block_count = memory_size / BLOCK_SIZE; + const size_t memory_size = Common::AlignUp(size, WINDOWS_BLOCK_SIZE); + const size_t block_count = memory_size / WINDOWS_BLOCK_SIZE; u8* memory = static_cast(reinterpret_cast(m_memory_functions.m_address_VirtualAlloc2)( nullptr, nullptr, memory_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, @@ -519,13 +519,14 @@ void* LazyMemoryRegion::Create(size_t size) // split into individual block-sized regions for (size_t i = 0; i < block_count - 1; ++i) { - if (!VirtualFree(memory + i * BLOCK_SIZE, BLOCK_SIZE, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) + if (!VirtualFree(memory + i * WINDOWS_BLOCK_SIZE, WINDOWS_BLOCK_SIZE, + MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) { NOTICE_LOG_FMT(MEMMAP, "Region splitting failed: {}", GetLastErrorString()); // release every split block as well as the remaining unsplit one for (size_t j = 0; j < i + 1; ++j) - VirtualFree(memory + j * BLOCK_SIZE, 0, MEM_RELEASE); + VirtualFree(memory + j * WINDOWS_BLOCK_SIZE, 0, MEM_RELEASE); return nullptr; } @@ -535,8 +536,9 @@ void* LazyMemoryRegion::Create(size_t size) m_size = memory_size; // allocate a single block of real memory in the page file - HANDLE zero_block = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READONLY, - GetHighDWORD(BLOCK_SIZE), GetLowDWORD(BLOCK_SIZE), nullptr); + HANDLE zero_block = + CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READONLY, + GetHighDWORD(WINDOWS_BLOCK_SIZE), GetLowDWORD(WINDOWS_BLOCK_SIZE), nullptr); if (zero_block == nullptr) { NOTICE_LOG_FMT(MEMMAP, "CreateFileMapping() failed for zero block: {}", GetLastErrorString()); @@ -550,8 +552,8 @@ void* LazyMemoryRegion::Create(size_t size) for (size_t i = 0; i < block_count; ++i) { void* result = reinterpret_cast(m_memory_functions.m_address_MapViewOfFile3)( - zero_block, nullptr, memory + i * BLOCK_SIZE, 0, BLOCK_SIZE, MEM_REPLACE_PLACEHOLDER, - PAGE_READONLY, nullptr, 0); + zero_block, nullptr, memory + i * WINDOWS_BLOCK_SIZE, 0, WINDOWS_BLOCK_SIZE, + MEM_REPLACE_PLACEHOLDER, PAGE_READONLY, nullptr, 0); if (!result) { NOTICE_LOG_FMT(MEMMAP, "Mapping the zero block failed: {}", GetLastErrorString()); @@ -578,7 +580,7 @@ void LazyMemoryRegion::Clear() // unmap the writable block if (!reinterpret_cast(m_memory_functions.m_address_UnmapViewOfFileEx)( - memory + i * BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER)) + memory + i * WINDOWS_BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER)) { PanicAlertFmt("Failed to unmap the writable block: {}", GetLastErrorString()); } @@ -593,8 +595,8 @@ void LazyMemoryRegion::Clear() // map the zero block void* map_result = reinterpret_cast(m_memory_functions.m_address_MapViewOfFile3)( - m_zero_block, nullptr, memory + i * BLOCK_SIZE, 0, BLOCK_SIZE, MEM_REPLACE_PLACEHOLDER, - PAGE_READONLY, nullptr, 0); + m_zero_block, nullptr, memory + i * WINDOWS_BLOCK_SIZE, 0, WINDOWS_BLOCK_SIZE, + MEM_REPLACE_PLACEHOLDER, PAGE_READONLY, nullptr, 0); if (!map_result) { PanicAlertFmt("Failed to re-map the zero block: {}", GetLastErrorString()); @@ -611,7 +613,7 @@ void LazyMemoryRegion::Release() for (size_t i = 0; i < m_writable_block_handles.size(); ++i) { reinterpret_cast(m_memory_functions.m_address_UnmapViewOfFileEx)( - memory + i * BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER); + memory + i * WINDOWS_BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER); if (m_writable_block_handles[i]) { CloseHandle(m_writable_block_handles[i]); @@ -627,9 +629,9 @@ void LazyMemoryRegion::Release() if (m_memory) { u8* const memory = static_cast(m_memory); - const size_t block_count = m_size / BLOCK_SIZE; + const size_t block_count = m_size / WINDOWS_BLOCK_SIZE; for (size_t i = 0; i < block_count; ++i) - VirtualFree(memory + i * BLOCK_SIZE, 0, MEM_RELEASE); + VirtualFree(memory + i * WINDOWS_BLOCK_SIZE, 0, MEM_RELEASE); m_memory = nullptr; m_size = 0; } @@ -641,15 +643,16 @@ void LazyMemoryRegion::MakeMemoryBlockWritable(size_t block_index) // unmap the zero block if (!reinterpret_cast(m_memory_functions.m_address_UnmapViewOfFileEx)( - memory + block_index * BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER)) + memory + block_index * WINDOWS_BLOCK_SIZE, MEM_PRESERVE_PLACEHOLDER)) { PanicAlertFmt("Failed to unmap the zero block: {}", GetLastErrorString()); return; } // allocate a fresh block to map - HANDLE block = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, - GetHighDWORD(BLOCK_SIZE), GetLowDWORD(BLOCK_SIZE), nullptr); + HANDLE block = + CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, + GetHighDWORD(WINDOWS_BLOCK_SIZE), GetLowDWORD(WINDOWS_BLOCK_SIZE), nullptr); if (block == nullptr) { PanicAlertFmt("CreateFileMapping() failed for writable block: {}", GetLastErrorString()); @@ -658,8 +661,8 @@ void LazyMemoryRegion::MakeMemoryBlockWritable(size_t block_index) // map the new block void* map_result = reinterpret_cast(m_memory_functions.m_address_MapViewOfFile3)( - block, nullptr, memory + block_index * BLOCK_SIZE, 0, BLOCK_SIZE, MEM_REPLACE_PLACEHOLDER, - PAGE_READWRITE, nullptr, 0); + block, nullptr, memory + block_index * WINDOWS_BLOCK_SIZE, 0, WINDOWS_BLOCK_SIZE, + MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0); if (!map_result) { PanicAlertFmt("Failed to map the writable block: {}", GetLastErrorString()); diff --git a/Source/Core/Core/FifoPlayer/FifoPlayer.cpp b/Source/Core/Core/FifoPlayer/FifoPlayer.cpp index f0c62d4e3f..808f0b62d6 100644 --- a/Source/Core/Core/FifoPlayer/FifoPlayer.cpp +++ b/Source/Core/Core/FifoPlayer/FifoPlayer.cpp @@ -217,7 +217,7 @@ public: ~CPUCore() override {} CPUCore& operator=(const CPUCore&) = delete; - void Init() override + bool Init() override { IsPlayingBackFifologWithBrokenEFBCopies = m_parent->m_File->HasBrokenEFBCopies(); // Without this call, we deadlock in initialization in dual core, as the FIFO is disabled and @@ -226,6 +226,8 @@ public: m_parent->m_CurrentFrame = m_parent->m_FrameRangeStart; m_parent->LoadMemory(); + + return true; } void Shutdown() override { IsPlayingBackFifologWithBrokenEFBCopies = false; } diff --git a/Source/Core/Core/PowerPC/CPUCoreBase.h b/Source/Core/Core/PowerPC/CPUCoreBase.h index 58f60416d5..029e3927be 100644 --- a/Source/Core/Core/PowerPC/CPUCoreBase.h +++ b/Source/Core/Core/PowerPC/CPUCoreBase.h @@ -7,7 +7,7 @@ class CPUCoreBase { public: virtual ~CPUCoreBase() = default; - virtual void Init() = 0; + [[nodiscard]] virtual bool Init() = 0; virtual void Shutdown() = 0; virtual void ClearCache() = 0; virtual void Run() = 0; diff --git a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.cpp b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.cpp index 3ec24a3016..2ec59b1180 100644 --- a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.cpp +++ b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.cpp @@ -31,8 +31,11 @@ CachedInterpreter::CachedInterpreter(Core::System& system) : JitBase(system), m_ CachedInterpreter::~CachedInterpreter() = default; -void CachedInterpreter::Init() +bool CachedInterpreter::Init() { + if (!CheckValidity()) + return false; + RefreshConfig(); AllocCodeSpace(CODE_SIZE); @@ -40,11 +43,14 @@ void CachedInterpreter::Init() jo.enableBlocklink = false; - m_block_cache.Init(); + if (!m_block_cache.Init()) + return false; code_block.m_stats = &js.st; code_block.m_gpa = &js.gpa; code_block.m_fpa = &js.fpa; + + return true; } void CachedInterpreter::Shutdown() diff --git a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.h b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.h index dd940e89a2..87d0b5cd86 100644 --- a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.h +++ b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreter.h @@ -32,7 +32,7 @@ public: CachedInterpreter& operator=(CachedInterpreter&&) = delete; ~CachedInterpreter() override; - void Init() override; + [[nodiscard]] bool Init() override; void Shutdown() override; bool HandleFault(uintptr_t access_address, SContext* ctx) override { return false; } diff --git a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.cpp b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.cpp index 3b4c4a0637..8dcac6dbde 100644 --- a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.cpp +++ b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.cpp @@ -10,10 +10,10 @@ CachedInterpreterBlockCache::CachedInterpreterBlockCache(JitBase& jit) : JitBase { } -void CachedInterpreterBlockCache::Init() +bool CachedInterpreterBlockCache::Init() { - JitBaseBlockCache::Init(); ClearRangesToFree(); + return JitBaseBlockCache::Init(); } void CachedInterpreterBlockCache::DestroyBlock(JitBlock& block) diff --git a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.h b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.h index 5b34a9cfc9..974a0710bd 100644 --- a/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.h +++ b/Source/Core/Core/PowerPC/CachedInterpreter/CachedInterpreterBlockCache.h @@ -16,7 +16,7 @@ class CachedInterpreterBlockCache final : public JitBaseBlockCache public: explicit CachedInterpreterBlockCache(JitBase& jit); - void Init() override; + [[nodiscard]] bool Init() override; void DestroyBlock(JitBlock& block) override; diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp index 5ff6fa08cb..affdc43936 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp @@ -70,9 +70,11 @@ Interpreter::Interpreter(Core::System& system, PowerPC::PowerPCState& ppc_state, Interpreter::~Interpreter() = default; -void Interpreter::Init() +bool Interpreter::Init() { m_end_block = false; + + return true; } void Interpreter::Shutdown() diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h index b3816e701b..52dd0d6cc9 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h @@ -32,7 +32,7 @@ public: Interpreter& operator=(Interpreter&&) = delete; ~Interpreter() override; - void Init() override; + bool Init() override; void Shutdown() override; void SingleStep() override; int SingleStepInner(); diff --git a/Source/Core/Core/PowerPC/Jit64/Jit.cpp b/Source/Core/Core/PowerPC/Jit64/Jit.cpp index 1248b1ab01..18af21a57e 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit.cpp @@ -34,6 +34,7 @@ #include "Core/Host.h" #include "Core/MachineContext.h" #include "Core/PatchEngine.h" +#include "Core/PowerPC/Gekko.h" #include "Core/PowerPC/Interpreter/Interpreter.h" #include "Core/PowerPC/Jit64/JitAsm.h" #include "Core/PowerPC/Jit64/RegCache/JitRegCache.h" @@ -253,8 +254,11 @@ bool Jit64::BackPatch(SContext* ctx) return true; } -void Jit64::Init() +bool Jit64::Init() { + if (!CheckValidity()) + return false; + InitFastmemArena(); RefreshConfig(); @@ -284,7 +288,9 @@ void Jit64::Init() m_stack_guard = nullptr; - blocks.Init(); + if (!blocks.Init()) + return false; + asm_routines.Init(); // important: do this *after* generating the global asm routines, because we can't use farcode in @@ -299,6 +305,8 @@ void Jit64::Init() EnableOptimization(); ResetFreeMemoryRanges(); + + return true; } void Jit64::ClearCache() @@ -958,7 +966,7 @@ bool Jit64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) // Assume that GQR values don't change often at runtime. Many paired-heavy games use largely float // loads and stores, which are significantly faster when inlined (especially in MMU mode, where // this lets them use fastmem). - if (!js.pairedQuantizeAddresses.contains(js.blockStart)) + if (!js.pairedQuantizeAddresses.TestBit(js.blockStart / sizeof(UGeckoInstruction))) { // If there are GQRs used but not set, we'll treat those as constant and optimize them BitSet8 gqr_static = ComputeStaticGQRs(code_block); @@ -987,7 +995,7 @@ bool Jit64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) } } - if (!js.noSpeculativeConstantsAddresses.contains(js.blockStart)) + if (!js.noSpeculativeConstantsAddresses.TestBit(js.blockStart / sizeof(UGeckoInstruction))) { IntializeSpeculativeConstants(); } @@ -1015,7 +1023,8 @@ bool Jit64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) { // Gather pipe writes using a non-immediate address are discovered by profiling. const u32 prev_address = m_code_buffer[i - 1].address; - bool gatherPipeIntCheck = js.fifoWriteAddresses.contains(prev_address); + bool gatherPipeIntCheck = + js.fifoWriteAddresses.TestBit(prev_address / sizeof(UGeckoInstruction)); // Gather pipe writes using an immediate address are explicitly tracked. if (jo.optimizeGatherPipe && diff --git a/Source/Core/Core/PowerPC/Jit64/Jit.h b/Source/Core/Core/PowerPC/Jit64/Jit.h index da1be2db04..34275eada4 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit.h +++ b/Source/Core/Core/PowerPC/Jit64/Jit.h @@ -50,7 +50,7 @@ public: Jit64& operator=(Jit64&&) = delete; ~Jit64() override; - void Init() override; + [[nodiscard]] bool Init() override; void Shutdown() override; bool HandleFault(uintptr_t access_address, SContext* ctx) override; diff --git a/Source/Core/Core/PowerPC/Jit64Common/BlockCache.cpp b/Source/Core/Core/PowerPC/Jit64Common/BlockCache.cpp index 34c327df28..fef250e026 100644 --- a/Source/Core/Core/PowerPC/Jit64Common/BlockCache.cpp +++ b/Source/Core/Core/PowerPC/Jit64Common/BlockCache.cpp @@ -50,10 +50,10 @@ void JitBlockCache::WriteDestroyBlock(const JitBlock& block) emit.INT3(); } -void JitBlockCache::Init() +bool JitBlockCache::Init() { - JitBaseBlockCache::Init(); ClearRangesToFree(); + return JitBaseBlockCache::Init(); } void JitBlockCache::DestroyBlock(JitBlock& block) diff --git a/Source/Core/Core/PowerPC/Jit64Common/BlockCache.h b/Source/Core/Core/PowerPC/Jit64Common/BlockCache.h index a4e7bfa5ce..af084ddc2c 100644 --- a/Source/Core/Core/PowerPC/Jit64Common/BlockCache.h +++ b/Source/Core/Core/PowerPC/Jit64Common/BlockCache.h @@ -14,7 +14,7 @@ class JitBlockCache : public JitBaseBlockCache public: explicit JitBlockCache(JitBase& jit); - void Init() override; + [[nodiscard]] bool Init() override; void DestroyBlock(JitBlock& block) override; diff --git a/Source/Core/Core/PowerPC/JitArm64/Jit.cpp b/Source/Core/Core/PowerPC/JitArm64/Jit.cpp index aba973346d..14ba572cf7 100644 --- a/Source/Core/Core/PowerPC/JitArm64/Jit.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/Jit.cpp @@ -31,6 +31,7 @@ #include "Core/HW/ProcessorInterface.h" #include "Core/Host.h" #include "Core/PatchEngine.h" +#include "Core/PowerPC/Gekko.h" #include "Core/PowerPC/Interpreter/Interpreter.h" #include "Core/PowerPC/JitArm64/JitArm64_RegCache.h" #include "Core/PowerPC/JitCommon/ConstantPropagation.h" @@ -56,8 +57,11 @@ JitArm64::JitArm64(Core::System& system) JitArm64::~JitArm64() = default; -void JitArm64::Init() +bool JitArm64::Init() { + if (!CheckValidity()) + return false; + InitFastmemArena(); RefreshConfig(); @@ -80,7 +84,9 @@ void JitArm64::Init() SetOptimizationEnabled(true); gpr.Init(this); fpr.Init(this); - blocks.Init(); + + if (!blocks.Init()) + return false; code_block.m_stats = &js.st; code_block.m_gpa = &js.gpa; @@ -89,6 +95,8 @@ void JitArm64::Init() InitBLROptimization(); GenerateAsmAndResetFreeMemoryRanges(); + + return true; } void JitArm64::SetBlockLinkingEnabled(bool enabled) @@ -1176,7 +1184,8 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) if (IsProfilingEnabled()) ABI_CallFunction(&JitBlock::ProfileData::BeginProfiling, b->profile_data.get()); - if (code_block.m_gqr_used.Count() == 1 && !js.pairedQuantizeAddresses.contains(js.blockStart)) + if (code_block.m_gqr_used.Count() == 1 && + !js.pairedQuantizeAddresses.TestBit(js.blockStart / sizeof(UGeckoInstruction))) { int gqr = *code_block.m_gqr_used.begin(); if (!code_block.m_gqr_modified[gqr] && !GQR(m_ppc_state, gqr)) @@ -1202,7 +1211,7 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) m_constant_propagation.Clear(); - if (!js.noSpeculativeConstantsAddresses.contains(js.blockStart)) + if (!js.noSpeculativeConstantsAddresses.TestBit(js.blockStart / sizeof(UGeckoInstruction))) { IntializeSpeculativeConstants(); } @@ -1233,7 +1242,8 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC) { // Gather pipe writes using a non-immediate address are discovered by profiling. const u32 prev_address = m_code_buffer[i - 1].address; - bool gatherPipeIntCheck = js.fifoWriteAddresses.contains(prev_address); + bool gatherPipeIntCheck = + js.fifoWriteAddresses.TestBit(prev_address / sizeof(UGeckoInstruction)); if (jo.optimizeGatherPipe && (js.fifoBytesSinceCheck >= GPFifo::GATHER_PIPE_SIZE || js.mustCheckFifo)) diff --git a/Source/Core/Core/PowerPC/JitArm64/Jit.h b/Source/Core/Core/PowerPC/JitArm64/Jit.h index 9c23112e1c..ef1ff620d8 100644 --- a/Source/Core/Core/PowerPC/JitArm64/Jit.h +++ b/Source/Core/Core/PowerPC/JitArm64/Jit.h @@ -30,7 +30,7 @@ public: JitArm64& operator=(JitArm64&&) = delete; ~JitArm64() override; - void Init() override; + [[nodiscard]] bool Init() override; void Shutdown() override; JitCommon::ConstantPropagation& GetConstantPropagation() { return m_constant_propagation; } diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.cpp index e991fe04c9..e1d0f46798 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.cpp @@ -13,10 +13,10 @@ JitArm64BlockCache::JitArm64BlockCache(JitBase& jit) : JitBaseBlockCache{jit} { } -void JitArm64BlockCache::Init() +bool JitArm64BlockCache::Init() { - JitBaseBlockCache::Init(); ClearRangesToFree(); + return JitBaseBlockCache::Init(); } void JitArm64BlockCache::WriteLinkBlock(Arm64Gen::ARM64XEmitter& emit, diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.h b/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.h index 1214e8c308..9e9f25141a 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.h +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64Cache.h @@ -17,7 +17,7 @@ class JitArm64BlockCache : public JitBaseBlockCache public: explicit JitArm64BlockCache(JitBase& jit); - void Init() override; + [[nodiscard]] bool Init() override; void DestroyBlock(JitBlock& block) override; diff --git a/Source/Core/Core/PowerPC/JitCommon/JitBase.cpp b/Source/Core/Core/PowerPC/JitCommon/JitBase.cpp index b2ff246cce..b47fffe15b 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitBase.cpp +++ b/Source/Core/Core/PowerPC/JitCommon/JitBase.cpp @@ -113,6 +113,29 @@ JitBase::~JitBase() CPUThreadConfigCallback::RemoveConfigChangedCallback(m_registered_config_callback_id); } +bool JitBase::CheckValidity() const +{ + if (!js.fifoWriteAddresses.IsValid()) + { + ERROR_LOG_FMT(DYNA_REC, "Failed to create fifoWriteAddresses"); + return false; + } + + if (!js.pairedQuantizeAddresses.IsValid()) + { + ERROR_LOG_FMT(DYNA_REC, "Failed to create pairedQuantizeAddresses"); + return false; + } + + if (!js.noSpeculativeConstantsAddresses.IsValid()) + { + ERROR_LOG_FMT(DYNA_REC, "Failed to create noSpeculativeConstantsAddresses"); + return false; + } + + return true; +} + bool JitBase::DoesConfigNeedRefresh() const { return std::ranges::any_of(JIT_SETTINGS, [this](const auto& pair) { diff --git a/Source/Core/Core/PowerPC/JitCommon/JitBase.h b/Source/Core/Core/PowerPC/JitCommon/JitBase.h index 612161b492..008009d31c 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitBase.h +++ b/Source/Core/Core/PowerPC/JitCommon/JitBase.h @@ -8,18 +8,19 @@ #include #include #include -#include #include #include #include "Common/BitSet.h" #include "Common/CommonTypes.h" #include "Common/Config/ConfigInfo.h" +#include "Common/LazyMemoryRegionBitSet.h" #include "Common/x64Emitter.h" #include "Core/CPUThreadConfigCallback.h" #include "Core/ConfigManager.h" #include "Core/MachineContext.h" #include "Core/PowerPC/CPUCoreBase.h" +#include "Core/PowerPC/Gekko.h" #include "Core/PowerPC/JitCommon/JitAsmCommon.h" #include "Core/PowerPC/JitCommon/JitCache.h" #include "Core/PowerPC/PPCAnalyst.h" @@ -80,6 +81,8 @@ protected: static constexpr size_t GUARD_SIZE = 64 * 1024; static constexpr size_t GUARD_OFFSET = SAFE_STACK_SIZE - GUARD_SIZE; + static constexpr size_t INSTRUCTION_BIT_SET_SIZE = (1ULL << 32) / sizeof(UGeckoInstruction); + struct JitOptions { bool enableBlocklink; @@ -128,9 +131,9 @@ protected: JitBlock* curBlock; - std::unordered_set fifoWriteAddresses; - std::unordered_set pairedQuantizeAddresses; - std::unordered_set noSpeculativeConstantsAddresses; + Common::LazyMemoryRegionBitSet fifoWriteAddresses{INSTRUCTION_BIT_SET_SIZE}; + Common::LazyMemoryRegionBitSet pairedQuantizeAddresses{INSTRUCTION_BIT_SET_SIZE}; + Common::LazyMemoryRegionBitSet noSpeculativeConstantsAddresses{INSTRUCTION_BIT_SET_SIZE}; }; PPCAnalyst::CodeBlock code_block; @@ -170,6 +173,8 @@ protected: static const std::array*>, 25> JIT_SETTINGS; + [[nodiscard]] bool CheckValidity() const; + bool DoesConfigNeedRefresh() const; void RefreshConfig(); diff --git a/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp b/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp index 4eecedc321..4039c09c4c 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp +++ b/Source/Core/Core/PowerPC/JitCommon/JitCache.cpp @@ -13,11 +13,13 @@ #include #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/JitRegister.h" #include "Core/Config/MainSettings.h" #include "Core/Core.h" #include "Core/Host.h" +#include "Core/PowerPC/Gekko.h" #include "Core/PowerPC/JitCommon/JitBase.h" #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PPCSymbolDB.h" @@ -52,8 +54,14 @@ JitBaseBlockCache::JitBaseBlockCache(JitBase& jit) : m_jit{jit} JitBaseBlockCache::~JitBaseBlockCache() = default; -void JitBaseBlockCache::Init() +bool JitBaseBlockCache::Init() { + if (!valid_block.IsValid()) + { + ERROR_LOG_FMT(DYNA_REC, "Failed to create valid_block"); + return false; + } + Common::JitRegister::Init(Config::Get(Config::MAIN_PERF_MAP_DIR)); m_entry_points_ptr = nullptr; @@ -63,6 +71,8 @@ void JitBaseBlockCache::Init() #endif Clear(); + + return true; } void JitBaseBlockCache::Shutdown() @@ -79,9 +89,9 @@ void JitBaseBlockCache::Clear() #if defined(_DEBUG) || defined(DEBUGFAST) Core::DisplayMessage("Clearing code cache.", 3000); #endif - m_jit.js.fifoWriteAddresses.clear(); - m_jit.js.pairedQuantizeAddresses.clear(); - m_jit.js.noSpeculativeConstantsAddresses.clear(); + m_jit.js.fifoWriteAddresses.Clear(); + m_jit.js.pairedQuantizeAddresses.Clear(); + m_jit.js.noSpeculativeConstantsAddresses.Clear(); for (auto& e : block_map) { DestroyBlock(e.second); @@ -90,16 +100,16 @@ void JitBaseBlockCache::Clear() links_to.clear(); block_range_map.clear(); - valid_block.ClearAll(); + valid_block.Clear(); if (m_entry_points_ptr) m_entry_points_arena.Clear(); } -void JitBaseBlockCache::Reset() +bool JitBaseBlockCache::Reset() { Shutdown(); - Init(); + return Init(); } u8** JitBaseBlockCache::GetEntryPoints() @@ -172,8 +182,9 @@ void JitBaseBlockCache::FinalizeBlock(JitBlock& block, bool block_link, for (auto [range_start, range_end] : block.physical_addresses) { - for (u32 i = range_start & ~31; i < range_end; i += 32) - valid_block.Set(i / 32); + valid_block.SetBits(range_start / VALID_BLOCK_CACHE_LINE_SIZE, + Common::AlignUp(range_end, VALID_BLOCK_CACHE_LINE_SIZE) / + VALID_BLOCK_CACHE_LINE_SIZE); for (u32 i = range_start & BLOCK_RANGE_MAP_MASK; i < range_end; i += BLOCK_RANGE_SIZE) block_range_map[i].insert(&block); @@ -311,20 +322,19 @@ void JitBaseBlockCache::InvalidateICacheInternal(u32 physical_address, u32 addre bool destroy_block = true; if (length == 32 && (physical_address & 0x1fu) == 0) { - if (!valid_block.Test(physical_address / 32)) + if (!valid_block.TestBit(physical_address / VALID_BLOCK_CACHE_LINE_SIZE)) destroy_block = false; else - valid_block.Clear(physical_address / 32); + valid_block.ClearBit(physical_address / VALID_BLOCK_CACHE_LINE_SIZE); } else if (length > 32) { // Even if we can't check the set for optimization, we still want to remove all fully covered // cache lines from the valid_block set so that later calls don't try to invalidate already // cleared regions. - const u32 covered_block_start = (physical_address + 0x1f) / 32; - const u32 covered_block_end = (physical_address + length) / 32; - for (u32 i = covered_block_start; i < covered_block_end; ++i) - valid_block.Clear(i); + const u32 covered_block_start = (physical_address + 0x1f) / VALID_BLOCK_CACHE_LINE_SIZE; + const u32 covered_block_end = (physical_address + length) / VALID_BLOCK_CACHE_LINE_SIZE; + valid_block.ClearBits(covered_block_start, covered_block_end); } if (destroy_block) @@ -338,12 +348,11 @@ void JitBaseBlockCache::InvalidateICacheInternal(u32 physical_address, u32 addre // being in the right place between instructions). if (!forced) { - for (u32 i = address; i < address + length; i += 4) - { - m_jit.js.fifoWriteAddresses.erase(i); - m_jit.js.pairedQuantizeAddresses.erase(i); - m_jit.js.noSpeculativeConstantsAddresses.erase(i); - } + const size_t start_bit = address / sizeof(UGeckoInstruction); + const size_t end_bit = (address + length) / sizeof(UGeckoInstruction); + m_jit.js.fifoWriteAddresses.ClearBits(start_bit, end_bit); + m_jit.js.pairedQuantizeAddresses.ClearBits(start_bit, end_bit); + m_jit.js.noSpeculativeConstantsAddresses.ClearBits(start_bit, end_bit); } } } @@ -422,9 +431,9 @@ void JitBaseBlockCache::EraseSingleBlock(const JitBlock& block) block_map.erase(block_map_iter); // The original JitBlock reference is now dangling. } -u32* JitBaseBlockCache::GetBlockBitSet() const +const u32* JitBaseBlockCache::GetBlockBitSet() const { - return valid_block.m_valid_block.get(); + return valid_block.GetRawMemory(); } void JitBaseBlockCache::WriteDestroyBlock(const JitBlock& block) diff --git a/Source/Core/Core/PowerPC/JitCommon/JitCache.h b/Source/Core/Core/PowerPC/JitCommon/JitCache.h index 8860a42b06..12977813a4 100644 --- a/Source/Core/Core/PowerPC/JitCommon/JitCache.h +++ b/Source/Core/Core/PowerPC/JitCommon/JitCache.h @@ -16,6 +16,7 @@ #include #include "Common/CommonTypes.h" +#include "Common/LazyMemoryRegionBitSet.h" #include "Common/RangeSet.h" #include "Core/HW/Memmap.h" #include "Core/PowerPC/Gekko.h" @@ -112,36 +113,6 @@ struct JitBlock : public JitBlockData typedef void (*CompiledCode)(); -// This is essentially just an std::bitset, but Visual Studia 2013's -// implementation of std::bitset is slow. -class ValidBlockBitSet final -{ -public: - enum - { - // ValidBlockBitSet covers the whole 32-bit address-space in 32-byte - // chunks. - // FIXME: Maybe we can get away with less? There isn't any actual - // RAM in most of this space. - VALID_BLOCK_MASK_SIZE = (1ULL << 32) / 32, - // The number of elements in the allocated array. Each u32 contains 32 bits. - VALID_BLOCK_ALLOC_ELEMENTS = VALID_BLOCK_MASK_SIZE / 32 - }; - // Directly accessed by Jit64. - std::unique_ptr m_valid_block; - - ValidBlockBitSet() - { - m_valid_block.reset(new u32[VALID_BLOCK_ALLOC_ELEMENTS]); - ClearAll(); - } - - void Set(u32 bit) { m_valid_block[bit / 32] |= 1u << (bit % 32); } - void Clear(u32 bit) { m_valid_block[bit / 32] &= ~(1u << (bit % 32)); } - void ClearAll() { memset(m_valid_block.get(), 0, sizeof(u32) * VALID_BLOCK_ALLOC_ELEMENTS); } - bool Test(u32 bit) const { return (m_valid_block[bit / 32] & (1u << (bit % 32))) != 0; } -}; - class JitBaseBlockCache { public: @@ -154,10 +125,10 @@ public: explicit JitBaseBlockCache(JitBase& jit); virtual ~JitBaseBlockCache(); - virtual void Init(); + [[nodiscard]] virtual bool Init(); void Shutdown(); void Clear(); - void Reset(); + [[nodiscard]] bool Reset(); // Code Cache u8** GetEntryPoints(); @@ -187,7 +158,7 @@ public: void ErasePhysicalRange(u32 address, u32 length); void EraseSingleBlock(const JitBlock& block); - u32* GetBlockBitSet() const; + const u32* GetBlockBitSet() const; protected: virtual void DestroyBlock(JitBlock& block); @@ -223,9 +194,10 @@ private: static constexpr u32 BLOCK_RANGE_MAP_MASK = ~(BLOCK_RANGE_SIZE - 1); std::map> block_range_map; - // This bitsets shows which cachelines overlap with any blocks. + // This bitset shows which cachelines overlap with any blocks. // It is used to provide a fast way to query if no icache invalidation is needed. - ValidBlockBitSet valid_block; + static constexpr u32 VALID_BLOCK_CACHE_LINE_SIZE = 32; + Common::LazyMemoryRegionBitSet valid_block{(1ULL << 32) / VALID_BLOCK_CACHE_LINE_SIZE}; // This contains the entry points for each block. // It is used by the assembly dispatcher to quickly diff --git a/Source/Core/Core/PowerPC/JitInterface.cpp b/Source/Core/Core/PowerPC/JitInterface.cpp index f229be35f2..2054441684 100644 --- a/Source/Core/Core/PowerPC/JitInterface.cpp +++ b/Source/Core/Core/PowerPC/JitInterface.cpp @@ -13,6 +13,7 @@ #include "Core/Core.h" #include "Core/PowerPC/CPUCoreBase.h" #include "Core/PowerPC/CachedInterpreter/CachedInterpreter.h" +#include "Core/PowerPC/Gekko.h" #include "Core/PowerPC/JitCommon/JitBase.h" #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PPCSymbolDB.h" @@ -68,7 +69,8 @@ CPUCoreBase* JitInterface::InitJitCore(PowerPC::CPUCore core) m_jit.reset(); return nullptr; } - m_jit->Init(); + if (!m_jit->Init()) + m_jit.reset(); return m_jit.get(); } @@ -311,7 +313,7 @@ void JitInterface::CompileExceptionCheck(ExceptionType type) if (!m_jit) return; - std::unordered_set* exception_addresses = nullptr; + Common::LazyMemoryRegionBitSet* exception_addresses = nullptr; switch (type) { @@ -327,7 +329,7 @@ void JitInterface::CompileExceptionCheck(ExceptionType type) } auto& ppc_state = m_system.GetPPCState(); - if (ppc_state.pc != 0 && !exception_addresses->contains(ppc_state.pc)) + if (ppc_state.pc != 0 && !exception_addresses->TestBit(ppc_state.pc / sizeof(UGeckoInstruction))) { if (type == ExceptionType::FIFOWrite) { @@ -341,11 +343,11 @@ void JitInterface::CompileExceptionCheck(ExceptionType type) if (optype != OpType::Store && optype != OpType::StoreFP && optype != OpType::StorePS) return; } - exception_addresses->insert(ppc_state.pc); + exception_addresses->SetBit(ppc_state.pc / sizeof(UGeckoInstruction)); // Invalidate the JIT block so that it gets recompiled with the external exception check // included. - m_jit->GetBlockCache()->InvalidateICache(ppc_state.pc, 4, true); + m_jit->GetBlockCache()->InvalidateICache(ppc_state.pc, sizeof(UGeckoInstruction), true); } } diff --git a/Source/Core/Core/PowerPC/PowerPC.cpp b/Source/Core/Core/PowerPC/PowerPC.cpp index 564d74d1f6..f6d09356f6 100644 --- a/Source/Core/Core/PowerPC/PowerPC.cpp +++ b/Source/Core/Core/PowerPC/PowerPC.cpp @@ -380,9 +380,11 @@ void PowerPCManager::InjectExternalCPUCore(CPUCoreBase* new_cpu) return; } - new_cpu->Init(); + if (!new_cpu->Init()) + new_cpu = nullptr; + m_cpu_core_base = new_cpu; - m_cpu_core_base_is_injected = true; + m_cpu_core_base_is_injected = new_cpu != nullptr; } void PowerPCManager::SingleStep() diff --git a/Source/UnitTests/Common/CMakeLists.txt b/Source/UnitTests/Common/CMakeLists.txt index f7689b3ec4..70425210a8 100644 --- a/Source/UnitTests/Common/CMakeLists.txt +++ b/Source/UnitTests/Common/CMakeLists.txt @@ -14,6 +14,7 @@ add_dolphin_test(FileUtilTest FileUtilTest.cpp) add_dolphin_test(FixedSizeQueueTest FixedSizeQueueTest.cpp) add_dolphin_test(FlagTest FlagTest.cpp) add_dolphin_test(FloatUtilsTest FloatUtilsTest.cpp) +add_dolphin_test(LazyMemoryRegionBitSetTest LazyMemoryRegionBitSetTest.cpp) add_dolphin_test(MathUtilTest MathUtilTest.cpp) add_dolphin_test(MutexTest MutexTest.cpp) add_dolphin_test(NandPathsTest NandPathsTest.cpp) diff --git a/Source/UnitTests/Common/LazyMemoryRegionBitSetTest.cpp b/Source/UnitTests/Common/LazyMemoryRegionBitSetTest.cpp new file mode 100644 index 0000000000..e9b9a12716 --- /dev/null +++ b/Source/UnitTests/Common/LazyMemoryRegionBitSetTest.cpp @@ -0,0 +1,162 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include "Common/LazyMemoryRegionBitSet.h" +#include "Common/MemArena.h" + +// Intentionally not divisible by 8 +static constexpr size_t BIT_COUNT = 1024 + 512 + 1; + +// Two blocks for the test to write to, and one block that remains mapped to the default page so we +// can check that we didn't accidentally write to the default page +static constexpr size_t WINDOWS_BOUNDARY_BIT_COUNT = + Common::LazyMemoryRegion::WINDOWS_BLOCK_SIZE * 8 * 3; + +static u32 ExpectedContentAllZeroes(size_t) +{ + return 0; +} + +static u32 ExpectedContentAllOnes(size_t) +{ + return 0xFFFFFFFF; +} + +static u32 ExpectedContentAfterSetting(size_t index) +{ + switch (index) + { + case 32: + return 0x0000FF00; + case 34: + return 0x0FFFFFF0; + case 35: + return 0xFFFFFFF0; + case 36: + return 0x0FFFFFFF; + case 37: + return 0xFFFFFFF0; + case 39: + return 0x0FFFFFFF; + case 40: + return 0xFFFFFFF0; + case 47: + return 0x0FFFFFFF; + default: + if (index < 32) + return 1 << index; + if (index < 48) + return 0xFFFFFFFF; + return 0; + } +} + +static u32 ExpectedContentAfterClearing(size_t index) +{ + return ~ExpectedContentAfterSetting(index); +} + +static u32 ExpectedContentForWindowsBoundary(size_t index) +{ + switch (index) + { + case Common::LazyMemoryRegion::WINDOWS_BLOCK_SIZE / sizeof(u32) - 1: + return 0x80000000; + case Common::LazyMemoryRegion::WINDOWS_BLOCK_SIZE / sizeof(u32): + return 0x00000001; + default: + return 0; + } +} + +template +static void VerifyContent(const u32* memory, size_t bits, F f) +{ + const size_t end_index = bits / 32; + for (size_t i = 0; i < end_index; ++i, ++memory) + { + const u32 expected = f(i); + ASSERT_EQ(*memory, expected) << "i=" << i; + } + if (bits % 32 != 0) + { + const u32 expected = f(end_index) & ~(0xFFFFFFFF << bits % 32); + ASSERT_EQ(*memory, expected) << "i=" << end_index; + } +} + +TEST(LazyMemoryRegionBitSet, Unwritten) +{ + Common::LazyMemoryRegionBitSet bit_set(BIT_COUNT); + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAllZeroes); +} + +TEST(LazyMemoryRegionBitSet, Set) +{ + Common::LazyMemoryRegionBitSet bit_set(BIT_COUNT); + + // Set single bits in the range 0-1023 + for (size_t i = 0; i < 32; ++i) + bit_set.SetBit(i * 33); + + // Set ranges of bits in bits 1024 and higher + bit_set.SetBits(1024 + 8, 1024 + 16); + bit_set.SetBits(1024 + 32, 1024 + 64); + bit_set.SetBits(1024 + 68, 1024 + 92); + bit_set.SetBits(1024 + 100, 1024 + 128); + bit_set.SetBits(1024 + 128, 1024 + 156); + bit_set.SetBits(1024 + 164, 1024 + 252); + bit_set.SetBits(1024 + 260, 1024 + 384); + bit_set.SetBits(1024 + 384, 1024 + 508); + + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAfterSetting); + + bit_set.Clear(); + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAllZeroes); +} + +TEST(LazyMemoryRegionBitSet, Clear) +{ + Common::LazyMemoryRegionBitSet bit_set(BIT_COUNT); + bit_set.SetBits(0, BIT_COUNT); + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAllOnes); + + // Clear single bits in the range 0-1023 + for (size_t i = 0; i < 32; ++i) + bit_set.ClearBit(i * 33); + + // Clear ranges of bits in bits 1024 and higher + bit_set.ClearBits(1024 + 8, 1024 + 16); + bit_set.ClearBits(1024 + 32, 1024 + 64); + bit_set.ClearBits(1024 + 68, 1024 + 92); + bit_set.ClearBits(1024 + 100, 1024 + 128); + bit_set.ClearBits(1024 + 128, 1024 + 156); + bit_set.ClearBits(1024 + 164, 1024 + 252); + bit_set.ClearBits(1024 + 260, 1024 + 384); + bit_set.ClearBits(1024 + 384, 1024 + 508); + + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAfterClearing); + + bit_set.Clear(); + VerifyContent(bit_set.GetRawMemory(), BIT_COUNT, ExpectedContentAllZeroes); +} + +TEST(LazyMemoryRegionBitSet, WindowsBoundary) +{ + // Unlike on other operating systems, LazyMemoryRegion's Windows implementation allocates one page + // containing only zeroes and maps it to every part of the region by default. If + // LazyMemoryRegionBitSet wants to write to any part of the region, it first needs to call + // LazyMemoryRegion::EnsureMemoryPageWritable. This test is checking that + // LazyMemoryRegion::EnsureMemoryPageWritable is being called with the correct parameters. + + Common::LazyMemoryRegionBitSet bit_set(WINDOWS_BOUNDARY_BIT_COUNT); + + bit_set.SetBits(Common::LazyMemoryRegion::WINDOWS_BLOCK_SIZE * 8 - 1, + Common::LazyMemoryRegion::WINDOWS_BLOCK_SIZE * 8 + 1); + + VerifyContent(bit_set.GetRawMemory(), WINDOWS_BOUNDARY_BIT_COUNT, + ExpectedContentForWindowsBoundary); +} diff --git a/Source/UnitTests/Core/StubJit.h b/Source/UnitTests/Core/StubJit.h index 50a1e6efb5..77fdcf0a03 100644 --- a/Source/UnitTests/Core/StubJit.h +++ b/Source/UnitTests/Core/StubJit.h @@ -12,7 +12,7 @@ public: explicit StubJit(Core::System& system) : JitBase(system) {} // CPUCoreBase methods - void Init() override {} + bool Init() override { return true; } void Shutdown() override {} void ClearCache() override {} void Run() override {}