more VMX real mode emulation fixes
cover more VMX real mode emulation fixes
This series goes on top of my previous one: "Fix emulate_invalid_guest_state=0 part 2". It does not only fixes bugs, but also does a nice cleanup of VMX real mode emulation. All real mode segment register mangling is now contained in fix_rmode_seg() function.
Gleb Natapov (5):
KVM: emulator: drop RPL check from linearize() function
KVM: emulator: implement fninit, fnstsw, fnstcw
KVM: VMX: make rmode_segment_valid() more strict.
KVM: VMX: fixing emulation of invalid guest state.
KVM: VMX: Do not fix segment register during vcpu initialization.
arch/x86/kvm/emulate.c | 133 +++++++++++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/vmx.c | 121 +++++++++++++++++++++---------------------- 2 files changed, 185 insertions(+), 69 deletions(-)
Signature · 1 line
1.7.10.4
PATCH 1/5 KVM: emulator: drop RPL check from linearize() function
According to Intel SDM Vol3 Section 5.5 "Privilege Levels" and 5.6 "Privilege Level Checking When Accessing Data Segments" RPL checking is done during loading of a segment selector, not during data access. We already do checking during segment selector loading, so drop the check during data access. Checking RPL during data access triggers #GP if after transition from real mode to protected mode RPL bits in a segment selector are set.
--- arch/x86/kvm/emulate.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/kvm/emulate.c +1 -6
@@ -665,7 +665,7 @@ static int __linearize(struct x86_emulate_ctxt *ctxt,ulong la;u32 lim;u16 sel; -unsigned cpl, rpl; +unsigned cpl; la =seg_base (ctxt, addr.seg) + addr.ea;switch (ctxt->mode) {
@@ -699,11 +699,6 @@ static int __linearize(struct x86_emulate_ctxt *ctxt, goto bad; } cpl = ctxt->ops->cpl (ctxt); -if (ctxt->mode ==X86EMUL_MODE_REAL ) - rpl =0 ; -else - rpl = sel &3 ; - cpl =max (cpl, rpl);if (!(desc.type &8 )) {/* data segment */ if (cpl > desc.dpl)
Signature · 1 line
1.7.10.4
PATCH 2/5 KVM: emulator: implement fninit, fnstsw, fnstcw
In reply to Gleb Natapov--- arch/x86/kvm/emulate.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +125 -1
@@ -115,6 +115,7 @@#define GroupDual (2<<15)/* Alternate decoding of mod == 3 */ #define Prefix (3<<15)/* Instruction varies with 66/f2/f3 prefix */ #define RMExt (4<<15)/* Opcode extension in ModRM r/m if mod == 3 */ +#define Escape (5<<15)/* Escape to coprocessor instruction */ #define Sse (1<<18)/* SSE Vector instruction */ /* Generic ModRM decode. */ #define ModRM (1<<19)
@@ -166,6 +167,7 @@ struct opcode {const struct opcode *group;const struct group_dual *gdual;const struct gprefix *gprefix; +const struct escape *esc; } u;int (*check_perm)(struct x86_emulate_ctxt *ctxt); };
@@ -182,6 +184,11 @@ struct gprefix {struct opcode pfx_f3; }; +struct escape { +struct opcode op[8 ]; +struct opcode high[64 ]; +}; +/* EFLAGS bit definitions. */ #define EFLG_ID (1<<21)#define EFLG_VIP (1<<20)
@@ -991,6 +998,53 @@ static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) ctxt->ops->put_fpu (ctxt); } +static int em_fninit (struct x86_emulate_ctxt *ctxt) +{ +if (ctxt->ops->get_cr (ctxt,0 ) & (X86_CR0_TS |X86_CR0_EM )) +return emulate_nm (ctxt); + + ctxt->ops->get_fpu (ctxt); + asmvolatile ("fninit" ); + ctxt->ops->put_fpu (ctxt); +return X86EMUL_CONTINUE ; +} + +static int em_fnstcw (struct x86_emulate_ctxt *ctxt) +{ +u16 fcw; + +if (ctxt->ops->get_cr (ctxt,0 ) & (X86_CR0_TS |X86_CR0_EM )) +return emulate_nm (ctxt); + + ctxt->ops->get_fpu (ctxt); + asmvolatile ("fnstcw %0" :"+m" (fcw)); + ctxt->ops->put_fpu (ctxt); + +/* force 2 byte destination */ + ctxt->dst.bytes =2 ; + ctxt->dst.val = fcw; + +return X86EMUL_CONTINUE ; +} + +static int em_fnstsw (struct x86_emulate_ctxt *ctxt) +{ +u16 fsw; + +if (ctxt->ops->get_cr (ctxt,0 ) & (X86_CR0_TS |X86_CR0_EM )) +return emulate_nm (ctxt); + + ctxt->ops->get_fpu (ctxt); + asmvolatile ("fnstsw %0" :"+m" (fsw)); + ctxt->ops->put_fpu (ctxt); + +/* force 2 byte destination */ + ctxt->dst.bytes =2 ; + ctxt->dst.val = fsw; + +return X86EMUL_CONTINUE ; +} +static void decode_register_operand (struct x86_emulate_ctxt *ctxt,struct operand *op) {
@@ -3590,6 +3644,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)#define EXT (_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }#define G (_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }#define GD (_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } +#define E (_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }#define I (_f, _e) { .flags = (_f), .u.execute = (_e) }#define II (_f, _e, _i) \ { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
@@ -3725,6 +3780,69 @@ static const struct gprefix pfx_vmovntpx = { I(0, em_mov), N, N, N, }; +static const struct escape escape_d9 = { { +N ,N ,N ,N ,N ,N ,N ,I (DstMem, em_fnstcw), +}, { +/* 0xC0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xC8 - 0xCF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD8 - 0xDF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xE0 - 0xE7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xE8 - 0xEF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF0 - 0xF7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF8 - 0xFF */ +N ,N ,N ,N ,N ,N ,N ,N , +} }; + +static const struct escape escape_db = { { +N ,N ,N ,N ,N ,N ,N ,N , +}, { +/* 0xC0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xC8 - 0xCF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD8 - 0xDF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xE0 - 0xE7 */ +N ,N ,N ,I (ImplicitOps, em_fninit),N ,N ,N ,N , +/* 0xE8 - 0xEF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF0 - 0xF7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF8 - 0xFF */ +N ,N ,N ,N ,N ,N ,N ,N , +} }; + +static const struct escape escape_dd = { { +N ,N ,N ,N ,N ,N ,N ,I (DstMem, em_fnstsw), +}, { +/* 0xC0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xC8 - 0xCF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD0 - 0xC7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xD8 - 0xDF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xE0 - 0xE7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xE8 - 0xEF */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF0 - 0xF7 */ +N ,N ,N ,N ,N ,N ,N ,N , +/* 0xF8 - 0xFF */ +N ,N ,N ,N ,N ,N ,N ,N , +} }; +static const struct opcode opcode_table[256 ] = {/* 0x00 - 0x07 */ I6ALU (Lock, em_add),
@@ -3821,7 +3939,7 @@ static const struct opcode opcode_table[256] = {D2bv (DstMem | SrcOne | ModRM),D2bv (DstMem | ModRM),N ,I (DstAcc | SrcImmByte | No64, em_aad),N ,N ,/* 0xD8 - 0xDF */ -N ,N ,N ,N ,N ,N ,N ,N , +N ,E (0 , &escape_d9),N ,E (0 , &escape_db),N ,E (0 , &escape_dd),N ,N ,/* 0xE0 - 0xE7 */ X3 (I (SrcImmByte, em_loop)), I(SrcImmByte, em_jcxz),
@@ -4246,6 +4364,12 @@ done_prefixes:case 0xf3 : opcode = opcode.u.gprefix->pfx_f3;break ; }break ; +case Escape: +if (ctxt->modrm >0xbf ) + opcode = opcode.u.esc->high[ctxt->modrm -0xc0 ]; +else + opcode = opcode.u.esc->op[(ctxt->modrm >>3 ) &7 ]; +break ;default :return EMULATION_FAILED ; }
Signature · 1 line
1.7.10.4
PATCH 3/5 KVM: VMX: make rmode_segment_valid() more strict.
In reply to Gleb NatapovCurrently it allows entering vm86 mode if segment limit is greater than 0xffff and db bit is set. Both of those can cause incorrect execution of instruction by cpu since in vm86 mode limit will be set to 0xffff and db will be forced to 0.
--- arch/x86/kvm/vmx.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/kvm/vmx.c +1 -3
@@ -3341,15 +3341,13 @@ static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)vmx_get_segment (vcpu, &var, seg); var.dpl =0x3 ; - var.g =0 ; - var.db =0 ;if (seg ==VCPU_SREG_CS ) var.type =0x3 ; ar =vmx_segment_access_rights (&var);if (var.base != (var.selector <<4 ))return false; -if (var.limit <0xffff ) +if (var.limit !=0xffff )return false;if (ar !=0xf3 )return false;
Signature · 1 line
1.7.10.4
PATCH 4/5 KVM: VMX: fixing emulation of invalid guest state.
In reply to Gleb NatapovCurrently when emulation of invalid guest state is enable (emulate_invalid_guest_state=1) segment registers are still fixed for entry to vm86 mode some times. Segment register fixing is avoided in enter_rmode(), but vmx_set_segment() still does it unconditionally. The patch fixes it.
--- arch/x86/kvm/vmx.c | 99 ++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 46 deletions(-)
arch/x86/kvm/vmx.c +53 -46
@@ -624,6 +624,8 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);static void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg); +static bool guest_state_valid (struct kvm_vcpu *vcpu); +static u32 vmx_segment_access_rights (struct kvm_segment *var); static DEFINE_PER_CPU(struct vmcs *, vmxarea); static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
@@ -2758,12 +2760,14 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } -static void fix_pmode_dataseg (struct kvm_vcpu *vcpu,int seg,struct kvm_segment *save) +static void fix_pmode_dataseg (struct kvm_vcpu *vcpu,int seg, +struct kvm_segment *save) {const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];struct kvm_segment tmp = *save; -if (!(vmcs_readl (sf->base) == tmp.base && tmp.s)) { +if (!emulate_invalid_guest_state && + (vmcs_readl (sf->base) != tmp.base || tmp.s ==0 )) { tmp.base =vmcs_readl (sf->base); tmp.selector =vmcs_read16 (sf->selector); tmp.dpl = tmp.selector &SELECTOR_RPL_MASK ;
@@ -2777,6 +2781,13 @@ static void enter_pmode(struct kvm_vcpu *vcpu)unsigned long flags;struct vcpu_vmx *vmx =to_vmx (vcpu); +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 ); +vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_FS ],VCPU_SREG_FS ); +vmx_get_segment (vcpu, &vmx->rmode.segs[VCPU_SREG_GS ],VCPU_SREG_GS ); +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 ;
@@ -2794,22 +2805,12 @@ static void enter_pmode(struct kvm_vcpu *vcpu)update_exception_bitmap (vcpu); -if (emulate_invalid_guest_state) -return ; - +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 ]); - -vmx_segment_cache_clear (vmx); - -vmcs_write16 (GUEST_SS_SELECTOR ,0 ); -vmcs_write32 (GUEST_SS_AR_BYTES ,0x93 ); - -vmcs_write16 (GUEST_CS_SELECTOR , -vmcs_read16 (GUEST_CS_SELECTOR ) & ~SELECTOR_RPL_MASK ); -vmcs_write32 (GUEST_CS_AR_BYTES ,0x9b ); }static gva_t rmode_tss_base (struct kvm *kvm)
@@ -2831,22 +2832,40 @@ static gva_t rmode_tss_base(struct kvm *kvm)static void fix_rmode_seg (int seg,struct kvm_segment *save) {const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; +struct kvm_segment var = *save; -vmcs_write16 (sf->selector, save->base >>4 ); -vmcs_write32 (sf->base, save->base &0xffff0 ); -vmcs_write32 (sf->limit,0xffff ); -vmcs_write32 (sf->ar_bytes,0xf3 ); -if (save->base &0xf ) -printk_once (KERN_WARNING "kvm: segment base is not paragraph" -" aligned when entering protected mode (seg=%d)" , - seg); + var.dpl =0x3 ; +if (seg ==VCPU_SREG_CS ) + var.type =0x3 ; + +if (!emulate_invalid_guest_state) { + var.selector = var.base >>4 ; + var.base = var.base &0xffff0 ; + var.limit =0xffff ; + var.g =0 ; + var.db =0 ; + var.present =1 ; + var.s =1 ; + var.l =0 ; + var.unusable =0 ; + var.type =0x3 ; + var.avl =0 ; +if (save->base &0xf ) +printk_once (KERN_WARNING "kvm: segment base is not " +"paragraph aligned when entering " +"protected mode (seg=%d)" , seg); + } + +vmcs_write16 (sf->selector, var.selector); +vmcs_write32 (sf->base, var.base); +vmcs_write32 (sf->limit, var.limit); +vmcs_write32 (sf->ar_bytes,vmx_segment_access_rights (&var)); }static void enter_rmode (struct kvm_vcpu *vcpu) {unsigned long flags;struct vcpu_vmx *vmx =to_vmx (vcpu); -struct kvm_segment var;if (enable_unrestricted_guest)return ;
@@ -2862,7 +2881,6 @@ static void enter_rmode(struct kvm_vcpu *vcpu) vmx->emulation_required =1 ; vmx->rmode.vm86_active =1 ; -/* * Very old userspace does not call KVM_SET_TSS_ADDR before entering * vcpu. Call it here with phys address pointing 16M below 4G.
@@ -2890,28 +2908,13 @@ static void enter_rmode(struct kvm_vcpu *vcpu)vmcs_writel (GUEST_CR4 ,vmcs_readl (GUEST_CR4 ) |X86_CR4_VME );update_exception_bitmap (vcpu); -if (emulate_invalid_guest_state) - goto continue_rmode; - -vmx_get_segment (vcpu, &var,VCPU_SREG_SS ); -vmx_set_segment (vcpu, &var,VCPU_SREG_SS ); - -vmx_get_segment (vcpu, &var,VCPU_SREG_CS ); -vmx_set_segment (vcpu, &var,VCPU_SREG_CS ); +fix_rmode_seg (VCPU_SREG_SS , &vmx->rmode.segs[VCPU_SREG_SS ]); +fix_rmode_seg (VCPU_SREG_CS , &vmx->rmode.segs[VCPU_SREG_CS ]); +fix_rmode_seg (VCPU_SREG_ES , &vmx->rmode.segs[VCPU_SREG_ES ]); +fix_rmode_seg (VCPU_SREG_DS , &vmx->rmode.segs[VCPU_SREG_DS ]); +fix_rmode_seg (VCPU_SREG_GS , &vmx->rmode.segs[VCPU_SREG_GS ]); +fix_rmode_seg (VCPU_SREG_FS , &vmx->rmode.segs[VCPU_SREG_FS ]); -vmx_get_segment (vcpu, &var,VCPU_SREG_ES ); -vmx_set_segment (vcpu, &var,VCPU_SREG_ES ); - -vmx_get_segment (vcpu, &var,VCPU_SREG_DS ); -vmx_set_segment (vcpu, &var,VCPU_SREG_DS ); - -vmx_get_segment (vcpu, &var,VCPU_SREG_GS ); -vmx_set_segment (vcpu, &var,VCPU_SREG_GS ); - -vmx_get_segment (vcpu, &var,VCPU_SREG_FS ); -vmx_set_segment (vcpu, &var,VCPU_SREG_FS ); - -continue_rmode:kvm_mmu_reset_context (vcpu); }
@@ -3278,7 +3281,7 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu,vmcs_write16 (sf->selector, var->selector); else if (var->s) fix_rmode_seg(seg, &vmx->rmode.segs[seg]); -return ; + goto out; }vmcs_writel (sf->base, var->base);
@@ -3300,6 +3303,10 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu, var->type |=0x1 ;/* Accessed */ vmcs_write32 (sf->ar_bytes,vmx_segment_access_rights (var)); + +out: +if (!vmx->emulation_required) + vmx->emulation_required = !guest_state_valid (vcpu); }static void vmx_get_cs_db_l_bits (struct kvm_vcpu *vcpu,int *db,int *l)
Signature · 1 line
1.7.10.4
PATCH 5/5 KVM: VMX: Do not fix segment register during vcpu initialization.
In reply to Gleb NatapovSegment registers will be fixed according to current emulation policy during switching to real mode for the first time.
--- arch/x86/kvm/vmx.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx.c +5 -13
@@ -3614,12 +3614,9 @@ static void seg_setup(int seg)vmcs_write16 (sf->selector,0 );vmcs_writel (sf->base,0 );vmcs_write32 (sf->limit,0xffff ); -if (enable_unrestricted_guest) { - ar =0x93 ; -if (seg ==VCPU_SREG_CS ) - ar |=0x08 ;/* code segment */ - }else - ar =0xf3 ; + ar =0x93 ; +if (seg ==VCPU_SREG_CS ) + ar |=0x08 ;/* code segment */ vmcs_write32 (sf->ar_bytes, ar); }
@@ -3960,14 +3957,9 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)vmx_segment_cache_clear (vmx);seg_setup (VCPU_SREG_CS ); -/* -* GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode -* insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. -*/ -if (kvm_vcpu_is_bsp (&vmx->vcpu)) { +if (kvm_vcpu_is_bsp (&vmx->vcpu))vmcs_write16 (GUEST_CS_SELECTOR ,0xf000 ); -vmcs_writel (GUEST_CS_BASE ,0x000f0000 ); - }else { +else {vmcs_write16 (GUEST_CS_SELECTOR , vmx->vcpu.arch.sipi_vector <<8 );vmcs_writel (GUEST_CS_BASE , vmx->vcpu.arch.sipi_vector <<12 ); }
Signature · 1 line
1.7.10.4