Skip to content

Commit 5c81659

Browse files
binbinwu1acrnsi
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: #3607 Signed-off-by: Binbin Wu <binbin.wu@intel.com> Reviewed-by: Anthony Xu <anthony.xu@intel.com>
1 parent 2abd8b3 commit 5c81659

File tree

7 files changed

+43
-30
lines changed

7 files changed

+43
-30
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_del_mr(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ int32_t create_vm(uint16_t vm_id, struct acrn_vm_config *vm_config, struct acrn_
429429

430430
init_ept_mem_ops(vm);
431431
vm->arch_vm.nworld_eptp = vm->arch_vm.ept_mem_ops.get_pml4_page(vm->arch_vm.ept_mem_ops.info);
432-
sanitize_pte((uint64_t *)vm->arch_vm.nworld_eptp);
432+
sanitize_pte((uint64_t *)vm->arch_vm.nworld_eptp, &vm->arch_vm.ept_mem_ops);
433433

434-
/* Register default handlers for PIO & MMIO if it is SOS VM or Pre-launched VM */
434+
/* Register default handlers for PIO & MMIO if it is, SOS VM or Pre-launched VM */
435435
if ((vm_config->load_order == SOS_VM) || (vm_config->load_order == PRE_LAUNCHED_VM)) {
436436
register_pio_default_emulation_handler(vm);
437437
register_mmio_default_emulation_handler(vm);

hypervisor/arch/x86/mmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ static inline uint64_t get_sanitized_page(void)
148148
return hva2hpa(sanitized_page);
149149
}
150150

151-
void sanitize_pte_entry(uint64_t *ptep)
151+
void sanitize_pte_entry(uint64_t *ptep, const struct memory_ops *mem_ops)
152152
{
153-
set_pgentry(ptep, get_sanitized_page());
153+
set_pgentry(ptep, get_sanitized_page(), mem_ops);
154154
}
155155

156-
void sanitize_pte(uint64_t *pt_page)
156+
void sanitize_pte(uint64_t *pt_page, const struct memory_ops *mem_ops)
157157
{
158158
uint64_t i;
159159
for (i = 0UL; i < PTRS_PER_PTE; i++) {
160-
sanitize_pte_entry(pt_page + i);
160+
sanitize_pte_entry(pt_page + i, mem_ops);
161161
}
162162
}
163163

@@ -294,7 +294,7 @@ void init_paging(void)
294294
enable_paging();
295295

296296
/* set ptep in sanitized_page point to itself */
297-
sanitize_pte((uint64_t *)sanitized_page);
297+
sanitize_pte((uint64_t *)sanitized_page, &ppt_mem_ops);
298298
}
299299

300300
/*

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
/*
@@ -99,7 +99,7 @@ static void modify_or_del_pte(const uint64_t *pde, uint64_t vaddr_start, uint64_
9999
if (mem_ops->pgentry_present(*pte) == 0UL) {
100100
ASSERT(false, "invalid op, pte not present");
101101
} else {
102-
local_modify_or_del_pte(pte, prot_set, prot_clr, type);
102+
local_modify_or_del_pte(pte, prot_set, prot_clr, type, mem_ops);
103103
vaddr += PTE_SIZE;
104104
if (vaddr >= vaddr_end) {
105105
break;
@@ -134,7 +134,7 @@ static void modify_or_del_pde(const uint64_t *pdpte, uint64_t vaddr_start, uint6
134134
if ((vaddr_next > vaddr_end) || (!mem_aligned_check(vaddr, PDE_SIZE))) {
135135
split_large_page(pde, IA32E_PD, vaddr, mem_ops);
136136
} else {
137-
local_modify_or_del_pte(pde, prot_set, prot_clr, type);
137+
local_modify_or_del_pte(pde, prot_set, prot_clr, type, mem_ops);
138138
if (vaddr_next < vaddr_end) {
139139
vaddr = vaddr_next;
140140
continue;
@@ -178,7 +178,7 @@ static void modify_or_del_pdpte(const uint64_t *pml4e, uint64_t vaddr_start, uin
178178
(!mem_aligned_check(vaddr, PDPTE_SIZE))) {
179179
split_large_page(pdpte, IA32E_PDPT, vaddr, mem_ops);
180180
} else {
181-
local_modify_or_del_pte(pdpte, prot_set, prot_clr, type);
181+
local_modify_or_del_pte(pdpte, prot_set, prot_clr, type, mem_ops);
182182
if (vaddr_next < vaddr_end) {
183183
vaddr = vaddr_next;
184184
continue;
@@ -251,7 +251,7 @@ static void add_pte(const uint64_t *pde, uint64_t paddr_start, uint64_t vaddr_st
251251
if (mem_ops->pgentry_present(*pte) != 0UL) {
252252
ASSERT(false, "invalid op, pte present");
253253
} else {
254-
set_pgentry(pte, paddr | prot);
254+
set_pgentry(pte, paddr | prot, mem_ops);
255255
paddr += PTE_SIZE;
256256
vaddr += PTE_SIZE;
257257

@@ -284,7 +284,7 @@ static void add_pde(const uint64_t *pdpte, uint64_t paddr_start, uint64_t vaddr_
284284
if (mem_aligned_check(paddr, PDE_SIZE) &&
285285
mem_aligned_check(vaddr, PDE_SIZE) &&
286286
(vaddr_next <= vaddr_end)) {
287-
set_pgentry(pde, paddr | (prot | PAGE_PSE));
287+
set_pgentry(pde, paddr | (prot | PAGE_PSE), mem_ops);
288288
if (vaddr_next < vaddr_end) {
289289
paddr += (vaddr_next - vaddr);
290290
vaddr = vaddr_next;
@@ -293,7 +293,7 @@ static void add_pde(const uint64_t *pdpte, uint64_t paddr_start, uint64_t vaddr_
293293
break; /* done */
294294
} else {
295295
void *pt_page = mem_ops->get_pt_page(mem_ops->info, vaddr);
296-
construct_pgentry(pde, pt_page, mem_ops->get_default_access_right());
296+
construct_pgentry(pde, pt_page, mem_ops->get_default_access_right(), mem_ops);
297297
}
298298
}
299299
add_pte(pde, paddr, vaddr, vaddr_end, prot, mem_ops);
@@ -326,7 +326,7 @@ static void add_pdpte(const uint64_t *pml4e, uint64_t paddr_start, uint64_t vadd
326326
if (mem_aligned_check(paddr, PDPTE_SIZE) &&
327327
mem_aligned_check(vaddr, PDPTE_SIZE) &&
328328
(vaddr_next <= vaddr_end)) {
329-
set_pgentry(pdpte, paddr | (prot | PAGE_PSE));
329+
set_pgentry(pdpte, paddr | (prot | PAGE_PSE), mem_ops);
330330
if (vaddr_next < vaddr_end) {
331331
paddr += (vaddr_next - vaddr);
332332
vaddr = vaddr_next;
@@ -335,7 +335,7 @@ static void add_pdpte(const uint64_t *pml4e, uint64_t paddr_start, uint64_t vadd
335335
break; /* done */
336336
} else {
337337
void *pd_page = mem_ops->get_pd_page(mem_ops->info, vaddr);
338-
construct_pgentry(pdpte, pd_page, mem_ops->get_default_access_right());
338+
construct_pgentry(pdpte, pd_page, mem_ops->get_default_access_right(), mem_ops);
339339
}
340340
}
341341
add_pde(pdpte, paddr, vaddr, vaddr_end, prot, mem_ops);
@@ -371,7 +371,7 @@ void mmu_add(uint64_t *pml4_page, uint64_t paddr_base, uint64_t vaddr_base, uint
371371
pml4e = pml4e_offset(pml4_page, vaddr);
372372
if (mem_ops->pgentry_present(*pml4e) == 0UL) {
373373
void *pdpt_page = mem_ops->get_pdpt_page(mem_ops->info, vaddr);
374-
construct_pgentry(pml4e, pdpt_page, mem_ops->get_default_access_right());
374+
construct_pgentry(pml4e, pdpt_page, mem_ops->get_default_access_right(), mem_ops);
375375
}
376376
add_pdpte(pml4e, paddr, vaddr, vaddr_end, prot, mem_ops);
377377

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
@@ -257,9 +257,10 @@ static inline uint64_t get_pgentry(const uint64_t *pte)
257257
/*
258258
* pgentry may means pml4e/pdpte/pde/pte
259259
*/
260-
static inline void set_pgentry(uint64_t *ptep, uint64_t pte)
260+
static inline void set_pgentry(uint64_t *ptep, uint64_t pte, const struct memory_ops *mem_ops)
261261
{
262262
*ptep = pte;
263+
mem_ops->clflush_pagewalk(ptep);
263264
}
264265

265266
static inline uint64_t pde_large(uint64_t pde)

0 commit comments

Comments
 (0)