arm_interface.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <span>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "core/hardware_properties.h"
  11. namespace Common {
  12. struct PageTable;
  13. }
  14. namespace Kernel {
  15. enum class VMAPermission : u8;
  16. enum class DebugWatchpointType : u8;
  17. struct DebugWatchpoint;
  18. } // namespace Kernel
  19. namespace Core {
  20. class System;
  21. class CPUInterruptHandler;
  22. using WatchpointArray = std::array<Kernel::DebugWatchpoint, Core::Hardware::NUM_WATCHPOINTS>;
  23. // NOTE: these values match the HaltReason enum in Dynarmic
  24. enum class HaltReason : u64 {
  25. StepThread = 0x00000001,
  26. DataAbort = 0x00000004,
  27. BreakLoop = 0x02000000,
  28. SupervisorCall = 0x04000000,
  29. InstructionBreakpoint = 0x08000000,
  30. PrefetchAbort = 0x20000000,
  31. };
  32. DECLARE_ENUM_FLAG_OPERATORS(HaltReason);
  33. enum class Architecture {
  34. Aarch32,
  35. Aarch64,
  36. };
  37. /// Generic ARMv8 CPU interface
  38. class ARM_Interface {
  39. public:
  40. YUZU_NON_COPYABLE(ARM_Interface);
  41. YUZU_NON_MOVEABLE(ARM_Interface);
  42. explicit ARM_Interface(System& system_, bool uses_wall_clock_)
  43. : system{system_}, uses_wall_clock{uses_wall_clock_} {}
  44. virtual ~ARM_Interface() = default;
  45. struct ThreadContext32 {
  46. std::array<u32, 16> cpu_registers{};
  47. std::array<u32, 64> extension_registers{};
  48. u32 cpsr{};
  49. u32 fpscr{};
  50. u32 fpexc{};
  51. u32 tpidr{};
  52. };
  53. // Internally within the kernel, it expects the AArch32 version of the
  54. // thread context to be 344 bytes in size.
  55. static_assert(sizeof(ThreadContext32) == 0x150);
  56. struct ThreadContext64 {
  57. std::array<u64, 31> cpu_registers{};
  58. u64 sp{};
  59. u64 pc{};
  60. u32 pstate{};
  61. std::array<u8, 4> padding{};
  62. std::array<u128, 32> vector_registers{};
  63. u32 fpcr{};
  64. u32 fpsr{};
  65. u64 tpidr{};
  66. };
  67. // Internally within the kernel, it expects the AArch64 version of the
  68. // thread context to be 800 bytes in size.
  69. static_assert(sizeof(ThreadContext64) == 0x320);
  70. /// Runs the CPU until an event happens
  71. void Run();
  72. /// Clear all instruction cache
  73. virtual void ClearInstructionCache() = 0;
  74. /**
  75. * Clear instruction cache range
  76. * @param addr Start address of the cache range to clear
  77. * @param size Size of the cache range to clear, starting at addr
  78. */
  79. virtual void InvalidateCacheRange(u64 addr, std::size_t size) = 0;
  80. /**
  81. * Notifies CPU emulation that the current page table has changed.
  82. * @param new_page_table The new page table.
  83. * @param new_address_space_size_in_bits The new usable size of the address space in bits.
  84. * This can be either 32, 36, or 39 on official software.
  85. */
  86. virtual void PageTableChanged(Common::PageTable& new_page_table,
  87. std::size_t new_address_space_size_in_bits) = 0;
  88. /**
  89. * Set the Program Counter to an address
  90. * @param addr Address to set PC to
  91. */
  92. virtual void SetPC(u64 addr) = 0;
  93. /*
  94. * Get the current Program Counter
  95. * @return Returns current PC
  96. */
  97. virtual u64 GetPC() const = 0;
  98. /**
  99. * Get the current Stack Pointer
  100. * @return Returns current SP
  101. */
  102. virtual u64 GetSP() const = 0;
  103. /**
  104. * Get an ARM register
  105. * @param index Register index
  106. * @return Returns the value in the register
  107. */
  108. virtual u64 GetReg(int index) const = 0;
  109. /**
  110. * Set an ARM register
  111. * @param index Register index
  112. * @param value Value to set register to
  113. */
  114. virtual void SetReg(int index, u64 value) = 0;
  115. /**
  116. * Gets the value of a specified vector register.
  117. *
  118. * @param index The index of the vector register.
  119. * @return the value within the vector register.
  120. */
  121. virtual u128 GetVectorReg(int index) const = 0;
  122. /**
  123. * Sets a given value into a vector register.
  124. *
  125. * @param index The index of the vector register.
  126. * @param value The new value to place in the register.
  127. */
  128. virtual void SetVectorReg(int index, u128 value) = 0;
  129. /**
  130. * Get the current PSTATE register
  131. * @return Returns the value of the PSTATE register
  132. */
  133. virtual u32 GetPSTATE() const = 0;
  134. /**
  135. * Set the current PSTATE register
  136. * @param pstate Value to set PSTATE to
  137. */
  138. virtual void SetPSTATE(u32 pstate) = 0;
  139. virtual u64 GetTlsAddress() const = 0;
  140. virtual void SetTlsAddress(u64 address) = 0;
  141. /**
  142. * Gets the value within the TPIDR_EL0 (read/write software thread ID) register.
  143. *
  144. * @return the value within the register.
  145. */
  146. virtual u64 GetTPIDR_EL0() const = 0;
  147. /**
  148. * Sets a new value within the TPIDR_EL0 (read/write software thread ID) register.
  149. *
  150. * @param value The new value to place in the register.
  151. */
  152. virtual void SetTPIDR_EL0(u64 value) = 0;
  153. virtual Architecture GetArchitecture() const = 0;
  154. virtual void SaveContext(ThreadContext32& ctx) const = 0;
  155. virtual void SaveContext(ThreadContext64& ctx) const = 0;
  156. virtual void LoadContext(const ThreadContext32& ctx) = 0;
  157. virtual void LoadContext(const ThreadContext64& ctx) = 0;
  158. void LoadWatchpointArray(const WatchpointArray* wp);
  159. /// Clears the exclusive monitor's state.
  160. virtual void ClearExclusiveState() = 0;
  161. /// Signal an interrupt and ask the core to halt as soon as possible.
  162. virtual void SignalInterrupt() = 0;
  163. /// Clear a previous interrupt.
  164. virtual void ClearInterrupt() = 0;
  165. struct BacktraceEntry {
  166. std::string module;
  167. u64 address;
  168. u64 original_address;
  169. u64 offset;
  170. std::string name;
  171. };
  172. static std::vector<BacktraceEntry> GetBacktraceFromContext(System& system,
  173. const ThreadContext32& ctx);
  174. static std::vector<BacktraceEntry> GetBacktraceFromContext(System& system,
  175. const ThreadContext64& ctx);
  176. std::vector<BacktraceEntry> GetBacktrace() const;
  177. void LogBacktrace() const;
  178. protected:
  179. /// System context that this ARM interface is running under.
  180. System& system;
  181. const WatchpointArray* watchpoints;
  182. bool uses_wall_clock;
  183. static void SymbolicateBacktrace(Core::System& system, std::vector<BacktraceEntry>& out);
  184. const Kernel::DebugWatchpoint* MatchingWatchpoint(
  185. u64 addr, u64 size, Kernel::DebugWatchpointType access_type) const;
  186. virtual HaltReason RunJit() = 0;
  187. virtual HaltReason StepJit() = 0;
  188. virtual u32 GetSvcNumber() const = 0;
  189. virtual const Kernel::DebugWatchpoint* HaltedWatchpoint() const = 0;
  190. virtual void RewindBreakpointInstruction() = 0;
  191. };
  192. } // namespace Core