arm_dynarmic_64.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "core/arm/cpu_interrupt_handler.h"
  12. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  13. #include "core/arm/dynarmic/arm_exclusive_monitor.h"
  14. #include "core/core.h"
  15. #include "core/core_timing.h"
  16. #include "core/hardware_properties.h"
  17. #include "core/hle/kernel/k_scheduler.h"
  18. #include "core/hle/kernel/process.h"
  19. #include "core/hle/kernel/svc.h"
  20. #include "core/memory.h"
  21. #include "core/settings.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. config.page_table = reinterpret_cast<void**>(page_table.pointers.data());
  135. config.page_table_address_space_bits = address_space_bits;
  136. config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS;
  137. config.silently_mirror_page_table = false;
  138. config.absolute_offset_page_table = true;
  139. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  140. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  141. // Multi-process state
  142. config.processor_id = core_index;
  143. config.global_monitor = &exclusive_monitor.monitor;
  144. // System registers
  145. config.tpidrro_el0 = &cb->tpidrro_el0;
  146. config.tpidr_el0 = &cb->tpidr_el0;
  147. config.dczid_el0 = 4;
  148. config.ctr_el0 = 0x8444c004;
  149. config.cntfrq_el0 = Hardware::CNTFREQ;
  150. // Unpredictable instructions
  151. config.define_unpredictable_behaviour = true;
  152. // Timing
  153. config.wall_clock_cntpct = uses_wall_clock;
  154. // Safe optimizations
  155. if (Settings::values.cpu_accuracy == Settings::CPUAccuracy::DebugMode) {
  156. if (!Settings::values.cpuopt_page_tables) {
  157. config.page_table = nullptr;
  158. }
  159. if (!Settings::values.cpuopt_block_linking) {
  160. config.optimizations &= ~Dynarmic::OptimizationFlag::BlockLinking;
  161. }
  162. if (!Settings::values.cpuopt_return_stack_buffer) {
  163. config.optimizations &= ~Dynarmic::OptimizationFlag::ReturnStackBuffer;
  164. }
  165. if (!Settings::values.cpuopt_fast_dispatcher) {
  166. config.optimizations &= ~Dynarmic::OptimizationFlag::FastDispatch;
  167. }
  168. if (!Settings::values.cpuopt_context_elimination) {
  169. config.optimizations &= ~Dynarmic::OptimizationFlag::GetSetElimination;
  170. }
  171. if (!Settings::values.cpuopt_const_prop) {
  172. config.optimizations &= ~Dynarmic::OptimizationFlag::ConstProp;
  173. }
  174. if (!Settings::values.cpuopt_misc_ir) {
  175. config.optimizations &= ~Dynarmic::OptimizationFlag::MiscIROpt;
  176. }
  177. if (!Settings::values.cpuopt_reduce_misalign_checks) {
  178. config.only_detect_misalignment_via_page_table_on_page_boundary = false;
  179. }
  180. }
  181. // Unsafe optimizations
  182. if (Settings::values.cpu_accuracy == Settings::CPUAccuracy::Unsafe) {
  183. config.unsafe_optimizations = true;
  184. if (Settings::values.cpuopt_unsafe_unfuse_fma) {
  185. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_UnfuseFMA;
  186. }
  187. if (Settings::values.cpuopt_unsafe_reduce_fp_error) {
  188. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_ReducedErrorFP;
  189. }
  190. if (Settings::values.cpuopt_unsafe_inaccurate_nan) {
  191. config.optimizations |= Dynarmic::OptimizationFlag::Unsafe_InaccurateNaN;
  192. }
  193. }
  194. return std::make_shared<Dynarmic::A64::Jit>(config);
  195. }
  196. void ARM_Dynarmic_64::Run() {
  197. jit->Run();
  198. }
  199. void ARM_Dynarmic_64::ExceptionalExit() {
  200. jit->ExceptionalExit();
  201. }
  202. void ARM_Dynarmic_64::Step() {
  203. cb->InterpreterFallback(jit->GetPC(), 1);
  204. }
  205. ARM_Dynarmic_64::ARM_Dynarmic_64(System& system, CPUInterrupts& interrupt_handlers,
  206. bool uses_wall_clock, ExclusiveMonitor& exclusive_monitor,
  207. std::size_t core_index)
  208. : ARM_Interface{system, interrupt_handlers, uses_wall_clock},
  209. cb(std::make_unique<DynarmicCallbacks64>(*this)), core_index{core_index},
  210. exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} {}
  211. ARM_Dynarmic_64::~ARM_Dynarmic_64() = default;
  212. void ARM_Dynarmic_64::SetPC(u64 pc) {
  213. jit->SetPC(pc);
  214. }
  215. u64 ARM_Dynarmic_64::GetPC() const {
  216. return jit->GetPC();
  217. }
  218. u64 ARM_Dynarmic_64::GetReg(int index) const {
  219. return jit->GetRegister(index);
  220. }
  221. void ARM_Dynarmic_64::SetReg(int index, u64 value) {
  222. jit->SetRegister(index, value);
  223. }
  224. u128 ARM_Dynarmic_64::GetVectorReg(int index) const {
  225. return jit->GetVector(index);
  226. }
  227. void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) {
  228. jit->SetVector(index, value);
  229. }
  230. u32 ARM_Dynarmic_64::GetPSTATE() const {
  231. return jit->GetPstate();
  232. }
  233. void ARM_Dynarmic_64::SetPSTATE(u32 pstate) {
  234. jit->SetPstate(pstate);
  235. }
  236. u64 ARM_Dynarmic_64::GetTlsAddress() const {
  237. return cb->tpidrro_el0;
  238. }
  239. void ARM_Dynarmic_64::SetTlsAddress(VAddr address) {
  240. cb->tpidrro_el0 = address;
  241. }
  242. u64 ARM_Dynarmic_64::GetTPIDR_EL0() const {
  243. return cb->tpidr_el0;
  244. }
  245. void ARM_Dynarmic_64::SetTPIDR_EL0(u64 value) {
  246. cb->tpidr_el0 = value;
  247. }
  248. void ARM_Dynarmic_64::ChangeProcessorID(std::size_t new_core_id) {
  249. jit->ChangeProcessorID(new_core_id);
  250. }
  251. void ARM_Dynarmic_64::SaveContext(ThreadContext64& ctx) {
  252. if (!jit) {
  253. return;
  254. }
  255. ctx.cpu_registers = jit->GetRegisters();
  256. ctx.sp = jit->GetSP();
  257. ctx.pc = jit->GetPC();
  258. ctx.pstate = jit->GetPstate();
  259. ctx.vector_registers = jit->GetVectors();
  260. ctx.fpcr = jit->GetFpcr();
  261. ctx.fpsr = jit->GetFpsr();
  262. ctx.tpidr = cb->tpidr_el0;
  263. }
  264. void ARM_Dynarmic_64::LoadContext(const ThreadContext64& ctx) {
  265. if (!jit) {
  266. return;
  267. }
  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. if (!jit) {
  282. return;
  283. }
  284. jit->ClearCache();
  285. }
  286. void ARM_Dynarmic_64::InvalidateCacheRange(VAddr addr, std::size_t size) {
  287. if (!jit) {
  288. return;
  289. }
  290. jit->InvalidateCacheRange(addr, size);
  291. }
  292. void ARM_Dynarmic_64::ClearExclusiveState() {
  293. if (!jit) {
  294. return;
  295. }
  296. jit->ClearExclusiveState();
  297. }
  298. void ARM_Dynarmic_64::PageTableChanged(Common::PageTable& page_table,
  299. std::size_t new_address_space_size_in_bits) {
  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. return;
  305. }
  306. jit = MakeJit(page_table, new_address_space_size_in_bits);
  307. jit_cache.emplace(key, jit);
  308. }
  309. } // namespace Core