redox/kernel/syscall/process.rs

112 lines
2.9 KiB
Rust
Raw Normal View History

2016-08-14 23:58:35 +02:00
///! Process syscalls
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-12 05:18:18 +02:00
use arch::paging::{VirtualAddress, entry};
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) -> Result<usize> {
println!("Clone {:X}", flags);
Ok(0)
}
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)
}