redox/kernel/syscall/process.rs

123 lines
3.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;
use arch::interrupt::halt;
use arch::paging::{ActivePageTable, Page, 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> {
//TODO: Make this more efficient
let mut active_table = unsafe { ActivePageTable::new() };
let mut current = arch::USER_HEAP_OFFSET;
{
let min_page = Page::containing_address(VirtualAddress::new(arch::USER_HEAP_OFFSET));
let max_page = Page::containing_address(VirtualAddress::new(arch::USER_HEAP_OFFSET + arch::USER_HEAP_SIZE - 1));
for page in Page::range_inclusive(min_page, max_page) {
if active_table.translate_page(page).is_none() {
break;
}
current = page.start_address().get() + 4096;
}
}
if address == 0 {
//println!("Brk query {:X}", current);
Ok(current)
} else if address > current {
let start_page = Page::containing_address(VirtualAddress::new(current));
let end_page = Page::containing_address(VirtualAddress::new(address - 1));
for page in Page::range_inclusive(start_page, end_page) {
//println!("Map {:X}", page.start_address().get());
if active_table.translate_page(page).is_none() {
//println!("Not found - mapping");
active_table.map(page, entry::PRESENT | entry::WRITABLE | entry::NO_EXECUTE | entry::USER_ACCESSIBLE);
active_table.flush(page);
2016-09-11 06:06:09 +02:00
} else {
//println!("Found - skipping");
}
}
//let new = end_page.start_address().get() + 4096;
//println!("Brk increase {:X}: from {:X} to {:X}", address, current, new);
Ok(address)
} else {
let start_page = Page::containing_address(VirtualAddress::new(address));
let end_page = Page::containing_address(VirtualAddress::new(current - 1));
for page in Page::range_inclusive(start_page, end_page) {
//println!("Unmap {:X}", page.start_address().get());
if active_table.translate_page(page).is_some() {
//println!("Found - unmapping");
active_table.unmap(page);
active_table.flush(page);
2016-09-11 06:06:09 +02:00
} else {
//println!("Not found - skipping");
}
}
//let new = start_page.start_address().get();
//println!("Brk decrease {:X}: from {:X} to {:X}", address, current, new);
Ok(address)
}
}
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);
loop {
unsafe { halt() };
}
}
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-11 06:06:09 +02:00
Ok(elf) => {
elf.run();
Ok(0)
},
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)
}