WIP: User scheme
This commit is contained in:
parent
abdbadfea3
commit
c512d04378
5 changed files with 143 additions and 208 deletions
|
@ -33,7 +33,7 @@ pub mod initfs;
|
|||
pub mod irq;
|
||||
|
||||
/// Userspace schemes
|
||||
//pub mod user;
|
||||
pub mod user;
|
||||
|
||||
/// Limit on number of schemes
|
||||
pub const SCHEME_MAX_SCHEMES: usize = 65536;
|
||||
|
|
|
@ -1,217 +1,126 @@
|
|||
use alloc::arc::{Arc, Weak};
|
||||
use alloc::boxed::Box;
|
||||
|
||||
use collections::{BTreeMap, VecDeque};
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use core::{mem, usize};
|
||||
use spin::RwLock;
|
||||
|
||||
use core::cell::Cell;
|
||||
use core::mem::size_of;
|
||||
use core::ops::DerefMut;
|
||||
use core::{ptr, slice};
|
||||
|
||||
use syscall::{Call, Error, Result};
|
||||
use context;
|
||||
use syscall::{convert_to_result, Call, Error, Result};
|
||||
|
||||
use super::Scheme;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[repr(packed)]
|
||||
pub struct Packet {
|
||||
pub id: usize,
|
||||
pub a: usize,
|
||||
pub b: usize,
|
||||
pub c: usize,
|
||||
pub d: usize
|
||||
}
|
||||
|
||||
/// UserScheme has to be wrapped
|
||||
pub struct UserScheme {
|
||||
next_id: Cell<usize>,
|
||||
todo: VecDeque<(usize, (usize, usize, usize, usize))>,
|
||||
done: BTreeMap<usize, (usize, usize, usize, usize)>,
|
||||
next_id: AtomicUsize,
|
||||
todo: RwLock<VecDeque<Packet>>,
|
||||
done: RwLock<BTreeMap<usize, usize>>
|
||||
}
|
||||
|
||||
impl UserScheme {
|
||||
fn call(&self, a: Call, b: usize, c: usize, d: usize) -> Result<usize> {
|
||||
let id = self.next_id.get();
|
||||
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
|
||||
|
||||
//TODO: What should be done about collisions in self.todo or self.done?
|
||||
let mut next_id = id + 1;
|
||||
if next_id <= 0 {
|
||||
next_id = 1;
|
||||
self.todo.write().push_back(Packet {
|
||||
id: id,
|
||||
a: a as usize,
|
||||
b: b,
|
||||
c: c,
|
||||
d: d
|
||||
});
|
||||
|
||||
loop {
|
||||
if let Some(a) = self.done.write().remove(&id) {
|
||||
return convert_to_result(a);
|
||||
}
|
||||
|
||||
unsafe { context::switch(); }
|
||||
}
|
||||
self.next_id.set(next_id);
|
||||
|
||||
// println!("{} {}: {} {} {:X} {:X} {:X}", UserScheme.name, id, a, ::syscall::name(a), b, c, d);
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn capture(&self, mut physical_address: usize, size: usize, writeable: bool) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn release(&self, virtual_address: usize) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Scheme for UserScheme {
|
||||
fn open(&mut self, path: &[u8], flags: usize) -> Result<usize> {
|
||||
let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false));
|
||||
|
||||
let result = self.call(Call::Open, virtual_address, path.len(), flags);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result
|
||||
fn open(&self, path: &[u8], flags: usize) -> Result<usize> {
|
||||
self.call(Call::Open, path.as_ptr() as usize, path.len(), flags)
|
||||
}
|
||||
|
||||
/*
|
||||
fn mkdir(&mut self, path: &str, flags: usize) -> Result<()> {
|
||||
let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false));
|
||||
|
||||
let result = self.call(Call::MkDir, virtual_address, path.len(), flags);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result.and(Ok(()))
|
||||
}
|
||||
|
||||
fn rmdir(&mut self, path: &str) -> Result<()> {
|
||||
let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false));
|
||||
|
||||
let result = self.call(SYS_RMDIR, virtual_address, path.len(), 0);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result.and(Ok(()))
|
||||
}
|
||||
|
||||
fn unlink(&mut self, path: &str) -> Result<()> {
|
||||
let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false));
|
||||
|
||||
let result = self.call(SYS_UNLINK, virtual_address, path.len(), 0);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result.and(Ok(()))
|
||||
}
|
||||
*/
|
||||
|
||||
/// Duplicate the resource
|
||||
fn dup(&mut self, file: usize) -> Result<usize> {
|
||||
self.call(Call::Dup, file, 0, 0)
|
||||
}
|
||||
|
||||
/*
|
||||
/// Return the URL of this resource
|
||||
fn path(&self, file: usize, buf: &mut [u8]) -> Result <usize> {
|
||||
let contexts = unsafe { & *::env().contexts.get() };
|
||||
let current = try!(contexts.current());
|
||||
if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) {
|
||||
let offset = physical_address % 4096;
|
||||
|
||||
let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true));
|
||||
|
||||
let result = self.call(SYS_FPATH, file, virtual_address + offset, buf.len());
|
||||
|
||||
//println!("Read {:X} mapped from {:X} to {:X} offset {} length {} size {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), virtual_size, result);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result
|
||||
fn dup(&self, file: usize) -> Result<usize> {
|
||||
if file == usize::MAX {
|
||||
Ok(file)
|
||||
} else {
|
||||
println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len());
|
||||
Err(Error::Fault)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// Read data to buffer
|
||||
fn read(&mut self, file: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
/*
|
||||
let contexts = unsafe { & *::env().contexts.get() };
|
||||
let current = try!(contexts.current());
|
||||
if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) {
|
||||
let offset = physical_address % 4096;
|
||||
|
||||
let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true));
|
||||
|
||||
let result = self.call(Call::Read, file, virtual_address + offset, buf.len());
|
||||
|
||||
//println!("Read {:X} mapped from {:X} to {:X} offset {} length {} size {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), virtual_size, result);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result
|
||||
} else */
|
||||
{
|
||||
println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len());
|
||||
Err(Error::Fault)
|
||||
self.call(Call::Dup, file, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Write to resource
|
||||
fn write(&mut self, file: usize, buf: &[u8]) -> Result<usize> {
|
||||
/*
|
||||
let contexts = unsafe { & *::env().contexts.get() };
|
||||
let current = try!(contexts.current());
|
||||
if let Ok(physical_address) = current.translate(buf.as_ptr() as usize, buf.len()) {
|
||||
let offset = physical_address % 4096;
|
||||
fn read(&self, file: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
if file == usize::MAX {
|
||||
let packet_size = mem::size_of::<Packet>();
|
||||
let len = buf.len()/packet_size;
|
||||
if len > 0 {
|
||||
loop {
|
||||
let mut i = 0;
|
||||
{
|
||||
let mut todo = self.todo.write();
|
||||
while ! todo.is_empty() && i < len {
|
||||
unsafe { *(buf.as_mut_ptr() as *mut Packet).offset(i as isize) = todo.pop_front().unwrap(); }
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, false));
|
||||
|
||||
let result = self.call(Call::Write, file, virtual_address + offset, buf.len());
|
||||
|
||||
// println!("Write {:X} mapped from {:X} to {:X} offset {} length {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), result);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result
|
||||
} else */
|
||||
{
|
||||
println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len());
|
||||
Err(Error::Fault)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Seek
|
||||
fn seek(&mut self, file: usize, pos: ResourceSeek) -> Result<usize> {
|
||||
let (whence, offset) = match pos {
|
||||
ResourceSeek::Start(offset) => (SEEK_SET, offset as usize),
|
||||
ResourceSeek::Current(offset) => (SEEK_CUR, offset as usize),
|
||||
ResourceSeek::End(offset) => (SEEK_END, offset as usize)
|
||||
};
|
||||
|
||||
self.call(SYS_LSEEK, file, offset, whence)
|
||||
}
|
||||
|
||||
/// Stat the resource
|
||||
fn stat(&self, file: usize, stat: &mut Stat) -> Result<()> {
|
||||
let buf = unsafe { slice::from_raw_parts_mut(stat as *mut Stat as *mut u8, size_of::<Stat>()) };
|
||||
|
||||
let contexts = unsafe { & *::env().contexts.get() };
|
||||
let current = try!(contexts.current());
|
||||
if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) {
|
||||
let offset = physical_address % 4096;
|
||||
|
||||
let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true));
|
||||
|
||||
let result = self.call(SYS_FSTAT, file, virtual_address + offset, 0);
|
||||
|
||||
self.release(virtual_address);
|
||||
|
||||
result.and(Ok(()))
|
||||
if i > 0 {
|
||||
return Ok(i * packet_size);
|
||||
} else {
|
||||
unsafe { context::switch(); }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
} else {
|
||||
println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len());
|
||||
Err(Error::Fault)
|
||||
self.call(Call::Read, file, buf.as_mut_ptr() as usize, buf.len())
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// Sync the resource
|
||||
fn fsync(&mut self, file: usize) -> Result<()> {
|
||||
self.call(Call::FSync, file, 0, 0).and(Ok(()))
|
||||
fn write(&self, file: usize, buf: &[u8]) -> Result<usize> {
|
||||
if file == usize::MAX {
|
||||
let packet_size = mem::size_of::<Packet>();
|
||||
let len = buf.len()/packet_size;
|
||||
let mut i = 0;
|
||||
while i < len {
|
||||
let packet = unsafe { *(buf.as_ptr() as *const Packet).offset(i as isize) };
|
||||
self.done.write().insert(packet.id, packet.a);
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Ok(i * packet_size)
|
||||
} else {
|
||||
self.call(Call::Write, file, buf.as_ptr() as usize, buf.len())
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Truncate the resource
|
||||
fn truncate(&mut self, file: usize, len: usize) -> Result<()> {
|
||||
self.call(SYS_FTRUNCATE, file, len, 0).and(Ok(()))
|
||||
fn fsync(&self, file: usize) -> Result<()> {
|
||||
if file == usize::MAX {
|
||||
Ok(())
|
||||
} else {
|
||||
self.call(Call::FSync, file, 0, 0).and(Ok(()))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fn close(&mut self, file: usize) -> Result<()> {
|
||||
self.call(Call::Close, file, 0, 0).and(Ok(()))
|
||||
fn close(&self, file: usize) -> Result<()> {
|
||||
if file == usize::MAX {
|
||||
println!("Close user scheme");
|
||||
Ok(())
|
||||
} else {
|
||||
self.call(Call::Close, file, 0, 0).and(Ok(()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue