Create example userspace scheme. Remove kernel duplication of syscalls, use syscall crate instead

This commit is contained in:
Jeremy Soller 2016-09-20 16:23:28 -06:00
parent 941fc0b494
commit f60661820d
25 changed files with 374 additions and 414 deletions

View file

@ -3,8 +3,8 @@ use core::str;
use spin::{Mutex, Once};
use context;
use syscall::Result;
use super::Scheme;
use syscall::error::*;
use syscall::scheme::Scheme;
/// Input
static INPUT: Once<Mutex<VecDeque<u8>>> = Once::new();
@ -62,12 +62,12 @@ impl Scheme for DebugScheme {
Ok(buffer.len())
}
fn fsync(&self, _file: usize) -> Result<()> {
Ok(())
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
/// Close the file `number`
fn close(&self, _file: usize) -> Result<()> {
Ok(())
fn close(&self, _file: usize) -> Result<usize> {
Ok(0)
}
}

View file

@ -2,8 +2,8 @@ use collections::BTreeMap;
use core::sync::atomic::{AtomicUsize, Ordering};
use spin::RwLock;
use syscall::{Error, Result};
use super::Scheme;
use syscall::error::*;
use syscall::scheme::Scheme;
struct Handle {
data: &'static [u8],
@ -35,7 +35,7 @@ impl EnvScheme {
impl Scheme for EnvScheme {
fn open(&self, path: &[u8], _flags: usize) -> Result<usize> {
let data = self.files.get(path).ok_or(Error::NoEntry)?;
let data = self.files.get(path).ok_or(Error::new(ENOENT))?;
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.write().insert(id, Handle {
@ -49,7 +49,7 @@ impl Scheme for EnvScheme {
fn dup(&self, file: usize) -> Result<usize> {
let (data, seek) = {
let handles = self.handles.read();
let handle = handles.get(&file).ok_or(Error::BadFile)?;
let handle = handles.get(&file).ok_or(Error::new(EBADF))?;
(handle.data, handle.seek)
};
@ -64,7 +64,7 @@ impl Scheme for EnvScheme {
fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
let mut handles = self.handles.write();
let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?;
let mut handle = handles.get_mut(&file).ok_or(Error::new(EBADF))?;
let mut i = 0;
while i < buffer.len() && handle.seek < handle.data.len() {
@ -76,15 +76,11 @@ impl Scheme for EnvScheme {
Ok(i)
}
fn write(&self, _file: usize, _buffer: &[u8]) -> Result<usize> {
Err(Error::NotPermitted)
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
fn fsync(&self, _file: usize) -> Result<()> {
Ok(())
}
fn close(&self, file: usize) -> Result<()> {
self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(()))
fn close(&self, file: usize) -> Result<usize> {
self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0))
}
}

View file

@ -2,8 +2,8 @@ use collections::BTreeMap;
use core::sync::atomic::{AtomicUsize, Ordering};
use spin::RwLock;
use syscall::{Error, Result};
use super::Scheme;
use syscall::error::*;
use syscall::scheme::Scheme;
struct Handle {
data: &'static [u8],
@ -24,7 +24,8 @@ impl InitFsScheme {
files.insert(b"bin/ion", include_bytes!("../../build/userspace/ion"));
files.insert(b"bin/pcid", include_bytes!("../../build/userspace/pcid"));
files.insert(b"bin/ps2d", include_bytes!("../../build/userspace/ps2d"));
files.insert(b"etc/init.rc", b"initfs:bin/pcid\ninitfs:bin/ps2d\ninitfs:bin/ion");
files.insert(b"bin/example", include_bytes!("../../build/userspace/example"));
files.insert(b"etc/init.rc", b"initfs:bin/pcid\ninitfs:bin/ps2d\ninitfs:bin/example\ninitfs:bin/ion");
InitFsScheme {
next_id: AtomicUsize::new(0),
@ -36,7 +37,7 @@ impl InitFsScheme {
impl Scheme for InitFsScheme {
fn open(&self, path: &[u8], _flags: usize) -> Result<usize> {
let data = self.files.get(path).ok_or(Error::NoEntry)?;
let data = self.files.get(path).ok_or(Error::new(ENOENT))?;
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.write().insert(id, Handle {
@ -50,7 +51,7 @@ impl Scheme for InitFsScheme {
fn dup(&self, file: usize) -> Result<usize> {
let (data, seek) = {
let handles = self.handles.read();
let handle = handles.get(&file).ok_or(Error::BadFile)?;
let handle = handles.get(&file).ok_or(Error::new(EBADF))?;
(handle.data, handle.seek)
};
@ -65,7 +66,7 @@ impl Scheme for InitFsScheme {
fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
let mut handles = self.handles.write();
let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?;
let mut handle = handles.get_mut(&file).ok_or(Error::new(EBADF))?;
let mut i = 0;
while i < buffer.len() && handle.seek < handle.data.len() {
@ -77,15 +78,11 @@ impl Scheme for InitFsScheme {
Ok(i)
}
fn write(&self, _file: usize, _buffer: &[u8]) -> Result<usize> {
Err(Error::NotPermitted)
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
fn fsync(&self, _file: usize) -> Result<()> {
Ok(())
}
fn close(&self, file: usize) -> Result<()> {
self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(()))
fn close(&self, file: usize) -> Result<usize> {
self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0))
}
}

View file

@ -1,21 +1,21 @@
use core::{mem, str};
use arch::interrupt::irq::{ACKS, COUNTS, acknowledge};
use syscall::{Error, Result};
use super::Scheme;
use syscall::error::*;
use syscall::scheme::Scheme;
pub struct IrqScheme;
impl Scheme for IrqScheme {
fn open(&self, path: &[u8], _flags: usize) -> Result<usize> {
let path_str = str::from_utf8(path).or(Err(Error::NoEntry))?;
let path_str = str::from_utf8(path).or(Err(Error::new(ENOENT)))?;
let id = path_str.parse::<usize>().or(Err(Error::NoEntry))?;
let id = path_str.parse::<usize>().or(Err(Error::new(ENOENT)))?;
if id < COUNTS.lock().len() {
Ok(id)
} else {
Err(Error::NoEntry)
Err(Error::new(ENOENT))
}
}
@ -37,7 +37,7 @@ impl Scheme for IrqScheme {
Ok(0)
}
} else {
Err(Error::InvalidValue)
Err(Error::new(EINVAL))
}
}
@ -54,15 +54,15 @@ impl Scheme for IrqScheme {
Ok(0)
}
} else {
Err(Error::InvalidValue)
Err(Error::new(EINVAL))
}
}
fn fsync(&self, _file: usize) -> Result<()> {
Ok(())
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
fn close(&self, _file: usize) -> Result<()> {
Ok(())
fn close(&self, _file: usize) -> Result<usize> {
Ok(0)
}
}

View file

@ -13,7 +13,8 @@ use collections::BTreeMap;
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
use syscall::{Error, Result};
use syscall::error::*;
use syscall::scheme::Scheme;
use self::debug::DebugScheme;
use self::env::EnvScheme;
@ -75,7 +76,7 @@ impl SchemeList {
/// Create a new scheme.
pub fn insert(&mut self, name: Box<[u8]>, scheme: Arc<Box<Scheme + Send + Sync>>) -> Result<&Arc<Box<Scheme + Send + Sync>>> {
if self.names.contains_key(&name) {
return Err(Error::FileExists);
return Err(Error::new(EEXIST));
}
if self.next_id >= SCHEME_MAX_SCHEMES {
@ -87,7 +88,7 @@ impl SchemeList {
}
if self.next_id >= SCHEME_MAX_SCHEMES {
return Err(Error::TryAgain);
return Err(Error::new(EAGAIN));
}
let id = self.next_id;
@ -123,32 +124,3 @@ pub fn schemes() -> RwLockReadGuard<'static, SchemeList> {
pub fn schemes_mut() -> RwLockWriteGuard<'static, SchemeList> {
SCHEMES.call_once(init_schemes).write()
}
/// A scheme trait, implemented by a scheme handler
pub trait Scheme {
/// Open the file at `path` with `flags`.
///
/// Returns a file descriptor or an error
fn open(&self, path: &[u8], flags: usize) -> Result<usize>;
/// Duplicate an open file descriptor
///
/// Returns a file descriptor or an error
fn dup(&self, file: usize) -> Result<usize>;
/// Read from some file descriptor into the `buffer`
///
/// Returns the number of bytes read
fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize>;
/// Write the `buffer` to the file descriptor
///
/// Returns the number of bytes written
fn write(&self, file: usize, buffer: &[u8]) -> Result<usize>;
/// Sync the file descriptor
fn fsync(&self, file: usize) -> Result<()>;
/// Close the file descriptor
fn close(&self, file: usize) -> Result<()>;
}

View file

@ -5,8 +5,9 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use spin::RwLock;
use context;
use syscall::{Error, Result};
use scheme::{self, Scheme};
use syscall::error::*;
use syscall::scheme::Scheme;
use scheme;
use scheme::user::{UserInner, UserScheme};
pub struct RootScheme {
@ -27,14 +28,14 @@ impl Scheme for RootScheme {
fn open(&self, path: &[u8], _flags: usize) -> Result<usize> {
let context = {
let contexts = context::contexts();
let context = contexts.current().ok_or(Error::NoProcess)?;
let context = contexts.current().ok_or(Error::new(ESRCH))?;
Arc::downgrade(&context)
};
let inner = {
let mut schemes = scheme::schemes_mut();
if schemes.get_name(path).is_some() {
return Err(Error::FileExists);
return Err(Error::new(EEXIST));
}
let inner = Arc::new(UserInner::new(context));
schemes.insert(path.to_vec().into_boxed_slice(), Arc::new(Box::new(UserScheme::new(Arc::downgrade(&inner))))).expect("failed to insert user scheme");
@ -50,7 +51,7 @@ impl Scheme for RootScheme {
fn dup(&self, file: usize) -> Result<usize> {
let mut handles = self.handles.write();
let inner = {
let inner = handles.get(&file).ok_or(Error::BadFile)?;
let inner = handles.get(&file).ok_or(Error::new(EBADF))?;
inner.clone()
};
@ -63,7 +64,7 @@ impl Scheme for RootScheme {
fn read(&self, file: usize, buf: &mut [u8]) -> Result<usize> {
let inner = {
let handles = self.handles.read();
let inner = handles.get(&file).ok_or(Error::BadFile)?;
let inner = handles.get(&file).ok_or(Error::new(EBADF))?;
inner.clone()
};
@ -73,18 +74,18 @@ impl Scheme for RootScheme {
fn write(&self, file: usize, buf: &[u8]) -> Result<usize> {
let inner = {
let handles = self.handles.read();
let inner = handles.get(&file).ok_or(Error::BadFile)?;
let inner = handles.get(&file).ok_or(Error::new(EBADF))?;
inner.clone()
};
inner.write(buf)
}
fn fsync(&self, _file: usize) -> Result<()> {
Ok(())
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
fn close(&self, file: usize) -> Result<()> {
self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(()))
fn close(&self, file: usize) -> Result<usize> {
self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0))
}
}

View file

@ -9,9 +9,9 @@ use arch::paging::{InactivePageTable, Page, VirtualAddress, entry};
use arch::paging::temporary_page::TemporaryPage;
use context::{self, Context};
use context::memory::Grant;
use syscall::{convert_to_result, Call, Error, Result};
use super::Scheme;
use syscall::error::*;
use syscall::number::*;
use syscall::scheme::Scheme;
#[derive(Copy, Clone, Debug, Default)]
#[repr(packed)]
@ -40,12 +40,12 @@ impl UserInner {
}
}
pub fn call(&self, a: Call, b: usize, c: usize, d: usize) -> Result<usize> {
pub fn call(&self, a: usize, b: usize, c: usize, d: usize) -> Result<usize> {
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
let packet = Packet {
id: id,
a: a as usize,
a: a,
b: b,
c: c,
d: d
@ -57,7 +57,7 @@ impl UserInner {
{
let mut done = self.done.lock();
if let Some(a) = done.remove(&id) {
return convert_to_result(a);
return Error::demux(a);
}
}
@ -74,7 +74,7 @@ impl UserInner {
}
fn capture_inner(&self, address: usize, size: usize, writable: bool) -> Result<usize> {
let context_lock = self.context.upgrade().ok_or(Error::NoProcess)?;
let context_lock = self.context.upgrade().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let mut grants = context.grants.lock();
@ -125,7 +125,7 @@ impl UserInner {
}
pub fn release(&self, address: usize) -> Result<()> {
let context_lock = self.context.upgrade().ok_or(Error::NoProcess)?;
let context_lock = self.context.upgrade().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let mut grants = context.grants.lock();
@ -143,7 +143,7 @@ impl UserInner {
}
}
Err(Error::Fault)
Err(Error::new(EFAULT))
}
pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
@ -201,41 +201,41 @@ impl UserScheme {
impl Scheme for UserScheme {
fn open(&self, path: &[u8], flags: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
let address = inner.capture(path)?;
let result = inner.call(Call::Open, address, path.len(), flags);
let result = inner.call(SYS_OPEN, address, path.len(), flags);
let _ = inner.release(address);
result
}
fn dup(&self, file: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
inner.call(Call::Dup, file, 0, 0)
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
inner.call(SYS_DUP, file, 0, 0)
}
fn read(&self, file: usize, buf: &mut [u8]) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
let address = inner.capture_mut(buf)?;
let result = inner.call(Call::Read, file, address, buf.len());
let result = inner.call(SYS_READ, file, address, buf.len());
let _ = inner.release(address);
result
}
fn write(&self, file: usize, buf: &[u8]) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
let address = inner.capture(buf)?;
let result = inner.call(Call::Write, file, buf.as_ptr() as usize, buf.len());
let result = inner.call(SYS_WRITE, file, buf.as_ptr() as usize, buf.len());
let _ = inner.release(address);
result
}
fn fsync(&self, file: usize) -> Result<()> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
inner.call(Call::FSync, file, 0, 0).and(Ok(()))
fn fsync(&self, file: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
inner.call(SYS_FSYNC, file, 0, 0)
}
fn close(&self, file: usize) -> Result<()> {
let inner = self.inner.upgrade().ok_or(Error::NoDevice)?;
inner.call(Call::Close, file, 0, 0).and(Ok(()))
fn close(&self, file: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
inner.call(SYS_CLOSE, file, 0, 0)
}
}