summaryrefslogtreecommitdiffhomepage
path: root/Private/Source
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
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')
-rw-r--r--Private/Source/Crc32.cxx2
-rw-r--r--Private/Source/ErrorOr.cxx5
-rw-r--r--Private/Source/ProcessManager.cxx370
-rw-r--r--Private/Source/SharedObjectEntry.cxx (renamed from Private/Source/SharedObject.cxx)16
-rw-r--r--Private/Source/Storage/ATA.cxx21
5 files changed, 190 insertions, 224 deletions
diff --git a/Private/Source/Crc32.cxx b/Private/Source/Crc32.cxx
index 37663b8d..e711626d 100644
--- a/Private/Source/Crc32.cxx
+++ b/Private/Source/Crc32.cxx
@@ -67,7 +67,7 @@ static Int crc_byte(Int crc, UChar byte) {
return crc;
}
-Int crc32(const Char *byte, Int len) {
+Int ke_crc32(const Char *byte, Int len) noexcept {
Int checksum = 0;
for (UChar index = 1; index < len; ++index)
diff --git a/Private/Source/ErrorOr.cxx b/Private/Source/ErrorOr.cxx
index 4137c243..aff86ed0 100644
--- a/Private/Source/ErrorOr.cxx
+++ b/Private/Source/ErrorOr.cxx
@@ -9,4 +9,7 @@
#include <NewKit/ErrorOr.hpp>
-/* BUILDME needs that file */
+/***********************************************************************************/
+/// @file ErrorOr.cxx
+/// @brief ErrorOr class.
+/***********************************************************************************/
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
diff --git a/Private/Source/SharedObject.cxx b/Private/Source/SharedObjectEntry.cxx
index 0e824b27..9fe14b39 100644
--- a/Private/Source/SharedObject.cxx
+++ b/Private/Source/SharedObjectEntry.cxx
@@ -17,13 +17,16 @@
using namespace hCore;
/***********************************************************************************/
+/// @file SharedObjectEntry.cxx
+/// @brief Shared Object Init code.
+/***********************************************************************************/
-extern "C" SharedObject *__LibMain(VoidPtr image) {
- /***********************************************************************************/
- /* Allocate new library to be added to the lookup table.
- */
- /***********************************************************************************/
+/***********************************************************************************/
+/* @brief Allocate new library to be added to the lookup table.
+ */
+/***********************************************************************************/
+extern "C" SharedObject *__LibMain(VoidPtr image) {
SharedObject *library = hcore_tls_new_class<SharedObject>();
if (!library) {
@@ -44,9 +47,10 @@ extern "C" SharedObject *__LibMain(VoidPtr image) {
library->Get()->fImageObject =
ProcessManager::Shared().Leak().GetCurrent().Leak().Image;
+
library->Get()->fImageEntrypointOffset = library->Load<VoidPtr>(kPefStart);
- kcout << "__LibMain: Done allocating Shared Library...\n";
+ kcout << "__LibMain: Done jumping to library...\n";
return library;
}
diff --git a/Private/Source/Storage/ATA.cxx b/Private/Source/Storage/ATA.cxx
index f960907b..ed62df4f 100644
--- a/Private/Source/Storage/ATA.cxx
+++ b/Private/Source/Storage/ATA.cxx
@@ -10,8 +10,12 @@
#include <ArchKit/Arch.hpp>
#include <StorageKit/ATA.hpp>
-//! @brief Driver for ATA, listens to a specific address for data to come.
-//! mapped by NewFirmware.
+#include "NewKit/Utils.hpp"
+
+//! @brief ATA DMA Driver
+//! mapped by UEFI.
+
+/// bugs 0
#define kATAError 2
@@ -51,10 +55,13 @@ const char* ata_read_28(ULong lba) {
return buffer;
}
+#define kBufferLen 512
+
const char* ata_read_48(ULong lba) {
- static char buffer[512];
+ static char buffer[kBufferLen];
+ rt_set_memory(buffer, 0, kBufferLen);
- UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress();
+ UIntPtr* packet = reinterpret_cast<UIntPtr*>(kPrdt.Leak()->PhysicalAddress());
packet[0] = k48BitRead;
packet[1] = (UIntPtr)&buffer;
@@ -65,11 +72,11 @@ const char* ata_read_48(ULong lba) {
return buffer;
}
-Int32 ata_write_48(ULong lba, const char* text) {
- UIntPtr* packet = (UIntPtr*)kPrdt.Leak()->PhysicalAddress();
+Int32 ata_write_48(ULong lba, const char* buffer) {
+ UIntPtr* packet = reinterpret_cast<UIntPtr*>(kPrdt.Leak()->PhysicalAddress());
packet[0] = k48BitWrite;
- packet[1] = (UIntPtr)&text;
+ packet[1] = (UIntPtr)&buffer;
packet[2] = lba;
rt_wait_for_io();