garage44.eu/jeroen/pinnacle

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

.. /kernel/src/x86_64/irq.rs

raw

//! Interrupts on and off on this core. (Their handling comes with the
//! APIC.)

/// Masks interrupts on this core for the life of the guard, restoring the
/// previous state when dropped. A compiler barrier (no `nomem`).
pub struct Masked(u64);

impl Masked {
    pub fn new() -> Self {
        let flags: u64;
        // SAFETY: saves RFLAGS and clears IF on this core.
        unsafe { core::arch::asm!("pushfq", "pop {}", "cli", out(reg) flags) };
        Self(flags)
    }
}

/// Unmasks interrupts on this core.
pub fn enable() {
    // SAFETY: the table and the APIC are set up before this is called.
    unsafe { core::arch::asm!("sti", options(nostack)) };
}

impl Drop for Masked {
    fn drop(&mut self) {
        const IF: u64 = 1 << 9;
        if self.0 & IF != 0 {
            // SAFETY: interrupts were on when `new` found them.
            unsafe { core::arch::asm!("sti", options(nostack)) };
        }
    }
}