summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64/HalKernelMain.cc
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-02 19:38:46 +0200
committerGitHub <noreply@github.com>2025-05-02 19:38:46 +0200
commit997be16e5ac9a68d54882ab69529815860d62955 (patch)
tree19d6129c2d776bb1edc5d4a7325e39ca176c3403 /dev/kernel/HALKit/AMD64/HalKernelMain.cc
parent618104e74c195d7508a18450524f8ed7f9af8cc6 (diff)
parentb3b4b1ebdcd6adeac914869017c86d892b7a8ced (diff)
Merge pull request #28 from nekernel-org/dev
0.0.2
Diffstat (limited to 'dev/kernel/HALKit/AMD64/HalKernelMain.cc')
-rw-r--r--dev/kernel/HALKit/AMD64/HalKernelMain.cc201
1 files changed, 126 insertions, 75 deletions
diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc
index ce8c1245..f41979bb 100644
--- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc
+++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc
@@ -1,105 +1,156 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
-#include <StorageKit/AHCI.h>
#include <ArchKit/ArchKit.h>
-#include <KernelKit/ProcessScheduler.h>
-#include <KernelKit/HardwareThreadScheduler.h>
-#include <KernelKit/CodeMgr.h>
-#include <modules/ACPI/ACPIFactoryInterface.h>
-#include <NetworkKit/IPC.h>
#include <CFKit/Property.h>
-#include <modules/CoreGfx/TextGfx.h>
-#include <KernelKit/Timer.h>
#include <FirmwareKit/EFI/API.h>
#include <FirmwareKit/EFI/EFI.h>
+#include <KernelKit/CodeMgr.h>
+#include <KernelKit/HardwareThreadScheduler.h>
+#include <KernelKit/ProcessScheduler.h>
+#include <KernelKit/Timer.h>
+#include <NetworkKit/IPC.h>
+#include <StorageKit/AHCI.h>
+#include <modules/ACPI/ACPIFactoryInterface.h>
+#include <modules/CoreGfx/TextGfx.h>
+#ifndef __NE_MODULAR_KERNEL_COMPONENTS__
EXTERN_C Kernel::VoidPtr kInterruptVectorTable[];
-EXTERN_C Kernel::VoidPtr mp_user_switch_proc;
-EXTERN_C Kernel::Char mp_user_switch_proc_stack_begin[];
-
-STATIC Kernel::Void hal_pre_init_scheduler()
-{
- for (Kernel::SizeT i = 0U; i < Kernel::UserProcessScheduler::The().CurrentTeam().AsArray().Count(); ++i)
- {
- Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[i] = Kernel::Process();
- Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[i].Status = Kernel::ProcessStatusKind::kKilled;
- }
-}
-/// @brief Kernel init procedure.
-EXTERN_C Int32 hal_init_platform(
- Kernel::HEL::BootInfoHeader* handover_hdr)
-{
- if (handover_hdr->f_Magic != kHandoverMagic &&
- handover_hdr->f_Version != kHandoverVersion)
- {
- return kEfiFail;
- }
-
- kHandoverHeader = handover_hdr;
-
- FB::fb_clear_video();
+STATIC Kernel::Void hal_pre_init_scheduler() noexcept {
+ for (Kernel::SizeT i = 0U;
+ i < Kernel::UserProcessScheduler::The().CurrentTeam().AsArray().Count(); ++i) {
+ Kernel::UserProcessScheduler::The().CurrentTeam().AsArray()[i] = Kernel::USER_PROCESS();
+ }
+}
- fw_init_efi((EfiSystemTable*)handover_hdr->f_FirmwareCustomTables[1]);
- Boot::ExitBootServices(handover_hdr->f_HardwareTables.f_ImageKey, handover_hdr->f_HardwareTables.f_ImageHandle);
+/// @brief Kernel init function.
+/// @param handover_hdr Handover boot header.
+EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) {
+ if (handover_hdr->f_Magic != kHandoverMagic && handover_hdr->f_Version != kHandoverVersion) {
+ return kEfiFail;
+ }
+
+ kHandoverHeader = handover_hdr;
+
+ FB::fb_clear_video();
+
+ fw_init_efi((EfiSystemTable*) handover_hdr->f_FirmwareCustomTables[1]);
+
+ Boot::ExitBootServices(handover_hdr->f_HardwareTables.f_ImageKey,
+ handover_hdr->f_HardwareTables.f_ImageHandle);
+
+ kKernelCR3 = kHandoverHeader->f_PageStart;
+
+ hal_write_cr3(kKernelCR3);
+
+ /************************************** */
+ /* INITIALIZE BIT MAP. */
+ /************************************** */
+
+ kKernelBitMpSize = kHandoverHeader->f_BitMapSize;
+ kKernelBitMpStart = reinterpret_cast<Kernel::VoidPtr>(
+ reinterpret_cast<Kernel::UIntPtr>(kHandoverHeader->f_BitMapStart));
+
+ /************************************** */
+ /* INITIALIZE GDT AND SEGMENTS. */
+ /************************************** */
+
+ STATIC CONST auto kGDTEntriesCount = 6;
+
+ /* GDT, mostly descriptors for user and kernel segments. */
+ STATIC Kernel::HAL::Detail::NE_GDT_ENTRY ALIGN(0x08) kGDTArray[kGDTEntriesCount] = {
+ {.fLimitLow = 0,
+ .fBaseLow = 0,
+ .fBaseMid = 0,
+ .fAccessByte = 0x00,
+ .fFlags = 0x00,
+ .fBaseHigh = 0}, // Null entry
+ {.fLimitLow = 0x0,
+ .fBaseLow = 0,
+ .fBaseMid = 0,
+ .fAccessByte = 0x9A,
+ .fFlags = 0xAF,
+ .fBaseHigh = 0}, // Kernel code
+ {.fLimitLow = 0x0,
+ .fBaseLow = 0,
+ .fBaseMid = 0,
+ .fAccessByte = 0x92,
+ .fFlags = 0xCF,
+ .fBaseHigh = 0}, // Kernel data
+ {.fLimitLow = 0x0,
+ .fBaseLow = 0,
+ .fBaseMid = 0,
+ .fAccessByte = 0xFA,
+ .fFlags = 0xAF,
+ .fBaseHigh = 0}, // User code
+ {.fLimitLow = 0x0,
+ .fBaseLow = 0,
+ .fBaseMid = 0,
+ .fAccessByte = 0xF2,
+ .fFlags = 0xCF,
+ .fBaseHigh = 0}, // User data
+ };
+
+ // Load memory descriptors.
+ Kernel::HAL::Register64 gdt_reg;
+
+ gdt_reg.Base = reinterpret_cast<Kernel::UIntPtr>(kGDTArray);
+ gdt_reg.Limit = (sizeof(Kernel::HAL::Detail::NE_GDT_ENTRY) * kGDTEntriesCount) - 1;
+
+ //! GDT will load hal_read_init after it successfully loads the segments.
+ Kernel::HAL::GDTLoader gdt_loader;
+ gdt_loader.Load(gdt_reg);
+
+ return kEfiFail;
+}
- /************************************** */
- /* INITIALIZE BIT MAP. */
- /************************************** */
+EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept {
+ hal_pre_init_scheduler();
- kKernelBitMpSize = kHandoverHeader->f_BitMapSize;
- kKernelBitMpStart = reinterpret_cast<Kernel::VoidPtr>(
- reinterpret_cast<Kernel::UIntPtr>(kHandoverHeader->f_BitMapStart));
+#ifdef __FSKIT_INCLUDES_HEFS__
+ Kernel::HeFS::fs_init_hefs();
+#elif defined(__FSKIT_INCLUDES_NEFS__)
+ Kernel::NeFS::fs_init_nefs();
+#endif
- /************************************** */
- /* INITIALIZE GDT AND SEGMENTS. */
- /************************************** */
+ Kernel::HAL::mp_init_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
- STATIC CONST auto kGDTEntriesCount = 6;
+ Kernel::HAL::Register64 idt_reg;
+ idt_reg.Base = (Kernel::UIntPtr) kInterruptVectorTable;
- /* GDT, mostly descriptors for user and kernel segments. */
- STATIC Kernel::HAL::Detail::NE_GDT_ENTRY ALIGN(0x08) kGDTArray[kGDTEntriesCount] = {
- {.fLimitLow = 0, .fBaseLow = 0, .fBaseMid = 0, .fAccessByte = 0x00, .fFlags = 0x00, .fBaseHigh = 0}, // Null entry
- {.fLimitLow = 0x0, .fBaseLow = 0, .fBaseMid = 0, .fAccessByte = 0x9A, .fFlags = 0xAF, .fBaseHigh = 0}, // Kernel code
- {.fLimitLow = 0x0, .fBaseLow = 0, .fBaseMid = 0, .fAccessByte = 0x92, .fFlags = 0xCF, .fBaseHigh = 0}, // Kernel data
- {.fLimitLow = 0x0, .fBaseLow = 0, .fBaseMid = 0, .fAccessByte = 0xFA, .fFlags = 0xAF, .fBaseHigh = 0}, // User code
- {.fLimitLow = 0x0, .fBaseLow = 0, .fBaseMid = 0, .fAccessByte = 0xF2, .fFlags = 0xCF, .fBaseHigh = 0}, // User data
- };
+ Kernel::HAL::IDTLoader idt_loader;
- // Load memory descriptors.
- Kernel::HAL::Register64 gdt_reg;
+ idt_loader.Load(idt_reg);
- gdt_reg.Base = reinterpret_cast<Kernel::UIntPtr>(kGDTArray);
- gdt_reg.Limit = (sizeof(Kernel::HAL::Detail::NE_GDT_ENTRY) * kGDTEntriesCount) - 1;
+ /// after the scheduler runs, we must look over teams, every 5000s in order to schedule every
+ /// process according to their affinity fairly.
- //! GDT will load hal_read_init after it successfully loads the segments.
- Kernel::HAL::GDTLoader gdt_loader;
- gdt_loader.Load(gdt_reg);
+ auto constexpr kSchedTeamSwitchMS = 5U; /// @brief Team switch time in milliseconds.
- return kEfiFail;
-}
+ Kernel::HardwareTimer timer(rtl_milliseconds(kSchedTeamSwitchMS));
-EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept
-{
- hal_pre_init_scheduler();
+ STATIC Kernel::Array<UserProcessTeam, kSchedTeamCount> kTeams;
- Kernel::NeFS::fs_init_nefs();
+ static SizeT team_index = 0U;
- Kernel::HAL::mp_init_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
+ /// @brief This just loops over the teams and switches between them.
+ /// @details Not even round-robin, just a simple loop in this boot core we're at.
+ while (YES) {
+ if (team_index > (kSchedTeamCount - 1)) {
+ team_index = 0U;
+ }
- Kernel::HAL::Register64 idt_reg;
- idt_reg.Base = (Kernel::UIntPtr)kInterruptVectorTable;
+ kTeams[team_index].Id() = team_index;
- Kernel::HAL::IDTLoader idt_loader;
+ while (!UserProcessScheduler::The().SwitchTeam(kTeams[team_index]));
- idt_loader.Load(idt_reg);
+ timer.Wait();
- while (YES)
- {
- ;
- }
+ ++team_index;
+ }
}
+#endif // ifndef __NE_MODULAR_KERNEL_COMPONENTS__