From patchwork Fri Feb 12 13:59:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 61848 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp840255lbl; Fri, 12 Feb 2016 06:01:13 -0800 (PST) X-Received: by 10.66.244.194 with SMTP id xi2mr2204147pac.139.1455285672925; Fri, 12 Feb 2016 06:01:12 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r6si20154328pap.212.2016.02.12.06.01.12; Fri, 12 Feb 2016 06:01:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752931AbcBLOBK (ORCPT + 30 others); Fri, 12 Feb 2016 09:01:10 -0500 Received: from mail-bn1bon0057.outbound.protection.outlook.com ([157.56.111.57]:56819 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752778AbcBLOBE (ORCPT ); Fri, 12 Feb 2016 09:01:04 -0500 Authentication-Results: 8bytes.org; dkim=none (message not signed) header.d=none; 8bytes.org; dmarc=none action=none header.from=amd.com; Received: from localhost.localdomain (124.121.8.20) by BY1PR12MB0440.namprd12.prod.outlook.com (10.162.147.141) with Microsoft SMTP Server (TLS) id 15.1.403.16; Fri, 12 Feb 2016 14:00:53 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , , Suravee Suthikulpanit , Suravee Suthikulpanit Subject: [PART1 RFC 6/9] svm: Add interrupt injection via AVIC Date: Fri, 12 Feb 2016 20:59:31 +0700 Message-ID: <1455285574-27892-7-git-send-email-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1455285574-27892-1-git-send-email-suravee.suthikulpanit@amd.com> References: <1455285574-27892-1-git-send-email-suravee.suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [124.121.8.20] X-ClientProxiedBy: SG2PR03CA0036.apcprd03.prod.outlook.com (25.160.233.46) To BY1PR12MB0440.namprd12.prod.outlook.com (25.162.147.141) X-MS-Office365-Filtering-Correlation-Id: b4534068-5934-4876-b72a-08d333b4f084 X-Microsoft-Exchange-Diagnostics: 1; BY1PR12MB0440; 2:rIeuYd2okbD9BPjQCb33JcZa06L8iWNluRbf5wkC5N0O3bEE/aeZ9KrPhPGNyLo/jBGLC9wfNhjorzU7G6R3D1JISXT2n+qIO9kxuP5DUS/3N1MU7QXA6W8A8BaJo0ocXG/W7uhXLqiCUMEJZlIHZgkYKCW5GUkVCxflA3Fl4hSQIH2r9cxa/hezs7NUk1wh; 3:omjvlFicWSQOiffT+tQ6jus3RoH9vdFCE1ccCsdBqzffT8sJyvKhbelW6GIt2KU1ggizJ0iPrcChvcQZhrVmyim6GRbPuBLXohxT/pCEHuzNZuGx6UdWz398PDwVPDJX; 25:Qg+JieJZdCWPOCLnnAxDI/J/X5F5gnJtUQ9sCpEOhwzSUluFCjeBakMQyUnprsQvIQpsmI4g/ZS83zswCL2alazAyacWnmUi19/Ff6Dc7tr5iPVTgVoAVcZGGMzO8LE1xqgn3NsbXUr17JCYmtirTwS0jm5kkvYZB+iWqdkthz/lDdbuQSDe56KVUUhP/p+zQla5EQk870prw4XsWx2qiGZpMLwEMa1mii573WJkUL1FfJFLm7iNRm4+Dhr03ZA+R3Mmyvl8NDfYhMjsHTkRIvhn3tQaSIHBosiGss/fMJxRqKTmxkl7QHMYAMfhGeaS X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BY1PR12MB0440; X-Microsoft-Exchange-Diagnostics: 1; BY1PR12MB0440; 20:4t66EIvOjsSWVwsAuRikhxIArJ5wNFDfM7kjo5Q5wp8yUDJwfrLyFVDkSFZCZ28KGkMIJT+eIcJusJxZlwhYqOnlf9msmDgcFs39irgIqqUmRrZBjnejr8H4uYeH57q3bCnqgrIqAnNAP0rFcjnYRN9oylmblq6WsJmWbGC5cwAGOro4ruVBsg890/kFytEX2buk/CEos7fkR+Y4lpyU6LcOYWJ13ogzdvi/bcYDLApJ9ggjerymaZplpqFTHoPy99iMUsG84fY9/oSgEl2VRd7+VXaS/XL4LsGOToZK8+VXxTmj2E6qnvMGtYuzoO/zrKhoCB0imhyYVZeX/gpZxWakbeU8tlBHgMuUoVxPdFGLIP2OoNe1F0N6pt5juHM9z55DCH8vR9sFo1XU1URQC+mbPPY+Wqwq7WrK+5k5PQMnfE80s6JMsFzBG06p+8ZakKU4+fn36TvaIZxqlf2Y0jkDXZzAJmYOOhWUn9x4kLkN1eTx9OI6j4zXXvVKXlO5; 4:zJ8zOflqahoGc6QphX3vzsxFXToB9LFLK7TalzgLmPqDhktv5HqkHcDgKiLN1ffXvCraC1zbIjbJ+kt3GKWKa2z+8xEsdJ80w7iDPqSuRLkhg09xbL0VBAHetqbdzRwbbhewVi0CdP4uba3bbCVGOqc7rftYw4iH4G2Scim/ti3OgBuZb2mJlAGK/7imCDcLPbJAXtbYeax9oVqlVFg4O4QzfuHDHNAMwaxK6zZnN1HXnh2+jNatMIvO4lS+fMKPiNE1RkReSnHqHILJXaAMxPeLlo4r5UWr/5ci4ZhOKFi7OZnVsvv3AXnUSwPj6XrIn5dww0HLkt5vK/OPBeqXYxvlqDM/Ah4v118An1Nzzeo/H1zU/UzJA75vRn8njZ6jaqDpc/9R34G5kt9zKpTvOgsYo8dH0OjJvHcToXDx75w= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046); SRVR:BY1PR12MB0440; BCL:0; PCL:0; RULEID:; SRVR:BY1PR12MB0440; X-Forefront-PRVS: 0850800A29 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(979002)(6009001)(6069001)(2201001)(36756003)(48376002)(586003)(40100003)(92566002)(3846002)(5003940100001)(77096005)(229853001)(2950100001)(19580395003)(19580405001)(6116002)(5004730100002)(189998001)(4326007)(42186005)(66066001)(50226001)(87976001)(5008740100001)(5001770100001)(122386002)(33646002)(50466002)(76176999)(50986999)(86362001)(5001960100002)(47776003)(1096002)(2906002)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:BY1PR12MB0440; H:localhost.localdomain; FPR:; SPF:None; MLV:ovrnspm; PTR:InfoNoRecords; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY1PR12MB0440; 23:vpq2M83yioTwOf7IbLzoTdtqTIPOvZBs7pz8Zc9ZS?= =?us-ascii?Q?DiiJqhZsI7nTKhiHPjGWQHKna3GZB4l+rwgZe7xhX+dZmAo+ZNQhWxcYcn6e?= =?us-ascii?Q?sANu1oaGc9LOA7/Yd4rQla4ghupHZdp2Or/DGPCvdZWf6gKf98lBGarCKTvS?= =?us-ascii?Q?oKTb2z8mWkZ3Oba1e3QEMzUryXEJFjDrA3fCOpTZvPmW1rbrVYtugz+Ds5Tk?= =?us-ascii?Q?P4Nfz53Vj54IyGDTXLDXQv7tEucRwg+gQdG7Qji+eKVU+OE8RRWsYFLcYRZD?= =?us-ascii?Q?qLmrEZRkHhIhu8qrhCOLv7TjepEX4ju4jg0CjSbpsgeZuP8Jb0ol4r/sNbkw?= =?us-ascii?Q?pGCT52q7/7BeD8M5caomsIxF1YbgrA+GKE8LgdgCxYCP7+ZT6C89jjprcJiv?= =?us-ascii?Q?lV0dtaJ1puZb1nxeNPwQqIspfZiiJ5h3inZGYv9nTHEbk2JGo3lzyy0G2GAf?= =?us-ascii?Q?qP+153gGS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QrBVW8qMKE533Cj0cZwsAx+B6toagzpQLBjsnnsvGdwfY/OHaA7KJmQUsGc1NGXelFAMf07oQFcEV9gsr0TrJgfi8GI/VJgLPGofA/syymK0zk/IISe7vGksgLY0HDwxsxrphXp3iSfY4MtAr5noZfPuW6S+wmsFj62cLdHEvM7hAlnPuZYNwIxFA4/N0J9l303yNH7YTvjf9fEer5relePijK X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Feb 2016 14:00:53.1572 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY1PR12MB0440 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Suravee Suthikulpanit VINTR is not supported when enable AVIC. Therefore, we need to inject interrupt via APIC backing page instead. Also, adding AVIC doorbell support to signal running vcpu to check IRR for injected interrupts. This patch also introduces kvm_x86_ops.apicv_intr_pending() to allow SVM to provide a function hook to query AVIC interrupt pending status. Signed-off-by: Suravee Suthikulpanit --- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/lapic.c | 3 +- arch/x86/kvm/svm.c | 110 +++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 4 +- 5 files changed, 114 insertions(+), 6 deletions(-) -- 1.9.1 diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 7b78328..a7c8852 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -973,6 +973,8 @@ struct kvm_x86_ops { void (*post_block)(struct kvm_vcpu *vcpu); int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set); + + bool (*apicv_intr_pending)(struct kvm_vcpu *vcpu); }; struct kvm_arch_async_pf { diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index b05402e..605b869 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -284,6 +284,7 @@ #define MSR_AMD64_TSC_RATIO 0xc0000104 #define MSR_AMD64_NB_CFG 0xc001001f #define MSR_AMD64_PATCH_LOADER 0xc0010020 +#define MSR_AMD64_AVIC_DOORBELL 0xc001011b #define MSR_AMD64_OSVW_ID_LENGTH 0xc0010140 #define MSR_AMD64_OSVW_STATUS 0xc0010141 #define MSR_AMD64_LS_CFG 0xc0011020 diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index fc313a0..f6deb04 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1828,7 +1828,8 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) struct kvm_lapic *apic = vcpu->arch.apic; int highest_irr; - if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic)) + if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic) || + kvm_x86_ops->apicv_intr_pending) return -1; apic_update_ppr(apic); diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index bedf52b..5d7b049 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -169,6 +169,7 @@ struct vcpu_svm { bool nrips_enabled : 1; struct page *avic_bk_page; + atomic_t avic_pending_cnt; }; struct __attribute__ ((__packed__)) @@ -232,6 +233,8 @@ static bool npt_enabled = true; static bool npt_enabled; #endif +static struct kvm_x86_ops svm_x86_ops; + /* allow nested paging (virtualized MMU) for all guests */ static int npt = true; module_param(npt, int, S_IRUGO); @@ -974,6 +977,9 @@ static __init int svm_hardware_setup(void) if (avic) { printk(KERN_INFO "kvm: AVIC enabled\n"); + } else { + svm_x86_ops.deliver_posted_interrupt = NULL; + svm_x86_ops.apicv_intr_pending = NULL; } return 0; @@ -1188,8 +1194,10 @@ static void init_vmcb(struct vcpu_svm *svm) enable_gif(svm); - if (avic) + if (avic) { avic_init_vmcb(svm); + atomic_set(&svm->avic_pending_cnt, 0); + } } static struct svm_avic_phy_ait_entry * @@ -3059,8 +3067,10 @@ static int clgi_interception(struct vcpu_svm *svm) disable_gif(svm); /* After a CLGI no interrupts should come */ - svm_clear_vintr(svm); - svm->vmcb->control.v_irq = 0; + if (!avic) { + svm_clear_vintr(svm); + svm->vmcb->control.v_irq = 0; + } mark_dirty(svm->vmcb, VMCB_INTR); @@ -3635,6 +3645,9 @@ static int msr_interception(struct vcpu_svm *svm) static int interrupt_window_interception(struct vcpu_svm *svm) { + if (avic) + BUG_ON(1); + kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); svm_clear_vintr(svm); svm->vmcb->control.v_irq = 0; @@ -4190,7 +4203,7 @@ static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) { struct vmcb_control_area *control; - + /* The following fields are ignored when AVIC is enabled */ control = &svm->vmcb->control; control->int_vector = irq; control->v_intr_prio = 0xf; @@ -4261,6 +4274,27 @@ static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu) return; } +static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + kvm_lapic_set_vector(vec, avic_get_bk_page_entry(svm, APIC_IRR)); + + /* Note: + * This gives us a hint to check for pending interrupts + * during #VMEXIT. + */ + atomic_inc(&svm->avic_pending_cnt); + + if (vcpu->mode == IN_GUEST_MODE) { + wrmsrl(MSR_AMD64_AVIC_DOORBELL, + __default_cpu_present_to_apicid(vcpu->cpu)); + } else { + kvm_make_request(KVM_REQ_EVENT, vcpu); + kvm_vcpu_kick(vcpu); + } +} + static int svm_nmi_allowed(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4321,6 +4355,9 @@ static void enable_irq_window(struct kvm_vcpu *vcpu) * get that intercept, this function will be called again though and * we'll get the vintr intercept. */ + if (avic) + return; + if (gif_set(svm) && nested_svm_intr(svm)) { svm_set_vintr(svm); svm_inject_irq(svm, 0x0); @@ -4462,6 +4499,67 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu) svm_complete_interrupts(svm); } +static bool avic_check_irr_pending(struct kvm_vcpu *vcpu) +{ + int i; + u32 irr; + struct vcpu_svm *svm = to_svm(vcpu); + + for (i = 0; i < 8; i++) { + irr = *(avic_get_bk_page_entry(svm, + APIC_IRR + (0x10 * i))); + if (irr) + return true; + } + + return false; +} + +static bool svm_avic_check_ppr(struct vcpu_svm *svm) +{ + u32 tpr = *(avic_get_bk_page_entry(svm, APIC_TASKPRI)); + u32 ppr = *(avic_get_bk_page_entry(svm, APIC_PROCPRI)); + + if (ppr && (ppr != tpr)) + return true; + + return false; +} + +/* Note: Returns true means do not block */ +static bool svm_apicv_intr_pending (struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!avic) + return false; + + if (atomic_read(&svm->avic_pending_cnt)) + return true; + + return avic_check_irr_pending(vcpu); +} + +static void avic_post_vmrun(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!avic) + return; + + if (atomic_read(&svm->avic_pending_cnt)) { + if (svm_avic_check_ppr(svm)) + return; + if (avic_check_irr_pending(vcpu)) + return; + /* + * At this point, if there is no interrupt pending. + * So, we decrement the pending count + */ + atomic_dec(&svm->avic_pending_cnt); + } +} + static void svm_vcpu_run(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4588,6 +4686,8 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) kvm_after_handle_nmi(&svm->vcpu); + avic_post_vmrun(vcpu); + sync_cr8_to_lapic(vcpu); svm->next_rip = 0; @@ -5050,7 +5150,9 @@ static struct kvm_x86_ops svm_x86_ops = { .sched_in = svm_sched_in, + .apicv_intr_pending = svm_apicv_intr_pending, .pmu_ops = &amd_pmu_ops, + .deliver_posted_interrupt = svm_deliver_avic_intr, }; static int __init svm_init(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4244c2b..2def290 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8087,7 +8087,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) kvm_x86_ops->check_nested_events(vcpu, false); - return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); + return (kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu) || + (kvm_x86_ops->apicv_intr_pending && + kvm_x86_ops->apicv_intr_pending(vcpu))); } int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)