diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 7b9a965..98b36e0 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -1,4 +1,5 @@ pub use self::area_frame_allocator::AreaFrameAllocator; +use self::paging::PhysicalAddress; mod area_frame_allocator; mod paging; @@ -14,6 +15,10 @@ impl Frame { fn containing_address(address: usize) -> Frame { Frame{ number: address / PAGE_SIZE } } + + fn start_address(&self) -> PhysicalAddress { + self.number * PAGE_SIZE + } } pub trait FrameAllocator { diff --git a/src/memory/paging/entry.rs b/src/memory/paging/entry.rs index 0f44031..18e47e8 100644 --- a/src/memory/paging/entry.rs +++ b/src/memory/paging/entry.rs @@ -14,6 +14,21 @@ impl Entry { pub fn flags(&self) -> EntryFlags { EntryFlags::from_bits_truncate(self.0) } + + pub fn pointed_frame(&self) -> Option { + if self.flags().contains(PRESENT) { + Some(Frame::containing_address( + self.0 as usize & 0x000fffff_fffff000 + )) + } else { + None + } + } + + 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(); + } } bitflags! {