File tree Expand file tree Collapse file tree 6 files changed +43
-2
lines changed
Expand file tree Collapse file tree 6 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ mod console;
22#[ cfg( target_os = "none" ) ]
33mod gdt;
44#[ cfg( target_os = "none" ) ]
5+ mod page_tables;
6+ #[ cfg( target_os = "none" ) ]
57mod physicalmem;
68mod platform;
79#[ cfg( target_os = "none" ) ]
Original file line number Diff line number Diff line change 1+ //! Page Tables.
2+ //!
3+ //! This file defines the page tables that we switch to by setting `CR3` to `LEVEL_4_TABLE`.
4+
5+ use core:: ptr;
6+
7+ use x86_64:: structures:: paging:: { PageSize , PageTableFlags , Size1GiB } ;
8+
9+ const LEVEL_4_FLAGS : PageTableFlags = PageTableFlags :: PRESENT . union ( PageTableFlags :: WRITABLE ) ;
10+ const LEVEL_3_FLAGS : PageTableFlags = LEVEL_4_FLAGS . union ( PageTableFlags :: HUGE_PAGE ) ;
11+
12+ pub static mut LEVEL_4_TABLE : PageTable = {
13+ let flags = LEVEL_4_FLAGS . bits ( ) as usize ;
14+ let mut page_table = [ ptr:: null_mut ( ) ; _] ;
15+
16+ page_table[ 0 ] = ( & raw mut LEVEL_3_TABLE ) . wrapping_byte_add ( flags) . cast ( ) ;
17+
18+ PageTable ( page_table)
19+ } ;
20+
21+ static mut LEVEL_3_TABLE : PageTable = {
22+ let flags: usize = LEVEL_3_FLAGS . bits ( ) as usize ;
23+ let mut page_table = [ ptr:: null_mut ( ) ; _] ;
24+
25+ let mut i = 0 ;
26+ while i < page_table. len ( ) {
27+ let addr = i * Size1GiB :: SIZE as usize ;
28+ page_table[ i] = ptr:: with_exposed_provenance_mut ( addr + flags) ;
29+ i += 1 ;
30+ }
31+
32+ PageTable ( page_table)
33+ } ;
34+
35+ #[ repr( align( 0x1000 ) ) ]
36+ #[ repr( C ) ]
37+ pub struct PageTable ( [ * mut ( ) ; 512 ] ) ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ _start:
1717 mov [ boot_params ], rsi
1818
1919 # Set CR3
20- mov rax , OFFSET LEVEL_4_TABLE
20+ mov rax , OFFSET {level_4_table}
2121 mov cr3 , rax
2222
2323 lgdt [ {gdt_ptr} ] # Load the 64 - bit global descriptor table.
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ mod entry {
2828 loader_main = sym crate :: os:: loader_main,
2929 stack = sym crate :: arch:: x86_64:: stack:: STACK ,
3030 stack_top_offset = const crate :: arch:: x86_64:: stack:: Stack :: top_offset( ) ,
31+ level_4_table = sym crate :: arch:: x86_64:: page_tables:: LEVEL_4_TABLE ,
3132 gdt_ptr = sym crate :: arch:: x86_64:: gdt:: GDT_PTR ,
3233 kernel_code_selector = const crate :: arch:: x86_64:: gdt:: Gdt :: kernel_code_selector( ) . 0 ,
3334 kernel_data_selector = const crate :: arch:: x86_64:: gdt:: Gdt :: kernel_data_selector( ) . 0 ,
Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ cpu_init:
7272 jz Linvalid # They aren't , there is no long mode.
7373
7474 # Set CR3
75- mov eax , OFFSET LEVEL_4_TABLE
75+ mov eax , OFFSET {level_4_table}
7676 mov cr3 , eax
7777
7878 # we need to enable PAE modus
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ mod entry {
2929 loader_main = sym crate :: os:: loader_main,
3030 stack = sym crate :: arch:: x86_64:: stack:: STACK ,
3131 stack_top_offset = const crate :: arch:: x86_64:: stack:: Stack :: top_offset( ) ,
32+ level_4_table = sym crate :: arch:: x86_64:: page_tables:: LEVEL_4_TABLE ,
3233 gdt_ptr = sym crate :: arch:: x86_64:: gdt:: GDT_PTR ,
3334 kernel_code_selector = const crate :: arch:: x86_64:: gdt:: Gdt :: kernel_code_selector( ) . 0 ,
3435 kernel_data_selector = const crate :: arch:: x86_64:: gdt:: Gdt :: kernel_data_selector( ) . 0 ,
You can’t perform that action at this time.
0 commit comments