Skip to content

Commit 2d55b49

Browse files
binbinwu1wenlingz
authored andcommitted
hv: ept: flush cache for modified ept entries
EPT tables are shared by MMU and IOMMU. Some IOMMUs don't support page-walk coherency, the cpu cache of EPT entires should be flushed to memory after modifications, so that the modifications are visible to the IOMMUs. This patch adds a new interface to flush the cache of modified EPT entires. There are different implementations for EPT/PPT entries: - For PPT, there is no need to flush the cpu cache after update. - For EPT, need to call iommu_flush_cache to make the modifications visible to IOMMUs. Tracked-On: #4120 Signed-off-by: Binbin Wu <binbin.wu@intel.com> Reviewed-by: Anthony Xu <anthony.xu@intel.com>
1 parent a6944fe commit 2d55b49

File tree

7 files changed

+42
-29
lines changed

7 files changed

+42
-29
lines changed

hypervisor/arch/x86/guest/trusty.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void create_secure_world_ept(struct acrn_vm *vm, uint64_t gpa_orig,
7979
pml4_base = vm->arch_vm.ept_mem_ops.info->ept.sworld_pgtable_base;
8080
(void)memset(pml4_base, 0U, PAGE_SIZE);
8181
vm->arch_vm.sworld_eptp = pml4_base;
82-
sanitize_pte((uint64_t *)vm->arch_vm.sworld_eptp);
82+
sanitize_pte((uint64_t *)vm->arch_vm.sworld_eptp, &vm->arch_vm.ept_mem_ops);
8383

8484
/* The trusty memory is remapped to guest physical address
8585
* of gpa_rebased to gpa_rebased + size
@@ -88,7 +88,7 @@ static void create_secure_world_ept(struct acrn_vm *vm, uint64_t gpa_orig,
8888
TRUSTY_PML4_PAGE_NUM(TRUSTY_EPT_REBASE_GPA);
8989
(void)memset(sub_table_addr, 0U, PAGE_SIZE);
9090
sworld_pml4e = hva2hpa(sub_table_addr) | table_present;
91-
set_pgentry((uint64_t *)pml4_base, sworld_pml4e);
91+
set_pgentry((uint64_t *)pml4_base, sworld_pml4e, &vm->arch_vm.ept_mem_ops);
9292

9393
nworld_pml4e = get_pgentry((uint64_t *)vm->arch_vm.nworld_eptp);
9494

@@ -102,7 +102,7 @@ static void create_secure_world_ept(struct acrn_vm *vm, uint64_t gpa_orig,
102102
pdpte = get_pgentry(src_pdpte_p);
103103
if ((pdpte & table_present) != 0UL) {
104104
pdpte &= ~EPT_EXE;
105-
set_pgentry(dest_pdpte_p, pdpte);
105+
set_pgentry(dest_pdpte_p, pdpte, &vm->arch_vm.ept_mem_ops);
106106
}
107107
src_pdpte_p++;
108108
dest_pdpte_p++;
@@ -133,7 +133,7 @@ void destroy_secure_world(struct acrn_vm *vm, bool need_clr_mem)
133133

134134
ept_mr_del(vm, vm->arch_vm.sworld_eptp, gpa_uos, size);
135135
/* sanitize trusty ept page-structures */
136-
sanitize_pte((uint64_t *)vm->arch_vm.sworld_eptp);
136+
sanitize_pte((uint64_t *)vm->arch_vm.sworld_eptp, &vm->arch_vm.ept_mem_ops);
137137
vm->arch_vm.sworld_eptp = NULL;
138138

139139
/* Restore memory to guest normal world */

hypervisor/arch/x86/guest/vm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ int32_t create_vm(uint16_t vm_id, struct acrn_vm_config *vm_config, struct acrn_
333333

334334
init_ept_mem_ops(vm);
335335
vm->arch_vm.nworld_eptp = vm->arch_vm.ept_mem_ops.get_pml4_page(vm->arch_vm.ept_mem_ops.info);
336-
sanitize_pte((uint64_t *)vm->arch_vm.nworld_eptp);
336+
sanitize_pte((uint64_t *)vm->arch_vm.nworld_eptp, &vm->arch_vm.ept_mem_ops);
337337

338338
/* Register default handlers for PIO & MMIO if it is SOS VM or Pre-launched VM */
339339
if ((vm_config->type == SOS_VM) || (vm_config->type == PRE_LAUNCHED_VM)) {

hypervisor/arch/x86/mmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ static inline uint64_t get_sanitized_page(void)
175175
return hva2hpa(sanitized_page);
176176
}
177177

178-
void sanitize_pte_entry(uint64_t *ptep)
178+
void sanitize_pte_entry(uint64_t *ptep, const struct memory_ops *mem_ops)
179179
{
180-
set_pgentry(ptep, get_sanitized_page());
180+
set_pgentry(ptep, get_sanitized_page(), mem_ops);
181181
}
182182

183-
void sanitize_pte(uint64_t *pt_page)
183+
void sanitize_pte(uint64_t *pt_page, const struct memory_ops *mem_ops)
184184
{
185185
uint64_t i;
186186
for (i = 0UL; i < PTRS_PER_PTE; i++) {
187-
sanitize_pte_entry(pt_page + i);
187+
sanitize_pte_entry(pt_page + i, mem_ops);
188188
}
189189
}
190190

@@ -327,5 +327,5 @@ void init_paging(void)
327327
enable_paging();
328328

329329
/* set ptep in sanitized_page point to itself */
330-
sanitize_pte((uint64_t *)sanitized_page);
330+
sanitize_pte((uint64_t *)sanitized_page, &ppt_mem_ops);
331331
}

hypervisor/arch/x86/page.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <mmu.h>
1111
#include <vm.h>
1212
#include <trusty.h>
13+
#include <vtd.h>
1314

1415
static struct page ppt_pml4_pages[PML4_PAGE_NUM(CONFIG_PLATFORM_RAM_SIZE + PLATFORM_LO_MMIO_SIZE)];
1516
static struct page ppt_pdpt_pages[PDPT_PAGE_NUM(CONFIG_PLATFORM_RAM_SIZE + PLATFORM_LO_MMIO_SIZE)];
@@ -29,6 +30,10 @@ static inline uint64_t ppt_get_default_access_right(void)
2930
return (PAGE_PRESENT | PAGE_RW | PAGE_USER);
3031
}
3132

