From 1e7b91d3f47745322a4c9be61c1461989c323aa2 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Tue, 3 Sep 2024 23:05:18 +0200 Subject: [PATCH] add interface to determine the name of a device driver in addition, riscv64 port like the other stores irq names in a hashmap --- src/arch/riscv64/kernel/interrupts.rs | 24 ++++++++++------- src/arch/riscv64/kernel/mod.rs | 6 +++-- src/drivers/fs/virtio_fs.rs | 4 +++ src/drivers/mod.rs | 3 +++ src/drivers/net/gem.rs | 4 +++ src/drivers/net/rtl8139.rs | 9 ++++--- src/drivers/net/virtio/mod.rs | 4 +++ src/drivers/virtio/transport/mmio.rs | 6 +++-- src/drivers/virtio/transport/pci.rs | 38 ++++++++------------------- src/drivers/vsock/mod.rs | 4 +++ 10 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/arch/riscv64/kernel/interrupts.rs b/src/arch/riscv64/kernel/interrupts.rs index 5e68eb317d..00a1096fb9 100644 --- a/src/arch/riscv64/kernel/interrupts.rs +++ b/src/arch/riscv64/kernel/interrupts.rs @@ -2,7 +2,7 @@ use alloc::vec::Vec; use ahash::RandomState; use hashbrown::HashMap; -use hermit_sync::{OnceCell, SpinMutex}; +use hermit_sync::{InterruptTicketMutex, OnceCell, SpinMutex}; use riscv::asm::wfi; use riscv::register::{scause, sie, sip, sstatus, stval}; use trapframe::TrapFrame; @@ -27,7 +27,7 @@ static INTERRUPT_HANDLERS: OnceCell> = + InterruptTicketMutex::new(HashMap::with_hasher(RandomState::with_seeds(0, 0, 0, 0))); + +#[allow(dead_code)] +pub(crate) fn add_irq_name(irq_number: u8, name: &'static str) { + debug!("Register name \"{}\" for interrupt {}", name, irq_number); + IRQ_NAMES.lock().insert(irq_number, name); } /// Waits for the next interrupt (Only Supervisor-level software/timer interrupt for now) /// and calls the specific handler #[inline] -pub fn enable_and_wait() { +pub(crate) fn enable_and_wait() { unsafe { //Enable Supervisor-level software interrupts sie::set_ssoft(); @@ -108,7 +112,7 @@ pub fn enable_and_wait() { /// Disable Interrupts #[inline] -pub fn disable() { +pub(crate) fn disable() { unsafe { sstatus::clear_sie() }; } @@ -231,3 +235,5 @@ fn external_eoi() { } } } + +pub(crate) fn print_statistics() {} diff --git a/src/arch/riscv64/kernel/mod.rs b/src/arch/riscv64/kernel/mod.rs index 5adcb3376e..66b0e65d08 100644 --- a/src/arch/riscv64/kernel/mod.rs +++ b/src/arch/riscv64/kernel/mod.rs @@ -29,7 +29,7 @@ use crate::env; // Used to store information about available harts. The index of the hart in the vector // represents its CpuId and does not need to match its hart_id -pub static mut HARTS_AVAILABLE: Vec = Vec::new(); +pub(crate) static mut HARTS_AVAILABLE: Vec = Vec::new(); /// Kernel header to announce machine features static BOOT_INFO: OnceCell = OnceCell::new(); @@ -211,4 +211,6 @@ pub fn boot_next_processor() { } } -pub fn print_statistics() {} +pub fn print_statistics() { + interrupts::print_statistics(); +} diff --git a/src/drivers/fs/virtio_fs.rs b/src/drivers/fs/virtio_fs.rs index af6909daf2..e325adca81 100644 --- a/src/drivers/fs/virtio_fs.rs +++ b/src/drivers/fs/virtio_fs.rs @@ -200,6 +200,10 @@ impl Driver for VirtioFsDriver { fn get_interrupt_number(&self) -> InterruptLine { self.irq } + + fn get_name(&self) -> &'static str { + "virtio" + } } /// Error module of virtios filesystem driver. diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs index 4f0a6d6f01..0ce3719401 100644 --- a/src/drivers/mod.rs +++ b/src/drivers/mod.rs @@ -112,6 +112,9 @@ pub mod error { pub(crate) trait Driver { /// Returns the interrupt number of the device fn get_interrupt_number(&self) -> InterruptLine; + + /// Returns the device driver name + fn get_name(&self) -> &'static str; } pub(crate) fn init() { diff --git a/src/drivers/net/gem.rs b/src/drivers/net/gem.rs index 0aeff53ab4..0b951a0acc 100644 --- a/src/drivers/net/gem.rs +++ b/src/drivers/net/gem.rs @@ -406,6 +406,10 @@ impl Driver for GEMDriver { fn get_interrupt_number(&self) -> InterruptLine { self.irq } + + fn get_name(&self) -> &'static str { + "gem" + } } impl GEMDriver { diff --git a/src/drivers/net/rtl8139.rs b/src/drivers/net/rtl8139.rs index 9f9ec2b8f9..e6f38507fc 100644 --- a/src/drivers/net/rtl8139.rs +++ b/src/drivers/net/rtl8139.rs @@ -349,6 +349,10 @@ impl Driver for RTL8139Driver { fn get_interrupt_number(&self) -> InterruptLine { self.irq } + + fn get_name(&self) -> &'static str { + "rtl8139" + } } impl RTL8139Driver { @@ -571,9 +575,8 @@ pub(crate) fn init_device( ); } - // Install interrupt handler for RTL8139 - debug!("Install interrupt handler for RTL8139 at {}", irq); - add_irq_name(irq, "rtl8139_net"); + info!("RTL8139 use interrupt line {}", irq); + add_irq_name(irq, "rtl8139"); Ok(RTL8139Driver { iobase, diff --git a/src/drivers/net/virtio/mod.rs b/src/drivers/net/virtio/mod.rs index 0b8030366d..cc8fab9953 100644 --- a/src/drivers/net/virtio/mod.rs +++ b/src/drivers/net/virtio/mod.rs @@ -414,6 +414,10 @@ impl Driver for VirtioNetDriver { fn get_interrupt_number(&self) -> InterruptLine { self.irq } + + fn get_name(&self) -> &'static str { + "virtio" + } } // Backend-independent interface for Virtio network driver diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 2a308b9470..c033ca6441 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -369,8 +369,9 @@ pub(crate) fn init_device( virtio::Id::Net => match VirtioNetDriver::init(dev_id, registers, irq_no) { Ok(virt_net_drv) => { info!("Virtio network driver initialized."); - #[cfg(not(target_arch = "riscv64"))] + crate::arch::interrupts::add_irq_name(irq_no, "virtio"); + info!("Virtio interrupt handler at line {}", irq_no); Ok(VirtioDriver::Network(virt_net_drv)) } @@ -383,8 +384,9 @@ pub(crate) fn init_device( virtio::Id::Vsock => match VirtioVsockDriver::init(dev_id, registers, irq_no) { Ok(virt_net_drv) => { info!("Virtio sock driver initialized."); - #[cfg(not(target_arch = "riscv64"))] + crate::arch::interrupts::add_irq_name(irq_no, "virtio"); + info!("Virtio interrupt handler at line {}", irq_no); Ok(VirtioDriver::Vsock(virt_vsock_drv)) } diff --git a/src/drivers/virtio/transport/pci.rs b/src/drivers/virtio/transport/pci.rs index c003a0e69f..f471213255 100644 --- a/src/drivers/virtio/transport/pci.rs +++ b/src/drivers/virtio/transport/pci.rs @@ -888,11 +888,16 @@ pub(crate) fn init_device( let id = virtio::Id::from(u8::try_from(device_id - 0x1040).unwrap()); - let virt_drv = match id { + match id { #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] virtio::Id::Net => match VirtioNetDriver::init(device) { Ok(virt_net_drv) => { info!("Virtio network driver initialized."); + + let irq = device.get_irq().unwrap(); + crate::arch::interrupts::add_irq_name(irq, "virtio"); + info!("Virtio interrupt handler at line {}", irq); + Ok(VirtioDriver::Network(virt_net_drv)) } Err(virtio_error) => { @@ -907,6 +912,11 @@ pub(crate) fn init_device( virtio::Id::Vsock => match VirtioVsockDriver::init(device) { Ok(virt_sock_drv) => { info!("Virtio sock driver initialized."); + + let irq = device.get_irq().unwrap(); + crate::arch::interrupts::add_irq_name(irq, "virtio"); + info!("Virtio interrupt handler at line {}", irq); + Ok(VirtioDriver::Vsock(virt_sock_drv)) } Err(virtio_error) => { @@ -943,32 +953,6 @@ pub(crate) fn init_device( VirtioError::DevNotSupported(device_id), )) } - }; - - match virt_drv { - Ok(drv) => match &drv { - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] - VirtioDriver::Network(_) => { - let irq = device.get_irq().unwrap(); - - info!("Install virtio interrupt handler at line {}", irq); - crate::arch::interrupts::add_irq_name(irq, "virtio"); - - Ok(drv) - } - #[cfg(feature = "vsock")] - VirtioDriver::Vsock(_) => { - let irq = device.get_irq().unwrap(); - - info!("Install virtio interrupt handler at line {}", irq); - crate::arch::interrupts::add_irq_name(irq, "virtio"); - - Ok(drv) - } - #[cfg(feature = "fuse")] - VirtioDriver::FileSystem(_) => Ok(drv), - }, - Err(virt_err) => Err(virt_err), } } diff --git a/src/drivers/vsock/mod.rs b/src/drivers/vsock/mod.rs index 5b20deae59..be7fe11ee4 100644 --- a/src/drivers/vsock/mod.rs +++ b/src/drivers/vsock/mod.rs @@ -296,6 +296,10 @@ impl Driver for VirtioVsockDriver { fn get_interrupt_number(&self) -> InterruptLine { self.irq } + + fn get_name(&self) -> &'static str { + "virtio" + } } impl VirtioVsockDriver {