diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index ab902d0..ad2d8f4 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -108,6 +108,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { 186 => sys_gettid(), 201 => sys_time(args[0] as *mut u64), 204 => sys_sched_getaffinity(args[0], args[1], args[2] as *mut u32), + 228 => sys_clock_gettime(args[0], args[1] as *mut TimeSpec), 217 => sys_getdents64(args[0], args[1] as *mut LinuxDirent64, args[2]), // 293 => sys_pipe(), @@ -168,10 +169,6 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { warn!("sys_set_tid_address is unimplemented"); Ok(thread::current().id() as isize) } - 228 => { - warn!("sys_clock_gettime is unimplemented"); - Ok(0) - } 231 => { warn!("sys_exit_group is unimplemented"); sys_exit(args[0] as isize); diff --git a/kernel/src/syscall/time.rs b/kernel/src/syscall/time.rs index 35c2ea5..d379f78 100644 --- a/kernel/src/syscall/time.rs +++ b/kernel/src/syscall/time.rs @@ -27,7 +27,25 @@ impl TimeVal { } } +#[repr(C)] +#[derive(Copy, Clone)] +pub struct TimeSpec { + sec: u64, + nsec: u64, +} + +impl TimeSpec { + pub fn to_msec(&self) -> u64 { + self.sec * 1000 + self.nsec / 1000_000 + } + + pub fn to_usec(&self) -> u64 { + self.sec * 1000_000 + self.nsec / 1000 + } +} + pub fn sys_gettimeofday(tv: *mut TimeVal, tz: *const u8) -> SysResult { + info!("gettimeofday: tv: {:?}, tz: {:?}", tv, tz); if tz as usize != 0 { return Err(SysError::EINVAL); } @@ -51,6 +69,28 @@ pub fn sys_gettimeofday(tv: *mut TimeVal, tz: *const u8) -> SysResult { Ok(0) } +pub fn sys_clock_gettime(clock: usize, ts: *mut TimeSpec) -> SysResult { + info!("clock_gettime: clock: {:?}, ts: {:?}", clock, ts); + + let mut proc = process(); + proc.memory_set.check_mut_ptr(ts)?; + + 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 timespec = TimeSpec { + sec, + nsec: (usec % 1_000_000) * 1000, + }; + unsafe { + *ts = timespec; + } + Ok(0) +} + pub fn sys_time(time: *mut u64) -> SysResult { let tick_base = *TICK_BASE; let epoch_base = *EPOCH_BASE;