More strict PhysicalAddress type

master
WangRunji 7 years ago
parent 3b4f855763
commit ca683e4787

@ -83,7 +83,7 @@ impl Frame {
} }
fn start_address(&self) -> PhysicalAddress { fn start_address(&self) -> PhysicalAddress {
self.number * PAGE_SIZE PhysicalAddress((self.number * PAGE_SIZE) as u64)
} }
fn clone(&self) -> Frame { fn clone(&self) -> Frame {

@ -0,0 +1,21 @@
use consts::KERNEL_OFFSET;
pub use x86_64::{PhysicalAddress};
pub type VirtualAddress = usize;
pub trait FromToVirtualAddress {
fn to_identity_virtual(&self) -> VirtualAddress;
fn to_kernel_virtual(&self) -> VirtualAddress;
fn from_kernel_virtual(addr: VirtualAddress) -> Self;
}
impl FromToVirtualAddress for PhysicalAddress {
fn to_identity_virtual(&self) -> VirtualAddress {
self.0 as usize
}
fn to_kernel_virtual(&self) -> VirtualAddress {
self.0 as usize + KERNEL_OFFSET
}
fn from_kernel_virtual(addr: VirtualAddress) -> Self {
PhysicalAddress((addr - KERNEL_OFFSET) as u64)
}
}

@ -27,8 +27,8 @@ impl Entry {
} }
pub fn set(&mut self, frame: Frame, flags: EntryFlags) { pub fn set(&mut self, frame: Frame, flags: EntryFlags) {
assert!(frame.start_address() & !0x000fffff_fffff000 == 0); assert!(frame.start_address().0 & !0x000fffff_fffff000 == 0);
self.0 = (frame.start_address() as u64) | flags.bits(); self.0 = (frame.start_address().0) | flags.bits();
} }
} }

@ -1,4 +1,4 @@
use super::{VirtualAddress, PhysicalAddress, Page, ENTRY_COUNT}; use super::{VirtualAddress, PhysicalAddress, Page, ENTRY_COUNT, FromToVirtualAddress};
use super::entry::*; use super::entry::*;
use super::table::{self, Table, Level4, Level1}; use super::table::{self, Table, Level4, Level1};
use memory::{PAGE_SIZE, Frame, FrameAllocator}; use memory::{PAGE_SIZE, Frame, FrameAllocator};
@ -26,7 +26,7 @@ impl Mapper {
pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> { pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> {
let offset = virtual_address % PAGE_SIZE; let offset = virtual_address % PAGE_SIZE;
self.translate_page(Page::containing_address(virtual_address)) self.translate_page(Page::containing_address(virtual_address))
.map(|frame| frame.number * PAGE_SIZE + offset) .map(|frame| PhysicalAddress((frame.number * PAGE_SIZE + offset) as u64))
} }
pub fn translate_page(&self, page: Page) -> Option<Frame> { pub fn translate_page(&self, page: Page) -> Option<Frame> {
@ -92,7 +92,7 @@ impl Mapper {
pub fn identity_map<A>(&mut self, frame: Frame, flags: EntryFlags, allocator: &mut A) pub fn identity_map<A>(&mut self, frame: Frame, flags: EntryFlags, allocator: &mut A)
where A: FrameAllocator where A: FrameAllocator
{ {
let page = Page::containing_address(frame.start_address()); let page = Page::containing_address(frame.start_address().to_identity_virtual());
self.map_to(page, frame, flags, allocator) self.map_to(page, frame, flags, allocator)
} }

@ -6,18 +6,17 @@ use memory::{PAGE_SIZE, Frame, FrameAllocator};
use multiboot2::BootInformation; use multiboot2::BootInformation;
use self::table::{Table, Level4}; use self::table::{Table, Level4};
use self::temporary_page::TemporaryPage; use self::temporary_page::TemporaryPage;
pub use self::address::*;
use consts::KERNEL_OFFSET; use consts::KERNEL_OFFSET;
mod entry; mod entry;
mod table; mod table;
mod temporary_page; mod temporary_page;
mod mapper; mod mapper;
mod address;
const ENTRY_COUNT: usize = 512; const ENTRY_COUNT: usize = 512;
pub type PhysicalAddress = usize;
pub type VirtualAddress = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Page { pub struct Page {
number: usize, number: usize,
@ -151,8 +150,7 @@ impl ActivePageTable {
), ),
}; };
unsafe { unsafe {
control_regs::cr3_write(PhysicalAddress( control_regs::cr3_write(new_table.p4_frame.start_address());
new_table.p4_frame.start_address() as u64));
} }
old_table old_table
} }
@ -223,7 +221,7 @@ pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
let end_frame = to_physical_frame(section.end_address() - 1); let end_frame = to_physical_frame(section.end_address() - 1);
for frame in Frame::range_inclusive(start_frame, end_frame) { for frame in Frame::range_inclusive(start_frame, end_frame) {
let page = Page::containing_address(frame.start_address() + KERNEL_OFFSET); let page = Page::containing_address(frame.start_address().to_kernel_virtual());
mapper.map_to(page, frame, flags, allocator); mapper.map_to(page, frame, flags, allocator);
} }
} }
@ -238,6 +236,7 @@ pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
for frame in Frame::range_inclusive(multiboot_start, multiboot_end) { for frame in Frame::range_inclusive(multiboot_start, multiboot_end) {
mapper.identity_map(frame, PRESENT, allocator); mapper.identity_map(frame, PRESENT, allocator);
} }
debug!("{:?}", mapper as *const Mapper);
}); });
let old_table = active_table.switch(new_table); let old_table = active_table.switch(new_table);
@ -245,7 +244,7 @@ pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
// turn the old p4 page into a guard page // turn the old p4 page into a guard page
let old_p4_page = Page::containing_address( let old_p4_page = Page::containing_address(
old_table.p4_frame.start_address() old_table.p4_frame.start_address().to_identity_virtual()
); );
active_table.unmap(old_p4_page, allocator); active_table.unmap(old_p4_page, allocator);
println!("guard page at {:#x}", old_p4_page.start_address()); println!("guard page at {:#x}", old_p4_page.start_address());

