kernel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /// Adds the given shared pointer to an internal list of active processes.
  99. void AppendNewProcess(KProcess* process);
  100. /// Makes the given process the new application process.
  101. void MakeApplicationProcess(KProcess* process);
  102. /// Retrieves a pointer to the application process.
  103. KProcess* ApplicationProcess();
  104. /// Retrieves a const pointer to the application process.
  105. const KProcess* ApplicationProcess() 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 hardware timer.
  129. Kernel::KHardwareTimer& HardwareTimer();
  130. /// Stops execution of 'id' core, in order to reschedule a new thread.
  131. void PrepareReschedule(std::size_t id);
  132. KAutoObjectWithListContainer& ObjectListContainer();
  133. const KAutoObjectWithListContainer& ObjectListContainer() const;
  134. /// Registers all kernel objects with the global emulation state, this is purely for tracking
  135. /// leaks after emulation has been shutdown.
  136. void RegisterKernelObject(KAutoObject* object);
  137. /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
  138. /// destroyed during the current emulation session.
  139. void UnregisterKernelObject(KAutoObject* object);
  140. /// Registers kernel objects with guest in use state, this is purely for close
  141. /// after emulation has been shutdown.
  142. void RegisterInUseObject(KAutoObject* object);
  143. /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
  144. /// destroyed during the current emulation session.
  145. void UnregisterInUseObject(KAutoObject* object);
  146. // Runs the given server manager until shutdown.
  147. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  148. /// Gets the current host_thread/guest_thread pointer.
  149. KThread* GetCurrentEmuThread() const;
  150. /// Sets the current guest_thread pointer.
  151. void SetCurrentEmuThread(KThread* thread);
  152. /// Gets the current host_thread handle.
  153. u32 GetCurrentHostThreadID() const;
  154. /// Register the current thread as a CPU Core Thread.
  155. void RegisterCoreThread(std::size_t core_id);
  156. /// Register the current thread as a non CPU core thread.
  157. void RegisterHostThread(KThread* existing_thread = nullptr);
  158. void RunOnGuestCoreProcess(std::string&& process_name, std::function<void()> func);
  159. std::jthread RunOnHostCoreProcess(std::string&& process_name, std::function<void()> func);
  160. std::jthread RunOnHostCoreThread(std::string&& thread_name, std::function<void()> func);
  161. /// Gets global data for KObjectName.
  162. KObjectNameGlobalData& ObjectNameGlobalData();
  163. /// Gets the virtual memory manager for the kernel.
  164. KMemoryManager& MemoryManager();
  165. /// Gets the virtual memory manager for the kernel.
  166. const KMemoryManager& MemoryManager() const;
  167. /// Gets the application resource manager.
  168. KSystemResource& GetAppSystemResource();
  169. /// Gets the application resource manager.
  170. const KSystemResource& GetAppSystemResource() const;
  171. /// Gets the system resource manager.
  172. KSystemResource& GetSystemSystemResource();
  173. /// Gets the system resource manager.
  174. const KSystemResource& GetSystemSystemResource() const;
  175. /// Gets the shared memory object for font services.
  176. Kernel::KSharedMemory& GetFontSharedMem();
  177. /// Gets the shared memory object for font services.
  178. const Kernel::KSharedMemory& GetFontSharedMem() const;
  179. /// Gets the shared memory object for IRS services.
  180. Kernel::KSharedMemory& GetIrsSharedMem();
  181. /// Gets the shared memory object for IRS services.
  182. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  183. /// Gets the shared memory object for Time services.
  184. Kernel::KSharedMemory& GetTimeSharedMem();
  185. /// Gets the shared memory object for Time services.
  186. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  187. /// Gets the shared memory object for HIDBus services.
  188. Kernel::KSharedMemory& GetHidBusSharedMem();
  189. /// Gets the shared memory object for HIDBus services.
  190. const Kernel::KSharedMemory& GetHidBusSharedMem() const;
  191. /// Suspend/unsuspend application process.
  192. void SuspendApplication(bool suspend);
  193. /// Exceptional exit application process.
  194. void ExceptionalExitApplication();
  195. /// Notify emulated CPU cores to shut down.
  196. void ShutdownCores();
  197. bool IsMulticore() const;
  198. bool IsShuttingDown() const;
  199. void EnterSVCProfile();
  200. void ExitSVCProfile();
  201. /// Workaround for single-core mode when preempting threads while idle.
  202. bool IsPhantomModeForSingleCore() const;
  203. void SetIsPhantomModeForSingleCore(bool value);
  204. Core::System& System();
  205. const Core::System& System() const;
  206. /// Gets the slab heap for the specified kernel object type.
  207. template <typename T>
  208. KSlabHeap<T>& SlabHeap();
  209. /// Gets the current slab resource counts.
  210. Init::KSlabResourceCounts& SlabResourceCounts();
  211. /// Gets the current slab resource counts.
  212. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  213. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  214. KWorkerTaskManager& WorkerTaskManager();
  215. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  216. const KWorkerTaskManager& WorkerTaskManager() const;
  217. /// Gets the memory layout.
  218. const KMemoryLayout& MemoryLayout() const;
  219. private:
  220. friend class KProcess;
  221. friend class KThread;
  222. /// Creates a new object ID, incrementing the internal object ID counter.
  223. u32 CreateNewObjectID();
  224. /// Creates a new process ID, incrementing the internal process ID counter;
  225. u64 CreateNewKernelProcessID();
  226. /// Creates a new process ID, incrementing the internal process ID counter;
  227. u64 CreateNewUserProcessID();
  228. /// Creates a new thread ID, incrementing the internal thread ID counter.
  229. u64 CreateNewThreadID();
  230. /// Provides a reference to the global handle table.
  231. KHandleTable& GlobalHandleTable();
  232. /// Provides a const reference to the global handle table.
  233. const KHandleTable& GlobalHandleTable() const;
  234. struct Impl;
  235. std::unique_ptr<Impl> impl;
  236. bool exception_exited{};
  237. private:
  238. /// Helper to encapsulate all slab heaps in a single heap allocated container
  239. struct SlabHeapContainer;
  240. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  241. };
  242. } // namespace Kernel