Skip to content

Commit cbf6d01

Browse files
committed
fix(x86_64): don't modify page tables at runtime
1 parent 23842ee commit cbf6d01

File tree

4 files changed

+36
-168
lines changed

4 files changed

+36
-168
lines changed

src/arch/x86_64/mod.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ mod console;
22
#[cfg(target_os = "none")]
33
mod gdt;
44
#[cfg(target_os = "none")]
5-
mod paging;
6-
#[cfg(target_os = "none")]
75
mod physicalmem;
86
mod platform;
97
#[cfg(target_os = "none")]
@@ -18,28 +16,14 @@ pub use self::platform::{boot_kernel, find_kernel};
1816
const KERNEL_STACK_SIZE: u64 = 32_768;
1917
pub const SERIAL_IO_PORT: u16 = 0x3F8;
2018

21-
#[cfg(target_os = "none")]
22-
unsafe fn map_memory(address: usize, memory_size: usize) -> usize {
23-
use align_address::Align;
24-
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB};
25-
26-
let address = address.align_up(Size2MiB::SIZE as usize);
27-
let page_count = memory_size.align_up(Size2MiB::SIZE as usize) / Size2MiB::SIZE as usize;
28-
29-
paging::map::<Size2MiB>(address, address, page_count, PageTableFlags::WRITABLE);
30-
31-
address
32-
}
33-
3419
#[cfg(target_os = "none")]
3520
pub unsafe fn get_memory(memory_size: u64) -> u64 {
3621
use align_address::Align;
3722
use x86_64::structures::paging::{PageSize, Size2MiB};
3823

3924
use self::physicalmem::PhysAlloc;
4025

41-
let address = PhysAlloc::allocate((memory_size as usize).align_up(Size2MiB::SIZE as usize));
42-
unsafe { map_memory(address, memory_size as usize) as u64 }
26+
PhysAlloc::allocate((memory_size as usize).align_up(Size2MiB::SIZE as usize)) as u64
4327
}
4428

4529
pub unsafe fn enter_kernel(

src/arch/x86_64/paging.rs

Lines changed: 0 additions & 114 deletions
This file was deleted.

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use hermit_entry::boot_info::{
1010
use hermit_entry::elf::LoadedKernel;
1111
use linux_boot_params::{BootE820Entry, BootParams};
1212
use log::{error, info};
13-
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB, Size4KiB};
13+
use x86_64::structures::paging::{PageSize, Size2MiB, Size4KiB};
1414

1515
use crate::BootInfoExt;
1616
use crate::arch::x86_64::physicalmem::PhysAlloc;
17-
use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT, paging};
17+
use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT};
1818
use crate::fdt::Fdt;
1919

