From 1b7d619914c8d132dbffb8b6a5bb3a1aab686f9d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 30 May 2019 08:52:20 -0400 Subject: memory: Add class to manage and enforce memory freezing --- src/core/CMakeLists.txt | 2 + src/core/core.cpp | 2 + src/core/memory/freezer.cpp | 186 ++++++++++++++++++++++++++++++++++++++++++++ src/core/memory/freezer.h | 58 ++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 src/core/memory/freezer.cpp create mode 100644 src/core/memory/freezer.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4204ace2b..c17528134 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -454,6 +454,8 @@ add_library(core STATIC loader/nsp.h loader/xci.cpp loader/xci.h + memory/freezer.cpp + memory/freezer.h memory.cpp memory.h memory_setup.h diff --git a/src/core/core.cpp b/src/core/core.cpp index ff0721079..94ebe0995 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -28,6 +28,7 @@ #include "core/hle/service/service.h" #include "core/hle/service/sm/sm.h" #include "core/loader/loader.h" +#include "core/memory/freezer.h" #include "core/perf_stats.h" #include "core/settings.h" #include "core/telemetry_session.h" @@ -243,6 +244,7 @@ struct System::Impl { bool is_powered_on = false; std::unique_ptr cheat_engine; + std::unique_ptr memory_freezer; /// Frontend applets Service::AM::Applets::AppletManager applet_manager; diff --git a/src/core/memory/freezer.cpp b/src/core/memory/freezer.cpp new file mode 100644 index 000000000..1d0ccf328 --- /dev/null +++ b/src/core/memory/freezer.cpp @@ -0,0 +1,186 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "core/core.h" +#include "core/core_timing_util.h" +#include "core/memory.h" +#include "core/memory/freezer.h" + +namespace Memory { + +constexpr s64 MEMORY_FREEZER_TICKS = static_cast(Core::Timing::BASE_CLOCK_RATE / 60); + +namespace { + +u64 MemoryReadWidth(u8 width, VAddr addr) { + switch (width) { + case 1: + return Read8(addr); + case 2: + return Read16(addr); + case 4: + return Read32(addr); + case 8: + return Read64(addr); + default: + UNREACHABLE(); + return 0; + } +} + +void MemoryWriteWidth(u8 width, VAddr addr, u64 value) { + switch (width) { + case 1: + Write8(addr, static_cast(value)); + break; + case 2: + Write16(addr, static_cast(value)); + break; + case 4: + Write32(addr, static_cast(value)); + break; + case 8: + Write64(addr, value); + break; + default: + UNREACHABLE(); + } +} + +} // Anonymous namespace + +Freezer::Freezer(Core::Timing::CoreTiming& core_timing) : core_timing(core_timing) { + event = core_timing.RegisterEvent( + "MemoryFreezer::FrameCallback", + [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); }); + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); +} + +Freezer::~Freezer() { + core_timing.UnscheduleEvent(event, 0); +} + +void Freezer::SetActive(bool active) { + if (!this->active.exchange(active)) { + FillEntryReads(); + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); + LOG_DEBUG(Common_Memory, "Memory freezer activated!"); + } else { + LOG_DEBUG(Common_Memory, "Memory freezer deactivated!"); + } +} + +bool Freezer::IsActive() const { + return active.load(); +} + +void Freezer::Clear() { + std::lock_guard lock(entries_mutex); + + LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); + + entries.clear(); +} + +u64 Freezer::Freeze(VAddr address, u8 width) { + std::lock_guard lock(entries_mutex); + + const auto current_value = MemoryReadWidth(width, address); + entries.push_back({address, width, current_value}); + + LOG_DEBUG(Common_Memory, + "Freezing memory for address={:016X}, width={:02X}, current_value={:016X}", address, + width, current_value); + + return current_value; +} + +void Freezer::Unfreeze(VAddr address) { + std::lock_guard lock(entries_mutex); + + LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); + + entries.erase( + std::remove_if(entries.begin(), entries.end(), + [&address](const Entry& entry) { return entry.address == address; }), + entries.end()); +} + +bool Freezer::IsFrozen(VAddr address) { + std::lock_guard lock(entries_mutex); + + return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }) != entries.end(); +} + +void Freezer::SetFrozenValue(VAddr address, u64 value) { + std::lock_guard lock(entries_mutex); + + const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }); + + if (iter == entries.end()) { + LOG_ERROR(Common_Memory, + "Tried to set freeze value for address={:016X} that is not frozen!", address); + return; + } + + LOG_DEBUG(Common_Memory, + "Manually overridden freeze value for address={:016X}, width={:02X} to value={:016X}", + iter->address, iter->width, value); + iter->value = value; +} + +std::optional Freezer::GetEntry(VAddr address) { + std::lock_guard lock(entries_mutex); + + const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }); + + if (iter == entries.end()) { + return std::nullopt; + } + + return *iter; +} + +std::vector Freezer::GetEntries() { + std::lock_guard lock(entries_mutex); + + return entries; +} + +void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { + if (!active.load()) { + LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); + return; + } + + std::lock_guard lock(entries_mutex); + + for (const auto& entry : entries) { + LOG_DEBUG(Common_Memory, + "Enforcing memory freeze at address={:016X}, value={:016X}, width={:02X}", + entry.address, entry.value, entry.width); + MemoryWriteWidth(entry.width, entry.address, entry.value); + } + + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS - cycles_late, event); +} + +void Freezer::FillEntryReads() { + std::lock_guard lock(entries_mutex); + + LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); + + for (auto& entry : entries) { + entry.value = MemoryReadWidth(entry.width, entry.address); + } +} + +} // namespace Memory diff --git a/src/core/memory/freezer.h b/src/core/memory/freezer.h new file mode 100644 index 000000000..3e271793e --- /dev/null +++ b/src/core/memory/freezer.h @@ -0,0 +1,58 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/common_types.h" +#include "core/core_timing.h" + +namespace Core { +class System; +} // namespace Core + +namespace Memory { + +// A class that will effectively freeze memory values. +class Freezer { +public: + struct Entry { + VAddr address; + u8 width; + u64 value; + }; + + Freezer(Core::Timing::CoreTiming& core_timing); + ~Freezer(); + + void SetActive(bool active); + bool IsActive() const; + + void Clear(); + + u64 Freeze(VAddr address, u8 width); + void Unfreeze(VAddr address); + + bool IsFrozen(VAddr address); + void SetFrozenValue(VAddr address, u64 value); + + std::optional GetEntry(VAddr address); + + std::vector GetEntries(); + +private: + void FrameCallback(u64 userdata, s64 cycles_late); + void FillEntryReads(); + + std::atomic_bool active{false}; + + std::recursive_mutex entries_mutex; + std::vector entries; + + Core::Timing::EventType* event; + Core::Timing::CoreTiming& core_timing; +}; + +} // namespace Memory -- cgit v1.2.3 From c9983ad9a71c9462319d27c3791e13fae9d73e46 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 30 May 2019 16:57:23 -0400 Subject: freezer: Add documentation for methods --- src/core/memory/freezer.cpp | 40 +++++++++++++++++++++------------------- src/core/memory/freezer.h | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/core/memory/freezer.cpp b/src/core/memory/freezer.cpp index 1d0ccf328..6b20e8388 100644 --- a/src/core/memory/freezer.cpp +++ b/src/core/memory/freezer.cpp @@ -3,18 +3,20 @@ // Refer to the license.txt file included. #include "common/assert.h" +#include "common/logging/log.h" #include "core/core.h" +#include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/memory.h" #include "core/memory/freezer.h" namespace Memory { -constexpr s64 MEMORY_FREEZER_TICKS = static_cast(Core::Timing::BASE_CLOCK_RATE / 60); - namespace { -u64 MemoryReadWidth(u8 width, VAddr addr) { +constexpr s64 MEMORY_FREEZER_TICKS = static_cast(Core::Timing::BASE_CLOCK_RATE / 60); + +u64 MemoryReadWidth(u32 width, VAddr addr) { switch (width) { case 1: return Read8(addr); @@ -30,7 +32,7 @@ u64 MemoryReadWidth(u8 width, VAddr addr) { } } -void MemoryWriteWidth(u8 width, VAddr addr, u64 value) { +void MemoryWriteWidth(u32 width, VAddr addr, u64 value) { switch (width) { case 1: Write8(addr, static_cast(value)); @@ -73,19 +75,19 @@ void Freezer::SetActive(bool active) { } bool Freezer::IsActive() const { - return active.load(); + return active.load(std::memory_order_relaxed); } void Freezer::Clear() { - std::lock_guard lock(entries_mutex); + std::lock_guard lock{entries_mutex}; LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); entries.clear(); } -u64 Freezer::Freeze(VAddr address, u8 width) { - std::lock_guard lock(entries_mutex); +u64 Freezer::Freeze(VAddr address, u32 width) { + std::lock_guard lock{entries_mutex}; const auto current_value = MemoryReadWidth(width, address); entries.push_back({address, width, current_value}); @@ -98,7 +100,7 @@ u64 Freezer::Freeze(VAddr address, u8 width) { } void Freezer::Unfreeze(VAddr address) { - std::lock_guard lock(entries_mutex); + std::lock_guard lock{entries_mutex}; LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); @@ -108,8 +110,8 @@ void Freezer::Unfreeze(VAddr address) { entries.end()); } -bool Freezer::IsFrozen(VAddr address) { - std::lock_guard lock(entries_mutex); +bool Freezer::IsFrozen(VAddr address) const { + std::lock_guard lock{entries_mutex}; return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { return entry.address == address; @@ -117,7 +119,7 @@ bool Freezer::IsFrozen(VAddr address) { } void Freezer::SetFrozenValue(VAddr address, u64 value) { - std::lock_guard lock(entries_mutex); + std::lock_guard lock{entries_mutex}; const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { return entry.address == address; @@ -135,8 +137,8 @@ void Freezer::SetFrozenValue(VAddr address, u64 value) { iter->value = value; } -std::optional Freezer::GetEntry(VAddr address) { - std::lock_guard lock(entries_mutex); +std::optional Freezer::GetEntry(VAddr address) const { + std::lock_guard lock{entries_mutex}; const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { return entry.address == address; @@ -149,19 +151,19 @@ std::optional Freezer::GetEntry(VAddr address) { return *iter; } -std::vector Freezer::GetEntries() { - std::lock_guard lock(entries_mutex); +std::vector Freezer::GetEntries() const { + std::lock_guard lock{entries_mutex}; return entries; } void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { - if (!active.load()) { + if (!IsActive()) { LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); return; } - std::lock_guard lock(entries_mutex); + std::lock_guard lock{entries_mutex}; for (const auto& entry : entries) { LOG_DEBUG(Common_Memory, @@ -174,7 +176,7 @@ void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { } void Freezer::FillEntryReads() { - std::lock_guard lock(entries_mutex); + std::lock_guard lock{entries_mutex}; LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); diff --git a/src/core/memory/freezer.h b/src/core/memory/freezer.h index 3e271793e..b0c610039 100644 --- a/src/core/memory/freezer.h +++ b/src/core/memory/freezer.h @@ -4,14 +4,16 @@ #pragma once +#include +#include #include #include #include "common/common_types.h" -#include "core/core_timing.h" -namespace Core { -class System; -} // namespace Core +namespace Core::Timing { +class CoreTiming; +struct EventType; +} // namespace Core::Timing namespace Memory { @@ -20,27 +22,42 @@ class Freezer { public: struct Entry { VAddr address; - u8 width; + u32 width; u64 value; }; - Freezer(Core::Timing::CoreTiming& core_timing); + explicit Freezer(Core::Timing::CoreTiming& core_timing); ~Freezer(); + // Enables or disables the entire memory freezer. void SetActive(bool active); + + // Returns whether or not the freezer is active. bool IsActive() const; + // Removes all entries from the freezer. void Clear(); - u64 Freeze(VAddr address, u8 width); + // Freezes a value to its current memory address. The value the memory is kept at will be the + // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes). + u64 Freeze(VAddr address, u32 width); + + // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op. void Unfreeze(VAddr address); - bool IsFrozen(VAddr address); + // Returns whether or not the address is frozen. + bool IsFrozen(VAddr address) const; + + // Sets the value that address should be frozen to. This doesn't change the width set by using + // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op. void SetFrozenValue(VAddr address, u64 value); - std::optional GetEntry(VAddr address); + // Returns the entry corresponding to the address if the address is frozen, otherwise + // std::nullopt. + std::optional GetEntry(VAddr address) const; - std::vector GetEntries(); + // Returns all the entries in the freezer, an empty vector means nothing is frozen. + std::vector GetEntries() const; private: void FrameCallback(u64 userdata, s64 cycles_late); @@ -48,7 +65,7 @@ private: std::atomic_bool active{false}; - std::recursive_mutex entries_mutex; + mutable std::mutex entries_mutex; std::vector entries; Core::Timing::EventType* event; -- cgit v1.2.3 From ed82fa3a91fc84f7f906b898d8f71e15fb42c16e Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 7 Jun 2019 11:11:11 -0400 Subject: core: Move Freezer class to tools namespace --- src/core/CMakeLists.txt | 4 +- src/core/core.cpp | 4 +- src/core/memory/freezer.cpp | 188 -------------------------------------------- src/core/memory/freezer.h | 75 ------------------ src/core/tools/freezer.cpp | 188 ++++++++++++++++++++++++++++++++++++++++++++ src/core/tools/freezer.h | 75 ++++++++++++++++++ 6 files changed, 267 insertions(+), 267 deletions(-) delete mode 100644 src/core/memory/freezer.cpp delete mode 100644 src/core/memory/freezer.h create mode 100644 src/core/tools/freezer.cpp create mode 100644 src/core/tools/freezer.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c17528134..59b64407a 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -454,8 +454,6 @@ add_library(core STATIC loader/nsp.h loader/xci.cpp loader/xci.h - memory/freezer.cpp - memory/freezer.h memory.cpp memory.h memory_setup.h @@ -465,6 +463,8 @@ add_library(core STATIC settings.h telemetry_session.cpp telemetry_session.h + tools/freezer.cpp + tools/freezer.h ) create_target_directory_groups(core) diff --git a/src/core/core.cpp b/src/core/core.cpp index 94ebe0995..48d953442 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -28,10 +28,10 @@ #include "core/hle/service/service.h" #include "core/hle/service/sm/sm.h" #include "core/loader/loader.h" -#include "core/memory/freezer.h" #include "core/perf_stats.h" #include "core/settings.h" #include "core/telemetry_session.h" +#include "core/tools/freezer.h" #include "file_sys/cheat_engine.h" #include "video_core/debug_utils/debug_utils.h" #include "video_core/renderer_base.h" @@ -244,7 +244,7 @@ struct System::Impl { bool is_powered_on = false; std::unique_ptr cheat_engine; - std::unique_ptr memory_freezer; + std::unique_ptr memory_freezer; /// Frontend applets Service::AM::Applets::AppletManager applet_manager; diff --git a/src/core/memory/freezer.cpp b/src/core/memory/freezer.cpp deleted file mode 100644 index 6b20e8388..000000000 --- a/src/core/memory/freezer.cpp +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright 2019 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/assert.h" -#include "common/logging/log.h" -#include "core/core.h" -#include "core/core_timing.h" -#include "core/core_timing_util.h" -#include "core/memory.h" -#include "core/memory/freezer.h" - -namespace Memory { - -namespace { - -constexpr s64 MEMORY_FREEZER_TICKS = static_cast(Core::Timing::BASE_CLOCK_RATE / 60); - -u64 MemoryReadWidth(u32 width, VAddr addr) { - switch (width) { - case 1: - return Read8(addr); - case 2: - return Read16(addr); - case 4: - return Read32(addr); - case 8: - return Read64(addr); - default: - UNREACHABLE(); - return 0; - } -} - -void MemoryWriteWidth(u32 width, VAddr addr, u64 value) { - switch (width) { - case 1: - Write8(addr, static_cast(value)); - break; - case 2: - Write16(addr, static_cast(value)); - break; - case 4: - Write32(addr, static_cast(value)); - break; - case 8: - Write64(addr, value); - break; - default: - UNREACHABLE(); - } -} - -} // Anonymous namespace - -Freezer::Freezer(Core::Timing::CoreTiming& core_timing) : core_timing(core_timing) { - event = core_timing.RegisterEvent( - "MemoryFreezer::FrameCallback", - [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); }); - core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); -} - -Freezer::~Freezer() { - core_timing.UnscheduleEvent(event, 0); -} - -void Freezer::SetActive(bool active) { - if (!this->active.exchange(active)) { - FillEntryReads(); - core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); - LOG_DEBUG(Common_Memory, "Memory freezer activated!"); - } else { - LOG_DEBUG(Common_Memory, "Memory freezer deactivated!"); - } -} - -bool Freezer::IsActive() const { - return active.load(std::memory_order_relaxed); -} - -void Freezer::Clear() { - std::lock_guard lock{entries_mutex}; - - LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); - - entries.clear(); -} - -u64 Freezer::Freeze(VAddr address, u32 width) { - std::lock_guard lock{entries_mutex}; - - const auto current_value = MemoryReadWidth(width, address); - entries.push_back({address, width, current_value}); - - LOG_DEBUG(Common_Memory, - "Freezing memory for address={:016X}, width={:02X}, current_value={:016X}", address, - width, current_value); - - return current_value; -} - -void Freezer::Unfreeze(VAddr address) { - std::lock_guard lock{entries_mutex}; - - LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); - - entries.erase( - std::remove_if(entries.begin(), entries.end(), - [&address](const Entry& entry) { return entry.address == address; }), - entries.end()); -} - -bool Freezer::IsFrozen(VAddr address) const { - std::lock_guard lock{entries_mutex}; - - return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { - return entry.address == address; - }) != entries.end(); -} - -void Freezer::SetFrozenValue(VAddr address, u64 value) { - std::lock_guard lock{entries_mutex}; - - const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { - return entry.address == address; - }); - - if (iter == entries.end()) { - LOG_ERROR(Common_Memory, - "Tried to set freeze value for address={:016X} that is not frozen!", address); - return; - } - - LOG_DEBUG(Common_Memory, - "Manually overridden freeze value for address={:016X}, width={:02X} to value={:016X}", - iter->address, iter->width, value); - iter->value = value; -} - -std::optional Freezer::GetEntry(VAddr address) const { - std::lock_guard lock{entries_mutex}; - - const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { - return entry.address == address; - }); - - if (iter == entries.end()) { - return std::nullopt; - } - - return *iter; -} - -std::vector Freezer::GetEntries() const { - std::lock_guard lock{entries_mutex}; - - return entries; -} - -void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { - if (!IsActive()) { - LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); - return; - } - - std::lock_guard lock{entries_mutex}; - - for (const auto& entry : entries) { - LOG_DEBUG(Common_Memory, - "Enforcing memory freeze at address={:016X}, value={:016X}, width={:02X}", - entry.address, entry.value, entry.width); - MemoryWriteWidth(entry.width, entry.address, entry.value); - } - - core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS - cycles_late, event); -} - -void Freezer::FillEntryReads() { - std::lock_guard lock{entries_mutex}; - - LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); - - for (auto& entry : entries) { - entry.value = MemoryReadWidth(entry.width, entry.address); - } -} - -} // namespace Memory diff --git a/src/core/memory/freezer.h b/src/core/memory/freezer.h deleted file mode 100644 index b0c610039..000000000 --- a/src/core/memory/freezer.h +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2019 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include -#include "common/common_types.h" - -namespace Core::Timing { -class CoreTiming; -struct EventType; -} // namespace Core::Timing - -namespace Memory { - -// A class that will effectively freeze memory values. -class Freezer { -public: - struct Entry { - VAddr address; - u32 width; - u64 value; - }; - - explicit Freezer(Core::Timing::CoreTiming& core_timing); - ~Freezer(); - - // Enables or disables the entire memory freezer. - void SetActive(bool active); - - // Returns whether or not the freezer is active. - bool IsActive() const; - - // Removes all entries from the freezer. - void Clear(); - - // Freezes a value to its current memory address. The value the memory is kept at will be the - // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes). - u64 Freeze(VAddr address, u32 width); - - // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op. - void Unfreeze(VAddr address); - - // Returns whether or not the address is frozen. - bool IsFrozen(VAddr address) const; - - // Sets the value that address should be frozen to. This doesn't change the width set by using - // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op. - void SetFrozenValue(VAddr address, u64 value); - - // Returns the entry corresponding to the address if the address is frozen, otherwise - // std::nullopt. - std::optional GetEntry(VAddr address) const; - - // Returns all the entries in the freezer, an empty vector means nothing is frozen. - std::vector GetEntries() const; - -private: - void FrameCallback(u64 userdata, s64 cycles_late); - void FillEntryReads(); - - std::atomic_bool active{false}; - - mutable std::mutex entries_mutex; - std::vector entries; - - Core::Timing::EventType* event; - Core::Timing::CoreTiming& core_timing; -}; - -} // namespace Memory diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp new file mode 100644 index 000000000..17f050068 --- /dev/null +++ b/src/core/tools/freezer.cpp @@ -0,0 +1,188 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "common/logging/log.h" +#include "core/core.h" +#include "core/core_timing.h" +#include "core/core_timing_util.h" +#include "core/memory.h" +#include "core/tools/freezer.h" + +namespace Tools { + +namespace { + +constexpr s64 MEMORY_FREEZER_TICKS = static_cast(Core::Timing::BASE_CLOCK_RATE / 60); + +u64 MemoryReadWidth(u32 width, VAddr addr) { + switch (width) { + case 1: + return Memory::Read8(addr); + case 2: + return Memory::Read16(addr); + case 4: + return Memory::Read32(addr); + case 8: + return Memory::Read64(addr); + default: + UNREACHABLE(); + return 0; + } +} + +void MemoryWriteWidth(u32 width, VAddr addr, u64 value) { + switch (width) { + case 1: + Memory::Write8(addr, static_cast(value)); + break; + case 2: + Memory::Write16(addr, static_cast(value)); + break; + case 4: + Memory::Write32(addr, static_cast(value)); + break; + case 8: + Memory::Write64(addr, value); + break; + default: + UNREACHABLE(); + } +} + +} // Anonymous namespace + +Freezer::Freezer(Core::Timing::CoreTiming& core_timing) : core_timing(core_timing) { + event = core_timing.RegisterEvent( + "MemoryFreezer::FrameCallback", + [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); }); + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); +} + +Freezer::~Freezer() { + core_timing.UnscheduleEvent(event, 0); +} + +void Freezer::SetActive(bool active) { + if (!this->active.exchange(active)) { + FillEntryReads(); + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); + LOG_DEBUG(Common_Memory, "Memory freezer activated!"); + } else { + LOG_DEBUG(Common_Memory, "Memory freezer deactivated!"); + } +} + +bool Freezer::IsActive() const { + return active.load(std::memory_order_relaxed); +} + +void Freezer::Clear() { + std::lock_guard lock{entries_mutex}; + + LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); + + entries.clear(); +} + +u64 Freezer::Freeze(VAddr address, u32 width) { + std::lock_guard lock{entries_mutex}; + + const auto current_value = MemoryReadWidth(width, address); + entries.push_back({address, width, current_value}); + + LOG_DEBUG(Common_Memory, + "Freezing memory for address={:016X}, width={:02X}, current_value={:016X}", address, + width, current_value); + + return current_value; +} + +void Freezer::Unfreeze(VAddr address) { + std::lock_guard lock{entries_mutex}; + + LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); + + entries.erase( + std::remove_if(entries.begin(), entries.end(), + [&address](const Entry& entry) { return entry.address == address; }), + entries.end()); +} + +bool Freezer::IsFrozen(VAddr address) const { + std::lock_guard lock{entries_mutex}; + + return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }) != entries.end(); +} + +void Freezer::SetFrozenValue(VAddr address, u64 value) { + std::lock_guard lock{entries_mutex}; + + const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }); + + if (iter == entries.end()) { + LOG_ERROR(Common_Memory, + "Tried to set freeze value for address={:016X} that is not frozen!", address); + return; + } + + LOG_DEBUG(Common_Memory, + "Manually overridden freeze value for address={:016X}, width={:02X} to value={:016X}", + iter->address, iter->width, value); + iter->value = value; +} + +std::optional Freezer::GetEntry(VAddr address) const { + std::lock_guard lock{entries_mutex}; + + const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { + return entry.address == address; + }); + + if (iter == entries.end()) { + return std::nullopt; + } + + return *iter; +} + +std::vector Freezer::GetEntries() const { + std::lock_guard lock{entries_mutex}; + + return entries; +} + +void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { + if (!IsActive()) { + LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); + return; + } + + std::lock_guard lock{entries_mutex}; + + for (const auto& entry : entries) { + LOG_DEBUG(Common_Memory, + "Enforcing memory freeze at address={:016X}, value={:016X}, width={:02X}", + entry.address, entry.value, entry.width); + MemoryWriteWidth(entry.width, entry.address, entry.value); + } + + core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS - cycles_late, event); +} + +void Freezer::FillEntryReads() { + std::lock_guard lock{entries_mutex}; + + LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); + + for (auto& entry : entries) { + entry.value = MemoryReadWidth(entry.width, entry.address); + } +} + +} // namespace Tools diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h new file mode 100644 index 000000000..5edd049c1 --- /dev/null +++ b/src/core/tools/freezer.h @@ -0,0 +1,75 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include "common/common_types.h" + +namespace Core::Timing { +class CoreTiming; +struct EventType; +} // namespace Core::Timing + +namespace Tools { + +// A class that will effectively freeze memory values. +class Freezer { +public: + struct Entry { + VAddr address; + u32 width; + u64 value; + }; + + explicit Freezer(Core::Timing::CoreTiming& core_timing); + ~Freezer(); + + // Enables or disables the entire memory freezer. + void SetActive(bool active); + + // Returns whether or not the freezer is active. + bool IsActive() const; + + // Removes all entries from the freezer. + void Clear(); + + // Freezes a value to its current memory address. The value the memory is kept at will be the + // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes). + u64 Freeze(VAddr address, u32 width); + + // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op. + void Unfreeze(VAddr address); + + // Returns whether or not the address is frozen. + bool IsFrozen(VAddr address) const; + + // Sets the value that address should be frozen to. This doesn't change the width set by using + // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op. + void SetFrozenValue(VAddr address, u64 value); + + // Returns the entry corresponding to the address if the address is frozen, otherwise + // std::nullopt. + std::optional GetEntry(VAddr address) const; + + // Returns all the entries in the freezer, an empty vector means nothing is frozen. + std::vector GetEntries() const; + +private: + void FrameCallback(u64 userdata, s64 cycles_late); + void FillEntryReads(); + + std::atomic_bool active{false}; + + mutable std::mutex entries_mutex; + std::vector entries; + + Core::Timing::EventType* event; + Core::Timing::CoreTiming& core_timing; +}; + +} // namespace Tools -- cgit v1.2.3 From 4111971cbdae3a4700156a802f8e0c9309a9cf1e Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 10 Jun 2019 14:11:06 -0400 Subject: freezer: Update documentation --- src/core/tools/freezer.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h index 5edd049c1..b58de5472 100644 --- a/src/core/tools/freezer.h +++ b/src/core/tools/freezer.h @@ -17,7 +17,14 @@ struct EventType; namespace Tools { -// A class that will effectively freeze memory values. +/** + * This class allows the user to prevent an application from writing new values to certain memory + * locations. This has a variety of uses when attempting to reverse a game. + * + * One example could be a cheat to prevent Mario from taking damage in SMO. One could freeze the + * memory address that the game uses to store Mario's health so when he takes damage (and the game + * tries to write the new health value to memory), the value won't change. + */ class Freezer { public: struct Entry { -- cgit v1.2.3