From ec22f3464c87b3d1b1688898053a569bc4839404 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Mon, 11 Nov 2019 18:42:59 +0100 Subject: Add expect fault handling --- kernel/src/interrupts/exceptions.rs | 66 ++++++++++++++++++++++++++----------- kernel/src/interrupts/idt.rs | 26 +++++++++++++++ kernel/src/interrupts/mod.rs | 14 ++++++-- kernel/src/interrupts/table.rs | 26 --------------- kernel/src/lib.rs | 2 +- 5 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 kernel/src/interrupts/idt.rs delete mode 100644 kernel/src/interrupts/table.rs (limited to 'kernel/src') diff --git a/kernel/src/interrupts/exceptions.rs b/kernel/src/interrupts/exceptions.rs index 1e7680a..f6d5176 100644 --- a/kernel/src/interrupts/exceptions.rs +++ b/kernel/src/interrupts/exceptions.rs @@ -1,3 +1,5 @@ +use super::Interrupts; +use crate::io::qemu; use x86_64::structures::idt::{InterruptStackFrame, PageFaultErrorCode}; macro_rules! exception_default { @@ -11,11 +13,26 @@ macro_rules! exception_default { }; } +static mut expected_fault: Interrupts = Interrupts::NONE; + +pub unsafe fn expect_fault(int: Interrupts) { + if expected_fault == Interrupts::NONE { + expected_fault = int; + } +} + +fn get_expected_fault() -> Interrupts { + unsafe { expected_fault } +} + pub extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) { - panic!( - "BREAKPOINT exception thrown\n\n{}", - exception_default!(stack_frame) - ); + match get_expected_fault() { + Interrupts::BREAKPOINT => qemu::exit_qemu(qemu::QemuExitCode::Success), + _ => panic!( + "BREAKPOINT exception thrown\n\n{}", + exception_default!(stack_frame) + ), + } } pub extern "x86-interrupt" fn page_fault_handler( @@ -24,32 +41,41 @@ pub extern "x86-interrupt" fn page_fault_handler( ) { use x86_64::registers::control::Cr2; - panic!( - "PAGE FAULT while accessing address: 0x{:08x}\nerror code: {:?}(0x{:x})\n{}", - Cr2::read().as_u64(), - error_code, - error_code.bits(), - exception_default!(stack_frame) - ); + match get_expected_fault() { + Interrupts::PAGE_FAULT => qemu::exit_qemu(qemu::QemuExitCode::Success), + _ => panic!( + "PAGE FAULT while accessing address: 0x{:08x}\nerror code: {:?}(0x{:x})\n{}", + Cr2::read().as_u64(), + error_code, + error_code.bits(), + exception_default!(stack_frame) + ), + } } pub extern "x86-interrupt" fn segment_not_present_handler( stack_frame: &mut InterruptStackFrame, error_code: u64, ) { - panic!( - "SEGMENT NOT PRESENT exception\nerror code: 0x{:x}\n{}", - error_code, - exception_default!(stack_frame) - ); + match get_expected_fault() { + Interrupts::NOT_PRESENT => qemu::exit_qemu(qemu::QemuExitCode::Success), + _ => panic!( + "SEGMENT NOT PRESENT exception\nerror code: 0x{:x}\n{}", + error_code, + exception_default!(stack_frame) + ), + } } pub extern "x86-interrupt" fn double_fault_handler( stack_frame: &mut InterruptStackFrame, _error_code: u64, // code is always zero ) { - panic!( - "DOUBLE FAULT\nthis is a fatal exception\n{}", - exception_default!(stack_frame) - ); + match get_expected_fault() { + Interrupts::NOT_PRESENT => qemu::exit_qemu(qemu::QemuExitCode::Success), + _ => panic!( + "DOUBLE FAULT\nthis is a fatal exception\n{}", + exception_default!(stack_frame) + ), + } } diff --git a/kernel/src/interrupts/idt.rs b/kernel/src/interrupts/idt.rs new file mode 100644 index 0000000..9541f22 --- /dev/null +++ b/kernel/src/interrupts/idt.rs @@ -0,0 +1,26 @@ +use lazy_static::lazy_static; +use x86_64::structures::idt::InterruptDescriptorTable; + +lazy_static! { + static ref IDT: InterruptDescriptorTable = { + let mut idt = InterruptDescriptorTable::new(); + idt.breakpoint + .set_handler_fn(super::exceptions::breakpoint_handler); + idt.segment_not_present + .set_handler_fn(super::exceptions::segment_not_present_handler); + unsafe { + idt.double_fault + .set_handler_fn(super::exceptions::double_fault_handler) + .set_stack_index(super::gdt::DOUBLE_FAULT_IST_INDEX); + idt.page_fault + .set_handler_fn(super::exceptions::page_fault_handler) + .set_stack_index(super::gdt::PAGE_FAULT_IST_INDEX); + } + + idt + }; +} + +pub fn init() { + IDT.load() +} diff --git a/kernel/src/interrupts/mod.rs b/kernel/src/interrupts/mod.rs index 14de3f2..4ff3c4d 100644 --- a/kernel/src/interrupts/mod.rs +++ b/kernel/src/interrupts/mod.rs @@ -1,3 +1,13 @@ -mod exceptions; +pub mod exceptions; pub mod gdt; -pub mod table; +pub mod idt; + +#[repr(usize)] +#[derive(PartialEq, Clone, Copy)] +pub enum Interrupts { + NONE, + PAGE_FAULT, + DOUBLE_FAULT, + NOT_PRESENT, + BREAKPOINT, +} diff --git a/kernel/src/interrupts/table.rs b/kernel/src/interrupts/table.rs deleted file mode 100644 index 9541f22..0000000 --- a/kernel/src/interrupts/table.rs +++ /dev/null @@ -1,26 +0,0 @@ -use lazy_static::lazy_static; -use x86_64::structures::idt::InterruptDescriptorTable; - -lazy_static! { - static ref IDT: InterruptDescriptorTable = { - let mut idt = InterruptDescriptorTable::new(); - idt.breakpoint - .set_handler_fn(super::exceptions::breakpoint_handler); - idt.segment_not_present - .set_handler_fn(super::exceptions::segment_not_present_handler); - unsafe { - idt.double_fault - .set_handler_fn(super::exceptions::double_fault_handler) - .set_stack_index(super::gdt::DOUBLE_FAULT_IST_INDEX); - idt.page_fault - .set_handler_fn(super::exceptions::page_fault_handler) - .set_stack_index(super::gdt::PAGE_FAULT_IST_INDEX); - } - - idt - }; -} - -pub fn init() { - IDT.load() -} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 52d41ed..8ab660d 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -32,5 +32,5 @@ fn panic(info: &core::panic::PanicInfo) -> ! { pub fn init() { interrupts::gdt::init(); - interrupts::table::init(); + interrupts::idt::init(); } -- cgit v1.2.3-70-g09d2