Create example userspace scheme. Remove kernel duplication of syscalls, use syscall crate instead
This commit is contained in:
parent
941fc0b494
commit
f60661820d
25 changed files with 374 additions and 414 deletions
|
@ -1,64 +0,0 @@
|
|||
/// System call list
|
||||
/// See http://syscalls.kernelgrok.com/ for numbers
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[repr(C)]
|
||||
pub enum Call {
|
||||
/// Exit syscall
|
||||
Exit = 1,
|
||||
/// Read syscall
|
||||
Read = 3,
|
||||
/// Write syscall
|
||||
Write = 4,
|
||||
/// Open syscall
|
||||
Open = 5,
|
||||
/// Close syscall
|
||||
Close = 6,
|
||||
/// Wait for a process
|
||||
WaitPid = 7,
|
||||
/// Execute syscall
|
||||
Exec = 11,
|
||||
/// Change working directory
|
||||
ChDir = 12,
|
||||
/// Get process ID
|
||||
GetPid = 20,
|
||||
/// Duplicate file descriptor
|
||||
Dup = 41,
|
||||
/// Set process break
|
||||
Brk = 45,
|
||||
/// Set process I/O privilege level
|
||||
Iopl = 110,
|
||||
/// Sync file descriptor
|
||||
FSync = 118,
|
||||
/// Clone process
|
||||
Clone = 120,
|
||||
/// Yield to scheduler
|
||||
SchedYield = 158,
|
||||
/// Get process working directory
|
||||
GetCwd = 183
|
||||
}
|
||||
|
||||
/// Convert numbers to calls
|
||||
/// See http://syscalls.kernelgrok.com/
|
||||
impl Call {
|
||||
pub fn from(number: usize) -> Option<Call> {
|
||||
match number {
|
||||
1 => Some(Call::Exit),
|
||||
3 => Some(Call::Read),
|
||||
4 => Some(Call::Write),
|
||||
5 => Some(Call::Open),
|
||||
6 => Some(Call::Close),
|
||||
7 => Some(Call::WaitPid),
|
||||
11 => Some(Call::Exec),
|
||||
12 => Some(Call::ChDir),
|
||||
20 => Some(Call::GetPid),
|
||||
41 => Some(Call::Dup),
|
||||
45 => Some(Call::Brk),
|
||||
110 => Some(Call::Iopl),
|
||||
118 => Some(Call::FSync),
|
||||
120 => Some(Call::Clone),
|
||||
158 => Some(Call::SchedYield),
|
||||
183 => Some(Call::GetCwd),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/// The error number for an invalid value
|
||||
/// See http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html for numbers
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[repr(C)]
|
||||
pub enum Error {
|
||||
/// Operation not permitted
|
||||
NotPermitted = 1,
|
||||
/// No such file or directory
|
||||
NoEntry = 2,
|
||||
/// No such process
|
||||
NoProcess = 3,
|
||||
/// Invalid executable format
|
||||
NoExec = 8,
|
||||
/// Bad file number
|
||||
BadFile = 9,
|
||||
/// Try again
|
||||
TryAgain = 11,
|
||||
/// Bad address
|
||||
Fault = 14,
|
||||
/// File exists
|
||||
FileExists = 17,
|
||||
/// No such device
|
||||
NoDevice = 19,
|
||||
/// Invalid argument
|
||||
InvalidValue = 22,
|
||||
/// Too many open files
|
||||
TooManyFiles = 24,
|
||||
/// Illegal seek
|
||||
IllegalSeek = 29,
|
||||
/// Syscall not implemented
|
||||
NoCall = 38
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn from(number: usize) -> Option<Error> {
|
||||
match number {
|
||||
1 => Some(Error::NotPermitted),
|
||||
2 => Some(Error::NoEntry),
|
||||
3 => Some(Error::NoProcess),
|
||||
8 => Some(Error::NoExec),
|
||||
9 => Some(Error::BadFile),
|
||||
11 => Some(Error::TryAgain),
|
||||
14 => Some(Error::Fault),
|
||||
17 => Some(Error::FileExists),
|
||||
19 => Some(Error::NoDevice),
|
||||
22 => Some(Error::InvalidValue),
|
||||
24 => Some(Error::TooManyFiles),
|
||||
29 => Some(Error::IllegalSeek),
|
||||
38 => Some(Error::NoCall),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = ::core::result::Result<T, Error>;
|
||||
|
||||
pub fn convert_to_result(number: usize) -> Result<usize> {
|
||||
if let Some(err) = Error::from((-(number as isize)) as usize) {
|
||||
Err(err)
|
||||
} else {
|
||||
Ok(number)
|
||||
}
|
||||
}
|
|
@ -2,13 +2,12 @@
|
|||
|
||||
use context;
|
||||
use scheme;
|
||||
|
||||
use super::{Error, Result};
|
||||
use syscall::error::*;
|
||||
|
||||
/// Change the current working directory
|
||||
pub fn chdir(path: &[u8]) -> Result<usize> {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let canonical = context.canonicalize(path);
|
||||
*context.cwd.lock() = canonical;
|
||||
|
@ -18,7 +17,7 @@ pub fn chdir(path: &[u8]) -> Result<usize> {
|
|||
/// Get the current working directory
|
||||
pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let cwd = context.cwd.lock();
|
||||
let mut i = 0;
|
||||
|
@ -33,7 +32,7 @@ pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
|
|||
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
||||
let path_canon = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
context.canonicalize(path)
|
||||
};
|
||||
|
@ -43,10 +42,10 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
|||
let reference_opt = parts.next();
|
||||
|
||||
let (scheme_id, file_id) = {
|
||||
let namespace = namespace_opt.ok_or(Error::NoEntry)?;
|
||||
let namespace = namespace_opt.ok_or(Error::new(ENOENT))?;
|
||||
let (scheme_id, scheme) = {
|
||||
let schemes = scheme::schemes();
|
||||
let (scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::NoEntry)?;
|
||||
let (scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::new(ENOENT))?;
|
||||
(scheme_id, scheme.clone())
|
||||
};
|
||||
let file_id = scheme.open(reference_opt.unwrap_or(b""), flags)?;
|
||||
|
@ -54,105 +53,100 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
|||
};
|
||||
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
context.add_file(::context::file::File {
|
||||
scheme: scheme_id,
|
||||
number: file_id
|
||||
}).ok_or(Error::TooManyFiles)
|
||||
}).ok_or(Error::new(EMFILE))
|
||||
}
|
||||
|
||||
/// Close syscall
|
||||
pub fn close(fd: usize) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let file = context.remove_file(fd).ok_or(Error::BadFile)?;
|
||||
let file = context.remove_file(fd).ok_or(Error::new(EBADF))?;
|
||||
file
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.close(file.number).and(Ok(0));
|
||||
result
|
||||
scheme.close(file.number)
|
||||
}
|
||||
|
||||
/// Duplicate file descriptor
|
||||
pub fn dup(fd: usize) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let file = context.get_file(fd).ok_or(Error::BadFile)?;
|
||||
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
|
||||
file
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.dup(file.number);
|
||||
result
|
||||
scheme.dup(file.number)
|
||||
}
|
||||
|
||||
/// Sync the file descriptor
|
||||
pub fn fsync(fd: usize) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let file = context.get_file(fd).ok_or(Error::BadFile)?;
|
||||
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
|
||||
file
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.fsync(file.number).and(Ok(0));
|
||||
result
|
||||
scheme.fsync(file.number)
|
||||
}
|
||||
|
||||
/// Read syscall
|
||||
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let file = context.get_file(fd).ok_or(Error::BadFile)?;
|
||||
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
|
||||
file
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.read(file.number, buf);
|
||||
result
|
||||
scheme.read(file.number, buf)
|
||||
}
|
||||
|
||||
/// Write syscall
|
||||
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let file = context.get_file(fd).ok_or(Error::BadFile)?;
|
||||
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
|
||||
file
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.write(file.number, buf);
|
||||
result
|
||||
scheme.write(file.number, buf)
|
||||
}
|
||||
|
|
|
@ -1,60 +1,51 @@
|
|||
///! Syscall handlers
|
||||
|
||||
pub use self::call::*;
|
||||
pub use self::error::*;
|
||||
extern crate syscall;
|
||||
|
||||
pub use self::syscall::{error, number, scheme};
|
||||
|
||||
use self::error::{Error, Result, ENOSYS};
|
||||
use self::number::*;
|
||||
pub use self::fs::*;
|
||||
pub use self::process::*;
|
||||
pub use self::validate::*;
|
||||
|
||||
/// System call numbers
|
||||
mod call;
|
||||
|
||||
/// System error codes
|
||||
mod error;
|
||||
|
||||
/// Filesystem syscalls
|
||||
mod fs;
|
||||
pub mod fs;
|
||||
|
||||
/// Process syscalls
|
||||
mod process;
|
||||
pub mod process;
|
||||
|
||||
/// Validate input
|
||||
mod validate;
|
||||
pub mod validate;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack: usize) -> usize {
|
||||
#[inline(always)]
|
||||
fn inner(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize, stack: usize) -> Result<usize> {
|
||||
//println!("{}: {:?}: {} {} {} {}", ::context::context_id(), Call::from(a), a, b, c, d);
|
||||
|
||||
match Call::from(a) {
|
||||
Some(call) => match call {
|
||||
Call::Exit => exit(b),
|
||||
Call::Read => read(b, validate_slice_mut(c as *mut u8, d)?),
|
||||
Call::Write => write(b, validate_slice(c as *const u8, d)?),
|
||||
Call::Open => open(validate_slice(b as *const u8, c)?, d),
|
||||
Call::Close => close(b),
|
||||
Call::WaitPid => waitpid(b, c, d),
|
||||
Call::Exec => exec(validate_slice(b as *const u8, c)?, validate_slice(d as *const [usize; 2], e)?),
|
||||
Call::ChDir => chdir(validate_slice(b as *const u8, c)?),
|
||||
Call::GetPid => getpid(),
|
||||
Call::Dup => dup(b),
|
||||
Call::Brk => brk(b),
|
||||
Call::Iopl => iopl(b),
|
||||
Call::FSync => fsync(b),
|
||||
Call::Clone => clone(b, stack),
|
||||
Call::SchedYield => sched_yield(),
|
||||
Call::GetCwd => getcwd(validate_slice_mut(b as *mut u8, c)?)
|
||||
},
|
||||
None => {
|
||||
match a {
|
||||
SYS_EXIT => exit(b),
|
||||
SYS_READ => read(b, validate_slice_mut(c as *mut u8, d)?),
|
||||
SYS_WRITE => write(b, validate_slice(c as *const u8, d)?),
|
||||
SYS_OPEN => open(validate_slice(b as *const u8, c)?, d),
|
||||
SYS_CLOSE => close(b),
|
||||
SYS_WAITPID => waitpid(b, c, d),
|
||||
SYS_EXECVE => exec(validate_slice(b as *const u8, c)?, validate_slice(d as *const [usize; 2], e)?),
|
||||
SYS_CHDIR => chdir(validate_slice(b as *const u8, c)?),
|
||||
SYS_GETPID => getpid(),
|
||||
SYS_DUP => dup(b),
|
||||
SYS_BRK => brk(b),
|
||||
SYS_IOPL => iopl(b),
|
||||
SYS_FSYNC => fsync(b),
|
||||
SYS_CLONE => clone(b, stack),
|
||||
SYS_YIELD => sched_yield(),
|
||||
SYS_GETCWD => getcwd(validate_slice_mut(b as *mut u8, c)?),
|
||||
_ => {
|
||||
println!("Unknown syscall {}", a);
|
||||
Err(Error::NoCall)
|
||||
Err(Error::new(ENOSYS))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match inner(a, b, c, d, e, f, stack) {
|
||||
Ok(value) => value,
|
||||
Err(value) => (-(value as isize)) as usize
|
||||
}
|
||||
Error::mux(inner(a, b, c, d, e, f, stack))
|
||||
}
|
||||
|
|
|
@ -15,11 +15,13 @@ use arch::start::usermode;
|
|||
use context;
|
||||
use elf::{self, program_header};
|
||||
use scheme;
|
||||
use syscall::{self, Error, Result, validate_slice, validate_slice_mut};
|
||||
use syscall;
|
||||
use syscall::error::*;
|
||||
use syscall::validate::{validate_slice, validate_slice_mut};
|
||||
|
||||
pub fn brk(address: usize) -> Result<usize> {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let current = if let Some(ref heap_shared) = context.heap {
|
||||
|
@ -45,8 +47,7 @@ pub fn brk(address: usize) -> Result<usize> {
|
|||
|
||||
Ok(address)
|
||||
} else {
|
||||
//TODO: Return correct error
|
||||
Err(Error::NotPermitted)
|
||||
Err(Error::new(ENOMEM))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,7 +76,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
|||
// Copy from old process
|
||||
{
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
ppid = context.id;
|
||||
|
@ -186,7 +187,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
|||
let result = {
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.dup(file.number);
|
||||
|
@ -377,7 +378,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 context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let mut context = context_lock.write();
|
||||
|
||||
// Unmap previous image and stack
|
||||
|
@ -478,7 +479,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
|||
},
|
||||
Err(err) => {
|
||||
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
|
||||
return Err(Error::NoExec);
|
||||
return Err(Error::new(ENOEXEC));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -489,7 +490,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
|||
|
||||
pub fn getpid() -> Result<usize> {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
Ok(context.id)
|
||||
}
|
||||
|
@ -512,7 +513,7 @@ pub fn waitpid(pid: usize, status_ptr: usize, _options: usize) -> Result<usize>
|
|||
|
||||
{
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.get(pid).ok_or(Error::NoProcess)?;
|
||||
let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
if let context::Status::Exited(status) = context.status {
|
||||
if status_ptr != 0 {
|
||||
|
@ -525,7 +526,7 @@ pub fn waitpid(pid: usize, status_ptr: usize, _options: usize) -> Result<usize>
|
|||
|
||||
if exited {
|
||||
let mut contexts = context::contexts_mut();
|
||||
return contexts.remove(pid).ok_or(Error::NoProcess).and(Ok(pid));
|
||||
return contexts.remove(pid).ok_or(Error::new(ESRCH)).and(Ok(pid));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use core::slice;
|
||||
|
||||
use super::Result;
|
||||
use syscall::error::*;
|
||||
|
||||
/// Convert a pointer and length to slice, if valid
|
||||
/// TODO: Check validity
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue