kernel.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2014 Citra Emulator Project / PPSSPP 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 <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/memory/memory_types.h"
  13. #include "core/hle/kernel/object.h"
  14. namespace Core {
  15. class CPUInterruptHandler;
  16. class ExclusiveMonitor;
  17. class System;
  18. } // namespace Core
  19. namespace Core::Timing {
  20. class CoreTiming;
  21. struct EventType;
  22. } // namespace Core::Timing
  23. namespace Kernel {
  24. namespace Memory {
  25. class MemoryManager;
  26. } // namespace Memory
  27. class ClientPort;
  28. class GlobalSchedulerContext;
  29. class HandleTable;
  30. class PhysicalCore;
  31. class Process;
  32. class KResourceLimit;
  33. class KScheduler;
  34. class KSharedMemory;
  35. class ServiceThread;
  36. class Synchronization;
  37. class KThread;
  38. class TimeManager;
  39. template <typename T>
  40. class KSlabHeap;
  41. using EmuThreadHandle = uintptr_t;
  42. constexpr EmuThreadHandle EmuThreadHandleInvalid{};
  43. constexpr EmuThreadHandle EmuThreadHandleReserved{1ULL << 63};
  44. /// Represents a single instance of the kernel.
  45. class KernelCore {
  46. private:
  47. using NamedPortTable = std::unordered_map<std::string, std::shared_ptr<ClientPort>>;
  48. public:
  49. /// Constructs an instance of the kernel using the given System
  50. /// instance as a context for any necessary system-related state,
  51. /// such as threads, CPU core state, etc.
  52. ///
  53. /// @post After execution of the constructor, the provided System
  54. /// object *must* outlive the kernel instance itself.
  55. ///
  56. explicit KernelCore(Core::System& system);
  57. ~KernelCore();
  58. KernelCore(const KernelCore&) = delete;
  59. KernelCore& operator=(const KernelCore&) = delete;
  60. KernelCore(KernelCore&&) = delete;
  61. KernelCore& operator=(KernelCore&&) = delete;
  62. /// Sets if emulation is multicore or single core, must be set before Initialize
  63. void SetMulticore(bool is_multicore);
  64. /// Resets the kernel to a clean slate for use.
  65. void Initialize();
  66. /// Initializes the CPU cores.
  67. void InitializeCores();
  68. /// Clears all resources in use by the kernel instance.
  69. void Shutdown();
  70. /// Retrieves a shared pointer to the system resource limit instance.
  71. std::shared_ptr<KResourceLimit> GetSystemResourceLimit() const;
  72. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  73. std::shared_ptr<KThread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
  74. /// Adds the given shared pointer to an internal list of active processes.
  75. void AppendNewProcess(std::shared_ptr<Process> process);
  76. /// Makes the given process the new current process.
  77. void MakeCurrentProcess(Process* process);
  78. /// Retrieves a pointer to the current process.
  79. Process* CurrentProcess();
  80. /// Retrieves a const pointer to the current process.
  81. const Process* CurrentProcess() const;
  82. /// Retrieves the list of processes.
  83. const std::vector<std::shared_ptr<Process>>& GetProcessList() const;
  84. /// Gets the sole instance of the global scheduler
  85. Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  86. /// Gets the sole instance of the global scheduler
  87. const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  88. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  89. Kernel::KScheduler& Scheduler(std::size_t id);
  90. /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
  91. const Kernel::KScheduler& Scheduler(std::size_t id) const;
  92. /// Gets the an instance of the respective physical CPU core.
  93. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  94. /// Gets the an instance of the respective physical CPU core.
  95. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  96. /// Gets the sole instance of the Scheduler at the current running core.
  97. Kernel::KScheduler* CurrentScheduler();
  98. /// Gets the an instance of the current physical CPU core.
  99. Kernel::PhysicalCore& CurrentPhysicalCore();
  100. /// Gets the an instance of the current physical CPU core.
  101. const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  102. /// Gets the an instance of the TimeManager Interface.
  103. Kernel::TimeManager& TimeManager();
  104. /// Gets the an instance of the TimeManager Interface.
  105. const Kernel::TimeManager& TimeManager() const;
  106. /// Stops execution of 'id' core, in order to reschedule a new thread.
  107. void PrepareReschedule(std::size_t id);
  108. Core::ExclusiveMonitor& GetExclusiveMonitor();
  109. const Core::ExclusiveMonitor& GetExclusiveMonitor() const;
  110. std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts();
  111. const std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES>& Interrupts() const;
  112. void InvalidateAllInstructionCaches();
  113. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  114. /// Adds a port to the named port table
  115. void AddNamedPort(std::string name, std::shared_ptr<ClientPort> port);
  116. /// Finds a port within the named port table with the given name.
  117. NamedPortTable::iterator FindNamedPort(const std::string& name);
  118. /// Finds a port within the named port table with the given name.
  119. NamedPortTable::const_iterator FindNamedPort(const std::string& name) const;
  120. /// Determines whether or not the given port is a valid named port.
  121. bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
  122. /// Gets the current host_thread/guest_thread pointer.
  123. KThread* GetCurrentEmuThread() const;
  124. /// Gets the current host_thread handle.
  125. u32 GetCurrentHostThreadID() const;
  126. /// Register the current thread as a CPU Core Thread.
  127. void RegisterCoreThread(std::size_t core_id);
  128. /// Register the current thread as a non CPU core thread.
  129. void RegisterHostThread();
  130. /// Gets the virtual memory manager for the kernel.
  131. Memory::MemoryManager& MemoryManager();
  132. /// Gets the virtual memory manager for the kernel.
  133. const Memory::MemoryManager& MemoryManager() const;
  134. /// Gets the slab heap allocated for user space pages.
  135. KSlabHeap<Memory::Page>& GetUserSlabHeapPages();
  136. /// Gets the slab heap allocated for user space pages.
  137. const KSlabHeap<Memory::Page>& GetUserSlabHeapPages() const;
  138. /// Gets the shared memory object for HID services.
  139. Kernel::KSharedMemory& GetHidSharedMem();
  140. /// Gets the shared memory object for HID services.
  141. const Kernel::KSharedMemory& GetHidSharedMem() const;
  142. /// Gets the shared memory object for font services.
  143. Kernel::KSharedMemory& GetFontSharedMem();
  144. /// Gets the shared memory object for font services.
  145. const Kernel::KSharedMemory& GetFontSharedMem() const;
  146. /// Gets the shared memory object for IRS services.
  147. Kernel::KSharedMemory& GetIrsSharedMem();
  148. /// Gets the shared memory object for IRS services.
  149. const Kernel::KSharedMemory& GetIrsSharedMem() const;
  150. /// Gets the shared memory object for Time services.
  151. Kernel::KSharedMemory& GetTimeSharedMem();
  152. /// Gets the shared memory object for Time services.
  153. const Kernel::KSharedMemory& GetTimeSharedMem() const;
  154. /// Suspend/unsuspend the OS.
  155. void Suspend(bool in_suspention);
  156. /// Exceptional exit the OS.
  157. void ExceptionalExit();
  158. bool IsMulticore() const;
  159. void EnterSVCProfile();
  160. void ExitSVCProfile();
  161. /**
  162. * Creates an HLE service thread, which are used to execute service routines asynchronously.
  163. * While these are allocated per ServerSession, these need to be owned and managed outside of
  164. * ServerSession to avoid a circular dependency.
  165. * @param name String name for the ServerSession creating this thread, used for debug purposes.
  166. * @returns The a weak pointer newly created service thread.
  167. */
  168. std::weak_ptr<Kernel::ServiceThread> CreateServiceThread(const std::string& name);
  169. /**
  170. * Releases a HLE service thread, instructing KernelCore to free it. This should be called when
  171. * the ServerSession associated with the thread is destroyed.
  172. * @param service_thread Service thread to release.
  173. */
  174. void ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread);
  175. /// Workaround for single-core mode when preempting threads while idle.
  176. bool IsPhantomModeForSingleCore() const;
  177. void SetIsPhantomModeForSingleCore(bool value);
  178. private:
  179. friend class Object;
  180. friend class Process;
  181. friend class KThread;
  182. /// Creates a new object ID, incrementing the internal object ID counter.
  183. u32 CreateNewObjectID();
  184. /// Creates a new process ID, incrementing the internal process ID counter;
  185. u64 CreateNewKernelProcessID();
  186. /// Creates a new process ID, incrementing the internal process ID counter;
  187. u64 CreateNewUserProcessID();
  188. /// Creates a new thread ID, incrementing the internal thread ID counter.
  189. u64 CreateNewThreadID();
  190. /// Provides a reference to the global handle table.
  191. Kernel::HandleTable& GlobalHandleTable();
  192. /// Provides a const reference to the global handle table.
  193. const Kernel::HandleTable& GlobalHandleTable() const;
  194. struct Impl;
  195. std::unique_ptr<Impl> impl;
  196. bool exception_exited{};
  197. };
  198. } // namespace Kernel