Skip to content

Commit

Permalink
add interface to determine the name of a device driver
Browse files Browse the repository at this point in the history
in addition, riscv64 port like the other stores irq names in a hashmap
  • Loading branch information
stlankes committed Sep 4, 2024
1 parent 3a761a2 commit 1e7b91d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 43 deletions.
24 changes: 15 additions & 9 deletions src/arch/riscv64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ static INTERRUPT_HANDLERS: OnceCell<HashMap<u8, InterruptHandlerQueue, RandomSta
OnceCell::new();

/// Init Interrupts
pub fn install() {
pub(crate) fn install() {
unsafe {
// Intstall trap handler
trapframe::init();
Expand All @@ -37,28 +37,32 @@ pub fn install() {
}

/// Init PLIC
pub fn init_plic(base: usize, context: u16) {
pub(crate) fn init_plic(base: usize, context: u16) {
*PLIC_BASE.lock() = base;
*PLIC_CONTEXT.lock() = context;
}

/// Enable Interrupts
#[inline]
pub fn enable() {
pub(crate) fn enable() {
unsafe {
sstatus::set_sie();
}
}

#[cfg(all(feature = "pci", feature = "tcp"))]
pub fn add_irq_name(irq_number: u8, name: &'static str) {
warn!("add_irq_name({irq_number}, {name}) called but not implemented");
static IRQ_NAMES: InterruptTicketMutex<HashMap<u8, &'static str, RandomState>> =
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();
Expand Down Expand Up @@ -108,7 +112,7 @@ pub fn enable_and_wait() {

/// Disable Interrupts
#[inline]
pub fn disable() {
pub(crate) fn disable() {
unsafe { sstatus::clear_sie() };
}

Expand Down Expand Up @@ -231,3 +235,5 @@ fn external_eoi() {
}
}
}

pub(crate) fn print_statistics() {}
6 changes: 4 additions & 2 deletions src/arch/riscv64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = Vec::new();
pub(crate) static mut HARTS_AVAILABLE: Vec<usize> = Vec::new();

/// Kernel header to announce machine features
static BOOT_INFO: OnceCell<BootInfo> = OnceCell::new();
Expand Down Expand Up @@ -211,4 +211,6 @@ pub fn boot_next_processor() {
}
}

pub fn print_statistics() {}
pub fn print_statistics() {
interrupts::print_statistics();
}
4 changes: 4 additions & 0 deletions src/drivers/fs/virtio_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/net/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src/drivers/net/rtl8139.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/net/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down
38 changes: 11 additions & 27 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/drivers/vsock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1e7b91d

Please sign in to comment.