some more vmx real mode emulation fixes and cleanups
PATCH 1/9 KVM: VMX: remove special CPL cache access during transition to real mode.
In reply to Gleb NatapovSince vmx_get_cpl() always returns 0 when VCPU is in real mode it is no longer needed. Also reset CPL cache to zero during transaction to protected mode since transaction may happen while CS.selectors & 3 != 0, but in reality CPL is 0.
--- arch/x86/kvm/vmx.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx.c +4 -8
@@ -2817,6 +2817,10 @@ static void enter_pmode(struct kvm_vcpu *vcpu)fix_pmode_dataseg (vcpu,VCPU_SREG_DS , &vmx->rmode.segs[VCPU_SREG_DS ]);fix_pmode_dataseg (vcpu,VCPU_SREG_FS , &vmx->rmode.segs[VCPU_SREG_FS ]);fix_pmode_dataseg (vcpu,VCPU_SREG_GS , &vmx->rmode.segs[VCPU_SREG_GS ]); + +/* CPL is always 0 when CPU enters protected mode */ +__set_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail); + vmx->cpl =0 ; }static gva_t rmode_tss_base (struct kvm *kvm)
@@ -3229,14 +3233,6 @@ static int vmx_get_cpl(struct kvm_vcpu *vcpu) && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */return 3 ; -/* -* If we enter real mode with cs.sel & 3 != 0, the normal CPL calculations -* fail; use the cache instead. -*/ -if (unlikely (vmx->emulation_required && emulate_invalid_guest_state)) { -return vmx->cpl; - } -if (!test_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail)) {__set_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail); vmx->cpl =vmx_read_guest_seg_selector (vmx,VCPU_SREG_CS ) &3 ;
Signature · 1 line
1.7.10.4
cover some more vmx real mode emulation fixes and cleanups
Gleb Natapov (9):
KVM: VMX: remove special CPL cache access during transition to real
mode.
KVM: VMX: reset CPL only on CS register write.
KVM: VMX: if unrestricted guest is enabled vcpu state is always
valid.
KVM: VMX: remove hack that disables emulation on vcpu reset/init
KVM: VMX: skip vmx->rmode.vm86_active check on cr0 write if
unrestricted guest is enabled
KVM: VMX: don't clobber segment AR of unusable segments.
KVM: VMX: rename fix_pmode_dataseg to fix_pmode_seg.
KVM: x86: fix use of uninitialized memory as segment descriptor in
emulator.
KVM: VMX: set vmx->emulation_required only when needed.
arch/x86/kvm/vmx.c | 70 +++++++++++++++++++++++++--------------------------- arch/x86/kvm/x86.c | 4 ++- 2 files changed, 37 insertions(+), 37 deletions(-)
Signature · 1 line
1.7.10.4
PATCH 2/9 KVM: VMX: reset CPL only on CS register write.
--- arch/x86/kvm/vmx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +2 -1
@@ -3269,7 +3269,8 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu,const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];vmx_segment_cache_clear (vmx); -__clear_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail); +if (seg ==VCPU_SREG_CS ) +__clear_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail);if (vmx->rmode.vm86_active && seg !=VCPU_SREG_LDTR ) { vmx->rmode.segs[seg] = *var;
Signature · 1 line
1.7.10.4
PATCH 3/9 KVM: VMX: if unrestricted guest is enabled vcpu state is always valid.
In reply to Gleb Natapov--- arch/x86/kvm/vmx.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx.c +3 -0
@@ -3488,6 +3488,9 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)*/ static bool guest_state_valid (struct kvm_vcpu *vcpu) { +if (enable_unrestricted_guest) +return true; +/* real mode guest state checks */ if (!is_protmode (vcpu)) {if (!rmode_segment_valid (vcpu,VCPU_SREG_CS ))
Signature · 1 line
1.7.10.4
PATCH 4/9 KVM: VMX: remove hack that disables emulation on vcpu reset/init
In reply to Gleb NatapovThere is no reason for it. If state is suitable for vmentry it will be detected during guest entry and no emulation will happen.
--- arch/x86/kvm/vmx.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kvm/vmx.c +0 -3
@@ -4035,9 +4035,6 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) ret =0 ; -/* HACK: Don't enable emulation on guest boot/reset */ - vmx->emulation_required =0 ; -return ret; }
Signature · 1 line
1.7.10.4
PATCH 5/9 KVM: VMX: skip vmx->rmode.vm86_active check on cr0 write if unrestricted guest is enabled
In reply to Gleb Natapovvmx->rmode.vm86_active is never true is unrestricted guest is enabled. Make it more explicit that neither enter_pmode() nor enter_rmode() is called in this case.
--- arch/x86/kvm/vmx.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx.c +6 -8
@@ -2877,9 +2877,6 @@ static void enter_rmode(struct kvm_vcpu *vcpu)unsigned long flags;struct vcpu_vmx *vmx =to_vmx (vcpu); -if (enable_unrestricted_guest) -return ; -vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_TR ],VCPU_SREG_TR );vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_ES ],VCPU_SREG_ES );vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_DS ],VCPU_SREG_DS );
@@ -3086,14 +3083,15 @@ static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)if (enable_unrestricted_guest) hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST ) |KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST ; -else +else { hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK ) |KVM_VM_CR0_ALWAYS_ON ; -if (vmx->rmode.vm86_active && (cr0 &X86_CR0_PE )) -enter_pmode (vcpu); +if (vmx->rmode.vm86_active && (cr0 &X86_CR0_PE )) +enter_pmode (vcpu); -if (!vmx->rmode.vm86_active && !(cr0 &X86_CR0_PE )) -enter_rmode (vcpu); +if (!vmx->rmode.vm86_active && !(cr0 &X86_CR0_PE )) +enter_rmode (vcpu); + }#ifdef CONFIG_X86_64 if (vcpu->arch.efer &EFER_LME ) {
Signature · 1 line
1.7.10.4
PATCH 6/9 KVM: VMX: don't clobber segment AR of unusable segments.
In reply to Gleb NatapovUsability is returned in unusable field, so not need to clobber entire AR. Callers have to know how to deal with unusable segments already since if emulate_invalid_guest_state=true AR is not zeroed.
--- arch/x86/kvm/vmx.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/kvm/vmx.c +0 -2
@@ -3196,8 +3196,6 @@ static void vmx_get_segment(struct kvm_vcpu *vcpu, var->limit =vmx_read_guest_seg_limit (vmx, seg); var->selector =vmx_read_guest_seg_selector (vmx, seg); ar =vmx_read_guest_seg_ar (vmx, seg); -if ((ar &AR_UNUSABLE_MASK ) && !emulate_invalid_guest_state) - ar =0 ; var->type = ar &15 ; var->s = (ar >>4 ) &1 ; var->dpl = (ar >>5 ) &3 ;
Signature · 1 line
1.7.10.4
PATCH 7/9 KVM: VMX: rename fix_pmode_dataseg to fix_pmode_seg.
In reply to Gleb NatapovThe function deals with code segment too.
--- arch/x86/kvm/vmx.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx.c +7 -7
@@ -2759,7 +2759,7 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } -static void fix_pmode_dataseg (struct kvm_vcpu *vcpu,int seg, +static void fix_pmode_seg (struct kvm_vcpu *vcpu,int seg,struct kvm_segment *save) {if (!emulate_invalid_guest_state) {
@@ -2811,12 +2811,12 @@ static void enter_pmode(struct kvm_vcpu *vcpu)update_exception_bitmap (vcpu); -fix_pmode_dataseg (vcpu,VCPU_SREG_CS , &vmx->rmode.segs[VCPU_SREG_CS ]); -fix_pmode_dataseg (vcpu,VCPU_SREG_SS , &vmx->rmode.segs[VCPU_SREG_SS ]); -fix_pmode_dataseg (vcpu,VCPU_SREG_ES , &vmx->rmode.segs[VCPU_SREG_ES ]); -fix_pmode_dataseg (vcpu,VCPU_SREG_DS , &vmx->rmode.segs[VCPU_SREG_DS ]); -fix_pmode_dataseg (vcpu,VCPU_SREG_FS , &vmx->rmode.segs[VCPU_SREG_FS ]); -fix_pmode_dataseg (vcpu,VCPU_SREG_GS , &vmx->rmode.segs[VCPU_SREG_GS ]); +fix_pmode_seg (vcpu,VCPU_SREG_CS , &vmx->rmode.segs[VCPU_SREG_CS ]); +fix_pmode_seg (vcpu,VCPU_SREG_SS , &vmx->rmode.segs[VCPU_SREG_SS ]); +fix_pmode_seg (vcpu,VCPU_SREG_ES , &vmx->rmode.segs[VCPU_SREG_ES ]); +fix_pmode_seg (vcpu,VCPU_SREG_DS , &vmx->rmode.segs[VCPU_SREG_DS ]); +fix_pmode_seg (vcpu,VCPU_SREG_FS , &vmx->rmode.segs[VCPU_SREG_FS ]); +fix_pmode_seg (vcpu,VCPU_SREG_GS , &vmx->rmode.segs[VCPU_SREG_GS ]);/* CPL is always 0 when CPU enters protected mode */ __set_bit (VCPU_EXREG_CPL , (ulong *)&vcpu->arch.regs_avail);
Signature · 1 line
1.7.10.4
PATCH 8/9 KVM: x86: fix use of uninitialized memory as segment descriptor in emulator.
In reply to Gleb NatapovIf VMX reports segment as unusable, zero descriptor passed by the emulator before returning. Such descriptor will be considered not present by the emulator.
--- arch/x86/kvm/x86.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +3 -1
@@ -4489,8 +4489,10 @@ static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,kvm_get_segment (emul_to_vcpu (ctxt), &var, seg); *selector = var.selector; -if (var.unusable) +if (var.unusable) { +memset (desc,0 ,sizeof (*desc));return false; + }if (var.g) var.limit >>=12 ;
Signature · 1 line
1.7.10.4
PATCH 9/9 KVM: VMX: set vmx->emulation_required only when needed.
In reply to Gleb NatapovIf emulate_invalid_guest_state=false vmx->emulation_required is never actually used, but it ends up to be always set to true since handle_invalid_guest_state(), the only place it is reset back to false, is never called. This, besides been not very clean, makes vmexit and vmentry path to check emulate_invalid_guest_state needlessly.
The patch fixes that by keeping emulation_required coherent with emulate_invalid_guest_state setting.
--- arch/x86/kvm/vmx.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx.c +12 -7
@@ -2759,6 +2759,11 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static bool emulation_required (struct kvm_vcpu *vcpu) +{ +return emulate_invalid_guest_state && !guest_state_valid (vcpu); +} +static void fix_pmode_seg (struct kvm_vcpu *vcpu,int seg,struct kvm_segment *save) {
@@ -2794,7 +2799,6 @@ static void enter_pmode(struct kvm_vcpu *vcpu)vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_SS ],VCPU_SREG_SS );vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_CS ],VCPU_SREG_CS ); - vmx->emulation_required =1 ; vmx->rmode.vm86_active =0 ;vmx_segment_cache_clear (vmx);
@@ -2885,7 +2889,6 @@ static void enter_rmode(struct kvm_vcpu *vcpu)vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_SS ],VCPU_SREG_SS );vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_CS ],VCPU_SREG_CS ); - vmx->emulation_required =1 ; vmx->rmode.vm86_active =1 ;/*
@@ -3111,6 +3114,9 @@ static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)vmcs_writel (CR0_READ_SHADOW , cr0);vmcs_writel (GUEST_CR0 , hw_cr0); vcpu->arch.cr0 = cr0; + +/* depends on vcpu->arch.cr0 to be set to a new value */ + vmx->emulation_required =emulation_required (vcpu); }static u64 construct_eptp (unsigned long root_hpa)
@@ -3298,8 +3304,7 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu,vmcs_write32 (sf->ar_bytes,vmx_segment_access_rights (var)); out: -if (!vmx->emulation_required) - vmx->emulation_required = !guest_state_valid (vcpu); + vmx->emulation_required |=emulation_required (vcpu); }static void vmx_get_cs_db_l_bits (struct kvm_vcpu *vcpu,int *db,int *l)
@@ -5027,7 +5032,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)schedule (); } - vmx->emulation_required = !guest_state_valid (vcpu); + vmx->emulation_required =emulation_required (vcpu); out:return ret; }
@@ -5970,7 +5975,7 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)u32 vectoring_info = vmx->idt_vectoring_info;/* If guest state is invalid, start emulating */ -if (vmx->emulation_required && emulate_invalid_guest_state) +if (vmx->emulation_required)return handle_invalid_guest_state (vcpu);/*
@@ -6253,7 +6258,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)/* Don't enter VMX if guest state is invalid, let the exit handler start emulation until we arrive back to a valid state */ -if (vmx->emulation_required && emulate_invalid_guest_state) +if (vmx->emulation_required)return ;if (test_bit (VCPU_REGS_RSP , (unsigned long *)&vcpu->arch.regs_dirty))
Signature · 1 line
1.7.10.4
Quoted 16 lines · Gleb Natapov
Gleb Natapov (9):
KVM: VMX: remove special CPL cache access during transition to real
mode.
KVM: VMX: reset CPL only on CS register write.
KVM: VMX: if unrestricted guest is enabled vcpu state is always
valid.
KVM: VMX: remove hack that disables emulation on vcpu reset/init
KVM: VMX: skipvmx->rmode.vm86_activecheck on cr0 write if
unrestricted guest is enabled
KVM: VMX: don't clobber segment AR of unusable segments.
KVM: VMX: renamefix_pmode_datasegtofix_pmode_seg.
KVM: x86: fix use of uninitialized memory as segment descriptor in
emulator.
KVM: VMX: setvmx->emulation_requiredonly when needed.
Quoted 6 lines
arch/x86/kvm/vmx.c | 70 +++++++++++++++++++++++++--------------------------- arch/x86/kvm/x86.c | 4 ++- 2 files changed, 37 insertions(+), 37 deletions(-)Signature · 1 line
1.7.10.4
Marcelo Tosatti
AppliedQuoted 16 lines · Gleb Natapov
Gleb Natapov (9):
KVM: VMX: remove special CPL cache access during transition to real
mode.
KVM: VMX: reset CPL only on CS register write.
KVM: VMX: if unrestricted guest is enabled vcpu state is always
valid.
KVM: VMX: remove hack that disables emulation on vcpu reset/init
KVM: VMX: skipvmx->rmode.vm86_activecheck on cr0 write if
unrestricted guest is enabled
KVM: VMX: don't clobber segment AR of unusable segments.
KVM: VMX: renamefix_pmode_datasegtofix_pmode_seg.
KVM: x86: fix use of uninitialized memory as segment descriptor in
emulator.
KVM: VMX: setvmx->emulation_requiredonly when needed.
Quoted 3 lines
arch/x86/kvm/vmx.c | 70 +++++++++++++++++++++++++--------------------------- arch/x86/kvm/x86.c | 4 ++- 2 files changed, 37 insertions(+), 37 deletions(-)
Applied, thanks.