@ -108,7 +108,7 @@ impl Debug for Table<Level4> {
// Ignore the 511th recursive entry // Ignore the 511th recursive entry
let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused() && *i != 511usize); let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused() && *i != 511usize);
for (i, e) in entries { for (i, e) in entries {
write!(f, "{:3}: {:?}\n", i, e)?; write!(f, "{:3X}: {:?}\n", i, e)?;
write!(f, "{:?}", self.next_table(i).unwrap())?; write!(f, "{:?}", self.next_table(i).unwrap())?;
} }
Ok(()) Ok(())
@ -119,7 +119,7 @@ impl Debug for Table<Level3> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused()); let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused());
for (i, e) in entries { for (i, e) in entries {
write!(f, " {:3}: {:?}\n", i, e)?; write!(f, " {:3X}: {:?}\n", i, e)?;
write!(f, "{:?}", self.next_table(i).unwrap())?; write!(f, "{:?}", self.next_table(i).unwrap())?;
} }
Ok(()) Ok(())
@ -130,7 +130,7 @@ impl Debug for Table<Level2> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused()); let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused());
for (i, e) in entries { for (i, e) in entries {
write!(f, " {:3}: {:?}\n", i, e)?; write!(f, " {:3X}: {:?}\n", i, e)?;
write!(f, "{:?}", self.next_table(i).unwrap())?; write!(f, "{:?}", self.next_table(i).unwrap())?;
} }
Ok(()) Ok(())
@ -141,7 +141,7 @@ impl Debug for Table<Level1> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused()); let entries = self.entries.iter().enumerate().filter(|(i, e)| !e.is_unused());
for (i, e) in entries { for (i, e) in entries {
write!(f, " {:3}: {:?}\n", i, e)?; write!(f, " {:3X}: {:?}\n", i, e)?;
} }
Ok(()) Ok(())
} }

Loading…
Cancel
Save