arm_interface.h 5.7 KB

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