Repository: SerenityOS/serenity
File: Kernel/Memory/MemoryManager.cpp (C++)

Code before:
            break;
        case EFI::MemoryType::Reserved:
        case EFI::MemoryType::LoaderCode:
        case EFI::MemoryType::LoaderData:
        case EFI::MemoryType::RuntimeServicesCode:
        case EFI::MemoryType::RuntimeServicesData:
        case EFI::MemoryType::MemoryMappedIO:
        case EFI::MemoryType::MemoryMappedIOPortSpace:
        case EFI::MemoryType::PALCode:
        case EFI::MemoryType::Persistent:
        case EFI::MemoryType::Unaccepted:
#if ARCH(X86_64)
            // Workaround for https://gitlab.com/qemu-project/qemu/-/commit/8504f129450b909c88e199ca44facd35d38ba4de
            // That commit added a reserved 12GiB entry for the benefit of virtual firmware.
            // We can safely ignore this block as it isn't actually reserved on any real hardware.
            // From: https://lore.kernel.org/all/20220701161014.3850-1-joao.m.martins@oracle.com/
            // "Always add the HyperTransport range into e820 even when the relocation isn't
            // done *and* there's >= 40 phys bit that would put max phyusical boundary to 1T
            // This should allow virtual firmware to avoid the reserved range at the
            // 1T boundary on VFs with big bars."
            //
            // On Meteor Lake 3fff0000000-3ffffffffff range covers the GFX address space reservations, at a very
            // high address. Nothing actually ends up using it, so we can ignore it.
            // Moreover if we don't we end up with highest_address at 4 TiB mark which
            // would need an enormous page table to map this huge (0-4TiB) range.
            // Until we support mapping I/O space piecewise we need to skip this range.
            if ((start_paddr.get() != 0x000000fd00000000 || length != (0x000000ffffffffff - 0x000000fd00000000) + 1)
                && start_paddr.get() != 0x000003fff0000000)
#endif
                global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Reserved, start_paddr, length });
            break;
        case EFI::MemoryType::ACPIReclaim:
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_Reclaimable, start_paddr, length });
            break;
        case EFI::MemoryType::ACPI_NVS:
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_NVS, start_paddr, length });
            break;
        case EFI::MemoryType::Unusable:
            dmesgln("MM: Warning, detected bad memory range!");
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::BadMemory, start_paddr, length });
            break;
        default:
            dbgln("MM: Unknown EFI memory type: {}", to_underlying(descriptor->type));
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Unknown, start_paddr, length });
            break;
        }
    }
}

UNMAP_AFTER_INIT void MemoryManager::parse_memory_map_fdt(MemoryManager::GlobalData& global_data, u8 const* fdt_addr)
{

Review comment (suggestion):
Are you sure it's the page table that grows too large?
It's more likely the number of needed `PhysicalPageEntry`s that is too big.

> Until we support mapping I/O space piecewise we need to skip this range.

Not quite, we need to find a better way of managing physical memory maps with large and possibly non-contiguos ranges (mapping IO memory is not the problem).

Code after:
            break;
        case EFI::MemoryType::Reserved:
        case EFI::MemoryType::LoaderCode:
        case EFI::MemoryType::LoaderData:
        case EFI::MemoryType::RuntimeServicesCode:
        case EFI::MemoryType::RuntimeServicesData:
        case EFI::MemoryType::MemoryMappedIO:
        case EFI::MemoryType::MemoryMappedIOPortSpace:
        case EFI::MemoryType::PALCode:
        case EFI::MemoryType::Persistent:
        case EFI::MemoryType::Unaccepted:
#if ARCH(X86_64)
            // Workaround for https://gitlab.com/qemu-project/qemu/-/commit/8504f129450b909c88e199ca44facd35d38ba4de
            // That commit added a reserved 12GiB entry for the benefit of virtual firmware.
            // We can safely ignore this block as it isn't actually reserved on any real hardware.
            // From: https://lore.kernel.org/all/20220701161014.3850-1-joao.m.martins@oracle.com/
            // "Always add the HyperTransport range into e820 even when the relocation isn't
            // done *and* there's >= 40 phys bit that would put max phyusical boundary to 1T
            // This should allow virtual firmware to avoid the reserved range at the
            // 1T boundary on VFs with big bars."
            //
            // On Meteor Lake systems, coreboot marks the address range used by the P2SB device
            // (0x3fff0000000-0x3ffffffffff) as reserved. Nothing actually ends up using it,
            // so we can ignore it.
            // Moreover if we don't we end up with highest_address at 4 TiB mark which
            // would need an enormous metadata storage for this huge (0-4TiB) range.
            // Until we support discontinous regions better we need to skip this range.
            if ((start_paddr.get() != 0x000000fd00000000 || length != (0x000000ffffffffff - 0x000000fd00000000) + 1)
                && (start_paddr.get() != 0x000003fff0000000 || length != 0x10000000))
#endif
                global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Reserved, start_paddr, length });
            break;
        case EFI::MemoryType::ACPIReclaim:
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_Reclaimable, start_paddr, length });
            break;
        case EFI::MemoryType::ACPI_NVS:
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_NVS, start_paddr, length });
            break;
        case EFI::MemoryType::Unusable:
            dmesgln("MM: Warning, detected bad memory range!");
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::BadMemory, start_paddr, length });
            break;
        default:
            dbgln("MM: Unknown EFI memory type: {}", to_underlying(descriptor->type));
            global_data.physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Unknown, start_paddr, length });
            break;
        }
    }
}

UNMAP_AFTER_INIT void MemoryManager::parse_memory_map_fdt(MemoryManager::GlobalData& global_data, u8 const* fdt_addr)