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/Cargo.toml | 9 +++-- 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 +- kernel/tests/int3.rs | 16 ++++----- kernel/tests/stack_overflow.rs | 11 ++++--- 8 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 kernel/src/interrupts/idt.rs delete mode 100644 kernel/src/interrupts/table.rs diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index a2ce08c..1baefdb 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -5,8 +5,13 @@ authors = ["Dennis Kobert ", "Jan Zwerschke"] edition = "2018" -# [lib] -# crate-type = ["staticlib"] +[[test]] +name = "int3" +harness = false + +[[test]] +name = "stack_overflow" +harness = false [package.metadata.cargo-xbuild] sysroot_path = "target/sysroot" 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(); } diff --git a/kernel/tests/int3.rs b/kernel/tests/int3.rs index 771c17d..f4e2116 100644 --- a/kernel/tests/int3.rs +++ b/kernel/tests/int3.rs @@ -1,28 +1,26 @@ #![no_main] #![feature(compiler_builtins_lib)] -#![feature(custom_test_frameworks)] #![feature(abi_x86_interrupt)] #![feature(panic_info_message)] -#![test_runner(kernel::testing::serial_test_runner)] -#![reexport_test_harness_main = "test_main"] #![no_std] use kernel; +use kernel::interrupts; +use kernel::interrupts::exceptions; use kernel::io::qemu; #[no_mangle] extern "C" fn _start() -> ! { kernel::init(); - test_main(); + unsafe { + exceptions::expect_fault(interrupts::Interrupts::BREAKPOINT); + } + x86_64::instructions::interrupts::int3(); + panic!("BREAKPOINT not caught"); loop {} } -#[test_case] -fn breakpoint() { - x86_64::instructions::interrupts::int3(); -} - #[cfg(test)] #[panic_handler] #[no_mangle] diff --git a/kernel/tests/stack_overflow.rs b/kernel/tests/stack_overflow.rs index 677a9e1..dc53537 100644 --- a/kernel/tests/stack_overflow.rs +++ b/kernel/tests/stack_overflow.rs @@ -1,25 +1,28 @@ #![no_main] #![feature(compiler_builtins_lib)] -#![feature(custom_test_frameworks)] #![feature(abi_x86_interrupt)] #![feature(panic_info_message)] -#![test_runner(kernel::testing::serial_test_runner_panic)] #![no_std] use core::fmt::Write; use kernel; +use kernel::interrupts; +use kernel::interrupts::exceptions; use kernel::io::qemu; #[no_mangle] extern "C" fn _start() -> ! { kernel::init(); + unsafe { + exceptions::expect_fault(interrupts::Interrupts::PAGE_FAULT); + } _loop(0); - + panic!("PAGE_FAULT not caught"); loop {} } fn _loop(i: u128) -> u128 { - if i > 1u128 << 83 { + if i > 1u128 << 120 { return i; } let n = i; -- cgit v1.2.3-70-g09d2