Skip to content

Commit

Permalink
Implement double fault handler
Browse files Browse the repository at this point in the history
  • Loading branch information
keawade committed Oct 18, 2020
1 parent 94e5979 commit 644d12b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ test-timeout = 15 # seconds
[[test]]
name = "should_panic"
harness = false

[[test]]
name = "stack_overflow"
harness = false
52 changes: 52 additions & 0 deletions src/gdt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use lazy_static::lazy_static;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;

struct Selectors {
code_selector: SegmentSelector,
tss_selector: SegmentSelector,
}

lazy_static! {
static ref GDT: (GlobalDescriptorTable, Selectors) = {
let mut gdt = GlobalDescriptorTable::new();
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
(
gdt,
Selectors {
code_selector,
tss_selector,
},
)
};
}

pub fn init() {
use x86_64::instructions::segmentation::set_cs;
use x86_64::instructions::tables::load_tss;

GDT.0.load();
unsafe {
set_cs(GDT.1.code_selector);
load_tss(GDT.1.tss_selector);
}
}

pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;

lazy_static! {
static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
const STACK_SIZE: usize = 4096 * 5;
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];

let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
let stack_end = stack_start + STACK_SIZE;
stack_end
};
tss
};
}
13 changes: 13 additions & 0 deletions src/interrupts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::gdt;
use crate::println;
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
Expand All @@ -6,6 +7,11 @@ 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(gdt::DOUBLE_FAULT_IST_INDEX);
}
idt
};
}
Expand All @@ -17,3 +23,10 @@ pub fn init_idt() {
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
}

extern "x86-interrupt" fn double_fault_handler(
stack_frame: &mut InterruptStackFrame,
_error_code: u64,
) -> ! {
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

use core::panic::PanicInfo;

pub mod gdt;
pub mod interrupts;
pub mod serial;
pub mod vga_buffer;

pub fn init() {
gdt::init();
interrupts::init_idt();
}

Expand Down
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ pub extern "C" fn _start() -> ! {

k_os::init();

// invoke a breakpoint exception
x86_64::instructions::interrupts::int3(); // new

#[cfg(test)]
test_main();

println!("It did not crash!");

loop {}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/stack_overflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]

use core::panic::PanicInfo;
use k_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};

#[no_mangle]
pub extern "C" fn _start() -> ! {
serial_print!("stack_overflow::stack_overflow...\t");

k_os::gdt::init();
// Use custom IDT to load custom fault handler
init_test_idt();

// trigger a stack overflow
stack_overflow();

panic!("Execution continued after stack overflow");
}

#[allow(unconditional_recursion)]
fn stack_overflow() {
stack_overflow(); // for each recursion, the return address is pushed
volatile::Volatile::new(0).read(); // prevent tail recursion optimizations
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
k_os::test_panic_handler(info)
}

// fault handler for test to avoid normal panic
lazy_static! {
static ref TEST_IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
unsafe {
idt.double_fault
.set_handler_fn(test_double_fault_handler)
.set_stack_index(k_os::gdt::DOUBLE_FAULT_IST_INDEX);
}

idt
};
}

pub fn init_test_idt() {
TEST_IDT.load();
}

extern "x86-interrupt" fn test_double_fault_handler(
_stack_frame: &mut InterruptStackFrame,
_error_code: u64,
) -> ! {
serial_println!("[ok]");
// Exit qemu instead of panic
exit_qemu(QemuExitCode::Success);
loop {}
}

0 comments on commit 644d12b

Please sign in to comment.