Fix handling of a fault during NMI unblocked due to IRET
PATCH 1/4 Fix handling of a fault during NMI unblocked due to IRET
Bit 12 is undefined in any of the following cases:
If the VM exit sets the valid bit in the IDT-vectoring information field. If the VM exit is due to a double fault.
---
arch/x86/kvm/vmx.c | 17 +++++++++++------ 1 files changed, 11 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx.c +11 -6
@@ -3259,36 +3259,41 @@ static void update_tpr_threshold(struct kvm_vcpu *vcpu)static void vmx_complete_interrupts (struct vcpu_vmx *vmx) {u32 exit_intr_info; -u32 idt_vectoring_info; +u32 idt_vectoring_info = vmx->idt_vectoring_info;bool unblock_nmi;u8 vector;int type;bool idtv_info_valid;u32 error; + idtv_info_valid = idt_vectoring_info &VECTORING_INFO_VALID_MASK ; exit_intr_info =vmcs_read32 (VM_EXIT_INTR_INFO );if (cpu_has_virtual_nmis ()) { unblock_nmi = (exit_intr_info &INTR_INFO_UNBLOCK_NMI ) !=0 ; vector = exit_intr_info &INTR_INFO_VECTOR_MASK ;/* -* SDM 3: 25.7.1.2 +* SDM 3: 27.7.1.2 (September 2008) * Re-set bit "block by NMI" before VM entry if vmexit caused by * a guest IRET fault. +* SDM 3: 23.2.2 (September 2008) +* Bit 12 is undefined in any of the following cases: +* If the VM exit sets the valid bit in the IDT-vectoring +* information field. +* If the VM exit is due to a double fault. */ -if (unblock_nmi && vector !=DF_VECTOR ) +if ((exit_intr_info &INTR_INFO_VALID_MASK ) && unblock_nmi && + vector !=DF_VECTOR && !idtv_info_valid)vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); }else if (unlikely (vmx->soft_vnmi_blocked)) vmx->vnmi_blocked_time +=ktime_to_ns (ktime_sub (ktime_get (), vmx->entry_time)); - idt_vectoring_info = vmx->idt_vectoring_info; - idtv_info_valid = idt_vectoring_info &VECTORING_INFO_VALID_MASK ; vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ;if (vmx->vcpu.arch.nmi_injected) {/* -* SDM 3: 25.7.1.2 +* SDM 3: 27.7.1.2 (September 2008) * Clear bit "block by NMI" before VM entry if a NMI delivery * faulted. */
PATCH 2/4 Rewrite twisted maze of if() statements with more straightforward switch()
---
arch/x86/kvm/vmx.c | 43 +++++++++++++++++++++++++------------------ 1 files changed, 25 insertions(+), 18 deletions(-)
arch/x86/kvm/vmx.c +25 -18
@@ -3264,7 +3264,6 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)u8 vector;int type;bool idtv_info_valid; -u32 error; idtv_info_valid = idt_vectoring_info &VECTORING_INFO_VALID_MASK ; exit_intr_info =vmcs_read32 (VM_EXIT_INTR_INFO );
@@ -3289,34 +3288,42 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx) vmx->vnmi_blocked_time +=ktime_to_ns (ktime_sub (ktime_get (), vmx->entry_time)); + vmx->vcpu.arch.nmi_injected = false; +kvm_clear_exception_queue (&vmx->vcpu); +kvm_clear_interrupt_queue (&vmx->vcpu); + +if (!idtv_info_valid) +return ; + vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ; -if (vmx->vcpu.arch.nmi_injected) { + +switch (type) { +case INTR_TYPE_NMI_INTR : + vmx->vcpu.arch.nmi_injected = true;/* * SDM 3: 27.7.1.2 (September 2008) -* Clear bit "block by NMI" before VM entry if a NMI delivery -* faulted. +* Clear bit "block by NMI" before VM entry if a NMI +* delivery faulted. */ -if (idtv_info_valid && type ==INTR_TYPE_NMI_INTR ) -vmcs_clear_bits (GUEST_INTERRUPTIBILITY_INFO , -GUEST_INTR_STATE_NMI ); -else - vmx->vcpu.arch.nmi_injected = false; - } -kvm_clear_exception_queue (&vmx->vcpu); -if (idtv_info_valid && (type ==INTR_TYPE_HARD_EXCEPTION || - type ==INTR_TYPE_SOFT_EXCEPTION )) { +vmcs_clear_bits (GUEST_INTERRUPTIBILITY_INFO , +GUEST_INTR_STATE_NMI ); +break ; +case INTR_TYPE_HARD_EXCEPTION : +case INTR_TYPE_SOFT_EXCEPTION :if (idt_vectoring_info &VECTORING_INFO_DELIVER_CODE_MASK ) { - error =vmcs_read32 (IDT_VECTORING_ERROR_CODE ); -kvm_queue_exception_e (&vmx->vcpu, vector, error); +u32 err =vmcs_read32 (IDT_VECTORING_ERROR_CODE ); +kvm_queue_exception_e (&vmx->vcpu, vector, err); }else kvm_queue_exception (&vmx->vcpu, vector); vmx->idt_vectoring_info =0 ; - } -kvm_clear_interrupt_queue (&vmx->vcpu); -if (idtv_info_valid && type ==INTR_TYPE_EXT_INTR ) { +break ; +case INTR_TYPE_EXT_INTR :kvm_queue_interrupt (&vmx->vcpu, vector); vmx->idt_vectoring_info =0 ; +break ; +default : +break ; } }
PATCH 3/4 Do not zero idt_vectoring_info in vmx_complete_interrupts().
In reply to Gleb NatapovWe will need it later in task_switch().
Code in handle_exception() is dead. is_external_interrupt(vect_info) will always be false since idt_vectoring_info is zeroed in vmx_complete_interrupts().
---
arch/x86/kvm/vmx.c | 7 ------- 1 files changed, 0 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx.c +0 -7
@@ -2613,11 +2613,6 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)printk (KERN_ERR "%s: unexpected, vectoring info 0x%x " "intr info 0x%x\n" , __func__, vect_info, intr_info); -if (!irqchip_in_kernel (vcpu->kvm) &&is_external_interrupt (vect_info)) { -int irq = vect_info &VECTORING_INFO_VECTOR_MASK ; -kvm_push_irq (vcpu, irq); - } -if ((intr_info &INTR_INFO_INTR_TYPE_MASK ) ==INTR_TYPE_NMI_INTR )return 1 ;/* already handled by vmx_vcpu_run() */
@@ -3316,11 +3311,9 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)kvm_queue_exception_e (&vmx->vcpu, vector, err); }else kvm_queue_exception (&vmx->vcpu, vector); - vmx->idt_vectoring_info =0 ;break ;case INTR_TYPE_EXT_INTR :kvm_queue_interrupt (&vmx->vcpu, vector); - vmx->idt_vectoring_info =0 ;break ;default :break ;
PATCH 4/4 Fix task switching.
In reply to Gleb NatapovThe patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.
2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
---
arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm.c | 25 ++++++++++++++++++------- arch/x86/kvm/vmx.c | 40 +++++++++++++++++++++++++++++----------- arch/x86/kvm/x86.c | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 79 insertions(+), 27 deletions(-)
arch/x86/include/asm/svm.h +1 -0
@@ -225,6 +225,7 @@ struct __attribute__ ((__packed__)) vmcb {#define SVM_EVTINJ_VALID_ERR (1 << 11)#define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK +#define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK#define SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR#define SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
arch/x86/kvm/svm.c +18 -7
@@ -1823,17 +1823,28 @@ static int task_switch_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) {u16 tss_selector; +int reason; +int int_type = svm->vmcb->control.exit_int_info & +SVM_EXITINTINFO_TYPE_MASK ; tss_selector = (u16 )svm->vmcb->control.exit_info_1; +if (svm->vmcb->control.exit_info_2 & (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_IRET )) -return kvm_task_switch (&svm->vcpu, tss_selector, -TASK_SWITCH_IRET ); -if (svm->vmcb->control.exit_info_2 & - (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_JMP )) -return kvm_task_switch (&svm->vcpu, tss_selector, -TASK_SWITCH_JMP ); -return kvm_task_switch (&svm->vcpu, tss_selector,TASK_SWITCH_CALL ); + reason =TASK_SWITCH_IRET ; +else if (svm->vmcb->control.exit_info_2 & + (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_JMP )) + reason =TASK_SWITCH_JMP ; +else if (svm->vmcb->control.exit_int_info &SVM_EXITINTINFO_VALID ) + reason =TASK_SWITCH_GATE ; +else + reason =TASK_SWITCH_CALL ; + + +if (reason !=TASK_SWITCH_GATE || int_type ==SVM_EXITINTINFO_TYPE_SOFT ) +skip_emulated_instruction (&svm->vcpu); + +return kvm_task_switch (&svm->vcpu, tss_selector, reason); }static int cpuid_interception (struct vcpu_svm *svm,struct kvm_run *kvm_run)
arch/x86/kvm/vmx.c +29 -11
@@ -3025,22 +3025,40 @@ static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long exit_qualification;u16 tss_selector; -int reason; +int reason, type, idt_v; + + idt_v = (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ); + type = (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ); exit_qualification =vmcs_readl (EXIT_QUALIFICATION ); reason = (u32 )exit_qualification >>30 ; -if (reason ==TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected && - (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ) - ==INTR_TYPE_NMI_INTR ) { - vcpu->arch.nmi_injected = false; -if (cpu_has_virtual_nmis ()) -vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , -GUEST_INTR_STATE_NMI ); +if (reason ==TASK_SWITCH_GATE && idt_v) { +switch (type) { +case INTR_TYPE_NMI_INTR : + vcpu->arch.nmi_injected = false; +if (cpu_has_virtual_nmis ()) +vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , +GUEST_INTR_STATE_NMI ); +break ; +case INTR_TYPE_EXT_INTR : +kvm_clear_interrupt_queue (vcpu); +break ; +case INTR_TYPE_HARD_EXCEPTION : +case INTR_TYPE_SOFT_EXCEPTION : +kvm_clear_exception_queue (vcpu); +break ; +default : +break ; + } } tss_selector = exit_qualification; +if (!idt_v || (type !=INTR_TYPE_HARD_EXCEPTION && + type !=INTR_TYPE_EXT_INTR && + type !=INTR_TYPE_NMI_INTR )) +skip_emulated_instruction (vcpu); +if (!kvm_task_switch (vcpu, tss_selector, reason))return 0 ;
@@ -3292,8 +3310,8 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx) vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ; - -switch (type) { + +switch (type) {case INTR_TYPE_NMI_INTR : vmx->vcpu.arch.nmi_injected = true;/*
arch/x86/kvm/x86.c +31 -9
@@ -3697,7 +3697,6 @@ static void save_state_to_tss32(struct kvm_vcpu *vcpu, tss->fs =get_segment_selector (vcpu,VCPU_SREG_FS ); tss->gs =get_segment_selector (vcpu,VCPU_SREG_GS ); tss->ldt_selector =get_segment_selector (vcpu,VCPU_SREG_LDTR ); - tss->prev_task_link =get_segment_selector (vcpu,VCPU_SREG_TR ); } static int load_state_from_tss32(struct kvm_vcpu *vcpu,
@@ -3794,8 +3793,8 @@ static int load_state_from_tss16(struct kvm_vcpu *vcpu, }static int kvm_task_switch_16 (struct kvm_vcpu *vcpu,u16 tss_selector, -u32 old_tss_base, -struct desc_struct *nseg_desc) +u16 old_tss_sel,u32 old_tss_base, +struct desc_struct *nseg_desc) {struct tss_segment_16 tss_segment_16;int ret =0 ;
@@ -3814,6 +3813,16 @@ static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, &tss_segment_16, sizeof tss_segment_16)) goto out; +if (old_tss_sel !=0xffff ) { + tss_segment_16.prev_task_link = old_tss_sel; + +if (kvm_write_guest (vcpu->kvm, +get_tss_base_addr (vcpu, nseg_desc), + &tss_segment_16.prev_task_link, +sizeof tss_segment_16.prev_task_link)) + goto out; + } +if (load_state_from_tss16 (vcpu, &tss_segment_16)) goto out;
@@ -3823,7 +3832,7 @@ out: }static int kvm_task_switch_32 (struct kvm_vcpu *vcpu,u16 tss_selector, -u32 old_tss_base, +u16 old_tss_sel,u32 old_tss_base,struct desc_struct *nseg_desc) {struct tss_segment_32 tss_segment_32;
@@ -3843,6 +3852,16 @@ static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, &tss_segment_32, sizeof tss_segment_32)) goto out; +if (old_tss_sel !=0xffff ) { + tss_segment_32.prev_task_link = old_tss_sel; + +if (kvm_write_guest (vcpu->kvm, +get_tss_base_addr (vcpu, nseg_desc), + &tss_segment_32.prev_task_link, +sizeof tss_segment_32.prev_task_link)) + goto out; + } +if (load_state_from_tss32 (vcpu, &tss_segment_32)) goto out;
@@ -3896,14 +3915,17 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) kvm_x86_ops->set_rflags (vcpu, eflags & ~X86_EFLAGS_NT ); } - kvm_x86_ops->skip_emulated_instruction (vcpu); +/* set back link to prev task only if NT bit is set in eflags +note that old_tss_sel is not used afetr this point */ +if (reason !=TASK_SWITCH_CALL && reason !=TASK_SWITCH_GATE ) + old_tss_sel =0xffff ;if (nseg_desc.type &8 ) - ret =kvm_task_switch_32 (vcpu, tss_selector, old_tss_base, - &nseg_desc); + ret =kvm_task_switch_32 (vcpu, tss_selector, old_tss_sel, + old_tss_base, &nseg_desc);else - ret =kvm_task_switch_16 (vcpu, tss_selector, old_tss_base, - &nseg_desc); + ret =kvm_task_switch_16 (vcpu, tss_selector, old_tss_sel, + old_tss_base, &nseg_desc);if (reason ==TASK_SWITCH_CALL || reason ==TASK_SWITCH_GATE ) {u32 eflags = kvm_x86_ops->get_rflags (vcpu);
Re: [PATCH 2/4] Rewrite twisted maze of if() statements with more straightforward switch()
In reply to Gleb NatapovQuoted 2 lines · Gleb Natapov
Signed-off-by Gleb Natapov <[email protected]>
This is actually not just a rewrite, but also a bugfix:
Quoted 11 lines
INTR_INFO);arch/x86/kvm/vmx.c +6
-0@@ -3289,34 +3288,42 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx) vmx->vnmi_blocked_time +=ktime_to_ns (ktime_sub (ktime_get (), vmx->entry_time)); + vmx->vcpu.arch.nmi_injected = false; +kvm_clear_exception_queue (&vmx->vcpu); +kvm_clear_interrupt_queue (&vmx->vcpu); + +if (!idtv_info_valid) +return ;
arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 3305
Quoted 10 lines
arch/x86/kvm/vmx.c +5
-1+ vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ; -if (vmx->vcpu.arch.nmi_injected) { + +switch (type) { +case INTR_TYPE_NMI_INTR : + vmx->vcpu.arch.nmi_injected = true;/*
The existing code would leave nmi_injected == false if we exit on NMI_INTR, so we drop an NMI here.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/4] Fix task switching.
In reply to Gleb NatapovQuoted 9 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Looks good, but please split into (at least) two patches. Also please provide a test case so we don't regress again.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/4] Fix task switching.
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.
Quoted 8 lines
Quoted 4 lines
2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Looks good, but please split into (at least) two patches. Also please provide a test case so we don't regress again.
This what I am using for testing. After running make you should get kernel.bin that can be booted from grub. Runs on real HW too. I am planing to add more test.
user/test/x86/kvmtest/Makefile new file +33 -0
@@ -0,0 +1,33 @@ +CC=gcc +AS=gcc +CFLAGS=-m32 -I. -O2 -Wall +ASFLAGS=-m32 -I. +OBJS=kernel.o lib.o boot.o memory.o gdt.o idt.o isrs.o tss.o uart.o +ALLOBJS=$(OBJS) tests/tests.o + +PHONY := all +all: kernel.bin + $(MAKE) -C tests + +kernel.bin: $(ALLOBJS) kernel.ld + ld -T kernel.ld $(ALLOBJS) -o $@ + +install: kernel.bin + cp $< /boot/ + +tests/tests.o: + $(MAKE) -C tests + +-include $(OBJS:.o=.d) + +# compile and generate dependency info +%.o: %.c + gcc -c $(CFLAGS) $*.c -o $*.o + gcc -MM $(CFLAGS) $*.c > $*.d + +PHONY += clean +clean: + $(MAKE) -C tests + -rm *.o *~ *.d kernel.bin + +.PHONY: $(PHONY)
user/test/x86/kvmtest/boot.S new file +357 -0
357 lines not on this page · raw message
user/test/x86/kvmtest/gdt.c new file +84 -0
84 lines not on this page · raw message
user/test/x86/kvmtest/idt.c new file +47 -0
47 lines not on this page · raw message
user/test/x86/kvmtest/isrs.c new file +112 -0
112 lines not on this page · raw message
user/test/x86/kvmtest/kernel.c new file +194 -0
194 lines not on this page · raw message
user/test/x86/kvmtest/kernel.h new file +68 -0
68 lines not on this page · raw message
user/test/x86/kvmtest/kernel.ld new file +1 -0
@@ -0,0 +1,28 @@ +OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(start)
+SECTIONS
+{
+ . = 0x100000;
+ _kernel_start = .;
+ .text : {
+ *(.text)
+ }
+ .rodata : {
+ *(.rodata)
+ }
+ .data : {
+ *(.data)
+ }
+ .bss : {
+ *(.bss)
+ }
+ __tests_start = .;
+ .tests : {
+ *(.tests)
+ }
+ __tests_end = .;
+ . = ALIGN(4096);
+ _kernel_end = .;
+ /DISCARD/ : { *(.comment) }
+}
\ No newline at end of file
user/test/x86/kvmtest/lib.c new file +296 -0
296 lines not on this page · raw message
user/test/x86/kvmtest/lib.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef _LIB_H +#define _LIB_H +void cls (void ); +void itoa (char *buf,int base,int d); +void printk (uint16_t level,const char *format, ...); +void *memset (void *s,int c,size_t n); +char *strstr (const char *phaystack,const char *pneedle); +int atoi_base (const char *buf,int base); +static inline int atoi (const char *buf) +{ +return atoi_base (buf,10 ); +} +#endif
user/test/x86/kvmtest/memory.c new file +100 -0
100 lines not on this page · raw message
user/test/x86/kvmtest/memory.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef _MEMORY_H +#define _MEMORY_H +void kalloc_init (void *mem,size_t size); +void *kalloc (size_t size); +void kfree (void *ptr); +#endif
user/test/x86/kvmtest/multiboot.h new file +109 -0
109 lines not on this page · raw message
user/test/x86/kvmtest/tests/Makefile new file +22 -0
@@ -0,0 +1,22 @@ +CC=gcc +AS=gcc +CFLAGS=-m32 -I. -I.. -O2 -Wall +ASFLAGS=-m32 -I. -I.. +OBJS=tss_test.o + +PHONY := all +all: tests.o + +tests.o: $(OBJS) + ld -m elf_i386 -r $(OBJS) -o $@ + +-include $(OBJS:.o=.d) + +# compile and generate dependency info +%.o: %.c + gcc -c $(CFLAGS) $*.c -o $*.o + gcc -MM $(CFLAGS) $*.c > $*.d + +PHONY += clean +clean: + -rm *.o *~ *.d
user/test/x86/kvmtest/tests/tss_test.c new file +74 -0
74 lines not on this page · raw message
user/test/x86/kvmtest/tss.c new file +108 -0
108 lines not on this page · raw message
user/test/x86/kvmtest/uart.c new file +43 -0
43 lines not on this page · raw message
--
Gleb.
Re: [PATCH 2/4] Rewrite twisted maze of if() statements with more straightforward switch()
In reply to Avi KivityQuoted 23 lines · Avi Kivity
Quoted 2 lines · Gleb Natapov
Signed-off-by Gleb Natapov <[email protected]>This is actually not just a rewrite, but also a bugfix:
Quoted 17 lines
INTR_INFO);arch/x86/kvm/vmx.c +8
-1@@ -3289,34 +3288,42 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx) vmx->vnmi_blocked_time += ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); + vmx->vcpu.arch.nmi_injected = false; +kvm_clear_exception_queue (&vmx->vcpu); +kvm_clear_interrupt_queue (&vmx->vcpu); + +if (!idtv_info_valid) +return ; + vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ; -if (vmx->vcpu.arch.nmi_injected) { +
Quoted 9 lines
arch/x86/kvm/vmx.c static void vmx_complete_interrupts(struct vcpu_vmx *vmx) · line 3329
Quoted 5 lines
arch/x86/kvm/vmx.c +3
-0+switch (type) { +case INTR_TYPE_NMI_INTR : + vmx->vcpu.arch.nmi_injected = true;/* The existing code would leave
nmi_injected== false if we exit onNMI_INTR, so we drop an NMI here.
I think NMI_INTR and nmi_injected always go together. However, the rework looks good and more logical to me, too. Will see that I can give this (more precisely -v2) a try with our scenarios ASAP.
Jan
Re: [PATCH 4/4] Fix task switching.
In reply to Gleb NatapovQuoted 7 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Jan
Quoted 222 lines
Signed-off-by Gleb Natapov <[email protected]>---
arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm.c | 25 ++++++++++++++++++------- arch/x86/kvm/vmx.c | 40 +++++++++++++++++++++++++++++----------- arch/x86/kvm/x86.c | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 79 insertions(+), 27 deletions(-)arch/x86/include/asm/svm.h +1
-0@@ -225,6 +225,7 @@ struct __attribute__ ((__packed__)) vmcb {#define SVM_EVTINJ_VALID_ERR (1 << 11)#define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK +#define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK#define SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR#define SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMIarch/x86/kvm/svm.c +18
-7@@ -1823,17 +1823,28 @@ static int task_switch_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) {u16 tss_selector; +int reason; +int int_type = svm->vmcb->control.exit_int_info & +SVM_EXITINTINFO_TYPE_MASK ; tss_selector = (u16 )svm->vmcb->control.exit_info_1; +if (svm->vmcb->control.exit_info_2 & (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_IRET )) -return kvm_task_switch (&svm->vcpu, tss_selector, -TASK_SWITCH_IRET ); -if (svm->vmcb->control.exit_info_2 & - (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_JMP )) -return kvm_task_switch (&svm->vcpu, tss_selector, -TASK_SWITCH_JMP ); -return kvm_task_switch (&svm->vcpu, tss_selector,TASK_SWITCH_CALL ); + reason =TASK_SWITCH_IRET ; +else if (svm->vmcb->control.exit_info_2 & + (1ULL <<SVM_EXITINFOSHIFT_TS_REASON_JMP )) + reason =TASK_SWITCH_JMP ; +else if (svm->vmcb->control.exit_int_info &SVM_EXITINTINFO_VALID ) + reason =TASK_SWITCH_GATE ; +else + reason =TASK_SWITCH_CALL ; + + +if (reason !=TASK_SWITCH_GATE || int_type ==SVM_EXITINTINFO_TYPE_SOFT ) +skip_emulated_instruction (&svm->vcpu); + +return kvm_task_switch (&svm->vcpu, tss_selector, reason); }static int cpuid_interception (struct vcpu_svm *svm,struct kvm_run *kvm_run)arch/x86/kvm/vmx.c +29
-11@@ -3025,22 +3025,40 @@ static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long exit_qualification;u16 tss_selector; -int reason; +int reason, type, idt_v; + + idt_v = (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ); + type = (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ); exit_qualification =vmcs_readl (EXIT_QUALIFICATION ); reason = (u32 )exit_qualification >>30 ; -if (reason ==TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected && - (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ) - ==INTR_TYPE_NMI_INTR ) { - vcpu->arch.nmi_injected = false; -if (cpu_has_virtual_nmis ()) -vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , -GUEST_INTR_STATE_NMI ); +if (reason ==TASK_SWITCH_GATE && idt_v) { +switch (type) { +case INTR_TYPE_NMI_INTR : + vcpu->arch.nmi_injected = false; +if (cpu_has_virtual_nmis ()) +vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO , +GUEST_INTR_STATE_NMI ); +break ; +case INTR_TYPE_EXT_INTR : +kvm_clear_interrupt_queue (vcpu); +break ; +case INTR_TYPE_HARD_EXCEPTION : +case INTR_TYPE_SOFT_EXCEPTION : +kvm_clear_exception_queue (vcpu); +break ; +default : +break ; + } } tss_selector = exit_qualification; +if (!idt_v || (type !=INTR_TYPE_HARD_EXCEPTION && + type !=INTR_TYPE_EXT_INTR && + type !=INTR_TYPE_NMI_INTR )) +skip_emulated_instruction (vcpu); +if (!kvm_task_switch (vcpu, tss_selector, reason))return 0 ;@@ -3292,8 +3310,8 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx) vector = idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ; type = idt_vectoring_info &VECTORING_INFO_TYPE_MASK ; - -switch (type) { + +switch (type) {case INTR_TYPE_NMI_INTR : vmx->vcpu.arch.nmi_injected = true;/* arch/x86/kvm/x86.c +31
-10@@ -3697,7 +3697,6 @@ static void save_state_to_tss32(struct kvm_vcpu *vcpu, tss->fs =get_segment_selector (vcpu,VCPU_SREG_FS ); tss->gs =get_segment_selector (vcpu,VCPU_SREG_GS ); tss->ldt_selector =get_segment_selector (vcpu,VCPU_SREG_LDTR ); - tss->prev_task_link =get_segment_selector (vcpu,VCPU_SREG_TR ); } static int load_state_from_tss32(struct kvm_vcpu *vcpu,@@ -3794,8 +3793,8 @@ static int load_state_from_tss16(struct kvm_vcpu *vcpu, }static int kvm_task_switch_16 (struct kvm_vcpu *vcpu,u16 tss_selector, -u32 old_tss_base, -struct desc_struct *nseg_desc) +u16 old_tss_sel,u32 old_tss_base, +struct desc_struct *nseg_desc) {struct tss_segment_16 tss_segment_16;int ret =0 ;@@ -3814,6 +3813,16 @@ static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, &tss_segment_16, sizeof tss_segment_16)) goto out; +if (old_tss_sel !=0xffff ) { + tss_segment_16.prev_task_link = old_tss_sel; + +if (kvm_write_guest (vcpu->kvm, +get_tss_base_addr (vcpu, nseg_desc), + &tss_segment_16.prev_task_link, +sizeof tss_segment_16.prev_task_link)) + goto out; + } +if (load_state_from_tss16 (vcpu, &tss_segment_16)) goto out;@@ -3823,7 +3832,7 @@ out: }static int kvm_task_switch_32 (struct kvm_vcpu *vcpu,u16 tss_selector, -u32 old_tss_base, +u16 old_tss_sel,u32 old_tss_base,struct desc_struct *nseg_desc) {struct tss_segment_32 tss_segment_32;@@ -3843,6 +3852,16 @@ static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, &tss_segment_32, sizeof tss_segment_32)) goto out; +if (old_tss_sel !=0xffff ) { + tss_segment_32.prev_task_link = old_tss_sel; + +if (kvm_write_guest (vcpu->kvm, +get_tss_base_addr (vcpu, nseg_desc), + &tss_segment_32.prev_task_link, +sizeof tss_segment_32.prev_task_link)) + goto out; + } +if (load_state_from_tss32 (vcpu, &tss_segment_32)) goto out;@@ -3896,14 +3915,17 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) kvm_x86_ops->set_rflags (vcpu, eflags & ~X86_EFLAGS_NT ); } - kvm_x86_ops->skip_emulated_instruction (vcpu); +/* set back link to prev task only if NT bit is set in eflags +note that old_tss_sel is not used afetr this point */ +if (reason !=TASK_SWITCH_CALL && reason !=TASK_SWITCH_GATE ) + old_tss_sel =0xffff ;if (nseg_desc.type &8 ) - ret =kvm_task_switch_32 (vcpu, tss_selector, old_tss_base, - &nseg_desc); + ret =kvm_task_switch_32 (vcpu, tss_selector, old_tss_sel, + old_tss_base, &nseg_desc);else - ret =kvm_task_switch_16 (vcpu, tss_selector, old_tss_base, - &nseg_desc); + ret =kvm_task_switch_16 (vcpu, tss_selector, old_tss_sel, + old_tss_base, &nseg_desc);if (reason ==TASK_SWITCH_CALL || reason ==TASK_SWITCH_GATE ) {u32 eflags = kvm_x86_ops->get_rflags (vcpu); --To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 4/4] Fix task switching.
Quoted 11 lines · Jan Kiszka
Quoted 7 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Haven't tried. I wrote my own tests for task switching. How can I check it?
--
Gleb.
Re: [PATCH 4/4] Fix task switching.
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Jan Kiszka
Quoted 5 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Haven't tried. I wrote my own tests for task switching. How can I check it?
There is a test case attached to Julian's sourceforge-reported bug:
https://sourceforge.net/tracker/?func=detail&atid=893831&aid=2681442&group_id=180599
And I guess Thomas or Bernhard will be happy to give it a try, too... :)
There was one issue, the IRQ injection bug [1] which was related to IRQ tasks IIRC. Thomas and I finally suspected after a private chat that there is actually a different reason behind it, something like interrupt.pending should be cleared when the injection took place via an (emulated) task switch. Any news on this, Thomas?
Jan
[1] http://permalink.gmane.org/gmane.comp.emulators.kvm.devel/29288
Re: [PATCH 4/4] Fix task switching.
Quoted 10 lines · Jan Kiszka
Quoted 9 lines · Gleb Natapov
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by an external even.2 is currently solved only for VMX since there is not reliable way to skip an instruction in SVM. We should emulate it instead.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Haven't tried. I wrote my own tests for task switching. How can I check it?
There is a test case attached to Julian's sourceforge-reported bug:
https://sourceforge.net/tracker/?func=detail&atid=893831&aid=2681442&group_id=180599
I'll try that.
Quoted 8 lines
And I guess Thomas or Bernhard will be happy to give it a try, too... :)
There was one issue, the IRQ injection bug [1] which was related to IRQ tasks IIRC. Thomas and I finally suspected after a private chat that there is actually a different reason behind it, something like interrupt.pending should be cleared when the injection took place via an (emulated) task switch. Any news on this, Thomas?
If this is the case then the patch series should fix it.
Quoted 4 lines
Jan
[1] http://permalink.gmane.org/gmane.comp.emulators.kvm.devel/29288
--
Gleb.
Re: [PATCH 4/4] Fix task switching.
In reply to Jan KiszkaQuoted 7 lines · Jan Kiszka
Quoted 2 lines
Haven't tried. I wrote my own tests for task switching. How can I check it?
There is a test case attached to Julian's sourceforge-reported bug:
https://sourceforge.net/tracker/?func=detail&atid=893831&aid=2681442&group_id=180599
Works for me.
--
Gleb.
Re: [PATCH 4/4] Fix task switching.
Quoted 9 lines · Gleb Natapov
Quoted 7 lines · Jan Kiszka
Quoted 2 lines
Haven't tried. I wrote my own tests for task switching. How can I check it?
There is a test case attached to Julian's sourceforge-reported bug:
https://sourceforge.net/tracker/?func=detail&atid=893831&aid=2681442&group_id=180599
Works for me.
Then the patches should be fine (at least for me *g*).
Regards,
Signature · 9 lines
Julian Stecklina The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners - Ernst Jan Plugge -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 4/4] Fix task switching.
In reply to Jan KiszkaQuoted 8 lines · Jan Kiszka
Quoted 4 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason for task switch is a task gate in IDT and access to it is caused by anexternal even.
Quoted 1 line
Quoted 8 lines
Quoted 1 line
2 is currently solved only for VMX since there is not
reliable way to
Quoted 1 line
skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Jan
I will try this today. Thanks.
Bernhard
RE: [PATCH 4/4] Fix task switching.
Quoted 13 lines · Bernhard Kohl
Quoted 5 lines · Jan Kiszka
Quoted 3 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason fortask switch
Quoted 5 lines
Quoted 1 line
is a task gate in IDT and access to it is caused by an
external even.
Quoted 2 lines
2 is currently solved only for VMX since there is not
reliable way to
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Jan
I will try this today. Thanks.
Yes, it works for us (Thomas + Bernhard).
Bernhard
Re: [PATCH 4/4] Fix task switching.
Quoted 16 lines · Bernhard (NSN - DE/Munich)
Quoted 15 lines · Bernhard Kohl
Quoted 5 lines · Jan Kiszka
Quoted 3 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason fortask switch
Quoted 7 lines
Quoted 1 line
is a task gate in IDT and access to it is caused by an
external even.
Quoted 2 lines
2 is currently solved only for VMX since there is not
reliable way to
Quoted 1 line
skip an instruction in SVM. We should emulate it instead.
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Jan
I will try this today. Thanks.
Yes, it works for us (Thomas + Bernhard).
Great. Thanks for testing.
--
Gleb.
Re: [PATCH 4/4] Fix task switching.
Quoted 16 lines · Gleb Natapov
Quoted 15 lines · Bernhard (NSN - DE/Munich)
Quoted 14 lines · Bernhard Kohl
Quoted 4 lines · Jan Kiszka
Quoted 3 lines · Gleb Natapov
The patch fixes two problems with task switching.
1. Back link is written to a wrong TSS.
2. Instruction emulation is not needed if the reason fortask switch
Quoted 8 lines
Quoted 1 line
is a task gate in IDT and access to it is caused by an
external even.
Quoted 1 line
2 is currently solved only for VMX since there is not
reliable way to
Quoted 1 line
skip an instruction in SVM. We should emulate it instead.
Does this series fix all issues Bernhard, Thomas and Julian stumbled over?
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Jan
I will try this today. Thanks.
Yes, it works for us (Thomas + Bernhard).
Great. Thanks for testing.
Same here: No obvious regressions found while running various NMI/IRQ tests.
Jan