core.h 8.5 KB

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