78 lines
2.0 KiB
Rust
78 lines
2.0 KiB
Rust
use crate::gdt;
|
|
use crate::{hlt_loop, serial_print, serial_println};
|
|
use conquer_once::spin::Lazy;
|
|
use core::convert::Into;
|
|
use pic8259::ChainedPics;
|
|
use spinning_top::Spinlock;
|
|
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
|
|
|
pub const PIC_1_OFFSET: u8 = 32;
|
|
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
#[repr(u8)]
|
|
pub enum InterruptIndex {
|
|
Timer = PIC_1_OFFSET,
|
|
Keyboard,
|
|
}
|
|
|
|
impl Into<u8> for InterruptIndex {
|
|
fn into(self) -> u8 {
|
|
self as u8
|
|
}
|
|
}
|
|
|
|
impl Into<usize> for InterruptIndex {
|
|
fn into(self) -> usize {
|
|
self as usize
|
|
}
|
|
}
|
|
|
|
pub static PICS: Spinlock<ChainedPics> =
|
|
Spinlock::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
|
|
|
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
|
|
let mut idt = InterruptDescriptorTable::new();
|
|
idt.page_fault.set_handler_fn(page_fault_handler);
|
|
idt.segment_not_present.set_handler_fn(segment_not_present);
|
|
unsafe {
|
|
idt.double_fault
|
|
.set_handler_fn(double_fault_handler)
|
|
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
|
}
|
|
|
|
idt
|
|
});
|
|
|
|
pub fn init_idt() {
|
|
IDT.load();
|
|
}
|
|
|
|
extern "x86-interrupt" fn double_fault_handler(
|
|
stack_frame: InterruptStackFrame,
|
|
_error_code: u64,
|
|
) -> ! {
|
|
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
|
}
|
|
|
|
extern "x86-interrupt" fn page_fault_handler(
|
|
stack_frame: InterruptStackFrame,
|
|
error_code: PageFaultErrorCode,
|
|
) {
|
|
use x86_64::registers::control::Cr2;
|
|
|
|
serial_print!("EXCEPTION: PAGE FAULT");
|
|
serial_println!("Accessed Address: {:?}", Cr2::read());
|
|
serial_println!("Error Code: {:?}", error_code);
|
|
serial_println!("{:#?}", stack_frame);
|
|
hlt_loop();
|
|
}
|
|
|
|
extern "x86-interrupt" fn segment_not_present(
|
|
_stack_frame: InterruptStackFrame,
|
|
_error_code: u64,
|
|
) {
|
|
// For some reason this sometimes gets thrown when running tests in qemu,
|
|
// so leave empty so the tests finish for now.
|
|
}
|