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