Implement vfork
This commit is contained in:
parent
aa0fb28edc
commit
b3d980b9ac
|
@ -20,10 +20,14 @@ pub enum Status {
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
/// The ID of this context
|
/// The ID of this context
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
|
/// The ID of the parent context
|
||||||
|
pub ppid: usize,
|
||||||
/// Status of context
|
/// Status of context
|
||||||
pub status: Status,
|
pub status: Status,
|
||||||
/// Context running or not
|
/// Context running or not
|
||||||
pub running: bool,
|
pub running: bool,
|
||||||
|
/// Context is halting parent
|
||||||
|
pub vfork: bool,
|
||||||
/// The architecture specific context
|
/// The architecture specific context
|
||||||
pub arch: arch::context::Context,
|
pub arch: arch::context::Context,
|
||||||
/// Kernel FX
|
/// Kernel FX
|
||||||
|
@ -53,8 +57,10 @@ impl Context {
|
||||||
pub fn new(id: usize) -> Context {
|
pub fn new(id: usize) -> Context {
|
||||||
Context {
|
Context {
|
||||||
id: id,
|
id: id,
|
||||||
|
ppid: 0,
|
||||||
status: Status::Blocked,
|
status: Status::Blocked,
|
||||||
running: false,
|
running: false,
|
||||||
|
vfork: false,
|
||||||
arch: arch::context::Context::new(),
|
arch: arch::context::Context::new(),
|
||||||
kfx: None,
|
kfx: None,
|
||||||
kstack: None,
|
kstack: None,
|
||||||
|
|
|
@ -19,7 +19,7 @@ use scheme;
|
||||||
use syscall;
|
use syscall;
|
||||||
use syscall::data::Stat;
|
use syscall::data::Stat;
|
||||||
use syscall::error::*;
|
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};
|
use syscall::validate::{validate_slice, validate_slice_mut};
|
||||||
|
|
||||||
pub fn brk(address: usize) -> Result<usize> {
|
pub fn brk(address: usize) -> Result<usize> {
|
||||||
|
@ -59,6 +59,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
||||||
let pid;
|
let pid;
|
||||||
{
|
{
|
||||||
let arch;
|
let arch;
|
||||||
|
let vfork;
|
||||||
let mut kfx_option = None;
|
let mut kfx_option = None;
|
||||||
let mut kstack_option = None;
|
let mut kstack_option = None;
|
||||||
let mut offset = 0;
|
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
|
// Set up new process
|
||||||
{
|
{
|
||||||
let mut contexts = context::contexts_mut();
|
let mut contexts = context::contexts_mut();
|
||||||
|
@ -235,6 +248,12 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
||||||
|
|
||||||
pid = context.id;
|
pid = context.id;
|
||||||
|
|
||||||
|
context.ppid = ppid;
|
||||||
|
|
||||||
|
context.status = context::Status::Runnable;
|
||||||
|
|
||||||
|
context.vfork = vfork;
|
||||||
|
|
||||||
context.arch = arch;
|
context.arch = arch;
|
||||||
|
|
||||||
let mut active_table = unsafe { ActivePageTable::new() };
|
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)
|
InactivePageTable::new(frame, &mut active_table, &mut temporary_page)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
context.arch.set_page_table(unsafe { new_table.address() });
|
||||||
|
|
||||||
// Copy kernel mapping
|
// Copy kernel mapping
|
||||||
{
|
{
|
||||||
let frame = active_table.p4()[510].pointed_frame().expect("kernel table not mapped");
|
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.env = env;
|
||||||
|
|
||||||
context.files = files;
|
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) -> ! {
|
pub fn exit(status: usize) -> ! {
|
||||||
{
|
{
|
||||||
let contexts = context::contexts();
|
let contexts = context::contexts();
|
||||||
|
let (vfork, ppid) = {
|
||||||
let context_lock = contexts.current().expect("tried to exit without context");
|
let context_lock = contexts.current().expect("tried to exit without context");
|
||||||
let mut context = context_lock.write();
|
let mut context = context_lock.write();
|
||||||
context.image.clear();
|
context.image.clear();
|
||||||
|
@ -373,6 +391,23 @@ pub fn exit(status: usize) -> ! {
|
||||||
context.grants = Arc::new(Mutex::new(Vec::new()));
|
context.grants = Arc::new(Mutex::new(Vec::new()));
|
||||||
context.files = Arc::new(Mutex::new(Vec::new()));
|
context.files = Arc::new(Mutex::new(Vec::new()));
|
||||||
context.status = context::Status::Exited(status);
|
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(); }
|
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
|
drop(arg_ptrs); // Drop so that usage is not allowed after unmapping context
|
||||||
|
|
||||||
let contexts = context::contexts();
|
let contexts = context::contexts();
|
||||||
|
let (vfork, ppid) = {
|
||||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||||
let mut context = context_lock.write();
|
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());
|
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) => {
|
Err(err) => {
|
||||||
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
|
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
|
||||||
|
|
Loading…
Reference in a new issue