mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Compare commits
28 Commits
better_exc
...
catching_e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df3917dfbf | ||
|
|
9d4a761be7 | ||
|
|
43ef84d68a | ||
|
|
b3ddd18e7a | ||
|
|
d905671f45 | ||
|
|
a46b3a13c4 | ||
|
|
7c8f62d312 | ||
|
|
fdec350e85 | ||
|
|
d0bb3f847a | ||
|
|
041aa2bfe6 | ||
|
|
02b8078e1d | ||
|
|
351ae60205 | ||
|
|
b050b475f7 | ||
|
|
29fd8983c5 | ||
|
|
40bc3e9059 | ||
|
|
30a75ec760 | ||
|
|
19f0b5443a | ||
|
|
689bd2e3e2 | ||
|
|
f5af9e25e4 | ||
|
|
0a7843abd9 | ||
|
|
dcab71016c | ||
|
|
3f4f2b8ac2 | ||
|
|
428f97d049 | ||
|
|
69b22d1fc0 | ||
|
|
20f7e5472b | ||
|
|
534d23c80f | ||
|
|
8c8f9dbb68 | ||
|
|
77f3ca3483 |
@@ -15,4 +15,5 @@ addons:
|
|||||||
packages:
|
packages:
|
||||||
- nasm
|
- nasm
|
||||||
|
|
||||||
script: make
|
script:
|
||||||
|
- make
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Blog OS (Better Exception Messages)
|
# Blog OS (Catching Exceptions)
|
||||||
[](https://travis-ci.org/phil-opp/blog_os/branches)
|
[](https://travis-ci.org/phil-opp/blog_os/branches)
|
||||||
|
|
||||||
This repository contains the source code for the [Better Exception Messages](http://os.phil-opp.com/better-exception-messages.html) post of the [Writing an OS in Rust](http://os.phil-opp.com) series.
|
This repository contains the source code for the [Catching Exceptions](http://os.phil-opp.com/catching-exceptions.html) post of the [Writing an OS in Rust](http://os.phil-opp.com) series.
|
||||||
|
|
||||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||||
|
|
||||||
|
|||||||
@@ -101,13 +101,11 @@ impl EntryOptions {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn set_privilege_level(&mut self, dpl: u16) -> &mut Self {
|
pub fn set_privilege_level(&mut self, dpl: u16) -> &mut Self {
|
||||||
self.0.set_bits(13..15, dpl);
|
self.0.set_bits(13..15, dpl);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn set_stack_index(&mut self, index: u16) -> &mut Self {
|
pub fn set_stack_index(&mut self, index: u16) -> &mut Self {
|
||||||
self.0.set_bits(0..3, index);
|
self.0.set_bits(0..3, index);
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -9,50 +9,11 @@
|
|||||||
|
|
||||||
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(
|
|
||||||
&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(
|
|
||||||
&ExceptionStackFrame, u64) -> !)
|
|
||||||
: "rdi","rsi" : "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(14, handler_with_error_code!(page_fault_handler));
|
|
||||||
|
|
||||||
idt
|
idt
|
||||||
};
|
};
|
||||||
@@ -62,44 +23,7 @@ pub fn init() {
|
|||||||
IDT.load();
|
IDT.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
extern "C" fn divide_by_zero_handler() -> ! {
|
||||||
#[repr(C)]
|
println!("EXCEPTION: DIVIDE BY ZERO");
|
||||||
struct ExceptionStackFrame {
|
|
||||||
instruction_pointer: u64,
|
|
||||||
code_segment: u64,
|
|
||||||
cpu_flags: u64,
|
|
||||||
stack_pointer: u64,
|
|
||||||
stack_segment: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" fn divide_by_zero_handler(stack_frame: &ExceptionStackFrame) -> ! {
|
|
||||||
println!("\nEXCEPTION: DIVIDE BY ZERO\n{:#?}", stack_frame);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" fn invalid_opcode_handler(stack_frame: &ExceptionStackFrame) -> ! {
|
|
||||||
println!("\nEXCEPTION: INVALID OPCODE at {:#x}\n{:#?}",
|
|
||||||
stack_frame.instruction_pointer,
|
|
||||||
stack_frame);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" fn page_fault_handler(stack_frame: &ExceptionStackFrame, error_code: u64) -> ! {
|
|
||||||
use x86::shared::control_regs;
|
|
||||||
println!("\nEXCEPTION: PAGE FAULT while accessing {:#x}\nerror code: \
|
|
||||||
{:?}\n{:#?}",
|
|
||||||
unsafe { control_regs::cr2() },
|
|
||||||
PageFaultErrorCode::from_bits(error_code).unwrap(),
|
|
||||||
stack_frame);
|
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -53,9 +51,12 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
|||||||
// initialize our IDT
|
// initialize our IDT
|
||||||
interrupts::init();
|
interrupts::init();
|
||||||
|
|
||||||
// provoke a page fault
|
fn divide_by_zero() {
|
||||||
unsafe { *(0xdeadbeaf as *mut u64) = 42 };
|
unsafe { asm!("mov dx, 0; div dx" ::: "ax", "dx" : "volatile", "intel") }
|
||||||
|
}
|
||||||
|
|
||||||
|
// provoke a divide by zero fault inside println
|
||||||
|
println!("{:?}", divide_by_zero());
|
||||||
|
|
||||||
println!("It did not crash!");
|
println!("It did not crash!");
|
||||||
loop {}
|
loop {}
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ impl AreaFrameAllocator {
|
|||||||
.clone()
|
.clone()
|
||||||
.filter(|area| {
|
.filter(|area| {
|
||||||
let address = area.base_addr + area.length - 1;
|
let address = area.base_addr + area.length - 1;
|
||||||
Frame::containing_address(address as usize) >= self.next_free_frame
|
Frame::containing_address(address as usize) >=
|
||||||
|
self.next_free_frame
|
||||||
})
|
})
|
||||||
.min_by_key(|area| area.base_addr);
|
.min_by_key(|area| area.base_addr);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user