mirror of
https://github.com/mii443/nel_os.git
synced 2025-08-22 16:15:38 +00:00
add double fault handler
This commit is contained in:
@ -29,3 +29,7 @@ test-timeout = 300
|
|||||||
[[test]]
|
[[test]]
|
||||||
name = "should_panic"
|
name = "should_panic"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "stack_overflow"
|
||||||
|
harness = false
|
||||||
|
57
src/gdt.rs
Normal file
57
src/gdt.rs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
|
use x86_64::{
|
||||||
|
instructions::tables::load_tss,
|
||||||
|
registers::segmentation::{Segment, CS},
|
||||||
|
structures::{
|
||||||
|
gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector},
|
||||||
|
tss::TaskStateSegment,
|
||||||
|
},
|
||||||
|
VirtAddr,
|
||||||
|
};
|
||||||
|
|
||||||
|
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(&raw const 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() {
|
||||||
|
GDT.0.load();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
CS::set_reg(GDT.1.code_selector);
|
||||||
|
load_tss(GDT.1.tss_selector);
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,18 @@
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
||||||
|
|
||||||
use crate::println;
|
use crate::{gdt, println};
|
||||||
|
|
||||||
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
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -19,6 +25,13 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
#[test_case]
|
#[test_case]
|
||||||
fn test_breakpoint_exception() {
|
fn test_breakpoint_exception() {
|
||||||
x86_64::instructions::interrupts::int3();
|
x86_64::instructions::interrupts::int3();
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
pub mod vga_buffer;
|
pub mod vga_buffer;
|
||||||
@ -72,5 +73,6 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
|
gdt::init();
|
||||||
interrupts::init_idt();
|
interrupts::init_idt();
|
||||||
}
|
}
|
||||||
|
58
tests/stack_overflow.rs
Normal file
58
tests/stack_overflow.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(abi_x86_interrupt)]
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use nel_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||||
|
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(nel_os::gdt::DOUBLE_FAULT_IST_INDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
idt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
|
serial_print!("stack_overflow::stack_overflow...\t");
|
||||||
|
|
||||||
|
nel_os::gdt::init();
|
||||||
|
init_test_idt();
|
||||||
|
|
||||||
|
stack_overflow();
|
||||||
|
|
||||||
|
panic!("Execution continued after stack overflow");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
nel_os::test_panic_handler(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unconditional_recursion)]
|
||||||
|
fn stack_overflow() {
|
||||||
|
stack_overflow();
|
||||||
|
volatile::Volatile::new(0).read();
|
||||||
|
}
|
Reference in New Issue
Block a user