core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <deque>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <span>
  10. #include <string>
  11. #include <vector>
  12. #include "common/common_types.h"
  13. #include "core/file_sys/vfs/vfs_types.h"
  14. namespace Core::Frontend {
  15. class EmuWindow;
  16. } // namespace Core::Frontend
  17. namespace FileSys {
  18. class ContentProvider;
  19. class ContentProviderUnion;
  20. enum class ContentProviderUnionSlot;
  21. class VfsFilesystem;
  22. } // namespace FileSys
  23. namespace Kernel {
  24. class GlobalSchedulerContext;
  25. class KernelCore;
  26. class PhysicalCore;
  27. class KProcess;
  28. class KScheduler;
  29. } // namespace Kernel
  30. namespace Loader {
  31. class AppLoader;
  32. enum class ResultStatus : u16;
  33. } // namespace Loader
  34. namespace Core::Memory {
  35. struct CheatEntry;
  36. class Memory;
  37. } // namespace Core::Memory
  38. namespace Service {
  39. namespace Account {
  40. class ProfileManager;
  41. } // namespace Account
  42. namespace AM {
  43. struct FrontendAppletParameters;
  44. class AppletManager;
  45. } // namespace AM
  46. namespace AM::Frontend {
  47. struct FrontendAppletSet;
  48. class FrontendAppletHolder;
  49. } // namespace AM::Frontend
  50. namespace APM {
  51. class Controller;
  52. }
  53. namespace FileSystem {
  54. class FileSystemController;
  55. } // namespace FileSystem
  56. namespace Glue {
  57. class ARPManager;
  58. }
  59. class ServerManager;
  60. namespace SM {
  61. class ServiceManager;
  62. } // namespace SM
  63. } // namespace Service
  64. namespace Tegra {
  65. class DebugContext;
  66. class GPU;
  67. namespace Host1x {
  68. class Host1x;
  69. } // namespace Host1x
  70. } // namespace Tegra
  71. namespace VideoCore {
  72. class RendererBase;
  73. } // namespace VideoCore
  74. namespace AudioCore {
  75. class AudioCore;
  76. } // namespace AudioCore
  77. namespace Core::Timing {
  78. class CoreTiming;
  79. }
  80. namespace Core::HID {
  81. class HIDCore;
  82. }
  83. namespace Network {
  84. class RoomNetwork;
  85. }
  86. namespace Tools {
  87. class RenderdocAPI;
  88. }
  89. namespace Core {
  90. class CpuManager;
  91. class Debugger;
  92. class DeviceMemory;
  93. class ExclusiveMonitor;
  94. class GPUDirtyMemoryManager;
  95. class PerfStats;
  96. class Reporter;
  97. class SpeedLimiter;
  98. class TelemetrySession;
  99. struct PerfStatsResults;
  100. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  101. const std::string& path);
  102. /// Enumeration representing the return values of the System Initialize and Load process.
  103. enum class SystemResultStatus : u32 {
  104. Success, ///< Succeeded
  105. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  106. ErrorGetLoader, ///< Error finding the correct application loader
  107. ErrorSystemFiles, ///< Error in finding system files
  108. ErrorSharedFont, ///< Error in finding shared font
  109. ErrorVideoCore, ///< Error in the video core
  110. ErrorUnknown, ///< Any other error
  111. ErrorLoader, ///< The base for loader errors (too many to repeat)
  112. };
  113. class System {
  114. public:
  115. using CurrentBuildProcessID = std::array<u8, 0x20>;
  116. explicit System();
  117. ~System();
  118. System(const System&) = delete;
  119. System& operator=(const System&) = delete;
  120. System(System&&) = delete;
  121. System& operator=(System&&) = delete;
  122. /**
  123. * Initializes the system
  124. * This function will initialize core functionality used for system emulation
  125. */
  126. void Initialize();
  127. /**
  128. * Run the OS and Application
  129. * This function will start emulation and run the relevant devices
  130. */
  131. void Run();
  132. /**
  133. * Pause the OS and Application
  134. * This function will pause emulation and stop the relevant devices
  135. */
  136. void Pause();
  137. /// Check if the core is currently paused.
  138. [[nodiscard]] bool IsPaused() const;
  139. /// Shutdown the main emulated process.
  140. void ShutdownMainProcess();
  141. /// Check if the core is shutting down.
  142. [[nodiscard]] bool IsShuttingDown() const;
  143. /// Set the shutting down state.
  144. void SetShuttingDown(bool shutting_down);
  145. /// Forcibly detach the debugger if it is running.
  146. void DetachDebugger();
  147. std::unique_lock<std::mutex> StallApplication();
  148. void UnstallApplication();
  149. void SetNVDECActive(bool is_nvdec_active);
  150. [[nodiscard]] bool GetNVDECActive();
  151. /**
  152. * Initialize the debugger.
  153. */
  154. void InitializeDebugger();
  155. /**
  156. * Load an executable application.
  157. * @param emu_window Reference to the host-system window used for video output and keyboard
  158. * input.
  159. * @param filepath String path to the executable application to load on the host file system.
  160. * @param program_index Specifies the index within the container of the program to launch.
  161. * @returns SystemResultStatus code, indicating if the operation succeeded.
  162. */
  163. [[nodiscard]] SystemResultStatus Load(Frontend::EmuWindow& emu_window,
  164. const std::string& filepath,
  165. Service::AM::FrontendAppletParameters& params);
  166. /**
  167. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  168. * application).
  169. * @returns True if the emulated system is powered on, otherwise false.
  170. */
  171. [[nodiscard]] bool IsPoweredOn() const;
  172. /// Gets a reference to the telemetry session for this emulation session.
  173. [[nodiscard]] Core::TelemetrySession& TelemetrySession();
  174. /// Gets a reference to the telemetry session for this emulation session.
  175. [[nodiscard]] const Core::TelemetrySession& TelemetrySession() const;
  176. /// Prepare the core emulation for a reschedule
  177. void PrepareReschedule(u32 core_index);
  178. std::span<GPUDirtyMemoryManager> GetGPUDirtyMemoryManager();
  179. void GatherGPUDirtyMemory(std::function<void(PAddr, size_t)>& callback);
  180. [[nodiscard]] size_t GetCurrentHostThreadID() const;
  181. /// Gets and resets core performance statistics
  182. [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
  183. /// Gets the physical core for the CPU core that is currently running
  184. [[nodiscard]] Kernel::PhysicalCore& CurrentPhysicalCore();
  185. /// Gets the physical core for the CPU core that is currently running
  186. [[nodiscard]] const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  187. /// Gets a reference to the underlying CPU manager.
  188. [[nodiscard]] CpuManager& GetCpuManager();
  189. /// Gets a const reference to the underlying CPU manager
  190. [[nodiscard]] const CpuManager& GetCpuManager() const;
  191. /// Gets a mutable reference to the system memory instance.
  192. [[nodiscard]] Core::Memory::Memory& ApplicationMemory();
  193. /// Gets a constant reference to the system memory instance.
  194. [[nodiscard]] const Core::Memory::Memory& ApplicationMemory() const;
  195. /// Gets a mutable reference to the GPU interface
  196. [[nodiscard]] Tegra::GPU& GPU();
  197. /// Gets an immutable reference to the GPU interface.
  198. [[nodiscard]] const Tegra::GPU& GPU() const;
  199. /// Gets a mutable reference to the Host1x interface
  200. [[nodiscard]] Tegra::Host1x::Host1x& Host1x();
  201. /// Gets an immutable reference to the Host1x interface.
  202. [[nodiscard]] const Tegra::Host1x::Host1x& Host1x() const;
  203. /// Gets a mutable reference to the renderer.
  204. [[nodiscard]] VideoCore::RendererBase& Renderer();
  205. /// Gets an immutable reference to the renderer.
  206. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  207. /// Gets a mutable reference to the audio interface
  208. [[nodiscard]] AudioCore::AudioCore& AudioCore();
  209. /// Gets an immutable reference to the audio interface.
  210. [[nodiscard]] const AudioCore::AudioCore& AudioCore() const;
  211. /// Gets the global scheduler
  212. [[nodiscard]] Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  213. /// Gets the global scheduler
  214. [[nodiscard]] const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  215. /// Gets the manager for the guest device memory
  216. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  217. /// Gets the manager for the guest device memory
  218. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  219. /// Provides a pointer to the application process
  220. [[nodiscard]] Kernel::KProcess* ApplicationProcess();
  221. /// Provides a constant pointer to the application process.
  222. [[nodiscard]] const Kernel::KProcess* ApplicationProcess() const;
  223. /// Provides a reference to the core timing instance.
  224. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  225. /// Provides a constant reference to the core timing instance.
  226. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  227. /// Provides a reference to the kernel instance.
  228. [[nodiscard]] Kernel::KernelCore& Kernel();
  229. /// Provides a constant reference to the kernel instance.
  230. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  231. /// Gets a mutable reference to the HID interface.
  232. [[nodiscard]] HID::HIDCore& HIDCore();
  233. /// Gets an immutable reference to the HID interface.
  234. [[nodiscard]] const HID::HIDCore& HIDCore() const;
  235. /// Provides a reference to the internal PerfStats instance.
  236. [[nodiscard]] Core::PerfStats& GetPerfStats();
  237. /// Provides a constant reference to the internal PerfStats instance.
  238. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  239. /// Provides a reference to the speed limiter;
  240. [[nodiscard]] Core::SpeedLimiter& SpeedLimiter();
  241. /// Provides a constant reference to the speed limiter
  242. [[nodiscard]] const Core::SpeedLimiter& SpeedLimiter() const;
  243. [[nodiscard]] u64 GetApplicationProcessProgramID() const;
  244. /// Gets the name of the current game
  245. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  246. void SetStatus(SystemResultStatus new_status, const char* details);
  247. [[nodiscard]] const std::string& GetStatusDetails() const;
  248. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  249. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  250. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  251. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  252. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  253. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  254. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  255. const std::array<u8, 0x20>& build_id, u64 main_region_begin,
  256. u64 main_region_size);
  257. void SetFrontendAppletSet(Service::AM::Frontend::FrontendAppletSet&& set);
  258. [[nodiscard]] Service::AM::Frontend::FrontendAppletHolder& GetFrontendAppletHolder();
  259. [[nodiscard]] const Service::AM::Frontend::FrontendAppletHolder& GetFrontendAppletHolder()
  260. const;
  261. [[nodiscard]] Service::AM::AppletManager& GetAppletManager();
  262. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  263. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  264. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  265. [[nodiscard]] FileSys::ContentProviderUnion& GetContentProviderUnion();
  266. [[nodiscard]] const FileSys::ContentProviderUnion& GetContentProviderUnion() const;
  267. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  268. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  269. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  270. FileSys::ContentProvider* provider);
  271. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  272. [[nodiscard]] const Reporter& GetReporter() const;
  273. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  274. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  275. [[nodiscard]] Service::APM::Controller& GetAPMController();
  276. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  277. [[nodiscard]] Service::Account::ProfileManager& GetProfileManager();
  278. [[nodiscard]] const Service::Account::ProfileManager& GetProfileManager() const;
  279. [[nodiscard]] Core::Debugger& GetDebugger();
  280. [[nodiscard]] const Core::Debugger& GetDebugger() const;
  281. /// Gets a mutable reference to the Room Network.
  282. [[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
  283. /// Gets an immutable reference to the Room Network.
  284. [[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
  285. [[nodiscard]] Tools::RenderdocAPI& GetRenderdocAPI();
  286. void SetExitLocked(bool locked);
  287. bool GetExitLocked() const;
  288. void SetExitRequested(bool requested);
  289. bool GetExitRequested() const;
  290. void SetApplicationProcessBuildID(const CurrentBuildProcessID& id);
  291. [[nodiscard]] const CurrentBuildProcessID& GetApplicationProcessBuildID() const;
  292. /// Register a host thread as an emulated CPU Core.
  293. void RegisterCoreThread(std::size_t id);
  294. /// Register a host thread as an auxiliary thread.
  295. void RegisterHostThread();
  296. /// Enter CPU Microprofile
  297. void EnterCPUProfile();
  298. /// Exit CPU Microprofile
  299. void ExitCPUProfile();
  300. /// Tells if system is running on multicore.
  301. [[nodiscard]] bool IsMulticore() const;
  302. /// Tells if the system debugger is enabled.
  303. [[nodiscard]] bool DebuggerEnabled() const;
  304. /// Runs a server instance until shutdown.
  305. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  306. /// Type used for the frontend to designate a callback for System to re-launch the application
  307. /// using a specified program index.
  308. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  309. /**
  310. * Registers a callback from the frontend for System to re-launch the application using a
  311. * specified program index.
  312. * @param callback Callback from the frontend to relaunch the application.
  313. */
  314. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  315. /**
  316. * Instructs the frontend to re-launch the application using the specified program_index.
  317. * @param program_index Specifies the index within the application of the program to launch.
  318. */
  319. void ExecuteProgram(std::size_t program_index);
  320. /**
  321. * Gets a reference to the user channel stack.
  322. * It is used to transfer data between programs.
  323. */
  324. [[nodiscard]] std::deque<std::vector<u8>>& GetUserChannel();
  325. /// Type used for the frontend to designate a callback for System to exit the application.
  326. using ExitCallback = std::function<void()>;
  327. /**
  328. * Registers a callback from the frontend for System to exit the application.
  329. * @param callback Callback from the frontend to exit the application.
  330. */
  331. void RegisterExitCallback(ExitCallback&& callback);
  332. /// Instructs the frontend to exit the application.
  333. void Exit();
  334. /// Applies any changes to settings to this core instance.
  335. void ApplySettings();
  336. private:
  337. struct Impl;
  338. std::unique_ptr<Impl> impl;
  339. };
  340. } // namespace Core