From fea102ea6959ede1f5c62740693ad33899757a2b Mon Sep 17 00:00:00 2001 From: Chaohong guo Date: Mon, 17 Sep 2018 13:21:58 +0800 Subject: [PATCH] Remove emalloc_for_low_mem() routine in EFI boot code of HV CPU boot binary need to reside in the memory below 1MB. UEFI firmware does provide that functionality to limit the highest physical addr of allocated memory. we just call the right UEFI API and no longer do it in EFI stub code. Tracked-On:#1260 Signed-off-by: Chaohong Guo Reviewed-by: Jason Chen Acked-by: Gen Zheng Reviewed-by: Anthony Xu --- hypervisor/bsp/uefi/efi/boot.c | 4 +- hypervisor/bsp/uefi/efi/efilinux.h | 18 +++++++++ hypervisor/bsp/uefi/efi/malloc.c | 59 ------------------------------ hypervisor/bsp/uefi/efi/stdlib.h | 1 - 4 files changed, 21 insertions(+), 61 deletions(-) diff --git a/hypervisor/bsp/uefi/efi/boot.c b/hypervisor/bsp/uefi/efi/boot.c index 8e2eeb669..d2282f523 100644 --- a/hypervisor/bsp/uefi/efi/boot.c +++ b/hypervisor/bsp/uefi/efi/boot.c @@ -224,9 +224,11 @@ switch_to_guest_mode(EFI_HANDLE image) efi_ctx = (struct boot_ctx *)(UINTN)addr; /* reserve secondary memory region for hv */ - err = emalloc_for_low_mem(&addr, CONFIG_LOW_RAM_SIZE); + err = emalloc_reserved_mem(&addr, CONFIG_LOW_RAM_SIZE, MEM_ADDR_1MB); if (err != EFI_SUCCESS) goto out; + if (addr < 4096) + Print(L"Warning: CPU trampoline code buf occupied zero-page\n"); efi_ctx->ap_trampoline_buf = (void *)addr; diff --git a/hypervisor/bsp/uefi/efi/efilinux.h b/hypervisor/bsp/uefi/efi/efilinux.h index 08fc49f90..70aad77e2 100644 --- a/hypervisor/bsp/uefi/efi/efilinux.h +++ b/hypervisor/bsp/uefi/efi/efilinux.h @@ -45,6 +45,8 @@ #define EFILINUX_VERSION_MAJOR 1 #define EFILINUX_VERSION_MINOR 0 +#define MEM_ADDR_1MB (1 << 20) + extern EFI_SYSTEM_TABLE *sys_table; extern EFI_BOOT_SERVICES *boot; @@ -184,6 +186,22 @@ handle_protocol(EFI_HANDLE handle, EFI_GUID *protocol, void **interface) } +/* + * emalloc_reserved_mem - it is called to allocate memory hypervisor itself + * and trampoline code, and mark the allocate memory as EfiReserved memory + * type so that SOS won't touch it during boot. + * @addr: a pointer to the allocated address on success + * @size: size in bytes of the requested allocation + * @max_addr: the allocated memory must be no more than this threshold + */ +static inline EFI_STATUS emalloc_reserved_mem(EFI_PHYSICAL_ADDRESS *addr, + UINTN size, EFI_PHYSICAL_ADDRESS max_addr) +{ + *addr = max_addr; + return allocate_pages(AllocateMaxAddress, EfiReservedMemoryType, + EFI_SIZE_TO_PAGES(size), addr); +} + /** * exit - Terminate a loaded EFI image * @image: firmware-allocated handle that identifies the image diff --git a/hypervisor/bsp/uefi/efi/malloc.c b/hypervisor/bsp/uefi/efi/malloc.c index 7b0591fe4..087670ca5 100644 --- a/hypervisor/bsp/uefi/efi/malloc.c +++ b/hypervisor/bsp/uefi/efi/malloc.c @@ -164,65 +164,6 @@ fail: return err; } -EFI_STATUS emalloc_for_low_mem(EFI_PHYSICAL_ADDRESS *addr, UINTN size) -{ - UINTN map_size, map_key, desc_size; - EFI_MEMORY_DESCRIPTOR *map_buf; - UINTN d, map_end; - UINT32 desc_version; - EFI_STATUS err; - UINTN nr_pages = EFI_SIZE_TO_PAGES(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 (; d < map_end; d += desc_size) { - EFI_MEMORY_DESCRIPTOR *desc; - EFI_PHYSICAL_ADDRESS start, end, aligned; - - desc = (EFI_MEMORY_DESCRIPTOR *)d; - if (desc->Type != EfiConventionalMemory) - continue; - - if (desc->NumberOfPages < nr_pages) - continue; - - start = desc->PhysicalStart; - end = start + (desc->NumberOfPages << EFI_PAGE_SHIFT); - size = nr_pages << EFI_PAGE_SHIFT; - - /* allocate in low memory only */ - if (start >= 1 << 20) - continue; - - if (end > 1 << 20) - end = (1 << 20); - - if (end - start >= size) { - aligned = end - size; - err = allocate_pages(AllocateAddress, EfiReservedMemoryType, - nr_pages, &aligned); - if (err == EFI_SUCCESS) { - *addr = aligned; - break; - } - } - } - - if (d == map_end) - err = EFI_OUT_OF_RESOURCES; - - free_pool(map_buf); -fail: - return err; -} - EFI_STATUS __emalloc(UINTN size, UINTN min_addr, EFI_PHYSICAL_ADDRESS *addr, EFI_MEMORY_TYPE mem_type) { diff --git a/hypervisor/bsp/uefi/efi/stdlib.h b/hypervisor/bsp/uefi/efi/stdlib.h index 5e1c20df1..8fbbff980 100644 --- a/hypervisor/bsp/uefi/efi/stdlib.h +++ b/hypervisor/bsp/uefi/efi/stdlib.h @@ -50,7 +50,6 @@ extern void *calloc(UINTN nmemb, UINTN size); extern EFI_STATUS emalloc(UINTN, UINTN, EFI_PHYSICAL_ADDRESS *); extern EFI_STATUS __emalloc(UINTN, UINTN, EFI_PHYSICAL_ADDRESS *, EFI_MEMORY_TYPE); -extern EFI_STATUS emalloc_for_low_mem(EFI_PHYSICAL_ADDRESS *addr, UINTN size); extern void efree(EFI_PHYSICAL_ADDRESS, UINTN); static inline void memset(void *dstv, char ch, UINTN size)