Skip to contentSkip to conversation

x86, apicv: Add APIC virtualization support

PATCH Superseded Yang Zhang 4 participants 45 messages Dec 3, 2012 – Dec 7, 2012

Subsystems (3)

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

v3 cover x86, apicv: Add APIC virtualization support

APIC virtualization is a new feature which can eliminate most of VM exit when vcpu handles a interrupt:

APIC register virtualization:
APIC read access doesn't cause APIC-access VM exits. APIC write becomes trap-like.

Virtual interrupt delivery:
Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware.

Please refer to Intel SDM volume 3, chapter 29 for more details.

Changes v2 to v3:
* Drop Posted Interrupt patch from v3.
According Gleb's suggestion, we will use global vector for all VCPUs as notification event vector. So we will rewrite the Posted Interrupt patch. And resend it later.
* Use TMR to set the eoi exiting bitmap. We only want to set eoi exiting bitmap for the interrupt which is level trigger or has notifier in EOI write path. So TMR is enough to distinguish the interrupt trigger mode.
* Simplify some code according Gleb's comments.
* rebased on top of KVM upstream.

Changes v1 to v2:
* Add Posted Interrupt support in this series patch.
* Since there is a notifer hook in vAPIC EOI for PIT interrupt. So always Set PIT interrupt in eoi exit bitmap to force vmexit when EOI to interrupt.
* Rebased on top of KVM upstream

Yang Zhang (4):
x86: PIT connects to pin 2 of IOAPIC
x86, apicv: add APICv register virtualization support x86, apicv: add virtual interrupt delivery support x86, apicv: add virtual x2apic support

 arch/x86/include/asm/kvm_host.h |    4 +
 arch/x86/include/asm/vmx.h      |   13 ++
 arch/x86/kvm/irq.c              |   53 ++++++---
 arch/x86/kvm/lapic.c            |   72 +++++++++++--
 arch/x86/kvm/lapic.h            |    8 ++
 arch/x86/kvm/svm.c              |   19 +++
 arch/x86/kvm/vmx.c              |  232 +++++++++++++++++++++++++++++++++++++--
 arch/x86/kvm/x86.c              |   34 +++++-
 virt/kvm/ioapic.c               |    3 +-
 9 files changed, 398 insertions(+), 40 deletions(-)
author
#1 lore raw

PATCH v3 1/4 x86: PIT connects to pin 2 of IOAPIC

When PIT connects to IOAPIC, it route to pin 2 not pin 0.

---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c +1 -1
index cfb7e4d..166c450 100644
@@ -181,7 +181,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 
 #ifdef CONFIG_X86
 	/* Always delivery PIT interrupt to vcpu 0 */
-	if (irq == 0) {
+	if (irq == 2) {
 		irqe.dest_mode = 0; /* Physical mode. */
 		/* need to read apic_id from apic regiest since
 		 * it can be rewritten */
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH v3 2/4 x86, apicv: add APICv register virtualization support

In reply to Yang Zhang

- APIC read doesn't cause VM-Exit
- APIC write becomes trap-like

---
 arch/x86/include/asm/vmx.h |    2 ++
 arch/x86/kvm/lapic.c       |   16 ++++++++++++++++
 arch/x86/kvm/lapic.h       |    2 ++
 arch/x86/kvm/vmx.c         |   32 +++++++++++++++++++++++++++++++-
 4 files changed, 51 insertions(+), 1 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
index 36ec21c..21101b6 100644
@@ -66,6 +66,7 @@
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
 #define EXIT_REASON_XSETBV              55
+#define EXIT_REASON_APIC_WRITE          56
 #define EXIT_REASON_INVPCID             58
 
 #define VMX_EXIT_REASONS \
@@ -141,6 +142,7 @@
 #define SECONDARY_EXEC_ENABLE_VPID              0x00000020
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
+#define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
arch/x86/kvm/lapic.c +16 -0
index 9392f52..7c96012 100644
@@ -1212,6 +1212,22 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
 
+/* emulate APIC access in a trap manner */
+int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
+{
+	u32 val = 0;
+
+	/* hw has done the conditional check and inst decode */
+	offset &= 0xff0;
+	if ((offset != APIC_EOI) &&
+	     apic_reg_read(vcpu->arch.apic, offset, 4, &val))
+		return 1;
+
+	/* TODO: optimize to just emulate side effect w/o one more write */
+	return apic_reg_write(vcpu->arch.apic, offset, val);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
+
 void kvm_free_lapic(struct kvm_vcpu *vcpu)
 {
 	struct kvm_lapic *apic = vcpu->arch.apic;
arch/x86/kvm/lapic.h +2 -0
index e5ebf9f..c42f111 100644
@@ -64,6 +64,8 @@ int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
 u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
+int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
 void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx.c +31 -1
index 2fd2046..6a5f651 100644
@@ -83,6 +83,9 @@ module_param(vmm_exclusive, bool, S_IRUGO);
 static bool __read_mostly fasteoi = 1;
 module_param(fasteoi, bool, S_IRUGO);
 
+static bool __read_mostly enable_apicv_reg;
+module_param(enable_apicv_reg, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -761,6 +764,12 @@ static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
 		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
 }
 
+static inline bool cpu_has_vmx_apic_register_virt(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_APIC_REGISTER_VIRT;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2498,7 +2507,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 			SECONDARY_EXEC_UNRESTRICTED_GUEST |
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
-			SECONDARY_EXEC_ENABLE_INVPCID;
+			SECONDARY_EXEC_ENABLE_INVPCID |
+			SECONDARY_EXEC_APIC_REGISTER_VIRT;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2509,6 +2519,11 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 				SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
 		_cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
 #endif
+
+	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
+		_cpu_based_2nd_exec_control &= ~(
+				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
 		/* CR3 accesses and invlpg don't need to cause VM Exits when EPT
 		   enabled */
@@ -2706,6 +2721,9 @@ static __init int hardware_setup(void)
 	if (!cpu_has_vmx_ple())
 		ple_gap = 0;
 
+	if (!cpu_has_vmx_apic_register_virt())
+		enable_apicv_reg = 0;
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
 
@@ -3819,6 +3837,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 		exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
 	if (!ple_gap)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
+	if (!enable_apicv_reg)
+		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
 	return exec_control;
 }
 
@@ -4786,6 +4806,15 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
 
+static int handle_apic_write(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	u32 offset = exit_qualification & 0xfff;
+
+	/* APIC-write VM exit is trap-like and thus no need to adjust IP */
+	return kvm_apic_write_nodecode(vcpu, offset) == 0;
+}
+
 static int handle_task_switch(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -5725,6 +5754,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_VMON]                    = handle_vmon,
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
+	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
Signature · 1 line
1.7.1
author
#11 lore raw

PATCH v3 3/4 x86, apicv: add virtual interrupt delivery support

In reply to Yang Zhang

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.

- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
 arch/x86/include/asm/kvm_host.h |    4 +
 arch/x86/include/asm/vmx.h      |   11 +++
 arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++
 arch/x86/kvm/svm.c              |   19 +++++
 arch/x86/kvm/vmx.c              |  140 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |   34 ++++++++--
 virt/kvm/ioapic.c               |    1 +
 9 files changed, 291 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index dc87b65..e5352c8 100644
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +11 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41
 #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
 #define EXIT_REASON_APIC_ACCESS         44
+#define EXIT_REASON_EOI_INDUCED         45
 #define EXIT_REASON_EPT_VIOLATION       48
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
@@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
 #define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
+#define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a,
 	GUEST_LDTR_SELECTOR             = 0x0000080c,
 	GUEST_TR_SELECTOR               = 0x0000080e,
+	GUEST_INTR_STATUS               = 0x00000810,
 	HOST_ES_SELECTOR                = 0x00000c00,
 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04,
@@ -207,6 +210,14 @@ enum vmcs_field {
 	APIC_ACCESS_ADDR_HIGH		= 0x00002015,
 	EPT_POINTER                     = 0x0000201a,
 	EPT_POINTER_HIGH                = 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -17

81 lines not on this page · raw message

arch/x86/kvm/lapic.c +47 -9

90 lines not on this page · raw message

arch/x86/kvm/lapic.h +6 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu);
 u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
 void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
@@ -50,6 +53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +19 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
 
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -4292,6 +4308,9 @@ static struct kvm_x86_ops svm_x86_ops = {
 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window,
 	.update_cr8_intercept = update_cr8_intercept,
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +138 -2

218 lines not on this page · raw message

arch/x86/kvm/x86.c +29 -5
index b0b8abe..02fe194 100644
@@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
 
 static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);
 
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
 		}
+
 	}
 }
 
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +1 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 		/* need to read apic_id from apic regiest since
 		 * it can be rewritten */
 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
+		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1);
 	}
 #endif
 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Signature · 1 line
1.7.1
author
#44 lore raw

PATCH v3 4/4 x86, apicv: add virtual x2apic support

In reply to Yang Zhang

basically to benefit from apicv, we need clear MSR bitmap for corresponding x2apic MSRs:
0x800 - 0x8ff: no read intercept for apicv register virtualization TPR,EOI,SELF-IPI: no write intercept for virtual interrupt delivery

---
 arch/x86/kvm/vmx.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 57 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx.c +57 -7
index 909ce90..ac0ebf1 100644
@@ -3737,7 +3737,10 @@ static void free_vpid(struct vcpu_vmx *vmx)
 	spin_unlock(&vmx_vpid_lock);
 }
 
-static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
+#define MSR_TYPE_R	1
+#define MSR_TYPE_W	2
+static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
+						u32 msr, int type)
 {
 	int f = sizeof(unsigned long);
 
@@ -3750,20 +3753,52 @@ static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
 	 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
 	 */
 	if (msr <= 0x1fff) {
-		__clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
-		__clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
+		if (type & MSR_TYPE_R)
+			/* read-low */
+			__clear_bit(msr, msr_bitmap + 0x000 / f);
+
+		if (type & MSR_TYPE_W)
+			/* write-low */
+			__clear_bit(msr, msr_bitmap + 0x800 / f);
+
 	} else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
 		msr &= 0x1fff;
-		__clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
-		__clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
+		if (type & MSR_TYPE_R)
+			/* read-high */
+			__clear_bit(msr, msr_bitmap + 0x400 / f);
+
+		if (type & MSR_TYPE_W)
+			/* write-high */
+			__clear_bit(msr, msr_bitmap + 0xc00 / f);
+
 	}
 }
 
 static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
 {
 	if (!longmode_only)
-		__vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
-	__vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
+		__vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
+						msr, MSR_TYPE_R | MSR_TYPE_W);
+	__vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
+						msr, MSR_TYPE_R | MSR_TYPE_W);
+}
+
+static void vmx_disable_intercept_for_msr_read(u32 msr, bool longmode_only)
+{
+	if (!longmode_only)
+		__vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
+						msr, MSR_TYPE_R);
+	__vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
+					msr, MSR_TYPE_R);
+}
+
+static void vmx_disable_intercept_for_msr_write(u32 msr, bool longmode_only)
+{
+	if (!longmode_only)
+		__vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
+						msr, MSR_TYPE_W);
+	__vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
+					msr, MSR_TYPE_W);
 }
 
 /*
@@ -7585,6 +7620,21 @@ static int __init vmx_init(void)
 	vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
 	vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
 
+	if (enable_apicv_reg) {
+		int msr;
+		for (msr = 0x800; msr <= 0x8ff; msr++)
+			vmx_disable_intercept_for_msr_read(msr, false);
+	}
+
+	if (enable_apicv_vid) {
+		/* TPR */
+		vmx_disable_intercept_for_msr_write(0x808, false);
+		/* EOI */
+		vmx_disable_intercept_for_msr_write(0x80b, false);
+		/* SELF-IPI */
+		vmx_disable_intercept_for_msr_write(0x83f, false);
+	}
+
 	if (enable_ept) {
 		kvm_mmu_set_mask_ptes(0ull,
 			(enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH v3 1/4] x86: PIT connects to pin 2 of IOAPIC

In reply to Yang Zhang
Quoted 2 lines · Yang Zhang

When PIT connects to IOAPIC, it route to pin 2 not pin 0.

This hack didn't work for a long time and nobody complained. It should be safe to get rid of it.

Quoted 20 lines
---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c +1 -1
index cfb7e4d..166c450 100644
@@ -181,7 +181,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 
 #ifdef CONFIG_X86
 	/* Always delivery PIT interrupt to vcpu 0 */
-	if (irq == 0) {
+	if (irq == 2) {
 		irqe.dest_mode = 0; /* Physical mode. */
 		/* need to read apic_id from apic regiest since
 		 * it can be rewritten */
Signature · 1 line
1.7.1

--
Gleb.

#12 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Yang Zhang
Quoted 4 lines · Yang Zhang

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

Quoted 8 lines

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.

- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 178 lines
---
 arch/x86/include/asm/kvm_host.h |    4 +
 arch/x86/include/asm/vmx.h      |   11 +++
 arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++
 arch/x86/kvm/svm.c              |   19 +++++
 arch/x86/kvm/vmx.c              |  140 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |   34 ++++++++--
 virt/kvm/ioapic.c               |    1 +
 9 files changed, 291 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index dc87b65..e5352c8 100644
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +11 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41
 #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
 #define EXIT_REASON_APIC_ACCESS         44
+#define EXIT_REASON_EOI_INDUCED         45
 #define EXIT_REASON_EPT_VIOLATION       48
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
@@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
 #define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
+#define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a,
 	GUEST_LDTR_SELECTOR             = 0x0000080c,
 	GUEST_TR_SELECTOR               = 0x0000080e,
+	GUEST_INTR_STATUS               = 0x00000810,
 	HOST_ES_SELECTOR                = 0x00000c00,
 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04,
@@ -207,6 +210,14 @@ enum vmcs_field {
 	APIC_ACCESS_ADDR_HIGH		= 0x00002015,
 	EPT_POINTER                     = 0x0000201a,
 	EPT_POINTER_HIGH                = 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -17
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
 
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 4 lines
arch/x86/kvm/lapic.c +1 -0
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
 		if (unlikely(!apic_enabled(apic)))
 			break;
 
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

Quoted 39 lines
arch/x86/kvm/lapic.c +14 -9
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
 
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic)
 {
 	int vector = apic_find_highest_isr(apic);
@@ -756,19 +778,24 @@ static int apic_set_eoi(struct kvm_lapic *apic)
 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
 
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 10 lines
arch/x86/kvm/lapic.c +8 -0
 }
 
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Quoted 9 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic)
 {
 	u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 8 lines
arch/x86/kvm/lapic.c +5 -0
@@ -1533,6 +1560,17 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
 	return highest_irr;
 }
 
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Quoted 135 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +6 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu);
 u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
 void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
@@ -50,6 +53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +19 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
 
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -4292,6 +4308,9 @@ static struct kvm_x86_ops svm_x86_ops = {
 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window,
 	.update_cr8_intercept = update_cr8_intercept,
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +16 -2
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
 
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
 
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool cpu_has_vmx_apic_register_virt(void)
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
 
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
 		/* CR3 accesses and invlpg don't need to cause VM Exits when EPT

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 8 lines
arch/x86/kvm/vmx.c +5 -0
@@ -2724,6 +2738,14 @@ static __init int hardware_setup(void)
 	if (!cpu_has_vmx_apic_register_virt())
 		enable_apicv_reg = 0;
 
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Quoted 56 lines
arch/x86/kvm/vmx.c +24 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
 
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
 
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 				vmx_secondary_exec_control(vmx));
 	}
 
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
 
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
+	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 8 lines
arch/x86/kvm/vmx.c +5 -0
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Quoted 109 lines
arch/x86/kvm/vmx.c +87 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
 
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
 {
 	u32 exit_intr_info;
@@ -7364,6 +7497,9 @@ static struct kvm_x86_ops vmx_x86_ops = {
 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window,
 	.update_cr8_intercept = update_cr8_intercept,
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c +0 -0
index b0b8abe..02fe194 100644

arch/x86/kvm/x86.c static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); · line 169

Quoted 6 lines
arch/x86/kvm/x86.c +3 -0
@@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
 
 static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);
 
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Quoted 19 lines
arch/x86/kvm/x86.c +8 -4
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;

arch/x86/kvm/x86.c static void inject_pending_event(struct kvm_vcpu *vcpu) · line 5556

Quoted 10 lines
arch/x86/kvm/x86.c +8 -0
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5680

Quoted 13 lines
arch/x86/kvm/x86.c +6 -0
+
 	}
 }
 
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside
if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Quoted 27 lines
arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +1 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 		/* need to read apic_id from apic regiest since
 		 * it can be rewritten */
 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
+		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1);
 	}
 #endif
 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
Signature · 1 line
1.7.1

--
Gleb.

#3 lore raw

RE: [PATCH v3 1/4] x86: PIT connects to pin 2 of IOAPIC

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-03:

Quoted 5 lines · Yang Zhang
Quoted 2 lines

When PIT connects to IOAPIC, it route to pin 2 not pin 0.

This hack didn't work for a long time and nobody complained. It should be safe to get rid of it.

This hack is used to work around an issue about RHEL4. Not sure whether anyone still uses RHEL4.

Quoted 9 lines
Quoted 9 lines
---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0

cfb7e4d..166c450 100644 --- a/virt/kvm/ioapic.c +++ b/virt/kvm/ioapic.c

virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0
@@ -181,7 +181,7 @@ static int ioapic_deliver(struct kvm_ioapic
 *ioapic, int irq)
 
Quoted 12 lines
Quoted 9 lines
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +1 -1
 #ifdef CONFIG_X86
 	/* Always delivery PIT interrupt to vcpu 0 */
-	if (irq == 0) {
+	if (irq == 2) {
 		irqe.dest_mode = 0; /* Physical mode. */
 		/* need to read apic_id from apic regiest since

* it can be rewritten */
--
1.7.1

--
Gleb.

Best regards,
Yang

#13 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-03:

Quoted 176 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.
- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 162 lines

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c          
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h

arch/x86/include/asm/kvm_host.h +4 -0
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +8 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define EXIT_REASON_APIC_ACCESS    
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define EXIT_REASON_EPT_MISCONFIG  
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a, 	GUEST_LDTR_SELECTOR   
          = 0x0000080c, 	GUEST_TR_SELECTOR               = 0x0000080e,
 +	GUEST_INTR_STATUS               = 0x00000810, 	HOST_ES_SELECTOR     
           = 0x00000c00, 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04, @@ -207,6 +210,14 @@
 enum vmcs_field { 	APIC_ACCESS_ADDR_HIGH		= 0x00002015, 	EPT_POINTER  
                   = 0x0000201a, 	EPT_POINTER_HIGH                =
 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

Quoted 6 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 3 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the target vcpu easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. For irq notifier, only PIT is special which is edge trigger but need an EOI notifier. So, just treat it specially. And TMR can cover others.

Quoted 39 lines

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 33 lines
arch/x86/kvm/lapic.c +15 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
Quoted 8 lines

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 7 lines

+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */

arch/x86/kvm/lapic.c +4 -0
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Ok.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 7 lines

kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) return highest_irr; }

arch/x86/kvm/lapic.c +5 -0
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Ok.

Quoted 132 lines

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

Quoted 34 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@ -50,6

+53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

*vcpu);

arch/x86/kvm/svm.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 12 lines
arch/x86/kvm/lapic.h +1 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

arch/x86/kvm/vmx.c static inline bool

Quoted 52 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct vcpu_svm
 *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct kvm_x86_ops
 svm_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/svm.c +3 -0
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +6 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

Quoted 18 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
Quoted 13 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*

CR3 accesses and invlpg don't need to cause VM Exits when EPT @@
-2724,6 +2738,14 @@ static __init int hardware_setup(void) if
(!cpu_has_vmx_apic_register_virt()) enable_apicv_reg = 0;

arch/x86/kvm/vmx.c +5 -0
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

Sure.

Quoted 3 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Yes, this is wrong.

Quoted 56 lines

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 5 lines
arch/x86/kvm/vmx.c +2 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 23 lines
arch/x86/kvm/vmx.c +11 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

arch/x86/kvm/vmx.c static int (*const kvm_vmx_exit_handlers[])(struct

Quoted 16 lines
arch/x86/kvm/vmx.c +10 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 +	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Ok.

Quoted 107 lines

arch/x86/kvm/vmx.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

Quoted 102 lines
arch/x86/kvm/vmx.c +84 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) { 	u32
 exit_intr_info; @@ -7364,6 +7497,9 @@ static struct kvm_x86_ops
 vmx_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/vmx.c +3 -0
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0

b0b8abe..02fe194 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);

Quoted 5 lines

arch/x86/kvm/x86.c index

Quoted 4 lines

static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -0
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Ok.

Quoted 29 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct kvm_vcpu

Quoted 9 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct kvm_vcpu

*vcpu)