2020
unsafe extern "C" {
@@ -35,8 +35,6 @@ mod entry {
3535
}
3636

3737
pub fn find_kernel() -> &'static [u8] {
38-
paging::clean_up();
39-
4038
unsafe {
4139
BootParams::map();
4240
}
@@ -67,12 +65,12 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
6765
// determine boot stack address
6866
let stack = (ptr::addr_of!(loader_end).addr() + Size4KiB::SIZE as usize)
6967
.align_up(Size4KiB::SIZE as usize);
70-
paging::map::<Size4KiB>(
71-
stack,
72-
stack,
73-
KERNEL_STACK_SIZE as usize / Size4KiB::SIZE as usize,
74-
PageTableFlags::WRITABLE,
75-
);
68+
// paging::map::<Size4KiB>(
69+
// stack,
70+
// stack,
71+
// KERNEL_STACK_SIZE as usize / Size4KiB::SIZE as usize,
72+
// PageTableFlags::WRITABLE,
73+
// );
7674
let stack = ptr::addr_of_mut!(loader_end).with_addr(stack);
7775
// clear stack
7876
unsafe {
@@ -152,7 +150,7 @@ impl BootParamsExt for BootParams {
152150
assert_ne!(addr, 0);
153151

154152
// Identity-map the boot parameters.
155-
paging::map::<Size4KiB>(addr, addr, 1, PageTableFlags::empty());
153+
// paging::map::<Size4KiB>(addr, addr, 1, PageTableFlags::empty());
156154
}
157155

158156
unsafe fn get() -> &'static Self {
@@ -197,15 +195,15 @@ impl BootParamsExt for BootParams {
197195
let count = (ramdisk_image.align_up(Size2MiB::SIZE as usize) - ramdisk_image)
198196
/ Size4KiB::SIZE as usize;
199197
if count > 0 {
200-
paging::map::<Size4KiB>(ramdisk_image, ramdisk_image, count, PageTableFlags::empty());
198+
// paging::map::<Size4KiB>(ramdisk_image, ramdisk_image, count, PageTableFlags::empty());
201199
}
202200

203201
// Map the rest of the image in 2MiB steps.
204202
let addr = ramdisk_image.align_up(Size2MiB::SIZE as usize);
205203
let count = ((ramdisk_image + ramdisk_size).align_up(Size2MiB::SIZE as usize) - addr)
206204
/ Size2MiB::SIZE as usize;
207205
if count > 0 {
208-
paging::map::<Size2MiB>(addr, addr, count, PageTableFlags::empty());
206+
// paging::map::<Size2MiB>(addr, addr, count, PageTableFlags::empty());
209207
}
210208

211209
let ramdisk_ptr = ptr::with_exposed_provenance(ramdisk_image);
@@ -221,7 +219,7 @@ impl BootParamsExt for BootParams {
221219
assert_ne!(cmd_line_ptr, 0, "boot protocol is older than 2.02");
222220
assert!(cmd_line_ptr.is_aligned_to(Size4KiB::SIZE as usize));
223221

224-
paging::map::<Size4KiB>(cmd_line_ptr, cmd_line_ptr, 1, PageTableFlags::empty());
222+
// paging::map::<Size4KiB>(cmd_line_ptr, cmd_line_ptr, 1, PageTableFlags::empty());
225223

226224
let ptr = ptr::with_exposed_provenance(cmd_line_ptr);
227225
let bytes = unsafe { core::slice::from_raw_parts(ptr, cmdline_size) };

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use hermit_entry::elf::LoadedKernel;
1010
use log::info;
1111
use multiboot::information::{MemoryManagement, Multiboot, PAddr};
1212
use vm_fdt::FdtWriterResult;
13-
use x86_64::structures::paging::{PageSize, PageTableFlags, Size2MiB, Size4KiB};
13+
use x86_64::structures::paging::{PageSize, Size2MiB, Size4KiB};
1414

1515
use crate::BootInfoExt;
1616
use crate::arch::x86_64::physicalmem::PhysAlloc;
17-
use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT, paging};
17+
use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT};
1818
use crate::fdt::Fdt;
1919

2020
unsafe extern "C" {
@@ -81,12 +81,12 @@ impl DeviceTree {
8181
pub fn find_kernel() -> &'static [u8] {
8282
use core::cmp;
8383

84-
paging::clean_up();
84+
// paging::clean_up();
8585
// Identity-map the Multiboot information.
8686
unsafe {
8787
assert!(mb_info > 0, "Could not find Multiboot information");
8888
info!("Found Multiboot information at {mb_info:#x}");
89-
paging::map::<Size4KiB>(mb_info, mb_info, 1, PageTableFlags::empty())
89+
// paging::map::<Size4KiB>(mb_info, mb_info, 1, PageTableFlags::empty())
9090
}
9191

9292
let mut mem = Mem;
@@ -126,21 +126,21 @@ pub fn find_kernel() -> &'static [u8] {
126126
// mapping starts. We cannot start the 2 MiB mapping right from
127127
// `first_module.end` because when it is aligned down, the
128128
// resulting mapping range may overlap with the 4 KiB mapping.
129-
let first_module_mapping_end = first_module.start.align_up(Size2MiB::SIZE) as usize;
130-
paging::map_range::<Size4KiB>(
131-
first_module.start as usize,
132-
first_module.start as usize,
133-
first_module_mapping_end,
134-
PageTableFlags::empty(),
135-
);
129+
// let first_module_mapping_end = first_module.start.align_up(Size2MiB::SIZE) as usize;
130+
// paging::map_range::<Size4KiB>(
131+
// first_module.start as usize,
132+
// first_module.start as usize,
133+
// first_module_mapping_end,
134+
// PageTableFlags::empty(),
135+
// );
136136

137137
// map also the rest of the modules
138-
paging::map_range::<Size2MiB>(
139-
first_module_mapping_end,
140-
first_module_mapping_end,
141-
modules_mapping_end,
142-
PageTableFlags::empty(),
143-
);
138+
// paging::map_range::<Size2MiB>(
139+
// first_module_mapping_end,
140+
// first_module_mapping_end,
141+
// modules_mapping_end,
142+
// PageTableFlags::empty(),
143+
// );
144144

145145
unsafe { slice::from_raw_parts(ptr::with_exposed_provenance(elf_start), elf_len) }
146146
}
@@ -174,12 +174,12 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
174174
}
175175

176176
// map stack in the address space
177-
paging::map::<Size4KiB>(
178-
new_stack,
179-
new_stack,
180-
KERNEL_STACK_SIZE as usize / Size4KiB::SIZE as usize,
181-
PageTableFlags::WRITABLE,
182-
);
177+
// paging::map::<Size4KiB>(
178+
// new_stack,
179+
// new_stack,
180+
// KERNEL_STACK_SIZE as usize / Size4KiB::SIZE as usize,
181+
// PageTableFlags::WRITABLE,
182+
// );
183183

184184
let stack = ptr::addr_of_mut!(loader_end).with_addr(new_stack);
185185

0 commit comments

Comments
 (0)