core_cpu.cpp 3.5 KB

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