Quoted 19 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +11 -4
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
Quoted 8 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct kvm_vcpu · line 5556

Quoted 1 line
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

As you mentioned, we still need to checks both extint and apic interrupt in some case. So how to do this? Introduce another argument to indicate whether check both?

Quoted 7 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu

Quoted 4 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +1 -0
+
 	}
 }
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 2 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 	}
 
Quoted 11 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu · line 5680

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Yes, it seems ok to move it.

Quoted 26 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic, int

Quoted 18 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int

irq)

Quoted 6 lines
virt/kvm/ioapic.c +0 -1
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1); 	}
 #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
--

1.7.1

Quoted 2 lines

--
Gleb.

Best regards,
Yang

#14 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Quoted 177 lines · Yang Z

Gleb Natapov wrote on 2012-12-03:

Quoted 176 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.
- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 162 lines

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c          
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h

arch/x86/include/asm/kvm_host.h +4 -0
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +8 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define EXIT_REASON_APIC_ACCESS    
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define EXIT_REASON_EPT_MISCONFIG  
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a, 	GUEST_LDTR_SELECTOR   
          = 0x0000080c, 	GUEST_TR_SELECTOR               = 0x0000080e,
 +	GUEST_INTR_STATUS               = 0x00000810, 	HOST_ES_SELECTOR     
           = 0x00000c00, 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04, @@ -207,6 +210,14 @@
 enum vmcs_field { 	APIC_ACCESS_ADDR_HIGH		= 0x00002015, 	EPT_POINTER  
                   = 0x0000201a, 	EPT_POINTER_HIGH                =
 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

Quoted 7 lines
Quoted 6 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 3 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the target vcpu easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap.

No need to check target vcpu. Enable exit on all vcpus for the vector programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

Quoted 2 lines

For irq notifier, only PIT is special which is edge trigger but need an EOI notifier. So, just treat it specially. And TMR can cover others.

We shouldn't assume that. If another notifier will be added it will be easy to forget to update apicv code to exclude another vector too.

Quoted 210 lines
Quoted 39 lines

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 33 lines
arch/x86/kvm/lapic.c +15 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
Quoted 8 lines

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 7 lines

+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */

arch/x86/kvm/lapic.c +4 -0
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Ok.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 7 lines

kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) return highest_irr; }

arch/x86/kvm/lapic.c +5 -0
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Ok.

Quoted 132 lines

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

Quoted 34 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@ -50,6

+53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

*vcpu);

arch/x86/kvm/svm.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 12 lines
arch/x86/kvm/lapic.h +1 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

arch/x86/kvm/vmx.c static inline bool

Quoted 52 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct vcpu_svm
 *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct kvm_x86_ops
 svm_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/svm.c +3 -0
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +6 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

Quoted 18 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
Quoted 13 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*

CR3 accesses and invlpg don't need to cause VM Exits when EPT @@
-2724,6 +2738,14 @@ static __init int hardware_setup(void) if
(!cpu_has_vmx_apic_register_virt()) enable_apicv_reg = 0;

arch/x86/kvm/vmx.c +5 -0
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

Quoted 6 lines

Sure.

Quoted 3 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Yes, this is wrong.

Please test the patches with vid disabled and Windows guests. This bug should have prevented it from working.

Quoted 220 lines
Quoted 57 lines

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 23 lines
arch/x86/kvm/vmx.c +11 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

arch/x86/kvm/vmx.c static int (*const kvm_vmx_exit_handlers[])(struct

Quoted 16 lines
arch/x86/kvm/vmx.c +10 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 +	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Ok.

Quoted 107 lines

arch/x86/kvm/vmx.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

Quoted 102 lines
arch/x86/kvm/vmx.c +84 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) { 	u32
 exit_intr_info; @@ -7364,6 +7497,9 @@ static struct kvm_x86_ops
 vmx_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/vmx.c +3 -0
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0

b0b8abe..02fe194 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);

Quoted 5 lines

arch/x86/kvm/x86.c index

Quoted 4 lines

static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -0
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Ok.

Quoted 29 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct kvm_vcpu

Quoted 9 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct kvm_vcpu

*vcpu)

Quoted 19 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +11 -4
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
Quoted 8 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct kvm_vcpu · line 5556

Quoted 1 line
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

Quoted 1 line

As you mentioned, we still need to checks both extint and apic interrupt in some case. So how to do this? Introduce another argument to indicate whether check both?

Yes, we need to check both in kvm_arch_vcpu_runnable(). Another argument is good option. We can have two functions: kvm_cpu_has_injectable_interrupt() for use in irq injection path and kvm_cpu_has_interrupt() for use in kvm_arch_vcpu_runnable(). They will call common one with additional argument to avoid code duplication.

Quoted 51 lines
Quoted 8 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +1 -0
+
 	}
 }
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 2 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 	}
 
Quoted 11 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu · line 5680

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Yes, it seems ok to move it.

Quoted 26 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic, int

Quoted 18 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int

irq)

Quoted 6 lines
virt/kvm/ioapic.c +0 -1
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1); 	}
 #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
--

1.7.1

Quoted 2 lines

--
Gleb.

Quoted 7 lines

Best regards,
Yang

--
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

--
Gleb.

#4 lore raw

Re: [PATCH v3 1/4] x86: PIT connects to pin 2 of IOAPIC

In reply to Zhang, Yang Z
Quoted 8 lines · Yang Z

Gleb Natapov wrote on 2012-12-03:

Quoted 5 lines · Yang Zhang
Quoted 2 lines

When PIT connects to IOAPIC, it route to pin 2 not pin 0.

This hack didn't work for a long time and nobody complained. It should be safe to get rid of it.

This hack is used to work around an issue about RHEL4. Not sure whether anyone still uses RHEL4.

Yes, I traced it back to 8c35f237, but the hack is broken for a long time (~3 years?) and nobody complained. It is broken in rhel6 too and rhel4 is tested as a guest there, may be the problem was solved in rhel4. I think we can remove it now. If problem re-appear, at least we will know which guest exactly actually needs it.

Quoted 23 lines
Quoted 9 lines
Quoted 9 lines
---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0

cfb7e4d..166c450 100644 --- a/virt/kvm/ioapic.c +++ b/virt/kvm/ioapic.c

virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0
@@ -181,7 +181,7 @@ static int ioapic_deliver(struct kvm_ioapic
 *ioapic, int irq)
 
Quoted 12 lines
Quoted 9 lines
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +1 -1
 #ifdef CONFIG_X86
 	/* Always delivery PIT interrupt to vcpu 0 */
-	if (irq == 0) {
+	if (irq == 2) {
 		irqe.dest_mode = 0; /* Physical mode. */
 		/* need to read apic_id from apic regiest since

* it can be rewritten */
--
1.7.1

--
Gleb.

Quoted 3 lines

Best regards,
Yang

--
Gleb.

#26 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Yang Zhang

Review

Quoted 1 line · Yang Zhang
(quoted diff) +1 -0
+		int vector = -1;

nitpicking: no need to initialize vector here

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
#7 lore raw

Re: [PATCH v3 2/4] x86, apicv: add APICv register virtualization support

In reply to Yang Zhang
Quoted 40 lines · Yang Zhang

- APIC read doesn't cause VM-Exit
- APIC write becomes trap-like

---
 arch/x86/include/asm/vmx.h |    2 ++
 arch/x86/kvm/lapic.c       |   16 ++++++++++++++++
 arch/x86/kvm/lapic.h       |    2 ++
 arch/x86/kvm/vmx.c         |   32 +++++++++++++++++++++++++++++++-
 4 files changed, 51 insertions(+), 1 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
index 36ec21c..21101b6 100644
@@ -66,6 +66,7 @@
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
 #define EXIT_REASON_XSETBV              55
+#define EXIT_REASON_APIC_WRITE          56
 #define EXIT_REASON_INVPCID             58
 
 #define VMX_EXIT_REASONS \
@@ -141,6 +142,7 @@
 #define SECONDARY_EXEC_ENABLE_VPID              0x00000020
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
+#define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
arch/x86/kvm/lapic.c +0 -0
index 9392f52..7c96012 100644
@@ -1212,6 +1212,22 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
 

arch/x86/kvm/lapic.c void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) · line 1224

Quoted 10 lines
arch/x86/kvm/lapic.c +10 -0
+/* emulate APIC access in a trap manner */
+int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
+{
+	u32 val = 0;
+
+	/* hw has done the conditional check and inst decode */
+	offset &= 0xff0;
+	if ((offset != APIC_EOI) &&
+	     apic_reg_read(vcpu->arch.apic, offset, 4, &val))
+		return 1;

What is apic_reg_read doing? None of the checks it performs that can result in return value of 1 are necessary for APIC-write VM-exit AFAICS.

arch/x86/kvm/lapic.c module_param(vmm_exclusive, bool, S_IRUGO); · line 88

Quoted 7 lines
arch/x86/kvm/lapic.c +3 -0
@@ -83,6 +83,9 @@ module_param(vmm_exclusive, bool, S_IRUGO);
 static bool __read_mostly fasteoi = 1;
 module_param(fasteoi, bool, S_IRUGO);
 
+static bool __read_mostly enable_apicv_reg;
+module_param(enable_apicv_reg, bool, S_IRUGO);
+

Are the different combinations of register virtualization / virtual interrupt delivery actually supported?

Why would it be useful to enable register virtualization / virtual interrupt delivery separately?

Quoted 59 lines
arch/x86/kvm/lapic.c +18 -1
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -761,6 +764,12 @@ static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
 		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
 }
 
+static inline bool cpu_has_vmx_apic_register_virt(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_APIC_REGISTER_VIRT;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2498,7 +2507,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 			SECONDARY_EXEC_UNRESTRICTED_GUEST |
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
-			SECONDARY_EXEC_ENABLE_INVPCID;
+			SECONDARY_EXEC_ENABLE_INVPCID |
+			SECONDARY_EXEC_APIC_REGISTER_VIRT;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2509,6 +2519,11 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 				SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
 		_cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
 #endif
+
+	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
+		_cpu_based_2nd_exec_control &= ~(
+				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
 		/* CR3 accesses and invlpg don't need to cause VM Exits when EPT
 		   enabled */
@@ -2706,6 +2721,9 @@ static __init int hardware_setup(void)
 	if (!cpu_has_vmx_ple())
 		ple_gap = 0;
 
+	if (!cpu_has_vmx_apic_register_virt())
+		enable_apicv_reg = 0;
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
 
@@ -3819,6 +3837,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 		exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
 	if (!ple_gap)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
+	if (!enable_apicv_reg)
+		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
 	return exec_control;
 }
 
@@ -4786,6 +4806,15 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;

arch/x86/kvm/lapic.c static int handle_apic_access(struct kvm_vcpu *vcpu) · line 4816

Quoted 10 lines
arch/x86/kvm/lapic.c +8 -0
 }
 
+static int handle_apic_write(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	u32 offset = exit_qualification & 0xfff;
+
+	/* APIC-write VM exit is trap-like and thus no need to adjust IP */
+	return kvm_apic_write_nodecode(vcpu, offset) == 0;
+}

Point of return value == 0?

#5 lore raw

RE: [PATCH v3 1/4] x86: PIT connects to pin 2 of IOAPIC

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-04:

Quoted 7 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 5 lines · Yang Zhang
Quoted 2 lines

When PIT connects to IOAPIC, it route to pin 2 not pin 0.

This hack didn't work for a long time and nobody complained. It should be safe to get rid of it.

Quoted 8 lines
Quoted 3 lines

This hack is used to work around an issue about RHEL4. Not sure whether anyone still uses RHEL4.

Yes, I traced it back to 8c35f237, but the hack is broken for a long time (~3 years?) and nobody complained. It is broken in rhel6 too and rhel4 is tested as a guest there, may be the problem was solved in rhel4. I think we can remove it now. If problem re-appear, at least we will know which guest exactly actually needs it.

Agree.

Quoted 28 lines
Quoted 24 lines
Quoted 10 lines
Quoted 10 lines
---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0

cfb7e4d..166c450 100644 --- a/virt/kvm/ioapic.c +++ b/virt/kvm/ioapic.c

virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +0 -0
@@ -181,7 +181,7 @@ static int ioapic_deliver(struct kvm_ioapic
 *ioapic, int irq)
 
Quoted 12 lines
Quoted 9 lines
virt/kvm/ioapic.c index renamed from virt/kvm/ioapic.c +1 -1
 #ifdef CONFIG_X86
 	/* Always delivery PIT interrupt to vcpu 0 */
-	if (irq == 0) {
+	if (irq == 2) {
 		irqe.dest_mode = 0; /* Physical mode. */
 		/* need to read apic_id from apic regiest since

* it can be rewritten */
--
1.7.1

--
Gleb.

Quoted 3 lines

Best regards,
Yang

Quoted 6 lines

--
Gleb.
--
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

Best regards,
Yang

#15 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-04:

Quoted 35 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/lapic.c +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic,

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic,

int

Quoted 7 lines
Quoted 7 lines

delivery_mode,

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 3 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

Quoted 5 lines
Quoted 1 line

It is too complicated. When program ioapic entry, we cannot get the target vcpu

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap.
No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We cannot ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 2 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination format register and logical destination register. So we must also trap the two registers. And if it uses lowest priority delivery mode, the PPR need to be trapped too. Since PPR will change on each interrupt injection, the cost should be higher than current approach.

Quoted 5 lines
Quoted 3 lines

For irq notifier, only PIT is special which is edge trigger but need an EOI notifier. So, just treat it specially. And TMR can cover others.

We shouldn't assume that. If another notifier will be added it will be easy to forget to update apicv code to exclude another vector too.

At this point, guest is not running(in device initializing), we cannot not know the vector. As you mentioned, the best point is when guest program ioapic entry. But it also is impossible to get the vector(see above). I can give some comments on the function to remind the caller to update eoi bitmap when the interrupt is edge and they still want to get EOI vmexit.

Quoted 439 lines
Quoted 5 lines
Quoted 5 lines

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu

*vcpu1,

Quoted 83 lines
Quoted 25 lines

struct kvm_vcpu *vcpu2)

Quoted 24 lines
arch/x86/kvm/lapic.c +2 -0
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{ +	if (!(kvm_apic_get_reg(apic, APIC_SPIV) &

APIC_SPIV_DIRECTED_EOI) && +
kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { + int
trigger_mode; + if (apic_test_vector(vector, apic->regs + APIC_TMR))

(quoted diff) +1 -0
+			trigger_mode = IOAPIC_LEVEL_TRIG; +		else +			trigger_mode =

IOAPIC_EDGE_TRIG; + kvm_ioapic_update_eoi(apic->vcpu->kvm, vector,
trigger_mode); + } +} +
static int apic_set_eoi(struct kvm_lapic *apic) { int vector =
apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
apic_set_eoi(struct kvm_lapic *apic) apic_clear_isr(vector, apic);

(quoted diff) +0 -1
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)

&& - kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { - int
trigger_mode; - if (apic_test_vector(vector, apic->regs + APIC_TMR))

(quoted diff) +0 -1
-			trigger_mode = IOAPIC_LEVEL_TRIG; -		else -			trigger_mode =

IOAPIC_EDGE_TRIG; - kvm_ioapic_update_eoi(apic->vcpu->kvm, vector,
trigger_mode); - } + kvm_ioapic_send_eoi(apic, vector);

arch/x86/kvm/lapic.c +1 -0
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
Quoted 8 lines

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 7 lines

+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */

arch/x86/kvm/lapic.c +4 -0
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Ok.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 7 lines

kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) return highest_irr; }

arch/x86/kvm/lapic.c +5 -0
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Ok.

Quoted 32 lines
Quoted 32 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@ -50,6

+53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
Quoted 1 line
Quoted 1 line
Quoted 1 line

bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic

*src,

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

Quoted 1 line
arch/x86/kvm/lapic.h +0 -0
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

*vcpu);

