diff options
| author | 2019-06-28 14:03:38 -0400 | |
|---|---|---|
| committer | 2019-06-28 14:03:38 -0400 | |
| commit | bb4a1e059c269c95ec9f85f5c2149c7cd1c96f02 (patch) | |
| tree | d5feb91cf0b4020881e68f9bc35db43fc5c63da5 | |
| parent | Merge pull request #2548 from DarkLordZach/applet-shopn (diff) | |
| parent | freezer: Update documentation (diff) | |
| download | yuzu-bb4a1e059c269c95ec9f85f5c2149c7cd1c96f02.tar.gz yuzu-bb4a1e059c269c95ec9f85f5c2149c7cd1c96f02.tar.xz yuzu-bb4a1e059c269c95ec9f85f5c2149c7cd1c96f02.zip | |
Merge pull request #2533 from DarkLordZach/memory-frozen
memory: Add class to manage and enforce memory freezing
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/core.cpp | 2 | ||||
| -rw-r--r-- | src/core/tools/freezer.cpp | 188 | ||||
| -rw-r--r-- | src/core/tools/freezer.h | 82 |
4 files changed, 274 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d65659b44..cb77b99ee 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -476,6 +476,8 @@ add_library(core STATIC | |||
| 476 | settings.h | 476 | settings.h |
| 477 | telemetry_session.cpp | 477 | telemetry_session.cpp |
| 478 | telemetry_session.h | 478 | telemetry_session.h |
| 479 | tools/freezer.cpp | ||
| 480 | tools/freezer.h | ||
| 479 | ) | 481 | ) |
| 480 | 482 | ||
| 481 | create_target_directory_groups(core) | 483 | create_target_directory_groups(core) |
diff --git a/src/core/core.cpp b/src/core/core.cpp index df26eb109..262411db8 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp | |||
| @@ -33,6 +33,7 @@ | |||
| 33 | #include "core/reporter.h" | 33 | #include "core/reporter.h" |
| 34 | #include "core/settings.h" | 34 | #include "core/settings.h" |
| 35 | #include "core/telemetry_session.h" | 35 | #include "core/telemetry_session.h" |
| 36 | #include "core/tools/freezer.h" | ||
| 36 | #include "file_sys/cheat_engine.h" | 37 | #include "file_sys/cheat_engine.h" |
| 37 | #include "file_sys/patch_manager.h" | 38 | #include "file_sys/patch_manager.h" |
| 38 | #include "video_core/debug_utils/debug_utils.h" | 39 | #include "video_core/debug_utils/debug_utils.h" |
| @@ -300,6 +301,7 @@ struct System::Impl { | |||
| 300 | bool is_powered_on = false; | 301 | bool is_powered_on = false; |
| 301 | 302 | ||
| 302 | std::unique_ptr<FileSys::CheatEngine> cheat_engine; | 303 | std::unique_ptr<FileSys::CheatEngine> cheat_engine; |
| 304 | std::unique_ptr<Tools::Freezer> memory_freezer; | ||
| 303 | 305 | ||
| 304 | /// Frontend applets | 306 | /// Frontend applets |
| 305 | Service::AM::Applets::AppletManager applet_manager; | 307 | Service::AM::Applets::AppletManager applet_manager; |
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 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "core/core.h" | ||
| 8 | #include "core/core_timing.h" | ||
| 9 | #include "core/core_timing_util.h" | ||
| 10 | #include "core/memory.h" | ||
| 11 | #include "core/tools/freezer.h" | ||
| 12 | |||
| 13 | namespace Tools { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | constexpr s64 MEMORY_FREEZER_TICKS = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60); | ||
| 18 | |||
| 19 | u64 MemoryReadWidth(u32 width, VAddr addr) { | ||
| 20 | switch (width) { | ||
| 21 | case 1: | ||
| 22 | return Memory::Read8(addr); | ||
| 23 | case 2: | ||
| 24 | return Memory::Read16(addr); | ||
| 25 | case 4: | ||
| 26 | return Memory::Read32(addr); | ||
| 27 | case 8: | ||
| 28 | return Memory::Read64(addr); | ||
| 29 | default: | ||
| 30 | UNREACHABLE(); | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | void MemoryWriteWidth(u32 width, VAddr addr, u64 value) { | ||
| 36 | switch (width) { | ||
| 37 | case 1: | ||
| 38 | Memory::Write8(addr, static_cast<u8>(value)); | ||
| 39 | break; | ||
| 40 | case 2: | ||
| 41 | Memory::Write16(addr, static_cast<u16>(value)); | ||
| 42 | break; | ||
| 43 | case 4: | ||
| 44 | Memory::Write32(addr, static_cast<u32>(value)); | ||
| 45 | break; | ||
| 46 | case 8: | ||
| 47 | Memory::Write64(addr, value); | ||
| 48 | break; | ||
| 49 | default: | ||
| 50 | UNREACHABLE(); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | } // Anonymous namespace | ||
| 55 | |||
| 56 | Freezer::Freezer(Core::Timing::CoreTiming& core_timing) : core_timing(core_timing) { | ||
| 57 | event = core_timing.RegisterEvent( | ||
| 58 | "MemoryFreezer::FrameCallback", | ||
| 59 | [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); }); | ||
| 60 | core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); | ||
| 61 | } | ||
| 62 | |||
| 63 | Freezer::~Freezer() { | ||
| 64 | core_timing.UnscheduleEvent(event, 0); | ||
| 65 | } | ||
| 66 | |||
| 67 | void Freezer::SetActive(bool active) { | ||
| 68 | if (!this->active.exchange(active)) { | ||
| 69 | FillEntryReads(); | ||
| 70 | core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event); | ||
| 71 | LOG_DEBUG(Common_Memory, "Memory freezer activated!"); | ||
| 72 | } else { | ||
| 73 | LOG_DEBUG(Common_Memory, "Memory freezer deactivated!"); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | bool Freezer::IsActive() const { | ||
| 78 | return active.load(std::memory_order_relaxed); | ||
| 79 | } | ||
| 80 | |||
| 81 | void Freezer::Clear() { | ||
| 82 | std::lock_guard lock{entries_mutex}; | ||
| 83 | |||
| 84 | LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); | ||
| 85 | |||
| 86 | entries.clear(); | ||
| 87 | } | ||
| 88 | |||
| 89 | u64 Freezer::Freeze(VAddr address, u32 width) { | ||
| 90 | std::lock_guard lock{entries_mutex}; | ||
| 91 | |||
| 92 | const auto current_value = MemoryReadWidth(width, address); | ||
| 93 | entries.push_back({address, width, current_value}); | ||
| 94 | |||
| 95 | LOG_DEBUG(Common_Memory, | ||
| 96 | "Freezing memory for address={:016X}, width={:02X}, current_value={:016X}", address, | ||
| 97 | width, current_value); | ||
| 98 | |||
| 99 | return current_value; | ||
| 100 | } | ||
| 101 | |||
| 102 | void Freezer::Unfreeze(VAddr address) { | ||
| 103 | std::lock_guard lock{entries_mutex}; | ||
| 104 | |||
| 105 | LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); | ||
| 106 | |||
| 107 | entries.erase( | ||
| 108 | std::remove_if(entries.begin(), entries.end(), | ||
| 109 | [&address](const Entry& entry) { return entry.address == address; }), | ||
| 110 | entries.end()); | ||
| 111 | } | ||
| 112 | |||
| 113 | bool Freezer::IsFrozen(VAddr address) const { | ||
| 114 | std::lock_guard lock{entries_mutex}; | ||
| 115 | |||
| 116 | return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { | ||
| 117 | return entry.address == address; | ||
| 118 | }) != entries.end(); | ||
| 119 | } | ||
| 120 | |||
| 121 | void Freezer::SetFrozenValue(VAddr address, u64 value) { | ||
| 122 | std::lock_guard lock{entries_mutex}; | ||
| 123 | |||
| 124 | const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { | ||
| 125 | return entry.address == address; | ||
| 126 | }); | ||
| 127 | |||
| 128 | if (iter == entries.end()) { | ||
| 129 | LOG_ERROR(Common_Memory, | ||
| 130 | "Tried to set freeze value for address={:016X} that is not frozen!", address); | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | LOG_DEBUG(Common_Memory, | ||
| 135 | "Manually overridden freeze value for address={:016X}, width={:02X} to value={:016X}", | ||
| 136 | iter->address, iter->width, value); | ||
| 137 | iter->value = value; | ||
| 138 | } | ||
| 139 | |||
| 140 | std::optional<Freezer::Entry> Freezer::GetEntry(VAddr address) const { | ||
| 141 | std::lock_guard lock{entries_mutex}; | ||
| 142 | |||
| 143 | const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { | ||
| 144 | return entry.address == address; | ||
| 145 | }); | ||
| 146 | |||
| 147 | if (iter == entries.end()) { | ||
| 148 | return std::nullopt; | ||
| 149 | } | ||
| 150 | |||
| 151 | return *iter; | ||
| 152 | } | ||
| 153 | |||
| 154 | std::vector<Freezer::Entry> Freezer::GetEntries() const { | ||
| 155 | std::lock_guard lock{entries_mutex}; | ||
| 156 | |||
| 157 | return entries; | ||
| 158 | } | ||
| 159 | |||
| 160 | void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { | ||
| 161 | if (!IsActive()) { | ||
| 162 | LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | std::lock_guard lock{entries_mutex}; | ||
| 167 | |||
| 168 | for (const auto& entry : entries) { | ||
| 169 | LOG_DEBUG(Common_Memory, | ||
| 170 | "Enforcing memory freeze at address={:016X}, value={:016X}, width={:02X}", | ||
| 171 | entry.address, entry.value, entry.width); | ||
| 172 | MemoryWriteWidth(entry.width, entry.address, entry.value); | ||
| 173 | } | ||
| 174 | |||
| 175 | core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS - cycles_late, event); | ||
| 176 | } | ||
| 177 | |||
| 178 | void Freezer::FillEntryReads() { | ||
| 179 | std::lock_guard lock{entries_mutex}; | ||
| 180 | |||
| 181 | LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); | ||
| 182 | |||
| 183 | for (auto& entry : entries) { | ||
| 184 | entry.value = MemoryReadWidth(entry.width, entry.address); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | } // namespace Tools | ||
diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h new file mode 100644 index 000000000..b58de5472 --- /dev/null +++ b/src/core/tools/freezer.h | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <mutex> | ||
| 9 | #include <optional> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/common_types.h" | ||
| 12 | |||
| 13 | namespace Core::Timing { | ||
| 14 | class CoreTiming; | ||
| 15 | struct EventType; | ||
| 16 | } // namespace Core::Timing | ||
| 17 | |||
| 18 | namespace Tools { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * This class allows the user to prevent an application from writing new values to certain memory | ||
| 22 | * locations. This has a variety of uses when attempting to reverse a game. | ||
| 23 | * | ||
| 24 | * One example could be a cheat to prevent Mario from taking damage in SMO. One could freeze the | ||
| 25 | * memory address that the game uses to store Mario's health so when he takes damage (and the game | ||
| 26 | * tries to write the new health value to memory), the value won't change. | ||
| 27 | */ | ||
| 28 | class Freezer { | ||
| 29 | public: | ||
| 30 | struct Entry { | ||
| 31 | VAddr address; | ||
| 32 | u32 width; | ||
| 33 | u64 value; | ||
| 34 | }; | ||
| 35 | |||
| 36 | explicit Freezer(Core::Timing::CoreTiming& core_timing); | ||
| 37 | ~Freezer(); | ||
| 38 | |||
| 39 | // Enables or disables the entire memory freezer. | ||
| 40 | void SetActive(bool active); | ||
| 41 | |||
| 42 | // Returns whether or not the freezer is active. | ||
| 43 | bool IsActive() const; | ||
| 44 | |||
| 45 | // Removes all entries from the freezer. | ||
| 46 | void Clear(); | ||
| 47 | |||
| 48 | // Freezes a value to its current memory address. The value the memory is kept at will be the | ||
| 49 | // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes). | ||
| 50 | u64 Freeze(VAddr address, u32 width); | ||
| 51 | |||
| 52 | // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op. | ||
| 53 | void Unfreeze(VAddr address); | ||
| 54 | |||
| 55 | // Returns whether or not the address is frozen. | ||
| 56 | bool IsFrozen(VAddr address) const; | ||
| 57 | |||
| 58 | // Sets the value that address should be frozen to. This doesn't change the width set by using | ||
| 59 | // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op. | ||
| 60 | void SetFrozenValue(VAddr address, u64 value); | ||
| 61 | |||
| 62 | // Returns the entry corresponding to the address if the address is frozen, otherwise | ||
| 63 | // std::nullopt. | ||
| 64 | std::optional<Entry> GetEntry(VAddr address) const; | ||
| 65 | |||
| 66 | // Returns all the entries in the freezer, an empty vector means nothing is frozen. | ||
| 67 | std::vector<Entry> GetEntries() const; | ||
| 68 | |||
| 69 | private: | ||
| 70 | void FrameCallback(u64 userdata, s64 cycles_late); | ||
| 71 | void FillEntryReads(); | ||
| 72 | |||
| 73 | std::atomic_bool active{false}; | ||
| 74 | |||
| 75 | mutable std::mutex entries_mutex; | ||
| 76 | std::vector<Entry> entries; | ||
| 77 | |||
| 78 | Core::Timing::EventType* event; | ||
| 79 | Core::Timing::CoreTiming& core_timing; | ||
| 80 | }; | ||
| 81 | |||
| 82 | } // namespace Tools | ||