Minimize locking in schemes. Reenable pcid and ion launch in init. WIP: Userspace schemes
This commit is contained in:
		
							parent
							
								
									94ad63de11
								
							
						
					
					
						commit
						abdbadfea3
					
				
					 9 changed files with 367 additions and 113 deletions
				
			
		| 
						 | 
					@ -23,18 +23,18 @@ pub extern fn debug_input(b: u8) {
 | 
				
			||||||
pub struct DebugScheme;
 | 
					pub struct DebugScheme;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Scheme for DebugScheme {
 | 
					impl Scheme for DebugScheme {
 | 
				
			||||||
    fn open(&mut self, _path: &[u8], _flags: usize) -> Result<usize> {
 | 
					    fn open(&self, _path: &[u8], _flags: usize) -> Result<usize> {
 | 
				
			||||||
        Ok(0)
 | 
					        Ok(0)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn dup(&mut self, _file: usize) -> Result<usize> {
 | 
					    fn dup(&self, _file: usize) -> Result<usize> {
 | 
				
			||||||
        Ok(0)
 | 
					        Ok(0)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Read the file `number` into the `buffer`
 | 
					    /// Read the file `number` into the `buffer`
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns the number of bytes read
 | 
					    /// Returns the number of bytes read
 | 
				
			||||||
    fn read(&mut self, _file: usize, buf: &mut [u8]) -> Result<usize> {
 | 
					    fn read(&self, _file: usize, buf: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
        loop {
 | 
					        loop {
 | 
				
			||||||
            let mut i = 0;
 | 
					            let mut i = 0;
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
| 
						 | 
					@ -56,18 +56,18 @@ impl Scheme for DebugScheme {
 | 
				
			||||||
    /// Write the `buffer` to the `file`
 | 
					    /// Write the `buffer` to the `file`
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns the number of bytes written
 | 
					    /// Returns the number of bytes written
 | 
				
			||||||
    fn write(&mut self, _file: usize, buffer: &[u8]) -> Result<usize> {
 | 
					    fn write(&self, _file: usize, buffer: &[u8]) -> Result<usize> {
 | 
				
			||||||
        //TODO: Write bytes, do not convert to str
 | 
					        //TODO: Write bytes, do not convert to str
 | 
				
			||||||
        print!("{}", unsafe { str::from_utf8_unchecked(buffer) });
 | 
					        print!("{}", unsafe { str::from_utf8_unchecked(buffer) });
 | 
				
			||||||
        Ok(buffer.len())
 | 
					        Ok(buffer.len())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn fsync(&mut self, _file: usize) -> Result<()> {
 | 
					    fn fsync(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Close the file `number`
 | 
					    /// Close the file `number`
 | 
				
			||||||
    fn close(&mut self, _file: usize) -> Result<()> {
 | 
					    fn close(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,6 @@
 | 
				
			||||||
use collections::BTreeMap;
 | 
					use collections::BTreeMap;
 | 
				
			||||||
 | 
					use core::sync::atomic::{AtomicUsize, Ordering};
 | 
				
			||||||
 | 
					use spin::RwLock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use syscall::{Error, Result};
 | 
					use syscall::{Error, Result};
 | 
				
			||||||
use super::Scheme;
 | 
					use super::Scheme;
 | 
				
			||||||
| 
						 | 
					@ -9,9 +11,9 @@ struct Handle {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct EnvScheme {
 | 
					pub struct EnvScheme {
 | 
				
			||||||
    next_id: usize,
 | 
					    next_id: AtomicUsize,
 | 
				
			||||||
    files: BTreeMap<&'static [u8], &'static [u8]>,
 | 
					    files: BTreeMap<&'static [u8], &'static [u8]>,
 | 
				
			||||||
    handles: BTreeMap<usize, Handle>
 | 
					    handles: RwLock<BTreeMap<usize, Handle>>
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl EnvScheme {
 | 
					impl EnvScheme {
 | 
				
			||||||
| 
						 | 
					@ -24,20 +26,19 @@ impl EnvScheme {
 | 
				
			||||||
        files.insert(b"LINES", b"30");
 | 
					        files.insert(b"LINES", b"30");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        EnvScheme {
 | 
					        EnvScheme {
 | 
				
			||||||
            next_id: 0,
 | 
					            next_id: AtomicUsize::new(0),
 | 
				
			||||||
            files: files,
 | 
					            files: files,
 | 
				
			||||||
            handles: BTreeMap::new()
 | 
					            handles: RwLock::new(BTreeMap::new())
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Scheme for EnvScheme {
 | 
					impl Scheme for EnvScheme {
 | 
				
			||||||
    fn open(&mut self, path: &[u8], _flags: usize) -> Result<usize> {
 | 
					    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::NoEntry)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let id = self.next_id;
 | 
					        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
 | 
				
			||||||
        self.next_id += 1;
 | 
					        self.handles.write().insert(id, Handle {
 | 
				
			||||||
        self.handles.insert(id, Handle {
 | 
					 | 
				
			||||||
            data: data,
 | 
					            data: data,
 | 
				
			||||||
            seek: 0
 | 
					            seek: 0
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -45,15 +46,15 @@ impl Scheme for EnvScheme {
 | 
				
			||||||
        Ok(id)
 | 
					        Ok(id)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn dup(&mut self, file: usize) -> Result<usize> {
 | 
					    fn dup(&self, file: usize) -> Result<usize> {
 | 
				
			||||||
        let (data, seek) = {
 | 
					        let (data, seek) = {
 | 
				
			||||||
            let handle = self.handles.get(&file).ok_or(Error::BadFile)?;
 | 
					            let handles = self.handles.read();
 | 
				
			||||||
 | 
					            let handle = handles.get(&file).ok_or(Error::BadFile)?;
 | 
				
			||||||
            (handle.data, handle.seek)
 | 
					            (handle.data, handle.seek)
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let id = self.next_id;
 | 
					        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
 | 
				
			||||||
        self.next_id += 1;
 | 
					        self.handles.write().insert(id, Handle {
 | 
				
			||||||
        self.handles.insert(id, Handle {
 | 
					 | 
				
			||||||
            data: data,
 | 
					            data: data,
 | 
				
			||||||
            seek: seek
 | 
					            seek: seek
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -61,8 +62,9 @@ impl Scheme for EnvScheme {
 | 
				
			||||||
        Ok(id)
 | 
					        Ok(id)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
					    fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
        let mut handle = self.handles.get_mut(&file).ok_or(Error::BadFile)?;
 | 
					        let mut handles = self.handles.write();
 | 
				
			||||||
 | 
					        let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let mut i = 0;
 | 
					        let mut i = 0;
 | 
				
			||||||
        while i < buffer.len() && handle.seek < handle.data.len() {
 | 
					        while i < buffer.len() && handle.seek < handle.data.len() {
 | 
				
			||||||
| 
						 | 
					@ -74,15 +76,15 @@ impl Scheme for EnvScheme {
 | 
				
			||||||
        Ok(i)
 | 
					        Ok(i)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn write(&mut self, _file: usize, _buffer: &[u8]) -> Result<usize> {
 | 
					    fn write(&self, _file: usize, _buffer: &[u8]) -> Result<usize> {
 | 
				
			||||||
        Err(Error::NotPermitted)
 | 
					        Err(Error::NotPermitted)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn fsync(&mut self, _file: usize) -> Result<()> {
 | 
					    fn fsync(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn close(&mut self, file: usize) -> Result<()> {
 | 
					    fn close(&self, file: usize) -> Result<()> {
 | 
				
			||||||
        self.handles.remove(&file).ok_or(Error::BadFile).and(Ok(()))
 | 
					        self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(()))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,6 @@
 | 
				
			||||||
use collections::BTreeMap;
 | 
					use collections::BTreeMap;
 | 
				
			||||||
 | 
					use core::sync::atomic::{AtomicUsize, Ordering};
 | 
				
			||||||
 | 
					use spin::RwLock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use syscall::{Error, Result};
 | 
					use syscall::{Error, Result};
 | 
				
			||||||
use super::Scheme;
 | 
					use super::Scheme;
 | 
				
			||||||
| 
						 | 
					@ -9,9 +11,9 @@ struct Handle {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct InitFsScheme {
 | 
					pub struct InitFsScheme {
 | 
				
			||||||
    next_id: usize,
 | 
					    next_id: AtomicUsize,
 | 
				
			||||||
    files: BTreeMap<&'static [u8], &'static [u8]>,
 | 
					    files: BTreeMap<&'static [u8], &'static [u8]>,
 | 
				
			||||||
    handles: BTreeMap<usize, Handle>
 | 
					    handles: RwLock<BTreeMap<usize, Handle>>
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl InitFsScheme {
 | 
					impl InitFsScheme {
 | 
				
			||||||
| 
						 | 
					@ -22,23 +24,22 @@ impl InitFsScheme {
 | 
				
			||||||
        files.insert(b"bin/ion", include_bytes!("../../build/userspace/ion"));
 | 
					        files.insert(b"bin/ion", include_bytes!("../../build/userspace/ion"));
 | 
				
			||||||
        files.insert(b"bin/pcid", include_bytes!("../../build/userspace/pcid"));
 | 
					        files.insert(b"bin/pcid", include_bytes!("../../build/userspace/pcid"));
 | 
				
			||||||
        files.insert(b"bin/ps2d", include_bytes!("../../build/userspace/ps2d"));
 | 
					        files.insert(b"bin/ps2d", include_bytes!("../../build/userspace/ps2d"));
 | 
				
			||||||
        files.insert(b"etc/init.rc", b"#initfs:bin/pcid\ninitfs:bin/ps2d\n#initfs:bin/ion");
 | 
					        files.insert(b"etc/init.rc", b"initfs:bin/pcid\ninitfs:bin/ps2d\ninitfs:bin/ion");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        InitFsScheme {
 | 
					        InitFsScheme {
 | 
				
			||||||
            next_id: 0,
 | 
					            next_id: AtomicUsize::new(0),
 | 
				
			||||||
            files: files,
 | 
					            files: files,
 | 
				
			||||||
            handles: BTreeMap::new()
 | 
					            handles: RwLock::new(BTreeMap::new())
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Scheme for InitFsScheme {
 | 
					impl Scheme for InitFsScheme {
 | 
				
			||||||
    fn open(&mut self, path: &[u8], _flags: usize) -> Result<usize> {
 | 
					    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::NoEntry)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let id = self.next_id;
 | 
					        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
 | 
				
			||||||
        self.next_id += 1;
 | 
					        self.handles.write().insert(id, Handle {
 | 
				
			||||||
        self.handles.insert(id, Handle {
 | 
					 | 
				
			||||||
            data: data,
 | 
					            data: data,
 | 
				
			||||||
            seek: 0
 | 
					            seek: 0
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -46,15 +47,15 @@ impl Scheme for InitFsScheme {
 | 
				
			||||||
        Ok(id)
 | 
					        Ok(id)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn dup(&mut self, file: usize) -> Result<usize> {
 | 
					    fn dup(&self, file: usize) -> Result<usize> {
 | 
				
			||||||
        let (data, seek) = {
 | 
					        let (data, seek) = {
 | 
				
			||||||
            let handle = self.handles.get(&file).ok_or(Error::BadFile)?;
 | 
					            let handles = self.handles.read();
 | 
				
			||||||
 | 
					            let handle = handles.get(&file).ok_or(Error::BadFile)?;
 | 
				
			||||||
            (handle.data, handle.seek)
 | 
					            (handle.data, handle.seek)
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let id = self.next_id;
 | 
					        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
 | 
				
			||||||
        self.next_id += 1;
 | 
					        self.handles.write().insert(id, Handle {
 | 
				
			||||||
        self.handles.insert(id, Handle {
 | 
					 | 
				
			||||||
            data: data,
 | 
					            data: data,
 | 
				
			||||||
            seek: seek
 | 
					            seek: seek
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -62,8 +63,9 @@ impl Scheme for InitFsScheme {
 | 
				
			||||||
        Ok(id)
 | 
					        Ok(id)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
					    fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
        let mut handle = self.handles.get_mut(&file).ok_or(Error::BadFile)?;
 | 
					        let mut handles = self.handles.write();
 | 
				
			||||||
 | 
					        let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let mut i = 0;
 | 
					        let mut i = 0;
 | 
				
			||||||
        while i < buffer.len() && handle.seek < handle.data.len() {
 | 
					        while i < buffer.len() && handle.seek < handle.data.len() {
 | 
				
			||||||
| 
						 | 
					@ -75,15 +77,15 @@ impl Scheme for InitFsScheme {
 | 
				
			||||||
        Ok(i)
 | 
					        Ok(i)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn write(&mut self, _file: usize, _buffer: &[u8]) -> Result<usize> {
 | 
					    fn write(&self, _file: usize, _buffer: &[u8]) -> Result<usize> {
 | 
				
			||||||
        Err(Error::NotPermitted)
 | 
					        Err(Error::NotPermitted)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn fsync(&mut self, _file: usize) -> Result<()> {
 | 
					    fn fsync(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn close(&mut self, file: usize) -> Result<()> {
 | 
					    fn close(&self, file: usize) -> Result<()> {
 | 
				
			||||||
        self.handles.remove(&file).ok_or(Error::BadFile).and(Ok(()))
 | 
					        self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(()))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,7 @@ use super::Scheme;
 | 
				
			||||||
pub struct IrqScheme;
 | 
					pub struct IrqScheme;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Scheme for IrqScheme {
 | 
					impl Scheme for IrqScheme {
 | 
				
			||||||
    fn open(&mut self, path: &[u8], _flags: usize) -> Result<usize> {
 | 
					    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::NoEntry))?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let id = path_str.parse::<usize>().or(Err(Error::NoEntry))?;
 | 
					        let id = path_str.parse::<usize>().or(Err(Error::NoEntry))?;
 | 
				
			||||||
| 
						 | 
					@ -19,11 +19,11 @@ impl Scheme for IrqScheme {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn dup(&mut self, file: usize) -> Result<usize> {
 | 
					    fn dup(&self, file: usize) -> Result<usize> {
 | 
				
			||||||
        Ok(file)
 | 
					        Ok(file)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
					    fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
        // Ensures that the length of the buffer is larger than the size of a usize
 | 
					        // Ensures that the length of the buffer is larger than the size of a usize
 | 
				
			||||||
        if buffer.len() >= mem::size_of::<usize>() {
 | 
					        if buffer.len() >= mem::size_of::<usize>() {
 | 
				
			||||||
            let ack = ACKS.lock()[file];
 | 
					            let ack = ACKS.lock()[file];
 | 
				
			||||||
| 
						 | 
					@ -41,7 +41,7 @@ impl Scheme for IrqScheme {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize> {
 | 
					    fn write(&self, file: usize, buffer: &[u8]) -> Result<usize> {
 | 
				
			||||||
        if buffer.len() >= mem::size_of::<usize>() {
 | 
					        if buffer.len() >= mem::size_of::<usize>() {
 | 
				
			||||||
            assert!(buffer.len() >= mem::size_of::<usize>());
 | 
					            assert!(buffer.len() >= mem::size_of::<usize>());
 | 
				
			||||||
            let ack = unsafe { *(buffer.as_ptr() as *const usize) };
 | 
					            let ack = unsafe { *(buffer.as_ptr() as *const usize) };
 | 
				
			||||||
| 
						 | 
					@ -58,11 +58,11 @@ impl Scheme for IrqScheme {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn fsync(&mut self, _file: usize) -> Result<()> {
 | 
					    fn fsync(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn close(&mut self, _file: usize) -> Result<()> {
 | 
					    fn close(&self, _file: usize) -> Result<()> {
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,7 +11,7 @@ use alloc::boxed::Box;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use collections::BTreeMap;
 | 
					use collections::BTreeMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use spin::{Once, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
 | 
					use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use syscall::{Error, Result};
 | 
					use syscall::{Error, Result};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,12 +32,15 @@ pub mod initfs;
 | 
				
			||||||
/// IRQ handling
 | 
					/// IRQ handling
 | 
				
			||||||
pub mod irq;
 | 
					pub mod irq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Userspace schemes
 | 
				
			||||||
 | 
					//pub mod user;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Limit on number of schemes
 | 
					/// Limit on number of schemes
 | 
				
			||||||
pub const SCHEME_MAX_SCHEMES: usize = 65536;
 | 
					pub const SCHEME_MAX_SCHEMES: usize = 65536;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Scheme list type
 | 
					/// Scheme list type
 | 
				
			||||||
pub struct SchemeList {
 | 
					pub struct SchemeList {
 | 
				
			||||||
    map: BTreeMap<usize, Arc<Mutex<Box<Scheme + Send>>>>,
 | 
					    map: BTreeMap<usize, Arc<Box<Scheme + Send + Sync>>>,
 | 
				
			||||||
    names: BTreeMap<Box<[u8]>, usize>,
 | 
					    names: BTreeMap<Box<[u8]>, usize>,
 | 
				
			||||||
    next_id: usize
 | 
					    next_id: usize
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -53,11 +56,11 @@ impl SchemeList {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Get the nth scheme.
 | 
					    /// Get the nth scheme.
 | 
				
			||||||
    pub fn get(&self, id: usize) -> Option<&Arc<Mutex<Box<Scheme + Send>>>> {
 | 
					    pub fn get(&self, id: usize) -> Option<&Arc<Box<Scheme + Send + Sync>>> {
 | 
				
			||||||
        self.map.get(&id)
 | 
					        self.map.get(&id)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn get_name(&self, name: &[u8]) -> Option<(usize, &Arc<Mutex<Box<Scheme + Send>>>)> {
 | 
					    pub fn get_name(&self, name: &[u8]) -> Option<(usize, &Arc<Box<Scheme + Send + Sync>>)> {
 | 
				
			||||||
        if let Some(&id) = self.names.get(name) {
 | 
					        if let Some(&id) = self.names.get(name) {
 | 
				
			||||||
            self.get(id).map(|scheme| (id, scheme))
 | 
					            self.get(id).map(|scheme| (id, scheme))
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
| 
						 | 
					@ -66,7 +69,7 @@ impl SchemeList {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create a new scheme.
 | 
					    /// Create a new scheme.
 | 
				
			||||||
    pub fn insert(&mut self, name: Box<[u8]>, scheme: Arc<Mutex<Box<Scheme + Send>>>) -> Result<&Arc<Mutex<Box<Scheme + Send>>>> {
 | 
					    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) {
 | 
					        if self.names.contains_key(&name) {
 | 
				
			||||||
            return Err(Error::FileExists);
 | 
					            return Err(Error::FileExists);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -99,10 +102,10 @@ static SCHEMES: Once<RwLock<SchemeList>> = Once::new();
 | 
				
			||||||
/// Initialize schemes, called if needed
 | 
					/// Initialize schemes, called if needed
 | 
				
			||||||
fn init_schemes() -> RwLock<SchemeList> {
 | 
					fn init_schemes() -> RwLock<SchemeList> {
 | 
				
			||||||
    let mut list: SchemeList = SchemeList::new();
 | 
					    let mut list: SchemeList = SchemeList::new();
 | 
				
			||||||
    list.insert(Box::new(*b"debug"), Arc::new(Mutex::new(Box::new(DebugScheme)))).expect("failed to insert debug: scheme");
 | 
					    list.insert(Box::new(*b"debug"), Arc::new(Box::new(DebugScheme))).expect("failed to insert debug: scheme");
 | 
				
			||||||
    list.insert(Box::new(*b"env"), Arc::new(Mutex::new(Box::new(EnvScheme::new())))).expect("failed to insert env: scheme");
 | 
					    list.insert(Box::new(*b"env"), Arc::new(Box::new(EnvScheme::new()))).expect("failed to insert env: scheme");
 | 
				
			||||||
    list.insert(Box::new(*b"initfs"), Arc::new(Mutex::new(Box::new(InitFsScheme::new())))).expect("failed to insert initfs: scheme");
 | 
					    list.insert(Box::new(*b"initfs"), Arc::new(Box::new(InitFsScheme::new()))).expect("failed to insert initfs: scheme");
 | 
				
			||||||
    list.insert(Box::new(*b"irq"), Arc::new(Mutex::new(Box::new(IrqScheme)))).expect("failed to insert irq: scheme");
 | 
					    list.insert(Box::new(*b"irq"), Arc::new(Box::new(IrqScheme))).expect("failed to insert irq: scheme");
 | 
				
			||||||
    RwLock::new(list)
 | 
					    RwLock::new(list)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -121,26 +124,26 @@ pub trait Scheme {
 | 
				
			||||||
    /// Open the file at `path` with `flags`.
 | 
					    /// Open the file at `path` with `flags`.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns a file descriptor or an error
 | 
					    /// Returns a file descriptor or an error
 | 
				
			||||||
    fn open(&mut self, path: &[u8], flags: usize) -> Result<usize>;
 | 
					    fn open(&self, path: &[u8], flags: usize) -> Result<usize>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Duplicate an open file descriptor
 | 
					    /// Duplicate an open file descriptor
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns a file descriptor or an error
 | 
					    /// Returns a file descriptor or an error
 | 
				
			||||||
    fn dup(&mut self, file: usize) -> Result<usize>;
 | 
					    fn dup(&self, file: usize) -> Result<usize>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Read from some file descriptor into the `buffer`
 | 
					    /// Read from some file descriptor into the `buffer`
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns the number of bytes read
 | 
					    /// Returns the number of bytes read
 | 
				
			||||||
    fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result<usize>;
 | 
					    fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Write the `buffer` to the file descriptor
 | 
					    /// Write the `buffer` to the file descriptor
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Returns the number of bytes written
 | 
					    /// Returns the number of bytes written
 | 
				
			||||||
    fn write(&mut self, file: usize, buffer: &[u8]) -> Result<usize>;
 | 
					    fn write(&self, file: usize, buffer: &[u8]) -> Result<usize>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Sync the file descriptor
 | 
					    /// Sync the file descriptor
 | 
				
			||||||
    fn fsync(&mut self, file: usize) -> Result<()>;
 | 
					    fn fsync(&self, file: usize) -> Result<()>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Close the file descriptor
 | 
					    /// Close the file descriptor
 | 
				
			||||||
    fn close(&mut self, file: usize) -> Result<()>;
 | 
					    fn close(&self, file: usize) -> Result<()>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										217
									
								
								kernel/scheme/user.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										217
									
								
								kernel/scheme/user.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,217 @@
 | 
				
			||||||
 | 
					use alloc::arc::{Arc, Weak};
 | 
				
			||||||
 | 
					use alloc::boxed::Box;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use collections::{BTreeMap, VecDeque};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use core::cell::Cell;
 | 
				
			||||||
 | 
					use core::mem::size_of;
 | 
				
			||||||
 | 
					use core::ops::DerefMut;
 | 
				
			||||||
 | 
					use core::{ptr, slice};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use syscall::{Call, Error, Result};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use super::Scheme;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// 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)>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl UserScheme {
 | 
				
			||||||
 | 
					    fn call(&self, a: Call, b: usize, c: usize, d: usize) -> Result<usize> {
 | 
				
			||||||
 | 
					        let id = self.next_id.get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //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.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 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
 | 
				
			||||||
 | 
					        } 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)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// 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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            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(()))
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len());
 | 
				
			||||||
 | 
					            Err(Error::Fault)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Sync the resource
 | 
				
			||||||
 | 
					    fn fsync(&mut self, file: usize) -> Result<()> {
 | 
				
			||||||
 | 
					        self.call(Call::FSync, file, 0, 0).and(Ok(()))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*
 | 
				
			||||||
 | 
					    /// Truncate the resource
 | 
				
			||||||
 | 
					    fn truncate(&mut self, file: usize, len: usize) -> Result<()> {
 | 
				
			||||||
 | 
					        self.call(SYS_FTRUNCATE, file, len, 0).and(Ok(()))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn close(&mut self, file: usize) -> Result<()> {
 | 
				
			||||||
 | 
					        self.call(Call::Close, file, 0, 0).and(Ok(()))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -15,12 +15,18 @@ pub enum Error {
 | 
				
			||||||
    BadFile = 9,
 | 
					    BadFile = 9,
 | 
				
			||||||
    /// Try again
 | 
					    /// Try again
 | 
				
			||||||
    TryAgain = 11,
 | 
					    TryAgain = 11,
 | 
				
			||||||
 | 
					    /// Bad address
 | 
				
			||||||
 | 
					    Fault = 14,
 | 
				
			||||||
    /// File exists
 | 
					    /// File exists
 | 
				
			||||||
    FileExists = 17,
 | 
					    FileExists = 17,
 | 
				
			||||||
 | 
					    /// No such device
 | 
				
			||||||
 | 
					    NoDevice = 19,
 | 
				
			||||||
    /// Invalid argument
 | 
					    /// Invalid argument
 | 
				
			||||||
    InvalidValue = 22,
 | 
					    InvalidValue = 22,
 | 
				
			||||||
    /// Too many open files
 | 
					    /// Too many open files
 | 
				
			||||||
    TooManyFiles = 24,
 | 
					    TooManyFiles = 24,
 | 
				
			||||||
 | 
					    /// Illegal seek
 | 
				
			||||||
 | 
					    IllegalSeek = 29,
 | 
				
			||||||
    /// Syscall not implemented
 | 
					    /// Syscall not implemented
 | 
				
			||||||
    NoCall = 38
 | 
					    NoCall = 38
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,7 @@ use scheme;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use super::{Error, Result};
 | 
					use super::{Error, Result};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Change the current working directory
 | 
				
			||||||
pub fn chdir(path: &[u8]) -> Result<usize> {
 | 
					pub fn chdir(path: &[u8]) -> Result<usize> {
 | 
				
			||||||
    let contexts = context::contexts();
 | 
					    let contexts = context::contexts();
 | 
				
			||||||
    let context_lock = contexts.current().ok_or(Error::NoProcess)?;
 | 
					    let context_lock = contexts.current().ok_or(Error::NoProcess)?;
 | 
				
			||||||
| 
						 | 
					@ -14,6 +15,7 @@ pub fn chdir(path: &[u8]) -> Result<usize> {
 | 
				
			||||||
    Ok(0)
 | 
					    Ok(0)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get the current working directory
 | 
				
			||||||
pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
 | 
					pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
    let contexts = context::contexts();
 | 
					    let contexts = context::contexts();
 | 
				
			||||||
    let context_lock = contexts.current().ok_or(Error::NoProcess)?;
 | 
					    let context_lock = contexts.current().ok_or(Error::NoProcess)?;
 | 
				
			||||||
| 
						 | 
					@ -27,38 +29,6 @@ pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
 | 
				
			||||||
    Ok(i)
 | 
					    Ok(i)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// 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 = context_lock.read();
 | 
					 | 
				
			||||||
        let file = context.get_file(fd).ok_or(Error::BadFile)?;
 | 
					 | 
				
			||||||
        file
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let schemes = scheme::schemes();
 | 
					 | 
				
			||||||
    let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					 | 
				
			||||||
    let result = scheme_mutex.lock().read(file.number, buf);
 | 
					 | 
				
			||||||
    result
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/// 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 = context_lock.read();
 | 
					 | 
				
			||||||
        let file = context.get_file(fd).ok_or(Error::BadFile)?;
 | 
					 | 
				
			||||||
        file
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let schemes = scheme::schemes();
 | 
					 | 
				
			||||||
    let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					 | 
				
			||||||
    let result = scheme_mutex.lock().write(file.number, buf);
 | 
					 | 
				
			||||||
    result
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/// Open syscall
 | 
					/// Open syscall
 | 
				
			||||||
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
 | 
					pub fn open(path: &[u8], flags: usize) -> Result<usize> {
 | 
				
			||||||
    let path_canon = {
 | 
					    let path_canon = {
 | 
				
			||||||
| 
						 | 
					@ -74,9 +44,12 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let (scheme_id, file_id) = {
 | 
					    let (scheme_id, file_id) = {
 | 
				
			||||||
        let namespace = namespace_opt.ok_or(Error::NoEntry)?;
 | 
					        let namespace = namespace_opt.ok_or(Error::NoEntry)?;
 | 
				
			||||||
        let schemes = scheme::schemes();
 | 
					        let (scheme_id, scheme) = {
 | 
				
			||||||
        let (scheme_id, scheme_mutex) = schemes.get_name(namespace).ok_or(Error::NoEntry)?;
 | 
					            let schemes = scheme::schemes();
 | 
				
			||||||
        let file_id = scheme_mutex.lock().open(reference_opt.unwrap_or(b""), flags)?;
 | 
					            let (scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::NoEntry)?;
 | 
				
			||||||
 | 
					            (scheme_id, scheme.clone())
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        let file_id = scheme.open(reference_opt.unwrap_or(b""), flags)?;
 | 
				
			||||||
        (scheme_id, file_id)
 | 
					        (scheme_id, file_id)
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -99,9 +72,12 @@ pub fn close(fd: usize) -> Result<usize> {
 | 
				
			||||||
        file
 | 
					        file
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let schemes = scheme::schemes();
 | 
					    let scheme = {
 | 
				
			||||||
    let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					        let schemes = scheme::schemes();
 | 
				
			||||||
    let result = scheme_mutex.lock().close(file.number).and(Ok(0));
 | 
					        let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        scheme.clone()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    let result = scheme.close(file.number).and(Ok(0));
 | 
				
			||||||
    result
 | 
					    result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -115,12 +91,16 @@ pub fn dup(fd: usize) -> Result<usize> {
 | 
				
			||||||
        file
 | 
					        file
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let schemes = scheme::schemes();
 | 
					    let scheme = {
 | 
				
			||||||
    let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					        let schemes = scheme::schemes();
 | 
				
			||||||
    let result = scheme_mutex.lock().dup(file.number);
 | 
					        let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        scheme.clone()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    let result = scheme.dup(file.number);
 | 
				
			||||||
    result
 | 
					    result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Sync the file descriptor
 | 
				
			||||||
pub fn fsync(fd: usize) -> Result<usize> {
 | 
					pub fn fsync(fd: usize) -> Result<usize> {
 | 
				
			||||||
    let file = {
 | 
					    let file = {
 | 
				
			||||||
        let contexts = context::contexts();
 | 
					        let contexts = context::contexts();
 | 
				
			||||||
| 
						 | 
					@ -130,8 +110,49 @@ pub fn fsync(fd: usize) -> Result<usize> {
 | 
				
			||||||
        file
 | 
					        file
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let schemes = scheme::schemes();
 | 
					    let scheme = {
 | 
				
			||||||
    let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					        let schemes = scheme::schemes();
 | 
				
			||||||
    let result = scheme_mutex.lock().fsync(file.number).and(Ok(0));
 | 
					        let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        scheme.clone()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    let result = scheme.fsync(file.number).and(Ok(0));
 | 
				
			||||||
 | 
					    result
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// 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 = context_lock.read();
 | 
				
			||||||
 | 
					        let file = context.get_file(fd).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        file
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let scheme = {
 | 
				
			||||||
 | 
					        let schemes = scheme::schemes();
 | 
				
			||||||
 | 
					        let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        scheme.clone()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    let result = scheme.read(file.number, buf);
 | 
				
			||||||
 | 
					    result
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// 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 = context_lock.read();
 | 
				
			||||||
 | 
					        let file = context.get_file(fd).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        file
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let scheme = {
 | 
				
			||||||
 | 
					        let schemes = scheme::schemes();
 | 
				
			||||||
 | 
					        let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					        scheme.clone()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    let result = scheme.write(file.number, buf);
 | 
				
			||||||
    result
 | 
					    result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -178,9 +178,12 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
 | 
				
			||||||
                for (fd, file_option) in context.files.lock().iter().enumerate() {
 | 
					                for (fd, file_option) in context.files.lock().iter().enumerate() {
 | 
				
			||||||
                    if let Some(file) = *file_option {
 | 
					                    if let Some(file) = *file_option {
 | 
				
			||||||
                        let result = {
 | 
					                        let result = {
 | 
				
			||||||
                            let schemes = scheme::schemes();
 | 
					                            let scheme = {
 | 
				
			||||||
                            let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
					                                let schemes = scheme::schemes();
 | 
				
			||||||
                            let result = scheme_mutex.lock().dup(file.number);
 | 
					                                let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?;
 | 
				
			||||||
 | 
					                                scheme.clone()
 | 
				
			||||||
 | 
					                            };
 | 
				
			||||||
 | 
					                            let result = scheme.dup(file.number);
 | 
				
			||||||
                            result
 | 
					                            result
 | 
				
			||||||
                        };
 | 
					                        };
 | 
				
			||||||
                        match result {
 | 
					                        match result {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue