2019-02-22 10:10:24 +04:00
|
|
|
//! Syscalls for time
|
|
|
|
|
|
|
|
use super::*;
|
2019-03-04 19:52:19 +04:00
|
|
|
use crate::arch::consts::USEC_PER_TICK;
|
2019-03-04 12:25:57 +04:00
|
|
|
use crate::arch::driver::rtc_cmos;
|
2019-03-04 19:52:19 +04:00
|
|
|
use lazy_static::lazy_static;
|
2019-02-22 10:10:24 +04:00
|
|
|
|
2019-03-04 19:52:19 +04:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref EPOCH_BASE: u64 = unsafe { rtc_cmos::read_epoch() };
|
|
|
|
pub static ref TICK_BASE: u64 = unsafe { crate::trap::TICK as u64 };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct TimeVal {
|
|
|
|
sec: u64,
|
|
|
|
usec: u64,
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:47:36 +04:00
|
|
|
impl TimeVal {
|
|
|
|
pub fn to_msec(&self) -> u64 {
|
|
|
|
self.sec * 1000 + self.usec / 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_usec(&self) -> u64 {
|
|
|
|
self.sec * 1000_000 + self.usec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:52:19 +04:00
|
|
|
pub fn sys_gettimeofday(tv: *mut TimeVal, tz: *const u8) -> SysResult {
|
2019-03-04 12:25:57 +04:00
|
|
|
if tz as usize != 0 {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut proc = process();
|
|
|
|
proc.memory_set.check_mut_ptr(tv)?;
|
|
|
|
|
2019-03-04 19:52:19 +04:00
|
|
|
let tick_base = *TICK_BASE;
|
|
|
|
let epoch_base = *EPOCH_BASE;
|
|
|
|
let tick = unsafe { crate::trap::TICK as u64 };
|
|
|
|
|
|
|
|
let usec = (tick - tick_base) * USEC_PER_TICK as u64;
|
|
|
|
let sec = epoch_base + usec / 1_000_000;
|
|
|
|
let timeval = TimeVal {
|
|
|
|
sec,
|
|
|
|
usec: usec % 1_000_000,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
*tv = timeval;
|
|
|
|
}
|
2019-03-04 12:25:57 +04:00
|
|
|
Ok(0)
|
2019-03-02 16:15:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sys_time(time: *mut u64) -> SysResult {
|
2019-03-04 19:52:19 +04:00
|
|
|
let tick_base = *TICK_BASE;
|
|
|
|
let epoch_base = *EPOCH_BASE;
|
|
|
|
let tick = unsafe { crate::trap::TICK as u64 };
|
|
|
|
|
|
|
|
let usec = (tick - tick_base) * USEC_PER_TICK as u64;
|
|
|
|
let sec = epoch_base + usec / 1_000_000;
|
2019-03-02 16:15:55 +04:00
|
|
|
if time as usize != 0 {
|
|
|
|
let mut proc = process();
|
2019-03-02 21:27:30 +04:00
|
|
|
proc.memory_set.check_mut_ptr(time)?;
|
2019-03-02 16:15:55 +04:00
|
|
|
unsafe {
|
2019-03-04 19:52:19 +04:00
|
|
|
time.write(sec as u64);
|
2019-03-02 16:15:55 +04:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 19:52:19 +04:00
|
|
|
Ok(sec as isize)
|
2019-03-02 16:15:55 +04:00
|
|
|
}
|