33+
static inline void ppt_clflush_pagewalk(const void* etry __attribute__((unused)))
34+
{
35+
}
36+
3237
static inline uint64_t ppt_pgentry_present(uint64_t pte)
3338
{
3439
return pte & PAGE_PRESENT;
@@ -62,6 +67,7 @@ const struct memory_ops ppt_mem_ops = {
6267
.get_pml4_page = ppt_get_pml4_page,
6368
.get_pdpt_page = ppt_get_pdpt_page,
6469
.get_pd_page = ppt_get_pd_page,
70+
.clflush_pagewalk = ppt_clflush_pagewalk,
6571
};
6672

6773
static struct page sos_vm_pml4_pages[PML4_PAGE_NUM(EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE))];
@@ -107,6 +113,11 @@ static inline uint64_t ept_pgentry_present(uint64_t pte)
107113
return pte & EPT_RWX;
108114
}
109115

116+
static inline void ept_clflush_pagewalk(const void* etry)
117+
{
118+
iommu_flush_cache(etry, sizeof(uint64_t));
119+
}
120+
110121
static inline struct page *ept_get_pml4_page(const union pgtable_pages_info *info)
111122
{
112123
struct page *pml4_page = info->ept.nworld_pml4_base;
@@ -175,5 +186,5 @@ void init_ept_mem_ops(struct acrn_vm *vm)
175186
vm->arch_vm.ept_mem_ops.get_pdpt_page = ept_get_pdpt_page;
176187
vm->arch_vm.ept_mem_ops.get_pd_page = ept_get_pd_page;
177188
vm->arch_vm.ept_mem_ops.get_pt_page = ept_get_pt_page;
178-
189+
vm->arch_vm.ept_mem_ops.clflush_pagewalk = ept_clflush_pagewalk;
179190
}

