arm_interface.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "core/hardware_properties.h"
  9. namespace Common {
  10. struct PageTable;
  11. }
  12. namespace Kernel {
  13. enum class VMAPermission : u8;
  14. }
  15. namespace Core {
  16. class System;
  17. class CPUInterruptHandler;
  18. using CPUInterrupts = std::array<CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>;
  19. /// Generic ARMv8 CPU interface
  20. class ARM_Interface : NonCopyable {
  21. public:
  22. explicit ARM_Interface(System& system_, CPUInterrupts& interrupt_handlers_,
  23. bool uses_wall_clock_)
  24. : system{system_}, interrupt_handlers{interrupt_handlers_}, uses_wall_clock{
  25. uses_wall_clock_} {}
  26. virtual ~ARM_Interface() = default;
  27. struct ThreadContext32 {
  28. std::array<u32, 16> cpu_registers{};
  29. std::array<u32, 64> extension_registers{};
  30. u32 cpsr{};
  31. u32 fpscr{};
  32. u32 fpexc{};
  33. u32 tpidr{};
  34. };
  35. // Internally within the kernel, it expects the AArch32 version of the
  36. // thread context to be 344 bytes in size.
  37. static_assert(sizeof(ThreadContext32) == 0x150);
  38. struct ThreadContext64 {
  39. std::array<u64, 31> cpu_registers{};
  40. u64 sp{};
  41. u64 pc{};
  42. u32 pstate{};
  43. std::array<u8, 4> padding{};
  44. std::array<u128, 32> vector_registers{};
  45. u32 fpcr{};
  46. u32 fpsr{};
  47. u64 tpidr{};
  48. };
  49. // Internally within the kernel, it expects the AArch64 version of the
  50. // thread context to be 800 bytes in size.
  51. static_assert(sizeof(ThreadContext64) == 0x320);
  52. /// Runs the CPU until an event happens
  53. virtual void Run() = 0;
  54. /// Step CPU by one instruction
  55. virtual void Step() = 0;
  56. /// Exits execution from a callback, the callback must rewind the stack
  57. virtual void ExceptionalExit() = 0;
  58. /// Clear all instruction cache
  59. virtual void ClearInstructionCache() = 0;
  60. /**
  61. * Clear instruction cache range
  62. * @param addr Start address of the cache range to clear
  63. * @param size Size of the cache range to clear, starting at addr
  64. */
  65. virtual void InvalidateCacheRange(VAddr addr, std::size_t size) = 0;
  66. /**
  67. * Notifies CPU emulation that the current page table has changed.
  68. * @param new_page_table The new page table.
  69. * @param new_address_space_size_in_bits The new usable size of the address space in bits.
  70. * This can be either 32, 36, or 39 on official software.
  71. */
  72. virtual void PageTableChanged(Common::PageTable& new_page_table,
  73. std::size_t new_address_space_size_in_bits) = 0;
  74. /**
  75. * Set the Program Counter to an address
  76. * @param addr Address to set PC to
  77. */
  78. virtual void SetPC(u64 addr) = 0;
  79. /*
  80. * Get the current Program Counter
  81. * @return Returns current PC
  82. */
  83. virtual u64 GetPC() const = 0;
  84. /**
  85. * Get an ARM register
  86. * @param index Register index
  87. * @return Returns the value in the register
  88. */
  89. virtual u64 GetReg(int index) const = 0;
  90. /**
  91. * Set an ARM register
  92. * @param index Register index
  93. * @param value Value to set register to
  94. */
  95. virtual void SetReg(int index, u64 value) = 0;
  96. /**
  97. * Gets the value of a specified vector register.
  98. *
  99. * @param index The index of the vector register.
  100. * @return the value within the vector register.
  101. */
  102. virtual u128 GetVectorReg(int index) const = 0;
  103. /**
  104. * Sets a given value into a vector register.
  105. *
  106. * @param index The index of the vector register.
  107. * @param value The new value to place in the register.
  108. */
  109. virtual void SetVectorReg(int index, u128 value) = 0;
  110. /**
  111. * Get the current PSTATE register
  112. * @return Returns the value of the PSTATE register
  113. */
  114. virtual u32 GetPSTATE() const = 0;
  115. /**
  116. * Set the current PSTATE register
  117. * @param pstate Value to set PSTATE to
  118. */
  119. virtual void SetPSTATE(u32 pstate) = 0;
  120. virtual VAddr GetTlsAddress() const = 0;
  121. virtual void SetTlsAddress(VAddr address) = 0;
  122. /**
  123. * Gets the value within the TPIDR_EL0 (read/write software thread ID) register.
  124. *
  125. * @return the value within the register.
  126. */
  127. virtual u64 GetTPIDR_EL0() const = 0;
  128. /**
  129. * Sets a new value within the TPIDR_EL0 (read/write software thread ID) register.
  130. *
  131. * @param value The new value to place in the register.
  132. */
  133. virtual void SetTPIDR_EL0(u64 value) = 0;
  134. virtual void SaveContext(ThreadContext32& ctx) = 0;
  135. virtual void SaveContext(ThreadContext64& ctx) = 0;
  136. virtual void LoadContext(const ThreadContext32& ctx) = 0;
  137. virtual void LoadContext(const ThreadContext64& ctx) = 0;
  138. /// Clears the exclusive monitor's state.
  139. virtual void ClearExclusiveState() = 0;
  140. /// Prepare core for thread reschedule (if needed to correctly handle state)
  141. virtual void PrepareReschedule() = 0;
  142. struct BacktraceEntry {
  143. std::string module;
  144. u64 address;
  145. u64 original_address;
  146. u64 offset;
  147. std::string name;
  148. };
  149. static std::vector<BacktraceEntry> GetBacktraceFromContext(System& system,
  150. const ThreadContext64& ctx);
  151. std::vector<BacktraceEntry> GetBacktrace() const;
  152. /// fp (= r29) points to the last frame record.
  153. /// Note that this is the frame record for the *previous* frame, not the current one.
  154. /// Note we need to subtract 4 from our last read to get the proper address
  155. /// Frame records are two words long:
  156. /// fp+0 : pointer to previous frame record
  157. /// fp+8 : value of lr for frame
  158. void LogBacktrace() const;
  159. protected:
  160. /// System context that this ARM interface is running under.
  161. System& system;
  162. CPUInterrupts& interrupt_handlers;
  163. bool uses_wall_clock;
  164. };
  165. } // namespace Core