6. Threads, the context switch, and preemption
Built in 782d4bb (2026-07-14): two threads that never yield, 48 preemptions in half a second,
"neither asked to be interrupted; both were." The argument from DECISIONS §5 (preemptive threads
with real stacks), executable.
What the commit records that still shapes the kernel:
- A suspended thread's entire saved CPU state is eight bytes, a stack pointer, because everything
else is on the stack it points at. The context switch is fifteen instructions ending in a
retthat returns somewhere else. - A thread that has never run needs no special start path: a forged switch frame makes the same
retthat resumes a thread also start one. Milestone 7a later pulled the identical trick to enter EL0 ("a fake way back" througheret). - The trampoline must unmask interrupts by hand, because a brand-new thread has no saved SPSR; miss that one instruction and the first spawned thread can never be preempted, "a cooperative scheduler with extra steps."
- The reaper test failed with exactly two leaked frames, which were page tables, which exposed that stack VAs were bump-allocated and never reused. Fixed with a free list, and a test asserts a second batch of eight threads costs zero additional frames.
Follow-on
- None. The block is a backfill of what the commit settled. Every surprise it records was closed in the same work: the trampoline unmasks interrupts by hand, and the two leaked page-table frames became a stack-VA free list with a test asserting a second batch of eight threads costs no frames.
Index row
a hostile loop that never yields is preempted anyway, DECISIONS §5 made executable