Reset src to master to be able to follow step-by-step

This commit is contained in:
Philipp Oppermann
2016-08-03 15:59:28 +02:00
parent 398ba7a424
commit 297f8442b9
2 changed files with 3 additions and 100 deletions

View File

@@ -1,48 +1,10 @@
mod idt; mod idt;
macro_rules! handler {
($name: ident) => {{
#[naked]
extern "C" fn wrapper() -> ! {
unsafe {
asm!("mov rdi, rsp
sub rsp, 8 // align the stack pointer
call $0"
:: "i"($name as extern "C" fn(*const ExceptionStackFrame) -> !)
: "rdi" : "intel");
::core::intrinsics::unreachable();
}
}
wrapper
}}
}
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, divide_by_zero_handler);
idt.set_handler(6, handler!(invalid_opcode_handler));
idt.set_handler(8, double_fault_handler);
idt.set_handler(14, handler_with_error_code!(page_fault_handler));
idt idt
}; };
@@ -54,55 +16,7 @@ pub fn init() {
use vga_buffer::print_error; use vga_buffer::print_error;
extern "C" fn divide_by_zero_handler(stack_frame: *const ExceptionStackFrame) -> ! { extern "C" fn divide_by_zero_handler() -> ! {
unsafe { unsafe { print_error(format_args!("EXCEPTION: DIVIDE BY ZERO")) };
print_error(format_args!("EXCEPTION: DIVIDE BY ZERO\n{:#?}", *stack_frame));
}
loop {} loop {}
} }
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,
*stack_frame));
}
loop {}
}
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: \
{:?}\n{:#?}",
controlregs::cr2(),
PageFaultErrorCode::from_bits(error_code).unwrap(),
*stack_frame));
}
loop {}
}
extern "C" fn double_fault_handler() -> ! {
unsafe { print_error(format_args!("EXCEPTION: DOUBLE FAULT")) };
loop {}
}
#[derive(Debug)]
#[repr(C)]
struct ExceptionStackFrame {
instruction_pointer: u64,
code_segment: u64,
cpu_flags: u64,
stack_pointer: u64,
stack_segment: u64,
}
bitflags! {
flags PageFaultErrorCode: u64 {
const PROTECTION_VIOLATION = 1 << 0,
const CAUSED_BY_WRITE = 1 << 1,
const USER_MODE = 1 << 2,
const MALFORMED_TABLE = 1 << 3,
const INSTRUCTION_FETCH = 1 << 4,
}
}

View File

@@ -11,8 +11,6 @@
#![feature(const_fn, unique)] #![feature(const_fn, unique)]
#![feature(alloc, collections)] #![feature(alloc, collections)]
#![feature(asm)] #![feature(asm)]
#![feature(naked_functions)]
#![feature(core_intrinsics)]
#![no_std] #![no_std]
extern crate rlibc; extern crate rlibc;
@@ -49,22 +47,13 @@ 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();
// provoke a page fault
unsafe { *(0xdeadbeaf as *mut u64) = 42 };
fn divide_by_zero() { fn divide_by_zero() {
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());