core.h 16 KB

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