Skip to contentSkip to conversation

KVM: VMX: enable acknowledge interupt on vmexit

PATCH Superseded Yang Zhang 2 participants 5 messages Jan 16, 2013 – Jan 17, 2013

Subsystems (3)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

PATCH 1/1 KVM: VMX: enable acknowledge interupt on vmexit

From: Yang Zhang <[email protected]>

The "acknowledge interrupt on exit" feature controls processor behavior for external interrupt acknowledgement. When this control is set, the processor acknowledges the interrupt controller to acquire the interrupt vector on VM exit.

This feature is required by Posted Interrupt. It will be turnned on only when posted interrupt is enabled.

Refer to Intel SDM volum 3, chapter 33.2.

---
 arch/x86/kvm/vmx.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +49 -2
index dd2a85c..d1ed9ae 100644
@@ -2565,7 +2565,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 #ifdef CONFIG_X86_64
 	min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
 #endif
-	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
+	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_ACK_INTR_ON_EXIT;
 	if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
 				&_vmexit_control) < 0)
 		return -EIO;
@@ -3926,7 +3926,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 		++vmx->nmsrs;
 	}
 
-	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl & ~VM_EXIT_ACK_INTR_ON_EXIT);
 
 	/* 22.2.1, 20.8.1 */
 	vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
@@ -6096,6 +6096,52 @@ static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
 	}
 }
 
+
+static noinline void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
+{
+	u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
+	if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_EXT_INTR &&
+	    (exit_intr_info & INTR_INFO_VALID_MASK) ) {
+		unsigned int vector;
+		unsigned long entry;
+		struct desc_ptr dt;
+		gate_desc *desc;
+
+		native_store_idt(&dt);
+
+		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		desc = (void *)dt.address + vector * 16;
+
+		entry = gate_offset(*desc);
+		asm(
+			"mov %0, %%" _ASM_DX "\n\t"
+			"mov %%" _ASM_SP ", %%" _ASM_BX "\n\t"
+#ifdef CONFIG_X86_64
+			"and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
+#endif
+			"mov %%ss, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push %%" _ASM_BX "\n\t"
+			"pushf\n\t"
+			"mov %%cs, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push intr_return\n\t"
+			"jmp *%% " _ASM_DX "\n\t"
+			".pushsection .rodata \n\t"
+			".global intr_return \n\t"
+			"intr_return: " _ASM_PTR " 1b \n\t"
+			".popsection\n\t"
+			: :"m"(entry) : 
+#ifdef CONFIG_X86_64
+			"rax", "rbx", "rdx"
+#else
+			"eax", "ebx", "edx"
+#endif
+			);
+	}
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;
@@ -6431,6 +6477,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	vmx_complete_atomic_exit(vmx);
 	vmx_recover_nmi_blocking(vmx);
 	vmx_complete_interrupts(vmx);
+	vmx_handle_external_intr(vcpu);
 }
 
 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
Signature · 1 line
1.7.1
#1 lore raw
Quoted 10 lines · Yang Zhang

From: Yang Zhang <[email protected]>

The "acknowledge interrupt on exit" feature controls processor behavior for external interrupt acknowledgement. When this control is set, the processor acknowledges the interrupt controller to acquire the interrupt vector on VM exit.

This feature is required by Posted Interrupt. It will be turnned on only when posted interrupt is enabled.

Why? Always enable it. It is faster than current approach.

Quoted 2 lines

Refer to Intel SDM volum 3, chapter 33.2.

Please CC H. Peter Anvin <[email protected]> on the next version.

Quoted 28 lines
---
 arch/x86/kvm/vmx.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +2 -2
index dd2a85c..d1ed9ae 100644
@@ -2565,7 +2565,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 #ifdef CONFIG_X86_64
 	min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
 #endif
-	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
+	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_ACK_INTR_ON_EXIT;
 	if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
 				&_vmexit_control) < 0)
 		return -EIO;
@@ -3926,7 +3926,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 		++vmx->nmsrs;
 	}
 
