Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit BIOS bootloader's max_phys_addr to 4 GiB #260

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/bin/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bootloader::{
};
use core::{
arch::{asm, global_asm},
cmp,
panic::PanicInfo,
slice,
};
Expand Down Expand Up @@ -78,16 +79,23 @@ fn bootloader_main(
use bootloader::binary::{
bios::memory_descriptor::E820MemoryRegion, legacy_memory_region::LegacyFrameAllocator,
};
const GIGABYTE: u64 = 4096 * 512 * 512;

let e820_memory_map = {
let ptr = usize_from(memory_map_addr.as_u64()) as *const E820MemoryRegion;
unsafe { slice::from_raw_parts(ptr, usize_from(memory_map_entry_count)) }
};
let max_phys_addr = e820_memory_map
.iter()
.map(|r| r.start_addr + r.len)
.max()
.expect("no physical memory regions found");
let max_phys_addr = {
let max = e820_memory_map
.iter()
.map(|r| r.start_addr + r.len)
.max()
.expect("no physical memory regions found");
// Don't consider addresses > 4GiB when determining the maximum physical
// address for the bootloader, as we are in protected mode and cannot
// address more than 4 GiB of memory anyway.
cmp::min(max, 4 * GIGABYTE)
};

let mut frame_allocator = {
let kernel_end = PhysFrame::containing_address(kernel_start + kernel_size - 1u64);
Expand All @@ -106,7 +114,7 @@ fn bootloader_main(
// identity-map remaining physical memory (first gigabyte is already identity-mapped)
{
let start_frame: PhysFrame<Size2MiB> =
PhysFrame::containing_address(PhysAddr::new(4096 * 512 * 512));
PhysFrame::containing_address(PhysAddr::new(GIGABYTE));
let end_frame = PhysFrame::containing_address(PhysAddr::new(max_phys_addr - 1));
for frame in PhysFrame::range_inclusive(start_frame, end_frame) {
unsafe {
Expand Down