hypervisor/arch/x86/pagetable.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,37 @@ static void split_large_page(uint64_t *pte, enum _page_table_level level,
4545

4646
paddr = ref_paddr;
4747
for (i = 0UL; i < PTRS_PER_PTE; i++) {
48-
set_pgentry(pbase + i, paddr | ref_prot);
48+
set_pgentry(pbase + i, paddr | ref_prot, mem_ops);
4949
paddr += paddrinc;
5050
}
5151

5252
ref_prot = mem_ops->get_default_access_right();
53-
set_pgentry(pte, hva2hpa((void *)pbase) | ref_prot);
53+
set_pgentry(pte, hva2hpa((void *)pbase) | ref_prot, mem_ops);
5454

5555
/* TODO: flush the TLB */
5656
}
5757

5858
static inline void local_modify_or_del_pte(uint64_t *pte,
59-
uint64_t prot_set, uint64_t prot_clr, uint32_t type)
59+
uint64_t prot_set, uint64_t prot_clr, uint32_t type, const struct memory_ops *mem_ops)
6060
{
6161
if (type == MR_MODIFY) {
6262
uint64_t new_pte = *pte;
6363
new_pte &= ~prot_clr;
6464
new_pte |= prot_set;
65-
set_pgentry(pte, new_pte);
65+
set_pgentry(pte, new_pte, mem_ops);
6666
} else {
67-
sanitize_pte_entry(pte);
67+
sanitize_pte_entry(pte, mem_ops);
6868
}
6969
}
7070

7171
/*
7272
* pgentry may means pml4e/pdpte/pde
7373
*/
74-
static inline void construct_pgentry(uint64_t *pde, void *pd_page, uint64_t prot)
74+
static inline void construct_pgentry(uint64_t *pde, void *pd_page, uint64_t prot, const struct memory_ops *mem_ops)
7575
{
76-
sanitize_pte((uint64_t *)pd_page);
76+
sanitize_pte((uint64_t *)pd_page, mem_ops);
7777

78-
set_pgentry(pde, hva2hpa(pd_page) | prot);
78+
set_pgentry(pde, hva2hpa(pd_page) | prot, mem_ops);
7979
}
8080

