From 8e45d43116393a85b53d8fd497c5300030e9313c Mon Sep 17 00:00:00 2001 From: Daniel Hammer Date: Fri, 14 Aug 2026 11:28:32 +0200 Subject: [PATCH] refactor: kernel entry point - add architecture preprocessor checks and move module to Boot --- kernel/src/Api/BuildNo.hpp | 2 +- kernel/src/{ => Boot}/Main.cpp | 45 +++++++++++++--------------------- 2 files changed, 18 insertions(+), 29 deletions(-) rename kernel/src/{ => Boot}/Main.cpp (73%) diff --git a/kernel/src/Api/BuildNo.hpp b/kernel/src/Api/BuildNo.hpp index 7c8d3b1..164480f 100644 --- a/kernel/src/Api/BuildNo.hpp +++ b/kernel/src/Api/BuildNo.hpp @@ -12,4 +12,4 @@ #pragma once -#define MONTAUK_BUILD_NUMBER 119 +#define MONTAUK_BUILD_NUMBER 120 diff --git a/kernel/src/Main.cpp b/kernel/src/Boot/Main.cpp similarity index 73% rename from kernel/src/Main.cpp rename to kernel/src/Boot/Main.cpp index d2f877a..5d14a82 100644 --- a/kernel/src/Main.cpp +++ b/kernel/src/Boot/Main.cpp @@ -1,7 +1,8 @@ /* * main.cpp * 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 @@ -29,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -62,12 +62,7 @@ extern "C" void kmain() { for (std::size_t i = 0; &__init_array[i] != __init_array_end; 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()) { Hal::Halt(); } @@ -119,20 +114,13 @@ extern "C" void kmain() { Memory::VMM::g_paging = &g_paging; 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(); - Kt::KernelLogStream(OK, "Hal") << "PAT reprogrammed (entry 1 = WC)"; #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); #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(); #endif @@ -145,19 +133,12 @@ extern "C" void kmain() { 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(); - // 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(); - // Now safe to enable interrupts (SWAPGS-aware ISR stubs are installed) asm volatile("sti"); - // Initialize ACPI events (SCI, power button) after APIC is ready Hal::AcpiEvents::Initialize(g_acpi.GetXSDT()); Pci::Initialize(g_acpi.GetXSDT()); @@ -186,29 +167,32 @@ extern "C" void kmain() { Fs::InitializeBootFilesystems(boot.modules); - +#if defined (__x86_64__) Hal::LoadTSS(); +#endif + montauk::abi::InitializeSyscalls(); Sched::Initialize(); Ipc::Initialize(); - // Boot Application Processors (all subsystems ready, APs can schedule) +#if defined (__x86_64__) Smp::BootAPs(boot.smp); +#endif - // Flush any stale PS/2 mouse bytes that accumulated during boot - // (edge-triggered IRQs can be lost while spinlocks disable interrupts) +#if defined (__x86_64__) Drivers::PS2::Mouse::FlushState(); +#endif Kt::SuppressKernelLog(); Sched::Spawn("0:/os/init.elf"); - // Enable preemptive scheduling via the APIC timer Timekeeping::EnableSchedulerTick(); - // Main loop: idle until next interrupt. +#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 (;;) { @@ -219,4 +203,9 @@ extern "C" void kmain() { Timekeeping::IdleOnce(false); } } +#else + for (;;) { + Timekeeping::IdleOnce(false); + } +#endif }