From patchwork Fri Mar 8 01:23:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Yang Z" X-Patchwork-Id: 2234131 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id C4A203FCF6 for ; Fri, 8 Mar 2013 01:27:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759920Ab3CHB1g (ORCPT ); Thu, 7 Mar 2013 20:27:36 -0500 Received: from mga09.intel.com ([134.134.136.24]:46520 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755343Ab3CHB12 (ORCPT ); Thu, 7 Mar 2013 20:27:28 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP; 07 Mar 2013 17:25:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.84,806,1355126400"; d="scan'208";a="296290574" Received: from yang-desktop.sh.intel.com ([10.239.13.6]) by orsmga002.jf.intel.com with ESMTP; 07 Mar 2013 17:27:26 -0800 From: Yang Zhang To: kvm@vger.kernel.org Cc: gleb@redhat.com, mtosatti@redhat.com, xiantao.zhang@intel.com, mingo@kernel.org, hpa@linux.intel.com, Yang Zhang Subject: [PATCH v5 4/5] KVM: VMX: Add the algorithm of deliver posted interrupt Date: Fri, 8 Mar 2013 09:23:20 +0800 Message-Id: <1362705801-4467-5-git-send-email-yang.z.zhang@intel.com> X-Mailer: git-send-email 1.7.1.1 In-Reply-To: <1362705801-4467-1-git-send-email-yang.z.zhang@intel.com> References: <1362705801-4467-1-git-send-email-yang.z.zhang@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Yang Zhang Only deliver the posted interrupt when target vcpu is running and there is no previous interrupt pending in pir. Signed-off-by: Yang Zhang --- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/lapic.c | 13 ++++++++ arch/x86/kvm/lapic.h | 1 + arch/x86/kvm/svm.c | 18 +++++++++++ arch/x86/kvm/vmx.c | 60 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 1 + 6 files changed, 96 insertions(+), 0 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b8388e9..0a9aeee 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -704,6 +704,9 @@ struct kvm_x86_ops { void (*hwapic_isr_update)(struct kvm *kvm, int isr); void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap); void (*set_virtual_x2apic_mode)(struct kvm_vcpu *vcpu, bool set); + bool (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu, int vector); + void (*posted_intr_clear_on)(struct kvm_vcpu *vcpu); + bool (*sync_pir_to_irr)(struct kvm_vcpu *vcpu, bool sync); 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); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 02b51dd..b3ea50e 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -357,6 +357,19 @@ static u8 count_vectors(void *bitmap) return count; } +void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir) +{ + u32 i, pir_val; + struct kvm_lapic *apic = vcpu->arch.apic; + + for (i = 0; i <= 7; i++) { + pir_val = xchg(&pir[i], 0); + if (pir_val) + *((u32 *)(apic->regs + APIC_IRR + i * 0x10)) |= pir_val; + } +} +EXPORT_SYMBOL_GPL(kvm_apic_update_irr); + static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic) { apic->irr_pending = true; diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 1676d34..e5327be 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -157,5 +157,6 @@ static inline u16 apic_logical_id(struct kvm_apic_map *map, u32 ldr) void kvm_calculate_eoi_exitmap(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, u64 *eoi_bitmap); +void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir); #endif diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index a7d60d7..7a33d75 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -3591,6 +3591,21 @@ static void svm_hwapic_isr_update(struct kvm *kvm, int isr) return; } +static bool svm_sync_pir_to_irr(struct kvm_vcpu *vcpu, bool sync) +{ + return false; +} + +static bool svm_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) +{ + return false; +} + +static void svm_posted_intr_clear_on(struct kvm_vcpu *vcpu) +{ + return; +} + static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4319,6 +4334,9 @@ static struct kvm_x86_ops svm_x86_ops = { .vm_has_apicv = svm_vm_has_apicv, .load_eoi_exitmap = svm_load_eoi_exitmap, .hwapic_isr_update = svm_hwapic_isr_update, + .sync_pir_to_irr = svm_sync_pir_to_irr, + .deliver_posted_interrupt = svm_deliver_posted_interrupt, + .posted_intr_clear_on = svm_posted_intr_clear_on, .set_tss_addr = svm_set_tss_addr, .get_tdp_level = get_npt_level, diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 625fd19..5a0be39 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -379,6 +379,17 @@ struct pi_desc { } u; } __aligned(64); +static bool pi_test_and_set_on(struct pi_desc *pi_desc) +{ + return test_and_set_bit(POSTED_INTR_ON, + (unsigned long *)&pi_desc->u.control); +} + +static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) +{ + return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); +} + struct vcpu_vmx { struct kvm_vcpu vcpu; unsigned long host_rsp; @@ -3911,6 +3922,52 @@ static int vmx_vm_has_apicv(struct kvm *kvm) return enable_apicv && irqchip_in_kernel(kvm); } +static void vmx_posted_intr_clear_on(struct kvm_vcpu *vcpu) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + if (!vmx_vm_has_apicv(vcpu->kvm)) + return; + + clear_bit(POSTED_INTR_ON, (unsigned long *)&vmx->pi_desc.u.control); +} + +static bool vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + if (!vmx_vm_has_apicv(vcpu->kvm)) + return false; + + if (pi_test_and_set_pir(vector, &vmx->pi_desc)) + return true; + + kvm_make_request(KVM_REQ_EVENT, vcpu); + if ((vcpu->mode == IN_GUEST_MODE)) { + if (!pi_test_and_set_on(&vmx->pi_desc)) + apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), + POSTED_INTR_VECTOR); + } else + kvm_vcpu_kick(vcpu); + + return true; +} + +static bool vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu, bool sync) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + if (!vmx_vm_has_apicv(vcpu->kvm)) + return false; + + if (bitmap_empty((unsigned long *)vmx->pi_desc.pir, 256)) + return false; + + if (sync) + kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); + return true; +} + /* * Set up the vmcs's constant host-state fields, i.e., host-state fields that * will not change in the lifetime of the guest. @@ -7735,6 +7792,9 @@ static struct kvm_x86_ops vmx_x86_ops = { .load_eoi_exitmap = vmx_load_eoi_exitmap, .hwapic_irr_update = vmx_hwapic_irr_update, .hwapic_isr_update = vmx_hwapic_isr_update, + .sync_pir_to_irr = vmx_sync_pir_to_irr, + .deliver_posted_interrupt = vmx_deliver_posted_interrupt, + .posted_intr_clear_on = vmx_posted_intr_clear_on, .set_tss_addr = vmx_set_tss_addr, .get_tdp_level = get_ept_level, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index adc68fe..cd73e0e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1692,6 +1692,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu) smp_send_reschedule(cpu); put_cpu(); } +EXPORT_SYMBOL_GPL(kvm_vcpu_kick); #endif /* !CONFIG_S390 */ void kvm_resched(struct kvm_vcpu *vcpu)