core_cpu.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <condition_variable>
  5. #include <mutex>
  6. #include "common/logging/log.h"
  7. #ifdef ARCHITECTURE_x86_64
  8. #include "core/arm/dynarmic/arm_dynarmic.h"
  9. #endif
  10. #include "core/arm/exclusive_monitor.h"
  11. #include "core/arm/unicorn/arm_unicorn.h"
  12. #include "core/core_cpu.h"
  13. #include "core/core_timing.h"
  14. #include "core/hle/kernel/scheduler.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/hle/lock.h"
  17. #include "core/settings.h"
  18. namespace Core {
  19. void CpuBarrier::NotifyEnd() {
  20. std::unique_lock<std::mutex> lock(mutex);
  21. end = true;
  22. condition.notify_all();
  23. }
  24. bool CpuBarrier::Rendezvous() {
  25. if (!Settings::values.use_multi_core) {
  26. // Meaningless when running in single-core mode
  27. return true;
  28. }
  29. if (!end) {
  30. std::unique_lock<std::mutex> lock(mutex);
  31. --cores_waiting;
  32. if (!cores_waiting) {
  33. cores_waiting = NUM_CPU_CORES;
  34. condition.notify_all();
  35. return true;
  36. }
  37. condition.wait(lock);
  38. return true;
  39. }
  40. return false;
  41. }
  42. Cpu::Cpu(std::shared_ptr<ExclusiveMonitor> exclusive_monitor,
  43. std::shared_ptr<CpuBarrier> cpu_barrier, std::size_t core_index)
  44. : cpu_barrier{std::move(cpu_barrier)}, core_index{core_index} {
  45. if (Settings::values.use_cpu_jit) {
  46. #ifdef ARCHITECTURE_x86_64
  47. arm_interface = std::make_shared<ARM_Dynarmic>(exclusive_monitor, core_index);
  48. #else
  49. arm_interface = std::make_shared<ARM_Unicorn>();
  50. LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
  51. #endif
  52. } else {
  53. arm_interface = std::make_shared<ARM_Unicorn>();
  54. }
  55. scheduler = std::make_shared<Kernel::Scheduler>(arm_interface.get());
  56. }
  57. Cpu::~Cpu() = default;
  58. std::shared_ptr<ExclusiveMonitor> Cpu::MakeExclusiveMonitor(std::size_t num_cores) {
  59. if (Settings::values.use_cpu_jit) {
  60. #ifdef ARCHITECTURE_x86_64
  61. return std::make_shared<DynarmicExclusiveMonitor>(num_cores);
  62. #else
  63. return nullptr; // TODO(merry): Passthrough exclusive monitor
  64. #endif
  65. } else {
  66. return nullptr; // TODO(merry): Passthrough exclusive monitor
  67. }
  68. }
  69. void Cpu::RunLoop(bool tight_loop) {
  70. // Wait for all other CPU cores to complete the previous slice, such that they run in lock-step
  71. if (!cpu_barrier->Rendezvous()) {
  72. // If rendezvous failed, session has been killed
  73. return;
  74. }
  75. // If we don't have a currently active thread then don't execute instructions,
  76. // instead advance to the next event and try to yield to the next thread
  77. if (Kernel::GetCurrentThread() == nullptr) {
  78. LOG_TRACE(Core, "Core-{} idling", core_index);
  79. if (IsMainCore()) {
  80. // TODO(Subv): Only let CoreTiming idle if all 4 cores are idling.
  81. CoreTiming::Idle();
  82. CoreTiming::Advance();
  83. }
  84. PrepareReschedule();
  85. } else {
  86. if (IsMainCore()) {
  87. CoreTiming::Advance();
  88. }
  89. if (tight_loop) {
  90. arm_interface->Run();
  91. } else {
  92. arm_interface->Step();
  93. }
  94. }
  95. Reschedule();
  96. }
  97. void Cpu::SingleStep() {
  98. return RunLoop(false);
  99. }
  100. void Cpu::PrepareReschedule() {
  101. arm_interface->PrepareReschedule();
  102. reschedule_pending = true;
  103. }
  104. void Cpu::Reschedule() {
  105. if (!reschedule_pending) {
  106. return;
  107. }
  108. reschedule_pending = false;
  109. // Lock the global kernel mutex when we manipulate the HLE state
  110. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  111. scheduler->Reschedule();
  112. }
  113. } // namespace Core