mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Compare commits
21 Commits
27b56832c1
...
post-04
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb5899e437 | ||
|
|
93d0daa1e0 | ||
|
|
83e6c0bc00 | ||
|
|
2b11ad8397 | ||
|
|
fa51f3adbf | ||
|
|
ceb91f955a | ||
|
|
3340babf51 | ||
|
|
37a0ae0097 | ||
|
|
6fc9fbfb7d | ||
|
|
1f097bba71 | ||
|
|
99fbbad9b0 | ||
|
|
94d14d8d62 | ||
|
|
ebfc7c5f7e | ||
|
|
6f0be666f3 | ||
|
|
d9fa84edd6 | ||
|
|
d1bf96108a | ||
|
|
97de98df33 | ||
|
|
9684f75819 | ||
|
|
d6cc562907 | ||
|
|
b44d8ab7fc | ||
|
|
c551fec1ce |
71
Cargo.lock
generated
71
Cargo.lock
generated
@@ -4,15 +4,21 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bit_field"
|
name = "bit_field"
|
||||||
version = "0.10.1"
|
version = "0.10.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
|
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.2.1"
|
version = "1.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a65b545ab31d687cff52899d4890855fec459eb6afe0da6417b8a18da87aa29"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "blog_os"
|
name = "blog_os"
|
||||||
@@ -20,9 +26,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bootloader",
|
"bootloader",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"pc-keyboard",
|
"spin 0.5.2",
|
||||||
"pic8259",
|
|
||||||
"spin",
|
|
||||||
"uart_16550",
|
"uart_16550",
|
||||||
"volatile 0.2.7",
|
"volatile 0.2.7",
|
||||||
"x86_64",
|
"x86_64",
|
||||||
@@ -30,33 +34,24 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bootloader"
|
name = "bootloader"
|
||||||
version = "0.9.31"
|
version = "0.9.33"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "974e79cf1b0b737839f01330fb5393095daf1124d52693696494e32523ae9ef5"
|
checksum = "7bdfddac270bbdd45903296bc1caf29a7fdce6b326aaf0bbab7f04c5f98b7447"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"spin",
|
"spin 0.9.8",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pc-keyboard"
|
name = "rustversion"
|
||||||
version = "0.7.0"
|
version = "1.0.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed089a1fbffe3337a1a345501c981f1eb1e47e69de5a40e852433e12953c3174"
|
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pic8259"
|
|
||||||
version = "0.10.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "08cc920d83ee33c0f9b73aa441e75468bf2d10c959a3eb6260cf720b05ac91a1"
|
|
||||||
dependencies = [
|
|
||||||
"x86_64",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spin"
|
name = "spin"
|
||||||
@@ -65,12 +60,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uart_16550"
|
name = "spin"
|
||||||
version = "0.2.14"
|
version = "0.9.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "503a6c0e6d82daa87985e662d120c0176b09587c92a68db22781b28ae95405dd"
|
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uart_16550"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
|
"rustversion",
|
||||||
"x86_64",
|
"x86_64",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -82,17 +84,18 @@ checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "volatile"
|
name = "volatile"
|
||||||
version = "0.4.4"
|
version = "0.4.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
|
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "x86_64"
|
name = "x86_64"
|
||||||
version = "0.14.7"
|
version = "0.14.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fb611915c917c6296d11e23f71ff1ecfe49c5766daba92cd3df52df6b58285b6"
|
checksum = "c101112411baafbb4bf8d33e4c4a80ab5b02d74d2612331c61e8192fc9710491"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bit_field",
|
"bit_field",
|
||||||
"bitflags",
|
"bitflags 2.9.2",
|
||||||
"volatile 0.4.4",
|
"rustversion",
|
||||||
|
"volatile 0.4.6",
|
||||||
]
|
]
|
||||||
|
|||||||
13
Cargo.toml
13
Cargo.toml
@@ -8,23 +8,22 @@ edition = "2024"
|
|||||||
name = "should_panic"
|
name = "should_panic"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[[test]]
|
|
||||||
name = "stack_overflow"
|
|
||||||
harness = false
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bootloader = { version = "0.9", features = ["map_physical_memory"] }
|
bootloader = "0.9"
|
||||||
volatile = "0.2.6"
|
volatile = "0.2.6"
|
||||||
spin = "0.5.2"
|
spin = "0.5.2"
|
||||||
x86_64 = "0.14.2"
|
x86_64 = "0.14.2"
|
||||||
uart_16550 = "0.2.0"
|
uart_16550 = "0.2.0"
|
||||||
pic8259 = "0.10.1"
|
|
||||||
pc-keyboard = "0.7.0"
|
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
features = ["spin_no_std"]
|
features = ["spin_no_std"]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "blog_os"
|
||||||
|
test = true
|
||||||
|
bench = false
|
||||||
|
|
||||||
|
|
||||||
[package.metadata.bootimage]
|
[package.metadata.bootimage]
|
||||||
test-args = [
|
test-args = [
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Blog OS (Paging Implementation)
|
# Blog OS (Testing)
|
||||||
|
|
||||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-09)
|
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-04)
|
||||||
|
|
||||||
This repository contains the source code for the [Paging Implementation][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
This repository contains the source code for the [Testing][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||||
|
|
||||||
[post]: https://os.phil-opp.com/paging-implementation/
|
[post]: https://os.phil-opp.com/testing/
|
||||||
|
|
||||||
**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.**
|
||||||
|
|
||||||
|
|||||||
52
src/gdt.rs
52
src/gdt.rs
@@ -1,52 +0,0 @@
|
|||||||
use lazy_static::lazy_static;
|
|
||||||
use x86_64::VirtAddr;
|
|
||||||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
|
|
||||||
use x86_64::structures::tss::TaskStateSegment;
|
|
||||||
|
|
||||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref TSS: TaskStateSegment = {
|
|
||||||
let mut tss = TaskStateSegment::new();
|
|
||||||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
|
||||||
const STACK_SIZE: usize = 4096 * 5;
|
|
||||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
|
||||||
|
|
||||||
let stack_start = VirtAddr::from_ptr(&raw const STACK);
|
|
||||||
let stack_end = stack_start + STACK_SIZE;
|
|
||||||
stack_end
|
|
||||||
};
|
|
||||||
tss
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref GDT: (GlobalDescriptorTable, Selectors) = {
|
|
||||||
let mut gdt = GlobalDescriptorTable::new();
|
|
||||||
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
|
|
||||||
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
|
|
||||||
(
|
|
||||||
gdt,
|
|
||||||
Selectors {
|
|
||||||
code_selector,
|
|
||||||
tss_selector,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Selectors {
|
|
||||||
code_selector: SegmentSelector,
|
|
||||||
tss_selector: SegmentSelector,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init() {
|
|
||||||
use x86_64::instructions::segmentation::{CS, Segment};
|
|
||||||
use x86_64::instructions::tables::load_tss;
|
|
||||||
|
|
||||||
GDT.0.load();
|
|
||||||
unsafe {
|
|
||||||
CS::set_reg(GDT.1.code_selector);
|
|
||||||
load_tss(GDT.1.tss_selector);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
use crate::{gdt, hlt_loop, print, println};
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use pic8259::ChainedPics;
|
|
||||||
use spin;
|
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
|
||||||
|
|
||||||
pub const PIC_1_OFFSET: u8 = 32;
|
|
||||||
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum InterruptIndex {
|
|
||||||
Timer = PIC_1_OFFSET,
|
|
||||||
Keyboard,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InterruptIndex {
|
|
||||||
fn as_u8(self) -> u8 {
|
|
||||||
self as u8
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_usize(self) -> usize {
|
|
||||||
usize::from(self.as_u8())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub static PICS: spin::Mutex<ChainedPics> =
|
|
||||||
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref IDT: InterruptDescriptorTable = {
|
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
|
||||||
idt.page_fault.set_handler_fn(page_fault_handler);
|
|
||||||
unsafe {
|
|
||||||
idt.double_fault
|
|
||||||
.set_handler_fn(double_fault_handler)
|
|
||||||
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
|
||||||
}
|
|
||||||
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
|
|
||||||
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
|
|
||||||
idt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_idt() {
|
|
||||||
IDT.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
|
||||||
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn page_fault_handler(
|
|
||||||
stack_frame: InterruptStackFrame,
|
|
||||||
error_code: PageFaultErrorCode,
|
|
||||||
) {
|
|
||||||
use x86_64::registers::control::Cr2;
|
|
||||||
|
|
||||||
println!("EXCEPTION: PAGE FAULT");
|
|
||||||
println!("Accessed Address: {:?}", Cr2::read());
|
|
||||||
println!("Error Code: {:?}", error_code);
|
|
||||||
println!("{:#?}", stack_frame);
|
|
||||||
hlt_loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn double_fault_handler(
|
|
||||||
stack_frame: InterruptStackFrame,
|
|
||||||
_error_code: u64,
|
|
||||||
) -> ! {
|
|
||||||
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
|
||||||
print!(".");
|
|
||||||
unsafe {
|
|
||||||
PICS.lock()
|
|
||||||
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
|
||||||
use pc_keyboard::{DecodedKey, HandleControl, Keyboard, ScancodeSet1, layouts};
|
|
||||||
use spin::Mutex;
|
|
||||||
use x86_64::instructions::port::Port;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
|
||||||
Mutex::new(Keyboard::new(
|
|
||||||
ScancodeSet1::new(),
|
|
||||||
layouts::Us104Key,
|
|
||||||
HandleControl::Ignore
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut keyboard = KEYBOARD.lock();
|
|
||||||
let mut port = Port::new(0x60);
|
|
||||||
|
|
||||||
let scancode: u8 = unsafe { port.read() };
|
|
||||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
PICS.lock()
|
|
||||||
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test_case]
|
|
||||||
fn test_breakpoint_exception() {
|
|
||||||
// invoke a breakpoint exception
|
|
||||||
x86_64::instructions::interrupts::int3();
|
|
||||||
}
|
|
||||||
30
src/lib.rs
30
src/lib.rs
@@ -1,24 +1,14 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![cfg_attr(test, no_main)]
|
#![cfg_attr(test, no_main)]
|
||||||
#![feature(custom_test_frameworks)]
|
#![feature(custom_test_frameworks)]
|
||||||
#![feature(abi_x86_interrupt)]
|
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
pub mod gdt;
|
|
||||||
pub mod interrupts;
|
|
||||||
pub mod memory;
|
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
pub mod vga_buffer;
|
pub mod vga_buffer;
|
||||||
|
|
||||||
pub fn init() {
|
|
||||||
gdt::init();
|
|
||||||
interrupts::init_idt();
|
|
||||||
unsafe { interrupts::PICS.lock().initialize() };
|
|
||||||
x86_64::instructions::interrupts::enable();
|
|
||||||
}
|
|
||||||
pub trait Testable {
|
pub trait Testable {
|
||||||
fn run(&self) -> ();
|
fn run(&self) -> ();
|
||||||
}
|
}
|
||||||
@@ -46,7 +36,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
|||||||
serial_println!("[failed]\n");
|
serial_println!("[failed]\n");
|
||||||
serial_println!("Error: {}\n", info);
|
serial_println!("Error: {}\n", info);
|
||||||
exit_qemu(QemuExitCode::Failed);
|
exit_qemu(QemuExitCode::Failed);
|
||||||
hlt_loop();
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -65,24 +55,12 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hlt_loop() -> ! {
|
|
||||||
loop {
|
|
||||||
x86_64::instructions::hlt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use bootloader::{BootInfo, entry_point};
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
entry_point!(test_kernel_main);
|
|
||||||
|
|
||||||
/// Entry point for `cargo xtest`
|
/// Entry point for `cargo xtest`
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
#[unsafe(no_mangle)]
|
||||||
init();
|
pub extern "C" fn _start() -> ! {
|
||||||
test_main();
|
test_main();
|
||||||
hlt_loop();
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
27
src/main.rs
27
src/main.rs
@@ -5,35 +5,16 @@
|
|||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use bootloader::{BootInfo, entry_point};
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
entry_point!(kernel_main);
|
#[unsafe(no_mangle)]
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
|
||||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
|
||||||
use x86_64::{VirtAddr, structures::paging::Page};
|
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
blog_os::init();
|
|
||||||
|
|
||||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
|
||||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
|
||||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
|
||||||
|
|
||||||
// map an unused page
|
|
||||||
let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000));
|
|
||||||
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
|
|
||||||
|
|
||||||
// write the string `New!` to the screen through the new mapping
|
|
||||||
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
|
|
||||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
test_main();
|
test_main();
|
||||||
|
|
||||||
println!("It did not crash!");
|
loop {}
|
||||||
blog_os::hlt_loop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is called on panic.
|
/// This function is called on panic.
|
||||||
@@ -41,7 +22,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
|||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
println!("{}", info);
|
println!("{}", info);
|
||||||
blog_os::hlt_loop();
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
106
src/memory.rs
106
src/memory.rs
@@ -1,106 +0,0 @@
|
|||||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
|
||||||
use x86_64::{
|
|
||||||
PhysAddr, VirtAddr,
|
|
||||||
structures::paging::{
|
|
||||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Initialize a new OffsetPageTable.
|
|
||||||
///
|
|
||||||
/// This function is unsafe because the caller must guarantee that the
|
|
||||||
/// complete physical memory is mapped to virtual memory at the passed
|
|
||||||
/// `physical_memory_offset`. Also, this function must be only called once
|
|
||||||
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
|
||||||
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
|
||||||
unsafe {
|
|
||||||
let level_4_table = active_level_4_table(physical_memory_offset);
|
|
||||||
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the active level 4 table.
|
|
||||||
///
|
|
||||||
/// This function is unsafe because the caller must guarantee that the
|
|
||||||
/// complete physical memory is mapped to virtual memory at the passed
|
|
||||||
/// `physical_memory_offset`. Also, this function must be only called once
|
|
||||||
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
|
||||||
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
|
|
||||||
use x86_64::registers::control::Cr3;
|
|
||||||
|
|
||||||
let (level_4_table_frame, _) = Cr3::read();
|
|
||||||
|
|
||||||
let phys = level_4_table_frame.start_address();
|
|
||||||
let virt = physical_memory_offset + phys.as_u64();
|
|
||||||
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
|
||||||
|
|
||||||
unsafe { &mut *page_table_ptr }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates an example mapping for the given page to frame `0xb8000`.
|
|
||||||
pub fn create_example_mapping(
|
|
||||||
page: Page,
|
|
||||||
mapper: &mut OffsetPageTable,
|
|
||||||
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
|
||||||
) {
|
|
||||||
use x86_64::structures::paging::PageTableFlags as Flags;
|
|
||||||
|
|
||||||
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
|
||||||
let flags = Flags::PRESENT | Flags::WRITABLE;
|
|
||||||
|
|
||||||
let map_to_result = unsafe {
|
|
||||||
// FIXME: this is not safe, we do it only for testing
|
|
||||||
mapper.map_to(page, frame, flags, frame_allocator)
|
|
||||||
};
|
|
||||||
map_to_result.expect("map_to failed").flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A FrameAllocator that always returns `None`.
|
|
||||||
pub struct EmptyFrameAllocator;
|
|
||||||
|
|
||||||
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|
||||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
|
||||||
pub struct BootInfoFrameAllocator {
|
|
||||||
memory_map: &'static MemoryMap,
|
|
||||||
next: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BootInfoFrameAllocator {
|
|
||||||
/// Create a FrameAllocator from the passed memory map.
|
|
||||||
///
|
|
||||||
/// This function is unsafe because the caller must guarantee that the passed
|
|
||||||
/// memory map is valid. The main requirement is that all frames that are marked
|
|
||||||
/// as `USABLE` in it are really unused.
|
|
||||||
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
|
||||||
BootInfoFrameAllocator {
|
|
||||||
memory_map,
|
|
||||||
next: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an iterator over the usable frames specified in the memory map.
|
|
||||||
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
|
||||||
// get usable regions from memory map
|
|
||||||
let regions = self.memory_map.iter();
|
|
||||||
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
|
||||||
// map each region to its address range
|
|
||||||
let addr_ranges = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
|
||||||
// transform to an iterator of frame start addresses
|
|
||||||
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
|
||||||
// create `PhysFrame` types from the start addresses
|
|
||||||
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
|
||||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
|
||||||
let frame = self.usable_frames().nth(self.next);
|
|
||||||
self.next += 1;
|
|
||||||
frame
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,14 +13,10 @@ lazy_static! {
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn _print(args: ::core::fmt::Arguments) {
|
pub fn _print(args: ::core::fmt::Arguments) {
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use x86_64::instructions::interrupts;
|
SERIAL1
|
||||||
|
.lock()
|
||||||
interrupts::without_interrupts(|| {
|
.write_fmt(args)
|
||||||
SERIAL1
|
.expect("Printing to serial failed");
|
||||||
.lock()
|
|
||||||
.write_fmt(args)
|
|
||||||
.expect("Printing to serial failed");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints to the host through the serial interface.
|
/// Prints to the host through the serial interface.
|
||||||
|
|||||||
@@ -163,16 +163,11 @@ macro_rules! println {
|
|||||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the given formatted string to the VGA text buffer
|
/// Prints the given formatted string to the VGA text buffer through the global `WRITER` instance.
|
||||||
/// through the global `WRITER` instance.
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn _print(args: fmt::Arguments) {
|
pub fn _print(args: fmt::Arguments) {
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use x86_64::instructions::interrupts;
|
WRITER.lock().write_fmt(args).unwrap();
|
||||||
|
|
||||||
interrupts::without_interrupts(|| {
|
|
||||||
WRITER.lock().write_fmt(args).unwrap();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_case]
|
#[test_case]
|
||||||
@@ -189,16 +184,10 @@ fn test_println_many() {
|
|||||||
|
|
||||||
#[test_case]
|
#[test_case]
|
||||||
fn test_println_output() {
|
fn test_println_output() {
|
||||||
use core::fmt::Write;
|
|
||||||
use x86_64::instructions::interrupts;
|
|
||||||
|
|
||||||
let s = "Some test string that fits on a single line";
|
let s = "Some test string that fits on a single line";
|
||||||
interrupts::without_interrupts(|| {
|
println!("{}", s);
|
||||||
let mut writer = WRITER.lock();
|
for (i, c) in s.chars().enumerate() {
|
||||||
writeln!(writer, "\n{}", s).expect("writeln failed");
|
let screen_char = WRITER.lock().buffer.chars[BUFFER_HEIGHT - 2][i].read();
|
||||||
for (i, c) in s.chars().enumerate() {
|
assert_eq!(char::from(screen_char.ascii_character), c);
|
||||||
let screen_char = writer.buffer.chars[BUFFER_HEIGHT - 2][i].read();
|
}
|
||||||
assert_eq!(char::from(screen_char.ascii_character), c);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![no_main]
|
|
||||||
#![feature(abi_x86_interrupt)]
|
|
||||||
|
|
||||||
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println};
|
|
||||||
use core::panic::PanicInfo;
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
|
||||||
pub extern "C" fn _start() -> ! {
|
|
||||||
serial_print!("stack_overflow::stack_overflow...\t");
|
|
||||||
|
|
||||||
blog_os::gdt::init();
|
|
||||||
init_test_idt();
|
|
||||||
|
|
||||||
// trigger a stack overflow
|
|
||||||
stack_overflow();
|
|
||||||
|
|
||||||
panic!("Execution continued after stack overflow");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unconditional_recursion)]
|
|
||||||
fn stack_overflow() {
|
|
||||||
stack_overflow(); // for each recursion, the return address is pushed
|
|
||||||
volatile::Volatile::new(0).read(); // prevent tail recursion optimizations
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref TEST_IDT: InterruptDescriptorTable = {
|
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
|
||||||
unsafe {
|
|
||||||
idt.double_fault
|
|
||||||
.set_handler_fn(test_double_fault_handler)
|
|
||||||
.set_stack_index(blog_os::gdt::DOUBLE_FAULT_IST_INDEX);
|
|
||||||
}
|
|
||||||
|
|
||||||
idt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_test_idt() {
|
|
||||||
TEST_IDT.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn test_double_fault_handler(
|
|
||||||
_stack_frame: InterruptStackFrame,
|
|
||||||
_error_code: u64,
|
|
||||||
) -> ! {
|
|
||||||
serial_println!("[ok]");
|
|
||||||
exit_qemu(QemuExitCode::Success);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[panic_handler]
|
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
|
||||||
blog_os::test_panic_handler(info)
|
|
||||||
}
|
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
|
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
|
||||||
"arch": "x86_64",
|
"arch": "x86_64",
|
||||||
"target-endian": "little",
|
"target-endian": "little",
|
||||||
"target-pointer-width": "64",
|
"target-pointer-width": 64,
|
||||||
"target-c-int-width": "32",
|
"target-c-int-width": 32,
|
||||||
"os": "none",
|
"os": "none",
|
||||||
"executables": true,
|
"executables": true,
|
||||||
"linker-flavor": "ld.lld",
|
"linker-flavor": "ld.lld",
|
||||||
|
|||||||
Reference in New Issue
Block a user