kernel.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "core/arm/cpu_interrupt_handler.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 CPUInterruptHandler;
  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::SM {
  25. class ServiceManager;
  26. }
  27. namespace Kernel {
  28. class KClientPort;
  29. class GlobalSchedulerContext;
  30. class KAutoObjectWithListContainer;
  31. class KClientSession;
  32. class KEvent;
  33. class KHandleTable;
  34. class KLinkedListNode;
  35. class KMemoryLayout;
  36. class KMemoryManager;
  37. class KPageBuffer;
  38. class KPort;
  39. class KProcess;
  40. class KResourceLimit;
  41. class KScheduler;
  42. class KServerSession;
  43. class KSession;
  44. class KSharedMemory;
  45. class KSharedMemoryInfo;
  46. class KThread;
  47. class KThreadLocalPage;
  48. class KTransferMemory;
  49. class KWorkerTaskManager;
  50. class KWritableEvent;
  51. class KCodeMemory;
  52. class PhysicalCore;
  53. class ServiceThread;
  54. class Synchronization;
  55. class TimeManager;
  56. using ServiceInterfaceFactory =
  57. std::function<KClientPort&(Service::SM::ServiceManager&, Core::System&)>;
  58. namespace Init {
  59. struct KSlabResourceCounts;
  60. }
  61. template <typename T>
  62. class KSlabHeap;
  63. using EmuThreadHandle = uintptr_t;
  64. constexpr EmuThreadHandle EmuThreadHandleInvalid{};
  65. constexpr EmuThreadHandle EmuThreadHandleReserved{1ULL << 63};
  66. /// Represents a single instance of the kernel.
  67. class KernelCore {
  68. private:
  69. using NamedPortTable = std::unordered_map<std::string, KClientPort*>;
  70. public:
  71. /// Constructs an instance of the kernel using the given System
  72. /// instance as a context for any necessary system-related state,
  73. /// such as threads, CPU core state, etc.
  74. ///
  75. /// @post After execution of the constructor, the provided System
  76. /// object *must* outlive the kernel instance itself.
  77. ///
  78. explicit KernelCore(Core::System& system);
  79. ~KernelCore();
  80. KernelCore(const KernelCore&) = delete;
  81. KernelCore& operator=(const KernelCore&) = delete;
  82. KernelCore(KernelCore&&) = delete;
  83. KernelCore& operator=(KernelCore&&) = delete;
  84. /// Sets if emulation is multicore or single core, must be set before Initialize
  85. void SetMulticore(bool is_multicore);
  86. /// Resets the kernel to a clean slate for use.
  87. void Initialize();
  88. /// Initializes the CPU cores.
  89. void InitializeCores();
  90. /// Clears all resources in use by the kernel instance.
  91. void Shutdown();
  92. /// Retrieves a shared pointer to the system resource limit instance.
  93. const KResourceLimit* GetSystemResourceLimit() const;
  94. /// Retrieves a shared pointer to the system resource limit instance.
  95. KResourceLimit* GetSystemResourceLimit();
  96. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  97. KScopedAutoObject<KThread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
  98. /// Adds the given shared pointer to an internal list of active processes.
  99. void AppendNewProcess(KProcess* process);
  100. /// Makes the given process the new current process.
  101. void MakeCurrentProcess(KProcess* process);
  102. /// Retrieves a pointer to the current process.
  103. KProcess* CurrentProcess();
  104. /// Retrieves a const pointer to the current process.
  105. const KProcess* CurrentProcess() const;
  106. /// Retrieves the list of processes.
  107. const std::vector<KProcess*>& GetProcessList() const;
  108. /// Gets the sole instance of the global scheduler
  109. Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  110. /// Gets the sole instance of the global scheduler
  111. const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  112. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  113. Kernel::KScheduler& Scheduler(std::size_t id);
  114. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  115. const Kernel::KScheduler& Scheduler(std::size_t id) const;
  116. /// Gets the an instance of the respective physical CPU core.
  117. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  118. /// Gets the an instance of the respective physical CPU core.
  119. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  120. /// Gets the current physical core index for the running host thread.
  121. std::size_t CurrentPhysicalCoreIndex() const;
  122. /// Gets the sole instance of the Scheduler at the current running core.
  123. Kernel::KScheduler* CurrentScheduler();
  124. /// Gets the an instance of the current physical CPU core.
  125. Kernel::PhysicalCore& CurrentPhysicalCore();
  126. /// Gets the an instance of the current physical CPU core.
  127. const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  128. /// Gets the an instance of the TimeManager Interface.
  129. Kernel::TimeManager& TimeManager();
  130. /// Gets the an instance of the TimeManager Interface.
  131. const Kernel::TimeManager& TimeManager() const;
  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. std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts();
  139. const std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts() const;
  140. void InterruptAllPhysicalCores();
  141. void InvalidateAllInstructionCaches();
  142. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  143. /// Registers a named HLE service, passing a factory used to open a port to that service.
  144. void RegisterNamedService(std::string name, ServiceInterfaceFactory&& factory);
  145. /// Opens a port to a service previously registered with RegisterNamedService.
  146. KClientPort* CreateNamedServicePort(std::string name);
  147. /// Registers a server session or port with the gobal emulation state, to be freed on shutdown.
  148. /// This is necessary because we do not emulate processes for HLE sessions and ports.
  149. void RegisterServerObject(KAutoObject* server_object);
  150. /// Unregisters a server session or port previously registered with RegisterServerSession when
  151. /// it was destroyed during the current emulation session.
  152. void UnregisterServerObject(KAutoObject* server_object);
  153. /// Registers all kernel objects with the global emulation state, this is purely for tracking
  154. /// leaks after emulation has been shutdown.
  155. void RegisterKernelObject(KAutoObject* object);
  156. /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
  157. /// destroyed during the current emulation session.
  158. void UnregisterKernelObject(KAutoObject* object);
  159. /// Registers kernel objects with guest in use state, this is purely for close
  160. /// after emulation has been shutdown.
  161. void RegisterInUseObject(KAutoObject* object);
  162. /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
  163. /// destroyed during the current emulation session.
  164. void UnregisterInUseObject(KAutoObject* object);
  165. /// Determines whether or not the given port is a valid named port.
  166. bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
  167. /// Gets the current host_thread/guest_thread pointer.
  168. KThread* GetCurrentEmuThread() const;
  169. /// Sets the current guest_thread pointer.
  170. void SetCurrentEmuThread(KThread* thread);
  171. /// Gets the current host_thread handle.
  172. u32 GetCurrentHostThreadID() const;
  173. /// Register the current thread as a CPU Core Thread.
  174. void RegisterCoreThread(std::size_t core_id);
  175. /// Register the current thread as a non CPU core thread.
  176. void RegisterHostThread();
  177. /// Gets the virtual memory manager for the kernel.
  178. KMemoryManager& MemoryManager();
  179. /// Gets the virtual memory manager for the kernel.
  180. const KMemoryManager& MemoryManager() const;
  181. /// Gets the shared memory object for HID services.
  182. Kernel::KSharedMemory& GetHidSharedMem();
  183. /// Gets the shared memory object for HID services.
  184. const Kernel::KSharedMemory& GetHidSharedMem() const;
  185. /// Gets the shared memory object for font services.
  186. Kernel::KSharedMemory& GetFontSharedMem();
  187. /// Gets the shared memory object for font services.
  188. const Kernel::KSharedMemory& GetFontSharedMem() const;
  189. /// Gets the shared memory object for IRS services.
  190. Kernel::KSharedMemory& GetIrsSharedMem();
  191. /// Gets the shared memory object for IRS services.
  192. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  193. /// Gets the shared memory object for Time services.
  194. Kernel::KSharedMemory& GetTimeSharedMem();
  195. /// Gets the shared memory object for Time services.
  196. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  197. /// Gets the shared memory object for HIDBus services.
  198. Kernel::KSharedMemory& GetHidBusSharedMem();
  199. /// Gets the shared memory object for HIDBus services.
  200. const Kernel::KSharedMemory& GetHidBusSharedMem() const;
  201. /// Suspend/unsuspend all processes.
  202. void Suspend(bool suspend);
  203. /// Exceptional exit all processes.
  204. void ExceptionalExit();
  205. /// Notify emulated CPU cores to shut down.
  206. void ShutdownCores();
  207. bool IsMulticore() const;
  208. bool IsShuttingDown() const;
  209. void EnterSVCProfile();
  210. void ExitSVCProfile();
  211. /**
  212. * Creates a host thread to execute HLE service requests, which are used to execute service
  213. * routines asynchronously. While these are allocated per ServerSession, these need to be owned
  214. * and managed outside of ServerSession to avoid a circular dependency. In general, most
  215. * services can just use the default service thread, and not need their own host service thread.
  216. * See GetDefaultServiceThread.
  217. * @param name String name for the ServerSession creating this thread, used for debug
  218. * purposes.
  219. * @returns The a weak pointer newly created service thread.
  220. */
  221. std::weak_ptr<Kernel::ServiceThread> CreateServiceThread(const std::string& name);
  222. /**
  223. * Gets the default host service thread, which executes HLE service requests. Unless service
  224. * requests need to block on the host, the default service thread should be used in favor of
  225. * creating a new service thread.
  226. * @returns The a weak pointer for the default service thread.
  227. */
  228. std::weak_ptr<Kernel::ServiceThread> GetDefaultServiceThread() const;
  229. /**
  230. * Releases a HLE service thread, instructing KernelCore to free it. This should be called when
  231. * the ServerSession associated with the thread is destroyed.
  232. * @param service_thread Service thread to release.
  233. */
  234. void ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread);
  235. /// Workaround for single-core mode when preempting threads while idle.
  236. bool IsPhantomModeForSingleCore() const;
  237. void SetIsPhantomModeForSingleCore(bool value);
  238. Core::System& System();
  239. const Core::System& System() const;
  240. /// Gets the slab heap for the specified kernel object type.
  241. template <typename T>
  242. KSlabHeap<T>& SlabHeap() {
  243. if constexpr (std::is_same_v<T, KClientSession>) {
  244. return slab_heap_container->client_session;
  245. } else if constexpr (std::is_same_v<T, KEvent>) {
  246. return slab_heap_container->event;
  247. } else if constexpr (std::is_same_v<T, KLinkedListNode>) {
  248. return slab_heap_container->linked_list_node;
  249. } else if constexpr (std::is_same_v<T, KPort>) {
  250. return slab_heap_container->port;
  251. } else if constexpr (std::is_same_v<T, KProcess>) {
  252. return slab_heap_container->process;
  253. } else if constexpr (std::is_same_v<T, KResourceLimit>) {
  254. return slab_heap_container->resource_limit;
  255. } else if constexpr (std::is_same_v<T, KSession>) {
  256. return slab_heap_container->session;
  257. } else if constexpr (std::is_same_v<T, KSharedMemory>) {
  258. return slab_heap_container->shared_memory;
  259. } else if constexpr (std::is_same_v<T, KSharedMemoryInfo>) {
  260. return slab_heap_container->shared_memory_info;
  261. } else if constexpr (std::is_same_v<T, KThread>) {
  262. return slab_heap_container->thread;
  263. } else if constexpr (std::is_same_v<T, KTransferMemory>) {
  264. return slab_heap_container->transfer_memory;
  265. } else if constexpr (std::is_same_v<T, KWritableEvent>) {
  266. return slab_heap_container->writeable_event;
  267. } else if constexpr (std::is_same_v<T, KCodeMemory>) {
  268. return slab_heap_container->code_memory;
  269. } else if constexpr (std::is_same_v<T, KPageBuffer>) {
  270. return slab_heap_container->page_buffer;
  271. } else if constexpr (std::is_same_v<T, KThreadLocalPage>) {
  272. return slab_heap_container->thread_local_page;
  273. }
  274. }
  275. /// Gets the current slab resource counts.
  276. Init::KSlabResourceCounts& SlabResourceCounts();
  277. /// Gets the current slab resource counts.
  278. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  279. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  280. KWorkerTaskManager& WorkerTaskManager();
  281. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  282. const KWorkerTaskManager& WorkerTaskManager() const;
  283. /// Gets the memory layout.
  284. const KMemoryLayout& MemoryLayout() const;
  285. private:
  286. friend class KProcess;
  287. friend class KThread;
  288. /// Creates a new object ID, incrementing the internal object ID counter.
  289. u32 CreateNewObjectID();
  290. /// Creates a new process ID, incrementing the internal process ID counter;
  291. u64 CreateNewKernelProcessID();
  292. /// Creates a new process ID, incrementing the internal process ID counter;
  293. u64 CreateNewUserProcessID();
  294. /// Creates a new thread ID, incrementing the internal thread ID counter.
  295. u64 CreateNewThreadID();
  296. /// Provides a reference to the global handle table.
  297. KHandleTable& GlobalHandleTable();
  298. /// Provides a const reference to the global handle table.
  299. const KHandleTable& GlobalHandleTable() const;
  300. struct Impl;
  301. std::unique_ptr<Impl> impl;
  302. bool exception_exited{};
  303. private:
  304. /// Helper to encapsulate all slab heaps in a single heap allocated container
  305. struct SlabHeapContainer {
  306. KSlabHeap<KClientSession> client_session;
  307. KSlabHeap<KEvent> event;
  308. KSlabHeap<KLinkedListNode> linked_list_node;
  309. KSlabHeap<KPort> port;
  310. KSlabHeap<KProcess> process;
  311. KSlabHeap<KResourceLimit> resource_limit;
  312. KSlabHeap<KSession> session;
  313. KSlabHeap<KSharedMemory> shared_memory;
  314. KSlabHeap<KSharedMemoryInfo> shared_memory_info;
  315. KSlabHeap<KThread> thread;
  316. KSlabHeap<KTransferMemory> transfer_memory;
  317. KSlabHeap<KWritableEvent> writeable_event;
  318. KSlabHeap<KCodeMemory> code_memory;
  319. KSlabHeap<KPageBuffer> page_buffer;
  320. KSlabHeap<KThreadLocalPage> thread_local_page;
  321. };
  322. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  323. };
  324. } // namespace Kernel