Compare commits

..

43 Commits

Author SHA1 Message Date
Philipp Oppermann
87d95dbf0d Update to latest bit_field version
(cherry picked from commit 46100ba24b)
2017-01-25 13:53:10 +01:00
Philipp Oppermann
91672e3cac The ReadWrite flag isn't needed for code segments (#279)
(cherry picked from commit fce85e68ee)
2017-01-18 13:13:59 +01:00
Philipp Oppermann
a91322017e Load null selectors to all data registers
This is required for some instructions such as iretq (ss must be valid or 0).

(cherry picked from commit 8f8b46a9b6)
2017-01-15 14:18:35 +01:00
Philipp Oppermann
f0df9fb937 Data segments aren't needed for 64-bit mode
(cherry picked from commit e179dadf70)
2017-01-15 14:14:47 +01:00
Philipp Oppermann
135d8891f3 Make panic_fmt public to fix private_no_mangle_fns warning
(cherry picked from commit 1be2f42791)
2017-01-04 16:24:35 +01:00
Philipp Oppermann
58de55a2d4 Mark panic_fmt as no_mangle to work around rust-lang/rust#38281
(cherry picked from commit 081105bbb6)
(cherry picked from commit 1acc1ff5806f2fa101f726644d258eb4e4543540)
2017-01-04 16:10:32 +01:00
Philipp Oppermann
94d4f8df8e Update x86 crate to version 0.8.0 (#266)
(cherry picked from commit 02697891e2)
2016-12-21 15:09:45 +01:00
Philipp Oppermann
d5c84a860a Remove cargo workspace (#265)
(cherry picked from commit fcd453ac29)
2016-12-21 14:47:40 +01:00
Philipp Oppermann
ce6b43cc9b Fix panic_fmt arguments: file is a &'static str
(cherry picked from commit e9735738f7)
2016-11-26 12:29:31 +01:00
Philipp Oppermann
090e0785ff Use references for the ExceptionStackFrame argument
We require/assume that these pointers are correct. Using references is cleaner than blindly dereferencing raw pointers.

Important: The Rust book guarantees that: “At runtime, a raw pointer * and a reference pointing to the same piece of data have an identical representation.”
(cherry picked from commit 15feb9a120)
2016-11-01 18:01:24 +01:00
Philipp Oppermann
5ce6655aa1 Remove print_error and use normal println in exception handlers
(cherry picked from commit 9b83b2853e)
2016-10-31 01:32:53 +01:00
Philipp Oppermann
e8d1f96b76 Create a new print function and use it in the print macro
This fixes the deadlock problem, because the format_args is now evaluated before locking the screen writer. So `println!("{:?}", {println!("");})` no longer triggers a deadlock.

(cherry picked from commit d96df500a7)
2016-10-31 01:22:52 +01:00
Philipp Oppermann
e09df45b9b Use volatile operations for accessing the VGA buffer
(cherry picked from commit d7a445b1bb)
2016-10-08 15:55:28 +02:00
Philipp Oppermann
7f80db6fb4 Add missing license headers and update copyright year (#232)
(cherry picked from commit a04924955a)
2016-10-02 18:31:20 +02:00
Philipp Oppermann
c764e6bb43 Add missing license headers (#233)
(cherry picked from commit 0f4d1ce303)
2016-10-02 18:29:52 +02:00
Philipp Oppermann
0ec126fc7d Use crates.io version of multiboot2
(cherry picked from commit c6b1346956)
2016-09-27 13:56:43 +02:00
Calvin Lee
258a7d1aba Fixes #220: BumpAllocator overflow
I also fixed a spelling error and replaced a tab with a space
in the blog post where `allocate()` was created.

(cherry picked from commit 54e02fd6b5)
(cherry picked from commit 3a2f0b33297164ebbfab92ee76881b6c06c4914d)
2016-09-27 13:41:01 +02:00
Philipp Oppermann
6e20fe17db Update bit_field to 0.5.0 and use new API
(cherry picked from commit 6eba03dd58)
2016-09-27 13:38:15 +02:00
Philipp Oppermann
2aeeaecf86 Create a cargo workspace for subcrates
(cherry picked from commit 488a3c16f0)
2016-08-05 11:29:19 +02:00
Philipp Oppermann
e63dda6953 Add missing rsi clobber in code as well
(cherry picked from commit 46d41d9c3d)
2016-08-04 01:13:34 +02:00
Philipp Oppermann
af8578ec78 Fix .travis.yml for tag build 2016-08-03 16:54:27 +02:00
Philipp Oppermann
4c9f7cfbee Update README for better_exception_messages tag 2016-08-03 16:53:44 +02:00
Philipp Oppermann
c26fb53547 Remove blog and scripts subdirs 2016-08-03 16:51:58 +02:00
Philipp Oppermann
7871e4008a Fix github tag link 2016-08-03 16:48:02 +02:00
Philipp Oppermann
e1a90fe3ab Final improvements 2016-08-03 16:42:47 +02:00
Philipp Oppermann
e17fd2d0de Run rustfmt 2016-08-03 16:41:49 +02:00
Philipp Oppermann
2df8517816 Translate error code and print cr2 register 2016-08-03 16:39:38 +02:00
Philipp Oppermann
550a99cfd4 Add and test a handler_with_error_code macro and a page fault handler 2016-08-03 16:36:58 +02:00
Philipp Oppermann
86a757cb61 Add and test an invalid opcode handler function 2016-08-03 16:26:22 +02:00
Philipp Oppermann
bf72618647 Add a handler macro that creates wrapper functions 2016-08-03 16:24:43 +02:00
Philipp Oppermann
308b033ea9 Add a wrapper function and print the exception stack frame 2016-08-03 16:22:59 +02:00
Philipp Oppermann
07bef978ad Silence dead code warnings 2016-08-03 16:00:04 +02:00
Philipp Oppermann
ee48ec5e29 Reset src to master to be able to follow step-by-step 2016-08-03 16:00:04 +02:00
Philipp Oppermann
6590531a41 Provoke a page fault 2016-08-03 16:00:04 +02:00
Philipp Oppermann
df99382cda Print cr2 in page_fault_handler 2016-08-03 16:00:04 +02:00
Philipp Oppermann
979663acda Rename to better-exception-messages 2016-08-03 16:00:04 +02:00
Philipp Oppermann
8bb46c6b62 Finish exceptions diagnostics post 2016-08-03 16:00:03 +02:00
Philipp Oppermann
4eda7993a2 Add handler! macros 2016-08-03 16:00:03 +02:00
Philipp Oppermann
b949fba62f Next section: Debug the failure on real hardware 2016-08-03 14:44:52 +02:00
Philipp Oppermann
83978e8417 Add a ToC 2016-08-03 14:44:52 +02:00
Philipp Oppermann
9113a63f5e Test the two-stage exception handler in code 2016-08-03 14:44:52 +02:00
Philipp Oppermann
9e45cf65bc Describe the two-stage exception handler 2016-08-03 14:44:52 +02:00
Philipp Oppermann
06fb4d6596 WIP code for upcoming post 2016-08-03 14:44:52 +02:00
8 changed files with 19 additions and 95 deletions

View File

@@ -3,15 +3,6 @@ language: rust
rust:
- nightly
cache: cargo
before_script:
- curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=$TRAVIS_RUST_VERSION -y
- source ~/.cargo/env
- rustup component add rust-src
- (test -x $HOME/.cargo/bin/rustfmt || cargo install rustfmt)
- (test -x $HOME/.cargo/bin/xargo || cargo install xargo)
sudo: false
notifications:

View File

@@ -7,7 +7,7 @@ version = "0.1.0"
bit_field = "0.7.0"
bitflags = "0.7.0"
multiboot2 = "0.1.0"
once = "0.3.2"
once = "0.2.1"
rlibc = "0.1.4"
spin = "0.3.4"
volatile = "0.1.0"
@@ -19,10 +19,6 @@ path = "libs/hole_list_allocator"
default-features = false
version = "0.8.0"
[dependencies.lazy_static]
version = "0.2.1"
features = ["spin_no_std"]
[lib]
crate-type = ["staticlib"]

View File

@@ -8,7 +8,7 @@
# except according to those terms.
arch ?= x86_64
target ?= $(arch)-blog_os
target ?= $(arch)-unknown-linux-gnu
kernel := build/kernel-$(arch).bin
iso := build/os-$(arch).iso
@@ -49,7 +49,7 @@ $(kernel): cargo $(rust_os) $(assembly_object_files) $(linker_script)
@ld -n --gc-sections -T $(linker_script) -o $(kernel) $(assembly_object_files) $(rust_os)
cargo:
@xargo build --target $(target)
@cargo build --target $(target)
# compile assembly files
build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm

View File

@@ -1,14 +1,12 @@
# Blog OS (Returning from Exceptions)
[![Build Status](https://travis-ci.org/phil-opp/blog_os.svg?branch=returning_from_exceptions)](https://travis-ci.org/phil-opp/blog_os/branches)
# Blog OS (Better Exception Messages)
[![Build Status](https://travis-ci.org/phil-opp/blog_os.svg?branch=better_exception_messages)](https://travis-ci.org/phil-opp/blog_os/branches)
This repository contains the source code for the [Returning from Exceptions](http://os.phil-opp.com/returning-from-exceptions.html) post of the [Writing an OS in Rust](http://os.phil-opp.com) series.
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.
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
## Building
You need to have `nasm`, `grub-mkrescue`, `xorriso`, `qemu`, a nightly Rust compiler, and [xargo] installed. Then you can run it using `make run`.
[xargo]: https://github.com/japaric/xargo
You need to have `nasm`, `grub-mkrescue`, `xorriso`, `qemu`, and a nightly Rust compiler installed. Then you can run it using `make run`.
Please file an issue if you have any problems.

View File

@@ -1,2 +0,0 @@
[target.x86_64-blog_os.dependencies]
collections = {}

View File

@@ -9,51 +9,17 @@
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
add rdi, 9*8 // calculate exception stack frame pointer
sub rsp, 8 // align the stack pointer
call $0"
:: "i"($name as extern "C" fn(
&ExceptionStackFrame))
&ExceptionStackFrame) -> !)
: "rdi" : "intel");
restore_scratch_registers!();
asm!("iretq" :::: "intel", "volatile");
::core::intrinsics::unreachable();
}
}
@@ -66,19 +32,13 @@ macro_rules! handler_with_error_code {
#[naked]
extern "C" fn wrapper() -> ! {
unsafe {
save_scratch_registers!();
asm!("mov rsi, [rsp + 9*8] // load error code into rsi
asm!("pop rsi // pop 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
add rsp, 8 // undo stack pointer alignment
" :: "i"($name as extern "C" fn(
&ExceptionStackFrame, u64))
call $0"
:: "i"($name as extern "C" fn(
&ExceptionStackFrame, u64) -> !)
: "rdi","rsi" : "intel");
restore_scratch_registers!();
asm!("add rsp, 8 // pop error code
iretq" :::: "intel", "volatile");
::core::intrinsics::unreachable();
}
}
@@ -91,7 +51,6 @@ 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));
@@ -113,18 +72,12 @@ struct ExceptionStackFrame {
stack_segment: u64,
}
extern "C" fn divide_by_zero_handler(stack_frame: &ExceptionStackFrame) {
extern "C" fn divide_by_zero_handler(stack_frame: &ExceptionStackFrame) -> ! {
println!("\nEXCEPTION: DIVIDE BY ZERO\n{:#?}", stack_frame);
loop {}
}
extern "C" fn breakpoint_handler(stack_frame: &ExceptionStackFrame) {
println!("\nEXCEPTION: BREAKPOINT at {:#x}\n{:#?}",
stack_frame.instruction_pointer,
stack_frame);
}
extern "C" fn invalid_opcode_handler(stack_frame: &ExceptionStackFrame) {
extern "C" fn invalid_opcode_handler(stack_frame: &ExceptionStackFrame) -> ! {
println!("\nEXCEPTION: INVALID OPCODE at {:#x}\n{:#?}",
stack_frame.instruction_pointer,
stack_frame);
@@ -141,7 +94,7 @@ bitflags! {
}
}
extern "C" fn page_fault_handler(stack_frame: &ExceptionStackFrame, error_code: u64) {
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{:#?}",

View File

@@ -21,13 +21,10 @@ extern crate spin;
extern crate multiboot2;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate x86;
#[macro_use]
extern crate once;
extern crate bit_field;
#[macro_use]
extern crate lazy_static;
extern crate hole_list_allocator;
extern crate alloc;
@@ -56,8 +53,9 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
// initialize our IDT
interrupts::init();
// trigger a breakpoint exception
unsafe { int!(3) };
// provoke a page fault
unsafe { *(0xdeadbeaf as *mut u64) = 42 };
println!("It did not crash!");
loop {}

View File

@@ -1,10 +0,0 @@
{
"llvm-target": "x86_64-unknown-linux-gnu",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"target-endian": "little",
"target-pointer-width": "64",
"arch": "x86_64",
"os": "none",
"features": "-mmx,-sse,+soft-float",
"disable-redzone": true
}