core.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <utility>
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/core_timing.h"
  9. #include "core/gdbstub/gdbstub.h"
  10. #include "core/hle/kernel/client_port.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/kernel/process.h"
  13. #include "core/hle/kernel/thread.h"
  14. #include "core/hle/service/service.h"
  15. #include "core/hle/service/sm/controller.h"
  16. #include "core/hle/service/sm/sm.h"
  17. #include "core/hw/hw.h"
  18. #include "core/loader/loader.h"
  19. #include "core/settings.h"
  20. #include "file_sys/vfs_real.h"
  21. #include "video_core/video_core.h"
  22. namespace Core {
  23. /*static*/ System System::s_instance;
  24. System::System() = default;
  25. System::~System() = default;
  26. /// Runs a CPU core while the system is powered on
  27. static void RunCpuCore(std::shared_ptr<Cpu> cpu_state) {
  28. while (Core::System::GetInstance().IsPoweredOn()) {
  29. cpu_state->RunLoop(true);
  30. }
  31. }
  32. Cpu& System::CurrentCpuCore() {
  33. // If multicore is enabled, use host thread to figure out the current CPU core
  34. if (Settings::values.use_multi_core) {
  35. const auto& search = thread_to_cpu.find(std::this_thread::get_id());
  36. ASSERT(search != thread_to_cpu.end());
  37. ASSERT(search->second);
  38. return *search->second;
  39. }
  40. // Otherwise, use single-threaded mode active_core variable
  41. return *cpu_cores[active_core];
  42. }
  43. System::ResultStatus System::RunLoop(bool tight_loop) {
  44. status = ResultStatus::Success;
  45. // Update thread_to_cpu in case Core 0 is run from a different host thread
  46. thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
  47. if (GDBStub::IsServerEnabled()) {
  48. GDBStub::HandlePacket();
  49. // If the loop is halted and we want to step, use a tiny (1) number of instructions to
  50. // execute. Otherwise, get out of the loop function.
  51. if (GDBStub::GetCpuHaltFlag()) {
  52. if (GDBStub::GetCpuStepFlag()) {
  53. GDBStub::SetCpuStepFlag(false);
  54. tight_loop = false;
  55. } else {
  56. return ResultStatus::Success;
  57. }
  58. }
  59. }
  60. for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) {
  61. cpu_cores[active_core]->RunLoop(tight_loop);
  62. if (Settings::values.use_multi_core) {
  63. // Cores 1-3 are run on other threads in this mode
  64. break;
  65. }
  66. }
  67. return status;
  68. }
  69. System::ResultStatus System::SingleStep() {
  70. return RunLoop(false);
  71. }
  72. System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
  73. app_loader = Loader::GetLoader(std::make_shared<FileSys::RealVfsFile>(filepath));
  74. if (!app_loader) {
  75. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  76. return ResultStatus::ErrorGetLoader;
  77. }
  78. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  79. app_loader->LoadKernelSystemMode();
  80. if (system_mode.second != Loader::ResultStatus::Success) {
  81. LOG_CRITICAL(Core, "Failed to determine system mode (Error {})!",
  82. static_cast<int>(system_mode.second));
  83. switch (system_mode.second) {
  84. case Loader::ResultStatus::ErrorEncrypted:
  85. return ResultStatus::ErrorLoader_ErrorEncrypted;
  86. case Loader::ResultStatus::ErrorInvalidFormat:
  87. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  88. case Loader::ResultStatus::ErrorUnsupportedArch:
  89. return ResultStatus::ErrorUnsupportedArch;
  90. default:
  91. return ResultStatus::ErrorSystemMode;
  92. }
  93. }
  94. ResultStatus init_result{Init(emu_window, system_mode.first.get())};
  95. if (init_result != ResultStatus::Success) {
  96. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  97. static_cast<int>(init_result));
  98. System::Shutdown();
  99. return init_result;
  100. }
  101. const Loader::ResultStatus load_result{app_loader->Load(current_process)};
  102. if (Loader::ResultStatus::Success != load_result) {
  103. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result));
  104. System::Shutdown();
  105. switch (load_result) {
  106. case Loader::ResultStatus::ErrorEncrypted:
  107. return ResultStatus::ErrorLoader_ErrorEncrypted;
  108. case Loader::ResultStatus::ErrorInvalidFormat:
  109. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  110. case Loader::ResultStatus::ErrorUnsupportedArch:
  111. return ResultStatus::ErrorUnsupportedArch;
  112. default:
  113. return ResultStatus::ErrorLoader;
  114. }
  115. }
  116. status = ResultStatus::Success;
  117. return status;
  118. }
  119. void System::PrepareReschedule() {
  120. CurrentCpuCore().PrepareReschedule();
  121. }
  122. PerfStats::Results System::GetAndResetPerfStats() {
  123. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  124. }
  125. const std::shared_ptr<Kernel::Scheduler>& System::Scheduler(size_t core_index) {
  126. ASSERT(core_index < NUM_CPU_CORES);
  127. return cpu_cores[core_index]->Scheduler();
  128. }
  129. ARM_Interface& System::ArmInterface(size_t core_index) {
  130. ASSERT(core_index < NUM_CPU_CORES);
  131. return cpu_cores[core_index]->ArmInterface();
  132. }
  133. Cpu& System::CpuCore(size_t core_index) {
  134. ASSERT(core_index < NUM_CPU_CORES);
  135. return *cpu_cores[core_index];
  136. }
  137. System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
  138. LOG_DEBUG(HW_Memory, "initialized OK");
  139. CoreTiming::Init();
  140. current_process = Kernel::Process::Create("main");
  141. cpu_barrier = std::make_shared<CpuBarrier>();
  142. cpu_exclusive_monitor = Cpu::MakeExclusiveMonitor(cpu_cores.size());
  143. for (size_t index = 0; index < cpu_cores.size(); ++index) {
  144. cpu_cores[index] = std::make_shared<Cpu>(cpu_exclusive_monitor, cpu_barrier, index);
  145. }
  146. gpu_core = std::make_unique<Tegra::GPU>();
  147. telemetry_session = std::make_unique<Core::TelemetrySession>();
  148. service_manager = std::make_shared<Service::SM::ServiceManager>();
  149. HW::Init();
  150. Kernel::Init(system_mode);
  151. Service::Init(service_manager);
  152. GDBStub::Init();
  153. if (!VideoCore::Init(emu_window)) {
  154. return ResultStatus::ErrorVideoCore;
  155. }
  156. // Create threads for CPU cores 1-3, and build thread_to_cpu map
  157. // CPU core 0 is run on the main thread
  158. thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
  159. if (Settings::values.use_multi_core) {
  160. for (size_t index = 0; index < cpu_core_threads.size(); ++index) {
  161. cpu_core_threads[index] =
  162. std::make_unique<std::thread>(RunCpuCore, cpu_cores[index + 1]);
  163. thread_to_cpu[cpu_core_threads[index]->get_id()] = cpu_cores[index + 1];
  164. }
  165. }
  166. LOG_DEBUG(Core, "Initialized OK");
  167. // Reset counters and set time origin to current frame
  168. GetAndResetPerfStats();
  169. perf_stats.BeginSystemFrame();
  170. return ResultStatus::Success;
  171. }
  172. void System::Shutdown() {
  173. // Log last frame performance stats
  174. auto perf_results = GetAndResetPerfStats();
  175. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_EmulationSpeed",
  176. perf_results.emulation_speed * 100.0);
  177. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Framerate",
  178. perf_results.game_fps);
  179. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Frametime",
  180. perf_results.frametime * 1000.0);
  181. // Shutdown emulation session
  182. VideoCore::Shutdown();
  183. GDBStub::Shutdown();
  184. Service::Shutdown();
  185. Kernel::Shutdown();
  186. HW::Shutdown();
  187. service_manager.reset();
  188. telemetry_session.reset();
  189. gpu_core.reset();
  190. // Close all CPU/threading state
  191. cpu_barrier->NotifyEnd();
  192. if (Settings::values.use_multi_core) {
  193. for (auto& thread : cpu_core_threads) {
  194. thread->join();
  195. thread.reset();
  196. }
  197. }
  198. thread_to_cpu.clear();
  199. for (auto& cpu_core : cpu_cores) {
  200. cpu_core.reset();
  201. }
  202. cpu_barrier.reset();
  203. // Close core timing
  204. CoreTiming::Shutdown();
  205. // Close app loader
  206. app_loader.reset();
  207. LOG_DEBUG(Core, "Shutdown OK");
  208. }
  209. Service::SM::ServiceManager& System::ServiceManager() {
  210. return *service_manager;
  211. }
  212. const Service::SM::ServiceManager& System::ServiceManager() const {
  213. return *service_manager;
  214. }
  215. } // namespace Core