-	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl & ~VM_EXIT_ACK_INTR_ON_EXIT);
 
 	/* 22.2.1, 20.8.1 */
 	vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6105

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
@@ -6096,6 +6096,52 @@ static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
 	}
 }
 
+
+static noinline void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
+{
+	u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
+
+	if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_EXT_INTR &&
+	    (exit_intr_info & INTR_INFO_VALID_MASK) ) {

if (exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK) == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR))

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6111

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+		unsigned int vector;
+		unsigned long entry;
+		struct desc_ptr dt;
+		gate_desc *desc;
+
+		native_store_idt(&dt);

This does not change. Store it in vcpu during vcpu creation instead of reading it each time.

Quoted 4 lines
arch/x86/kvm/vmx.c +4 -0
+
+		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		desc = (void *)dt.address + vector * 16;
+

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6125

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		entry = gate_offset(*desc);
+		asm(
+			"mov %0, %%" _ASM_DX "\n\t"
+			"mov %%" _ASM_SP ", %%" _ASM_BX "\n\t"
+#ifdef CONFIG_X86_64
+			"and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
+#endif
+			"mov %%ss, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push %%" _ASM_BX "\n\t"

For 32bit you do not need to save ss:esp.

Quoted 23 lines
arch/x86/kvm/vmx.c +19 -0
+			"pushf\n\t"
+			"mov %%cs, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push intr_return\n\t"
+			"jmp *%% " _ASM_DX "\n\t"
+			".pushsection .rodata \n\t"
+			".global intr_return \n\t"
+			"intr_return: " _ASM_PTR " 1b \n\t"
+			".popsection\n\t"
+			: :"m"(entry) : 
+#ifdef CONFIG_X86_64
+			"rax", "rbx", "rdx"
+#else
+			"eax", "ebx", "edx"
+#endif
+			);
+	}
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · line 6480

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
@@ -6431,6 +6477,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	vmx_complete_atomic_exit(vmx);
 	vmx_recover_nmi_blocking(vmx);
 	vmx_complete_interrupts(vmx);
+	vmx_handle_external_intr(vcpu);

This should be done just before enabling interrupts, otherwise we are taking an interrupt before vmexit is fully complete.

--
Gleb.

#2 lore raw

Gleb Natapov wrote on 2013-01-16:

Quoted 8 lines · Yang Zhang
Quoted 7 lines

From: Yang Zhang <[email protected]>

The "acknowledge interrupt on exit" feature controls processor behavior for external interrupt acknowledgement. When this control is set, the processor acknowledges the interrupt controller to acquire the interrupt vector on VM exit.

Quoted 4 lines
Quoted 3 lines

This feature is required by Posted Interrupt. It will be turnned on only when posted interrupt is enabled.

Why? Always enable it. It is faster than current approach.

I don't think it will faster. It should be same.

Quoted 41 lines
Quoted 2 lines

Refer to Intel SDM volum 3, chapter 33.2.

Please CC H. Peter Anvin <[email protected]> on the next version.

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct

Quoted 10 lines
---
 arch/x86/kvm/vmx.c |   51

+++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 49 insertions(+), 2 deletions(-)

arch/x86/kvm/vmx.c +0 -0
index dd2a85c..d1ed9ae 100644
@@ -2565,7 +2565,7 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -1
 #ifdef CONFIG_X86_64
 	min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
 #endif
-	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
+	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |

VM_EXIT_ACK_INTR_ON_EXIT;

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -1
 	if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
 				&_vmexit_control) < 0) 		return -EIO; @@ -3926,7 +3926,7 @@ static
 int vmx_vcpu_setup(struct vcpu_vmx *vmx) 		++vmx->nmsrs; 	}
-	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl &

~VM_EXIT_ACK_INTR_ON_EXIT);

arch/x86/kvm/vmx.c +0 -0
 	/* 22.2.1, 20.8.1 */
 	vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
@@ -6096,6 +6096,52 @@ static void vmx_complete_atomic_exit(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6105

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
 	}
 }
