WIP code for upcoming post

This commit is contained in:
Philipp Oppermann
2016-07-16 17:19:45 +02:00
parent 95c0452bcb
commit 06fb4d6596
2 changed files with 80 additions and 1 deletions

View File

@@ -5,6 +5,9 @@ lazy_static! {
let mut idt = idt::Idt::new(); let mut idt = idt::Idt::new();
idt.set_handler(0, divide_by_zero_handler); 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 idt
}; };
@@ -16,7 +19,76 @@ pub fn init() {
use vga_buffer::print_error; use vga_buffer::print_error;
#[naked]
extern "C" fn divide_by_zero_handler() -> ! { 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 {} 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 {}
}
}

View File

@@ -11,6 +11,7 @@
#![feature(const_fn, unique)] #![feature(const_fn, unique)]
#![feature(alloc, collections)] #![feature(alloc, collections)]
#![feature(asm)] #![feature(asm)]
#![feature(naked_functions)]
#![no_std] #![no_std]
extern crate rlibc; 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 // set up guard page and map the heap pages
memory::init(boot_info); memory::init(boot_info);
// initialize our IDT // initialize our IDT
interrupts::init(); 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") } 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 // provoke a divide by zero fault inside println
println!("{:?}", divide_by_zero()); println!("{:?}", divide_by_zero());