core.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <memory>
  7. #include <string>
  8. #include <thread>
  9. #include "common/common_types.h"
  10. #include "core/arm/exclusive_monitor.h"
  11. #include "core/core_cpu.h"
  12. #include "core/hle/kernel/object.h"
  13. #include "core/hle/kernel/scheduler.h"
  14. #include "core/loader/loader.h"
  15. #include "core/memory.h"
  16. #include "core/perf_stats.h"
  17. #include "core/telemetry_session.h"
  18. #include "file_sys/vfs_real.h"
  19. #include "hle/service/filesystem/filesystem.h"
  20. #include "video_core/debug_utils/debug_utils.h"
  21. #include "video_core/gpu.h"
  22. class EmuWindow;
  23. class ARM_Interface;
  24. namespace Service::SM {
  25. class ServiceManager;
  26. }
  27. namespace VideoCore {
  28. class RendererBase;
  29. }
  30. namespace Core {
  31. class System {
  32. public:
  33. ~System();
  34. /**
  35. * Gets the instance of the System singleton class.
  36. * @returns Reference to the instance of the System singleton class.
  37. */
  38. static System& GetInstance() {
  39. return s_instance;
  40. }
  41. /// Enumeration representing the return values of the System Initialize and Load process.
  42. enum class ResultStatus : u32 {
  43. Success, ///< Succeeded
  44. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  45. ErrorGetLoader, ///< Error finding the correct application loader
  46. ErrorSystemMode, ///< Error determining the system mode
  47. ErrorLoader, ///< Error loading the specified application
  48. ErrorLoader_ErrorMissingKeys, ///< Error because the key/keys needed to run could not be
  49. ///< found.
  50. ErrorLoader_ErrorDecrypting, ///< Error loading the specified application due to encryption
  51. ErrorLoader_ErrorInvalidFormat, ///< Error loading the specified application due to an
  52. /// invalid format
  53. ErrorSystemFiles, ///< Error in finding system files
  54. ErrorSharedFont, ///< Error in finding shared font
  55. ErrorVideoCore, ///< Error in the video core
  56. ErrorUnsupportedArch, ///< Unsupported Architecture (32-Bit ROMs)
  57. ErrorUnknown ///< Any other error
  58. };
  59. /**
  60. * Run the core CPU loop
  61. * This function runs the core for the specified number of CPU instructions before trying to
  62. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  63. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  64. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  65. * update is requested (e.g. on a thread switch).
  66. * @param tight_loop If false, the CPU single-steps.
  67. * @return Result status, indicating whether or not the operation succeeded.
  68. */
  69. ResultStatus RunLoop(bool tight_loop = true);
  70. /**
  71. * Step the CPU one instruction
  72. * @return Result status, indicating whether or not the operation succeeded.
  73. */
  74. ResultStatus SingleStep();
  75. /**
  76. * Invalidate the CPU instruction caches
  77. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  78. * step/continue commands.
  79. */
  80. void InvalidateCpuInstructionCaches() {
  81. for (auto& cpu : cpu_cores) {
  82. cpu->ArmInterface().ClearInstructionCache();
  83. }
  84. }
  85. /// Shutdown the emulated system.
  86. void Shutdown();
  87. /**
  88. * Load an executable application.
  89. * @param emu_window Reference to the host-system window used for video output and keyboard
  90. * input.
  91. * @param filepath String path to the executable application to load on the host file system.
  92. * @returns ResultStatus code, indicating if the operation succeeded.
  93. */
  94. ResultStatus Load(EmuWindow& emu_window, const std::string& filepath);
  95. /**
  96. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  97. * application).
  98. * @returns True if the emulated system is powered on, otherwise false.
  99. */
  100. bool IsPoweredOn() const {
  101. return cpu_barrier && cpu_barrier->IsAlive();
  102. }
  103. /**
  104. * Returns a reference to the telemetry session for this emulation session.
  105. * @returns Reference to the telemetry session.
  106. */
  107. Core::TelemetrySession& TelemetrySession() const {
  108. return *telemetry_session;
  109. }
  110. /// Prepare the core emulation for a reschedule
  111. void PrepareReschedule();
  112. /// Gets and resets core performance statistics
  113. PerfStats::Results GetAndResetPerfStats();
  114. /// Gets an ARM interface to the CPU core that is currently running
  115. ARM_Interface& CurrentArmInterface() {
  116. return CurrentCpuCore().ArmInterface();
  117. }
  118. /// Gets the index of the currently running CPU core
  119. size_t CurrentCoreIndex() {
  120. return CurrentCpuCore().CoreIndex();
  121. }
  122. /// Gets an ARM interface to the CPU core with the specified index
  123. ARM_Interface& ArmInterface(size_t core_index);
  124. /// Gets a CPU interface to the CPU core with the specified index
  125. Cpu& CpuCore(size_t core_index);
  126. /// Gets a mutable reference to the GPU interface
  127. Tegra::GPU& GPU() {
  128. return *gpu_core;
  129. }
  130. /// Gets an immutable reference to the GPU interface.
  131. const Tegra::GPU& GPU() const {
  132. return *gpu_core;
  133. }
  134. /// Gets a mutable reference to the renderer.
  135. VideoCore::RendererBase& Renderer() {
  136. return *renderer;
  137. }
  138. /// Gets an immutable reference to the renderer.
  139. const VideoCore::RendererBase& Renderer() const {
  140. return *renderer;
  141. }
  142. /// Gets the scheduler for the CPU core that is currently running
  143. Kernel::Scheduler& CurrentScheduler() {
  144. return *CurrentCpuCore().Scheduler();
  145. }
  146. /// Gets the exclusive monitor
  147. ExclusiveMonitor& Monitor() {
  148. return *cpu_exclusive_monitor;
  149. }
  150. /// Gets the scheduler for the CPU core with the specified index
  151. const std::shared_ptr<Kernel::Scheduler>& Scheduler(size_t core_index);
  152. /// Gets the current process
  153. Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
  154. return current_process;
  155. }
  156. PerfStats perf_stats;
  157. FrameLimiter frame_limiter;
  158. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  159. status = new_status;
  160. if (details) {
  161. status_details = details;
  162. }
  163. }
  164. const std::string& GetStatusDetails() const {
  165. return status_details;
  166. }
  167. Loader::AppLoader& GetAppLoader() const {
  168. return *app_loader;
  169. }
  170. Service::SM::ServiceManager& ServiceManager();
  171. const Service::SM::ServiceManager& ServiceManager() const;
  172. void SetGPUDebugContext(std::shared_ptr<Tegra::DebugContext> context) {
  173. debug_context = std::move(context);
  174. }
  175. std::shared_ptr<Tegra::DebugContext> GetGPUDebugContext() const {
  176. return debug_context;
  177. }
  178. void SetFilesystem(FileSys::VirtualFilesystem vfs) {
  179. virtual_filesystem = std::move(vfs);
  180. }
  181. FileSys::VirtualFilesystem GetFilesystem() const {
  182. return virtual_filesystem;
  183. }
  184. private:
  185. System();
  186. /// Returns the currently running CPU core
  187. Cpu& CurrentCpuCore();
  188. /**
  189. * Initialize the emulated system.
  190. * @param emu_window Reference to the host-system window used for video output and keyboard
  191. * input.
  192. * @return ResultStatus code, indicating if the operation succeeded.
  193. */
  194. ResultStatus Init(EmuWindow& emu_window);
  195. /// RealVfsFilesystem instance
  196. FileSys::VirtualFilesystem virtual_filesystem;
  197. /// AppLoader used to load the current executing application
  198. std::unique_ptr<Loader::AppLoader> app_loader;
  199. std::unique_ptr<VideoCore::RendererBase> renderer;
  200. std::unique_ptr<Tegra::GPU> gpu_core;
  201. std::shared_ptr<Tegra::DebugContext> debug_context;
  202. Kernel::SharedPtr<Kernel::Process> current_process;
  203. std::shared_ptr<ExclusiveMonitor> cpu_exclusive_monitor;
  204. std::shared_ptr<CpuBarrier> cpu_barrier;
  205. std::array<std::shared_ptr<Cpu>, NUM_CPU_CORES> cpu_cores;
  206. std::array<std::unique_ptr<std::thread>, NUM_CPU_CORES - 1> cpu_core_threads;
  207. size_t active_core{}; ///< Active core, only used in single thread mode
  208. /// Service manager
  209. std::shared_ptr<Service::SM::ServiceManager> service_manager;
  210. /// Telemetry session for this emulation session
  211. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  212. static System s_instance;
  213. ResultStatus status = ResultStatus::Success;
  214. std::string status_details = "";
  215. /// Map of guest threads to CPU cores
  216. std::map<std::thread::id, std::shared_ptr<Cpu>> thread_to_cpu;
  217. };
  218. inline ARM_Interface& CurrentArmInterface() {
  219. return System::GetInstance().CurrentArmInterface();
  220. }
  221. inline TelemetrySession& Telemetry() {
  222. return System::GetInstance().TelemetrySession();
  223. }
  224. inline Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
  225. return System::GetInstance().CurrentProcess();
  226. }
  227. } // namespace Core