Quoted 5 lines
arch/x86/kvm/lapic.h +1 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/svm.c static void update_cr8_intercept(struct

Quoted 7 lines
arch/x86/kvm/lapic.h +0 -0
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t
 vapic_addr); void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu, int tpr, int irr)

Quoted 27 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct vcpu_svm
 *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct kvm_x86_ops
 svm_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/svm.c +3 -0
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/svm.c +0 -0
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +0 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
Quoted 8 lines

arch/x86/kvm/vmx.c module_param(fasteoi, bool, S_IRUGO);

Quoted 8 lines
arch/x86/kvm/vmx.c +3 -0
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may

not

Quoted 56 lines
Quoted 42 lines

arch/x86/kvm/vmx.c static inline bool

Quoted 10 lines
arch/x86/kvm/vmx.c +3 -0
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

Quoted 18 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
Quoted 14 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*

CR3 accesses and invlpg don't need to cause VM Exits when EPT @@
-2724,6 +2738,14 @@ static __init int hardware_setup(void) if
(!cpu_has_vmx_apic_register_virt()) enable_apicv_reg = 0;

arch/x86/kvm/vmx.c +5 -0
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

Quoted 6 lines

Sure.

Quoted 3 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Yes, this is wrong.

Please test the patches with vid disabled and Windows guests. This bug should have prevented it from working.

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx.c static int vmx_vcpu_setup(struct vcpu_vmx

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx

*vmx)

Quoted 53 lines
Quoted 43 lines

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 15 lines
arch/x86/kvm/vmx.c +9 -0
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

arch/x86/kvm/vmx.c static int (*const kvm_vmx_exit_handlers[])(struct

Quoted 16 lines
arch/x86/kvm/vmx.c +10 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 +	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu
 *vcpu)
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Quoted 6 lines

Ok.

Quoted 4 lines

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

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
arch/x86/kvm/vmx.c +0 -0
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 110 lines
Quoted 103 lines

*vcpu, int tpr, int irr)

Quoted 102 lines
arch/x86/kvm/vmx.c +84 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) { 	u32
 exit_intr_info; @@ -7364,6 +7497,9 @@ static struct kvm_x86_ops
 vmx_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/vmx.c +3 -0
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0

b0b8abe..02fe194 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);

Quoted 5 lines

arch/x86/kvm/x86.c index

Quoted 4 lines

static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -0
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Ok.

Quoted 9 lines
Quoted 9 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct

Quoted 9 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu)

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -4
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;

arch/x86/kvm/x86.c index static void inject_pending_event(struct · line 5556

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +8 -0
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

Quoted 8 lines
Quoted 1 line

As you mentioned, we still need to checks both extint and apic interrupt in

some case. So how to do this? Introduce another argument to indicate whether check both? Yes, we need to check both in kvm_arch_vcpu_runnable(). Another argument is good option. We can have two functions: kvm_cpu_has_injectable_interrupt() for use in irq injection path and kvm_cpu_has_interrupt() for use in kvm_arch_vcpu_runnable(). They will call common one with additional argument to avoid code duplication.

Ok. will follow this way.

Quoted 54 lines
Quoted 54 lines
Quoted 8 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +1 -0
+
 	}
 }
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 2 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 	}
 
Quoted 11 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu · line 5680

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Yes, it seems ok to move it.

Quoted 26 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic, int

Quoted 18 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int

irq)

Quoted 6 lines
virt/kvm/ioapic.c +0 -1
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1); 	}
 #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
--

1.7.1

Quoted 2 lines

--
Gleb.

Best regards,
Yang

Quoted 7 lines
Quoted 4 lines

--
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

--
Gleb.

Best regards,
Yang

#27 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Yang Zhang
Quoted 504 lines · Yang Zhang

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.

- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
 arch/x86/include/asm/kvm_host.h |    4 +
 arch/x86/include/asm/vmx.h      |   11 +++
 arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++
 arch/x86/kvm/svm.c              |   19 +++++
 arch/x86/kvm/vmx.c              |  140 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |   34 ++++++++--
 virt/kvm/ioapic.c               |    1 +
 9 files changed, 291 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index dc87b65..e5352c8 100644
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +11 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41
 #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
 #define EXIT_REASON_APIC_ACCESS         44
+#define EXIT_REASON_EOI_INDUCED         45
 #define EXIT_REASON_EPT_VIOLATION       48
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
@@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
 #define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
+#define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a,
 	GUEST_LDTR_SELECTOR             = 0x0000080c,
 	GUEST_TR_SELECTOR               = 0x0000080e,
+	GUEST_INTR_STATUS               = 0x00000810,
 	HOST_ES_SELECTOR                = 0x00000c00,
 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04,
@@ -207,6 +210,14 @@ enum vmcs_field {
 	APIC_ACCESS_ADDR_HIGH		= 0x00002015,
 	EPT_POINTER                     = 0x0000201a,
 	EPT_POINTER_HIGH                = 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -17
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +47 -9
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
 
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
 		if (unlikely(!apic_enabled(apic)))
 			break;
 
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
 
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic)
 {
 	int vector = apic_find_highest_isr(apic);
@@ -756,19 +778,24 @@ static int apic_set_eoi(struct kvm_lapic *apic)
 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
 
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
 
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic)
 {
 	u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
@@ -1533,6 +1560,17 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
 	return highest_irr;
 }
 
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +6 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu);
 u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
 void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
@@ -50,6 +53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +19 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
 
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -4292,6 +4308,9 @@ static struct kvm_x86_ops svm_x86_ops = {
 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window,
 	.update_cr8_intercept = update_cr8_intercept,
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +80 -2
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
 
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
 
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool cpu_has_vmx_apic_register_virt(void)
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
 
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
 		/* CR3 accesses and invlpg don't need to cause VM Exits when EPT
@@ -2724,6 +2738,14 @@ static __init int hardware_setup(void)
 	if (!cpu_has_vmx_apic_register_virt())
 		enable_apicv_reg = 0;
 
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;
+		kvm_x86_ops->update_cr8_intercept = NULL;
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
 
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
 
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 				vmx_secondary_exec_control(vmx));
 	}
 
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
 
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
+	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
 
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)

arch/x86/kvm/vmx.c static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) · line 6194

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;

1. Is the pseudocode sequence of virtual interrupt delivery in 29.2.2 guaranteed to be atomic (not interruptible by other events) ? The question is, when hardware is performing virtual interrupt delivery is it guaranteed that RVI matches VIRR ? (the answer must be yes, just checking).

2. Section 29.6 mentions that "Use of the posted-interrupt descriptor differs from that of other data structures that are referenced by pointers in a VMCS. There is a general requirement that software ensure that each such data structure is modified only when no logical processor with a current VMCS that references it is in VMX non-root operation. That requirement does not apply to the posted-interrupt descriptor. There is a requirement, however, that such modifications be done using locked read-modify-write instructions."

The APIC virtual page is being modified by a CPU while a logical processor with current VMCS that references it is in VMX non-root operation, in fact even modifying the APIC virtual page with EOI virtualizaton, virtual interrupt delivery, etc. What are the requirements in this case?

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/vmx.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/vmx.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the “interrupt-window exiting” VM-execution control is 0."

Quoted 16 lines
arch/x86/kvm/vmx.c +1 -0
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +1 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 		/* need to read apic_id from apic regiest since
 		 * it can be rewritten */
 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
+		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1);
 	}
 #endif
 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
#8 lore raw

RE: [PATCH v3 2/4] x86, apicv: add APICv register virtualization support

In reply to Marcelo Tosatti

Marcelo Tosatti wrote on 2012-12-05:

Quoted 40 lines · Yang Zhang
Quoted 39 lines

- APIC read doesn't cause VM-Exit
- APIC write becomes trap-like

---
 arch/x86/include/asm/vmx.h |    2 ++ arch/x86/kvm/lapic.c       |   16
 ++++++++++++++++ arch/x86/kvm/lapic.h       |    2 ++

arch/x86/kvm/vmx.c | 32 +++++++++++++++++++++++++++++++- 4
files changed, 51 insertions(+), 1 deletions(-)

arch/x86/include/asm/vmx.h +0 -0
index 36ec21c..21101b6 100644
@@ -66,6 +66,7 @@
 #define EXIT_REASON_EPT_MISCONFIG       49 #define EXIT_REASON_WBINVD 
             54 #define EXIT_REASON_XSETBV              55 +#define
 EXIT_REASON_APIC_WRITE          56 #define EXIT_REASON_INVPCID        
     58
 
 #define VMX_EXIT_REASONS \ @@ -141,6 +142,7 @@ #define
 SECONDARY_EXEC_ENABLE_VPID              0x00000020 #define
 SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 +#define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
arch/x86/kvm/lapic.c +5 -0
index 9392f52..7c96012 100644
@@ -1212,6 +1212,22 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
+/* emulate APIC access in a trap manner */
+int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
+{
+	u32 val = 0;
+
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) · line 1224

Quoted 5 lines
arch/x86/kvm/lapic.c +5 -0
+	/* hw has done the conditional check and inst decode */
+	offset &= 0xff0;
+	if ((offset != APIC_EOI) &&
+	     apic_reg_read(vcpu->arch.apic, offset, 4, &val))
+		return 1;

What is apic_reg_read doing? None of the checks it performs that can result in return value of 1 are necessary for APIC-write VM-exit AFAICS.

Right. We should remove this check.

Quoted 9 lines

arch/x86/kvm/lapic.c module_param(vmm_exclusive, bool, S_IRUGO); · line 87

Quoted 6 lines
arch/x86/kvm/lapic.c +3 -0
@@ -83,6 +83,9 @@ module_param(vmm_exclusive, bool, S_IRUGO);
 static bool __read_mostly fasteoi = 1;
 module_param(fasteoi, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_reg;
+module_param(enable_apicv_reg, bool, S_IRUGO);
+

Are the different combinations of register virtualization / virtual interrupt delivery actually supported?

Yes.

Quoted 2 lines

Why would it be useful to enable register virtualization / virtual interrupt delivery separately?

It's ok to use one option: enable_apicv.

Quoted 57 lines

arch/x86/kvm/lapic.c static inline bool

Quoted 4 lines
arch/x86/kvm/lapic.c +0 -0
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -761,6 +764,12 @@ static inline bool

cpu_has_vmx_virtualize_apic_accesses(void)

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

Quoted 12 lines
arch/x86/kvm/lapic.c +6 -0
 		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
 }
+static inline bool cpu_has_vmx_apic_register_virt(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_APIC_REGISTER_VIRT;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2498,7 +2507,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

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

Quoted 10 lines
arch/x86/kvm/lapic.c +2 -1
 			SECONDARY_EXEC_UNRESTRICTED_GUEST |
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
-			SECONDARY_EXEC_ENABLE_INVPCID;
+			SECONDARY_EXEC_ENABLE_INVPCID |
+			SECONDARY_EXEC_APIC_REGISTER_VIRT;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2509,6 +2519,11 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/lapic.c static u32 vmx_secondary_exec_control(struct

Quoted 18 lines
arch/x86/kvm/lapic.c +5 -0
 				SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
 		_cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
 #endif
+
+	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
+		_cpu_based_2nd_exec_control &= ~(
+				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*
 CR3 accesses and invlpg don't need to cause VM Exits when EPT 		  
 enabled */ @@ -2706,6 +2721,9 @@ static __init int

hardware_setup(void) if (!cpu_has_vmx_ple()) ple_gap = 0;

arch/x86/kvm/lapic.c +3 -0
+	if (!cpu_has_vmx_apic_register_virt())
+		enable_apicv_reg = 0;
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3819,6 +3837,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

arch/x86/kvm/lapic.c static int handle_apic_access(struct kvm_vcpu

Quoted 8 lines
arch/x86/kvm/lapic.c +2 -0
 		exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
 	if (!ple_gap)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
+	if (!enable_apicv_reg)
+		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
 	return exec_control;
 }
@@ -4786,6 +4806,15 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

Quoted 12 lines

arch/x86/kvm/lapic.c static int handle_apic_access(struct kvm_vcpu · line 4815

Quoted 10 lines
arch/x86/kvm/lapic.c +8 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_write(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	u32 offset = exit_qualification & 0xfff;
+
+	/* APIC-write VM exit is trap-like and thus no need to adjust IP */
+	return kvm_apic_write_nodecode(vcpu, offset) == 0;
+}

Point of return value == 0?

if kvm_apic_write_nodecode() handle successfully, it will return zero. Then it will return 1 for this vmexit handle. What's wrong?

Best regards,
Yang

#28 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Marcelo Tosatti

Marcelo Tosatti wrote on 2012-12-05:

Quoted 483 lines · Yang Zhang

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 172 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.
- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c          
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h

arch/x86/include/asm/kvm_host.h +4 -0
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +8 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define EXIT_REASON_APIC_ACCESS    
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define EXIT_REASON_EPT_MISCONFIG  
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a, 	GUEST_LDTR_SELECTOR   
          = 0x0000080c, 	GUEST_TR_SELECTOR               = 0x0000080e,
 +	GUEST_INTR_STATUS               = 0x00000810, 	HOST_ES_SELECTOR     
           = 0x00000c00, 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04, @@ -207,6 +210,14 @@
 enum vmcs_field { 	APIC_ACCESS_ADDR_HIGH		= 0x00002015, 	EPT_POINTER  
                   = 0x0000201a, 	EPT_POINTER_HIGH                =
 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 7 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

Quoted 86 lines
arch/x86/kvm/lapic.c +38 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@ -50,6

+53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

*vcpu);

arch/x86/kvm/svm.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 12 lines
arch/x86/kvm/lapic.h +1 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

arch/x86/kvm/vmx.c static inline bool

Quoted 52 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct vcpu_svm
 *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct kvm_x86_ops
 svm_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/svm.c +3 -0
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
 
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +6 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 33 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*

CR3 accesses and invlpg don't need to cause VM Exits when EPT @@
-2724,6 +2738,14 @@ static __init int hardware_setup(void) if
(!cpu_has_vmx_apic_register_virt()) enable_apicv_reg = 0;

arch/x86/kvm/vmx.c +8 -0
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;
+		kvm_x86_ops->update_cr8_intercept = NULL;
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 23 lines
arch/x86/kvm/vmx.c +11 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

arch/x86/kvm/vmx.c static int (*const kvm_vmx_exit_handlers[])(struct

Quoted 16 lines
arch/x86/kvm/vmx.c +10 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

arch/x86/kvm/vmx.c static void update_cr8_intercept(struct kvm_vcpu

Quoted 20 lines
arch/x86/kvm/vmx.c +5 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 +	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
 
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct kvm_vcpu

*vcpu, int tpr, int irr)

Quoted 39 lines
arch/x86/kvm/vmx.c +37 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
Quoted 8 lines

arch/x86/kvm/vmx.c static void update_cr8_intercept(struct kvm_vcpu · line 6193

Quoted 2 lines
arch/x86/kvm/vmx.c +2 -0
+	if (vector == -1)
+		return;

1. Is the pseudocode sequence of virtual interrupt delivery in 29.2.2 guaranteed to be atomic (not interruptible by other events) ? The question is, when hardware is performing virtual interrupt delivery is it guaranteed that RVI matches VIRR ? (the answer must be yes, just checking).

I don't think so. Think that, we may update VIRR in other vcpu when the target vcpu is doing vmentry. The only problem is that this interrupt will not handled until the previous interrupt is handled. Without this patch, we also will meet this corner case.

Quoted 9 lines

2. Section 29.6 mentions that "Use of the posted-interrupt descriptor differs from that of other data structures that are referenced by pointers in a VMCS. There is a general requirement that software ensure that each such data structure is modified only when no logical processor with a current VMCS that references it is in VMX non-root operation. That requirement does not apply to the posted-interrupt descriptor. There is a requirement, however, that such modifications be done using locked read-modify-write instructions."

Quoted 5 lines

The APIC virtual page is being modified by a CPU while a logical processor with current VMCS that references it is in VMX non-root operation, in fact even modifying the APIC virtual page with EOI virtualizaton, virtual interrupt delivery, etc. What are the requirements in this case?

It should be same with posted interrupt. Software must ensure to use atomic access to virtual apic page.

Quoted 23 lines

arch/x86/kvm/vmx.c static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
arch/x86/kvm/vmx.c +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/vmx.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/vmx.c static int vcpu_enter_guest(struct kvm_vcpu · line 5688

Quoted 10 lines
arch/x86/kvm/vmx.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the “interrupt-window exiting” VM-execution control is 0."

Right. Must check RVI here.

Quoted 14 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic, int

Quoted 9 lines
arch/x86/kvm/vmx.c +1 -0
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int

irq)

Quoted 4 lines
virt/kvm/ioapic.c +0 -0
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1); 	}
 #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);

Best regards,
Yang

#16 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Zhang, Yang Z
Quoted 36 lines · Yang Z

Gleb Natapov wrote on 2012-12-04:

Quoted 35 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/lapic.c +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic,

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic,

int

Quoted 7 lines
Quoted 7 lines

delivery_mode,

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 3 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

It is too complicated. When program ioapic entry, we cannot get the target vcpu

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap.
No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We cannot ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

I never saw OSes reuse vector used by ioapic, as far as I see this is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

Quoted 4 lines
Quoted 2 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination format register and logical destination register. So we must also trap the two registers. And if it uses lowest priority delivery mode, the PPR need to be trapped too. Since PPR will change on each interrupt injection, the cost should be higher than current approach.

No need for all of that if bitmask it global.

Quoted 517 lines
Quoted 6 lines
Quoted 4 lines

For irq notifier, only PIT is special which is edge trigger but need an EOI notifier. So, just treat it specially. And TMR can cover others.

We shouldn't assume that. If another notifier will be added it will be easy to forget to update apicv code to exclude another vector too.

At this point, guest is not running(in device initializing), we cannot not know the vector. As you mentioned, the best point is when guest program ioapic entry. But it also is impossible to get the vector(see above). I can give some comments on the function to remind the caller to update eoi bitmap when the interrupt is edge and they still want to get EOI vmexit.

Quoted 439 lines
Quoted 5 lines
Quoted 5 lines

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu

*vcpu1,

Quoted 83 lines
Quoted 25 lines

struct kvm_vcpu *vcpu2)

Quoted 24 lines
arch/x86/kvm/lapic.c +2 -0
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{ +	if (!(kvm_apic_get_reg(apic, APIC_SPIV) &

APIC_SPIV_DIRECTED_EOI) && +
kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { + int
trigger_mode; + if (apic_test_vector(vector, apic->regs + APIC_TMR))

(quoted diff) +1 -0
+			trigger_mode = IOAPIC_LEVEL_TRIG; +		else +			trigger_mode =

IOAPIC_EDGE_TRIG; + kvm_ioapic_update_eoi(apic->vcpu->kvm, vector,
trigger_mode); + } +} +
static int apic_set_eoi(struct kvm_lapic *apic) { int vector =
apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
apic_set_eoi(struct kvm_lapic *apic) apic_clear_isr(vector, apic);

(quoted diff) +0 -1
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)

&& - kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { - int
trigger_mode; - if (apic_test_vector(vector, apic->regs + APIC_TMR))

(quoted diff) +0 -1
-			trigger_mode = IOAPIC_LEVEL_TRIG; -		else -			trigger_mode =

IOAPIC_EDGE_TRIG; - kvm_ioapic_update_eoi(apic->vcpu->kvm, vector,
trigger_mode); - } + kvm_ioapic_send_eoi(apic, vector);

arch/x86/kvm/lapic.c +1 -0
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
Quoted 8 lines

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 7 lines

+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */

arch/x86/kvm/lapic.c +4 -0
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Ok.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 7 lines

kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) return highest_irr; }

arch/x86/kvm/lapic.c +5 -0
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Ok.

Quoted 32 lines
Quoted 32 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@ -50,6

+53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
Quoted 1 line
Quoted 1 line
Quoted 1 line

bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic

*src,

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

Quoted 1 line
arch/x86/kvm/lapic.h +0 -0
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu

*vcpu);

Quoted 5 lines
arch/x86/kvm/lapic.h +1 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/svm.c static void update_cr8_intercept(struct

Quoted 7 lines
arch/x86/kvm/lapic.h +0 -0
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t
 vapic_addr); void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu, int tpr, int irr)

