notes

Wall-clock time

What the machine knows about the time, who is allowed to change it, and why the three answers are three different capabilities. Milestone 51 (wall-clock time) lane A. The decision and its argument are DECISIONS §43 (reading the clock is a page), the contract is clock_contract (the repository partsfoundry/clock_contract).

Why not the counter alone

SystemTime is not the monotonic counter offset from UNIX_EPOCH, because that reports 1 January 1970 plus however long the machine has been up. The interface says nothing about it. A caveat in the notes does not fix that: a program does not read the notes. Differencing two SystemTimes would give a correct duration. Any absolute reading would be a fiction that looked exactly like a fact.

The defect is not missing hardware. It is an interface that lies, which is the same shape §42 forbids for filesystems: not that FAT is weak, but that rename() on it succeeds and the caller cannot tell.

Counter plus offset

   Instant  =  counter                     (ambient, one instruction, nobody writes it)
   wall     =  counter + offset            (the offset is the only writable thing here)

The counter is CNTVCT_EL0 on aarch64 and the time CSR on RISC-V. It is readable from EL0/U-mode because the kernel opened it (see notes/abi.md). Nothing in this design writes it, and nothing can.

That is the whole payoff: adjusting the wall clock cannot perturb monotonic time by construction. Unix has adjtime slewing partly because a backwards step breaks code that assumed time only moves forward. Here a step is an offset write and Instant never observes it. So slewing becomes a policy choice for wall-clock readers rather than a correctness requirement. The kernel test adjusting_the_wall_clock_leaves_the_monotonic_counter_alone steps the clock half a second and requires the counter to have moved only by the cost of the round trip.

Three authorities

                          the RTC's registers (a device mapping, one holder)
                                    │
                          ┌─────────▼────────┐
 propose ──an endpoint───►│  clock service   │
(bounded, policy applies) └─────────┬────────┘
                                    │ publishes
                          ┌─────────▼────────┐
                          │  the clock page  │◄── set: the SAME page, mapped read/WRITE
                          └─────────┬────────┘
                                    │ mapped read-only
                                readers
authority the capability cost of a read/write
none nothing the machine does not know what time it is, and says so
read the clock page, MAP_READ_ONLY a few loads and an add; no system call, no server, no wait
set the clock page, MAP_READ_WRITE one publish; nothing polices it
propose the propose endpoint, WRITE one CALL; the service may refuse

Nothing new was needed in the kernel: the ladder is PageFrame with READ, PageFrame with WRITE, and Rendezvous with WRITE. No new system call, no new method, no new object type. Rights are already introspectable, so caps shows which rung a process is on.

