Compare commits

..

130 Commits

Author SHA1 Message Date
Philipp Oppermann
f4b961ad50 Update to latest bit_field version
(cherry picked from commit 46100ba24b)
2017-01-25 13:54:07 +01:00
Philipp Oppermann
f11a564f5a The ReadWrite flag isn't needed for code segments (#279)
(cherry picked from commit fce85e68ee)
2017-01-18 13:14:02 +01:00
Philipp Oppermann
24e9bcb659 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:39 +01:00
Philipp Oppermann
5a87c688e9 Data segments aren't needed for 64-bit mode
(cherry picked from commit e179dadf70)
2017-01-15 14:14:50 +01:00
Philipp Oppermann
4ce38163ba Make panic_fmt public to fix private_no_mangle_fns warning
(cherry picked from commit 1be2f42791)
2017-01-04 16:24:39 +01:00
Philipp Oppermann
229eb78279 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:36 +01:00
Philipp Oppermann
6f259de5db Add a Xargo.toml for xargo 0.3.0
(cherry picked from commit 14babd8c2a)
2016-12-29 16:46:45 +01:00
Philipp Oppermann
0b9c3734b2 Update x86 crate to version 0.8.0 (#266)
(cherry picked from commit 02697891e2)
2016-12-21 15:10:29 +01:00
Philipp Oppermann
7ee6683cfa Remove cargo workspace (#265)
(cherry picked from commit fcd453ac29)
2016-12-21 14:48:31 +01:00
Philipp Oppermann
f643b2fd7b Test for rustfmt/xargo instead of using || true (#257)
(cherry picked from commit 9583cdea28)
2016-11-30 16:30:13 +01:00
Philipp Oppermann
0fa05fb187 Fix CI for xargo 0.2.0
(cherry picked from commit c7268cff3b)
2016-11-26 12:38:29 +01:00
Philipp Oppermann
36abafab8c Fix panic_fmt arguments: file is a &'static str
(cherry picked from commit e9735738f7)
2016-11-26 12:29:35 +01:00
Philipp Oppermann
29505630ea 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:02:09 +01:00
Philipp Oppermann
33a96b26d3 Remove print_error and use normal println in exception handlers
(cherry picked from commit 9b83b2853e)
2016-10-31 01:35:05 +01:00
Philipp Oppermann
987c56a153 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:20:22 +01:00
Philipp Oppermann
11516adc53 Use volatile operations for accessing the VGA buffer
(cherry picked from commit d7a445b1bb)
2016-10-08 15:55:30 +02:00
Philipp Oppermann
a5c96a048e Add missing license headers and update copyright year (#232)
(cherry picked from commit a04924955a)
2016-10-02 18:31:22 +02:00
Philipp Oppermann
3dd5411608 Add missing license headers (#233)
(cherry picked from commit 0f4d1ce303)
2016-10-02 18:29:54 +02:00
Philipp Oppermann
287e43a5c4 Use crates.io version of multiboot2
(cherry picked from commit c6b1346956)
2016-09-27 13:57:25 +02:00
Calvin Lee
0cca3ae90c 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:05 +02:00
Philipp Oppermann
7ce8d08b2d Update bit_field to 0.5.0 and use new API
(cherry picked from commit 6eba03dd58)
2016-09-27 13:38:18 +02:00
Philipp Oppermann
bc4ebcf4e0 Fix .travis.yml for tag build 2016-09-21 15:24:06 +02:00
Philipp Oppermann
998064ae2c Adjust README for returning_from_exceptions tag 2016-09-21 15:24:06 +02:00
Philipp Oppermann
d15f51d386 Remove blog and scripts subdirs 2016-09-21 15:22:40 +02:00
Philipp Oppermann
9f7994772b Add ~/.cargo/bin to PATH 2016-09-21 15:17:39 +02:00
Philipp Oppermann
bd285b1731 Use kalakris-cmake for cmake 2.8.11 2016-09-21 14:54:49 +02:00
Philipp Oppermann
43ed189b35 Fix travis: Install xargo in before_script 2016-09-21 14:45:03 +02:00
Philipp Oppermann
5c05f7c438 Add new post to README 2016-09-21 14:41:47 +02:00
Philipp Oppermann
00701ca474 Add code for “Returning from Exceptions” 2016-09-21 14:34:32 +02:00
Philipp Oppermann
a4054c478c We require xargo now 2016-09-21 14:33:09 +02:00
Philipp Oppermann
f38805c3d3 Final improvements 2016-09-21 14:32:42 +02:00
Philipp Oppermann
706c48a410 Add source files for the diagrams 2016-09-20 17:35:39 +02:00
Philipp Oppermann
5a00bddd7c Write introduction 2016-09-20 17:31:28 +02:00
Philipp Oppermann
626763d452 Write “What's next?” 2016-09-20 17:16:13 +02:00
Philipp Oppermann
e4ae45e863 Write “Exceptions with Error Codes” section 2016-09-20 16:17:22 +02:00
Philipp Oppermann
0bb7f1a53e New post about returning from exceptions 2016-09-20 02:47:55 +02:00
Philipp Oppermann
8cee4161de Update to hugo 0.16 2016-09-18 19:26:45 +02:00
Philipp Oppermann
9b842e9578 Rename posts: Use ascending numbers instead of date 2016-09-18 19:25:08 +02:00
dns2utf8
917eb78255 Add mtools to readme (#213) 2016-09-12 11:22:42 +02:00
Philipp Oppermann
9830f4ef48 Check rustfmt formatting in travis builds (#210)
* Install rustfmt

* Check formatting in travis builds
2016-08-27 13:45:18 +02:00
Philipp Oppermann
68921b625d Cache cargo directories (#209) 2016-08-27 13:11:44 +02:00
Philipp Oppermann
18d02e01e1 Merge pull request #208 from KarboniteKream/master
Fix various typos
2016-08-20 14:05:19 +02:00
Klemen Košir
30a8c06295 reexport -> re-export 2016-08-20 13:04:26 +02:00
Klemen Košir
e56df793d4 Fix an incorrect function name 2016-08-20 13:03:54 +02:00
Klemen Košir
02ef52c677 Fix an incorrect year of update in post title 2016-08-20 13:03:25 +02:00
Klemen Košir
a33bf9c889 Fix a function name typo 2016-08-20 13:02:41 +02:00
Philipp Oppermann
7c0082ecb4 Fix broken unwinding link (link to nomicon now) (#207) 2016-08-17 14:35:42 +02:00
Jeremy Ruten
c6f35a953a Fix code to trigger memcpy linker errors (#206)
Fixes #205
2016-08-16 12:49:36 +02:00
Leon Loopik
98c3b2a4a1 Fix incorrect uses of therefor (#204)
* Fix typo (therefor -> therefore)

* Rephrased a sentence

* Added backticks back in
2016-08-10 12:22:53 +02:00
Philipp Oppermann
462a38d0ca Merge pull request #203 from phil-opp/kernel-heap-improvements
Various improvements to “Kernel Heap”
2016-08-05 11:25:06 +02:00
Philipp Oppermann
72199bb7d7 Fix link and link to both “page tables” and “remap the kernel” 2016-08-05 11:22:20 +02:00
Philipp Oppermann
86efbda8d8 Remove panic=abort note 2016-08-05 11:22:20 +02:00
Philipp Oppermann
a5acfee7c3 Improve some heading levels 2016-08-05 11:22:20 +02:00
Philipp Oppermann
9603ac1ccc Create a cargo workspace for subcrates (#202) 2016-08-05 11:21:57 +02:00
Philipp Oppermann
a01034cbfc Merge pull request #201 from phil-opp/lazy_static
Use upstream lazy_static and upgrade `once` to latest version
2016-08-04 20:14:55 +02:00
Philipp Oppermann
4a1fa3a65a Explain how to add lazy_static to the main crate 2016-08-04 20:09:25 +02:00
Philipp Oppermann
3bd5e56e3f Use upstream lazy_static and update once crate 2016-08-04 20:09:25 +02:00
Mark Schmale
74cadb15a3 removed extra "use" (#197)
I'm not a native speaker so I'm not 100% sure but I guess this "use" shouldn't be there.
2016-08-04 13:58:49 +02:00
Philipp Oppermann
9ec1860f02 Merge pull request #196 from phil-opp/p1
Fix clobber syntax and add missing rsi clobber in code
2016-08-04 01:12:50 +02:00
Philipp Oppermann
46d41d9c3d Add missing rsi clobber in code as well 2016-08-04 01:07:51 +02:00
Philipp Oppermann
cde3f535c7 Fix clobber syntax in post 2016-08-04 01:07:39 +02:00
Philipp Oppermann
3a62b42414 Link to nightly docs as stable still has wrong clobber syntax
See #195
2016-08-04 01:00:55 +02:00
Leon Loopik
ca925c49e0 Add rsi to the clobber list (#195)
In the second macro (handler_with_error_code), we pop the error code into rsi, this means we are changing the value of rsi in the assembly block, so we should add it to the clobbers.

I am quite new to this, so please close this request if I am completely wrong.

(Side question: An other thing I came across while trying to figure out if this was correct, is that every documentation says you should add curly brackets around register names, can someone tell me why this is not done here?)
2016-08-04 00:19:12 +02:00
Philipp Oppermann
5e6a8cb69b Link gitter chat 2016-08-03 20:48:24 +02:00
Philipp Oppermann
88fe633b87 Merge pull request #194 from phil-opp/better-exception-messages
New post: Better Exception Messages
2016-08-03 17:03:13 +02:00
Philipp Oppermann
3312478f71 Include “Better Exception Messages” in README 2016-08-03 17:00:18 +02:00
Philipp Oppermann
60a58a8c9c Fix github tag link 2016-08-03 17:00:18 +02:00
Philipp Oppermann
2b629415a4 Final improvements 2016-08-03 17:00:18 +02:00
Philipp Oppermann
d13460812d Run rustfmt 2016-08-03 17:00:18 +02:00
Philipp Oppermann
a9319ea83e Translate error code and print cr2 register 2016-08-03 17:00:18 +02:00
Philipp Oppermann
69f1b58bb0 Add and test a handler_with_error_code macro and a page fault handler 2016-08-03 17:00:18 +02:00
Philipp Oppermann
df1e39edb2 Add and test an invalid opcode handler function 2016-08-03 17:00:18 +02:00
Philipp Oppermann
71ebb23ec3 Add a handler macro that creates wrapper functions 2016-08-03 17:00:18 +02:00
Philipp Oppermann
e7ee1faaba Add a wrapper function and print the exception stack frame 2016-08-03 17:00:18 +02:00
Philipp Oppermann
d97a8a3210 Silence dead code warnings 2016-08-03 17:00:18 +02:00
Philipp Oppermann
297f8442b9 Reset src to master to be able to follow step-by-step 2016-08-03 17:00:18 +02:00
Philipp Oppermann
398ba7a424 Provoke a page fault 2016-08-03 17:00:18 +02:00
Philipp Oppermann
261f92e2e5 Print cr2 in page_fault_handler 2016-08-03 17:00:18 +02:00
Philipp Oppermann
1ba595aab3 Rename to better-exception-messages 2016-08-03 17:00:18 +02:00
Philipp Oppermann
710cebe695 Finish exceptions diagnostics post 2016-08-03 17:00:18 +02:00
Philipp Oppermann
e9d2d594ab Add handler! macros 2016-08-03 17:00:18 +02:00
Philipp Oppermann
f14876dfff Next section: Debug the failure on real hardware 2016-08-03 17:00:18 +02:00
Philipp Oppermann
b1de7a9100 Add a ToC 2016-08-03 17:00:18 +02:00
Philipp Oppermann
40b565947b Test the two-stage exception handler in code 2016-08-03 17:00:18 +02:00
Philipp Oppermann
e8220b3781 Describe the two-stage exception handler 2016-08-03 17:00:18 +02:00
Philipp Oppermann
2e0f4f9161 Begin code for upcoming post 2016-08-03 17:00:11 +02:00
Philipp Oppermann
95c0452bcb Begin new post 2016-08-03 14:44:52 +02:00
Philipp Oppermann
68e2495d9c Rename modifying-page-tables.md to page-tables.md 2016-08-03 14:43:01 +02:00
Philipp Oppermann
9469dc5891 Remove coarse class from ToCs
We will add it dynamically via javascript when the Toc is higher than the viewport.
2016-07-28 19:42:37 +02:00
Anton Strömkvist
6b2f1281ae Fix hex constant (#193)
I assume these were in the wrong order since all the others looked this way :)
2016-07-28 18:01:42 +02:00
Philipp Oppermann
826e303ed0 Merge pull request #192 from phil-opp/post-toc
Add tables of contents to all blog posts
2016-07-17 17:14:50 +02:00
Philipp Oppermann
d4e82b0aa4 Improve heading levels 2016-07-17 17:10:42 +02:00
Philipp Oppermann
bc6f496eb5 Add tables of contents to all blog posts 2016-07-17 17:10:23 +02:00
Philipp Oppermann
62dd13b2e7 Merge pull request #190 from phil-opp/p1
Add aligned .got and .got.plt sections to linker script
2016-07-16 17:22:25 +02:00
Philipp Oppermann
68c1e42356 Update linker script in post (add .got and .got.plt) 2016-07-14 18:11:58 +02:00
Philipp Oppermann
2445b47661 Add aligned .got and .got.plt sections to linker script 2016-07-14 18:07:26 +02:00
Philipp Oppermann
d64e4ba2fc Update x86 crate to 0.7.1 (#189) 2016-07-14 11:21:04 +02:00
Aaron Hill
48e7faa521 Add ENTRY(start) to 'Allocating Frames' blog post linker script (#188) 2016-06-29 09:19:08 +02:00
Philipp Oppermann
f8c5bc5dec Merge pull request #187 from phil-opp/divide_by_zero
Catch divide error instead of page fault
2016-06-25 17:13:29 +02:00
Philipp Oppermann
ca438d9740 Revise plans for the upcoming post 2016-06-25 17:12:04 +02:00
Philipp Oppermann
702e49eaf7 Catch divide-by-zero errors and introduce inline assembly
This commit updates the “Catching Exceptions” post to catch divide-by-zero errors instead of page faults. The reason is a subtle stack alignment bug, which is somehow caused by the additional error code on a page fault. This commit also adds a short introduction to inline assembly.
2016-06-25 17:12:04 +02:00
Philipp Oppermann
e8b3a1fff1 Catch divide error instead of page fault
The divide error pushes no error code. Thus we avoid stack misalignment (see #184).
2016-06-25 17:12:04 +02:00
Philipp Oppermann
c9d8afe434 Run rustfmt 2016-06-25 17:08:26 +02:00
Philipp Oppermann
2f9e6cc0d0 Minor improvements to catching exceptions post 2016-06-25 17:06:57 +02:00
Philipp Oppermann
84ba085a0a The section will be only about exceptions (#186) 2016-06-24 13:59:17 +02:00
Philipp Oppermann
b7a47ee001 Add gitter badge (#183) 2016-06-24 13:54:35 +02:00
Philipp Oppermann
e4cc5b84f1 Merge pull request #185 from phil-opp/unwind_resume
Create dummy _Unwind_Resume function to fix libcore linker errors
2016-06-24 13:44:54 +02:00
Philipp Oppermann
f00360d553 Add and explain dummy _Unwind_Resume function in “Set Up Rust” post 2016-06-24 13:37:03 +02:00
Philipp Oppermann
952f7ed5e3 Create dummy _Unwind_Resume function to fix libcore linker errors 2016-06-24 13:37:02 +02:00
Philipp Oppermann
76b83c623e Revert "Remove unneeded gcc_except_table section" (#180) 2016-06-13 18:36:01 +02:00
Philipp Oppermann
20d9037740 Merge pull request #178 from phil-opp/patch5
Remove unneeded gcc_except_table section
2016-06-12 16:36:00 +02:00
Philipp Oppermann
1fb6369f61 Remove unneeded gcc_except_table section from blog post 2016-06-12 16:33:55 +02:00
Philipp Oppermann
1083ac9bac Remove unneeded gcc_except_table section 2016-06-12 16:32:34 +02:00
Ryan Campbell
793eeebfb3 Add missing open brace (#177) 2016-06-10 20:36:33 +02:00
Steve Klabnik
1183f5d1e1 Update section header to reflect changes in the text (#176) 2016-06-07 21:48:54 +02:00
Philipp Oppermann
bddf35f365 Add missing link to bit_field crate (#174) 2016-06-01 01:21:16 +02:00
Jordan Danford
bf0e70746d Change 'boot load' to 'boot loop' in 'Remap the Kernel' section (#173) 2016-05-31 09:11:14 +02:00
Homu
c22e1e8bb1 Auto merge of #172 - phil-opp:phil-opp-patch-1, r=phil-opp
Recommend rustup instead of multirust
2016-05-30 23:58:02 +09:00
Philipp Oppermann
4fae6e4731 Recommend rustup instead of multirust 2016-05-30 16:57:28 +02:00
Philipp Oppermann
c2336ceece Make cross-compile-* and set-up-gdb pages additional resourses 2016-05-29 19:58:44 +02:00
Philipp Oppermann
2f0a3459ad Merge pull request #170 from phil-opp/cargo-panic-abort
Use the `panic=abort` option instead of `-Z no-landing-pads`
2016-05-29 18:49:30 +02:00
Philipp Oppermann
0f5ec6f129 Update “Kernel Heap”: The _Unwind_Resume linker error shouldn't occur anymore 2016-05-29 18:47:38 +02:00
Philipp Oppermann
5d13ff7c3a Add update note 2016-05-29 18:38:14 +02:00
Philipp Oppermann
d7b05bc6b4 Update “Set Up Rust” post to use the cargo panic option 2016-05-29 18:35:16 +02:00
Philipp Oppermann
f4eb589f19 We don't need _Unwind_Resume anymore 2016-05-29 18:00:58 +02:00
Philipp Oppermann
725c452157 Use the panic=abort option instead of -Z no-landing-pads 2016-05-29 18:00:29 +02:00
Philipp Oppermann
01f9f1056c Merge pull request #169 from phil-opp/missing-pub
Make methods of EntryOptions public
2016-05-29 17:26:48 +02:00
Philipp Oppermann
9ea7ee6f57 Make methods of EntryOptions public in post 2016-05-29 17:25:11 +02:00
Philipp Oppermann
480265872d Make methods of EntryOptions public 2016-05-29 17:24:21 +02:00
Philipp Oppermann
165bf096a7 Merge pull request #164 from phil-opp/catching-exceptions
Add new post about “Catching Exceptions”
2016-05-28 16:00:31 +02:00
13 changed files with 192 additions and 41 deletions

View File

@@ -3,6 +3,15 @@ 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:
@@ -15,5 +24,4 @@ addons:
packages:
- nasm
script:
- make
script: make

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.2.1"
once = "0.3.2"
rlibc = "0.1.4"
spin = "0.3.4"
volatile = "0.1.0"
@@ -19,6 +19,10 @@ 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)-unknown-linux-gnu
target ?= $(arch)-blog_os
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:
@cargo build --target $(target)
@xargo build --target $(target)
# compile assembly files
build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm

View File

@@ -1,12 +1,14 @@
# Blog OS (Catching Exceptions)
[![Build Status](https://travis-ci.org/phil-opp/blog_os.svg?branch=catching_exceptions)](https://travis-ci.org/phil-opp/blog_os/branches)
# 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)
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.
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.
**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`, and a nightly Rust compiler installed. Then you can run it using `make run`.
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
Please file an issue if you have any problems.

2
Xargo.toml Normal file
View File

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

View File

@@ -101,11 +101,13 @@ 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

View File

@@ -9,11 +9,91 @@
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, divide_by_zero_handler);
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
};
@@ -23,7 +103,50 @@ pub fn init() {
IDT.load();
}
extern "C" fn divide_by_zero_handler() -> ! {
println!("EXCEPTION: DIVIDE BY ZERO");
#[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);
loop {}
}

View File

@@ -11,6 +11,8 @@
#![feature(const_fn, unique)]
#![feature(alloc, collections)]
#![feature(asm)]
#![feature(naked_functions)]
#![feature(core_intrinsics)]
#![no_std]
extern crate rlibc;
@@ -19,10 +21,13 @@ 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;
@@ -51,12 +56,8 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
// initialize our IDT
interrupts::init();
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());
// trigger a breakpoint exception
unsafe { int!(3) };
println!("It did not crash!");
loop {}

View File

@@ -47,13 +47,12 @@ 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);

View File

@@ -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,

View File

@@ -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()) };

View File

@@ -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() {

10
x86_64-blog_os.json Normal file
View File

@@ -0,0 +1,10 @@
{
"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
}