Skip to content

Commit 518a82d

Browse files
ZideChen0Eddie Dong
authored andcommitted
hv: cleanup some hva/hpa conversion code
The init page tables installed in either cpu_primary.S or trampoline.S are 1:1 mapping and won't be changed in the future. The 'actual' hypervisor page table installed in enable_paging() is 1:1 mapping currently but it could be changed in the future. Both hva2hpa() and hpa2hva() are implemented based on these page tables and can't be used when the init page tables take effect. This patch does the following cleanup: - remove all hva2hpa()/hpa2hva() before calling enable_paging() - get_hv_image_base() returns HVA, not HPA. So add hva2hpa() for all cases that are called afte enable_paging(). Tracked-On: #2700 Signed-off-by: Zide Chen <zide.chen@intel.com> Acked-by: Eddie Dong <Eddie.dong@intel.com>
1 parent e9335fc commit 518a82d

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

hypervisor/arch/x86/e820.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,18 @@ void init_e820(void)
111111
uint32_t i;
112112

113113
if (boot_regs[0] == MULTIBOOT_INFO_MAGIC) {
114-
struct multiboot_info *mbi = (struct multiboot_info *)(hpa2hva((uint64_t)boot_regs[1]));
114+
/*
115+
* Before installing new PML4 table in enable_paging(), HPA->HVA is always 1:1 mapping
116+
* and hpa2hva() can't be used to do the conversion. Here we simply treat boot_reg[1] as HPA.
117+
*/
118+
uint64_t hpa = (uint64_t)boot_regs[1];
119+
struct multiboot_info *mbi = (struct multiboot_info *)hpa;
115120

116121
pr_info("Multiboot info detected\n");
117122
if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_MMAP) != 0U) {
118-
struct multiboot_mmap *mmap = (struct multiboot_mmap *)hpa2hva((uint64_t)mbi->mi_mmap_addr);
123+
/* HPA->HVA is always 1:1 mapping at this moment */
124+
hpa = (uint64_t)mbi->mi_mmap_addr;
125+
struct multiboot_mmap *mmap = (struct multiboot_mmap *)hpa;
119126

120127
e820_entries_count = mbi->mi_mmap_length / sizeof(struct multiboot_mmap);
121128
if (e820_entries_count > E820_MAX_ENTRIES) {

hypervisor/arch/x86/guest/vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static void create_sos_vm_e820(struct acrn_vm *vm)
187187
uint32_t i;
188188
uint64_t entry_start;
189189
uint64_t entry_end;
190-
uint64_t hv_start_pa = get_hv_image_base();
190+
uint64_t hv_start_pa = hva2hpa((void *)(get_hv_image_base()));
191191
uint64_t hv_end_pa = hv_start_pa + CONFIG_HV_RAM_SIZE;
192192
uint32_t entries_count = get_e820_entries_count();
193193
struct e820_entry *entry, new_entry = {0};
@@ -301,7 +301,7 @@ static void prepare_sos_vm_memmap(struct acrn_vm *vm)
301301
/* unmap hypervisor itself for safety
302302
* will cause EPT violation if sos accesses hv memory
303303
*/
304-
hv_hpa = get_hv_image_base();
304+
hv_hpa = hva2hpa((void *)(get_hv_image_base()));
305305
ept_mr_del(vm, pml4_page, hv_hpa, CONFIG_HV_RAM_SIZE);
306306
}
307307

hypervisor/arch/x86/mmu.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ void enable_paging(void)
204204
CPU_CR_READ(cr0, &tmp64);
205205
CPU_CR_WRITE(cr0, tmp64 | CR0_WP);
206206

207-
CPU_CR_WRITE(cr3, hva2hpa(ppt_mmu_pml4_addr));
208-
207+
/* HPA->HVA is 1:1 mapping at this moment, simply treat ppt_mmu_pml4_addr as HPA. */
208+
CPU_CR_WRITE(cr3, ppt_mmu_pml4_addr);
209209
}
210210

211211
void enable_smep(void)
@@ -293,6 +293,9 @@ void init_paging(void)
293293
/*
294294
* set the paging-structure entries' U/S flag to supervisor-mode for hypervisor owned memroy.
295295
* (exclude the memory reserve for trusty)
296+
*
297+
* Before the new PML4 take effect in enable_paging(), HPA->HVA is always 1:1 mapping,
298+
* simply treat the return value of get_hv_image_base() as HPA.
296299
*/
297300
hv_hpa = get_hv_image_base();
298301
mmu_modify_or_del((uint64_t *)ppt_mmu_pml4_addr, hv_hpa & PDE_MASK,

hypervisor/boot/reloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ uint64_t get_hv_image_delta(void)
4949
return addr;
5050
}
5151

52-
/* get the actual Hypervisor load address */
52+
/* get the actual Hypervisor load address (HVA) */
5353
uint64_t get_hv_image_base(void)
5454
{
5555
return (get_hv_image_delta() + CONFIG_HV_RAM_START);

hypervisor/common/hypercall.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ static int32_t add_vm_memory_region(struct acrn_vm *vm, struct acrn_vm *target_v
571571
__func__, vm->vm_id, region->sos_vm_gpa);
572572
ret = -EINVAL;
573573
} else {
574-
base_paddr = get_hv_image_base();
574+
base_paddr = hva2hpa((void *)(get_hv_image_base()));
575575
if (((hpa <= base_paddr) && ((hpa + region->size) > base_paddr)) ||
576576
((hpa >= base_paddr) && (hpa < (base_paddr + CONFIG_HV_RAM_SIZE)))) {
577577
pr_err("%s: overlap the HV memory region.", __func__);
@@ -719,7 +719,7 @@ static int32_t write_protect_page(struct acrn_vm *vm,const struct wp_data *wp)
719719
dev_dbg(ACRN_DBG_HYCALL, "[vm%d] gpa=0x%x hpa=0x%x",
720720
vm->vm_id, wp->gpa, hpa);
721721

722-
base_paddr = get_hv_image_base();
722+
base_paddr = hva2hpa((void *)(get_hv_image_base()));
723723
if (((hpa <= base_paddr) && ((hpa + PAGE_SIZE) > base_paddr)) ||
724724
((hpa >= base_paddr) &&
725725
(hpa < (base_paddr + CONFIG_HV_RAM_SIZE)))) {

0 commit comments

Comments
 (0)