80 lines
2.7 KiB
Rust
80 lines
2.7 KiB
Rust
use core::ptr::Unique;
|
|
|
|
use memory::{allocate_frame, deallocate_frame, Frame};
|
|
|
|
use super::{Page, PAGE_SIZE, PhysicalAddress, VirtualAddress};
|
|
use super::entry::{self, EntryFlags};
|
|
use super::table::{self, Table, Level4};
|
|
|
|
pub struct Mapper {
|
|
p4: Unique<Table<Level4>>,
|
|
}
|
|
|
|
impl Mapper {
|
|
/// Create a new page table
|
|
pub unsafe fn new() -> Mapper {
|
|
Mapper {
|
|
p4: Unique::new(table::P4),
|
|
}
|
|
}
|
|
|
|
pub fn p4(&self) -> &Table<Level4> {
|
|
unsafe { self.p4.get() }
|
|
}
|
|
|
|
pub fn p4_mut(&mut self) -> &mut Table<Level4> {
|
|
unsafe { self.p4.get_mut() }
|
|
}
|
|
|
|
/// Map a page to a frame
|
|
pub fn map_to(&mut self, page: Page, frame: Frame, flags: EntryFlags) {
|
|
let mut p3 = self.p4_mut().next_table_create(page.p4_index());
|
|
let mut p2 = p3.next_table_create(page.p3_index());
|
|
let mut p1 = p2.next_table_create(page.p2_index());
|
|
|
|
assert!(p1[page.p1_index()].is_unused());
|
|
p1[page.p1_index()].set(frame, flags | entry::PRESENT);
|
|
}
|
|
|
|
/// Map a page to the next free frame
|
|
pub fn map(&mut self, page: Page, flags: EntryFlags) {
|
|
let frame = allocate_frame().expect("out of memory");
|
|
self.map_to(page, frame, flags)
|
|
}
|
|
|
|
/// Identity map a frame
|
|
pub fn identity_map(&mut self, frame: Frame, flags: EntryFlags) {
|
|
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get()));
|
|
self.map_to(page, frame, flags)
|
|
}
|
|
|
|
/// Unmap a page
|
|
pub fn unmap(&mut self, page: Page) {
|
|
assert!(self.translate(page.start_address()).is_some());
|
|
|
|
let p1 = self.p4_mut()
|
|
.next_table_mut(page.p4_index())
|
|
.and_then(|p3| p3.next_table_mut(page.p3_index()))
|
|
.and_then(|p2| p2.next_table_mut(page.p2_index()))
|
|
.expect("mapping code does not support huge pages");
|
|
let frame = p1[page.p1_index()].pointed_frame().unwrap();
|
|
p1[page.p1_index()].set_unused();
|
|
// TODO free p(1,2,3) table if empty
|
|
deallocate_frame(frame);
|
|
}
|
|
|
|
pub fn translate_page(&self, page: Page) -> Option<Frame> {
|
|
self.p4().next_table(page.p4_index())
|
|
.and_then(|p3| p3.next_table(page.p3_index()))
|
|
.and_then(|p2| p2.next_table(page.p2_index()))
|
|
.and_then(|p1| p1[page.p1_index()].pointed_frame())
|
|
}
|
|
|
|
/// Translate a virtual address to a physical one
|
|
pub fn translate(&self, virtual_address: VirtualAddress) -> Option<PhysicalAddress> {
|
|
let offset = virtual_address.get() % PAGE_SIZE;
|
|
self.translate_page(Page::containing_address(virtual_address))
|
|
.map(|frame| PhysicalAddress::new(frame.start_address().get() + offset))
|
|
}
|
|
}
|