refactor interrupts (#476)

This commit is contained in:
acheronfail
2018-10-18 18:08:17 +11:00
committed by Philipp Oppermann
parent ac9582bd8b
commit 68ffc3cd59
4 changed files with 111 additions and 96 deletions

View File

@@ -7,10 +7,8 @@
#[macro_use]
extern crate blog_os;
extern crate x86_64;
#[macro_use]
extern crate lazy_static;
use blog_os::interrupts::{self, PICS};
use blog_os::interrupts::PICS;
use core::panic::PanicInfo;
/// This function is the entry point, since the linker looks for a function
@@ -21,7 +19,7 @@ pub extern "C" fn _start() -> ! {
println!("Hello World{}", "!");
blog_os::gdt::init();
init_idt();
blog_os::interrupts::init_idt();
unsafe { PICS.lock().initialize() };
x86_64::instructions::interrupts::enable();
@@ -37,76 +35,3 @@ pub fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
blog_os::hlt_loop();
}
use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable};
lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
unsafe {
idt.double_fault
.set_handler_fn(double_fault_handler)
.set_stack_index(blog_os::gdt::DOUBLE_FAULT_IST_INDEX);
}
let timer_interrupt_id = usize::from(interrupts::TIMER_INTERRUPT_ID);
idt[timer_interrupt_id].set_handler_fn(timer_interrupt_handler);
let keyboard_interrupt_id = usize::from(interrupts::KEYBOARD_INTERRUPT_ID);
idt[keyboard_interrupt_id].set_handler_fn(keyboard_interrupt_handler);
idt
};
}
pub fn init_idt() {
IDT.load();
}
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut ExceptionStackFrame) {
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn double_fault_handler(
stack_frame: &mut ExceptionStackFrame,
_error_code: u64,
) {
println!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
loop {}
}
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut ExceptionStackFrame) {
print!(".");
unsafe {
PICS.lock()
.notify_end_of_interrupt(interrupts::TIMER_INTERRUPT_ID)
}
}
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut ExceptionStackFrame) {
use x86_64::instructions::port::Port;
let port = Port::new(0x60);
let scancode: u8 = unsafe { port.read() };
let key = match scancode {
0x02 => Some('1'),
0x03 => Some('2'),
0x04 => Some('3'),
0x05 => Some('4'),
0x06 => Some('5'),
0x07 => Some('6'),
0x08 => Some('7'),
0x09 => Some('8'),
0x0a => Some('9'),
0x0b => Some('0'),
_ => None,
};
if let Some(key) = key {
print!("{}", key);
}
unsafe {
PICS.lock()
.notify_end_of_interrupt(interrupts::KEYBOARD_INTERRUPT_ID)
}
}