Why set is a page and not a message. A process has exactly one blocking wait point, because this kernel has no wait-any primitive and no threads sharing an address space. Two message-borne authorities would therefore need two server processes, and the second would hold full set authority anyway. Making set a page write is not a workaround for that limitation: writing the offset is what setting the clock means. §33 (the compositor's authority is memory) settled the same shape for the compositor ("the compositor's authority is memory, not messages"). §43 records the alternative (a minted endpoint carrying a badge, §25's tracked later step) and why this design does not need it.

Two copies, and why the memory ordering is load-bearing

The clock page is seven words: the magic, a publish count, the writers' claim, and two copies of the state and the offset. Readers are many and writers are few. There is no lock a process could hold across an address-space boundary, and a reader must never wait on a writer, because a writer that stops mid-update would otherwise stop SystemTime::now in every process on the machine.

This is the project's rule 4 in a place where it matters. On a strongly ordered machine a sloppy version passes every test forever, and on ARM and RISC-V it does not. clock_contract argues each ordering at its load or store. Two of them carry the case two copies alone cannot. A reader holding count g reads copy g mod 2. Two publishes later, the writer of g+2 rewrites that copy while the reader is still in it. The reader must then see a count above g on its second load. The writer's release fence before its data stores pairs with the reader's acquire fence before that load. So a reader that saw any word stored after the writer's fence also sees the count g+1 that the writer read before it. Linux's latch has the same barrier, the smp_wmb() at the head of raw_write_seqcount_latch. The comparison is equality, not "within one", because a count one higher can belong to a writer already refilling the reader's copy.

writers serialized by copies a reader retries when
Linux seqcount latch, include/linux/seqlock.h the caller two, both written per modification the count changed
Linux vDSO time, include/vdso/helpers.h the kernel's timekeeping update (not in that file) one the count is odd (it spins), or changed
FreeBSD timehands, sys/kern/kern_tc.c a spin mutex a ring, two by default and up to sixteen its record's generation is zero or changed
this page the claim word, on the page two, one written per publish the count changed

Linux's latch writes both copies so that a reader interrupting its own writer (NMI context) still has an older value; a clock page reader never runs inside its writer, so one copy per publish is enough. FreeBSD keeps a generation in each record and zeroes it during a rewrite; here the one count does both jobs, which keeps the page at seven words.

Two limits stand. Loom checks the orderings against the C11 model, not ARM's or RISC-V's, so a tear it finds is real and a clean run proves nothing about either ISA. Litmus-level confidence would need herd7-style tooling, and nothing yet reads this page from two physical cores under load. And a writer stopped while it holds the claim blocks every later writer, since a stopped writer looks like a slow one; readers keep reading the last publish.

A torn read here would be a wrong time rather than a crash, which is the worst kind of bug to leave possible, so the invariant has its own test in clock_contract.

"I do not know what time it is" is a state, and it is the default

clock_contract::state has four values:

meaning
UNKNOWN no clock page, no service, no RTC, or an RTC reading nobody believes
RTC read once at startup from the hardware clock
SET written outright by an authority holding the page read/write
SYNCED an accepted proposal, i.e. an external source the service bounded

A frame nobody has published to is zero, and zero is UNKNOWN, so the honest answer is what you get by default rather than something initialization has to remember. The clock service does not publish a reading it does not believe: an RTC outside the sanity window leaves the clock unknown rather than confidently wrong.

SET and SYNCED are kept apart because "a human told me" and "an external source I bounded" are different provenance, and that difference is what a caller weighing a certificate expiry wants.

The policy, and the asymmetry that is the point

clock_contract::policy is a pure function, host-tested in milliseconds, and it lives in the contract crate rather than inside the service so a well-behaved proposer can predict the answer. The bounds are public because the authority was never secrecy about them; it is that a proposer cannot write the page.

rule value
sanity floor 2026-01-01 (also the build-era anchor NTS's chicken-and-egg needs)
sanity ceiling 2100-01-01
max step forward 1 hour
max step backward 1 second

Forward and backward are not the same problem. Moving forward skips over instants nobody has observed yet. Moving backward makes instants happen twice, which is what breaks log ordering, cache expiries, build stamps, and anything that recorded a timestamp and assumed it would not be reissued. Hence three orders of magnitude between the two constants, and a test that fails if somebody tidies them into one.

When the clock is UNKNOWN a plausible proposal is accepted outright. That is the bootstrap, not a hole: a machine that does not know the time holds no belief a step limit could protect.

Two drivers, because parity is a gate

board binding address register layout
QEMU virt, aarch64 arm,pl031 0x9010000 one 32-bit DR at offset 0, seconds since the epoch
QEMU virt, riscv64 google,goldfish-rtc 0x101000 TIME_LOW at 0, TIME_HIGH at 4, nanoseconds; read LOW first, it latches HIGH
VisionFive 2 its own RTC via DTB milestone 16a, board bring-up

Both drivers are in the one portable clock binary, compiled on both ISAs, each taking a base address and knowing nothing else (rule 2).

Discovery is by compatible, not by node name. The aarch64 board calls the node pl031@9010000; the RISC-V board calls its RTC rtc@101000. No name prefix finds both, so this uses dtb::node_register_regions_compatible. Two wrinkles the fixture tests pin. compatible is a NUL-separated list and a match on any entry counts (the PL031 declares "arm,pl031", "arm,primecell"). The node writes reg before compatible, so the decode has to wait for the node to close rather than decide at the reg property.

The kernel passes the binding to the service at spawn, so the driver picks its layout from what the machine said rather than from target_arch. That is not fastidiousness: the VisionFive 2 is riscv64 and has neither of these devices, so an ISA-keyed driver would compile clean and read garbage on the first real board.

The x86 CMOS RTC, read by the clock service

x86's real-time clock is the CMOS RTC behind two fixed I/O ports, 0x70 (index) and 0x71 (data), clock_contract::real_time_clock::INDEX_PORT and DATA_PORT. There is no page to map, so the kernel grants the clock service a PortRange capability over both ports (§121 (x86 port I/O)), with WRITE because the TSS I/O permission bitmap cannot permit in without out. The service's cmos module reads the device itself, as its PL031 and Goldfish drivers read theirs, and the kernel reads no CMOS (§130 (how the userspace clock service reads the x86 CMOS RTC)). The kernel writes the bitmap lazily, so only a switch to or from the clock service pays for it.

register what it holds
0x00 seconds
0x02 minutes
0x04 hours, bit 7 the PM flag in 12-hour mode
0x07 day of month
0x08 month
0x09 year, two digits, no century
0x0A status A; bit 7 is Update In Progress (UIP)
0x0B status B; bit 2 binary (1) or BCD (0), bit 1 24-hour (1) or 12-hour (0)

Every field but status B may be BCD. Status B says which, so it is read after the six date and time registers.

Tearing

The update cycle rewrites the time and date bytes about once a second. A read that lands mid-update can pair a minute of 59 with an hour that already rolled over. UIP going low promises the previous update finished, not that the next will not start before the first port read. So the driver takes two full snapshots, each after its own UIP wait, and retries until they agree. A real BIOS RTC driver does the same (the OSDev wiki's CMOS page documents it).

The driver leaves daylight saving and the alarm and periodic-interrupt registers alone. The service reads the clock once; after that, clock_contract's wall clock is the counter plus an offset, so nothing needs an alarm, a periodic tick or status B's DST bit.

The century

Most firmware exposes a non-standard century byte at a chipset-specific index (0x32 on many; the FADT names it when it exists). The driver does not read it. Every machine able to run this kernel is past 2000, so the year decodes as 2000 + value. Linux's drivers/rtc/rtc-cmos.c makes the same assumption.

Where the interactive boot puts it (milestone 51's wiring)

Both ISAs' interactive boots start the clock service before the progenitor exists and grant the progenitor the page with READ and GRANT: slot 3 on every architecture, set in one function, boot_progenitor. The progenitor hands a read-only copy plus a read-only mapping at 0x00c0_0000 to any child whose grant_plan manifest declares clock, which today is date and nothing else.

Three things about that shape are deliberate:

script/swish-check types date at the real prompt on both ISAs and requires UTC in the answer, which is the one word neither unknown-clock sentence contains.

What uptime can and cannot say

crates/uptime prints how long the counter has been counting, and that is not quite how long the kernel has been up. CNTVCT_EL0 and its RISC-V and x86_64 equivalents are free-running hardware counters, set by firmware or the VMM at reset and not by this kernel. date's module docs call the counter ambient and untouched for the same reason. So "up" means "since the counter started counting", which is at or before kernel entry. On real hardware the gap is whatever the boot ROM and firmware took, and it is unmeasured. There is no kernel-boot epoch, Linux's CLOCK_BOOTTIME, to read instead.

Upstream uptime also prints a load average and a logged-in-user count, and neither exists here. sched.rs tracks preemptions and per-core run queues, not a decaying load average. "Logged in" presumes a login registry this system does not keep. A placeholder zero would read as a real answer.

What a std program sees

SystemTime::now() is the clock page plus the counter, and a std program's whole wall-clock authority is slot 5 (a PageFrame capability with READ) and a read-only mapping at rt::CLOCK_PAGE. See notes/std.md.

The uncomfortable part, recorded rather than smoothed over: SystemTime::now() has no error channel, so the only loud refusal available when the clock is unknown is a panic. That is what it does, with a message naming which of the two causes it was. A program that never asks the time is unaffected, but a program cannot ask whether it can ask, because std has no way to represent "I do not know". The readable form of the state lives one level down in clock_contract for anything that wants to check first, and a no_std component simply reads the page.

What this lane did not build