The thread control block (TCB)
(Written during milestone 14 phase B, when "where do TCBs live" became a decision.)
Renamed 2026-08-24 (DECISIONS §113, milestone 158): the kernel capability object this note
describes, "a thread under construction", was spelled Tcb (and its pointer wrapper TcbPtr) in
code until this rename; both are now ThreadControlBlock and ThreadControlBlockPointer. The
companion thread-id type Tid is now ThreadId. This note keeps saying "TCB" throughout, on
purpose. That acronym is what the type is now spelled out in full, so the informal usage and the
type name finally agree. The acronym-collision section below (Thread Control Block vs Trusted
Computing Base) is unaffected by the rename either way.
What it is
The kernel's bookkeeping record for one thread: our Thread struct in kernel/src/thread.rs.
Milestone 1 defined a thread as "a stack plus a set of register values"; the TCB is where the
kernel keeps everything needed to manage that thread while it is not running. Ours holds the id (a
generational name, notes/generational-names.md), the state, and the context pointer naming where
the registers are saved on the thread's own stack. It holds ownership of that stack, the address
space, the capability table (CapabilityTable), the IPC mailbox, the intrusive queue link
(notes/intrusive-queues.md), and the on_cpu/wake_pending switch-out flags. "Spawn allocates
a TCB" means allocating this struct. seL4 uses the same name for the same object.
The acronym collision, so nobody trips on it
TCB also means Trusted Computing Base (DECISIONS §14: "a small, machine-checked trusted core"), which is unrelated. Both senses appear in this project's documents. In milestone 14 and scheduler contexts, TCB is the thread struct; in §14 and verification contexts, it is the trusted core. Expand the term when there is any doubt.
Where TCBs live (the phase B.2 decision)
Decided at B.2: a static pool, a MAXIMUM_THREADS-sized array in BSS, a ThreadId's slot bits naming its storage directly. That pool was always a scaffold: the B.2 note said "the pool upgrades to retype-backed storage behind the table when init lands."
Milestone 19c.2 landed that upgrade, and the static pool is gone. Every Thread is now
page-resident: it lives at the start of one page, and the generational table stores a
pointer to it rather than indexing a BSS array. Kernel threads' TCB pages come from the kernel's
own budget (kmem, notes/kernel-budget.md); a user process (19c.3) retypes its TCB page from
its own memory region by the same mechanism, the page merely coming from a different budget. A page's
address never changes (direct-mapped, its region pinned), which supplies the pinning the Box
and then the pool both provided. The win over the pool: the kernel reserves no per-thread memory
it was not handed, the last corner of milestone 14's no-open-ended-spending thesis. Why this is
now worth doing when B.2 said retype earned nothing: B.2's premise was "the kernel is the only
payer," and 19c is exactly when that expires (the progenitor becomes a payer).
On the page-granularity worry the pool decision leaned on: a TCB is sub-page, so page-residency
"wastes" most of a page. That was a real reason to prefer the pool while the kernel paid. It is
not one now, because the page is paid by whoever owns the thread (a user thread's by its
creator's memory region, a kernel thread's by kmem, both bounded budgets). Sub-page packing stays
declined for the same reason it was for endpoints (19a). One memory rule for the whole object
family, packing a later placement optimization behind the table, not a slab rebuilt in the
milestone that deleted the slab.
What shares the TCB page
A Thread sits at the start of its page, and the page's idle space holds two more things: the
thread's FP/SIMD register file, then its capability table behind its own lock. The saved
general-purpose state is one stack pointer in the struct. The vector registers are four times its
size, and they live in the page beside it. Compile-time assertions in kernel/src/thread.rs are what keep
all three inside 4,096 bytes; growing Thread past the point where they fit stops the build.
The capability table lives outside the struct for soundness, not layout. The running thread reads
its own table with IPC_TABLES not held. Meanwhile another core holding IPC_TABLES may hold a
&mut Thread for the same thread: a revocation sweep's iter_mut, or an IPC writing its mailbox.
A &mut asserts that nothing else touches any byte it covers. A lock inside the struct would be
read by one core under another core's exclusive reference. No reference to the Thread covers the
bytes past its end, which is the same provenance rule the FP register file follows. Its own lock at
rank::CAPABILITY_TABLE keeps the running thread's lookups off the global lock. The order is
written at the rank in kernel/src/sync.rs.
Fields that are there for a measured reason
last_cpurecords the core a thread last ran on, to count migrations. The scheduler's ownPlaceRemotetrace event cannot. A rendezvous wake makes its peer ready on the waker's core, so it is local by construction. On 2026-09-01 (UTC) a four-core IPC soak doing 65,000 round trips a second reportedremotefrozen at 23 while threads moved between cores continuously. The write sits inschedule()'s switch. Shipping it cost 5.7% ofipc_fastpath's footprint on aarch64 (5,788 to 6,120 bytes), over the boundscript/fastpath-footprintholds. riscv64 and x86_64 grew 4.7% and 4.6%. So it exists only in asoak_testbuild.placementrecords where a thread was placed when it started, written once at creation. A supervisor in the kernel gets the same fact as a return value from the spawn. A userspace supervisor did not do the spawning, so the field gives it a path out through a survey. One store per creation is free where a store per switch was not.port_range_grantmakes the TSS I/O bitmap lazy. An always-write cost about 2,682 ns a switch. Rewriting only when the incoming thread's grant differs costs one compare on nearly every switch, since one process ever holds a port capability. This lazy form is §121 (x86 port I/O)'s refinement of 2026-08-25 (UTC), and binds here.cycle_counter_grantcostssched::schedule192 bytes for an instrument nothing can request yet, so the whole mechanism is a measurement build, like the soak.own_tokenholds the thread's queue token while no queue does. It deviates from ruling C's letter, which put the running thread's token in a per-core slot. Round 10's report of milestone 139 (drive the unsafe count down) puts that deviation to calef.
A parked capability in a corpse
Thread::outgoing_capability holds a capability a SEND_CAPABILITY parked while no receiver was waiting. The
teardown paths depart, finish_blocked_resident and reap_region_objects never clear it. That
is a latent hazard, not a defect today. Cap is Copy and lives in the TCB page, so nothing
leaks. Victims are unlinked before their pages go, so nothing is used after free. Every revocation
sweep clears a parked capability on Finished and Dead threads too, so none outlives a revoke.
Every wake of a parked sender goes through set_ipc_aborted, which clears the slot. A future wake
that skipped it would deliver the parked capability to the supervisor's endpoint. The fix is one
line in each of the three paths; finish_blocked_resident already clears a parked Reply through
delete_reply_capabilities_naming. notes/confinement-claims.md row 30 lists the sweeps that clear it.
The thread table (kernel/src/sched/thread_table.rs)
Threads is a generational table of ThreadControlBlockPointers, one per page-resident TCB. The
table stores the pointer, and the generational name is what everything else carries. A pointer may
move between cores. The Thread it names is touched only under IPC_TABLES, and its queue link
only under the intrusive discipline (notes/intrusive-queues.md).
One page, three things
The Thread sits at the start of its page. The FP register file comes after it, and the capability
table after that. get and get_mut narrow the page pointer to a reference over
size_of::<Thread>() bytes. So the register file and the table are reached from the stored page
pointer (Threads::pointer, thread::capability_table_of), never from a &Thread. A pointer
derived from the reference would reach outside its own provenance.
The two inserts initialize the register file and the table in place. The register file's live flag
must be written before anything reads it (thread::initialize_fp_state). That is a fact about a
place, so it is not in a Thread constructor. insert_at_in_place hands build the destination
rather than taking a returned Thread, because a Thread is large and a debug build copies it at
every hop.
Queue tokens
Every thread's queue token is minted once, at insert. mint_token is the only place in the kernel
a token is made. From there it moves onto a queue at a push, and back onto Thread::own_token at a
pop or removal (hold_token). It goes nowhere else. mint_token discharges Unqueued::new's
obligations:
- The
Threadlives untilThreads::remove. That is reached only for aFinishedthread off its core, which is on no queue because a running thread holds its own token, or for one thatfinish_blocked_residenthas unlinked. - The name is a moment old, so no other token for the page exists.
- Every queue holding a
Threadis behindIPC_TABLESor an inbox lock. - The token's pointer is the page pointer the table stores, which every later access derives from.
The next
get_muton the same thread would invalidate a pointer taken fromget_mut's&mut Thread, andget_mutruns on queued threads.
Removal order
remove first reads whether the page came from kmem. Then it drops the Thread in place, which
frees its kernel stack, address space and quota token. Then it kills the name. Only then does a
kmem page go home. A user TCB's page belongs to its region and is reclaimed when the region is
destroyed.
CPU time, per slot
CPU_TICKS holds each thread's scheduled ticks in an array beside the table, keyed by slot.
moho_abi::survey::record::CPU_TIME reads it (§150 (how does a thread's CPU time reach userspace?)).
It is not a u64 on Thread. The increment happens in on_tick, in interrupt context, where a core
knows its running thread only as a tid. Reaching the Thread would take IPC_TABLES, a lock wait
at every tick on every core. try_lock would drop samples exactly when the machine is busiest. The
slot is the tid's low 32 bits, so the array needs no lock, no lookup and no ordering beyond relaxed.
cpu::NO_TID (u64::MAX) falls out of range at no cost. The array is 2 KiB of .bss.
It is not on cpu::PerCpu either. The counter is per thread. Growing PerCpu from 128 to 136 bytes
cost riscv64's IPC fastpath 5.4%, because PERCPU[id] stopped indexing with a shift, and
kernel/src/cpu.rs const-asserts that size.
A core's tick increments only the slot of the thread it runs, so the write is uncontended. A survey
reading a slot another core is incrementing gets a value that was true a moment ago. It is never
torn, since the unit is an aligned u64. The counter is zeroed when a slot is filled, not emptied,
so a corpse keeps the time it earned until its supervisor reaps it.
The peak
PEAK_THREADS is the most threads alive at once on this boot, which makes MAXIMUM_THREADS a
measured number. By the time a spawn is refused, the table is full and every earlier number is gone.
It is one high-water mark rather than a per-test ledger. What fills the table is the services
earlier tests left running on purpose (notes/frames.md's "held" list), not one test's spend. Both
inserts update it under IPC_TABLES with fetch_max.
The running thread, per core
kernel/src/sched/current.rs keeps two things per core. The running thread's id is in
cpu::PerCpu::current. A pointer to its capability table is in CURRENT_CAPABILITIES, so a
capability lookup takes the table's own lock and never IPC_TABLES. set_current is the only
writer of either, on the owning core. So a lookup never finds one thread's name beside another's
table.
The id is read through arch::read_current_thread, because the read must be indivisible from a
preemptible caller. A kernel thread migrated between finding its per-CPU block and reading the field
would get another thread's name. adopt_address_space would then bind a new space to that thread.
smp::tests::a_migrated_kernel_thread_reads_its_own_name fails on all three architectures without
it.
CURRENT_CAPABILITIES is its own array, because PerCpu is held to 128 bytes. Each entry fills a
cache line. Every core writes its entry on every switch and reads it on every system call, and a shared
line would trade the lock for line traffic between the same cores
(notes/job-mix/null-system-call-under-load.md's BUGS has that shape).
The pointer is valid without a lock, because it names the page of the thread running on this core,
and a running thread's page is not recycled. Threads::remove is reached only from
reap_switched_out, for a thread its successor has switched off, and from reap_region_objects.
That one's verdict refuses a Ready or Running thread and one a core still stands on. schedule
replaces the pointer before the switch that makes the old thread reapable.
current_thread_id and set_current are on every IPC and must land in .text.hot beside their
callers. Whether LLVM inlines them moves with the codegen unit and the build host. So they are
forced inline where that does not cost the band: current_thread_id on riscv64, and set_current
on riscv64 and x86_64. On aarch64, forcing either grows system_call_entry past its band. There
set_current carries a .text.hot section instead. Inlined, the section is unused; out of line, the
copy lands beside its caller.
BUGS
thread::initialize_fp_state's doc, andthread::initialize_capability_table's after it, give "akmempage is not zeroed" as their reason. That is stale:kmem::pagezeroes a page it recycles, andmemory_region::retype_object_pagezeroes a fresh one.