Quoted 27 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct vcpu_svm
 *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct kvm_x86_ops
 svm_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/svm.c +3 -0
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/svm.c +0 -0
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +0 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
Quoted 8 lines

arch/x86/kvm/vmx.c module_param(fasteoi, bool, S_IRUGO);

Quoted 8 lines
arch/x86/kvm/vmx.c +3 -0
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may

not

Quoted 56 lines
Quoted 42 lines

arch/x86/kvm/vmx.c static inline bool

Quoted 10 lines
arch/x86/kvm/vmx.c +3 -0
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

Quoted 18 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
Quoted 14 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*

CR3 accesses and invlpg don't need to cause VM Exits when EPT @@
-2724,6 +2738,14 @@ static __init int hardware_setup(void) if
(!cpu_has_vmx_apic_register_virt()) enable_apicv_reg = 0;

arch/x86/kvm/vmx.c +5 -0
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

Quoted 6 lines

Sure.

Quoted 3 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Yes, this is wrong.

Please test the patches with vid disabled and Windows guests. This bug should have prevented it from working.

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx.c static int vmx_vcpu_setup(struct vcpu_vmx

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx

*vmx)

Quoted 53 lines
Quoted 43 lines

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 15 lines
arch/x86/kvm/vmx.c +9 -0
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

arch/x86/kvm/vmx.c static int (*const kvm_vmx_exit_handlers[])(struct

Quoted 16 lines
arch/x86/kvm/vmx.c +10 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct

kvm_vcpu *vcpu) = {

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_APIC_WRITE]              = handle_apic_write,
 +	[EXIT_REASON_EOI_INDUCED]             = handle_apic_eoi_induced,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
 	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu
 *vcpu)
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Quoted 6 lines

Ok.

Quoted 4 lines

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

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
arch/x86/kvm/vmx.c +0 -0
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 110 lines
Quoted 103 lines

*vcpu, int tpr, int irr)

Quoted 102 lines
arch/x86/kvm/vmx.c +84 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) { 	u32
 exit_intr_info; @@ -7364,6 +7497,9 @@ static struct kvm_x86_ops
 vmx_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/vmx.c +3 -0
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0

b0b8abe..02fe194 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);

Quoted 5 lines

arch/x86/kvm/x86.c index

Quoted 4 lines

static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -0
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Ok.

Quoted 9 lines
Quoted 9 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct

Quoted 9 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu)

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -4
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;

arch/x86/kvm/x86.c index static void inject_pending_event(struct · line 5556

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +8 -0
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

Quoted 8 lines
Quoted 1 line

As you mentioned, we still need to checks both extint and apic interrupt in

some case. So how to do this? Introduce another argument to indicate whether check both? Yes, we need to check both in kvm_arch_vcpu_runnable(). Another argument is good option. We can have two functions: kvm_cpu_has_injectable_interrupt() for use in irq injection path and kvm_cpu_has_interrupt() for use in kvm_arch_vcpu_runnable(). They will call common one with additional argument to avoid code duplication.

Ok. will follow this way.

Quoted 59 lines
Quoted 58 lines
Quoted 8 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +1 -0
+
 	}
 }
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 2 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 	}
 
Quoted 11 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu · line 5680

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Yes, it seems ok to move it.

Quoted 26 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic, int

Quoted 18 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int

irq)

Quoted 6 lines
virt/kvm/ioapic.c +0 -1
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1); 	}
 #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
--

1.7.1

Quoted 2 lines

--
Gleb.

Best regards,
Yang

--
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

Quoted 7 lines
Quoted 2 lines

--
Gleb.

Best regards,
Yang

--
Gleb.

#17 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Gleb Natapov wrote on 2012-12-05:

Quoted 44 lines · Yang Z
Quoted 16 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 15 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/lapic.c +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 672

Quoted 3 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the target

vcpu

Quoted 4 lines
Quoted 4 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap.
No need to check target vcpu. Enable exit on all vcpus for the vector

Quoted 6 lines
Quoted 1 line

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

Quoted 3 lines

is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

yes, real HW works in this way. But why it is helpful in this case?

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination

format register and logical destination register. So we must also trap the two registers.

Quoted 1 line

And if it uses lowest priority delivery mode, the PPR need to be trapped too.

Since PPR will change on each interrupt injection, the cost should be higher than current approach.
No need for all of that if bitmask it global.

No, the bitmask is per VCPU. Also, why it will work if bitmask is global?

Quoted 534 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

For irq notifier, only PIT is special which is edge trigger but need an EOI notifier. So, just treat it specially. And TMR can cover others.

We shouldn't assume that. If another notifier will be added it will be easy to forget to update apicv code to exclude another vector too.

At this point, guest is not running(in device initializing), we cannot not know the

vector. As you mentioned, the best point is when guest program ioapic entry. But it also is impossible to get the vector(see above).

Quoted 96 lines

I can give some comments on the function to remind the caller to update eoi bitmap when the interrupt is edge and they still want to get EOI vmexit.

Quoted 92 lines
Quoted 5 lines
Quoted 5 lines

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu

*vcpu1,

Quoted 86 lines
Quoted 27 lines

struct kvm_vcpu *vcpu2)

Quoted 26 lines
arch/x86/kvm/lapic.c +1 -0
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int

vector) +{ + if (!(kvm_apic_get_reg(apic, APIC_SPIV) &
APIC_SPIV_DIRECTED_EOI) && +
kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { + int
trigger_mode; + if (apic_test_vector(vector, apic->regs +
APIC_TMR)) + trigger_mode = IOAPIC_LEVEL_TRIG; + else +

		trigger_mode = IOAPIC_EDGE_TRIG;
(quoted diff) +2 -1
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	} +} +
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)

&& - kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { -

		int trigger_mode; -		if (apic_test_vector(vector, apic->regs +
APIC_TMR)) -			trigger_mode = IOAPIC_LEVEL_TRIG; -		else -
		trigger_mode = IOAPIC_EDGE_TRIG;
arch/x86/kvm/lapic.c +1 -2
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	} +	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
Quoted 8 lines

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic) · line 793

Quoted 7 lines

+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */

arch/x86/kvm/lapic.c +4 -0
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+

trace_kvm_eoi()

Ok.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +5 -0
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1567

Quoted 7 lines

kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) return highest_irr; }

arch/x86/kvm/lapic.c +5 -0
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))

Use kvm_vcpu_has_lapic() instead of checking arch.apic directly.

Ok.

Quoted 33 lines
Quoted 33 lines
arch/x86/kvm/lapic.c +6 -0
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_get_highest_irr);
+
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
 {
 	u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
arch/x86/kvm/lapic.h +3 -0
index c42f111..749661a 100644
@@ -39,6 +39,9 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
+int kvm_cpu_has_extint(struct kvm_vcpu *v);
+int kvm_cpu_get_extint(struct kvm_vcpu *v);
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
 void kvm_lapic_reset(struct kvm_vcpu *vcpu); u64
 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void
 kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); @@

-50,6 +53,8 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu); int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest); int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda); int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);

arch/x86/kvm/lapic.h +2 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int need_eoi, int global);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic

*src,

Quoted 1 line
Quoted 1 line

arch/x86/kvm/lapic.h u64 kvm_get_lapic_tscdeadline_msr(struct

Quoted 1 line
arch/x86/kvm/lapic.h +0 -0
@@ -65,6 +70,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct

kvm_vcpu

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

*vcpu);

Quoted 1 line
arch/x86/kvm/lapic.h +0 -0
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64

data);

Quoted 153 lines
Quoted 153 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/svm.c static void update_cr8_intercept(struct

Quoted 11 lines
arch/x86/kvm/lapic.h +1 -0
 int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
 
 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t
 vapic_addr); void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +0 -0
index dcb7952..8f0903b 100644
@@ -3573,6 +3573,22 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu, int tpr, int irr)

Quoted 27 lines
arch/x86/kvm/svm.c +16 -0
 		set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
 }
+static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static void svm_update_irq(struct kvm_vcpu *vcpu)
+{
+	return ;
+}
+
+static void svm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+				int trig_mode, int always_set)
+{
+	return ;
+}
+
 static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { 	struct
 vcpu_svm *svm = to_svm(vcpu); @@ -4292,6 +4308,9 @@ static struct
 kvm_x86_ops svm_x86_ops = { 	.enable_nmi_window =
 enable_nmi_window, 	.enable_irq_window = enable_irq_window,
arch/x86/kvm/svm.c +3 -0
 	.update_cr8_intercept = update_cr8_intercept,
+	.has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
+	.update_irq = svm_update_irq;
+	.set_eoi_exitmap = svm_set_eoi_exitmap;
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/svm.c +0 -0
 	.set_tss_addr = svm_set_tss_addr,
 	.get_tdp_level = get_npt_level,
arch/x86/kvm/vmx.c +0 -0
index 6a5f651..909ce90 100644
@@ -86,6 +86,9 @@ module_param(fasteoi, bool, S_IRUGO);
Quoted 8 lines

arch/x86/kvm/vmx.c module_param(fasteoi, bool, S_IRUGO);

Quoted 8 lines
arch/x86/kvm/vmx.c +3 -0
 static bool __read_mostly enable_apicv_reg;
 module_param(enable_apicv_reg, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_vid;
+module_param(enable_apicv_vid, bool, S_IRUGO);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may

not

Quoted 56 lines
Quoted 42 lines

arch/x86/kvm/vmx.c static inline bool

Quoted 10 lines
arch/x86/kvm/vmx.c +3 -0
@@ -432,6 +435,9 @@ struct vcpu_vmx {
 
 	bool rdtscp_enabled;
+	u8 eoi_exitmap_changed;
+	u32 eoi_exit_bitmap[8];
+
 	/* Support for a guest hypervisor (nested VMX) */
 	struct nested_vmx nested;
 };
@@ -770,6 +776,12 @@ static inline bool

cpu_has_vmx_apic_register_virt(void)

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

Quoted 12 lines
arch/x86/kvm/vmx.c +6 -0
 		SECONDARY_EXEC_APIC_REGISTER_VIRT;
 }
+static inline bool cpu_has_vmx_virtual_intr_delivery(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2508,7 +2520,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

Quoted 18 lines
arch/x86/kvm/vmx.c +4 -2
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
 			SECONDARY_EXEC_ENABLE_INVPCID |
-			SECONDARY_EXEC_APIC_REGISTER_VIRT;
+			SECONDARY_EXEC_APIC_REGISTER_VIRT |
+			SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2522,7 +2535,8 @@ static __init int setup_vmcs_config(struct
 vmcs_config *vmcs_conf)
 
 	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
 		_cpu_based_2nd_exec_control &= ~(
-				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+				SECONDARY_EXEC_APIC_REGISTER_VIRT |
+				SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 
Quoted 14 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2745

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -0
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
arch/x86/kvm/vmx.c +5 -0
 		/* CR3 accesses and invlpg don't need to cause VM Exits when EPT
 @@ -2724,6 +2738,14 @@ static __init int hardware_setup(void) 	if
 (!cpu_has_vmx_apic_register_virt()) 		enable_apicv_reg = 0;
+	if (!cpu_has_vmx_virtual_intr_delivery())
+		enable_apicv_vid = 0;
+
+	if (!enable_apicv_vid) {
+		kvm_x86_ops->update_irq = NULL;

Why setting it to NULL? Either drop this since vmx_update_irq() checks enable_apicv_vid or better set it to function that does nothing and drop enable_apicv_vid check in vmx_update_irq(). Since kvm_x86_ops->update_irq will never be NULL you can drop the check before calling it.

Quoted 6 lines

Sure.

Quoted 3 lines

arch/x86/kvm/vmx.c static __init int hardware_setup(void) · line 2746

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+		kvm_x86_ops->update_cr8_intercept = NULL;

Why? It should be other way around: if apicv is enabled set update_cr8_intercept callback to NULL.

Yes, this is wrong.

Please test the patches with vid disabled and Windows guests. This bug should have prevented it from working.

Quoted 7 lines
Quoted 7 lines

arch/x86/kvm/vmx.c static u32 vmx_secondary_exec_control(struct

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
+	}
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3839,6 +3861,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx.c static int vmx_vcpu_setup(struct vcpu_vmx

Quoted 8 lines
arch/x86/kvm/vmx.c +2 -0
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
 	if (!enable_apicv_reg)
 		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
+	if (!enable_apicv_vid)
+		exec_control &= ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
 	return exec_control;
 }
@@ -3883,6 +3907,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx

*vmx)

Quoted 16 lines
Quoted 16 lines

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu

Quoted 15 lines
arch/x86/kvm/vmx.c +9 -0
 				vmx_secondary_exec_control(vmx));
 	}
+	if (enable_apicv_vid) {
+		vmcs_write64(EOI_EXIT_BITMAP0, 0);
+		vmcs_write64(EOI_EXIT_BITMAP1, 0);
+		vmcs_write64(EOI_EXIT_BITMAP2, 0);
+		vmcs_write64(EOI_EXIT_BITMAP3, 0);
+
+		vmcs_write16(GUEST_INTR_STATUS, 0);
+	}
+
 	if (ple_gap) {
 		vmcs_write32(PLE_GAP, ple_gap);
 		vmcs_write32(PLE_WINDOW, ple_window);
@@ -4806,6 +4839,16 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx.c +6 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	int vector = exit_qualification & 0xff;
+
+	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx.c static int (*const

Quoted 8 lines
arch/x86/kvm/vmx.c +4 -0
+	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	return 1;
+}
+
 static int handle_apic_write(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
@@ -5755,6 +5798,7 @@ static int (*const

kvm_vmx_exit_handlers[])(struct

Quoted 220 lines
Quoted 177 lines
Quoted 22 lines
Quoted 12 lines

kvm_vcpu *vcpu) = {

Quoted 11 lines
arch/x86/kvm/vmx.c +0 -0
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     =
 handle_tpr_below_threshold, 	[EXIT_REASON_APIC_ACCESS]            
 = handle_apic_access, 	[EXIT_REASON_APIC_WRITE]              =
 handle_apic_write, +	[EXIT_REASON_EOI_INDUCED]             =
 handle_apic_eoi_induced, 	[EXIT_REASON_WBINVD]                  =
 handle_wbinvd, 	[EXIT_REASON_XSETBV]                  =
 handle_xsetbv, 	[EXIT_REASON_TASK_SWITCH]             =
 handle_task_switch,
@@ -6096,6 +6140,11 @@ static int vmx_handle_exit(struct kvm_vcpu
 *vcpu)
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 6147

Quoted 7 lines
arch/x86/kvm/vmx.c +5 -0
 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
+	/* no need for tpr_threshold update if APIC virtual
+	 * interrupt delivery is enabled */
+	if (!enable_apicv_vid)
+		return ;
+

Since you (will) set ->update_cr8_intercept callback to NULL if vid is enabled this function will never be called with !enable_apicv_vid, so this check can be dropped.

Quoted 6 lines

Ok.

Quoted 4 lines

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

Quoted 4 lines
arch/x86/kvm/vmx.c +0 -0
 	if (irr == -1 || tpr < irr) {
 		vmcs_write32(TPR_THRESHOLD, 0);
arch/x86/kvm/vmx.c +0 -0
 		return;
@@ -6104,6 +6153,90 @@ static void update_cr8_intercept(struct

kvm_vcpu

Quoted 110 lines
Quoted 103 lines

*vcpu, int tpr, int irr)

Quoted 102 lines
arch/x86/kvm/vmx.c +84 -0
 	vmcs_write32(TPR_THRESHOLD, irr);
 }
+static int vmx_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
+{
+	return irqchip_in_kernel(vcpu->kvm) && enable_apicv_vid;
+}
+
+static void vmx_update_eoi_exitmap(struct vcpu_vmx *vmx, int index)
+{
+	int tmr;
+	tmr = kvm_apic_get_reg(vmx->vcpu.arch.apic,
+			APIC_TMR + 0x10 * index);
+	vmcs_write32(EOI_EXIT_BITMAP0 + index,
+			vmx->eoi_exit_bitmap[index] | tmr);
+}
+
+static void vmx_update_rvi(int vector)
+{
+	u16 status;
+	u8 old;
+
+	status = vmcs_read16(GUEST_INTR_STATUS);
+	old = (u8)status & 0xff;
+	if ((u8)vector != old) {
+		status &= ~0xff;
+		status |= (u8)vector;
+		vmcs_write16(GUEST_INTR_STATUS, status);
+	}
+}
+
+static void vmx_update_irq(struct kvm_vcpu *vcpu)
+{
+	int vector;
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+	if (!enable_apicv_vid)
+		return ;
+
+	vector = kvm_apic_get_highest_irr(vcpu);
+	if (vector == -1)
+		return;
+
+	vmx_update_rvi(vector);
+
+	if (vmx->eoi_exitmap_changed) {
+		int index;
+		for_each_set_bit(index,
+				(unsigned long *)(&vmx->eoi_exitmap_changed), 8)
+			vmx_update_eoi_exitmap(vmx, index);
+		vmx->eoi_exitmap_changed = 0;
+	}
+}
+
+static void vmx_set_eoi_exitmap(struct kvm_vcpu *vcpu,
+				int vector, int trig_mode,
+				int always_set)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	int index, offset, changed;
+	struct kvm_lapic *apic;
+
+	if (!enable_apicv_vid)
+		return ;
+
+	if (WARN_ONCE((vector < 0) || (vector > 255),
+		"KVM VMX: vector (%d) out of range\n", vector))
+		return;
+
+	apic = vcpu->arch.apic;
+	index = vector >> 5;
+	offset = vector & 31;
+
+	if (always_set)
+		changed = !test_and_set_bit(offset,
+				(unsigned long *)&vmx->eoi_exit_bitmap);
+	else if (trig_mode)
+		changed = !test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+	else
+		changed = test_bit(offset,
+				apic->regs + APIC_TMR + index * 0x10);
+
+	if (changed)
+		vmx->eoi_exitmap_changed |= 1 << index;
+}
+
 static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) { 	u32
 exit_intr_info; @@ -7364,6 +7497,9 @@ static struct kvm_x86_ops
 vmx_x86_ops = { 	.enable_nmi_window = enable_nmi_window,
 	.enable_irq_window = enable_irq_window, 	.update_cr8_intercept =

update_cr8_intercept,

arch/x86/kvm/vmx.c +3 -0
+	.has_virtual_interrupt_delivery = vmx_has_virtual_interrupt_delivery,
+	.update_irq = vmx_update_irq,
+	.set_eoi_exitmap = vmx_set_eoi_exitmap,
 
 	.set_tss_addr = vmx_set_tss_addr,
 	.get_tdp_level = get_ept_level,
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0

b0b8abe..02fe194 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -164,6 +164,14 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);

Quoted 5 lines

arch/x86/kvm/x86.c index

Quoted 4 lines

static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);

arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -0
+static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->has_virtual_interrupt_delivery)

This callback is never NULL.

Ok.

Quoted 9 lines
Quoted 9 lines

arch/x86/kvm/x86.c index static void inject_pending_event(struct

Quoted 9 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
+		return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
+
+	return 0;
+}
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -5533,12 +5541,20 @@ static void inject_pending_event(struct

kvm_vcpu

Quoted 28 lines
Quoted 28 lines

*vcpu)

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +3 -4
 			vcpu->arch.nmi_injected = true;
 			kvm_x86_ops->set_nmi(vcpu);
 		}
