Compare commits

...

3 Commits

Author SHA1 Message Date
Greg Shuflin 92533cb5d2 Some code? 2023-11-09 17:32:46 -08:00
Greg Shuflin 084945688b Interrupts: Handle double fault 2022-04-04 00:24:30 -07:00
Greg Shuflin 79efbdc5a0 Starting to add interrupt handling 2022-04-03 23:38:56 -07:00
3 changed files with 73 additions and 1 deletions

33
src/gdt.rs Normal file
View File

@ -0,0 +1,33 @@
use lazy_static::lazy_static;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
use x86_64::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(unsafe { &STACK });
let stack_end = stack_start + STACK_SIZE;
stack_end
};
tss
};
}
lazy_static! {
static ref GDT: GlobalDescriptorTable = {
let mut gdt = GlobalDescriptorTable::new();
gdt.add_entry(Descriptor::kernel_code_segment());
gdt.add_entry(Descriptor::tss_segment(&TSS));
gdt
};
}
pub fn init() {
GDT.load();
}

26
src/interrupts.rs Normal file
View File

@ -0,0 +1,26 @@
use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
lazy_static! {
static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
idt.double_fault.set_handler_fn(double_fault_handler);
idt
};
}
pub fn init_idt() {
IDT.load();
}
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
println!("EXCEPTION: Breakpoint\n{:#?}", stack_frame);
}
// The error code of the double-fault handler is always 0
extern "x86-interrupt" fn double_fault_handler(
stack_frame: InterruptStackFrame,
_error_code: u64,
) -> ! {
panic!("Exception: Double fault\n{:#?}", stack_frame)
}

View File

@ -1,4 +1,4 @@
// #![feature(abi_x86_interrupt, asm)]
#![feature(abi_x86_interrupt)]
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
@ -9,6 +9,8 @@
mod vga_buffer;
#[macro_use]
mod serial;
mod gdt;
mod interrupts;
mod test_utils;
#[cfg(not(test))]
@ -24,6 +26,12 @@ pub extern "C" fn _start() -> ! {
test_main();
println!("Gamarjoba, munde!");
init();
// Deliberate breakpoint exception
x86_64::instructions::interrupts::int3();
println!("We're here now");
loop {}
}
@ -36,3 +44,8 @@ fn basic_test() {
fn basic_test2() {
assert_eq!(4, 4);
}
pub fn init() {
gdt::init();
interrupts::init_idt();
}