Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rt_sigprocmask syscall implementation #112

Merged
merged 8 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions kernel/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use crate::{
switch, UserVAddr, JOIN_WAIT_QUEUE, SCHEDULER,
},
random::read_secure_random,
result::Errno,
INITIAL_ROOT_FS,
};

use alloc::collections::BTreeMap;
use alloc::sync::{Arc, Weak};
use alloc::vec::Vec;
Expand All @@ -36,7 +36,9 @@ use kerla_runtime::{
page_allocator::{alloc_pages, AllocPageFlags},
spinlock::{SpinLock, SpinLockGuard},
};
use kerla_utils::alignment::align_up;
use kerla_utils::{alignment::align_up, bitmap::BitMap};

use super::signal::SigSet;

type ProcessTable = BTreeMap<PId, Arc<Process>>;

Expand Down Expand Up @@ -104,6 +106,7 @@ pub struct Process {
root_fs: Arc<SpinLock<RootFs>>,
signals: SpinLock<SignalDelivery>,
signaled_frame: AtomicCell<Option<PtRegs>>,
sigset: SpinLock<SigSet>,
}

impl Process {
Expand All @@ -126,6 +129,7 @@ impl Process {
opened_files: SpinLock::new(OpenedFileTable::new()),
signals: SpinLock::new(SignalDelivery::new()),
signaled_frame: AtomicCell::new(None),
sigset: SpinLock::new(BitMap::zeroed()),
});

process_group.lock().add(Arc::downgrade(&proc));
Expand Down Expand Up @@ -187,6 +191,7 @@ impl Process {
root_fs,
signals: SpinLock::new(SignalDelivery::new()),
signaled_frame: AtomicCell::new(None),
sigset: SpinLock::new(BitMap::zeroed()),
});

process_group.lock().add(Arc::downgrade(&process));
Expand Down Expand Up @@ -351,6 +356,38 @@ impl Process {
self.signals.lock().is_pending()
}

/// Sets signal mask
pub fn rt_sigprocmask(
nuta marked this conversation as resolved.
Show resolved Hide resolved
&self,
how: usize,
set: Option<UserVAddr>,
oldset: Option<UserVAddr>,
_length: usize,
) -> Result<isize> {
nuta marked this conversation as resolved.
Show resolved Hide resolved
let mut sigset = self.sigset.lock();

if let Some(old) = oldset {
if let Err(_) = old.write_bytes(sigset.as_slice()) {
return Err(Error::new(Errno::EFAULT));
}
}

if let Some(new) = set {
if let Ok(new_set) = new.read::<[u8; 128]>() {
nuta marked this conversation as resolved.
Show resolved Hide resolved
match how {
0 => sigset.assign_or(new_set),
1 => sigset.assign_material_nonimplication(new_set),
2 => sigset.assign(new_set),
_ => return Err(Error::new(Errno::EINVAL)),
}
} else {
return Err(Error::new(Errno::EFAULT));
}
}

Ok(0)
nuta marked this conversation as resolved.
Show resolved Hide resolved
}

