@@ -27,7 +27,7 @@ impl<'a> OffsetPageTable<'a> {
2727 /// by writing to an illegal memory location.
2828 #[ inline]
2929 pub unsafe fn new ( level_4_table : & ' a mut PageTable , phys_offset : VirtAddr ) -> Self {
30- let phys_offset = PhysOffset { phys_offset } ;
30+ let phys_offset = unsafe { PhysOffset :: new ( phys_offset) } ;
3131 Self {
3232 inner : unsafe { MappedPageTable :: new ( level_4_table, phys_offset) } ,
3333 }
@@ -45,15 +45,43 @@ impl<'a> OffsetPageTable<'a> {
4545
4646 /// Returns the offset used for converting virtual to physical addresses.
4747 pub fn phys_offset ( & self ) -> VirtAddr {
48- self . inner . page_table_frame_mapping ( ) . phys_offset
48+ self . inner . page_table_frame_mapping ( ) . phys_offset ( )
4949 }
5050}
5151
52+ /// A [`PageTableFrameMapping`] implementation that requires that the complete physical memory is mapped at some
53+ /// offset in the virtual address space.
5254#[ derive( Debug ) ]
53- struct PhysOffset {
55+ pub struct PhysOffset {
5456 phys_offset : VirtAddr ,
5557}
5658
59+ impl PhysOffset {
60+ /// Creates a new `PhysOffset` that uses the given offset for converting virtual
61+ /// to physical addresses.
62+ ///
63+ /// The complete physical memory must be mapped in the virtual address space starting at
64+ /// address `phys_offset`. This means that for example physical address `0x5000` can be
65+ /// accessed through virtual address `phys_offset + 0x5000`. This mapping is required because
66+ /// the mapper needs to access page tables, which are not mapped into the virtual address
67+ /// space by default.
68+ ///
69+ /// ## Safety
70+ ///
71+ /// This function is unsafe because the caller must guarantee that the passed `phys_offset`
72+ /// is correct. Otherwise this function might break memory safety, e.g. by writing to an
73+ /// illegal memory location.
74+ #[ inline]
75+ pub unsafe fn new ( phys_offset : VirtAddr ) -> Self {
76+ Self { phys_offset }
77+ }
78+
79+ /// Returns the offset used for converting virtual to physical addresses.
80+ pub fn phys_offset ( & self ) -> VirtAddr {
81+ self . phys_offset
82+ }
83+ }
84+
5785unsafe impl PageTableFrameMapping for PhysOffset {
5886 fn frame_to_pointer ( & self , frame : PhysFrame ) -> * mut PageTable {
5987 let virt = self . phys_offset + frame. start_address ( ) . as_u64 ( ) ;
0 commit comments