Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1701435ybh; Tue, 14 Jul 2020 05:13:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhAIv5jAhLm7PKUdc/eI+7h20Nvf4cZYxHq/CbGFBxOBjbOPWReDab0Zlq/XEdp4GfmsW1 X-Received: by 2002:a05:6402:1841:: with SMTP id v1mr4194197edy.198.1594728785400; Tue, 14 Jul 2020 05:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594728785; cv=none; d=google.com; s=arc-20160816; b=IVRQzmMl3YdzKVw7bhX1MCyyIgaHjMzluBKVIm3jJ35IhKF9G2Gc6WV6Kt11ANSgRU KwagSra/htmF7+ywIaCg8Fi+BZta6czXLHjpf0IBZDYBrQ1vYBgRc9As/5ACIJ4GxPYk ofL1h7zelpEKq7Eq6U4I5YuUC/v2b/MB2RphZ/qAIsF3Xp9OUOHWk4yRt2c4W8/RdUk+ cS0gxsN7Fu9PknUpCPmY3QzjnRPRDNwSb98E6llrHKA1gTzI95bc6DmpBUOnFhtxVgFu 9QVKkErs3D+6C/6ccD/e+ndg5PARIkq8uLfXR7yTzyEAoqwy3Dy9IXJ8IFa1PD7Xzzvx /q7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=jnboFFHTAarEpOoIgLKtwcaSKMqoDZBl/++FXX5dQAo=; b=tS18XPygybypQ1BNKf5qAmkTqzfA2Qn9e/C1zuV6PH/evYF8wbYVQjTsydNF4dRIMU g22y7/rqv65vJC/8J47Cwj0BvcAax9o1EXANrZ/xg+yT+Kz+GxfN/zp9VIqQPYz+hs99 yBU9WxduuuhIwrx+1+LbPxYeTaydXFT5Fvj2vx9pbiLzf99llodfRi/i7LbOPrdaRCfV 1186opUG8usBrlZ4keABVdevf7H06yLdU4IEVgMVe90ZF5PNhL5XVoqC80A+ThUCne6A qS1rMAPiDf+tDnRUcxkxZFDW/WHIEbMS8KE4JBZOcSoUQj9vETdqLQ2m7DHpuhoUMTkd oZxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q26si10162647ejc.433.2020.07.14.05.12.39; Tue, 14 Jul 2020 05:13:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728685AbgGNMLo (ORCPT + 99 others); Tue, 14 Jul 2020 08:11:44 -0400 Received: from 8bytes.org ([81.169.241.247]:54846 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728605AbgGNMLX (ORCPT ); Tue, 14 Jul 2020 08:11:23 -0400 Received: from cap.home.8bytes.org (p5b006776.dip0.t-ipconnect.de [91.0.103.118]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id 7452BFF0; Tue, 14 Jul 2020 14:11:13 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v4 73/75] x86/sev-es: Handle NMI State Date: Tue, 14 Jul 2020 14:09:15 +0200 Message-Id: <20200714120917.11253-74-joro@8bytes.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714120917.11253-1-joro@8bytes.org> References: <20200714120917.11253-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel When running under SEV-ES the kernel has to tell the hypervisor when to open the NMI window again after an NMI was injected. This is done with an NMI-complete message to the hypervisor. Add code to the kernels NMI handler to send this message right at the beginning of do_nmi(). This always allows nesting NMIs. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/sev-es.h | 2 ++ arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kernel/nmi.c | 6 ++++++ arch/x86/kernel/sev-es.c | 21 +++++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/arch/x86/include/asm/sev-es.h b/arch/x86/include/asm/sev-es.h index 25ab44f977db..de4d3f63fdf8 100644 --- a/arch/x86/include/asm/sev-es.h +++ b/arch/x86/include/asm/sev-es.h @@ -84,10 +84,12 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); extern void sev_es_ist_enter(struct pt_regs *regs); extern void sev_es_ist_exit(void); extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh); +extern void sev_es_nmi_complete(void); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } +static inline void sev_es_nmi_complete(void) { } #endif #endif diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 20a05839dd9a..0f837339db66 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -84,6 +84,7 @@ /* SEV-ES software-defined VMGEXIT events */ #define SVM_VMGEXIT_MMIO_READ 0x80000001 #define SVM_VMGEXIT_MMIO_WRITE 0x80000002 +#define SVM_VMGEXIT_NMI_COMPLETE 0x80000003 #define SVM_VMGEXIT_AP_HLT_LOOP 0x80000004 #define SVM_VMGEXIT_AP_JUMP_TABLE 0x80000005 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c index d94a5bb0bebc..2d60220bf6d5 100644 --- a/arch/x86/kernel/nmi.c +++ b/arch/x86/kernel/nmi.c @@ -479,6 +479,12 @@ static DEFINE_PER_CPU(unsigned long, nmi_dr7); DEFINE_IDTENTRY_RAW(exc_nmi) { + /* + * Re-enable NMIs right here when running as an SEV-ES guest. This might + * cause nested NMIs, but those can be handled safely. + */ + sev_es_nmi_complete(); + if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id())) return; diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index b147d97856c3..ff440b6e5e78 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -423,6 +423,27 @@ static bool vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt, /* Include code shared with pre-decompression boot stage */ #include "sev-es-shared.c" +void sev_es_nmi_complete(void) +{ + struct ghcb_state state; + struct ghcb *ghcb; + + if (!sev_es_active()) + return; + + ghcb = sev_es_get_ghcb(&state); + + vc_ghcb_invalidate(ghcb); + ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE); + ghcb_set_sw_exit_info_1(ghcb, 0); + ghcb_set_sw_exit_info_2(ghcb, 0); + + sev_es_wr_ghcb_msr(__pa(ghcb)); + VMGEXIT(); + + sev_es_put_ghcb(&state); +} + static u64 sev_es_get_jump_table_addr(void) { struct ghcb_state state; -- 2.27.0