redox/kernel/syscall/process.rs

194 lines
5.9 KiB
Rust
Raw Normal View History

2016-08-14 23:58:35 +02:00
///! Process syscalls
use core::mem;
2016-09-11 06:06:09 +02:00
use core::str;
2016-08-14 23:58:35 +02:00
2016-09-11 06:06:09 +02:00
use arch;
2016-09-15 06:21:52 +02:00
use arch::memory::allocate_frame;
use arch::paging::{ActivePageTable, InactivePageTable, Page, VirtualAddress, entry};
use arch::paging::temporary_page::TemporaryPage;
2016-08-20 22:32:45 +02:00
use context;
2016-09-11 06:06:09 +02:00
use elf;
use syscall::{self, Error, Result};
2016-08-20 22:32:45 +02:00
2016-09-11 06:06:09 +02:00
pub fn brk(address: usize) -> Result<usize> {
2016-09-12 05:18:18 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
let mut context = context_lock.write();
2016-09-11 06:06:09 +02:00
let mut current = arch::USER_HEAP_OFFSET;
2016-09-12 05:18:18 +02:00
if let Some(ref heap) = context.heap {
current = heap.start_address().get() + heap.size();
2016-09-11 06:06:09 +02:00
}
if address == 0 {
//println!("Brk query {:X}", current);
Ok(current)
2016-09-12 05:18:18 +02:00
} else if address >= arch::USER_HEAP_OFFSET {
//TODO: out of memory errors
if let Some(ref mut heap) = context.heap {
2016-09-12 05:47:44 +02:00
heap.resize(address - arch::USER_HEAP_OFFSET, true, true);
2016-09-12 05:18:18 +02:00
return Ok(address);
2016-09-11 06:06:09 +02:00
}
2016-09-12 05:18:18 +02:00
context.heap = Some(context::memory::Memory::new(
VirtualAddress::new(arch::USER_HEAP_OFFSET),
address - arch::USER_HEAP_OFFSET,
2016-09-12 05:47:44 +02:00
entry::WRITABLE | entry::NO_EXECUTE | entry::USER_ACCESSIBLE,
true,
true
2016-09-12 05:18:18 +02:00
));
2016-09-11 06:06:09 +02:00
Ok(address)
} else {
2016-09-12 05:18:18 +02:00
//TODO: Return correct error
Err(Error::NotPermitted)
2016-09-11 06:06:09 +02:00
}
}
2016-08-15 00:07:41 +02:00
pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
println!("Clone {:X}: {:X}", flags, stack_base);
let arch;
2016-09-14 05:27:27 +02:00
let mut kstack_option = None;
let mut offset = 0;
2016-09-14 05:27:27 +02:00
let mut stack_option = None;
// Copy from old process
{
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
let context = context_lock.read();
arch = context.arch.clone();
if let Some(ref stack) = context.kstack {
offset = stack_base - stack.as_ptr() as usize - mem::size_of::<usize>(); // Add clone ret
let mut new_stack = stack.clone();
unsafe {
let func_ptr = new_stack.as_mut_ptr().offset(offset as isize);
*(func_ptr as *mut usize) = arch::interrupt::syscall::clone_ret as usize;
}
2016-09-14 05:27:27 +02:00
kstack_option = Some(new_stack);
}
if let Some(ref stack) = context.stack {
2016-09-15 06:21:52 +02:00
let new_stack = context::memory::Memory::new(
2016-09-14 05:27:27 +02:00
VirtualAddress::new(arch::USER_TMP_STACK_OFFSET),
stack.size(),
entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE,
true,
true //TODO: Don't clear stack?
);
unsafe {
arch::externs::memcpy(new_stack.start_address().get() as *mut u8,
stack.start_address().get() as *const u8,
stack.size());
}
stack_option = Some(new_stack);
}
}
// Set up new process
let pid;
{
let mut contexts = context::contexts_mut();
let context_lock = contexts.new_context()?;
let mut context = context_lock.write();
context.arch = arch;
2016-09-15 06:21:52 +02:00
let mut active_table = unsafe { ActivePageTable::new() };
let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(0x8_0000_0000)));
let mut new_table = {
let frame = allocate_frame().expect("no more frames in syscall::clone new_table");
InactivePageTable::new(frame, &mut active_table, &mut temporary_page)
};
// Copy kernel mapping
let kernel_frame = active_table.p4()[510].pointed_frame().expect("kernel table not mapped");
active_table.with(&mut new_table, &mut temporary_page, |mapper| {
mapper.p4_mut()[510].set(kernel_frame, entry::PRESENT | entry::WRITABLE);
});
2016-09-14 05:27:27 +02:00
if let Some(stack) = kstack_option.take() {
context.arch.set_stack(stack.as_ptr() as usize + offset);
context.kstack = Some(stack);
}
2016-09-15 06:21:52 +02:00
2016-09-14 05:27:27 +02:00
if let Some(mut stack) = stack_option.take() {
2016-09-15 06:21:52 +02:00
stack.move_to(VirtualAddress::new(arch::USER_STACK_OFFSET), &mut new_table, &mut temporary_page, true);
2016-09-14 05:27:27 +02:00
context.stack = Some(stack);
}
2016-09-15 06:21:52 +02:00
context.arch.set_page_table(unsafe { new_table.address() });
context.blocked = false;
pid = context.id;
}
2016-09-15 06:21:52 +02:00
unsafe { context::switch(); }
Ok(pid)
}
2016-08-14 23:58:35 +02:00
pub fn exit(status: usize) -> ! {
println!("Exit {}", status);
2016-09-12 06:03:03 +02:00
{
let contexts = context::contexts();
let context_lock = contexts.current().expect("tried to exit without context");
let mut context = context_lock.write();
context.image.clear();
drop(context.heap.take());
drop(context.stack.take());
context.exited = true;
2016-08-14 23:58:35 +02:00
}
2016-09-12 06:03:03 +02:00
unsafe { context::switch(); }
unreachable!();
2016-08-14 23:58:35 +02:00
}
2016-08-15 00:07:41 +02:00
2016-09-11 06:06:09 +02:00
pub fn exec(path: &[u8], _args: &[[usize; 2]]) -> Result<usize> {
//TODO: Use args
//TODO: Unmap previous mappings
//TODO: Drop data vec
println!("Exec {}", unsafe { str::from_utf8_unchecked(path) });
2016-09-11 06:06:09 +02:00
let file = syscall::open(path, 0)?;
let mut data = vec![];
2016-09-11 06:06:09 +02:00
loop {
let mut buf = [0; 4096];
let count = syscall::read(file, &mut buf)?;
2016-09-11 06:06:09 +02:00
if count > 0 {
data.extend_from_slice(&buf[..count]);
2016-09-11 06:06:09 +02:00
} else {
break;
}
}
let _ = syscall::close(file);
2016-09-11 06:06:09 +02:00
match elf::Elf::from(&data) {
2016-09-12 05:04:34 +02:00
Ok(elf) => elf.run().and(Ok(0)),
2016-09-11 06:06:09 +02:00
Err(err) => {
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
Err(Error::NoExec)
}
2016-08-15 00:07:41 +02:00
}
}
2016-08-20 22:32:45 +02:00
pub fn getpid() -> Result<usize> {
2016-09-09 03:31:26 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
let context = context_lock.read();
Ok(context.id)
2016-08-20 22:32:45 +02:00
}
2016-09-11 23:56:48 +02:00
pub fn iopl(_level: usize) -> Result<usize> {
//TODO
Ok(0)
}
pub fn sched_yield() -> Result<usize> {
unsafe { context::switch(); }
Ok(0)
}