Use OffsetPageTable instead of MappedPageTable

This commit is contained in:
Philipp Oppermann
2019-09-11 13:40:26 +02:00
parent 2227fa434f
commit 211ec3898b
2 changed files with 8 additions and 13 deletions

View File

@@ -17,7 +17,8 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
println!("Hello World{}", "!"); println!("Hello World{}", "!");
blog_os::init(); 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) }; let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
// map a previously unmapped page // map a previously unmapped page

View File

@@ -1,26 +1,20 @@
use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use x86_64::{ use x86_64::{
structures::paging::{ structures::paging::{
FrameAllocator, MappedPageTable, Mapper, MapperAllSizes, Page, PageTable, PhysFrame, FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
Size4KiB,
}, },
PhysAddr, VirtAddr, PhysAddr, VirtAddr,
}; };
/// Initialize a new MappedPageTable. /// Initialize a new OffsetPageTable.
/// ///
/// This function is unsafe because the caller must guarantee that the /// This function is unsafe because the caller must guarantee that the
/// complete physical memory is mapped to virtual memory at the passed /// complete physical memory is mapped to virtual memory at the passed
/// `physical_memory_offset`. Also, this function must be only called once /// `physical_memory_offset`. Also, this function must be only called once
/// to avoid aliasing `&mut` references (which is undefined behavior). /// 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 level_4_table = active_level_4_table(physical_memory_offset);
let phys_to_virt = move |frame: PhysFrame| -> *mut PageTable { OffsetPageTable::new(level_4_table, physical_memory_offset)
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)
} }
/// Returns a mutable reference to the active level 4 table. /// 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 /// complete physical memory is mapped to virtual memory at the passed
/// `physical_memory_offset`. Also, this function must be only called once /// `physical_memory_offset`. Also, this function must be only called once
/// to avoid aliasing `&mut` references (which is undefined behavior). /// 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; use x86_64::registers::control::Cr3;
let (level_4_table_frame, _) = Cr3::read(); let (level_4_table_frame, _) = Cr3::read();
let phys = level_4_table_frame.start_address(); 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(); let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
&mut *page_table_ptr // unsafe &mut *page_table_ptr // unsafe