kernel.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <functional>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "core/arm/cpu_interrupt_handler.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/svc_common.h"
  16. namespace Core {
  17. class CPUInterruptHandler;
  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::SM {
  26. class ServiceManager;
  27. }
  28. namespace Kernel {
  29. class KClientPort;
  30. class GlobalSchedulerContext;
  31. class KAutoObjectWithListContainer;
  32. class KClientSession;
  33. class KEvent;
  34. class KHandleTable;
  35. class KLinkedListNode;
  36. class KMemoryLayout;
  37. class KMemoryManager;
  38. class KPageBuffer;
  39. class KPort;
  40. class KProcess;
  41. class KResourceLimit;
  42. class KScheduler;
  43. class KServerSession;
  44. class KSession;
  45. class KSharedMemory;
  46. class KSharedMemoryInfo;
  47. class KThread;
  48. class KThreadLocalPage;
  49. class KTransferMemory;
  50. class KWorkerTaskManager;
  51. class KWritableEvent;
  52. class KCodeMemory;
  53. class PhysicalCore;
  54. class ServiceThread;
  55. class Synchronization;
  56. class TimeManager;
  57. using ServiceInterfaceFactory =
  58. std::function<KClientPort&(Service::SM::ServiceManager&, Core::System&)>;
  59. namespace Init {
  60. struct KSlabResourceCounts;
  61. }
  62. template <typename T>
  63. class KSlabHeap;
  64. using EmuThreadHandle = uintptr_t;
  65. constexpr EmuThreadHandle EmuThreadHandleInvalid{};
  66. constexpr EmuThreadHandle EmuThreadHandleReserved{1ULL << 63};
  67. /// Represents a single instance of the kernel.
  68. class KernelCore {
  69. private:
  70. using NamedPortTable = std::unordered_map<std::string, KClientPort*>;
  71. public:
  72. /// Constructs an instance of the kernel using the given System
  73. /// instance as a context for any necessary system-related state,
  74. /// such as threads, CPU core state, etc.
  75. ///
  76. /// @post After execution of the constructor, the provided System
  77. /// object *must* outlive the kernel instance itself.
  78. ///
  79. explicit KernelCore(Core::System& system);
  80. ~KernelCore();
  81. KernelCore(const KernelCore&) = delete;
  82. KernelCore& operator=(const KernelCore&) = delete;
  83. KernelCore(KernelCore&&) = delete;
  84. KernelCore& operator=(KernelCore&&) = delete;
  85. /// Sets if emulation is multicore or single core, must be set before Initialize
  86. void SetMulticore(bool is_multicore);
  87. /// Resets the kernel to a clean slate for use.
  88. void Initialize();
  89. /// Initializes the CPU cores.
  90. void InitializeCores();
  91. /// Clears all resources in use by the kernel instance.
  92. void Shutdown();
  93. /// Retrieves a shared pointer to the system resource limit instance.
  94. const KResourceLimit* GetSystemResourceLimit() const;
  95. /// Retrieves a shared pointer to the system resource limit instance.
  96. KResourceLimit* GetSystemResourceLimit();
  97. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  98. KScopedAutoObject<KThread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
  99. /// Adds the given shared pointer to an internal list of active processes.
  100. void AppendNewProcess(KProcess* process);
  101. /// Makes the given process the new current process.
  102. void MakeCurrentProcess(KProcess* process);
  103. /// Retrieves a pointer to the current process.
  104. KProcess* CurrentProcess();
  105. /// Retrieves a const pointer to the current process.
  106. const KProcess* CurrentProcess() const;
  107. /// Retrieves the list of processes.
  108. const std::vector<KProcess*>& GetProcessList() const;
  109. /// Gets the sole instance of the global scheduler
  110. Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  111. /// Gets the sole instance of the global scheduler
  112. const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  113. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  114. Kernel::KScheduler& Scheduler(std::size_t id);
  115. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  116. const Kernel::KScheduler& Scheduler(std::size_t id) const;
  117. /// Gets the an instance of the respective physical CPU core.
  118. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  119. /// Gets the an instance of the respective physical CPU core.
  120. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  121. /// Gets the current physical core index for the running host thread.
  122. std::size_t CurrentPhysicalCoreIndex() const;
  123. /// Gets the sole instance of the Scheduler at the current running core.
  124. Kernel::KScheduler* CurrentScheduler();
  125. /// Gets the an instance of the current physical CPU core.
  126. Kernel::PhysicalCore& CurrentPhysicalCore();
  127. /// Gets the an instance of the current physical CPU core.
  128. const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  129. /// Gets the an instance of the TimeManager Interface.
  130. Kernel::TimeManager& TimeManager();
  131. /// Gets the an instance of the TimeManager Interface.
  132. const Kernel::TimeManager& TimeManager() const;
  133. /// Stops execution of 'id' core, in order to reschedule a new thread.
  134. void PrepareReschedule(std::size_t id);
  135. Core::ExclusiveMonitor& GetExclusiveMonitor();
  136. const Core::ExclusiveMonitor& GetExclusiveMonitor() const;
  137. KAutoObjectWithListContainer& ObjectListContainer();
  138. const KAutoObjectWithListContainer& ObjectListContainer() const;
  139. std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts();
  140. const std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts() const;
  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. /// Gets the current host_thread handle.
  170. u32 GetCurrentHostThreadID() const;
  171. /// Register the current thread as a CPU Core Thread.
  172. void RegisterCoreThread(std::size_t core_id);
  173. /// Register the current thread as a non CPU core thread.
  174. void RegisterHostThread();
  175. /// Gets the virtual memory manager for the kernel.
  176. KMemoryManager& MemoryManager();
  177. /// Gets the virtual memory manager for the kernel.
  178. const KMemoryManager& MemoryManager() 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. /// Suspend/unsuspend the OS.
  196. void Suspend(bool in_suspention);
  197. /// Exceptional exit the OS.
  198. void ExceptionalExit();
  199. bool IsMulticore() const;
  200. bool IsShuttingDown() const;
  201. void EnterSVCProfile();
  202. void ExitSVCProfile();
  203. /**
  204. * Creates a host thread to execute HLE service requests, which are used to execute service
  205. * routines asynchronously. While these are allocated per ServerSession, these need to be owned
  206. * and managed outside of ServerSession to avoid a circular dependency. In general, most
  207. * services can just use the default service thread, and not need their own host service thread.
  208. * See GetDefaultServiceThread.
  209. * @param name String name for the ServerSession creating this thread, used for debug
  210. * purposes.
  211. * @returns The a weak pointer newly created service thread.
  212. */
  213. std::weak_ptr<Kernel::ServiceThread> CreateServiceThread(const std::string& name);
  214. /**
  215. * Gets the default host service thread, which executes HLE service requests. Unless service
  216. * requests need to block on the host, the default service thread should be used in favor of
  217. * creating a new service thread.
  218. * @returns The a weak pointer for the default service thread.
  219. */
  220. std::weak_ptr<Kernel::ServiceThread> GetDefaultServiceThread() const;
  221. /**
  222. * Releases a HLE service thread, instructing KernelCore to free it. This should be called when
  223. * the ServerSession associated with the thread is destroyed.
  224. * @param service_thread Service thread to release.
  225. */
  226. void ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread);
  227. /// Workaround for single-core mode when preempting threads while idle.
  228. bool IsPhantomModeForSingleCore() const;
  229. void SetIsPhantomModeForSingleCore(bool value);
  230. Core::System& System();
  231. const Core::System& System() const;
  232. /// Gets the slab heap for the specified kernel object type.
  233. template <typename T>
  234. KSlabHeap<T>& SlabHeap() {
  235. if constexpr (std::is_same_v<T, KClientSession>) {
  236. return slab_heap_container->client_session;
  237. } else if constexpr (std::is_same_v<T, KEvent>) {
  238. return slab_heap_container->event;
  239. } else if constexpr (std::is_same_v<T, KLinkedListNode>) {
  240. return slab_heap_container->linked_list_node;
  241. } else if constexpr (std::is_same_v<T, KPort>) {
  242. return slab_heap_container->port;
  243. } else if constexpr (std::is_same_v<T, KProcess>) {
  244. return slab_heap_container->process;
  245. } else if constexpr (std::is_same_v<T, KResourceLimit>) {
  246. return slab_heap_container->resource_limit;
  247. } else if constexpr (std::is_same_v<T, KSession>) {
  248. return slab_heap_container->session;
  249. } else if constexpr (std::is_same_v<T, KSharedMemory>) {
  250. return slab_heap_container->shared_memory;
  251. } else if constexpr (std::is_same_v<T, KSharedMemoryInfo>) {
  252. return slab_heap_container->shared_memory_info;
  253. } else if constexpr (std::is_same_v<T, KThread>) {
  254. return slab_heap_container->thread;
  255. } else if constexpr (std::is_same_v<T, KTransferMemory>) {
  256. return slab_heap_container->transfer_memory;
  257. } else if constexpr (std::is_same_v<T, KWritableEvent>) {
  258. return slab_heap_container->writeable_event;
  259. } else if constexpr (std::is_same_v<T, KCodeMemory>) {
  260. return slab_heap_container->code_memory;
  261. } else if constexpr (std::is_same_v<T, KPageBuffer>) {
  262. return slab_heap_container->page_buffer;
  263. } else if constexpr (std::is_same_v<T, KThreadLocalPage>) {
  264. return slab_heap_container->thread_local_page;
  265. }
  266. }
  267. /// Gets the current slab resource counts.
  268. Init::KSlabResourceCounts& SlabResourceCounts();
  269. /// Gets the current slab resource counts.
  270. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  271. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  272. KWorkerTaskManager& WorkerTaskManager();
  273. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  274. const KWorkerTaskManager& WorkerTaskManager() const;
  275. /// Gets the memory layout.
  276. const KMemoryLayout& MemoryLayout() const;
  277. private:
  278. friend class KProcess;
  279. friend class KThread;
  280. /// Creates a new object ID, incrementing the internal object ID counter.
  281. u32 CreateNewObjectID();
  282. /// Creates a new process ID, incrementing the internal process ID counter;
  283. u64 CreateNewKernelProcessID();
  284. /// Creates a new process ID, incrementing the internal process ID counter;
  285. u64 CreateNewUserProcessID();
  286. /// Creates a new thread ID, incrementing the internal thread ID counter.
  287. u64 CreateNewThreadID();
  288. /// Provides a reference to the global handle table.
  289. KHandleTable& GlobalHandleTable();
  290. /// Provides a const reference to the global handle table.
  291. const KHandleTable& GlobalHandleTable() const;
  292. struct Impl;
  293. std::unique_ptr<Impl> impl;
  294. bool exception_exited{};
  295. private:
  296. /// Helper to encapsulate all slab heaps in a single heap allocated container
  297. struct SlabHeapContainer {
  298. KSlabHeap<KClientSession> client_session;
  299. KSlabHeap<KEvent> event;
  300. KSlabHeap<KLinkedListNode> linked_list_node;
  301. KSlabHeap<KPort> port;
  302. KSlabHeap<KProcess> process;
  303. KSlabHeap<KResourceLimit> resource_limit;
  304. KSlabHeap<KSession> session;
  305. KSlabHeap<KSharedMemory> shared_memory;
  306. KSlabHeap<KSharedMemoryInfo> shared_memory_info;
  307. KSlabHeap<KThread> thread;
  308. KSlabHeap<KTransferMemory> transfer_memory;
  309. KSlabHeap<KWritableEvent> writeable_event;
  310. KSlabHeap<KCodeMemory> code_memory;
  311. KSlabHeap<KPageBuffer> page_buffer;
  312. KSlabHeap<KThreadLocalPage> thread_local_page;
  313. };
  314. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  315. };
  316. } // namespace Kernel