kernel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <functional>
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "common/polyfill_thread.h"
  11. #include "core/hardware_properties.h"
  12. #include "core/hle/kernel/k_auto_object.h"
  13. #include "core/hle/kernel/k_slab_heap.h"
  14. #include "core/hle/kernel/k_typed_address.h"
  15. #include "core/hle/kernel/svc_common.h"
  16. namespace Core {
  17. class ExclusiveMonitor;
  18. class System;
  19. } // namespace Core
  20. namespace Core::Timing {
  21. class CoreTiming;
  22. struct EventType;
  23. } // namespace Core::Timing
  24. namespace Service {
  25. class ServerManager;
  26. }
  27. namespace Service::SM {
  28. class ServiceManager;
  29. }
  30. namespace Kernel {
  31. class KClientPort;
  32. class GlobalSchedulerContext;
  33. class KAutoObjectWithListContainer;
  34. class KClientSession;
  35. class KDebug;
  36. class KDeviceAddressSpace;
  37. class KDynamicPageManager;
  38. class KEvent;
  39. class KEventInfo;
  40. class KHandleTable;
  41. class KHardwareTimer;
  42. class KMemoryLayout;
  43. class KMemoryManager;
  44. class KObjectName;
  45. class KObjectNameGlobalData;
  46. class KPageBuffer;
  47. class KPageBufferSlabHeap;
  48. class KPort;
  49. class KProcess;
  50. class KResourceLimit;
  51. class KScheduler;
  52. class KServerPort;
  53. class KServerSession;
  54. class KSession;
  55. class KSessionRequest;
  56. class KSharedMemory;
  57. class KSharedMemoryInfo;
  58. class KSecureSystemResource;
  59. class KThread;
  60. class KThreadLocalPage;
  61. class KTransferMemory;
  62. class KWorkerTaskManager;
  63. class KCodeMemory;
  64. class PhysicalCore;
  65. namespace Init {
  66. struct KSlabResourceCounts;
  67. }
  68. template <typename T>
  69. class KSlabHeap;
  70. /// Represents a single instance of the kernel.
  71. class KernelCore {
  72. public:
  73. /// Constructs an instance of the kernel using the given System
  74. /// instance as a context for any necessary system-related state,
  75. /// such as threads, CPU core state, etc.
  76. ///
  77. /// @post After execution of the constructor, the provided System
  78. /// object *must* outlive the kernel instance itself.
  79. ///
  80. explicit KernelCore(Core::System& system);
  81. ~KernelCore();
  82. KernelCore(const KernelCore&) = delete;
  83. KernelCore& operator=(const KernelCore&) = delete;
  84. KernelCore(KernelCore&&) = delete;
  85. KernelCore& operator=(KernelCore&&) = delete;
  86. /// Sets if emulation is multicore or single core, must be set before Initialize
  87. void SetMulticore(bool is_multicore);
  88. /// Resets the kernel to a clean slate for use.
  89. void Initialize();
  90. /// Clears all resources in use by the kernel instance.
  91. void Shutdown();
  92. /// Close all active services in use by the kernel instance.
  93. void CloseServices();
  94. /// Retrieves a shared pointer to the system resource limit instance.
  95. const KResourceLimit* GetSystemResourceLimit() const;
  96. /// Retrieves a shared pointer to the system resource limit instance.
  97. KResourceLimit* GetSystemResourceLimit();
  98. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  99. KScopedAutoObject<KThread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
  100. /// Adds the given shared pointer to an internal list of active processes.
  101. void AppendNewProcess(KProcess* process);
  102. /// Makes the given process the new application process.
  103. void MakeApplicationProcess(KProcess* process);
  104. /// Retrieves a pointer to the application process.
  105. KProcess* ApplicationProcess();
  106. /// Retrieves a const pointer to the application process.
  107. const KProcess* ApplicationProcess() const;
  108. /// Retrieves the list of processes.
  109. const std::vector<KProcess*>& GetProcessList() const;
  110. /// Gets the sole instance of the global scheduler
  111. Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  112. /// Gets the sole instance of the global scheduler
  113. const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  114. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  115. Kernel::KScheduler& Scheduler(std::size_t id);
  116. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  117. const Kernel::KScheduler& Scheduler(std::size_t id) const;
  118. /// Gets the an instance of the respective physical CPU core.
  119. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  120. /// Gets the an instance of the respective physical CPU core.
  121. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  122. /// Gets the current physical core index for the running host thread.
  123. std::size_t CurrentPhysicalCoreIndex() const;
  124. /// Gets the sole instance of the Scheduler at the current running core.
  125. Kernel::KScheduler* CurrentScheduler();
  126. /// Gets the an instance of the current physical CPU core.
  127. Kernel::PhysicalCore& CurrentPhysicalCore();
  128. /// Gets the an instance of the current physical CPU core.
  129. const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  130. /// Gets the an instance of the hardware timer.
  131. Kernel::KHardwareTimer& HardwareTimer();
  132. /// Stops execution of 'id' core, in order to reschedule a new thread.
  133. void PrepareReschedule(std::size_t id);
  134. Core::ExclusiveMonitor& GetExclusiveMonitor();
  135. const Core::ExclusiveMonitor& GetExclusiveMonitor() const;
  136. KAutoObjectWithListContainer& ObjectListContainer();
  137. const KAutoObjectWithListContainer& ObjectListContainer() const;
  138. /// Registers all kernel objects with the global emulation state, this is purely for tracking
  139. /// leaks after emulation has been shutdown.
  140. void RegisterKernelObject(KAutoObject* object);
  141. /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
  142. /// destroyed during the current emulation session.
  143. void UnregisterKernelObject(KAutoObject* object);
  144. /// Registers kernel objects with guest in use state, this is purely for close
  145. /// after emulation has been shutdown.
  146. void RegisterInUseObject(KAutoObject* object);
  147. /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
  148. /// destroyed during the current emulation session.
  149. void UnregisterInUseObject(KAutoObject* object);
  150. // Runs the given server manager until shutdown.
  151. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  152. /// Gets the current host_thread/guest_thread pointer.
  153. KThread* GetCurrentEmuThread() const;
  154. /// Sets the current guest_thread pointer.
  155. void SetCurrentEmuThread(KThread* thread);
  156. /// Gets the current host_thread handle.
  157. u32 GetCurrentHostThreadID() const;
  158. /// Register the current thread as a CPU Core Thread.
  159. void RegisterCoreThread(std::size_t core_id);
  160. /// Register the current thread as a non CPU core thread.
  161. void RegisterHostThread(KThread* existing_thread = nullptr);
  162. void RunOnGuestCoreProcess(std::string&& process_name, std::function<void()> func);
  163. std::jthread RunOnHostCoreProcess(std::string&& process_name, std::function<void()> func);
  164. std::jthread RunOnHostCoreThread(std::string&& thread_name, std::function<void()> func);
  165. /// Gets global data for KObjectName.
  166. KObjectNameGlobalData& ObjectNameGlobalData();
  167. /// Gets the virtual memory manager for the kernel.
  168. KMemoryManager& MemoryManager();
  169. /// Gets the virtual memory manager for the kernel.
  170. const KMemoryManager& MemoryManager() const;
  171. /// Gets the application resource manager.
  172. KSystemResource& GetAppSystemResource();
  173. /// Gets the application resource manager.
  174. const KSystemResource& GetAppSystemResource() const;
  175. /// Gets the system resource manager.
  176. KSystemResource& GetSystemSystemResource();
  177. /// Gets the system resource manager.
  178. const KSystemResource& GetSystemSystemResource() const;
  179. /// Gets the shared memory object for HID services.
  180. Kernel::KSharedMemory& GetHidSharedMem();
  181. /// Gets the shared memory object for HID services.
  182. const Kernel::KSharedMemory& GetHidSharedMem() const;
  183. /// Gets the shared memory object for font services.
  184. Kernel::KSharedMemory& GetFontSharedMem();
  185. /// Gets the shared memory object for font services.
  186. const Kernel::KSharedMemory& GetFontSharedMem() const;
  187. /// Gets the shared memory object for IRS services.
  188. Kernel::KSharedMemory& GetIrsSharedMem();
  189. /// Gets the shared memory object for IRS services.
  190. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  191. /// Gets the shared memory object for Time services.
  192. Kernel::KSharedMemory& GetTimeSharedMem();
  193. /// Gets the shared memory object for Time services.
  194. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  195. /// Gets the shared memory object for HIDBus services.
  196. Kernel::KSharedMemory& GetHidBusSharedMem();
  197. /// Gets the shared memory object for HIDBus services.
  198. const Kernel::KSharedMemory& GetHidBusSharedMem() const;
  199. /// Suspend/unsuspend application process.
  200. void SuspendApplication(bool suspend);
  201. /// Exceptional exit application process.
  202. void ExceptionalExitApplication();
  203. /// Notify emulated CPU cores to shut down.
  204. void ShutdownCores();
  205. bool IsMulticore() const;
  206. bool IsShuttingDown() const;
  207. void EnterSVCProfile();
  208. void ExitSVCProfile();
  209. /// Workaround for single-core mode when preempting threads while idle.
  210. bool IsPhantomModeForSingleCore() const;
  211. void SetIsPhantomModeForSingleCore(bool value);
  212. Core::System& System();
  213. const Core::System& System() const;
  214. /// Gets the slab heap for the specified kernel object type.
  215. template <typename T>
  216. KSlabHeap<T>& SlabHeap();
  217. /// Gets the current slab resource counts.
  218. Init::KSlabResourceCounts& SlabResourceCounts();
  219. /// Gets the current slab resource counts.
  220. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  221. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  222. KWorkerTaskManager& WorkerTaskManager();
  223. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  224. const KWorkerTaskManager& WorkerTaskManager() const;
  225. /// Gets the memory layout.
  226. const KMemoryLayout& MemoryLayout() const;
  227. private:
  228. friend class KProcess;
  229. friend class KThread;
  230. /// Creates a new object ID, incrementing the internal object ID counter.
  231. u32 CreateNewObjectID();
  232. /// Creates a new process ID, incrementing the internal process ID counter;
  233. u64 CreateNewKernelProcessID();
  234. /// Creates a new process ID, incrementing the internal process ID counter;
  235. u64 CreateNewUserProcessID();
  236. /// Creates a new thread ID, incrementing the internal thread ID counter.
  237. u64 CreateNewThreadID();
  238. /// Provides a reference to the global handle table.
  239. KHandleTable& GlobalHandleTable();
  240. /// Provides a const reference to the global handle table.
  241. const KHandleTable& GlobalHandleTable() const;
  242. struct Impl;
  243. std::unique_ptr<Impl> impl;
  244. bool exception_exited{};
  245. private:
  246. /// Helper to encapsulate all slab heaps in a single heap allocated container
  247. struct SlabHeapContainer;
  248. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  249. };
  250. } // namespace Kernel