Skip to content

Commit

Permalink
don't switch to new page tables in bootloader
Browse files Browse the repository at this point in the history
We don't need to do this anymore because we no longer modify the
bootloader's page tables. This also finally makes it possible to access
all memory mapped in by UEFI and not just the memory accessible through
the first PML4 entry. Some UEFI implementations map the frame buffer
into ranges not accessible through the first PML4 entry.
  • Loading branch information
Freax13 committed Oct 26, 2024
1 parent 94a549d commit 1775317
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 46 deletions.
8 changes: 0 additions & 8 deletions bios/stage-4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,6 @@ fn create_page_tables(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Pa
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
let phys_offset = VirtAddr::new(0);

// copy the currently active level 4 page table, because it might be read-only
let bootloader_page_table = {
let frame = x86_64::registers::control::Cr3::read().0;
let table: *mut PageTable = (phys_offset + frame.start_address().as_u64()).as_mut_ptr();
unsafe { OffsetPageTable::new(&mut *table, phys_offset) }
};

// create a new page table hierarchy for the kernel
let (kernel_page_table, kernel_level_4_frame) = {
// get an unused frame for new level 4 page table
Expand All @@ -246,7 +239,6 @@ fn create_page_tables(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> Pa
};

PageTables {
bootloader: bootloader_page_table,
kernel: kernel_page_table,
kernel_level_4_frame,
}
Expand Down
2 changes: 0 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,6 @@ pub fn switch_to_kernel(page_tables: PageTables, mappings: Mappings, boot_info:

/// Provides access to the page tables of the bootloader and kernel address space.
pub struct PageTables {
/// Provides access to the page tables of the bootloader address space.
pub bootloader: OffsetPageTable<'static>,
/// Provides access to the page tables of the kernel address space (not active).
pub kernel: OffsetPageTable<'static>,
/// The physical frame where the level 4 page table of the kernel address space is stored.
Expand Down
36 changes: 0 additions & 36 deletions uefi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,41 +389,6 @@ fn create_page_tables(
// UEFI identity-maps all memory, so the offset between physical and virtual addresses is 0
let phys_offset = VirtAddr::new(0);

// copy the currently active level 4 page table, because it might be read-only
log::trace!("switching to new level 4 table");
let bootloader_page_table = {
let old_table = {
let frame = x86_64::registers::control::Cr3::read().0;
let ptr: *const PageTable = (phys_offset + frame.start_address().as_u64()).as_ptr();
unsafe { &*ptr }
};
let new_frame = frame_allocator
.allocate_frame()
.expect("Failed to allocate frame for new level 4 table");
let new_table: &mut PageTable = {
let ptr: *mut PageTable =
(phys_offset + new_frame.start_address().as_u64()).as_mut_ptr();
// create a new, empty page table
unsafe {
ptr.write(PageTable::new());
&mut *ptr
}
};

// copy the first entry (we don't need to access more than 512 GiB; also, some UEFI
// implementations seem to create an level 4 table entry 0 in all slots)
new_table[0] = old_table[0].clone();

// the first level 4 table entry is now identical, so we can just load the new one
unsafe {
x86_64::registers::control::Cr3::write(
new_frame,
x86_64::registers::control::Cr3Flags::empty(),
);
OffsetPageTable::new(&mut *new_table, phys_offset)
}
};

// create a new page table hierarchy for the kernel
let (kernel_page_table, kernel_level_4_frame) = {
// get an unused frame for new level 4 page table
Expand All @@ -442,7 +407,6 @@ fn create_page_tables(
};

bootloader_x86_64_common::PageTables {
bootloader: bootloader_page_table,
kernel: kernel_page_table,
kernel_level_4_frame,
}
Expand Down

0 comments on commit 1775317

Please sign in to comment.