-	} else if (kvm_cpu_has_interrupt(vcpu)) {
-		if (kvm_x86_ops->interrupt_allowed(vcpu)) {
-			kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
-					    false);
+	} else if (kvm_cpu_has_interrupt(vcpu) &&
+			kvm_x86_ops->interrupt_allowed(vcpu)) {
+		int vector = -1;

arch/x86/kvm/x86.c index static void inject_pending_event(struct · line 5556

Quoted 10 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +8 -0
+
+		if (kvm_apic_vid_enabled(vcpu))
+			vector = kvm_cpu_get_extint(vcpu);
+		else
+			vector = kvm_cpu_get_interrupt(vcpu);
+
+		if (vector != -1) {
+			kvm_queue_interrupt(vcpu, vector, false);
 			kvm_x86_ops->set_irq(vcpu);
 		}

If vid is enabled kvm_cpu_has_interrupt() should return true only if there is extint interrupt. Similarly kvm_cpu_get_interrupt() will only return extint if vid is enabled. This basically moves kvm_apic_vid_enabled() logic deeper into kvm_cpu_(has|get)_interrupt() functions instead of changing interrupt injection logic here and in vcpu_enter_guest() bellow. We still need kvm_cpu_has_interrupt() variant that always checks both extint and apic for use in kvm_arch_vcpu_runnable() though.

Quoted 8 lines
Quoted 1 line

As you mentioned, we still need to checks both extint and apic interrupt in

some case. So how to do this? Introduce another argument to indicate whether check both? Yes, we need to check both in kvm_arch_vcpu_runnable(). Another argument is good option. We can have two functions: kvm_cpu_has_injectable_interrupt() for use in irq injection path and kvm_cpu_has_interrupt() for use in kvm_arch_vcpu_runnable(). They will call common one with additional argument to avoid code duplication.

Ok. will follow this way.

Quoted 33 lines
Quoted 33 lines
Quoted 8 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +1 -0
+
 	}
 }
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 2 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 	}
 
Quoted 11 lines

arch/x86/kvm/x86.c index static int vcpu_enter_guest(struct kvm_vcpu · line 5680

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +5 -0
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);
+

I do not see why this have to be here instead of inside if (kvm_lapic_enabled(vcpu)){} near update_cr8_intercept() a couple of lines bellow. If you move it there you can drop apic enable check in kvm_apic_get_highest_irr().

Yes, it seems ok to move it.

Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +4 -1
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);
+		} else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

virt/kvm/ioapic.c static int ioapic_deliver(struct kvm_ioapic *ioapic,

Quoted 6 lines
arch/x86/kvm/x86.c index renamed from arch/x86/kvm/x86.c +0 -0
 		if (kvm_lapic_enabled(vcpu)) {
virt/kvm/ioapic.c +0 -0
index 166c450..898aa62 100644
@@ -186,6 +186,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic,

int

Quoted 25 lines
Quoted 23 lines
Quoted 13 lines
Quoted 11 lines

irq)

Quoted 7 lines
virt/kvm/ioapic.c +0 -1
 		/* need to read apic_id from apic regiest since 		 * it can be
 rewritten */ 		irqe.dest_id = ioapic->kvm->bsp_vcpu_id;
 +		kvm_set_eoi_exitmap(ioapic->kvm->vcpus[0], irqe.vector, 1, 1);
 	} #endif 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL,
 &irqe);
--

1.7.1

--
Gleb.

Quoted 7 lines

Best regards,
Yang

--
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

--
Gleb.

Quoted 6 lines
Quoted 3 lines

Best regards,
Yang

--
Gleb.

Best regards,
Yang

#18 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Quoted 45 lines · Yang Z

Gleb Natapov wrote on 2012-12-05:

Quoted 44 lines · Yang Z
Quoted 16 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 15 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct kvm_lapic · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the target

vcpu

Quoted 4 lines
Quoted 4 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap.
No need to check target vcpu. Enable exit on all vcpus for the vector

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Quoted 4 lines
Quoted 3 lines

is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

yes, real HW works in this way. But why it is helpful in this case?

