Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on irq_handler overwrite #53

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions kernel/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
use crate::arch::{enable_irq, SpinLock};
use alloc::boxed::Box;
use core::mem::MaybeUninit;
use kerla_utils::bitmap::BitMap;

fn empty_irq_handler() {}

type IrqHandler = dyn FnMut() + Send + Sync;
const UNINITIALIZED_IRQ_HANDLER: MaybeUninit<Box<IrqHandler>> = MaybeUninit::uninit();
static IRQ_HANDLERS: SpinLock<[MaybeUninit<Box<IrqHandler>>; 256]> =
SpinLock::new([UNINITIALIZED_IRQ_HANDLER; 256]);
static ATTACHED_IRQS: SpinLock<BitMap<32 /* = 256 / 8 */>> = SpinLock::new(BitMap::zeroed());

pub fn init() {
let mut handlers = IRQ_HANDLERS.lock();
Expand All @@ -19,8 +21,16 @@ pub fn init() {
}

pub fn attach_irq<F: FnMut() + Send + Sync + 'static>(irq: u8, f: F) {
IRQ_HANDLERS.lock()[irq as usize].write(Box::new(f));
enable_irq(irq);
let mut attached_irq_map = ATTACHED_IRQS.lock();
match attached_irq_map.get(irq as usize) {
Some(true) => panic!("handler for IRQ #{} is already attached", irq),
Some(false) => {
attached_irq_map.set(irq as usize);
IRQ_HANDLERS.lock()[irq as usize].write(Box::new(f));
enable_irq(irq);
}
None => panic!("IRQ #{} is out of bound", irq),
}
}

pub fn handle_irq(irq: u8) {
Expand Down