mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
164 Commits
post-08
...
ba7b36aa21
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba7b36aa21 | ||
|
|
28c9558a7a | ||
|
|
bb3beb22da | ||
|
|
2870ac4e13 | ||
|
|
c250dd0a91 | ||
|
|
2fdc7c3b5c | ||
|
|
be58db8907 | ||
|
|
b654651a85 | ||
|
|
bf7cf4c65d | ||
|
|
4343f9be6a | ||
|
|
b6437c5cc1 | ||
|
|
ee550a64e0 | ||
|
|
a249900aed | ||
|
|
e56cd71689 | ||
|
|
17e1ae2508 | ||
|
|
f345162ee5 | ||
|
|
01f42c65e8 | ||
|
|
3c1db5e79f | ||
|
|
0e1efb591c | ||
|
|
6c161ff3e0 | ||
|
|
66d0d5d11c | ||
|
|
b5d4b9b212 | ||
|
|
3c2b79b1f0 | ||
|
|
8f20817ea3 | ||
|
|
d21dbc7c58 | ||
|
|
3532a12264 | ||
|
|
4bcfdf4b03 | ||
|
|
c79bf064c8 | ||
|
|
71a70a3b95 | ||
|
|
722c5579de | ||
|
|
31d3eef0da | ||
|
|
ce20cc8661 | ||
|
|
93844bee41 | ||
|
|
92238b15b0 | ||
|
|
624596adf1 | ||
|
|
ff72429a55 | ||
|
|
df3a464cec | ||
|
|
c2ef594a78 | ||
|
|
d2e7a4a970 | ||
|
|
6bbaf9de16 | ||
|
|
4cec056568 | ||
|
|
b7835f3976 | ||
|
|
472ddcc696 | ||
|
|
832ab43bdd | ||
|
|
cecab1a310 | ||
|
|
8b90c04c62 | ||
|
|
5e510e41ee | ||
|
|
491889a237 | ||
|
|
a2f07828e4 | ||
|
|
d94b9513e7 | ||
|
|
70deb3168e | ||
|
|
a5d409b777 | ||
|
|
79a0172ace | ||
|
|
8fc746555d | ||
|
|
ed3af8e984 | ||
|
|
1d1075b35e | ||
|
|
8eb44dcbd5 | ||
|
|
f392d8a7f5 | ||
|
|
c7bc26d8ee | ||
|
|
6c1594ea24 | ||
|
|
94aa2c4093 | ||
|
|
259742a5e3 | ||
|
|
6876e82fc5 | ||
|
|
84ddda6a3f | ||
|
|
f85664fd6b | ||
|
|
a65573b061 | ||
|
|
385004cab2 | ||
|
|
9696612b2b | ||
|
|
d5d7db0de6 | ||
|
|
45375d1f1b | ||
|
|
4897e9c4d5 | ||
|
|
5cced71fb0 | ||
|
|
7ec727f69f | ||
|
|
8059c229c5 | ||
|
|
bc2099d31d | ||
|
|
211ec3898b | ||
|
|
2227fa434f | ||
|
|
23d554548a | ||
|
|
091f7ef153 | ||
|
|
38d606b4d1 | ||
|
|
ae75d8b209 | ||
|
|
e0d5cdd625 | ||
|
|
3f95494ae8 | ||
|
|
cabbbec72f | ||
|
|
1dd7f03a7a | ||
|
|
66c4eae9cf | ||
|
|
e721878e4b | ||
|
|
f2eb6cec9a | ||
|
|
bc4ddcef05 | ||
|
|
4580b85fe2 | ||
|
|
45e1e99390 | ||
|
|
b5793c34c6 | ||
|
|
67ee45090d | ||
|
|
64d1a587e7 | ||
|
|
fbe279831e | ||
|
|
375d4d0479 | ||
|
|
7796d4c14a | ||
|
|
78e4b22a2f | ||
|
|
a867450e3b | ||
|
|
b5ee44621c | ||
|
|
24a9e7abd2 | ||
|
|
180c77d1f4 | ||
|
|
247af45791 | ||
|
|
fd0646fecf | ||
|
|
c8821cb226 | ||
|
|
2e531850b8 | ||
|
|
65dbb5ac48 | ||
|
|
16cf7e8e42 | ||
|
|
19556d9e68 | ||
|
|
338833262c | ||
|
|
77800d9212 | ||
|
|
be618df7f5 | ||
|
|
ff85a2e502 | ||
|
|
74675346c9 | ||
|
|
e9344ae046 | ||
|
|
4acf12bb69 | ||
|
|
161d5fe7be | ||
|
|
9bf4ea7341 | ||
|
|
a1bf5651fc | ||
|
|
763228c859 | ||
|
|
770af27d75 | ||
|
|
3e59283c19 | ||
|
|
6146ccba2d | ||
|
|
b0e1527a95 | ||
|
|
cb4410c84e | ||
|
|
98b5976656 | ||
|
|
9335386928 | ||
|
|
7c30d62f33 | ||
|
|
61683bccda | ||
|
|
e1ec5159b8 | ||
|
|
7b7d19592f | ||
|
|
e387c0b6b8 | ||
|
|
d5abc119f3 | ||
|
|
59da6e5620 | ||
|
|
ef1cc0ed4f | ||
|
|
90f0caec1a | ||
|
|
5c0fb63f33 | ||
|
|
036a8e7608 | ||
|
|
10c4d0509d | ||
|
|
57998ea4f8 | ||
|
|
ef9a629ddc | ||
|
|
d974cf5200 | ||
|
|
36d6c6d0e9 | ||
|
|
ff49104764 | ||
|
|
03e43da9f9 | ||
|
|
e696d65b60 | ||
|
|
ebf626061f | ||
|
|
c3d023ad40 | ||
|
|
0a10b3e784 | ||
|
|
e0f66a8196 | ||
|
|
e5dfbd4b23 | ||
|
|
5ad2962389 | ||
|
|
a56e22b6fc | ||
|
|
67f536d7c6 | ||
|
|
741224411b | ||
|
|
818417d119 | ||
|
|
f272785861 | ||
|
|
5d807ee622 | ||
|
|
90c3cdf0f3 | ||
|
|
2bc233b2f6 | ||
|
|
051b23f577 | ||
|
|
f2bc2d33f0 | ||
|
|
38a121a887 | ||
|
|
8b380f0692 |
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 (Introduction to Paging)
|
||||
# Blog OS (Paging Implementation)
|
||||
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-08)
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-09)
|
||||
|
||||
This repository contains the source code for the [Introduction to Paging][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/paging-introduction/
|
||||
[post]: https://os.phil-opp.com/paging-implementation/
|
||||
|
||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||
|
||||
|
||||
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::{entry_point, BootInfo};
|
||||
|
||||
#[cfg(test)]
|
||||
entry_point!(test_kernel_main);
|
||||
|
||||
/// Entry point for `cargo xtest`
|
||||
#[cfg(test)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||
init();
|
||||
test_main();
|
||||
hlt_loop();
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@@ -5,20 +5,29 @@
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use blog_os::println;
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
use x86_64::registers::control::Cr3;
|
||||
println!("Hello World{}", "!");
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||
use x86_64::{structures::paging::Page, VirtAddr};
|
||||
|
||||
println!("Hello World{}", "!");
|
||||
blog_os::init();
|
||||
|
||||
let (level_4_page_table, _) = Cr3::read();
|
||||
println!(
|
||||
"Level 4 page table at: {:?}",
|
||||
level_4_page_table.start_address()
|
||||
);
|
||||
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();
|
||||
|
||||
104
src/memory.rs
Normal file
104
src/memory.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||
use x86_64::{
|
||||
structures::paging::{
|
||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
||||
},
|
||||
PhysAddr, VirtAddr,
|
||||
};
|
||||
|
||||
/// 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> {
|
||||
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();
|
||||
|
||||
&mut *page_table_ptr // unsafe
|
||||
}
|
||||
|
||||
/// 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