From 211ec3898bb84e39a2a40e4b88ee68a178a2f1c8 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 11 Sep 2019 13:40:26 +0200 Subject: [PATCH 1/3] Use OffsetPageTable instead of MappedPageTable --- src/main.rs | 3 ++- src/memory.rs | 18 ++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10b38955..af63c56f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,8 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { println!("Hello World{}", "!"); blog_os::init(); - let mut mapper = unsafe { memory::init(boot_info.physical_memory_offset) }; + 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 a previously unmapped page diff --git a/src/memory.rs b/src/memory.rs index 626f4fdb..b54225c2 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,26 +1,20 @@ use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use x86_64::{ structures::paging::{ - FrameAllocator, MappedPageTable, Mapper, MapperAllSizes, Page, PageTable, PhysFrame, - Size4KiB, + FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB, }, PhysAddr, VirtAddr, }; -/// Initialize a new MappedPageTable. +/// 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: u64) -> impl MapperAllSizes { +pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> { let level_4_table = active_level_4_table(physical_memory_offset); - let phys_to_virt = move |frame: PhysFrame| -> *mut PageTable { - let phys = frame.start_address().as_u64(); - let virt = VirtAddr::new(phys + physical_memory_offset); - virt.as_mut_ptr() - }; - MappedPageTable::new(level_4_table, phys_to_virt) + OffsetPageTable::new(level_4_table, physical_memory_offset) } /// Returns a mutable reference to the active level 4 table. @@ -29,13 +23,13 @@ pub unsafe fn init(physical_memory_offset: u64) -> impl MapperAllSizes { /// 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: u64) -> &'static mut PageTable { +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 = VirtAddr::new(phys.as_u64() + physical_memory_offset); + let virt = physical_memory_offset + phys.as_u64(); let page_table_ptr: *mut PageTable = virt.as_mut_ptr(); &mut *page_table_ptr // unsafe From 7ec727f69f9113d4fb23d9edc39bb3c6436acd23 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 14 Sep 2019 18:33:16 +0200 Subject: [PATCH 2/3] Update comment --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index af63c56f..2974192a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { let mut mapper = unsafe { memory::init(phys_mem_offset) }; let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) }; - // map a previously unmapped page + // map an unused page let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000)); memory::create_example_mapping(page, &mut mapper, &mut frame_allocator); From 5cced71fb0cc00aea3b9e361bd3e39f55aaa7bb9 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 14 Sep 2019 18:33:37 +0200 Subject: [PATCH 3/3] Directly use OffsetPageTable for create_example_mapping instead of impl trait --- src/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/memory.rs b/src/memory.rs index b54225c2..11c50445 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -38,7 +38,7 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut /// Creates an example mapping for the given page to frame `0xb8000`. pub fn create_example_mapping( page: Page, - mapper: &mut impl Mapper, + mapper: &mut OffsetPageTable, frame_allocator: &mut impl FrameAllocator, ) { use x86_64::structures::paging::PageTableFlags as Flags;