diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index f704adc..e2ee608 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -58,7 +58,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { 024 => sys_yield(), 033 => sys_dup2(args[0], args[1]), // 034 => sys_pause(), - 035 => sys_sleep(args[0]), // TODO: nanosleep + 035 => sys_nanosleep(args[0] as *const TimeSpec), 039 => sys_getpid(), 041 => sys_socket(args[0], args[1], args[2]), 042 => sys_connect(args[0], args[1] as *const SockaddrIn, args[2]), diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 447a81f..1838b28 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -199,14 +199,11 @@ pub fn sys_exit(exit_code: isize) -> ! { unreachable!(); } -pub fn sys_sleep(time: usize) -> SysResult { - info!("sleep: time: {}", time); - if time >= 1 << 31 { - thread::park(); - } else { - use core::time::Duration; - thread::sleep(Duration::from_millis(time as u64 * 10)); - } +pub fn sys_nanosleep(req: *const TimeSpec) -> SysResult { + process().memory_set.check_ptr(req)?; + let time = unsafe { req.read() }; + info!("nanosleep: time: {:#?}", time); + thread::sleep(time.to_duration()); Ok(0) } diff --git a/kernel/src/syscall/time.rs b/kernel/src/syscall/time.rs index 6eef594..95789ca 100644 --- a/kernel/src/syscall/time.rs +++ b/kernel/src/syscall/time.rs @@ -4,6 +4,7 @@ use super::*; use crate::arch::consts::USEC_PER_TICK; use crate::arch::driver::rtc_cmos; use lazy_static::lazy_static; +use core::time::Duration; lazy_static! { pub static ref EPOCH_BASE: u64 = unsafe { rtc_cmos::read_epoch() }; @@ -11,7 +12,7 @@ lazy_static! { } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct TimeVal { sec: u64, usec: u64, @@ -28,19 +29,15 @@ impl TimeVal { } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, 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 to_duration(&self) -> Duration { + Duration::new(self.sec, self.nsec as u32) } }