kernel.h 4.7 KB

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