Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp6247727pxb; Thu, 27 Jan 2022 09:28:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJxm4DpXzcHW+qtdDjauRvYyVZNMnjC+q0eevtQRqGmXk4FAQAMW0/h80sb5bh4kcvWBGDxr X-Received: by 2002:a17:907:1ddd:: with SMTP id og29mr3912587ejc.395.1643304499482; Thu, 27 Jan 2022 09:28:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643304499; cv=none; d=google.com; s=arc-20160816; b=rY1HpPbnzbqw187JQyWsNNlNKVHAtmOBe71paPBtU6MMwddTJxtpQ03L/A52l+R3Gv bGicgl8465d4P/j44OiadsMg+v6YkMMszFSrQijjKkEHz0DHJYblWa2ow9ADDxBx7r/K Scdkyo4s4olynTWdoa5m71WZIcEYT8bnU2wn135rUXKzBtlXhcesHcba6LDacMwQo+S4 sEeDmAG0gzDXEDfrIGdVsG5jG2uJIva6kUPtHFELLZ1udIs0VY5BjqZG9LA/SeAfaGnO Pf21KvdO57xXpgJe7xdEZQyVquV4inl7eEq5TT0qIdWTIIVh/sD3s+YVGQ//D2E2k4oY At4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=IbiXeNAoRbWn7FiiIKc2UdvXFsxaVZTpSl8kJKSOsDI=; b=JV8M0RYday0jV7iM6LOP0lg3jP0gG5AFygiIKqtXs5DQq03p3oBMhiGwXVGSY0vv7H TWIa8sTilCbNMaOaB85jGHhX2FgVKVOXqj8hskyHLXTHBwiEAQI+zqSM7aPhPwq5DUxA hiy6LipX7E7BhWx+WliuVn1tNS0QM3nQs2+UDuBx8ZKHw4Rz7dua1soDAFTi9nJFZsX/ i7J6PSxeIodYSrBeHnpdO1j5SjMtQwSspcogELD8LqpVS8WNUhXzU9QBjs1xrKNtByKK Egy1c9Iquwml+jx/Jfs77ojr9FbI1D8Ksfcx0uAUGV2fA6XhaMXvQxhSc/1QrPbkCWQa 93xg== 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 dp19si1816581ejc.985.2022.01.27.09.27.53; Thu, 27 Jan 2022 09:28:19 -0800 (PST) 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 S238985AbiA0KL1 (ORCPT + 99 others); Thu, 27 Jan 2022 05:11:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236767AbiA0KLW (ORCPT ); Thu, 27 Jan 2022 05:11:22 -0500 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D653C061714; Thu, 27 Jan 2022 02:11:22 -0800 (PST) Received: from cap.home.8bytes.org (p549ad610.dip0.t-ipconnect.de [84.154.214.16]) (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 9E8EE507; Thu, 27 Jan 2022 11:11:19 +0100 (CET) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , Eric Biederman , kexec@lists.infradead.org, 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 , Arvind Sankar , linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v3 02/10] x86/sev: Save and print negotiated GHCB protocol version Date: Thu, 27 Jan 2022 11:10:36 +0100 Message-Id: <20220127101044.13803-3-joro@8bytes.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220127101044.13803-1-joro@8bytes.org> References: <20220127101044.13803-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Save the results of the GHCB protocol negotiation into a data structure and print information about versions supported and used to the kernel log. This is useful for debugging kexec issues in SEV-ES guests down the road to quickly spot whether kexec is supported on the given host. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/kernel/sev-shared.c | 36 ++++++++++++++++++++++++++++++++++-- arch/x86/kernel/sev.c | 8 ++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ec060c433589..17b75f6ee11a 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -12,8 +12,8 @@ #include #include -#define GHCB_PROTO_OUR 0x0001UL -#define GHCB_PROTOCOL_MAX 1ULL +#define GHCB_PROTOCOL_MIN 1ULL +#define GHCB_PROTOCOL_MAX 2ULL #define GHCB_DEFAULT_USAGE 0ULL #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); } diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index ce987688bbc0..60ca7dd64d64 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -14,6 +14,23 @@ #define has_cpuflag(f) boot_cpu_has(f) #endif +/* + * struct ghcb_info - Used to return GHCB protocol + * negotiation details. + * + * @hv_proto_min: Minimum GHCB protocol version supported by Hypervisor + * @hv_proto_max: Maximum GHCB protocol version supported by Hypervisor + * @vm_proto: Protocol version the VM (this kernel) will use + */ +struct ghcb_info { + unsigned int hv_proto_min; + unsigned int hv_proto_max; + unsigned int vm_proto; +}; + +/* Negotiated GHCB protocol version */ +static struct ghcb_info ghcb_info __ro_after_init; + static bool __init sev_es_check_cpu_features(void) { if (!has_cpuflag(X86_FEATURE_RDRAND)) { @@ -44,6 +61,7 @@ static void __noreturn sev_es_terminate(unsigned int reason) static bool sev_es_negotiate_protocol(void) { + unsigned int protocol; u64 val; /* Do the GHCB protocol version negotiation */ @@ -54,10 +72,24 @@ static bool sev_es_negotiate_protocol(void) if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP) return false; - if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTO_OUR || - GHCB_MSR_PROTO_MIN(val) > GHCB_PROTO_OUR) + /* Sanity check untrusted input */ + if (GHCB_MSR_PROTO_MIN(val) > GHCB_MSR_PROTO_MAX(val)) return false; + /* Use maximum supported protocol version */ + protocol = min_t(unsigned int, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX); + + /* + * Hypervisor does not support any protocol version required for this + * kernel. + */ + if (protocol < GHCB_MSR_PROTO_MIN(val)) + return false; + + ghcb_info.hv_proto_min = GHCB_MSR_PROTO_MIN(val); + ghcb_info.hv_proto_max = GHCB_MSR_PROTO_MAX(val); + ghcb_info.vm_proto = protocol; + return true; } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index e6d316a01fdd..8a4317fa699a 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -779,6 +779,14 @@ void __init sev_es_init_vc_handling(void) /* Secondary CPUs use the runtime #VC handler */ initial_vc_handler = (unsigned long)kernel_exc_vmm_communication; + + /* + * Print information about supported and negotiated GHCB protocol + * versions. + */ + pr_info("Hypervisor GHCB protocol version support: min=%u max=%u\n", + ghcb_info.hv_proto_min, ghcb_info.hv_proto_max); + pr_info("Using GHCB protocol version %u\n", ghcb_info.vm_proto); } static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt) -- 2.34.1