Context list class, static context ID magic
This commit is contained in:
parent
9cd48a36a5
commit
3b8f396229
5 changed files with 87 additions and 58 deletions
|
@ -8,7 +8,7 @@ use super::{Error, Result};
|
|||
/// Read syscall
|
||||
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
println!("Read {}: {}", fd, buf.len());
|
||||
if let Some(context_lock) = context::contexts().get(&0) {
|
||||
if let Some(context_lock) = context::contexts().current() {
|
||||
let context = context_lock.read();
|
||||
if let Some(file) = context.files.get(fd) {
|
||||
println!("{:?}", file);
|
||||
|
@ -24,7 +24,7 @@ pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
|||
/// Write syscall
|
||||
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
|
||||
println!("Write {}: {}", fd, buf.len());
|
||||
if let Some(context_lock) = context::contexts().get(&0) {
|
||||
if let Some(context_lock) = context::contexts().current() {
|
||||
let context = context_lock.read();
|
||||
if let Some(file) = context.files.get(fd) {
|
||||
println!("{:?}: {:?}", file, ::core::str::from_utf8(buf));
|
||||
|
@ -57,7 +57,7 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
|||
}
|
||||
}?;
|
||||
|
||||
if let Some(context_lock) = context::contexts().get(&0) {
|
||||
if let Some(context_lock) = context::contexts().current() {
|
||||
let mut context = context_lock.write();
|
||||
if let Some(fd) = context.add_file(::context::file::File {
|
||||
scheme: 0,
|
||||
|
|
|
@ -12,73 +12,61 @@ pub mod fs;
|
|||
pub mod process;
|
||||
|
||||
/// System call list
|
||||
/// See http://syscalls.kernelgrok.com/ for numbers
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[repr(C)]
|
||||
pub enum Call {
|
||||
/// Exit syscall
|
||||
Exit,
|
||||
Exit = 1,
|
||||
/// Read syscall
|
||||
Read,
|
||||
Read = 3,
|
||||
/// Write syscall
|
||||
Write,
|
||||
Write = 4,
|
||||
/// Open syscall
|
||||
Open,
|
||||
Open = 5,
|
||||
/// Close syscall
|
||||
Close,
|
||||
Close = 6,
|
||||
/// Execute syscall
|
||||
Exec,
|
||||
/// Unknown syscall
|
||||
Unknown
|
||||
Exec = 11,
|
||||
}
|
||||
|
||||
/// Convert numbers to calls
|
||||
/// See http://syscalls.kernelgrok.com/
|
||||
impl From<usize> for Call {
|
||||
fn from(number: usize) -> Call {
|
||||
impl Call {
|
||||
fn from(number: usize) -> Result<Call> {
|
||||
match number {
|
||||
1 => Call::Exit,
|
||||
3 => Call::Read,
|
||||
4 => Call::Write,
|
||||
5 => Call::Open,
|
||||
6 => Call::Close,
|
||||
11 => Call::Exec,
|
||||
_ => Call::Unknown
|
||||
1 => Ok(Call::Exit),
|
||||
3 => Ok(Call::Read),
|
||||
4 => Ok(Call::Write),
|
||||
5 => Ok(Call::Open),
|
||||
6 => Ok(Call::Close),
|
||||
11 => Ok(Call::Exec),
|
||||
_ => Err(Error::NoCall)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
NotPermitted = 1,
|
||||
/// No such file or directory
|
||||
NoEntry,
|
||||
NoEntry = 2,
|
||||
/// No such process
|
||||
NoProcess,
|
||||
NoProcess = 3,
|
||||
/// Bad file number
|
||||
BadFile,
|
||||
BadFile = 9,
|
||||
/// Try again
|
||||
TryAgain = 11,
|
||||
/// Invalid argument
|
||||
InvalidValue,
|
||||
InvalidValue = 22,
|
||||
/// Too many open files
|
||||
TooManyFiles,
|
||||
TooManyFiles = 24,
|
||||
/// Syscall not implemented
|
||||
NoCall
|
||||
}
|
||||
|
||||
/// Convert errors to numbers
|
||||
/// See http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html
|
||||
impl From<Error> for usize {
|
||||
fn from(err: Error) -> usize {
|
||||
match err {
|
||||
Error::NotPermitted => 1,
|
||||
Error::NoEntry => 2,
|
||||
Error::NoProcess => 3,
|
||||
Error::BadFile => 9,
|
||||
Error::InvalidValue => 22,
|
||||
Error::TooManyFiles => 24,
|
||||
Error::NoCall => 38
|
||||
}
|
||||
}
|
||||
NoCall = 38
|
||||
}
|
||||
|
||||
pub type Result<T> = ::core::result::Result<T, Error>;
|
||||
|
@ -95,22 +83,21 @@ pub fn convert_slice_mut<T>(ptr: *mut T, len: usize) -> Result<&'static mut [T]>
|
|||
Ok(unsafe { slice::from_raw_parts_mut(ptr, len) })
|
||||
}
|
||||
|
||||
pub fn handle(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize) -> ::core::result::Result<usize, usize> {
|
||||
match Call::from(a) {
|
||||
pub fn handle(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize) -> Result<usize> {
|
||||
match Call::from(a)? {
|
||||
Call::Exit => exit(b),
|
||||
Call::Read => read(b, convert_slice_mut(c as *mut u8, d)?),
|
||||
Call::Write => write(b, convert_slice(c as *const u8, d)?),
|
||||
Call::Open => open(convert_slice(b as *const u8, c)?, d),
|
||||
Call::Close => close(b),
|
||||
Call::Exec => exec(convert_slice(b as *const u8, c)?, convert_slice(d as *const [usize; 2], e)?),
|
||||
Call::Unknown => Err(Error::NoCall)
|
||||
}.map_err(|err| err.into())
|
||||
Call::Exec => exec(convert_slice(b as *const u8, c)?, convert_slice(d as *const [usize; 2], e)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize) -> usize {
|
||||
match handle(a, b, c, d, e, f) {
|
||||
Ok(value) => value,
|
||||
Err(value) => !value
|
||||
Err(value) => !(value as usize)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue