core_cpu.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(ExclusiveMonitor& exclusive_monitor, CpuBarrier& cpu_barrier, std::size_t core_index)
  43. : cpu_barrier{cpu_barrier}, core_index{core_index} {
  44. if (Settings::values.use_cpu_jit) {
  45. #ifdef ARCHITECTURE_x86_64
  46. arm_interface = std::make_unique<ARM_Dynarmic>(exclusive_monitor, core_index);
  47. #else
  48. arm_interface = std::make_unique<ARM_Unicorn>();
  49. LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
  50. #endif
  51. } else {
  52. arm_interface = std::make_unique<ARM_Unicorn>();
  53. }
  54. scheduler = std::make_unique<Kernel::Scheduler>(*arm_interface);
  55. }
  56. Cpu::~Cpu() = default;
  57. std::unique_ptr<ExclusiveMonitor> Cpu::MakeExclusiveMonitor(std::size_t num_cores) {
  58. if (Settings::values.use_cpu_jit) {
  59. #ifdef ARCHITECTURE_x86_64
  60. return std::make_unique<DynarmicExclusiveMonitor>(num_cores);
  61. #else
  62. return nullptr; // TODO(merry): Passthrough exclusive monitor
  63. #endif
  64. } else {
  65. return nullptr; // TODO(merry): Passthrough exclusive monitor
  66. }
  67. }
  68. void Cpu::RunLoop(bool tight_loop) {
  69. // Wait for all other CPU cores to complete the previous slice, such that they run in lock-step
  70. if (!cpu_barrier.Rendezvous()) {
  71. // If rendezvous failed, session has been killed
  72. return;
  73. }
  74. // If we don't have a currently active thread then don't execute instructions,
  75. // instead advance to the next event and try to yield to the next thread
  76. if (Kernel::GetCurrentThread() == nullptr) {
  77. LOG_TRACE(Core, "Core-{} idling", core_index);
  78. if (IsMainCore()) {
  79. // TODO(Subv): Only let CoreTiming idle if all 4 cores are idling.
  80. CoreTiming::Idle();
  81. CoreTiming::Advance();
  82. }
  83. PrepareReschedule();
  84. } else {
  85. if (IsMainCore()) {
  86. CoreTiming::Advance();
  87. }
  88. if (tight_loop) {
  89. arm_interface->Run();
  90. } else {
  91. arm_interface->Step();
  92. }
  93. }
  94. Reschedule();
  95. }
  96. void Cpu::SingleStep() {
  97. return RunLoop(false);
  98. }
  99. void Cpu::PrepareReschedule() {
  100. arm_interface->PrepareReschedule();
  101. reschedule_pending = true;
  102. }
  103. void Cpu::Reschedule() {
  104. if (!reschedule_pending) {
  105. return;
  106. }
  107. reschedule_pending = false;
  108. // Lock the global kernel mutex when we manipulate the HLE state
  109. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  110. scheduler->Reschedule();
  111. }
  112. } // namespace Core