arm_dynarmic_64.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/logging/log.h"
  9. #include "common/microprofile.h"
  10. #include "common/page_table.h"
  11. #include "core/arm/dynarmic/arm_dynarmic_64.h"
  12. #include "core/core.h"
  13. #include "core/core_manager.h"
  14. #include "core/core_timing.h"
  15. #include "core/core_timing_util.h"
  16. #include "core/gdbstub/gdbstub.h"
  17. #include "core/hardware_properties.h"
  18. #include "core/hle/kernel/process.h"
  19. #include "core/hle/kernel/scheduler.h"
  20. #include "core/hle/kernel/svc.h"
  21. #include "core/memory.h"
  22. #include "core/settings.h"
  23. namespace Core {
  24. using Vector = Dynarmic::A64::Vector;
  25. class DynarmicCallbacks64 : public Dynarmic::A64::UserCallbacks {
  26. public:
  27. explicit DynarmicCallbacks64(ARM_Dynarmic_64& parent) : parent(parent) {}
  28. u8 MemoryRead8(u64 vaddr) override {
  29. return parent.system.Memory().Read8(vaddr);
  30. }
  31. u16 MemoryRead16(u64 vaddr) override {
  32. return parent.system.Memory().Read16(vaddr);
  33. }
  34. u32 MemoryRead32(u64 vaddr) override {
  35. return parent.system.Memory().Read32(vaddr);
  36. }
  37. u64 MemoryRead64(u64 vaddr) override {
  38. return parent.system.Memory().Read64(vaddr);
  39. }
  40. Vector MemoryRead128(u64 vaddr) override {
  41. auto& memory = parent.system.Memory();
  42. return {memory.Read64(vaddr), memory.Read64(vaddr + 8)};
  43. }
  44. void MemoryWrite8(u64 vaddr, u8 value) override {
  45. parent.system.Memory().Write8(vaddr, value);
  46. }
  47. void MemoryWrite16(u64 vaddr, u16 value) override {
  48. parent.system.Memory().Write16(vaddr, value);
  49. }
  50. void MemoryWrite32(u64 vaddr, u32 value) override {
  51. parent.system.Memory().Write32(vaddr, value);
  52. }
  53. void MemoryWrite64(u64 vaddr, u64 value) override {
  54. parent.system.Memory().Write64(vaddr, value);
  55. }
  56. void MemoryWrite128(u64 vaddr, Vector value) override {
  57. auto& memory = parent.system.Memory();
  58. memory.Write64(vaddr, value[0]);
  59. memory.Write64(vaddr + 8, value[1]);
  60. }
  61. void InterpreterFallback(u64 pc, std::size_t num_instructions) override {
  62. LOG_INFO(Core_ARM, "Unicorn fallback @ 0x{:X} for {} instructions (instr = {:08X})", pc,
  63. num_instructions, MemoryReadCode(pc));
  64. ARM_Interface::ThreadContext64 ctx;
  65. parent.SaveContext(ctx);
  66. parent.inner_unicorn.LoadContext(ctx);
  67. parent.inner_unicorn.ExecuteInstructions(num_instructions);
  68. parent.inner_unicorn.SaveContext(ctx);
  69. parent.LoadContext(ctx);
  70. num_interpreted_instructions += num_instructions;
  71. }
  72. void ExceptionRaised(u64 pc, Dynarmic::A64::Exception exception) override {
  73. switch (exception) {
  74. case Dynarmic::A64::Exception::WaitForInterrupt:
  75. case Dynarmic::A64::Exception::WaitForEvent:
  76. case Dynarmic::A64::Exception::SendEvent:
  77. case Dynarmic::A64::Exception::SendEventLocal:
  78. case Dynarmic::A64::Exception::Yield:
  79. return;
  80. case Dynarmic::A64::Exception::Breakpoint:
  81. if (GDBStub::IsServerEnabled()) {
  82. parent.jit->HaltExecution();
  83. parent.SetPC(pc);
  84. Kernel::Thread* const thread = parent.system.CurrentScheduler().GetCurrentThread();
  85. parent.SaveContext(thread->GetContext64());
  86. GDBStub::Break();
  87. GDBStub::SendTrap(thread, 5);
  88. return;
  89. }
  90. [[fallthrough]];
  91. default:
  92. ASSERT_MSG(false, "ExceptionRaised(exception = {}, pc = {:X})",
  93. static_cast<std::size_t>(exception), pc);
  94. }
  95. }
  96. void CallSVC(u32 swi) override {
  97. Kernel::Svc::Call(parent.system, swi);
  98. }
  99. void AddTicks(u64 ticks) override {
  100. // Divide the number of ticks by the amount of CPU cores. TODO(Subv): This yields only a
  101. // rough approximation of the amount of executed ticks in the system, it may be thrown off
  102. // if not all cores are doing a similar amount of work. Instead of doing this, we should
  103. // device a way so that timing is consistent across all cores without increasing the ticks 4
  104. // times.
  105. u64 amortized_ticks = (ticks - num_interpreted_instructions) / Core::NUM_CPU_CORES;
  106. // Always execute at least one tick.
  107. amortized_ticks = std::max<u64>(amortized_ticks, 1);
  108. parent.system.CoreTiming().AddTicks(amortized_ticks);
  109. num_interpreted_instructions = 0;
  110. }
  111. u64 GetTicksRemaining() override {
  112. return std::max(parent.system.CoreTiming().GetDowncount(), s64{0});
  113. }
  114. u64 GetCNTPCT() override {
  115. return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks());
  116. }
  117. ARM_Dynarmic_64& parent;
  118. std::size_t num_interpreted_instructions = 0;
  119. u64 tpidrro_el0 = 0;
  120. u64 tpidr_el0 = 0;
  121. };
  122. std::shared_ptr<Dynarmic::A64::Jit> ARM_Dynarmic_64::MakeJit(Common::PageTable& page_table,
  123. std::size_t address_space_bits) const {
  124. Dynarmic::A64::UserConfig config;
  125. // Callbacks
  126. config.callbacks = cb.get();
  127. // Memory
  128. config.page_table = reinterpret_cast<void**>(page_table.pointers.data());
  129. config.page_table_address_space_bits = address_space_bits;
  130. config.silently_mirror_page_table = false;
  131. config.absolute_offset_page_table = true;
  132. config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128;
  133. config.only_detect_misalignment_via_page_table_on_page_boundary = true;
  134. // Multi-process state
  135. config.processor_id = core_index;
  136. config.global_monitor = &exclusive_monitor.monitor;
  137. // System registers
  138. config.tpidrro_el0 = &cb->tpidrro_el0;
  139. config.tpidr_el0 = &cb->tpidr_el0;
  140. config.dczid_el0 = 4;
  141. config.ctr_el0 = 0x8444c004;
  142. config.cntfrq_el0 = Hardware::CNTFREQ;
  143. // Unpredictable instructions
  144. config.define_unpredictable_behaviour = true;
  145. // Optimizations
  146. if (Settings::values.disable_cpu_opt) {
  147. config.enable_optimizations = false;
  148. config.enable_fast_dispatch = false;
  149. }
  150. return std::make_shared<Dynarmic::A64::Jit>(config);
  151. }
  152. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_64, "ARM JIT", "Dynarmic", MP_RGB(255, 64, 64));
  153. void ARM_Dynarmic_64::Run() {
  154. MICROPROFILE_SCOPE(ARM_Jit_Dynarmic_64);
  155. jit->Run();
  156. }
  157. void ARM_Dynarmic_64::Step() {
  158. cb->InterpreterFallback(jit->GetPC(), 1);
  159. }
  160. ARM_Dynarmic_64::ARM_Dynarmic_64(System& system, ExclusiveMonitor& exclusive_monitor,
  161. std::size_t core_index)
  162. : ARM_Interface{system},
  163. cb(std::make_unique<DynarmicCallbacks64>(*this)), inner_unicorn{system},
  164. core_index{core_index}, exclusive_monitor{
  165. dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} {}
  166. ARM_Dynarmic_64::~ARM_Dynarmic_64() = default;
  167. void ARM_Dynarmic_64::SetPC(u64 pc) {
  168. jit->SetPC(pc);
  169. }
  170. u64 ARM_Dynarmic_64::GetPC() const {
  171. return jit->GetPC();
  172. }
  173. u64 ARM_Dynarmic_64::GetReg(int index) const {
  174. return jit->GetRegister(index);
  175. }
  176. void ARM_Dynarmic_64::SetReg(int index, u64 value) {
  177. jit->SetRegister(index, value);
  178. }
  179. u128 ARM_Dynarmic_64::GetVectorReg(int index) const {
  180. return jit->GetVector(index);
  181. }
  182. void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) {
  183. jit->SetVector(index, value);
  184. }
  185. u32 ARM_Dynarmic_64::GetPSTATE() const {
  186. return jit->GetPstate();
  187. }
  188. void ARM_Dynarmic_64::SetPSTATE(u32 pstate) {
  189. jit->SetPstate(pstate);
  190. }
  191. u64 ARM_Dynarmic_64::GetTlsAddress() const {
  192. return cb->tpidrro_el0;
  193. }
  194. void ARM_Dynarmic_64::SetTlsAddress(VAddr address) {
  195. cb->tpidrro_el0 = address;
  196. }
  197. u64 ARM_Dynarmic_64::GetTPIDR_EL0() const {
  198. return cb->tpidr_el0;
  199. }
  200. void ARM_Dynarmic_64::SetTPIDR_EL0(u64 value) {
  201. cb->tpidr_el0 = value;
  202. }
  203. void ARM_Dynarmic_64::SaveContext(ThreadContext64& ctx) {
  204. ctx.cpu_registers = jit->GetRegisters();
  205. ctx.sp = jit->GetSP();
  206. ctx.pc = jit->GetPC();
  207. ctx.pstate = jit->GetPstate();
  208. ctx.vector_registers = jit->GetVectors();
  209. ctx.fpcr = jit->GetFpcr();
  210. ctx.fpsr = jit->GetFpsr();
  211. ctx.tpidr = cb->tpidr_el0;
  212. }
  213. void ARM_Dynarmic_64::LoadContext(const ThreadContext64& ctx) {
  214. jit->SetRegisters(ctx.cpu_registers);
  215. jit->SetSP(ctx.sp);
  216. jit->SetPC(ctx.pc);
  217. jit->SetPstate(ctx.pstate);
  218. jit->SetVectors(ctx.vector_registers);
  219. jit->SetFpcr(ctx.fpcr);
  220. jit->SetFpsr(ctx.fpsr);
  221. SetTPIDR_EL0(ctx.tpidr);
  222. }
  223. void ARM_Dynarmic_64::PrepareReschedule() {
  224. jit->HaltExecution();
  225. }
  226. void ARM_Dynarmic_64::ClearInstructionCache() {
  227. jit->ClearCache();
  228. }
  229. void ARM_Dynarmic_64::ClearExclusiveState() {
  230. jit->ClearExclusiveState();
  231. }
  232. void ARM_Dynarmic_64::PageTableChanged(Common::PageTable& page_table,
  233. std::size_t new_address_space_size_in_bits) {
  234. auto key = std::make_pair(&page_table, new_address_space_size_in_bits);
  235. auto iter = jit_cache.find(key);
  236. if (iter != jit_cache.end()) {
  237. jit = iter->second;
  238. return;
  239. }
  240. jit = MakeJit(page_table, new_address_space_size_in_bits);
  241. jit_cache.emplace(key, jit);
  242. }
  243. DynarmicExclusiveMonitor::DynarmicExclusiveMonitor(Memory::Memory& memory, std::size_t core_count)
  244. : monitor(core_count), memory{memory} {}
  245. DynarmicExclusiveMonitor::~DynarmicExclusiveMonitor() = default;
  246. void DynarmicExclusiveMonitor::SetExclusive(std::size_t core_index, VAddr addr) {
  247. // Size doesn't actually matter.
  248. monitor.Mark(core_index, addr, 16);
  249. }
  250. void DynarmicExclusiveMonitor::ClearExclusive() {
  251. monitor.Clear();
  252. }
  253. bool DynarmicExclusiveMonitor::ExclusiveWrite8(std::size_t core_index, VAddr vaddr, u8 value) {
  254. return monitor.DoExclusiveOperation(core_index, vaddr, 1, [&] { memory.Write8(vaddr, value); });
  255. }
  256. bool DynarmicExclusiveMonitor::ExclusiveWrite16(std::size_t core_index, VAddr vaddr, u16 value) {
  257. return monitor.DoExclusiveOperation(core_index, vaddr, 2,
  258. [&] { memory.Write16(vaddr, value); });
  259. }
  260. bool DynarmicExclusiveMonitor::ExclusiveWrite32(std::size_t core_index, VAddr vaddr, u32 value) {
  261. return monitor.DoExclusiveOperation(core_index, vaddr, 4,
  262. [&] { memory.Write32(vaddr, value); });
  263. }
  264. bool DynarmicExclusiveMonitor::ExclusiveWrite64(std::size_t core_index, VAddr vaddr, u64 value) {
  265. return monitor.DoExclusiveOperation(core_index, vaddr, 8,
  266. [&] { memory.Write64(vaddr, value); });
  267. }
  268. bool DynarmicExclusiveMonitor::ExclusiveWrite128(std::size_t core_index, VAddr vaddr, u128 value) {
  269. return monitor.DoExclusiveOperation(core_index, vaddr, 16, [&] {
  270. memory.Write64(vaddr + 0, value[0]);
  271. memory.Write64(vaddr + 8, value[1]);
  272. });
  273. }
  274. } // namespace Core