Skip to content

Guard the lower 1MB of memory #446

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

Merged
merged 14 commits into from
Jun 24, 2024
Merged
Prev Previous commit
Next Next commit
Rework: LegacyFrameAllocator::construct_memory_map
The new implementation takes in a list of slices that are used by the
bootloader. It then goes through all regions and checks if they overlap
any of those slices. If so, the region is split and this process is
started recursively until none of the usable regions overlap the memory
used by the bootloader.
  • Loading branch information
Wasabi375 committed Jun 17, 2024
commit e09e68a9081d3c50414b58997fbe6af942e8a91b
1 change: 1 addition & 0 deletions bios/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod racy_cell;
#[cfg_attr(feature = "debug", derive(Debug))]
#[repr(C)]
pub struct BiosInfo {
pub stage_3: Region,
pub stage_4: Region,
pub kernel: Region,
pub ramdisk: Region,
Expand Down
4 changes: 4 additions & 0 deletions bios/stage-2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ fn start(disk_number: u16, partition_table_start: *const u8) -> ! {
vesa_mode.enable().unwrap();

let mut info = BiosInfo {
stage_3: Region {
start: STAGE_3_DST as u64,
len: stage_3_len,
},
stage_4: Region {
start: stage_4_dst as u64,
len: stage_4_len,
Expand Down
17 changes: 15 additions & 2 deletions bios/stage-4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use crate::memory_descriptor::MemoryRegion;
use bootloader_api::info::{FrameBufferInfo, PixelFormat};
use bootloader_boot_config::{BootConfig, LevelFilter};
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion};
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion, Region};
use bootloader_x86_64_common::legacy_memory_region::UsedMemorySlice;
use bootloader_x86_64_common::RawFrameBufferInfo;
use bootloader_x86_64_common::{
legacy_memory_region::LegacyFrameAllocator, load_and_switch_to_kernel, Kernel, PageTables,
Expand Down Expand Up @@ -55,9 +56,10 @@ pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
};
let kernel_size = info.kernel.len;
let next_free_frame = PhysFrame::containing_address(PhysAddr::new(info.last_used_addr)) + 1;
let mut frame_allocator = LegacyFrameAllocator::new_starting_at(
let mut frame_allocator = LegacyFrameAllocator::new_with_used_slices(
next_free_frame,
memory_map.iter().copied().map(MemoryRegion),
used_memory_slices(info),
);

// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
Expand Down Expand Up @@ -216,6 +218,17 @@ fn init_logger(
framebuffer_info
}

fn used_memory_slices(info: &BiosInfo) -> impl Iterator<Item = UsedMemorySlice> + Clone {
// skip kernel and ramdisk because they are handled individually by the
// uefi/bios common code
[info.stage_3, info.stage_4, info.config_file]
.into_iter()
.map(|region| UsedMemorySlice {
start: PhysAddr::new(region.start).as_u64(),
len: region.len,
})
}

/// Creates page table abstraction types for both the bootloader and kernel page tables.
fn create_page_tables(frame_allocator: &mut impl FrameAllocator<Size4KiB>) -> PageTables {
// We identity-mapped all memory, so the offset between physical and virtual addresses is 0
Expand Down
Loading