diff options
| author | Amlal <amlalelmahrouss@icloud.com> | 2024-05-11 06:43:54 +0000 |
|---|---|---|
| committer | Amlal <amlalelmahrouss@icloud.com> | 2024-05-11 06:43:54 +0000 |
| commit | ca675beb41dba8d7d16c5793b55d1672f38be3b4 (patch) | |
| tree | c995ada42729ac2059a0ed87a4539d1a7e10b14a /Kernel/KernelKit/SMPManager.hpp | |
| parent | 2b4a4792abf51487ab4a16106f9376f43acf381a (diff) | |
| parent | bc57a29a24b98b00ba17710ba84ec2188ab73504 (diff) | |
Merged in MHR-23 (pull request #12)
MHR-23: Merge work.
Diffstat (limited to 'Kernel/KernelKit/SMPManager.hpp')
| -rw-r--r-- | Kernel/KernelKit/SMPManager.hpp | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/Kernel/KernelKit/SMPManager.hpp b/Kernel/KernelKit/SMPManager.hpp new file mode 100644 index 00000000..3d7a4189 --- /dev/null +++ b/Kernel/KernelKit/SMPManager.hpp @@ -0,0 +1,129 @@ +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#ifndef __SMP_MANAGER__ +#define __SMP_MANAGER__ + +#include <ArchKit/ArchKit.hpp> +#include <CompilerKit/CompilerKit.hxx> +#include <NewKit/Ref.hpp> + +// Last Rev +// Sat Feb 24 CET 2024 + +#define kMaxHarts 8 + +namespace NewOS +{ + using ThreadID = UInt32; + + enum ThreadKind + { + kHartSystemReserved, // System reserved thread, well user can't use it + kHartStandard, // user thread, cannot be used by kernel + kHartFallback, // fallback thread, cannot be used by user if not clear or + // used by kernel. + kHartBoot, // The core we booted from, the mama. + kInvalidHart, + kHartCount, + }; + + typedef enum ThreadKind SmThreadKind; + typedef ThreadID SmThreadID; + + /// + /// \name HardwareThread + /// @brief CPU Hardware Thread (POWER, x64, or 64x0) + /// + + class HardwareThread final + { + public: + explicit HardwareThread(); + ~HardwareThread(); + + public: + NEWOS_COPY_DEFAULT(HardwareThread) + + public: + operator bool(); + + public: + void Wake(const bool wakeup = false) noexcept; + void Busy(const bool busy = false) noexcept; + + public: + bool Switch(HAL::StackFrame* stack); + bool IsWakeup() noexcept; + + public: + HAL::StackFrame* StackFrame() noexcept; + const ThreadKind& Kind() noexcept; + bool IsBusy() noexcept; + const ThreadID& ID() noexcept; + + private: + HAL::StackFrame* fStack; + ThreadKind fKind; + ThreadID fID; + bool fWakeup; + bool fBusy; + Int64 fPID; + + private: + friend class SMPManager; + }; + + /// + /// \name SMPManager + /// @brief Multi processor manager to manage other cores and dispatch tasks. + /// + + class SMPManager final + { + private: + explicit SMPManager(); + + public: + ~SMPManager(); + + public: + NEWOS_COPY_DEFAULT(SMPManager); + + public: + bool Switch(HAL::StackFrame* the); + HAL::StackFramePtr GetStackFrame() noexcept; + + public: + Ref<HardwareThread> operator[](const SizeT& idx); + bool operator!() noexcept; + operator bool() noexcept; + + public: + /// @brief Shared instance of the SMP Manager. + /// @return the reference to the smp manager. + static Ref<SMPManager> Shared(); + + public: + /// @brief Returns the amount of threads present in the system. + /// @returns SizeT the amount of cores present. + SizeT Count() noexcept; + + private: + Array<HardwareThread, kMaxHarts> fThreadList; + ThreadID fCurrentThread{0}; + }; + + /// @brief wakes up thread. + /// wakes up thread from hang. + Void rt_wakeup_thread(HAL::StackFramePtr stack); + + /// @brief makes thread sleep. + /// hooks and hangs thread to prevent code from executing. + Void rt_hang_thread(HAL::StackFramePtr stack); +} // namespace NewOS + +#endif // !__SMP_MANAGER__ |
