Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1703202ybh; Tue, 14 Jul 2020 05:15:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzS79Njeb3jbbUTQafJn8yAZpN8xIg2coLtJjSVLwtAqFT5btmx1Q30S5jeAolXQHG3i165 X-Received: by 2002:a50:fb93:: with SMTP id e19mr4113302edq.106.1594728955508; Tue, 14 Jul 2020 05:15:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594728955; cv=none; d=google.com; s=arc-20160816; b=coThCwDDXEXKOMaEMO+ETRSqPhvB6swP8BOmHEmB5kK41SP5NBNiwYykzeV32UJTNS YBzeIf3+rvOhDc+Vlg8b0l8vhNWGhxwE/bdny2in+/32+h153j9PgA4/kkM4nL3zRKI2 I9THLVbJ4dVIOCAdLQ96i+XahUl8YDNEUZF27kbmOUj7pFs7wU0b0dsQnzVYbsLxmODy O3+LY1ZKOrDbsaxwRUlU0on/RX9qMP9AEpxto4CgUBYPy6O5tkqVbJI6YO45joGj0j9G EVapq5Twy5HLr6N/vsR7J2YiOrSa2N/etVJPWkifO7c0AfRKBN+2m0I+dFzvoKLKgCWS 5WkA== 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=OZcht06sDDPNAnqLK2nxBJuoiAA9EOFE0rcgI4uMjuE=; b=ZED06cEoJA1j9H1x71rRGKlU9oDtr66hMogASxQRlWR/MqIIl0nnPlm0S7Ljmb68yo CdUaxdBo8S6nCtjWB43zLyf9Fnh1kMepRtyjR40pw7wq/K6md9nIqVaOWXlz3Y4awjjG IteJrYOciuaXWSnmNhKpXGLEwARY2UKDFxpHI7KJtRvCSlGbGEOruLpL6tjI+D9ksH8W BZ1xIF6FlkrZN41oAVjN6mfhfzbOnuAT3fLY37RU8ZcuZD0Hf3X7kymQfv1wcZs/wziM v9PKDcu9arsQx9lfRFQKfsv3AZHtAnfpZu+A5CCVAJQ71vBDSSFd0sdf/djBHMYD3nRI Jx0Q== 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 i9si11115407edx.408.2020.07.14.05.15.30; Tue, 14 Jul 2020 05:15:55 -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 S1728350AbgGNMOJ (ORCPT + 99 others); Tue, 14 Jul 2020 08:14:09 -0400 Received: from 8bytes.org ([81.169.241.247]:53718 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728399AbgGNMLB (ORCPT ); Tue, 14 Jul 2020 08:11:01 -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 728A9F91; Tue, 14 Jul 2020 14:10:57 +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 45/75] x86/sev-es: Adjust #VC IST Stack on entering NMI handler Date: Tue, 14 Jul 2020 14:08:47 +0200 Message-Id: <20200714120917.11253-46-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 an NMI hits in the #VC handler entry code before it switched to another stack, any subsequent #VC exception in the NMI code-path will overwrite the interrupted #VC handlers stack. Make sure this doesn't happen by explicitly adjusting the #VC IST entry in the NMI handler for the time in can cause #VC exceptions. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/sev-es.h | 8 +++++ arch/x86/kernel/nmi.c | 6 ++++ arch/x86/kernel/sev-es.c | 61 +++++++++++++++++++++++++++++++++++ arch/x86/kernel/traps.c | 2 ++ 4 files changed, 77 insertions(+) diff --git a/arch/x86/include/asm/sev-es.h b/arch/x86/include/asm/sev-es.h index 824e9e6b067c..330140a189be 100644 --- a/arch/x86/include/asm/sev-es.h +++ b/arch/x86/include/asm/sev-es.h @@ -77,4 +77,12 @@ static inline u64 lower_bits(u64 val, unsigned int bits) extern void vc_no_ghcb(void); extern bool handle_vc_boot_ghcb(struct pt_regs *regs); +#ifdef CONFIG_AMD_MEM_ENCRYPT +extern void sev_es_ist_enter(struct pt_regs *regs); +extern void sev_es_ist_exit(void); +#else +static inline void sev_es_ist_enter(struct pt_regs *regs) { } +static inline void sev_es_ist_exit(void) { } +#endif + #endif diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c index d7c5e44b26f7..d94a5bb0bebc 100644 --- a/arch/x86/kernel/nmi.c +++ b/arch/x86/kernel/nmi.c @@ -33,6 +33,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -489,6 +490,9 @@ DEFINE_IDTENTRY_RAW(exc_nmi) this_cpu_write(nmi_cr2, read_cr2()); nmi_restart: + /* Needs to happen before DR7 is accessed */ + sev_es_ist_enter(regs); + this_cpu_write(nmi_dr7, local_db_save()); nmi_enter(); @@ -502,6 +506,8 @@ DEFINE_IDTENTRY_RAW(exc_nmi) local_db_restore(this_cpu_read(nmi_dr7)); + sev_es_ist_exit(); + if (unlikely(this_cpu_read(nmi_cr2) != read_cr2())) write_cr2(this_cpu_read(nmi_cr2)); if (this_cpu_dec_return(nmi_state)) diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index d415368f16ec..2a7cc72db1d5 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -78,6 +78,67 @@ static void __init sev_es_setup_vc_stacks(int cpu) tss->x86_tss.ist[IST_INDEX_VC] = CEA_ESTACK_TOP(&cea->estacks, VC); } +static bool on_vc_stack(unsigned long sp) +{ + return ((sp >= __this_cpu_ist_bot_va(VC)) && (sp < __this_cpu_ist_top_va(VC))); +} + +/* + * This function handles the case when an NMI or an NMI-like exception + * like #DB is raised in the #VC exception handler entry code. In this + * case the IST entry for VC must be adjusted, so that any subsequent VC + * exception will not overwrite the stack contents of the interrupted VC + * handler. + * + * The IST entry is adjusted unconditionally so that it can be also be + * unconditionally back-adjusted in sev_es_nmi_exit(). Otherwise a + * nested nmi_exit() call (#VC->NMI->#DB) may back-adjust the IST entry + * too early. + */ +void noinstr sev_es_ist_enter(struct pt_regs *regs) +{ + unsigned long old_ist, new_ist; + unsigned long *p; + + if (!sev_es_active()) + return; + + /* Read old IST entry */ + old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]); + + /* Make room on the IST stack */ + if (on_vc_stack(regs->sp)) + new_ist = ALIGN_DOWN(regs->sp, 8) - sizeof(old_ist); + else + new_ist = old_ist - sizeof(old_ist); + + /* Store old IST entry */ + p = (unsigned long *)new_ist; + *p = old_ist; + + /* Set new IST entry */ + this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], new_ist); +} + +void noinstr sev_es_ist_exit(void) +{ + unsigned long ist; + unsigned long *p; + + if (!sev_es_active()) + return; + + /* Read IST entry */ + ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]); + + if (WARN_ON(ist == __this_cpu_ist_top_va(VC))) + return; + + /* Read back old IST entry and write it to the TSS */ + p = (unsigned long *)ist; + this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *p); +} + /* Needed in vc_early_forward_exception */ void do_early_exception(struct pt_regs *regs, int trapnr); diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 907ac2b378a8..59d17e541df9 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -59,6 +59,7 @@ #include #include #include +#include #ifdef CONFIG_X86_64 #include @@ -733,6 +734,7 @@ static bool is_sysenter_singlestep(struct pt_regs *regs) static __always_inline void debug_enter(unsigned long *dr6, unsigned long *dr7) { + /* * Disable breakpoints during exception handling; recursive exceptions * are exceedingly 'fun'. -- 2.27.0