From 2e0f4f9161f3a2726ed428ecd2366488640646d0 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 16 Jul 2016 17:19:45 +0200 Subject: [PATCH] Begin code for upcoming post --- src/interrupts/mod.rs | 74 ++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 7 ++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/interrupts/mod.rs b/src/interrupts/mod.rs index 07d19874..dfba070d 100644 --- a/src/interrupts/mod.rs +++ b/src/interrupts/mod.rs @@ -5,6 +5,9 @@ lazy_static! { let mut idt = idt::Idt::new(); idt.set_handler(0, divide_by_zero_handler); + //idt.set_handler(8, double_fault_handler); + //idt.set_handler(13, general_protection_fault_handler); + idt.set_handler(14, page_fault_handler); idt }; @@ -16,7 +19,76 @@ pub fn init() { use vga_buffer::print_error; +#[naked] extern "C" fn divide_by_zero_handler() -> ! { - unsafe { print_error(format_args!("EXCEPTION: DIVIDE BY ZERO")) }; + unsafe { + asm!(/* load excepiton fram pointer and call main_handler*/); + } + ::core::intrinsics::unreachable(); + + extern "C" fn main_handler(stack_frame: *const ExceptionStackFrameErrorCode) -> ! { + unsafe { + print_error(format_args!("EXCEPTION: DIVIDE BY ZERO\n{:#?}", *stack_frame)); + } + loop {} + } +} + +extern "C" fn divide_by_zero_handler() -> ! { + let stack_frame: *const ExceptionStackFrame; + unsafe { + asm!("mov $0, rsp" : "=r"(stack_frame) ::: "intel"); + print_error(format_args!("EXCEPTION: DIVIDE BY ZERO\n{:#?}", *stack_frame)); + }; loop {} } + +extern "C" fn double_fault_handler() -> ! { + unsafe { print_error(format_args!("EXCEPTION: DOUBLE FAULT")) }; + loop {} +} + +extern "C" fn general_protection_fault_handler() -> ! { + loop {} + unsafe { print_error(format_args!("EXCEPTION: GENERAL PROTECTION FAULT")) }; +} + + +#[derive(Debug)] +#[repr(C)] +struct ExceptionStackFrame { + instruction_pointer: u64, + code_segment: u64, + cpu_flags: u64, + stack_pointer: u64, + stack_segment: u64, +} + +#[derive(Debug)] +#[repr(C)] +struct ExceptionStackFrameErrorCode { + error_code: u64, + instruction_pointer: u64, + code_segment: u64, + cpu_flags: u64, + stack_pointer: u64, + stack_segment: u64, +} + +#[naked] +extern "C" fn page_fault_handler() -> ! { + unsafe { + asm!(" + mov rdi, rsp + call $0 + " :: "i"(handler as extern "C" fn(*const ExceptionStackFrameErrorCode) -> !) :: "intel"); + } + loop{} + + extern "C" fn handler(stack_frame: *const ExceptionStackFrameErrorCode) -> ! { + unsafe { + print_error(format_args!("EXCEPTION: PAGE FAULT\n stack frame: {:#?}", *stack_frame)); + } + loop {} + } +} diff --git a/src/lib.rs b/src/lib.rs index 7a35129e..23c1c144 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ #![feature(const_fn, unique)] #![feature(alloc, collections)] #![feature(asm)] +#![feature(naked_functions)] #![no_std] extern crate rlibc; @@ -47,6 +48,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) { // set up guard page and map the heap pages memory::init(boot_info); + // initialize our IDT interrupts::init(); @@ -54,6 +56,11 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) { unsafe { asm!("mov dx, 0; div dx" ::: "ax", "dx" : "volatile", "intel") } } + //println!("{:?}", divide_by_zero()); + + // provoke a page fault inside println + println!("{:?}", unsafe { *(0xdeadbeaf as *mut u64) = 42 }); + // provoke a divide by zero fault inside println println!("{:?}", divide_by_zero());