mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Add a map_to function
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use memory::{PAGE_SIZE, Frame};
|
use memory::{PAGE_SIZE, Frame, FrameAllocator};
|
||||||
|
pub use self::entry::*;
|
||||||
|
|
||||||
mod entry;
|
mod entry;
|
||||||
mod table;
|
mod table;
|
||||||
@@ -86,3 +87,16 @@ fn translate_page(page: Page) -> Option<Frame> {
|
|||||||
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
||||||
.or_else(huge_page)
|
.or_else(huge_page)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn map_to<A>(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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
use core::ops::{Index, IndexMut};
|
use core::ops::{Index, IndexMut};
|
||||||
|
use memory::FrameAllocator;
|
||||||
use memory::paging::entry::*;
|
use memory::paging::entry::*;
|
||||||
use memory::paging::ENTRY_COUNT;
|
use memory::paging::ENTRY_COUNT;
|
||||||
|
|
||||||
@@ -38,6 +39,22 @@ impl<L> Table<L> where L: HierarchicalLevel {
|
|||||||
self.next_table_address(index)
|
self.next_table_address(index)
|
||||||
.map(|address| unsafe { &mut *(address as *mut _) })
|
.map(|address| unsafe { &mut *(address as *mut _) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn next_table_create<A>(&mut self,
|
||||||
|
index: usize,
|
||||||
|
allocator: &mut A)
|
||||||
|
-> &mut Table<L::NextLevel>
|
||||||
|
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<L> Index<usize> for Table<L> where L: TableLevel {
|
impl<L> Index<usize> for Table<L> where L: TableLevel {
|
||||||
|
|||||||
Reference in New Issue
Block a user