8181
/*
@@ -106,7 +106,7 @@ static void modify_or_del_pte(const uint64_t *pde, uint64_t vaddr_start, uint64_
106106
pr_warn("%s, vaddr: 0x%lx pte is not present.\n", __func__, vaddr);
107107
}
108108
} else {
109-
local_modify_or_del_pte(pte, prot_set, prot_clr, type);
109+
local_modify_or_del_pte(pte, prot_set, prot_clr, type, mem_ops);
110110
}
111111

112112
vaddr += PTE_SIZE;
@@ -142,7 +142,7 @@ static void modify_or_del_pde(const uint64_t *pdpte, uint64_t vaddr_start, uint6
142142
if ((vaddr_next > vaddr_end) || (!mem_aligned_check(vaddr, PDE_SIZE))) {
143143
split_large_page(pde, IA32E_PD, vaddr, mem_ops);
144144
} else {
145-
local_modify_or_del_pte(pde, prot_set, prot_clr, type);
145+
local_modify_or_del_pte(pde, prot_set, prot_clr, type, mem_ops);
146146
if (vaddr_next < vaddr_end) {
147147
vaddr = vaddr_next;
148148
continue;
@@ -187,7 +187,7 @@ static void modify_or_del_pdpte(const uint64_t *pml4e, uint64_t vaddr_start, uin
187187
(!mem_aligned_check(vaddr, PDPTE_SIZE))) {
188188
split_large_page(pdpte, IA32E_PDPT, vaddr, mem_ops);
189189
} else {
190-
local_modify_or_del_pte(pdpte, prot_set, prot_clr, type);
190+
local_modify_or_del_pte(pdpte, prot_set, prot_clr, type, mem_ops);
191191
if (vaddr_next < vaddr_end) {
192192
vaddr = vaddr_next;
193193
continue;
@@ -261,7 +261,7 @@ static void add_pte(const uint64_t *pde, uint64_t paddr_start, uint64_t vaddr_st
261261
if (mem_ops->pgentry_present(*pte) != 0UL) {
262262
ASSERT(false, "invalid op, pte present");
263263
} else {
264-
set_pgentry(pte, paddr | prot);
264+
set_pgentry(pte, paddr | prot, mem_ops);
265265
paddr += PTE_SIZE;
266266
vaddr += PTE_SIZE;
267267

@@ -294,7 +294,7 @@ static void add_pde(const uint64_t *pdpte, uint64_t paddr_start, uint64_t vaddr_
294294
if (mem_aligned_check(paddr, PDE_SIZE) &&
295295
mem_aligned_check(vaddr, PDE_SIZE) &&
296296
(vaddr_next <= vaddr_end)) {
297-
set_pgentry(pde, paddr | (prot | PAGE_PSE));
297+
set_pgentry(pde, paddr | (prot | PAGE_PSE), mem_ops);
298298
if (vaddr_next < vaddr_end) {
299299
paddr += (vaddr_next - vaddr);
300300
vaddr = vaddr_next;
@@ -303,7 +303,7 @@ static void add_pde(const uint64_t *pdpte, uint64_t paddr_start, uint64_t vaddr_
303303
break; /* done */
304304
} else {
305305
void *pt_page = mem_ops->get_pt_page(mem_ops->info, vaddr);
306-
construct_pgentry(pde, pt_page, mem_ops->get_default_access_right());
306+
construct_pgentry(pde, pt_page, mem_ops->get_default_access_right(), mem_ops);
307307
}
308308
}
309309
add_pte(pde, paddr, vaddr, vaddr_end, prot, mem_ops);
@@ -336,7 +336,7 @@ static void add_pdpte(const uint64_t *pml4e, uint64_t paddr_start, uint64_t vadd
336336
if (mem_aligned_check(paddr, PDPTE_SIZE) &&
337337
mem_aligned_check(vaddr, PDPTE_SIZE) &&
338338
(vaddr_next <= vaddr_end)) {
339-
set_pgentry(pdpte, paddr | (prot | PAGE_PSE));
339+
set_pgentry(pdpte, paddr | (prot | PAGE_PSE), mem_ops);
340340
if (vaddr_next < vaddr_end) {
341341
paddr += (vaddr_next - vaddr);
342342
vaddr = vaddr_next;
@@ -345,7 +345,7 @@ static void add_pdpte(const uint64_t *pml4e, uint64_t paddr_start, uint64_t vadd
345345
break; /* done */
346346
} else {
347347
void *pd_page = mem_ops->get_pd_page(mem_ops->info, vaddr);
348-
construct_pgentry(pdpte, pd_page, mem_ops->get_default_access_right());
348+
construct_pgentry(pdpte, pd_page, mem_ops->get_default_access_right(), mem_ops);
349349
}
350350
}
351351
add_pde(pdpte, paddr, vaddr, vaddr_end, prot, mem_ops);
@@ -381,7 +381,7 @@ void mmu_add(uint64_t *pml4_page, uint64_t paddr_base, uint64_t vaddr_base, uint
381381
pml4e = pml4e_offset(pml4_page, vaddr);
382382
if (mem_ops->pgentry_present(*pml4e) == 0UL) {
383383
void *pdpt_page = mem_ops->get_pdpt_page(mem_ops->info, vaddr);
384-
construct_pgentry(pml4e, pdpt_page, mem_ops->get_default_access_right());
384+
construct_pgentry(pml4e, pdpt_page, mem_ops->get_default_access_right(), mem_ops);
385385
}
386386
add_pdpte(pml4e, paddr, vaddr, vaddr_end, prot, mem_ops);
387387

hypervisor/include/arch/x86/page.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct memory_ops {
7171
struct page *(*get_pd_page)(const union pgtable_pages_info *info, uint64_t gpa);
7272
struct page *(*get_pt_page)(const union pgtable_pages_info *info, uint64_t gpa);
7373
void *(*get_sworld_memory_base)(const union pgtable_pages_info *info);
74+
void (*clflush_pagewalk)(const void *p);
7475
};
7576

7677
extern const struct memory_ops ppt_mem_ops;

hypervisor/include/arch/x86/pgtable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ static inline uint64_t get_pgentry(const uint64_t *pte)
168168
/*
169169
* pgentry may means pml4e/pdpte/pde/pte
170170
*/
171-
static inline void set_pgentry(uint64_t *ptep, uint64_t pte)
171+
static inline void set_pgentry(uint64_t *ptep, uint64_t pte, const struct memory_ops *mem_ops)
172172
{
173173
*ptep = pte;
174+
mem_ops->clflush_pagewalk(ptep);
174175
}
175176

176177
static inline uint64_t pde_large(uint64_t pde)

0 commit comments

Comments
 (0)