From f60661820d3541a05e04a401f848050e710a1bca Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 20 Sep 2016 16:23:28 -0600 Subject: [PATCH] Create example userspace scheme. Remove kernel duplication of syscalls, use syscall crate instead --- Cargo.toml | 1 + Makefile | 7 ++- drivers/pcid/src/main.rs | 19 +----- drivers/ps2d/src/controller.rs | 4 -- kernel/context/list.rs | 4 +- kernel/scheme/debug.rs | 12 ++-- kernel/scheme/env.rs | 22 +++---- kernel/scheme/initfs.rs | 25 ++++---- kernel/scheme/irq.rs | 22 +++---- kernel/scheme/mod.rs | 36 ++--------- kernel/scheme/root.rs | 23 +++---- kernel/scheme/user.rs | 46 +++++++------- kernel/syscall/call.rs | 64 ------------------- kernel/syscall/error.rs | 63 ------------------- kernel/syscall/fs.rs | 62 +++++++++--------- kernel/syscall/mod.rs | 67 +++++++++----------- kernel/syscall/process.rs | 23 +++---- kernel/syscall/validate.rs | 2 +- kernel/tests/mod.rs | 4 +- schemes/example/Cargo.toml | 6 ++ schemes/example/src/main.rs | 38 +++++++++++ syscall/src/error.rs | 1 + syscall/src/lib.rs | 111 +++++++++++++-------------------- syscall/src/number.rs | 28 +++++++++ syscall/src/scheme.rs | 98 +++++++++++++++++++++++++++++ 25 files changed, 374 insertions(+), 414 deletions(-) delete mode 100644 kernel/syscall/call.rs delete mode 100644 kernel/syscall/error.rs create mode 100644 schemes/example/Cargo.toml create mode 100644 schemes/example/src/main.rs create mode 100644 syscall/src/number.rs diff --git a/Cargo.toml b/Cargo.toml index 9817d48..097d7d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ crate-type = ["staticlib"] [dependencies] bitflags = "*" spin = "*" +syscall = { path = "syscall/" } [dependencies.goblin] git = "https://github.com/m4b/goblin.git" diff --git a/Makefile b/Makefile index fc04650..6bd76aa 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ clean: cargo clean --manifest-path ion/Cargo.toml cargo clean --manifest-path drivers/ps2d/Cargo.toml cargo clean --manifest-path drivers/pcid/Cargo.toml + cargo clean --manifest-path schemes/example/Cargo.toml rm -rf build FORCE: @@ -138,4 +139,8 @@ $(BUILD)/ps2d: drivers/ps2d/Cargo.toml drivers/ps2d/src/** $(BUILD)/libstd.rlib $(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@ strip $@ -$(BUILD)/initfs.rs: $(BUILD)/init $(BUILD)/ion $(BUILD)/pcid $(BUILD)/ps2d +$(BUILD)/example: schemes/example/Cargo.toml schemes/example/src/** $(BUILD)/libstd.rlib + $(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@ + strip $@ + +$(BUILD)/initfs.rs: $(BUILD)/init $(BUILD)/ion $(BUILD)/pcid $(BUILD)/ps2d $(BUILD)/example diff --git a/drivers/pcid/src/main.rs b/drivers/pcid/src/main.rs index 976d02a..0886bef 100644 --- a/drivers/pcid/src/main.rs +++ b/drivers/pcid/src/main.rs @@ -2,10 +2,8 @@ extern crate syscall; -use std::fs::File; -use std::io::{Read, Write}; use std::thread; -use syscall::{iopl, Packet}; +use syscall::iopl; use pci::{Pci, PciBar, PciClass}; @@ -77,20 +75,5 @@ fn main() { unsafe { iopl(3).unwrap() }; enumerate_pci(); - - let mut scheme = File::create(":pci").expect("pcid: failed to create pci scheme"); - loop { - let mut packet = Packet::default(); - scheme.read(&mut packet).expect("pcid: failed to read events from pci scheme"); - - println!("{:?}", packet); - if packet.a == 5 { - println!("{}", unsafe { ::std::str::from_utf8_unchecked(::std::slice::from_raw_parts(packet.b as *const u8, packet.c)) }); - } - - packet.a = 0; - - scheme.write(&packet).expect("pcid: failed to write responses to pci scheme"); - } }); } diff --git a/drivers/ps2d/src/controller.rs b/drivers/ps2d/src/controller.rs index 8355dba..e68d4f1 100644 --- a/drivers/ps2d/src/controller.rs +++ b/drivers/ps2d/src/controller.rs @@ -1,9 +1,5 @@ use io::{Io, Pio, ReadOnly, WriteOnly}; -pub unsafe fn init() { - Ps2::new().init(); -} - bitflags! { flags StatusFlags: u8 { const OUTPUT_FULL = 1, diff --git a/kernel/context/list.rs b/kernel/context/list.rs index 22ebbe9..2ad5efb 100644 --- a/kernel/context/list.rs +++ b/kernel/context/list.rs @@ -5,7 +5,7 @@ use core::sync::atomic::Ordering; use spin::RwLock; use arch; -use syscall::{Result, Error}; +use syscall::error::{Result, Error, EAGAIN}; use super::context::Context; /// Context list type @@ -48,7 +48,7 @@ impl ContextList { } if self.next_id >= super::CONTEXT_MAX_CONTEXTS { - return Err(Error::TryAgain); + return Err(Error::new(EAGAIN)); } let id = self.next_id; diff --git a/kernel/scheme/debug.rs b/kernel/scheme/debug.rs index a282191..70459e7 100644 --- a/kernel/scheme/debug.rs +++ b/kernel/scheme/debug.rs @@ -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>> = 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 { + Ok(0) } /// Close the file `number` - fn close(&self, _file: usize) -> Result<()> { - Ok(()) + fn close(&self, _file: usize) -> Result { + Ok(0) } } diff --git a/kernel/scheme/env.rs b/kernel/scheme/env.rs index 98c0948..8448cfe 100644 --- a/kernel/scheme/env.rs +++ b/kernel/scheme/env.rs @@ -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 { - 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 { 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 { 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 { - Err(Error::NotPermitted) + fn fsync(&self, _file: usize) -> Result { + 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 { + self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0)) } } diff --git a/kernel/scheme/initfs.rs b/kernel/scheme/initfs.rs index 186d89d..1380c6e 100644 --- a/kernel/scheme/initfs.rs +++ b/kernel/scheme/initfs.rs @@ -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 { - 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 { 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 { 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 { - Err(Error::NotPermitted) + fn fsync(&self, _file: usize) -> Result { + 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 { + self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0)) } } diff --git a/kernel/scheme/irq.rs b/kernel/scheme/irq.rs index 41928b0..7ec202a 100644 --- a/kernel/scheme/irq.rs +++ b/kernel/scheme/irq.rs @@ -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 { - 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::().or(Err(Error::NoEntry))?; + let id = path_str.parse::().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 { + Ok(0) } - fn close(&self, _file: usize) -> Result<()> { - Ok(()) + fn close(&self, _file: usize) -> Result { + Ok(0) } } diff --git a/kernel/scheme/mod.rs b/kernel/scheme/mod.rs index d53688b..4d0580a 100644 --- a/kernel/scheme/mod.rs +++ b/kernel/scheme/mod.rs @@ -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>) -> Result<&Arc>> { 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; - - /// Duplicate an open file descriptor - /// - /// Returns a file descriptor or an error - fn dup(&self, file: usize) -> Result; - - /// Read from some file descriptor into the `buffer` - /// - /// Returns the number of bytes read - fn read(&self, file: usize, buffer: &mut [u8]) -> Result; - - /// Write the `buffer` to the file descriptor - /// - /// Returns the number of bytes written - fn write(&self, file: usize, buffer: &[u8]) -> Result; - - /// Sync the file descriptor - fn fsync(&self, file: usize) -> Result<()>; - - /// Close the file descriptor - fn close(&self, file: usize) -> Result<()>; -} diff --git a/kernel/scheme/root.rs b/kernel/scheme/root.rs index 2264ade..a2e0060 100644 --- a/kernel/scheme/root.rs +++ b/kernel/scheme/root.rs @@ -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 { 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 { 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 { 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 { 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 { + 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 { + self.handles.write().remove(&file).ok_or(Error::new(EBADF)).and(Ok(0)) } } diff --git a/kernel/scheme/user.rs b/kernel/scheme/user.rs index 9825f9d..9d9ae7e 100644 --- a/kernel/scheme/user.rs +++ b/kernel/scheme/user.rs @@ -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 { + pub fn call(&self, a: usize, b: usize, c: usize, d: usize) -> Result { 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 { - 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 { @@ -201,41 +201,41 @@ impl UserScheme { impl Scheme for UserScheme { fn open(&self, path: &[u8], flags: usize) -> Result { - 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 { - 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 { - 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 { - 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 { + 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 { + let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?; + inner.call(SYS_CLOSE, file, 0, 0) } } diff --git a/kernel/syscall/call.rs b/kernel/syscall/call.rs deleted file mode 100644 index 2aa41ae..0000000 --- a/kernel/syscall/call.rs +++ /dev/null @@ -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 { - 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 - } - } -} diff --git a/kernel/syscall/error.rs b/kernel/syscall/error.rs deleted file mode 100644 index 8fcffea..0000000 --- a/kernel/syscall/error.rs +++ /dev/null @@ -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 { - 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 = ::core::result::Result; - -pub fn convert_to_result(number: usize) -> Result { - if let Some(err) = Error::from((-(number as isize)) as usize) { - Err(err) - } else { - Ok(number) - } -} diff --git a/kernel/syscall/fs.rs b/kernel/syscall/fs.rs index 15a2453..a487019 100644 --- a/kernel/syscall/fs.rs +++ b/kernel/syscall/fs.rs @@ -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 { 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 { /// Get the current working directory pub fn getcwd(buf: &mut [u8]) -> Result { 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 { pub fn open(path: &[u8], flags: usize) -> Result { 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 { 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 { }; 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 { 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 { 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 { 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 { 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 { 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) } diff --git a/kernel/syscall/mod.rs b/kernel/syscall/mod.rs index 9ec87ed..70400e7 100644 --- a/kernel/syscall/mod.rs +++ b/kernel/syscall/mod.rs @@ -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 { - //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)) } diff --git a/kernel/syscall/process.rs b/kernel/syscall/process.rs index b6be85f..3d94a11 100644 --- a/kernel/syscall/process.rs +++ b/kernel/syscall/process.rs @@ -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 { 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 { 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 { // 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 { 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 { 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 { }, 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 { pub fn getpid() -> Result { 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 { 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 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)); } } diff --git a/kernel/syscall/validate.rs b/kernel/syscall/validate.rs index 61c7384..2ba6ef7 100644 --- a/kernel/syscall/validate.rs +++ b/kernel/syscall/validate.rs @@ -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 diff --git a/kernel/tests/mod.rs b/kernel/tests/mod.rs index 3b92c18..0ad27af 100644 --- a/kernel/tests/mod.rs +++ b/kernel/tests/mod.rs @@ -24,6 +24,6 @@ fn stdio() { /// Test that invalid reads/writes cause errors #[test] fn invalid_path() { - assert_eq!(syscall::read(999, &mut []), Err(Error::BadFile)); - assert_eq!(syscall::write(999, &[]), Err(Error::BadFile)); + assert_eq!(syscall::read(999, &mut []), Err(Error::new(EBADF))); + assert_eq!(syscall::write(999, &[]), Err(Error::new(EBADF))); } diff --git a/schemes/example/Cargo.toml b/schemes/example/Cargo.toml new file mode 100644 index 0000000..68b930f --- /dev/null +++ b/schemes/example/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example" +version = "0.1.0" + +[dependencies.syscall] +path = "../../syscall/" diff --git a/schemes/example/src/main.rs b/schemes/example/src/main.rs new file mode 100644 index 0000000..9c7d3fe --- /dev/null +++ b/schemes/example/src/main.rs @@ -0,0 +1,38 @@ +extern crate syscall; + +use std::fs::File; +use std::io::{Read, Write}; +use std::str; +use std::thread; + +use syscall::{Packet, Result, Scheme}; + +struct ExampleScheme; + +impl Scheme for ExampleScheme { + fn open(&self, path: &[u8], _flags: usize) -> Result { + println!("{}", unsafe { str::from_utf8_unchecked(path) }); + Ok(0) + } + + fn dup(&self, file: usize) -> Result { + Ok(file) + } + + fn close(&self, _file: usize) -> Result { + Ok(0) + } +} + +fn main(){ + thread::spawn(move || { + let mut socket = File::create(":example").expect("example: failed to create example scheme"); + let scheme = ExampleScheme; + loop { + let mut packet = Packet::default(); + socket.read(&mut packet).expect("example: failed to read events from example scheme"); + scheme.handle(&mut packet); + socket.write(&packet).expect("example: failed to write responses to example scheme"); + } + }); +} diff --git a/syscall/src/error.rs b/syscall/src/error.rs index 9af8f4a..5481f9f 100644 --- a/syscall/src/error.rs +++ b/syscall/src/error.rs @@ -1,5 +1,6 @@ use core::{fmt, result}; +#[derive(Eq, PartialEq)] pub struct Error { pub errno: isize, } diff --git a/syscall/src/lib.rs b/syscall/src/lib.rs index 51032bb..e55ed44 100644 --- a/syscall/src/lib.rs +++ b/syscall/src/lib.rs @@ -3,6 +3,7 @@ pub use self::arch::*; pub use self::error::*; +pub use self::number::*; pub use self::scheme::*; #[cfg(target_arch = "x86")] @@ -13,74 +14,52 @@ mod arch; #[path="x86_64.rs"] mod arch; -mod error; +pub mod error; -mod scheme; +pub mod number; -pub const SYS_BRK: usize = 45; -pub const SYS_CHDIR: usize = 12; -pub const SYS_CLONE: usize = 120; - pub const CLONE_VM: usize = 0x100; - pub const CLONE_FS: usize = 0x200; - pub const CLONE_FILES: usize = 0x400; - pub const CLONE_VFORK: usize = 0x4000; - /// Mark this clone as supervised. - /// - /// This means that the process can run in supervised mode, even not being connected to - /// a supervisor yet. In other words, the parent can later on supervise the process and handle - /// the potential blocking syscall. - /// - /// This is an important security measure, since otherwise the process would be able to fork it - /// self right after starting, making supervising it impossible. - pub const CLONE_SUPERVISE: usize = 0x400000; -pub const SYS_CLOSE: usize = 6; -pub const SYS_CLOCK_GETTIME: usize = 265; - pub const CLOCK_REALTIME: usize = 1; - pub const CLOCK_MONOTONIC: usize = 4; -pub const SYS_DUP: usize = 41; -pub const SYS_EXECVE: usize = 11; -pub const SYS_EXIT: usize = 1; -pub const SYS_FPATH: usize = 928; -pub const SYS_FSTAT: usize = 28; - pub const MODE_DIR: u16 = 0x4000; - pub const MODE_FILE: u16 = 0x8000; - pub const MODE_ALL: u16 = MODE_DIR | MODE_FILE; -pub const SYS_FSYNC: usize = 118; -pub const SYS_FTRUNCATE: usize = 93; -pub const SYS_FUTEX: usize = 240; - pub const FUTEX_WAIT: usize = 0; - pub const FUTEX_WAKE: usize = 1; - pub const FUTEX_REQUEUE: usize = 2; -pub const SYS_GETCWD: usize = 183; -pub const SYS_GETPID: usize = 20; -pub const SYS_IOPL: usize = 110; -pub const SYS_LINK: usize = 9; -pub const SYS_LSEEK: usize = 19; - pub const SEEK_SET: usize = 0; - pub const SEEK_CUR: usize = 1; - pub const SEEK_END: usize = 2; -pub const SYS_MKDIR: usize = 39; -pub const SYS_NANOSLEEP: usize = 162; -pub const SYS_OPEN: usize = 5; - pub const O_RDONLY: usize = 0; - pub const O_WRONLY: usize = 1; - pub const O_RDWR: usize = 2; - pub const O_NONBLOCK: usize = 4; - pub const O_APPEND: usize = 8; - pub const O_SHLOCK: usize = 0x10; - pub const O_EXLOCK: usize = 0x20; - pub const O_ASYNC: usize = 0x40; - pub const O_FSYNC: usize = 0x80; - pub const O_CREAT: usize = 0x200; - pub const O_TRUNC: usize = 0x400; - pub const O_EXCL: usize = 0x800; -pub const SYS_PIPE2: usize = 331; -pub const SYS_READ: usize = 3; -pub const SYS_RMDIR: usize = 84; -pub const SYS_UNLINK: usize = 10; -pub const SYS_WAITPID: usize = 7; -pub const SYS_WRITE: usize = 4; -pub const SYS_YIELD: usize = 158; +pub mod scheme; + +pub const CLONE_VM: usize = 0x100; +pub const CLONE_FS: usize = 0x200; +pub const CLONE_FILES: usize = 0x400; +pub const CLONE_VFORK: usize = 0x4000; +/// Mark this clone as supervised. +/// +/// This means that the process can run in supervised mode, even not being connected to +/// a supervisor yet. In other words, the parent can later on supervise the process and handle +/// the potential blocking syscall. +/// +/// This is an important security measure, since otherwise the process would be able to fork it +/// self right after starting, making supervising it impossible. +pub const CLONE_SUPERVISE: usize = 0x400000; +pub const CLOCK_REALTIME: usize = 1; +pub const CLOCK_MONOTONIC: usize = 4; + +pub const MODE_DIR: u16 = 0x4000; +pub const MODE_FILE: u16 = 0x8000; +pub const MODE_ALL: u16 = MODE_DIR | MODE_FILE; + +pub const FUTEX_WAIT: usize = 0; +pub const FUTEX_WAKE: usize = 1; +pub const FUTEX_REQUEUE: usize = 2; + +pub const SEEK_SET: usize = 0; +pub const SEEK_CUR: usize = 1; +pub const SEEK_END: usize = 2; + +pub const O_RDONLY: usize = 0; +pub const O_WRONLY: usize = 1; +pub const O_RDWR: usize = 2; +pub const O_NONBLOCK: usize = 4; +pub const O_APPEND: usize = 8; +pub const O_SHLOCK: usize = 0x10; +pub const O_EXLOCK: usize = 0x20; +pub const O_ASYNC: usize = 0x40; +pub const O_FSYNC: usize = 0x80; +pub const O_CREAT: usize = 0x200; +pub const O_TRUNC: usize = 0x400; +pub const O_EXCL: usize = 0x800; #[derive(Copy, Clone, Debug, Default)] #[repr(packed)] diff --git a/syscall/src/number.rs b/syscall/src/number.rs new file mode 100644 index 0000000..e65c37f --- /dev/null +++ b/syscall/src/number.rs @@ -0,0 +1,28 @@ +pub const SYS_BRK: usize = 45; +pub const SYS_CHDIR: usize = 12; +pub const SYS_CLONE: usize = 120; +pub const SYS_CLOSE: usize = 6; +pub const SYS_CLOCK_GETTIME: usize = 265; +pub const SYS_DUP: usize = 41; +pub const SYS_EXECVE: usize = 11; +pub const SYS_EXIT: usize = 1; +pub const SYS_FPATH: usize = 928; +pub const SYS_FSTAT: usize = 28; +pub const SYS_FSYNC: usize = 118; +pub const SYS_FTRUNCATE: usize = 93; +pub const SYS_FUTEX: usize = 240; +pub const SYS_GETCWD: usize = 183; +pub const SYS_GETPID: usize = 20; +pub const SYS_IOPL: usize = 110; +pub const SYS_LINK: usize = 9; +pub const SYS_LSEEK: usize = 19; +pub const SYS_MKDIR: usize = 39; +pub const SYS_NANOSLEEP: usize = 162; +pub const SYS_OPEN: usize = 5; +pub const SYS_PIPE2: usize = 331; +pub const SYS_READ: usize = 3; +pub const SYS_RMDIR: usize = 84; +pub const SYS_UNLINK: usize = 10; +pub const SYS_WAITPID: usize = 7; +pub const SYS_WRITE: usize = 4; +pub const SYS_YIELD: usize = 158; diff --git a/syscall/src/scheme.rs b/syscall/src/scheme.rs index 756fc26..40405c4 100644 --- a/syscall/src/scheme.rs +++ b/syscall/src/scheme.rs @@ -1,6 +1,8 @@ use core::ops::{Deref, DerefMut}; use core::{mem, slice}; +use super::*; + #[derive(Copy, Clone, Debug, Default)] #[repr(packed)] pub struct Packet { @@ -27,3 +29,99 @@ impl DerefMut for Packet { } } } + +pub trait Scheme { + fn handle(&self, packet: &mut Packet) { + packet.a = Error::mux(match packet.a { + SYS_OPEN => self.open(unsafe { slice::from_raw_parts(packet.b as *const u8, packet.c) }, packet.d), + SYS_MKDIR => self.mkdir(unsafe { slice::from_raw_parts(packet.b as *const u8, packet.c) }, packet.d), + SYS_RMDIR => self.rmdir(unsafe { slice::from_raw_parts(packet.b as *const u8, packet.c) }), + SYS_UNLINK => self.unlink(unsafe { slice::from_raw_parts(packet.b as *const u8, packet.c) }), + + SYS_DUP => self.dup(packet.b), + SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), + SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), + SYS_LSEEK => self.seek(packet.b, packet.c, packet.d), + SYS_FPATH => self.fpath(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), + SYS_FSTAT => self.fstat(packet.b, unsafe { &mut *(packet.c as *mut Stat) }), + SYS_FSYNC => self.fsync(packet.b), + SYS_FTRUNCATE => self.ftruncate(packet.b, packet.c), + SYS_CLOSE => self.close(packet.b), + + _ => Err(Error::new(ENOSYS)) + }); + } + + /* Scheme operations */ + + #[allow(unused_variables)] + fn open(&self, path: &[u8], flags: usize) -> Result { + Err(Error::new(ENOENT)) + } + + #[allow(unused_variables)] + fn mkdir(&self, path: &[u8], mode: usize) -> Result { + Err(Error::new(ENOENT)) + } + + #[allow(unused_variables)] + fn rmdir(&self, path: &[u8]) -> Result { + Err(Error::new(ENOENT)) + } + + #[allow(unused_variables)] + fn stat(&self, path: &[u8], stat: &mut Stat) -> Result { + Err(Error::new(ENOENT)) + } + + #[allow(unused_variables)] + fn unlink(&self, path: &[u8]) -> Result { + Err(Error::new(ENOENT)) + } + + /* Resource operations */ + #[allow(unused_variables)] + fn dup(&self, old_id: usize) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn read(&self, id: usize, buf: &mut [u8]) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn write(&self, id: usize, buf: &[u8]) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn seek(&self, id: usize, pos: usize, whence: usize) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn fpath(&self, id: usize, buf: &mut [u8]) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn fstat(&self, id: usize, stat: &mut Stat) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn fsync(&self, id: usize) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn ftruncate(&self, id: usize, len: usize) -> Result { + Err(Error::new(EBADF)) + } + + #[allow(unused_variables)] + fn close(&self, id: usize) -> Result { + Err(Error::new(EBADF)) + } +}