summaryrefslogtreecommitdiffhomepage
path: root/Private/Source/ProcessManager.cxx
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-29 22:38:43 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-29 22:38:43 +0100
commita8c17ccd6d97cc78830917dc6282b040b21ba16c (patch)
tree2181e96ccf9c89c677d2208661bce5584a667470 /Private/Source/ProcessManager.cxx
parent78861f1b16f18a85e9f6890e16eb320412b6ab80 (diff)
Kernel: Update SPECS and TODO list.
Cleaned up the SPECS to get into the point. Current Task: Load kernel into memory. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/Source/ProcessManager.cxx')
-rw-r--r--Private/Source/ProcessManager.cxx370
1 files changed, 161 insertions, 209 deletions
diff --git a/Private/Source/ProcessManager.cxx b/Private/Source/ProcessManager.cxx
index 987b8093..ce20cad4 100644
--- a/Private/Source/ProcessManager.cxx
+++ b/Private/Source/ProcessManager.cxx
@@ -7,13 +7,17 @@
* ========================================================
*/
-#include "NewKit/Panic.hpp"
+/***********************************************************************************/
+/// @file ProcessManager.cxx
+/// @brief Process Scheduler API.
+/***********************************************************************************/
+
#include <KernelKit/ProcessManager.hpp>
#include <KernelKit/SMPManager.hpp>
#include <NewKit/KernelHeap.hpp>
#include <NewKit/String.hpp>
-#define kPoolAlign (4)
+#include "NewKit/Panic.hpp"
///! bugs = 0
@@ -23,312 +27,260 @@
* For MT see SMPManager. */
/***********************************************************************************/
-namespace hCore
-{
+namespace hCore {
/***********************************************************************************/
/// Exit Code stuff
/***********************************************************************************/
static Int32 kExitCode = 0;
-const Int32 &rt_get_exit_code() noexcept
-{
- return kExitCode;
-}
+const Int32 &rt_get_exit_code() noexcept { return kExitCode; }
/***********************************************************************************/
-void Process::Crash()
-{
- kcout << this->Name << ": Crashed\n";
- this->Exit(-1);
+void Process::Crash() {
+ kcout << this->Name << ": Crashed\n";
+ this->Exit(-1);
}
-void Process::Wake(const bool should_wakeup)
-{
- this->Status = should_wakeup ? ProcessStatus::kRunning : ProcessStatus::kFrozen;
+void Process::Wake(const bool should_wakeup) {
+ this->Status =
+ should_wakeup ? ProcessStatus::kRunning : ProcessStatus::kFrozen;
}
/***********************************************************************************/
-VoidPtr Process::New(const SizeT &sz)
-{
- if (this->FreeMemory < 1)
- return nullptr;
-
- // RAM allocation
- if (this->PoolCursor)
- {
- VoidPtr ptr = this->PoolCursor;
- this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor + (sizeof(sz) + kPoolAlign));
+VoidPtr Process::New(const SizeT &sz) {
+ if (this->FreeMemory < 1) return nullptr;
- ++this->UsedMemory;
- --this->FreeMemory;
+ // RAM allocation
+ if (this->PoolCursor) {
+ VoidPtr ptr = this->PoolCursor;
+ this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor + (sizeof(sz)));
- return ptr;
- }
+ ++this->UsedMemory;
+ --this->FreeMemory;
- //! TODO: Disk allocation
+ return ptr;
+ }
- return nullptr;
+ return nullptr;
}
/***********************************************************************************/
/* @brief checks if runtime pointer is in region. */
-bool rt_in_pool_region(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz)
-{
- Char *_pool_ptr = (Char *)pool_ptr;
- Char *_pool = (Char *)pool;
-
- for (SizeT index = sz; _pool[sz] != 0x55; --index)
- {
- if (&_pool[index] > &_pool_ptr[sz])
- continue;
-
- if (_pool[index] == _pool_ptr[index])
- return true;
- }
+bool rt_in_pool_region(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz) {
+ Char *_pool_ptr = (Char *)pool_ptr;
+ Char *_pool = (Char *)pool;
- return false;
+ for (SizeT index = sz; _pool[sz] != 0x55; --index) {
+ if (&_pool[index] > &_pool_ptr[sz]) continue;
+
+ if (_pool[index] == _pool_ptr[index]) return true;
+ }
+
+ return false;
}
/* @brief free pointer from usage. */
-Boolean Process::Delete(VoidPtr ptr, const SizeT &sz)
-{
- if (sz < 1 || this->PoolCursor == this->Pool)
- return false;
+Boolean Process::Delete(VoidPtr ptr, const SizeT &sz) {
+ if (sz < 1 || this->PoolCursor == this->Pool) return false;
- // also check for the amount of allocations we've done so far.
- if (this->UsedMemory < 1)
- return false;
+ // also check for the amount of allocations we've done so far.
+ if (this->UsedMemory < 1) return false;
- if (rt_in_pool_region(ptr, this->PoolCursor, this->UsedMemory))
- {
- this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor - (sizeof(sz) - kPoolAlign));
- rt_zero_memory(ptr, sz);
+ if (rt_in_pool_region(ptr, this->PoolCursor, this->UsedMemory)) {
+ this->PoolCursor = (VoidPtr)((UIntPtr)this->PoolCursor - (sizeof(sz)));
+ rt_zero_memory(ptr, sz);
- ++this->FreeMemory;
- --this->UsedMemory;
+ ++this->FreeMemory;
+ --this->UsedMemory;
- return true;
- }
+ return true;
+ }
- return false;
+ return false;
}
-const Char *Process::GetName()
-{
- return this->Name;
-}
+const Char *Process::GetName() { return this->Name; }
-const ProcessSelector &Process::GetSelector()
-{
- return this->Selector;
-}
+const ProcessSelector &Process::GetSelector() { return this->Selector; }
-const ProcessStatus &Process::GetStatus()
-{
- return this->Status;
-}
+const ProcessStatus &Process::GetStatus() { return this->Status; }
/***********************************************************************************/
/**
@brief Affinity is the time slot allowed for the process.
*/
-const AffinityKind &Process::GetAffinity()
-{
- return this->Affinity;
-}
+const AffinityKind &Process::GetAffinity() { return this->Affinity; }
/**
@brief Standard exit proc.
*/
-void Process::Exit(Int32 exit_code)
-{
- if (this->ProcessId != ProcessManager::Shared().Leak().GetCurrent().Leak().ProcessId)
- panic(RUNTIME_CHECK_PROCESS);
+void Process::Exit(Int32 exit_code) {
+ if (this->ProcessId !=
+ ProcessManager::Shared().Leak().GetCurrent().Leak().ProcessId)
+ panic(RUNTIME_CHECK_PROCESS);
- if (this->Ring == (Int32)ProcessSelector::kRingKernel &&
- ProcessManager::Shared().Leak().GetCurrent().Leak().Ring > 0)
- panic(RUNTIME_CHECK_PROCESS);
+ if (this->Ring == (Int32)ProcessSelector::kRingKernel &&
+ ProcessManager::Shared().Leak().GetCurrent().Leak().Ring > 0)
+ panic(RUNTIME_CHECK_PROCESS);
- kExitCode = exit_code;
+ kExitCode = exit_code;
- if (this->Ring != (Int32)ProcessSelector::kRingDriver && this->Ring != (Int32)ProcessSelector::kRingKernel)
- {
- pool_free_ptr(this->Pool);
+ if (this->Ring != (Int32)ProcessSelector::kRingDriver &&
+ this->Ring != (Int32)ProcessSelector::kRingKernel) {
+ pool_free_ptr(this->Pool);
- this->PoolCursor = nullptr;
+ this->PoolCursor = nullptr;
- this->FreeMemory = kPoolMaxSz;
- this->UsedMemory = 0UL;
- }
+ this->FreeMemory = kPoolMaxSz;
+ this->UsedMemory = 0UL;
+ }
- //! Delete image if not done already.
- if (this->Image)
- kernel_delete_ptr(this->Image);
+ //! Delete image if not done already.
+ if (this->Image) kernel_delete_ptr(this->Image);
- if (this->StackFrame)
- kernel_delete_ptr((VoidPtr)this->StackFrame);
+ if (this->StackFrame) kernel_delete_ptr((VoidPtr)this->StackFrame);
- ProcessManager::Shared().Leak().Remove(this->ProcessId);
+ ProcessManager::Shared().Leak().Remove(this->ProcessId);
}
-bool ProcessManager::Add(Ref<Process> &process)
-{
- if (!process)
- return false;
+bool ProcessManager::Add(Ref<Process> &process) {
+ if (!process) return false;
- kcout << "ProcessManager::Add(Ref<Process>& process)\r\n";
+ kcout << "ProcessManager::Add(Ref<Process>& process)\r\n";
- process.Leak().Pool = pool_new_ptr(kPoolUser | kPoolRw);
- process.Leak().ProcessId = this->m_Headers.Count();
- process.Leak().PoolCursor = process.Leak().Pool;
+ process.Leak().Pool = pool_new_ptr(kPoolUser | kPoolRw);
+ process.Leak().ProcessId = this->m_Headers.Count();
+ process.Leak().PoolCursor = process.Leak().Pool;
- process.Leak().StackFrame =
- reinterpret_cast<HAL::StackFrame *>(kernel_new_ptr(sizeof(HAL::StackFrame), true, false));
+ process.Leak().StackFrame = reinterpret_cast<HAL::StackFrame *>(
+ kernel_new_ptr(sizeof(HAL::StackFrame), true, false));
- MUST_PASS(process.Leak().StackFrame);
+ MUST_PASS(process.Leak().StackFrame);
- UIntPtr imageStart = reinterpret_cast<UIntPtr>(process.Leak().Image);
+ UIntPtr imageStart = reinterpret_cast<UIntPtr>(process.Leak().Image);
- process.Leak().AssignStart(imageStart);
+ process.Leak().AssignStart(imageStart);
- this->m_Headers.Add(process);
+ this->m_Headers.Add(process);
- return true;
+ return true;
}
-bool ProcessManager::Remove(SizeT process)
-{
- if (process > this->m_Headers.Count())
- return false;
+bool ProcessManager::Remove(SizeT process) {
+ if (process > this->m_Headers.Count()) return false;
- kcout << "ProcessManager::Remove(SizeT process)\r\n";
+ kcout << "ProcessManager::Remove(SizeT process)\r\n";
- return this->m_Headers.Remove(process);
+ return this->m_Headers.Remove(process);
}
-SizeT ProcessManager::Run() noexcept
-{
- SizeT processIndex = 0; //! we store this guy to tell the scheduler how many things we
- //! have scheduled.
+SizeT ProcessManager::Run() noexcept {
+ SizeT processIndex = 0; //! we store this guy to tell the scheduler how many
+ //! things we have scheduled.
- for (; processIndex < this->m_Headers.Count(); ++processIndex)
- {
- auto process = this->m_Headers[processIndex];
+ for (; processIndex < this->m_Headers.Count(); ++processIndex) {
+ auto process = this->m_Headers[processIndex];
- MUST_PASS(
- process); //! no need for a MUST_PASS(process.Leak());, it is recursive because of the nature of the class;
+ MUST_PASS(process); //! no need for a MUST_PASS(process.Leak());, it is
+ //! recursive because of the nature of the class;
- //! run any process needed to be scheduled.
- if (ProcessHelper::CanBeScheduled(process.Leak()))
- {
- auto unwrapped_process = *process.Leak();
+ //! run any process needed to be scheduled.
+ if (ProcessHelper::CanBeScheduled(process.Leak())) {
+ auto unwrapped_process = *process.Leak();
- unwrapped_process.PTime = 0;
+ unwrapped_process.PTime = 0;
- // set the current process.
- m_CurrentProcess = unwrapped_process;
+ // set the current process.
+ m_CurrentProcess = unwrapped_process;
- ProcessHelper::Switch(m_CurrentProcess.Leak().StackFrame, m_CurrentProcess.Leak().ProcessId);
- }
- else
- {
- // otherwise increment the micro-time.
- ++m_CurrentProcess.Leak().PTime;
- }
+ ProcessHelper::Switch(m_CurrentProcess.Leak().StackFrame,
+ m_CurrentProcess.Leak().ProcessId);
+ } else {
+ // otherwise increment the micro-time.
+ ++m_CurrentProcess.Leak().PTime;
}
+ }
- return processIndex;
+ return processIndex;
}
-Ref<ProcessManager> ProcessManager::Shared()
-{
- static ProcessManager ref;
- return {ref};
+Ref<ProcessManager> ProcessManager::Shared() {
+ static ProcessManager ref;
+ return {ref};
}
-Ref<Process> &ProcessManager::GetCurrent()
-{
- return m_CurrentProcess;
-}
+Ref<Process> &ProcessManager::GetCurrent() { return m_CurrentProcess; }
-PID &ProcessHelper::GetCurrentPID()
-{
- kcout << "ProcessHelper::GetCurrentPID\r\n";
- return ProcessManager::Shared().Leak().GetCurrent().Leak().ProcessId;
+PID &ProcessHelper::GetCurrentPID() {
+ kcout << "ProcessHelper::GetCurrentPID\r\n";
+ return ProcessManager::Shared().Leak().GetCurrent().Leak().ProcessId;
}
-bool ProcessHelper::CanBeScheduled(Ref<Process> &process)
-{
- if (process.Leak().Status == ProcessStatus::kFrozen || process.Leak().Status == ProcessStatus::kDead)
- return false;
-
- if (process.Leak().GetStatus() == ProcessStatus::kStarting)
- {
- if (process.Leak().PTime < static_cast<Int>(kMinMicroTime))
- {
- process.Leak().Status = ProcessStatus::kRunning;
- process.Leak().Affinity = AffinityKind::kStandard;
+bool ProcessHelper::CanBeScheduled(Ref<Process> &process) {
+ if (process.Leak().Status == ProcessStatus::kFrozen ||
+ process.Leak().Status == ProcessStatus::kDead)
+ return false;
- return true;
- }
+ if (process.Leak().GetStatus() == ProcessStatus::kStarting) {
+ if (process.Leak().PTime < static_cast<Int>(kMinMicroTime)) {
+ process.Leak().Status = ProcessStatus::kRunning;
+ process.Leak().Affinity = AffinityKind::kStandard;
- ++process.Leak().PTime;
+ return true;
}
- return process.Leak().PTime > static_cast<Int>(kMinMicroTime);
+ ++process.Leak().PTime;
+ }
+
+ return process.Leak().PTime > static_cast<Int>(kMinMicroTime);
}
-bool ProcessHelper::StartScheduling()
-{
- if (ProcessHelper::CanBeScheduled(ProcessManager::Shared().Leak().GetCurrent()))
- {
- --ProcessManager::Shared().Leak().GetCurrent().Leak().PTime;
- return false;
- }
+bool ProcessHelper::StartScheduling() {
+ if (ProcessHelper::CanBeScheduled(
+ ProcessManager::Shared().Leak().GetCurrent())) {
+ --ProcessManager::Shared().Leak().GetCurrent().Leak().PTime;
+ return false;
+ }
- auto process_ref = ProcessManager::Shared().Leak();
+ auto process_ref = ProcessManager::Shared().Leak();
- if (!process_ref)
- return false; // we have nothing to schedule. simply return.
+ if (!process_ref)
+ return false; // we have nothing to schedule. simply return.
- SizeT ret = process_ref.Run();
+ SizeT ret = process_ref.Run();
- kcout << StringBuilder::FromInt("ProcessHelper::StartScheduling() iterated over: % processes\r\n", ret);
+ kcout << StringBuilder::FromInt(
+ "ProcessHelper::StartScheduling() iterated over: % processes\r\n", ret);
- return true;
+ return true;
}
-bool ProcessHelper::Switch(HAL::StackFrame *the_stack, const PID &new_pid)
-{
- if (!the_stack || new_pid < 0)
- return false;
-
- for (SizeT index = 0UL; index < kMaxHarts; ++index)
- {
- if (SMPManager::Shared().Leak()[index].Leak().StackFrame() == the_stack)
- {
- SMPManager::Shared().Leak()[index].Leak().Busy(false);
- continue;
- }
-
- if (SMPManager::Shared().Leak()[index].Leak().IsBusy())
- continue;
-
- if (SMPManager::Shared().Leak()[index].Leak().Kind() != ThreadKind::kBoot ||
- SMPManager::Shared().Leak()[index].Leak().Kind() != ThreadKind::kSystemReserved)
- {
- SMPManager::Shared().Leak()[index].Leak().Busy(true);
- ProcessHelper::GetCurrentPID() = new_pid;
-
- return SMPManager::Shared().Leak()[index].Leak().Switch(the_stack);
- }
+bool ProcessHelper::Switch(HAL::StackFrame *the_stack, const PID &new_pid) {
+ if (!the_stack || new_pid < 0) return false;
+
+ for (SizeT index = 0UL; index < kMaxHarts; ++index) {
+ if (SMPManager::Shared().Leak()[index].Leak().StackFrame() == the_stack) {
+ SMPManager::Shared().Leak()[index].Leak().Busy(false);
+ continue;
}
- return false;
+ if (SMPManager::Shared().Leak()[index].Leak().IsBusy()) continue;
+
+ if (SMPManager::Shared().Leak()[index].Leak().Kind() != ThreadKind::kBoot ||
+ SMPManager::Shared().Leak()[index].Leak().Kind() !=
+ ThreadKind::kSystemReserved) {
+ SMPManager::Shared().Leak()[index].Leak().Busy(true);
+ ProcessHelper::GetCurrentPID() = new_pid;
+
+ return SMPManager::Shared().Leak()[index].Leak().Switch(the_stack);
+ }
+ }
+
+ return false;
}
-} // namespace hCore
+} // namespace hCore