Fix task switches into/out of VM86
v2 cover Fix task switches into/out of VM86
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Kevin Wolf (3):
KVM: x86 emulator: Fix task switch privilege checks
KVM: x86 emulator: VM86 segments must have DPL 3
KVM: x86 emulator: Allow PM/VM86 switch during task switch
arch/x86/include/asm/kvm_emulate.h | 3 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/emulate.c | 79 ++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm.c | 5 ++- arch/x86/kvm/vmx.c | 8 ++- arch/x86/kvm/x86.c | 12 ++++- 6 files changed, 94 insertions(+), 17 deletions(-)
Signature · 1 line
1.7.6.5
PATCH v2 1/3 KVM: x86 emulator: Fix task switch privilege checks
Currently, all task switches check privileges against the DPL of the TSS. This is only correct for jmp/call to a TSS. If a task gate is used, the DPL of this take gate is used for the check instead. Exceptions, external interrupts and iret shouldn't perform any check.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/emulate.c | 51 +++++++++++++++++++++++++++++++----- arch/x86/kvm/svm.c | 5 +++- arch/x86/kvm/vmx.c | 8 +++-- arch/x86/kvm/x86.c | 6 ++-- 6 files changed, 59 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -372,7 +372,7 @@ bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);#define EMULATION_INTERCEPTED 2int x86_emulate_insn (struct x86_emulate_ctxt *ctxt);int emulator_task_switch (struct x86_emulate_ctxt *ctxt, -u16 tss_selector,int reason, +u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code);int emulate_int_real (struct x86_emulate_ctxt *ctxt,int irq);#endif /* _ASM_X86_KVM_X86_EMULATE_H */
arch/x86/include/asm/kvm_host.h +2 -2
@@ -741,8 +741,8 @@ int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);void kvm_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);int kvm_load_segment_descriptor (struct kvm_vcpu *vcpu,u16 selector,int seg); -int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int reason, -bool has_error_code,u32 error_code); +int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index, +int reason,bool has_error_code,u32 error_code);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3);
arch/x86/kvm/emulate.c +44 -7
89 lines not on this page · raw message
arch/x86/kvm/svm.c +4 -1
@@ -2730,7 +2730,10 @@ static int task_switch_interception(struct vcpu_svm *svm) (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))skip_emulated_instruction (&svm->vcpu); -if (kvm_task_switch (&svm->vcpu, tss_selector, reason, +if (int_type !=SVM_EXITINTINFO_TYPE_SOFT ) + int_vec =-1 ; + +if (kvm_task_switch (&svm->vcpu, tss_selector, int_vec, reason, has_error_code, error_code) ==EMULATE_FAIL ) { svm->vcpu.run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; svm->vcpu.run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ;
arch/x86/kvm/vmx.c +5 -3
@@ -4672,9 +4672,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)bool has_error_code = false;u32 error_code =0 ;u16 tss_selector; -int reason, type, idt_v; +int reason, type, idt_v, idt_index; idt_v = (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ); + idt_index = (vmx->idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ); type = (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ); exit_qualification =vmcs_readl (EXIT_QUALIFICATION );
@@ -4712,8 +4713,9 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) type != INTR_TYPE_NMI_INTR))skip_emulated_instruction (vcpu); -if (kvm_task_switch (vcpu, tss_selector, reason, - has_error_code, error_code) ==EMULATE_FAIL ) { +if (kvm_task_switch (vcpu, tss_selector, + type ==INTR_TYPE_SOFT_INTR ? idt_index :-1 , reason, + has_error_code, error_code) ==EMULATE_FAIL ) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; vcpu->run->internal.ndata =0 ;
arch/x86/kvm/x86.c +3 -3
@@ -5541,15 +5541,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,return 0 ; } -int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int reason, -bool has_error_code,u32 error_code) +int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index, +int reason,bool has_error_code,u32 error_code) {struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;int ret;init_emulate_ctxt (vcpu); - ret =emulator_task_switch (ctxt, tss_selector, reason, + ret =emulator_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code);if (ret)
Signature · 1 line
1.7.6.5
Re: [PATCH v2 1/3] KVM: x86 emulator: Fix task switch privilege checks
Quoted 29 lines · Kevin Wolf
Currently, all task switches check privileges against the DPL of the TSS. This is only correct for jmp/call to a TSS. If a task gate is used, the DPL of this take gate is used for the check instead. Exceptions, external interrupts and iret shouldn't perform any check.
arch/x86/kvm/emulate.c +15
-4@@ -2372,12 +2389,32 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,/* FIXME: check that next_tss_desc is tss */ -if (reason !=TASK_SWITCH_IRET ) { -if ((tss_selector &3 ) > next_tss_desc.dpl || - ops->cpl (ctxt) > next_tss_desc.dpl) -return emulate_gp (ctxt,0 ); +/* +* Check privileges. The three cases are task switch caused by... +* +* 1. Software interrupt: Check against DPL of the task gate +* 2. Exception/IRQ/iret: No check is performed +* 3. jmp/call: Check agains DPL of the TSS +*/ + dpl =-1 ; +if (reason ==TASK_SWITCH_GATE ) { +if (idt_index !=-1 ) { +struct kvm_desc_struct task_gate_desc; + + ret =read_interrupt_descriptor (ctxt, idt_index, + &task_gate_desc); +if (ret !=X86EMUL_CONTINUE )
arch/x86/kvm/emulate.c static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, · line 2416
Quoted 10 lines
arch/x86/kvm/emulate.c +8
-0+return ret; + + dpl = task_gate_desc.dpl; + } + }else if (reason !=TASK_SWITCH_IRET ) { + dpl = next_tss_desc.dpl; } +if (dpl !=-1 && ((tss_selector &3 ) > dpl || ops->cpl (ctxt) > dpl)) +return emulate_gp (ctxt,0 );
Should be #GP(selector), no?
The TASK-GATE: branch of the CALL instruction documentation lists many other conditions which we don't check, so I'll accept this, otherwise we'll never make any progress.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 1/3] KVM: x86 emulator: Fix task switch privilege checks
Am 30.01.2012 11:39, schrieb Avi Kivity:
Quoted 38 lines · Kevin Wolf
Quoted 37 lines
Currently, all task switches check privileges against the DPL of the TSS. This is only correct for jmp/call to a TSS. If a task gate is used, the DPL of this take gate is used for the check instead. Exceptions, external interrupts and iret shouldn't perform any check.
arch/x86/kvm/emulate.c +21
-4@@ -2372,12 +2389,32 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,/* FIXME: check that next_tss_desc is tss */ -if (reason !=TASK_SWITCH_IRET ) { -if ((tss_selector &3 ) > next_tss_desc.dpl || - ops->cpl (ctxt) > next_tss_desc.dpl) -return emulate_gp (ctxt,0 ); +/* +* Check privileges. The three cases are task switch caused by... +* +* 1. Software interrupt: Check against DPL of the task gate +* 2. Exception/IRQ/iret: No check is performed +* 3. jmp/call: Check agains DPL of the TSS +*/ + dpl =-1 ; +if (reason ==TASK_SWITCH_GATE ) { +if (idt_index !=-1 ) { +struct kvm_desc_struct task_gate_desc; + + ret =read_interrupt_descriptor (ctxt, idt_index, + &task_gate_desc); +if (ret !=X86EMUL_CONTINUE ) +return ret; + + dpl = task_gate_desc.dpl; + } + }else if (reason !=TASK_SWITCH_IRET ) { + dpl = next_tss_desc.dpl; }
Quoted 4 lines
arch/x86/kvm/emulate.c static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, · line 2416
Quoted 2 lines
arch/x86/kvm/emulate.c +2
-0+if (dpl !=-1 && ((tss_selector &3 ) > dpl || ops->cpl (ctxt) > dpl)) +return emulate_gp (ctxt,0 );Should be #GP(selector), no?
This line is just moved code, but without looking it up I would expect the selector, yes. Would be an independent bug. I can add a patch to the series if you like.
Quoted 3 lines
The TASK-GATE: branch of the CALL instruction documentation lists many other conditions which we don't check, so I'll accept this, otherwise we'll never make any progress.
Hm, I didn't really look at call, just iret and exceptions.
Seems there are many test cases left to write.
Kevin
PATCH v2 2/3 KVM: x86 emulator: VM86 segments must have DPL 3
In reply to Kevin WolfSetting the segment DPL to 0 for at least the VM86 code segment makes the VM entry fail on VMX.
--- arch/x86/kvm/emulate.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +2 -0
@@ -1243,6 +1243,8 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, seg_desc.type =3 ; seg_desc.p =1 ; seg_desc.s =1 ; +if (ctxt->mode ==X86EMUL_MODE_VM86 ) + seg_desc.dpl =3 ; goto load; }
Signature · 1 line
1.7.6.5
PATCH v2 3/3 KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Kevin WolfTask switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 33 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -176,6 +176,7 @@ struct x86_emulate_ops {void (*set_idt)(struct x86_emulate_ctxt *ctxt,struct desc_ptr *dt);ulong (*get_cr)(struct x86_emulate_ctxt *ctxt,int cr);int (*set_cr)(struct x86_emulate_ctxt *ctxt,int cr,ulong val); +void (*set_rflags)(struct x86_emulate_ctxt *ctxt,ulong val);int (*cpl)(struct x86_emulate_ctxt *ctxt);int (*get_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong *dest);int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);
arch/x86/kvm/emulate.c +26 -0
@@ -552,6 +552,14 @@ static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,struct desc_struct desc; ctxt->ops->get_segment (ctxt, &dummy, &desc, &base3, seg); + +if (ctxt->mode ==X86EMUL_MODE_REAL ) + desc.dpl =0 ; +else if (ctxt->mode ==X86EMUL_MODE_VM86 ) + desc.dpl =3 ; +else + desc.dpl = selector &0x3 ; + ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }
@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ; +else + ctxt->mode =X86EMUL_MODE_PROT32 ; + + ctxt->ops->set_rflags (ctxt, ctxt->eflags); + +/* General purpose registers */ ctxt->regs[VCPU_REGS_RAX ] = tss->eax; ctxt->regs[VCPU_REGS_RCX ] = tss->ecx; ctxt->regs[VCPU_REGS_RDX ] = tss->edx;
arch/x86/kvm/x86.c +6 -0
@@ -4040,6 +4040,11 @@ static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)return res; } +static void emulator_set_rflags (struct x86_emulate_ctxt *ctxt,ulong val) +{ +kvm_set_rflags (emul_to_vcpu (ctxt), val); +} +static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));
@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,
Signature · 1 line
1.7.6.5
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 9 lines · Kevin Wolf
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
svm has vmcb_save_area::cpl; it's independent of CS.RPL.
Quoted 25 lines
Signed-off-by Kevin Wolf <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 33 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_emulate.h +1
-0@@ -176,6 +176,7 @@ struct x86_emulate_ops {void (*set_idt)(struct x86_emulate_ctxt *ctxt,struct desc_ptr *dt);ulong (*get_cr)(struct x86_emulate_ctxt *ctxt,int cr);int (*set_cr)(struct x86_emulate_ctxt *ctxt,int cr,ulong val); +void (*set_rflags)(struct x86_emulate_ctxt *ctxt,ulong val);int (*cpl)(struct x86_emulate_ctxt *ctxt);int (*get_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong *dest);int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);arch/x86/kvm/emulate.c +0
-0
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 557
Quoted 6 lines
arch/x86/kvm/emulate.c +3
-0@@ -552,6 +552,14 @@ static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,struct desc_struct desc; ctxt->ops->get_segment (ctxt, &dummy, &desc, &base3, seg); + +if (ctxt->mode ==X86EMUL_MODE_REAL ) + desc.dpl =0 ;
Can't happen.
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 561
Quoted 4 lines
arch/x86/kvm/emulate.c +4
-0+else if (ctxt->mode ==X86EMUL_MODE_VM86 ) + desc.dpl =3 ; +else + desc.dpl = selector &0x3 ;
I don't think this is right. The SDM explicitly says that just the selectors are loaded, yet you're modifying the DPL here too. Unless there's an abort, we'll be loading the descriptors later anyway (with their DPL). If there's an abort, I think we should continue with the mismatched DPL.
Quoted 13 lines
arch/x86/kvm/emulate.c +6
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly.
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 10 lines
arch/x86/kvm/emulate.c +10
-0+* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ; +else + ctxt->mode =X86EMUL_MODE_PROT32 ; +
Shouldn't this be done after the set_segment_selector() block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.
Quoted 23 lines
arch/x86/kvm/emulate.c +3
-0+ ctxt->ops->set_rflags (ctxt, ctxt->eflags); + +/* General purpose registers */ ctxt->regs[VCPU_REGS_RAX ] = tss->eax; ctxt->regs[VCPU_REGS_RCX ] = tss->ecx; ctxt->regs[VCPU_REGS_RDX ] = tss->edx;arch/x86/kvm/x86.c +5
-0@@ -4040,6 +4040,11 @@ static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)return res; } +static void emulator_set_rflags (struct x86_emulate_ctxt *ctxt,ulong val) +{ +kvm_set_rflags (emul_to_vcpu (ctxt), val); +} +static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,
It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 20 lines · Avi Kivity
Quoted 20 lines
arch/x86/kvm/emulate.c +13
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;
Quoted 9 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.
No, spec says:
Any errors associated with this loading and qualification occur in the context of the new task.
--
Gleb.
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 30 lines · Gleb Natapov
Quoted 20 lines · Avi Kivity
Quoted 20 lines
arch/x86/kvm/emulate.c +13
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;Quoted 9 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.
Quoted 3 lines
No, spec says:
Any errors associated with this loading and qualification occur in the context of the new task.
There aren't any possible errors until that point. But we don't want set_segment_selector() to use the new mode, it just changes the segment selectors, nothing else.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 31 lines · Avi Kivity
Quoted 30 lines · Gleb Natapov
Quoted 29 lines · Avi Kivity
Quoted 13 lines
arch/x86/kvm/emulate.c +6
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 10 lines
arch/x86/kvm/emulate.c +10
-0+* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ; +else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.
Quoted 8 lines
Quoted 3 lines
No, spec says:
Any errors associated with this loading and qualification occur in the context of the new task.There aren't any possible errors until that point. But we don't want
set_segment_selector()to use the new mode, it just changes the segment selectors, nothing else.
I agree with that set_segment_selector() part, but load_segment_descriptor() happens on a new task.
--
Gleb.
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 36 lines · Gleb Natapov
Quoted 35 lines · Avi Kivity
Quoted 33 lines · Gleb Natapov
Quoted 20 lines · Avi Kivity
Quoted 20 lines
arch/x86/kvm/emulate.c +13
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;Quoted 9 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.No, spec says:
Any errors associated with this loading and qualification occur in the context of the new task.
Quoted 7 lines
Quoted 4 lines
There aren't any possible errors until that point. But we don't want
set_segment_selector()to use the new mode, it just changes the segment selectors, nothing else.I agree with that
set_segment_selector()part, butload_segment_descriptor()happens on a new task.
Yes. What I think is that the mode change should happen on the boundary, after the set_segment_selector() block and before the load_segment_descriptor() block.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Avi KivityAm 30.01.2012 11:24, schrieb Avi Kivity:
Quoted 6 lines · Kevin Wolf
Quoted 5 lines
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
Quoted 6 lines
Quoted 4 lines
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
svm has
vmcb_save_area::cpl; it's independent of CS.RPL.
Right, but cpl in the VMCB is updated when you reload a segment (and I think only then), so it's closely related.
Quoted 26 lines
Quoted 26 lines
Signed-off-by Kevin Wolf <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 33 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_emulate.h +1
-0@@ -176,6 +176,7 @@ struct x86_emulate_ops {void (*set_idt)(struct x86_emulate_ctxt *ctxt,struct desc_ptr *dt);ulong (*get_cr)(struct x86_emulate_ctxt *ctxt,int cr);int (*set_cr)(struct x86_emulate_ctxt *ctxt,int cr,ulong val); +void (*set_rflags)(struct x86_emulate_ctxt *ctxt,ulong val);int (*cpl)(struct x86_emulate_ctxt *ctxt);int (*get_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong *dest);int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);arch/x86/kvm/emulate.c +0
-0@@ -552,6 +552,14 @@ static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,struct desc_struct desc;
Quoted 6 lines
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 557
Quoted 4 lines
arch/x86/kvm/emulate.c +3
-0ctxt->ops->get_segment (ctxt, &dummy, &desc, &base3, seg); + +if (ctxt->mode ==X86EMUL_MODE_REAL ) + desc.dpl =0 ;Can't happen.
set_segment_selector is only called during task switches right now, so yes, this is impossible. Want me to drop the check? Or BUG()?
Quoted 31 lines
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 561
Quoted 4 lines
arch/x86/kvm/emulate.c +4
-0+else if (ctxt->mode ==X86EMUL_MODE_VM86 ) + desc.dpl =3 ; +else + desc.dpl = selector &0x3 ;I don't think this is right. The SDM explicitly says that just the selectors are loaded, yet you're modifying the DPL here too. Unless there's an abort, we'll be loading the descriptors later anyway (with their DPL). If there's an abort, I think we should continue with the mismatched DPL.
Quoted 20 lines
arch/x86/kvm/emulate.c +13
-0+ ctxt->ops->set_segment (ctxt, selector, &desc, base3, seg); }@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;
Quoted 8 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.
This is closely related to my argument with Gleb whether CPL changes when rflags is reloaded or if it only happens when cs is reloaded. I argued that it makes more sense to couple it with cs, so I guess I have to agree with you mostly.
The SDM says that any faults during loading the segment descriptors happen in the context of the new task, and the task switch is completed before an exception is generated. So it shouldn't make any difference to the guest what the exact point is at which we change CPL, it's an KVM internal decision.
So what about this order:
1. Reload general purpose registers and eflags without updating mode or writing back rflags to the vcpu struct.
2. Load segment selectors without changing the DPL yet.
3. Load segment descriptors, and disable the privilege checks in load_segment_descriptor using a new flag. For SVM, this updates the CPL when cs is reloaded.
4. Call ctxt->ops.set_rflag so that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX uses set_segment to update the CPL, like SVM does.
Would this match your interpretation?
Quoted 25 lines
Quoted 25 lines
arch/x86/kvm/emulate.c +3
-0+ ctxt->ops->set_rflags (ctxt, ctxt->eflags); + +/* General purpose registers */ ctxt->regs[VCPU_REGS_RAX ] = tss->eax; ctxt->regs[VCPU_REGS_RCX ] = tss->ecx; ctxt->regs[VCPU_REGS_RDX ] = tss->edx;arch/x86/kvm/x86.c +5
-0@@ -4040,6 +4040,11 @@ static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)return res; } +static void emulator_set_rflags (struct x86_emulate_ctxt *ctxt,ulong val) +{ +kvm_set_rflags (emul_to_vcpu (ctxt), val); +} +static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr,
Quoted 8 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0.set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
If we change the CPL only with a cs reload, set_rflags can be dropped completely in the end.
Kevin
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 26 lines · Kevin Wolf
Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/emulate.c +12
-0+ +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;Quoted 8 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.This is closely related to my argument with Gleb whether CPL changes when rflags is reloaded or if it only happens when cs is reloaded. I argued that it makes more sense to couple it with cs, so I guess I have to agree with you mostly.
Quoted 6 lines
The SDM says that any faults during loading the segment descriptors happen in the context of the new task, and the task switch is completed before an exception is generated. So it shouldn't make any difference to the guest what the exact point is at which we change CPL, it's an KVM internal decision.
Actually no. What if fault happens during loading of CS descriptor? Spec clearly says it should be performed in a new task context. Meaning in vm86 mode. So cpl switch should happen already.
Quoted 51 lines
So what about this order:
1. Reload general purpose registers and eflags without updating mode or writing back rflags to the vcpu struct.
2. Load segment selectors without changing the DPL yet.
3. Load segment descriptors, and disable the privilege checks in
load_segment_descriptorusing a new flag. For SVM, this updates the CPL when cs is reloaded.4. Call
ctxt->ops.set_rflagso that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX usesset_segmentto update the CPL, like SVM does.Would this match your interpretation?
Quoted 25 lines
Quoted 25 lines
arch/x86/kvm/emulate.c +3
-0+ ctxt->ops->set_rflags (ctxt, ctxt->eflags); + +/* General purpose registers */ ctxt->regs[VCPU_REGS_RAX ] = tss->eax; ctxt->regs[VCPU_REGS_RCX ] = tss->ecx; ctxt->regs[VCPU_REGS_RDX ] = tss->edx;arch/x86/kvm/x86.c +5
-0@@ -4040,6 +4040,11 @@ static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)return res; } +static void emulator_set_rflags (struct x86_emulate_ctxt *ctxt,ulong val) +{ +kvm_set_rflags (emul_to_vcpu (ctxt), val); +} +static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr,Quoted 8 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0.set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
Quoted 4 lines
If we change the CPL only with a cs reload,
set_rflagscan be dropped completely in the end.Kevin
--
Gleb.
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Kevin WolfQuoted 12 lines · Kevin Wolf
Am 30.01.2012 11:24, schrieb Avi Kivity:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
Quoted 4 lines
Quoted 1 line
svm has
vmcb_save_area::cpl; it's independent of CS.RPL.Right, but cpl in the VMCB is updated when you reload a segment (and I think only then),
Gah, it's broken. It should be qualified by more - real mode should keep cpl at zero, vm86 at 3. And setting rflags.vm86 should update cpl as well.
For example, live migration while in real mode with cs&3!=0 or in vm86 mode with cs&3==0 would set the wrong cpl.
Quoted 11 lines
so it's closely related.
Quoted 6 lines
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 557
Quoted 4 lines
arch/x86/kvm/emulate.c +3
-0ctxt->ops->get_segment (ctxt, &dummy, &desc, &base3, seg); + +if (ctxt->mode ==X86EMUL_MODE_REAL ) + desc.dpl =0 ;Can't happen.
set_segment_selectoris only called during task switches right now, so yes, this is impossible. Want me to drop the check? OrBUG()?
BUG()s are dangerous in rarely used code since they can be exploited as a DoS. So maybe a WARN_ON_ONCE().
Quoted 36 lines
Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/emulate.c +12
-0@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;Quoted 8 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.This is closely related to my argument with Gleb whether CPL changes when rflags is reloaded or if it only happens when cs is reloaded. I argued that it makes more sense to couple it with cs, so I guess I have to agree with you mostly.
The SDM says that any faults during loading the segment descriptors happen in the context of the new task, and the task switch is completed before an exception is generated. So it shouldn't make any difference to the guest what the exact point is at which we change CPL, it's an KVM internal decision.
Quoted 6 lines
So what about this order:
1. Reload general purpose registers and eflags without updating mode or writing back rflags to the vcpu struct.
2. Load segment selectors without changing the DPL yet.
(or changing anything in the segment cache)
Quoted 3 lines
3. Load segment descriptors, and disable the privilege checks in
load_segment_descriptorusing a new flag.
I don't like doing things that aren't directly traceable to the SDM. Perhaps we should pass the cpl as a parameter to load_segment_descriptor(). Or we should ->set_cpl() just before.
Quoted 11 lines
For SVM, this updates
the CPL when cs is reloaded.4. Call
ctxt->ops.set_rflagso that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX usesset_segmentto update the CPL, like SVM does.Would this match your interpretation?
Not exactly. I claim that the cpl is a separate state from cs.cpl/ss.rpl/cr0.pe/eflags.vm - it cannot be derived just from those. On the transition from real mode to protected mode, you can have (say) cs=0x13 and cpl=0.
Outside of mode switches, it's good to have set_segment() update the cpl automatically. But inside mode switches it can screw up further checks or aborts.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr,Quoted 8 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0.set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
Quoted 2 lines
If we change the CPL only with a cs reload,
set_rflagscan be dropped completely in the end.
That's attractive, yes.
Suppose load_segment_descriptor(..., VCPU_SREG_LDTR) fails. What should the new cpl be? Does it matter?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Am 30.01.2012 14:23, schrieb Avi Kivity:
Quoted 18 lines · Kevin Wolf
Quoted 12 lines
Am 30.01.2012 11:24, schrieb Avi Kivity:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
Quoted 4 lines
Quoted 1 line
svm has
vmcb_save_area::cpl; it's independent of CS.RPL.Right, but cpl in the VMCB is updated when you reload a segment (and I think only then),
Quoted 6 lines
Gah, it's broken. It should be qualified by more - real mode should keep cpl at zero, vm86 at 3. And setting rflags.vm86 should update cpl as well.
For example, live migration while in real mode with cs&3!=0 or in vm86 mode with cs&3==0 would set the wrong cpl.
Ah, right, I didn't think of this case.
Not sure if I should fix it in this series. If we do the right fixes to the task switch, it won't be strictly needed for the bug I'm fixing.
Quoted 9 lines
Quoted 9 lines
so it's closely related.
Quoted 6 lines
arch/x86/kvm/emulate.c static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, · line 557
Quoted 4 lines
arch/x86/kvm/emulate.c +3
-0ctxt->ops->get_segment (ctxt, &dummy, &desc, &base3, seg); + +if (ctxt->mode ==X86EMUL_MODE_REAL ) + desc.dpl =0 ;Can't happen.
Quoted 5 lines
Quoted 2 lines
set_segment_selectoris only called during task switches right now, so yes, this is impossible. Want me to drop the check? OrBUG()?BUG()s are dangerous in rarely used code since they can be exploited as a DoS. So maybe a
WARN_ON_ONCE().
Ok.
Quoted 47 lines
Quoted 37 lines
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/emulate.c +12
-0@@ -2273,6 +2281,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get it to the vcpu struct immediately because it influences +* the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ;Quoted 8 lines
arch/x86/kvm/emulate.c static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, · line 2298
Quoted 3 lines
arch/x86/kvm/emulate.c +3
-0+else + ctxt->mode =X86EMUL_MODE_PROT32 ; +Shouldn't this be done after the
set_segment_selector()block? My interpretation of the SDM is that if a fault happens while loading descriptors the fault happens with old segment cache, that is, it needs to be interpreted according to the old mode.This is closely related to my argument with Gleb whether CPL changes when rflags is reloaded or if it only happens when cs is reloaded. I argued that it makes more sense to couple it with cs, so I guess I have to agree with you mostly.
The SDM says that any faults during loading the segment descriptors happen in the context of the new task, and the task switch is completed before an exception is generated. So it shouldn't make any difference to the guest what the exact point is at which we change CPL, it's an KVM internal decision.
Quoted 6 lines
So what about this order:
1. Reload general purpose registers and eflags without updating mode or writing back rflags to the vcpu struct.
2. Load segment selectors without changing the DPL yet.
(or changing anything in the segment cache)
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
3. Load segment descriptors, and disable the privilege checks in
load_segment_descriptorusing a new flag.I don't like doing things that aren't directly traceable to the SDM. Perhaps we should pass the cpl as a parameter to
load_segment_descriptor(). Or we should ->set_cpl() just before.
I like the idea of having a ->set_cpl(). For SVM it should be trivial to implement.
For VMX, I think instead of clearing VCPU_EXREG_CPL, vcpu_vmx->cpl should directly be updated in set_rflags and set_segment (and in the new set_cpl, obviously).
Would that be enough or would we have to avoid clearing it in all other places as well? Where would it be initialised if it's not enough?
Quoted 10 lines
Quoted 10 lines
For SVM, this updates
the CPL when cs is reloaded.4. Call
ctxt->ops.set_rflagso that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX usesset_segmentto update the CPL, like SVM does.
Quoted 6 lines
Quoted 1 line
Would this match your interpretation?
Not exactly. I claim that the cpl is a separate state from cs.cpl/ss.rpl/cr0.pe/eflags.vm - it cannot be derived just from those. On the transition from real mode to protected mode, you can have (say) cs=0x13 and cpl=0.
But even with cs=0x13 cs.dpl would still be 0 (in the segment cache) before cs is reloaded and you switch to a real protected mode segment, right?
Quoted 3 lines
Outside of mode switches, it's good to have
set_segment()update the cpl automatically. But inside mode switches it can screw up further checks or aborts.
Can you give a specific example of how it screws things up?
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 11 lines
arch/x86/kvm/x86.c +1
-0static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
Quoted 7 lines
Quoted 2 lines
If we change the CPL only with a cs reload,
set_rflagscan be dropped completely in the end.That's attractive, yes.
Suppose
load_segment_descriptor(..., VCPU_SREG_LDTR)fails. What should the new cpl be? Does it matter?
I think the one matching the new cs/eflags. The SDM says that the task switch is completed without further segment checks and then an exception is thrown.
Kevin
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 23 lines · Kevin Wolf
Am 30.01.2012 14:23, schrieb Avi Kivity:
Quoted 22 lines · Kevin Wolf
Quoted 12 lines
Am 30.01.2012 11:24, schrieb Avi Kivity:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
Quoted 4 lines
Quoted 1 line
svm has
vmcb_save_area::cpl; it's independent of CS.RPL.Right, but cpl in the VMCB is updated when you reload a segment (and I think only then),
Gah, it's broken. It should be qualified by more - real mode should keep cpl at zero, vm86 at 3. And setting rflags.vm86 should update cpl as well.
Quoted 6 lines
Quoted 2 lines
For example, live migration while in real mode with cs&3!=0 or in vm86 mode with cs&3==0 would set the wrong cpl.
Ah, right, I didn't think of this case.
Not sure if I should fix it in this series.
No need. But we need to have a clear picture of where we're going so we move in the right direction. And that direction is cpl decoupled from cs.rpl/ss.rpl/cr0.pe/eflags.vm (but changes in response to them, except cr0.pe).
Note that we don't expose cpl as separate state for save/restore, so the kvm ABI follows vmx instead of svm (unfortunately).
Quoted 2 lines
If we do the right fixes to
the task switch, it won't be strictly needed for the bug I'm fixing.
Right.
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
3. Load segment descriptors, and disable the privilege checks in
load_segment_descriptorusing a new flag.I don't like doing things that aren't directly traceable to the SDM. Perhaps we should pass the cpl as a parameter to
load_segment_descriptor(). Or we should ->set_cpl() just before.
Quoted 6 lines
I like the idea of having a ->set_cpl(). For SVM it should be trivial to implement.
For VMX, I think instead of clearing
VCPU_EXREG_CPL,vcpu_vmx->cplshould directly be updated inset_rflagsandset_segment(and in the newset_cpl, obviously).
vcpu_vmx::cpl is currently just a cache, when valid it reflects the state of cr0.pe/eflags.vm/cs.rpl. It's not separate state as the VMCS has nowhere to hold it. vmx cannot virtualize the sequence 'mov $3, %ss; mov $1, %eax; mov %eax, %cr0; nop' - we have emulate_invalid_guest_state for that, but it's not on by default.
What you propose is converting vcpu_vmx::cpl from a cache to separate state, but that state can change after a vmexit. It's not entirely clear when it's valid and when it isn't.
Quoted 2 lines
Would that be enough or would we have to avoid clearing it in all other places as well? Where would it be initialised if it's not enough?
Maybe vmx_vcpu_reset().
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
For SVM, this updates
the CPL when cs is reloaded.4. Call
ctxt->ops.set_rflagso that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX usesset_segmentto update the CPL, like SVM does.Would this match your interpretation?
Quoted 8 lines
Quoted 4 lines
Not exactly. I claim that the cpl is a separate state from cs.cpl/ss.rpl/cr0.pe/eflags.vm - it cannot be derived just from those. On the transition from real mode to protected mode, you can have (say) cs=0x13 and cpl=0.
But even with cs=0x13 cs.dpl would still be 0 (in the segment cache) before cs is reloaded and you switch to a real protected mode segment, right?
Right. But cpl is defined as cs.rpl, not cs.dpl, and I think there are cases where cs.rpl != cs.dpl.
Quoted 5 lines
Quoted 3 lines
Outside of mode switches, it's good to have
set_segment()update the cpl automatically. But inside mode switches it can screw up further checks or aborts.Can you give a specific example of how it screws things up?
KVM_SET_REGS/KVM_SET_SREGS happen non-atomically, so you might have KVM_SET_SREGS raising the CPL to 3 only to lower it afterwards. Things in the middle happen with the wrong CPL. Not sure if anything actually checks it there.
The other case is what we're looking at, task switch. To actually update cpl, set_segment() needs to look at cr0.pe and eflags, but these might not have been committed yet. It's all solvable but the solution involves knowing exactly what kvm and the emulator depend on, which makes it very fragile. I think giving the emulator less complicated primitives will make it more readable.
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 15 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 12 lines
static int
emulator_get_cpl(struct x86_emulate_ctxt *ctxt){arch/x86/kvm/x86.c +1
-0return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
If we change the CPL only with a cs reload,
set_rflagscan be dropped completely in the end.
Quoted 8 lines
Quoted 4 lines
That's attractive, yes.
Suppose
load_segment_descriptor(..., VCPU_SREG_LDTR)fails. What should the new cpl be? Does it matter?I think the one matching the new cs/eflags. The SDM says that the task switch is completed without further segment checks and then an exception is thrown.
My bet is that it will be 3 if eflags.vm=1 and unchanged otherwise -- the cpl update happens when the segment cache is updated. But that's just a guess.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Am 30.01.2012 15:32, schrieb Avi Kivity:
Quoted 31 lines · Kevin Wolf
Quoted 23 lines
Am 30.01.2012 14:23, schrieb Avi Kivity:
Quoted 22 lines · Kevin Wolf
Quoted 12 lines
Am 30.01.2012 11:24, schrieb Avi Kivity:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
Task switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly and privilege checks succeed.
VMX code calculates the CPL from the code segment selector and rflags, so it needs rflags to be updated in the vcpu struct. SVM stores the DPL of the code segment instead, so we must be sure to give the right one when updating the selector.
Quoted 4 lines
Quoted 1 line
svm has
vmcb_save_area::cpl; it's independent of CS.RPL.Right, but cpl in the VMCB is updated when you reload a segment (and I think only then),
Gah, it's broken. It should be qualified by more - real mode should keep cpl at zero, vm86 at 3. And setting rflags.vm86 should update cpl as well.
Quoted 6 lines
Quoted 2 lines
For example, live migration while in real mode with cs&3!=0 or in vm86 mode with cs&3==0 would set the wrong cpl.
Ah, right, I didn't think of this case.
Not sure if I should fix it in this series.
Quoted 7 lines
No need. But we need to have a clear picture of where we're going so we move in the right direction. And that direction is cpl decoupled from cs.rpl/ss.rpl/cr0.pe/eflags.vm (but changes in response to them, except cr0.pe).
Note that we don't expose cpl as separate state for save/restore, so the kvm ABI follows vmx instead of svm (unfortunately).
Which means that restoring a VM that was in the middle of a RM -> PM mode switch will result in corrupted state. Probably not a huge problem in practice, but can we fail the save?
Quoted 26 lines
Quoted 2 lines
If we do the right fixes to
the task switch, it won't be strictly needed for the bug I'm fixing.Right.
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
3. Load segment descriptors, and disable the privilege checks in
load_segment_descriptorusing a new flag.I don't like doing things that aren't directly traceable to the SDM. Perhaps we should pass the cpl as a parameter to
load_segment_descriptor(). Or we should ->set_cpl() just before.Quoted 6 lines
I like the idea of having a ->set_cpl(). For SVM it should be trivial to implement.
For VMX, I think instead of clearing
VCPU_EXREG_CPL,vcpu_vmx->cplshould directly be updated inset_rflagsandset_segment(and in the newset_cpl, obviously).
vcpu_vmx::cplis currently just a cache, when valid it reflects the state of cr0.pe/eflags.vm/cs.rpl. It's not separate state as the VMCS has nowhere to hold it. vmx cannot virtualize the sequence 'mov $3, %ss; mov $1, %eax; mov %eax, %cr0; nop' - we haveemulate_invalid_guest_statefor that, but it's not on by default.
Quoted 3 lines
What you propose is converting
vcpu_vmx::cplfrom a cache to separate state, but that state can change after a vmexit. It's not entirely clear when it's valid and when it isn't.
It's the only way I can imagine to get a set_cpl() callback. Do you have another one?
That it's not entirely clear where it's valid and where it needs to be set is exactly the problem I have with it. Probably even more than you as I'm not very familiar with the KVM code.
Quoted 4 lines
Quoted 2 lines
Would that be enough or would we have to avoid clearing it in all other places as well? Where would it be initialised if it's not enough?
Maybe
vmx_vcpu_reset().
Do all CPL changes go through set_cr0/segment/rflags/cpl? I guess yes, so initialising on reset and keeping it valid all the time should be possible indeed.
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
For SVM, this updates
the CPL when cs is reloaded.4. Call
ctxt->ops.set_rflagso that VMX updates the CPL. Should be cleaned up in a follow-up patch, so that VMX usesset_segmentto update the CPL, like SVM does.Would this match your interpretation?
Not exactly. I claim that the cpl is a separate state from cs.cpl/ss.rpl/cr0.pe/eflags.vm - it cannot be derived just from those. On the transition from real mode to protected mode, you can have (say) cs=0x13 and cpl=0.
Quoted 6 lines
Quoted 3 lines
But even with cs=0x13 cs.dpl would still be 0 (in the segment cache) before cs is reloaded and you switch to a real protected mode segment, right?
Right. But cpl is defined as cs.rpl, not cs.dpl, and I think there are cases where cs.rpl != cs.dpl.
Sounds like conforming code segments? A part of protected mode magic that I've never touched and never intended to... But using the RPL instead makes sense in that context.
So SVM isn't only wrong in not considering real mode and VM86, but also in calculating CPL from vmcb->save.cs.attrib rather than from the RPL in the selector?
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
Outside of mode switches, it's good to have
set_segment()update the cpl automatically. But inside mode switches it can screw up further checks or aborts.Can you give a specific example of how it screws things up?
KVM_SET_REGS/KVM_SET_SREGS happen non-atomically, so you might have
KVM_SET_SREGSraising the CPL to 3 only to lower it afterwards. Things in the middle happen with the wrong CPL. Not sure if anything actually checks it there.
Quoted 6 lines
The other case is what we're looking at, task switch. To actually update cpl,
set_segment()needs to look at cr0.pe and eflags, but these might not have been committed yet. It's all solvable but the solution involves knowing exactly what kvm and the emulator depend on, which makes it very fragile. I think giving the emulator less complicated primitives will make it more readable.
I think the main problem here is that you have two sets of registers, one in the vcpu struct and one in the emulator context.
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
static int
emulator_get_cpl(struct x86_emulate_ctxt *ctxt){arch/x86/kvm/x86.c +0
-0return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr,Quoted 8 lines
arch/x86/kvm/x86.c static struct x86_emulate_ops emulate_ops = { · line 4210
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0.set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,It would be good to switch the entire emulator to use ->set_rflags() instead of sometimes letting the caller do it.
Quoted 2 lines
If we change the CPL only with a cs reload,
set_rflagscan be dropped completely in the end.That's attractive, yes.
Suppose
load_segment_descriptor(..., VCPU_SREG_LDTR)fails. What should the new cpl be? Does it matter?
Quoted 7 lines
Quoted 3 lines
I think the one matching the new cs/eflags. The SDM says that the task switch is completed without further segment checks and then an exception is thrown.
My bet is that it will be 3 if eflags.vm=1 and unchanged otherwise -- the cpl update happens when the segment cache is updated. But that's just a guess.
Does even anyone see the new CPL in error cases? An exception is thrown immediately, so cs is reloaded and we get an even newer CPL. So to take any notice of the CPL, the "complete the task switch" part would have to fail a privilege check. The one thing that comes to mind is that pushing an error code could fail, but I don't think this is considered part of the task switch.
Kevin
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 7 lines · Kevin Wolf
Quoted 4 lines
My bet is that it will be 3 if eflags.vm=1 and unchanged otherwise -- the cpl update happens when the segment cache is updated. But that's just a guess.
Does even anyone see the new CPL in error cases? An exception is thrown immediately, so cs is reloaded and we get an even newer CPL.
Depends on what we have on the IDT for the exception handler...
Quoted 5 lines
So to take
any notice of the CPL, the "complete the task switch" part would have to fail a privilege check. The one thing that comes to mind is that pushing an error code could fail, but I don't think this is considered part of the task switch.
Right.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Kevin WolfQuoted 10 lines · On Mon, 30 Jan 2012 16:26:06 +0100 Kevin Wolf <[email protected]> wrote:
Quoted 6 lines
The other case is what we're looking at, task switch. To actually update cpl,
set_segment()needs to look at cr0.pe and eflags, but these might not have been committed yet. It's all solvable but the solution involves knowing exactly what kvm and the emulator depend on, which makes it very fragile. I think giving the emulator less complicated primitives will make it more readable.I think the main problem here is that you have two sets of registers, one in the vcpu struct and one in the emulator context.
I think we can, partly?, eliminate the usage of the latter by moving the register initialization to the decode/emulation stage, as once talked on kvm ml, and changing the writeback code to use callbacks.
But still some refactoring is needed before that.
Takuya
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
Quoted 4 lines
Quoted 2 lines
Would that be enough or would we have to avoid clearing it in all other places as well? Where would it be initialised if it's not enough?
Maybe
vmx_vcpu_reset().Do all CPL changes go through set_cr0/segment/rflags/cpl? I guess yes, so initialising on reset and keeping it valid all the time should be possible indeed.
CPL can be changed while guest is running. SVM saves it for us in cpl field. VMX does not, so we either will have to update cpl on each exit (cpl = cs & 3) or somehow mark it not up-to-date and recalculate on access. Can VMX exit while cpl != cs & 3 or can this happen only during emulation? If it can we cannot know real cpl after exit.
--
Gleb.
Re: [PATCH v2 3/3] KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 10 lines · Gleb Natapov
Quoted 9 lines · Kevin Wolf
Quoted 4 lines
Quoted 2 lines
Would that be enough or would we have to avoid clearing it in all other places as well? Where would it be initialised if it's not enough?
Maybe
vmx_vcpu_reset().Do all CPL changes go through set_cr0/segment/rflags/cpl? I guess yes, so initialising on reset and keeping it valid all the time should be possible indeed.
Quoted 6 lines
CPL can be changed while guest is running. SVM saves it for us in cpl field. VMX does not, so we either will have to update cpl on each exit (cpl = cs & 3) or somehow mark it not up-to-date and recalculate on access. Can VMX exit while cpl != cs & 3 or can this happen only during emulation? If it can we cannot know real cpl after exit.
Perhaps it can, with unrestricted guests, but I think we don't allow those conditions (we trap cr0 writes).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 4 lines · Kevin Wolf
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
You forgot to set cpl to 3 in vmcb in svm_set_rflags() when vm86 is enabled, no?
Quoted 5 lines
Kevin Wolf (3):
KVM: x86 emulator: Fix task switch privilege checks
KVM: x86 emulator: VM86 segments must have DPL 3
KVM: x86 emulator: Allow PM/VM86 switch during task switch
Quoted 10 lines
arch/x86/include/asm/kvm_emulate.h | 3 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/emulate.c | 79 ++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm.c | 5 ++- arch/x86/kvm/vmx.c | 8 ++- arch/x86/kvm/x86.c | 12 ++++- 6 files changed, 94 insertions(+), 17 deletions(-)Signature · 1 line
1.7.6.5
--
Gleb.
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 6 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?
SVM updates the CPL when the segment selector for CS is loaded. From a svm.c POV, segment selectors are updated immediately after set_rflags, so it wouldn't really make a difference to do it twice.
Kevin
Quoted 6 lines · Kevin Wolf
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.
It is too subtle to rely on that. The fact is that checking cpl after set_rflags provides incorrect value. This better be fixed. BTW does load_state_from_tss16() need the same fix?
--
Gleb.
Quoted 9 lines · Gleb Natapov
Quoted 8 lines · Kevin Wolf
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 6 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?
Quoted 8 lines
Quoted 4 lines
SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed. BTW doesload_state_from_tss16()need the same fix?
Probably not since you can't switch to vm86 this way. But may be for other reasons.
--
Gleb.
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.
Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Depending on what we decide on (Gleb and I disagree on this, so more input would be helpful), either VMX or SVM need a cleanup. I think it can be done independent from and on top of this fix.
Quoted 1 line
BTW does
load_state_from_tss16()need the same fix?
The manual says "Do not use a 16-bit TSS to implement a virtual-8086 task." Actually, I don't think you could do that, even if you wanted, with a 16-bit flags field.
Kevin
Quoted 14 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 13 lines · Kevin Wolf
Quoted 12 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 6 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.
Quoted 8 lines
Quoted 2 lines
It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl. Making it depend on the mode causes subtle issues during the mode switch - if you switch from real mode to protected mode while cs & 3 != 0 you end up with the wrong cpl until the jmp instruction is executed.
Quoted 4 lines
Depending on what we decide on (Gleb and I disagree on this, so more input would be helpful), either VMX or SVM need a cleanup. I think it can be done independent from and on top of this fix.
Right. IMO we should follow svm and make vmx be more flexible.
One way to do it is to have a new variable for vmx cpl, and reconcile all the places where cpl is stored (cs.rpl, ss.rpl, cr0.pe, rflags.vm) just before entry. If we can't reconcile it, we have to emulate.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 24 lines · Avi Kivity
Quoted 14 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 13 lines · Kevin Wolf
Quoted 12 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 6 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.Quoted 8 lines
Quoted 2 lines
It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm. svm checks vmcb->cpl that can be outdated during emulation.
Quoted 10 lines
Making it depend on
the mode causes subtle issues during the mode switch - if you switch from real mode to protected mode while cs & 3 != 0 you end up with the wrong cpl until the jmp instruction is executed.Quoted 4 lines
Depending on what we decide on (Gleb and I disagree on this, so more input would be helpful), either VMX or SVM need a cleanup. I think it can be done independent from and on top of this fix.
Quoted 10 lines
Right. IMO we should follow svm and make vmx be more flexible.
One way to do it is to have a new variable for vmx cpl, and reconcile all the places where cpl is stored (cs.rpl, ss.rpl, cr0.pe, rflags.vm) just before entry. If we can't reconcile it, we have to emulate.
Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
Quoted 25 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).
Quoted 2 lines
svm checks
vmcb->cplthat can be
outdated during emulation.
This decoupling is actually helpful, since you can defer the cpl change until the end of the switch, and avoid inconsistencies like those checked by cs_ss_rpl_check().
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 36 lines · Avi Kivity
Quoted 25 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).
Quoted 7 lines
Quoted 2 lines
svm checks
vmcb->cplthat can be
outdated during emulation.This decoupling is actually helpful, since you can defer the cpl change until the end of the switch, and avoid inconsistencies like those checked by
cs_ss_rpl_check().
I am not saying it is not helpful. The fact that it exists tells us that dpl and cpl are not always the same. But cpl change should not be delayed until the end of the switch! Mode switch happens in the middle of a task switch. Task switch happens in 3 stages according to the spec. If error happens during the first one (steps 1-11) it is handled by an old task, if error happens during second stage (12 this is where mode change happens) then anything can happen (we may kill vcpu till reset if we wish) after that new task is running and all errors are handled by a new task.
To model this accurately we need to do task switch in this three stages too and do a full register writeback after stage 2 before stage 3. Or alternatively emulator should never access vcpu state during emulation. Entire vcpu state should be in emulation ctx. But this is more complicated and slow.
--
Gleb.
Quoted 44 lines · Gleb Natapov
Quoted 36 lines · Avi Kivity
Quoted 25 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).Quoted 7 lines
Quoted 2 lines
svm checks
vmcb->cplthat can be
outdated during emulation.This decoupling is actually helpful, since you can defer the cpl change until the end of the switch, and avoid inconsistencies like those checked by
cs_ss_rpl_check().
Quoted 8 lines
I am not saying it is not helpful. The fact that it exists tells us that dpl and cpl are not always the same. But cpl change should not be delayed until the end of the switch! Mode switch happens in the middle of a task switch. Task switch happens in 3 stages according to the spec. If error happens during the first one (steps 1-11) it is handled by an old task, if error happens during second stage (12 this is where mode change happens) then anything can happen (we may kill vcpu till reset if we wish) after that new task is running and all errors are handled by a new task.
Agree.
Quoted 5 lines
To model this accurately we need to do task switch in this three stages too and do a full register writeback after stage 2 before stage 3. Or alternatively emulator should never access vcpu state during emulation. Entire vcpu state should be in emulation ctx. But this is more complicated and slow.
Speed is immaterial here, but I agree about the complexity. I guess we should do full writeback after stage 2.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).
The fact that vmx check for cpl by (cs & 3) looks incorrect. It should check dpl.
--
Gleb.
Quoted 6 lines · Gleb Natapov
Quoted 5 lines · Avi Kivity
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
Quoted 8 lines
Quoted 5 lines
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).The fact that vmx check for cpl by (cs & 3) looks incorrect. It should check dpl.
Which check?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 12 lines · Avi Kivity
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Avi Kivity
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).
Quoted 6 lines
Quoted 3 lines
The fact that vmx check for cpl by (cs & 3) looks incorrect. It should check dpl.
Which check?
In vmx_get_cpl().
--
Gleb.
Quoted 13 lines · Gleb Natapov
Quoted 12 lines · Avi Kivity
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Avi Kivity
Quoted 4 lines
Quoted 3 lines
It's best to make it independent (like svm, and force vmx to emulate this behaviour). Real mode forces cpl to 0, vm86 forces cpl to 3, protected mode (and I think long mode) uses cs.rpl.
This is what vmx does, not svm.
That's the architectural definition, except for mode switch sequences. vmx implements it directly which means that mode switch sequences sometimes fail, either in guest software (setting cr0.pe while cs & 3 !=
0) or in "microcode" (emulate.c).
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
The fact that vmx check for cpl by (cs & 3) looks incorrect. It should check dpl.
Which check?
In
vmx_get_cpl().
3A 3.2:
Current privilege level (CPL) field — (Bits 0 and 1 of the CS segment register.) Indicates the privilege level of the currently executing program or procedure. The term current privilege level (CPL) refers to the setting of this field.
(probably allows a library with a low DPL to be run as part of a program with high RPL/CPL; or you can use a "conforming code segment" as a sort of SUID bit).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 23 lines · Kevin Wolf
Am 30.01.2012 09:55, schrieb Gleb Natapov:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Am 27.01.2012 20:52, schrieb Gleb Natapov:
Quoted 5 lines · Kevin Wolf
Quoted 4 lines
I believe this should work with both VMX and SVM now. Gleb, Jörg, can one of you test this with SVM? I did some testing on my buggy processor and it looks as good as it gets, but it would be better if you could confirm.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
You forgot to set cpl to 3 in vmcb in
svm_set_rflags()when vm86 is enabled, no?SVM updates the CPL when the segment selector for CS is loaded. From a
svm.cPOV, segment selectors are updated immediately afterset_rflags, so it wouldn't really make a difference to do it twice.It is too subtle to rely on that. The fact is that checking cpl after
set_rflagsprovides incorrect value. This better be fixed.Depends on what value you consider to be correct between reloading eflags and reloading cs. I think it's logical and more consistent to say that CPL only changes when cs is reloaded, but you could argue that it's effective with the reload of rflags. It doesn't make a difference to guests, so we can decide to choose whatever we like.
Quoted 4 lines
Depending on what we decide on (Gleb and I disagree on this, so more input would be helpful), either VMX or SVM need a cleanup. I think it can be done independent from and on top of this fix.
I think you made my point (that cpl in svm should be updated on rflags update) by pointing me to this part of the spec:
The processor tests the VM flag under three general conditions:
• When loading segment registers, to determine whether to use 8086-style address translation.
• When decoding instructions, to determine which instructions are not supported in virtual-8086 mode and which instructions are sensitive to IOPL.
• When checking privileged instructions, on page accesses, or when performing other permission checks. (Virtual-8086 mode always executes at CPL 3.)
Bullet 3 clearly proves it.
Furthermore task switch loads eflags and segment selector at stage 12. After that CPU runs on a new task, but since segment descriptors are still not loaded CS dpl is not updated yet, but task is in CPL3 already.
Quoted 6 lines
Quoted 1 line
BTW does
load_state_from_tss16()need the same fix?The manual says "Do not use a 16-bit TSS to implement a virtual-8086 task." Actually, I don't think you could do that, even if you wanted, with a 16-bit flags field.
Yes. May be there are other reasons to update flags earlier like spec specifies, but I can think of any. Will fix them when we find them.
--
Gleb.