core.cpp 8.2 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/memory_setup.h"
  20. #include "core/settings.h"
  21. #include "file_sys/vfs_real.h"
  22. #include "video_core/video_core.h"
  23. namespace Core {
  24. /*static*/ System System::s_instance;
  25. System::System() = default;
  26. System::~System() = default;
  27. /// Runs a CPU core while the system is powered on
  28. static void RunCpuCore(std::shared_ptr<Cpu> cpu_state) {
  29. while (Core::System::GetInstance().IsPoweredOn()) {
  30. cpu_state->RunLoop(true);
  31. }
  32. }
  33. Cpu& System::CurrentCpuCore() {
  34. // If multicore is enabled, use host thread to figure out the current CPU core
  35. if (Settings::values.use_multi_core) {
  36. const auto& search = thread_to_cpu.find(std::this_thread::get_id());
  37. ASSERT(search != thread_to_cpu.end());
  38. ASSERT(search->second);
  39. return *search->second;
  40. }
  41. // Otherwise, use single-threaded mode active_core variable
  42. return *cpu_cores[active_core];
  43. }
  44. System::ResultStatus System::RunLoop(bool tight_loop) {
  45. status = ResultStatus::Success;
  46. // Update thread_to_cpu in case Core 0 is run from a different host thread
  47. thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
  48. if (GDBStub::IsServerEnabled()) {
  49. GDBStub::HandlePacket();
  50. // If the loop is halted and we want to step, use a tiny (1) number of instructions to
  51. // execute. Otherwise, get out of the loop function.
  52. if (GDBStub::GetCpuHaltFlag()) {
  53. if (GDBStub::GetCpuStepFlag()) {
  54. GDBStub::SetCpuStepFlag(false);
  55. tight_loop = false;
  56. } else {
  57. return ResultStatus::Success;
  58. }
  59. }
  60. }
  61. for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) {
  62. cpu_cores[active_core]->RunLoop(tight_loop);
  63. if (Settings::values.use_multi_core) {
  64. // Cores 1-3 are run on other threads in this mode
  65. break;
  66. }
  67. }
  68. return status;
  69. }
  70. System::ResultStatus System::SingleStep() {
  71. return RunLoop(false);
  72. }
  73. System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
  74. app_loader = Loader::GetLoader(std::make_shared<FileSys::RealVfsFile>(filepath));
  75. if (!app_loader) {
  76. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  77. return ResultStatus::ErrorGetLoader;
  78. }
  79. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  80. app_loader->LoadKernelSystemMode();
  81. if (system_mode.second != Loader::ResultStatus::Success) {
  82. LOG_CRITICAL(Core, "Failed to determine system mode (Error {})!",
  83. static_cast<int>(system_mode.second));
  84. switch (system_mode.second) {
  85. case Loader::ResultStatus::ErrorEncrypted:
  86. return ResultStatus::ErrorLoader_ErrorEncrypted;
  87. case Loader::ResultStatus::ErrorInvalidFormat:
  88. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  89. case Loader::ResultStatus::ErrorUnsupportedArch:
  90. return ResultStatus::ErrorUnsupportedArch;
  91. default:
  92. return ResultStatus::ErrorSystemMode;
  93. }
  94. }
  95. ResultStatus init_result{Init(emu_window, system_mode.first.get())};
  96. if (init_result != ResultStatus::Success) {
  97. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  98. static_cast<int>(init_result));
  99. System::Shutdown();
  100. return init_result;
  101. }
  102. const Loader::ResultStatus load_result{app_loader->Load(current_process)};
  103. if (Loader::ResultStatus::Success != load_result) {
  104. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result));
  105. System::Shutdown();
  106. switch (load_result) {
  107. case Loader::ResultStatus::ErrorEncrypted:
  108. return ResultStatus::ErrorLoader_ErrorEncrypted;
  109. case Loader::ResultStatus::ErrorInvalidFormat:
  110. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  111. case Loader::ResultStatus::ErrorUnsupportedArch:
  112. return ResultStatus::ErrorUnsupportedArch;
  113. default:
  114. return ResultStatus::ErrorLoader;
  115. }
  116. }
  117. status = ResultStatus::Success;
  118. return status;
  119. }
  120. void System::PrepareReschedule() {
  121. CurrentCpuCore().PrepareReschedule();
  122. }
  123. PerfStats::Results System::GetAndResetPerfStats() {
  124. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  125. }
  126. const std::shared_ptr<Kernel::Scheduler>& System::Scheduler(size_t core_index) {
  127. ASSERT(core_index < NUM_CPU_CORES);
  128. return cpu_cores[core_index]->Scheduler();
  129. }
  130. ARM_Interface& System::ArmInterface(size_t core_index) {
  131. ASSERT(core_index < NUM_CPU_CORES);
  132. return cpu_cores[core_index]->ArmInterface();
  133. }
  134. Cpu& System::CpuCore(size_t core_index) {
  135. ASSERT(core_index < NUM_CPU_CORES);
  136. return *cpu_cores[core_index];
  137. }
  138. System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
  139. LOG_DEBUG(HW_Memory, "initialized OK");
  140. CoreTiming::Init();
  141. current_process = Kernel::Process::Create("main");
  142. cpu_barrier = std::make_shared<CpuBarrier>();
  143. for (size_t index = 0; index < cpu_cores.size(); ++index) {
  144. cpu_cores[index] = std::make_shared<Cpu>(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