1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 16:36:18 +04:00
rCore/kernel/src/syscall/proc.rs

313 lines
9.0 KiB
Rust
Raw Normal View History

2019-02-22 10:10:24 +04:00
//! Syscalls for process
use super::*;
use crate::fs::INodeExt;
2019-02-22 10:10:24 +04:00
/// Fork the current process. Return the child's PID.
pub fn sys_fork(tf: &TrapFrame) -> SysResult {
2019-03-08 18:37:05 +04:00
let new_thread = current_thread().fork(tf);
2019-03-10 11:23:15 +04:00
let pid = processor().manager().add(new_thread);
2019-02-22 10:10:24 +04:00
info!("fork: {} -> {}", thread::current().id(), pid);
2019-03-08 15:04:39 +04:00
Ok(pid)
2019-02-22 10:10:24 +04:00
}
2019-03-08 18:37:05 +04:00
/// Create a new thread in the current process.
/// The new thread's stack pointer will be set to `newsp`,
/// and thread pointer will be set to `newtls`.
2019-03-08 18:37:05 +04:00
/// The child tid will be stored at both `parent_tid` and `child_tid`.
/// This is partially implemented for musl only.
2019-03-27 14:35:08 +04:00
pub fn sys_clone(
flags: usize,
newsp: usize,
parent_tid: *mut u32,
child_tid: *mut u32,
newtls: usize,
tf: &TrapFrame,
) -> SysResult {
info!(
"clone: flags: {:#x}, newsp: {:#x}, parent_tid: {:?}, child_tid: {:?}, newtls: {:#x}",
flags, newsp, parent_tid, child_tid, newtls
);
if flags == 0x4111 {
warn!("sys_clone is calling sys_fork instead, ignoring other args");
return sys_fork(tf);
}
2019-03-08 18:37:05 +04:00
if flags != 0x7d0f00 {
warn!("sys_clone only support musl pthread_create");
return Err(SysError::ENOSYS);
}
{
let proc = process();
2019-03-22 19:45:57 +04:00
proc.vm.check_write_ptr(parent_tid)?;
proc.vm.check_write_ptr(child_tid)?;
2019-03-08 18:37:05 +04:00
}
let new_thread = current_thread().clone(tf, newsp, newtls, child_tid as usize);
2019-03-08 18:37:05 +04:00
// FIXME: parent pid
2019-03-10 11:23:15 +04:00
let tid = processor().manager().add(new_thread);
2019-03-08 18:37:05 +04:00
info!("clone: {} -> {}", thread::current().id(), tid);
unsafe {
parent_tid.write(tid as u32);
child_tid.write(tid as u32);
2019-03-08 18:37:05 +04:00
}
Ok(tid)
}
2019-03-10 21:06:44 +04:00
/// Wait for the process exit.
/// Return the PID. Store exit code to `wstatus` if it's not null.
2019-03-08 15:04:39 +04:00
pub fn sys_wait4(pid: isize, wstatus: *mut i32) -> SysResult {
info!("wait4: pid: {}, code: {:?}", pid, wstatus);
if !wstatus.is_null() {
2019-03-22 19:45:57 +04:00
process().vm.check_write_ptr(wstatus)?;
}
2019-03-08 11:54:03 +04:00
#[derive(Debug)]
enum WaitFor {
AnyChild,
Pid(usize),
}
let target = match pid {
2019-03-22 19:45:57 +04:00
-1 | 0 => WaitFor::AnyChild,
2019-03-08 11:54:03 +04:00
p if p > 0 => WaitFor::Pid(p as usize),
_ => unimplemented!(),
};
2019-02-22 10:10:24 +04:00
loop {
2019-03-10 21:06:44 +04:00
let mut proc = process();
// check child_exit_code
let find = match target {
2019-03-27 14:35:08 +04:00
WaitFor::AnyChild => proc
.child_exit_code
.iter()
.next()
.map(|(&pid, &code)| (pid, code)),
WaitFor::Pid(pid) => proc.child_exit_code.get(&pid).map(|&code| (pid, code)),
2019-02-22 10:10:24 +04:00
};
2019-03-10 21:06:44 +04:00
// if found, return
if let Some((pid, exit_code)) = find {
proc.child_exit_code.remove(&pid);
if !wstatus.is_null() {
2019-03-27 14:35:08 +04:00
unsafe {
wstatus.write(exit_code as i32);
}
2019-02-22 10:10:24 +04:00
}
2019-03-10 21:06:44 +04:00
return Ok(pid);
2019-02-22 10:10:24 +04:00
}
2019-03-10 21:06:44 +04:00
// if not, check pid
2019-03-27 14:35:08 +04:00
let children: Vec<_> = proc
.children
.iter()
2019-03-10 21:06:44 +04:00
.filter_map(|weak| weak.upgrade())
.collect();
let invalid = match target {
WaitFor::AnyChild => children.len() == 0,
2019-03-27 14:35:08 +04:00
WaitFor::Pid(pid) => children
.iter()
.find(|p| p.lock().pid.get() == pid)
.is_none(),
2019-03-10 21:06:44 +04:00
};
if invalid {
return Err(SysError::ECHILD);
2019-02-22 10:10:24 +04:00
}
2019-03-27 14:35:08 +04:00
info!(
"wait: thread {} -> {:?}, sleep",
thread::current().id(),
target
);
2019-03-10 21:06:44 +04:00
let condvar = proc.child_exit.clone();
drop(proc); // must release lock of current process
condvar._wait();
2019-02-22 10:10:24 +04:00
}
}
2019-03-27 14:35:08 +04:00
pub fn sys_exec(
name: *const u8,
argv: *const *const u8,
envp: *const *const u8,
tf: &mut TrapFrame,
) -> SysResult {
info!("exec: name: {:?}, argv: {:?} envp: {:?}", name, argv, envp);
let proc = process();
2019-03-27 14:35:08 +04:00
let _name = if name.is_null() {
String::from("")
} else {
2019-03-22 19:45:57 +04:00
unsafe { proc.vm.check_and_clone_cstr(name)? }
2019-02-22 10:10:24 +04:00
};
if argv.is_null() {
2019-02-28 06:59:52 +04:00
return Err(SysError::EINVAL);
2019-02-22 10:10:24 +04:00
}
// Check and copy args to kernel
let mut args = Vec::new();
unsafe {
let mut current_argv = argv as *const *const u8;
2019-03-22 19:45:57 +04:00
proc.vm.check_read_ptr(current_argv)?;
while !(*current_argv).is_null() {
2019-03-22 19:45:57 +04:00
let arg = proc.vm.check_and_clone_cstr(*current_argv)?;
args.push(arg);
current_argv = current_argv.add(1);
}
}
info!("exec: args {:?}", args);
2019-02-22 10:10:24 +04:00
// Read program file
let path = args[0].as_str();
let inode = proc.lookup_inode(path)?;
2019-03-17 20:18:03 +04:00
let buf = inode.read_as_vec()?;
2019-02-22 10:10:24 +04:00
// Make new Thread
let iter = args.iter().map(|s| s.as_str());
let mut thread = Thread::new_user(buf.as_slice(), iter);
2019-03-17 20:18:03 +04:00
thread.proc.lock().clone_for_exec(&proc);
2019-02-22 10:10:24 +04:00
// Activate new page table
2019-03-27 14:35:08 +04:00
unsafe {
thread.proc.lock().vm.activate();
}
2019-02-22 10:10:24 +04:00
// Modify the TrapFrame
*tf = unsafe { thread.context.get_init_tf() };
// Swap Context but keep KStack
::core::mem::swap(&mut current_thread().kstack, &mut thread.kstack);
::core::mem::swap(current_thread(), &mut *thread);
Ok(0)
}
pub fn sys_yield() -> SysResult {
thread::yield_now();
Ok(0)
}
/// Kill the process
2019-03-11 13:55:39 +04:00
pub fn sys_kill(pid: usize, sig: usize) -> SysResult {
2019-03-27 14:35:08 +04:00
info!(
"kill: {} killed: {} with sig {}",
thread::current().id(),
pid,
sig
);
2019-03-11 13:55:39 +04:00
let current_pid = process().pid.get().clone();
if current_pid == pid {
// killing myself
sys_exit_group(sig);
} else {
if let Some(proc_arc) = PROCESSES.read().get(&pid).and_then(|weak| weak.upgrade()) {
let proc = proc_arc.lock();
// quit all threads
for tid in proc.threads.iter() {
processor().manager().exit(*tid, sig);
}
// notify parent and fill exit code
// avoid deadlock
let proc_parent = proc.parent.clone();
let pid = proc.pid.get();
drop(proc);
if let Some(parent) = proc_parent {
let mut parent = parent.lock();
parent.child_exit_code.insert(pid, sig);
parent.child_exit.notify_one();
}
Ok(0)
} else {
Err(SysError::EINVAL)
}
2019-02-22 10:10:24 +04:00
}
}
/// Get the current process id
pub fn sys_getpid() -> SysResult {
info!("getpid");
2019-03-10 11:23:15 +04:00
Ok(process().pid.get())
2019-02-22 10:10:24 +04:00
}
/// Get the current thread id
pub fn sys_gettid() -> SysResult {
info!("gettid");
// use pid as tid for now
2019-03-08 15:04:39 +04:00
Ok(thread::current().id())
}
/// Get the parent process id
pub fn sys_getppid() -> SysResult {
2019-03-10 21:06:44 +04:00
Ok(process().parent.as_ref().unwrap().lock().pid.get())
}
/// Exit the current thread
2019-03-10 11:23:15 +04:00
pub fn sys_exit(exit_code: usize) -> ! {
let tid = thread::current().id();
info!("exit: {}, code: {}", tid, exit_code);
let mut proc = process();
proc.threads.retain(|&id| id != tid);
2019-03-11 13:19:00 +04:00
// for last thread,
// notify parent and fill exit code
// avoid deadlock
let exit = proc.threads.len() == 0;
let proc_parent = proc.parent.clone();
let pid = proc.pid.get();
2019-03-10 11:23:15 +04:00
drop(proc);
2019-03-11 13:19:00 +04:00
if exit {
if let Some(parent) = proc_parent {
let mut parent = parent.lock();
parent.child_exit_code.insert(pid, exit_code);
parent.child_exit.notify_one();
}
}
// perform futex wake 1
// ref: http://man7.org/linux/man-pages/man2/set_tid_address.2.html
// FIXME: do it in all possible ways a thread can exit
// it has memory access so we can't move it to Thread::drop?
let clear_child_tid = current_thread().clear_child_tid;
if clear_child_tid != 0 {
2019-03-27 14:35:08 +04:00
unsafe {
(clear_child_tid as *mut u32).write(0);
}
let queue = process().get_futex(clear_child_tid);
queue.notify_one();
}
2019-03-10 11:23:15 +04:00
processor().manager().exit(tid, exit_code as usize);
processor().yield_now();
unreachable!();
}
2019-03-10 21:06:44 +04:00
/// Exit the current thread group (i.e. process)
2019-03-10 11:23:15 +04:00
pub fn sys_exit_group(exit_code: usize) -> ! {
2019-03-23 20:36:13 +04:00
let proc = process();
2019-03-10 11:23:15 +04:00
info!("exit_group: {}, code: {}", proc.pid, exit_code);
// quit all threads
for tid in proc.threads.iter() {
processor().manager().exit(*tid, exit_code);
}
2019-03-11 13:19:00 +04:00
// notify parent and fill exit code
// avoid deadlock
let proc_parent = proc.parent.clone();
let pid = proc.pid.get();
2019-03-10 11:23:15 +04:00
drop(proc);
2019-03-11 13:19:00 +04:00
if let Some(parent) = proc_parent {
let mut parent = parent.lock();
parent.child_exit_code.insert(pid, exit_code);
parent.child_exit.notify_one();
}
2019-03-10 11:23:15 +04:00
2019-02-22 10:10:24 +04:00
processor().yield_now();
unreachable!();
}
2019-03-09 10:08:56 +04:00
pub fn sys_nanosleep(req: *const TimeSpec) -> SysResult {
2019-03-22 19:45:57 +04:00
process().vm.check_read_ptr(req)?;
2019-03-09 10:08:56 +04:00
let time = unsafe { req.read() };
info!("nanosleep: time: {:#?}", time);
2019-03-24 19:53:09 +04:00
// TODO: handle spurious wakeup
2019-03-09 10:08:56 +04:00
thread::sleep(time.to_duration());
2019-02-22 10:10:24 +04:00
Ok(0)
}
pub fn sys_set_priority(priority: usize) -> SysResult {
let pid = thread::current().id();
processor().manager().set_priority(pid, priority as u8);
Ok(0)
}