arm_dynarmic_32.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/assert.h"
  10. #include "common/logging/log.h"
  11. #include "common/page_table.h"
  12. #include "core/arm/cpu_interrupt_handler.h"
  13. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  14. #include "core/arm/dynarmic/arm_dynarmic_cp15.h"
  15. #include "core/arm/dynarmic/arm_exclusive_monitor.h"
  16. #include "core/core.h"
  17. #include "core/core_timing.h"
  18. #include "core/hle/kernel/svc.h"
  19. #include "core/memory.h"
  20. #include "core/settings.h"
  21. namespace Core {
  22. class DynarmicCallbacks32 : public Dynarmic::A32::UserCallbacks {
  23. public:
  24. explicit DynarmicCallbacks32(ARM_Dynarmic_32& parent) : parent(parent) {}
  25. u8 MemoryRead8(u32 vaddr) override {
  26. return parent.system.Memory().Read8(vaddr);
  27. }
  28. u16 MemoryRead16(u32 vaddr) override {
  29. return parent.system.Memory().Read16(vaddr);
  30. }
  31. u32 MemoryRead32(u32 vaddr) override {
  32. return parent.system.Memory().Read32(vaddr);
  33. }
  34. u64 MemoryRead64(u32 vaddr) override {
  35. return parent.system.Memory().Read64(vaddr);
  36. }
  37. void MemoryWrite8(u32 vaddr, u8 value) override {
  38. parent.system.Memory().Write8(vaddr, value);
  39. }
  40. void MemoryWrite16(u32 vaddr, u16 value) override {
  41. parent.system.Memory().Write16(vaddr, value);
  42. }
  43. void MemoryWrite32(u32 vaddr, u32 value) override {
  44. parent.system.Memory().Write32(vaddr, value);
  45. }
  46. void MemoryWrite64(u32 vaddr, u64 value) override {
  47. parent.system.Memory().Write64(vaddr, value);
  48. }
  49. bool MemoryWriteExclusive8(u32 vaddr, u8 value, u8 expected) override {
  50. return parent.system.Memory().WriteExclusive8(vaddr, value, expected);
  51. }
  52. bool MemoryWriteExclusive16(u32 vaddr, u16 value, u16 expected) override {
  53. return parent.system.Memory().WriteExclusive16(vaddr, value, expected);
  54. }
  55. bool MemoryWriteExclusive32(u32 vaddr, u32 value, u32 expected) override {
  56. return parent.system.Memory().WriteExclusive32(vaddr, value, expected);
  57. }
  58. bool MemoryWriteExclusive64(u32 vaddr, u64 value, u64 expected) override {
  59. return parent.system.Memory().WriteExclusive64(vaddr, value, expected);
  60. }
  61. void InterpreterFallback(u32 pc, std::size_t num_instructions) override {
  62. UNIMPLEMENTED_MSG("This should never happen, pc = {:08X}, code = {:08X}", pc,
  63. MemoryReadCode(pc));
  64. }
  65. void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override {
  66. switch (exception) {
  67. case Dynarmic::A32::Exception::UndefinedInstruction:
  68. case Dynarmic::A32::Exception::UnpredictableInstruction:
  69. break;
  70. case Dynarmic::A32::Exception::Breakpoint:
  71. break;
  72. }
  73. LOG_CRITICAL(Core_ARM, "ExceptionRaised(exception = {}, pc = {:08X}, code = {:08X})",
  74. static_cast<std::size_t>(exception), pc, MemoryReadCode(pc));
  75. UNIMPLEMENTED();
  76. }
  77. void CallSVC(u32 swi) override {
  78. Kernel::Svc::Call(parent.system, swi);
  79. }
  80. void AddTicks(u64 ticks) override {
  81. if (parent.uses_wall_clock) {
  82. return;
  83. }
  84. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  85. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  86. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  87. // device a way so that timing is consistent across all cores without increasing the ticks 4
  88. // times.
  89. u64 amortized_ticks =
  90. (ticks - num_interpreted_instructions) / Core::Hardware::NUM_CPU_CORES;
  91. // Always execute at least one tick.
  92. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  93. parent.system.CoreTiming().AddTicks(amortized_ticks);
  94. num_interpreted_instructions = 0;
  95. }
  96. u64 GetTicksRemaining() override {
  97. if (parent.uses_wall_clock) {
  98. if (!parent.interrupt_handlers[parent.core_index].IsInterrupted()) {
  99. return minimum_run_cycles;
  100. }
  101. return 0U;
  102. }
  103. return std::max<s64>(parent.system.CoreTiming().GetDowncount(), 0);
  104. }
  105. ARM_Dynarmic_32& parent;
  106. std::size_t num_interpreted_instructions{};
  107. static constexpr u64 minimum_run_cycles = 1000U;
  108. };
  109. std::shared_ptr<Dynarmic::A32::Jit> ARM_Dynarmic_32::MakeJit(Common::PageTable& page_table,
  110. std::size_t address_space_bits) const {
  111. Dynarmic::A32::UserConfig config;
  112. config.callbacks = cb.get();
  113. // TODO(bunnei): Implement page table for 32-bit
  114. // config.page_table = &page_table.pointers;
  115. config.coprocessors[15] = cp15;
  116. config.define_unpredictable_behaviour = true;
  117. static constexpr std::size_t PAGE_BITS = 12;
  118. static constexpr std::size_t NUM_PAGE_TABLE_ENTRIES = 1 << (32 - PAGE_BITS);
  119. config.page_table = reinterpret_cast<std::array<std::uint8_t*, NUM_PAGE_TABLE_ENTRIES>*>(
  120. page_table.pointers.data());
  121. config.absolute_offset_page_table = true;
  122. config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS;
  123. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  124. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  125. // Multi-process state
  126. config.processor_id = core_index;
  127. config.global_monitor = &exclusive_monitor.monitor;
  128. // Timing
  129. config.wall_clock_cntpct = uses_wall_clock;
  130. // Safe optimizations
  131. if (Settings::values.cpu_accuracy == Settings::CPUAccuracy::DebugMode) {
  132. if (!Settings::values.cpuopt_page_tables) {
  133. config.page_table = nullptr;
  134. }
  135. if (!Settings::values.cpuopt_block_linking) {
  136. config.optimizations &= ~Dynarmic::OptimizationFlag::BlockLinking;
  137. }
  138. if (!Settings::values.cpuopt_return_stack_buffer) {
  139. config.optimizations &= ~Dynarmic::OptimizationFlag::ReturnStackBuffer;
  140. }
  141. if (!Settings::values.cpuopt_fast_dispatcher) {
  142. config.optimizations &= ~Dynarmic::OptimizationFlag::FastDispatch;
  143. }
  144. if (!Settings::values.cpuopt_context_elimination) {
  145. config.optimizations &= ~Dynarmic::OptimizationFlag::GetSetElimination;
  146. }
  147. if (!Settings::values.cpuopt_const_prop) {
  148. config.optimizations &= ~Dynarmic::OptimizationFlag::ConstProp;
  149. }
  150. if (!Settings::values.cpuopt_misc_ir) {
  151. config.optimizations &= ~Dynarmic::OptimizationFlag::MiscIROpt;
  152. }
  153. if (!Settings::values.cpuopt_reduce_misalign_checks) {
  154. config.only_detect_misalignment_via_page_table_on_page_boundary = false;
  155. }
  156. }
  157. // Unsafe optimizations
  158. if (Settings::values.cpu_accuracy == Settings::CPUAccuracy::Unsafe) {
  159. config.unsafe_optimizations = true;
  160. if (Settings::values.cpuopt_unsafe_unfuse_fma) {
  161. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_UnfuseFMA;
  162. }
  163. if (Settings::values.cpuopt_unsafe_reduce_fp_error) {
  164. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_ReducedErrorFP;
  165. }
  166. }
  167. return std::make_unique<Dynarmic::A32::Jit>(config);
  168. }
  169. void ARM_Dynarmic_32::Run() {
  170. jit->Run();
  171. }
  172. void ARM_Dynarmic_32::ExceptionalExit() {
  173. jit->ExceptionalExit();
  174. }
  175. void ARM_Dynarmic_32::Step() {
  176. jit->Step();
  177. }
  178. ARM_Dynarmic_32::ARM_Dynarmic_32(System& system, CPUInterrupts& interrupt_handlers,
  179. bool uses_wall_clock, ExclusiveMonitor& exclusive_monitor,
  180. std::size_t core_index)
  181. : ARM_Interface{system, interrupt_handlers, uses_wall_clock},
  182. cb(std::make_unique<DynarmicCallbacks32>(*this)),
  183. cp15(std::make_shared<DynarmicCP15>(*this)), core_index{core_index},
  184. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} {}
  185. ARM_Dynarmic_32::~ARM_Dynarmic_32() = default;
  186. void ARM_Dynarmic_32::SetPC(u64 pc) {
  187. jit->Regs()[15] = static_cast<u32>(pc);
  188. }
  189. u64 ARM_Dynarmic_32::GetPC() const {
  190. return jit->Regs()[15];
  191. }
  192. u64 ARM_Dynarmic_32::GetReg(int index) const {
  193. return jit->Regs()[index];
  194. }
  195. void ARM_Dynarmic_32::SetReg(int index, u64 value) {
  196. jit->Regs()[index] = static_cast<u32>(value);
  197. }
  198. u128 ARM_Dynarmic_32::GetVectorReg(int index) const {
  199. return {};
  200. }
  201. void ARM_Dynarmic_32::SetVectorReg(int index, u128 value) {}
  202. u32 ARM_Dynarmic_32::GetPSTATE() const {
  203. return jit->Cpsr();
  204. }
  205. void ARM_Dynarmic_32::SetPSTATE(u32 cpsr) {
  206. jit->SetCpsr(cpsr);
  207. }
  208. u64 ARM_Dynarmic_32::GetTlsAddress() const {
  209. return cp15->uro;
  210. }
  211. void ARM_Dynarmic_32::SetTlsAddress(VAddr address) {
  212. cp15->uro = static_cast<u32>(address);
  213. }
  214. u64 ARM_Dynarmic_32::GetTPIDR_EL0() const {
  215. return cp15->uprw;
  216. }
  217. void ARM_Dynarmic_32::SetTPIDR_EL0(u64 value) {
  218. cp15->uprw = static_cast<u32>(value);
  219. }
  220. void ARM_Dynarmic_32::ChangeProcessorID(std::size_t new_core_id) {
  221. jit->ChangeProcessorID(new_core_id);
  222. }
  223. void ARM_Dynarmic_32::SaveContext(ThreadContext32& ctx) {
  224. Dynarmic::A32::Context context;
  225. jit->SaveContext(context);
  226. ctx.cpu_registers = context.Regs();
  227. ctx.extension_registers = context.ExtRegs();
  228. ctx.cpsr = context.Cpsr();
  229. ctx.fpscr = context.Fpscr();
  230. }
  231. void ARM_Dynarmic_32::LoadContext(const ThreadContext32& ctx) {
  232. Dynarmic::A32::Context context;
  233. context.Regs() = ctx.cpu_registers;
  234. context.ExtRegs() = ctx.extension_registers;
  235. context.SetCpsr(ctx.cpsr);
  236. context.SetFpscr(ctx.fpscr);
  237. jit->LoadContext(context);
  238. }
  239. void ARM_Dynarmic_32::PrepareReschedule() {
  240. jit->HaltExecution();
  241. }
  242. void ARM_Dynarmic_32::ClearInstructionCache() {
  243. if (!jit) {
  244. return;
  245. }
  246. jit->ClearCache();
  247. }
  248. void ARM_Dynarmic_32::InvalidateCacheRange(VAddr addr, std::size_t size) {
  249. if (!jit) {
  250. return;
  251. }
  252. jit->InvalidateCacheRange(static_cast<u32>(addr), size);
  253. }
  254. void ARM_Dynarmic_32::ClearExclusiveState() {
  255. if (!jit) {
  256. return;
  257. }
  258. jit->ClearExclusiveState();
  259. }
  260. void ARM_Dynarmic_32::PageTableChanged(Common::PageTable& page_table,
  261. std::size_t new_address_space_size_in_bits) {
  262. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  263. auto iter = jit_cache.find(key);
  264. if (iter != jit_cache.end()) {
  265. jit = iter->second;
  266. return;
  267. }
  268. jit = MakeJit(page_table, new_address_space_size_in_bits);
  269. jit_cache.emplace(key, jit);
  270. }
  271. } // namespace Core