diff --git a/src/memory/paging/mod.rs b/src/memory/paging/mod.rs
index fcf39df..3e74077 100644
--- a/src/memory/paging/mod.rs
+++ b/src/memory/paging/mod.rs
@@ -1,4 +1,5 @@
-use memory::{PAGE_SIZE, Frame};
+use memory::{PAGE_SIZE, Frame, FrameAllocator};
+pub use self::entry::*;
mod entry;
mod table;
@@ -86,3 +87,16 @@ fn translate_page(page: Page) -> Option {
.and_then(|p1| p1[page.p1_index()].pointed_frame())
.or_else(huge_page)
}
+
+pub fn map_to(page: Page, frame: Frame, flags: EntryFlags,
+ allocator: &mut A)
+ where A: FrameAllocator
+{
+ let p4 = unsafe { &mut *table::P4 };
+ let mut p3 = p4.next_table_create(page.p4_index(), allocator);
+ let mut p2 = p3.next_table_create(page.p3_index(), allocator);
+ let mut p1 = p2.next_table_create(page.p2_index(), allocator);
+
+ assert!(p1[page.p1_index()].is_unused());
+ p1[page.p1_index()].set(frame, flags | PRESENT);
+}
diff --git a/src/memory/paging/table.rs b/src/memory/paging/table.rs
index 949c35a..8fecefe 100644
--- a/src/memory/paging/table.rs
+++ b/src/memory/paging/table.rs
@@ -1,5 +1,6 @@
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
+use memory::FrameAllocator;
use memory::paging::entry::*;
use memory::paging::ENTRY_COUNT;
@@ -38,6 +39,22 @@ impl Table where L: HierarchicalLevel {
self.next_table_address(index)
.map(|address| unsafe { &mut *(address as *mut _) })
}
+
+ pub fn next_table_create(&mut self,
+ index: usize,
+ allocator: &mut A)
+ -> &mut Table
+ where A: FrameAllocator
+ {
+ if self.next_table(index).is_none() {
+ assert!(!self.entries[index].flags().contains(HUGE_PAGE),
+ "mapping code does not support huge pages");
+ let frame = allocator.allocate_frame().expect("no frames available");
+ self.entries[index].set(frame, PRESENT | WRITABLE);
+ self.next_table_mut(index).unwrap().zero();
+ }
+ self.next_table_mut(index).unwrap()
+ }
}
impl Index for Table where L: TableLevel {