mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
28 Commits
returning_
...
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 |
12
.travis.yml
12
.travis.yml
@@ -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:
|
||||
@@ -24,4 +15,5 @@ addons:
|
||||
packages:
|
||||
- nasm
|
||||
|
||||
script: make
|
||||
script:
|
||||
- make
|
||||
|
||||
@@ -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"]
|
||||
|
||||
|
||||
4
Makefile
4
Makefile
@@ -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
|
||||
|
||||
10
README.md
10
README.md
@@ -1,14 +1,12 @@
|
||||
# Blog OS (Returning from Exceptions)
|
||||
[](https://travis-ci.org/phil-opp/blog_os/branches)
|
||||
# Blog OS (Catching Exceptions)
|
||||
[](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 [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.**
|
||||
|
||||
## 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.
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
[target.x86_64-blog_os.dependencies]
|
||||
collections = {}
|
||||
@@ -101,13 +101,11 @@ impl EntryOptions {
|
||||
self
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn set_privilege_level(&mut self, dpl: u16) -> &mut Self {
|
||||
self.0.set_bits(13..15, dpl);
|
||||
self
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn set_stack_index(&mut self, index: u16) -> &mut Self {
|
||||
self.0.set_bits(0..3, index);
|
||||
self
|
||||
|
||||
@@ -9,91 +9,11 @@
|
||||
|
||||
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
|
||||
call $0"
|
||||
:: "i"($name as extern "C" fn(
|
||||
&ExceptionStackFrame))
|
||||
: "rdi" : "intel");
|
||||
|
||||
restore_scratch_registers!();
|
||||
asm!("iretq" :::: "intel", "volatile");
|
||||
::core::intrinsics::unreachable();
|
||||
}
|
||||
}
|
||||
wrapper
|
||||
}}
|
||||
}
|
||||
|
||||
macro_rules! handler_with_error_code {
|
||||
($name: ident) => {{
|
||||
#[naked]
|
||||
extern "C" fn wrapper() -> ! {
|
||||
unsafe {
|
||||
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
|
||||
add rsp, 8 // undo stack pointer alignment
|
||||
" :: "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();
|
||||
}
|
||||
}
|
||||
wrapper
|
||||
}}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: idt::Idt = {
|
||||
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));
|
||||
idt.set_handler(0, divide_by_zero_handler);
|
||||
|
||||
idt
|
||||
};
|
||||
@@ -103,50 +23,7 @@ pub fn init() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[repr(C)]
|
||||
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 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) {
|
||||
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);
|
||||
extern "C" fn divide_by_zero_handler() -> ! {
|
||||
println!("EXCEPTION: DIVIDE BY ZERO");
|
||||
loop {}
|
||||
}
|
||||
|
||||
13
src/lib.rs
13
src/lib.rs
@@ -11,8 +11,6 @@
|
||||
#![feature(const_fn, unique)]
|
||||
#![feature(alloc, collections)]
|
||||
#![feature(asm)]
|
||||
#![feature(naked_functions)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![no_std]
|
||||
|
||||
extern crate rlibc;
|
||||
@@ -21,13 +19,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 +51,12 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
||||
// initialize our IDT
|
||||
interrupts::init();
|
||||
|
||||
// trigger a breakpoint exception
|
||||
unsafe { int!(3) };
|
||||
fn divide_by_zero() {
|
||||
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!");
|
||||
loop {}
|
||||
|
||||
@@ -47,12 +47,13 @@ impl AreaFrameAllocator {
|
||||
|
||||
fn choose_next_area(&mut self) {
|
||||
self.current_area = self.areas
|
||||
.clone()
|
||||
.filter(|area| {
|
||||
let address = area.base_addr + area.length - 1;
|
||||
Frame::containing_address(address as usize) >= self.next_free_frame
|
||||
})
|
||||
.min_by_key(|area| area.base_addr);
|
||||
.clone()
|
||||
.filter(|area| {
|
||||
let address = area.base_addr + area.length - 1;
|
||||
Frame::containing_address(address as usize) >=
|
||||
self.next_free_frame
|
||||
})
|
||||
.min_by_key(|area| area.base_addr);
|
||||
|
||||
if let Some(area) = self.current_area {
|
||||
let start_frame = Frame::containing_address(area.base_addr as usize);
|
||||
|
||||
@@ -24,15 +24,15 @@ pub fn init(boot_info: &BootInformation) {
|
||||
let elf_sections_tag = boot_info.elf_sections_tag().expect("Elf sections tag required");
|
||||
|
||||
let kernel_start = elf_sections_tag.sections()
|
||||
.filter(|s| s.is_allocated())
|
||||
.map(|s| s.addr)
|
||||
.min()
|
||||
.unwrap();
|
||||
.filter(|s| s.is_allocated())
|
||||
.map(|s| s.addr)
|
||||
.min()
|
||||
.unwrap();
|
||||
let kernel_end = elf_sections_tag.sections()
|
||||
.filter(|s| s.is_allocated())
|
||||
.map(|s| s.addr + s.size)
|
||||
.max()
|
||||
.unwrap();
|
||||
.filter(|s| s.is_allocated())
|
||||
.map(|s| s.addr + s.size)
|
||||
.max()
|
||||
.unwrap();
|
||||
|
||||
println!("kernel start: {:#x}, kernel end: {:#x}",
|
||||
kernel_start,
|
||||
|
||||
@@ -69,9 +69,9 @@ impl Mapper {
|
||||
};
|
||||
|
||||
p3.and_then(|p3| p3.next_table(page.p3_index()))
|
||||
.and_then(|p2| p2.next_table(page.p2_index()))
|
||||
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
||||
.or_else(huge_page)
|
||||
.and_then(|p2| p2.next_table(page.p2_index()))
|
||||
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
||||
.or_else(huge_page)
|
||||
}
|
||||
|
||||
pub fn map_to<A>(&mut self, page: Page, frame: Frame, flags: EntryFlags, allocator: &mut A)
|
||||
@@ -105,10 +105,10 @@ impl Mapper {
|
||||
assert!(self.translate(page.start_address()).is_some());
|
||||
|
||||
let p1 = self.p4_mut()
|
||||
.next_table_mut(page.p4_index())
|
||||
.and_then(|p3| p3.next_table_mut(page.p3_index()))
|
||||
.and_then(|p2| p2.next_table_mut(page.p2_index()))
|
||||
.expect("mapping code does not support huge pages");
|
||||
.next_table_mut(page.p4_index())
|
||||
.and_then(|p3| p3.next_table_mut(page.p3_index()))
|
||||
.and_then(|p2| p2.next_table_mut(page.p2_index()))
|
||||
.expect("mapping code does not support huge pages");
|
||||
let frame = p1[page.p1_index()].pointed_frame().unwrap();
|
||||
p1[page.p1_index()].set_unused();
|
||||
unsafe { ::x86::shared::tlb::flush(page.start_address()) };
|
||||
|
||||
@@ -180,7 +180,7 @@ pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation) -> Ac
|
||||
|
||||
active_table.with(&mut new_table, &mut temporary_page, |mapper| {
|
||||
let elf_sections_tag = boot_info.elf_sections_tag()
|
||||
.expect("Memory map tag required");
|
||||
.expect("Memory map tag required");
|
||||
|
||||
// identity map the allocated kernel sections
|
||||
for section in elf_sections_tag.sections() {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user