kernel.h 10 KB

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