11use core:: panic;
22
3- use x86_64:: { PhysAddr , VirtAddr , structures:: paging:: PageTable } ;
3+ use x86_64:: {
4+ PhysAddr , VirtAddr ,
5+ structures:: paging:: {
6+ FrameAllocator , Mapper , OffsetPageTable , Page , PageTable , PhysFrame , Size4KiB ,
7+ } ,
8+ } ;
49
5- pub unsafe fn active_level_4_table ( physical_memory_offset : VirtAddr ) -> & ' static mut PageTable {
10+ pub unsafe fn init ( physical_memory_offset : VirtAddr ) -> OffsetPageTable < ' static > {
11+ unsafe {
12+ let level_4_table = active_level_4_table ( physical_memory_offset) ;
13+ OffsetPageTable :: new ( level_4_table, physical_memory_offset)
14+ }
15+ }
16+
17+ unsafe fn active_level_4_table ( physical_memory_offset : VirtAddr ) -> & ' static mut PageTable {
618 use x86_64:: registers:: control:: Cr3 ;
719
820 let ( level_4_table_frame, _) = Cr3 :: read ( ) ;
@@ -14,6 +26,29 @@ pub unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static
1426 unsafe { & mut * page_table_ptr }
1527}
1628
29+ pub fn create_example_mapping (
30+ page : Page ,
31+ mapper : & mut OffsetPageTable ,
32+ frame_allocator : & mut impl FrameAllocator < Size4KiB > ,
33+ ) {
34+ use x86_64:: structures:: paging:: PageTableFlags as Flags ;
35+
36+ let frame = PhysFrame :: containing_address ( PhysAddr :: new ( 0xb8000 ) ) ;
37+ let flags = Flags :: PRESENT | Flags :: WRITABLE ;
38+
39+ let map_to_result = unsafe { mapper. map_to ( page, frame, flags, frame_allocator) } ;
40+
41+ map_to_result. expect ( "map_to failed" ) . flush ( ) ;
42+ }
43+
44+ pub struct EmptyFrameAllocator ;
45+
46+ unsafe impl FrameAllocator < Size4KiB > for EmptyFrameAllocator {
47+ fn allocate_frame ( & mut self ) -> Option < PhysFrame > {
48+ None
49+ }
50+ }
51+
1752pub unsafe fn translate_addr ( addr : VirtAddr , physical_memory_offset : VirtAddr ) -> Option < PhysAddr > {
1853 translate_addr_inner ( addr, physical_memory_offset)
1954}
0 commit comments