Skip to content

Commit 951a24c

Browse files
chaohong-guolijinxia
authored andcommitted
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 <chaohong.guo@intel.com> Reviewed-by: Jason Chen <jason.cj.chen@intel.com> Reviewed-by: Anthony Xu <Anthony.Xu@intel.com> Reviewed-by: Eddie Dong <Eddid.Dong@intel.com> Acked-by: Gen Zheng <gen.zheng@intel.com>
1 parent 6085781 commit 951a24c

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

hypervisor/bsp/uefi/efi/boot.c

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ static inline void hv_jump(EFI_PHYSICAL_ADDRESS hv_start,
6666
hf(MULTIBOOT_INFO_MAGIC, mbi);
6767
}
6868

69-
EFI_STATUS
70-
construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx)
69+
EFI_STATUS construct_mbi(EFI_PHYSICAL_ADDRESS hv_hpa)
7170
{
7271
UINTN map_size, _map_size, map_key;
7372
UINT32 desc_version;
@@ -77,22 +76,14 @@ construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx)
7776
EFI_STATUS err = EFI_SUCCESS;
7877
struct multiboot_info *mbi;
7978
struct multiboot_mmap *mmap;
79+
struct boot_ctx *efi_ctx;
8080
int i, j;
8181

82-
/* multiboot info */
83-
err = emalloc(16384, 8, &addr);
84-
if (err != EFI_SUCCESS)
85-
goto out;
86-
87-
mbi = (struct multiboot_info *)(UINTN)addr;
88-
(void)memset((void *)mbi, 0x0, sizeof(*mbi));
89-
90-
/* allocate mmap[] */
91-
err = emalloc(sizeof(struct multiboot_mmap)*128, 8, &addr);
92-
if (err != EFI_SUCCESS)
93-
goto out;
94-
mmap = (struct multiboot_mmap *)(UINTN)addr;
95-
(void)memset((void *)mmap, 0x0, sizeof(*mmap)*128);
82+
mbi = MBOOT_INFO_PTR(hv_hpa);
83+
mmap = MBOOT_MMAP_PTR(hv_hpa);
84+
efi_ctx = BOOT_CTX_PTR(hv_hpa);
85+
(void)memset((void *)mbi, 0x0, MBOOT_INFO_SIZE);
86+
(void)memset((void *)mmap, 0x0, MBOOT_MMAP_SIZE);
9687

9788
/* We're just interested in the map's size for now */
9889
map_size = 0;
@@ -188,7 +179,7 @@ construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx)
188179
* available RAM in e820 table
189180
*/
190181
mmap[j].mm_base_addr = hv_hpa;
191-
mmap[j].mm_length = CONFIG_RAM_SIZE;
182+
mmap[j].mm_length = HV_RUNTIME_MEM_SIZE;
192183
mmap[j].mm_type = E820_RAM;
193184
j++;
194185

@@ -201,29 +192,26 @@ construct_mbi(struct multiboot_info **mbi_ret, struct boot_ctx *efi_ctx)
201192
mbi->mi_flags |= MULTIBOOT_INFO_HAS_DRIVES;
202193
mbi->mi_drives_addr = (UINT32)(UINTN)efi_ctx;
203194

204-
*mbi_ret = mbi;
205195
out:
206196
return err;
207197
}
208198

