Skip to content

Commit e84d4de

Browse files
JasonChenCJlijinxia
authored andcommitted
trusty: init & switch world fix
- when init, cr0 & cr4 should read from VMCS - when world switch, cr0/cr4 read shadow should also be save/restore v2: - use context->vmx_cr0/cr4 to save/restore VMX_GUEST_CR0/CR4 - use context->cr0/cr4 to save/restore VMX_CR0/CR4_READ_SHADOW Signed-off-by: Jason Chen CJ <jason.cj.chen@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 75c1573 commit e84d4de

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

hypervisor/arch/x86/trusty.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ static void save_world_ctx(struct run_context *context)
177177
{
178178
/* VMCS GUEST field */
179179
/* TSC_OFFSET, CR3, RIP, RSP, RFLAGS already saved on VMEXIT */
180-
context->cr0 = exec_vmread(VMX_GUEST_CR0);
181-
context->cr4 = exec_vmread(VMX_GUEST_CR4);
180+
context->cr0 = exec_vmread(VMX_CR0_READ_SHADOW);
181+
context->cr4 = exec_vmread(VMX_CR4_READ_SHADOW);
182+
context->vmx_cr0 = exec_vmread(VMX_GUEST_CR0);
183+
context->vmx_cr4 = exec_vmread(VMX_GUEST_CR4);
182184
context->dr7 = exec_vmread(VMX_GUEST_DR7);
183185
context->ia32_debugctl = exec_vmread64(VMX_GUEST_IA32_DEBUGCTL_FULL);
184186
context->ia32_pat = exec_vmread64(VMX_GUEST_IA32_PAT_FULL);
@@ -217,9 +219,11 @@ static void load_world_ctx(struct run_context *context)
217219
exec_vmwrite64(VMX_TSC_OFFSET_FULL, context->tsc_offset);
218220

219221
/* VMCS GUEST field */
220-
exec_vmwrite(VMX_GUEST_CR0, context->cr0);
222+
exec_vmwrite(VMX_CR0_READ_SHADOW, context->cr0);
221223
exec_vmwrite(VMX_GUEST_CR3, context->cr3);
222-
exec_vmwrite(VMX_GUEST_CR4, context->cr4);
224+
exec_vmwrite(VMX_CR4_READ_SHADOW, context->cr4);
225+
exec_vmwrite(VMX_GUEST_CR0, context->vmx_cr0);
226+
exec_vmwrite(VMX_GUEST_CR4, context->vmx_cr4);
223227
exec_vmwrite(VMX_GUEST_RIP, context->rip);
224228
exec_vmwrite(VMX_GUEST_RSP, context->rsp);
225229
exec_vmwrite(VMX_GUEST_RFLAGS, context->rflags);
@@ -358,9 +362,17 @@ static bool init_secure_world_env(struct vcpu *vcpu,
358362
vcpu->arch_vcpu.contexts[SECURE_WORLD].tsc_offset = 0;
359363

360364
vcpu->arch_vcpu.contexts[SECURE_WORLD].cr0 =
361-
vcpu->arch_vcpu.contexts[NORMAL_WORLD].cr0;
365+
vcpu->arch_vcpu.contexts[NORMAL_WORLD].cr0 =
366+
exec_vmread(VMX_CR0_READ_SHADOW);
362367
vcpu->arch_vcpu.contexts[SECURE_WORLD].cr4 =
363-
vcpu->arch_vcpu.contexts[NORMAL_WORLD].cr4;
368+
vcpu->arch_vcpu.contexts[NORMAL_WORLD].cr4 =
369+
exec_vmread(VMX_CR4_READ_SHADOW);
370+
vcpu->arch_vcpu.contexts[SECURE_WORLD].vmx_cr0 =
371+
vcpu->arch_vcpu.contexts[NORMAL_WORLD].vmx_cr0 =
372+
exec_vmread(VMX_GUEST_CR0);
373+
vcpu->arch_vcpu.contexts[SECURE_WORLD].vmx_cr4 =
374+
vcpu->arch_vcpu.contexts[NORMAL_WORLD].vmx_cr4 =
375+
exec_vmread(VMX_GUEST_CR4);
364376

365377
exec_vmwrite(VMX_GUEST_RSP,
366378
TRUSTY_EPT_REBASE_GPA + size);

hypervisor/common/trusty_hypercall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <hypervisor.h>
88
#include <hypercall.h>
99

10+
/* this hcall is only come from trusty enabled vcpu itself, and cannot be
11+
* called from other vcpus
12+
*/
1013
int64_t hcall_world_switch(struct vcpu *vcpu)
1114
{
1215
int next_world_id = !(vcpu->arch_vcpu.cur_context);
@@ -31,6 +34,9 @@ int64_t hcall_world_switch(struct vcpu *vcpu)
3134
return 0;
3235
}
3336

37+
/* this hcall is only come from trusty enabled vcpu itself, and cannot be
38+
* called from other vcpus
39+
*/
3440
int64_t hcall_initialize_trusty(struct vcpu *vcpu, uint64_t param)
3541
{
3642
if (!vcpu->vm->sworld_control.sworld_enabled) {

hypervisor/include/arch/x86/guest/vcpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ struct run_context {
138138
uint64_t ia32_sysenter_eip;
139139
uint64_t ia32_debugctl;
140140

141+
uint64_t vmx_cr0;
142+
uint64_t vmx_cr4;
143+
141144
/* segment registers */
142145
struct segment cs;
143146
struct segment ss;

0 commit comments

Comments
 (0)