From 8c28d804bfd883b1cf569fd43c6f12503af78d70 Mon Sep 17 00:00:00 2001 From: Aelin Date: Thu, 30 Jul 2026 13:17:59 +0000 Subject: [PATCH 1/2] fix(aarch64/processor): avoid losing timer precision Dividing first and then multiplying could hypothetically result in losing accuracy. If, for example, wt is 999, then wt / 1000 is 0 and we end up with a deadline that is 0. If we perform the multiplication first, we preserve precision. --- src/arch/aarch64/kernel/processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/aarch64/kernel/processor.rs b/src/arch/aarch64/kernel/processor.rs index ce14944412..69d6fa4fa2 100644 --- a/src/arch/aarch64/kernel/processor.rs +++ b/src/arch/aarch64/kernel/processor.rs @@ -330,7 +330,7 @@ fn __set_oneshot_timer(wakeup_time: Option) { // wt is the absolute wakeup time in microseconds based on processor::get_timer_ticks. let freq: u64 = CPU_FREQUENCY.get().into(); // frequency in KHz - let deadline = (wt / 1000) * freq; + let deadline = wt * freq / 1000; CNTP_CVAL_EL0.set(deadline); CNTP_CTL_EL0.write(CNTP_CTL_EL0::ENABLE::SET); From 14a9820634d2f162fdb4f7fa9c7f0e20632fc20c Mon Sep 17 00:00:00 2001 From: Aelin Date: Thu, 30 Jul 2026 13:22:02 +0000 Subject: [PATCH 2/2] fix(aarch64/processor): configure timer correctly All of our absolute times are boot-relative, so we need to add the BOOT_COUNTER offset to the absolute value before programming CNTP_CVAL_EL0. Otherwise, if BOOT_COUNTER has a nonzero value, our timer interrupts would always be in the past and fire constantly. --- src/arch/aarch64/kernel/processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/aarch64/kernel/processor.rs b/src/arch/aarch64/kernel/processor.rs index 69d6fa4fa2..b29e621dcc 100644 --- a/src/arch/aarch64/kernel/processor.rs +++ b/src/arch/aarch64/kernel/processor.rs @@ -330,7 +330,7 @@ fn __set_oneshot_timer(wakeup_time: Option) { // wt is the absolute wakeup time in microseconds based on processor::get_timer_ticks. let freq: u64 = CPU_FREQUENCY.get().into(); // frequency in KHz - let deadline = wt * freq / 1000; + let deadline = BOOT_COUNTER.get().unwrap() + wt * freq / 1000; CNTP_CVAL_EL0.set(deadline); CNTP_CTL_EL0.write(CNTP_CTL_EL0::ENABLE::SET);