Handle all double faults (3h44m)

This commit is contained in:
Conzer 2024-12-03 19:24:57 -05:00
parent 2b2079d734
commit beb2227d7e
4 changed files with 71 additions and 4 deletions

47
src/gdt.rs Normal file
View file

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

View file

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

View file

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

View file

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