garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/irq.rs

raw

//! Interrupts: which device asked, and whether a task switch follows.

use crate::exceptions::Frame;

pub fn handle(frame: *mut Frame) -> *mut Frame {
    let intid = crate::gic::acknowledge();
    let next = match intid {
        crate::timer::INTID => {
            crate::timer::tick();
            crate::profile::sample(frame);
            // The network's timers ride on core 0's tick.
            if crate::arch::core_id() == 0 {
                crate::drivers::net::tick();
            }
            crate::sched::preempt(frame)
        }
        crate::sched::RESCHEDULE => crate::sched::preempt(frame),
        crate::gic::SPURIOUS => return frame,
        _ if crate::drivers::interrupt(intid) => frame,
        _ => {
            crate::println!("irq: interrupt {intid} has no handler; ignored");
            frame
        }
    };
    crate::gic::end(intid);
    next
}

/// Masks IRQs on this core for the life of the guard, restoring the previous
/// mask when dropped. Its asm is a compiler barrier (no `nomem`): what the
/// guard protects is read inside it, never from a register loaded before.
pub struct Masked(u64);

impl Masked {
    pub fn new() -> Self {
        let daif: u64;
        // SAFETY: reading and setting the IRQ mask of this core.
        unsafe { core::arch::asm!("mrs {}, daif", "msr daifset, #2", out(reg) daif, options(nostack)) };
        Self(daif)
    }
}

impl Drop for Masked {
    fn drop(&mut self) {
        // SAFETY: puts back the mask `new` found.
        unsafe { core::arch::asm!("msr daif, {}", in(reg) self.0, options(nostack)) };
    }
}

/// Unmasks IRQs on this core.
pub fn enable() {
    // SAFETY: the vectors and the GIC are set up before this is called.
    unsafe { core::arch::asm!("msr daifclr, #2", options(nostack)) };
}