Skip to content

Commit

Permalink
Move clocks to clk module
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 20, 2024
1 parent 3e2a337 commit f8b368d
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 96 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/sys/ata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/sys/clk/boot.rs
Original file line number Diff line number Diff line change
@@ -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::<f64>()
}
}

impl FileIO for Uptime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
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<usize, ()> {
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);
}
70 changes: 4 additions & 66 deletions src/sys/clock.rs → src/sys/clk/epoch.rs
Original file line number Diff line number Diff line change
@@ -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::<f64>()
}
}

impl FileIO for Uptime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
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<usize, ()> {
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;

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/sys/clk/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion src/sys/clk/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 2 additions & 2 deletions src/sys/clk/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/sys/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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!(
"{}[{:.6}]{} {}\n",
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)*)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 8 additions & 8 deletions src/sys/net/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -119,10 +119,10 @@ impl TcpSocket {

pub fn accept(&mut self) -> Result<IpAddress, ()> {
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();
Expand All @@ -147,12 +147,12 @@ impl TcpSocket {
impl FileIO for TcpSocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
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);
Expand Down Expand Up @@ -184,12 +184,12 @@ impl FileIO for TcpSocket {

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
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);
Expand Down
12 changes: 6 additions & 6 deletions src/sys/net/socket/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -98,12 +98,12 @@ impl UdpSocket {
impl FileIO for UdpSocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
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);
Expand Down Expand Up @@ -132,12 +132,12 @@ impl FileIO for UdpSocket {

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
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);
Expand Down
4 changes: 2 additions & 2 deletions src/sys/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit f8b368d

Please sign in to comment.