From f8b368de4de04b7390ed0a8f2efae418719a3057 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Sun, 20 Oct 2024 17:20:00 +0200 Subject: [PATCH] Move clocks to clk module --- src/lib.rs | 3 +- src/sys/ata.rs | 4 +- src/sys/clk/boot.rs | 50 +++++++++++++++++++++ src/sys/{clock.rs => clk/epoch.rs} | 70 ++---------------------------- src/sys/clk/mod.rs | 18 ++++++++ src/sys/clk/rtc.rs | 2 +- src/sys/clk/sleep.rs | 4 +- src/sys/fs/device.rs | 3 +- src/sys/fs/dir.rs | 4 +- src/sys/mod.rs | 5 +-- src/sys/net/mod.rs | 2 +- src/sys/net/socket/tcp.rs | 16 +++---- src/sys/net/socket/udp.rs | 12 ++--- src/sys/rng.rs | 4 +- 14 files changed, 101 insertions(+), 96 deletions(-) create mode 100644 src/sys/clk/boot.rs rename src/sys/{clock.rs => clk/epoch.rs} (56%) diff --git a/src/lib.rs b/src/lib.rs index 23638b295..d0123b31d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,8 @@ pub fn init(boot_info: &'static BootInfo) { sys::net::init(); // Require PCI sys::ata::init(); sys::fs::init(); // Require ATA - sys::clock::init(); // Require MEM + + sys::clk::log_rtc(); // Require MEM } #[allow(dead_code)] diff --git a/src/sys/ata.rs b/src/sys/ata.rs index 1ddf69caf..6fe63835c 100644 --- a/src/sys/ata.rs +++ b/src/sys/ata.rs @@ -132,9 +132,9 @@ impl Bus { } fn poll(&mut self, bit: Status, val: bool) -> Result<(), ()> { - let start = sys::clock::uptime(); + let start = sys::clk::uptime(); while self.status().get_bit(bit as usize) != val { - if sys::clock::uptime() - start > 1.0 { + if sys::clk::uptime() - start > 1.0 { debug!( "ATA hanged while polling {:?} bit in status register", bit diff --git a/src/sys/clk/boot.rs b/src/sys/clk/boot.rs new file mode 100644 index 000000000..9119b98fe --- /dev/null +++ b/src/sys/clk/boot.rs @@ -0,0 +1,50 @@ +use crate::api::fs::{FileIO, IO}; + +#[derive(Debug, Clone)] +pub struct Uptime; + +impl Uptime { + pub fn new() -> Self { + Self {} + } + + pub fn size() -> usize { + core::mem::size_of::() + } +} + +impl FileIO for Uptime { + fn read(&mut self, buf: &mut [u8]) -> Result { + let time = uptime().to_be_bytes(); + let n = time.len(); + if buf.len() >= n { + buf[0..n].clone_from_slice(&time); + Ok(n) + } else { + Err(()) + } + } + + fn write(&mut self, _buf: &[u8]) -> Result { + unimplemented!(); + } + + fn close(&mut self) {} + + fn poll(&mut self, event: IO) -> bool { + match event { + IO::Read => true, + IO::Write => false, + } + } +} + +// NOTE: This clock is monotonic +pub fn uptime() -> f64 { + super::time_between_ticks() * super::ticks() as f64 +} + +#[test_case] +fn test_uptime() { + assert!(uptime() > 0.0); +} diff --git a/src/sys/clock.rs b/src/sys/clk/epoch.rs similarity index 56% rename from src/sys/clock.rs rename to src/sys/clk/epoch.rs index 988b5a1b8..cd2823b75 100644 --- a/src/sys/clock.rs +++ b/src/sys/clk/epoch.rs @@ -1,58 +1,11 @@ -use crate::api::clock::DATE_TIME_ZONE; -use crate::api::fs::{FileIO, IO}; -use crate::sys; -use crate::sys::clk::CMOS; +use super::CMOS; -use time::{Duration, OffsetDateTime}; +use crate::api::fs::{FileIO, IO}; const DAYS_BEFORE_MONTH: [u64; 13] = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ]; -#[derive(Debug, Clone)] -pub struct Uptime; - -impl Uptime { - pub fn new() -> Self { - Self {} - } - - pub fn size() -> usize { - core::mem::size_of::() - } -} - -impl FileIO for Uptime { - fn read(&mut self, buf: &mut [u8]) -> Result { - let time = uptime().to_be_bytes(); - let n = time.len(); - if buf.len() >= n { - buf[0..n].clone_from_slice(&time); - Ok(n) - } else { - Err(()) - } - } - - fn write(&mut self, _buf: &[u8]) -> Result { - unimplemented!(); - } - - fn close(&mut self) {} - - fn poll(&mut self, event: IO) -> bool { - match event { - IO::Read => true, - IO::Write => false, - } - } -} - -// NOTE: This clock is monotonic -pub fn uptime() -> f64 { - sys::clk::time_between_ticks() * sys::clk::ticks() as f64 -} - #[derive(Debug, Clone)] pub struct Realtime; @@ -103,8 +56,8 @@ pub fn realtime() -> f64 { + 60 * rtc.minute as u64 + rtc.second as u64; - let fract = sys::clk::time_between_ticks() - * (sys::clk::ticks() - sys::clk::last_rtc_update()) as f64; + let fract = super::time_between_ticks() + * (super::ticks() - super::last_rtc_update()) as f64; (ts as f64) + fract } @@ -132,21 +85,6 @@ fn is_leap_year(year: u64) -> bool { } } -pub fn init() { - let s = realtime(); - let ns = Duration::nanoseconds( - libm::floor(1e9 * (s - libm::floor(s))) as i64 - ); - let dt = OffsetDateTime::from_unix_timestamp(s as i64) + ns; - let rtc = dt.format(DATE_TIME_ZONE); - log!("RTC {}", rtc); -} - -#[test_case] -fn test_uptime() { - assert!(uptime() > 0.0); -} - #[test_case] fn test_realtime() { assert!(realtime() > 1234567890.0); diff --git a/src/sys/clk/mod.rs b/src/sys/clk/mod.rs index ac8af6c4b..d2ed8a2b4 100644 --- a/src/sys/clk/mod.rs +++ b/src/sys/clk/mod.rs @@ -1,8 +1,12 @@ mod cmos; +mod boot; +mod epoch; mod rtc; mod sleep; mod timer; +pub use boot::{uptime, Uptime}; // TODO: Rename to boot_time +pub use epoch::{realtime, Realtime}; // TODO: Rename to epoch_time pub use cmos::CMOS; pub use rtc::RTC; pub use sleep::{sleep, nanowait, halt}; @@ -14,6 +18,20 @@ pub use timer::{ use rtc::{Interrupt, Register, RTC_CENTURY}; use timer::{rdtsc, CLOCKS_PER_NANOSECOND}; +use crate::api::clock::DATE_TIME_ZONE; + +use time::{Duration, OffsetDateTime}; + pub fn init() { timer::init(); } + +pub fn log_rtc() { + let s = realtime(); + let ns = Duration::nanoseconds( + libm::floor(1e9 * (s - libm::floor(s))) as i64 + ); + let dt = OffsetDateTime::from_unix_timestamp(s as i64) + ns; + let rtc = dt.format(DATE_TIME_ZONE); + log!("RTC {}", rtc); +} diff --git a/src/sys/clk/rtc.rs b/src/sys/clk/rtc.rs index a8943f60d..eb30dbe66 100644 --- a/src/sys/clk/rtc.rs +++ b/src/sys/clk/rtc.rs @@ -84,7 +84,7 @@ impl FileIO for RTC { return Err(()); } CMOS::new().update_rtc(self); - sys::clock::init(); + sys::clk::log_rtc(); Ok(buf.len()) } diff --git a/src/sys/clk/sleep.rs b/src/sys/clk/sleep.rs index 4ac2053c5..0ea69772e 100644 --- a/src/sys/clk/sleep.rs +++ b/src/sys/clk/sleep.rs @@ -12,8 +12,8 @@ pub fn halt() { } pub fn sleep(seconds: f64) { - let start = sys::clock::uptime(); - while sys::clock::uptime() - start < seconds { + let start = sys::clk::uptime(); + while sys::clk::uptime() - start < seconds { halt(); } } diff --git a/src/sys/fs/device.rs b/src/sys/fs/device.rs index ae3976622..ba9111ecb 100644 --- a/src/sys/fs/device.rs +++ b/src/sys/fs/device.rs @@ -4,8 +4,7 @@ use super::file::File; use super::{dirname, filename, realpath, FileIO, IO}; use crate::sys::ata::Drive; -use crate::sys::clock::{Realtime, Uptime}; -use crate::sys::clk::RTC; +use crate::sys::clk::{RTC, Realtime, Uptime}; use crate::sys::console::Console; use crate::sys::net::socket::tcp::TcpSocket; use crate::sys::net::socket::udp::UdpSocket; diff --git a/src/sys/fs/dir.rs b/src/sys/fs/dir.rs index c5e0fe926..a556c9e14 100644 --- a/src/sys/fs/dir.rs +++ b/src/sys/fs/dir.rs @@ -135,7 +135,7 @@ impl Dir { let entry_kind = kind as u8; let entry_addr = entry_block.addr(); let entry_size = 0u32; - let entry_time = sys::clock::realtime() as u64; + let entry_time = sys::clk::realtime() as u64; let entry_name = truncate(name, u8::MAX as usize); let n = entry_name.len(); let i = entries.block_offset(); @@ -193,7 +193,7 @@ impl Dir { } pub fn update_entry(&self, name: &str, size: u32) { - let time = sys::clock::realtime() as u64; + let time = sys::clk::realtime() as u64; let mut entries = self.entries(); for entry in &mut entries { if entry.name() == name { diff --git a/src/sys/mod.rs b/src/sys/mod.rs index da62b9929..48ea590ba 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -20,7 +20,7 @@ macro_rules! debug { macro_rules! log { ($($arg:tt)*) => ({ if !cfg!(test) { - let uptime = $crate::sys::clock::uptime(); + let uptime = $crate::sys::clk::uptime(); let csi_color = $crate::api::console::Style::color("lime"); let csi_reset = $crate::api::console::Style::reset(); $crate::sys::console::print_fmt(format_args!( @@ -28,7 +28,7 @@ macro_rules! log { csi_color, uptime, csi_reset, format_args!($($arg)*) )); - let realtime = $crate::sys::clock::realtime(); + let realtime = $crate::sys::clk::realtime(); $crate::sys::log::write_fmt(format_args!( "[{:.6}] {}\n", realtime, format_args!($($arg)*) @@ -39,7 +39,6 @@ macro_rules! log { pub mod acpi; pub mod ata; -pub mod clock; pub mod clk; pub mod console; pub mod cpu; diff --git a/src/sys/net/mod.rs b/src/sys/net/mod.rs index 481c71512..17c8d2b6a 100644 --- a/src/sys/net/mod.rs +++ b/src/sys/net/mod.rs @@ -28,7 +28,7 @@ pub enum SocketStatus { } fn time() -> Instant { - Instant::from_micros((sys::clock::realtime() * 1000000.0) as i64) + Instant::from_micros((sys::clk::realtime() * 1000000.0) as i64) } #[derive(Clone)] diff --git a/src/sys/net/socket/tcp.rs b/src/sys/net/socket/tcp.rs index e91909f6c..d4019d115 100644 --- a/src/sys/net/socket/tcp.rs +++ b/src/sys/net/socket/tcp.rs @@ -56,10 +56,10 @@ impl TcpSocket { pub fn connect(&mut self, addr: IpAddress, port: u16) -> Result<(), ()> { let mut connecting = false; let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } let mut sockets = SOCKETS.lock(); @@ -119,10 +119,10 @@ impl TcpSocket { pub fn accept(&mut self) -> Result { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } let mut sockets = SOCKETS.lock(); @@ -147,12 +147,12 @@ impl TcpSocket { impl FileIO for TcpSocket { fn read(&mut self, buf: &mut [u8]) -> Result { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); let mut bytes = 0; if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { let mut sockets = SOCKETS.lock(); loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } iface.poll(sys::net::time(), device, &mut sockets); @@ -184,12 +184,12 @@ impl FileIO for TcpSocket { fn write(&mut self, buf: &[u8]) -> Result { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); let mut sent = false; if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { let mut sockets = SOCKETS.lock(); loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } iface.poll(sys::net::time(), device, &mut sockets); diff --git a/src/sys/net/socket/udp.rs b/src/sys/net/socket/udp.rs index 8214743a5..41da3780d 100644 --- a/src/sys/net/socket/udp.rs +++ b/src/sys/net/socket/udp.rs @@ -60,10 +60,10 @@ impl UdpSocket { pub fn connect(&mut self, addr: IpAddress, port: u16) -> Result<(), ()> { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } let mut sockets = SOCKETS.lock(); @@ -98,12 +98,12 @@ impl UdpSocket { impl FileIO for UdpSocket { fn read(&mut self, buf: &mut [u8]) -> Result { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { let bytes; let mut sockets = SOCKETS.lock(); loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } iface.poll(sys::net::time(), device, &mut sockets); @@ -132,12 +132,12 @@ impl FileIO for UdpSocket { fn write(&mut self, buf: &[u8]) -> Result { let timeout = 5.0; - let started = sys::clock::realtime(); + let started = sys::clk::realtime(); let mut sent = false; if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() { let mut sockets = SOCKETS.lock(); loop { - if sys::clock::realtime() - started > timeout { + if sys::clk::realtime() - started > timeout { return Err(()); } iface.poll(sys::net::time(), device, &mut sockets); diff --git a/src/sys/rng.rs b/src/sys/rng.rs index 2352d1213..25c88b889 100644 --- a/src/sys/rng.rs +++ b/src/sys/rng.rs @@ -82,8 +82,8 @@ pub fn init() { log!("RNG RDRAND unavailable"); let mut hasher = Sha256::new(); hasher.update(sys::clk::ticks().to_be_bytes()); - hasher.update(sys::clock::realtime().to_be_bytes()); - hasher.update(sys::clock::uptime().to_be_bytes()); + hasher.update(sys::clk::realtime().to_be_bytes()); + hasher.update(sys::clk::uptime().to_be_bytes()); seed = hasher.finalize().into(); }