arm_dynarmic_64.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright 2018 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/A64/a64.h>
  7. #include <dynarmic/A64/config.h>
  8. #include "common/assert.h"
  9. #include "common/logging/log.h"
  10. #include "common/page_table.h"
  11. #include "common/settings.h"
  12. #include "core/arm/cpu_interrupt_handler.h"
  13. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  14. #include "core/arm/dynarmic/arm_exclusive_monitor.h"
  15. #include "core/core.h"
  16. #include "core/core_timing.h"
  17. #include "core/hardware_properties.h"
  18. #include "core/hle/kernel/k_process.h"
  19. #include "core/hle/kernel/k_scheduler.h"
  20. #include "core/hle/kernel/svc.h"
  21. #include "core/memory.h"
  22. namespace Core {
  23. using Vector = Dynarmic::A64::Vector;
  24. class DynarmicCallbacks64 : public Dynarmic::A64::UserCallbacks {
  25. public:
  26. explicit DynarmicCallbacks64(ARM_Dynarmic_64& parent_) : parent{parent_} {}
  27. u8 MemoryRead8(u64 vaddr) override {
  28. return parent.system.Memory().Read8(vaddr);
  29. }
  30. u16 MemoryRead16(u64 vaddr) override {
  31. return parent.system.Memory().Read16(vaddr);
  32. }
  33. u32 MemoryRead32(u64 vaddr) override {
  34. return parent.system.Memory().Read32(vaddr);
  35. }
  36. u64 MemoryRead64(u64 vaddr) override {
  37. return parent.system.Memory().Read64(vaddr);
  38. }
  39. Vector MemoryRead128(u64 vaddr) override {
  40. auto& memory = parent.system.Memory();
  41. return {memory.Read64(vaddr), memory.Read64(vaddr + 8)};
  42. }
  43. void MemoryWrite8(u64 vaddr, u8 value) override {
  44. parent.system.Memory().Write8(vaddr, value);
  45. }
  46. void MemoryWrite16(u64 vaddr, u16 value) override {
  47. parent.system.Memory().Write16(vaddr, value);
  48. }
  49. void MemoryWrite32(u64 vaddr, u32 value) override {
  50. parent.system.Memory().Write32(vaddr, value);
  51. }
  52. void MemoryWrite64(u64 vaddr, u64 value) override {
  53. parent.system.Memory().Write64(vaddr, value);
  54. }
  55. void MemoryWrite128(u64 vaddr, Vector value) override {
  56. auto& memory = parent.system.Memory();
  57. memory.Write64(vaddr, value[0]);
  58. memory.Write64(vaddr + 8, value[1]);
  59. }
  60. bool MemoryWriteExclusive8(u64 vaddr, std::uint8_t value, std::uint8_t expected) override {
  61. return parent.system.Memory().WriteExclusive8(vaddr, value, expected);
  62. }
  63. bool MemoryWriteExclusive16(u64 vaddr, std::uint16_t value, std::uint16_t expected) override {
  64. return parent.system.Memory().WriteExclusive16(vaddr, value, expected);
  65. }
  66. bool MemoryWriteExclusive32(u64 vaddr, std::uint32_t value, std::uint32_t expected) override {
  67. return parent.system.Memory().WriteExclusive32(vaddr, value, expected);
  68. }
  69. bool MemoryWriteExclusive64(u64 vaddr, std::uint64_t value, std::uint64_t expected) override {
  70. return parent.system.Memory().WriteExclusive64(vaddr, value, expected);
  71. }
  72. bool MemoryWriteExclusive128(u64 vaddr, Vector value, Vector expected) override {
  73. return parent.system.Memory().WriteExclusive128(vaddr, value, expected);
  74. }
  75. void InterpreterFallback(u64 pc, std::size_t num_instructions) override {
  76. LOG_ERROR(Core_ARM,
  77. "Unimplemented instruction @ 0x{:X} for {} instructions (instr = {:08X})", pc,
  78. num_instructions, MemoryReadCode(pc));
  79. }
  80. void ExceptionRaised(u64 pc, Dynarmic::A64::Exception exception) override {
  81. switch (exception) {
  82. case Dynarmic::A64::Exception::WaitForInterrupt:
  83. case Dynarmic::A64::Exception::WaitForEvent:
  84. case Dynarmic::A64::Exception::SendEvent:
  85. case Dynarmic::A64::Exception::SendEventLocal:
  86. case Dynarmic::A64::Exception::Yield:
  87. return;
  88. case Dynarmic::A64::Exception::Breakpoint:
  89. default:
  90. ASSERT_MSG(false, "ExceptionRaised(exception = {}, pc = {:08X}, code = {:08X})",
  91. static_cast<std::size_t>(exception), pc, MemoryReadCode(pc));
  92. }
  93. }
  94. void CallSVC(u32 swi) override {
  95. Kernel::Svc::Call(parent.system, swi);
  96. }
  97. void AddTicks(u64 ticks) override {
  98. if (parent.uses_wall_clock) {
  99. return;
  100. }
  101. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  102. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  103. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  104. // device a way so that timing is consistent across all cores without increasing the ticks 4
  105. // times.
  106. u64 amortized_ticks = ticks / Core::Hardware::NUM_CPU_CORES;
  107. // Always execute at least one tick.
  108. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  109. parent.system.CoreTiming().AddTicks(amortized_ticks);
  110. }
  111. u64 GetTicksRemaining() override {
  112. if (parent.uses_wall_clock) {
  113. if (!parent.interrupt_handlers[parent.core_index].IsInterrupted()) {
  114. return minimum_run_cycles;
  115. }
  116. return 0U;
  117. }
  118. return std::max<s64>(parent.system.CoreTiming().GetDowncount(), 0);
  119. }
  120. u64 GetCNTPCT() override {
  121. return parent.system.CoreTiming().GetClockTicks();
  122. }
  123. ARM_Dynarmic_64& parent;
  124. u64 tpidrro_el0 = 0;
  125. u64 tpidr_el0 = 0;
  126. static constexpr u64 minimum_run_cycles = 1000U;
  127. };
  128. std::shared_ptr<Dynarmic::A64::Jit> ARM_Dynarmic_64::MakeJit(Common::PageTable* page_table,
  129. std::size_t address_space_bits) const {
  130. Dynarmic::A64::UserConfig config;
  131. // Callbacks
  132. config.callbacks = cb.get();
  133. // Memory
  134. if (page_table) {
  135. config.page_table = reinterpret_cast<void**>(page_table->pointers.data());
  136. config.page_table_address_space_bits = address_space_bits;
  137. config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS;
  138. config.silently_mirror_page_table = false;
  139. config.absolute_offset_page_table = true;
  140. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  141. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  142. }
  143. // Multi-process state
  144. config.processor_id = core_index;
  145. config.global_monitor = &exclusive_monitor.monitor;
  146. // System registers
  147. config.tpidrro_el0 = &cb->tpidrro_el0;
  148. config.tpidr_el0 = &cb->tpidr_el0;
  149. config.dczid_el0 = 4;
  150. config.ctr_el0 = 0x8444c004;
  151. config.cntfrq_el0 = Hardware::CNTFREQ;
  152. // Unpredictable instructions
  153. config.define_unpredictable_behaviour = true;
  154. // Timing
  155. config.wall_clock_cntpct = uses_wall_clock;
  156. // Code cache size
  157. config.code_cache_size = 512 * 1024 * 1024;
  158. config.far_code_offset = 256 * 1024 * 1024;
  159. // Safe optimizations
  160. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::DebugMode) {
  161. if (!Settings::values.cpuopt_page_tables) {
  162. config.page_table = nullptr;
  163. }
  164. if (!Settings::values.cpuopt_block_linking) {
  165. config.optimizations &= ~Dynarmic::OptimizationFlag::BlockLinking;
  166. }
  167. if (!Settings::values.cpuopt_return_stack_buffer) {
  168. config.optimizations &= ~Dynarmic::OptimizationFlag::ReturnStackBuffer;
  169. }
  170. if (!Settings::values.cpuopt_fast_dispatcher) {
  171. config.optimizations &= ~Dynarmic::OptimizationFlag::FastDispatch;
  172. }
  173. if (!Settings::values.cpuopt_context_elimination) {
  174. config.optimizations &= ~Dynarmic::OptimizationFlag::GetSetElimination;
  175. }
  176. if (!Settings::values.cpuopt_const_prop) {
  177. config.optimizations &= ~Dynarmic::OptimizationFlag::ConstProp;
  178. }
  179. if (!Settings::values.cpuopt_misc_ir) {
  180. config.optimizations &= ~Dynarmic::OptimizationFlag::MiscIROpt;
  181. }
  182. if (!Settings::values.cpuopt_reduce_misalign_checks) {
  183. config.only_detect_misalignment_via_page_table_on_page_boundary = false;
  184. }
  185. }
  186. // Unsafe optimizations
  187. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::Unsafe) {
  188. config.unsafe_optimizations = true;
  189. if (Settings::values.cpuopt_unsafe_unfuse_fma.GetValue()) {
  190. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_UnfuseFMA;
  191. }
  192. if (Settings::values.cpuopt_unsafe_reduce_fp_error.GetValue()) {
  193. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_ReducedErrorFP;
  194. }
  195. if (Settings::values.cpuopt_unsafe_inaccurate_nan.GetValue()) {
  196. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_InaccurateNaN;
  197. }
  198. }
  199. return std::make_shared<Dynarmic::A64::Jit>(config);
  200. }
  201. void ARM_Dynarmic_64::Run() {
  202. jit->Run();
  203. }
  204. void ARM_Dynarmic_64::ExceptionalExit() {
  205. jit->ExceptionalExit();
  206. }
  207. void ARM_Dynarmic_64::Step() {
  208. cb->InterpreterFallback(jit->GetPC(), 1);
  209. }
  210. ARM_Dynarmic_64::ARM_Dynarmic_64(System& system_, CPUInterrupts& interrupt_handlers_,
  211. bool uses_wall_clock_, ExclusiveMonitor& exclusive_monitor_,
  212. std::size_t core_index_)
  213. : ARM_Interface{system_, interrupt_handlers_, uses_wall_clock_},
  214. cb(std::make_unique<DynarmicCallbacks64>(*this)), core_index{core_index_},
  215. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor_)},
  216. jit(MakeJit(nullptr, 48)) {}
  217. ARM_Dynarmic_64::~ARM_Dynarmic_64() = default;
  218. void ARM_Dynarmic_64::SetPC(u64 pc) {
  219. jit->SetPC(pc);
  220. }
  221. u64 ARM_Dynarmic_64::GetPC() const {
  222. return jit->GetPC();
  223. }
  224. u64 ARM_Dynarmic_64::GetReg(int index) const {
  225. return jit->GetRegister(index);
  226. }
  227. void ARM_Dynarmic_64::SetReg(int index, u64 value) {
  228. jit->SetRegister(index, value);
  229. }
  230. u128 ARM_Dynarmic_64::GetVectorReg(int index) const {
  231. return jit->GetVector(index);
  232. }
  233. void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) {
  234. jit->SetVector(index, value);
  235. }
  236. u32 ARM_Dynarmic_64::GetPSTATE() const {
  237. return jit->GetPstate();
  238. }
  239. void ARM_Dynarmic_64::SetPSTATE(u32 pstate) {
  240. jit->SetPstate(pstate);
  241. }
  242. u64 ARM_Dynarmic_64::GetTlsAddress() const {
  243. return cb->tpidrro_el0;
  244. }
  245. void ARM_Dynarmic_64::SetTlsAddress(VAddr address) {
  246. cb->tpidrro_el0 = address;
  247. }
  248. u64 ARM_Dynarmic_64::GetTPIDR_EL0() const {
  249. return cb->tpidr_el0;
  250. }
  251. void ARM_Dynarmic_64::SetTPIDR_EL0(u64 value) {
  252. cb->tpidr_el0 = value;
  253. }
  254. void ARM_Dynarmic_64::ChangeProcessorID(std::size_t new_core_id) {
  255. jit->ChangeProcessorID(new_core_id);
  256. }
  257. void ARM_Dynarmic_64::SaveContext(ThreadContext64& ctx) {
  258. ctx.cpu_registers = jit->GetRegisters();
  259. ctx.sp = jit->GetSP();
  260. ctx.pc = jit->GetPC();
  261. ctx.pstate = jit->GetPstate();
  262. ctx.vector_registers = jit->GetVectors();
  263. ctx.fpcr = jit->GetFpcr();
  264. ctx.fpsr = jit->GetFpsr();
  265. ctx.tpidr = cb->tpidr_el0;
  266. }
  267. void ARM_Dynarmic_64::LoadContext(const ThreadContext64& ctx) {
  268. jit->SetRegisters(ctx.cpu_registers);
  269. jit->SetSP(ctx.sp);
  270. jit->SetPC(ctx.pc);
  271. jit->SetPstate(ctx.pstate);
  272. jit->SetVectors(ctx.vector_registers);
  273. jit->SetFpcr(ctx.fpcr);
  274. jit->SetFpsr(ctx.fpsr);
  275. SetTPIDR_EL0(ctx.tpidr);
  276. }
  277. void ARM_Dynarmic_64::PrepareReschedule() {
  278. jit->HaltExecution();
  279. }
  280. void ARM_Dynarmic_64::ClearInstructionCache() {
  281. jit->ClearCache();
  282. }
  283. void ARM_Dynarmic_64::InvalidateCacheRange(VAddr addr, std::size_t size) {
  284. jit->InvalidateCacheRange(addr, size);
  285. }
  286. void ARM_Dynarmic_64::ClearExclusiveState() {
  287. jit->ClearExclusiveState();
  288. }
  289. void ARM_Dynarmic_64::PageTableChanged(Common::PageTable& page_table,
  290. std::size_t new_address_space_size_in_bits) {
  291. ThreadContext64 ctx{};
  292. SaveContext(ctx);
  293. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  294. auto iter = jit_cache.find(key);
  295. if (iter != jit_cache.end()) {
  296. jit = iter->second;
  297. LoadContext(ctx);
  298. return;
  299. }
  300. jit = MakeJit(&page_table, new_address_space_size_in_bits);
  301. LoadContext(ctx);
  302. jit_cache.emplace(key, jit);
  303. }
  304. } // namespace Core