2018-05-18 07:49:27 +04:00
|
|
|
use simple_filesystem::*;
|
2018-11-15 21:22:47 +04:00
|
|
|
use alloc::{boxed::Box, sync::Arc, string::String, collections::VecDeque, vec::Vec};
|
2018-11-11 19:08:59 +04:00
|
|
|
use core::any::Any;
|
2018-11-15 20:49:42 +04:00
|
|
|
use core::slice;
|
2018-11-19 13:21:06 +04:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
use crate::arch::driver::ide;
|
|
|
|
use crate::sync::Condvar;
|
|
|
|
use crate::sync::SpinNoIrqLock as Mutex;
|
2018-11-05 16:37:05 +04:00
|
|
|
|
2018-08-04 19:08:10 +04:00
|
|
|
// Hard link user program
|
|
|
|
#[cfg(target_arch = "riscv32")]
|
|
|
|
global_asm!(r#"
|
|
|
|
.section .rodata
|
|
|
|
.align 12
|
2018-11-06 22:00:40 +04:00
|
|
|
.global _user_img_start
|
|
|
|
.global _user_img_end
|
|
|
|
_user_img_start:
|
2018-11-22 14:06:39 +04:00
|
|
|
.incbin "../user/img/ucore-rv32.img"
|
2018-11-06 22:00:40 +04:00
|
|
|
_user_img_end:
|
2018-08-04 19:08:10 +04:00
|
|
|
"#);
|
|
|
|
|
2018-11-20 14:29:51 +04:00
|
|
|
// Hard link user program
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
global_asm!(r#"
|
|
|
|
.section .rodata
|
|
|
|
.align 12
|
|
|
|
.global _user_img_start
|
|
|
|
.global _user_img_end
|
|
|
|
_user_img_start:
|
2018-11-30 09:36:17 +04:00
|
|
|
.incbin "../user/user-aarch64.img"
|
2018-11-20 14:29:51 +04:00
|
|
|
_user_img_end:
|
2018-08-04 19:08:10 +04:00
|
|
|
"#);
|
|
|
|
|
2018-11-06 22:00:40 +04:00
|
|
|
lazy_static! {
|
2018-11-07 09:34:31 +04:00
|
|
|
pub static ref ROOT_INODE: Arc<INode> = {
|
2018-12-01 20:51:52 +04:00
|
|
|
#[cfg(any(target_arch = "riscv32", target_arch = "aarch64"))]
|
2018-11-06 22:00:40 +04:00
|
|
|
let device = {
|
|
|
|
extern {
|
|
|
|
fn _user_img_start();
|
|
|
|
fn _user_img_end();
|
|
|
|
}
|
|
|
|
Box::new(unsafe { MemBuf::new(_user_img_start, _user_img_end) })
|
|
|
|
};
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-11-07 09:34:31 +04:00
|
|
|
let device = Box::new(ide::IDE::new(1));
|
2018-11-14 14:36:47 +04:00
|
|
|
|
2018-11-06 22:00:40 +04:00
|
|
|
let sfs = SimpleFileSystem::open(device).expect("failed to open SFS");
|
|
|
|
sfs.root_inode()
|
2018-07-14 13:41:45 +04:00
|
|
|
};
|
2018-05-23 20:25:27 +04:00
|
|
|
}
|
|
|
|
|
2018-05-18 07:49:27 +04:00
|
|
|
struct MemBuf(&'static [u8]);
|
|
|
|
|
|
|
|
impl MemBuf {
|
|
|
|
unsafe fn new(begin: unsafe extern fn(), end: unsafe extern fn()) -> Self {
|
|
|
|
use core::slice;
|
|
|
|
MemBuf(slice::from_raw_parts(begin as *const u8, end as usize - begin as usize))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Device for MemBuf {
|
|
|
|
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
|
|
|
|
let slice = self.0;
|
|
|
|
let len = buf.len().min(slice.len() - offset);
|
|
|
|
buf[..len].copy_from_slice(&slice[offset..offset + len]);
|
|
|
|
Some(len)
|
|
|
|
}
|
|
|
|
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:41:45 +04:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-11-07 09:34:31 +04:00
|
|
|
impl BlockedDevice for ide::IDE {
|
2018-07-15 12:44:21 +04:00
|
|
|
const BLOCK_SIZE_LOG2: u8 = 9;
|
2018-05-23 20:25:27 +04:00
|
|
|
fn read_at(&mut self, block_id: usize, buf: &mut [u8]) -> bool {
|
|
|
|
assert!(buf.len() >= ide::BLOCK_SIZE);
|
|
|
|
let buf = unsafe { slice::from_raw_parts_mut(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
|
2018-11-06 22:00:40 +04:00
|
|
|
self.read(block_id as u64, 1, buf).is_ok()
|
2018-05-23 20:25:27 +04:00
|
|
|
}
|
|
|
|
fn write_at(&mut self, block_id: usize, buf: &[u8]) -> bool {
|
|
|
|
assert!(buf.len() >= ide::BLOCK_SIZE);
|
|
|
|
let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
|
2018-11-06 22:00:40 +04:00
|
|
|
self.write(block_id as u64, 1, buf).is_ok()
|
2018-05-18 07:49:27 +04:00
|
|
|
}
|
2018-11-11 19:08:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Stdin {
|
|
|
|
buf: Mutex<VecDeque<char>>,
|
|
|
|
pushed: Condvar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdin {
|
|
|
|
pub fn push(&self, c: char) {
|
|
|
|
self.buf.lock().push_back(c);
|
|
|
|
self.pushed.notify_one();
|
|
|
|
}
|
|
|
|
pub fn pop(&self) -> char {
|
2018-12-01 13:37:37 +04:00
|
|
|
// QEMU v3.0 don't support M-mode external interrupt (bug?)
|
|
|
|
// So we have to use polling.
|
2018-12-02 15:51:16 +04:00
|
|
|
// TODO: serial interrupt on aarch64
|
|
|
|
#[cfg(any(feature = "m_mode", target_arch = "aarch64"))]
|
2018-12-01 13:37:37 +04:00
|
|
|
loop {
|
|
|
|
let c = crate::arch::io::getchar();
|
|
|
|
if c != '\0' { return c; }
|
|
|
|
}
|
2018-12-02 15:51:16 +04:00
|
|
|
#[cfg(not(any(feature = "m_mode", target_arch = "aarch64")))]
|
2018-11-11 19:08:59 +04:00
|
|
|
loop {
|
|
|
|
let ret = self.buf.lock().pop_front();
|
|
|
|
match ret {
|
|
|
|
Some(c) => return c,
|
|
|
|
None => self.pushed._wait(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Stdout;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref STDIN: Arc<Stdin> = Arc::new(Stdin::default());
|
|
|
|
pub static ref STDOUT: Arc<Stdout> = Arc::new(Stdout::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: better way to provide default impl?
|
|
|
|
macro_rules! impl_inode {
|
|
|
|
() => {
|
|
|
|
fn info(&self) -> Result<FileInfo> { unimplemented!() }
|
|
|
|
fn sync(&self) -> Result<()> { unimplemented!() }
|
|
|
|
fn resize(&self, len: usize) -> Result<()> { unimplemented!() }
|
|
|
|
fn create(&self, name: &str, type_: FileType) -> Result<Arc<INode>> { unimplemented!() }
|
|
|
|
fn unlink(&self, name: &str) -> Result<()> { unimplemented!() }
|
|
|
|
fn link(&self, name: &str, other: &Arc<INode>) -> Result<()> { unimplemented!() }
|
|
|
|
fn rename(&self, old_name: &str, new_name: &str) -> Result<()> { unimplemented!() }
|
|
|
|
fn move_(&self, old_name: &str, target: &Arc<INode>, new_name: &str) -> Result<()> { unimplemented!() }
|
|
|
|
fn find(&self, name: &str) -> Result<Arc<INode>> { unimplemented!() }
|
|
|
|
fn get_entry(&self, id: usize) -> Result<String> { unimplemented!() }
|
|
|
|
fn fs(&self) -> Arc<FileSystem> { unimplemented!() }
|
|
|
|
fn as_any_ref(&self) -> &Any { self }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl INode for Stdin {
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
buf[0] = self.pop() as u8;
|
|
|
|
Ok(1)
|
|
|
|
}
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> { unimplemented!() }
|
|
|
|
impl_inode!();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl INode for Stdout {
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { unimplemented!() }
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> {
|
|
|
|
use core::str;
|
|
|
|
let s = str::from_utf8(buf).map_err(|_| ())?;
|
|
|
|
print!("{}", s);
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
impl_inode!();
|
2018-11-15 21:22:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait INodeExt {
|
|
|
|
fn read_as_vec(&self) -> Result<Vec<u8>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl INodeExt for INode {
|
|
|
|
fn read_as_vec(&self) -> Result<Vec<u8>> {
|
|
|
|
let size = self.info()?.size;
|
|
|
|
let mut buf = Vec::with_capacity(size);
|
|
|
|
unsafe { buf.set_len(size); }
|
|
|
|
self.read_at(0, buf.as_mut_slice())?;
|
|
|
|
Ok(buf)
|
2018-05-18 07:49:27 +04:00
|
|
|
}
|
2018-10-29 09:41:25 +04:00
|
|
|
}
|