core.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/loader/loader.h"
  18. #include "core/settings.h"
  19. #include "file_sys/vfs_real.h"
  20. #include "video_core/renderer_base.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. 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. if (GDBStub::IsServerEnabled()) {
  67. GDBStub::SetCpuStepFlag(false);
  68. }
  69. return status;
  70. }
  71. System::ResultStatus System::SingleStep() {
  72. return RunLoop(false);
  73. }
  74. System::ResultStatus System::Load(EmuWindow& emu_window, const std::string& filepath) {
  75. app_loader = Loader::GetLoader(std::make_shared<FileSys::RealVfsFile>(filepath));
  76. if (!app_loader) {
  77. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  78. return ResultStatus::ErrorGetLoader;
  79. }
  80. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  81. app_loader->LoadKernelSystemMode();
  82. if (system_mode.second != Loader::ResultStatus::Success) {
  83. LOG_CRITICAL(Core, "Failed to determine system mode (Error {})!",
  84. static_cast<int>(system_mode.second));
  85. switch (system_mode.second) {
  86. case Loader::ResultStatus::ErrorMissingKeys:
  87. return ResultStatus::ErrorLoader_ErrorMissingKeys;
  88. case Loader::ResultStatus::ErrorDecrypting:
  89. return ResultStatus::ErrorLoader_ErrorDecrypting;
  90. case Loader::ResultStatus::ErrorInvalidFormat:
  91. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  92. case Loader::ResultStatus::ErrorUnsupportedArch:
  93. return ResultStatus::ErrorUnsupportedArch;
  94. default:
  95. return ResultStatus::ErrorSystemMode;
  96. }
  97. }
  98. ResultStatus init_result{Init(emu_window)};
  99. if (init_result != ResultStatus::Success) {
  100. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  101. static_cast<int>(init_result));
  102. System::Shutdown();
  103. return init_result;
  104. }
  105. const Loader::ResultStatus load_result{app_loader->Load(current_process)};
  106. if (Loader::ResultStatus::Success != load_result) {
  107. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result));
  108. System::Shutdown();
  109. switch (load_result) {
  110. case Loader::ResultStatus::ErrorMissingKeys:
  111. return ResultStatus::ErrorLoader_ErrorMissingKeys;
  112. case Loader::ResultStatus::ErrorDecrypting:
  113. return ResultStatus::ErrorLoader_ErrorDecrypting;
  114. case Loader::ResultStatus::ErrorInvalidFormat:
  115. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  116. case Loader::ResultStatus::ErrorUnsupportedArch:
  117. return ResultStatus::ErrorUnsupportedArch;
  118. default:
  119. return ResultStatus::ErrorLoader;
  120. }
  121. }
  122. status = ResultStatus::Success;
  123. return status;
  124. }
  125. void System::PrepareReschedule() {
  126. CurrentCpuCore().PrepareReschedule();
  127. }
  128. PerfStats::Results System::GetAndResetPerfStats() {
  129. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  130. }
  131. const std::shared_ptr<Kernel::Scheduler>& System::Scheduler(size_t core_index) {
  132. ASSERT(core_index < NUM_CPU_CORES);
  133. return cpu_cores[core_index]->Scheduler();
  134. }
  135. ARM_Interface& System::ArmInterface(size_t core_index) {
  136. ASSERT(core_index < NUM_CPU_CORES);
  137. return cpu_cores[core_index]->ArmInterface();
  138. }
  139. Cpu& System::CpuCore(size_t core_index) {
  140. ASSERT(core_index < NUM_CPU_CORES);
  141. return *cpu_cores[core_index];
  142. }
  143. System::ResultStatus System::Init(EmuWindow& emu_window) {
  144. LOG_DEBUG(HW_Memory, "initialized OK");
  145. CoreTiming::Init();
  146. current_process = Kernel::Process::Create("main");
  147. cpu_barrier = std::make_shared<CpuBarrier>();
  148. cpu_exclusive_monitor = Cpu::MakeExclusiveMonitor(cpu_cores.size());
  149. for (size_t index = 0; index < cpu_cores.size(); ++index) {
  150. cpu_cores[index] = std::make_shared<Cpu>(cpu_exclusive_monitor, cpu_barrier, index);
  151. }
  152. telemetry_session = std::make_unique<Core::TelemetrySession>();
  153. service_manager = std::make_shared<Service::SM::ServiceManager>();
  154. Kernel::Init();
  155. Service::Init(service_manager);
  156. GDBStub::Init();
  157. renderer = VideoCore::CreateRenderer(emu_window);
  158. if (!renderer->Init()) {
  159. return ResultStatus::ErrorVideoCore;
  160. }
  161. gpu_core = std::make_unique<Tegra::GPU>(renderer->Rasterizer());
  162. // Create threads for CPU cores 1-3, and build thread_to_cpu map
  163. // CPU core 0 is run on the main thread
  164. thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
  165. if (Settings::values.use_multi_core) {
  166. for (size_t index = 0; index < cpu_core_threads.size(); ++index) {
  167. cpu_core_threads[index] =
  168. std::make_unique<std::thread>(RunCpuCore, cpu_cores[index + 1]);
  169. thread_to_cpu[cpu_core_threads[index]->get_id()] = cpu_cores[index + 1];
  170. }
  171. }
  172. LOG_DEBUG(Core, "Initialized OK");
  173. // Reset counters and set time origin to current frame
  174. GetAndResetPerfStats();
  175. perf_stats.BeginSystemFrame();
  176. return ResultStatus::Success;
  177. }
  178. void System::Shutdown() {
  179. // Log last frame performance stats
  180. auto perf_results = GetAndResetPerfStats();
  181. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_EmulationSpeed",
  182. perf_results.emulation_speed * 100.0);
  183. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Framerate",
  184. perf_results.game_fps);
  185. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Frametime",
  186. perf_results.frametime * 1000.0);
  187. // Shutdown emulation session
  188. renderer.reset();
  189. GDBStub::Shutdown();
  190. Service::Shutdown();
  191. Kernel::Shutdown();
  192. service_manager.reset();
  193. telemetry_session.reset();
  194. gpu_core.reset();
  195. // Close all CPU/threading state
  196. cpu_barrier->NotifyEnd();
  197. if (Settings::values.use_multi_core) {
  198. for (auto& thread : cpu_core_threads) {
  199. thread->join();
  200. thread.reset();
  201. }
  202. }
  203. thread_to_cpu.clear();
  204. for (auto& cpu_core : cpu_cores) {
  205. cpu_core.reset();
  206. }
  207. cpu_barrier.reset();
  208. // Close core timing
  209. CoreTiming::Shutdown();
  210. // Close app loader
  211. app_loader.reset();
  212. LOG_DEBUG(Core, "Shutdown OK");
  213. }
  214. Service::SM::ServiceManager& System::ServiceManager() {
  215. return *service_manager;
  216. }
  217. const Service::SM::ServiceManager& System::ServiceManager() const {
  218. return *service_manager;
  219. }
  220. } // namespace Core