/// Tries to delivering a pending signal to the current process.
///
/// If there's a pending signal, it may modify `frame` (e.g. user return
Expand Down Expand Up @@ -433,6 +470,7 @@ impl Process {
let vm = parent.vm().as_ref().unwrap().lock().fork()?;
let opened_files = parent.opened_files().lock().fork();
let process_group = parent.process_group();
let sig_set = parent.sigset.lock();

let child = Arc::new(Process {
process_group: AtomicRefCell::new(Arc::downgrade(&process_group)),
Expand All @@ -447,6 +485,7 @@ impl Process {
arch,
signals: SpinLock::new(SignalDelivery::new()),
signaled_frame: AtomicCell::new(None),
sigset: SpinLock::new(sig_set.clone()),
});

process_group.lock().add(Arc::downgrade(&child));
Expand Down
3 changes: 3 additions & 0 deletions kernel/process/signal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{ctypes::c_int, prelude::*};
use kerla_runtime::address::UserVAddr;
use kerla_utils::bitmap::BitMap;

pub type Signal = c_int;
#[allow(unused)]
Expand Down Expand Up @@ -153,3 +154,5 @@ impl SignalDelivery {
self.pending |= 1 << (signal);
}
}

pub type SigSet = BitMap<128 /* 1024 / 8 */>;
5 changes: 5 additions & 0 deletions kernel/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(self) mod readlink;
pub(self) mod reboot;
pub(self) mod recvfrom;
pub(self) mod rt_sigaction;
pub(self) mod rt_sigprocmask;
pub(self) mod rt_sigreturn;
pub(self) mod select;
pub(self) mod sendto;
Expand Down Expand Up @@ -112,6 +113,7 @@ const SYS_POLL: usize = 7;
const SYS_MMAP: usize = 9;
const SYS_BRK: usize = 12;
const SYS_RT_SIGACTION: usize = 13;
const SYS_RT_SIGPROCMASK: usize = 14;
const SYS_RT_SIGRETURN: usize = 15;
const SYS_IOCTL: usize = 16;
const SYS_WRITEV: usize = 20;
Expand Down Expand Up @@ -368,6 +370,9 @@ impl<'a> SyscallHandler<'a> {
SYS_SYSLOG => self.sys_syslog(a1 as c_int, UserVAddr::new(a2), a3 as c_int),
SYS_REBOOT => self.sys_reboot(a1 as c_int, a2 as c_int, a3),
SYS_GETTID => self.sys_gettid(),
SYS_RT_SIGPROCMASK => {
self.sys_rt_sigprocmask(a1, UserVAddr::new(a2), UserVAddr::new(a3), a4)
}
_ => {
debug_warn!(
"unimplemented system call: {} (n={})",
Expand Down
17 changes: 17 additions & 0 deletions kernel/syscalls/rt_sigprocmask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::prelude::*;
use crate::process::current_process;

use crate::syscalls::SyscallHandler;
use kerla_runtime::address::UserVAddr;

impl SyscallHandler<'_> {
pub fn sys_rt_sigprocmask(
&mut self,
how: usize,
set: Option<UserVAddr>,
oldset: Option<UserVAddr>,
length: usize,
) -> Result<isize> {
current_process().rt_sigprocmask(how, set, oldset, length)
}
}
39 changes: 38 additions & 1 deletion libs/kerla_utils/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ impl<const SZ: usize> BitMap<SZ> {
BitMap(array)
}

#[cfg(test)]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
Expand Down Expand Up @@ -51,6 +50,30 @@ impl<const SZ: usize> BitMap<SZ> {

None
}

pub fn assign(&mut self, rhs: [u8; SZ]) {
self.0 = rhs;
}

pub fn assign_or(&mut self, rhs: [u8; SZ]) {
let mut iter = rhs.into_iter();
for byte in &mut self.0 {
*byte |= iter.next().unwrap_or(0);
}
nuta marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn assign_material_nonimplication(&mut self, rhs: [u8; SZ]) {
nuta marked this conversation as resolved.
Show resolved Hide resolved
let mut iter = rhs.into_iter();
for byte in &mut self.0 {
*byte &= !iter.next().unwrap_or(0);
}
nuta marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<const SZ: usize> Clone for BitMap<SZ> {
fn clone(&self) -> Self {
BitMap(self.0)
nuta marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[cfg(all(test, not(feature = "no_std")))]
Expand All @@ -71,4 +94,18 @@ mod tests {
assert_eq!(bitmap.as_slice(), &[0b1111_1111, 0b1111_1001]);
assert_eq!(bitmap.first_zero(), Some(9));
}

#[test]
fn or() {
let mut bitmap = BitMap::from_array([0b0100_0010, 0b1000_0001]);
bitmap.assign_or([0b0010_0100, 0b1010_0110]);
assert_eq!(bitmap.as_slice(), &[0b0110_0110, 0b1010_0111]);
}

#[test]
fn material_nonimplication() {
Lurk marked this conversation as resolved.
Show resolved Hide resolved
let mut bitmap = BitMap::from_array([0b0100_0010, 0b1000_0001]);
bitmap.assign_material_nonimplication([0b0110_0100, 0b1010_0110]);
assert_eq!(bitmap.as_slice(), &[0b0000_0010, 0b0000_0001]);
}
}