Better virtual file system, keyboard through MMIO&VirtIO

This commit is contained in:
2026-03-05 14:41:28 +01:00
parent 041e544330
commit 9b6aec28f5
37 changed files with 1191 additions and 355 deletions

View File

@@ -4,18 +4,22 @@
//! 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 io::SeekFrom;
use log::info;
use shared::syscall::SysCall;
use crate::{
KBD_DRIVER,
boot::sbi::{EextensionID, TimerFunctionID},
clear_csr,
process::{exit_process, sleep, ExecutionContext},
clear_csr, println,
process::{ExecutionContext, exit_process, sleep},
read_csr,
riscv::disable_interrupt,
scheduler::SCHEDULER,
set_csr, syscall,
time::{setup_next_timer_interrupt, IRQ_M_TIMER},
time::{IRQ_M_EXTERNAL, IRQ_M_TIMER, setup_next_timer_interrupt},
virtio::input::S_MODE_CLAIM_COMPLETE,
virtual_fs::{FILE_SYSTEM, VirtualFileSystem},
write_csr,
};
use core::{alloc::Layout, arch::naked_asm, time::Duration};
@@ -89,6 +93,9 @@ unsafe extern "C" fn machine_trap_handler(
setup_next_timer_interrupt();
set_csr!(mip, 1 << 5);
}
11 => {
set_csr!(mip, 1 << 9);
}
_ => {}
}
}
@@ -123,7 +130,7 @@ unsafe extern "C" fn supervisor_trap_handler(
match syscall {
SysCall::Open => {
let path = unsafe { str::from_raw_parts(a1 as *const u8, a2 as usize) };
let virtual_node = syscall::open(path, false).unwrap();
let virtual_node = unsafe { FILE_SYSTEM.open(path.as_ref()).unwrap() };
let mut scheduler = SCHEDULER.lock();
let current_process = scheduler.get_current_process();
@@ -132,6 +139,29 @@ unsafe extern "C" fn supervisor_trap_handler(
current_process.next_fd += 1;
unsafe { (*interrupt_state).a[0] = fd };
}
SysCall::Write => {
let fd = a1;
let buf =
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 vnode = current_process.fd_table.get_mut(&fd).unwrap();
vnode.write(buf).unwrap();
}
SysCall::Seek => {
let fd = a1;
let seek = match a2 {
0 => SeekFrom::Start(a3),
1 => SeekFrom::End(a3 as i64),
2 => SeekFrom::Current(a3 as i64),
_ => unimplemented!(),
};
let mut scheduler = SCHEDULER.lock();
let current_process = scheduler.get_current_process();
let vnode = current_process.fd_table.get_mut(&fd).unwrap();
vnode.seek(seek).unwrap();
}
SysCall::Alloc => {
let layout = Layout::from_size_align(a1 as usize, a2 as usize).unwrap();
// Allocate memory and put the pointer in a0
@@ -176,6 +206,20 @@ unsafe extern "C" fn supervisor_trap_handler(
timer_interrupt();
SCHEDULER.lock().schedule(&mut interrupt_state);
}
9 => {
println!("click");
let irq = core::ptr::read_volatile(S_MODE_CLAIM_COMPLETE);
if irq != 0 {
// ... Traiter l'interruption VirtIO ici ...
// 2. Écrire l'IRQ (Complete) <--- INDISPENSABLE
core::ptr::write_volatile(S_MODE_CLAIM_COMPLETE, irq);
KBD_DRIVER.handle_interrupt();
} else {
panic!()
}
}
_ => {}
}
}
@@ -186,11 +230,13 @@ unsafe extern "C" fn supervisor_trap_handler(
pub unsafe fn setup_machine_trap_handler() {
write_csr!(mtvec, _machine_mode_trap);
set_csr!(mie, IRQ_M_TIMER);
set_csr!(mie, IRQ_M_EXTERNAL);
}
/// Install the supervisor-mode trap entry point and configure periodic timer.
pub unsafe fn setup_supervisor_trap_handler() {
write_csr!(stvec, _supervisor_mode_trap);
set_csr!(sie, 1 << 9);
setup_timer_interrupt();
}