Skip to content

Commit 5eb7d54

Browse files
committed
refactor(x86_64): move page tables to Rust
1 parent cbf6d01 commit 5eb7d54

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

src/arch/x86_64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ mod console;
22
#[cfg(target_os = "none")]
33
mod gdt;
44
#[cfg(target_os = "none")]
5+
mod page_tables;
6+
#[cfg(target_os = "none")]
57
mod physicalmem;
68
mod platform;
79
#[cfg(target_os = "none")]

src/arch/x86_64/page_tables.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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]);

src/arch/x86_64/platform/linux/entry.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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.

src/arch/x86_64/platform/linux/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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,

src/arch/x86_64/platform/multiboot/entry.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

src/arch/x86_64/platform/multiboot/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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,

0 commit comments

Comments
 (0)