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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* ========================================================
*
* hCore
* Copyright Mahrouss Logic, all rights reserved.
*
* ========================================================
*/
#pragma once
#include <KernelKit/PermissionSelector.hxx>
#include <KernelKit/FileManager.hpp>
#include <NewKit/MutableArray.hpp>
#include <NewKit/LockDelegate.hpp>
#include <ArchKit/Arch.hpp>
#include <NewKit/Heap.hpp>
#define kMinMicroTime AffinityKind::kStandard
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace hCore
{
//! @brief Process identifier.
typedef Int64 ProcessID;
//! @brief Process name length.
inline constexpr SizeT kProcessLen = 256U;
//! @brief Forward declaration.
class Process;
class ProcessManager;
class ProcessHelper;
//! @brief Process status enum.
enum class ProcessStatus : Int32
{
kStarting,
kRunning,
kKilled,
kFrozen,
kDead
};
//! @brief Affinity is the amount of nano-seconds this process is going
//! to run.
enum class AffinityKind : Int32
{
kInvalid = 300,
kVeryHigh = 250,
kHigh = 200,
kStandard = 150,
kLowUsage = 100,
kVeryLowUsage = 50,
};
// operator overloading.
inline bool operator<(AffinityKind lhs, AffinityKind rhs)
{
Int32 lhs_int = static_cast<Int>(lhs);
Int32 rhs_int = static_cast<Int>(rhs);
return lhs_int < rhs_int;
}
inline bool operator>(AffinityKind lhs, AffinityKind rhs)
{
Int32 lhs_int = static_cast<Int>(lhs);
Int32 rhs_int = static_cast<Int>(rhs);
return lhs_int > rhs_int;
}
inline bool operator<=(AffinityKind lhs, AffinityKind rhs)
{
Int32 lhs_int = static_cast<Int>(lhs);
Int32 rhs_int = static_cast<Int>(rhs);
return lhs_int <= rhs_int;
}
inline bool operator>=(AffinityKind lhs, AffinityKind rhs)
{
Int32 lhs_int = static_cast<Int>(lhs);
Int32 rhs_int = static_cast<Int>(rhs);
return lhs_int >= rhs_int;
}
// end of operator overloading.
using ProcessSubsystem = UInt32;
using ProcessTime = UInt64;
using PID = Int64;
// for permission manager, tells where we run the code.
enum class ProcessSelector : Int
{
kRingUser, /* user ring (or ring 3 in x86) */
kRingDriver, /* ring 2 in x86, hypervisor privileges in other archs */
kRingKernel, /* machine privileges */
};
// Helper types.
using ImagePtr = VoidPtr;
using HeapPtr = VoidPtr;
// @brief Process header structure.
class Process final
{
public:
explicit Process(VoidPtr startImage = nullptr) : Image(startImage) { MUST_PASS(startImage); }
~Process() = default;
HCORE_COPY_DEFAULT(Process)
private:
Char Name[kProcessLen] = { "hCore Process" };
ProcessSubsystem SubSystem;
ProcessSelector Selector;
HAL::StackFrame* StackFrame{ nullptr };
AffinityKind Affinity;
ProcessStatus Status;
// Memory, images.
HeapPtr PoolCursor{ nullptr };
ImagePtr Image{ nullptr };
HeapPtr Pool{ nullptr };
// memory usage
SizeT UsedMemory{ 0 };
SizeT FreeMemory{ 0 };
ProcessTime PTime;
PID ProcessId{ -1 };
Int32 Ring{ 3 };
public:
//! @brief boolean operator, check status.
operator bool() { return Status != ProcessStatus::kDead; }
//! @brief Crash program, exits with code ~0.
void Crash();
//! @brief Exits program.
void Exit(Int32 exit_code = 0);
//! @brief TLS new
VoidPtr New(const SizeT& sz);
//! @brief TLS delete.
Boolean Delete(VoidPtr ptr, const SizeT& sz);
//! @brief Process name getter, example: "C RunTime"
const Char* GetName();
//! @brief Wakes up threads.
void Wake(const bool wakeup = false);
public:
const ProcessSelector& GetSelector();
const ProcessStatus& GetStatus();
const AffinityKind& GetAffinity();
private:
friend ProcessManager;
friend ProcessHelper;
};
using ProcessPtr = Process*;
//! @brief Kernel scheduler..
class ProcessManager final
{
private:
explicit ProcessManager() = default;
public:
~ProcessManager() = default;
HCORE_COPY_DEFAULT(ProcessManager)
operator bool() { return m_Headers.Count() > 0; }
bool operator!() { return m_Headers.Count() == 0; }
bool Add(Ref<Process> &Header);
bool Remove(SizeT Header);
Ref<Process>& GetCurrent();
SizeT Run() noexcept;
static Ref<ProcessManager> Shared();
private:
MutableArray<Ref<Process>> m_Headers;
Ref<Process> m_CurrentProcess;
};
/*
* Just a helper class, which contains some utilities for the scheduler.
*/
class ProcessHelper final
{
public:
static bool Switch(HAL::StackFrame* newStack, const PID& newPid);
static bool CanBeScheduled(Ref<Process>& process);
static PID& GetCurrentPID();
static bool StartScheduling();
};
const Int32 &rt_get_exit_code() noexcept;
} // namespace hCore
#include <KernelKit/ThreadLocalStorage.hxx>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|