mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 23:07:50 +00:00
66 lines
2.4 KiB
Rust
66 lines
2.4 KiB
Rust
use x86_64::{
|
|
structures::paging::{
|
|
FrameAllocator, MappedPageTable, Mapper, MapperAllSizes, Page, PageTable, PhysFrame,
|
|
Size4KiB,
|
|
},
|
|
PhysAddr, VirtAddr,
|
|
};
|
|
|
|
/// Initialize a new MappedPageTable.
|
|
///
|
|
/// 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 {
|
|
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)
|
|
}
|
|
|
|
/// 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: u64) -> &'static mut PageTable {
|
|
use x86_64::{registers::control::Cr3, VirtAddr};
|
|
|
|
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 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 impl Mapper<Size4KiB>,
|
|
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 { 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;
|
|
|
|
impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
|
None
|
|
}
|
|
}
|