From a3bc614725827a8e7eea5dc32ff37489dc91f377 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Mon, 10 Jan 2022 17:45:04 +0800 Subject: [PATCH] Double Faults --- Cargo.toml | 4 ++++ src/gdt.rs | 48 +++++++++++++++++++++++++++++++++++++++ src/interrupts.rs | 9 ++++++++ src/lib.rs | 2 ++ src/main.rs | 9 ++++++++ tests/stack_overflow.rs | 50 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 src/gdt.rs create mode 100644 tests/stack_overflow.rs diff --git a/Cargo.toml b/Cargo.toml index 7b81e50..21935d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,7 @@ test-timeout = 300 # 设定 300 秒后超时 [[test]] name = "should_panic" harness = false # 禁用 test runner, 让 should_panic 作为一个常规的可执行文件存在 + +[[test]] +name = "stack_overflow" +harness = false diff --git a/src/gdt.rs b/src/gdt.rs new file mode 100644 index 0000000..84758ec --- /dev/null +++ b/src/gdt.rs @@ -0,0 +1,48 @@ +use x86_64::VirtAddr; +use x86_64::structures::tss::TaskStateSegment; +use lazy_static::lazy_static; +use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor}; +use x86_64::structures::gdt::SegmentSelector; + +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 + }; +} + +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() { + #[allow(deprecated)] + use x86_64::instructions::segmentation::set_cs; + use x86_64::instructions::tables::load_tss; + + GDT.0.load(); + unsafe { + #[allow(deprecated)] + set_cs(GDT.1.code_selector); + load_tss(GDT.1.tss_selector); + } +} diff --git a/src/interrupts.rs b/src/interrupts.rs index 6b94536..9918371 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,11 +1,16 @@ use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; use crate::println; use lazy_static::lazy_static; +use crate::gdt; 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 }; } @@ -17,3 +22,7 @@ pub fn init_idt() { extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); } + +extern "x86-interrupt" fn double_fault_handler(stack_frame: InterruptStackFrame, _error_code: u64) -> ! { + panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); +} diff --git a/src/lib.rs b/src/lib.rs index da85caa..5c91a6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ use core::panic::PanicInfo; pub mod serial; pub mod vga_buffer; pub mod interrupts; +pub mod gdt; pub trait Testable { fn run(&self) -> (); @@ -79,5 +80,6 @@ pub fn exit_qemu(exit_code: QemuExitCode) { } pub fn init() { + gdt::init(); interrupts::init_idt(); } diff --git a/src/main.rs b/src/main.rs index 4f867d0..4be060b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,9 +15,18 @@ pub extern "C" fn _start() -> ! { println!("Hello World{}", "!"); anos::init(); + + // 触发一个中断 x86_64::instructions::interrupts::int3(); println!("It didn't crash!"); + // 触发一个页错误 + // 由于我们没有设定页错误的处理函数 + // 所以会触发 double fault + unsafe { + *(0xdeadbeef as *mut u64) = 42; // 强制写入某个内存地址 + } + #[cfg(test)] test_main(); diff --git a/tests/stack_overflow.rs b/tests/stack_overflow.rs new file mode 100644 index 0000000..d46c760 --- /dev/null +++ b/tests/stack_overflow.rs @@ -0,0 +1,50 @@ +#![no_std] +#![no_main] +#![feature(abi_x86_interrupt)] + +use core::panic::PanicInfo; +use anos::{exit_qemu, QemuExitCode, serial_println, serial_print}; +use lazy_static::lazy_static; +use x86_64::structures::idt::{InterruptDescriptorTable, 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(anos::gdt::DOUBLE_FAULT_IST_INDEX); + } + idt + }; +} + +#[no_mangle] +pub extern "C" fn _start() -> ! { + serial_print!("stack_overflow::stack_overflow...\t"); + anos::gdt::init(); + init_test_idt(); + stack_overflow(); + panic!("Execution continued after stack overflow"); +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + anos::test_panic_handler(info) +} + +#[allow(unconditional_recursion)] +fn stack_overflow() { + stack_overflow(); + volatile::Volatile::new(0).read(); +} + +pub fn init_test_idt() { + TEST_IDT.load(); +} + +extern "x86-interrupt" fn test_double_fault_handler(_stack_frame: InterruptStackFrame, _error_code: u64) -> ! { + serial_println!("[ok]"); + exit_qemu(QemuExitCode::Success); + loop {} +}