From babf9d8cce0ccf9731a2c682f38b2e8ab80c57a8 Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 12 Feb 2019 18:28:24 +0000 Subject: [PATCH] Introduce an InterruptIndex enum (#557) The following modifications aim to group the hardware interrupts' indexes in an easily accessible structure, while being more friendly to eventual evolutions. * the hardware interrupts' indexes `TIMER_INTERRUPT_ID` and `KEYBOARD_INTERRUPT_ID` have been replaced by the attributes `Timer` and `Keyboard` contained in `enum InterruptIndex`. * only the first attribute `Timer` is explicitly declared, the following as inferred by the compiler. * the functions `as_u8` and `as_usize` avoid the need of casts to `u8` or `usize`. --- src/interrupts.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/interrupts.rs b/src/interrupts.rs index ce21fa29..bb866a77 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -13,8 +13,17 @@ use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable}; pub const PIC_1_OFFSET: u8 = 32; pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; -pub const TIMER_INTERRUPT_ID: u8 = PIC_1_OFFSET; -pub const KEYBOARD_INTERRUPT_ID: u8 = PIC_1_OFFSET + 1; +#[derive(Debug, Clone, Copy)] +#[repr(u8)] +pub enum InterruptIndex { + Timer = PIC_1_OFFSET, + Keyboard, +} + +impl InterruptIndex { + fn as_u8(self) -> u8 { self as u8 } + fn as_usize(self) -> usize { usize::from(self.as_u8()) } +} pub static PICS: spin::Mutex = spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) }); @@ -28,8 +37,8 @@ lazy_static! { .set_handler_fn(double_fault_handler) .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); } - idt[usize::from(TIMER_INTERRUPT_ID)].set_handler_fn(timer_interrupt_handler); - idt[usize::from(KEYBOARD_INTERRUPT_ID)].set_handler_fn(keyboard_interrupt_handler); + idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler); + idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler); idt }; } @@ -52,7 +61,7 @@ extern "x86-interrupt" fn double_fault_handler( extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut ExceptionStackFrame) { print!("."); - unsafe { PICS.lock().notify_end_of_interrupt(TIMER_INTERRUPT_ID) } + unsafe { PICS.lock().notify_end_of_interrupt(InterruptIndex::Timer.as_u8()) } } extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut ExceptionStackFrame) { @@ -78,5 +87,5 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut Exceptio } } - unsafe { PICS.lock().notify_end_of_interrupt(KEYBOARD_INTERRUPT_ID) } + unsafe { PICS.lock().notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8()) } }