hv: free ept memory enhancement

--add free_paging_struct api, used for free page tables
  it will clear memory before free.
--add HPA2HVA translation when free ept memory

Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com>
Reviewed-by: Jason Chen CJ <jason.cj.chen@intel.com>
Reviewed-by: Anthony Xu <anthony.xu@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Mingqiang Chi 2018-04-03 09:30:15 +08:00 committed by Jack Ren
parent cf7fe07276
commit 5e3dca4c0c
3 changed files with 26 additions and 13 deletions

View File

@ -57,12 +57,11 @@ void *create_guest_paging(struct vm *vm)
return (void *)CPU_Boot_Page_Tables_Start_VM; return (void *)CPU_Boot_Page_Tables_Start_VM;
} }
static void *find_next_table(uint32_t table_offset, static uint64_t find_next_table(uint32_t table_offset, void *table_base)
void *table_base)
{ {
uint64_t table_entry; uint64_t table_entry;
uint64_t table_present; uint64_t table_present;
void *sub_table_addr = 0; uint64_t sub_table_addr = 0;
/* Read the table entry */ /* Read the table entry */
table_entry = MEM_READ64(table_base table_entry = MEM_READ64(table_base
@ -83,13 +82,12 @@ static void *find_next_table(uint32_t table_offset,
} }
/* Get address of the sub-table */ /* Get address of the sub-table */
sub_table_addr = (void *)(table_entry & IA32E_REF_MASK); sub_table_addr = table_entry & IA32E_REF_MASK;
/* Return the next table in the walk */ /* Return the next table in the walk */
return sub_table_addr; return sub_table_addr;
} }
void free_ept_mem(void *pml4_addr) void free_ept_mem(void *pml4_addr)
{ {
void *pdpt_addr; void *pdpt_addr;
@ -99,16 +97,22 @@ void free_ept_mem(void *pml4_addr)
uint32_t pdpt_index; uint32_t pdpt_index;
uint32_t pde_index; uint32_t pde_index;
if (pml4_addr == NULL) {
ASSERT(false, "EPTP is NULL");
return;
}
for (pml4_index = 0; pml4_index < IA32E_NUM_ENTRIES; pml4_index++) { for (pml4_index = 0; pml4_index < IA32E_NUM_ENTRIES; pml4_index++) {
/* Walk from the PML4 table to the PDPT table */ /* Walk from the PML4 table to the PDPT table */
pdpt_addr = find_next_table(pml4_index, pml4_addr); pdpt_addr = HPA2HVA(find_next_table(pml4_index, pml4_addr));
if (pdpt_addr == NULL) if (pdpt_addr == NULL)
continue; continue;
for (pdpt_index = 0; pdpt_index < IA32E_NUM_ENTRIES; for (pdpt_index = 0; pdpt_index < IA32E_NUM_ENTRIES;
pdpt_index++) { pdpt_index++) {
/* Walk from the PDPT table to the PD table */ /* Walk from the PDPT table to the PD table */
pde_addr = find_next_table(pdpt_index, pdpt_addr); pde_addr = HPA2HVA(find_next_table(pdpt_index,
pdpt_addr));
if (pde_addr == NULL) if (pde_addr == NULL)
continue; continue;
@ -116,20 +120,20 @@ void free_ept_mem(void *pml4_addr)
for (pde_index = 0; pde_index < IA32E_NUM_ENTRIES; for (pde_index = 0; pde_index < IA32E_NUM_ENTRIES;
pde_index++) { pde_index++) {
/* Walk from the PD table to the page table */ /* Walk from the PD table to the page table */
pte_addr = find_next_table(pde_index, pte_addr = HPA2HVA(find_next_table(pde_index,
pde_addr); pde_addr));
/* Free page table entry table */ /* Free page table entry table */
if (pte_addr) if (pte_addr)
free(pte_addr); free_paging_struct(pte_addr);
} }
/* Free page directory entry table */ /* Free page directory entry table */
if (pde_addr) if (pde_addr)
free(pde_addr); free_paging_struct(pde_addr);
} }
free(pdpt_addr); free_paging_struct(pdpt_addr);
} }
free(pml4_addr); free_paging_struct(pml4_addr);
} }
void destroy_ept(struct vm *vm) void destroy_ept(struct vm *vm)

View File

@ -512,6 +512,14 @@ void *alloc_paging_struct(void)
return ptr; return ptr;
} }
void free_paging_struct(void *ptr)
{
if (ptr) {
memset(ptr, 0, CPU_PAGE_SIZE);
free(ptr);
}
}
uint64_t config_page_table_attr(struct map_params *map_params, uint32_t flags) uint64_t config_page_table_attr(struct map_params *map_params, uint32_t flags)
{ {
int table_type = map_params->page_table_type; int table_type = map_params->page_table_type;

View File

@ -315,6 +315,7 @@ struct mem_io_node {
void *get_paging_pml4(void); void *get_paging_pml4(void);
void *alloc_paging_struct(void); void *alloc_paging_struct(void);
void free_paging_struct(void *ptr);
void enable_paging(void *pml4_base_addr); void enable_paging(void *pml4_base_addr);
void init_paging(void); void init_paging(void);
void map_mem(struct map_params *map_params, void *paddr, void *vaddr, void map_mem(struct map_params *map_params, void *paddr, void *vaddr,