From 308b033ea9ba7768d75add7fea402d4ef66edf7f Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 3 Aug 2016 16:22:59 +0200 Subject: [PATCH] Add a wrapper function and print the exception stack frame --- src/interrupts/mod.rs | 33 ++++++++++++++++++++++++++++++--- src/lib.rs | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/interrupts/mod.rs b/src/interrupts/mod.rs index 07d19874..b41db333 100644 --- a/src/interrupts/mod.rs +++ b/src/interrupts/mod.rs @@ -4,7 +4,7 @@ lazy_static! { static ref IDT: idt::Idt = { let mut idt = idt::Idt::new(); - idt.set_handler(0, divide_by_zero_handler); + idt.set_handler(0, divide_by_zero_wrapper); idt }; @@ -14,9 +14,36 @@ pub fn init() { IDT.load(); } +#[derive(Debug)] +#[repr(C)] +struct ExceptionStackFrame { + instruction_pointer: u64, + code_segment: u64, + cpu_flags: u64, + stack_pointer: u64, + stack_segment: u64, +} + use vga_buffer::print_error; -extern "C" fn divide_by_zero_handler() -> ! { - unsafe { print_error(format_args!("EXCEPTION: DIVIDE BY ZERO")) }; +#[naked] +extern "C" fn divide_by_zero_wrapper() -> ! { + unsafe { + asm!("mov rdi, rsp + sub rsp, 8 // align the stack pointer + call $0" + :: "i"(divide_by_zero_handler as extern "C" fn(_) -> !) + : "rdi" : "intel"); + ::core::intrinsics::unreachable(); + } +} + +extern "C" fn divide_by_zero_handler(stack_frame: *const ExceptionStackFrame) + -> ! +{ + unsafe { + print_error(format_args!("EXCEPTION: DIVIDE BY ZERO\n{:#?}", + *stack_frame)); + } loop {} } diff --git a/src/lib.rs b/src/lib.rs index 7a35129e..37df865e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,8 @@ #![feature(const_fn, unique)] #![feature(alloc, collections)] #![feature(asm)] +#![feature(naked_functions)] +#![feature(core_intrinsics)] #![no_std] extern crate rlibc;