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.
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().
- Each CPU has a
struct cpuholdingcurrent,idle, its TSS, LAPIC id, tick period and scheduling flags. %gs:0points at the current CPU'scurrentfield; switch.S reads it directly. A static assert locks the offsets.- The TSS
esp0is updated byschedule(), so ring-3 entry always lands on the process's kernel stack. process_init()must run beforetimer_init(): once the PIT starts, the timer stub can switch context on every tick.
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.
| Source | Vector | Role |
|---|---|---|
| 8259 PIC + PIT | IRQ0, etc. | BSP global clock and device interrupts; PIT tick advances the millisecond clock. |
| LAPIC timer | 0x30 | Per-AP scheduling tick; 1 ms busy, 10 ms idle. |
| LAPIC IPI | 0x31 | Reschedule request; must be EOIed on the target LAPIC. |
int 0x80 | syscall gate | Ring-3 service entry. |
int 0x81 | voluntary yield | Blocking 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.
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.
- Copy the AP trampoline to
0x6000(SIPI vector0x06). - Allocate an AP idle PCB and kernel stack via
process_init_ap(). - Fill the startup block at
0x6F00with CR3, stack top, C entry and CPU index. - Send INIT, then SIPI-SIPI, with a second SIPI retry.
- The AP enters
smp_ap_main, initialises its LAPIC timer and idle loop, and waits. - 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.
- Kernel threads are created with
process_createand take the BKL automatically. - Ring-3 processes are created from an ELF image with their own page directory, 8-page user stack and cwd.
- The kworker is an ordinary kernel process that performs housekeeping and zombie reaping, so it cannot be starved by a busy system.
- Stable naming: process names are 12 bytes; the PCB holds the TTY id, cwd, affinity mask, CPU accounting and the scheduling entity.
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.
- 10 ms slice. A running process is preempted at the slice boundary unless it holds the BKL or has preemption disabled.
- Global run queue. A single linked list protected by
sched_lock; every CPU scans it for the minimum vruntime. - Wake-up clamp. A process waking from sleep or a wait queue is moved forward to the current minimum vruntime rather than receiving unlimited credit.
- Affinity. A CPU bitmask restricts where a process may run; kernel threads are pinned to the BSP.
renicechanges a live process's weight from the shell; the new stride takes effect immediately.
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.
- Not timer-preempted. A CPU running a BKL holder is skipped by the tick decision, so the owner can safely be in the middle of kernel data structures.
- Recursive. The owner may re-enter; the depth is restored across a block in the PCB's
bkl_savedfield. - Sleep, not spin. A waiter blocks on the BKL wait queue and is woken when the owner releases.
- Idle uses trylock. The idle fallback never blocks on the BKL; kworker and kernel commands take it normally.
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; 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 the
Nehalem/Westmere allowlist, HLT everywhere else. The Atom boards keep
HLT because of an old USB-legacy SMM hang; the UHCI/OHCI
0xC0 legacy-SMI disable addresses it.
- P-states. On the allowlisted Nehalem/Westmere models,
power_initenables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios and starts at the maximum non-turbo ratio. - Governor. A 100 ms window picks min/max/linear target ratios; short busy streaks jump to max immediately. Each CPU writes its own
IA32_PERF_CTL. - Telemetry.
powerprints effective MHz from APERF/MPERF, ratios and C3/C6 residency;sensorsprints DTS, board sensors and ThinkPad EC temperatures/fan;powerinfoprints the AC adapter and battery state, and the WM top bar shows the AC adapter state and battery percentage on ThinkPads. - Thermal. Each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3.
Source map
sched.c,sched.h— scheduler core and BKLprocess.c,process.h— PCBs and lifecycleswitch.S— context switchtss.c,tss.h— per-CPU TSSacpi.c,lapic.c,smp.c— SMPap_tramp.S— AP startup blobtimer.S,idle.c,power.cthermal.c— DTS samplingspinlock.h— IRQ-safe spinlocks