|
| 1 | +// openkal.time on the RISC-V Supervisor Binary Interface. |
| 2 | +// |
| 3 | +// ⚠️ THIS FILE EXISTS BECAUSE THE REASON RECORDED FOR ITS ABSENCE WAS WRONG. |
| 4 | +// |
| 5 | +// kal.cpp said, and said it as a design decision rather than as a note: |
| 6 | +// |
| 7 | +// `time` is deliberately absent even though SBI has a timer extension: |
| 8 | +// SBI can arm a timer interrupt, which is a mechanism for a kernel rather |
| 9 | +// than a clock a program can read, and reporting a clock that does not |
| 10 | +// advance would make every timed wait silently wrong. |
| 11 | +// |
| 12 | +// The first half is true and the second does not follow from it. SBI's timer |
| 13 | +// extension does arm an interrupt, and it is indeed a kernel's mechanism. But |
| 14 | +// this architecture ALSO exposes a counter directly to the program: `rdtime` |
| 15 | +// reads the `time` CSR, which the Zicntr extension defines and which firmware |
| 16 | +// makes readable below its own privilege level. Those are two different |
| 17 | +// facilities, and the absence of the first says nothing about the second. |
| 18 | +// |
| 19 | +// Measured 2026-08-23 under OpenSBI on QEMU's `virt`, from a program in |
| 20 | +// supervisor mode with no kernel beneath it: |
| 21 | +// |
| 22 | +// t0=333572 t1=381292 ADVANCES |
| 23 | +// |
| 24 | +// ⭐ The lesson is the one the repository keeps relearning: a conclusion gets |
| 25 | +// rechecked and the reason written beside it does not. The reason above sat in |
| 26 | +// a comment for as long as the file existed, and the two minutes that refuted |
| 27 | +// it were available the whole time. |
| 28 | +// |
| 29 | +// WHAT IS AND IS NOT AVAILABLE HERE |
| 30 | +// |
| 31 | +// A monotonic count, precisely; a granularity, exactly; a sleep, by spinning on |
| 32 | +// the same counter, which on a machine with one execution context and no |
| 33 | +// scheduler is what sleeping is rather than a simulation of it. |
| 34 | +// |
| 35 | +// ⚠️ NOT a wall clock. SBI defines no facility for one, and no board fact would |
| 36 | +// supply it either --- a real-time clock is a device, and reading it is what a |
| 37 | +// board backend does. `KAL_TIME_PROP_WALL_AVAILABLE` is left clear, which is |
| 38 | +// the specification's own way of saying so: clause 6.2 makes availability of a |
| 39 | +// wall clock a PROPERTY rather than an interface, so `kal_time_wall` is present |
| 40 | +// and reports zero, and a program reads the word before it reads the clock. |
| 41 | + |
| 42 | +#include <openkal/time.h> |
| 43 | + |
| 44 | +#include "sbi.h" |
| 45 | + |
| 46 | +namespace { |
| 47 | + |
| 48 | +// ⚠️ THE ONE BOARD FACT THIS PACKAGE TAKES, AND IT TAKES IT AS AN INPUT. |
| 49 | +// |
| 50 | +// `rdtime` counts at a rate the architecture does not fix. The rate is |
| 51 | +// published in the device tree as `/cpus/timebase-frequency`, and this package |
| 52 | +// does not read one: the device tree is handed to the image in a register at |
| 53 | +// entry, the entry sequence belongs to the consumer rather than to this |
| 54 | +// package, and requiring every consumer to forward it would change a contract |
| 55 | +// that today is "jump here with a stack". |
| 56 | +// |
| 57 | +// So the rate is a build input, exactly as the heap size already is, and for |
| 58 | +// the same reason --- a figure that belongs to the machine is declared by the |
| 59 | +// project that knows which machine, rather than assumed by a package that does |
| 60 | +// not. The default is QEMU's `virt`, which is what the example runs on. |
| 61 | +// |
| 62 | +// ⚠️ A project on other hardware that leaves the default in place gets a clock |
| 63 | +// that advances at the wrong rate. That is a stated bound and not a hidden one: |
| 64 | +// the figure has a name, the name appears in the manifest, and this comment is |
| 65 | +// what a reader finds when they look for it. |
| 66 | +#ifndef OPENKAL_OPENSBI_TIMEBASE_HZ |
| 67 | +# define OPENKAL_OPENSBI_TIMEBASE_HZ 10000000 |
| 68 | +#endif |
| 69 | + |
| 70 | +constexpr kal_u64 kHz = OPENKAL_OPENSBI_TIMEBASE_HZ; |
| 71 | +constexpr kal_u64 kNano = 1000000000ULL; |
| 72 | + |
| 73 | +inline kal_u64 ticks() { |
| 74 | + kal_u64 v; |
| 75 | + __asm__ __volatile__("rdtime %0" : "=r"(v)); |
| 76 | + return v; |
| 77 | +} |
| 78 | + |
| 79 | +// Split rather than `t * kNano / kHz`, which overflows a 64-bit product after |
| 80 | +// about eighteen seconds at ten megahertz. The split form is exact for every |
| 81 | +// value the counter can hold. |
| 82 | +inline kal_u64 to_ns(kal_u64 t) { |
| 83 | + return (t / kHz) * kNano + (t % kHz) * kNano / kHz; |
| 84 | +} |
| 85 | + |
| 86 | +inline kal_u64 to_ticks(kal_u64 ns) { |
| 87 | + return (ns / kNano) * kHz + (ns % kNano) * kHz / kNano; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace |
| 91 | + |
| 92 | +extern "C" { |
| 93 | + |
| 94 | +kal_duration kal_time_monotonic(void) { return to_ns(ticks()); } |
| 95 | + |
| 96 | +// Present and reporting zero, which clause 6.2 provides for: the availability |
| 97 | +// of a wall clock is a property of the implementation, the property word says |
| 98 | +// this one has none, and a program that reads the word does not reach here. |
| 99 | +kal_duration kal_time_wall(void) { return 0; } |
| 100 | + |
| 101 | +// One tick, in nanoseconds, rounded up so that the figure is never reported as |
| 102 | +// finer than it is. At ten megahertz this is 100. |
| 103 | +kal_duration kal_time_monotonic_granularity(void) { |
| 104 | + const kal_u64 g = kNano / kHz; |
| 105 | + return g ? g : 1; |
| 106 | +} |
| 107 | + |
| 108 | +// ⚠️ Spinning, and that is the accurate implementation rather than a stand-in. |
| 109 | +// |
| 110 | +// Sleeping means giving the machine to something else until a time arrives. On |
| 111 | +// a machine with one execution context and nothing to give it to, the time |
| 112 | +// still has to arrive, and waiting for it on the same counter the caller would |
| 113 | +// read is precise to a tick. `KAL_TIME_PROP_SLEEP_PRECISE` is therefore set, |
| 114 | +// and it is set truthfully: there is no scheduler to overshoot. |
| 115 | +// |
| 116 | +// A `wfi` between polls would lower power, and it is not used, because it |
| 117 | +// requires an interrupt to be pending to wake from --- which requires SBI's |
| 118 | +// timer extension, which requires a trap handler, which this arrangement does |
| 119 | +// not have. The comment kal.cpp got wrong was about that extension; this is |
| 120 | +// where it would have been right. |
| 121 | +void kal_time_sleep(kal_duration ns) { |
| 122 | + if (ns == 0) return; |
| 123 | + const kal_u64 deadline = ticks() + to_ticks(ns); |
| 124 | + while (ticks() < deadline) {} |
| 125 | +} |
| 126 | + |
| 127 | +const kal_uintptr kal_time_props = KAL_TIME_PROP_SLEEP_PRECISE; |
| 128 | + |
| 129 | +} // extern "C" |
0 commit comments