Skip to content

Architecture

Boot flow

  1. GRUB loads atlOS.kernel (multiboot2, see kernel/arch/x86_64/boot/) at physical 0x400000 and jumps to _start in 32-bit protected mode.
  2. boot.S builds the boot page tables and enters long mode:
  3. PML4[0] → low_pdpt → low_page_directory_table: identity map of 0 .. kernel_physical_end with 2 MB pages
  4. PML4[511] → high_pdpt[510] → high_page_directory_table: the same physical range at 0xFFFFFFFF80000000 (the higher half)
  5. enables PAE/PSE, EFER.LME and paging, loads the GDT and far jumps to _start64, which calls kernelMain(mbInfo, mbMagic).
  6. kernelMain (kernel/core/main.cpp):
  7. constructs the Drivers::VGATextConsole and the COM1 Drivers::SerialPort and binds them as the console (Core::setConsole): printf goes to the screen first, to COM1 once it passed its loopback test
  8. banner, global C++ constructors (cxx::callConstructors)
  9. IDT::init (exception and IRQ vectors)
  10. checks the multiboot2 magic
  11. constructs the Memory::PhysicalMemoryManager, fills it from the multiboot memory map (Memory::populateFromMultiboot, reserves the kernel image), constructs the Memory::AddressSpace on top, and the kernel's Common::Heap on its pages, bound to kmalloc and new (see kernel/mm/README.md)
  12. constructs the Fs::Vfs and mounts the FAT16 partitions of the ATA disks (Fs::mountAtaDisks, see kernel/fs/README.md)
  13. finds the ACPI tables (Firmware::findRsdp, Firmware::AcpiTables, see kernel/firmware/acpi/README.md)
  14. constructs the Drivers::PIC (8259 remapped to vectors 0x20/0x28, all lines masked); if the MADT describes an I/O APIC, the Drivers::LocalApic, the Drivers::IoApics and the Drivers::ApicInterruptController on top (see kernel/drivers/apic/README.md). The APIC, or the PIC without it, is bound to the ISR dispatcher
  15. the tick: the Drivers::ApicTimer, measured against the PIT, or the Drivers::PIT without the APIC; enables interrupts
  16. constructs the Threading::Scheduler on that tick (the boot flow becomes task 0, the idle thread task 1, 1 ms tick)
  17. self tests (ATLOS_SELFTEST builds), the crash test (ATLOS_CRASHTEST) or the shell (after constructing the Process::Runtime for user programs, bound with Process::setRuntime): a CharacterQueue fed by the keyboard and COM1, and Shell::Interpreter running in the boot flow (see kernel/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 to kernel/ (#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 in libc/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 reason printf has no %f/%e/%g.