Exception Handlers
Exception handlers catch CPU faults before they become triple faults. With the new signal callback system, handlers are no longer hardcoded extern "C" functions — they’re plain Rust functions registered in the signal table.
The New Handler Format
Each handler now matches the SignalHandler type:
#![allow(unused)]
fn main() {
type SignalHandler = fn(interrupt: u32, frame: &InterruptFrame);
}
This gives every handler access to both the interrupt number and the full CPU state. Here’s the complete set:
#![allow(unused)]
fn main() {
pub fn divide_by_zero(interrupt: u32, frame: &InterruptFrame) {
println!("EXCEPTION #0: DIVIDE BY ZERO");
frame.print();
loop {}
}
pub fn invalid_opcode(interrupt: u32, frame: &InterruptFrame) {
println!("EXCEPTION #6: INVALID OPCODE");
frame.print();
loop {}
}
pub fn double_fault(interrupt: u32, frame: &InterruptFrame) {
println!("EXCEPTION #8: DOUBLE FAULT (STACK OVERFLOW?)");
frame.print();
loop {}
}
pub fn general_protection_fault(interrupt: u32, frame: &InterruptFrame) {
println!("EXCEPTION #13: GENERAL PROTECTION FAULT");
frame.print();
loop {}
}
pub fn page_fault(interrupt: u32, frame: &InterruptFrame) {
println!("EXCEPTION #14: PAGE FAULT");
frame.print();
loop {}
}
pub fn default_interrupt(interrupt: u32, frame: &InterruptFrame) {
println!("UNHANDLED INTERRUPT: {}", interrupt);
frame.print();
loop {}
}
}
Notice there’s no #[no_mangle] or extern "C" anymore — these are just regular Rust functions. The assembly stubs handle the calling convention, and the dispatcher handles the lookup. Each handler calls frame.print() to dump the register state, then loops forever since these are all fatal exceptions.
What Changed From KFS_1
In KFS_1, each exception had its own dedicated assembly wrapper that called a specific #[no_mangle] Rust function. Adding a new handler meant touching the IDT, writing a new assembly stub, and writing a new Rust function.
Now adding a handler is just one line:
#![allow(unused)]
fn main() {
signal::register(42, my_handler);
}
The signal table does the rest.