From 6e3528f5f53cae53fc8508481ed9d8744490293b Mon Sep 17 00:00:00 2001 From: ami-chuu Date: Mon, 30 Mar 2026 14:28:26 +0300 Subject: [PATCH] Added CPU exceptions --- src/interrupts.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 7 +++++++ src/main.rs | 5 +++++ 3 files changed, 37 insertions(+) create mode 100644 src/interrupts.rs diff --git a/src/interrupts.rs b/src/interrupts.rs new file mode 100644 index 0000000..405c1e9 --- /dev/null +++ b/src/interrupts.rs @@ -0,0 +1,25 @@ +use crate::println; +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 + }; +} + +pub fn init_idt() { + IDT.load(); +} + +extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { + println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); +} + +#[test_case] +fn test_breakpoint_exception() { + // invoke a breakpoint exception + x86_64::instructions::interrupts::int3(); +} diff --git a/src/lib.rs b/src/lib.rs index c7387d9..ae4e3a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,11 @@ #![feature(custom_test_frameworks)] #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] +#![feature(abi_x86_interrupt)] use core::panic::PanicInfo; +pub mod interrupts; pub mod serial; pub mod vga_buffer; @@ -43,6 +45,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! { #[cfg(test)] #[unsafe(no_mangle)] pub extern "C" fn _start() -> ! { + init(); test_main(); loop {} } @@ -68,3 +71,7 @@ pub fn exit_qemu(exit_code: QemuExitCode) { port.write(exit_code as u32); } } + +pub fn init() { + interrupts::init_idt(); +} diff --git a/src/main.rs b/src/main.rs index 22fb183..f153785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,9 +11,14 @@ use core::panic::PanicInfo; pub extern "C" fn _start() -> ! { println!("Welcome to Amix{}", "!"); + amix::init(); + + x86_64::instructions::interrupts::int3(); + #[cfg(test)] test_main(); + println!("It did not crash!"); loop {} }