mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Make translate_addr unsafe (#544)
This function casts a usize argument to a pointer, and dereferences it. This is undefined behavior unless level_4_table_addr is the address of a valid PageTable, so the function should be marked 'unsafe'
This commit is contained in:
committed by
Philipp Oppermann
parent
b9ff59fd75
commit
0c5e303175
@@ -180,7 +180,10 @@ use x86_64::structures::paging::PageTable;
|
||||
|
||||
/// Returns the physical address for the given virtual address, or `None` if the
|
||||
/// virtual address is not mapped.
|
||||
pub fn translate_addr(addr: usize, level_4_table_addr: usize) -> Option<PhysAddr> {
|
||||
///
|
||||
/// Safety: This requires level_4_table_addr to be the address of a valid
|
||||
/// level-4 PageTable
|
||||
pub unsafe fn translate_addr(addr: usize, level_4_table_addr: usize) -> Option<PhysAddr> {
|
||||
// retrieve the page table indices of the address that we want to translate
|
||||
let level_4_index = (addr >> 39) & 0o777;
|
||||
let level_3_index = (addr >> 30) & 0o777;
|
||||
@@ -252,6 +255,7 @@ pub extern "C" fn _start() -> ! {
|
||||
|
||||
const LEVEL_4_TABLE_ADDR: usize = 0o_177777_777_777_777_777_0000;
|
||||
|
||||
unsafe {
|
||||
// the identity-mapped vga buffer page
|
||||
println!("0xb8000 -> {:?}", translate_addr(0xb8000, LEVEL_4_TABLE_ADDR));
|
||||
// some code page
|
||||
@@ -259,6 +263,7 @@ pub extern "C" fn _start() -> ! {
|
||||
// some stack page
|
||||
println!("0x57ac001ffe48 -> {:?}", translate_addr(0x57ac001ffe48,
|
||||
LEVEL_4_TABLE_ADDR));
|
||||
}
|
||||
|
||||
println!("It did not crash!");
|
||||
blog_os::hlt_loop();
|
||||
|
||||
Reference in New Issue
Block a user