Calibrated timer / clock

This commit is contained in:
2026-07-03 14:17:00 +01:00
parent 9a834c91ff
commit e80043b611
9 changed files with 212 additions and 14 deletions
+23
View File
@@ -49,6 +49,8 @@ pub fn run(case: []const u8, boot_info: *const BootInfo) void {
smoke(boot_info);
} else if (eql(case, "timer")) {
timer();
} else if (eql(case, "clock")) {
clock();
} else if (eql(case, "vmm")) {
vmm();
} else if (eql(case, "heap")) {
@@ -204,6 +206,27 @@ fn heapTest() void {
result();
}
/// Verify the calibrated clock: a plausible measured LAPIC frequency, the
/// configured tick rate, and monotonic uptime that advances with real ticks.
fn clock() void {
log("DANOS-TEST-BEGIN: clock\n", .{});
// Calibration produced a sane LAPIC frequency (roughly 1 MHz .. 100 GHz).
const lapic = arch.lapicHz();
check("LAPIC frequency measured", lapic > 1_000_000 and lapic < 100_000_000_000);
// Wait for ~5 real ticks and confirm uptime advanced by about that many ms
// (tick rate is 1000 Hz, so 1 tick == 1 ms).
const start_ticks = arch.ticks();
const start_ms = arch.uptimeMs();
var spins: u64 = 0;
while (arch.ticks() < start_ticks + 5 and spins < 5_000_000_000) spins +%= 1;
const elapsed_ms = arch.uptimeMs() - start_ms;
check("uptime advances with ticks", elapsed_ms >= 5 and elapsed_ms < 100);
result();
}
fn faultInvalidOpcode() void {
log("DANOS-TEST-BEGIN: fault-ud\n", .{});
asm volatile ("ud2");