core.cpp 8.1 KB

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