core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 Core {
  82. class ARM_Interface;
  83. class CpuManager;
  84. class Debugger;
  85. class DeviceMemory;
  86. class ExclusiveMonitor;
  87. class GPUDirtyMemoryManager;
  88. class PerfStats;
  89. class Reporter;
  90. class SpeedLimiter;
  91. class TelemetrySession;
  92. struct PerfStatsResults;
  93. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  94. const std::string& path);
  95. /// Enumeration representing the return values of the System Initialize and Load process.
  96. enum class SystemResultStatus : u32 {
  97. Success, ///< Succeeded
  98. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  99. ErrorGetLoader, ///< Error finding the correct application loader
  100. ErrorSystemFiles, ///< Error in finding system files
  101. ErrorSharedFont, ///< Error in finding shared font
  102. ErrorVideoCore, ///< Error in the video core
  103. ErrorUnknown, ///< Any other error
  104. ErrorLoader, ///< The base for loader errors (too many to repeat)
  105. };
  106. class System {
  107. public:
  108. using CurrentBuildProcessID = std::array<u8, 0x20>;
  109. explicit System();
  110. ~System();
  111. System(const System&) = delete;
  112. System& operator=(const System&) = delete;
  113. System(System&&) = delete;
  114. System& operator=(System&&) = delete;
  115. /**
  116. * Initializes the system
  117. * This function will initialize core functionality used for system emulation
  118. */
  119. void Initialize();
  120. /**
  121. * Run the OS and Application
  122. * This function will start emulation and run the relevant devices
  123. */
  124. void Run();
  125. /**
  126. * Pause the OS and Application
  127. * This function will pause emulation and stop the relevant devices
  128. */
  129. void Pause();
  130. /// Check if the core is currently paused.
  131. [[nodiscard]] bool IsPaused() const;
  132. /**
  133. * Invalidate the CPU instruction caches
  134. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  135. * step/continue commands.
  136. */
  137. void InvalidateCpuInstructionCaches();
  138. void InvalidateCpuInstructionCacheRange(u64 addr, std::size_t size);
  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, u64 program_id = 0,
  165. std::size_t program_index = 0);
  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. /// Provides a reference to the gou dirty memory manager.
  179. [[nodiscard]] Core::GPUDirtyMemoryManager& CurrentGPUDirtyMemoryManager();
  180. /// Provides a constant reference to the current gou dirty memory manager.
  181. [[nodiscard]] const Core::GPUDirtyMemoryManager& CurrentGPUDirtyMemoryManager() const;
  182. void GatherGPUDirtyMemory(std::function<void(VAddr, size_t)>& callback);
  183. [[nodiscard]] size_t GetCurrentHostThreadID() const;
  184. /// Gets and resets core performance statistics
  185. [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
  186. /// Gets an ARM interface to the CPU core that is currently running
  187. [[nodiscard]] ARM_Interface& CurrentArmInterface();
  188. /// Gets an ARM interface to the CPU core that is currently running
  189. [[nodiscard]] const ARM_Interface& CurrentArmInterface() const;
  190. /// Gets the physical core for the CPU core that is currently running
  191. [[nodiscard]] Kernel::PhysicalCore& CurrentPhysicalCore();
  192. /// Gets the physical core for the CPU core that is currently running
  193. [[nodiscard]] const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  194. /// Gets a reference to an ARM interface for the CPU core with the specified index
  195. [[nodiscard]] ARM_Interface& ArmInterface(std::size_t core_index);
  196. /// Gets a const reference to an ARM interface from the CPU core with the specified index
  197. [[nodiscard]] const ARM_Interface& ArmInterface(std::size_t core_index) const;
  198. /// Gets a reference to the underlying CPU manager.
  199. [[nodiscard]] CpuManager& GetCpuManager();
  200. /// Gets a const reference to the underlying CPU manager
  201. [[nodiscard]] const CpuManager& GetCpuManager() const;
  202. /// Gets a reference to the exclusive monitor
  203. [[nodiscard]] ExclusiveMonitor& Monitor();
  204. /// Gets a constant reference to the exclusive monitor
  205. [[nodiscard]] const ExclusiveMonitor& Monitor() const;
  206. /// Gets a mutable reference to the system memory instance.
  207. [[nodiscard]] Core::Memory::Memory& ApplicationMemory();
  208. /// Gets a constant reference to the system memory instance.
  209. [[nodiscard]] const Core::Memory::Memory& ApplicationMemory() const;
  210. /// Gets a mutable reference to the GPU interface
  211. [[nodiscard]] Tegra::GPU& GPU();
  212. /// Gets an immutable reference to the GPU interface.
  213. [[nodiscard]] const Tegra::GPU& GPU() const;
  214. /// Gets a mutable reference to the Host1x interface
  215. [[nodiscard]] Tegra::Host1x::Host1x& Host1x();
  216. /// Gets an immutable reference to the Host1x interface.
  217. [[nodiscard]] const Tegra::Host1x::Host1x& Host1x() const;
  218. /// Gets a mutable reference to the renderer.
  219. [[nodiscard]] VideoCore::RendererBase& Renderer();
  220. /// Gets an immutable reference to the renderer.
  221. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  222. /// Gets a mutable reference to the audio interface
  223. [[nodiscard]] AudioCore::AudioCore& AudioCore();
  224. /// Gets an immutable reference to the audio interface.
  225. [[nodiscard]] const AudioCore::AudioCore& AudioCore() const;
  226. /// Gets the global scheduler
  227. [[nodiscard]] Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  228. /// Gets the global scheduler
  229. [[nodiscard]] const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  230. /// Gets the manager for the guest device memory
  231. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  232. /// Gets the manager for the guest device memory
  233. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  234. /// Provides a pointer to the application process
  235. [[nodiscard]] Kernel::KProcess* ApplicationProcess();
  236. /// Provides a constant pointer to the application process.
  237. [[nodiscard]] const Kernel::KProcess* ApplicationProcess() const;
  238. /// Provides a reference to the core timing instance.
  239. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  240. /// Provides a constant reference to the core timing instance.
  241. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  242. /// Provides a reference to the kernel instance.
  243. [[nodiscard]] Kernel::KernelCore& Kernel();
  244. /// Provides a constant reference to the kernel instance.
  245. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  246. /// Gets a mutable reference to the HID interface.
  247. [[nodiscard]] HID::HIDCore& HIDCore();
  248. /// Gets an immutable reference to the HID interface.
  249. [[nodiscard]] const HID::HIDCore& HIDCore() const;
  250. /// Provides a reference to the internal PerfStats instance.
  251. [[nodiscard]] Core::PerfStats& GetPerfStats();
  252. /// Provides a constant reference to the internal PerfStats instance.
  253. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  254. /// Provides a reference to the speed limiter;
  255. [[nodiscard]] Core::SpeedLimiter& SpeedLimiter();
  256. /// Provides a constant reference to the speed limiter
  257. [[nodiscard]] const Core::SpeedLimiter& SpeedLimiter() const;
  258. [[nodiscard]] u64 GetApplicationProcessProgramID() const;
  259. /// Gets the name of the current game
  260. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  261. void SetStatus(SystemResultStatus new_status, const char* details);
  262. [[nodiscard]] const std::string& GetStatusDetails() const;
  263. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  264. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  265. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  266. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  267. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  268. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  269. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  270. const std::array<u8, 0x20>& build_id, u64 main_region_begin,
  271. u64 main_region_size);
  272. void SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set);
  273. void SetDefaultAppletFrontendSet();
  274. [[nodiscard]] Service::AM::Applets::AppletManager& GetAppletManager();
  275. [[nodiscard]] const Service::AM::Applets::AppletManager& GetAppletManager() const;
  276. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  277. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  278. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  279. [[nodiscard]] FileSys::ContentProviderUnion& GetContentProviderUnion();
  280. [[nodiscard]] const FileSys::ContentProviderUnion& GetContentProviderUnion() const;
  281. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  282. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  283. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  284. FileSys::ContentProvider* provider);
  285. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  286. [[nodiscard]] const Reporter& GetReporter() const;
  287. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  288. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  289. [[nodiscard]] Service::APM::Controller& GetAPMController();
  290. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  291. [[nodiscard]] Service::Time::TimeManager& GetTimeManager();
  292. [[nodiscard]] const Service::Time::TimeManager& GetTimeManager() const;
  293. [[nodiscard]] Core::Debugger& GetDebugger();
  294. [[nodiscard]] const Core::Debugger& GetDebugger() const;
  295. /// Gets a mutable reference to the Room Network.
  296. [[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
  297. /// Gets an immutable reference to the Room Network.
  298. [[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
  299. void SetExitLocked(bool locked);
  300. bool GetExitLocked() const;
  301. void SetExitRequested(bool requested);
  302. bool GetExitRequested() const;
  303. void SetApplicationProcessBuildID(const CurrentBuildProcessID& id);
  304. [[nodiscard]] const CurrentBuildProcessID& GetApplicationProcessBuildID() const;
  305. /// Register a host thread as an emulated CPU Core.
  306. void RegisterCoreThread(std::size_t id);
  307. /// Register a host thread as an auxiliary thread.
  308. void RegisterHostThread();
  309. /// Enter CPU Microprofile
  310. void EnterCPUProfile();
  311. /// Exit CPU Microprofile
  312. void ExitCPUProfile();
  313. /// Tells if system is running on multicore.
  314. [[nodiscard]] bool IsMulticore() const;
  315. /// Tells if the system debugger is enabled.
  316. [[nodiscard]] bool DebuggerEnabled() const;
  317. /// Runs a server instance until shutdown.
  318. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  319. /// Type used for the frontend to designate a callback for System to re-launch the application
  320. /// using a specified program index.
  321. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  322. /**
  323. * Registers a callback from the frontend for System to re-launch the application using a
  324. * specified program index.
  325. * @param callback Callback from the frontend to relaunch the application.
  326. */
  327. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  328. /**
  329. * Instructs the frontend to re-launch the application using the specified program_index.
  330. * @param program_index Specifies the index within the application of the program to launch.
  331. */
  332. void ExecuteProgram(std::size_t program_index);
  333. /**
  334. * Gets a reference to the user channel stack.
  335. * It is used to transfer data between programs.
  336. */
  337. [[nodiscard]] std::deque<std::vector<u8>>& GetUserChannel();
  338. /// Type used for the frontend to designate a callback for System to exit the application.
  339. using ExitCallback = std::function<void()>;
  340. /**
  341. * Registers a callback from the frontend for System to exit the application.
  342. * @param callback Callback from the frontend to exit the application.
  343. */
  344. void RegisterExitCallback(ExitCallback&& callback);
  345. /// Instructs the frontend to exit the application.
  346. void Exit();
  347. /// Applies any changes to settings to this core instance.
  348. void ApplySettings();
  349. private:
  350. struct Impl;
  351. std::unique_ptr<Impl> impl;
  352. };
  353. } // namespace Core