mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
305 Commits
post-07
...
27b56832c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27b56832c1 | ||
|
|
48dfc6d027 | ||
|
|
a7c5741ab5 | ||
|
|
65d8ef5478 | ||
|
|
2049f23abb | ||
|
|
0dab4b7ec2 | ||
|
|
3a988748de | ||
|
|
889ceba24a | ||
|
|
1489b0aa06 | ||
|
|
bb2c57e1b4 | ||
|
|
18fd90b7a7 | ||
|
|
ba7b36aa21 | ||
|
|
8b5ae877b8 | ||
|
|
28c9558a7a | ||
|
|
96af1fdc88 | ||
|
|
bb3beb22da | ||
|
|
b20e62e86b | ||
|
|
2870ac4e13 | ||
|
|
aa13e0388c | ||
|
|
c250dd0a91 | ||
|
|
1deebed468 | ||
|
|
2fdc7c3b5c | ||
|
|
d909d2456e | ||
|
|
be58db8907 | ||
|
|
614ae005a2 | ||
|
|
b654651a85 | ||
|
|
7bd3ecb9dc | ||
|
|
bf7cf4c65d | ||
|
|
1f6c000970 | ||
|
|
4343f9be6a | ||
|
|
e738b599e0 | ||
|
|
b6437c5cc1 | ||
|
|
5db0a55172 | ||
|
|
ee550a64e0 | ||
|
|
cce1bfd387 | ||
|
|
a249900aed | ||
|
|
709c389e96 | ||
|
|
e56cd71689 | ||
|
|
87799fad74 | ||
|
|
17e1ae2508 | ||
|
|
541b2211bb | ||
|
|
f345162ee5 | ||
|
|
7123daa4a5 | ||
|
|
01f42c65e8 | ||
|
|
020e91c111 | ||
|
|
3c1db5e79f | ||
|
|
4285dd4488 | ||
|
|
0e1efb591c | ||
|
|
bf19acea61 | ||
|
|
6c161ff3e0 | ||
|
|
c3d3b132b3 | ||
|
|
66d0d5d11c | ||
|
|
80e52e4e99 | ||
|
|
b5d4b9b212 | ||
|
|
d61e579799 | ||
|
|
3c2b79b1f0 | ||
|
|
d008e26592 | ||
|
|
8f20817ea3 | ||
|
|
45347e819f | ||
|
|
d21dbc7c58 | ||
|
|
4718be3507 | ||
|
|
3532a12264 | ||
|
|
1c7c81cf98 | ||
|
|
4bcfdf4b03 | ||
|
|
00c14bca69 | ||
|
|
c79bf064c8 | ||
|
|
be44d4d5c1 | ||
|
|
71a70a3b95 | ||
|
|
62235e4ba2 | ||
|
|
722c5579de | ||
|
|
a046cb6ed0 | ||
|
|
31d3eef0da | ||
|
|
b200b6eae6 | ||
|
|
ce20cc8661 | ||
|
|
c5fbae25e6 | ||
|
|
93844bee41 | ||
|
|
a280600fae | ||
|
|
92238b15b0 | ||
|
|
8823313fe1 | ||
|
|
624596adf1 | ||
|
|
0464c6e180 | ||
|
|
ff72429a55 | ||
|
|
137422b9ab | ||
|
|
df3a464cec | ||
|
|
7ccb43fafa | ||
|
|
c2ef594a78 | ||
|
|
01c9f7439d | ||
|
|
d2e7a4a970 | ||
|
|
988e248a6f | ||
|
|
6bbaf9de16 | ||
|
|
9a7dc7f053 | ||
|
|
4cec056568 | ||
|
|
03d9bee072 | ||
|
|
b7835f3976 | ||
|
|
6d2b8f3f6c | ||
|
|
472ddcc696 | ||
|
|
5d355277db | ||
|
|
832ab43bdd | ||
|
|
2a88224080 | ||
|
|
cecab1a310 | ||
|
|
d26577ad86 | ||
|
|
8b90c04c62 | ||
|
|
b9f5b43c04 | ||
|
|
5e510e41ee | ||
|
|
729c56d4ae | ||
|
|
491889a237 | ||
|
|
a2f07828e4 | ||
|
|
8d5aade432 | ||
|
|
d94b9513e7 | ||
|
|
50853609d1 | ||
|
|
70deb3168e | ||
|
|
9d4a960bcb | ||
|
|
a5d409b777 | ||
|
|
33df0bb2a3 | ||
|
|
79a0172ace | ||
|
|
9af6337f67 | ||
|
|
8fc746555d | ||
|
|
9324ea45a5 | ||
|
|
ed3af8e984 | ||
|
|
1d1075b35e | ||
|
|
00f39aaccb | ||
|
|
f815a1742a | ||
|
|
8eb44dcbd5 | ||
|
|
1f771a6820 | ||
|
|
f392d8a7f5 | ||
|
|
c7bc26d8ee | ||
|
|
3171ab584b | ||
|
|
6c1594ea24 | ||
|
|
9d31eb8aa8 | ||
|
|
94aa2c4093 | ||
|
|
2436801a05 | ||
|
|
259742a5e3 | ||
|
|
6876e82fc5 | ||
|
|
1b1953678c | ||
|
|
84ddda6a3f | ||
|
|
37b8b42cba | ||
|
|
f85664fd6b | ||
|
|
1e86c9f5f0 | ||
|
|
a65573b061 | ||
|
|
723776f852 | ||
|
|
385004cab2 | ||
|
|
fab00675e1 | ||
|
|
9696612b2b | ||
|
|
b0b0ebda06 | ||
|
|
d5d7db0de6 | ||
|
|
271f65e21c | ||
|
|
45375d1f1b | ||
|
|
9cab6fb659 | ||
|
|
4897e9c4d5 | ||
|
|
5cced71fb0 | ||
|
|
7ec727f69f | ||
|
|
e75c623985 | ||
|
|
8059c229c5 | ||
|
|
dce26ede7e | ||
|
|
bc2099d31d | ||
|
|
211ec3898b | ||
|
|
2227fa434f | ||
|
|
20ffda14f4 | ||
|
|
23d554548a | ||
|
|
0cd7d4cbcc | ||
|
|
091f7ef153 | ||
|
|
76d03974fe | ||
|
|
38d606b4d1 | ||
|
|
3dcc43b374 | ||
|
|
ae75d8b209 | ||
|
|
f7fc89fd31 | ||
|
|
e0d5cdd625 | ||
|
|
db92a921b8 | ||
|
|
3f95494ae8 | ||
|
|
80686ded94 | ||
|
|
cabbbec72f | ||
|
|
5a67b64d20 | ||
|
|
1dd7f03a7a | ||
|
|
1a74b36c99 | ||
|
|
66c4eae9cf | ||
|
|
175065ca2f | ||
|
|
e721878e4b | ||
|
|
7020999ab8 | ||
|
|
f2eb6cec9a | ||
|
|
30e0b16a81 | ||
|
|
bc4ddcef05 | ||
|
|
d7d63af4cc | ||
|
|
4580b85fe2 | ||
|
|
954cfe977a | ||
|
|
45e1e99390 | ||
|
|
eb86565308 | ||
|
|
b5793c34c6 | ||
|
|
bbeb63ec3d | ||
|
|
67ee45090d | ||
|
|
a4a7b5e8d6 | ||
|
|
64d1a587e7 | ||
|
|
189ab7d0d8 | ||
|
|
fbe279831e | ||
|
|
375d4d0479 | ||
|
|
7796d4c14a | ||
|
|
a2beb9d2a6 | ||
|
|
78e4b22a2f | ||
|
|
a867450e3b | ||
|
|
b5ee44621c | ||
|
|
24a9e7abd2 | ||
|
|
180c77d1f4 | ||
|
|
247af45791 | ||
|
|
667c093594 | ||
|
|
fd0646fecf | ||
|
|
c8821cb226 | ||
|
|
2e531850b8 | ||
|
|
65dbb5ac48 | ||
|
|
62f913facc | ||
|
|
d6f48d72aa | ||
|
|
2784998301 | ||
|
|
6099fddd54 | ||
|
|
16cf7e8e42 | ||
|
|
9c7faf92ed | ||
|
|
19556d9e68 | ||
|
|
22ba71a3f6 | ||
|
|
338833262c | ||
|
|
59d0a267d6 | ||
|
|
77800d9212 | ||
|
|
71b3d0431f | ||
|
|
be618df7f5 | ||
|
|
0ca216d4fe | ||
|
|
ff85a2e502 | ||
|
|
80d4065b4c | ||
|
|
74675346c9 | ||
|
|
d36d1dc18b | ||
|
|
e9344ae046 | ||
|
|
4acf12bb69 | ||
|
|
6f25c34d46 | ||
|
|
161d5fe7be | ||
|
|
9bf4ea7341 | ||
|
|
a1bf5651fc | ||
|
|
763228c859 | ||
|
|
770af27d75 | ||
|
|
3e59283c19 | ||
|
|
6146ccba2d | ||
|
|
b0e1527a95 | ||
|
|
cb4410c84e | ||
|
|
98b5976656 | ||
|
|
9335386928 | ||
|
|
7c30d62f33 | ||
|
|
61683bccda | ||
|
|
e1ec5159b8 | ||
|
|
7b7d19592f | ||
|
|
e387c0b6b8 | ||
|
|
d5abc119f3 | ||
|
|
59da6e5620 | ||
|
|
ef1cc0ed4f | ||
|
|
90f0caec1a | ||
|
|
7198a4d110 | ||
|
|
5c0fb63f33 | ||
|
|
6ffcb2cf1a | ||
|
|
036a8e7608 | ||
|
|
3b960751f4 | ||
|
|
10c4d0509d | ||
|
|
57998ea4f8 | ||
|
|
f05aaeb0ac | ||
|
|
78a30984bc | ||
|
|
ef9a629ddc | ||
|
|
18d8d311cb | ||
|
|
d974cf5200 | ||
|
|
94447af25a | ||
|
|
36d6c6d0e9 | ||
|
|
76d3715eef | ||
|
|
ff49104764 | ||
|
|
bda1b8929c | ||
|
|
03e43da9f9 | ||
|
|
58e171cce7 | ||
|
|
e696d65b60 | ||
|
|
ef09418cbf | ||
|
|
ebf626061f | ||
|
|
4c9352d898 | ||
|
|
c3d023ad40 | ||
|
|
f3cf5b51de | ||
|
|
0a10b3e784 | ||
|
|
9617680e45 | ||
|
|
e0f66a8196 | ||
|
|
ba54fd2503 | ||
|
|
e5dfbd4b23 | ||
|
|
4e6ce8d16e | ||
|
|
5ad2962389 | ||
|
|
82e6c4b066 | ||
|
|
a56e22b6fc | ||
|
|
67f536d7c6 | ||
|
|
741224411b | ||
|
|
818417d119 | ||
|
|
f272785861 | ||
|
|
5d807ee622 | ||
|
|
90c3cdf0f3 | ||
|
|
2bc233b2f6 | ||
|
|
0df629df47 | ||
|
|
cca85de5ed | ||
|
|
051b23f577 | ||
|
|
97e884e6a3 | ||
|
|
bd1f5345da | ||
|
|
f2bc2d33f0 | ||
|
|
38a121a887 | ||
|
|
8b380f0692 | ||
|
|
f23ee04161 | ||
|
|
b184f7d996 | ||
|
|
7c07a67bf5 | ||
|
|
abaf5bd862 | ||
|
|
b7005b766f | ||
|
|
91ca04e8c2 | ||
|
|
ada45c6e52 | ||
|
|
207a466707 |
12
Cargo.toml
12
Cargo.toml
@@ -13,7 +13,7 @@ name = "stack_overflow"
|
||||
harness = false
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9"
|
||||
bootloader = { version = "0.9", features = ["map_physical_memory"] }
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
x86_64 = "0.14.2"
|
||||
@@ -28,7 +28,11 @@ features = ["spin_no_std"]
|
||||
|
||||
[package.metadata.bootimage]
|
||||
test-args = [
|
||||
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||
"-display", "none"
|
||||
"-device",
|
||||
"isa-debug-exit,iobase=0xf4,iosize=0x04",
|
||||
"-serial",
|
||||
"stdio",
|
||||
"-display",
|
||||
"none",
|
||||
]
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Blog OS (Hardware Interrupts)
|
||||
# Blog OS (Paging Implementation)
|
||||
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-07)
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-09)
|
||||
|
||||
This repository contains the source code for the [Hardware Interrupts][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
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.
|
||||
|
||||
[post]: https://os.phil-opp.com/hardware-interrupts/
|
||||
[post]: https://os.phil-opp.com/paging-implementation/
|
||||
|
||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::{gdt, print, println};
|
||||
use crate::{gdt, hlt_loop, print, println};
|
||||
use lazy_static::lazy_static;
|
||||
use pic8259::ChainedPics;
|
||||
use spin;
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
||||
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;
|
||||
@@ -31,6 +31,7 @@ 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)
|
||||
@@ -50,6 +51,19 @@ 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,
|
||||
|
||||
10
src/lib.rs
10
src/lib.rs
@@ -9,6 +9,7 @@ use core::panic::PanicInfo;
|
||||
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod vga_buffer;
|
||||
|
||||
@@ -70,10 +71,15 @@ pub fn hlt_loop() -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
|
||||
#[cfg(test)]
|
||||
entry_point!(test_kernel_main);
|
||||
|
||||
/// Entry point for `cargo xtest`
|
||||
#[cfg(test)]
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||
init();
|
||||
test_main();
|
||||
hlt_loop();
|
||||
|
||||
22
src/main.rs
22
src/main.rs
@@ -5,14 +5,30 @@
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use blog_os::println;
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
println!("Hello World{}", "!");
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||
use x86_64::{VirtAddr, structures::paging::Page};
|
||||
|
||||
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)]
|
||||
test_main();
|
||||
|
||||
|
||||
106
src/memory.rs
Normal file
106
src/memory.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user