kernel.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <memory>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include "core/hle/kernel/object.h"
  10. namespace Core {
  11. class ExclusiveMonitor;
  12. class System;
  13. } // namespace Core
  14. namespace Core::Timing {
  15. class CoreTiming;
  16. struct EventType;
  17. } // namespace Core::Timing
  18. namespace Kernel {
  19. class AddressArbiter;
  20. class ClientPort;
  21. class GlobalScheduler;
  22. class HandleTable;
  23. class PhysicalCore;
  24. class Process;
  25. class ResourceLimit;
  26. class Synchronization;
  27. class Thread;
  28. /// Represents a single instance of the kernel.
  29. class KernelCore {
  30. private:
  31. using NamedPortTable = std::unordered_map<std::string, std::shared_ptr<ClientPort>>;
  32. public:
  33. /// Constructs an instance of the kernel using the given System
  34. /// instance as a context for any necessary system-related state,
  35. /// such as threads, CPU core state, etc.
  36. ///
  37. /// @post After execution of the constructor, the provided System
  38. /// object *must* outlive the kernel instance itself.
  39. ///
  40. explicit KernelCore(Core::System& system);
  41. ~KernelCore();
  42. KernelCore(const KernelCore&) = delete;
  43. KernelCore& operator=(const KernelCore&) = delete;
  44. KernelCore(KernelCore&&) = delete;
  45. KernelCore& operator=(KernelCore&&) = delete;
  46. /// Resets the kernel to a clean slate for use.
  47. void Initialize();
  48. /// Clears all resources in use by the kernel instance.
  49. void Shutdown();
  50. /// Retrieves a shared pointer to the system resource limit instance.
  51. std::shared_ptr<ResourceLimit> GetSystemResourceLimit() const;
  52. /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
  53. std::shared_ptr<Thread> RetrieveThreadFromWakeupCallbackHandleTable(Handle handle) const;
  54. /// Adds the given shared pointer to an internal list of active processes.
  55. void AppendNewProcess(std::shared_ptr<Process> process);
  56. /// Makes the given process the new current process.
  57. void MakeCurrentProcess(Process* process);
  58. /// Retrieves a pointer to the current process.
  59. Process* CurrentProcess();
  60. /// Retrieves a const pointer to the current process.
  61. const Process* CurrentProcess() const;
  62. /// Retrieves the list of processes.
  63. const std::vector<std::shared_ptr<Process>>& GetProcessList() const;
  64. /// Gets the sole instance of the global scheduler
  65. Kernel::GlobalScheduler& GlobalScheduler();
  66. /// Gets the sole instance of the global scheduler
  67. const Kernel::GlobalScheduler& GlobalScheduler() const;
  68. /// Gets the an instance of the respective physical CPU core.
  69. Kernel::PhysicalCore& PhysicalCore(std::size_t id);
  70. /// Gets the an instance of the respective physical CPU core.
  71. const Kernel::PhysicalCore& PhysicalCore(std::size_t id) const;
  72. /// Gets the an instance of the Synchronization Interface.
  73. Kernel::Synchronization& Synchronization();
  74. /// Gets the an instance of the Synchronization Interface.
  75. const Kernel::Synchronization& Synchronization() const;
  76. /// Stops execution of 'id' core, in order to reschedule a new thread.
  77. void PrepareReschedule(std::size_t id);
  78. Core::ExclusiveMonitor& GetExclusiveMonitor();
  79. const Core::ExclusiveMonitor& GetExclusiveMonitor() const;
  80. void InvalidateAllInstructionCaches();
  81. /// Adds a port to the named port table
  82. void AddNamedPort(std::string name, std::shared_ptr<ClientPort> port);
  83. /// Finds a port within the named port table with the given name.
  84. NamedPortTable::iterator FindNamedPort(const std::string& name);
  85. /// Finds a port within the named port table with the given name.
  86. NamedPortTable::const_iterator FindNamedPort(const std::string& name) const;
  87. /// Determines whether or not the given port is a valid named port.
  88. bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
  89. private:
  90. friend class Object;
  91. friend class Process;
  92. friend class Thread;
  93. /// Creates a new object ID, incrementing the internal object ID counter.
  94. u32 CreateNewObjectID();
  95. /// Creates a new process ID, incrementing the internal process ID counter;
  96. u64 CreateNewKernelProcessID();
  97. /// Creates a new process ID, incrementing the internal process ID counter;
  98. u64 CreateNewUserProcessID();
  99. /// Creates a new thread ID, incrementing the internal thread ID counter.
  100. u64 CreateNewThreadID();
  101. /// Retrieves the event type used for thread wakeup callbacks.
  102. const std::shared_ptr<Core::Timing::EventType>& ThreadWakeupCallbackEventType() const;
  103. /// Provides a reference to the thread wakeup callback handle table.
  104. Kernel::HandleTable& ThreadWakeupCallbackHandleTable();
  105. /// Provides a const reference to the thread wakeup callback handle table.
  106. const Kernel::HandleTable& ThreadWakeupCallbackHandleTable() const;
  107. struct Impl;
  108. std::unique_ptr<Impl> impl;
  109. };
  110. } // namespace Kernel