CPU & scheduling

Weighted fairness, one global run queue, one big lock.

NebOS runs a CFS-lite scheduler over a single global run queue and per-CPU current pointers. ACPI/MADT brings up the APs, each CPU has its own GDT, TSS and timer, and a sleeping recursive mutex — the big kernel lock — serializes kernel execution.

8 CPUs max 64 process slots 10 ms slice 1 kHz busy / 100 Hz idle

Boot CPU bring-up

After kernel_main clears BSS and starts the boot console, the first structural step is gdt_tss_init(0). The GDT/TSS and the per-CPU GS base must exist before any code that calls current_proc() or this_cpu().

Interrupts & timers

The BSP keeps the legacy 8259 PIC and PIT. Device IRQs stay on the BSP because there is no IOAPIC driver. APs mask their LINT0/LINT1 inputs and use only their local APIC timer and IPIs.

SourceVectorRole
8259 PIC + PITIRQ0, etc.BSP global clock and device interrupts; PIT tick advances the millisecond clock.
LAPIC timer0x30Per-AP scheduling tick; 1 ms busy, 10 ms idle.
LAPIC IPI0x31Reschedule request; must be EOIed on the target LAPIC.
int 0x80syscall gateRing-3 service entry.
int 0x81voluntary yieldBlocking helpers and schedule().

The timer period is not fixed. While a process runs, the BSP PIT ticks at 1 kHz and AP LAPIC timers tick every 1 ms. While a CPU is idle, the BSP drops to 100 Hz and APs to 10 ms so MWAIT/HLT can hold a deeper state. The global millisecond clock remains accurate because the PIT stub advances it by the current tick period.

Race to idle, then stay there. When a wake-up arrives, smp_resched_wake() shortens the idle period so the woken process runs at the next millisecond instead of waiting out a 10 ms nap.

SMP bring-up

SMP is ACPI-only: RSDP → RSDT/XSDT → MADT → CPU/LAPIC list. There is deliberately no Intel MP-table parser. A machine without ACPI, a usable MADT, the CPUID APIC bit or a LAPIC stays uniprocessor and follows exactly the old 8259/PIT path.

The same table walk reads the FADT's century byte (offset 108) and exposes the CMOS register to rtc.c, which writes it when setting the clock so the two-digit year stays in the intended century. Boards without ACPI get no century write and keep the 1970–2069 read window.

  1. Copy the AP trampoline to 0x6000 (SIPI vector 0x06).
  2. Allocate an AP idle PCB and kernel stack via process_init_ap().
  3. Fill the startup block at 0x6F00 with CR3, stack top, C entry and CPU index.
  4. Send INIT, then SIPI-SIPI, with a second SIPI retry.
  5. The AP enters smp_ap_main, initialises its LAPIC timer and idle loop, and waits.
  6. After device/FS setup, smp_enable_sched() lets APs take user work.

Kernel threads stay pinned to the BSP; only ring-3 processes may run on an AP. Each AP runs cpu_enable_sse() because it starts with firmware CR4; without OSFXSR/OSXMMEXCPT the SSE2 drawing fast paths would #UD.

No IOAPIC. Device IRQs never reach an AP, and pit_irq_stub is BSP-only. A platform that routes ISA IRQs through the IOAPIC would need a mask-all setup step after ACPI init.

Process model

Every process is a stable heap-allocated PCB referenced from a sparse procs[64] table, so a process pointer never moves when another slot changes. The idle context is not a process slot: the kernel_main context becomes the BSP idle state, and APs have their own idle PCBs.

Process state transitions Processes move between ready, running, sleeping, blocked and dead states. Wake-ups return to ready, and exit or kill moves to dead. READY run queue RUNNING one per CPU SLEEPING deadline in sleep list BLOCKED wait queue DEAD zombie queue pick slice / yield sleep wait wake wake exit / kill
Five states, two queues. The run queue is global; the sleep list is sorted by deadline; wait queues are per-object. A woken process is clamped to the current minimum vruntime so an interactive wake-up is scheduled promptly without accumulating unbounded credit.

CFS-lite scheduler

Weight is an integer from 1 to 16. Each ready process carries a 64-bit vruntime and a stride = 220 / weight. While a process runs, its vruntime advances by elapsed_ms × stride; the scheduler picks the runnable process with the smallest vruntime. The result is a long-term CPU share proportional to weight.

Weights seen in the tree. The ring-3 WM is raised to 16, the kworker is set to 4 and a new process starts at 8. Bar widths are relative to the maximum weight of 16.

The run queue is a flat list rather than a red-black tree; picking the minimum is a scan. With at most 64 processes this is simple and predictable, and the scheduler core stays small enough to audit. Sleeping processes sit in a sorted sleep list, so the tick handler wakes due sleepers without scanning every process.

The big kernel lock

bkl_* is a sleeping recursive mutex, not a spinlock. Every ring-3 syscall and every kernel thread takes it; voluntary block and sleep points release it through sched_yield. Long serial and network waits use bkl_pause() / bkl_resume() so another CPU can make progress.

Why one big lock? The kernel is small and not designed around fine-grained locking. The BKL keeps every kernel invariant simple, at the cost of serializing long kernel operations. Disk I/O still holds it, except a destructive format's raw-sector loop, which releases it so the WM and terminals keep running; network and serial waits drop it.

Idle, power & thermals

idle auto is the default. It asks power.c for the deepest usable hint: C6 (0x20) on Nehalem/Westmere, C6N (0x58) on Airmont/Braswell when CPUID advertises the C6 substates, and HLT on the older Atom boards where an old USB-legacy SMM hang made deep states risky.

Source map