arm_interface.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, bool uses_wall_clock)
  23. : system{system_}, interrupt_handlers{interrupt_handlers}, uses_wall_clock{
  24. uses_wall_clock} {}
  25. virtual ~ARM_Interface() = default;
  26. struct ThreadContext32 {
  27. std::array<u32, 16> cpu_registers{};
  28. u32 cpsr{};
  29. std::array<u8, 4> padding{};
  30. std::array<u64, 32> fprs{};
  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) == 0x158);
  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. /// Clear all instruction cache
  57. virtual void ClearInstructionCache() = 0;
  58. /// Notifies CPU emulation that the current page table has changed.
  59. ///
  60. /// @param new_page_table The new page table.
  61. /// @param new_address_space_size_in_bits The new usable size of the address space in bits.
  62. /// This can be either 32, 36, or 39 on official software.
  63. ///
  64. virtual void PageTableChanged(Common::PageTable& new_page_table,
  65. std::size_t new_address_space_size_in_bits) = 0;
  66. /**
  67. * Set the Program Counter to an address
  68. * @param addr Address to set PC to
  69. */
  70. virtual void SetPC(u64 addr) = 0;
  71. /*
  72. * Get the current Program Counter
  73. * @return Returns current PC
  74. */
  75. virtual u64 GetPC() const = 0;
  76. /**
  77. * Get an ARM register
  78. * @param index Register index
  79. * @return Returns the value in the register
  80. */
  81. virtual u64 GetReg(int index) const = 0;
  82. /**
  83. * Set an ARM register
  84. * @param index Register index
  85. * @param value Value to set register to
  86. */
  87. virtual void SetReg(int index, u64 value) = 0;
  88. /**
  89. * Gets the value of a specified vector register.
  90. *
  91. * @param index The index of the vector register.
  92. * @return the value within the vector register.
  93. */
  94. virtual u128 GetVectorReg(int index) const = 0;
  95. /**
  96. * Sets a given value into a vector register.
  97. *
  98. * @param index The index of the vector register.
  99. * @param value The new value to place in the register.
  100. */
  101. virtual void SetVectorReg(int index, u128 value) = 0;
  102. /**
  103. * Get the current PSTATE register
  104. * @return Returns the value of the PSTATE register
  105. */
  106. virtual u32 GetPSTATE() const = 0;
  107. /**
  108. * Set the current PSTATE register
  109. * @param pstate Value to set PSTATE to
  110. */
  111. virtual void SetPSTATE(u32 pstate) = 0;
  112. virtual VAddr GetTlsAddress() const = 0;
  113. virtual void SetTlsAddress(VAddr address) = 0;
  114. /**
  115. * Gets the value within the TPIDR_EL0 (read/write software thread ID) register.
  116. *
  117. * @return the value within the register.
  118. */
  119. virtual u64 GetTPIDR_EL0() const = 0;
  120. /**
  121. * Sets a new value within the TPIDR_EL0 (read/write software thread ID) register.
  122. *
  123. * @param value The new value to place in the register.
  124. */
  125. virtual void SetTPIDR_EL0(u64 value) = 0;
  126. virtual void ChangeProcessorId(std::size_t new_core_id) = 0;
  127. virtual void SaveContext(ThreadContext32& ctx) = 0;
  128. virtual void SaveContext(ThreadContext64& ctx) = 0;
  129. virtual void LoadContext(const ThreadContext32& ctx) = 0;
  130. virtual void LoadContext(const ThreadContext64& ctx) = 0;
  131. /// Clears the exclusive monitor's state.
  132. virtual void ClearExclusiveState() = 0;
  133. /// Prepare core for thread reschedule (if needed to correctly handle state)
  134. virtual void PrepareReschedule() = 0;
  135. struct BacktraceEntry {
  136. std::string module;
  137. u64 address;
  138. u64 original_address;
  139. u64 offset;
  140. std::string name;
  141. };
  142. static std::vector<BacktraceEntry> GetBacktraceFromContext(System& system,
  143. const ThreadContext64& ctx);
  144. std::vector<BacktraceEntry> GetBacktrace() const;
  145. /// fp (= r29) points to the last frame record.
  146. /// Note that this is the frame record for the *previous* frame, not the current one.
  147. /// Note we need to subtract 4 from our last read to get the proper address
  148. /// Frame records are two words long:
  149. /// fp+0 : pointer to previous frame record
  150. /// fp+8 : value of lr for frame
  151. void LogBacktrace() const;
  152. protected:
  153. /// System context that this ARM interface is running under.
  154. System& system;
  155. CPUInterrupts& interrupt_handlers;
  156. bool uses_wall_clock;
  157. };
  158. } // namespace Core