It makes it impossible to use the same vector for different devices on different cpus if the vector is delivered to at least one cpu through ioapic. It may cause spurious interrupts, it will bring havoc to our ack notifiers (albeit this is KVM's implementation problem). Also look at various comment in arch/x86/kernel/apic/io_apic.c, it looks like ioapics tend to misbehave if you look at them funny. Who knows what troubles EOIing the same vector twice on real HW may bring.

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination

format register and logical destination register. So we must also trap the two registers.

Quoted 1 line

And if it uses lowest priority delivery mode, the PPR need to be trapped too.

Since PPR will change on each interrupt injection, the cost should be higher than current approach.
No need for all of that if bitmask it global.

No, the bitmask is per VCPU. Also, why it will work if bitmask is global?

Make in global. Why what will work?

And we need to trap format/logical destination/id registers anyway since we need to build kvm->arch.apic_map table that is used to deliver interrupts. BTW you can use this table to build per VCPU eoi bitmask too, but I am not convinced it is needed in practice.

--
Gleb.

#29 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Zhang, Yang Z
Quoted 23 lines · Yang Z
Quoted 23 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 7 lines
Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the “interrupt-window exiting” VM-execution control is 0."

Right. Must check RVI here.

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

--
Gleb.

#19 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-05:

Quoted 53 lines · Yang Z
Quoted 40 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 39 lines · Yang Z
Quoted 16 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 15 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct kvm_lapic · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

target

Quoted 11 lines
Quoted 11 lines

vcpu

Quoted 6 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Yes, when create an new irq, it will allocate vector from all online cpus. But after user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 26 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Quoted 4 lines
Quoted 3 lines

is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

yes, real HW works in this way. But why it is helpful in this case?

It makes it impossible to use the same vector for different devices on different cpus if the vector is delivered to at least one cpu through ioapic. It may cause spurious interrupts, it will bring havoc to our ack notifiers (albeit this is KVM's implementation problem). Also look at various comment in arch/x86/kernel/apic/io_apic.c, it looks like ioapics tend to misbehave if you look at them funny. Who knows what troubles EOIing the same vector twice on real HW may bring.

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination

format register and logical destination register. So we must also trap the two registers.

Quoted 1 line

And if it uses lowest priority delivery mode, the PPR need to be trapped too.

Since PPR will change on each interrupt injection, the cost should be higher than current approach. No need for all of that if bitmask it global.

Quoted 7 lines
Quoted 1 line

No, the bitmask is per VCPU. Also, why it will work if bitmask is global?

Make in global. Why what will work?

And we need to trap format/logical destination/id registers anyway since we need to build kvm->arch.apic_map table that is used to deliver interrupts. BTW you can use this table to build per VCPU eoi bitmask too, but I am not convinced it is needed in practice.

Even KVM uses a simple way to implement the lowest priority delivery mode, we still need to trap all interrupts that use the lowest priority delivery mode. Because each interrupt will change CPU's priority and we need to recalculate the priority and iterate the whole ioapic entry to renew the eoi exiting bitmap. The cost should be worse than current way. I don't think it worth to do.

Best regards,
Yang

#30 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-05:

Quoted 29 lines · Yang Z
Quoted 28 lines
Quoted 28 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the "interrupt-window exiting" VM-execution control is 0."

Quoted 8 lines
Quoted 2 lines

Right. Must check RVI here.

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are

allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

One queston: kvm_cpu_has_extint() function check the interrupt from PIC. If PIC is working, APIC must in virtual wire mode. According to spec, when APIC is virtual wire mode, then APIC is totally bypassing. So apic_has_interrupt() and pic_has_interrupt() are mutually exclusive. If kvm_apic_has_interrupt() return true, then kvm_cpu_has_extint will never return true, and vice versa. Am I right? If answer is yes, then we don't check RVI here.

Quoted 2 lines

--
Gleb.

Best regards,
Yang

#20 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Zhang, Yang Z
Quoted 55 lines · Yang Z

Gleb Natapov wrote on 2012-12-05:

Quoted 54 lines · Yang Z
Quoted 40 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 39 lines · Yang Z
Quoted 16 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 15 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 13 lines · Yang Zhang
Quoted 10 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct kvm_lapic · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

target

Quoted 12 lines
Quoted 12 lines

vcpu

Quoted 6 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Yes, when create an new irq, it will allocate vector from all online cpus. But after user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Can you answer this?

Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 3 lines

is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

yes, real HW works in this way. But why it is helpful in this case?

It makes it impossible to use the same vector for different devices on different cpus if the vector is delivered to at least one cpu through ioapic. It may cause spurious interrupts, it will bring havoc to our ack notifiers (albeit this is KVM's implementation problem). Also look at various comment in arch/x86/kernel/apic/io_apic.c, it looks like ioapics tend to misbehave if you look at them funny. Who knows what troubles EOIing the same vector twice on real HW may bring.

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's destination

format register and logical destination register. So we must also trap the two registers.

Quoted 1 line

And if it uses lowest priority delivery mode, the PPR need to be trapped too.

Since PPR will change on each interrupt injection, the cost should be higher than current approach. No need for all of that if bitmask it global.

Quoted 9 lines
Quoted 7 lines
Quoted 1 line

No, the bitmask is per VCPU. Also, why it will work if bitmask is global?

Make in global. Why what will work?

And we need to trap format/logical destination/id registers anyway since we need to build kvm->arch.apic_map table that is used to deliver interrupts. BTW you can use this table to build per VCPU eoi bitmask too, but I am not convinced it is needed in practice.

Even KVM uses a simple way to implement the lowest priority delivery mode, we still need to trap all interrupts that use the lowest priority delivery mode. Because each interrupt will change CPU's priority and we need to recalculate the priority and iterate the whole ioapic entry to renew the eoi exiting bitmap. The cost should be worse than current way. I don't think it worth to do.

Just set the bit on every vcpu that can get interrupt with lowest priority.

--
Gleb.

#31 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Zhang, Yang Z
Quoted 32 lines · Yang Z

Gleb Natapov wrote on 2012-12-05:

Quoted 31 lines · Yang Z
Quoted 30 lines
Quoted 23 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the "interrupt-window exiting" VM-execution control is 0."

Right. Must check RVI here.

Quoted 7 lines
Quoted 6 lines

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are

allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

One queston: kvm_cpu_has_extint() function check the interrupt from PIC. If PIC is working, APIC must in virtual wire mode. According to spec, when APIC is virtual wire mode, then APIC is totally bypassing. So apic_has_interrupt() and pic_has_interrupt() are mutually exclusive. If kvm_apic_has_interrupt() return true, then kvm_cpu_has_extint will never return true, and vice versa. Am I right? If answer is yes, then we don't check RVI here.

According to what spec "when APIC is virtual wire mode, then APIC is totally bypassed"? SDP volume 3 section 10.8.2 says differently, but it gives priority to ExtINT interrupts. And we do it other way around currently, but your apicv patches actually do it correct by injecting ExtINT if it is asserted without considering apic state.

--
Gleb.

#36 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Yang Zhang
Quoted 254 lines · Yang Zhang

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.

- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
 arch/x86/include/asm/kvm_host.h |    4 +
 arch/x86/include/asm/vmx.h      |   11 +++
 arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++
 arch/x86/kvm/svm.c              |   19 +++++
 arch/x86/kvm/vmx.c              |  140 ++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |   34 ++++++++--
 virt/kvm/ioapic.c               |    1 +
 9 files changed, 291 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index dc87b65..e5352c8 100644
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +11 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41
 #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
 #define EXIT_REASON_APIC_ACCESS         44
+#define EXIT_REASON_EOI_INDUCED         45
 #define EXIT_REASON_EPT_VIOLATION       48
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD              54
@@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040
 #define SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080
 #define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
+#define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
 
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a,
 	GUEST_LDTR_SELECTOR             = 0x0000080c,
 	GUEST_TR_SELECTOR               = 0x0000080e,
+	GUEST_INTR_STATUS               = 0x00000810,
 	HOST_ES_SELECTOR                = 0x00000c00,
 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04,
@@ -207,6 +210,14 @@ enum vmcs_field {
 	APIC_ACCESS_ADDR_HIGH		= 0x00002015,
 	EPT_POINTER                     = 0x0000201a,
 	EPT_POINTER_HIGH                = 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -17
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
 
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +36 -9
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
 
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
 		if (unlikely(!apic_enabled(apic)))
 			break;
 
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
 
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic)
 {
 	int vector = apic_find_highest_isr(apic);
@@ -756,19 +778,24 @@ static int apic_set_eoi(struct kvm_lapic *apic)
 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
 
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
 
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic)
 {
 	u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
@@ -1533,6 +1560,17 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
 	return highest_irr;
 }

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1571

Quoted 10 lines
arch/x86/kvm/lapic.c +9 -0
 
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

#33 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov
Quoted 31 lines · Gleb Natapov
Quoted 23 lines · Yang Z
Quoted 23 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 7 lines
Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the “interrupt-window exiting” VM-execution control is 0."

Right. Must check RVI here.

Quoted 8 lines

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

--
Gleb.

Right. BTW, delivery of ExtINT has no EOI, so there is no evaluation of pending virtual interrupts. Therefore, shouldnt interrupt window be enabled when injecting ExtINT so that evaluation of pending virtual interrupts is performed on next vm-entry?

#9 lore raw

Re: [PATCH v3 2/4] x86, apicv: add APICv register virtualization support

In reply to Zhang, Yang Z
Quoted 61 lines · Yang Z

Marcelo Tosatti wrote on 2012-12-05:

Quoted 40 lines · Yang Zhang
Quoted 39 lines

- APIC read doesn't cause VM-Exit
- APIC write becomes trap-like

---
 arch/x86/include/asm/vmx.h |    2 ++ arch/x86/kvm/lapic.c       |   16
 ++++++++++++++++ arch/x86/kvm/lapic.h       |    2 ++

arch/x86/kvm/vmx.c | 32 +++++++++++++++++++++++++++++++- 4
files changed, 51 insertions(+), 1 deletions(-)

arch/x86/include/asm/vmx.h +0 -0
index 36ec21c..21101b6 100644
@@ -66,6 +66,7 @@
 #define EXIT_REASON_EPT_MISCONFIG       49 #define EXIT_REASON_WBINVD 
             54 #define EXIT_REASON_XSETBV              55 +#define
 EXIT_REASON_APIC_WRITE          56 #define EXIT_REASON_INVPCID        
     58
 
 #define VMX_EXIT_REASONS \ @@ -141,6 +142,7 @@ #define
 SECONDARY_EXEC_ENABLE_VPID              0x00000020 #define
 SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 +#define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
arch/x86/kvm/lapic.c +5 -0
index 9392f52..7c96012 100644
@@ -1212,6 +1212,22 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
+/* emulate APIC access in a trap manner */
+int kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
+{
+	u32 val = 0;
+
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) · line 1224

Quoted 5 lines
arch/x86/kvm/lapic.c +5 -0
+	/* hw has done the conditional check and inst decode */
+	offset &= 0xff0;
+	if ((offset != APIC_EOI) &&
+	     apic_reg_read(vcpu->arch.apic, offset, 4, &val))
+		return 1;

What is apic_reg_read doing? None of the checks it performs that can result in return value of 1 are necessary for APIC-write VM-exit AFAICS.

Right. We should remove this check.

Quoted 9 lines

arch/x86/kvm/lapic.c module_param(vmm_exclusive, bool, S_IRUGO); · line 87

Quoted 6 lines
arch/x86/kvm/lapic.c +3 -0
@@ -83,6 +83,9 @@ module_param(vmm_exclusive, bool, S_IRUGO);
 static bool __read_mostly fasteoi = 1;
 module_param(fasteoi, bool, S_IRUGO);
+static bool __read_mostly enable_apicv_reg;
+module_param(enable_apicv_reg, bool, S_IRUGO);
+

Are the different combinations of register virtualization / virtual interrupt delivery actually supported?

Quoted 5 lines

Yes.

Quoted 2 lines

Why would it be useful to enable register virtualization / virtual interrupt delivery separately?

It's ok to use one option: enable_apicv.

Why would someone (as a user, not developer), would ever want different combinations?

Allowing different combinations means they must be supported.

Why would someone enable virtual interrupt delivery without apic register virtualization, for example?

Perhaps one module option to control apic register virtualization + virtual interrupt delivery, and another for posted-interrupts (which can be useful for debugging).

Quoted 68 lines
Quoted 56 lines

arch/x86/kvm/lapic.c static inline bool

Quoted 3 lines
arch/x86/kvm/lapic.c +0 -0
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -761,6 +764,12 @@ static inline bool

cpu_has_vmx_virtualize_apic_accesses(void)

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

Quoted 12 lines
arch/x86/kvm/lapic.c +6 -0
 		SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
 }
+static inline bool cpu_has_vmx_apic_register_virt(void)
+{
+	return vmcs_config.cpu_based_2nd_exec_ctrl &
+		SECONDARY_EXEC_APIC_REGISTER_VIRT;
+}
+
 static inline bool cpu_has_vmx_flexpriority(void)
 {
 	return cpu_has_vmx_tpr_shadow() &&
@@ -2498,7 +2507,8 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

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

Quoted 10 lines
arch/x86/kvm/lapic.c +2 -1
 			SECONDARY_EXEC_UNRESTRICTED_GUEST |
 			SECONDARY_EXEC_PAUSE_LOOP_EXITING |
 			SECONDARY_EXEC_RDTSCP |
-			SECONDARY_EXEC_ENABLE_INVPCID;
+			SECONDARY_EXEC_ENABLE_INVPCID |
+			SECONDARY_EXEC_APIC_REGISTER_VIRT;
 		if (adjust_vmx_controls(min2, opt2,
 					MSR_IA32_VMX_PROCBASED_CTLS2,
 					&_cpu_based_2nd_exec_control) < 0)
@@ -2509,6 +2519,11 @@ static __init int setup_vmcs_config(struct

vmcs_config *vmcs_conf)

arch/x86/kvm/lapic.c static u32 vmx_secondary_exec_control(struct

Quoted 18 lines
arch/x86/kvm/lapic.c +5 -0
 				SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
 		_cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
 #endif
+
+	if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
+		_cpu_based_2nd_exec_control &= ~(
+				SECONDARY_EXEC_APIC_REGISTER_VIRT);
+
 	if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { 		/*
 CR3 accesses and invlpg don't need to cause VM Exits when EPT 		  
 enabled */ @@ -2706,6 +2721,9 @@ static __init int

hardware_setup(void) if (!cpu_has_vmx_ple()) ple_gap = 0;

arch/x86/kvm/lapic.c +3 -0
+	if (!cpu_has_vmx_apic_register_virt())
+		enable_apicv_reg = 0;
+
 	if (nested)
 		nested_vmx_setup_ctls_msrs();
@@ -3819,6 +3837,8 @@ static u32 vmx_secondary_exec_control(struct

vcpu_vmx *vmx)

arch/x86/kvm/lapic.c static int handle_apic_access(struct kvm_vcpu

Quoted 8 lines
arch/x86/kvm/lapic.c +2 -0
 		exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
 	if (!ple_gap)
 		exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
+	if (!enable_apicv_reg)
+		exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
 	return exec_control;
 }
@@ -4786,6 +4806,15 @@ static int handle_apic_access(struct kvm_vcpu

*vcpu)

Quoted 12 lines

arch/x86/kvm/lapic.c static int handle_apic_access(struct kvm_vcpu · line 4815

Quoted 10 lines
arch/x86/kvm/lapic.c +8 -0
 	return emulate_instruction(vcpu, 0) == EMULATE_DONE;
 }
+static int handle_apic_write(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	u32 offset = exit_qualification & 0xfff;
+
+	/* APIC-write VM exit is trap-like and thus no need to adjust IP */
+	return kvm_apic_write_nodecode(vcpu, offset) == 0;
+}

Point of return value == 0?

Quoted 2 lines

if kvm_apic_write_nodecode() handle successfully, it will return zero. Then it will return 1 for this vmexit handle. What's wrong?

If kvm_apic_write_nodecode fails (return 0 for the vmexit handle), there is an exit to userspace. Why is that necessary?

#21 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-05:

Quoted 64 lines · Yang Z
Quoted 63 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 62 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 40 lines · Yang Z
Quoted 17 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 16 lines · Yang Z
Quoted 15 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 14 lines · Yang Zhang
Quoted 11 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct kvm_lapic · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

target

Quoted 6 lines
Quoted 6 lines

vcpu

Quoted 5 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Quoted 6 lines
Quoted 1 line

Yes, when create an new irq, it will allocate vector from all online cpus. But after

user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 1 line

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Sorry, I still cannot find this check in kernel. Can you point me out to it? Also, I do see the vector reused by MSI and IOAPIC in my system.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Can you answer this?

I am sure Xen will reused the IOAPIC vector.

Quoted 37 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 3 lines

is not how Linux code works. Furthermore it will not work with KVM currently since apic eoi redirected to ioapic based on vector alone, not vector/vcpu pair and as far as I am aware this is how real HW works.

yes, real HW works in this way. But why it is helpful in this case?

It makes it impossible to use the same vector for different devices on different cpus if the vector is delivered to at least one cpu through ioapic. It may cause spurious interrupts, it will bring havoc to our ack notifiers (albeit this is KVM's implementation problem). Also look at various comment in arch/x86/kernel/apic/io_apic.c, it looks like ioapics tend to misbehave if you look at them funny. Who knows what troubles EOIing the same vector twice on real HW may bring.

Quoted 1 line
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

programmed into ioapic. Which two registers? All accesses to ioapic are trapped and reconfiguration is rare.

In logical mode, the destination VCPU is depend on each CPU's

destination

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

format register and logical destination register. So we must also trap the two registers.

Quoted 1 line

And if it uses lowest priority delivery mode, the PPR need to be trapped

too.

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Quoted 3 lines

Since PPR will change on each interrupt injection, the cost should be higher than current approach. No need for all of that if bitmask it global.

No, the bitmask is per VCPU. Also, why it will work if bitmask is global?

Make in global. Why what will work?

And we need to trap format/logical destination/id registers anyway since we need to build kvm->arch.apic_map table that is used to deliver interrupts. BTW you can use this table to build per VCPU eoi bitmask too, but I am not convinced it is needed in practice.

Even KVM uses a simple way to implement the lowest priority delivery mode,

we still need to trap all interrupts that use the lowest priority delivery mode. Because each interrupt will change CPU's priority and we need to recalculate the priority and iterate the whole ioapic entry to renew the eoi exiting bitmap. The cost should be worse than current way. I don't think it worth to do.

Quoted 1 line
Quoted 4 lines

Just set the bit on every vcpu that can get interrupt with lowest priority.

--
Gleb.

Best regards,
Yang

#32 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-05:

Quoted 40 lines · Yang Z
Quoted 32 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 31 lines · Yang Z
Quoted 30 lines
Quoted 23 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the "interrupt-window exiting" VM-execution control is 0."

Right. Must check RVI here.

Quoted 7 lines
Quoted 6 lines

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are

allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

One queston: kvm_cpu_has_extint() function check the interrupt from PIC. If

Quoted 10 lines

PIC is working, APIC must in virtual wire mode. According to spec, when APIC is virtual wire mode, then APIC is totally bypassing. So apic_has_interrupt() and pic_has_interrupt() are mutually exclusive. If kvm_apic_has_interrupt() return true, then kvm_cpu_has_extint will never return true, and vice versa. Am I right? If answer is yes, then we don't check RVI here.
According to what spec "when APIC is virtual wire mode, then APIC is totally bypassed"? SDP volume 3 section 10.8.2 says differently, but it gives priority to ExtINT interrupts. And we do it other way around currently, but your apicv patches actually do it correct by injecting ExtINT if it is asserted without considering apic state.

Thanks for correct me. I misunderstood the spec. :(

Quoted 6 lines

--
Gleb.
--
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

Best regards,
Yang

#37 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Marcelo Tosatti

Marcelo Tosatti wrote on 2012-12-06:

Quoted 234 lines · Yang Zhang

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 172 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest.
- A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c          
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h

arch/x86/include/asm/kvm_host.h +4 -0
@@ -697,6 +697,10 @@ struct kvm_x86_ops {
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
arch/x86/include/asm/vmx.h +8 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define EXIT_REASON_APIC_ACCESS    
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define EXIT_REASON_EPT_MISCONFIG  
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6 +144,7 @@
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000
@@ -180,6 +182,7 @@ enum vmcs_field {
 	GUEST_GS_SELECTOR               = 0x0000080a, 	GUEST_LDTR_SELECTOR   
          = 0x0000080c, 	GUEST_TR_SELECTOR               = 0x0000080e,
 +	GUEST_INTR_STATUS               = 0x00000810, 	HOST_ES_SELECTOR     
           = 0x00000c00, 	HOST_CS_SELECTOR                = 0x00000c02,
 	HOST_SS_SELECTOR                = 0x00000c04, @@ -207,6 +210,14 @@
 enum vmcs_field { 	APIC_ACCESS_ADDR_HIGH		= 0x00002015, 	EPT_POINTER  
                   = 0x0000201a, 	EPT_POINTER_HIGH                =
 0x0000201b,
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 7 lines
arch/x86/kvm/lapic.c +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 52 lines
arch/x86/kvm/lapic.c +31 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1571

Quoted 5 lines
arch/x86/kvm/lapic.c +5 -0
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

Quoted 3 lines

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

Best regards,
Yang

#38 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Marcelo Tosatti

Zhang, Yang Z wrote on 2012-12-06:

Quoted 251 lines

Marcelo Tosatti wrote on 2012-12-06:

Quoted 29 lines · Yang Zhang
Quoted 28 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -697,6 +697,10 @@ struct kvm_x86_ops {

arch/x86/include/asm/kvm_host.h +0 -0
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
Quoted 8 lines

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops {

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -0
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool

is_mmio);

Quoted 7 lines

arch/x86/include/asm/vmx.h line 63

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define

EXIT_REASON_APIC_ACCESS

Quoted 2 lines

arch/x86/include/asm/vmx.h line 65

Quoted 2 lines
arch/x86/include/asm/vmx.h +0 -0
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define

EXIT_REASON_EPT_MISCONFIG

Quoted 1 line

arch/x86/include/asm/vmx.h line 66

Quoted 1 line
arch/x86/include/asm/vmx.h +0 -0
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6

+144,7 @@

Quoted 7 lines

arch/x86/include/asm/vmx.h

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000 @@ -180,6 +182,7 @@ enum
 vmcs_field { 	GUEST_GS_SELECTOR               = 0x0000080a,

GUEST_LDTR_SELECTOR

Quoted 1 line
Quoted 1 line
          = 0x0000080c, 	GUEST_TR_SELECTOR               =

0x0000080e,

Quoted 1 line
Quoted 1 line

+ GUEST_INTR_STATUS = 0x00000810,

HOST_ES_SELECTOR

Quoted 1 line
Quoted 1 line
           = 0x00000c00, 	HOST_CS_SELECTOR                =

0x00000c02,

Quoted 2 lines
Quoted 2 lines

HOST_SS_SELECTOR = 0x00000c04, @@ -207,6 +210,14 @@
enum vmcs_field { APIC_ACCESS_ADDR_HIGH = 0x00002015,

EPT_POINTER

Quoted 1 line
Quoted 1 line
                   = 0x0000201a, 	EPT_POINTER_HIGH

=

Quoted 172 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 113 lines

0x0000201b,

arch/x86/include/asm/vmx.h +8 -0
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (unlikely(!apic_enabled(apic))) 			break;
 +		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0); 		if (trig_mode)
 { 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 52 lines
arch/x86/kvm/lapic.c +31 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines

arch/x86/kvm/lapic.c int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) · line 1571

Quoted 5 lines
arch/x86/kvm/lapic.c +5 -0
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

Quoted 2 lines

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

One problem is that when HW clears VIRR, after vmexit, the irr_peding will still true if there are more than one bit is set in VIRR in last vmentry. How about to recaculate irr_pending according the VIRR on each vmexit?

Best regards,
Yang

#39 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Quoted 252 lines · Yang Z

Zhang, Yang Z wrote on 2012-12-06:

Quoted 251 lines

Marcelo Tosatti wrote on 2012-12-06:

Quoted 29 lines · Yang Zhang
Quoted 28 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -697,6 +697,10 @@ struct kvm_x86_ops {

arch/x86/include/asm/kvm_host.h +0 -0
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
Quoted 8 lines

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops {

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -0
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool

is_mmio);

Quoted 7 lines

arch/x86/include/asm/vmx.h line 63

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define

EXIT_REASON_APIC_ACCESS

Quoted 2 lines

arch/x86/include/asm/vmx.h line 65

Quoted 2 lines
arch/x86/include/asm/vmx.h +0 -0
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define

EXIT_REASON_EPT_MISCONFIG

Quoted 1 line

arch/x86/include/asm/vmx.h line 66

Quoted 1 line
arch/x86/include/asm/vmx.h +0 -0
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6

+144,7 @@

Quoted 7 lines

arch/x86/include/asm/vmx.h

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000 @@ -180,6 +182,7 @@ enum
 vmcs_field { 	GUEST_GS_SELECTOR               = 0x0000080a,

GUEST_LDTR_SELECTOR

Quoted 1 line
Quoted 1 line
          = 0x0000080c, 	GUEST_TR_SELECTOR               =

0x0000080e,

Quoted 1 line
Quoted 1 line

+ GUEST_INTR_STATUS = 0x00000810,

HOST_ES_SELECTOR

Quoted 1 line
Quoted 1 line
           = 0x00000c00, 	HOST_CS_SELECTOR                =

0x00000c02,

Quoted 2 lines
Quoted 2 lines

HOST_SS_SELECTOR = 0x00000c04, @@ -207,6 +210,14 @@
enum vmcs_field { APIC_ACCESS_ADDR_HIGH = 0x00002015,

EPT_POINTER

Quoted 1 line
Quoted 1 line
                   = 0x0000201a, 	EPT_POINTER_HIGH

=

Quoted 180 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 113 lines

0x0000201b,

arch/x86/include/asm/vmx.h +8 -0
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (unlikely(!apic_enabled(apic))) 			break;
 +		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0); 		if (trig_mode)
 { 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 47 lines
arch/x86/kvm/lapic.c +27 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 10 lines
arch/x86/kvm/lapic.c +9 -0
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

Quoted 3 lines
Quoted 2 lines

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

One problem is that when HW clears VIRR, after vmexit, the irr_peding will still true if there are more than one bit is set in VIRR in last vmentry.

It doesn't matter how much bits were set in VIRR before entry, as long as some bit were set irr_peding will be true.

Quoted 2 lines

How about to recaculate irr_pending according the VIRR on each vmexit?

No need really. Since HW can only clear VIRR the only situation that may happen is that irr_pending will be true but VIRR is empty and apic_find_highest_irr() will return correct result in this case.

If we will see a lot of unneeded irr scans because of stale irr_pending value we can do irr_pending = rvi != 0 on vmexit.

--
Gleb.

#10 lore raw

Re: [PATCH v3 2/4] x86, apicv: add APICv register virtualization support

In reply to Marcelo Tosatti
Quoted 10 lines · Marcelo Tosatti
Quoted 10 lines
Quoted 10 lines

arch/x86/kvm/vmx.c static int handle_apic_access(struct kvm_vcpu *vcpu) · line 4816

Quoted 8 lines
arch/x86/kvm/vmx.c +8 -0
+static int handle_apic_write(struct kvm_vcpu *vcpu)
+{
+	unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
+	u32 offset = exit_qualification & 0xfff;
+
+	/* APIC-write VM exit is trap-like and thus no need to adjust IP */
+	return kvm_apic_write_nodecode(vcpu, offset) == 0;
+}

Point of return value == 0?

Quoted 5 lines
Quoted 2 lines

if kvm_apic_write_nodecode() handle successfully, it will return zero. Then it will return 1 for this vmexit handle. What's wrong?

If kvm_apic_write_nodecode fails (return 0 for the vmexit handle), there is an exit to userspace. Why is that necessary?

kvm_apic_write_nodecode() returns apic_reg_write() value. apic_reg_write() returns 1 when #GP should be injected if used from MSR, if used from mmio apic_reg_write() is ignored since errors cannot be reported to a cpu. Since kvm_apic_write_nodecode() is used for mmio only make it void and ignore apic_reg_write() return value.

--
Gleb.

#40 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov

Gleb Natapov wrote on 2012-12-06:

Quoted 257 lines · Yang Z
Quoted 17 lines

Zhang, Yang Z wrote on 2012-12-06:

Quoted 16 lines

Marcelo Tosatti wrote on 2012-12-06:

Quoted 15 lines · Yang Zhang
Quoted 14 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
 arch/x86/include/asm/kvm_host.h |    4 +

arch/x86/include/asm/vmx.h

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
   |   11 +++ arch/x86/kvm/irq.c              |   53

++++++++++-----

Quoted 159 lines
Quoted 159 lines
Quoted 13 lines
Quoted 13 lines
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -697,6 +697,10 @@ struct kvm_x86_ops {

arch/x86/include/asm/kvm_host.h +0 -0
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
Quoted 8 lines

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops {

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -0
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool

is_mmio);

Quoted 7 lines

arch/x86/include/asm/vmx.h line 63

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define

EXIT_REASON_APIC_ACCESS

Quoted 2 lines

arch/x86/include/asm/vmx.h line 65

Quoted 2 lines
arch/x86/include/asm/vmx.h +0 -0
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define

EXIT_REASON_EPT_MISCONFIG

Quoted 1 line

arch/x86/include/asm/vmx.h line 66

Quoted 1 line
arch/x86/include/asm/vmx.h +0 -0
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6

+144,7 @@

Quoted 7 lines

arch/x86/include/asm/vmx.h

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000 @@ -180,6 +182,7 @@ enum
 vmcs_field { 	GUEST_GS_SELECTOR               = 0x0000080a,

GUEST_LDTR_SELECTOR

Quoted 1 line
Quoted 1 line
          = 0x0000080c, 	GUEST_TR_SELECTOR               =

0x0000080e,

Quoted 1 line
Quoted 1 line

+ GUEST_INTR_STATUS = 0x00000810,

HOST_ES_SELECTOR

Quoted 1 line
Quoted 1 line
           = 0x00000c00, 	HOST_CS_SELECTOR                =

0x00000c02,

Quoted 2 lines
Quoted 2 lines

HOST_SS_SELECTOR = 0x00000c04, @@ -207,6 +210,14 @@
enum vmcs_field { APIC_ACCESS_ADDR_HIGH = 0x00002015,

EPT_POINTER

Quoted 1 line
Quoted 1 line
                   = 0x0000201a, 	EPT_POINTER_HIGH

=

Quoted 105 lines
Quoted 105 lines

0x0000201b,

arch/x86/include/asm/vmx.h +8 -0
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +4 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic,

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic,

int

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (unlikely(!apic_enabled(apic))) 			break;
 +		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0); 		if
 (trig_mode) { 			apic_debug("level trig mode for vector %d",
 vector); 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu

*vcpu1,

Quoted 54 lines
Quoted 54 lines
Quoted 54 lines

struct kvm_vcpu *vcpu2)

Quoted 40 lines
arch/x86/kvm/lapic.c +2 -0
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{ +	if (!(kvm_apic_get_reg(apic, APIC_SPIV) &

APIC_SPIV_DIRECTED_EOI) && +
kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { + int
trigger_mode; + if (apic_test_vector(vector, apic->regs +
APIC_TMR)) + trigger_mode = IOAPIC_LEVEL_TRIG; + else

arch/x86/kvm/lapic.c +3 -1
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode); +	}
+} +
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)

&& - kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {

arch/x86/kvm/lapic.c +0 -1
-		int trigger_mode; -		if (apic_test_vector(vector, apic->regs +

APIC_TMR)) - trigger_mode = IOAPIC_LEVEL_TRIG; - else

arch/x86/kvm/lapic.c +14 -2
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode); -	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic)

Quoted 10 lines
arch/x86/kvm/lapic.c +9 -0
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

Quoted 3 lines
Quoted 2 lines

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

One problem is that when HW clears VIRR, after vmexit, the irr_peding will still

true if there are more than one bit is set in VIRR in last vmentry. It doesn't matter how much bits were set in VIRR before entry, as long as some bit were set irr_peding will be true.

Quoted 8 lines
Quoted 2 lines

How about to recaculate irr_pending according the VIRR on each vmexit?

No need really. Since HW can only clear VIRR the only situation that may happen is that irr_pending will be true but VIRR is empty and apic_find_highest_irr() will return correct result in this case.

If we will see a lot of unneeded irr scans because of stale irr_pending value we can do irr_pending = rvi != 0 on vmexit.

Ok. Currently, I think no need to add this. I will do some data collection to see whether it is needed in future.

Quoted 2 lines

--
Gleb.

Best regards,
Yang

#22 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Zhang, Yang Z
Quoted 65 lines · Yang Z

Gleb Natapov wrote on 2012-12-05:

Quoted 64 lines · Yang Z
Quoted 63 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 62 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 40 lines · Yang Z
Quoted 17 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 16 lines · Yang Z
Quoted 15 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 14 lines · Yang Zhang
Quoted 11 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct kvm_lapic

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct kvm_lapic · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

target

Quoted 6 lines
Quoted 6 lines

vcpu

Quoted 5 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

This is wrong. As we known, modern OS uses per VCPU vector. We cannot

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Yes, when create an new irq, it will allocate vector from all online cpus. But after

user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 1 line

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Sorry, I still cannot find this check in kernel. Can you point me out to it?

You pointed to it above by yourself:
"Yes, when create an new irq, it will allocate vector from all online
cpus"
So if vector is allocated by at least one online cpu it cannot be reused during allocation.

Quoted 2 lines

Also, I do see the vector reused by MSI and IOAPIC in my system.

What is your system? What is your qemu command line? We only care if MSI uses the same vector as IOAPICs level interrupt but on different cpu. If this happens we can use apic_map to calculate per cpu eoi exit bitmap.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Can you answer this?

I am sure Xen will reused the IOAPIC vector.

What configuration it creates exactly? Also if Xen running as KVM guest will takes small performance hit it is not a big problem. Remember there is not correctness issue here.

--
Gleb.

#23 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Gleb Natapov wrote on 2012-12-06:

Quoted 92 lines · Yang Z
Quoted 23 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 22 lines · Yang Z
Quoted 21 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 20 lines · Yang Z
Quoted 19 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 18 lines · Yang Z
Quoted 17 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 16 lines · Yang Z
Quoted 15 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 14 lines · Yang Zhang
Quoted 11 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review static int __apic_accept_irq(struct

Quoted 8 lines
(quoted diff) +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct

kvm_lapic

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Review static int __apic_accept_irq(struct · line 671

Quoted 3 lines
(quoted diff) +1 -0
 		if (unlikely(!apic_enabled(apic)))
 			break;
+		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

target

Quoted 7 lines
Quoted 7 lines

vcpu

Quoted 6 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We

cannot

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Yes, when create an new irq, it will allocate vector from all online cpus. But

after

Quoted 6 lines
Quoted 5 lines

user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 1 line

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Sorry, I still cannot find this check in kernel. Can you point me out to it?

You pointed to it above by yourself:
"Yes, when create an new irq, it will allocate vector from all online
cpus"
So if vector is allocated by at least one online cpu it cannot be reused during allocation.

Quoted 2 lines

Also, I do see the vector reused by MSI and IOAPIC in my system.

What is your system? What is your qemu command line? We only care if MSI uses the same vector as IOAPICs level interrupt but on different cpu. If this happens we can use apic_map to calculate per cpu eoi exit bitmap.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you answer this?

I am sure Xen will reused the IOAPIC vector.

What configuration it creates exactly? Also if Xen running as KVM guest will takes small performance hit it is not a big problem. Remember there is not correctness issue here.

Anyway, it's doesn't matter to discuss which OS will reuse the vector. So use apic_map is the final decision?

Best regards,
Yang

#24 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Quoted 93 lines · Yang Z

Gleb Natapov wrote on 2012-12-06:

Quoted 92 lines · Yang Z
Quoted 23 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 22 lines · Yang Z
Quoted 21 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 20 lines · Yang Z
Quoted 19 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 18 lines · Yang Z
Quoted 17 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 16 lines · Yang Z
Quoted 15 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 14 lines · Yang Zhang
Quoted 11 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts
manually, which is fully taken care of by the hardware. This needs
some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need
update architecture specific indicators before resuming to
guest. - A pending interrupt, which is masked by ISR, should
be also considered in above update action, since hardware
will decide when to inject it at right time. Current
has_interrupt and get_interrupt only returns a valid vector
from injection p.o.v.

Most of my previous comments still apply.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+ int trig_mode, int always_set)
+{
+ if (kvm_x86_ops->set_eoi_exitmap)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+ kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+ trig_mode, always_set);
+}
+
/*
* Add a pending IRQ into lapic.
* Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct

kvm_lapic

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

*apic,

Quoted 9 lines
Quoted 9 lines

int

Quoted 8 lines
Quoted 7 lines

delivery_mode,

Quoted 3 lines

if (unlikely(!apic_enabled(apic)))
break;
+ kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);

As I said in the last review rebuild the bitmap when ioapic or irq notifier configuration changes, user request bit to notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

target

Quoted 7 lines
Quoted 7 lines

vcpu

Quoted 6 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We

cannot

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Yes, when create an new irq, it will allocate vector from all online cpus. But

after

Quoted 6 lines
Quoted 5 lines

user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 1 line

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Sorry, I still cannot find this check in kernel. Can you point me out to it?

You pointed to it above by yourself:
"Yes, when create an new irq, it will allocate vector from all online
cpus"
So if vector is allocated by at least one online cpu it cannot be reused during allocation.

Quoted 2 lines

Also, I do see the vector reused by MSI and IOAPIC in my system.

What is your system? What is your qemu command line? We only care if MSI uses the same vector as IOAPICs level interrupt but on different cpu. If this happens we can use apic_map to calculate per cpu eoi exit bitmap.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you answer this?

I am sure Xen will reused the IOAPIC vector.

What configuration it creates exactly? Also if Xen running as KVM guest will takes small performance hit it is not a big problem. Remember there is not correctness issue here.

Anyway, it's doesn't matter to discuss which OS will reuse the vector. So use apic_map is the final decision?

If you are firmly committed to per vcpu bitmap then yes. I tried to make it simpler for you :)

--
Gleb.

#25 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Gleb Natapov wrote on 2012-12-06:

Quoted 98 lines · Yang Z
Quoted 18 lines

Gleb Natapov wrote on 2012-12-06:

Quoted 17 lines · Yang Z
Quoted 16 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 15 lines · Yang Z
Quoted 14 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 13 lines · Yang Z
Quoted 12 lines

Gleb Natapov wrote on 2012-12-05:

Quoted 11 lines · Yang Z
Quoted 10 lines

Gleb Natapov wrote on 2012-12-04:

Quoted 9 lines · Yang Z
Quoted 8 lines

Gleb Natapov wrote on 2012-12-03:

Quoted 7 lines · Yang Zhang

Virtual interrupt delivery avoids KVM to inject vAPIC
interrupts manually, which is fully taken care of by the
hardware. This needs some special awareness into existing
interrupr injection path:

- for pending interrupt, instead of direct injection, we may

need

Quoted 20 lines
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

update architecture specific indicators before resuming to
guest. - A pending interrupt, which is masked by ISR, should
be also considered in above update action, since hardware
will decide when to inject it at right time. Current
has_interrupt and get_interrupt only returns a valid vector
from injection p.o.v.
Most of my previous comments still apply.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int
vector, + int trig_mode, int always_set) +{ + if
(kvm_x86_ops->set_eoi_exitmap)
+ kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+ trig_mode, always_set); +} +
/*
* Add a pending IRQ into lapic.
* Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct

kvm_lapic

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

*apic,

Quoted 1 line
Quoted 1 line

int

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

delivery_mode,
if (unlikely(!apic_enabled(apic)))
break;
+ kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0);
As I said in the last review rebuild the bitmap when ioapic
or irq notifier configuration changes, user request bit to
notify vcpus to reload the bitmap.

It is too complicated. When program ioapic entry, we cannot get

the

Quoted 14 lines
Quoted 14 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

target

Quoted 7 lines
Quoted 7 lines

vcpu

Quoted 6 lines
Quoted 5 lines

easily. We need to read destination format register and logical destination register to find out target vcpu if using logical mode. Also, we must trap every modification to the two registers to update eoi bitmap. No need to check target vcpu. Enable exit on all vcpus for the vector

This is wrong. As we known, modern OS uses per VCPU vector. We

cannot

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

ensure all vectors have same trigger mode. And what's worse, the vector in another vcpu is used to handle high frequency interrupts(like 10G NIC), then it will hurt performance.

Quoted 1 line

I never saw OSes reuse vector used by ioapic, as far as I see this

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Could you point out which code does this check in Linux kernel? I don't see any special checks when Linux kernel allocates a vector.

arch/x86/kernel/apic/io_apic.c:create_irq_nr(). It uses apic->target_cpus() to get cpu mask. target_cpus() return mask of all online cpus. Actually you wrote arch_pi_create_irq() in PI patches to workaround this behaviour and allocated vector per cpu, no?

Yes, when create an new irq, it will allocate vector from all online cpus.

But

Quoted 26 lines
Quoted 26 lines

after

Quoted 6 lines
Quoted 5 lines

user changes the irq affinity, then the vector will reallocate with new cpumask. And this will leave the vector available on other cpus.

Quoted 1 line

Since during vector allocation all cpus are checked vector will not be reused if it is allocated on any cpu.

Sorry, I still cannot find this check in kernel. Can you point me out to it?

You pointed to it above by yourself:
"Yes, when create an new irq, it will allocate vector from all online
cpus"
So if vector is allocated by at least one online cpu it cannot be reused during allocation.

Quoted 2 lines

Also, I do see the vector reused by MSI and IOAPIC in my system.

What is your system? What is your qemu command line? We only care if MSI uses the same vector as IOAPICs level interrupt but on different cpu. If this happens we can use apic_map to calculate per cpu eoi exit bitmap.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

Are you aware of any guest that I can run, examine ioapic/apic configuration and see that the same vector is used on different vcpus for different devices? Can you point me to it?

Can you answer this?

I am sure Xen will reused the IOAPIC vector.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

What configuration it creates exactly? Also if Xen running as KVM guest will takes small performance hit it is not a big problem. Remember there is not correctness issue here.

Anyway, it's doesn't matter to discuss which OS will reuse the vector. So use apic_map is the final decision?

If you are firmly committed to per vcpu bitmap then yes. I tried to make it simpler for you :)

Ok. Got it. Thanks.:)

Best regards,
Yang

#34 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Marcelo Tosatti
Quoted 38 lines · Marcelo Tosatti
Quoted 37 lines · Gleb Natapov
Quoted 23 lines · Yang Z
Quoted 23 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 7 lines
Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the “interrupt-window exiting” VM-execution control is 0."

Right. Must check RVI here.

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

Quoted 8 lines
Quoted 2 lines

--
Gleb.

Right. BTW, delivery of ExtINT has no EOI, so there is no evaluation of pending virtual interrupts. Therefore, shouldnt interrupt window be enabled when injecting ExtINT so that evaluation of pending virtual interrupts is performed on next vm-entry?

Good question and I think, luckily for us, the answer is no. Spec uses two different terms when it talks about virtual interrupts "Evaluation of Pending Virtual Interrupts" and "Virtual-Interrupt Delivery". As far as my reading of the spec goes they are not necessary happen at the same time. So during ExtINT injection "evaluation" will happen (due to vmentry) and virtual interrupt will be recognized, but not "delivered". It will be delivered when condition described in section 29.2.2 will be met i.e when interrupts will be enabled.

Yang, can you confirm this?

--
Gleb.

#35 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Gleb Natapov wrote on 2012-12-06:

Quoted 57 lines · Marcelo Tosatti
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Yang Z
Quoted 5 lines
Quoted 5 lines

Review static int vcpu_enter_guest(struct kvm_vcpu

Quoted 1 line
(quoted diff) +0 -0
@@ -5657,12 +5673,20 @@ static int vcpu_enter_guest(struct kvm_vcpu

*vcpu)

Quoted 3 lines
(quoted diff) +0 -0
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win)

{

Quoted 31 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+		/* update archtecture specific hints for APIC
+		 * virtual interrupt delivery */
+		if (kvm_x86_ops->update_irq)
+			kvm_x86_ops->update_irq(vcpu);

arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5688

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
+
 		inject_pending_event(vcpu);
 
 		/* enable NMI/IRQ window open exits if needed */
 		if (vcpu->arch.nmi_pending)
 			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+		else if (kvm_apic_vid_enabled(vcpu)) {
+			if (kvm_cpu_has_extint(vcpu))
+				kvm_x86_ops->enable_irq_window(vcpu);

If RVI is non-zero, then interrupt window should not be enabled, accordingly to 29.2.2:

Quoted 7 lines
Quoted 5 lines

"If a virtual interrupt has been recognized (see Section 29.2.1), it will be delivered at an instruction boundary when the following conditions all
hold: (1) RFLAGS.IF = 1; (2) there is no blocking by STI; (3) there is no blocking by MOV SS or by POP SS; and (4) the "interrupt-window exiting" VM-execution control is 0."

Right. Must check RVI here.

Why? We request interrupt window here because there is ExtINT interrupt pending. ExtINT interrupt has a precedence over APIC interrupts (our current code is incorrect!), so we want vmexit as soon as interrupts are allowed to inject ExtINT and we do not want virtual interrupt to be delivered. I think the (4) there is exactly for this situation.

Quoted 8 lines
Quoted 2 lines

--
Gleb.

Right. BTW, delivery of ExtINT has no EOI, so there is no evaluation of pending virtual interrupts. Therefore, shouldnt interrupt window be enabled when injecting ExtINT so that evaluation of pending virtual interrupts is performed on next vm-entry?

Good question and I think, luckily for us, the answer is no. Spec uses two different terms when it talks about virtual interrupts "Evaluation of Pending Virtual Interrupts" and "Virtual-Interrupt Delivery". As far as my reading of the spec goes they are not necessary happen at the same time. So during ExtINT injection "evaluation" will happen (due to vmentry) and virtual interrupt will be recognized, but not "delivered". It will be delivered when condition described in section 29.2.2 will be met i.e when interrupts will be enabled.

Quoted 1 line

Yang, can you confirm this?

Right.
Vmentry causes the evaluation of pending virtual interrupt even during ExtINT injection. If RVI[7:4] > VPPR[7:4], the logical process recognizes a pending virtual interrupt. Then it will be delivery when condition is met.

Best regards,
Yang

#41 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

In reply to Gleb Natapov
Quoted 265 lines · Gleb Natapov
Quoted 252 lines · Yang Z

Zhang, Yang Z wrote on 2012-12-06:

Quoted 251 lines

Marcelo Tosatti wrote on 2012-12-06:

Quoted 29 lines · Yang Zhang
Quoted 28 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/vmx.h

   |   11 +++ arch/x86/kvm/irq.c              |   53 ++++++++++-----
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -697,6 +697,10 @@ struct kvm_x86_ops {

arch/x86/include/asm/kvm_host.h +0 -0
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
Quoted 8 lines

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops {

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -0
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool

is_mmio);

Quoted 7 lines

arch/x86/include/asm/vmx.h line 63

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define

EXIT_REASON_APIC_ACCESS

Quoted 2 lines

arch/x86/include/asm/vmx.h line 65

Quoted 2 lines
arch/x86/include/asm/vmx.h +0 -0
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define

EXIT_REASON_EPT_MISCONFIG

Quoted 1 line

arch/x86/include/asm/vmx.h line 66

Quoted 1 line
arch/x86/include/asm/vmx.h +0 -0
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6

+144,7 @@

Quoted 7 lines

arch/x86/include/asm/vmx.h

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000 @@ -180,6 +182,7 @@ enum
 vmcs_field { 	GUEST_GS_SELECTOR               = 0x0000080a,

GUEST_LDTR_SELECTOR

Quoted 1 line
Quoted 1 line
          = 0x0000080c, 	GUEST_TR_SELECTOR               =

0x0000080e,

Quoted 1 line
Quoted 1 line

+ GUEST_INTR_STATUS = 0x00000810,

HOST_ES_SELECTOR

Quoted 1 line
Quoted 1 line
           = 0x00000c00, 	HOST_CS_SELECTOR                =

0x00000c02,

Quoted 2 lines
Quoted 2 lines

HOST_SS_SELECTOR = 0x00000c04, @@ -207,6 +210,14 @@
enum vmcs_field { APIC_ACCESS_ADDR_HIGH = 0x00002015,

EPT_POINTER

Quoted 1 line
Quoted 1 line
                   = 0x0000201a, 	EPT_POINTER_HIGH

=

Quoted 180 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int

Quoted 113 lines

0x0000201b,

arch/x86/include/asm/vmx.h +8 -0
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic *s;
 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +8 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (unlikely(!apic_enabled(apic))) 			break;
 +		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0); 		if (trig_mode)
 { 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

struct kvm_vcpu *vcpu2)

Quoted 47 lines
arch/x86/kvm/lapic.c +27 -9
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+{
+	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
+	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
+		int trigger_mode;
+		if (apic_test_vector(vector, apic->regs + APIC_TMR))
+			trigger_mode = IOAPIC_LEVEL_TRIG;
+		else
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	}
+}
+
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
-	    kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
-		int trigger_mode;
-		if (apic_test_vector(vector, apic->regs + APIC_TMR))
-			trigger_mode = IOAPIC_LEVEL_TRIG;
-		else
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	}
+	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1,

Quoted 10 lines
arch/x86/kvm/lapic.c +9 -0
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

Quoted 3 lines
Quoted 2 lines

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

One problem is that when HW clears VIRR, after vmexit, the irr_peding will still true if there are more than one bit is set in VIRR in last vmentry.

It doesn't matter how much bits were set in VIRR before entry, as long as some bit were set irr_peding will be true.

Quoted 2 lines

How about to recaculate irr_pending according the VIRR on each vmexit?

No need really. Since HW can only clear VIRR the only situation that may happen is that irr_pending will be true but VIRR is empty and apic_find_highest_irr() will return correct result in this case.

Quoted 5 lines

If we will see a lot of unneeded irr scans because of stale irr_pending value we can do irr_pending = rvi != 0 on vmexit.

--
Gleb.

Yes, it is harmless ATM. But its clearer if irr_pending is not used when virtual interrupt delivery is in effect (that is, just skip irr_pending if virtual interrupt delivery is enabled).

#42 lore raw

RE: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Marcelo Tosatti wrote on 2012-12-07:

Quoted 267 lines · Gleb Natapov
Quoted 8 lines · Yang Z
Quoted 7 lines

Zhang, Yang Z wrote on 2012-12-06:

Quoted 6 lines

Marcelo Tosatti wrote on 2012-12-06:

Quoted 5 lines · Yang Zhang
Quoted 4 lines

Virtual interrupt delivery avoids KVM to inject vAPIC interrupts manually, which is fully taken care of by the hardware. This needs some special awareness into existing interrupr injection path:

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

- for pending interrupt, instead of direct injection, we may need update architecture specific indicators before resuming to guest. - A pending interrupt, which is masked by ISR, should be also considered in above update action, since hardware will decide when to inject it at right time. Current has_interrupt and get_interrupt only returns a valid vector from injection p.o.v.

---
 arch/x86/include/asm/kvm_host.h |    4 +

arch/x86/include/asm/vmx.h

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
   |   11 +++ arch/x86/kvm/irq.c              |   53

++++++++++-----

Quoted 159 lines
Quoted 159 lines
Quoted 159 lines
Quoted 13 lines
Quoted 13 lines
 arch/x86/kvm/lapic.c            |   56 +++++++++++++---
 arch/x86/kvm/lapic.h            |    6 ++ arch/x86/kvm/svm.c
    |   19 +++++ arch/x86/kvm/vmx.c              |  140

++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c
| 34 ++++++++-- virt/kvm/ioapic.c | 1 + 9 files
changed, 291 insertions(+), 33 deletions(-)

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index dc87b65..e5352c8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -697,6 +697,10 @@ struct kvm_x86_ops {

arch/x86/include/asm/kvm_host.h +0 -0
 	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
 	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
Quoted 8 lines

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops {

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -0
 	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
+	int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
+	void (*update_irq)(struct kvm_vcpu *vcpu);
+	void (*set_eoi_exitmap)(struct kvm_vcpu *vcpu, int vector,
+			int trig_mode, int always_set);
 	int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
 	int (*get_tdp_level)(void);
 	u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool

is_mmio);

Quoted 7 lines

arch/x86/include/asm/vmx.h line 63

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
index 21101b6..1003341 100644
@@ -62,6 +62,7 @@
 #define EXIT_REASON_MCE_DURING_VMENTRY  41 #define
 EXIT_REASON_TPR_BELOW_THRESHOLD 43 #define

EXIT_REASON_APIC_ACCESS

Quoted 2 lines

arch/x86/include/asm/vmx.h line 65

Quoted 2 lines
arch/x86/include/asm/vmx.h +0 -0
     44 +#define EXIT_REASON_EOI_INDUCED         45 #define
 EXIT_REASON_EPT_VIOLATION       48 #define

EXIT_REASON_EPT_MISCONFIG

Quoted 1 line

arch/x86/include/asm/vmx.h line 66

Quoted 1 line
arch/x86/include/asm/vmx.h +0 -0
     49 #define EXIT_REASON_WBINVD              54 @@ -143,6

+144,7 @@

Quoted 7 lines

arch/x86/include/asm/vmx.h

Quoted 7 lines
arch/x86/include/asm/vmx.h +0 -0
 #define SECONDARY_EXEC_WBINVD_EXITING		0x00000040 #define
 SECONDARY_EXEC_UNRESTRICTED_GUEST	0x00000080 #define
 SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100 +#define
 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200 #define
 SECONDARY_EXEC_PAUSE_LOOP_EXITING	0x00000400 #define
 SECONDARY_EXEC_ENABLE_INVPCID		0x00001000 @@ -180,6 +182,7 @@ enum
 vmcs_field { 	GUEST_GS_SELECTOR               = 0x0000080a,

GUEST_LDTR_SELECTOR

Quoted 1 line
Quoted 1 line
          = 0x0000080c, 	GUEST_TR_SELECTOR               =

0x0000080e,

Quoted 1 line
Quoted 1 line

+ GUEST_INTR_STATUS = 0x00000810,

HOST_ES_SELECTOR

Quoted 1 line
Quoted 1 line
           = 0x00000c00, 	HOST_CS_SELECTOR                =

0x00000c02,

Quoted 2 lines
Quoted 2 lines

HOST_SS_SELECTOR = 0x00000c04, @@ -207,6 +210,14
@@ enum vmcs_field { APIC_ACCESS_ADDR_HIGH = 0x00002015,

EPT_POINTER

Quoted 1 line
Quoted 1 line
                   = 0x0000201a, 	EPT_POINTER_HIGH

=

Quoted 105 lines
Quoted 105 lines

0x0000201b,

arch/x86/include/asm/vmx.h +8 -0
+	EOI_EXIT_BITMAP0                = 0x0000201c,
+	EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
+	EOI_EXIT_BITMAP1                = 0x0000201e,
+	EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
+	EOI_EXIT_BITMAP2                = 0x00002020,
+	EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
+	EOI_EXIT_BITMAP3                = 0x00002022,
+	EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
 	GUEST_PHYSICAL_ADDRESS          = 0x00002400,
 	GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
 	VMCS_LINK_POINTER               = 0x00002800,
arch/x86/kvm/irq.c +36 -16
index 7e06ba1..f782788 100644
@@ -43,45 +43,64 @@ EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  */
 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
 {
-	struct kvm_pic *s;
-
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.pending;
-	if (kvm_apic_has_interrupt(v) == -1) {	/* LAPIC */
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);	/* PIC */
-			return s->output;
-		} else
-			return 0;
-	}
+	if (kvm_apic_has_interrupt(v) == -1) /* LAPIC */
+		return kvm_cpu_has_extint(v); /* non-APIC */
 	return 1;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
 
 /*
+ * check if there is pending interrupt from
+ * non-APIC source without intack.
+ */
+int kvm_cpu_has_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm); /* PIC */
+		return s->output;
+	} else
+		return 0;
+}
+
+/*
  * Read pending interrupt vector and intack.
  */
 int kvm_cpu_get_interrupt(struct kvm_vcpu *v) { -	struct kvm_pic
 *s; 	int vector;
 
 	if (!irqchip_in_kernel(v->kvm))
 		return v->arch.interrupt.nr;
 
 	vector = kvm_get_apic_interrupt(v);	/* APIC */
-	if (vector == -1) {
-		if (kvm_apic_accept_pic_intr(v)) {
-			s = pic_irqchip(v->kvm);
-			s->output = 0;		/* PIC */
-			vector = kvm_pic_read_irq(v->kvm);
-		}
-	}
+	if (vector == -1)
+		return kvm_cpu_get_extint(v); /* non-APIC */
 	return vector;
 }
 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
+/*
+ * Read pending interrupt(from non-APIC source)
+ * vector and intack.
+ */
+int kvm_cpu_get_extint(struct kvm_vcpu *v)
+{
+	struct kvm_pic *s;
+	int vector = -1;
+
+	if (kvm_apic_accept_pic_intr(v)) {
+		s = pic_irqchip(v->kvm);
+		s->output = 0;		/* PIC */
+		vector = kvm_pic_read_irq(v->kvm);
+	}
+	return vector;
+}
+
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_apic_timer_irqs(vcpu);
arch/x86/kvm/lapic.c +4 -0
index 7c96012..400d3ba 100644
@@ -643,6 +643,14 @@ out:
 	return ret;
 }
+void kvm_set_eoi_exitmap(struct kvm_vcpu *vcpu, int vector,
+		int trig_mode, int always_set)
+{
+	if (kvm_x86_ops->set_eoi_exitmap)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic,

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+		kvm_x86_ops->set_eoi_exitmap(vcpu, vector,
+					trig_mode, always_set);
+}
+
 /*
  * Add a pending IRQ into lapic.
  * Return 1 if successfully added and 0 if discarded.
@@ -661,6 +669,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic,

int

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

delivery_mode,

arch/x86/kvm/lapic.c int kvm_apic_compare_prio(struct kvm_vcpu

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 		if (unlikely(!apic_enabled(apic))) 			break;
 +		kvm_set_eoi_exitmap(vcpu, vector, trig_mode, 0); 	if
 (trig_mode) { 			apic_debug("level trig mode for vector %d",
 vector); 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -740,6 +749,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu

*vcpu1,

Quoted 54 lines
Quoted 54 lines
Quoted 54 lines
Quoted 54 lines

struct kvm_vcpu *vcpu2)

Quoted 40 lines
arch/x86/kvm/lapic.c +1 -0
 	return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
 }
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int

vector) +{ + if (!(kvm_apic_get_reg(apic, APIC_SPIV) &
APIC_SPIV_DIRECTED_EOI) && +
kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) { + int
trigger_mode; + if (apic_test_vector(vector, apic->regs +
APIC_TMR)) + trigger_mode = IOAPIC_LEVEL_TRIG; + else

arch/x86/kvm/lapic.c +3 -1
+			trigger_mode = IOAPIC_EDGE_TRIG;
+		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+	} +} +
 static int apic_set_eoi(struct kvm_lapic *apic) { 	int vector =
 apic_find_highest_isr(apic); @@ -756,19 +778,24 @@ static int
 apic_set_eoi(struct kvm_lapic *apic) 	apic_clear_isr(vector, apic);
 	apic_update_ppr(apic);
-	if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)

&& - kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {

arch/x86/kvm/lapic.c +0 -1
-		int trigger_mode; -		if (apic_test_vector(vector, apic->regs +

APIC_TMR)) - trigger_mode = IOAPIC_LEVEL_TRIG; - else

arch/x86/kvm/lapic.c +13 -3
-			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
-	} +	kvm_ioapic_send_eoi(apic, vector);
 	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
 	return vector;
 }
+/*
+ * this interface assumes a trap-like exit, which has already finished
+ * desired side effect including vISR and vPPR update.
+ */
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	kvm_ioapic_send_eoi(apic, vector);
+	kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
+
 static void apic_send_ipi(struct kvm_lapic *apic) { 	u32 icr_low =
 kvm_apic_get_reg(apic, APIC_ICR); @@ -1533,6 +1560,17 @@ int

arch/x86/kvm/lapic.c static int apic_set_eoi(struct kvm_lapic *apic)

Quoted 10 lines
arch/x86/kvm/lapic.c +9 -0
 kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) 	return highest_irr; }
+int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	if (!apic || !apic_enabled(apic))
+		return -1;
+
+	return apic_find_highest_irr(apic);
+}

irr_pending of apic_find_highest_irr() is meaningless (stale) if HW is updating VIRR.

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

I don't think target vcpu is running when we call this function. So it is safe to check irr_pending and read the irr.

One problem is that when HW clears VIRR, after vmexit, the irr_peding will

still true if there are more than one bit is set in VIRR in last vmentry.

Quoted 12 lines

It doesn't matter how much bits were set in VIRR before entry, as long as some bit were set irr_peding will be true.

Quoted 2 lines

How about to recaculate irr_pending according the VIRR on each vmexit?

No need really. Since HW can only clear VIRR the only situation that may happen is that irr_pending will be true but VIRR is empty and apic_find_highest_irr() will return correct result in this case.

If we will see a lot of unneeded irr scans because of stale irr_pending value we can do irr_pending = rvi != 0 on vmexit.

Quoted 6 lines
Quoted 2 lines

--
Gleb.

Yes, it is harmless ATM. But its clearer if irr_pending is not used when virtual interrupt delivery is in effect (that is, just skip irr_pending if virtual interrupt delivery is enabled).

irr_pending still is useful in virtual interrupt delivery case. Or else, as gleb said ,there may be lots of unneeded irr scan.

Best regards,
Yang

#43 lore raw

Re: [PATCH v3 3/4] x86, apicv: add virtual interrupt delivery support

Quoted 10 lines · Yang Z

Marcelo Tosatti wrote on 2012-12-07:

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

How about to recaculate irr_pending according the VIRR on each vmexit?

No need really. Since HW can only clear VIRR the only situation that may happen is that irr_pending will be true but VIRR is empty and apic_find_highest_irr() will return correct result in this case.

If we will see a lot of unneeded irr scans because of stale irr_pending value we can do irr_pending = rvi != 0 on vmexit.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

--
Gleb.

Yes, it is harmless ATM. But its clearer if irr_pending is not used when virtual interrupt delivery is in effect (that is, just skip irr_pending if virtual interrupt delivery is enabled).

irr_pending still is useful in virtual interrupt delivery case. Or else, as gleb said ,there may be lots of unneeded irr scan.

Actually, looking at it close, irr_pending will always be true (and thus effectively disabled without any additional checks) since it is cleared only in kvm_get_apic_interrupt() which will not be called with vid enabled. It is racy to do irr_pending = rvi != 0 on vmexit either. The code should be something like that:
irr_pending = (rvi != 0) ? : apic_search_irr(apic) != -1;
But we do not want to do that on each exit since rvi will be mostly zero and irr is, more often than not, empty.

--
Gleb.