Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752250AbdHPPzS (ORCPT ); Wed, 16 Aug 2017 11:55:18 -0400 Received: from mail-dm3nam03on0042.outbound.protection.outlook.com ([104.47.41.42]:35008 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752113AbdHPPzO (ORCPT ); Wed, 16 Aug 2017 11:55:14 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Janakarajan.Natarajan@amd.com; From: Janakarajan Natarajan To: kvm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org Cc: Paolo Bonzini , Radim Krcmar , Joerg Roedel , Andy Lutomirski , Tony Luck , Borislav Petkov , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Yazen Ghannam , Janakarajan Natarajan Subject: [PATCH 2/2] KVM: SVM: Enable Virtual GIF feature Date: Wed, 16 Aug 2017 10:54:50 -0500 Message-Id: <34f2f25e05496f4212c622ecce999dabb0bd2357.1502896608.git.Janakarajan.Natarajan@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CY4PR15CA0004.namprd15.prod.outlook.com (10.172.74.14) To CY4PR12MB1671.namprd12.prod.outlook.com (10.172.72.143) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: adfdc8e1-9695-4ce8-f500-08d4e4bf2ce6 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:CY4PR12MB1671; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1671;3:pqcwwkTR/l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4SGXeKBW6CApQY+Kys6lp1jJKV0Y1r18fptloi4IPz+cleBxgj36NyLpG15N0xHgiWFmq3wCPurXF15sG9H1BozSHxWN1Yatv3IJZUWfXeV84Q4ULLm9L49f2qLeVxNd0OfmQbW6WtPmqDLqCsUWfEanHmuONSbeI4JyTm0jP7fjkhwYMPvTXITN77eC1EyXBkhuw2UkXGF5Rs9WDmM9bdz9epF17iSMRr/oPVgsEqGwgCwKIF7Oku9Mjfe+zvDhNpI0cGcxWk/Rc2Igyyslyn/t1+vx/p+oNVEwhD6/04yti+zQZOrAyqrwbAFTezj/t31S7jwu;4:Gk23F5ivVz6Ll5e9QPd3NxFwYE7ZA9TKl1uKx7e3qLYZsWETsVzA1g9m3TrfDlzCMGmcQFZhT14B7XU8JjSHwMSUteGVhdP2JvlZl3YhQE+ZCrD3ATAEq7/cALP9W6sy+TR3uPxUGI0MzcRCiu20E8HdwgUDyXx7ZLPdicPiP65qij5A8th5IftjHLhW4gReyIZn/yi+vOCYrXEazNnuNn/8f9eoJpSmJOlKYWyDQPbB3+xcQLRdUhJc7CEW9RfdQxNDxoA4DgteoqAO/v7rjnK5+p1MoiqMoUChQbl+HTM= X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(100000703101)(100105400095)(93006095)(93001095)(10201501046)(3002001)(6055026)(6041248)(20161123560025)(20161123558100)(20161123555025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CY4PR12MB1671;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CY4PR12MB1671; X-Forefront-PRVS: 0401647B7F X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(7370300001)(6009001)(39860400002)(199003)(189002)(110136004)(50466002)(305945005)(7736002)(81166006)(81156014)(54906002)(6116002)(575784001)(53936002)(3846002)(86362001)(8676002)(36756003)(66066001)(50986999)(76176999)(7350300001)(47776003)(48376002)(6666003)(2906002)(2950100002)(101416001)(68736007)(42186005)(53416004)(118296001)(5003940100001)(7416002)(189998001)(5660300001)(72206003)(97736004)(25786009)(50226002)(478600001)(106356001)(6486002)(4326008)(105586002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1671;H:gi-joe.amd.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY4PR12MB1671;23:VDszzZxPyTS3SpuGWwu7RVhfXrr1GP7XkoeHBFqfZ?= =?us-ascii?Q?jLCfQyXiQrwEa2wH2xXFfJN5NR7ipDUvI9Pa8zqC9hZXwZEbOhMg0iMK30uU?= =?us-ascii?Q?be7A7AF1f4xZabsNM0fn6OXhup0qdsxV8tKmuizbTgCJ5GQsv/R7YCezqJvq?= =?us-ascii?Q?zgS/P/vNFMRze9UaOIHH/izpwQKqezZ38Q/cKT3ySQumnHRb6hvYURgpgCNF?= =?us-ascii?Q?gA6BsMt6kQ/B5PGfROdpF+3Hw0851b3uCQnh+nvE1qnoIoVlWHBSw5l3aKAT?= =?us-ascii?Q?qPn9hxSw5UIvTgqzgytwm4gzQyaoMynJWiWm7Nmtsz0Umkw2W/+L48vtaQlm?= =?us-ascii?Q?urcCctD7q3xlzh6xDox/wQwc+ROg4JDtJMd/gmZ/uU3WHuWKYK1EKDTMlFzQ?= =?us-ascii?Q?ZtRp66DmQl8wmNIpmdc2HJ1S3KE2KXoQQt50iy8fBGuoBBydmnpkQ/3ZikdZ?= =?us-ascii?Q?0lCCAQPFPpgduNdD4wu3bEwpKDLSAM/B10HStgEj3y2+tVF9PBlhG9qa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GPIgqIxGyHLE0POSJwpKqkbXOCSZBDYe4wL8uJDe5wKC7l6ivd/X1LS57T+z+o6zI1NtzL0EZ8iNmIAgyCM9jnBnV5ZAnIZGtBR6iJt7spPzqlYy/Q==;5:oAU4l7W57/xKF2qjdW7B9NAEPnZLLpvGHdojvp3FeOGNHFfJ6Qz7MfncLwBBfhwC1yUWBm7aTcNFLpbYYf+i4m7kr5WfHAHgAejWFtciV6hxCKHrJM0jD1wqW1qlBCXinqH9GFXnYGKpWUF35x7DMg==;24:Hl/OTOD+u7dRYGi2Zov0uob7hQYxDW7oLbHkEuZOVfVZes3khB54+vzmK21FIr/nW3B50pNnOW0ZAQLke3Q4b5gGRjr9ZxvJQlVwxzHSbls=;7:wbUN8D6OD4+ui9g3km6KqdnCPCg0M/Qa+ppIvSIAcLOKRmwPYgLIVdcNQ6GO/B3mpyDYSZ7EfTSFG/ZsxRlYx2HLy5CSLwsMBxCEofRnJ6JzuypdAXRwIiPs4uRbDCM6iOQjOWGHKmbLYYhuJ3wOfiQHpB/9wnBIa0lZrJWEDXvoAYoUZVLfyk6OXVGO5Zj/x6Xs/BNIotUEpRL404hW2cwnXeDMQTgjbphH2oD6qe0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1671;20:cfV3/EcncO1mEjlcfmtqOeoEUdqeL48nnhRqugOBO+/NgO0bT5Use8RlXCWMASnfdbWXG1vB12m3y61JT6hLome5IyAPCx2w2pedTc3o3jQ0HqpPSJ1s1GRHRMiZmKIEMuXmIaA3WgQBOhNmdzXFCx0HPBuWxK6tMq4d0aERH6rVJhVTCwuIxpLf5ooZavru+6RmChrOXfwvtCB3oZKzbVP3RZ4f4HHAkV37lt7CUTYsYJO7sqGvN9ouv2dkLpW1 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2017 15:55:10.4188 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1671 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4783 Lines: 150 Enable the Virtual GIF feature. This is done by setting bit 25 at position 60h in the vmcb. With this feature enabled, the processor uses bit 9 at position 60h as the virtual GIF when executing STGI/CLGI instructions. Since the execution of STGI by the L1 hypervisor does not cause a return to the outermost (L0) hypervisor, the enable_irq_window and enable_nmi_window are modified. The IRQ and NMI windows will be opened even if GIF is not set, under the assumption that on resuming the L1 hypervisor the IRQ and NMI will be held pending until the processor executes the STGI instruction. Signed-off-by: Janakarajan Natarajan --- arch/x86/include/asm/svm.h | 6 ++++++ arch/x86/kvm/svm.c | 47 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 58fffe7..14835dd 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -107,6 +107,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define V_IRQ_SHIFT 8 #define V_IRQ_MASK (1 << V_IRQ_SHIFT) +#define V_GIF_SHIFT 9 +#define V_GIF_MASK (1 << V_GIF_SHIFT) + #define V_INTR_PRIO_SHIFT 16 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT) @@ -116,6 +119,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define V_INTR_MASKING_SHIFT 24 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT) +#define V_GIF_ENABLE_SHIFT 25 +#define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT) + #define AVIC_ENABLE_SHIFT 31 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 1107626..e6ddabb 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -280,6 +280,10 @@ module_param(avic, int, S_IRUGO); static int vls = true; module_param(vls, int, 0444); +/* enable/disable Virtual GIF */ +static int vgif = true; +module_param(vgif, int, 0444); + /* AVIC VM ID bit masks and lock */ static DECLARE_BITMAP(avic_vm_id_bitmap, AVIC_VM_ID_NR); static DEFINE_SPINLOCK(avic_vm_id_lock); @@ -479,19 +483,33 @@ static inline void clr_intercept(struct vcpu_svm *svm, int bit) recalc_intercepts(svm); } +static inline bool vgif_enabled(struct vcpu_svm *svm) +{ + return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); +} + static inline void enable_gif(struct vcpu_svm *svm) { - svm->vcpu.arch.hflags |= HF_GIF_MASK; + if (vgif_enabled(svm)) + svm->vmcb->control.int_ctl |= V_GIF_MASK; + else + svm->vcpu.arch.hflags |= HF_GIF_MASK; } static inline void disable_gif(struct vcpu_svm *svm) { - svm->vcpu.arch.hflags &= ~HF_GIF_MASK; + if (vgif_enabled(svm)) + svm->vmcb->control.int_ctl &= ~V_GIF_MASK; + else + svm->vcpu.arch.hflags &= ~HF_GIF_MASK; } static inline bool gif_set(struct vcpu_svm *svm) { - return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); + if (vgif_enabled(svm)) + return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); + else + return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); } static unsigned long iopm_base; @@ -1108,6 +1126,13 @@ static __init int svm_hardware_setup(void) } } + if (vgif) { + if (!boot_cpu_has(X86_FEATURE_V_GIF)) + vgif = false; + else + pr_info("Virtual GIF supported\n"); + } + return 0; err: @@ -1305,6 +1330,12 @@ static void init_vmcb(struct vcpu_svm *svm) svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; } + if (vgif) { + clr_intercept(svm, INTERCEPT_STGI); + clr_intercept(svm, INTERCEPT_CLGI); + svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; + } + mark_all_dirty(svm->vmcb); enable_gif(svm); @@ -4686,9 +4717,11 @@ static void enable_irq_window(struct kvm_vcpu *vcpu) * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes * 1, because that's a separate STGI/VMRUN intercept. The next time we * get that intercept, this function will be called again though and - * we'll get the vintr intercept. + * we'll get the vintr intercept. However, if the VGIF feature is + * enabled, the STGI interception will not occur. Enable the irq + * window under the assumption that the hardware will set the GIF. */ - if (gif_set(svm) && nested_svm_intr(svm)) { + if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) { svm_set_vintr(svm); svm_inject_irq(svm, 0x0); } @@ -4702,8 +4735,8 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu) == HF_NMI_MASK) return; /* IRET will cause a vm exit */ - if ((svm->vcpu.arch.hflags & HF_GIF_MASK) == 0) - return; /* STGI will cause a vm exit */ + if (!vgif_enabled(svm) && !gif_set(svm)) + return; /* STGI will cause a vm exit or HW will set VGIF*/ if (svm->nested.exit_required) return; /* we're not going to run the guest yet */ -- 2.7.4