Double Faults

This commit is contained in:
Sainnhe Park 2022-01-10 17:45:04 +08:00
parent 70099da497
commit a3bc614725
6 changed files with 122 additions and 0 deletions

View File

@ -34,3 +34,7 @@ test-timeout = 300 # 设定 300 秒后超时
[[test]] [[test]]
name = "should_panic" name = "should_panic"
harness = false # 禁用 test runner, 让 should_panic 作为一个常规的可执行文件存在 harness = false # 禁用 test runner, 让 should_panic 作为一个常规的可执行文件存在
[[test]]
name = "stack_overflow"
harness = false

48
src/gdt.rs Normal file
View File

@ -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);
}
}

View File

@ -1,11 +1,16 @@
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
use crate::println; use crate::println;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use crate::gdt;
lazy_static! { lazy_static! {
static ref IDT: InterruptDescriptorTable = { static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new(); let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler); 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 idt
}; };
} }
@ -17,3 +22,7 @@ pub fn init_idt() {
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); 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);
}

View File

@ -9,6 +9,7 @@ use core::panic::PanicInfo;
pub mod serial; pub mod serial;
pub mod vga_buffer; pub mod vga_buffer;
pub mod interrupts; pub mod interrupts;
pub mod gdt;
pub trait Testable { pub trait Testable {
fn run(&self) -> (); fn run(&self) -> ();
@ -79,5 +80,6 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
} }
pub fn init() { pub fn init() {
gdt::init();
interrupts::init_idt(); interrupts::init_idt();
} }

View File

@ -15,9 +15,18 @@ pub extern "C" fn _start() -> ! {
println!("Hello World{}", "!"); println!("Hello World{}", "!");
anos::init(); anos::init();
// 触发一个中断
x86_64::instructions::interrupts::int3(); x86_64::instructions::interrupts::int3();
println!("It didn't crash!"); println!("It didn't crash!");
// 触发一个页错误
// 由于我们没有设定页错误的处理函数
// 所以会触发 double fault
unsafe {
*(0xdeadbeef as *mut u64) = 42; // 强制写入某个内存地址
}
#[cfg(test)] #[cfg(test)]
test_main(); test_main();

50
tests/stack_overflow.rs Normal file
View File

@ -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 {}
}