Implement vfork

This commit is contained in:
Jeremy Soller 2016-09-28 21:33:54 -06:00
parent aa0fb28edc
commit b3d980b9ac
2 changed files with 154 additions and 94 deletions

View file

@ -20,10 +20,14 @@ pub enum Status {
pub struct Context {
/// The ID of this context
pub id: usize,
/// The ID of the parent context
pub ppid: usize,
/// Status of context
pub status: Status,
/// Context running or not
pub running: bool,
/// Context is halting parent
pub vfork: bool,
/// The architecture specific context
pub arch: arch::context::Context,
/// Kernel FX
@ -53,8 +57,10 @@ impl Context {
pub fn new(id: usize) -> Context {
Context {
id: id,
ppid: 0,
status: Status::Blocked,
running: false,
vfork: false,
arch: arch::context::Context::new(),
kfx: None,
kstack: None,

View file

@ -19,7 +19,7 @@ use scheme;
use syscall;
use syscall::data::Stat;
use syscall::error::*;
use syscall::flag::{CLONE_VM, CLONE_FS, CLONE_FILES, MAP_WRITE, MAP_WRITE_COMBINE, WNOHANG};
use syscall::flag::{CLONE_VFORK, CLONE_VM, CLONE_FS, CLONE_FILES, MAP_WRITE, MAP_WRITE_COMBINE, WNOHANG};
use syscall::validate::{validate_slice, validate_slice_mut};
pub fn brk(address: usize) -> Result<usize> {
@ -59,6 +59,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
let pid;
{
let arch;
let vfork;
let mut kfx_option = None;
let mut kstack_option = None;
let mut offset = 0;
@ -227,6 +228,18 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
}
}
// If vfork, block the current process
// This has to be done after the operations that may require context switches
if flags & CLONE_VFORK == CLONE_VFORK {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let mut context = context_lock.write();
context.status = context::Status::Blocked;
vfork = true;
} else {
vfork = false;
}
// Set up new process
{
let mut contexts = context::contexts_mut();
@ -235,6 +248,12 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
pid = context.id;
context.ppid = ppid;
context.status = context::Status::Runnable;
context.vfork = vfork;
context.arch = arch;
let mut active_table = unsafe { ActivePageTable::new() };
@ -246,6 +265,8 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
InactivePageTable::new(frame, &mut active_table, &mut temporary_page)
};
context.arch.set_page_table(unsafe { new_table.address() });
// Copy kernel mapping
{
let frame = active_table.p4()[510].pointed_frame().expect("kernel table not mapped");
@ -350,10 +371,6 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
context.env = env;
context.files = files;
context.arch.set_page_table(unsafe { new_table.address() });
context.status = context::Status::Runnable;
}
}
@ -365,6 +382,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
pub fn exit(status: usize) -> ! {
{
let contexts = context::contexts();
let (vfork, ppid) = {
let context_lock = contexts.current().expect("tried to exit without context");
let mut context = context_lock.write();
context.image.clear();
@ -373,6 +391,23 @@ pub fn exit(status: usize) -> ! {
context.grants = Arc::new(Mutex::new(Vec::new()));
context.files = Arc::new(Mutex::new(Vec::new()));
context.status = context::Status::Exited(status);
let vfork = context.vfork;
context.vfork = false;
(vfork, context.ppid)
};
if vfork {
if let Some(context_lock) = contexts.get(ppid) {
let mut context = context_lock.write();
if context.status == context::Status::Blocked {
context.status = context::Status::Runnable;
} else {
println!("{} not blocked for exit vfork unblock", ppid);
}
} else {
println!("{} not found for exit vfork unblock", ppid);
}
}
}
unsafe { context::switch(); }
@ -407,6 +442,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
drop(arg_ptrs); // Drop so that usage is not allowed after unmapping context
let contexts = context::contexts();
let (vfork, ppid) = {
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let mut context = context_lock.write();
@ -506,6 +542,24 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
context.image.push(memory.to_shared());
}
let vfork = context.vfork;
context.vfork = false;
(vfork, context.ppid)
};
if vfork {
if let Some(context_lock) = contexts.get(ppid) {
let mut context = context_lock.write();
if context.status == context::Status::Blocked {
context.status = context::Status::Runnable;
} else {
println!("{} not blocked for exec vfork unblock", ppid);
}
} else {
println!("{} not found for exec vfork unblock", ppid);
}
}
},
Err(err) => {
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);