mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Update some of the following posts to use blog_os::init
This commit is contained in:
@@ -35,7 +35,7 @@ Let's provoke a double fault by triggering an exception for that we didn't defin
|
|||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
blog_os::interrupts::init_idt();
|
blog_os::init();
|
||||||
|
|
||||||
// trigger a page fault
|
// trigger a page fault
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -162,7 +162,7 @@ Let's try it ourselves! We can easily provoke a kernel stack overflow by calling
|
|||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
blog_os::interrupts::init_idt();
|
blog_os::init();
|
||||||
|
|
||||||
fn stack_overflow() {
|
fn stack_overflow() {
|
||||||
stack_overflow(); // for each recursion, the return address is pushed
|
stack_overflow(); // for each recursion, the return address is pushed
|
||||||
@@ -298,7 +298,7 @@ We use `lazy_static` again, because Rust's const evaluator is not powerful enoug
|
|||||||
|
|
||||||
#### Loading the GDT
|
#### Loading the GDT
|
||||||
|
|
||||||
To load our GDT we create a new `gdt::init` function, that we call from our `_start` function:
|
To load our GDT we create a new `gdt::init` function, that we call from our `init` function:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// in src/gdt.rs
|
// in src/gdt.rs
|
||||||
@@ -307,22 +307,15 @@ pub fn init() {
|
|||||||
GDT.load();
|
GDT.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
// in src/main.rs
|
// in src/lib.rs
|
||||||
|
|
||||||
#[cfg(not(test))]
|
pub fn init() {
|
||||||
#[no_mangle]
|
gdt::init();
|
||||||
pub extern "C" fn _start() -> ! {
|
interrupts::init_idt();
|
||||||
println!("Hello World{}", "!");
|
|
||||||
|
|
||||||
blog_os::gdt::init();
|
|
||||||
blog_os::interrupts::init_idt();
|
|
||||||
|
|
||||||
[…]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Now our GDT is loaded, but we still see the boot loop on stack overflow.
|
Now our GDT is loaded (since the `_start` function calls `init`), but we still see the boot loop on stack overflow.
|
||||||
|
|
||||||
### The final Steps
|
### The final Steps
|
||||||
|
|
||||||
|
|||||||
@@ -109,24 +109,15 @@ We're setting the offsets for the pics to the range 32–47 as we noted above. B
|
|||||||
|
|
||||||
[spin mutex lock]: https://docs.rs/spin/0.4.8/spin/struct.Mutex.html#method.lock
|
[spin mutex lock]: https://docs.rs/spin/0.4.8/spin/struct.Mutex.html#method.lock
|
||||||
|
|
||||||
We can now initialize the 8259 PIC from our `_start` function:
|
We can now initialize the 8259 PIC in our `init` function:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// in src/main.rs
|
// in src/lib.rs
|
||||||
|
|
||||||
#[cfg(not(test))]
|
pub fn init() {
|
||||||
#[no_mangle]
|
gdt::init();
|
||||||
pub extern "C" fn _start() -> ! {
|
interrupts::init_idt();
|
||||||
use blog_os::interrupts::PICS; // new
|
unsafe { interrupts::PICS.lock().initialize() }; // new
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
|
||||||
|
|
||||||
blog_os::gdt::init();
|
|
||||||
blog_os::interrupts::init_idt();
|
|
||||||
unsafe { PICS.lock().initialize() }; // new
|
|
||||||
|
|
||||||
println!("It did not crash!");
|
|
||||||
loop {}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -141,22 +132,13 @@ If all goes well we should continue to see the "It did not crash" message when e
|
|||||||
Until now nothing happened because interrupts are still disabled in the CPU configuration. This means that the CPU does not listen to the interrupt controller at all, so no interrupts can reach the CPU. Let's change that:
|
Until now nothing happened because interrupts are still disabled in the CPU configuration. This means that the CPU does not listen to the interrupt controller at all, so no interrupts can reach the CPU. Let's change that:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// in src/main.rs
|
// in src/lib.rs
|
||||||
|
|
||||||
#[cfg(not(test))]
|
pub fn init() {
|
||||||
#[no_mangle]
|
gdt::init();
|
||||||
pub extern "C" fn _start() -> ! {
|
interrupts::init_idt();
|
||||||
use blog_os::interrupts::PICS;
|
unsafe { interrupts::PICS.lock().initialize() };
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
|
||||||
|
|
||||||
blog_os::gdt::init();
|
|
||||||
blog_os::interrupts::init_idt();
|
|
||||||
unsafe { PICS.lock().initialize() };
|
|
||||||
x86_64::instructions::interrupts::enable(); // new
|
x86_64::instructions::interrupts::enable(); // new
|
||||||
|
|
||||||
println!("It did not crash!");
|
|
||||||
loop {}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -305,16 +305,9 @@ Now we can try to access some memory outside our kernel:
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
use blog_os::interrupts::PICS;
|
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
// set up the IDT first, otherwise we would enter a boot loop instead of
|
blog_os::init();
|
||||||
// invoking our page fault handler
|
|
||||||
blog_os::gdt::init();
|
|
||||||
blog_os::interrupts::init_idt();
|
|
||||||
unsafe { PICS.lock().initialize() };
|
|
||||||
x86_64::instructions::interrupts::enable();
|
|
||||||
|
|
||||||
// new
|
// new
|
||||||
let ptr = 0xdeadbeaf as *mut u32;
|
let ptr = 0xdeadbeaf as *mut u32;
|
||||||
@@ -354,7 +347,9 @@ Let's try to take a look at the page tables that our kernel runs on:
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
[…] // initialize GDT, IDT, PICS
|
println!("Hello World{}", "!");
|
||||||
|
|
||||||
|
blog_os::init();
|
||||||
|
|
||||||
use x86_64::registers::control::Cr3;
|
use x86_64::registers::control::Cr3;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user