Skip to content

Commit fea102e

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

File tree

4 files changed

+21
-61
lines changed

4 files changed

+21
-61
lines changed

hypervisor/bsp/uefi/efi/boot.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,11 @@ switch_to_guest_mode(EFI_HANDLE image)
224224
efi_ctx = (struct boot_ctx *)(UINTN)addr;
225225

226226
/* reserve secondary memory region for hv */
227-
err = emalloc_for_low_mem(&addr, CONFIG_LOW_RAM_SIZE);
227+
err = emalloc_reserved_mem(&addr, CONFIG_LOW_RAM_SIZE, MEM_ADDR_1MB);
228228
if (err != EFI_SUCCESS)
229229
goto out;
230+
if (addr < 4096)
231+
Print(L"Warning: CPU trampoline code buf occupied zero-page\n");
230232

231233
efi_ctx->ap_trampoline_buf = (void *)addr;
232234

hypervisor/bsp/uefi/efi/efilinux.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#define EFILINUX_VERSION_MAJOR 1
4646
#define EFILINUX_VERSION_MINOR 0
4747

48+
#define MEM_ADDR_1MB (1 << 20)
49+
4850

4951
extern EFI_SYSTEM_TABLE *sys_table;
5052
extern EFI_BOOT_SERVICES *boot;
@@ -184,6 +186,22 @@ handle_protocol(EFI_HANDLE handle, EFI_GUID *protocol, void **interface)
184186
}
185187

186188

189+
/*
190+
* emalloc_reserved_mem - it is called to allocate memory hypervisor itself
191+
* and trampoline code, and mark the allocate memory as EfiReserved memory
192+
* type so that SOS won't touch it during boot.
193+
* @addr: a pointer to the allocated address on success
194+
* @size: size in bytes of the requested allocation
195+
* @max_addr: the allocated memory must be no more than this threshold
196+
*/
197+
static inline EFI_STATUS emalloc_reserved_mem(EFI_PHYSICAL_ADDRESS *addr,
198+
UINTN size, EFI_PHYSICAL_ADDRESS max_addr)
199+
{
200+
*addr = max_addr;
201+
return allocate_pages(AllocateMaxAddress, EfiReservedMemoryType,
202+
EFI_SIZE_TO_PAGES(size), addr);
203+
}
204+
187205
/**
188206
* exit - Terminate a loaded EFI image
189207
* @image: firmware-allocated handle that identifies the image

hypervisor/bsp/uefi/efi/malloc.c

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -164,65 +164,6 @@ EFI_STATUS emalloc(UINTN size, UINTN align, EFI_PHYSICAL_ADDRESS *addr)
164164
return err;
165165
}
166166

167-
EFI_STATUS emalloc_for_low_mem(EFI_PHYSICAL_ADDRESS *addr, UINTN size)
168-
{
169-
UINTN map_size, map_key, desc_size;
170-
EFI_MEMORY_DESCRIPTOR *map_buf;
171-
UINTN d, map_end;
172-
UINT32 desc_version;
173-
EFI_STATUS err;
174-
UINTN nr_pages = EFI_SIZE_TO_PAGES(size);
175-
176-
err = memory_map(&map_buf, &map_size, &map_key,
177-
&desc_size, &desc_version);
178-
179-
if (err != EFI_SUCCESS)
180-
goto fail;
181-
182-
d = (UINTN)map_buf;
183-
map_end = (UINTN)map_buf + map_size;
184-
185-
for (; d < map_end; d += desc_size) {
186-
EFI_MEMORY_DESCRIPTOR *desc;
187-
EFI_PHYSICAL_ADDRESS start, end, aligned;
188-
189-
desc = (EFI_MEMORY_DESCRIPTOR *)d;
190-
if (desc->Type != EfiConventionalMemory)
191-
continue;
192-
193-
if (desc->NumberOfPages < nr_pages)
194-
continue;
195-
196-
start = desc->PhysicalStart;
197-
end = start + (desc->NumberOfPages << EFI_PAGE_SHIFT);
198-
size = nr_pages << EFI_PAGE_SHIFT;
199-
200-
/* allocate in low memory only */
201-
if (start >= 1 << 20)
202-
continue;
203-
204-
if (end > 1 << 20)
205-
end = (1 << 20);
206-
207-
if (end - start >= size) {
208-
aligned = end - size;
209-
err = allocate_pages(AllocateAddress, EfiReservedMemoryType,
210-
nr_pages, &aligned);
211-
if (err == EFI_SUCCESS) {
212-
*addr = aligned;
213-
break;
214-
}
215-
}
216-
}
217-
218-
if (d == map_end)
219-
err = EFI_OUT_OF_RESOURCES;
220-
221-
free_pool(map_buf);
222-
fail:
223-
return err;
224-
}
225-
226167
EFI_STATUS __emalloc(UINTN size, UINTN min_addr, EFI_PHYSICAL_ADDRESS *addr,
227168
EFI_MEMORY_TYPE mem_type)
228169
{

hypervisor/bsp/uefi/efi/stdlib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ extern void *calloc(UINTN nmemb, UINTN size);
5050

5151
extern EFI_STATUS emalloc(UINTN, UINTN, EFI_PHYSICAL_ADDRESS *);
5252
extern EFI_STATUS __emalloc(UINTN, UINTN, EFI_PHYSICAL_ADDRESS *, EFI_MEMORY_TYPE);
53-
extern EFI_STATUS emalloc_for_low_mem(EFI_PHYSICAL_ADDRESS *addr, UINTN size);
5453
extern void efree(EFI_PHYSICAL_ADDRESS, UINTN);
5554

5655
static inline void memset(void *dstv, char ch, UINTN size)

0 commit comments

Comments
 (0)