2019-02-22 10:10:24 +04:00
|
|
|
//! Syscalls for process
|
|
|
|
|
|
|
|
use super::*;
|
2019-03-10 11:23:15 +04:00
|
|
|
use crate::process::{PROCESSES, CHILD_PROCESSES};
|
|
|
|
use crate::sync::Condvar;
|
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.
|
2019-03-09 11:55:00 +04:00
|
|
|
/// 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-09 08:54:26 +04:00
|
|
|
pub fn sys_clone(flags: usize, newsp: usize, parent_tid: *mut u32, child_tid: *mut u32, newtls: usize, tf: &TrapFrame) -> SysResult {
|
2019-03-08 20:47:02 +04:00
|
|
|
info!("clone: flags: {:#x}, newsp: {:#x}, parent_tid: {:?}, child_tid: {:?}, newtls: {:#x}",
|
|
|
|
flags, newsp, parent_tid, child_tid, newtls);
|
2019-03-08 18:37:05 +04:00
|
|
|
if flags != 0x7d0f00 {
|
|
|
|
warn!("sys_clone only support musl pthread_create");
|
|
|
|
return Err(SysError::ENOSYS);
|
|
|
|
}
|
|
|
|
{
|
2019-03-09 11:55:00 +04:00
|
|
|
let proc = process();
|
|
|
|
proc.memory_set.check_mut_ptr(parent_tid)?;
|
|
|
|
proc.memory_set.check_mut_ptr(child_tid)?;
|
2019-03-08 18:37:05 +04:00
|
|
|
}
|
2019-03-09 08:54:26 +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 {
|
2019-03-09 08:54:26 +04:00
|
|
|
parent_tid.write(tid as u32);
|
|
|
|
child_tid.write(tid as u32);
|
2019-03-08 18:37:05 +04:00
|
|
|
}
|
|
|
|
Ok(tid)
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:10:24 +04:00
|
|
|
/// Wait the process exit.
|
|
|
|
/// Return the PID. Store exit code to `code` 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);
|
2019-03-10 11:23:15 +04:00
|
|
|
let cur_pid = process().pid.get();
|
2019-03-08 15:04:39 +04:00
|
|
|
if !wstatus.is_null() {
|
|
|
|
process().memory_set.check_mut_ptr(wstatus)?;
|
2019-03-07 19:32:47 +04:00
|
|
|
}
|
2019-03-08 11:54:03 +04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum WaitFor {
|
|
|
|
AnyChild,
|
|
|
|
Pid(usize),
|
|
|
|
}
|
|
|
|
let target = match pid {
|
|
|
|
-1 => WaitFor::AnyChild,
|
|
|
|
p if p > 0 => WaitFor::Pid(p as usize),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
2019-02-22 10:10:24 +04:00
|
|
|
loop {
|
|
|
|
use alloc::vec;
|
2019-03-10 11:23:15 +04:00
|
|
|
let all_child: Vec<_> = CHILD_PROCESSES.read().get(&cur_pid).unwrap().clone();
|
2019-03-08 11:54:03 +04:00
|
|
|
let wait_procs = match target {
|
2019-03-10 11:23:15 +04:00
|
|
|
WaitFor::AnyChild => all_child,
|
2019-03-08 11:54:03 +04:00
|
|
|
WaitFor::Pid(pid) => {
|
|
|
|
// check if pid is a child
|
2019-03-10 11:23:15 +04:00
|
|
|
if let Some(proc) = all_child.iter().find(|p| p.lock().pid.get() == pid) {
|
|
|
|
vec![proc.clone()]
|
2019-03-07 19:32:47 +04:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 10:10:24 +04:00
|
|
|
};
|
|
|
|
if wait_procs.is_empty() {
|
2019-03-07 19:32:47 +04:00
|
|
|
return Err(SysError::ECHILD);
|
2019-02-22 10:10:24 +04:00
|
|
|
}
|
2019-03-07 19:32:47 +04:00
|
|
|
|
2019-03-10 11:23:15 +04:00
|
|
|
for proc_lock in wait_procs.iter() {
|
|
|
|
let proc = proc_lock.lock();
|
|
|
|
if let Some(exit_code) = proc.exit_code {
|
|
|
|
// recycle process
|
|
|
|
let pid = proc.pid.get();
|
|
|
|
drop(proc);
|
|
|
|
|
|
|
|
let mut child_processes = CHILD_PROCESSES.write();
|
|
|
|
child_processes.get_mut(&cur_pid).unwrap().retain(|p| p.lock().pid.get() != pid);
|
|
|
|
child_processes.remove(&pid);
|
|
|
|
return Ok(pid);
|
2019-02-22 10:10:24 +04:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 11:54:03 +04:00
|
|
|
info!("wait: {} -> {:?}, sleep", thread::current().id(), target);
|
2019-03-10 11:23:15 +04:00
|
|
|
|
|
|
|
for proc in wait_procs.iter() {
|
|
|
|
proc.lock().exit_cond.add_to_wait_queue();
|
2019-02-22 10:10:24 +04:00
|
|
|
}
|
2019-03-10 11:23:15 +04:00
|
|
|
thread::park();
|
2019-02-22 10:10:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 10:21:26 +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);
|
2019-03-02 15:09:39 +04:00
|
|
|
let proc = process();
|
|
|
|
let name = if name.is_null() { String::from("") } else {
|
2019-03-02 21:27:30 +04:00
|
|
|
unsafe { proc.memory_set.check_and_clone_cstr(name)? }
|
2019-02-22 10:10:24 +04:00
|
|
|
};
|
2019-03-07 10:21:26 +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
|
|
|
}
|
2019-03-02 15:09:39 +04:00
|
|
|
// Check and copy args to kernel
|
|
|
|
let mut args = Vec::new();
|
|
|
|
unsafe {
|
2019-03-07 10:21:26 +04:00
|
|
|
let mut current_argv = argv as *const *const u8;
|
|
|
|
while !(*current_argv).is_null() {
|
|
|
|
let arg = proc.memory_set.check_and_clone_cstr(*current_argv)?;
|
2019-03-02 15:09:39 +04:00
|
|
|
args.push(arg);
|
2019-03-07 10:21:26 +04:00
|
|
|
current_argv = current_argv.add(1);
|
2019-03-02 15:09:39 +04:00
|
|
|
}
|
|
|
|
}
|
2019-03-07 10:21:26 +04:00
|
|
|
info!("exec: args {:?}", args);
|
2019-03-02 15:09:39 +04:00
|
|
|
|
2019-02-22 10:10:24 +04:00
|
|
|
// Read program file
|
|
|
|
let path = args[0].as_str();
|
|
|
|
let inode = crate::fs::ROOT_INODE.lookup(path)?;
|
2019-02-22 13:10:07 +04:00
|
|
|
let size = inode.metadata()?.size;
|
2019-02-22 10:10:24 +04:00
|
|
|
let mut buf = Vec::with_capacity(size);
|
|
|
|
unsafe { buf.set_len(size); }
|
|
|
|
inode.read_at(0, buf.as_mut_slice())?;
|
|
|
|
|
|
|
|
// Make new Thread
|
|
|
|
let iter = args.iter().map(|s| s.as_str());
|
|
|
|
let mut thread = Thread::new_user(buf.as_slice(), iter);
|
2019-03-07 10:21:26 +04:00
|
|
|
thread.proc.lock().files = proc.files.clone();
|
|
|
|
thread.proc.lock().cwd = proc.cwd.clone();
|
2019-02-22 10:10:24 +04:00
|
|
|
|
|
|
|
// Activate new page table
|
|
|
|
unsafe { thread.proc.lock().memory_set.activate(); }
|
|
|
|
|
|
|
|
// 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
|
|
|
|
pub fn sys_kill(pid: usize) -> SysResult {
|
|
|
|
info!("{} killed: {}", thread::current().id(), pid);
|
|
|
|
processor().manager().exit(pid, 0x100);
|
|
|
|
if pid == thread::current().id() {
|
|
|
|
processor().yield_now();
|
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the current process id
|
|
|
|
pub fn sys_getpid() -> SysResult {
|
2019-03-09 20:58:10 +04:00
|
|
|
info!("getpid");
|
2019-03-10 11:23:15 +04:00
|
|
|
Ok(process().pid.get())
|
2019-02-22 10:10:24 +04:00
|
|
|
}
|
|
|
|
|
2019-03-07 19:04:52 +04:00
|
|
|
/// Get the current thread id
|
|
|
|
pub fn sys_gettid() -> SysResult {
|
2019-03-09 20:58:10 +04:00
|
|
|
info!("gettid");
|
2019-03-07 19:04:52 +04:00
|
|
|
// use pid as tid for now
|
2019-03-08 15:04:39 +04:00
|
|
|
Ok(thread::current().id())
|
2019-03-07 19:04:52 +04:00
|
|
|
}
|
|
|
|
|
2019-03-02 15:09:39 +04:00
|
|
|
/// Get the parent process id
|
|
|
|
pub fn sys_getppid() -> SysResult {
|
2019-03-10 11:23:15 +04:00
|
|
|
Ok(process().ppid.get())
|
2019-03-02 15:09:39 +04:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:49:59 +04:00
|
|
|
/// 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);
|
|
|
|
if proc.threads.len() == 0 {
|
|
|
|
// last thread
|
|
|
|
proc.exit_code = Some(exit_code);
|
|
|
|
proc.exit_cond.notify_all();
|
|
|
|
}
|
|
|
|
drop(proc);
|
2019-03-07 10:55:31 +04:00
|
|
|
|
2019-03-09 08:54:26 +04:00
|
|
|
// 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 {
|
|
|
|
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!();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exit the current thread group (i.e. progress)
|
|
|
|
pub fn sys_exit_group(exit_code: usize) -> ! {
|
|
|
|
let mut proc = process();
|
|
|
|
info!("exit_group: {}, code: {}", proc.pid, exit_code);
|
|
|
|
|
|
|
|
// quit all threads
|
|
|
|
for tid in proc.threads.iter() {
|
|
|
|
processor().manager().exit(*tid, exit_code);
|
|
|
|
}
|
|
|
|
proc.exit_code = Some(exit_code);
|
|
|
|
proc.exit_cond.notify_all();
|
|
|
|
drop(proc);
|
|
|
|
|
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 {
|
|
|
|
process().memory_set.check_ptr(req)?;
|
|
|
|
let time = unsafe { req.read() };
|
|
|
|
info!("nanosleep: time: {:#?}", time);
|
|
|
|
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)
|
|
|
|
}
|