Architecture¶
Boot flow¶
- GRUB loads
atlOS.kernel(multiboot2, seekernel/arch/x86_64/boot/) at physical0x400000and jumps to_startin 32-bit protected mode. boot.Sbuilds the boot page tables and enters long mode:PML4[0] → low_pdpt → low_page_directory_table: identity map of0 .. kernel_physical_endwith 2 MB pagesPML4[511] → high_pdpt[510] → high_page_directory_table: the same physical range at0xFFFFFFFF80000000(the higher half)- enables PAE/PSE,
EFER.LMEand paging, loads the GDT and far jumps to_start64, which callskernelMain(mbInfo, mbMagic). kernelMain(kernel/core/main.cpp):- constructs the
Drivers::VGATextConsoleand the COM1Drivers::SerialPortand binds them as the console (Core::setConsole):printfgoes to the screen first, to COM1 once it passed its loopback test - banner, global C++ constructors (
cxx::callConstructors) IDT::init(exception and IRQ vectors)- checks the multiboot2 magic
- constructs the
Memory::PhysicalMemoryManager, fills it from the multiboot memory map (Memory::populateFromMultiboot, reserves the kernel image), constructs theMemory::AddressSpaceon top, and the kernel'sCommon::Heapon its pages, bound tokmallocandnew(see kernel/mm/README.md) - constructs the
Fs::Vfsand mounts the FAT16 partitions of the ATA disks (Fs::mountAtaDisks, seekernel/fs/README.md) - finds the ACPI tables (
Firmware::findRsdp,Firmware::AcpiTables, seekernel/firmware/acpi/README.md) - constructs the
Drivers::PIC(8259 remapped to vectors 0x20/0x28, all lines masked); if the MADT describes an I/O APIC, theDrivers::LocalApic, theDrivers::IoApics and theDrivers::ApicInterruptControlleron top (seekernel/drivers/apic/README.md). The APIC, or the PIC without it, is bound to the ISR dispatcher - the tick: the
Drivers::ApicTimer, measured against the PIT, or theDrivers::PITwithout the APIC; enables interrupts - constructs the
Threading::Scheduleron that tick (the boot flow becomes task 0, the idle thread task 1, 1 ms tick) - self tests (
ATLOS_SELFTESTbuilds), the crash test (ATLOS_CRASHTEST) or the shell (after constructing theProcess::Runtimefor user programs, bound withProcess::setRuntime): aCharacterQueuefed by the keyboard and COM1, andShell::Interpreterrunning in the boot flow (seekernel/shell/README.md)
kernelMain is the composition root: driver objects live in
Cxx::Uninitialized<T> storage, are constructed there explicitly in
dependency order and passed to their users by reference.
The linker script (kernel/arch/x86_64/linker.ld) places the boot sections
at their physical address and everything else at
KERNEL_VIRTUAL_LOAD_OFFSET + physical. kernel_physical_end and
kernel_virtual_end are rounded up to 2 MB.
Address space¶
| Virtual range | Contents |
|---|---|
0x0 .. kernel_physical_end |
identity map from boot (multiboot info, boot tables), kernel only |
0x0000008000000000 .. 0x00007FFFFFFFFFFF (PML4[1..255]) |
user programs (per address space) |
0xFFFF800000000000 .. + 64 GB (PML4[256]) |
physmap: all physical memory, how page tables are edited |
0xFFFFFFFF80000000 .. kernel_virtual_end |
kernel image |
kernel_virtual_end .. 0xFFFFFFFFFFFFFFFF |
kernel heap, thread stacks, mapPhysical mappings (2 MB pages) |
Interrupts¶
CPU → IDT vector → INT_<n> stub (arch/x86_64/idt/isr.S)
push err_code (dummy 0 if the CPU does not push one), vector, rax..r15
→ intHandler(frame) (arch/x86_64/idt/idt.cpp)
exceptions 0..31: print the register dump and halt
other vectors: Handlers::interruptHandler(vector)
→ InterruptController::endOfInterrupt(vector)
→ registered handler(arg)
← pop registers, drop vector/err_code, iretq
Drivers do not talk to the interrupt controller's chip directly. They get a
Drivers::InterruptController& (drivers/interrupt_controller.hpp) and call
attach(irq, fn, arg) and unmask(irq); the APIC
(Drivers::ApicInterruptController) and the 8259 PIC are its two
implementations, and ISA IRQ n is vector 0x20 + n with either. A periodic
timer is a Drivers::TickSource (drivers/tick_source.hpp), implemented by
the local APIC timer (vector 0x40) and the PIT. The only place that
reaches the controller without being handed it is the ISR dispatcher, bound
once with Handlers::setInterruptController.
IRQ handlers run on the stack of whatever thread was interrupted; they must not call into the scheduler or the heap.
All lines start masked; a line is enabled by the driver that uses it. Today only IRQ 0 (PIT, scheduler tick) is enabled.
Conventions¶
- Each subsystem is a directory under
kernel/with headers next to their sources; includes are relative tokernel/(#include <sched/thread.hpp>). - File names are lower case. Kernel code lives in
Kernel::, with a namespace per subsystem inside it (Kernel::Memory,Kernel::Threading,Kernel::Drivers, ...); naming, formatting and the design direction are in style.md. libc/is the freestanding C library (libk) that the kernel links; its public headers are inlibc/include.- Kernel code is built without exceptions, RTTI or the red zone.
- No SIMD: the kernel and libc are built with
-mno-mmx -mno-sse -mno-sse2(ATLOS_NO_SIMD_FLAGS). Boot does not enable SSE and neither the context switch nor the ISRs save SIMD state, so the build fails if the linked image contains an MMX/SSE/AVX instruction (cmake/check-no-simd.cmake). For the same reasonprintfhas no%f/%e/%g.