Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1959019pxb; Mon, 13 Sep 2021 08:59:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwj2UpbLHDiCRH2Y/s5OgzFWGuenuo6ReboKg3TZcOP84nqiYByP9Y8vWFrq6PqfvY+c50 X-Received: by 2002:a92:dd03:: with SMTP id n3mr5964078ilm.76.1631548771590; Mon, 13 Sep 2021 08:59:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631548771; cv=none; d=google.com; s=arc-20160816; b=b6A+vsYfdM5kS9Sdvw1tk413XaAKomgQ/JH8niC2fVz1d4Cj18eyOPWA+JGR+ZUoW5 td3Yq2Kvmg2ILfutLWoHddJmc921RrKr4lsAmCOvdMBMR4hXF5+lRSaJec8WYqarvAvg xt8yOYpPjupAQFRyWdtMINp9IF0O+1L0QOudPZAdGkmIQ1rD/wptnX40YiFStkbDSUc8 FciFX5wRvIriZEYvp8XmtnVsrFFiT2VUp5+//RvYvB8rEzohD+PPh92/jnXxBKEdydQF RINtXNY99iaGUvHKb3Jdwsado/eKMK7LOhtK4ww9ymSha3fjR7a17tM0vpQ0V315I6r1 E3HQ== 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=DPnoU6ptyD9VpokztKJ0tC7jad1Obtgn8KnH6T0P1b8=; b=aTk18w2NH5i2mp/AVlLxeAXilUNSiAP0xRNTcPSN/N49NjJy9Flu0a74ipFded9A3U YkBQwy+EP4fR/6kPI6d+WepL+b7Droa/TWSFVx9nSL7ObhQDOW3wXoTZ9W2h3JcjVRq4 qbzFLckPdJhBj3/e5pJ4zu4oIDkZnQfPSHXP/DZhpzIVJiLi/wWKNIxm0R6sx+fdDq9Y 0j+QmCqLAtxiLsloXARUOVf4Qszp6Ze3k7dnFPkdFrEETaB4mGpVFm8sgTF05aCNpaT9 aVd2QUNh5BVrj9+GTM6Wrq6CL26LXHVdG8DOgexGONUPLqV9UnxFYOaMuuqr1Idjjtb0 tlsg== 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 a9si6628956ila.32.2021.09.13.08.59.19; Mon, 13 Sep 2021 08:59:31 -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 S1345120AbhIMP62 (ORCPT + 99 others); Mon, 13 Sep 2021 11:58:28 -0400 Received: from 8bytes.org ([81.169.241.247]:56874 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244891AbhIMP5i (ORCPT ); Mon, 13 Sep 2021 11:57:38 -0400 Received: from cap.home.8bytes.org (p549ad441.dip0.t-ipconnect.de [84.154.212.65]) (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 8B1EA1043; Mon, 13 Sep 2021 17:56:19 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Eric Biederman , kexec@lists.infradead.org, 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 , Arvind Sankar , Joerg Roedel , linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 05/12] x86/sev: Use GHCB protocol version 2 if supported Date: Mon, 13 Sep 2021 17:55:56 +0200 Message-Id: <20210913155603.28383-6-joro@8bytes.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913155603.28383-1-joro@8bytes.org> References: <20210913155603.28383-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 Check whether the hypervisor supports GHCB version 2 and use it if available. Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/sev.c | 10 ++++++++-- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/kernel/sev-shared.c | 17 ++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 101e08c67296..7f8416f76be7 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -119,16 +119,22 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, /* Include code for early handlers */ #include "../../kernel/sev-shared.c" +static unsigned int ghcb_protocol; + static u64 sev_get_ghcb_proto_ver(void) { - return GHCB_PROTOCOL_MAX; + return ghcb_protocol; } static bool early_setup_sev_es(void) { - if (!sev_es_negotiate_protocol(NULL)) + struct sev_ghcb_protocol_info info; + + if (!sev_es_negotiate_protocol(&info)) sev_es_terminate(GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED); + ghcb_protocol = info.vm_proto; + if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..134a7c9d91b6 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 36eaac2773ed..40a1ca81bdb8 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -61,6 +61,7 @@ static void __noreturn sev_es_terminate(unsigned int reason) static bool sev_es_negotiate_protocol(struct sev_ghcb_protocol_info *info) { + unsigned int protocol; u64 val; /* Do the GHCB protocol version negotiation */ @@ -71,14 +72,24 @@ static bool sev_es_negotiate_protocol(struct sev_ghcb_protocol_info *info) 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; if (info) { info->hv_proto_min = GHCB_MSR_PROTO_MIN(val); info->hv_proto_max = GHCB_MSR_PROTO_MAX(val); - info->vm_proto = GHCB_PROTO_OUR; + info->vm_proto = protocol; } return true; -- 2.33.0