Skip to content

Paging/Mapper: include active page table flags in translate() result #150

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions src/structures/paging/mapper/mapped_page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,27 @@ impl<'a, P: PhysToVirt> MapperAllSizes for MappedPageTable<'a, P> {
Err(PageTableWalkError::NotMapped) => return TranslateResult::PageNotMapped,
Err(PageTableWalkError::MappedToHugePage) => {
let frame = PhysFrame::containing_address(p3[addr.p3_index()].addr());
let flags = p3[addr.p3_index()].flags();
let offset = addr.as_u64() & 0o_777_777_7777;
return TranslateResult::Frame1GiB { frame, offset };
return TranslateResult::Frame1GiB {
frame,
offset,
flags,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, if we're making this struct larger, it might make more sense to initialize it like:

TranslateResult::Frame1GiB {
    frame: PhysFrame::containing_address(p3[addr.p3_index()].addr()),
    offset: addr.as_u64() & 0o_777_777_7777,
    flags:  p3[addr.p3_index()].flags(),
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the struct init accordingly, though I've left variables in two places, as it's defined in a match and I feel like that isn't as neat to look at.

}
};
let p1 = match self.page_table_walker.next_table(&p2[addr.p2_index()]) {
Ok(page_table) => page_table,
Err(PageTableWalkError::NotMapped) => return TranslateResult::PageNotMapped,
Err(PageTableWalkError::MappedToHugePage) => {
let frame = PhysFrame::containing_address(p2[addr.p2_index()].addr());
let flags = p2[addr.p2_index()].flags();
let offset = addr.as_u64() & 0o_777_7777;
return TranslateResult::Frame2MiB { frame, offset };
return TranslateResult::Frame2MiB {
frame,
offset,
flags,
};
}
};

Expand All @@ -402,7 +412,12 @@ impl<'a, P: PhysToVirt> MapperAllSizes for MappedPageTable<'a, P> {
Err(()) => return TranslateResult::InvalidFrameAddress(p1_entry.addr()),
};
let offset = u64::from(addr.page_offset());
TranslateResult::Frame4KiB { frame, offset }
let flags = p1_entry.flags();
TranslateResult::Frame4KiB {
frame,
offset,
flags,
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/structures/paging/mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ pub trait MapperAllSizes: Mapper<Size4KiB> + Mapper<Size2MiB> + Mapper<Size1GiB>
fn translate_addr(&self, addr: VirtAddr) -> Option<PhysAddr> {
match self.translate(addr) {
TranslateResult::PageNotMapped | TranslateResult::InvalidFrameAddress(_) => None,
TranslateResult::Frame4KiB { frame, offset } => Some(frame.start_address() + offset),
TranslateResult::Frame2MiB { frame, offset } => Some(frame.start_address() + offset),
TranslateResult::Frame1GiB { frame, offset } => Some(frame.start_address() + offset),
TranslateResult::Frame4KiB { frame, offset, .. } => {
Some(frame.start_address() + offset)
}
TranslateResult::Frame2MiB { frame, offset, .. } => {
Some(frame.start_address() + offset)
}
TranslateResult::Frame1GiB { frame, offset, .. } => {
Some(frame.start_address() + offset)
}
}
}
}
Expand All @@ -53,20 +59,26 @@ pub enum TranslateResult {
Frame4KiB {
/// The mapped frame.
frame: PhysFrame<Size4KiB>,
/// Flags in the pagetable for this entry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above we use the order (frame, offset, flags), the declaration here should be consistent. Either we should always use (frame, offset, flags) or (frame, flags, offset).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reordered the fields to adhere to (frame, offset, flags), which is think is the neater way to add this field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clarify here that these are only the flags of the last-level page table entry. (Flags of higher-level page table entries can affect the effective flags for an address too, for example when the WRITABLE flag is not set for a level 3 entry.)

flags: PageTableFlags,
/// The offset whithin the mapped frame.
offset: u64,
},
/// The page is mapped to a physical frame of size 2MiB.
Frame2MiB {
/// The mapped frame.
frame: PhysFrame<Size2MiB>,
/// Flags in the pagetable for this entry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

flags: PageTableFlags,
/// The offset whithin the mapped frame.
offset: u64,
},
/// The page is mapped to a physical frame of size 2MiB.
Frame1GiB {
/// The mapped frame.
frame: PhysFrame<Size1GiB>,
/// Flags in the pagetable for this entry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

flags: PageTableFlags,
/// The offset whithin the mapped frame.
offset: u64,
},
Expand Down
21 changes: 18 additions & 3 deletions src/structures/paging/mapper/recursive_page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,13 @@ impl<'a> MapperAllSizes for RecursivePageTable<'a> {
}
if p3_entry.flags().contains(PageTableFlags::HUGE_PAGE) {
let frame = PhysFrame::containing_address(p3[addr.p3_index()].addr());
let flags = p3[addr.p3_index()].flags();
let offset = addr.as_u64() & 0o_777_777_7777;
return TranslateResult::Frame1GiB { frame, offset };
return TranslateResult::Frame1GiB {
frame,
offset,
flags,
};
}

let p2 = unsafe { &*(p2_ptr(page, self.recursive_index)) };
Expand All @@ -573,8 +578,13 @@ impl<'a> MapperAllSizes for RecursivePageTable<'a> {
}
if p2_entry.flags().contains(PageTableFlags::HUGE_PAGE) {
let frame = PhysFrame::containing_address(p2[addr.p2_index()].addr());
let flags = p2[addr.p2_index()].flags();
let offset = addr.as_u64() & 0o_777_7777;
return TranslateResult::Frame2MiB { frame, offset };
return TranslateResult::Frame2MiB {
frame,
offset,
flags,
};
}

let p1 = unsafe { &*(p1_ptr(page, self.recursive_index)) };
Expand All @@ -591,7 +601,12 @@ impl<'a> MapperAllSizes for RecursivePageTable<'a> {
Err(()) => return TranslateResult::InvalidFrameAddress(p1_entry.addr()),
};
let offset = u64::from(addr.page_offset());
TranslateResult::Frame4KiB { frame, offset }
let flags = p1_entry.flags();
TranslateResult::Frame4KiB {
frame,
offset,
flags,
}
}
}

Expand Down