More strict PhysicalAddress type

master
WangRunji 7 years ago
parent 3b4f855763
commit ca683e4787

@ -83,7 +83,7 @@ impl Frame {
}
fn start_address(&self) -> PhysicalAddress {
self.number * PAGE_SIZE
PhysicalAddress((self.number * PAGE_SIZE) as u64)
}
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) {
assert!(frame.start_address() & !0x000fffff_fffff000 == 0);
self.0 = (frame.start_address() as u64) | flags.bits();
assert!(frame.start_address().0 & !0x000fffff_fffff000 == 0);
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::table::{self, Table, Level4, Level1};
use memory::{PAGE_SIZE, Frame, FrameAllocator};
@ -26,7 +26,7 @@ impl Mapper {
pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> {
let offset = virtual_address % PAGE_SIZE;
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> {
@ -92,7 +92,7 @@ impl Mapper {
pub fn identity_map<A>(&mut self, frame: Frame, flags: EntryFlags, allocator: &mut A)
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)
}

@ -6,18 +6,17 @@ use memory::{PAGE_SIZE, Frame, FrameAllocator};
use multiboot2::BootInformation;
use self::table::{Table, Level4};
use self::temporary_page::TemporaryPage;
pub use self::address::*;
use consts::KERNEL_OFFSET;
mod entry;
mod table;
mod temporary_page;
mod mapper;
mod address;
const ENTRY_COUNT: usize = 512;
pub type PhysicalAddress = usize;
pub type VirtualAddress = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Page {
number: usize,
@ -151,8 +150,7 @@ impl ActivePageTable {
),
};
unsafe {
control_regs::cr3_write(PhysicalAddress(
new_table.p4_frame.start_address() as u64));
control_regs::cr3_write(new_table.p4_frame.start_address());
}
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);
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);
}
}
@ -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) {
mapper.identity_map(frame, PRESENT, allocator);
}
debug!("{:?}", mapper as *const Mapper);
});
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
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);
println!("guard page at {:#x}", old_p4_page.start_address());

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

Loading…
Cancel
Save