209199
static EFI_STATUS
210-
switch_to_guest_mode(EFI_HANDLE image)
200+
switch_to_guest_mode(EFI_HANDLE image, EFI_PHYSICAL_ADDRESS hv_hpa)
211201
{
212202
EFI_PHYSICAL_ADDRESS addr;
213203
EFI_STATUS err;
214-
struct multiboot_info *mbi = NULL;
204+
struct multiboot_info *mbi;
215205
struct boot_ctx *efi_ctx;
216206
struct acpi_table_rsdp *rsdp = NULL;
217207
int i;
218208
EFI_CONFIGURATION_TABLE *config_table;
219209

220-
err = emalloc(sizeof(struct boot_ctx), 8, &addr);
221-
if (err != EFI_SUCCESS)
222-
goto out;
223-
224-
efi_ctx = (struct boot_ctx *)(UINTN)addr;
210+
mbi = MBOOT_INFO_PTR(hv_hpa);
211+
efi_ctx = BOOT_CTX_PTR(hv_hpa);
212+
(void)memset((void *)efi_ctx, 0x0, BOOT_CTX_SIZE);
225213

226-
/* reserve secondary memory region for hv */
214+
/* reserve secondary memory region for CPU trampoline code */
227215
err = emalloc_reserved_mem(&addr, CONFIG_LOW_RAM_SIZE, MEM_ADDR_1MB);
228216
if (err != EFI_SUCCESS)
229217
goto out;
@@ -259,7 +247,7 @@ switch_to_guest_mode(EFI_HANDLE image)
259247
efi_ctx->rsdp = rsdp;
260248

261249
/* construct multiboot info and deliver it to hypervisor */
262-
err = construct_mbi(&mbi, efi_ctx);
250+
err = construct_mbi(hv_hpa);
263251
if (err != EFI_SUCCESS)
264252
goto out;
265253

@@ -377,17 +365,17 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
377365
* instead.
378366
*/
379367
#ifdef CONFIG_RELOC
380-
err = emalloc_reserved_mem(&hv_hpa, CONFIG_RAM_SIZE, MEM_ADDR_4GB);
368+
err = emalloc_reserved_mem(&hv_hpa, HV_RUNTIME_MEM_SIZE, MEM_ADDR_4GB);
381369
#else
382-
err = emalloc_fixed_addr(&hv_hpa, CONFIG_RAM_SIZE, CONFIG_RAM_START);
370+
err = emalloc_fixed_addr(&hv_hpa, HV_RUNTIME_MEM_SIZE, CONFIG_RAM_START);
383371
#endif
384372
if (err != EFI_SUCCESS)
385373
goto failed;
386374

387375
memcpy((char *)hv_hpa, info->ImageBase + sec_addr, sec_size);
388376

389377
/* load hypervisor and begin to run on it */
390-
err = switch_to_guest_mode(image);
378+
err = switch_to_guest_mode(image, hv_hpa);
391379
if (err != EFI_SUCCESS)
392380
goto failed;
393381

hypervisor/bsp/uefi/efi/boot.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@
6969
EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size);
7070
typedef void(*hv_func)(int, struct multiboot_info*);
7171

72+
/*
73+
* We allocate memory for the following struct together with hyperivosr itself
74+
* memory allocation during boot.
75+
*/
76+
#define MBOOT_MMAP_NUMS 128
77+
#define MBOOT_MMAP_SIZE (sizeof(struct multiboot_mmap) * MBOOT_MMAP_NUMS)
78+
#define MBOOT_INFO_SIZE (sizeof(struct multiboot_info))
79+
#define BOOT_CTX_SIZE (sizeof(struct boot_ctx))
80+
#define HV_RUNTIME_MEM_SIZE \
81+
(CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE + BOOT_CTX_SIZE)
82+
#define MBOOT_MMAP_PTR(addr) \
83+
((struct multiboot_mmap *)((VOID *)addr + CONFIG_RAM_SIZE))
84+
#define MBOOT_INFO_PTR(addr) ((struct multiboot_info *) \
85+
((VOID *)addr + CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE))
86+
#define BOOT_CTX_PTR(addr) ((struct boot_ctx *) \
87+
((VOID *)addr + CONFIG_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE))
88+
89+
7290
struct efi_info {
7391
UINT32 efi_loader_signature;
7492
UINT32 efi_systab;

hypervisor/bsp/uefi/efi/multiboot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct multiboot_info {
147147
uint32_t unused_mi_vbe_interface_seg;
148148
uint32_t unused_mi_vbe_interface_off;
149149
uint32_t unused_mi_vbe_interface_len;
150-
};
150+
}__attribute__((aligned(8)));
151151

152152

153153
/*

0 commit comments

Comments
 (0)