kernel.h 8.9 KB

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