kernel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/svc_common.h"
  15. namespace Core {
  16. class ExclusiveMonitor;
  17. class System;
  18. } // namespace Core
  19. namespace Core::Timing {
  20. class CoreTiming;
  21. struct EventType;
  22. } // namespace Core::Timing
  23. namespace Service {
  24. class ServerManager;
  25. }
  26. namespace Service::SM {
  27. class ServiceManager;
  28. }
  29. namespace Kernel {
  30. class KClientPort;
  31. class GlobalSchedulerContext;
  32. class KAutoObjectWithListContainer;
  33. class KClientSession;
  34. class KDebug;
  35. class KDeviceAddressSpace;
  36. class KDynamicPageManager;
  37. class KEvent;
  38. class KEventInfo;
  39. class KHandleTable;
  40. class KHardwareTimer;
  41. class KLinkedListNode;
  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. /// Initializes the CPU cores.
  91. void InitializeCores();
  92. /// Clears all resources in use by the kernel instance.
  93. void Shutdown();
  94. /// Close all active services in use by the kernel instance.
  95. void CloseServices();
  96. /// Retrieves a shared pointer to the system resource limit instance.
  97. const KResourceLimit* GetSystemResourceLimit() const;
  98. /// Retrieves a shared pointer to the system resource limit instance.
  99. KResourceLimit* GetSystemResourceLimit();
  100. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  101. KScopedAutoObject<KThread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
  102. /// Adds the given shared pointer to an internal list of active processes.
  103. void AppendNewProcess(KProcess* process);
  104. /// Makes the given process the new application process.
  105. void MakeApplicationProcess(KProcess* process);
  106. /// Retrieves a pointer to the application process.
  107. KProcess* ApplicationProcess();
  108. /// Retrieves a const pointer to the application process.
  109. const KProcess* ApplicationProcess() const;
  110. /// Closes the application process.
  111. void CloseApplicationProcess();
  112. /// Retrieves the list of processes.
  113. const std::vector<KProcess*>& GetProcessList() const;
  114. /// Gets the sole instance of the global scheduler
  115. Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  116. /// Gets the sole instance of the global scheduler
  117. const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  118. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  119. Kernel::KScheduler& Scheduler(std::size_t id);
  120. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  121. const Kernel::KScheduler& Scheduler(std::size_t id) const;
  122. /// Gets the an instance of the respective physical CPU core.
  123. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  124. /// Gets the an instance of the respective physical CPU core.
  125. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  126. /// Gets the current physical core index for the running host thread.
  127. std::size_t CurrentPhysicalCoreIndex() const;
  128. /// Gets the sole instance of the Scheduler at the current running core.
  129. Kernel::KScheduler* CurrentScheduler();
  130. /// Gets the an instance of the current physical CPU core.
  131. Kernel::PhysicalCore& CurrentPhysicalCore();
  132. /// Gets the an instance of the current physical CPU core.
  133. const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  134. /// Gets the an instance of the hardware timer.
  135. Kernel::KHardwareTimer& HardwareTimer();
  136. /// Stops execution of 'id' core, in order to reschedule a new thread.
  137. void PrepareReschedule(std::size_t id);
  138. Core::ExclusiveMonitor& GetExclusiveMonitor();
  139. const Core::ExclusiveMonitor& GetExclusiveMonitor() const;
  140. KAutoObjectWithListContainer& ObjectListContainer();
  141. const KAutoObjectWithListContainer& ObjectListContainer() const;
  142. void InvalidateAllInstructionCaches();
  143. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  144. /// Registers all kernel objects with the global emulation state, this is purely for tracking
  145. /// leaks after emulation has been shutdown.
  146. void RegisterKernelObject(KAutoObject* object);
  147. /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
  148. /// destroyed during the current emulation session.
  149. void UnregisterKernelObject(KAutoObject* object);
  150. /// Registers kernel objects with guest in use state, this is purely for close
  151. /// after emulation has been shutdown.
  152. void RegisterInUseObject(KAutoObject* object);
  153. /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
  154. /// destroyed during the current emulation session.
  155. void UnregisterInUseObject(KAutoObject* object);
  156. // Runs the given server manager until shutdown.
  157. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  158. /// Gets the current host_thread/guest_thread pointer.
  159. KThread* GetCurrentEmuThread() const;
  160. /// Sets the current guest_thread pointer.
  161. void SetCurrentEmuThread(KThread* thread);
  162. /// Gets the current host_thread handle.
  163. u32 GetCurrentHostThreadID() const;
  164. /// Register the current thread as a CPU Core Thread.
  165. void RegisterCoreThread(std::size_t core_id);
  166. /// Register the current thread as a non CPU core thread.
  167. void RegisterHostThread(KThread* existing_thread = nullptr);
  168. void RunOnGuestCoreProcess(std::string&& process_name, std::function<void()> func);
  169. std::jthread RunOnHostCoreProcess(std::string&& process_name, std::function<void()> func);
  170. std::jthread RunOnHostCoreThread(std::string&& thread_name, std::function<void()> func);
  171. /// Gets global data for KObjectName.
  172. KObjectNameGlobalData& ObjectNameGlobalData();
  173. /// Gets the virtual memory manager for the kernel.
  174. KMemoryManager& MemoryManager();
  175. /// Gets the virtual memory manager for the kernel.
  176. const KMemoryManager& MemoryManager() const;
  177. /// Gets the application resource manager.
  178. KSystemResource& GetAppSystemResource();
  179. /// Gets the application resource manager.
  180. const KSystemResource& GetAppSystemResource() const;
  181. /// Gets the system resource manager.
  182. KSystemResource& GetSystemSystemResource();
  183. /// Gets the system resource manager.
  184. const KSystemResource& GetSystemSystemResource() const;
  185. /// Gets the shared memory object for HID services.
  186. Kernel::KSharedMemory& GetHidSharedMem();
  187. /// Gets the shared memory object for HID services.
  188. const Kernel::KSharedMemory& GetHidSharedMem() const;
  189. /// Gets the shared memory object for font services.
  190. Kernel::KSharedMemory& GetFontSharedMem();
  191. /// Gets the shared memory object for font services.
  192. const Kernel::KSharedMemory& GetFontSharedMem() const;
  193. /// Gets the shared memory object for IRS services.
  194. Kernel::KSharedMemory& GetIrsSharedMem();
  195. /// Gets the shared memory object for IRS services.
  196. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  197. /// Gets the shared memory object for Time services.
  198. Kernel::KSharedMemory& GetTimeSharedMem();
  199. /// Gets the shared memory object for Time services.
  200. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  201. /// Gets the shared memory object for HIDBus services.
  202. Kernel::KSharedMemory& GetHidBusSharedMem();
  203. /// Gets the shared memory object for HIDBus services.
  204. const Kernel::KSharedMemory& GetHidBusSharedMem() const;
  205. /// Suspend/unsuspend application process.
  206. void SuspendApplication(bool suspend);
  207. /// Exceptional exit application process.
  208. void ExceptionalExitApplication();
  209. /// Notify emulated CPU cores to shut down.
  210. void ShutdownCores();
  211. bool IsMulticore() const;
  212. bool IsShuttingDown() const;
  213. void EnterSVCProfile();
  214. void ExitSVCProfile();
  215. /// Workaround for single-core mode when preempting threads while idle.
  216. bool IsPhantomModeForSingleCore() const;
  217. void SetIsPhantomModeForSingleCore(bool value);
  218. Core::System& System();
  219. const Core::System& System() const;
  220. /// Gets the slab heap for the specified kernel object type.
  221. template <typename T>
  222. KSlabHeap<T>& SlabHeap() {
  223. if constexpr (std::is_same_v<T, KClientSession>) {
  224. return slab_heap_container->client_session;
  225. } else if constexpr (std::is_same_v<T, KEvent>) {
  226. return slab_heap_container->event;
  227. } else if constexpr (std::is_same_v<T, KLinkedListNode>) {
  228. return slab_heap_container->linked_list_node;
  229. } else if constexpr (std::is_same_v<T, KPort>) {
  230. return slab_heap_container->port;
  231. } else if constexpr (std::is_same_v<T, KProcess>) {
  232. return slab_heap_container->process;
  233. } else if constexpr (std::is_same_v<T, KResourceLimit>) {
  234. return slab_heap_container->resource_limit;
  235. } else if constexpr (std::is_same_v<T, KSession>) {
  236. return slab_heap_container->session;
  237. } else if constexpr (std::is_same_v<T, KSharedMemory>) {
  238. return slab_heap_container->shared_memory;
  239. } else if constexpr (std::is_same_v<T, KSharedMemoryInfo>) {
  240. return slab_heap_container->shared_memory_info;
  241. } else if constexpr (std::is_same_v<T, KThread>) {
  242. return slab_heap_container->thread;
  243. } else if constexpr (std::is_same_v<T, KTransferMemory>) {
  244. return slab_heap_container->transfer_memory;
  245. } else if constexpr (std::is_same_v<T, KCodeMemory>) {
  246. return slab_heap_container->code_memory;
  247. } else if constexpr (std::is_same_v<T, KDeviceAddressSpace>) {
  248. return slab_heap_container->device_address_space;
  249. } else if constexpr (std::is_same_v<T, KPageBuffer>) {
  250. return slab_heap_container->page_buffer;
  251. } else if constexpr (std::is_same_v<T, KThreadLocalPage>) {
  252. return slab_heap_container->thread_local_page;
  253. } else if constexpr (std::is_same_v<T, KObjectName>) {
  254. return slab_heap_container->object_name;
  255. } else if constexpr (std::is_same_v<T, KSessionRequest>) {
  256. return slab_heap_container->session_request;
  257. } else if constexpr (std::is_same_v<T, KSecureSystemResource>) {
  258. return slab_heap_container->secure_system_resource;
  259. } else if constexpr (std::is_same_v<T, KEventInfo>) {
  260. return slab_heap_container->event_info;
  261. } else if constexpr (std::is_same_v<T, KDebug>) {
  262. return slab_heap_container->debug;
  263. }
  264. }
  265. /// Gets the current slab resource counts.
  266. Init::KSlabResourceCounts& SlabResourceCounts();
  267. /// Gets the current slab resource counts.
  268. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  269. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  270. KWorkerTaskManager& WorkerTaskManager();
  271. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  272. const KWorkerTaskManager& WorkerTaskManager() const;
  273. /// Gets the memory layout.
  274. const KMemoryLayout& MemoryLayout() const;
  275. private:
  276. friend class KProcess;
  277. friend class KThread;
  278. /// Creates a new object ID, incrementing the internal object ID counter.
  279. u32 CreateNewObjectID();
  280. /// Creates a new process ID, incrementing the internal process ID counter;
  281. u64 CreateNewKernelProcessID();
  282. /// Creates a new process ID, incrementing the internal process ID counter;
  283. u64 CreateNewUserProcessID();
  284. /// Creates a new thread ID, incrementing the internal thread ID counter.
  285. u64 CreateNewThreadID();
  286. /// Provides a reference to the global handle table.
  287. KHandleTable& GlobalHandleTable();
  288. /// Provides a const reference to the global handle table.
  289. const KHandleTable& GlobalHandleTable() const;
  290. struct Impl;
  291. std::unique_ptr<Impl> impl;
  292. bool exception_exited{};
  293. private:
  294. /// Helper to encapsulate all slab heaps in a single heap allocated container
  295. struct SlabHeapContainer {
  296. KSlabHeap<KClientSession> client_session;
  297. KSlabHeap<KEvent> event;
  298. KSlabHeap<KLinkedListNode> linked_list_node;
  299. KSlabHeap<KPort> port;
  300. KSlabHeap<KProcess> process;
  301. KSlabHeap<KResourceLimit> resource_limit;
  302. KSlabHeap<KSession> session;
  303. KSlabHeap<KSharedMemory> shared_memory;
  304. KSlabHeap<KSharedMemoryInfo> shared_memory_info;
  305. KSlabHeap<KThread> thread;
  306. KSlabHeap<KTransferMemory> transfer_memory;
  307. KSlabHeap<KCodeMemory> code_memory;
  308. KSlabHeap<KDeviceAddressSpace> device_address_space;
  309. KSlabHeap<KPageBuffer> page_buffer;
  310. KSlabHeap<KThreadLocalPage> thread_local_page;
  311. KSlabHeap<KObjectName> object_name;
  312. KSlabHeap<KSessionRequest> session_request;
  313. KSlabHeap<KSecureSystemResource> secure_system_resource;
  314. KSlabHeap<KEventInfo> event_info;
  315. KSlabHeap<KDebug> debug;
  316. };
  317. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  318. };
  319. } // namespace Kernel