Skip to content

Commit fb09f9d

Browse files
binbinwu1lijinxia
authored andcommitted
hv: update vcpu mode when vmexit
Signed-off-by: Binbin Wu <binbin.wu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Acked-by: Xu, Anthony <anthony.xu@intel.com>
1 parent 5c7f120 commit fb09f9d

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

hypervisor/arch/x86/guest/instr_emul_wrapper.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,6 @@ static int32_t get_vmcs_field(int ident)
269269
}
270270
}
271271

272-
static enum vm_cpu_mode get_vmx_cpu_mode(void)
273-
{
274-
uint32_t csar;
275-
276-
if (exec_vmread(VMX_GUEST_IA32_EFER_FULL) & EFER_LMA) {
277-
csar = exec_vmread(VMX_GUEST_CS_ATTR);
278-
if (csar & 0x2000)
279-
return CPU_MODE_64BIT; /* CS.L = 1 */
280-
else
281-
return CPU_MODE_COMPATIBILITY;
282-
} else if (exec_vmread(VMX_GUEST_CR0) & CR0_PE) {
283-
return CPU_MODE_PROTECTED;
284-
} else {
285-
return CPU_MODE_REAL;
286-
}
287-
}
288-
289272
static void get_guest_paging_info(struct vcpu *vcpu, struct emul_cnx *emul_cnx)
290273
{
291274
uint32_t cpl, csar;
@@ -297,7 +280,7 @@ static void get_guest_paging_info(struct vcpu *vcpu, struct emul_cnx *emul_cnx)
297280
emul_cnx->paging.cr3 =
298281
vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context].cr3;
299282
emul_cnx->paging.cpl = cpl;
300-
emul_cnx->paging.cpu_mode = get_vmx_cpu_mode();
283+
emul_cnx->paging.cpu_mode = get_vcpu_mode(vcpu);
301284
emul_cnx->paging.paging_mode = PAGING_MODE_FLAT;/*maybe change later*/
302285
}
303286

@@ -358,7 +341,7 @@ uint8_t decode_instruction(struct vcpu *vcpu)
358341

359342
get_guest_paging_info(vcpu, emul_cnx);
360343
csar = exec_vmread(VMX_GUEST_CS_ATTR);
361-
cpu_mode = get_vmx_cpu_mode();
344+
cpu_mode = get_vcpu_mode(vcpu);
362345

363346
retval = __decode_instruction(vcpu, guest_rip_gva,
364347
cpu_mode, SEG_DESC_DEF32(csar), &emul_cnx->vie);

hypervisor/arch/x86/guest/vcpu.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ int create_vcpu(int cpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
106106
return 0;
107107
}
108108

109+
static void set_vcpu_mode(struct vcpu *vcpu, uint32_t cs_attr)
110+
{
111+
struct run_context *cur_context =
112+
&vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context];
113+
114+
if (cur_context->ia32_efer & MSR_IA32_EFER_LMA_BIT) {
115+
if (cs_attr & 0x2000) /* CS.L = 1 */
116+
vcpu->arch_vcpu.cpu_mode = CPU_MODE_64BIT;
117+
else
118+
vcpu->arch_vcpu.cpu_mode = CPU_MODE_COMPATIBILITY;
119+
} else if (cur_context->cr0 & CR0_PE) {
120+
vcpu->arch_vcpu.cpu_mode = CPU_MODE_PROTECTED;
121+
} else {
122+
vcpu->arch_vcpu.cpu_mode = CPU_MODE_REAL;
123+
}
124+
}
125+
109126
int start_vcpu(struct vcpu *vcpu)
110127
{
111128
uint32_t instlen;
@@ -160,6 +177,7 @@ int start_vcpu(struct vcpu *vcpu)
160177

161178
/* Save guest IA32_EFER register */
162179
cur_context->ia32_efer = exec_vmread64(VMX_GUEST_IA32_EFER_FULL);
180+
set_vcpu_mode(vcpu, exec_vmread(VMX_GUEST_CS_ATTR));
163181

164182
/* Obtain current VCPU instruction pointer and length */
165183
cur_context->rip = exec_vmread(VMX_GUEST_RIP);

0 commit comments

Comments
 (0)