core_cpu.cpp 3.2 KB

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