core.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <atomic>
  5. #include <exception>
  6. #include <memory>
  7. #include <utility>
  8. #include "audio_core/audio_core.h"
  9. #include "common/fs/fs.h"
  10. #include "common/logging/log.h"
  11. #include "common/microprofile.h"
  12. #include "common/settings.h"
  13. #include "common/string_util.h"
  14. #include "core/arm/exclusive_monitor.h"
  15. #include "core/core.h"
  16. #include "core/core_timing.h"
  17. #include "core/cpu_manager.h"
  18. #include "core/debugger/debugger.h"
  19. #include "core/device_memory.h"
  20. #include "core/file_sys/bis_factory.h"
  21. #include "core/file_sys/mode.h"
  22. #include "core/file_sys/patch_manager.h"
  23. #include "core/file_sys/registered_cache.h"
  24. #include "core/file_sys/romfs_factory.h"
  25. #include "core/file_sys/savedata_factory.h"
  26. #include "core/file_sys/vfs_concat.h"
  27. #include "core/file_sys/vfs_real.h"
  28. #include "core/hid/hid_core.h"
  29. #include "core/hle/kernel/k_memory_manager.h"
  30. #include "core/hle/kernel/k_process.h"
  31. #include "core/hle/kernel/k_resource_limit.h"
  32. #include "core/hle/kernel/k_scheduler.h"
  33. #include "core/hle/kernel/kernel.h"
  34. #include "core/hle/kernel/physical_core.h"
  35. #include "core/hle/service/am/applets/applets.h"
  36. #include "core/hle/service/apm/apm_controller.h"
  37. #include "core/hle/service/filesystem/filesystem.h"
  38. #include "core/hle/service/glue/glue_manager.h"
  39. #include "core/hle/service/service.h"
  40. #include "core/hle/service/sm/sm.h"
  41. #include "core/hle/service/time/time_manager.h"
  42. #include "core/internal_network/network.h"
  43. #include "core/loader/loader.h"
  44. #include "core/memory.h"
  45. #include "core/memory/cheat_engine.h"
  46. #include "core/perf_stats.h"
  47. #include "core/reporter.h"
  48. #include "core/telemetry_session.h"
  49. #include "core/tools/freezer.h"
  50. #include "network/network.h"
  51. #include "video_core/host1x/host1x.h"
  52. #include "video_core/renderer_base.h"
  53. #include "video_core/video_core.h"
  54. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU0, "ARM JIT", "Dynarmic CPU 0", MP_RGB(255, 64, 64));
  55. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU1, "ARM JIT", "Dynarmic CPU 1", MP_RGB(255, 64, 64));
  56. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU2, "ARM JIT", "Dynarmic CPU 2", MP_RGB(255, 64, 64));
  57. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU3, "ARM JIT", "Dynarmic CPU 3", MP_RGB(255, 64, 64));
  58. namespace Core {
  59. namespace {
  60. FileSys::StorageId GetStorageIdForFrontendSlot(
  61. std::optional<FileSys::ContentProviderUnionSlot> slot) {
  62. if (!slot.has_value()) {
  63. return FileSys::StorageId::None;
  64. }
  65. switch (*slot) {
  66. case FileSys::ContentProviderUnionSlot::UserNAND:
  67. return FileSys::StorageId::NandUser;
  68. case FileSys::ContentProviderUnionSlot::SysNAND:
  69. return FileSys::StorageId::NandSystem;
  70. case FileSys::ContentProviderUnionSlot::SDMC:
  71. return FileSys::StorageId::SdCard;
  72. case FileSys::ContentProviderUnionSlot::FrontendManual:
  73. return FileSys::StorageId::Host;
  74. default:
  75. return FileSys::StorageId::None;
  76. }
  77. }
  78. } // Anonymous namespace
  79. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  80. const std::string& path) {
  81. // To account for split 00+01+etc files.
  82. std::string dir_name;
  83. std::string filename;
  84. Common::SplitPath(path, &dir_name, &filename, nullptr);
  85. if (filename == "00") {
  86. const auto dir = vfs->OpenDirectory(dir_name, FileSys::Mode::Read);
  87. std::vector<FileSys::VirtualFile> concat;
  88. for (u32 i = 0; i < 0x10; ++i) {
  89. const auto file_name = fmt::format("{:02X}", i);
  90. auto next = dir->GetFile(file_name);
  91. if (next != nullptr) {
  92. concat.push_back(std::move(next));
  93. } else {
  94. next = dir->GetFile(file_name);
  95. if (next == nullptr) {
  96. break;
  97. }
  98. concat.push_back(std::move(next));
  99. }
  100. }
  101. if (concat.empty()) {
  102. return nullptr;
  103. }
  104. return FileSys::ConcatenatedVfsFile::MakeConcatenatedFile(std::move(concat),
  105. dir->GetName());
  106. }
  107. if (Common::FS::IsDir(path)) {
  108. return vfs->OpenFile(path + "/main", FileSys::Mode::Read);
  109. }
  110. return vfs->OpenFile(path, FileSys::Mode::Read);
  111. }
  112. struct System::Impl {
  113. explicit Impl(System& system)
  114. : kernel{system}, fs_controller{system}, memory{system}, hid_core{}, room_network{},
  115. cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system} {}
  116. void Initialize(System& system) {
  117. device_memory = std::make_unique<Core::DeviceMemory>();
  118. is_multicore = Settings::values.use_multi_core.GetValue();
  119. extended_memory_layout = Settings::values.use_extended_memory_layout.GetValue();
  120. core_timing.SetMulticore(is_multicore);
  121. core_timing.Initialize([&system]() { system.RegisterHostThread(); });
  122. const auto posix_time = std::chrono::system_clock::now().time_since_epoch();
  123. const auto current_time =
  124. std::chrono::duration_cast<std::chrono::seconds>(posix_time).count();
  125. Settings::values.custom_rtc_differential =
  126. Settings::values.custom_rtc.value_or(current_time) - current_time;
  127. // Create a default fs if one doesn't already exist.
  128. if (virtual_filesystem == nullptr) {
  129. virtual_filesystem = std::make_shared<FileSys::RealVfsFilesystem>();
  130. }
  131. if (content_provider == nullptr) {
  132. content_provider = std::make_unique<FileSys::ContentProviderUnion>();
  133. }
  134. // Create default implementations of applets if one is not provided.
  135. applet_manager.SetDefaultAppletsIfMissing();
  136. is_async_gpu = Settings::values.use_asynchronous_gpu_emulation.GetValue();
  137. kernel.SetMulticore(is_multicore);
  138. cpu_manager.SetMulticore(is_multicore);
  139. cpu_manager.SetAsyncGpu(is_async_gpu);
  140. }
  141. void ReinitializeIfNecessary(System& system) {
  142. const bool must_reinitialize =
  143. is_multicore != Settings::values.use_multi_core.GetValue() ||
  144. extended_memory_layout != Settings::values.use_extended_memory_layout.GetValue();
  145. if (!must_reinitialize) {
  146. return;
  147. }
  148. LOG_DEBUG(Kernel, "Re-initializing");
  149. is_multicore = Settings::values.use_multi_core.GetValue();
  150. extended_memory_layout = Settings::values.use_extended_memory_layout.GetValue();
  151. Initialize(system);
  152. }
  153. void Run() {
  154. std::unique_lock<std::mutex> lk(suspend_guard);
  155. kernel.Suspend(false);
  156. core_timing.SyncPause(false);
  157. is_paused.store(false, std::memory_order_relaxed);
  158. }
  159. void Pause() {
  160. std::unique_lock<std::mutex> lk(suspend_guard);
  161. core_timing.SyncPause(true);
  162. kernel.Suspend(true);
  163. is_paused.store(true, std::memory_order_relaxed);
  164. }
  165. bool IsPaused() const {
  166. return is_paused.load(std::memory_order_relaxed);
  167. }
  168. std::unique_lock<std::mutex> StallProcesses() {
  169. std::unique_lock<std::mutex> lk(suspend_guard);
  170. kernel.Suspend(true);
  171. core_timing.SyncPause(true);
  172. return lk;
  173. }
  174. void UnstallProcesses() {
  175. if (!IsPaused()) {
  176. core_timing.SyncPause(false);
  177. kernel.Suspend(false);
  178. }
  179. }
  180. void InitializeDebugger(System& system, u16 port) {
  181. debugger = std::make_unique<Debugger>(system, port);
  182. }
  183. SystemResultStatus SetupForMainProcess(System& system, Frontend::EmuWindow& emu_window) {
  184. LOG_DEBUG(Core, "initialized OK");
  185. // Setting changes may require a full system reinitialization (e.g., disabling multicore).
  186. ReinitializeIfNecessary(system);
  187. kernel.Initialize();
  188. cpu_manager.Initialize();
  189. /// Reset all glue registrations
  190. arp_manager.ResetAll();
  191. telemetry_session = std::make_unique<Core::TelemetrySession>();
  192. host1x_core = std::make_unique<Tegra::Host1x::Host1x>(system);
  193. gpu_core = VideoCore::CreateGPU(emu_window, system);
  194. if (!gpu_core) {
  195. return SystemResultStatus::ErrorVideoCore;
  196. }
  197. audio_core = std::make_unique<AudioCore::AudioCore>(system);
  198. service_manager = std::make_shared<Service::SM::ServiceManager>(kernel);
  199. services = std::make_unique<Service::Services>(service_manager, system);
  200. // Initialize time manager, which must happen after kernel is created
  201. time_manager.Initialize();
  202. is_powered_on = true;
  203. exit_lock = false;
  204. microprofile_dynarmic[0] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU0);
  205. microprofile_dynarmic[1] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU1);
  206. microprofile_dynarmic[2] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU2);
  207. microprofile_dynarmic[3] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU3);
  208. LOG_DEBUG(Core, "Initialized OK");
  209. return SystemResultStatus::Success;
  210. }
  211. SystemResultStatus Load(System& system, Frontend::EmuWindow& emu_window,
  212. const std::string& filepath, u64 program_id,
  213. std::size_t program_index) {
  214. app_loader = Loader::GetLoader(system, GetGameFileFromPath(virtual_filesystem, filepath),
  215. program_id, program_index);
  216. if (!app_loader) {
  217. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  218. return SystemResultStatus::ErrorGetLoader;
  219. }
  220. SystemResultStatus init_result{SetupForMainProcess(system, emu_window)};
  221. if (init_result != SystemResultStatus::Success) {
  222. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  223. static_cast<int>(init_result));
  224. ShutdownMainProcess();
  225. return init_result;
  226. }
  227. telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider);
  228. // Create a resource limit for the process.
  229. const auto physical_memory_size =
  230. kernel.MemoryManager().GetSize(Kernel::KMemoryManager::Pool::Application);
  231. auto* resource_limit = Kernel::CreateResourceLimitForProcess(system, physical_memory_size);
  232. // Create the process.
  233. auto main_process = Kernel::KProcess::Create(system.Kernel());
  234. ASSERT(Kernel::KProcess::Initialize(main_process, system, "main",
  235. Kernel::KProcess::ProcessType::Userland, resource_limit)
  236. .IsSuccess());
  237. const auto [load_result, load_parameters] = app_loader->Load(*main_process, system);
  238. if (load_result != Loader::ResultStatus::Success) {
  239. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", load_result);
  240. ShutdownMainProcess();
  241. return static_cast<SystemResultStatus>(
  242. static_cast<u32>(SystemResultStatus::ErrorLoader) + static_cast<u32>(load_result));
  243. }
  244. AddGlueRegistrationForProcess(*app_loader, *main_process);
  245. kernel.MakeCurrentProcess(main_process);
  246. kernel.InitializeCores();
  247. // Initialize cheat engine
  248. if (cheat_engine) {
  249. cheat_engine->Initialize();
  250. }
  251. // All threads are started, begin main process execution, now that we're in the clear.
  252. main_process->Run(load_parameters->main_thread_priority,
  253. load_parameters->main_thread_stack_size);
  254. if (Settings::values.gamecard_inserted) {
  255. if (Settings::values.gamecard_current_game) {
  256. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, filepath));
  257. } else if (!Settings::values.gamecard_path.GetValue().empty()) {
  258. const auto& gamecard_path = Settings::values.gamecard_path.GetValue();
  259. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, gamecard_path));
  260. }
  261. }
  262. if (app_loader->ReadProgramId(program_id) != Loader::ResultStatus::Success) {
  263. LOG_ERROR(Core, "Failed to find title id for ROM (Error {})", load_result);
  264. }
  265. perf_stats = std::make_unique<PerfStats>(program_id);
  266. // Reset counters and set time origin to current frame
  267. GetAndResetPerfStats();
  268. perf_stats->BeginSystemFrame();
  269. std::string name = "Unknown Game";
  270. if (app_loader->ReadTitle(name) != Loader::ResultStatus::Success) {
  271. LOG_ERROR(Core, "Failed to read title for ROM (Error {})", load_result);
  272. }
  273. std::string title_version;
  274. const FileSys::PatchManager pm(program_id, system.GetFileSystemController(),
  275. system.GetContentProvider());
  276. const auto metadata = pm.GetControlMetadata();
  277. if (metadata.first != nullptr) {
  278. title_version = metadata.first->GetVersionString();
  279. }
  280. if (auto room_member = room_network.GetRoomMember().lock()) {
  281. Network::GameInfo game_info;
  282. game_info.name = name;
  283. game_info.id = program_id;
  284. game_info.version = title_version;
  285. room_member->SendGameInfo(game_info);
  286. }
  287. status = SystemResultStatus::Success;
  288. return status;
  289. }
  290. void ShutdownMainProcess() {
  291. SetShuttingDown(true);
  292. // Log last frame performance stats if game was loded
  293. if (perf_stats) {
  294. const auto perf_results = GetAndResetPerfStats();
  295. constexpr auto performance = Common::Telemetry::FieldType::Performance;
  296. telemetry_session->AddField(performance, "Shutdown_EmulationSpeed",
  297. perf_results.emulation_speed * 100.0);
  298. telemetry_session->AddField(performance, "Shutdown_Framerate",
  299. perf_results.average_game_fps);
  300. telemetry_session->AddField(performance, "Shutdown_Frametime",
  301. perf_results.frametime * 1000.0);
  302. telemetry_session->AddField(performance, "Mean_Frametime_MS",
  303. perf_stats->GetMeanFrametime());
  304. }
  305. is_powered_on = false;
  306. exit_lock = false;
  307. if (gpu_core != nullptr) {
  308. gpu_core->NotifyShutdown();
  309. }
  310. kernel.ShutdownCores();
  311. cpu_manager.Shutdown();
  312. debugger.reset();
  313. if (services) {
  314. services->KillNVNFlinger();
  315. }
  316. kernel.CloseServices();
  317. services.reset();
  318. service_manager.reset();
  319. cheat_engine.reset();
  320. telemetry_session.reset();
  321. time_manager.Shutdown();
  322. core_timing.ClearPendingEvents();
  323. app_loader.reset();
  324. audio_core.reset();
  325. gpu_core.reset();
  326. host1x_core.reset();
  327. perf_stats.reset();
  328. kernel.Shutdown();
  329. memory.Reset();
  330. if (auto room_member = room_network.GetRoomMember().lock()) {
  331. Network::GameInfo game_info{};
  332. room_member->SendGameInfo(game_info);
  333. }
  334. LOG_DEBUG(Core, "Shutdown OK");
  335. }
  336. bool IsShuttingDown() const {
  337. return is_shutting_down;
  338. }
  339. void SetShuttingDown(bool shutting_down) {
  340. is_shutting_down = shutting_down;
  341. }
  342. Loader::ResultStatus GetGameName(std::string& out) const {
  343. if (app_loader == nullptr)
  344. return Loader::ResultStatus::ErrorNotInitialized;
  345. return app_loader->ReadTitle(out);
  346. }
  347. void AddGlueRegistrationForProcess(Loader::AppLoader& loader, Kernel::KProcess& process) {
  348. std::vector<u8> nacp_data;
  349. FileSys::NACP nacp;
  350. if (loader.ReadControlData(nacp) == Loader::ResultStatus::Success) {
  351. nacp_data = nacp.GetRawBytes();
  352. } else {
  353. nacp_data.resize(sizeof(FileSys::RawNACP));
  354. }
  355. Service::Glue::ApplicationLaunchProperty launch{};
  356. launch.title_id = process.GetProgramID();
  357. FileSys::PatchManager pm{launch.title_id, fs_controller, *content_provider};
  358. launch.version = pm.GetGameVersion().value_or(0);
  359. // TODO(DarkLordZach): When FSController/Game Card Support is added, if
  360. // current_process_game_card use correct StorageId
  361. launch.base_game_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  362. launch.title_id, FileSys::ContentRecordType::Program));
  363. launch.update_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  364. FileSys::GetUpdateTitleID(launch.title_id), FileSys::ContentRecordType::Program));
  365. arp_manager.Register(launch.title_id, launch, std::move(nacp_data));
  366. }
  367. void SetStatus(SystemResultStatus new_status, const char* details = nullptr) {
  368. status = new_status;
  369. if (details) {
  370. status_details = details;
  371. }
  372. }
  373. PerfStatsResults GetAndResetPerfStats() {
  374. return perf_stats->GetAndResetStats(core_timing.GetGlobalTimeUs());
  375. }
  376. mutable std::mutex suspend_guard;
  377. std::atomic_bool is_paused{};
  378. std::atomic<bool> is_shutting_down{};
  379. Timing::CoreTiming core_timing;
  380. Kernel::KernelCore kernel;
  381. /// RealVfsFilesystem instance
  382. FileSys::VirtualFilesystem virtual_filesystem;
  383. /// ContentProviderUnion instance
  384. std::unique_ptr<FileSys::ContentProviderUnion> content_provider;
  385. Service::FileSystem::FileSystemController fs_controller;
  386. /// AppLoader used to load the current executing application
  387. std::unique_ptr<Loader::AppLoader> app_loader;
  388. std::unique_ptr<Tegra::GPU> gpu_core;
  389. std::unique_ptr<Tegra::Host1x::Host1x> host1x_core;
  390. std::unique_ptr<Core::DeviceMemory> device_memory;
  391. std::unique_ptr<AudioCore::AudioCore> audio_core;
  392. Core::Memory::Memory memory;
  393. Core::HID::HIDCore hid_core;
  394. Network::RoomNetwork room_network;
  395. CpuManager cpu_manager;
  396. std::atomic_bool is_powered_on{};
  397. bool exit_lock = false;
  398. Reporter reporter;
  399. std::unique_ptr<Memory::CheatEngine> cheat_engine;
  400. std::unique_ptr<Tools::Freezer> memory_freezer;
  401. std::array<u8, 0x20> build_id{};
  402. /// Frontend applets
  403. Service::AM::Applets::AppletManager applet_manager;
  404. /// APM (Performance) services
  405. Service::APM::Controller apm_controller{core_timing};
  406. /// Service State
  407. Service::Glue::ARPManager arp_manager;
  408. Service::Time::TimeManager time_manager;
  409. /// Service manager
  410. std::shared_ptr<Service::SM::ServiceManager> service_manager;
  411. /// Services
  412. std::unique_ptr<Service::Services> services;
  413. /// Telemetry session for this emulation session
  414. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  415. /// Network instance
  416. Network::NetworkInstance network_instance;
  417. /// Debugger
  418. std::unique_ptr<Core::Debugger> debugger;
  419. SystemResultStatus status = SystemResultStatus::Success;
  420. std::string status_details = "";
  421. std::unique_ptr<Core::PerfStats> perf_stats;
  422. Core::SpeedLimiter speed_limiter;
  423. bool is_multicore{};
  424. bool is_async_gpu{};
  425. bool extended_memory_layout{};
  426. ExecuteProgramCallback execute_program_callback;
  427. ExitCallback exit_callback;
  428. std::array<u64, Core::Hardware::NUM_CPU_CORES> dynarmic_ticks{};
  429. std::array<MicroProfileToken, Core::Hardware::NUM_CPU_CORES> microprofile_dynarmic{};
  430. };
  431. System::System() : impl{std::make_unique<Impl>(*this)} {}
  432. System::~System() = default;
  433. CpuManager& System::GetCpuManager() {
  434. return impl->cpu_manager;
  435. }
  436. const CpuManager& System::GetCpuManager() const {
  437. return impl->cpu_manager;
  438. }
  439. void System::Initialize() {
  440. impl->Initialize(*this);
  441. }
  442. void System::Run() {
  443. impl->Run();
  444. }
  445. void System::Pause() {
  446. impl->Pause();
  447. }
  448. bool System::IsPaused() const {
  449. return impl->IsPaused();
  450. }
  451. void System::InvalidateCpuInstructionCaches() {
  452. impl->kernel.InvalidateAllInstructionCaches();
  453. }
  454. void System::InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size) {
  455. impl->kernel.InvalidateCpuInstructionCacheRange(addr, size);
  456. }
  457. void System::ShutdownMainProcess() {
  458. impl->ShutdownMainProcess();
  459. }
  460. bool System::IsShuttingDown() const {
  461. return impl->IsShuttingDown();
  462. }
  463. void System::SetShuttingDown(bool shutting_down) {
  464. impl->SetShuttingDown(shutting_down);
  465. }
  466. void System::DetachDebugger() {
  467. if (impl->debugger) {
  468. impl->debugger->NotifyShutdown();
  469. }
  470. }
  471. std::unique_lock<std::mutex> System::StallProcesses() {
  472. return impl->StallProcesses();
  473. }
  474. void System::UnstallProcesses() {
  475. impl->UnstallProcesses();
  476. }
  477. void System::InitializeDebugger() {
  478. impl->InitializeDebugger(*this, Settings::values.gdbstub_port.GetValue());
  479. }
  480. SystemResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::string& filepath,
  481. u64 program_id, std::size_t program_index) {
  482. return impl->Load(*this, emu_window, filepath, program_id, program_index);
  483. }
  484. bool System::IsPoweredOn() const {
  485. return impl->is_powered_on.load(std::memory_order::relaxed);
  486. }
  487. void System::PrepareReschedule(const u32 core_index) {
  488. impl->kernel.PrepareReschedule(core_index);
  489. }
  490. PerfStatsResults System::GetAndResetPerfStats() {
  491. return impl->GetAndResetPerfStats();
  492. }
  493. TelemetrySession& System::TelemetrySession() {
  494. return *impl->telemetry_session;
  495. }
  496. const TelemetrySession& System::TelemetrySession() const {
  497. return *impl->telemetry_session;
  498. }
  499. ARM_Interface& System::CurrentArmInterface() {
  500. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  501. }
  502. const ARM_Interface& System::CurrentArmInterface() const {
  503. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  504. }
  505. Kernel::PhysicalCore& System::CurrentPhysicalCore() {
  506. return impl->kernel.CurrentPhysicalCore();
  507. }
  508. const Kernel::PhysicalCore& System::CurrentPhysicalCore() const {
  509. return impl->kernel.CurrentPhysicalCore();
  510. }
  511. /// Gets the global scheduler
  512. Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() {
  513. return impl->kernel.GlobalSchedulerContext();
  514. }
  515. /// Gets the global scheduler
  516. const Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() const {
  517. return impl->kernel.GlobalSchedulerContext();
  518. }
  519. Kernel::KProcess* System::CurrentProcess() {
  520. return impl->kernel.CurrentProcess();
  521. }
  522. Core::DeviceMemory& System::DeviceMemory() {
  523. return *impl->device_memory;
  524. }
  525. const Core::DeviceMemory& System::DeviceMemory() const {
  526. return *impl->device_memory;
  527. }
  528. const Kernel::KProcess* System::CurrentProcess() const {
  529. return impl->kernel.CurrentProcess();
  530. }
  531. ARM_Interface& System::ArmInterface(std::size_t core_index) {
  532. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  533. }
  534. const ARM_Interface& System::ArmInterface(std::size_t core_index) const {
  535. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  536. }
  537. ExclusiveMonitor& System::Monitor() {
  538. return impl->kernel.GetExclusiveMonitor();
  539. }
  540. const ExclusiveMonitor& System::Monitor() const {
  541. return impl->kernel.GetExclusiveMonitor();
  542. }
  543. Memory::Memory& System::Memory() {
  544. return impl->memory;
  545. }
  546. const Core::Memory::Memory& System::Memory() const {
  547. return impl->memory;
  548. }
  549. Tegra::GPU& System::GPU() {
  550. return *impl->gpu_core;
  551. }
  552. const Tegra::GPU& System::GPU() const {
  553. return *impl->gpu_core;
  554. }
  555. Tegra::Host1x::Host1x& System::Host1x() {
  556. return *impl->host1x_core;
  557. }
  558. const Tegra::Host1x::Host1x& System::Host1x() const {
  559. return *impl->host1x_core;
  560. }
  561. VideoCore::RendererBase& System::Renderer() {
  562. return impl->gpu_core->Renderer();
  563. }
  564. const VideoCore::RendererBase& System::Renderer() const {
  565. return impl->gpu_core->Renderer();
  566. }
  567. Kernel::KernelCore& System::Kernel() {
  568. return impl->kernel;
  569. }
  570. const Kernel::KernelCore& System::Kernel() const {
  571. return impl->kernel;
  572. }
  573. HID::HIDCore& System::HIDCore() {
  574. return impl->hid_core;
  575. }
  576. const HID::HIDCore& System::HIDCore() const {
  577. return impl->hid_core;
  578. }
  579. AudioCore::AudioCore& System::AudioCore() {
  580. return *impl->audio_core;
  581. }
  582. const AudioCore::AudioCore& System::AudioCore() const {
  583. return *impl->audio_core;
  584. }
  585. Timing::CoreTiming& System::CoreTiming() {
  586. return impl->core_timing;
  587. }
  588. const Timing::CoreTiming& System::CoreTiming() const {
  589. return impl->core_timing;
  590. }
  591. Core::PerfStats& System::GetPerfStats() {
  592. return *impl->perf_stats;
  593. }
  594. const Core::PerfStats& System::GetPerfStats() const {
  595. return *impl->perf_stats;
  596. }
  597. Core::SpeedLimiter& System::SpeedLimiter() {
  598. return impl->speed_limiter;
  599. }
  600. const Core::SpeedLimiter& System::SpeedLimiter() const {
  601. return impl->speed_limiter;
  602. }
  603. u64 System::GetCurrentProcessProgramID() const {
  604. return impl->kernel.CurrentProcess()->GetProgramID();
  605. }
  606. Loader::ResultStatus System::GetGameName(std::string& out) const {
  607. return impl->GetGameName(out);
  608. }
  609. void System::SetStatus(SystemResultStatus new_status, const char* details) {
  610. impl->SetStatus(new_status, details);
  611. }
  612. const std::string& System::GetStatusDetails() const {
  613. return impl->status_details;
  614. }
  615. Loader::AppLoader& System::GetAppLoader() {
  616. return *impl->app_loader;
  617. }
  618. const Loader::AppLoader& System::GetAppLoader() const {
  619. return *impl->app_loader;
  620. }
  621. void System::SetFilesystem(FileSys::VirtualFilesystem vfs) {
  622. impl->virtual_filesystem = std::move(vfs);
  623. }
  624. FileSys::VirtualFilesystem System::GetFilesystem() const {
  625. return impl->virtual_filesystem;
  626. }
  627. void System::RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  628. const std::array<u8, 32>& build_id, VAddr main_region_begin,
  629. u64 main_region_size) {
  630. impl->cheat_engine = std::make_unique<Memory::CheatEngine>(*this, list, build_id);
  631. impl->cheat_engine->SetMainMemoryParameters(main_region_begin, main_region_size);
  632. }
  633. void System::SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set) {
  634. impl->applet_manager.SetAppletFrontendSet(std::move(set));
  635. }
  636. void System::SetDefaultAppletFrontendSet() {
  637. impl->applet_manager.SetDefaultAppletFrontendSet();
  638. }
  639. Service::AM::Applets::AppletManager& System::GetAppletManager() {
  640. return impl->applet_manager;
  641. }
  642. const Service::AM::Applets::AppletManager& System::GetAppletManager() const {
  643. return impl->applet_manager;
  644. }
  645. void System::SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider) {
  646. impl->content_provider = std::move(provider);
  647. }
  648. FileSys::ContentProvider& System::GetContentProvider() {
  649. return *impl->content_provider;
  650. }
  651. const FileSys::ContentProvider& System::GetContentProvider() const {
  652. return *impl->content_provider;
  653. }
  654. Service::FileSystem::FileSystemController& System::GetFileSystemController() {
  655. return impl->fs_controller;
  656. }
  657. const Service::FileSystem::FileSystemController& System::GetFileSystemController() const {
  658. return impl->fs_controller;
  659. }
  660. void System::RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  661. FileSys::ContentProvider* provider) {
  662. impl->content_provider->SetSlot(slot, provider);
  663. }
  664. void System::ClearContentProvider(FileSys::ContentProviderUnionSlot slot) {
  665. impl->content_provider->ClearSlot(slot);
  666. }
  667. const Reporter& System::GetReporter() const {
  668. return impl->reporter;
  669. }
  670. Service::Glue::ARPManager& System::GetARPManager() {
  671. return impl->arp_manager;
  672. }
  673. const Service::Glue::ARPManager& System::GetARPManager() const {
  674. return impl->arp_manager;
  675. }
  676. Service::APM::Controller& System::GetAPMController() {
  677. return impl->apm_controller;
  678. }
  679. const Service::APM::Controller& System::GetAPMController() const {
  680. return impl->apm_controller;
  681. }
  682. Service::Time::TimeManager& System::GetTimeManager() {
  683. return impl->time_manager;
  684. }
  685. const Service::Time::TimeManager& System::GetTimeManager() const {
  686. return impl->time_manager;
  687. }
  688. void System::SetExitLock(bool locked) {
  689. impl->exit_lock = locked;
  690. }
  691. bool System::GetExitLock() const {
  692. return impl->exit_lock;
  693. }
  694. void System::SetCurrentProcessBuildID(const CurrentBuildProcessID& id) {
  695. impl->build_id = id;
  696. }
  697. const System::CurrentBuildProcessID& System::GetCurrentProcessBuildID() const {
  698. return impl->build_id;
  699. }
  700. Service::SM::ServiceManager& System::ServiceManager() {
  701. return *impl->service_manager;
  702. }
  703. const Service::SM::ServiceManager& System::ServiceManager() const {
  704. return *impl->service_manager;
  705. }
  706. void System::RegisterCoreThread(std::size_t id) {
  707. impl->kernel.RegisterCoreThread(id);
  708. }
  709. void System::RegisterHostThread() {
  710. impl->kernel.RegisterHostThread();
  711. }
  712. void System::EnterDynarmicProfile() {
  713. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  714. impl->dynarmic_ticks[core] = MicroProfileEnter(impl->microprofile_dynarmic[core]);
  715. }
  716. void System::ExitDynarmicProfile() {
  717. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  718. MicroProfileLeave(impl->microprofile_dynarmic[core], impl->dynarmic_ticks[core]);
  719. }
  720. bool System::IsMulticore() const {
  721. return impl->is_multicore;
  722. }
  723. bool System::DebuggerEnabled() const {
  724. return Settings::values.use_gdbstub.GetValue();
  725. }
  726. Core::Debugger& System::GetDebugger() {
  727. return *impl->debugger;
  728. }
  729. const Core::Debugger& System::GetDebugger() const {
  730. return *impl->debugger;
  731. }
  732. Network::RoomNetwork& System::GetRoomNetwork() {
  733. return impl->room_network;
  734. }
  735. const Network::RoomNetwork& System::GetRoomNetwork() const {
  736. return impl->room_network;
  737. }
  738. void System::RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback) {
  739. impl->execute_program_callback = std::move(callback);
  740. }
  741. void System::ExecuteProgram(std::size_t program_index) {
  742. if (impl->execute_program_callback) {
  743. impl->execute_program_callback(program_index);
  744. } else {
  745. LOG_CRITICAL(Core, "execute_program_callback must be initialized by the frontend");
  746. }
  747. }
  748. void System::RegisterExitCallback(ExitCallback&& callback) {
  749. impl->exit_callback = std::move(callback);
  750. }
  751. void System::Exit() {
  752. if (impl->exit_callback) {
  753. impl->exit_callback();
  754. } else {
  755. LOG_CRITICAL(Core, "exit_callback must be initialized by the frontend");
  756. }
  757. }
  758. void System::ApplySettings() {
  759. if (IsPoweredOn()) {
  760. Renderer().RefreshBaseSettings();
  761. }
  762. }
  763. } // namespace Core