diff --git a/kernel/comps/time/src/tsc.rs b/kernel/comps/time/src/tsc.rs index f069e4d8..981f577d 100644 --- a/kernel/comps/time/src/tsc.rs +++ b/kernel/comps/time/src/tsc.rs @@ -6,10 +6,9 @@ use alloc::sync::Arc; use core::sync::atomic::{AtomicU64, Ordering}; -use ostd::arch::{ - read_tsc, - timer::{self, TIMER_FREQ}, - tsc_freq, +use ostd::{ + arch::{read_tsc, timer::TIMER_FREQ, tsc_freq}, + timer, }; use spin::Once; diff --git a/kernel/libs/aster-bigtcp/src/iface/time.rs b/kernel/libs/aster-bigtcp/src/iface/time.rs index 7936dd74..cd6d70df 100644 --- a/kernel/libs/aster-bigtcp/src/iface/time.rs +++ b/kernel/libs/aster-bigtcp/src/iface/time.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::arch::timer::Jiffies; +use ostd::timer::Jiffies; pub(super) fn get_network_timestamp() -> smoltcp::time::Instant { let millis = Jiffies::elapsed().as_duration().as_millis(); diff --git a/kernel/src/net/iface/poll.rs b/kernel/src/net/iface/poll.rs index 67f2ca70..f46284e3 100644 --- a/kernel/src/net/iface/poll.rs +++ b/kernel/src/net/iface/poll.rs @@ -4,7 +4,7 @@ use alloc::sync::Arc; use core::time::Duration; use log::trace; -use ostd::arch::timer::Jiffies; +use ostd::timer::Jiffies; use super::{ext::IfaceEx, Iface, IFACES}; use crate::{ diff --git a/kernel/src/process/process/timer_manager.rs b/kernel/src/process/process/timer_manager.rs index 0a31aabd..bb42b073 100644 --- a/kernel/src/process/process/timer_manager.rs +++ b/kernel/src/process/process/timer_manager.rs @@ -9,11 +9,9 @@ use core::time::Duration; use id_alloc::IdAlloc; use ostd::{ - arch::{ - timer::{self, TIMER_FREQ}, - trap::is_kernel_interrupted, - }, + arch::{timer::TIMER_FREQ, trap::is_kernel_interrupted}, sync::Mutex, + timer, }; use super::Process; diff --git a/kernel/src/time/clocks/system_wide.rs b/kernel/src/time/clocks/system_wide.rs index 0969d53e..12c6ef92 100644 --- a/kernel/src/time/clocks/system_wide.rs +++ b/kernel/src/time/clocks/system_wide.rs @@ -5,11 +5,11 @@ use core::time::Duration; use aster_time::read_monotonic_time; use ostd::{ - arch::timer::Jiffies, cpu::{num_cpus, PinCurrentCpu}, cpu_local, sync::SpinLock, task::disable_preempt, + timer::Jiffies, }; use paste::paste; use spin::Once; diff --git a/kernel/src/time/softirq.rs b/kernel/src/time/softirq.rs index afcae100..52bcbed8 100644 --- a/kernel/src/time/softirq.rs +++ b/kernel/src/time/softirq.rs @@ -2,7 +2,7 @@ use alloc::{boxed::Box, vec::Vec}; -use ostd::{arch::timer, sync::RwLock, trap::SoftIrqLine}; +use ostd::{sync::RwLock, timer, trap::SoftIrqLine}; use crate::softirq_id::TIMER_SOFTIRQ_ID; diff --git a/ostd/src/arch/x86/timer/mod.rs b/ostd/src/arch/x86/timer/mod.rs index 2efc3a73..44177c99 100644 --- a/ostd/src/arch/x86/timer/mod.rs +++ b/ostd/src/arch/x86/timer/mod.rs @@ -4,19 +4,16 @@ mod apic; mod hpet; -mod jiffies; pub(crate) mod pit; -use alloc::{boxed::Box, vec::Vec}; -use core::{cell::RefCell, sync::atomic::Ordering}; +use core::sync::atomic::Ordering; -pub use jiffies::Jiffies; use spin::Once; use self::apic::APIC_TIMER_CALLBACK; use crate::{ arch::x86::kernel, - cpu_local, + timer::INTERRUPT_CALLBACKS, trap::{self, IrqLine, TrapFrame}, }; @@ -51,24 +48,8 @@ pub(super) fn init() { TIMER_IRQ.call_once(|| timer_irq); } -cpu_local! { - static INTERRUPT_CALLBACKS: RefCell>> = RefCell::new(Vec::new()); -} - -/// Registers a function that will be executed during the system timer interruption. -pub fn register_callback(func: F) -where - F: Fn() + Sync + Send + 'static, -{ - let irq_guard = trap::disable_local(); - INTERRUPT_CALLBACKS - .get_with(&irq_guard) - .borrow_mut() - .push(Box::new(func)); -} - fn timer_callback(_: &TrapFrame) { - jiffies::ELAPSED.fetch_add(1, Ordering::SeqCst); + crate::timer::jiffies::ELAPSED.fetch_add(1, Ordering::SeqCst); let irq_guard = trap::disable_local(); let callbacks_guard = INTERRUPT_CALLBACKS.get_with(&irq_guard); diff --git a/ostd/src/lib.rs b/ostd/src/lib.rs index 0a1c0442..eef6f7b7 100644 --- a/ostd/src/lib.rs +++ b/ostd/src/lib.rs @@ -45,6 +45,7 @@ pub mod prelude; pub mod smp; pub mod sync; pub mod task; +pub mod timer; pub mod trap; pub mod user; diff --git a/ostd/src/logger.rs b/ostd/src/logger.rs index e1474126..8a58798b 100644 --- a/ostd/src/logger.rs +++ b/ostd/src/logger.rs @@ -13,8 +13,8 @@ use log::{LevelFilter, Metadata, Record}; use crate::{ - arch::timer::Jiffies, boot::{kcmdline::ModuleArg, kernel_cmdline}, + timer::Jiffies, }; const LOGGER: Logger = Logger {}; diff --git a/ostd/src/task/scheduler/mod.rs b/ostd/src/task/scheduler/mod.rs index 5baa619b..016d4853 100644 --- a/ostd/src/task/scheduler/mod.rs +++ b/ostd/src/task/scheduler/mod.rs @@ -13,7 +13,7 @@ use core::sync::atomic::{AtomicBool, Ordering}; use spin::Once; use super::{preempt::cpu_local, processor, Task}; -use crate::{arch::timer, cpu::PinCurrentCpu, prelude::*, task::disable_preempt}; +use crate::{cpu::PinCurrentCpu, prelude::*, task::disable_preempt, timer}; /// Injects a scheduler implementation into framework. /// diff --git a/ostd/src/arch/x86/timer/jiffies.rs b/ostd/src/timer/jiffies.rs similarity index 91% rename from ostd/src/arch/x86/timer/jiffies.rs rename to ostd/src/timer/jiffies.rs index 1269ceaa..3b1c1dbf 100644 --- a/ostd/src/arch/x86/timer/jiffies.rs +++ b/ostd/src/timer/jiffies.rs @@ -5,7 +5,7 @@ use core::{ time::Duration, }; -use super::TIMER_FREQ; +use crate::arch::timer::TIMER_FREQ; /// Jiffies is a term used to denote the units of time measurement by the kernel. /// @@ -14,7 +14,7 @@ use super::TIMER_FREQ; #[derive(Copy, Clone, Debug)] pub struct Jiffies(u64); -pub(super) static ELAPSED: AtomicU64 = AtomicU64::new(0); +pub(crate) static ELAPSED: AtomicU64 = AtomicU64::new(0); impl Jiffies { /// Creates a new instance. diff --git a/ostd/src/timer/mod.rs b/ostd/src/timer/mod.rs new file mode 100644 index 00000000..92cd0859 --- /dev/null +++ b/ostd/src/timer/mod.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! The timer support. + +pub(crate) mod jiffies; + +use alloc::{boxed::Box, vec::Vec}; +use core::cell::RefCell; + +pub use jiffies::Jiffies; + +use crate::{cpu_local, trap}; + +type InterruptCallback = Box; + +cpu_local! { + pub(crate) static INTERRUPT_CALLBACKS: RefCell> = RefCell::new(Vec::new()); +} + +/// Register a function that will be executed during the system timer interruption. +pub fn register_callback(func: F) +where + F: Fn() + Sync + Send + 'static, +{ + let irq_guard = trap::disable_local(); + INTERRUPT_CALLBACKS + .get_with(&irq_guard) + .borrow_mut() + .push(Box::new(func)); +}