Skip to content

Commit 229bf32

Browse files
mingqiangchilijinxia
authored andcommitted
hv:Refine destroy_secure_world API
-- add clear trusty memory flag In some cases such as UOS power off or UOS full reset, need to clear trusty memory,no need to clear memory such as UOS S3 or UOS system reset,then add a flag to distinguish it when destroy secure world. -- Restore trusty memory to guest normal world. -- Moved free trusty EPT inside destroy_secure_world In some cases such as UOS S3 or UOS system reset, only need to free trusty EPT, this patch move free trusty EPT inside destroy_secure_world. Because PD/PT are shared in both secure world's EPT and normal world's EPT,before freeing trusty EPT, it will memset all PDPTEs except trusty memory, then call 'free_ept_mem', it can only free trusty EPT, and does't affect shared normal world EPT. v2-->v3: -- Used new mmu api ept_mr_add when restore trusty memory to SOS and normal world -- Dropped this patch "Removed reverted page tables for trusty memory" because map_mem will be removed in future It will have a patch, need to update this api(ept_mr_add), it will not create inverted page tables for trusty memory. v1-->v2: -- free trusty ept still use free_ept_mem, not add a new api,but need to memset pdptes except trusty memory -- Removed reverted page tables for trusty memory. Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 40196d1 commit 229bf32

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

hypervisor/arch/x86/ept.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static uint64_t find_next_table(uint32_t table_offset, void *table_base)
4545
/**
4646
* @pre pml4_addr != NULL
4747
*/
48-
static void free_ept_mem(void *pml4_addr)
48+
void free_ept_mem(void *pml4_addr)
4949
{
5050
void *pdpt_addr;
5151
void *pde_addr;
@@ -98,18 +98,6 @@ void destroy_ept(struct vm *vm)
9898
free_ept_mem(vm->arch_vm.nworld_eptp);
9999
if (vm->arch_vm.m2p != NULL)
100100
free_ept_mem(vm->arch_vm.m2p);
101-
102-
/*
103-
* If secure world is initialized, destroy Secure world ept.
104-
* There are two cases secure world is not initialized:
105-
* - trusty is not enabled. Check sworld_enabled.
106-
* - trusty is enabled. But not initialized yet.
107-
* Check vm->arch_vm.sworld_eptp.
108-
*/
109-
if (vm->sworld_control.flag.active) {
110-
free_ept_mem(HPA2HVA(vm->arch_vm.sworld_eptp));
111-
vm->arch_vm.sworld_eptp = NULL;
112-
}
113101
}
114102

115103
uint64_t local_gpa2hpa(struct vm *vm, uint64_t gpa, uint32_t *size)

hypervisor/arch/x86/guest/vm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ int shutdown_vm(struct vm *vm)
271271

272272
/* Destroy secure world */
273273
if (vm->sworld_control.flag.active) {
274-
destroy_secure_world(vm);
274+
destroy_secure_world(vm, true);
275275
}
276276
/* Free EPT allocated resources assigned to VM */
277277
destroy_ept(vm);

hypervisor/arch/x86/trusty.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ static void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig,
131131
dest_pdpte_p++;
132132
}
133133

134-
/* Map gpa_rebased~gpa_rebased+size
135-
* to secure ept mapping
134+
/* Map gpa_rebased~gpa_rebased+size to secure ept mapping
135+
* TODO: not create inverted page tables for trusty memory
136136
*/
137137
map_params.page_table_type = PTT_EPT;
138138
map_params.pml4_inverted = vm->arch_vm.m2p;
@@ -162,33 +162,49 @@ static void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig,
162162
}
163163
}
164164

