kernel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/memory_types.h"
  16. #include "core/hle/kernel/svc_common.h"
  17. namespace Core {
  18. class CPUInterruptHandler;
  19. class ExclusiveMonitor;
  20. class System;
  21. } // namespace Core
  22. namespace Core::Timing {
  23. class CoreTiming;
  24. struct EventType;
  25. } // namespace Core::Timing
  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 KEvent;
  35. class KHandleTable;
  36. class KLinkedListNode;
  37. class KMemoryLayout;
  38. class KMemoryManager;
  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 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 InvalidateAllInstructionCaches();
  141. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  142. /// Registers a named HLE service, passing a factory used to open a port to that service.
  143. void RegisterNamedService(std::string name, ServiceInterfaceFactory&& factory);
  144. /// Opens a port to a service previously registered with RegisterNamedService.
  145. KClientPort* CreateNamedServicePort(std::string name);
  146. /// Registers a server session with the gobal emulation state, to be freed on shutdown. This is
  147. /// necessary because we do not emulate processes for HLE sessions.
  148. void RegisterServerSession(KServerSession* server_session);
  149. /// Unregisters a server session previously registered with RegisterServerSession when it was
  150. /// destroyed during the current emulation session.
  151. void UnregisterServerSession(KServerSession* server_session);
  152. /// Registers all kernel objects with the global emulation state, this is purely for tracking
  153. /// leaks after emulation has been shutdown.
  154. void RegisterKernelObject(KAutoObject* object);
  155. /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
  156. /// destroyed during the current emulation session.
  157. void UnregisterKernelObject(KAutoObject* object);
  158. /// Registers kernel objects with guest in use state, this is purely for close
  159. /// after emulation has been shutdown.
  160. void RegisterInUseObject(KAutoObject* object);
  161. /// Unregisters a kernel object previously registered with RegisterInUseObject when it was
  162. /// destroyed during the current emulation session.
  163. void UnregisterInUseObject(KAutoObject* object);
  164. /// Determines whether or not the given port is a valid named port.
  165. bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
  166. /// Gets the current host_thread/guest_thread pointer.
  167. KThread* GetCurrentEmuThread() const;
  168. /// Gets the current host_thread handle.
  169. u32 GetCurrentHostThreadID() const;
  170. /// Register the current thread as a CPU Core Thread.
  171. void RegisterCoreThread(std::size_t core_id);
  172. /// Register the current thread as a non CPU core thread.
  173. void RegisterHostThread();
  174. /// Gets the virtual memory manager for the kernel.
  175. KMemoryManager& MemoryManager();
  176. /// Gets the virtual memory manager for the kernel.
  177. const KMemoryManager& MemoryManager() const;
  178. /// Gets the slab heap allocated for user space pages.
  179. KSlabHeap<Page>& GetUserSlabHeapPages();
  180. /// Gets the slab heap allocated for user space pages.
  181. const KSlabHeap<Page>& GetUserSlabHeapPages() const;
  182. /// Gets the shared memory object for HID services.
  183. Kernel::KSharedMemory& GetHidSharedMem();
  184. /// Gets the shared memory object for HID services.
  185. const Kernel::KSharedMemory& GetHidSharedMem() const;
  186. /// Gets the shared memory object for font services.
  187. Kernel::KSharedMemory& GetFontSharedMem();
  188. /// Gets the shared memory object for font services.
  189. const Kernel::KSharedMemory& GetFontSharedMem() const;
  190. /// Gets the shared memory object for IRS services.
  191. Kernel::KSharedMemory& GetIrsSharedMem();
  192. /// Gets the shared memory object for IRS services.
  193. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  194. /// Gets the shared memory object for Time services.
  195. Kernel::KSharedMemory& GetTimeSharedMem();
  196. /// Gets the shared memory object for Time services.
  197. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  198. /// Suspend/unsuspend the OS.
  199. void Suspend(bool in_suspention);
  200. /// Exceptional exit the OS.
  201. void ExceptionalExit();
  202. bool IsMulticore() const;
  203. bool IsShuttingDown() const;
  204. void EnterSVCProfile();
  205. void ExitSVCProfile();
  206. /**
  207. * Creates an HLE service thread, which are used to execute service routines asynchronously.
  208. * While these are allocated per ServerSession, these need to be owned and managed outside
  209. * of ServerSession to avoid a circular dependency.
  210. * @param name String name for the ServerSession creating this thread, used for debug
  211. * purposes.
  212. * @returns The a weak pointer newly created service thread.
  213. */
  214. std::weak_ptr<Kernel::ServiceThread> CreateServiceThread(const std::string& name);
  215. /**
  216. * Releases a HLE service thread, instructing KernelCore to free it. This should be called when
  217. * the ServerSession associated with the thread is destroyed.
  218. * @param service_thread Service thread to release.
  219. */
  220. void ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread);
  221. /// Workaround for single-core mode when preempting threads while idle.
  222. bool IsPhantomModeForSingleCore() const;
  223. void SetIsPhantomModeForSingleCore(bool value);
  224. Core::System& System();
  225. const Core::System& System() const;
  226. /// Gets the slab heap for the specified kernel object type.
  227. template <typename T>
  228. KSlabHeap<T>& SlabHeap() {
  229. if constexpr (std::is_same_v<T, KClientSession>) {
  230. return slab_heap_container->client_session;
  231. } else if constexpr (std::is_same_v<T, KEvent>) {
  232. return slab_heap_container->event;
  233. } else if constexpr (std::is_same_v<T, KLinkedListNode>) {
  234. return slab_heap_container->linked_list_node;
  235. } else if constexpr (std::is_same_v<T, KPort>) {
  236. return slab_heap_container->port;
  237. } else if constexpr (std::is_same_v<T, KProcess>) {
  238. return slab_heap_container->process;
  239. } else if constexpr (std::is_same_v<T, KResourceLimit>) {
  240. return slab_heap_container->resource_limit;
  241. } else if constexpr (std::is_same_v<T, KSession>) {
  242. return slab_heap_container->session;
  243. } else if constexpr (std::is_same_v<T, KSharedMemory>) {
  244. return slab_heap_container->shared_memory;
  245. } else if constexpr (std::is_same_v<T, KSharedMemoryInfo>) {
  246. return slab_heap_container->shared_memory_info;
  247. } else if constexpr (std::is_same_v<T, KThread>) {
  248. return slab_heap_container->thread;
  249. } else if constexpr (std::is_same_v<T, KTransferMemory>) {
  250. return slab_heap_container->transfer_memory;
  251. } else if constexpr (std::is_same_v<T, KWritableEvent>) {
  252. return slab_heap_container->writeable_event;
  253. } else if constexpr (std::is_same_v<T, KCodeMemory>) {
  254. return slab_heap_container->code_memory;
  255. }
  256. }
  257. /// Gets the current slab resource counts.
  258. Init::KSlabResourceCounts& SlabResourceCounts();
  259. /// Gets the current slab resource counts.
  260. const Init::KSlabResourceCounts& SlabResourceCounts() const;
  261. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  262. KWorkerTaskManager& WorkerTaskManager();
  263. /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks.
  264. const KWorkerTaskManager& WorkerTaskManager() const;
  265. /// Gets the memory layout.
  266. const KMemoryLayout& MemoryLayout() const;
  267. private:
  268. friend class KProcess;
  269. friend class KThread;
  270. /// Creates a new object ID, incrementing the internal object ID counter.
  271. u32 CreateNewObjectID();
  272. /// Creates a new process ID, incrementing the internal process ID counter;
  273. u64 CreateNewKernelProcessID();
  274. /// Creates a new process ID, incrementing the internal process ID counter;
  275. u64 CreateNewUserProcessID();
  276. /// Creates a new thread ID, incrementing the internal thread ID counter.
  277. u64 CreateNewThreadID();
  278. /// Provides a reference to the global handle table.
  279. KHandleTable& GlobalHandleTable();
  280. /// Provides a const reference to the global handle table.
  281. const KHandleTable& GlobalHandleTable() const;
  282. struct Impl;
  283. std::unique_ptr<Impl> impl;
  284. bool exception_exited{};
  285. private:
  286. /// Helper to encapsulate all slab heaps in a single heap allocated container
  287. struct SlabHeapContainer {
  288. KSlabHeap<KClientSession> client_session;
  289. KSlabHeap<KEvent> event;
  290. KSlabHeap<KLinkedListNode> linked_list_node;
  291. KSlabHeap<KPort> port;
  292. KSlabHeap<KProcess> process;
  293. KSlabHeap<KResourceLimit> resource_limit;
  294. KSlabHeap<KSession> session;
  295. KSlabHeap<KSharedMemory> shared_memory;
  296. KSlabHeap<KSharedMemoryInfo> shared_memory_info;
  297. KSlabHeap<KThread> thread;
  298. KSlabHeap<KTransferMemory> transfer_memory;
  299. KSlabHeap<KWritableEvent> writeable_event;
  300. KSlabHeap<KCodeMemory> code_memory;
  301. };
  302. std::unique_ptr<SlabHeapContainer> slab_heap_container;
  303. };
  304. } // namespace Kernel