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; there is no IOAPIC driver. After ACPI init the kernel masks every normal IOAPIC redirection entry (leaving ExtINT entries alone, in case the 8259 is routed through an IOAPIC pin), so firmware-left vectors cannot collide with the PIC vectors or the LAPIC timer. 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.
It also caches the FADT power-management blocks and scans the DSDT
(or an SSDT) for \_S5. The poweroff command
read-modify-writes the sleep type into PM1a/PM1b, keeping the existing
SCI_EN bit set — writing only the sleep bits clears it,
and some real chipsets then ignore the request. It writes
SLP_TYP first and SLP_EN in a second write.
Intel-style southbridges clear SMI_EN.SLP_SMI_EN (bit 4
at PMBASE+0x30): while that bit is set the chipset turns
the SLP_EN write into an SMI instead of a power
transition. NVIDIA nForce/MCP boards (the ION) run S5 through the
firmware SMM instead: the kernel writes the sleep state into the
BIOS NVS (ASSB=5, Linux AOTB/OSTP,
AAXB=0) and arms PS1S/PS1E in
the SMI block, matching the DSDT's _PTS(5), before the
SLP_EN write. If SCI_EN is clear it first
enables ACPI mode through the FADT SMI command (or the conventional
0xB2/0xF1 pair). BIOS and UEFI boots share
this path because both hand the kernel the same ACPI tables; only the
RSDP discovery differs (EBDA scan versus the loader's boot info).
Emulators whose tables do not decode \_S5 fall back to
the Bochs/QEMU shutdown ports. A failed poweroff returns to the
desktop instead of halting, and the boot/poweroff log carries the
PM1, \_S5 and SCI_EN state.
- 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 trampoline clears
CR0.CD/CR0.NW: INIT leaves an AP with caching disabled (CR0 = 0x60000010), and before this every ring-3 process on an AP ran about 100× slower on real hardware (QEMU does not model caches). The wake-up value is written back to0x6F10and logged assmp: AP n start cr0 …. - 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. Every CPU also runs
cpu_init_fpu() (CR0.NE/MP,
FNINIT): an AP leaves INIT with CR0.NE clear
and an x87 control word of 0x0040, so every inexact
floating-point result froze it until the next timer tick, and the
browser's JavaScript ran hundreds of times slower on an AP. Each
process owns its x87 state; schedule() saves it with
FNSAVE and restores it with FRSTOR on every
switch (108 bytes in struct process, no FXSR needed).
No IOAPIC driver. Device IRQs never reach an AP,
and pit_irq_stub is BSP-only. The kernel masks the
IOAPIC's normal redirection entries after ACPI init
(ioapic.c) and keeps the 8259 path on
LAPIC LINT0; ExtINT entries survive so firmware that routes the
8259 through an IOAPIC pin still works.
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, 32-page (128 KB) 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.
- Lock-free clock.
SYS_GET_TICKS_MSis answered before the lock is taken (one aligned counter read). It is the hottest syscall: QuickJS's interrupt check called it every ~10000 operations and slept on a contended lock each time, and the browser's interrupt hook now reads the clock only every 16th check. - Contention statistics.
schedprints how often a taker had to sleep, the total wait and the longest single hold with its holder, all since the previoussched. - 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, 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.
- Nehalem/Westmere P-states.
power_initenables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios fromPLATFORM_INFOand starts at the maximum non-turbo ratio. The target ratio goes in bits 15:8 ofIA32_PERF_CTLwith the low byte zero. - Airmont/Braswell P-states. Family 6 model
0x4Creads the Atom ratio/VID MSRs (0x66A..0x66D),MSR_FSB_FREQand the FSB table. The N3060 reports pstates 6..20, BCLK 80 MHz (1600 MHz nominal), turbo pstate 31 (2480 MHz) and VID 0x2D..0x3C.power_initenters at max non-turbo and the governor drops to pstate 6 (480 MHz);IA32_PERF_CTLcarries the pstate in 15:8 and the Linux-interpolated VID in 7:0. Turbo is detected but not requested. The probe deliberately avoids package/power-SKU MSRs; an unsupportedrdmsrbefore the IDT triple-faults. - Governor. A 100 ms window picks min/max/linear target pstates; short busy streaks jump to max immediately. Each CPU writes its own
IA32_PERF_CTL;cpufreq [auto|min|max|off|<pstate>]overrides it, andSYS_CPUFREQexposes the same auto/min/max/off mode to the control panel's Power mode tile. - Telemetry.
powerprints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and livePERF_CTL;sensorsprints the CPU temperature, board sensors and both EC types;powerinfoprints the AC adapter and battery state, and the WM top bar shows the temperature plus a far-right AC plug or level-filled battery icon on ThinkPads and Chromebooks. - Thermal. On Intel, each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3. TjMax comes from
IA32_TEMPERATURE_TARGETon Nehalem/Westmere, the coretemp 90 °C table on Airmont, and the Bonnell table on older Atoms. On AMD family 15h models below0x60(Bulldozer/Piledriver/Steamroller), thermal.c reads the package Tctl fromD18F3xA4at PCI0:18.3(k10temp's(raw × 125) m°C, with the 49 °C range offset when bits 17:16 are both set) and publishes it to every CPU from the kworker under the BKL; PCI config space is deliberately not touched from the per-CPU timer interrupt. - Limits. Package C-states/S0ix remain firmware-managed; Airmont reports no C3/C6 residency and does not request the turbo pstate. The explicit
idle c6override is the Nehalem0x20hint; useidle autofor the Airmont C6N (0x58) path.
Source map
kernel/sched.c,kernel/sched.h— scheduler core and BKLkernel/process.c,kernel/process.h— PCBs and lifecyclekernel/switch.S— context switchkernel/tss.c,kernel/tss.h— per-CPU TSSkernel/acpi.c,kernel/ioapic.c,kernel/lapic.c,kernel/smp.c— SMP, IOAPIC masking and the FADT S5 shutdown datakernel/poweroff.c— ACPI S5 poweroff with the emulator port fallbackkernel/ap_tramp.S— AP startup blobkernel/timer.S,kernel/idle.c,kernel/power.ckernel/thermal.c— Intel DTS and AMD D18F3xA4 samplingkernel/spinlock.h— IRQ-safe spinlocks