Add mouse and clean virtio

This commit is contained in:
2026-03-11 16:42:58 +01:00
parent 60ddc88b38
commit f67718c3fe
14 changed files with 453 additions and 255 deletions

View File

@@ -19,8 +19,9 @@ use embedded_alloc::LlffHeap as Heap;
use log::info;
use crate::{
draw::{Color, Draw},
io::init_log,
pci::{PciDeviceIterator, scan_pci_for_virtio_keyboard},
pci::{PciDeviceIterator, scan_virtio_devices},
riscv::enable_supervisor_interrupt,
scheduler::{SCHEDULER, idle},
user::{proc2, test},
@@ -54,7 +55,6 @@ mod vga;
mod virtio;
mod virtual_console;
mod virtual_fs;
mod volatile;
pub const HEAP_SIZE: usize = 1024 * 1024 * 32; // 32Mo RAM
#[global_allocator]
@@ -64,13 +64,56 @@ static HEAP_INITIALIZED: AtomicBool = AtomicBool::new(false);
// Usize is assumed to be an u64 in the whole kernel
const _: () = assert!(core::mem::size_of::<usize>() == core::mem::size_of::<u64>());
#[cfg(not(target_endian = "little"))]
compile_error! {"This kernel implementation assume endianness is little-endian. Some memory access like PCI could not work in big-endian."}
// 1. Allouer de la mémoire statique alignée pour la queue
static mut KBD_QUEUE: Virtqueue = unsafe { core::mem::zeroed() };
pub static mut KBD_DRIVER: Option<VirtioPciDriver> = None;
pub static mut KBD_DRIVER: VirtioPciDriver = unsafe {
VirtioPciDriver::new(
|event| {
if event.is_key() {
let event = event.as_key_event();
println!("key, {:#?}", event);
} else {
println!("key pressed, {:#?}", event);
}
},
&mut KBD_QUEUE,
)
};
pub static mut MOUSE_POSITION: (u16, u16) = (0, 0);
static mut MOUSE_QUEUE: Virtqueue = unsafe { core::mem::zeroed() };
pub static mut MOUSE_DRIVER: VirtioPciDriver = unsafe {
VirtioPciDriver::new(
|event| {
if event.is_relative() {
let event = event.as_relative_event();
vga::Vga.write_pixel_unsafe(MOUSE_POSITION.0, MOUSE_POSITION.1, Color::BLACK);
match event.code {
virtio::input::EventCodeRelative::X => {
MOUSE_POSITION.0 = (MOUSE_POSITION.0 as i32 + event.value) as u16
}
virtio::input::EventCodeRelative::Y => {
MOUSE_POSITION.1 = (MOUSE_POSITION.1 as i32 + event.value) as u16
}
_ => {}
}
vga::Vga.write_pixel_unsafe(MOUSE_POSITION.0, MOUSE_POSITION.1, Color::RED);
// println!("mouse moved relatively, {:#?}", event);
} else {
// println!("mouse moved, {:#?}", event);
}
},
&mut MOUSE_QUEUE,
)
};
#[unsafe(no_mangle)]
pub extern "C" fn supervisor_mode_entry() {
@@ -100,16 +143,25 @@ pub extern "C" fn supervisor_mode_entry() {
}
unsafe {
let pci_info = scan_pci_for_virtio_keyboard().unwrap();
KBD_DRIVER = Some(VirtioPciDriver::new(
pci_info.common_cfg,
pci_info.notify_cfg,
pci_info.isr_cfg,
pci_info.notify_multiplier,
&mut KBD_QUEUE,
));
KBD_DRIVER.as_mut().unwrap().init();
init_plic_pci(34);
let (pci_keyboard, pci_mouse) = scan_virtio_devices();
let (pci_keyboard, pci_mouse) = (pci_keyboard.unwrap(), pci_mouse.unwrap());
KBD_DRIVER.init(
pci_keyboard.common_cfg,
pci_keyboard.notify_cfg,
pci_keyboard.isr_cfg,
pci_keyboard.notify_multiplier,
);
MOUSE_DRIVER.init(
pci_mouse.common_cfg,
pci_mouse.notify_cfg,
pci_mouse.isr_cfg,
pci_mouse.notify_multiplier,
);
init_plic_pci(pci_keyboard.irq);
init_plic_pci(pci_mouse.irq);
}
idle();
}