arm_dynarmic_32.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "common/settings.h"
  13. #include "core/arm/cpu_interrupt_handler.h"
  14. #include "core/arm/dynarmic/arm_dynarmic_32.h"
  15. #include "core/arm/dynarmic/arm_dynarmic_cp15.h"
  16. #include "core/arm/dynarmic/arm_exclusive_monitor.h"
  17. #include "core/core.h"
  18. #include "core/core_timing.h"
  19. #include "core/hle/kernel/svc.h"
  20. #include "core/memory.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. LOG_CRITICAL(Core_ARM,
  67. "ExceptionRaised(exception = {}, pc = {:08X}, code = {:08X}, thumb = {})",
  68. exception, pc, MemoryReadCode(pc), parent.IsInThumbMode());
  69. UNIMPLEMENTED();
  70. }
  71. void CallSVC(u32 swi) override {
  72. Kernel::Svc::Call(parent.system, swi);
  73. }
  74. void AddTicks(u64 ticks) override {
  75. if (parent.uses_wall_clock) {
  76. return;
  77. }
  78. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  79. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  80. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  81. // device a way so that timing is consistent across all cores without increasing the ticks 4
  82. // times.
  83. u64 amortized_ticks =
  84. (ticks - num_interpreted_instructions) / Core::Hardware::NUM_CPU_CORES;
  85. // Always execute at least one tick.
  86. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  87. parent.system.CoreTiming().AddTicks(amortized_ticks);
  88. num_interpreted_instructions = 0;
  89. }
  90. u64 GetTicksRemaining() override {
  91. if (parent.uses_wall_clock) {
  92. if (!parent.interrupt_handlers[parent.core_index].IsInterrupted()) {
  93. return minimum_run_cycles;
  94. }
  95. return 0U;
  96. }
  97. return std::max<s64>(parent.system.CoreTiming().GetDowncount(), 0);
  98. }
  99. ARM_Dynarmic_32& parent;
  100. std::size_t num_interpreted_instructions{};
  101. static constexpr u64 minimum_run_cycles = 1000U;
  102. };
  103. std::shared_ptr<Dynarmic::A32::Jit> ARM_Dynarmic_32::MakeJit(Common::PageTable* page_table) const {
  104. Dynarmic::A32::UserConfig config;
  105. config.callbacks = cb.get();
  106. config.coprocessors[15] = cp15;
  107. config.define_unpredictable_behaviour = true;
  108. static constexpr std::size_t PAGE_BITS = 12;
  109. static constexpr std::size_t NUM_PAGE_TABLE_ENTRIES = 1 << (32 - PAGE_BITS);
  110. if (page_table) {
  111. config.page_table = reinterpret_cast<std::array<std::uint8_t*, NUM_PAGE_TABLE_ENTRIES>*>(
  112. page_table->pointers.data());
  113. }
  114. config.absolute_offset_page_table = true;
  115. config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS;
  116. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  117. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  118. // Multi-process state
  119. config.processor_id = core_index;
  120. config.global_monitor = &exclusive_monitor.monitor;
  121. // Timing
  122. config.wall_clock_cntpct = uses_wall_clock;
  123. // Code cache size
  124. config.code_cache_size = 512 * 1024 * 1024;
  125. config.far_code_offset = 256 * 1024 * 1024;
  126. // Safe optimizations
  127. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::DebugMode) {
  128. if (!Settings::values.cpuopt_page_tables) {
  129. config.page_table = nullptr;
  130. }
  131. if (!Settings::values.cpuopt_block_linking) {
  132. config.optimizations &= ~Dynarmic::OptimizationFlag::BlockLinking;
  133. }
  134. if (!Settings::values.cpuopt_return_stack_buffer) {
  135. config.optimizations &= ~Dynarmic::OptimizationFlag::ReturnStackBuffer;
  136. }
  137. if (!Settings::values.cpuopt_fast_dispatcher) {
  138. config.optimizations &= ~Dynarmic::OptimizationFlag::FastDispatch;
  139. }
  140. if (!Settings::values.cpuopt_context_elimination) {
  141. config.optimizations &= ~Dynarmic::OptimizationFlag::GetSetElimination;
  142. }
  143. if (!Settings::values.cpuopt_const_prop) {
  144. config.optimizations &= ~Dynarmic::OptimizationFlag::ConstProp;
  145. }
  146. if (!Settings::values.cpuopt_misc_ir) {
  147. config.optimizations &= ~Dynarmic::OptimizationFlag::MiscIROpt;
  148. }
  149. if (!Settings::values.cpuopt_reduce_misalign_checks) {
  150. config.only_detect_misalignment_via_page_table_on_page_boundary = false;
  151. }
  152. }
  153. // Unsafe optimizations
  154. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::Unsafe) {
  155. config.unsafe_optimizations = true;
  156. if (Settings::values.cpuopt_unsafe_unfuse_fma.GetValue()) {
  157. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_UnfuseFMA;
  158. }
  159. if (Settings::values.cpuopt_unsafe_reduce_fp_error.GetValue()) {
  160. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_ReducedErrorFP;
  161. }
  162. if (Settings::values.cpuopt_unsafe_inaccurate_nan.GetValue()) {
  163. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_InaccurateNaN;
  164. }
  165. }
  166. return std::make_unique<Dynarmic::A32::Jit>(config);
  167. }
  168. void ARM_Dynarmic_32::Run() {
  169. jit->Run();
  170. }
  171. void ARM_Dynarmic_32::ExceptionalExit() {
  172. jit->ExceptionalExit();
  173. }
  174. void ARM_Dynarmic_32::Step() {
  175. jit->Step();
  176. }
  177. ARM_Dynarmic_32::ARM_Dynarmic_32(System& system_, CPUInterrupts& interrupt_handlers_,
  178. bool uses_wall_clock_, ExclusiveMonitor& exclusive_monitor_,
  179. std::size_t core_index_)
  180. : ARM_Interface{system_, interrupt_handlers_, uses_wall_clock_},
  181. cb(std::make_unique<DynarmicCallbacks32>(*this)),
  182. cp15(std::make_shared<DynarmicCP15>(*this)), core_index{core_index_},
  183. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor_)},
  184. jit(MakeJit(nullptr)) {}
  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::SaveContext(ThreadContext32& ctx) {
  221. Dynarmic::A32::Context context;
  222. jit->SaveContext(context);
  223. ctx.cpu_registers = context.Regs();
  224. ctx.extension_registers = context.ExtRegs();
  225. ctx.cpsr = context.Cpsr();
  226. ctx.fpscr = context.Fpscr();
  227. }
  228. void ARM_Dynarmic_32::LoadContext(const ThreadContext32& ctx) {
  229. Dynarmic::A32::Context context;
  230. context.Regs() = ctx.cpu_registers;
  231. context.ExtRegs() = ctx.extension_registers;
  232. context.SetCpsr(ctx.cpsr);
  233. context.SetFpscr(ctx.fpscr);
  234. jit->LoadContext(context);
  235. }
  236. void ARM_Dynarmic_32::PrepareReschedule() {
  237. jit->HaltExecution();
  238. }
  239. void ARM_Dynarmic_32::ClearInstructionCache() {
  240. jit->ClearCache();
  241. }
  242. void ARM_Dynarmic_32::InvalidateCacheRange(VAddr addr, std::size_t size) {
  243. jit->InvalidateCacheRange(static_cast<u32>(addr), size);
  244. }
  245. void ARM_Dynarmic_32::ClearExclusiveState() {
  246. jit->ClearExclusiveState();
  247. }
  248. void ARM_Dynarmic_32::PageTableChanged(Common::PageTable& page_table,
  249. std::size_t new_address_space_size_in_bits) {
  250. ThreadContext32 ctx{};
  251. SaveContext(ctx);
  252. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  253. auto iter = jit_cache.find(key);
  254. if (iter != jit_cache.end()) {
  255. jit = iter->second;
  256. LoadContext(ctx);
  257. return;
  258. }
  259. jit = MakeJit(&page_table);
  260. LoadContext(ctx);
  261. jit_cache.emplace(key, jit);
  262. }
  263. } // namespace Core