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

Code before:
        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."
            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)
{
    VERIFY(g_boot_info.boot_method == BootMethod::PreInit);

    auto const& fdt_header = *reinterpret_cast<::DeviceTree::FlattenedDeviceTreeHeader const*>(fdt_addr);
    auto fdt_buffer = ReadonlyBytes(fdt_addr, fdt_header.totalsize);

    auto const* mem_reserve_block = reinterpret_cast<::DeviceTree::FlattenedDeviceTreeReserveEntry const*>(&fdt_buffer[fdt_header.off_mem_rsvmap]);

    u64 next_block_offset = fdt_header.off_mem_rsvmap + sizeof(::DeviceTree::FlattenedDeviceTreeReserveEntry);

Review comment (bug):
The above comment mainly talks about an old qemu bug,
Could you maybe amend it to explain the new range

Also the commit message is missing a word here and there (suggestion:)
```diff
- Kernel: Ignore reserved range at 0x000003ff f000 0000
+ Kernel/Memory: Ignore Reserved range at 0x000003ff'f000'0000

- On Meteor Lake this range covers gfx address space reservation at very
- high address. Nothing actually uses it, we can ignore it for now.
+ On Meteor Lake this 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.
```

Code after:
        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)
{
    VERIFY(g_boot_info.boot_method == BootMethod::PreInit);