arm_dynarmic_32.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2020 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <memory>
  6. #include <dynarmic/A32/a32.h>
  7. #include <dynarmic/A32/config.h>
  8. #include <dynarmic/A32/context.h>
  9. #include "common/microprofile.h"
  10. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  11. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  12. #include "core/arm/dynarmic/arm_dynarmic_cp15.h"
  13. #include "core/core.h"
  14. #include "core/core_manager.h"
  15. #include "core/core_timing.h"
  16. #include "core/hle/kernel/svc.h"
  17. #include "core/memory.h"
  18. namespace Core {
  19. class DynarmicCallbacks32 : public Dynarmic::A32::UserCallbacks {
  20. public:
  21. explicit DynarmicCallbacks32(ARM_Dynarmic_32& parent) : parent(parent) {}
  22. u8 MemoryRead8(u32 vaddr) override {
  23. return parent.system.Memory().Read8(vaddr);
  24. }
  25. u16 MemoryRead16(u32 vaddr) override {
  26. return parent.system.Memory().Read16(vaddr);
  27. }
  28. u32 MemoryRead32(u32 vaddr) override {
  29. return parent.system.Memory().Read32(vaddr);
  30. }
  31. u64 MemoryRead64(u32 vaddr) override {
  32. return parent.system.Memory().Read64(vaddr);
  33. }
  34. void MemoryWrite8(u32 vaddr, u8 value) override {
  35. parent.system.Memory().Write8(vaddr, value);
  36. }
  37. void MemoryWrite16(u32 vaddr, u16 value) override {
  38. parent.system.Memory().Write16(vaddr, value);
  39. }
  40. void MemoryWrite32(u32 vaddr, u32 value) override {
  41. parent.system.Memory().Write32(vaddr, value);
  42. }
  43. void MemoryWrite64(u32 vaddr, u64 value) override {
  44. parent.system.Memory().Write64(vaddr, value);
  45. }
  46. void InterpreterFallback(u32 pc, std::size_t num_instructions) override {
  47. UNIMPLEMENTED();
  48. }
  49. void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override {
  50. switch (exception) {
  51. case Dynarmic::A32::Exception::UndefinedInstruction:
  52. case Dynarmic::A32::Exception::UnpredictableInstruction:
  53. break;
  54. case Dynarmic::A32::Exception::Breakpoint:
  55. break;
  56. }
  57. LOG_CRITICAL(HW_GPU, "ExceptionRaised(exception = {}, pc = {:08X}, code = {:08X})",
  58. static_cast<std::size_t>(exception), pc, MemoryReadCode(pc));
  59. UNIMPLEMENTED();
  60. }
  61. void CallSVC(u32 swi) override {
  62. Kernel::Svc::Call(parent.system, swi);
  63. }
  64. void AddTicks(u64 ticks) override {
  65. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  66. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  67. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  68. // device a way so that timing is consistent across all cores without increasing the ticks 4
  69. // times.
  70. u64 amortized_ticks = (ticks - num_interpreted_instructions) / Core::NUM_CPU_CORES;
  71. // Always execute at least one tick.
  72. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  73. parent.system.CoreTiming().AddTicks(amortized_ticks);
  74. num_interpreted_instructions = 0;
  75. }
  76. u64 GetTicksRemaining() override {
  77. return std::max(parent.system.CoreTiming().GetDowncount(), {});
  78. }
  79. ARM_Dynarmic_32& parent;
  80. std::size_t num_interpreted_instructions{};
  81. u64 tpidrro_el0{};
  82. u64 tpidr_el0{};
  83. };
  84. std::shared_ptr<Dynarmic::A32::Jit> ARM_Dynarmic_32::MakeJit(Common::PageTable& page_table,
  85. std::size_t address_space_bits) const {
  86. Dynarmic::A32::UserConfig config;
  87. config.callbacks = cb.get();
  88. // TODO(bunnei): Implement page table for 32-bit
  89. // config.page_table = &page_table.pointers;
  90. config.coprocessors[15] = std::make_shared<DynarmicCP15>((u32*)&CP15_regs[0]);
  91. config.define_unpredictable_behaviour = true;
  92. return std::make_unique<Dynarmic::A32::Jit>(config);
  93. }
  94. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_32, "ARM JIT", "Dynarmic", MP_RGB(255, 64, 64));
  95. void ARM_Dynarmic_32::Run() {
  96. MICROPROFILE_SCOPE(ARM_Jit_Dynarmic_32);
  97. jit->Run();
  98. }
  99. void ARM_Dynarmic_32::Step() {
  100. cb->InterpreterFallback(jit->Regs()[15], 1);
  101. }
  102. ARM_Dynarmic_32::ARM_Dynarmic_32(System& system, ExclusiveMonitor& exclusive_monitor,
  103. std::size_t core_index)
  104. : ARM_Interface{system},
  105. cb(std::make_unique<DynarmicCallbacks32>(*this)), core_index{core_index},
  106. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} {}
  107. ARM_Dynarmic_32::~ARM_Dynarmic_32() = default;
  108. void ARM_Dynarmic_32::SetPC(u64 pc) {
  109. jit->Regs()[15] = static_cast<u32>(pc);
  110. }
  111. u64 ARM_Dynarmic_32::GetPC() const {
  112. return jit->Regs()[15];
  113. }
  114. u64 ARM_Dynarmic_32::GetReg(int index) const {
  115. return jit->Regs()[index];
  116. }
  117. void ARM_Dynarmic_32::SetReg(int index, u64 value) {
  118. jit->Regs()[index] = static_cast<u32>(value);
  119. }
  120. u128 ARM_Dynarmic_32::GetVectorReg(int index) const {
  121. return {};
  122. }
  123. void ARM_Dynarmic_32::SetVectorReg(int index, u128 value) {}
  124. u32 ARM_Dynarmic_32::GetPSTATE() const {
  125. return jit->Cpsr();
  126. }
  127. void ARM_Dynarmic_32::SetPSTATE(u32 cpsr) {
  128. jit->SetCpsr(cpsr);
  129. }
  130. u64 ARM_Dynarmic_32::GetTlsAddress() const {
  131. return CP15_regs[static_cast<std::size_t>(CP15Register::CP15_THREAD_URO)];
  132. }
  133. void ARM_Dynarmic_32::SetTlsAddress(VAddr address) {
  134. CP15_regs[static_cast<std::size_t>(CP15Register::CP15_THREAD_URO)] = static_cast<u32>(address);
  135. }
  136. u64 ARM_Dynarmic_32::GetTPIDR_EL0() const {
  137. return cb->tpidr_el0;
  138. }
  139. void ARM_Dynarmic_32::SetTPIDR_EL0(u64 value) {
  140. cb->tpidr_el0 = value;
  141. }
  142. void ARM_Dynarmic_32::SaveContext(ThreadContext32& ctx) {
  143. Dynarmic::A32::Context context;
  144. jit->SaveContext(context);
  145. ctx.cpu_registers = context.Regs();
  146. ctx.cpsr = context.Cpsr();
  147. }
  148. void ARM_Dynarmic_32::LoadContext(const ThreadContext32& ctx) {
  149. Dynarmic::A32::Context context;
  150. context.Regs() = ctx.cpu_registers;
  151. context.SetCpsr(ctx.cpsr);
  152. jit->LoadContext(context);
  153. }
  154. void ARM_Dynarmic_32::PrepareReschedule() {
  155. jit->HaltExecution();
  156. }
  157. void ARM_Dynarmic_32::ClearInstructionCache() {
  158. jit->ClearCache();
  159. }
  160. void ARM_Dynarmic_32::ClearExclusiveState() {}
  161. void ARM_Dynarmic_32::PageTableChanged(Common::PageTable& page_table,
  162. std::size_t new_address_space_size_in_bits) {
  163. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  164. auto iter = jit_cache.find(key);
  165. if (iter != jit_cache.end()) {
  166. jit = iter->second;
  167. return;
  168. }
  169. jit = MakeJit(page_table, new_address_space_size_in_bits);
  170. jit_cache.emplace(key, jit);
  171. }
  172. } // namespace Core