+ +static noinline void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
+{ +	u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); + +	if

((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_EXT_INTR &&

arch/x86/kvm/vmx.c +1 -0
+	    (exit_intr_info & INTR_INFO_VALID_MASK) ) {

if (exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK) == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR))

Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6111

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+		unsigned int vector;
+		unsigned long entry;
+		struct desc_ptr dt;
+		gate_desc *desc;
+
+		native_store_idt(&dt);

This does not change. Store it in vcpu during vcpu creation instead of reading it each time.

Right. It will reduce the cost.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+
+		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		desc = (void *)dt.address + vector * 16;
+
+		entry = gate_offset(*desc);
+		asm(
+			"mov %0, %%" _ASM_DX "\n\t"
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6125

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+			"mov %%" _ASM_SP ", %%" _ASM_BX "\n\t"
+#ifdef CONFIG_X86_64
+			"and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
+#endif
+			"mov %%ss, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push %%" _ASM_BX "\n\t"

For 32bit you do not need to save ss:esp.

You are right. No need to save it for 32bit.

Quoted 24 lines

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct

Quoted 23 lines
arch/x86/kvm/vmx.c +19 -0
+			"pushf\n\t"
+			"mov %%cs, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push intr_return\n\t"
+			"jmp *%% " _ASM_DX "\n\t"
+			".pushsection .rodata \n\t"
+			".global intr_return \n\t"
+			"intr_return: " _ASM_PTR " 1b \n\t"
+			".popsection\n\t"
+			: :"m"(entry) :
+#ifdef CONFIG_X86_64
+			"rax", "rbx", "rdx"
+#else
+			"eax", "ebx", "edx"
+#endif
+			);
+	}
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;
@@ -6431,6 +6477,7 @@ static void __noclone vmx_vcpu_run(struct

kvm_vcpu *vcpu)

Quoted 6 lines

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · line 6480

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
 	vmx_complete_atomic_exit(vmx);
 	vmx_recover_nmi_blocking(vmx);
 	vmx_complete_interrupts(vmx);
+	vmx_handle_external_intr(vcpu);

This should be done just before enabling interrupts, otherwise we are taking an interrupt before vmexit is fully complete.

How about to put it just after set vcpu->mode to OUTSIDE_GUEST_MODE? At that point, the vmexit is considered as fully complete.

Best regards,
Yang

#3 lore raw
Quoted 9 lines · Yang Z

Gleb Natapov wrote on 2013-01-16:

Quoted 8 lines · Yang Zhang
Quoted 7 lines

From: Yang Zhang <[email protected]>

The "acknowledge interrupt on exit" feature controls processor behavior for external interrupt acknowledgement. When this control is set, the processor acknowledges the interrupt controller to acquire the interrupt vector on VM exit.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

This feature is required by Posted Interrupt. It will be turnned on only when posted interrupt is enabled.

Why? Always enable it. It is faster than current approach.

I don't think it will faster. It should be same.

Interrupt delivered->vmexit->interrupt enabled->interrupt redelivered->acked->dispatched through idt.

vs

Interrupt delivered->acked->vmexit->dispatched through idt.

Anyway even if it is not slower it is better to use the same code path to make sure it is well tested.

Quoted 92 lines
Quoted 41 lines
Quoted 2 lines

Refer to Intel SDM volum 3, chapter 33.2.

Please CC H. Peter Anvin <[email protected]> on the next version.

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct

Quoted 10 lines
---
 arch/x86/kvm/vmx.c |   51

+++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 49 insertions(+), 2 deletions(-)

arch/x86/kvm/vmx.c +0 -0
index dd2a85c..d1ed9ae 100644
@@ -2565,7 +2565,7 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -1
 #ifdef CONFIG_X86_64
 	min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
 #endif
-	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
+	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |

VM_EXIT_ACK_INTR_ON_EXIT;

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -1
 	if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
 				&_vmexit_control) < 0) 		return -EIO; @@ -3926,7 +3926,7 @@ static
 int vmx_vcpu_setup(struct vcpu_vmx *vmx) 		++vmx->nmsrs; 	}
