2018-05-18 07:49:27 +04:00
|
|
|
use simple_filesystem::*;
|
2018-11-06 22:00:40 +04:00
|
|
|
use alloc::{boxed::Box, sync::Arc};
|
2018-07-14 13:41:45 +04:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-11-06 22:00:40 +04:00
|
|
|
use arch::driver::ide::IDE;
|
2018-10-26 10:56:06 +04:00
|
|
|
use spin::Mutex;
|
2018-05-18 07:49:27 +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-08-04 19:08:10 +04:00
|
|
|
.incbin "../user/user-riscv.img"
|
2018-11-06 22:00:40 +04:00
|
|
|
_user_img_end:
|
2018-08-04 19:08:10 +04:00
|
|
|
"#);
|
|
|
|
|
2018-11-06 22:00:40 +04:00
|
|
|
lazy_static! {
|
|
|
|
static ref ROOT_INODE: Arc<INode> = {
|
|
|
|
#[cfg(target_arch = "riscv32")]
|
|
|
|
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")]
|
|
|
|
let device = Box::new(IDE::new(1));
|
|
|
|
|
|
|
|
let sfs = SimpleFileSystem::open(device).expect("failed to open SFS");
|
|
|
|
sfs.root_inode()
|
2018-07-14 13:41:45 +04:00
|
|
|
};
|
2018-11-06 22:00:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shell() {
|
|
|
|
let files = ROOT_INODE.list().unwrap();
|
2018-07-16 13:31:29 +04:00
|
|
|
println!("Available programs: {:?}", files);
|
2018-05-23 20:25:27 +04:00
|
|
|
|
2018-08-06 15:04:18 +04:00
|
|
|
// Avoid stack overflow in release mode
|
|
|
|
// Equal to: `buf = Box::new([0; 64 << 12])`
|
|
|
|
use alloc::alloc::{alloc, dealloc, Layout};
|
|
|
|
const BUF_SIZE: usize = 0x40000;
|
|
|
|
let layout = Layout::from_size_align(BUF_SIZE, 0x1000).unwrap();
|
|
|
|
let buf = unsafe{ slice::from_raw_parts_mut(alloc(layout), BUF_SIZE) };
|
2018-07-16 13:31:29 +04:00
|
|
|
loop {
|
2018-07-16 18:02:03 +04:00
|
|
|
print!(">> ");
|
2018-07-16 13:31:29 +04:00
|
|
|
use console::get_line;
|
|
|
|
let name = get_line();
|
|
|
|
if name == "" {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-06 22:00:40 +04:00
|
|
|
if let Ok(file) = ROOT_INODE.lookup(name.as_str()) {
|
2018-07-16 20:23:02 +04:00
|
|
|
use process::*;
|
2018-11-06 22:00:40 +04:00
|
|
|
let len = file.read_at(0, &mut *buf).unwrap();
|
2018-10-25 20:49:19 +04:00
|
|
|
let pid = processor().manager().add(ContextImpl::new_user(&buf[..len]));
|
|
|
|
processor().manager().wait(thread::current().id(), pid);
|
|
|
|
processor().yield_now();
|
2018-07-16 13:31:29 +04:00
|
|
|
} else {
|
|
|
|
println!("Program not exist");
|
|
|
|
}
|
2018-05-23 20:25:27 +04:00
|
|
|
}
|
2018-08-06 15:04:18 +04:00
|
|
|
unsafe { dealloc(buf.as_mut_ptr(), layout) };
|
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-05-23 20:25:27 +04:00
|
|
|
use core::slice;
|
2018-05-18 07:49:27 +04:00
|
|
|
|
2018-07-14 13:41:45 +04:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2018-11-06 22:00:40 +04:00
|
|
|
impl BlockedDevice for 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
|
|
|
}
|
|
|
|
}
|