diff --git a/hypervisor/arch/x86/cpu.c b/hypervisor/arch/x86/cpu.c index d995f8e06..07dc044d2 100644 --- a/hypervisor/arch/x86/cpu.c +++ b/hypervisor/arch/x86/cpu.c @@ -289,7 +289,7 @@ void init_pcpu_post(uint16_t pcpu_id) /* * Reserve memory from platform E820 for shadow EPT 4K pages */ - reserve_buffer_for_sept_pages(); + allocate_buffer_for_sept_pages(); pcpu_sync = ALL_CPUS_MASK; /* Start all secondary cores */ diff --git a/hypervisor/arch/x86/guest/vept.c b/hypervisor/arch/x86/guest/vept.c index ac5f43177..26c9ae29b 100644 --- a/hypervisor/arch/x86/guest/vept.c +++ b/hypervisor/arch/x86/guest/vept.c @@ -26,27 +26,42 @@ static spinlock_t nept_desc_bucket_lock; * and sharing of memory between L2 VMs. * * Page table entry need 8 bytes to represent every 4K page frame. - * Total number of bytes = (CONFIG_PLATFORM_RAM_SIZE/4096) * 8 - * Number of pages needed = Total number of bytes needed/4096 + * Total number of bytes = (get_e820_ram_size() / PAGE_SIZE) * 8 + * Number of pages needed = Total number of bytes needed/PAGE_SIZE */ -#define TOTAL_SEPT_4K_PAGES_SIZE ((CONFIG_PLATFORM_RAM_SIZE * 8UL) / 4096UL) -#define TOTAL_SEPT_4K_PAGES_NUM (TOTAL_SEPT_4K_PAGES_SIZE / PAGE_SIZE) +static uint64_t get_total_vept_4k_page_size(void) +{ + return (get_e820_ram_size() * 8UL) / PAGE_SIZE; +} + +static uint64_t get_total_vept_4k_page_num(void) +{ + return get_total_vept_4k_page_size() / PAGE_SIZE; +} static struct page_pool sept_page_pool; static struct page *sept_pages; -static uint64_t sept_page_bitmap[TOTAL_SEPT_4K_PAGES_NUM / 64U]; +static uint64_t *sept_page_bitmap; + +static void allocate_vept_bitmap(void) +{ + sept_page_bitmap = e820_alloc_memory((get_total_4k_page_num() / 64U), ~0UL); +} /* * @brief Reserve space for SEPT 4K pages from platform E820 table * At moment, we only support nested VMX for Service VM. */ -void reserve_buffer_for_sept_pages(void) +void allocate_buffer_for_sept_pages(void) { uint64_t page_base; - page_base = e820_alloc_memory(TOTAL_SEPT_4K_PAGES_SIZE, ~0UL); - set_paging_supervisor(page_base, TOTAL_SEPT_4K_PAGES_SIZE); + page_base = e820_alloc_memory(get_total_vept_4k_page_size(), ~0UL); + + set_paging_supervisor(page_base, get_total_vept_4k_page_size()); + sept_pages = (struct page *)page_base; + allocate_vept_bitmap(); } static bool is_present_ept_entry(uint64_t ept_entry) @@ -535,7 +550,7 @@ int32_t invept_vmexit_handler(struct acrn_vcpu *vcpu) void init_vept(void) { sept_page_pool.start_page = sept_pages; - sept_page_pool.bitmap_size = TOTAL_SEPT_4K_PAGES_NUM / 64U; + sept_page_pool.bitmap_size = get_total_vept_4k_page_num() / 64U; sept_page_pool.bitmap = sept_page_bitmap; sept_page_pool.dummy_page = NULL; spinlock_init(&sept_page_pool.lock); diff --git a/hypervisor/include/arch/x86/asm/guest/vept.h b/hypervisor/include/arch/x86/asm/guest/vept.h index 3d7d030e4..159a62537 100644 --- a/hypervisor/include/arch/x86/asm/guest/vept.h +++ b/hypervisor/include/arch/x86/asm/guest/vept.h @@ -38,7 +38,7 @@ struct nept_desc { uint32_t ref_count; }; -void reserve_buffer_for_sept_pages(void); +void allocate_buffer_for_sept_pages(void); void init_vept(void); uint64_t get_shadow_eptp(uint64_t guest_eptp); struct nept_desc *get_nept_desc(uint64_t guest_eptp); @@ -46,6 +46,6 @@ void put_nept_desc(uint64_t guest_eptp); bool handle_l2_ept_violation(struct acrn_vcpu *vcpu); int32_t invept_vmexit_handler(struct acrn_vcpu *vcpu); #else -static inline void reserve_buffer_for_sept_pages(void) {}; +static inline void allocate_buffer_for_sept_pages(void) {}; #endif /* CONFIG_NVMX_ENABLED */ #endif /* VEPT_H */