summaryrefslogtreecommitdiffhomepage
path: root/Private/KernelKit/SMPManager.hpp
blob: b4d04dde040974b351ebef0784b190d49da8a5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -------------------------------------------

    Copyright Mahrouss Logic

------------------------------------------- */

#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,
};

///
/// \name HardwareThread
/// @brief CPU Hardware Thread (PowerPC, Intel, or NewCPU)
///

class HardwareThread final {
 public:
  explicit HardwareThread();
  ~HardwareThread();

 public:
  HCORE_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* m_Stack;
  ThreadKind m_Kind;
  ThreadID m_ID;
  bool m_Wakeup;
  bool m_Busy;
  Int64 m_PID;

 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:
  HCORE_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> m_ThreadList;
  ThreadID m_CurrentThread{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__