165-
void destroy_secure_world(struct vm *vm)
165+
void destroy_secure_world(struct vm *vm, bool need_clr_mem)
166166
{
167-
struct mem_map_params map_params;
167+
void *pdpt_addr;
168168
struct vm *vm0 = get_vm_from_vmid(0U);
169169

170170
if (vm0 == NULL) {
171171
pr_err("Parse vm0 context failed.");
172172
return;
173173
}
174-
175-
/* clear trusty memory space */
176-
(void)memset(HPA2HVA(vm->sworld_control.sworld_memory.base_hpa),
177-
0, vm->sworld_control.sworld_memory.length);
174+
if (need_clr_mem) {
175+
/* clear trusty memory space */
176+
(void)memset(HPA2HVA(vm->sworld_control.sworld_memory.base_hpa),
177+
0U, vm->sworld_control.sworld_memory.length);
178+
}
178179

179180
/* restore memory to SOS ept mapping */
180-
map_params.page_table_type = PTT_EPT;
181-
map_params.pml4_base = vm0->arch_vm.nworld_eptp;
182-
map_params.pml4_inverted = vm0->arch_vm.m2p;
181+
if (ept_mr_add(vm0, vm->sworld_control.sworld_memory.base_hpa,
182+
vm->sworld_control.sworld_memory.base_gpa_in_sos,
183+
vm->sworld_control.sworld_memory.length,
184+
EPT_RWX | EPT_WB) != 0) {
185+
pr_warn("Restore trusty mem to SOS failed");
186+
}
183187

184-
map_mem(&map_params, (void *)vm->sworld_control.sworld_memory.base_hpa,
185-
(void *)vm->sworld_control.sworld_memory.base_gpa_in_sos,
188+
/* Restore memory to guest normal world */
189+
if (ept_mr_add(vm, vm->sworld_control.sworld_memory.base_hpa,
190+
vm->sworld_control.sworld_memory.base_gpa_in_uos,
186191
vm->sworld_control.sworld_memory.length,
187-
(IA32E_EPT_R_BIT |
188-
IA32E_EPT_W_BIT |
189-
IA32E_EPT_X_BIT |
190-
IA32E_EPT_WB));
192+
EPT_RWX | EPT_WB) != 0) {
193+
pr_warn("Restore trusty mem to nworld failed");
194+
}
191195

196+
/* Free trusty ept page-structures */
197+
if (vm->arch_vm.sworld_eptp != NULL) {
198+
pdpt_addr =
199+
(void *)pml4e_page_vaddr(*(uint64_t *)vm->arch_vm.sworld_eptp);
200+
/* memset PDPTEs except trusty memory */
201+
(void)memset(pdpt_addr, 0UL,
202+
NON_TRUSTY_PDPT_ENTRIES * IA32E_COMM_ENTRY_SIZE);
203+
free_ept_mem(vm->arch_vm.sworld_eptp);
204+
vm->arch_vm.sworld_eptp = NULL;
205+
} else {
206+
pr_err("sworld eptp is NULL");
207+
}
192208
}
193209

194210
static void save_world_ctx(struct vcpu *vcpu, struct ext_context *ext_ctx)

hypervisor/include/arch/x86/mmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int ept_mr_modify(struct vm *vm, uint64_t *pml4_page,
389389
uint64_t prot_set, uint64_t prot_clr);
390390
int ept_mr_del(struct vm *vm, uint64_t *pml4_page,
391391
uint64_t gpa, uint64_t size);
392-
392+
void free_ept_mem(void *pml4_addr);
393393
int ept_violation_vmexit_handler(struct vcpu *vcpu);
394394
int ept_misconfig_vmexit_handler(__unused struct vcpu *vcpu);
395395

hypervisor/include/arch/x86/trusty.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#define TRUSTY_EPT_REBASE_GPA (511UL * 1024UL * 1024UL * 1024UL)
1717
#define TRUSTY_MEMORY_SIZE 0x01000000
1818

19+
#define NON_TRUSTY_PDPT_ENTRIES 511U
20+
1921
/* Structure of seed info */
2022
struct seed_info {
2123
uint8_t cse_svn;
@@ -127,7 +129,7 @@ struct trusty_startup_param {
127129

128130
void switch_world(struct vcpu *vcpu, int next_world);
129131
bool initialize_trusty(struct vcpu *vcpu, uint64_t param);
130-
void destroy_secure_world(struct vm *vm);
132+
void destroy_secure_world(struct vm *vm, bool need_clr_mem);
131133
void save_sworld_context(struct vcpu *vcpu);
132134
void restore_sworld_context(struct vcpu *vcpu);
133135
void trusty_set_dseed(void *dseed, uint8_t dseed_num);

0 commit comments

Comments
 (0)