From 951a24cd3d31fb9db4e8e0ab3ab70f8c990bf297 Mon Sep 17 00:00:00 2001 From: Chaohong guo Date: Mon, 17 Sep 2018 14:19:33 +0800 Subject: [PATCH] allocate boot related struct right after hypervisor memory To reduce the call to dynamic memory allocation, the patch tries to alloate memroy together with hypervisor when hypervisor is being relocated by efi stub code. The memory allocated will be right at the end of HV memory. Three structs will be done in this way: 1) boot_ctx, which saves EFI boot state and is passed to SOS; 2) multiboot_info, faked multi-boot header for passing boot info to hypervisor; and 3) multiboot_mmap, e820 mmap structure. after this, the EFI stub code (which boot hypervisor) will only have 3 to dynamic memory: 1. the call for hv binary and the 3 struct; 2. the call to CPU boot trampoline code; 3. the call to alloc mmap buf when inquery memory layout from UEFI FW; Tracked-On:#1260 Signed-off-by: Chaohong Guo Reviewed-by: Jason Chen Reviewed-by: Anthony Xu Reviewed-by: Eddie Dong Acked-by: Gen Zheng --- hypervisor/bsp/uefi/efi/boot.c | 48 +++++++++++------------------ hypervisor/bsp/uefi/efi/boot.h | 18 +++++++++++ hypervisor/bsp/uefi/efi/multiboot.h | 2 +- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/hypervisor/bsp/uefi/efi/boot.c b/hypervisor/bsp/uefi/efi/boot.c index 12195e728..38c92ca3d 100644 --- a/hypervisor/bsp/uefi/efi/boot.c +++ b/hypervisor/bsp/uefi/efi/boot.c @@ -66,8 +66,7 @@ static inline void hv_jump(EFI_PHYSICAL_ADDRESS hv_start, hf(MULTIBOOT_INFO_MAGIC, mbi); } -EFI_STATUS -construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx) +EFI_STATUS construct_mbi(EFI_PHYSICAL_ADDRESS hv_hpa) { UINTN map_size, _map_size, map_key; UINT32 desc_version; @@ -77,22 +76,14 @@ construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx) EFI_STATUS err = EFI_SUCCESS; struct multiboot_info *mbi; struct multiboot_mmap *mmap; + struct boot_ctx *efi_ctx; int i, j; - /* multiboot info */ - err = emalloc(16384, 8, &addr); - if (err != EFI_SUCCESS) - goto out; - - mbi = (struct multiboot_info *)(UINTN)addr; - (void)memset((void *)mbi, 0x0, sizeof(*mbi)); - - /* allocate mmap[] */ - err = emalloc(sizeof(struct multiboot_mmap)*128, 8, &addr); - if (err != EFI_SUCCESS) - goto out; - mmap = (struct multiboot_mmap *)(UINTN)addr; - (void)memset((void *)mmap, 0x0, sizeof(*mmap)*128); + mbi = MBOOT_INFO_PTR(hv_hpa); + mmap = MBOOT_MMAP_PTR(hv_hpa); + efi_ctx = BOOT_CTX_PTR(hv_hpa); + (void)memset((void *)mbi, 0x0, MBOOT_INFO_SIZE); + (void)memset((void *)mmap, 0x0, MBOOT_MMAP_SIZE); /* We're just interested in the map's size for now */ map_size = 0; @@ -188,7 +179,7 @@ again: * available RAM in e820 table */ mmap[j].mm_base_addr = hv_hpa; - mmap[j].mm_length = CONFIG_RAM_SIZE; + mmap[j].mm_length = HV_RUNTIME_MEM_SIZE; mmap[j].mm_type = E820_RAM; j++; @@ -201,29 +192,26 @@ again: mbi->mi_flags |= MULTIBOOT_INFO_HAS_DRIVES; mbi->mi_drives_addr = (UINT32)(UINTN)efi_ctx; - *mbi_ret = mbi; out: return err; } static EFI_STATUS -switch_to_guest_mode(EFI_HANDLE image) +switch_to_guest_mode(EFI_HANDLE image, EFI_PHYSICAL_ADDRESS hv_hpa) { EFI_PHYSICAL_ADDRESS addr; EFI_STATUS err; - struct multiboot_info *mbi = NULL; + struct multiboot_info *mbi; struct boot_ctx *efi_ctx; struct acpi_table_rsdp *rsdp = NULL; int i; EFI_CONFIGURATION_TABLE *config_table; - err = emalloc(sizeof(struct boot_ctx), 8, &addr); - if (err != EFI_SUCCESS) - goto out; + mbi = MBOOT_INFO_PTR(hv_hpa); + efi_ctx = BOOT_CTX_PTR(hv_hpa); + (void)memset((void *)efi_ctx, 0x0, BOOT_CTX_SIZE); - efi_ctx = (struct boot_ctx *)(UINTN)addr; - - /* reserve secondary memory region for hv */ + /* reserve secondary memory region for CPU trampoline code */ err = emalloc_reserved_mem(&addr, CONFIG_LOW_RAM_SIZE, MEM_ADDR_1MB); if (err != EFI_SUCCESS) goto out; @@ -259,7 +247,7 @@ switch_to_guest_mode(EFI_HANDLE image) efi_ctx->rsdp = rsdp; /* construct multiboot info and deliver it to hypervisor */ - err = construct_mbi(&mbi, efi_ctx); + err = construct_mbi(hv_hpa); if (err != EFI_SUCCESS) goto out; @@ -377,9 +365,9 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table) * instead. */ #ifdef CONFIG_RELOC - err = emalloc_reserved_mem(&hv_hpa, CONFIG_RAM_SIZE, MEM_ADDR_4GB); + err = emalloc_reserved_mem(&hv_hpa, HV_RUNTIME_MEM_SIZE, MEM_ADDR_4GB); #else - err = emalloc_fixed_addr(&hv_hpa, CONFIG_RAM_SIZE, CONFIG_RAM_START); + err = emalloc_fixed_addr(&hv_hpa, HV_RUNTIME_MEM_SIZE, CONFIG_RAM_START); #endif if (err != EFI_SUCCESS) goto failed; @@ -387,7 +375,7 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table) memcpy((char *)hv_hpa, info->ImageBase + sec_addr, sec_size); /* load hypervisor and begin to run on it */ - err = switch_to_guest_mode(image); + err = switch_to_guest_mode(image, hv_hpa); if (err != EFI_SUCCESS) goto failed; diff --git a/hypervisor/bsp/uefi/efi/boot.h b/hypervisor/bsp/uefi/efi/boot.h index e7bbf38ce..57e76ce66 100644 --- a/hypervisor/bsp/uefi/efi/boot.h +++ b/hypervisor/bsp/uefi/efi/boot.h @@ -69,6 +69,24 @@ EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size); typedef void(*hv_func)(int, struct multiboot_info*); +/* + * We allocate memory for the following struct together with hyperivosr itself + * memory allocation during boot. + */ +#define MBOOT_MMAP_NUMS 128 +#define MBOOT_MMAP_SIZE (sizeof(struct multiboot_mmap) * MBOOT_MMAP_NUMS) +#define MBOOT_INFO_SIZE (sizeof(struct multiboot_info)) +#define BOOT_CTX_SIZE (sizeof(struct boot_ctx)) +#define HV_RUNTIME_MEM_SIZE \ + (CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE + BOOT_CTX_SIZE) +#define MBOOT_MMAP_PTR(addr) \ + ((struct multiboot_mmap *)((VOID *)addr + CONFIG_RAM_SIZE)) +#define MBOOT_INFO_PTR(addr) ((struct multiboot_info *) \ + ((VOID *)addr + CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE)) +#define BOOT_CTX_PTR(addr) ((struct boot_ctx *) \ + ((VOID *)addr + CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE)) + + struct efi_info { UINT32 efi_loader_signature; UINT32 efi_systab; diff --git a/hypervisor/bsp/uefi/efi/multiboot.h b/hypervisor/bsp/uefi/efi/multiboot.h index 28fcd7187..b28fb1c43 100644 --- a/hypervisor/bsp/uefi/efi/multiboot.h +++ b/hypervisor/bsp/uefi/efi/multiboot.h @@ -147,7 +147,7 @@ struct multiboot_info { uint32_t unused_mi_vbe_interface_seg; uint32_t unused_mi_vbe_interface_off; uint32_t unused_mi_vbe_interface_len; -}; +}__attribute__((aligned(8))); /*