From 730df58cc89c8c36e02327378a8d275bd476e52d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 24 Sep 2019 16:59:20 -0400 Subject: [PATCH] post-06 --- Cargo.toml | 4 +++ src/gdt.rs | 45 ++++++++++++++++++++++++++++++ src/interrupts.rs | 12 ++++++++ src/lib.rs | 2 ++ src/main.rs | 3 +- tests/stack_overflow.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/gdt.rs create mode 100644 tests/stack_overflow.rs diff --git a/Cargo.toml b/Cargo.toml index fc94f7e..204f961 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,4 +32,8 @@ test-success-exit-code = 33 # (0x10 << 1) | 1 [[test]] name = "should_panic" +harness = false + +[[test]] +name = "stack_overflow" harness = false \ No newline at end of file diff --git a/src/gdt.rs b/src/gdt.rs new file mode 100644 index 0000000..f8146a9 --- /dev/null +++ b/src/gdt.rs @@ -0,0 +1,45 @@ +use lazy_static::lazy_static; +use x86_64::VirtAddr; +use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector}; +use x86_64::structures::tss::TaskStateSegment; + +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; + 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 + }; +} + +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 }) + }; +} + +struct Selectors { + code_selector: SegmentSelector, + tss_selector: SegmentSelector, +} + +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); + } +} \ No newline at end of file diff --git a/src/interrupts.rs b/src/interrupts.rs index 7fa4d5e..3adacc5 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,6 +1,7 @@ use lazy_static::lazy_static; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; +use crate::gdt; use crate::println; #[cfg(test)] @@ -10,6 +11,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 }; } @@ -24,6 +30,12 @@ extern "x86-interrupt" fn breakpoint_handler( 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); +} + #[test_case] fn test_breakpoint_exception() { serial_print!("test_breakpoint_exception..."); diff --git a/src/lib.rs b/src/lib.rs index 395f595..f65c260 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ use core::panic::PanicInfo; +pub mod gdt; pub mod interrupts; pub mod serial; pub mod macros; @@ -59,5 +60,6 @@ pub fn exit_qemu(exit_code: QemuExitCode) { } pub fn init() { + gdt::init(); interrupts::init_idt(); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1e32c3f..aa31d6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,10 +28,9 @@ pub extern "C" fn _start() -> ! { blog_os::init(); - x86_64::instructions::interrupts::int3(); - #[cfg(test)] test_main(); + println!("It did not crash!"); loop {} } diff --git a/tests/stack_overflow.rs b/tests/stack_overflow.rs new file mode 100644 index 0000000..638143a --- /dev/null +++ b/tests/stack_overflow.rs @@ -0,0 +1,62 @@ +// in tests/stack_overflow.rs + +#![no_std] +#![no_main] +#![feature(abi_x86_interrupt)] + +use core::panic::PanicInfo; +use lazy_static::lazy_static; +use x86_64::structures::idt::InterruptDescriptorTable; +use x86_64::structures::idt::InterruptStackFrame; + +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(blog_os::gdt::DOUBLE_FAULT_IST_INDEX); + } + + idt + }; +} + + +use blog_os::{exit_qemu, QemuExitCode, serial_print, serial_println}; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + serial_print!("stack_overflow... "); + + blog_os::gdt::init(); + init_test_idt(); + + // trigger a stack overflow + stack_overflow(); + + panic!("Execution continued after stack overflow"); +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + blog_os::test_panic_handler(info) +} + +#[allow(unconditional_recursion)] +fn stack_overflow() { + stack_overflow(); // for each recursion, the return address is pushed +} + +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(QemuExitCode::Success); + loop {} +}