Skip to content

Commit 99ff6c4

Browse files
authored
Merge pull request #214 from rust-osdev/rename-phys-to-virt
Rename `PhysToVirt` trait to `PageTableFrameMapping`
2 parents eb9ccb5 + f2940dd commit 99ff6c4

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- **Breaking:** Also return flags for `MapperAllSizes::translate()` ([#207](https://github.com/rust-osdev/x86_64/pull/207))
44
- **Breaking:** Restructure the `TranslateResult` type and create separate `Translate` trait ([#211](https://github.com/rust-osdev/x86_64/pull/211))
5+
- **Breaking:** Rename `PhysToVirt` trait to `PageTableFrameMapping` ([#214](https://github.com/rust-osdev/x86_64/pull/214))
56
- **Breaking:** Use custom error types instead of `()` ([#199](https://github.com/rust-osdev/x86_64/pull/199))
67
- **Breaking:** Remove deprecated items
78
- `UnusedPhysFrame`

src/structures/paging/mapper/mapped_page_table.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ use crate::structures::paging::{
1414
/// memory are possible too, as long as they can be calculated as an `PhysAddr` to
1515
/// `VirtAddr` closure.
1616
#[derive(Debug)]
17-
pub struct MappedPageTable<'a, P: PhysToVirt> {
17+
pub struct MappedPageTable<'a, P: PageTableFrameMapping> {
1818
page_table_walker: PageTableWalker<P>,
1919
level_4_table: &'a mut PageTable,
2020
}
2121

22-
impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
22+
impl<'a, P: PageTableFrameMapping> MappedPageTable<'a, P> {
2323
/// Creates a new `MappedPageTable` that uses the passed closure for converting virtual
2424
/// to physical addresses.
2525
///
2626
/// ## Safety
2727
///
28-
/// This function is unsafe because the caller must guarantee that the passed `phys_to_virt`
28+
/// This function is unsafe because the caller must guarantee that the passed `page_table_frame_mapping`
2929
/// closure is correct. Also, the passed `level_4_table` must point to the level 4 page table
3030
/// of a valid page table hierarchy. Otherwise this function might break memory safety, e.g.
3131
/// by writing to an illegal memory location.
3232
#[inline]
33-
pub unsafe fn new(level_4_table: &'a mut PageTable, phys_to_virt: P) -> Self {
33+
pub unsafe fn new(level_4_table: &'a mut PageTable, page_table_frame_mapping: P) -> Self {
3434
Self {
3535
level_4_table,
36-
page_table_walker: PageTableWalker::new(phys_to_virt),
36+
page_table_walker: PageTableWalker::new(page_table_frame_mapping),
3737
}
3838
}
3939

@@ -142,7 +142,7 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
142142
}
143143
}
144144

145-
impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
145+
impl<'a, P: PageTableFrameMapping> Mapper<Size1GiB> for MappedPageTable<'a, P> {
146146
#[inline]
147147
unsafe fn map_to_with_table_flags<A>(
148148
&mut self,
@@ -250,7 +250,7 @@ impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
250250
}
251251
}
252252

253-
impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
253+
impl<'a, P: PageTableFrameMapping> Mapper<Size2MiB> for MappedPageTable<'a, P> {
254254
#[inline]
255255
unsafe fn map_to_with_table_flags<A>(
256256
&mut self,
@@ -378,7 +378,7 @@ impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
378378
}
379379
}
380380

381-
impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
381+
impl<'a, P: PageTableFrameMapping> Mapper<Size4KiB> for MappedPageTable<'a, P> {
382382
#[inline]
383383
unsafe fn map_to_with_table_flags<A>(
384384
&mut self,
@@ -522,7 +522,7 @@ impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
522522
}
523523
}
524524

525-
impl<'a, P: PhysToVirt> Translate for MappedPageTable<'a, P> {
525+
impl<'a, P: PageTableFrameMapping> Translate for MappedPageTable<'a, P> {
526526
#[allow(clippy::inconsistent_digit_grouping)]
527527
fn translate(&self, addr: VirtAddr) -> TranslateResult {
528528
let p4 = &self.level_4_table;
@@ -585,14 +585,16 @@ impl<'a, P: PhysToVirt> Translate for MappedPageTable<'a, P> {
585585
}
586586

587587
#[derive(Debug)]
588-
struct PageTableWalker<P: PhysToVirt> {
589-
phys_to_virt: P,
588+
struct PageTableWalker<P: PageTableFrameMapping> {
589+
page_table_frame_mapping: P,
590590
}
591591

592-
impl<P: PhysToVirt> PageTableWalker<P> {
592+
impl<P: PageTableFrameMapping> PageTableWalker<P> {
593593
#[inline]
594-
pub unsafe fn new(phys_to_virt: P) -> Self {
595-
Self { phys_to_virt }
594+
pub unsafe fn new(page_table_frame_mapping: P) -> Self {
595+
Self {
596+
page_table_frame_mapping,
597+
}
596598
}
597599

598600
/// Internal helper function to get a reference to the page table of the next level.
@@ -605,7 +607,9 @@ impl<P: PhysToVirt> PageTableWalker<P> {
605607
&self,
606608
entry: &'b PageTableEntry,
607609
) -> Result<&'b PageTable, PageTableWalkError> {
608-
let page_table_ptr = self.phys_to_virt.phys_to_virt(entry.frame()?);
610+
let page_table_ptr = self
611+
.page_table_frame_mapping
612+
.frame_to_pointer(entry.frame()?);
609613
let page_table: &PageTable = unsafe { &*page_table_ptr };
610614

611615
Ok(page_table)
@@ -621,7 +625,9 @@ impl<P: PhysToVirt> PageTableWalker<P> {
621625
&self,
622626
entry: &'b mut PageTableEntry,
623627
) -> Result<&'b mut PageTable, PageTableWalkError> {
624-
let page_table_ptr = self.phys_to_virt.phys_to_virt(entry.frame()?);
628+
let page_table_ptr = self
629+
.page_table_frame_mapping
630+
.frame_to_pointer(entry.frame()?);
625631
let page_table: &mut PageTable = unsafe { &mut *page_table_ptr };
626632

627633
Ok(page_table)
@@ -758,21 +764,16 @@ impl From<PageTableWalkError> for TranslateError {
758764
}
759765
}
760766

761-
/// Trait for converting a physical address to a virtual one.
767+
/// Provides a virtual address mapping for physical page table frames.
762768
///
763769
/// This only works if the physical address space is somehow mapped to the virtual
764770
/// address space, e.g. at an offset.
765-
pub trait PhysToVirt {
771+
///
772+
/// ## Safety
773+
///
774+
/// This trait is unsafe to implement because the implementer must ensure that
775+
/// `frame_to_pointer` returns a valid page table pointer for any given physical frame.
776+
pub unsafe trait PageTableFrameMapping {
766777
/// Translate the given physical frame to a virtual page table pointer.
767-
fn phys_to_virt(&self, phys_frame: PhysFrame) -> *mut PageTable;
768-
}
769-
770-
impl<T> PhysToVirt for T
771-
where
772-
T: Fn(PhysFrame) -> *mut PageTable,
773-
{
774-
#[inline]
775-
fn phys_to_virt(&self, phys_frame: PhysFrame) -> *mut PageTable {
776-
self(phys_frame)
777-
}
778+
fn frame_to_pointer(&self, frame: PhysFrame) -> *mut PageTable;
778779
}

src/structures/paging/mapper/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Abstractions for reading and modifying the mapping of pages.
22
3-
pub use self::mapped_page_table::{MappedPageTable, PhysToVirt};
3+
pub use self::mapped_page_table::{MappedPageTable, PageTableFrameMapping};
44
#[cfg(target_pointer_width = "64")]
55
pub use self::offset_page_table::OffsetPageTable;
66
#[cfg(feature = "instructions")]

src/structures/paging/mapper/offset_page_table.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ struct PhysOffset {
4848
offset: VirtAddr,
4949
}
5050

51-
impl PhysToVirt for PhysOffset {
52-
#[inline]
53-
fn phys_to_virt(&self, frame: PhysFrame) -> *mut PageTable {
54-
let phys = frame.start_address().as_u64();
55-
let virt = self.offset + phys;
51+
unsafe impl PageTableFrameMapping for PhysOffset {
52+
fn frame_to_pointer(&self, frame: PhysFrame) -> *mut PageTable {
53+
let virt = self.offset + frame.start_address().as_u64();
5654
virt.as_mut_ptr()
5755
}
5856
}

0 commit comments

Comments
 (0)