refactor: kernel entry point - add architecture preprocessor checks and move module to Boot
This commit is contained in:
1 parent
0f81276efc
commit
8e45d43116
2 files changed
+18
-29
No files matched your search
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* main.cpp
|
||||
* Kernel entry point
|
||||
* Copyright (c) 2025 Daniel Hammer.
|
||||
* Further copyright information and third party notices can be found at https://montaukos.org/license.txt.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <Boot/Boot.hpp>
|
||||
#include <Hal/GDT.hpp>
|
||||
#include <Terminal/Terminal.hpp>
|
||||
#include <Efi/UEFI.hpp>
|
||||
#include <Common/Panic.hpp>
|
||||
#include <Memory/Memmap.hpp>
|
||||
#include <Memory/Heap.hpp>
|
||||
#include <Memory/HHDM.hpp>
|
||||
#include <Platform/Util.hpp>
|
||||
#include <Hal/IDT.hpp>
|
||||
#include <Memory/PageFrameAllocator.hpp>
|
||||
#include <Memory/Paging.hpp>
|
||||
#include <ACPI/ACPI.hpp>
|
||||
#include <ACPI/CpuIdle.hpp>
|
||||
#include <ACPI/AcpiShutdown.hpp>
|
||||
#include <ACPI/AcpiEvents.hpp>
|
||||
#include <Hal/Apic/ApicInit.hpp>
|
||||
#include <Pci/Pci.hpp>
|
||||
#include <Timekeeping/ApicTimer.hpp>
|
||||
#include <Drivers/PS2/PS2Controller.hpp>
|
||||
#include <Drivers/PS2/Keyboard.hpp>
|
||||
#include <Drivers/PS2/Mouse.hpp>
|
||||
#include <Drivers/Init.hpp>
|
||||
#include <Graphics/Framebuffer.hpp>
|
||||
#include <Hal/MSR.hpp>
|
||||
#include <Hal/Cpu.hpp>
|
||||
#include <Hal/CpuPower.hpp>
|
||||
#include <Fs/Boot.hpp>
|
||||
#include <Sched/Scheduler.hpp>
|
||||
#include <Ipc/Ipc.hpp>
|
||||
#include <Api/Syscall.hpp>
|
||||
#include <Hal/SmpBoot.hpp>
|
||||
using namespace Kt;
|
||||
|
||||
namespace Memory {
|
||||
HeapAllocator* g_heap;
|
||||
PageFrameAllocator* g_pfa;
|
||||
uint64_t HHDMBase;
|
||||
};
|
||||
|
||||
KernelOutStream kout{};
|
||||
KernelErrorStream kerr{};
|
||||
|
||||
// Extern declarations for global constructors array.
|
||||
extern void (*__init_array[])();
|
||||
extern void (*__init_array_end[])();
|
||||
|
||||
extern "C" uint64_t KernelStartSymbol;
|
||||
extern "C" uint64_t KernelEndSymbol;
|
||||
|
||||
extern "C" void kmain() {
|
||||
// Call global constructors.
|
||||
for (std::size_t i = 0; &__init_array[i] != __init_array_end; i++) {
|
||||
__init_array[i]();
|
||||
}
|
||||
|
||||
if (!montauk::boot::Initialize()) {
|
||||
Hal::Halt();
|
||||
}
|
||||
const montauk::boot::BootInfo& boot = montauk::boot::Info();
|
||||
const montauk::boot::Framebuffer& framebuffer = boot.framebuffer;
|
||||
|
||||
Kt::Initialize(
|
||||
(uint32_t*)framebuffer.address,
|
||||
framebuffer.width,
|
||||
framebuffer.height,
|
||||
framebuffer.pitch,
|
||||
framebuffer.redMaskSize,
|
||||
framebuffer.redMaskShift,
|
||||
framebuffer.greenMaskSize,
|
||||
framebuffer.greenMaskShift,
|
||||
framebuffer.blueMaskSize,
|
||||
framebuffer.blueMaskShift
|
||||
);
|
||||
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Hal::PrepareGDT();
|
||||
Hal::BridgeLoadGDT();
|
||||
|
||||
Hal::EnableSSE();
|
||||
#endif
|
||||
|
||||
Memory::HHDMBase = boot.hhdmBase;
|
||||
|
||||
if (boot.memoryMap.regions == nullptr || boot.memoryMap.count == 0) {
|
||||
Panic("System memory map missing!", nullptr);
|
||||
}
|
||||
|
||||
Kt::KernelLogStream(OK, "Mem") << "Creating PageFrameAllocator";
|
||||
Memory::PageFrameAllocator pmm(Memory::Scan(boot.memoryMap));
|
||||
Memory::g_pfa = &pmm;
|
||||
|
||||
Kt::KernelLogStream(OK, "Mem") << "Creating HeapAllocator";
|
||||
Memory::HeapAllocator heap{};
|
||||
Memory::g_heap = &heap;
|
||||
|
||||
heap.Walk();
|
||||
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Hal::IDTInitialize();
|
||||
|
||||
Memory::VMM::Paging g_paging{};
|
||||
Memory::VMM::g_paging = &g_paging;
|
||||
g_paging.Init((uint64_t)&KernelStartSymbol, ((uint64_t)&KernelEndSymbol - (uint64_t)&KernelStartSymbol), boot.memoryMap, framebuffer);
|
||||
|
||||
Hal::InitializePAT();
|
||||
|
||||
#endif
|
||||
|
||||
Graphics::Framebuffer::Initialize(framebuffer);
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Graphics::Framebuffer::MapWriteCombining();
|
||||
#endif
|
||||
|
||||
Hal::ACPI g_acpi((Hal::ACPI::XSDP*)Memory::HHDM(boot.rsdpPhysical));
|
||||
|
||||
#if defined (__x86_64__)
|
||||
if (g_acpi.GetXSDT() != nullptr) {
|
||||
Hal::AcpiShutdown::Initialize(g_acpi.GetXSDT());
|
||||
Hal::CpuIdle::Initialize(g_acpi.GetXSDT());
|
||||
|
||||
Hal::ApicInitialize(g_acpi.GetXSDT());
|
||||
|
||||
Smp::InitBsp();
|
||||
|
||||
Hal::CpuPower::InitializeBsp();
|
||||
|
||||
asm volatile("sti");
|
||||
|
||||
Hal::AcpiEvents::Initialize(g_acpi.GetXSDT());
|
||||
|
||||
Pci::Initialize(g_acpi.GetXSDT());
|
||||
|
||||
Drivers::ProbeEarly();
|
||||
Drivers::InitializeGraphics();
|
||||
|
||||
Timekeeping::ApicTimerInitialize();
|
||||
|
||||
Drivers::PS2::Initialize();
|
||||
Drivers::PS2::Keyboard::Initialize();
|
||||
Drivers::PS2::Mouse::Initialize();
|
||||
|
||||
Drivers::ProbeNormal();
|
||||
Drivers::InitializeNetwork();
|
||||
Drivers::InitializeStorage();
|
||||
Drivers::InitializeAudio();
|
||||
}
|
||||
#endif
|
||||
|
||||
// UEFI runtime services are optional (absent on legacy-BIOS boots).
|
||||
if (boot.has(montauk::boot::FeatureEfiSystemTable)) {
|
||||
Efi::SystemTable* ST = (Efi::SystemTable*)Memory::HHDM(boot.efi.systemTablePhysical);
|
||||
Efi::Init(ST, boot.efi);
|
||||
}
|
||||
|
||||
Fs::InitializeBootFilesystems(boot.modules);
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Hal::LoadTSS();
|
||||
#endif
|
||||
|
||||
montauk::abi::InitializeSyscalls();
|
||||
|
||||
Sched::Initialize();
|
||||
Ipc::Initialize();
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Smp::BootAPs(boot.smp);
|
||||
#endif
|
||||
|
||||
#if defined (__x86_64__)
|
||||
Drivers::PS2::Mouse::FlushState();
|
||||
#endif
|
||||
|
||||
Kt::SuppressKernelLog();
|
||||
Sched::Spawn("0:/os/init.elf");
|
||||
|
||||
Timekeeping::EnableSchedulerTick();
|
||||
|
||||
#if defined (__x86_64__)
|
||||
// Use MWAIT for deeper C-states if available, otherwise HLT.
|
||||
auto* bspCpu = Smp::GetCpuData(0);
|
||||
|
||||
if (bspCpu && bspCpu->hasMwait) {
|
||||
static volatile uint64_t s_bspIdleMonitor = 0;
|
||||
for (;;) {
|
||||
Timekeeping::IdleOnce(true, &s_bspIdleMonitor);
|
||||
}
|
||||
} else {
|
||||
for (;;) {
|
||||
Timekeeping::IdleOnce(false);
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (;;) {
|
||||
Timekeeping::IdleOnce(false);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user