From beb2227d7e4c1d95854dddba6b55bfbe9c9068a8 Mon Sep 17 00:00:00 2001 From: Conzer Date: Tue, 3 Dec 2024 19:24:57 -0500 Subject: [PATCH] Handle all double faults (3h44m) --- src/gdt.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/interrupts.rs | 13 +++++++++++++ src/lib.rs | 9 ++++++--- src/main.rs | 6 +++++- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/gdt.rs diff --git a/src/gdt.rs b/src/gdt.rs new file mode 100644 index 0000000..707716d --- /dev/null +++ b/src/gdt.rs @@ -0,0 +1,47 @@ +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; + +pub fn init() { + use x86_64::instructions::tables::load_tss; + use x86_64::instructions::segmentation::{CS, Segment}; + + GDT.0.load(); + unsafe { + CS::set_reg(GDT.1.code_selector); + load_tss(GDT.1.tss_selector); + } +} + +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, +} \ No newline at end of file diff --git a/src/interrupts.rs b/src/interrupts.rs index d0440b1..c9f1da3 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,10 +1,17 @@ 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); // new + } + idt }; } @@ -19,6 +26,12 @@ extern "x86-interrupt" fn breakpoint_handler( println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); } +extern "x86-interrupt" fn double_fault_handler( + stack_frame: InterruptStackFrame, _error_code: u64) -> ! +{ + panic!("EXCEPTION: DLB FAULT\n{:#?}", stack_frame); +} + #[test_case] fn test_breakpoint_exception() { // invoke a breakpoint exception diff --git a/src/lib.rs b/src/lib.rs index 297bb59..a28f671 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,13 +6,16 @@ #![reexport_test_harness_main = "test_main"] #![feature(abi_x86_interrupt)] -pub fn init() { - interrupts::init_idt(); -} pub mod serial; pub mod vga_buffer; pub mod interrupts; +pub mod gdt; + +pub fn init() { + gdt::init(); + interrupts::init_idt(); +} use core::panic::PanicInfo; diff --git a/src/main.rs b/src/main.rs index f794e6b..b66d41a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,11 @@ pub extern "C" fn _start() -> ! { donald::init(); - x86_64::instructions::interrupts::int3(); + fn stack_overflow() { + stack_overflow(); + } + + stack_overflow(); #[cfg(test)] test_main();