refactor: kernel entry point - add architecture preprocessor checks and move module to Boot

This commit is contained in:
2026-08-14 11:28:32 +02:00
parent 0f81276efc
commit 8e45d43116
2 changed files with 18 additions and 29 deletions
+1 -1
View File
@@ -12,4 +12,4 @@
#pragma once #pragma once
#define MONTAUK_BUILD_NUMBER 119 #define MONTAUK_BUILD_NUMBER 120
@@ -1,7 +1,8 @@
/* /*
* main.cpp * main.cpp
* Kernel entry point * Kernel entry point
* Copyright (c) 2025 Daniel Hammer, Limine Contributors (via Limine C++ example) * Copyright (c) 2025 Daniel Hammer.
* Further copyright information and third party notices can be found at https://montaukos.org/license.txt.
*/ */
#include <cstdint> #include <cstdint>
@@ -29,7 +30,6 @@
#include <Drivers/PS2/Keyboard.hpp> #include <Drivers/PS2/Keyboard.hpp>
#include <Drivers/PS2/Mouse.hpp> #include <Drivers/PS2/Mouse.hpp>
#include <Drivers/Init.hpp> #include <Drivers/Init.hpp>
#include <Drivers/USB/Bluetooth/Bluetooth.hpp>
#include <Graphics/Framebuffer.hpp> #include <Graphics/Framebuffer.hpp>
#include <Hal/MSR.hpp> #include <Hal/MSR.hpp>
#include <Hal/Cpu.hpp> #include <Hal/Cpu.hpp>
@@ -62,12 +62,7 @@ extern "C" void kmain() {
for (std::size_t i = 0; &__init_array[i] != __init_array_end; i++) { for (std::size_t i = 0; &__init_array[i] != __init_array_end; i++) {
__init_array[i](); __init_array[i]();
} }
// Acquire the boot environment through the Montauk Boot Contract. The
// active bootloader adapter (see Boot/Protocols/) translates its native
// handoff into this bootloader-agnostic structure. A false return means
// we cannot even bring up a console (unsupported loader, no HHDM, or no
// framebuffer) -- there is nothing to do but halt.
if (!montauk::boot::Initialize()) { if (!montauk::boot::Initialize()) {
Hal::Halt(); Hal::Halt();
} }
@@ -119,20 +114,13 @@ extern "C" void kmain() {
Memory::VMM::g_paging = &g_paging; Memory::VMM::g_paging = &g_paging;
g_paging.Init((uint64_t)&KernelStartSymbol, ((uint64_t)&KernelEndSymbol - (uint64_t)&KernelStartSymbol), boot.memoryMap, framebuffer); g_paging.Init((uint64_t)&KernelStartSymbol, ((uint64_t)&KernelEndSymbol - (uint64_t)&KernelStartSymbol), boot.memoryMap, framebuffer);
// Reprogram PAT so entry 1 = Write-Combining (default is Write-Through).
// Must be done after paging init and before any WC mappings.
Hal::InitializePAT(); Hal::InitializePAT();
Kt::KernelLogStream(OK, "Hal") << "PAT reprogrammed (entry 1 = WC)";
#endif #endif
// Initialize the framebuffer early so we can WC-map it before
// the bulk of boot logging begins (ACPI, PCI, drivers, etc.)
Graphics::Framebuffer::Initialize(framebuffer); Graphics::Framebuffer::Initialize(framebuffer);
#if defined (__x86_64__) #if defined (__x86_64__)
// Map framebuffer as Write-Combining immediately for faster screen writes.
// All subsequent log output benefits from WC burst transfers.
Graphics::Framebuffer::MapWriteCombining(); Graphics::Framebuffer::MapWriteCombining();
#endif #endif
@@ -145,19 +133,12 @@ extern "C" void kmain() {
Hal::ApicInitialize(g_acpi.GetXSDT()); Hal::ApicInitialize(g_acpi.GetXSDT());
// Set up BSP per-CPU data (GS base) before enabling interrupts.
// ISR stubs use SWAPGS which requires GS base to point to CpuData.
Smp::InitBsp(); Smp::InitBsp();
// Enable hardware P-state scaling and the thermal governor.
// Needs GS base (per-CPU data) set up, and must run before the
// APs boot so they inherit the shared policy in ApEntry.
Hal::CpuPower::InitializeBsp(); Hal::CpuPower::InitializeBsp();
// Now safe to enable interrupts (SWAPGS-aware ISR stubs are installed)
asm volatile("sti"); asm volatile("sti");
// Initialize ACPI events (SCI, power button) after APIC is ready
Hal::AcpiEvents::Initialize(g_acpi.GetXSDT()); Hal::AcpiEvents::Initialize(g_acpi.GetXSDT());
Pci::Initialize(g_acpi.GetXSDT()); Pci::Initialize(g_acpi.GetXSDT());
@@ -186,29 +167,32 @@ extern "C" void kmain() {
Fs::InitializeBootFilesystems(boot.modules); Fs::InitializeBootFilesystems(boot.modules);
#if defined (__x86_64__)
Hal::LoadTSS(); Hal::LoadTSS();
#endif
montauk::abi::InitializeSyscalls(); montauk::abi::InitializeSyscalls();
Sched::Initialize(); Sched::Initialize();
Ipc::Initialize(); Ipc::Initialize();
// Boot Application Processors (all subsystems ready, APs can schedule) #if defined (__x86_64__)
Smp::BootAPs(boot.smp); Smp::BootAPs(boot.smp);
#endif
// Flush any stale PS/2 mouse bytes that accumulated during boot #if defined (__x86_64__)
// (edge-triggered IRQs can be lost while spinlocks disable interrupts)
Drivers::PS2::Mouse::FlushState(); Drivers::PS2::Mouse::FlushState();
#endif
Kt::SuppressKernelLog(); Kt::SuppressKernelLog();
Sched::Spawn("0:/os/init.elf"); Sched::Spawn("0:/os/init.elf");
// Enable preemptive scheduling via the APIC timer
Timekeeping::EnableSchedulerTick(); Timekeeping::EnableSchedulerTick();
// Main loop: idle until next interrupt. #if defined (__x86_64__)
// Use MWAIT for deeper C-states if available, otherwise HLT. // Use MWAIT for deeper C-states if available, otherwise HLT.
auto* bspCpu = Smp::GetCpuData(0); auto* bspCpu = Smp::GetCpuData(0);
if (bspCpu && bspCpu->hasMwait) { if (bspCpu && bspCpu->hasMwait) {
static volatile uint64_t s_bspIdleMonitor = 0; static volatile uint64_t s_bspIdleMonitor = 0;
for (;;) { for (;;) {
@@ -219,4 +203,9 @@ extern "C" void kmain() {
Timekeeping::IdleOnce(false); Timekeeping::IdleOnce(false);
} }
} }
#else
for (;;) {
Timekeeping::IdleOnce(false);
}
#endif
} }