core_cpu.cpp 3.3 KB

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