arm_dynarmic_32.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_MSG("This should never happen, pc = {:08X}, code = {:08X}", pc,
  48. MemoryReadCode(pc));
  49. }
  50. void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override {
  51. switch (exception) {
  52. case Dynarmic::A32::Exception::UndefinedInstruction:
  53. case Dynarmic::A32::Exception::UnpredictableInstruction:
  54. break;
  55. case Dynarmic::A32::Exception::Breakpoint:
  56. break;
  57. }
  58. LOG_CRITICAL(HW_GPU, "ExceptionRaised(exception = {}, pc = {:08X}, code = {:08X})",
  59. static_cast<std::size_t>(exception), pc, MemoryReadCode(pc));
  60. UNIMPLEMENTED();
  61. }
  62. void CallSVC(u32 swi) override {
  63. Kernel::Svc::Call(parent.system, swi);
  64. }
  65. void AddTicks(u64 ticks) override {
  66. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  67. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  68. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  69. // device a way so that timing is consistent across all cores without increasing the ticks 4
  70. // times.
  71. u64 amortized_ticks = (ticks - num_interpreted_instructions) / Core::NUM_CPU_CORES;
  72. // Always execute at least one tick.
  73. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  74. parent.system.CoreTiming().AddTicks(amortized_ticks);
  75. num_interpreted_instructions = 0;
  76. }
  77. u64 GetTicksRemaining() override {
  78. return std::max(parent.system.CoreTiming().GetDowncount(), {});
  79. }
  80. ARM_Dynarmic_32& parent;
  81. std::size_t num_interpreted_instructions{};
  82. };
  83. std::shared_ptr<Dynarmic::A32::Jit> ARM_Dynarmic_32::MakeJit(Common::PageTable& page_table,
  84. std::size_t address_space_bits) const {
  85. Dynarmic::A32::UserConfig config;
  86. config.callbacks = cb.get();
  87. // TODO(bunnei): Implement page table for 32-bit
  88. // config.page_table = &page_table.pointers;
  89. config.coprocessors[15] = cp15;
  90. config.define_unpredictable_behaviour = true;
  91. return std::make_unique<Dynarmic::A32::Jit>(config);
  92. }
  93. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_32, "ARM JIT", "Dynarmic", MP_RGB(255, 64, 64));
  94. void ARM_Dynarmic_32::Run() {
  95. MICROPROFILE_SCOPE(ARM_Jit_Dynarmic_32);
  96. jit->Run();
  97. }
  98. void ARM_Dynarmic_32::Step() {
  99. jit->Step();
  100. }
  101. ARM_Dynarmic_32::ARM_Dynarmic_32(System& system, ExclusiveMonitor& exclusive_monitor,
  102. std::size_t core_index)
  103. : ARM_Interface{system}, cb(std::make_unique<DynarmicCallbacks32>(*this)),
  104. cp15(std::make_shared<DynarmicCP15>(*this)), core_index{core_index},
  105. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} {}
  106. ARM_Dynarmic_32::~ARM_Dynarmic_32() = default;
  107. void ARM_Dynarmic_32::SetPC(u64 pc) {
  108. jit->Regs()[15] = static_cast<u32>(pc);
  109. }
  110. u64 ARM_Dynarmic_32::GetPC() const {
  111. return jit->Regs()[15];
  112. }
  113. u64 ARM_Dynarmic_32::GetReg(int index) const {
  114. return jit->Regs()[index];
  115. }
  116. void ARM_Dynarmic_32::SetReg(int index, u64 value) {
  117. jit->Regs()[index] = static_cast<u32>(value);
  118. }
  119. u128 ARM_Dynarmic_32::GetVectorReg(int index) const {
  120. return {};
  121. }
  122. void ARM_Dynarmic_32::SetVectorReg(int index, u128 value) {}
  123. u32 ARM_Dynarmic_32::GetPSTATE() const {
  124. return jit->Cpsr();
  125. }
  126. void ARM_Dynarmic_32::SetPSTATE(u32 cpsr) {
  127. jit->SetCpsr(cpsr);
  128. }
  129. u64 ARM_Dynarmic_32::GetTlsAddress() const {
  130. return cp15->uro;
  131. }
  132. void ARM_Dynarmic_32::SetTlsAddress(VAddr address) {
  133. cp15->uro = static_cast<u32>(address);
  134. }
  135. u64 ARM_Dynarmic_32::GetTPIDR_EL0() const {
  136. return cp15->uprw;
  137. }
  138. void ARM_Dynarmic_32::SetTPIDR_EL0(u64 value) {
  139. cp15->uprw = value;
  140. }
  141. void ARM_Dynarmic_32::SaveContext(ThreadContext32& ctx) {
  142. Dynarmic::A32::Context context;
  143. jit->SaveContext(context);
  144. ctx.cpu_registers = context.Regs();
  145. ctx.cpsr = context.Cpsr();
  146. }
  147. void ARM_Dynarmic_32::LoadContext(const ThreadContext32& ctx) {
  148. Dynarmic::A32::Context context;
  149. context.Regs() = ctx.cpu_registers;
  150. context.SetCpsr(ctx.cpsr);
  151. jit->LoadContext(context);
  152. }
  153. void ARM_Dynarmic_32::PrepareReschedule() {
  154. jit->HaltExecution();
  155. }
  156. void ARM_Dynarmic_32::ClearInstructionCache() {
  157. jit->ClearCache();
  158. }
  159. void ARM_Dynarmic_32::ClearExclusiveState() {}
  160. void ARM_Dynarmic_32::PageTableChanged(Common::PageTable& page_table,
  161. std::size_t new_address_space_size_in_bits) {
  162. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  163. auto iter = jit_cache.find(key);
  164. if (iter != jit_cache.end()) {
  165. jit = iter->second;
  166. return;
  167. }
  168. jit = MakeJit(page_table, new_address_space_size_in_bits);
  169. jit_cache.emplace(key, jit);
  170. }
  171. } // namespace Core