arm_dynarmic_64.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. parent.svc_called = true;
  96. parent.svc_swi = swi;
  97. parent.jit->HaltExecution();
  98. }
  99. void AddTicks(u64 ticks) override {
  100. if (parent.uses_wall_clock) {
  101. return;
  102. }
  103. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  104. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  105. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  106. // device a way so that timing is consistent across all cores without increasing the ticks 4
  107. // times.
  108. u64 amortized_ticks = ticks / Core::Hardware::NUM_CPU_CORES;
  109. // Always execute at least one tick.
  110. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  111. parent.system.CoreTiming().AddTicks(amortized_ticks);
  112. }
  113. u64 GetTicksRemaining() override {
  114. if (parent.uses_wall_clock) {
  115. if (!parent.interrupt_handlers[parent.core_index].IsInterrupted()) {
  116. return minimum_run_cycles;
  117. }
  118. return 0U;
  119. }
  120. return std::max<s64>(parent.system.CoreTiming().GetDowncount(), 0);
  121. }
  122. u64 GetCNTPCT() override {
  123. return parent.system.CoreTiming().GetClockTicks();
  124. }
  125. ARM_Dynarmic_64& parent;
  126. u64 tpidrro_el0 = 0;
  127. u64 tpidr_el0 = 0;
  128. static constexpr u64 minimum_run_cycles = 1000U;
  129. };
  130. std::shared_ptr<Dynarmic::A64::Jit> ARM_Dynarmic_64::MakeJit(Common::PageTable* page_table,
  131. std::size_t address_space_bits) const {
  132. Dynarmic::A64::UserConfig config;
  133. // Callbacks
  134. config.callbacks = cb.get();
  135. // Memory
  136. if (page_table) {
  137. config.page_table = reinterpret_cast<void**>(page_table->pointers.data());
  138. config.page_table_address_space_bits = address_space_bits;
  139. config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS;
  140. config.silently_mirror_page_table = false;
  141. config.absolute_offset_page_table = true;
  142. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  143. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  144. }
  145. // Multi-process state
  146. config.processor_id = core_index;
  147. config.global_monitor = &exclusive_monitor.monitor;
  148. // System registers
  149. config.tpidrro_el0 = &cb->tpidrro_el0;
  150. config.tpidr_el0 = &cb->tpidr_el0;
  151. config.dczid_el0 = 4;
  152. config.ctr_el0 = 0x8444c004;
  153. config.cntfrq_el0 = Hardware::CNTFREQ;
  154. // Unpredictable instructions
  155. config.define_unpredictable_behaviour = true;
  156. // Timing
  157. config.wall_clock_cntpct = uses_wall_clock;
  158. // Code cache size
  159. config.code_cache_size = 512 * 1024 * 1024;
  160. config.far_code_offset = 256 * 1024 * 1024;
  161. // Safe optimizations
  162. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::DebugMode) {
  163. if (!Settings::values.cpuopt_page_tables) {
  164. config.page_table = nullptr;
  165. }
  166. if (!Settings::values.cpuopt_block_linking) {
  167. config.optimizations &= ~Dynarmic::OptimizationFlag::BlockLinking;
  168. }
  169. if (!Settings::values.cpuopt_return_stack_buffer) {
  170. config.optimizations &= ~Dynarmic::OptimizationFlag::ReturnStackBuffer;
  171. }
  172. if (!Settings::values.cpuopt_fast_dispatcher) {
  173. config.optimizations &= ~Dynarmic::OptimizationFlag::FastDispatch;
  174. }
  175. if (!Settings::values.cpuopt_context_elimination) {
  176. config.optimizations &= ~Dynarmic::OptimizationFlag::GetSetElimination;
  177. }
  178. if (!Settings::values.cpuopt_const_prop) {
  179. config.optimizations &= ~Dynarmic::OptimizationFlag::ConstProp;
  180. }
  181. if (!Settings::values.cpuopt_misc_ir) {
  182. config.optimizations &= ~Dynarmic::OptimizationFlag::MiscIROpt;
  183. }
  184. if (!Settings::values.cpuopt_reduce_misalign_checks) {
  185. config.only_detect_misalignment_via_page_table_on_page_boundary = false;
  186. }
  187. }
  188. // Unsafe optimizations
  189. if (Settings::values.cpu_accuracy.GetValue() == Settings::CPUAccuracy::Unsafe) {
  190. config.unsafe_optimizations = true;
  191. if (Settings::values.cpuopt_unsafe_unfuse_fma.GetValue()) {
  192. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_UnfuseFMA;
  193. }
  194. if (Settings::values.cpuopt_unsafe_reduce_fp_error.GetValue()) {
  195. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_ReducedErrorFP;
  196. }
  197. if (Settings::values.cpuopt_unsafe_inaccurate_nan.GetValue()) {
  198. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_InaccurateNaN;
  199. }
  200. }
  201. return std::make_shared<Dynarmic::A64::Jit>(config);
  202. }
  203. void ARM_Dynarmic_64::Run() {
  204. while (true) {
  205. jit->Run();
  206. if (!svc_called) {
  207. break;
  208. }
  209. svc_called = false;
  210. Kernel::Svc::Call(system, svc_swi);
  211. if (shutdown) {
  212. break;
  213. }
  214. }
  215. }
  216. void ARM_Dynarmic_64::Step() {
  217. cb->InterpreterFallback(jit->GetPC(), 1);
  218. }
  219. ARM_Dynarmic_64::ARM_Dynarmic_64(System& system_, CPUInterrupts& interrupt_handlers_,
  220. bool uses_wall_clock_, ExclusiveMonitor& exclusive_monitor_,
  221. std::size_t core_index_)
  222. : ARM_Interface{system_, interrupt_handlers_, uses_wall_clock_},
  223. cb(std::make_unique<DynarmicCallbacks64>(*this)), core_index{core_index_},
  224. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor_)},
  225. jit(MakeJit(nullptr, 48)) {}
  226. ARM_Dynarmic_64::~ARM_Dynarmic_64() = default;
  227. void ARM_Dynarmic_64::SetPC(u64 pc) {
  228. jit->SetPC(pc);
  229. }
  230. u64 ARM_Dynarmic_64::GetPC() const {
  231. return jit->GetPC();
  232. }
  233. u64 ARM_Dynarmic_64::GetReg(int index) const {
  234. return jit->GetRegister(index);
  235. }
  236. void ARM_Dynarmic_64::SetReg(int index, u64 value) {
  237. jit->SetRegister(index, value);
  238. }
  239. u128 ARM_Dynarmic_64::GetVectorReg(int index) const {
  240. return jit->GetVector(index);
  241. }
  242. void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) {
  243. jit->SetVector(index, value);
  244. }
  245. u32 ARM_Dynarmic_64::GetPSTATE() const {
  246. return jit->GetPstate();
  247. }
  248. void ARM_Dynarmic_64::SetPSTATE(u32 pstate) {
  249. jit->SetPstate(pstate);
  250. }
  251. u64 ARM_Dynarmic_64::GetTlsAddress() const {
  252. return cb->tpidrro_el0;
  253. }
  254. void ARM_Dynarmic_64::SetTlsAddress(VAddr address) {
  255. cb->tpidrro_el0 = address;
  256. }
  257. u64 ARM_Dynarmic_64::GetTPIDR_EL0() const {
  258. return cb->tpidr_el0;
  259. }
  260. void ARM_Dynarmic_64::SetTPIDR_EL0(u64 value) {
  261. cb->tpidr_el0 = value;
  262. }
  263. void ARM_Dynarmic_64::SaveContext(ThreadContext64& ctx) {
  264. ctx.cpu_registers = jit->GetRegisters();
  265. ctx.sp = jit->GetSP();
  266. ctx.pc = jit->GetPC();
  267. ctx.pstate = jit->GetPstate();
  268. ctx.vector_registers = jit->GetVectors();
  269. ctx.fpcr = jit->GetFpcr();
  270. ctx.fpsr = jit->GetFpsr();
  271. ctx.tpidr = cb->tpidr_el0;
  272. }
  273. void ARM_Dynarmic_64::LoadContext(const ThreadContext64& ctx) {
  274. jit->SetRegisters(ctx.cpu_registers);
  275. jit->SetSP(ctx.sp);
  276. jit->SetPC(ctx.pc);
  277. jit->SetPstate(ctx.pstate);
  278. jit->SetVectors(ctx.vector_registers);
  279. jit->SetFpcr(ctx.fpcr);
  280. jit->SetFpsr(ctx.fpsr);
  281. SetTPIDR_EL0(ctx.tpidr);
  282. }
  283. void ARM_Dynarmic_64::PrepareReschedule() {
  284. jit->HaltExecution();
  285. shutdown = true;
  286. }
  287. void ARM_Dynarmic_64::ClearInstructionCache() {
  288. jit->ClearCache();
  289. }
  290. void ARM_Dynarmic_64::InvalidateCacheRange(VAddr addr, std::size_t size) {
  291. jit->InvalidateCacheRange(addr, size);
  292. }
  293. void ARM_Dynarmic_64::ClearExclusiveState() {
  294. jit->ClearExclusiveState();
  295. }
  296. void ARM_Dynarmic_64::PageTableChanged(Common::PageTable& page_table,
  297. std::size_t new_address_space_size_in_bits) {
  298. ThreadContext64 ctx{};
  299. SaveContext(ctx);
  300. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  301. auto iter = jit_cache.find(key);
  302. if (iter != jit_cache.end()) {
  303. jit = iter->second;
  304. LoadContext(ctx);
  305. return;
  306. }
  307. jit = MakeJit(&page_table, new_address_space_size_in_bits);
  308. LoadContext(ctx);
  309. jit_cache.emplace(key, jit);
  310. }
  311. } // namespace Core