mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Add and test a handler_with_error_code macro and a page fault handler
This commit is contained in:
@@ -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! {
|
lazy_static! {
|
||||||
static ref IDT: idt::Idt = {
|
static ref IDT: idt::Idt = {
|
||||||
let mut idt = idt::Idt::new();
|
let mut idt = idt::Idt::new();
|
||||||
|
|
||||||
idt.set_handler(0, handler!(divide_by_zero_handler));
|
idt.set_handler(0, handler!(divide_by_zero_handler));
|
||||||
idt.set_handler(6, handler!(invalid_opcode_handler));
|
idt.set_handler(6, handler!(invalid_opcode_handler));
|
||||||
|
idt.set_handler(14, handler_with_error_code!(page_fault_handler));
|
||||||
|
|
||||||
idt
|
idt
|
||||||
};
|
};
|
||||||
@@ -64,3 +84,14 @@ extern "C" fn invalid_opcode_handler(stack_frame: *const ExceptionStackFrame)
|
|||||||
}
|
}
|
||||||
loop {}
|
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 {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -52,8 +52,9 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
|||||||
// initialize our IDT
|
// initialize our IDT
|
||||||
interrupts::init();
|
interrupts::init();
|
||||||
|
|
||||||
// provoke a invalid opcode exception
|
// provoke a page fault
|
||||||
unsafe { asm!("ud2") };
|
unsafe { *(0xdeadbeaf as *mut u64) = 42 };
|
||||||
|
|
||||||
|
|
||||||
println!("It did not crash!");
|
println!("It did not crash!");
|
||||||
loop {}
|
loop {}
|
||||||
|
|||||||
Reference in New Issue
Block a user