core.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. cpu_exclusive_monitor = Cpu::MakeExclusiveMonitor(cpu_cores.size());
  144. for (size_t index = 0; index < cpu_cores.size(); ++index) {
  145. cpu_cores[index] = std::make_shared<Cpu>(cpu_exclusive_monitor, cpu_barrier, index);
  146. }
  147. gpu_core = std::make_unique<Tegra::GPU>();
  148. telemetry_session = std::make_unique<Core::TelemetrySession>();
  149. service_manager = std::make_shared<Service::SM::ServiceManager>();
  150. HW::Init();
  151. Kernel::Init(system_mode);
  152. Service::Init(service_manager);
  153. GDBStub::Init();
  154. if (!VideoCore::Init(emu_window)) {
  155. return ResultStatus::ErrorVideoCore;
  156. }
  157. // Create threads for CPU cores 1-3, and build thread_to_cpu map
  158. // CPU core 0 is run on the main thread
  159. thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
  160. if (Settings::values.use_multi_core) {
  161. for (size_t index = 0; index < cpu_core_threads.size(); ++index) {
  162. cpu_core_threads[index] =
  163. std::make_unique<std::thread>(RunCpuCore, cpu_cores[index + 1]);
  164. thread_to_cpu[cpu_core_threads[index]->get_id()] = cpu_cores[index + 1];
  165. }
  166. }
  167. LOG_DEBUG(Core, "Initialized OK");
  168. // Reset counters and set time origin to current frame
  169. GetAndResetPerfStats();
  170. perf_stats.BeginSystemFrame();
  171. return ResultStatus::Success;
  172. }
  173. void System::Shutdown() {
  174. // Log last frame performance stats
  175. auto perf_results = GetAndResetPerfStats();
  176. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_EmulationSpeed",
  177. perf_results.emulation_speed * 100.0);
  178. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Framerate",
  179. perf_results.game_fps);
  180. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Frametime",
  181. perf_results.frametime * 1000.0);
  182. // Shutdown emulation session
  183. VideoCore::Shutdown();
  184. GDBStub::Shutdown();
  185. Service::Shutdown();
  186. Kernel::Shutdown();
  187. HW::Shutdown();
  188. service_manager.reset();
  189. telemetry_session.reset();
  190. gpu_core.reset();
  191. // Close all CPU/threading state
  192. cpu_barrier->NotifyEnd();
  193. if (Settings::values.use_multi_core) {
  194. for (auto& thread : cpu_core_threads) {
  195. thread->join();
  196. thread.reset();
  197. }
  198. }
  199. thread_to_cpu.clear();
  200. for (auto& cpu_core : cpu_cores) {
  201. cpu_core.reset();
  202. }
  203. cpu_barrier.reset();
  204. // Close core timing
  205. CoreTiming::Shutdown();
  206. // Close app loader
  207. app_loader.reset();
  208. LOG_DEBUG(Core, "Shutdown OK");
  209. }
  210. Service::SM::ServiceManager& System::ServiceManager() {
  211. return *service_manager;
  212. }
  213. const Service::SM::ServiceManager& System::ServiceManager() const {
  214. return *service_manager;
  215. }
  216. } // namespace Core