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.
- 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 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.
- 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 (percentage tooltip below it on hover) 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
sched.c,sched.h— scheduler core and BKLprocess.c,process.h— PCBs and lifecycleswitch.S— context switchtss.c,tss.h— per-CPU TSSacpi.c,ioapic.c,lapic.c,smp.c— SMP and IOAPIC maskingap_tramp.S— AP startup blobtimer.S,idle.c,power.cthermal.c— Intel DTS and AMD D18F3xA4 samplingspinlock.h— IRQ-safe spinlocks