-	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl &

~VM_EXIT_ACK_INTR_ON_EXIT);

arch/x86/kvm/vmx.c +0 -0
 	/* 22.2.1, 20.8.1 */
 	vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
@@ -6096,6 +6096,52 @@ static void vmx_complete_atomic_exit(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6105

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
 	}
 }
+ +static noinline void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
+{ +	u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); + +	if

((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_EXT_INTR &&

arch/x86/kvm/vmx.c +1 -0
+	    (exit_intr_info & INTR_INFO_VALID_MASK) ) {

if (exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK) == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR))

Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6111

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+		unsigned int vector;
+		unsigned long entry;
+		struct desc_ptr dt;
+		gate_desc *desc;
+
+		native_store_idt(&dt);

This does not change. Store it in vcpu during vcpu creation instead of reading it each time.

Right. It will reduce the cost.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+
+		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		desc = (void *)dt.address + vector * 16;
+
+		entry = gate_offset(*desc);
+		asm(
+			"mov %0, %%" _ASM_DX "\n\t"
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6125

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+			"mov %%" _ASM_SP ", %%" _ASM_BX "\n\t"
+#ifdef CONFIG_X86_64
+			"and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
+#endif
+			"mov %%ss, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push %%" _ASM_BX "\n\t"

For 32bit you do not need to save ss:esp.

You are right. No need to save it for 32bit.

Quoted 24 lines

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct

Quoted 23 lines
arch/x86/kvm/vmx.c +19 -0
+			"pushf\n\t"
+			"mov %%cs, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push intr_return\n\t"
+			"jmp *%% " _ASM_DX "\n\t"
+			".pushsection .rodata \n\t"
+			".global intr_return \n\t"
+			"intr_return: " _ASM_PTR " 1b \n\t"
+			".popsection\n\t"
+			: :"m"(entry) :
+#ifdef CONFIG_X86_64
+			"rax", "rbx", "rdx"
+#else
+			"eax", "ebx", "edx"
+#endif
+			);
+	}
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;
@@ -6431,6 +6477,7 @@ static void __noclone vmx_vcpu_run(struct

kvm_vcpu *vcpu)

Quoted 8 lines
Quoted 6 lines

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · line 6480

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
 	vmx_complete_atomic_exit(vmx);
 	vmx_recover_nmi_blocking(vmx);
 	vmx_complete_interrupts(vmx);
+	vmx_handle_external_intr(vcpu);

This should be done just before enabling interrupts, otherwise we are taking an interrupt before vmexit is fully complete.

How about to put it just after set vcpu->mode to OUTSIDE_GUEST_MODE? At that point, the vmexit is considered as fully complete.

As I said, right before local_irq_enable().

--
Gleb.

#4 lore raw

Gleb Natapov wrote on 2013-01-17:

Quoted 19 lines · Yang Z
Quoted 9 lines

Gleb Natapov wrote on 2013-01-16:

Quoted 8 lines · Yang Zhang
Quoted 7 lines

From: Yang Zhang <[email protected]>

The "acknowledge interrupt on exit" feature controls processor behavior for external interrupt acknowledgement. When this control is set, the processor acknowledges the interrupt controller to acquire the interrupt vector on VM exit.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

This feature is required by Posted Interrupt. It will be turnned on only when posted interrupt is enabled.

Why? Always enable it. It is faster than current approach.

I don't think it will faster. It should be same.

Interrupt delivered->vmexit->interrupt enabled->interrupt redelivered->acked->dispatched through idt.

Quoted 6 lines

vs

Interrupt delivered->acked->vmexit->dispatched through idt.

Anyway even if it is not slower it is better to use the same code path to make sure it is well tested.

Ok.

Quoted 100 lines
Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

Refer to Intel SDM volum 3, chapter 33.2.

Please CC H. Peter Anvin <[email protected]> on the next version.

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct

Quoted 10 lines
---
 arch/x86/kvm/vmx.c |   51

