Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle parent table flags in Mapper methods #114

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 185 additions & 25 deletions src/structures/paging/mapper/mapped_page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
page: Page<Size1GiB>,
frame: PhysFrame<Size1GiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
where
A: FrameAllocator<Size4KiB>,
{
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.create_next_table(&mut p4[page.p4_index()], allocator)?;
let p3 = self.page_table_walker.create_next_table(
&mut p4[page.p4_index()],
parent_table_flags,
allocator,
)?;

if !p3[page.p3_index()].is_unused() {
return Err(MapToError::PageAlreadyMapped(frame));
Expand All @@ -69,18 +72,23 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
page: Page<Size2MiB>,
frame: PhysFrame<Size2MiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
where
A: FrameAllocator<Size4KiB>,
{
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.create_next_table(&mut p4[page.p4_index()], allocator)?;
let p2 = self
.page_table_walker
.create_next_table(&mut p3[page.p3_index()], allocator)?;
let p3 = self.page_table_walker.create_next_table(
&mut p4[page.p4_index()],
parent_table_flags,
allocator,
)?;
let p2 = self.page_table_walker.create_next_table(
&mut p3[page.p3_index()],
parent_table_flags,
allocator,
)?;

if !p2[page.p2_index()].is_unused() {
return Err(MapToError::PageAlreadyMapped(frame));
Expand All @@ -97,21 +105,28 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
page: Page<Size4KiB>,
frame: PhysFrame<Size4KiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>
where
A: FrameAllocator<Size4KiB>,
{
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.create_next_table(&mut p4[page.p4_index()], allocator)?;
let p2 = self
.page_table_walker
.create_next_table(&mut p3[page.p3_index()], allocator)?;
let p1 = self
.page_table_walker
.create_next_table(&mut p2[page.p2_index()], allocator)?;
let p3 = self.page_table_walker.create_next_table(
&mut p4[page.p4_index()],
parent_table_flags,
allocator,
)?;
let p2 = self.page_table_walker.create_next_table(
&mut p3[page.p3_index()],
parent_table_flags,
allocator,
)?;
let p1 = self.page_table_walker.create_next_table(
&mut p2[page.p2_index()],
parent_table_flags,
allocator,
)?;

if !p1[page.p1_index()].is_unused() {
return Err(MapToError::PageAlreadyMapped(frame));
Expand All @@ -124,17 +139,18 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {

impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
#[inline]
unsafe fn map_to<A>(
unsafe fn map_to_with_table_flags<A>(
&mut self,
page: Page<Size1GiB>,
frame: PhysFrame<Size1GiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size1GiB>, MapToError<Size1GiB>>
where
A: FrameAllocator<Size4KiB>,
{
self.map_to_1gib(page, frame, flags, allocator)
self.map_to_1gib(page, frame, flags, parent_table_flags, allocator)
}

fn unmap(
Expand Down Expand Up @@ -181,6 +197,39 @@ impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
Ok(MapperFlush::new(page))
}

unsafe fn set_flags_p4_entry(
&mut self,
page: Page<Size1GiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p4_entry = &mut p4[page.p4_index()];

if p4_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p4_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

unsafe fn set_flags_p3_entry(
&mut self,
_page: Page<Size1GiB>,
_flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
Err(FlagUpdateError::ParentEntryHugePage)
}

unsafe fn set_flags_p2_entry(
&mut self,
_page: Page<Size1GiB>,
_flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
Err(FlagUpdateError::ParentEntryHugePage)
}

fn translate_page(&self, page: Page<Size1GiB>) -> Result<PhysFrame<Size1GiB>, TranslateError> {
let p4 = &self.level_4_table;
let p3 = self.page_table_walker.next_table(&p4[page.p4_index()])?;
Expand All @@ -198,17 +247,18 @@ impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {

impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
#[inline]
unsafe fn map_to<A>(
unsafe fn map_to_with_table_flags<A>(
&mut self,
page: Page<Size2MiB>,
frame: PhysFrame<Size2MiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size2MiB>, MapToError<Size2MiB>>
where
A: FrameAllocator<Size4KiB>,
{
self.map_to_2mib(page, frame, flags, allocator)
self.map_to_2mib(page, frame, flags, parent_table_flags, allocator)
}

fn unmap(
Expand Down Expand Up @@ -262,6 +312,51 @@ impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
Ok(MapperFlush::new(page))
}

unsafe fn set_flags_p4_entry(
&mut self,
page: Page<Size2MiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p4_entry = &mut p4[page.p4_index()];

if p4_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p4_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

unsafe fn set_flags_p3_entry(
&mut self,
page: Page<Size2MiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.next_table_mut(&mut p4[page.p4_index()])?;
let p3_entry = &mut p3[page.p3_index()];

if p3_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p3_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

unsafe fn set_flags_p2_entry(
&mut self,
_page: Page<Size2MiB>,
_flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
Err(FlagUpdateError::ParentEntryHugePage)
}

fn translate_page(&self, page: Page<Size2MiB>) -> Result<PhysFrame<Size2MiB>, TranslateError> {
let p4 = &self.level_4_table;
let p3 = self.page_table_walker.next_table(&p4[page.p4_index()])?;
Expand All @@ -280,17 +375,18 @@ impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {

impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
#[inline]
unsafe fn map_to<A>(
unsafe fn map_to_with_table_flags<A>(
&mut self,
page: Page<Size4KiB>,
frame: PhysFrame<Size4KiB>,
flags: PageTableFlags,
parent_table_flags: PageTableFlags,
allocator: &mut A,
) -> Result<MapperFlush<Size4KiB>, MapToError<Size4KiB>>
where
A: FrameAllocator<Size4KiB>,
{
self.map_to_4kib(page, frame, flags, allocator)
self.map_to_4kib(page, frame, flags, parent_table_flags, allocator)
}

fn unmap(
Expand Down Expand Up @@ -344,6 +440,66 @@ impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
Ok(MapperFlush::new(page))
}

unsafe fn set_flags_p4_entry(
&mut self,
page: Page<Size4KiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p4_entry = &mut p4[page.p4_index()];

if p4_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p4_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

unsafe fn set_flags_p3_entry(
&mut self,
page: Page<Size4KiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.next_table_mut(&mut p4[page.p4_index()])?;
let p3_entry = &mut p3[page.p3_index()];

if p3_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p3_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

unsafe fn set_flags_p2_entry(
&mut self,
page: Page<Size4KiB>,
flags: PageTableFlags,
) -> Result<MapperFlushAll, FlagUpdateError> {
let p4 = &mut self.level_4_table;
let p3 = self
.page_table_walker
.next_table_mut(&mut p4[page.p4_index()])?;
let p2 = self
.page_table_walker
.next_table_mut(&mut p3[page.p3_index()])?;
let p2_entry = &mut p2[page.p2_index()];

if p2_entry.is_unused() {
return Err(FlagUpdateError::PageNotMapped);
}

p2_entry.set_flags(flags);

Ok(MapperFlushAll::new())
}

fn translate_page(&self, page: Page<Size4KiB>) -> Result<PhysFrame<Size4KiB>, TranslateError> {
let p4 = &self.level_4_table;
let p3 = self.page_table_walker.next_table(&p4[page.p4_index()])?;
Expand Down Expand Up @@ -461,6 +617,7 @@ impl<P: PhysToVirt> PageTableWalker<P> {
fn create_next_table<'b, A>(
&self,
entry: &'b mut PageTableEntry,
insert_flags: PageTableFlags,
allocator: &mut A,
) -> Result<&'b mut PageTable, PageTableCreateError>
where
Expand All @@ -470,12 +627,15 @@ impl<P: PhysToVirt> PageTableWalker<P> {

if entry.is_unused() {
if let Some(frame) = allocator.allocate_frame() {
entry.set_frame(frame, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
entry.set_frame(frame, insert_flags);
created = true;
} else {
return Err(PageTableCreateError::FrameAllocationFailed);
}
} else {
if !insert_flags.is_empty() && !entry.flags().contains(insert_flags) {
entry.set_flags(entry.flags() | insert_flags);
}
created = false;
}

Expand Down
Loading