arm_interface.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <map>
  4. #include <optional>
  5. #include "common/bit_field.h"
  6. #include "common/common_types.h"
  7. #include "common/demangle.h"
  8. #include "common/logging/log.h"
  9. #include "core/arm/arm_interface.h"
  10. #include "core/arm/symbols.h"
  11. #include "core/core.h"
  12. #include "core/debugger/debugger.h"
  13. #include "core/hle/kernel/k_process.h"
  14. #include "core/hle/kernel/svc.h"
  15. #include "core/loader/loader.h"
  16. #include "core/memory.h"
  17. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  18. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  19. namespace Core {
  20. constexpr u64 SEGMENT_BASE = 0x7100000000ull;
  21. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  22. Core::System& system, const ARM_Interface::ThreadContext32& ctx) {
  23. return ARM_Dynarmic_32::GetBacktraceFromContext(system, ctx);
  24. }
  25. std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContext(
  26. Core::System& system, const ARM_Interface::ThreadContext64& ctx) {
  27. return ARM_Dynarmic_64::GetBacktraceFromContext(system, ctx);
  28. }
  29. void ARM_Interface::SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out) {
  30. std::map<VAddr, std::string> modules;
  31. auto& loader{system.GetAppLoader()};
  32. if (loader.ReadNSOModules(modules) != Loader::ResultStatus::Success) {
  33. return;
  34. }
  35. std::map<std::string, Symbols::Symbols> symbols;
  36. for (const auto& module : modules) {
  37. symbols.insert_or_assign(module.second,
  38. Symbols::GetSymbols(module.first, system.Memory(),
  39. system.CurrentProcess()->Is64BitProcess()));
  40. }
  41. for (auto& entry : out) {
  42. VAddr base = 0;
  43. for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
  44. const auto& module{*iter};
  45. if (entry.original_address >= module.first) {
  46. entry.module = module.second;
  47. base = module.first;
  48. break;
  49. }
  50. }
  51. entry.offset = entry.original_address - base;
  52. entry.address = SEGMENT_BASE + entry.offset;
  53. if (entry.module.empty()) {
  54. entry.module = "unknown";
  55. }
  56. const auto symbol_set = symbols.find(entry.module);
  57. if (symbol_set != symbols.end()) {
  58. const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
  59. if (symbol) {
  60. entry.name = Common::DemangleSymbol(*symbol);
  61. }
  62. }
  63. }
  64. }
  65. void ARM_Interface::LogBacktrace() const {
  66. const VAddr sp = GetSP();
  67. const VAddr pc = GetPC();
  68. LOG_ERROR(Core_ARM, "Backtrace, sp={:016X}, pc={:016X}", sp, pc);
  69. LOG_ERROR(Core_ARM, "{:20}{:20}{:20}{:20}{}", "Module Name", "Address", "Original Address",
  70. "Offset", "Symbol");
  71. LOG_ERROR(Core_ARM, "");
  72. const auto backtrace = GetBacktrace();
  73. for (const auto& entry : backtrace) {
  74. LOG_ERROR(Core_ARM, "{:20}{:016X} {:016X} {:016X} {}", entry.module, entry.address,
  75. entry.original_address, entry.offset, entry.name);
  76. }
  77. }
  78. void ARM_Interface::Run() {
  79. using Kernel::StepState;
  80. using Kernel::SuspendType;
  81. while (true) {
  82. Kernel::KThread* current_thread{Kernel::GetCurrentThreadPointer(system.Kernel())};
  83. Dynarmic::HaltReason hr{};
  84. // Notify the debugger and go to sleep if a step was performed
  85. // and this thread has been scheduled again.
  86. if (current_thread->GetStepState() == StepState::StepPerformed) {
  87. system.GetDebugger().NotifyThreadStopped(current_thread);
  88. current_thread->RequestSuspend(SuspendType::Debug);
  89. break;
  90. }
  91. // Otherwise, run the thread.
  92. system.EnterDynarmicProfile();
  93. if (current_thread->GetStepState() == StepState::StepPending) {
  94. hr = StepJit();
  95. if (Has(hr, step_thread)) {
  96. current_thread->SetStepState(StepState::StepPerformed);
  97. }
  98. } else {
  99. hr = RunJit();
  100. }
  101. system.ExitDynarmicProfile();
  102. // If the thread is scheduled for termination, exit the thread.
  103. if (current_thread->HasDpc()) {
  104. if (current_thread->IsTerminationRequested()) {
  105. current_thread->Exit();
  106. UNREACHABLE();
  107. }
  108. }
  109. // Notify the debugger and go to sleep if a breakpoint was hit,
  110. // or if the thread is unable to continue for any reason.
  111. if (Has(hr, breakpoint) || Has(hr, no_execute)) {
  112. if (!Has(hr, no_execute)) {
  113. RewindBreakpointInstruction();
  114. }
  115. if (system.DebuggerEnabled()) {
  116. system.GetDebugger().NotifyThreadStopped(current_thread);
  117. } else {
  118. LogBacktrace();
  119. }
  120. current_thread->RequestSuspend(SuspendType::Debug);
  121. break;
  122. }
  123. // Notify the debugger and go to sleep if a watchpoint was hit.
  124. if (Has(hr, watchpoint)) {
  125. if (system.DebuggerEnabled()) {
  126. system.GetDebugger().NotifyThreadWatchpoint(current_thread, *HaltedWatchpoint());
  127. }
  128. current_thread->RequestSuspend(SuspendType::Debug);
  129. break;
  130. }
  131. // Handle syscalls and scheduling (this may change the current thread/core)
  132. if (Has(hr, svc_call)) {
  133. Kernel::Svc::Call(system, GetSvcNumber());
  134. break;
  135. }
  136. if (Has(hr, break_loop) || !uses_wall_clock) {
  137. break;
  138. }
  139. }
  140. }
  141. void ARM_Interface::LoadWatchpointArray(const WatchpointArray& wp) {
  142. watchpoints = &wp;
  143. }
  144. const Kernel::DebugWatchpoint* ARM_Interface::MatchingWatchpoint(
  145. VAddr addr, u64 size, Kernel::DebugWatchpointType access_type) const {
  146. if (!watchpoints) {
  147. return nullptr;
  148. }
  149. const VAddr start_address{addr};
  150. const VAddr end_address{addr + size};
  151. for (size_t i = 0; i < Core::Hardware::NUM_WATCHPOINTS; i++) {
  152. const auto& watch{(*watchpoints)[i]};
  153. if (end_address <= watch.start_address) {
  154. continue;
  155. }
  156. if (start_address >= watch.end_address) {
  157. continue;
  158. }
  159. if ((access_type & watch.type) == Kernel::DebugWatchpointType::None) {
  160. continue;
  161. }
  162. return &watch;
  163. }
  164. return nullptr;
  165. }
  166. } // namespace Core