Skip to content

better handling of very large memory maps #259

Open
@hawkw

Description

@hawkw

Currently, the bootloader has some issues handling BIOS memory maps that contain very high addresses, such as around the 1TB mark, and/or very large regions (in excess of 500 GB). Depending on the bootloader version and configuration, memory maps with high addresses or large regions may result in crashes or degrade boot performance significantly.

In particular, the following issues have been observed:

  • v0.9.x of bootloader contains an assertion that may be triggered when the memory map contains an address too big for a single PML4 entry if the map_physical_memory feature flag is enabled:

    bootloader/src/main.rs

    Lines 289 to 291 in 42da77b

    // If offset not manually provided, find a free p4 entry and map memory here.
    // One level 4 entry spans 2^48/512 bytes (over 500gib) so this should suffice.
    assert!(max_phys_addr < (1 << 48) / 512);
    (see also panicked at 'assertion failed: max_phys_addr < (1 << 48) / 512' in post-09 phil-opp/blog_os#1138)
  • v0.10.x of bootloader doesn't contain that assertion, but does have offset selection code that assumes no region will require multiple PML4 entries:
    let base =
    Page::from_page_table_indices_1gib(self.get_free_entry(), PageTableIndex::new(0))
    .start_address();
  • v0.10.x of bootloader exhibits very long boot times when a memory map contains a reserved region at a ~1TB offset. This is because the bootloader will identity map all pages up to the highest reserved address in the memory map using 4K pages, and does this twice (once for the bootloader itself, and a second time when setting up physical memory mappings for the kernel). This results in a very slow boot process.

These issues are relevant because AMD systems with an IOMMU have a reserved hole close to the 1TB mark. In recent QEMU versions (>= v7.1.0), QEMU will report this region as reserved in its e380 BIOS memory map, resulting in assertion failures or boot performance degradation. I would assume this would cause issues when booting on real AMD hardware, as well.

Some changes that would improve how bootloader handles high addresses in the memory map (many of which were suggested by @phil-opp on Gitter):

  • Change the automatic offset selection to support regions that need multiple entries in the level 4 page table (v0.9.x, v0.10.x, and probably v0.11.x)
  • Don't identity map reserved regions at all when performing the initial identity mapping for the bootloader itself (v0.10.x and probably v0.11.x)
    • The framebuffer would still need to be identity mapped so that the bootloader can write to it.
  • Use 2MB rather than 4KB pages when identity mapping the kernel address space, which would improve performance (v0.10.x and v0.11.x)
  • Consider not identity mapping holes in the memory map at all, only the reserved regions that would need to be mapped for the kernel. This way, we wouldn't map every page between the second-highest reserved region and the 1TB hole on AMD systems.
  • Consider not identity mapping that specific reserved region, at all. It's an unusable hole, not a MMIO region or BIOS structure that the kernel would actually want to access...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions