From af886fee8c03aa2288e507350e103ddd8455b501 Mon Sep 17 00:00:00 2001 From: Li Fei1 Date: Tue, 5 Nov 2019 18:51:32 +0800 Subject: [PATCH] efi-stub: reserve unconfigure high memory Now ACRN support more and more platforms. However, the default configuration only support board which memory is less than 16 GB. If a board memory is large than 16 GB, the developer needs to configure the memory configuration according to his board. Otherwise, the boot will fail. This's because UEFI BIOS will use the high memory as possible. This patch try to allocate the memory as eraly as possible. So that the BIOS will not access this region. Tracked-On: #4007 Signed-off-by: Li Fei1 --- misc/efi-stub/boot.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/misc/efi-stub/boot.c b/misc/efi-stub/boot.c index 0ff725eda..7658c43e8 100644 --- a/misc/efi-stub/boot.c +++ b/misc/efi-stub/boot.c @@ -347,6 +347,53 @@ static inline EFI_STATUS isspace(CHAR8 ch) return ((uint8_t)ch <= ' '); } +EFI_STATUS reserve_unconfigure_high_memory(void) +{ +#define PLATFORM_LO_MMIO_SIZE 0x80000000UL + UINTN map_size, map_key, desc_size; + EFI_MEMORY_DESCRIPTOR *map_buf; + UINTN d, map_end; + UINTN i; + UINT32 desc_version; + EFI_STATUS err; + UINT64 reserved_hpa; + EFI_PHYSICAL_ADDRESS top_addr_space = CONFIG_PLATFORM_RAM_SIZE + PLATFORM_LO_MMIO_SIZE; + + err = memory_map(&map_buf, &map_size, &map_key, &desc_size, &desc_version); + if (err != EFI_SUCCESS) + goto fail; + + d = (UINTN)map_buf; + map_end = (UINTN)map_buf + map_size; + + for (i = 0; d < map_end; d += desc_size, i++) { + EFI_MEMORY_DESCRIPTOR *desc; + EFI_PHYSICAL_ADDRESS start, end; + + desc = (EFI_MEMORY_DESCRIPTOR *)d; + if (desc->Type != EfiConventionalMemory) + continue; + + start = desc->PhysicalStart; + end = start + (desc->NumberOfPages << EFI_PAGE_SHIFT); + + if (end > top_addr_space) { + if (start < top_addr_space) + start = top_addr_space; + err = emalloc_fixed_addr(&reserved_hpa, end - start, start); + Print(L"memory region (%lx, %lx) is truncated from region (%lx, %lx).", + start, end, desc->PhysicalStart, end); + if (err != EFI_SUCCESS) + break; + } + } + + free_pool(map_buf); +fail: + return err; + +} + #define DEFAULT_UEFI_OS_LOADER_NAME L"\\EFI\\org.clearlinux\\bootloaderx64.efi" /** * efi_main - The entry point for the OS loader image. @@ -423,6 +470,12 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table) goto free_args; } + err = reserve_unconfigure_high_memory(); + if (err != EFI_SUCCESS) { + Print(L"Unable to reserve un-configure high memory %r ", err); + goto free_args; + } + /* without relocateion enabled, hypervisor binary need to reside in * fixed memory address starting from CONFIG_HV_RAM_START, make a call * to emalloc_fixed_addr for that case. With CONFIG_RELOC enabled,