From 01fa6de42cf1abda6bf2636438d7a073edcccd13 Mon Sep 17 00:00:00 2001 From: Chenli Wei Date: Fri, 28 Jan 2022 17:22:23 +0800 Subject: [PATCH] hv: refine the e820 add new entry logic Sometimes the memory to be allocated is not at the end of an entry, that means we have to break one enty into 2 smaller entries, there are two ways to add the new entry to hv_e820, adds to the end or insert it. The initial e820 table is ordered, that's why the e820_alloc_memory interface asssum all entries was sorted, but add new entry to the end will break the orde of hv_e820. So we use insert_e820_entry to replace the add_e820_entry, the new interfeac will keep the orde and users do not need sort again after alloc region Tracked-On: #6690 Acked-by: Anthony Xu Signed-off-by: Chenli Wei --- hypervisor/arch/x86/e820.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/hypervisor/arch/x86/e820.c b/hypervisor/arch/x86/e820.c index fe26ffac4..d2a06254c 100644 --- a/hypervisor/arch/x86/e820.c +++ b/hypervisor/arch/x86/e820.c @@ -102,13 +102,20 @@ uint64_t e820_alloc_memory(uint64_t size_arg, uint64_t max_addr) return ret; } -static void add_e820_entry(uint64_t addr, uint64_t length, uint64_t type) +static void insert_e820_entry(uint32_t index, uint64_t addr, uint64_t length, uint64_t type) { + uint32_t i; + hv_e820_entries_nr++; ASSERT(hv_e820_entries_nr <= E820_MAX_ENTRIES, "e820 entry overflow"); - hv_e820[hv_e820_entries_nr - 1U].baseaddr = addr; - hv_e820[hv_e820_entries_nr - 1U].length = length; - hv_e820[hv_e820_entries_nr - 1U].type = type; + + for (i = hv_e820_entries_nr - 1; i > index; i--) { + hv_e820[i] = hv_e820[i-1]; + } + + hv_e820[index].baseaddr = addr; + hv_e820[index].length = length; + hv_e820[index].type = type; } static uint64_t e820_alloc_region(uint64_t addr, uint64_t size) @@ -137,7 +144,7 @@ static uint64_t e820_alloc_region(uint64_t addr, uint64_t size) * |entry_start..............................entry_end| */ if (end_pa < entry_end) { - add_e820_entry(end_pa, entry_end - end_pa, E820_TYPE_RAM); + insert_e820_entry(i + 1, end_pa, entry_end - end_pa, E820_TYPE_RAM); break; } } else {