diff options
| author | 2023-12-06 14:19:17 +0100 | |
|---|---|---|
| committer | 2023-12-06 14:19:17 +0100 | |
| commit | 8a79dd2d6c6445bff63ea1f2f5f1611a6afcd97a (patch) | |
| tree | 265bf3c7970a570479c6a3ac1250549995f0329c /src/core/arm/arm_interface.cpp | |
| parent | Merge pull request #12271 from liamwhite/pretext-fix (diff) | |
| parent | arm: fix context save of vector regs (diff) | |
| download | yuzu-8a79dd2d6c6445bff63ea1f2f5f1611a6afcd97a.tar.gz yuzu-8a79dd2d6c6445bff63ea1f2f5f1611a6afcd97a.tar.xz yuzu-8a79dd2d6c6445bff63ea1f2f5f1611a6afcd97a.zip | |
Merge pull request #12236 from liamwhite/cpu-refactor
core: refactor emulated cpu core activation
Diffstat (limited to 'src/core/arm/arm_interface.cpp')
| -rw-r--r-- | src/core/arm/arm_interface.cpp | 217 |
1 files changed, 9 insertions, 208 deletions
diff --git a/src/core/arm/arm_interface.cpp b/src/core/arm/arm_interface.cpp index d231bf89c..698c9c8ad 100644 --- a/src/core/arm/arm_interface.cpp +++ b/src/core/arm/arm_interface.cpp | |||
| @@ -1,231 +1,32 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include <map> | ||
| 5 | #include <optional> | ||
| 6 | |||
| 7 | #include "common/bit_field.h" | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/demangle.h" | ||
| 10 | #include "common/logging/log.h" | 4 | #include "common/logging/log.h" |
| 11 | #include "core/arm/arm_interface.h" | 5 | #include "core/arm/arm_interface.h" |
| 12 | #include "core/arm/symbols.h" | 6 | #include "core/arm/debug.h" |
| 13 | #include "core/core.h" | 7 | #include "core/core.h" |
| 14 | #include "core/debugger/debugger.h" | ||
| 15 | #include "core/hle/kernel/k_process.h" | 8 | #include "core/hle/kernel/k_process.h" |
| 16 | #include "core/hle/kernel/k_thread.h" | ||
| 17 | #include "core/hle/kernel/svc.h" | ||
| 18 | #include "core/loader/loader.h" | ||
| 19 | #include "core/memory.h" | ||
| 20 | 9 | ||
| 21 | namespace Core { | 10 | namespace Core { |
| 22 | 11 | ||
| 23 | constexpr u64 SEGMENT_BASE = 0x7100000000ull; | 12 | void ArmInterface::LogBacktrace(const Kernel::KProcess* process) const { |
| 24 | 13 | Kernel::Svc::ThreadContext ctx; | |
| 25 | std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext( | 14 | this->GetContext(ctx); |
| 26 | Core::System& system, const ARM_Interface::ThreadContext32& ctx) { | ||
| 27 | std::vector<BacktraceEntry> out; | ||
| 28 | auto& memory = system.ApplicationMemory(); | ||
| 29 | |||
| 30 | const auto& reg = ctx.cpu_registers; | ||
| 31 | u32 pc = reg[15], lr = reg[14], fp = reg[11]; | ||
| 32 | out.push_back({"", 0, pc, 0, ""}); | ||
| 33 | |||
| 34 | // fp (= r11) points to the last frame record. | ||
| 35 | // Frame records are two words long: | ||
| 36 | // fp+0 : pointer to previous frame record | ||
| 37 | // fp+4 : value of lr for frame | ||
| 38 | for (size_t i = 0; i < 256; i++) { | ||
| 39 | out.push_back({"", 0, lr, 0, ""}); | ||
| 40 | if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 8)) { | ||
| 41 | break; | ||
| 42 | } | ||
| 43 | lr = memory.Read32(fp + 4); | ||
| 44 | fp = memory.Read32(fp); | ||
| 45 | } | ||
| 46 | |||
| 47 | SymbolicateBacktrace(system, out); | ||
| 48 | |||
| 49 | return out; | ||
| 50 | } | ||
| 51 | |||
| 52 | std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext( | ||
| 53 | Core::System& system, const ARM_Interface::ThreadContext64& ctx) { | ||
| 54 | std::vector<BacktraceEntry> out; | ||
| 55 | auto& memory = system.ApplicationMemory(); | ||
| 56 | |||
| 57 | const auto& reg = ctx.cpu_registers; | ||
| 58 | u64 pc = ctx.pc, lr = reg[30], fp = reg[29]; | ||
| 59 | |||
| 60 | out.push_back({"", 0, pc, 0, ""}); | ||
| 61 | |||
| 62 | // fp (= x29) points to the previous frame record. | ||
| 63 | // Frame records are two words long: | ||
| 64 | // fp+0 : pointer to previous frame record | ||
| 65 | // fp+8 : value of lr for frame | ||
| 66 | for (size_t i = 0; i < 256; i++) { | ||
| 67 | out.push_back({"", 0, lr, 0, ""}); | ||
| 68 | if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 16)) { | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | lr = memory.Read64(fp + 8); | ||
| 72 | fp = memory.Read64(fp); | ||
| 73 | } | ||
| 74 | |||
| 75 | SymbolicateBacktrace(system, out); | ||
| 76 | |||
| 77 | return out; | ||
| 78 | } | ||
| 79 | |||
| 80 | void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) { | ||
| 81 | std::map<VAddr, std::string> modules; | ||
| 82 | auto& loader{system.GetAppLoader()}; | ||
| 83 | if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) { | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | std::map<std::string, Symbols::Symbols> symbols; | ||
| 88 | for (const auto& module : modules) { | ||
| 89 | symbols.insert_or_assign(module.second, | ||
| 90 | Symbols::GetSymbols(module.first, system.ApplicationMemory(), | ||
| 91 | system.ApplicationProcess()->Is64Bit())); | ||
| 92 | } | ||
| 93 | |||
| 94 | for (auto& entry : out) { | ||
| 95 | VAddr base = 0; | ||
| 96 | for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) { | ||
| 97 | const auto& module{*iter}; | ||
| 98 | if (entry.original_address >= module.first) { | ||
| 99 | entry.module = module.second; | ||
| 100 | base = module.first; | ||
| 101 | break; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | entry.offset = entry.original_address - base; | ||
| 106 | entry.address = SEGMENT_BASE + entry.offset; | ||
| 107 | |||
| 108 | if (entry.module.empty()) { | ||
| 109 | entry.module = "unknown"; | ||
| 110 | } | ||
| 111 | |||
| 112 | const auto symbol_set = symbols.find(entry.module); | ||
| 113 | if (symbol_set != symbols.end()) { | ||
| 114 | const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset); | ||
| 115 | if (symbol) { | ||
| 116 | entry.name = Common::DemangleSymbol(*symbol); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const { | ||
| 123 | if (GetArchitecture() == Architecture::Aarch64) { | ||
| 124 | ThreadContext64 ctx; | ||
| 125 | SaveContext(ctx); | ||
| 126 | return GetBacktraceFromContext(system, ctx); | ||
| 127 | } else { | ||
| 128 | ThreadContext32 ctx; | ||
| 129 | SaveContext(ctx); | ||
| 130 | return GetBacktraceFromContext(system, ctx); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | 15 | ||
| 134 | void ARM_Interface::LogBacktrace() const { | 16 | LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", ctx.sp, ctx.pc); |
| 135 | const VAddr sp = GetSP(); | ||
| 136 | const VAddr pc = GetPC(); | ||
| 137 | LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc); | ||
| 138 | LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address", | 17 | LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address", |
| 139 | "Offset", "Symbol"); | 18 | "Offset", "Symbol"); |
| 140 | LOG_ERROR(Core_ARM, ""); | 19 | LOG_ERROR(Core_ARM, ""); |
| 141 | const auto backtrace = GetBacktrace(); | 20 | const auto backtrace = GetBacktraceFromContext(process, ctx); |
| 142 | for (const auto& entry : backtrace) { | 21 | for (const auto& entry : backtrace) { |
| 143 | LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address, | 22 | LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address, |
| 144 | entry.original_address, entry.offset, entry.name); | 23 | entry.original_address, entry.offset, entry.name); |
| 145 | } | 24 | } |
| 146 | } | 25 | } |
| 147 | 26 | ||
| 148 | void ARM_Interface::Run() { | 27 | const Kernel::DebugWatchpoint* ArmInterface::MatchingWatchpoint( |
| 149 | using Kernel::StepState; | ||
| 150 | using Kernel::SuspendType; | ||
| 151 | |||
| 152 | while (true) { | ||
| 153 | Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())}; | ||
| 154 | HaltReason hr{}; | ||
| 155 | |||
| 156 | // If the thread is scheduled for termination, exit the thread. | ||
| 157 | if (current_thread->HasDpc()) { | ||
| 158 | if (current_thread->IsTerminationRequested()) { | ||
| 159 | current_thread->Exit(); | ||
| 160 | UNREACHABLE(); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | // Notify the debugger and go to sleep if a step was performed | ||
| 165 | // and this thread has been scheduled again. | ||
| 166 | if (current_thread->GetStepState() == StepState::StepPerformed) { | ||
| 167 | system.GetDebugger().NotifyThreadStopped(current_thread); | ||
| 168 | current_thread->RequestSuspend(SuspendType::Debug); | ||
| 169 | break; | ||
| 170 | } | ||
| 171 | |||
| 172 | // Otherwise, run the thread. | ||
| 173 | system.EnterCPUProfile(); | ||
| 174 | if (current_thread->GetStepState() == StepState::StepPending) { | ||
| 175 | hr = StepJit(); | ||
| 176 | |||
| 177 | if (True(hr & HaltReason::StepThread)) { | ||
| 178 | current_thread->SetStepState(StepState::StepPerformed); | ||
| 179 | } | ||
| 180 | } else { | ||
| 181 | hr = RunJit(); | ||
| 182 | } | ||
| 183 | system.ExitCPUProfile(); | ||
| 184 | |||
| 185 | // Notify the debugger and go to sleep if a breakpoint was hit, | ||
| 186 | // or if the thread is unable to continue for any reason. | ||
| 187 | if (True(hr & HaltReason::InstructionBreakpoint) || True(hr & HaltReason::PrefetchAbort)) { | ||
| 188 | if (!True(hr & HaltReason::PrefetchAbort)) { | ||
| 189 | RewindBreakpointInstruction(); | ||
| 190 | } | ||
| 191 | if (system.DebuggerEnabled()) { | ||
| 192 | system.GetDebugger().NotifyThreadStopped(current_thread); | ||
| 193 | } else { | ||
| 194 | LogBacktrace(); | ||
| 195 | } | ||
| 196 | current_thread->RequestSuspend(SuspendType::Debug); | ||
| 197 | break; | ||
| 198 | } | ||
| 199 | |||
| 200 | // Notify the debugger and go to sleep if a watchpoint was hit. | ||
| 201 | if (True(hr & HaltReason::DataAbort)) { | ||
| 202 | if (system.DebuggerEnabled()) { | ||
| 203 | system.GetDebugger().NotifyThreadWatchpoint(current_thread, *HaltedWatchpoint()); | ||
| 204 | } else { | ||
| 205 | LogBacktrace(); | ||
| 206 | } | ||
| 207 | current_thread->RequestSuspend(SuspendType::Debug); | ||
| 208 | break; | ||
| 209 | } | ||
| 210 | |||
| 211 | // Handle syscalls and scheduling (this may change the current thread/core) | ||
| 212 | if (True(hr & HaltReason::SupervisorCall)) { | ||
| 213 | Kernel::Svc::Call(system, GetSvcNumber()); | ||
| 214 | break; | ||
| 215 | } | ||
| 216 | if (True(hr & HaltReason::BreakLoop) || !uses_wall_clock) { | ||
| 217 | break; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | void ARM_Interface::LoadWatchpointArray(const WatchpointArray* wp) { | ||
| 223 | watchpoints = wp; | ||
| 224 | } | ||
| 225 | |||
| 226 | const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint( | ||
| 227 | u64 addr, u64 size, Kernel::DebugWatchpointType access_type) const { | 28 | u64 addr, u64 size, Kernel::DebugWatchpointType access_type) const { |
| 228 | if (!watchpoints) { | 29 | if (!m_watchpoints) { |
| 229 | return nullptr; | 30 | return nullptr; |
| 230 | } | 31 | } |
| 231 | 32 | ||
| @@ -233,7 +34,7 @@ const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint( | |||
| 233 | const u64 end_address{addr + size}; | 34 | const u64 end_address{addr + size}; |
| 234 | 35 | ||
| 235 | for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) { | 36 | for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) { |
| 236 | const auto& watch{(*watchpoints)[i]}; | 37 | const auto& watch{(*m_watchpoints)[i]}; |
| 237 | 38 | ||
| 238 | if (end_address <= GetInteger(watch.start_address)) { | 39 | if (end_address <= GetInteger(watch.start_address)) { |
| 239 | continue; | 40 | continue; |