From fc01f03714fca8ed9c749db3e1728fe2e562a4ca Mon Sep 17 00:00:00 2001 From: Berkus Decker Date: Sun, 30 Jul 2023 00:08:53 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Read=20actual=20timer=20f?= =?UTF-8?q?requency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/src/arch/aarch64/time.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/machine/src/arch/aarch64/time.rs b/machine/src/arch/aarch64/time.rs index 89f34f0..c7b0b00 100644 --- a/machine/src/arch/aarch64/time.rs +++ b/machine/src/arch/aarch64/time.rs @@ -12,13 +12,14 @@ //! crate::time::arch_time use { - crate::warn, + crate::{sync, warn}, aarch64_cpu::{asm::barrier, registers::*}, core::{ num::{NonZeroU128, NonZeroU32, NonZeroU64}, ops::{Add, Div}, time::Duration, }, + once_cell::unsync::Lazy, tock_registers::interfaces::Readable, }; @@ -35,22 +36,18 @@ struct GenericTimerCounterValue(u64); // Global instances //-------------------------------------------------------------------------------------------------- -/// Boot assembly code overwrites this value with the value of CNTFRQ_EL0 before any Rust code is -/// executed. This given value here is just a (safe) dummy. -#[no_mangle] -static ARCH_TIMER_COUNTER_FREQUENCY: NonZeroU32 = NonZeroU32::MIN; +static ARCH_TIMER_COUNTER_FREQUENCY: sync::NullLock> = + sync::NullLock::new(Lazy::new(|| { + NonZeroU32::try_from(CNTFRQ_EL0.get() as u32).unwrap() + })); //-------------------------------------------------------------------------------------------------- // Private Code //-------------------------------------------------------------------------------------------------- fn arch_timer_counter_frequency() -> NonZeroU32 { - // Read volatile is needed here to prevent the compiler from optimizing - // ARCH_TIMER_COUNTER_FREQUENCY away. - // - // This is safe, because all the safety requirements as stated in read_volatile()'s - // documentation are fulfilled. - unsafe { core::ptr::read_volatile(&ARCH_TIMER_COUNTER_FREQUENCY) } + use crate::sync::interface::Mutex; + ARCH_TIMER_COUNTER_FREQUENCY.lock(|inner| **inner) } impl GenericTimerCounterValue {