fix(timer): don't panic when the timer frequency is zero

PlatformTimer::start panicked if the timer frequency was zero. On
aarch64 the frequency comes from CNTFRQ_EL0, which some firmware leaves
unprogrammed, and on x86_64 a Stall that returns immediately measures
zero. The timer is only used for bootloader interface timestamps, so
log a warning and report zero durations instead.
This commit is contained in:
alex authored and Alex Zenla committed 2026-09-26 20:52:15 -07:00
1 parent afbba2c6d3
commit 69fd8a0064
1 file changed
+8 -2
+8 -2
View File
@@ -2,6 +2,7 @@
// Architecturally modified to support UEFI and remove x86 (32-bit) support.
use core::time::Duration;
use log::warn;
/// Support for aarch64 timers.
#[cfg(target_arch = "aarch64")]
@@ -35,7 +36,11 @@ impl TickFrequency {
}
/// Produce a duration from the provided elapsed `ticks` value.
/// If the frequency is unknown, the duration is zero.
fn duration(&self, ticks: u64) -> Duration {
if self.ticks() == 0 {
return Duration::ZERO;
}
let accuracy = self.nanos();
let nanos = ticks as f64 * accuracy;
Duration::from_nanos(nanos as u64)
@@ -56,9 +61,10 @@ fn arch_frequency() -> TickFrequency {
let frequency = aarch64::frequency();
#[cfg(target_arch = "x86_64")]
let frequency = x86_64::frequency();
// If the frequency is 0, then something went very wrong and we should panic.
// Some firmware does not program the timer frequency. The timer is only used to report
// timestamps to the bootloader interface, so this should not prevent booting.
if frequency.ticks() == 0 {
panic!("timer frequency is zero");
warn!("timer frequency is zero, timestamps will not be reported");
}
frequency
}