mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Delete our memory::translate_addr function again
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use x86_64::{
|
||||
structures::paging::{MappedPageTable, MapperAllSizes, PageTable, PhysFrame},
|
||||
PhysAddr, VirtAddr,
|
||||
VirtAddr,
|
||||
};
|
||||
|
||||
/// Initialize a new MappedPageTable.
|
||||
@@ -36,53 +36,3 @@ unsafe fn active_level_4_table(physical_memory_offset: u64) -> &'static mut Page
|
||||
|
||||
&mut *page_table_ptr // unsafe
|
||||
}
|
||||
|
||||
/// Translates the given virtual address to the mapped physical address, or
|
||||
/// `None` if the address is not mapped.
|
||||
///
|
||||
/// 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`.
|
||||
pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: u64) -> Option<PhysAddr> {
|
||||
translate_addr_inner(addr, physical_memory_offset)
|
||||
}
|
||||
|
||||
/// Private function that is called by `translate_addr`.
|
||||
///
|
||||
/// This function is safe to limit the scope of `unsafe` because Rust treats
|
||||
/// the whole body of unsafe functions as an unsafe block. This function must
|
||||
/// only be reachable through `unsafe fn` from outside of this module.
|
||||
fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: u64) -> Option<PhysAddr> {
|
||||
use x86_64::registers::control::Cr3;
|
||||
use x86_64::structures::paging::page_table::FrameError;
|
||||
|
||||
// read the active level 4 frame from the CR3 register
|
||||
let (level_4_table_frame, _) = Cr3::read();
|
||||
|
||||
let table_indexes = [
|
||||
addr.p4_index(),
|
||||
addr.p3_index(),
|
||||
addr.p2_index(),
|
||||
addr.p1_index(),
|
||||
];
|
||||
let mut frame = level_4_table_frame;
|
||||
|
||||
// traverse the multi-level page table
|
||||
for &index in &table_indexes {
|
||||
// convert the frame into a page table reference
|
||||
let virt = frame.start_address().as_u64() + physical_memory_offset;
|
||||
let table_ptr: *const PageTable = VirtAddr::new(virt).as_ptr();
|
||||
let table = unsafe { &*table_ptr };
|
||||
|
||||
// read the page table entry and update `frame`
|
||||
let entry = &table[index];
|
||||
frame = match entry.frame() {
|
||||
Ok(frame) => frame,
|
||||
Err(FrameError::FrameNotPresent) => return None,
|
||||
Err(FrameError::HugeFrame) => panic!("huge pages not supported"),
|
||||
};
|
||||
}
|
||||
|
||||
// calculate the physical address by adding the page offset
|
||||
Some(frame.start_address() + u64::from(addr.page_offset()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user