Skip to content

Commit 8f2fce6

Browse files
author
Serhiy Barhamon
authored
Panic on irq_handler overwrite (#53)
* Panic on irq_handler overwrite * using BitMap to track attached IRQ numbers
1 parent 997e288 commit 8f2fce6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

kernel/interrupt.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
use crate::arch::{enable_irq, SpinLock};
44
use alloc::boxed::Box;
55
use core::mem::MaybeUninit;
6+
use kerla_utils::bitmap::BitMap;
67

78
fn empty_irq_handler() {}
89

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

1416
pub fn init() {
1517
let mut handlers = IRQ_HANDLERS.lock();
@@ -19,8 +21,16 @@ pub fn init() {
1921
}
2022

2123
pub fn attach_irq<F: FnMut() + Send + Sync + 'static>(irq: u8, f: F) {
22-
IRQ_HANDLERS.lock()[irq as usize].write(Box::new(f));
23-
enable_irq(irq);
24+
let mut attached_irq_map = ATTACHED_IRQS.lock();
25+
match attached_irq_map.get(irq as usize) {
26+
Some(true) => panic!("handler for IRQ #{} is already attached", irq),
27+
Some(false) => {
28+
attached_irq_map.set(irq as usize);
29+
IRQ_HANDLERS.lock()[irq as usize].write(Box::new(f));
30+
enable_irq(irq);
31+
}
32+
None => panic!("IRQ #{} is out of bound", irq),
33+
}
2434
}
2535

2636
pub fn handle_irq(irq: u8) {

0 commit comments

Comments
 (0)