Double Faults
This commit is contained in:
48
src/gdt.rs
Normal file
48
src/gdt.rs
Normal 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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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();
|
||||
|
||||
|
Reference in New Issue
Block a user