mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Add code for “Returning from Exceptions”
This commit is contained in:
@@ -1,16 +1,50 @@
|
||||
mod idt;
|
||||
|
||||
macro_rules! save_scratch_registers {
|
||||
() => {
|
||||
asm!("push rax
|
||||
push rcx
|
||||
push rdx
|
||||
push rsi
|
||||
push rdi
|
||||
push r8
|
||||
push r9
|
||||
push r10
|
||||
push r11
|
||||
" :::: "intel", "volatile");
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! restore_scratch_registers {
|
||||
() => {
|
||||
asm!("pop r11
|
||||
pop r10
|
||||
pop r9
|
||||
pop r8
|
||||
pop rdi
|
||||
pop rsi
|
||||
pop rdx
|
||||
pop rcx
|
||||
pop rax
|
||||
" :::: "intel", "volatile");
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! handler {
|
||||
($name: ident) => {{
|
||||
#[naked]
|
||||
extern "C" fn wrapper() -> ! {
|
||||
unsafe {
|
||||
save_scratch_registers!();
|
||||
asm!("mov rdi, rsp
|
||||
sub rsp, 8 // align the stack pointer
|
||||
add rdi, 9*8 // calculate exception stack frame pointer
|
||||
call $0"
|
||||
:: "i"($name as extern "C" fn(
|
||||
*const ExceptionStackFrame) -> !)
|
||||
*const ExceptionStackFrame))
|
||||
: "rdi" : "intel");
|
||||
|
||||
restore_scratch_registers!();
|
||||
asm!("iretq" :::: "intel", "volatile");
|
||||
::core::intrinsics::unreachable();
|
||||
}
|
||||
}
|
||||
@@ -23,13 +57,19 @@ macro_rules! handler_with_error_code {
|
||||
#[naked]
|
||||
extern "C" fn wrapper() -> ! {
|
||||
unsafe {
|
||||
asm!("pop rsi // pop error code into rsi
|
||||
save_scratch_registers!();
|
||||
asm!("mov rsi, [rsp + 9*8] // load error code into rsi
|
||||
mov rdi, rsp
|
||||
add rdi, 10*8 // calculate exception stack frame pointer
|
||||
sub rsp, 8 // align the stack pointer
|
||||
call $0"
|
||||
:: "i"($name as extern "C" fn(
|
||||
*const ExceptionStackFrame, u64) -> !)
|
||||
call $0
|
||||
add rsp, 8 // undo stack pointer alignment
|
||||
" :: "i"($name as extern "C" fn(
|
||||
*const ExceptionStackFrame, u64))
|
||||
: "rdi","rsi" : "intel");
|
||||
restore_scratch_registers!();
|
||||
asm!("add rsp, 8 // pop error code
|
||||
iretq" :::: "intel", "volatile");
|
||||
::core::intrinsics::unreachable();
|
||||
}
|
||||
}
|
||||
@@ -42,6 +82,7 @@ lazy_static! {
|
||||
let mut idt = idt::Idt::new();
|
||||
|
||||
idt.set_handler(0, handler!(divide_by_zero_handler));
|
||||
idt.set_handler(3, handler!(breakpoint_handler));
|
||||
idt.set_handler(6, handler!(invalid_opcode_handler));
|
||||
idt.set_handler(14, handler_with_error_code!(page_fault_handler));
|
||||
|
||||
@@ -65,14 +106,22 @@ struct ExceptionStackFrame {
|
||||
|
||||
use vga_buffer::print_error;
|
||||
|
||||
extern "C" fn divide_by_zero_handler(stack_frame: *const ExceptionStackFrame) -> ! {
|
||||
extern "C" fn divide_by_zero_handler(stack_frame: *const ExceptionStackFrame) {
|
||||
unsafe {
|
||||
print_error(format_args!("EXCEPTION: DIVIDE BY ZERO\n{:#?}", *stack_frame));
|
||||
}
|
||||
loop {}
|
||||
}
|
||||
|
||||
extern "C" fn invalid_opcode_handler(stack_frame: *const ExceptionStackFrame) -> ! {
|
||||
extern "C" fn breakpoint_handler(stack_frame: *const ExceptionStackFrame) {
|
||||
unsafe {
|
||||
print_error(format_args!("EXCEPTION: BREAKPOINT at {:#x}\n{:#?}",
|
||||
(*stack_frame).instruction_pointer,
|
||||
*stack_frame));
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn invalid_opcode_handler(stack_frame: *const ExceptionStackFrame) {
|
||||
unsafe {
|
||||
print_error(format_args!("EXCEPTION: INVALID OPCODE at {:#x}\n{:#?}",
|
||||
(*stack_frame).instruction_pointer,
|
||||
@@ -91,7 +140,7 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn page_fault_handler(stack_frame: *const ExceptionStackFrame, error_code: u64) -> ! {
|
||||
extern "C" fn page_fault_handler(stack_frame: *const ExceptionStackFrame, error_code: u64) {
|
||||
use x86::controlregs;
|
||||
unsafe {
|
||||
print_error(format_args!("EXCEPTION: PAGE FAULT while accessing {:#x}\nerror code: \
|
||||
|
||||
Reference in New Issue
Block a user