diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 4e74ce0..d5bfdba 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -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 { diff --git a/src/memory/paging/address.rs b/src/memory/paging/address.rs new file mode 100644 index 0000000..f861b14 --- /dev/null +++ b/src/memory/paging/address.rs @@ -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) + } +} \ No newline at end of file diff --git a/src/memory/paging/entry.rs b/src/memory/paging/entry.rs index 848f6d1..ce735d4 100644 --- a/src/memory/paging/entry.rs +++ b/src/memory/paging/entry.rs @@ -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(); } } diff --git a/src/memory/paging/mapper.rs b/src/memory/paging/mapper.rs index 677d330..4f6986f 100644 --- a/src/memory/paging/mapper.rs +++ b/src/memory/paging/mapper.rs @@ -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 { 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 { @@ -92,7 +92,7 @@ impl Mapper { pub fn identity_map(&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) } diff --git a/src/memory/paging/mod.rs b/src/memory/paging/mod.rs index 7a7d9b8..cf00716 100644 --- a/src/memory/paging/mod.rs +++ b/src/memory/paging/mod.rs @@ -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(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(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(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()); diff --git a/src/memory/paging/table.rs b/src/memory/paging/table.rs index 84c17ea..11003ce 100644 --- a/src/memory/paging/table.rs +++ b/src/memory/paging/table.rs @@ -108,7 +108,7 @@ impl Debug for Table { // 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 { 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 { 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 { 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(()) }