Add and test a handler_with_error_code macro and a page fault handler

This commit is contained in:
Philipp Oppermann
2016-08-03 16:36:58 +02:00
parent df1e39edb2
commit 69f1b58bb0
2 changed files with 34 additions and 2 deletions

View File

@@ -18,12 +18,32 @@ macro_rules! handler {
}}
}
macro_rules! handler_with_error_code {
($name: ident) => {{
#[naked]
extern "C" fn wrapper() -> ! {
unsafe {
asm!("pop rsi // pop error code into rsi
mov rdi, rsp
sub rsp, 8 // align the stack pointer
call $0"
:: "i"($name as extern "C" fn(
*const ExceptionStackFrame, u64) -> !)
: "rdi" : "intel");
::core::intrinsics::unreachable();
}
}
wrapper
}}
}
lazy_static! {
static ref IDT: idt::Idt = {
let mut idt = idt::Idt::new();
idt.set_handler(0, handler!(divide_by_zero_handler));
idt.set_handler(6, handler!(invalid_opcode_handler));
idt.set_handler(14, handler_with_error_code!(page_fault_handler));
idt
};
@@ -64,3 +84,14 @@ extern "C" fn invalid_opcode_handler(stack_frame: *const ExceptionStackFrame)
}
loop {}
}
extern "C" fn page_fault_handler(stack_frame: *const ExceptionStackFrame,
error_code: u64) -> !
{
unsafe {
print_error(format_args!(
"EXCEPTION: PAGE FAULT with error code {:?}\n{:#?}",
error_code, *stack_frame));
}
loop {}
}

View File

@@ -52,8 +52,9 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
// initialize our IDT
interrupts::init();
// provoke a invalid opcode exception
unsafe { asm!("ud2") };
// provoke a page fault
unsafe { *(0xdeadbeaf as *mut u64) = 42 };
println!("It did not crash!");
loop {}