roadmap BUILT

229. Build the cycle-counter grant DECISIONS 139 decided

This is the build work for DECISIONS §139 (who may read the cycle counter, and by what authority).

It was minted Gate: NONE, and that held: DECISIONS 139 was already answered, the default was made a fact rather than an assumption by milestone 228 (the cycle counters are closed by assumption), and nothing here needed a board. What only a board can settle is in BUGS below.

In brief. DECISIONS 139 is DECIDED in three parts, and this milestone is the first two of them:

Why the cost is known before the work starts

139 priced it: one comparison, and one msr only when the value changes. That is exactly the shape kernel/src/arch/aarch64/mmu.rs's switch_user_root already has, which early-returns when TTBR0_EL1 already holds the wanted value. If no thread is granted, the value never changes and the whole cost is a compare.

script/fastpath-footprint has a 5% bound and it fired on a lane today (milestone 221's soak counters grew the aarch64 IPC path 5.7%). The switch is not the IPC fastpath, but it is adjacent enough that the gate should be run rather than assumed.

What it is not

It is not milestone 147 (a profiler that holds exactly the counters it was granted). This says this thread may read the counter, not this profiler may read that subtree's counters. 147 wants cross-thread authority with a named target, which neither this nor any option in 139 provides, and 139 records that explicitly so nobody reads this as having built it.

It does not buy timing confinement, and notes/confinement-claims.md now carries the row saying so: two threads and a shared word reconstruct a 6.8 ns clock on any of the three architectures. What this buys is accountable authority, meaning the cheap accurate path is granted rather than ambient and the kernel knows which threads hold it. Anything this milestone prints or documents must not imply more.

What was built

2026-09-02. Four pieces, and the third is the one that had to be measured rather than argued.

The grant is a bool on the TCB, Thread::cycle_counter_grant, set by sched::grant_cycle_counter, which refuses a thread that is not an embryo. That refusal is the security property rather than housekeeping. CONFIGURE and CAP_INSERT make the same one, so the things you may do to a thread before it runs are exactly the creation-time manifest DECISIONS 139 part 2 asked for, and there is no later. One-way: an embryo starts closed and nothing but this opens it.

And there is deliberately no system call method to call it, which is the one thing about this milestone a reader will otherwise assume somebody forgot.

The lane built one (abi::thread_control_block::GRANT_CYCLE_COUNTER, method 3) and calef removed it on review, on an argument the lane did not have. DECISIONS 139 declined a first-class capability object partly because seL4's own RFC-16 for that shape has been unmerged since 2024-02-02. The prior art for this shape is seL4_TCB_SetAffinity, a per-thread property expressed as a TCB method, which MCS deleted outright and replaced with a core that is a field of sched_control_cap. seL4 could not see that corner coming. This tree can: milestone 147 (a profiler that holds exactly the counters it was granted) is already written down, wants cross-thread authority with a named target, and no method here would provide it. Minting a method number whose retirement is already on the roadmap is spending an irreversible thing on a path with a visible end.

The alternative was never method-now against capability-now. 139 records that 147's target-naming has no precedent in this tree to price from, so the object is not buildable yet either. It was method-now against not yet, and not-yet costs almost nothing, because the grant has no consumer: milestone 74's aarch64 half is the first and does not exist. Whoever needs it mints it, with a requirement in hand.

A field on CONFIGURE is not the alternative either, and that was established by looking. invoke carries three argument registers; CONFIGURE spends all three (entry, user stack, address-space slot) and START spends all three on the child's first registers. There is no spare word, so expressing the grant through the existing methods would mean widening invoke itself, which is a larger and equally irreversible change than the method being deferred. The finding is kept because the next person needs it: it is the reason the cheap-looking door is not a door.

The standing note lives in crates/moho_abi's thread_control_block module, where a reader looking for the method meets the reason it is absent. Thread::cycle_counter_grant and every arch name here are provisional.

The switch writes it beside the address-space root. sched::schedule reads the incoming thread's bool out of the same locked block that reads its ttbr0, and calls arch::timer::set_cycle_counter_grant immediately after mmu::switch_user_root, which is the adopt_address_space shape one register over. The arch call compares before it writes, so a machine where nothing is granted never writes the register at all.

Two honest implementations, not a shared abstraction, which is the question the block below left open. The two registers disagree on both halves, and each disagreement decides one implementation:

One call site, three implementations behind one name, which is what switch_user_root itself already is. A trait would have added a vocabulary without removing a difference.

What it costs, measured

script/fastpath-footprint, this branch against 8a418981 built in a separate worktree:

ISA ipc_fastpath system_call_entry
aarch64 5852 -> 5988 (+136 bytes, +2.3%) 3304 -> 3304 (unchanged)
riscv64 5132 -> 5148 (+16 bytes, +0.3%) 1870 -> 1870 (unchanged)
x86_64 6687 -> 6687 (unchanged) 1637 -> 1637 (unchanged)

The gate is green on all three. system_call_entry is byte-identical everywhere, which is what minting no method means measured rather than asserted, and x86_64 is byte-identical on both, which is the no-op compiling to nothing rather than a coincidence. That is the closest thing to "an ungranted kernel is unchanged" that is actually true: on the other two the switch mechanism has to exist in the image, and what an ungranted thread pays at runtime is a load, a compare and a return.

The gate fired once, on the ABI that was then removed, and the number is worth keeping because it is an argument. Written as an ordinary arm inside invoke, the method put 224 bytes on riscv64's system_call_entry, 12% against the 5% bound, because invoke inlines into the dispatcher every system call fetches. Extracting it #[inline(never)] the way milestone 156 (system_call_entry's measured size) extracted the other spawn-path bodies brought it to +1.5%; removing the method brought it to zero. A method on this object is not free even when nothing calls it, which is one more thing to weigh when somebody mints the real surface.

How it was verified

script/test green on all three architectures, with three new tests.

What that test buys through a back door, and what the back door costs. With no ABI there is no honest userspace route to a granted thread, so the grant is applied by a #[cfg(test)] function, sched::grant_cycle_counter_to_current, which deliberately breaks the embryo rule and cannot exist in a shipped kernel. Same spirit as the soak and fastpath_pad affordances this tree already carries. What is therefore not exercised end to end is the embryo-only path a real ABI would go through; sched's own refusal test covers that rule directly, and the two together are what the one EL0 test used to cover alone.

The negative half does not run on x86_64, where rdtsc is ambient and an ungranted read is not an error. That skip is DECISIONS 139 part 3 showing up in a test rather than a gap in one.

BUGS

Follow-on

Index row

the kernel half is complete and the system call method is deliberately not minted: a grant on the TCB, written at the context switch beside the address-space root, aarch64 PMUSERENR_EL0.CR cached, riscv64 scounteren.CY read back so TM survives. x86_64 is a no-op that leaves its fastpath byte-identical