asterinas/kernel/comps/time/src/lib.rs

62 lines
1.4 KiB
Rust

// SPDX-License-Identifier: MPL-2.0
//! The system time of Asterinas.
#![no_std]
#![deny(unsafe_code)]
extern crate alloc;
use alloc::sync::Arc;
use core::time::Duration;
pub use clocksource::{ClockSource, Instant};
use component::{ComponentInitError, init_component};
use rtc::Driver;
use spin::Once;
mod clocksource;
mod rtc;
mod tsc;
pub static VDSO_DATA_HIGH_RES_UPDATE_FN: Once<fn(Instant, u64)> = Once::new();
static RTC_DRIVER: Once<Arc<dyn Driver + Send + Sync>> = Once::new();
#[init_component]
fn time_init() -> Result<(), ComponentInitError> {
let rtc = rtc::init_rtc_driver();
RTC_DRIVER.call_once(|| rtc);
tsc::init();
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SystemTime {
pub year: u16,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub nanos: u64,
}
static START_TIME: Once<SystemTime> = Once::new();
/// Returns the `START_TIME`, which is the system time when calibrating.
pub fn read_start_time() -> SystemTime {
*START_TIME.get().unwrap()
}
/// Returns the monotonic time from the TSC clocksource.
pub fn read_monotonic_time() -> Duration {
let instant = tsc::read_instant();
Duration::new(instant.secs(), instant.nanos())
}
/// Returns the default (TSC) clocksource.
pub fn default_clocksource() -> Arc<ClockSource> {
tsc::CLOCK.get().unwrap().clone()
}