From 8590d9f1bbc213d864dc4a447900835a8128577d Mon Sep 17 00:00:00 2001 From: Knarkzel Date: Mon, 29 Jan 2024 01:22:12 +0100 Subject: [PATCH] paging --- os/Cargo.toml | 2 +- os/src/interrupts.rs | 18 +++++++- os/src/lib.rs | 13 ++++-- os/src/main.rs | 25 +++++++++++- os/src/memory.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 os/src/memory.rs diff --git a/os/Cargo.toml b/os/Cargo.toml index d7d8c1b..b62de60 100644 --- a/os/Cargo.toml +++ b/os/Cargo.toml @@ -12,10 +12,10 @@ spin = "0.9.8" x86_64 = "0.14.2" volatile = "0.2.6" uart_16550 = "0.2.0" -bootloader = "0.9.23" pic8259 = "0.10.4" pc-keyboard = "0.7.0" lazy_static = { version = "1.0", features = ["spin_no_std"] } +bootloader = { version = "0.9.23", features = ["map_physical_memory"]} [package.metadata.bootimage] test-success-exit-code = 33 diff --git a/os/src/interrupts.rs b/os/src/interrupts.rs index dc2e4bf..7611b7e 100644 --- a/os/src/interrupts.rs +++ b/os/src/interrupts.rs @@ -1,7 +1,7 @@ -use crate::{gdt, print, println}; +use crate::{gdt, print, println, hlt_loop}; use lazy_static::lazy_static; use pic8259::ChainedPics; -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; @@ -37,6 +37,7 @@ lazy_static! { .set_handler_fn(double_fault_handler) .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); } + idt.page_fault.set_handler_fn(page_fault_handler); idt }; } @@ -102,3 +103,16 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac .notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8()); } } + +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(); +} diff --git a/os/src/lib.rs b/os/src/lib.rs index 6f72dab..23e8075 100644 --- a/os/src/lib.rs +++ b/os/src/lib.rs @@ -5,10 +5,14 @@ #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] +#[cfg(test)] +use bootloader::{entry_point, BootInfo}; + pub mod gdt; pub mod interrupts; pub mod serial; pub mod vga; +pub mod memory; pub fn init() { gdt::init(); @@ -25,11 +29,14 @@ pub fn hlt_loop() -> ! { /// Entry point for `cargo test` #[cfg(test)] -#[no_mangle] -pub extern "C" fn _start() -> ! { +entry_point!(test_kernel_main); + +#[cfg(test)] +fn test_kernel_main(_boot_info: &'static BootInfo) -> ! { + // like before init(); test_main(); - crate::hlt_loop() + hlt_loop(); } // Boilerplate for testing diff --git a/os/src/main.rs b/os/src/main.rs index 50e5865..7da4565 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -5,13 +5,34 @@ #![reexport_test_harness_main = "test_main"] use knarkos::println; +use bootloader::{BootInfo, entry_point}; -#[no_mangle] -pub extern "C" fn _start() -> ! { +entry_point!(kernel_main); + +fn kernel_main(boot_info: &'static BootInfo) -> ! { + use knarkos::memory; + use knarkos::memory::BootInfoFrameAllocator; + use x86_64::{structures::paging::Page, VirtAddr}; // new import + // Initialize operating system knarkos::init(); println!("Hello, world!"); + // Read page tables + 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(0)); + 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(); diff --git a/os/src/memory.rs b/os/src/memory.rs new file mode 100644 index 0000000..1cd860c --- /dev/null +++ b/os/src/memory.rs @@ -0,0 +1,97 @@ +use x86_64::{ + structures::paging::{Page, PhysFrame, Mapper, Size4KiB, FrameAllocator, OffsetPageTable, PageTable}, + PhysAddr, VirtAddr, +}; +use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; + +/// 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). +pub 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, +) { + 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 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 { + // 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 for BootInfoFrameAllocator { + fn allocate_frame(&mut self) -> Option { + let frame = self.usable_frames().nth(self.next); + self.next += 1; + frame + } +}