From 1bba33a05fe8e8a9910a3ba57c72b69a5b04e34b Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Wed, 17 Apr 2019 01:16:27 +0800 Subject: [PATCH] Implement sys_ppoll over sys_poll --- kernel/src/syscall/fs.rs | 22 +++++++++++++++++----- kernel/src/syscall/mod.rs | 14 +++++++------- kernel/src/syscall/time.rs | 5 +++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 1ef806e4..7381d4b8 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -66,6 +66,21 @@ pub fn sys_pwrite(fd: usize, base: *const u8, len: usize, offset: usize) -> SysR Ok(len) } +pub fn sys_ppoll(ufds: *mut PollFd, nfds: usize, timeout: *const TimeSpec) -> SysResult { + let proc = process(); + let timeout_msecs = if timeout.is_null() { + 1 << 31 // infinity + } else { + proc.vm.check_read_ptr(timeout)?; + unsafe { + (*timeout).to_msec() + } + }; + drop(proc); + + sys_poll(ufds, nfds, timeout_msecs as usize) +} + pub fn sys_poll(ufds: *mut PollFd, nfds: usize, timeout_msecs: usize) -> SysResult { info!( "poll: ufds: {:?}, nfds: {}, timeout_msecs: {:#x}", @@ -472,12 +487,9 @@ pub fn sys_chdir(path: *const u8) -> SysResult { if path.len() > 0 { let cwd = match path.as_bytes()[0] { b'/' => String::from("/"), - _ => proc.cwd.clone() + _ => proc.cwd.clone(), }; - let mut cwd_vec:Vec<_> = - cwd.split("/") - .filter(|&x| x != "") - .collect(); + let mut cwd_vec: Vec<_> = cwd.split("/").filter(|&x| x != "").collect(); let path_split = path.split("/").filter(|&x| x != ""); for seg in path_split { if seg == ".." { diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 8deeaa22..f865a437 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -275,6 +275,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { ), SYS_SYMLINKAT => Err(SysError::EACCES), SYS_FACCESSAT => sys_faccessat(args[0], args[1] as *const u8, args[2], args[3]), + SYS_PPOLL => sys_ppoll(args[0] as *mut PollFd, args[1], args[2] as *const TimeSpec), // ignore sigmask // 280 SYS_UTIMENSAT => { warn!("sys_utimensat is unimplemented"); @@ -340,7 +341,10 @@ fn x86_64_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_dup2(args[0], args[1]), - // SYS_PAUSE => sys_pause(), + SYS_ALARM => { + warn!("sys_alarm is unimplemented"); + Ok(0) + } SYS_FORK => sys_fork(tf), // use fork for vfork SYS_VFORK => sys_fork(tf), @@ -355,16 +359,12 @@ fn x86_64_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_arch_prctl(args[0] as i32, args[1], tf), - SYS_TIME => sys_time(args[0] as *mut u64), - SYS_ALARM => { - warn!("sys_alarm is unimplemented"); - Ok(0) - } SYS_CHOWN => { warn!("sys_chown is unimplemented"); Ok(0) } + SYS_ARCH_PRCTL => sys_arch_prctl(args[0] as i32, args[1], tf), + SYS_TIME => sys_time(args[0] as *mut u64), SYS_EPOLL_CREATE => { warn!("sys_epoll_create is unimplemented"); Err(SysError::ENOSYS) diff --git a/kernel/src/syscall/time.rs b/kernel/src/syscall/time.rs index 2ae76d46..dd5cfcd3 100644 --- a/kernel/src/syscall/time.rs +++ b/kernel/src/syscall/time.rs @@ -19,6 +19,7 @@ const USEC_PER_SEC: u64 = 1_000_000; const MSEC_PER_SEC: u64 = 1_000; const USEC_PER_MSEC: u64 = 1_000; const NSEC_PER_USEC: u64 = 1_000; +const NSEC_PER_MSEC: u64 = 1_000_000; /// Get time since epoch in usec fn get_epoch_usec() -> u64 { @@ -58,6 +59,10 @@ pub struct TimeSpec { } impl TimeSpec { + pub fn to_msec(&self) -> u64 { + self.sec * MSEC_PER_SEC + self.nsec / NSEC_PER_MSEC + } + pub fn to_duration(&self) -> Duration { Duration::new(self.sec, self.nsec as u32) }