Change io crate & add a small shell
This commit is contained in:
@@ -3,15 +3,25 @@
|
||||
//!
|
||||
//! This module contains the low-level trap handlers for machine and supervisor
|
||||
//! modes and the syscall dispatch implementation used by user processes.
|
||||
use alloc::str;
|
||||
use alloc::{rc::Rc, str};
|
||||
use io::SeekFrom;
|
||||
use log::info;
|
||||
use shared::syscall::SysCall;
|
||||
|
||||
use crate::{
|
||||
boot::sbi::{ExtensionID, TimerFunctionID}, clear_csr, drivers::{keyboard::KBD_DRIVER, mouse::MOUSE_DRIVER}, println, process::{ExecutionContext, exit_process, sleep}, read_csr, riscv::{disable_interrupt, dump_cpu}, scheduler::SCHEDULER, set_csr, syscall, time::{IRQ_M_EXTERNAL, IRQ_M_TIMER, setup_next_timer_interrupt}, virtio::input::HANDLING_INTERRUPT, virtual_fs::{FILE_SYSTEM, VirtualFileSystem}, write_csr
|
||||
boot::sbi::{ExtensionID, TimerFunctionID},
|
||||
clear_csr,
|
||||
drivers::{keyboard::KBD_DRIVER, mouse::MOUSE_DRIVER},
|
||||
process::{ExecutionContext, exit_process, sleep},
|
||||
read_csr,
|
||||
riscv::{disable_interrupt, dump_cpu},
|
||||
scheduler::SCHEDULER,
|
||||
set_csr, syscall,
|
||||
time::{IRQ_M_EXTERNAL, IRQ_M_TIMER, setup_next_timer_interrupt},
|
||||
virtio::input::HANDLING_INTERRUPT,
|
||||
virtual_fs::{FILE_SYSTEM, VirtualFileSystem},
|
||||
write_csr,
|
||||
};
|
||||
use core::{alloc::Layout, arch::naked_asm, time::Duration};
|
||||
use core::{alloc::Layout, arch::naked_asm, cell::RefCell, time::Duration};
|
||||
|
||||
use crate::time::{setup_timer_interrupt, timer_interrupt};
|
||||
|
||||
@@ -127,14 +137,17 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
let a1: u64 = unsafe { (*interrupt_state).a[1] };
|
||||
let a2: u64 = unsafe { (*interrupt_state).a[2] };
|
||||
let a3: u64 = unsafe { (*interrupt_state).a[3] };
|
||||
let a4: u64 = unsafe { (*interrupt_state).a[4] };
|
||||
let syscall: SysCall = syscall_u64.into();
|
||||
match syscall {
|
||||
SysCall::Open => {
|
||||
let path = unsafe { str::from_raw_parts(a1 as *const u8, a2 as usize) };
|
||||
let virtual_node = unsafe { FILE_SYSTEM.open(path.as_ref()).unwrap() };
|
||||
let virtual_node = Rc::new(RefCell::new(unsafe {
|
||||
FILE_SYSTEM.open(path.as_ref()).unwrap()
|
||||
}));
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let current_process = scheduler.get_current_process();
|
||||
let current_process = scheduler.get_current_process_mut();
|
||||
|
||||
let fd = if let Some(fd) =
|
||||
current_process.fd_table.iter().position(Option::is_none)
|
||||
@@ -153,10 +166,10 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
let fd = a1;
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let current_process = scheduler.get_current_process();
|
||||
let mut vnode = current_process.fd_table[fd as usize].take().unwrap();
|
||||
let current_process = scheduler.get_current_process_mut();
|
||||
let vnode = current_process.fd_table[fd as usize].take().unwrap();
|
||||
|
||||
vnode.close();
|
||||
vnode.borrow_mut().close();
|
||||
}
|
||||
SysCall::Write => {
|
||||
let fd = a1;
|
||||
@@ -164,9 +177,9 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
unsafe { core::slice::from_raw_parts(a2 as *const u8, a3 as usize) };
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let current_process = scheduler.get_current_process();
|
||||
let current_process = scheduler.get_current_process_mut();
|
||||
let vnode = current_process.fd_table[fd as usize].as_mut().unwrap();
|
||||
let res = vnode.write(buf).unwrap();
|
||||
let res = vnode.borrow_mut().write(buf).unwrap();
|
||||
unsafe { (*interrupt_state).a[0] = res as u64 };
|
||||
}
|
||||
SysCall::Read => {
|
||||
@@ -174,11 +187,10 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
let buf =
|
||||
unsafe { core::slice::from_raw_parts_mut(a2 as *mut u8, a3 as usize) };
|
||||
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let current_process = scheduler.get_current_process();
|
||||
let current_process = scheduler.get_current_process_mut();
|
||||
let vnode = current_process.fd_table[fd as usize].as_mut().unwrap();
|
||||
let res = vnode.read(buf).unwrap();
|
||||
let res = vnode.borrow_mut().read(buf).unwrap();
|
||||
if res == 0 && !buf.is_empty() {
|
||||
loop_syscall(interrupt_state);
|
||||
scheduler.schedule(&mut interrupt_state);
|
||||
@@ -195,14 +207,22 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let current_process = scheduler.get_current_process();
|
||||
let current_process = scheduler.get_current_process_mut();
|
||||
let vnode = current_process.fd_table[fd as usize].as_mut().unwrap();
|
||||
vnode.seek(seek).unwrap();
|
||||
vnode.borrow_mut().seek(seek).unwrap();
|
||||
}
|
||||
SysCall::Spawn => {
|
||||
let path = unsafe { str::from_raw_parts(a1 as *const u8, a2 as usize) };
|
||||
let argc = a3 as isize;
|
||||
let argv = a4 as *const *const u8;
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
scheduler.create_process_from_file(path, 0, core::ptr::null());
|
||||
let current_process = scheduler.get_current_process();
|
||||
let fd = current_process.fd_table.clone();
|
||||
let new_process = scheduler.create_process_from_file(path, argc, argv);
|
||||
new_process.fd_table = fd;
|
||||
|
||||
unsafe { (*interrupt_state).a[0] = new_process.pid as u64 };
|
||||
}
|
||||
SysCall::ExecVE => {
|
||||
// let path = unsafe { str::from_raw_parts(a1 as *const u8, a2 as usize) };
|
||||
@@ -210,6 +230,16 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
// scheduler.create_process_from_file(path, &[]);
|
||||
unimplemented!("ExecVE is not implemented")
|
||||
}
|
||||
SysCall::WaitPid => {
|
||||
let pid = a1;
|
||||
|
||||
let mut scheduler = SCHEDULER.lock();
|
||||
let alive = scheduler.is_process_alive(pid);
|
||||
if alive {
|
||||
loop_syscall(interrupt_state);
|
||||
scheduler.schedule(&mut interrupt_state);
|
||||
}
|
||||
}
|
||||
SysCall::Alloc => {
|
||||
let layout = Layout::from_size_align(a1 as usize, a2 as usize).unwrap();
|
||||
// Allocate memory and put the pointer in a0
|
||||
@@ -223,14 +253,6 @@ unsafe extern "C" fn supervisor_trap_handler(
|
||||
}
|
||||
SysCall::Exit => exit_process(&mut interrupt_state),
|
||||
SysCall::NanoSleep => sleep(Duration::new(a1, a2 as u32), &mut interrupt_state),
|
||||
SysCall::WriteTemp => {
|
||||
info!("Print from user space : {}", unsafe {
|
||||
str::from_raw_parts(a1 as *const u8, a2 as usize)
|
||||
})
|
||||
}
|
||||
SysCall::WriteIntTemp => {
|
||||
info!("Print from user space int : {}, {:x}", a1, a1)
|
||||
}
|
||||
SysCall::Unimplemented => {
|
||||
unimplemented!("Syscall {syscall_u64} is not implemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user