@@ -14,26 +14,26 @@ use crate::structures::paging::{
14
14
/// memory are possible too, as long as they can be calculated as an `PhysAddr` to
15
15
/// `VirtAddr` closure.
16
16
#[ derive( Debug ) ]
17
- pub struct MappedPageTable < ' a , P : PhysToVirt > {
17
+ pub struct MappedPageTable < ' a , P : PageTableFrameMapping > {
18
18
page_table_walker : PageTableWalker < P > ,
19
19
level_4_table : & ' a mut PageTable ,
20
20
}
21
21
22
- impl < ' a , P : PhysToVirt > MappedPageTable < ' a , P > {
22
+ impl < ' a , P : PageTableFrameMapping > MappedPageTable < ' a , P > {
23
23
/// Creates a new `MappedPageTable` that uses the passed closure for converting virtual
24
24
/// to physical addresses.
25
25
///
26
26
/// ## Safety
27
27
///
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 `
29
29
/// closure is correct. Also, the passed `level_4_table` must point to the level 4 page table
30
30
/// of a valid page table hierarchy. Otherwise this function might break memory safety, e.g.
31
31
/// by writing to an illegal memory location.
32
32
#[ 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 {
34
34
Self {
35
35
level_4_table,
36
- page_table_walker : PageTableWalker :: new ( phys_to_virt ) ,
36
+ page_table_walker : PageTableWalker :: new ( page_table_frame_mapping ) ,
37
37
}
38
38
}
39
39
@@ -142,7 +142,7 @@ impl<'a, P: PhysToVirt> MappedPageTable<'a, P> {
142
142
}
143
143
}
144
144
145
- impl < ' a , P : PhysToVirt > Mapper < Size1GiB > for MappedPageTable < ' a , P > {
145
+ impl < ' a , P : PageTableFrameMapping > Mapper < Size1GiB > for MappedPageTable < ' a , P > {
146
146
#[ inline]
147
147
unsafe fn map_to_with_table_flags < A > (
148
148
& mut self ,
@@ -250,7 +250,7 @@ impl<'a, P: PhysToVirt> Mapper<Size1GiB> for MappedPageTable<'a, P> {
250
250
}
251
251
}
252
252
253
- impl < ' a , P : PhysToVirt > Mapper < Size2MiB > for MappedPageTable < ' a , P > {
253
+ impl < ' a , P : PageTableFrameMapping > Mapper < Size2MiB > for MappedPageTable < ' a , P > {
254
254
#[ inline]
255
255
unsafe fn map_to_with_table_flags < A > (
256
256
& mut self ,
@@ -378,7 +378,7 @@ impl<'a, P: PhysToVirt> Mapper<Size2MiB> for MappedPageTable<'a, P> {
378
378
}
379
379
}
380
380
381
- impl < ' a , P : PhysToVirt > Mapper < Size4KiB > for MappedPageTable < ' a , P > {
381
+ impl < ' a , P : PageTableFrameMapping > Mapper < Size4KiB > for MappedPageTable < ' a , P > {
382
382
#[ inline]
383
383
unsafe fn map_to_with_table_flags < A > (
384
384
& mut self ,
@@ -522,7 +522,7 @@ impl<'a, P: PhysToVirt> Mapper<Size4KiB> for MappedPageTable<'a, P> {
522
522
}
523
523
}
524
524
525
- impl < ' a , P : PhysToVirt > Translate for MappedPageTable < ' a , P > {
525
+ impl < ' a , P : PageTableFrameMapping > Translate for MappedPageTable < ' a , P > {
526
526
#[ allow( clippy:: inconsistent_digit_grouping) ]
527
527
fn translate ( & self , addr : VirtAddr ) -> TranslateResult {
528
528
let p4 = & self . level_4_table ;
@@ -585,14 +585,16 @@ impl<'a, P: PhysToVirt> Translate for MappedPageTable<'a, P> {
585
585
}
586
586
587
587
#[ derive( Debug ) ]
588
- struct PageTableWalker < P : PhysToVirt > {
589
- phys_to_virt : P ,
588
+ struct PageTableWalker < P : PageTableFrameMapping > {
589
+ page_table_frame_mapping : P ,
590
590
}
591
591
592
- impl < P : PhysToVirt > PageTableWalker < P > {
592
+ impl < P : PageTableFrameMapping > PageTableWalker < P > {
593
593
#[ 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
+ }
596
598
}
597
599
598
600
/// Internal helper function to get a reference to the page table of the next level.
@@ -605,7 +607,9 @@ impl<P: PhysToVirt> PageTableWalker<P> {
605
607
& self ,
606
608
entry : & ' b PageTableEntry ,
607
609
) -> 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 ( ) ?) ;
609
613
let page_table: & PageTable = unsafe { & * page_table_ptr } ;
610
614
611
615
Ok ( page_table)
@@ -621,7 +625,9 @@ impl<P: PhysToVirt> PageTableWalker<P> {
621
625
& self ,
622
626
entry : & ' b mut PageTableEntry ,
623
627
) -> 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 ( ) ?) ;
625
631
let page_table: & mut PageTable = unsafe { & mut * page_table_ptr } ;
626
632
627
633
Ok ( page_table)
@@ -758,21 +764,16 @@ impl From<PageTableWalkError> for TranslateError {
758
764
}
759
765
}
760
766
761
- /// Trait for converting a physical address to a virtual one .
767
+ /// Provides a virtual address mapping for physical page table frames .
762
768
///
763
769
/// This only works if the physical address space is somehow mapped to the virtual
764
770
/// 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 {
766
777
/// 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 ;
778
779
}
0 commit comments