+++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 49 insertions(+), 2 deletions(-)

arch/x86/kvm/vmx.c +0 -0
index dd2a85c..d1ed9ae 100644
@@ -2565,7 +2565,7 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -1
 #ifdef CONFIG_X86_64
 	min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
 #endif
-	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
+	opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |

VM_EXIT_ACK_INTR_ON_EXIT;

Quoted 3 lines
Quoted 3 lines

arch/x86/kvm/vmx.c static __init int setup_vmcs_config(struct · line 2571

Quoted 3 lines
arch/x86/kvm/vmx.c +0 -0
 	if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
 				&_vmexit_control) < 0) 		return -EIO; @@ -3926,7 +3926,7 @@
 static int vmx_vcpu_setup(struct vcpu_vmx *vmx) 		++vmx->nmsrs;
	}
Quoted 75 lines
Quoted 18 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct

Quoted 7 lines
arch/x86/kvm/vmx.c +1 -1
-	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+	vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl &

~VM_EXIT_ACK_INTR_ON_EXIT);

arch/x86/kvm/vmx.c +0 -0
 	/* 22.2.1, 20.8.1 */
 	vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
@@ -6096,6 +6096,52 @@ static void vmx_complete_atomic_exit(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct · line 6102

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
 	}
 }
+ +static noinline void vmx_handle_external_intr(struct kvm_vcpu
 *vcpu) +{ +	u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); +
+	if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) ==
 INTR_TYPE_EXT_INTR && +	    (exit_intr_info & INTR_INFO_VALID_MASK) )
 {

if (exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK) == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR))

Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6111

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+		unsigned int vector;
+		unsigned long entry;
+		struct desc_ptr dt;
+		gate_desc *desc;
+
+		native_store_idt(&dt);

This does not change. Store it in vcpu during vcpu creation instead of reading it each time.

Right. It will reduce the cost.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+
+		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		desc = (void *)dt.address + vector * 16;
+
+		entry = gate_offset(*desc);
+		asm(
+			"mov %0, %%" _ASM_DX "\n\t"
Quoted 8 lines

arch/x86/kvm/vmx.c static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) · line 6125

Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+			"mov %%" _ASM_SP ", %%" _ASM_BX "\n\t"
+#ifdef CONFIG_X86_64
+			"and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
+#endif
+			"mov %%ss, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push %%" _ASM_BX "\n\t"

For 32bit you do not need to save ss:esp.

You are right. No need to save it for 32bit.

Quoted 30 lines

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct

Quoted 23 lines
arch/x86/kvm/vmx.c +19 -0
+			"pushf\n\t"
+			"mov %%cs, %%" _ASM_AX "\n\t"
+			"push %%" _ASM_AX "\n\t"
+			"push intr_return\n\t"
+			"jmp *%% " _ASM_DX "\n\t"
+			".pushsection .rodata \n\t"
+			".global intr_return \n\t"
+			"intr_return: " _ASM_PTR " 1b \n\t"
+			".popsection\n\t"
+			: :"m"(entry) :
+#ifdef CONFIG_X86_64
+			"rax", "rbx", "rdx"
+#else
+			"eax", "ebx", "edx"
+#endif
+			);
+	}
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;
@@ -6431,6 +6477,7 @@ static void __noclone vmx_vcpu_run(struct

kvm_vcpu *vcpu)

arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · line 6480

Quoted 4 lines
arch/x86/kvm/vmx.c +1 -0
 	vmx_complete_atomic_exit(vmx);
 	vmx_recover_nmi_blocking(vmx);
 	vmx_complete_interrupts(vmx);
+	vmx_handle_external_intr(vcpu);

This should be done just before enabling interrupts, otherwise we are taking an interrupt before vmexit is fully complete.

Quoted 4 lines
Quoted 3 lines

How about to put it just after set vcpu->mode to OUTSIDE_GUEST_MODE? At that point, the vmexit is considered as fully complete.

As I said, right before local_irq_enable().

Ok.

Best regards,
Yang