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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/* -------------------------------------------
Copyright ZKA Technologies.
FILE: UserProcessScheduler.cxx
PURPOSE: Low Exception Process scheduler.
------------------------------------------- */
/***********************************************************************************/
/// @file UserProcessScheduler.cxx
/// @brief User Process scheduler.
/***********************************************************************************/
#include <KernelKit/UserProcessScheduler.hxx>
#include <KernelKit/IPEFDLLObject.hxx>
#include <KernelKit/HardwareThreadScheduler.hxx>
#include <KernelKit/Heap.hxx>
#include <NewKit/String.hxx>
#include <KernelKit/LPC.hxx>
///! BUGS: 0
/***********************************************************************************/
/** TODO: Document the Kernel, SDK and kits. */
/***********************************************************************************/
namespace Kernel
{
/***********************************************************************************/
/// @brief Exit Code global variable.
/***********************************************************************************/
STATIC UInt32 cLastExitCode = 0U;
/***********************************************************************************/
/// @brief User Process scheduler global object.
/***********************************************************************************/
UserProcessScheduler* cProcessScheduler = nullptr;
/// @brief Gets the last exit code.
/// @note Not thread-safe.
/// @return Int32 the last exit code.
const UInt32& sched_get_exit_code(void) noexcept
{
return cLastExitCode;
}
/***********************************************************************************/
/// @brief crash current process.
/***********************************************************************************/
Void UserProcess::Crash()
{
if (*this->Name == 0)
return;
kcout << this->Name << ": crashed, ID = " << number(kErrorProcessFault) << endl;
this->Exit(kErrorProcessFault);
}
/// @brief Gets the local last exit code.
/// @note Not thread-safe.
/// @return Int32 the last exit code.
const UInt32& UserProcess::GetExitCode() noexcept
{
return this->fLastExitCode;
}
/***********************************************************************************/
/// @brief Error code variable getter.
/***********************************************************************************/
Int32& UserProcess::GetLocalCode() noexcept
{
return fLocalCode;
}
void UserProcess::Wake(const bool should_wakeup)
{
this->Status =
should_wakeup ? ProcessStatusKind::kRunning : ProcessStatusKind::kFrozen;
}
/***********************************************************************************/
/** @brief Add pointer to entry. */
VoidPtr UserProcess::New(const SizeT& sz)
{
#ifdef __ZKA_AMD64__
auto pd = hal_read_cr3();
hal_write_cr3(reinterpret_cast<VoidPtr>(this->MemoryPD));
auto ptr = mm_new_ke_heap(sz, Yes, Yes);
hal_write_cr3(reinterpret_cast<VoidPtr>(pd));
#else
auto ptr = mm_new_ke_heap(sz, Yes, Yes);
#endif
if (!this->MemoryEntryList)
{
this->MemoryEntryList = new UserProcess::PROCESS_MEMORY_ENTRY();
this->MemoryEntryList->MemoryEntry = ptr;
this->MemoryEntryList->MemoryPrev = nullptr;
this->MemoryEntryList->MemoryNext = nullptr;
return ptr;
}
else
{
auto entry = this->MemoryEntryList;
while (entry->MemoryNext)
{
if (entry->MemoryNext)
entry = entry->MemoryNext;
}
entry->MemoryNext = new UserProcess::PROCESS_MEMORY_ENTRY();
entry->MemoryNext->MemoryEntry = ptr;
entry->MemoryNext->MemoryPrev = entry;
entry->MemoryNext->MemoryNext = nullptr;
}
return nullptr;
}
/***********************************************************************************/
/** @brief Free pointer from usage. */
Boolean UserProcess::Delete(VoidPtr ptr, const SizeT& sz)
{
auto entry = this->MemoryEntryList;
while (entry->MemoryNext)
{
if (entry->MemoryEntry == ptr)
{
#ifdef __ZKA_AMD64__
auto pd = hal_read_cr3();
hal_write_cr3(reinterpret_cast<VoidPtr>(this->MemoryPD));
bool ret = mm_delete_ke_heap(ptr);
hal_write_cr3(reinterpret_cast<VoidPtr>(pd));
return ret;
#else
bool ret = mm_delete_ke_heap(ptr);
return ret;
#endif
}
if (entry->MemoryNext)
entry = entry->MemoryNext;
}
return false;
}
/// @brief UserProcess name getter.
const Char* UserProcess::GetProcessName() noexcept
{
return this->Name;
}
/// @brief UserProcess user getter.
const User* UserProcess::GetOwner() noexcept
{
return this->Owner;
}
/// @brief UserProcess status getter.
const ProcessStatusKind& UserProcess::GetStatus() noexcept
{
return this->Status;
}
/***********************************************************************************/
/**
@brief Affinity is the time slot allowed for the process.
*/
const AffinityKind& UserProcess::GetAffinity() noexcept
{
return this->Affinity;
}
/**
@brief Standard exit proc.
*/
void UserProcess::Exit(const Int32& exit_code)
{
this->Status = ProcessStatusKind::kDead;
fLastExitCode = exit_code;
cLastExitCode = exit_code;
//! Delete image if not done already.
if (this->Image && mm_is_valid_heap(this->Image))
mm_delete_ke_heap(this->Image);
if (this->StackFrame && mm_is_valid_heap(this->StackFrame))
mm_delete_ke_heap((VoidPtr)this->StackFrame);
this->Image = nullptr;
this->StackFrame = nullptr;
if (this->Kind == kDLLKind)
{
Bool success = false;
rtl_fini_dll(this, this->DLLPtr, &success);
if (success)
{
this->DLLPtr = nullptr;
}
}
if (this->StackReserve)
delete[] this->StackReserve;
this->ProcessId = 0;
if (this->ProcessId > 0)
UserProcessScheduler::The().Remove(this->ProcessId);
}
/// @brief Add process to list.
/// @param process the process *Ref* class.
/// @return the process index inside the team.
SizeT UserProcessScheduler::Add(UserProcess process)
{
if (mTeam.mProcessAmount > kSchedProcessLimitPerTeam)
return 0;
#ifdef __ZKA_AMD64__
process.MemoryPD = reinterpret_cast<UIntPtr>(hal_read_cr3());
#endif // __ZKA_AMD64__
process.Status = ProcessStatusKind::kStarting;
process.StackFrame = (HAL::StackFramePtr)mm_new_ke_heap(sizeof(HAL::StackFrame), Yes, Yes);
if (!process.StackFrame)
{
process.Crash();
return -kErrorProcessFault;
}
// Create heap according to type of process.
if (process.Kind == UserProcess::kDLLKind)
{
process.DLLPtr = rtl_init_dll(&process);
}
if (!process.Image)
{
if (process.Kind != UserProcess::kDLLKind)
{
process.Crash();
return -kErrorProcessFault;
}
}
// get preferred stack size by app.
const auto cMaxStackSize = process.StackSize;
process.StackReserve = (UInt8*)mm_new_ke_heap(sizeof(UInt8) * cMaxStackSize, Yes, Yes);
if (!process.StackReserve)
{
mm_delete_ke_heap(process.StackFrame);
process.StackFrame = nullptr;
return -kErrorProcessFault;
}
++mTeam.mProcessAmount;
process.ProcessId = mTeam.mProcessAmount;
process.Status = ProcessStatusKind::kRunning;
// avoid the pitfalls of moving process.
auto ret_pid = process.ProcessId;
mTeam.AsArray()[process.ProcessId] = move(process);
return ret_pid;
}
/***********************************************************************************/
UserProcessScheduler& UserProcessScheduler::The()
{
MUST_PASS(cProcessScheduler);
return *cProcessScheduler;
}
/***********************************************************************************/
/// @brief Remove process from list.
/// @param process_id process slot inside team.
/// @retval true process was removed.
/// @retval false process doesn't exist in team.
Bool UserProcessScheduler::Remove(ProcessID process_id)
{
// check if process is within range.
if (process_id > mTeam.AsArray().Count())
return false;
mTeam.AsArray()[process_id].Status = ProcessStatusKind::kDead;
--mTeam.mProcessAmount;
return true;
}
/// @brief Run scheduler.
/// @return
SizeT UserProcessScheduler::Run() noexcept
{
SizeT process_index = 0; //! we store this guy to tell the scheduler how many
//! things we have scheduled.
for (; process_index < mTeam.AsArray().Capacity(); ++process_index)
{
kcout << "Grabbing available process...\r";
auto& process = mTeam.AsArray()[process_index];
//! check if process needs to be scheduled.
if (UserProcessHelper::CanBeScheduled(process))
{
kcout << process.Name << ": will be runned.\r";
// Set current process header.
this->CurrentProcess() = process;
process.PTime = static_cast<Int32>(process.Affinity);
// tell helper to find a core to schedule on.
if (!UserProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize - 1], process.StackFrame,
process.ProcessId))
{
process.Crash();
continue;
}
}
}
kcout << "Scheduled Process Count: " << number(process_index) << endl;
return process_index;
}
/// @brief Gets the current scheduled team.
/// @return
UserProcessTeam& UserProcessScheduler::CurrentTeam()
{
return mTeam;
}
/// @internal
/// @brief Gets current running process.
/// @return
Ref<UserProcess>& UserProcessScheduler::CurrentProcess()
{
return mTeam.AsRef();
}
/// @brief Current proccess id getter.
/// @return UserProcess ID integer.
PID& UserProcessHelper::TheCurrentPID()
{
kcout << "UserProcessHelper::TheCurrentPID: Leaking ProcessId...\r";
return cProcessScheduler->CurrentProcess().Leak().ProcessId;
}
/// @brief Check if process can be schedulded.
/// @param process the process reference.
/// @retval true can be schedulded.
/// @retval false cannot be schedulded.
bool UserProcessHelper::CanBeScheduled(const UserProcess& process)
{
kcout << "Checking process status...\r";
if (process.Status == ProcessStatusKind::kFrozen ||
process.Status == ProcessStatusKind::kDead)
return No;
if (!process.Image &&
process.Kind == UserProcess::kExeKind)
return No;
return Yes;
}
/**
* @brief Scheduler helper class.
*/
EXTERN
HardwareThreadScheduler* cHardwareThreadScheduler;
SizeT UserProcessHelper::StartScheduling()
{
if (!cHardwareThreadScheduler)
{
cHardwareThreadScheduler = mm_new_class<HardwareThreadScheduler>();
MUST_PASS(cHardwareThreadScheduler);
}
if (!cProcessScheduler)
{
cProcessScheduler = mm_new_class<UserProcessScheduler>();
MUST_PASS(cProcessScheduler);
}
SizeT ret = cProcessScheduler->Run();
return ret;
}
/**
* \brief Does a context switch in a CPU.
* \param the_stack the stackframe of the running app.
* \param new_pid the process's PID.
*/
Bool UserProcessHelper::Switch(VoidPtr image_ptr, UInt8* stack, HAL::StackFramePtr frame_ptr, const PID& new_pid)
{
if (!stack || !frame_ptr || !image_ptr || new_pid < 0)
return No;
for (SizeT index = 0UL; index < HardwareThreadScheduler::The().Count(); ++index)
{
if (HardwareThreadScheduler::The()[index].Leak()->Kind() == kInvalidHart)
continue;
if (HardwareThreadScheduler::The()[index].Leak()->Kind() !=
ThreadKind::kHartBoot &&
HardwareThreadScheduler::The()[index].Leak()->Kind() !=
ThreadKind::kHartSystemReserved)
{
PID prev_pid = UserProcessHelper::TheCurrentPID();
UserProcessHelper::TheCurrentPID() = new_pid;
auto prev_ptime = HardwareThreadScheduler::The()[index].Leak()->fPTime;
HardwareThreadScheduler::The()[index].Leak()->fPTime = UserProcessScheduler::The().CurrentTeam().AsArray()[new_pid].ProcessId;
Bool ret = HardwareThreadScheduler::The()[index].Leak()->Switch(image_ptr, stack, frame_ptr);
if (!ret)
{
HardwareThreadScheduler::The()[index].Leak()->fPTime = prev_ptime;
UserProcessHelper::TheCurrentPID() = prev_pid;
continue;
}
}
}
return false;
}
/// @brief this checks if any process is on the team.
UserProcessScheduler::operator bool()
{
return mTeam.AsArray().Count() > 0;
}
/// @brief this checks if no process is on the team.
bool UserProcessScheduler::operator!()
{
return mTeam.AsArray().Count() == 0;
}
} // namespace Kernel
|