From patchwork Sat Jul 22 11:18:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124281 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp759320vqg; Sat, 22 Jul 2023 04:54:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlHGwMEzYgOfadlmQboO2DY+u/jZIs1wrDPxNKDfGMVkMH/aG6GyakftlG/VziA8tPqToEXP X-Received: by 2002:a17:907:7614:b0:992:6656:4043 with SMTP id jx20-20020a170907761400b0099266564043mr4007680ejc.53.1690026864384; Sat, 22 Jul 2023 04:54:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690026864; cv=pass; d=google.com; s=arc-20160816; b=N9zlKYkQkL+OxVdW8MXSCl8+TQhM2Is9djo7iYQzr8v8JcCHMvmXyfDBCPuHICa+1i 7mszKn7UUoT8xpsEne59TkG6uzmQcn9xdDlrbGjqu4sxV+8MlYUtfSBnFhRJpHN3Hwyo AIuXwEfhD1fHZi5Q47M5wvWFlJVwep+OnYmcfCFhM1X9upVc3VttnRJDYoVvY/6qXzvC Fy6Y6HOeKoBUDh4HYT8gdLZhDOwdpsyn4xhYv17yE5Tjj/GdLZ+kZp1Q73vX5q8YoF1c JbPeJiFPP5oO7mg0j8aDSulSYesPjyvDVO03awbuB5Hjr8KLycO6eawtlzCPM+L9Fpy4 AtmA== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=VRFylGabQzzvJqhEhXUBSxSlnX16uDNWgUblpWO7iA0=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=0gBMAbGYz7kw7A9o9d1OS+1RqfgVYxH0HheQWDzK5mJWnrrQu1bHg0NVknFaHCcrMA cZIWJR216qCGqZrfWOhleu6ILQb95tKis8u0YlJeEN3EZHE8awzhtJGJw2kGKKA609+V 4uFXDeSiP6I1Su1+Q/Nlvdpo1aaWenh9HJFHNB8kxfBVY8St+ni+2lZCMw2B91NzScm7 CA+RaO2t7N5eQfUdCcRBg3HpqpKmbp6ptQSe6Fpi1TGbY9TnStHR06C28y7Xe0MwowaS Q9nrqGzW/6F77VvFlM8/24rQkSKU2aSrxw0mBYUu8Ayv1cOu6iaOSE0BMzii4OS3JIy6 vQRg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=U37wRY9+; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o5-20020a170906288500b00992cd72e0b1si3432835ejd.667.2023.07.22.04.53.59; Sat, 22 Jul 2023 04:54:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=U37wRY9+; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229663AbjGVLU3 (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229626AbjGVLUY (ORCPT ); Sat, 22 Jul 2023 07:20:24 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2075.outbound.protection.outlook.com [40.107.223.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A6382691 for ; Sat, 22 Jul 2023 04:20:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Kk5iE1Re+7ajfK2Eyq6jJmhRpgXnRPdturyZldGAx2w/SRnZDu+tQGx3Q/+5WIBE72FO/8cI1iHvzgwlo7IjW/ZqZz3JjRNDgN83618Gv9PBsFo09AacN9mz9rRizAlnS89Rqwr4X1wgjY6oWFRPPIJ6/CPXYzZDP9OSXJ0WHFH5CTPV0Wo6ATvWZMHD2cOu30kXyVuTEhVKvdrYv17R2t6b2FVtyvMvVrTyE4QWVqUVOO7JJ3L5PqTlCNBFR2lZFDNSMjECDrxy8tnaP4AsL3DEdqPBR4sbpRCyV5nfFv9llbgSpV/g80FkouSe0QMpXEljN0ZZqyqxrYmrbQayUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VRFylGabQzzvJqhEhXUBSxSlnX16uDNWgUblpWO7iA0=; b=ZEI+4WPI2fcCP4fsIhWWbTWYdgIHoOXZeIjpnxasMNsY1ol9seLZgKFBiKuF+dmwWxlUO6xkQb0wM5gbiWmnmEU0Jv5qwA6PkOt4vOCYcI16E3yogVsFFb3WLs1+0KctFARz36NppXYne9pDtWOYir2/9+539bXvVZJxGUhaxwFVzAUebu7vZ68WPtfx7/3pIgdas1IvIMVKUWeFSCLiag2r6QDVc6fZNw/Gtxv17qN3+FTNbqnID5aBq03yiF2N0LKMZefmCJXpsaGsuNyyVs+HFMN5Gg5nB+lNsY+hyh1xEPn9I0xD5yu0G9PuECDKR5140UgM87usGOGq0DGedA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VRFylGabQzzvJqhEhXUBSxSlnX16uDNWgUblpWO7iA0=; b=U37wRY9+1R9XCG7UNueJUc1JZGMlFLnBJJ43tesaK0Ic9z2nAwDhCZ/OeFMIR4BoJmy3K2YnDVKKTbi4CLAMcQaFu8PkzFOy/fEFRgEr1BF9Vo3wyJKzFQesHm1MZsB+y6+cTqE0iY8OsBEo+cxmxnwWAkvdk5S83aLpTsCKhqw= Received: from DM5PR07CA0072.namprd07.prod.outlook.com (2603:10b6:4:ad::37) by PH7PR12MB7019.namprd12.prod.outlook.com (2603:10b6:510:1b9::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.29; Sat, 22 Jul 2023 11:20:18 +0000 Received: from DM6NAM11FT052.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ad:cafe::de) by DM5PR07CA0072.outlook.office365.com (2603:10b6:4:ad::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30 via Frontend Transport; Sat, 22 Jul 2023 11:20:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT052.mail.protection.outlook.com (10.13.172.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:18 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:13 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 01/14] virt: sev-guest: Use AES GCM crypto library Date: Sat, 22 Jul 2023 16:48:56 +0530 Message-ID: <20230722111909.15166-2-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT052:EE_|PH7PR12MB7019:EE_ X-MS-Office365-Filtering-Correlation-Id: 85fe9f5b-a2fc-42e8-dd6e-08db8aa5a16f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZczAdonh/phlz5K6JExTl18JiS4iRKVfg+QXPiJ6oeXSWNiLBuXwUWos1WuuMH/8ZmZI2noYi1OM7JjTzu3HvR35czV2alpHQp5S1ZS2q15DlGROLgsVd9QTXbR9n9uGfCm6gD207bsCcHmif12M5JUPEFfMxTowYxP2DkrgA8etgSf0WJDESTxvpte+lx+TiPBrTmTPw1urC0FDwPxoQ68w6Oo85pZ+x7V/Bn8A8aqco7LjVQKWCifzfAHq0zX+yW3nQ8N6mBphVTqLKZJbwAqSLSs5AZBF/7LnNfFlBrom9OSZGgNwkvUIlygWaV+x9KGtg5OB9nXqKBYDRdz8pq5J0lI5fQsGLgL9A1KhKXEIyJhIk+YFpwbMll/3+XIYRiZGX22ldAlQnXMvUCs7PYhbI/Z9zY/96Nqg13kuPFbgFytfy8hFumJpxhnwsGOgeFjm54S++n06J5JUcyDdOC3osxRSUgZmffotKOBSW1XNNTKY/H99nBL7OVewrnKhniZXiD/JFToH1YfokO39gSbbyQmd7PoLDnoMbWRbpwebo+7kDd0oR0kogScFS0ZJz//Xf/U+ZQHqm/PmH82JWfWGqw8JP80egJxYfIOQy83BklxhZ2vs6HAReFZIOIZ0QblglyYhovM8La8DsOt6GmuzN44lbGz0l2+013ws8I68hH6hM4IyNPRZ7zWIu3Y+w9iz4O+jKBZyY7L821RodokOZA6ijae+osd/S42B6GgBRJp+7aPICIgqNa4y4NLHz77/FADVmrI2cGPG85zGtw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(376002)(346002)(396003)(82310400008)(451199021)(36840700001)(46966006)(40470700004)(7696005)(966005)(54906003)(110136005)(478600001)(6666004)(36860700001)(83380400001)(47076005)(426003)(40460700003)(36756003)(40480700001)(30864003)(2906002)(16526019)(336012)(2616005)(186003)(1076003)(26005)(356005)(82740400003)(81166007)(70206006)(4326008)(316002)(70586007)(41300700001)(8676002)(8936002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:18.4819 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 85fe9f5b-a2fc-42e8-dd6e-08db8aa5a16f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT052.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7019 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772121609414319471 X-GMAIL-MSGID: 1772121609414319471 The sev-guest driver encryption code uses Crypto API for SNP guest messaging to interact with AMD Security processor. For enabling SecureTSC, SEV-SNP guests need to send a TSC_INFO request guest message before the smpboot phase starts. Details from the TSC_INFO response will be used to program the VMSA before the secondary CPUs are brought up. The Crypto API is not available this early in the boot phase. In preparation of moving the encryption code out of sev-guest driver to support SecureTSC and make reviewing the diff easier, start using AES GCM library implementation instead of Crypto API. Link: https://lore.kernel.org/all/20221103192259.2229-1-ardb@kernel.org CC: Ard Biesheuvel Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/Kconfig | 3 +- drivers/virt/coco/sev-guest/sev-guest.c | 172 +++++++----------------- drivers/virt/coco/sev-guest/sev-guest.h | 3 + 3 files changed, 53 insertions(+), 125 deletions(-) diff --git a/drivers/virt/coco/sev-guest/Kconfig b/drivers/virt/coco/sev-guest/Kconfig index f9db0799ae67..bcc760bfb468 100644 --- a/drivers/virt/coco/sev-guest/Kconfig +++ b/drivers/virt/coco/sev-guest/Kconfig @@ -2,8 +2,7 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - select CRYPTO_AEAD2 - select CRYPTO_GCM + select CRYPTO_LIB_AESGCM help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read, diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 97dbe715e96a..520e2b6613a7 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -16,8 +16,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -28,24 +27,16 @@ #include "sev-guest.h" #define DEVICE_NAME "sev-guest" -#define AAD_LEN 48 -#define MSG_HDR_VER 1 #define SNP_REQ_MAX_RETRY_DURATION (60*HZ) #define SNP_REQ_RETRY_DELAY (2*HZ) -struct snp_guest_crypto { - struct crypto_aead *tfm; - u8 *iv, *authtag; - int iv_len, a_len; -}; - struct snp_guest_dev { struct device *dev; struct miscdevice misc; void *certs_data; - struct snp_guest_crypto *crypto; + struct aesgcm_ctx *ctx; /* request and response are in unencrypted memory */ struct snp_guest_msg *request, *response; @@ -68,6 +59,15 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. /* Mutex to serialize the shared buffer access and command handling. */ static DEFINE_MUTEX(snp_cmd_mutex); +static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) +{ + if (snp_dev && snp_dev->ctx) + return snp_dev->ctx->authsize; + + WARN_ONCE(1, "Unable to get crypto authsize\n"); + return 0; +} + static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) { char zero_key[VMPCK_KEY_LEN] = {0}; @@ -152,132 +152,59 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct snp_guest_crypto *init_crypto(struct snp_guest_dev *snp_dev, u8 *key, size_t keylen) +static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) { - struct snp_guest_crypto *crypto; + struct aesgcm_ctx *ctx; - crypto = kzalloc(sizeof(*crypto), GFP_KERNEL_ACCOUNT); - if (!crypto) + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); + if (!ctx) return NULL; - crypto->tfm = crypto_alloc_aead("gcm(aes)", 0, 0); - if (IS_ERR(crypto->tfm)) - goto e_free; - - if (crypto_aead_setkey(crypto->tfm, key, keylen)) - goto e_free_crypto; - - crypto->iv_len = crypto_aead_ivsize(crypto->tfm); - crypto->iv = kmalloc(crypto->iv_len, GFP_KERNEL_ACCOUNT); - if (!crypto->iv) - goto e_free_crypto; - - if (crypto_aead_authsize(crypto->tfm) > MAX_AUTHTAG_LEN) { - if (crypto_aead_setauthsize(crypto->tfm, MAX_AUTHTAG_LEN)) { - dev_err(snp_dev->dev, "failed to set authsize to %d\n", MAX_AUTHTAG_LEN); - goto e_free_iv; - } + if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { + pr_err("SNP: crypto init failed\n"); + kfree(ctx); + return NULL; } - crypto->a_len = crypto_aead_authsize(crypto->tfm); - crypto->authtag = kmalloc(crypto->a_len, GFP_KERNEL_ACCOUNT); - if (!crypto->authtag) - goto e_free_iv; - - return crypto; - -e_free_iv: - kfree(crypto->iv); -e_free_crypto: - crypto_free_aead(crypto->tfm); -e_free: - kfree(crypto); - - return NULL; + return ctx; } -static void deinit_crypto(struct snp_guest_crypto *crypto) -{ - crypto_free_aead(crypto->tfm); - kfree(crypto->iv); - kfree(crypto->authtag); - kfree(crypto); -} - -static int enc_dec_message(struct snp_guest_crypto *crypto, struct snp_guest_msg *msg, - u8 *src_buf, u8 *dst_buf, size_t len, bool enc) -{ - struct snp_guest_msg_hdr *hdr = &msg->hdr; - struct scatterlist src[3], dst[3]; - DECLARE_CRYPTO_WAIT(wait); - struct aead_request *req; - int ret; - - req = aead_request_alloc(crypto->tfm, GFP_KERNEL); - if (!req) - return -ENOMEM; - - /* - * AEAD memory operations: - * +------ AAD -------+------- DATA -----+---- AUTHTAG----+ - * | msg header | plaintext | hdr->authtag | - * | bytes 30h - 5Fh | or | | - * | | cipher | | - * +------------------+------------------+----------------+ - */ - sg_init_table(src, 3); - sg_set_buf(&src[0], &hdr->algo, AAD_LEN); - sg_set_buf(&src[1], src_buf, hdr->msg_sz); - sg_set_buf(&src[2], hdr->authtag, crypto->a_len); - - sg_init_table(dst, 3); - sg_set_buf(&dst[0], &hdr->algo, AAD_LEN); - sg_set_buf(&dst[1], dst_buf, hdr->msg_sz); - sg_set_buf(&dst[2], hdr->authtag, crypto->a_len); - - aead_request_set_ad(req, AAD_LEN); - aead_request_set_tfm(req, crypto->tfm); - aead_request_set_callback(req, 0, crypto_req_done, &wait); - - aead_request_set_crypt(req, src, dst, len, crypto->iv); - ret = crypto_wait_req(enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req), &wait); - - aead_request_free(req); - return ret; -} - -static int __enc_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, +static int __enc_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; - memset(crypto->iv, 0, crypto->iv_len); - memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) + return -EBADMSG; - return enc_dec_message(crypto, msg, plaintext, msg->payload, len, true); + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + aesgcm_encrypt(ctx, msg->payload, plaintext, len, &hdr->algo, AAD_LEN, + iv, hdr->authtag); + return 0; } -static int dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, +static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, void *plaintext, size_t len) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; - /* Build IV with response buffer sequence number */ - memset(crypto->iv, 0, crypto->iv_len); - memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); - - return enc_dec_message(crypto, msg, msg->payload, plaintext, len, false); + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (aesgcm_decrypt(ctx, plaintext, msg->payload, len, &hdr->algo, + AAD_LEN, iv, hdr->authtag)) + return 0; + else + return -EBADMSG; } static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_guest_msg *resp = &snp_dev->secret_response; struct snp_guest_msg *req = &snp_dev->secret_request; struct snp_guest_msg_hdr *req_hdr = &req->hdr; struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + struct aesgcm_ctx *ctx = snp_dev->ctx; dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); @@ -298,11 +225,11 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, * If the message size is greater than our buffer length then return * an error. */ - if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz)) + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > sz)) return -EBADMSG; /* Decrypt the payload */ - return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len); + return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); } static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type, @@ -329,7 +256,7 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); - return __enc_payload(snp_dev, req, payload, sz); + return __enc_payload(snp_dev->ctx, req, payload, sz); } static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, @@ -472,7 +399,6 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_report_resp *resp; struct snp_report_req req; int rc, resp_len; @@ -490,7 +416,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp->data) + crypto->a_len; + resp_len = sizeof(resp->data) + get_ctx_authsize(snp_dev); resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); if (!resp) return -ENOMEM; @@ -511,7 +437,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_derived_key_resp resp = {0}; struct snp_derived_key_req req; int rc, resp_len; @@ -528,7 +453,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp.data) + crypto->a_len; + resp_len = sizeof(resp.data) + get_ctx_authsize(snp_dev); if (sizeof(buf) < resp_len) return -ENOMEM; @@ -552,7 +477,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { - struct snp_guest_crypto *crypto = snp_dev->crypto; struct snp_ext_report_req req; struct snp_report_resp *resp; int ret, npages = 0, resp_len; @@ -590,7 +514,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques * response payload. Make sure that it has enough space to cover the * authtag. */ - resp_len = sizeof(resp->data) + crypto->a_len; + resp_len = sizeof(resp->data) + get_ctx_authsize(snp_dev); resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); if (!resp) return -ENOMEM; @@ -802,8 +726,8 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO; - snp_dev->crypto = init_crypto(snp_dev, snp_dev->vmpck, VMPCK_KEY_LEN); - if (!snp_dev->crypto) + snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); + if (!snp_dev->ctx) goto e_free_cert_data; misc = &snp_dev->misc; @@ -818,11 +742,13 @@ static int __init sev_guest_probe(struct platform_device *pdev) ret = misc_register(misc); if (ret) - goto e_free_cert_data; + goto e_free_ctx; dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id); return 0; +e_free_ctx: + kfree(snp_dev->ctx); e_free_cert_data: free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); e_free_response: @@ -841,7 +767,7 @@ static int __exit sev_guest_remove(struct platform_device *pdev) free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); - deinit_crypto(snp_dev->crypto); + kfree(snp_dev->ctx); misc_deregister(&snp_dev->misc); return 0; diff --git a/drivers/virt/coco/sev-guest/sev-guest.h b/drivers/virt/coco/sev-guest/sev-guest.h index 21bda26fdb95..ceb798a404d6 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.h +++ b/drivers/virt/coco/sev-guest/sev-guest.h @@ -13,6 +13,9 @@ #include #define MAX_AUTHTAG_LEN 32 +#define AUTHTAG_LEN 16 +#define AAD_LEN 48 +#define MSG_HDR_VER 1 /* See SNP spec SNP_GUEST_REQUEST section for the structure */ enum msg_type { From patchwork Sat Jul 22 11:18:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124268 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp747716vqg; Sat, 22 Jul 2023 04:22:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlEMXzls2QowiCzBAiSV7r9NQe7ceMDZm+L9fITEs1D3ocFmgO9FmmMK+r0Owv8LHQzyUtRc X-Received: by 2002:a17:907:77da:b0:994:19:133b with SMTP id kz26-20020a17090777da00b009940019133bmr4189562ejc.14.1690024955456; Sat, 22 Jul 2023 04:22:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690024955; cv=pass; d=google.com; s=arc-20160816; b=ExlldB2us3NmU5dVzbw3fD0rj9Qchp+lrbfQ3ifyuOxxQ1HMq26EdVTZlKGp+dRuHv 04i9tCJFeYZGWiQX1OMAthwRSnwykBvExKrfBl9wBO3dJ3N0CI2IeGQt2JOZ5McnWkrx C91TjlGJtavPAf+iuvCgKKaNqE2ImfSzR4ZaPqgfXzKbziv6ulo3T3vwA/RsK0aVdAjk RQ803zOKwIcq6mVwgjWm/a9npFZ9X0aP56v0+61XTE48rgk1q8JMg0kYoD808n6meBMl dYmy0pfyLaZGQVvPpLPg/9tHKEpgvW5k+cV7C7hed1wMvXCLvhnKSeoDP6IrQq/pMb8R pVYA== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=FuBmIhqf5s50MDqpiNEq7ipa9JYgkOBMMnMLOaInExY=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=H+qo3/cqwa/bQIn/YlrILe0nMjyUXEzvMdk5QD0y//hKNcfu7JuBE2lEN1WDrt/huw PUMxpHR2gVTPEOGk/Gqdj7MOQ5V0mig1nABn2UDgzrtpm/MMFUuKWyoMeIC0EaGb38BN JtyqaBTCCX30ncFhMH4dFFsDnDZuBTjYzmEc3TC6wGo7rxheuTpomO03bqC59T6XtB/J DDLny4WHnDXvb5KJnOvju5ORRGdS0KZtFHJCHCeF5bZSOF78s4S3q2PIDRqhq8Eh0P+F g6ik36yOKvDB15ZSpvyNKBxszbsqnkxkhfu0q0EKr+Zxro66Xzb7wp8J25O7fVBn5rRd M7iQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=BVQZ8T3L; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o14-20020a17090611ce00b0098e05d1a61csi3522818eja.99.2023.07.22.04.22.12; Sat, 22 Jul 2023 04:22:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=BVQZ8T3L; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229688AbjGVLUb (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbjGVLUZ (ORCPT ); Sat, 22 Jul 2023 07:20:25 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2074.outbound.protection.outlook.com [40.107.93.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 573AC30E3 for ; Sat, 22 Jul 2023 04:20:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GK0zBrPVIgxs4piW1AVVS91nRokxDcDTfdJkmZfOVwswcTvHoLraGyg6SBlvkFjllECQ/OU7yDglXj9dFnucszpd2KCSe/uuZxm4OQ3PaFFmorpmBKvPbEYf4i55zSLyQ2kYr+2aULx/FueAFXiTaV5si4/fWk563QMAfArPrC0newh+U7Ilb/f/x/9JJ5EtJItRDsj6aNzNLQPdB/ZVRAFowbOlM5llFP0mxlct182HZ2DB8InvbvV/E5F4NDTntePVS+0y/hJr24mIqgwa3D1y/NKGq5guMMYOArC1Fgr61NlOC9rk3fMfWP3yjIvCpcqmQ7UK1rThCbRIWIpxQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FuBmIhqf5s50MDqpiNEq7ipa9JYgkOBMMnMLOaInExY=; b=ANH+wfKkcZaErbH6v1qevRwjUnc5xuOE9DyMRy+FcFe6RQno1gf4IlftmD4NV5VN4AlDquZyZS4a8aN7ZgMxksF1LAUyOyrSE1AmRZ4J8ONXRlpKsTye24zrn7Fjf15CnCH8VBMFb00ynAECK0Lg5DnDg4Swarp5zzSyVUJthNZxRP4hX+Rpk78i2by9Pitc4UJDr6EjyID/BsqLU+or2Y6rnzd+fmtXSdcR3FX0kKJaLlwbKVZhCGRWzyIOS4ZWPBCjWvMuhUR2BvEtcY2G/fNggApzkxkN/LxfbaVOriaO90ZSID1iASSllBRFY5cbJJe+T8n+V3dlX9TdNfA/NA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FuBmIhqf5s50MDqpiNEq7ipa9JYgkOBMMnMLOaInExY=; b=BVQZ8T3LagNNC0prDWT2cFre/cSH/UD7gHeOR1pc9w8GHUIjykrAPtXom4DHhExDnjdhEnxMD8Ecr+uUnH8yG9FvJmubuW53Z/3THdAt09fLGWrnqdEQ5f2JQAYDDWGtWnZzfkYar4e+qlnlF1qZo1JGM90sAbqno1YHlF3zwlA= Received: from DS7PR03CA0143.namprd03.prod.outlook.com (2603:10b6:5:3b4::28) by SA1PR12MB6871.namprd12.prod.outlook.com (2603:10b6:806:25f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28; Sat, 22 Jul 2023 11:20:20 +0000 Received: from DM6NAM11FT092.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b4:cafe::9d) by DS7PR03CA0143.outlook.office365.com (2603:10b6:5:3b4::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.33 via Frontend Transport; Sat, 22 Jul 2023 11:20:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT092.mail.protection.outlook.com (10.13.173.44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:20 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:16 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 02/14] virt: sev-guest: Move mutex to SNP guest device structure Date: Sat, 22 Jul 2023 16:48:57 +0530 Message-ID: <20230722111909.15166-3-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT092:EE_|SA1PR12MB6871:EE_ X-MS-Office365-Filtering-Correlation-Id: a218a749-b854-464f-54fc-08db8aa5a273 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QL+8uR6eSgE+9+rrX9gB7Am3d1AU3hMQpIwz0o/iazLnDU15z+j91AnbXcQzobdw77634fqg333Q68hkTvJVKkNM1Ak4Hw1m3JAeEqlLXltH11rAWYEyeSMxsAKcV7k0Fm9Z1KrLKC6JEuycpoJahOiMDzDpKOgOeoBi94bR5STul8M03Mc48AoNjH4LBL+O7sKoRCzI6Cz2fEWdJgJzPXlfZYOzV2DQ5Yw+vG7qoduJZXSHQWIHhIT3T9QCkGh8USc+Z/TOluXLkA6uGqFWKA+fWrd3H7imj0BlarzUiJFsBcmjj4PdMjK0jRKOUTjNpyRk40WQRzKWQtmf3d/0J3bc77mutR7SE/Zvw/QOg7MtSxx7srS++hGuaQGmfS1CcRZJlKcuUhJtjvj4DbIjImlfCli6PZbH9u9RBgLRRc5IZ0XDQ0OoWgzhOA2Nss/evn0qiGZx29EiANFwozA5hUfeZHuKcHPcGdoCLe5MsXHWn6hSIPo9PqyzYDmQtz+7otzdd1CJ3WcAed63rJO1m7uvTJz5PB0TO4n6nR/5VHMDmHzsKQDxzpHb3+o52H9xYQ4nbrwysLqU0GYRA3H531iuFHpmCFrLCbhoy4qfedO8vNWs6L5I5/qKah6eWfCoy/IQk/4QiPYFWwuKKhTrChkNy4BiXQVljAFtCSlsCvxIFsdZ+hOxP9FHHTVOpbBV5EDGdyR2vk8FtPyBV0yJeYDgXLubSC3X6KLro10NGtw36svLBlgdqpWcBn6S/nylALBxnWQMAOyhIaVo8rwCqQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199021)(82310400008)(46966006)(40470700004)(36840700001)(83380400001)(40460700003)(36860700001)(2906002)(2616005)(47076005)(426003)(82740400003)(356005)(81166007)(40480700001)(70206006)(70586007)(4326008)(316002)(41300700001)(1076003)(186003)(36756003)(336012)(478600001)(26005)(16526019)(110136005)(7696005)(54906003)(6666004)(5660300002)(8936002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:20.2136 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a218a749-b854-464f-54fc-08db8aa5a273 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT092.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB6871 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119607933494643 X-GMAIL-MSGID: 1772119607933494643 In preparation for providing a new API to the sev-guest driver for sending an SNP guest message, move the SNP command mutex to the snp_guest_dev structure. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/sev-guest.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 520e2b6613a7..8ba624088d73 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -35,6 +35,9 @@ struct snp_guest_dev { struct device *dev; struct miscdevice misc; + /* Mutex to serialize the shared buffer access and command handling. */ + struct mutex cmd_mutex; + void *certs_data; struct aesgcm_ctx *ctx; /* request and response are in unencrypted memory */ @@ -56,9 +59,6 @@ static u32 vmpck_id; module_param(vmpck_id, uint, 0444); MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP."); -/* Mutex to serialize the shared buffer access and command handling. */ -static DEFINE_MUTEX(snp_cmd_mutex); - static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) { if (snp_dev && snp_dev->ctx) @@ -107,7 +107,7 @@ static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) { u64 count; - lockdep_assert_held(&snp_cmd_mutex); + lockdep_assert_held(&snp_dev->cmd_mutex); /* Read the current message sequence counter from secrets pages */ count = *snp_dev->os_area_msg_seqno; @@ -403,7 +403,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io struct snp_report_req req; int rc, resp_len; - lockdep_assert_held(&snp_cmd_mutex); + lockdep_assert_held(&snp_dev->cmd_mutex); if (!arg->req_data || !arg->resp_data) return -EINVAL; @@ -443,7 +443,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque /* Response data is 64 bytes and max authsize for GCM is 16 bytes. */ u8 buf[64 + 16]; - lockdep_assert_held(&snp_cmd_mutex); + lockdep_assert_held(&snp_dev->cmd_mutex); if (!arg->req_data || !arg->resp_data) return -EINVAL; @@ -481,7 +481,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques struct snp_report_resp *resp; int ret, npages = 0, resp_len; - lockdep_assert_held(&snp_cmd_mutex); + lockdep_assert_held(&snp_dev->cmd_mutex); if (!arg->req_data || !arg->resp_data) return -EINVAL; @@ -566,12 +566,12 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long if (!input.msg_version) return -EINVAL; - mutex_lock(&snp_cmd_mutex); + mutex_lock(&snp_dev->cmd_mutex); /* Check if the VMPCK is not empty */ if (is_vmpck_empty(snp_dev)) { dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); - mutex_unlock(&snp_cmd_mutex); + mutex_unlock(&snp_dev->cmd_mutex); return -ENOTTY; } @@ -589,7 +589,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long break; } - mutex_unlock(&snp_cmd_mutex); + mutex_unlock(&snp_dev->cmd_mutex); if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input))) return -EFAULT; @@ -708,6 +708,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; } + mutex_init(&snp_dev->cmd_mutex); platform_set_drvdata(pdev, snp_dev); snp_dev->dev = dev; snp_dev->layout = layout; From patchwork Sat Jul 22 11:18:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124269 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp747799vqg; Sat, 22 Jul 2023 04:22:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlF2rcsZGujDuNI2S2pPuqSJo277XgjnDhAFOQuc3yZPXLqOBW2toXdGWnbA9qZ5DuxDbDoo X-Received: by 2002:a05:6402:294:b0:51d:e7b5:547d with SMTP id l20-20020a056402029400b0051de7b5547dmr3837010edv.34.1690024968771; Sat, 22 Jul 2023 04:22:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690024968; cv=pass; d=google.com; s=arc-20160816; b=lQDSefmbJbI2fqdv0Z1nBSbcXktobD1+UDT1JIzUrs1cgFab5nvrQVjPOSNOYrucW3 pZ0OdSiQxP9ofOJxkRwbx4tmzkWvFS2/r3+1W1m/fD4BVW5ou6JOCzea/EcUt5g/d+y3 SirmcBDx6hVLvVhmBxOZIO/UnZ758AdpNj0dNYN4bmaivPNDjv2wN3iAl2wjB8g9OVPd cTRKm4b+CLw4z6ztLoCyVdfThmYBjjmC8+jisK2yXpptaC5dZ3whxBgBUE+oippDy2+P ZpTAaNjK1LDjuIQHr1qaLtQsabBia2lrJ4ne9FZd/k92urkSAB8DeW3yBwafTTodwIH9 nhwQ== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=wf2e9idX4lvI5Wg8UX9KE4a4P7OUa7ldcMXtVNnLRlA=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=T6SaggbTWW4FhvTqUo/ThmXlHd4lBmnXFd5PR6ekNrutV/iXKrmzcAZ8nMq9LF8Wc6 dD0ulaDjaXg0MeowHgg3SRnAyzYbOv8HT2x7Iv7D8ATNz5f5u9VenIGUaYL2IJ/aXFRy YV1y0l1ZzRK8YoGOsb8p9/o5pm84VjrgAL1QnpXIW6qVFVJVq0hVIgl8239RyCIrVMwb 0epLQMdm9a17u1Y50EwRyUAgheXlSLilOkkef9/0LO+UkusenrcjWQfXiR40IN7iifwH BPer1H57gUwA/mtvr8gf9eJVdRXjNzc4RmJwoaVN2H6zQdfO2hDws1PSvyvdkX+LHTN8 fyMg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Iq4k2Jgm; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e25-20020a056402149900b0051df350ee56si3552033edv.100.2023.07.22.04.22.25; Sat, 22 Jul 2023 04:22:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Iq4k2Jgm; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229711AbjGVLUg (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229653AbjGVLU2 (ORCPT ); Sat, 22 Jul 2023 07:20:28 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2055.outbound.protection.outlook.com [40.107.243.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8049A30F2 for ; Sat, 22 Jul 2023 04:20:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HHkURZqBeSLZqWE+XJNoVO7lJcFhNredH+1DyZzpirMlcxrZjCcLL6LhX5O+keczsSXZqb0Kx+2z8BJCv3ODPxGoWiCD3HqC+yWHY4moQNV4zMUGnRMcTlCNpbB5pGFFgbu6pC1DcN8KjwfTZFuQsQCwVbd/yw9F7f3JmPeAEd6ZhXTQcEYQKFg/gVXmT44S+wRTUf66FKZI0/lPop85ZR5VLgBqP1g1Qf0GzOdcHhpKg76cxZwNdqIi8mkI2QMOwWHFiLbDISFkwbP9eNk/xxmnOs5sRD9+nUTuChyiC4XefIx1Swms16mLclw8sF1XRC0Ajg6kt96iJtLBjzqQ2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wf2e9idX4lvI5Wg8UX9KE4a4P7OUa7ldcMXtVNnLRlA=; b=BA3jpmZVKeIOHRncHewN62rN4LtQE/6Kf6u3vbqwm6cRsGxX04F+1n41l4SBX6Iili3mHqoTqD/5e4IL5HriivcrcqYRxzszWSQgKOrojNrtIUFDM7aKQVq85VRbbEDtJbka6PI4g+OdFQvaODuc3uVYuomOQx+9OY3AxWPWEYRk4CzSZ9Id3XGb7f9KKYrl8WRty2LBcuSWgGJlVOOpAhMA2UKTrTmTCtaMzyLZld3/e1wypUQem7mY7hZCDrwhGi6hjiFFGlsYtD/x2tVDrmAhNZV1Dqr0qp5mOtvx/ghOjCFH3dKVYlnyZ2Ftg6goJUqzBx1azvEa2cadZda1ow== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wf2e9idX4lvI5Wg8UX9KE4a4P7OUa7ldcMXtVNnLRlA=; b=Iq4k2JgmV03KI6S/GkD5UbujwThxZVOnuSG5yqWg0yrcxz9ylrtpxDKnzxxJyUTT/4UXdzYbXvrsCc1P0lacA6WP7JiSYcJwNEMYeiRFOFf3IGGTLCagMFOJYCXMqJgPWKefdRWOhpuYfoKRH5cZPu3pP7wiehytIJUNSQOs5H0= Received: from BY3PR04CA0025.namprd04.prod.outlook.com (2603:10b6:a03:217::30) by IA0PR12MB8085.namprd12.prod.outlook.com (2603:10b6:208:400::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.25; Sat, 22 Jul 2023 11:20:24 +0000 Received: from DM6NAM11FT077.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:217:cafe::ec) by BY3PR04CA0025.outlook.office365.com (2603:10b6:a03:217::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT077.mail.protection.outlook.com (10.13.173.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:23 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:20 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 03/14] virt: sev-guest: Replace pr_debug with dev_dbg Date: Sat, 22 Jul 2023 16:48:58 +0530 Message-ID: <20230722111909.15166-4-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT077:EE_|IA0PR12MB8085:EE_ X-MS-Office365-Filtering-Correlation-Id: 93236ce4-7094-4372-c2e3-08db8aa5a491 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YScvlvQmevpRJ2OQR7ilqM+wCBOibQXgmQc92vqW+559z8+K/HSlUdkgnTStOlnvrQdo+Jn1UYDKWWrP6n26ktLAQpju7eCvQedSXC3V3eHJmeDDAkhFER1eXJCrtvfud5rcYIW7sN6QjfRds2T0U+VTbk0dD1ltdbZmW2E9B6736M3aB7gstFC9tvitjXNXePx7AkPXX7ywA12qPqJ+EJLmOY1R/6bol9dBVM5J4BqO5HhvyH2hL57u2++aCl2TOQdBHwmTpvR24KjzxZAO9giPEmS3iOPdnG6mjYBL8yqPREp4e2hNl8YOK/3cXUWy2bO/ctmjQqCc0U3bObm4bM4W3InMswVP57BxTSV+DEaMOYxj1mp9nQHUXUg6/XwcuvHIwNr3GVyeSEdQO6raLYIy+wZj98XLGduWCblsAl4ES4HBhmK3ClvnAqDcbWqS2jChYMW59cDqWEBeOvqwzAurnOI7ars9KFikImyj/PIjyRGNR7vHLHfVwjbA5MMx+iRlV86H0DU7Ye/ZO7k0Zm7nFGmBVFv0qPUdNl5emQ8EP0v7rXFzDQ4s7gLer9pcuvZmUuvjQh1BgLzQcGYdiD2tYo6Iug7RO5zPaLV5JiZ+2qwjRl96vU7NEUU8eNWlYszU/qYeRWQIqDBx7cbvVHiyJMTKeyk0TnS5ybmehPCJE34bVUNcSDHdI9YkEt3FJKyOP9xrruaKyGv6TZXg6ReUUXhOkD+2s+cWrog/IL/g6Y8GHw1Vj1Nn0wJLh0WV0rghA0nVFX+h+NLh9qbcIg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(396003)(346002)(376002)(82310400008)(451199021)(46966006)(40470700004)(36840700001)(40460700003)(83380400001)(2616005)(426003)(2906002)(47076005)(36860700001)(82740400003)(356005)(81166007)(40480700001)(26005)(70206006)(70586007)(4326008)(316002)(5660300002)(186003)(16526019)(336012)(1076003)(6666004)(41300700001)(7696005)(110136005)(54906003)(478600001)(8936002)(36756003)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:23.7528 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 93236ce4-7094-4372-c2e3-08db8aa5a491 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT077.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8085 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119622032103208 X-GMAIL-MSGID: 1772119622032103208 In preparation of moving code to arch/x86/kernel/sev.c, replace dev_dbg with pr_debug. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- drivers/virt/coco/sev-guest/sev-guest.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 8ba624088d73..538c42e64baa 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -206,8 +206,9 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; struct aesgcm_ctx *ctx = snp_dev->ctx; - dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", - resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); + pr_debug("response [seqno %lld type %d version %d sz %d]\n", + resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, + resp_hdr->msg_sz); /* Copy response from shared memory to encrypted memory. */ memcpy(resp, snp_dev->response, sizeof(*resp)); @@ -253,8 +254,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 if (!hdr->msg_seqno) return -ENOSR; - dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", - hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); + pr_debug("request [seqno %lld type %d version %d sz %d]\n", + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); return __enc_payload(snp_dev->ctx, req, payload, sz); } From patchwork Sat Jul 22 11:18:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124276 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp757015vqg; Sat, 22 Jul 2023 04:47:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlE4WGRzZaT45HO3ZNLuSSv40jZAl8mDWFw/SYwyWgofhPvAD7Qo7MQMaCBsiSL5bZES7IE4 X-Received: by 2002:a17:903:22c2:b0:1ae:6947:e63b with SMTP id y2-20020a17090322c200b001ae6947e63bmr7268843plg.16.1690026477389; Sat, 22 Jul 2023 04:47:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690026477; cv=pass; d=google.com; s=arc-20160816; b=lVECKNW9eByFa7nBmIJBJfOk0Yc5/Afv3gfzP4rl3ILWZgy5B0SPchTSlkXvnqZyLu nKTPe3AnWlHGoA+8HPIZMF5GaZGNcUotntYRg84dECvz5O+8rf/k7M8W87OTfIYBr1Vq WGnLR8O5/Jv5i/LQL4T7nw2bgkNjDpHwBVJOEcJ2WT4YlsQhitliJkerC+TY4D6zk17a gN8vm5mlNnGKQOTf0RNie3BEkjGDLQB3n3oQrXfsSYHxUnrHjYCZDY9NDuYrgAoogdc5 2iqu549tnfd0gYNMZgsB10KdZRby5/DIdGKD4ObHAKy61dUXbTi3DOFG3sONwze9G5u+ /9rQ== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=ffpilB/3qGPS3ggbgJHMnuvXxdipbw488Qhjx0AVJPc=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=HiWI2GrGNcQlhSk6zUXY6V79WDyxmoJw7CTH6PufkH2WF+W0s6lVuaLN9uSqppB/Qf qGuypjlaUDdgXr6wHLxt8I6hXL7R3whRbbjkEikBU1frHEdekT2LNVylLcdOIQQiEotp OqUXrU7igsqLvsJzarwFvAc/bM4QJBV7votLG866Qr5ZyqtCSM2FtlLO3zlRsl7JBMNO 0MvzNbfDQ0styp6eQNDk6T7xwnfpxM3y3nwcq4bsmWQPOzsIEBW9IxKyISK1GmSBoltP N4V64pewxklS21vwgyfdy6Wqyqf2IH7R4flTWamYPqrpkBJ1yFFgNS68vQhq3GnNnAYb nKLQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="5QRQyQ/U"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id km5-20020a17090327c500b001b8c689793fsi4730672plb.514.2023.07.22.04.47.34; Sat, 22 Jul 2023 04:47:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="5QRQyQ/U"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbjGVLUl (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjGVLUh (ORCPT ); Sat, 22 Jul 2023 07:20:37 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2078.outbound.protection.outlook.com [40.107.102.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B67530EF for ; Sat, 22 Jul 2023 04:20:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e82mC/lwAop4514tnuJKMszQabi7rsyKl7UmZ10uNsEcdkIWLZmoXTiKzOXkWtdORxPNgKKsAMPhwWEWtq3OH9mgXIK3PHlUqwGGX326MoMPqBWiAAkrQY3d7HowhmY//mg2QFUWB+1lAkQzuhzf7E8RkEWSpN/zlOgS1ROH3FuWKEDqiQuZ0irTckSexHC//eazuItLjTYTIwR7dbTiJbonzDmfUbcBhk9ztX3vvPy1uOf6WoEXksQcrYPwRzP7gre+kLp9d6xOVUB7+CFcCPVcQpR2wWkntuUyfkHOL1Phl67wRWAeA7VFv7r8kzBSc61x2aslQ3DIQFum8RwLLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ffpilB/3qGPS3ggbgJHMnuvXxdipbw488Qhjx0AVJPc=; b=k7cedH9EeZmz8n5LCMYceSs3cEEOIbvk2DY7e40/oXPWr39gyqBV57hyF0FZp/fUgiGOrOGnYiBbmo3724fFh39gGAzGcZ8AmnT4Eia137g77KIQHOtRXTn8PrTnP6I3c4TeSwUOcLvrgAGatMuKA/Y4El1zEWIWuqylFB273n3fL7TBcJrL96sBZD8jx51GIBn+xF4M6gAgQW2+a9KUMl/wZ8btimSqLFQ6zLR/blCrMno7zfkMiujcBGacDluFany/JRA7iWf27qgG5AIsKH4/StvPZ2vmtY9H8/TA5rU/zAwrEAYrVleGM2mKG9VaRsKn7QIMaHXTduNS4wpcaQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ffpilB/3qGPS3ggbgJHMnuvXxdipbw488Qhjx0AVJPc=; b=5QRQyQ/UT5Qv9llZ4HEZpscITtrZp5mgRNn2NzinnCHYcHY2S58luOQlBBIn2CgV4cX3+uirmEcwp03z4RATZ7zlTBFzdLNZk50MSG/Wk102aHOFeo6XDsP3+kOvRu1JvuUDGLNWXvYc3bPKEhnJawCYWJgplU8ug7lIRxbkQrw= Received: from SJ0PR13CA0053.namprd13.prod.outlook.com (2603:10b6:a03:2c2::28) by LV8PR12MB9357.namprd12.prod.outlook.com (2603:10b6:408:1ff::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.25; Sat, 22 Jul 2023 11:20:27 +0000 Received: from DM6NAM11FT084.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:2c2:cafe::c9) by SJ0PR13CA0053.outlook.office365.com (2603:10b6:a03:2c2::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.16 via Frontend Transport; Sat, 22 Jul 2023 11:20:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT084.mail.protection.outlook.com (10.13.172.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:26 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:23 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 04/14] virt: sev-guest: Add SNP guest request structure Date: Sat, 22 Jul 2023 16:48:59 +0530 Message-ID: <20230722111909.15166-5-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT084:EE_|LV8PR12MB9357:EE_ X-MS-Office365-Filtering-Correlation-Id: 942a793b-def1-407a-c598-08db8aa5a644 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ea7xun6G8DOzAf7DjrQsomslp6r88n1lTEojuXvOeDcnlie5McLmmwh360oJFExLbIxqqFpJn3daYGv6Z8QBd5GHJlulqOYoX6Ttz2a7S8EYzyykK5YEn6JynPK4y75wIVY0YLDT+DGYeZnl2FoadX8JIGrihkA2COqH9dikcBwSsBf6skYof/rtCB0wGzA6xhv3+DFxw1fbZ/hrdkzjmDo+1mwl3glOGFxmHDYhL16wYsMQHlaS9Yy+GchPEKesalBeo9RRd03Wl6HuYQLVoVHCWmkI9BHKcsEQUH4z5PBtesP8ADEs1NfHUsQEpxyboqgvzjUxPcUIyveORMF387rGIdWH8wooPrI5wGeI7xz+SeidEdQxQPOitPochCOvZGwGQl6Yib0E0HjI8XcdQ59NSFzXTqkyXCU/wAkQiOTSg98olCW4U0CCp04XU1uJ4HU8dVAPJ6xqB14TvALwlsw0KQROFam+6ypoI8cPv+R6dXD2eVWHHTS0I12diJdEkWN5IIa0VqGqZbTGdlEIOohpWX3c4uxL+Wf9TPNd8epxlpMJSnIYmMkQwfUonQuMA23YyVqsXNPQS53ZtGPBoDC/HSbX3LAk+sAFN4Wg0aZBH7axtJqAebza3nJgyqCEREdO65aNmjAAAusqSTxg0c+6yc2vk+M3Trbs5sCArUwJzaqDsV43TB4MVCTxuj4AZAfVLnZrDIFYOCzpW0zsV7GnTF7knZ7xfeFL+iZr+pgHRwcHOn0Snes4moPz5kS3FLJCIo4ZESD3mIs7YknKhw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(39860400002)(136003)(346002)(451199021)(82310400008)(46966006)(36840700001)(40470700004)(36756003)(6666004)(70586007)(70206006)(478600001)(110136005)(54906003)(7696005)(26005)(4326008)(1076003)(8676002)(2906002)(40480700001)(8936002)(41300700001)(30864003)(82740400003)(81166007)(5660300002)(356005)(316002)(186003)(47076005)(16526019)(336012)(2616005)(36860700001)(426003)(83380400001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:26.6037 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 942a793b-def1-407a-c598-08db8aa5a644 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT084.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9357 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772121203288024685 X-GMAIL-MSGID: 1772121203288024685 Add a snp_guest_req structure to simplify the function arguments. The structure will be used to call the SNP Guest message request API instead of passing a long list of parameters. Add two helper functions for filling up the parameters: handle_guest_request() and handle_guest_request_ext(). GET_EXT_REPORT queries for certs_data from the AMD Security processor. handle_guest_request_ext() provides those extra parameters for receiving certs_data from the AMD security processor. Signed-off-by: Nikunj A Dadhania --- .../x86/include/asm}/sev-guest.h | 11 ++ arch/x86/include/asm/sev.h | 7 -- arch/x86/kernel/sev.c | 15 ++- drivers/virt/coco/sev-guest/sev-guest.c | 107 ++++++++++++------ 4 files changed, 93 insertions(+), 47 deletions(-) rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (80%) diff --git a/drivers/virt/coco/sev-guest/sev-guest.h b/arch/x86/include/asm/sev-guest.h similarity index 80% rename from drivers/virt/coco/sev-guest/sev-guest.h rename to arch/x86/include/asm/sev-guest.h index ceb798a404d6..22ef97b55069 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.h +++ b/arch/x86/include/asm/sev-guest.h @@ -63,4 +63,15 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { + void *req_buf, *resp_buf, *data; + size_t req_sz, resp_sz, *data_npages; + u64 exit_code; + unsigned int vmpck_id; + u8 msg_version; + u8 msg_type; +}; + +int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, + struct snp_guest_request_ioctl *rio); #endif /* __VIRT_SEVGUEST_H__ */ diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 13dc2a9d23c1..d8e972aeb22c 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -92,8 +92,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); struct snp_req_data { unsigned long req_gpa; unsigned long resp_gpa; - unsigned long data_gpa; - unsigned int data_npages; }; struct sev_guest_platform_data { @@ -201,7 +199,6 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -221,10 +218,6 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npag static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } -static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio) -{ - return -ENOTTY; -} #endif #endif diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index b031244d6d2d..a10cf9f21e22 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -2177,15 +2178,21 @@ static int __init init_sev_config(char *str) } __setup("sev=", init_sev_config); -int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio) +int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, + struct snp_guest_request_ioctl *rio) { struct ghcb_state state; struct es_em_ctxt ctxt; unsigned long flags; struct ghcb *ghcb; + u64 exit_code; int ret; rio->exitinfo2 = SEV_RET_NO_FW_CALL; + if (!req) + return -EINVAL; + + exit_code = req->exit_code; /* * __sev_get_ghcb() needs to run with IRQs disabled because it is using @@ -2202,8 +2209,8 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn vc_ghcb_invalidate(ghcb); if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { - ghcb_set_rax(ghcb, input->data_gpa); - ghcb_set_rbx(ghcb, input->data_npages); + ghcb_set_rax(ghcb, __pa(req->data)); + ghcb_set_rbx(ghcb, *req->data_npages); } ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa); @@ -2222,7 +2229,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN): /* Number of expected pages are returned in RBX */ if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { - input->data_npages = ghcb_get_rbx(ghcb); + *req->data_npages = ghcb_get_rbx(ghcb); ret = -ENOSPC; break; } diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 538c42e64baa..d4241048b397 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -23,8 +23,7 @@ #include #include - -#include "sev-guest.h" +#include #define DEVICE_NAME "sev-guest" @@ -198,7 +197,7 @@ static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, return -EBADMSG; } -static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz) +static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req) { struct snp_guest_msg *resp = &snp_dev->secret_response; struct snp_guest_msg *req = &snp_dev->secret_request; @@ -226,29 +225,28 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, * If the message size is greater than our buffer length then return * an error. */ - if (unlikely((resp_hdr->msg_sz + ctx->authsize) > sz)) + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) return -EBADMSG; /* Decrypt the payload */ - return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); + return dec_payload(ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); } -static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 type, - void *payload, size_t sz) +static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, struct snp_guest_req *req) { - struct snp_guest_msg *req = &snp_dev->secret_request; - struct snp_guest_msg_hdr *hdr = &req->hdr; + struct snp_guest_msg *msg = &snp_dev->secret_request; + struct snp_guest_msg_hdr *hdr = &msg->hdr; - memset(req, 0, sizeof(*req)); + memset(msg, 0, sizeof(*msg)); hdr->algo = SNP_AEAD_AES_256_GCM; hdr->hdr_version = MSG_HDR_VER; hdr->hdr_sz = sizeof(*hdr); - hdr->msg_type = type; - hdr->msg_version = version; + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; hdr->msg_seqno = seqno; - hdr->msg_vmpck = vmpck_id; - hdr->msg_sz = sz; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; /* Verify the sequence number is non-zero */ if (!hdr->msg_seqno) @@ -257,10 +255,10 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 pr_debug("request [seqno %lld type %d version %d sz %d]\n", hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); - return __enc_payload(snp_dev->ctx, req, payload, sz); + return __enc_payload(snp_dev->ctx, msg, req->req_buf, req->req_sz); } -static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, +static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio) { unsigned long req_start = jiffies; @@ -275,7 +273,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, * sequence number must be incremented or the VMPCK must be deleted to * prevent reuse of the IV. */ - rc = snp_issue_guest_request(exit_code, &snp_dev->input, rio); + rc = snp_issue_guest_request(req, &snp_dev->input, rio); switch (rc) { case -ENOSPC: /* @@ -285,8 +283,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, * order to increment the sequence number and thus avoid * IV reuse. */ - override_npages = snp_dev->input.data_npages; - exit_code = SVM_VMGEXIT_GUEST_REQUEST; + override_npages = *req->data_npages; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; /* * Override the error to inform callers the given extended @@ -341,15 +339,13 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, } if (override_npages) - snp_dev->input.data_npages = override_npages; + *req->data_npages = override_npages; return rc; } -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, - struct snp_guest_request_ioctl *rio, u8 type, - void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz) +static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) { u64 seqno; int rc; @@ -363,7 +359,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload in snp_dev->secret_request. */ - rc = enc_payload(snp_dev, seqno, rio->msg_version, type, req_buf, req_sz); + rc = enc_payload(snp_dev, seqno, req); if (rc) return rc; @@ -374,7 +370,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, memcpy(snp_dev->request, &snp_dev->secret_request, sizeof(snp_dev->secret_request)); - rc = __handle_guest_request(snp_dev, exit_code, rio); + rc = __handle_guest_request(snp_dev, req, rio); if (rc) { if (rc == -EIO && rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) @@ -383,12 +379,11 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", rc, rio->exitinfo2); - snp_disable_vmpck(snp_dev); return rc; } - rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz); + rc = verify_and_dec_payload(snp_dev, req); if (rc) { dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); snp_disable_vmpck(snp_dev); @@ -398,6 +393,46 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, return 0; } +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, + struct snp_guest_request_ioctl *rio, u8 type, + void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz) +{ + struct snp_guest_req guest_req = { + .msg_version = rio->msg_version, + .msg_type = type, + .vmpck_id = vmpck_id, + .req_buf = req_buf, + .req_sz = req_sz, + .resp_buf = resp_buf, + .resp_sz = resp_sz, + .exit_code = exit_code, + }; + + return snp_send_guest_request(snp_dev, &guest_req, rio); +} + +static int handle_guest_request_ext(struct snp_guest_dev *snp_dev, u64 exit_code, + struct snp_guest_request_ioctl *rio, u8 type, + void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz, void *certs_data, size_t *npages) +{ + struct snp_guest_req guest_req = { + .msg_version = rio->msg_version, + .msg_type = type, + .vmpck_id = vmpck_id, + .req_buf = req_buf, + .req_sz = req_sz, + .resp_buf = resp_buf, + .resp_sz = resp_sz, + .exit_code = exit_code, + .data = certs_data, + .data_npages = npages, + }; + + return snp_send_guest_request(snp_dev, &guest_req, rio); +} + static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { struct snp_report_resp *resp; @@ -480,7 +515,8 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques { struct snp_ext_report_req req; struct snp_report_resp *resp; - int ret, npages = 0, resp_len; + int ret, resp_len; + size_t npages = 0; lockdep_assert_held(&snp_dev->cmd_mutex); @@ -520,14 +556,14 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques if (!resp) return -ENOMEM; - snp_dev->input.data_npages = npages; - ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg, - SNP_MSG_REPORT_REQ, &req.data, - sizeof(req.data), resp->data, resp_len); + ret = handle_guest_request_ext(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, + arg, SNP_MSG_REPORT_REQ, &req.data, + sizeof(req.data), resp->data, resp_len, + snp_dev->certs_data, &npages); /* If certs length is invalid then copy the returned length */ if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) { - req.certs_len = snp_dev->input.data_npages << PAGE_SHIFT; + req.certs_len = npages << PAGE_SHIFT; if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) ret = -EFAULT; @@ -536,7 +572,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques if (ret) goto e_free; - if (npages && + if (npages && req.certs_len && copy_to_user((void __user *)req.certs_address, snp_dev->certs_data, req.certs_len)) { ret = -EFAULT; @@ -740,7 +776,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) /* initial the input address for guest request */ snp_dev->input.req_gpa = __pa(snp_dev->request); snp_dev->input.resp_gpa = __pa(snp_dev->response); - snp_dev->input.data_gpa = __pa(snp_dev->certs_data); ret = misc_register(misc); if (ret) From patchwork Sat Jul 22 11:19:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124270 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp747906vqg; Sat, 22 Jul 2023 04:23:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlFdJHmQQZ6Xww5ABhWGFWrO+X5wWi+8kSRSoFRdFUP55zDwBZj12O54wD99XB8bgrPzzu8o X-Received: by 2002:a17:906:3059:b0:992:58fc:bcb8 with SMTP id d25-20020a170906305900b0099258fcbcb8mr3896366ejd.56.1690024985788; Sat, 22 Jul 2023 04:23:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690024985; cv=pass; d=google.com; s=arc-20160816; b=Qsi4m3nxrdMHnapdWYpbs8OYb5Swgq9NvUXM5JuulLivwz/5t8vxwpkvU/zwCCRoKz SNqH9v46O1d1i4bFAvVKc/g70TGVfF1fkjQG16J3+/K0CRZYe01j23+fyyiPUul+eugx RrwIcepaYIfnebNTpPE/mbQQb2C+8bHP/Hble8qahT3bYThZEmSvVZXLlXkYnDUqBje9 JFmnmndO8O/tfJ7W7JP5As1DKmTKH2BPXryMQL8sjB02vdR4sEMhU8FHrRL741umNA8I fkf8TW008IHIKk6D+0ysfUlZM5B71k3QI/ZxbfsgUnweM9k2AYmzcJ44qRLyrxPdtboU ySHg== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=fELucEziKjmc65JVfEfBO+V5Qa0SARR8xrFj5Kr0h/U=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=G4QJI3DFw/VPmxFVkCK+s0IlnJcG+K5ub0MMzJOg8I8mdQI9t09bRyyIQfl9dnMNNr IZFL5rgB4FVsUekXVkFuQ6jK9VJg2eVcTD58bstCOhon2NiFqahmNgP/wXZGIK6Es4p9 8jWxHAo4Se4dcuNF0TwwOqG/iBAYYMy7RMAWZ2xsMnndJRXtfeStpcL3ZSnfNJaJ0Gj2 qynCh+8A0d2vHDi8LFi5CE1TwWWOoTMcvg2Jj3+vavU7UmUcBHr73zdo5/6/db7S2vrh HL1gOeCdtE5v6FIigNvSp5rloJE7Ifjtvwo0q064kwByl/0sUOukqZtVB8MDSJqaSVxs 5JUw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=1InIZ7OE; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n15-20020a170906688f00b00986749b140dsi3609875ejr.894.2023.07.22.04.22.41; Sat, 22 Jul 2023 04:23:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=1InIZ7OE; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229792AbjGVLUp (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229785AbjGVLUl (ORCPT ); Sat, 22 Jul 2023 07:20:41 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2089.outbound.protection.outlook.com [40.107.93.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 907253A80 for ; Sat, 22 Jul 2023 04:20:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GhSHWZnkR54nZ5PAHLGT6EN8xZrx0c3gQXC+hqdwDFeLKCi3EONHa23/Ni8BRcbzdmr0E6fN4oWLfIqQXIrH9AKIWSAL/7130Wizk78kBD+AHVcS+L8ympZlbsvqAKcBN2QPSRVcNLj6bwmraCN+BIYrBhpz9g3bosu8GfGpTGgclMbvhzzjHsT4i7d04c+U3NsiOk7bFtvSgBS2ldbGN244dur4G/YqMSg4uZsR7D66deb92ZyrscZ8SzmKyBzHeZTAH+uUtEJzXFWVoRQijZwmAzk0CNGlLYWVFN4d080/+Aeagz+qul9HdqQNUGo8eZ5Y4ivu+i4wFzPt4MJe3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fELucEziKjmc65JVfEfBO+V5Qa0SARR8xrFj5Kr0h/U=; b=GpCRb7HVePpHCq869qbTTMmcSP4ctN4d4qSIeRfKd1PZxTBr+gsYXU/Kf/glo3/g6Xu/uCiVoyuCH6LBfOHcgmUM+jUDztDfORcsmCeu5DwV1S99CvwvZQdYpgEHFNfXZOI+mOonWlg5FimXJMFw64GG5OuHmNYJDhO5OVX74I3XcQONYlbov5ksvpNblKLYPvFmo+kH/2t3h5LIbxaDEaR5hElOm4+h5To002/9gWLL/UVWfaEG8ND0e+XHVFhOEFDTmv8wsxlPxWnpZOrdoUQ+y28C5m0c8cjNDLJfh7CAFJ18W+GfUxJyNTygMvgsU7cXgPsxGamQshBjjpaP3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fELucEziKjmc65JVfEfBO+V5Qa0SARR8xrFj5Kr0h/U=; b=1InIZ7OETO8jZV9dGtF5Y0ui/T1iQOs4A3h4URyFwk4roR5z81FEcxb54VV0P+c4dLPZE+/rlAlslXWv1Q8+g5g5cUdk0QUNo2NUtn0gUGxNR/5+RPsASUTB3m8tAcif4B1QJtnPab2Lh/ZQdkEjWYMlMihRif03qPbAy+2ofw8= Received: from DM6PR07CA0092.namprd07.prod.outlook.com (2603:10b6:5:337::25) by DM6PR12MB4893.namprd12.prod.outlook.com (2603:10b6:5:1bd::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.29; Sat, 22 Jul 2023 11:20:32 +0000 Received: from DM6NAM11FT083.eop-nam11.prod.protection.outlook.com (2603:10b6:5:337::4) by DM6PR07CA0092.outlook.office365.com (2603:10b6:5:337::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30 via Frontend Transport; Sat, 22 Jul 2023 11:20:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT083.mail.protection.outlook.com (10.13.173.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:31 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:26 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct Date: Sat, 22 Jul 2023 16:49:00 +0530 Message-ID: <20230722111909.15166-6-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT083:EE_|DM6PR12MB4893:EE_ X-MS-Office365-Filtering-Correlation-Id: 18361cc6-99d2-4dda-117c-08db8aa5a973 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0qRfJUL/IthYdH+pCwKU812SjAjEMEMWiGWwZu8cKDA07goDWOflsh1k3BJKdJFavLdSEIOH9p8m3fITyXDSiNicPSCVdTH3mOAievVcs+bRr436NAmBm1fuzNKsrWaog3DUPLtpKOChxgGbCIN35Kcyp+J5YOcvaBNeXmn94FM0zMZtjCfnLctykt1hs3bAL4oRLQrUWr6e5G6cuOmFSImKyB6nlJT4uLXbY9WJvYQZPdG1E+Qq2Oo9v7uswHX7/REcAkuNzx0VexN1ZMx/PEs96ykWKi3BWyLsBcNWX68GPwkjGLFCrkghxFLKfU/sYlY2ipMxL1LPxuJoOjgv4vnqEUbV9boJfKz0D/2RrwvFS3FO3h46ZuhvIPKNl1nGdNiA5a4aHsSJp2AmsFtS/fRCE5/YeRNYA11ijfFTAJapMSipIAqCKB3ma6cxzQMKApPDqf11sNsu4Q3l9WBngwh0A2c9VAqZAk7i19ebKvWD4QAbG4mga4rotJqwIKKFTWGO+aUe5+sh89eaEQQBp0jYflFH9rcRwk/Pmg/xLdUaTlmHiUwzT3K7i1Ob175t02/mZuCOuT/U92uEtnEgDOnYYScGEAEFKcZAQePdxTWfpcQKsYMF/5NQdDpRNoUro14eAud86h6XiaUw3WQNZTbYXTtddMLkQt04MjFuS+XAZAgqLWnppR2SIqNEc+umpeKX2+UW7i8UpOFkMjQO58WWZaJbk39NUJVRSg6GWxnNcLs4pGlc50WeBXzG/1LRzNFhWNcxpmA40JSlo58/Ew== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(82310400008)(40470700004)(46966006)(36840700001)(2906002)(40480700001)(81166007)(356005)(82740400003)(83380400001)(426003)(47076005)(16526019)(186003)(26005)(336012)(2616005)(36860700001)(1076003)(40460700003)(5660300002)(8936002)(478600001)(8676002)(36756003)(6666004)(7696005)(4326008)(70206006)(70586007)(41300700001)(316002)(110136005)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:31.9403 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 18361cc6-99d2-4dda-117c-08db8aa5a973 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT083.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4893 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119639690977894 X-GMAIL-MSGID: 1772119639690977894 Drop vmpck and os_area_msg_seqno pointers so that secret page layout does not need to be exposed to the sev-guest driver after the rework. Instead, add helper APIs to access vmpck and os_area_msg_seqno when needed. Signed-off-by: Nikunj A Dadhania --- drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index d4241048b397..8ad43e007d3b 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -50,8 +50,7 @@ struct snp_guest_dev { struct snp_secrets_page_layout *layout; struct snp_req_data input; - u32 *os_area_msg_seqno; - u8 *vmpck; + unsigned int vmpck_id; }; static u32 vmpck_id; @@ -67,12 +66,23 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) return 0; } -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) +{ + return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; +} + +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) +{ + return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; +} + +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) { char zero_key[VMPCK_KEY_LEN] = {0}; + u8 *key = snp_get_vmpck(snp_dev); - if (snp_dev->vmpck) - return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN); + if (key) + return !memcmp(key, zero_key, VMPCK_KEY_LEN); return true; } @@ -96,20 +106,22 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) */ static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) { + u8 *key = snp_get_vmpck(snp_dev); + dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - vmpck_id); - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); - snp_dev->vmpck = NULL; + snp_dev->vmpck_id); + memzero_explicit(key, VMPCK_KEY_LEN); } static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) { + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); u64 count; lockdep_assert_held(&snp_dev->cmd_mutex); /* Read the current message sequence counter from secrets pages */ - count = *snp_dev->os_area_msg_seqno; + count = *os_area_msg_seqno; return count + 1; } @@ -137,11 +149,13 @@ static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) { + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); + /* * The counter is also incremented by the PSP, so increment it by 2 * and save in secrets page. */ - *snp_dev->os_area_msg_seqno += 2; + *os_area_msg_seqno += 2; } static inline struct snp_guest_dev *to_snp_dev(struct file *file) @@ -151,15 +165,22 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) +static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) { struct aesgcm_ctx *ctx; + u8 *key; + + if (snp_is_vmpck_empty(snp_dev)) { + pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); + return NULL; + } ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); if (!ctx) return NULL; - if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { + key = snp_get_vmpck(snp_dev); + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { pr_err("SNP: crypto init failed\n"); kfree(ctx); return NULL; @@ -606,7 +627,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long mutex_lock(&snp_dev->cmd_mutex); /* Check if the VMPCK is not empty */ - if (is_vmpck_empty(snp_dev)) { + if (snp_is_vmpck_empty(snp_dev)) { dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); mutex_unlock(&snp_dev->cmd_mutex); return -ENOTTY; @@ -676,32 +697,14 @@ static const struct file_operations snp_guest_fops = { .unlocked_ioctl = snp_guest_ioctl, }; -static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno) +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) { - u8 *key = NULL; + if (WARN_ON(vmpck_id > 3)) + return false; - switch (id) { - case 0: - *seqno = &layout->os_area.msg_seqno_0; - key = layout->vmpck0; - break; - case 1: - *seqno = &layout->os_area.msg_seqno_1; - key = layout->vmpck1; - break; - case 2: - *seqno = &layout->os_area.msg_seqno_2; - key = layout->vmpck2; - break; - case 3: - *seqno = &layout->os_area.msg_seqno_3; - key = layout->vmpck3; - break; - default: - break; - } + dev->vmpck_id = vmpck_id; - return key; + return true; } static int __init sev_guest_probe(struct platform_device *pdev) @@ -733,14 +736,14 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_unmap; ret = -EINVAL; - snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno); - if (!snp_dev->vmpck) { + snp_dev->layout = layout; + if (!snp_assign_vmpck(snp_dev, vmpck_id)) { dev_err(dev, "invalid vmpck id %d\n", vmpck_id); goto e_unmap; } /* Verify that VMPCK is not zero. */ - if (is_vmpck_empty(snp_dev)) { + if (snp_is_vmpck_empty(snp_dev)) { dev_err(dev, "vmpck id %d is null\n", vmpck_id); goto e_unmap; } @@ -748,7 +751,6 @@ static int __init sev_guest_probe(struct platform_device *pdev) mutex_init(&snp_dev->cmd_mutex); platform_set_drvdata(pdev, snp_dev); snp_dev->dev = dev; - snp_dev->layout = layout; /* Allocate the shared page used for the request and response message. */ snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); @@ -764,7 +766,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) goto e_free_response; ret = -EIO; - snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN); + snp_dev->ctx = snp_init_crypto(snp_dev); if (!snp_dev->ctx) goto e_free_cert_data; From patchwork Sat Jul 22 11:19:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124271 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp747955vqg; Sat, 22 Jul 2023 04:23:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlH4bZ0wymRoqr/wJyxo02krGF80tosXCd9Lv7KzA+fo04NyTvKk+pmgyjsdbWuwbCTLwQX1 X-Received: by 2002:a17:906:cc49:b0:982:8c28:c50d with SMTP id mm9-20020a170906cc4900b009828c28c50dmr4372780ejb.58.1690024992722; Sat, 22 Jul 2023 04:23:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690024992; cv=pass; d=google.com; s=arc-20160816; b=tcSlPTBmbZVGcjp8mNuH7/8fZWhUC0xFe2OgPhm8nggkaCRKsrfzkLy3jbWSPU9RAf nFHjRgvIazWlnFkrvC2f3z7kEVaZva69NxaEal1oMOAVcLtx0/Jm0YjY2gtJ4ELy4iau ngSJDWaLWtWB2IAYo5oiA97KgRuw/DatrLjVE9zMGgnrWvbWqyt5EV04O2ZbVaoZogYy Qs8AA3PmlvMkZ95OqNUo3zNdwlewB8FodXFqrKEsZq75x9K9An/RWhOB0QtTN0NwEen5 XKrq4/22ZLC3Vt64/KbhMo22CBtO7TAz9OUQlN2GXDoJu57619kU6GSU2jYOS0Lvz/qC 61nw== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=3tgvaZKGyl4nDbChRpthhng+3C8nnip3DQgCAw7+Dgg=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=XQ1YGxsJKe9KZk8kvyRyE/Y2QY4ei0G+YetW/8RgCalGANlujMJuZ4GSCY5uB34Zks X4t7i6Dr0Hdwiygu3jTfQK3BioaiLosLNu6MtbUPMDJEbnk3nTjwlzWLeDsLZmSq42oB RmFek8McZRe8QlrU/UIgc5zu+x5X5I36WeGXPAfWkiDHaJyA9SDP7/7JQbXQ7ewgawPU NqVPwkr/5CNaCuR0qA6qBVnnzsK8fpo3MXXtmR+Owe5T8p9rslJLGF2D0cqewQ59P+ne O1Bu/7r5r0pjwnQiGEJNilEGkezVy8ShiKNrgqJfkx2nZUZgBKAfddBdhOz+jD4TJ/l8 pSVg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZWm57U2y; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y11-20020a170906524b00b009784f00c5besi3302175ejm.263.2023.07.22.04.22.47; Sat, 22 Jul 2023 04:23:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZWm57U2y; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229640AbjGVLUv (ORCPT + 99 others); Sat, 22 Jul 2023 07:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbjGVLUp (ORCPT ); Sat, 22 Jul 2023 07:20:45 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2073.outbound.protection.outlook.com [40.107.93.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C9333A9C for ; Sat, 22 Jul 2023 04:20:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P2on5LEmIACJfbuSi9dIyFyGw1Ghj3vyX2UqsdGZrSrMXoHBcojCfDHj2bb53nBNTPMlOI4+oADUMeMn7n9PrfwsW95bFTF2csdQU5BLLbX7pd4aBXbsEvpOg1ckNq2o8coWMensqjGUqiSGlqxkuepMChHCNxyzvvUOeWgPPrGwZWwFGEkfp1nV4sb8a4X8ZhhO6J21otBjeIQNTsdBqoI9q5uQjhg7v3V7s9KsPUJm635JlkcBmExLigkPS7soTuiw3xj2/sWdfLqvhcI5bG2LHXPqTDCedK3y6MCqzBfYUIXCddg9hNP9OIFAXnk/X90hQ3If2PQ4BBKLGqPJOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3tgvaZKGyl4nDbChRpthhng+3C8nnip3DQgCAw7+Dgg=; b=HcDkwzGXfC0veY1ubxFEm9Cvlx0NOuACjEaklKzZVxj8btomfzn5MS2mmcCK40vQOJn4q76Yw4XBr/wAg15Y10kmyWLvMcbVujT2lfMWtxEj6N0al6bt6W3/OKSBDNNarshfVLeAQtNpfUIrK5vEdGAmC75SksGLKukdToK2Ab9ZevlfHATR//cJk8oTY8INVdTLorIuz/MXSeJq71guYidmZO/gpx/UNcf+Cgh/2wdISY7KltRRi2cDr2ng3grnZTl+IcV79FFms6nARthjRch5JLiOuBU7rR8CaMRcYwpzmXNadQs+DUWqUkHttcGweApcnwd6OrkV6PBYxq265g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3tgvaZKGyl4nDbChRpthhng+3C8nnip3DQgCAw7+Dgg=; b=ZWm57U2yJwYOT2hZAG+QFOs6VoUHTkRr3QPZ95S14MUQ/apwmH4FNWGchKOR0GqPGGfqbINL/w/aqrqi9OsP2BNMwy6oATRn/ry7vTcZleQUfAX3wEXeIlmCXbLpsGUGwTYnRn4WsppTlOKJOeVUG2QLLOz00A4N6NsXWjbWxtE= Received: from DM6PR06CA0051.namprd06.prod.outlook.com (2603:10b6:5:54::28) by BL0PR12MB4961.namprd12.prod.outlook.com (2603:10b6:208:1c9::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.29; Sat, 22 Jul 2023 11:20:35 +0000 Received: from DM6NAM11FT102.eop-nam11.prod.protection.outlook.com (2603:10b6:5:54:cafe::e1) by DM6PR06CA0051.outlook.office365.com (2603:10b6:5:54::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.29 via Frontend Transport; Sat, 22 Jul 2023 11:20:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT102.mail.protection.outlook.com (10.13.173.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:35 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:31 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 06/14] x86/sev: Cache the secrets page address Date: Sat, 22 Jul 2023 16:49:01 +0530 Message-ID: <20230722111909.15166-7-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT102:EE_|BL0PR12MB4961:EE_ X-MS-Office365-Filtering-Correlation-Id: 9023d043-a33f-44eb-9451-08db8aa5ab63 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tx9dKhk7VWMlHowmUBMRCO+UzbJAFWLfl4H7oWAIDLxgf3657Z/r+Rv0IKqAOcf2hlGkol/CChm2soMMZZOqwfQgoLinXgMwy5n+9wbeE/R+EIP7oq2XW3FN3lwto41NTIy3hQDI70eerk3Y6uZZfzBOhxe/1f/ZSTDQXBSk+pRaMu/QgIUyv+4hQrn+jxYisnn/kk/gqO/VlkRK9EN/l3THedTW8z5YdMpvvvIQVzGLDMeAM52AvkevM7X9yCfgLH9VMV+JCRNtPAlrjTesSnh6mSeDzuKFTPPX7ga0ap136TkSwPGeeS8oZyPqvl4UWasB2KgXlOpqBFqP5WO91XKzjZ5WcVvGcLMISKWzkr6EE6PK8TpDi2Fm3D9vjA1Im2KtVOS0uhvglfN7FbNCorNeRUXNbP/7xOr4aOoe+Q8ibqg+Cy0aNuL9x6D7hAlkHGlUsNsIdOJMH7R/VjLfgfEWkoTAQmq/zlG6hI0L4t3e3kWoCPwFHnHJeCzEDV1GYabOpgeKT7Ir7QBfiMxuBqUUXytwwSTUmDFA8aiL3uL/H92ac2Y65OuXqc1v7D/fwrwauhTxFYsyemXnx18IV3WgtqjQXb0256AALjKKgkGLQueF6UdcDAFWwHT+11y3f2uLS6mmCX3JpzEn3FOao+JigK9s4IYnYG9duwehrF3N/mMsIn0KA1S0mWyG87PN7u7FebtlheNFh+OqhV08DAVQHhM2iZuwyZF++mMQf4RK2td36MD4/Hd7JL68iJpFxczebKT4X12ml7+b37i1vA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(396003)(376002)(39850400004)(136003)(451199021)(82310400008)(40470700004)(36840700001)(46966006)(40460700003)(81166007)(40480700001)(356005)(36860700001)(36756003)(426003)(2616005)(47076005)(83380400001)(16526019)(8676002)(5660300002)(8936002)(110136005)(478600001)(54906003)(316002)(4326008)(70206006)(70586007)(41300700001)(26005)(186003)(336012)(1076003)(7696005)(6666004)(2906002)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:35.1630 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9023d043-a33f-44eb-9451-08db8aa5ab63 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT102.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4961 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119646488395948 X-GMAIL-MSGID: 1772119646488395948 Save the secrets page address during snp_init() from the CC blob. Use secrets_pa instead of calling get_secrets_page() that remaps the CC blob for getting the secrets page every time. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- arch/x86/kernel/sev.c | 52 +++++++++++++------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a10cf9f21e22..a63a3edf13d0 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -72,6 +72,9 @@ static struct ghcb *boot_ghcb __section(".data"); /* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features __ro_after_init; +/* Secrets page physical address from the CC blob */ +static u64 secrets_pa __ro_after_init; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -561,45 +564,16 @@ void noinstr __sev_es_nmi_complete(void) __sev_put_ghcb(&state); } -static u64 __init get_secrets_page(void) -{ - u64 pa_data = boot_params.cc_blob_address; - struct cc_blob_sev_info info; - void *map; - - /* - * The CC blob contains the address of the secrets page, check if the - * blob is present. - */ - if (!pa_data) - return 0; - - map = early_memremap(pa_data, sizeof(info)); - if (!map) { - pr_err("Unable to locate SNP secrets page: failed to map the Confidential Computing blob.\n"); - return 0; - } - memcpy(&info, map, sizeof(info)); - early_memunmap(map, sizeof(info)); - - /* smoke-test the secrets page passed */ - if (!info.secrets_phys || info.secrets_len != PAGE_SIZE) - return 0; - - return info.secrets_phys; -} - static u64 __init get_snp_jump_table_addr(void) { struct snp_secrets_page_layout *layout; void __iomem *mem; - u64 pa, addr; + u64 addr; - pa = get_secrets_page(); - if (!pa) + if (!secrets_pa) return 0; - mem = ioremap_encrypted(pa, PAGE_SIZE); + mem = ioremap_encrypted(secrets_pa, PAGE_SIZE); if (!mem) { pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n"); return 0; @@ -2093,6 +2067,12 @@ static __init struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp) return cc_info; } +static void __init set_secrets_pa(const struct cc_blob_sev_info *cc_info) +{ + if (cc_info && cc_info->secrets_phys && cc_info->secrets_len == PAGE_SIZE) + secrets_pa = cc_info->secrets_phys; +} + bool __init snp_init(struct boot_params *bp) { struct cc_blob_sev_info *cc_info; @@ -2104,6 +2084,8 @@ bool __init snp_init(struct boot_params *bp) if (!cc_info) return false; + set_secrets_pa(cc_info); + setup_cpuid_table(cc_info); /* @@ -2256,16 +2238,14 @@ static struct platform_device sev_guest_device = { static int __init snp_init_platform_device(void) { struct sev_guest_platform_data data; - u64 gpa; if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - gpa = get_secrets_page(); - if (!gpa) + if (!secrets_pa) return -ENODEV; - data.secrets_gpa = gpa; + data.secrets_gpa = secrets_pa; if (platform_device_add_data(&sev_guest_device, &data, sizeof(data))) return -ENODEV; From patchwork Sat Jul 22 11:19:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp780218vqg; Sat, 22 Jul 2023 05:38:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlGSqIh53OUT2W1s+Otky/qobOO17XvhIsRqUdmAL9iJQ/71rFmoMnidttQo/yXHOFHEXFq8 X-Received: by 2002:a05:6a20:3947:b0:12e:6e0f:535 with SMTP id r7-20020a056a20394700b0012e6e0f0535mr4856266pzg.34.1690029515703; Sat, 22 Jul 2023 05:38:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690029515; cv=pass; d=google.com; s=arc-20160816; b=YckkCsY1765SG8SIyZoP1cdkU8EvkKIbT3vIRH9YNI5fQzIgmVNYp1gzUXSwS0QVGp OziPrfb0Paq2iLzLV5P9DEc9kOYKoxaTulff+fRIKl4k5pRxpjeaoaRNy2L1k09XnwD3 GvSwISuORLY4gHJs5OIHftrHreZV31ms6bo37rQOBJKQjFJzV0Yg02udhtKPK6Vxkdya snANZtYaf1cndilCBJiUmGlCugA/GcUiOMKGFCjVYKKVJB1J4F5QawcnwNwy4pVwQNXs hNJJLPlXkFs954QroMBD/b5KnY+rRpTTFNSaVM9mT8L2fFOA5e2LqZGH03VD3s5mvVTJ ndxw== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=R87G9pIWOIMaBx+OZBVq4xLweRbra1tnNaDaoZIdA/0=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=qEqdbMegUZ0zhuochIOl6XpenU59ZZPCfBziajlCS+orePJdoy1mHoUGK0jxjDtdeE h5JVv7iU25kupQ9kEv7TnEqKDYVd63AIRB/kG5aFzPaG32rUWBo+83++NJ5Xeuhqeg01 DjeBhF7UDWH2K4Uyr1DPQ0QCb1cvPgsCXzjRhMvoNTElT2LXgA8HnA7/KdngI83Vl9l7 z++KHqv4sXc5vRCFM/1eStPLoFNoB0M0Owuic27XIbjUR118UksKMA8IxZHQ86gYRzuI xuFz4CvK2mV3ZusbsWysVaHssRpssd9NmXRy3+E9/eGlyQ5Q7QZS1A+Syu34Bd/o3caM 1lQg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZMzZ4oTc; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s21-20020a63af55000000b005639fa45a28si198832pgo.630.2023.07.22.05.38.22; Sat, 22 Jul 2023 05:38:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZMzZ4oTc; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229692AbjGVLVV (ORCPT + 99 others); Sat, 22 Jul 2023 07:21:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbjGVLVT (ORCPT ); Sat, 22 Jul 2023 07:21:19 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2085.outbound.protection.outlook.com [40.107.237.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA5CE3A84 for ; Sat, 22 Jul 2023 04:20:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GHeF9rOArXJvmknL71/a7NY3Vwg9u211CsI1QuWwuGPZf0KQW/iwr3Bi5EV7BBDPwK0t5YSV39s3AddbazbuqRt+zLE/jRH6Q8v5Hky6Qge4FGat/5qACTfV2OhAnKoklkVBG9S2bZZZkM2RJ3Z877w6O0HLKdgBYOVO+WCzDqjivzJGXAOQh7ds+GFvDOYp5ryA4CPN2Gc+vyFfoHAgIsvEjJzuTDlJwlb6z84LmsgU2E1RKVbtO11Rmi6BasA0VFJYkckBWbEqfBjA/pEkjWYbmFXCiK7iJg4sK7NxuYIYBnWtiFY6JTHf2pAeEeLABKmgISX8MnV/l/yk9lGKew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=R87G9pIWOIMaBx+OZBVq4xLweRbra1tnNaDaoZIdA/0=; b=aL6jYKdAclvGIoapUI9Hrsc6lxWae4BIdjQqbCZxyM3HMjl6z5+R/YHDeJBVoj+RTdSnwQ3JcysbWIoKkJ9fOFv6msDcJyhVhvhOUAiHDRTmvAuzxey+iIW5kM1AIyVC8Y1z4IfTzmfkML+l/uyVeRr1BPPN1nV/HwOMhbIVu7o5h2TTGsneWx5JFqgzqeNiKwrQ4Iyz6ECgyXpBE3FNxgfI9VlggrulcOdOFkb1LOYBrQ66dUMwKRYQYHYMEb+kSC0rNPLuIrdTXoriZ48N2o5jRHJXlGp5wYwXExMtcsABwDNAMzBS86Uz44Ej0LedFsthA/ZAe7jDn5BI8D0auw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=R87G9pIWOIMaBx+OZBVq4xLweRbra1tnNaDaoZIdA/0=; b=ZMzZ4oTciNcTOhi6E1cR0ZDV7pZLN2pY8mgygnOYkv3rvpJ+eVLT7l9JyK3l98qXTm60XFhitkU5Bzag2tgRmWM4MkEKL+Vg6KTZrDGQWdSkhmQ7z6RfQzT5tsIhwqcnEwDizZybRS9LIjwLj0csfaJBK55DpB92GZAJY/PFlOs= Received: from DS7PR03CA0349.namprd03.prod.outlook.com (2603:10b6:8:55::24) by CYYPR12MB8964.namprd12.prod.outlook.com (2603:10b6:930:bc::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.25; Sat, 22 Jul 2023 11:20:39 +0000 Received: from DM6NAM11FT093.eop-nam11.prod.protection.outlook.com (2603:10b6:8:55:cafe::4f) by DS7PR03CA0349.outlook.office365.com (2603:10b6:8:55::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT093.mail.protection.outlook.com (10.13.172.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:38 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:34 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 07/14] x86/sev: Move and reorganize sev guest request api Date: Sat, 22 Jul 2023 16:49:02 +0530 Message-ID: <20230722111909.15166-8-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT093:EE_|CYYPR12MB8964:EE_ X-MS-Office365-Filtering-Correlation-Id: 3a6392f1-faad-470e-dc34-08db8aa5ada2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xza6c5/qnqWrfoHmJXl5gYVwLCQom27Qd8dWUOvDC5t+KhsiJAIvlfci+ndrXtX8NRbYmNmJuPdYfgeljGQ4uZjzz5Ikdi+Zs94vamq0WJM5pjOcQh06qfrxi84RRQoQu3IlkrtG+Yd4StyFUlqmMuPy4BiqCnk39giyvypzRVTNPH6ijG9UxdScivmcWReTwk+1lpmPBtS9nuesFSKu1VP4coYrF6lABeT0syqzu3U5zl2LFu/TmQHmWfBeTJegJA00z5Mv6gNNmulYK+ycHSmZSmAcJkQMVWhtgBPsQMwhnoHaA9eA5uke+/IQA4Ze4re2b2IfcaIGSkWvVc0KPkrJ0mBfdo/eoQtLVNisyOYwxrNOLn5SJ9XjnZmpu7AP0MU4UvfkU7danI/0BGxTMesQ5Uui1zgaxPcKsK7rfN7CbRkzuOJk1ZG2KsDp0nVkGNVAzkXDYDcbTVIdTuiAbNCVdL+zxNIRDQRBM2Ow++1zgaDQi/5OqVdgvgGWKuLueH53NcLCiw6ESX+bA8h3JfyYVZuP9qv5Kz7sEA+P8j7nvbg7lUGwBzylVS/nJZthLgEIhnuAP2EkgdPMrSPjy6DvTEi5WNlVXdUoNLFV3k9db+APA5v360/+dpxN4QWBjIkKAxRhiKZ5psgJGbkdJL+OEzVPXDCK5Iij26LfIWzjAkMGPg3E/xkumcCIdRB3mz09gBO0BruF6yVOFUehHKlHu7GYMpD8kPZjMOgmXkc4BVqsfA+1O0vDWUcFROi6E+eiGN+0uu3Z9eGAAyazAA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(39860400002)(136003)(396003)(346002)(451199021)(82310400008)(46966006)(36840700001)(40470700004)(81166007)(40460700003)(16526019)(186003)(2616005)(26005)(1076003)(47076005)(426003)(83380400001)(336012)(70206006)(30864003)(41300700001)(316002)(70586007)(4326008)(8676002)(2906002)(5660300002)(8936002)(36860700001)(7696005)(478600001)(110136005)(54906003)(40480700001)(82740400003)(6666004)(356005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:38.9800 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3a6392f1-faad-470e-dc34-08db8aa5ada2 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT093.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8964 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772124389339408469 X-GMAIL-MSGID: 1772124389339408469 For enabling Secure TSC, SEV-SNP guests need to communicate with the AMD Security Processor early during boot. Many of the required functions are implemented in the sev-guest driver and therefore not available at early boot. Move the required functions and provide an API to the driver to assign key and send guest request. Signed-off-by: Nikunj A Dadhania --- arch/x86/Kconfig | 1 + arch/x86/include/asm/sev-guest.h | 84 +++- arch/x86/include/asm/sev.h | 10 - arch/x86/kernel/sev.c | 466 +++++++++++++++++++++- drivers/virt/coco/sev-guest/Kconfig | 1 - drivers/virt/coco/sev-guest/sev-guest.c | 493 +----------------------- 6 files changed, 557 insertions(+), 498 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 53bab123a8ee..7502903cbc6b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1546,6 +1546,7 @@ config AMD_MEM_ENCRYPT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + select CRYPTO_LIB_AESGCM help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory diff --git a/arch/x86/include/asm/sev-guest.h b/arch/x86/include/asm/sev-guest.h index 22ef97b55069..e6f94208173d 100644 --- a/arch/x86/include/asm/sev-guest.h +++ b/arch/x86/include/asm/sev-guest.h @@ -11,6 +11,11 @@ #define __VIRT_SEVGUEST_H__ #include +#include +#include + +#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) +#define SNP_REQ_RETRY_DELAY (2*HZ) #define MAX_AUTHTAG_LEN 32 #define AUTHTAG_LEN 16 @@ -58,11 +63,45 @@ struct snp_guest_msg_hdr { u8 rsvd3[35]; } __packed; +/* SNP Guest message request */ +struct snp_req_data { + unsigned long req_gpa; + unsigned long resp_gpa; +}; + struct snp_guest_msg { struct snp_guest_msg_hdr hdr; u8 payload[4000]; } __packed; +struct sev_guest_platform_data { + /* request and response are in unencrypted memory */ + struct snp_guest_msg *request, *response; + + struct snp_secrets_page_layout *layout; + struct snp_req_data input; +}; + +struct snp_guest_dev { + struct device *dev; + struct miscdevice misc; + + /* Mutex to serialize the shared buffer access and command handling. */ + struct mutex cmd_mutex; + + void *certs_data; + struct aesgcm_ctx *ctx; + + /* + * Avoid information leakage by double-buffering shared messages + * in fields that are in regular encrypted memory + */ + struct snp_guest_msg secret_request, secret_response; + + struct sev_guest_platform_data *pdata; + unsigned int vmpck_id; +}; + struct snp_guest_req { void *req_buf, *resp_buf, *data; size_t req_sz, resp_sz, *data_npages; @@ -72,6 +111,47 @@ struct snp_guest_req { u8 msg_type; }; -int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio); +int snp_setup_psp_messaging(struct snp_guest_dev *snp_dev); +int snp_send_guest_request(struct snp_guest_dev *dev, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio); +bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id); +bool snp_is_vmpck_empty(unsigned int vmpck_id); + +static void free_shared_pages(void *buf, size_t sz) +{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + int ret; + + if (!buf) + return; + + ret = set_memory_encrypted((unsigned long)buf, npages); + if (ret) { + WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n"); + return; + } + + __free_pages(virt_to_page(buf), get_order(sz)); +} + +static void *alloc_shared_pages(size_t sz) +{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + struct page *page; + int ret; + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); + if (!page) + return NULL; + + ret = set_memory_decrypted((unsigned long)page_address(page), npages); + if (ret) { + pr_err("%s: failed to mark page shared, ret=%d\n", __func__, ret); + __free_pages(page, get_order(sz)); + return NULL; + } + + return page_address(page); +} + #endif /* __VIRT_SEVGUEST_H__ */ diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index d8e972aeb22c..807f85f8014c 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -88,16 +88,6 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); #define RMPADJUST_VMSA_PAGE_BIT BIT(16) -/* SNP Guest message request */ -struct snp_req_data { - unsigned long req_gpa; - unsigned long resp_gpa; -}; - -struct sev_guest_platform_data { - u64 secrets_gpa; -}; - /* * The secrets page contains 96-bytes of reserved field that can be used by * the guest OS. The guest OS uses the area to save the message sequence diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a63a3edf13d0..72e76c58aebd 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -959,6 +960,457 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) free_page((unsigned long)vmsa); } +static struct sev_guest_platform_data *platform_data; + +static inline u8 *snp_get_vmpck(unsigned int vmpck_id) +{ + if (!platform_data) + return NULL; + + return platform_data->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; +} + +static inline u32 *snp_get_os_area_msg_seqno(unsigned int vmpck_id) +{ + if (!platform_data) + return NULL; + + return &platform_data->layout->os_area.msg_seqno_0 + vmpck_id; +} + +bool snp_is_vmpck_empty(unsigned int vmpck_id) +{ + char zero_key[VMPCK_KEY_LEN] = {0}; + u8 *key = snp_get_vmpck(vmpck_id); + + if (key) + return !memcmp(key, zero_key, VMPCK_KEY_LEN); + + return true; +} +EXPORT_SYMBOL_GPL(snp_is_vmpck_empty); + +/* + * If an error is received from the host or AMD Secure Processor (ASP) there + * are two options. Either retry the exact same encrypted request or discontinue + * using the VMPCK. + * + * This is because in the current encryption scheme GHCB v2 uses AES-GCM to + * encrypt the requests. The IV for this scheme is the sequence number. GCM + * cannot tolerate IV reuse. + * + * The ASP FW v1.51 only increments the sequence numbers on a successful + * guest<->ASP back and forth and only accepts messages at its exact sequence + * number. + * + * So if the sequence number were to be reused the encryption scheme is + * vulnerable. If the sequence number were incremented for a fresh IV the ASP + * will reject the request. + */ +static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) +{ + u8 *key = snp_get_vmpck(snp_dev->vmpck_id); + + pr_alert("Disabling vmpck_id %d to prevent IV reuse.\n", snp_dev->vmpck_id); + memzero_explicit(key, VMPCK_KEY_LEN); +} + +static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) +{ + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev->vmpck_id); + u64 count; + + if (!os_area_msg_seqno) { + pr_err("SNP unable to get message sequence counter\n"); + return 0; + } + + lockdep_assert_held(&snp_dev->cmd_mutex); + + /* Read the current message sequence counter from secrets pages */ + count = *os_area_msg_seqno; + + return count + 1; +} + +/* Return a non-zero on success */ +static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) +{ + u64 count = __snp_get_msg_seqno(snp_dev); + + /* + * The message sequence counter for the SNP guest request is a 64-bit + * value but the version 2 of GHCB specification defines a 32-bit storage + * for it. If the counter exceeds the 32-bit value then return zero. + * The caller should check the return value, but if the caller happens to + * not check the value and use it, then the firmware treats zero as an + * invalid number and will fail the message request. + */ + if (count >= UINT_MAX) { + pr_err("SNP request message sequence counter overflow\n"); + return 0; + } + + return count; +} + +static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) +{ + u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev->vmpck_id); + + if (!os_area_msg_seqno) { + pr_err("SNP unable to get message sequence counter\n"); + return; + } + + /* + * The counter is also incremented by the PSP, so increment it by 2 + * and save in secrets page. + */ + *os_area_msg_seqno += 2; +} + +static struct aesgcm_ctx *snp_init_crypto(unsigned int vmpck_id) +{ + struct aesgcm_ctx *ctx; + u8 *key; + + if (snp_is_vmpck_empty(vmpck_id)) { + pr_err("SNP: vmpck id %d is null\n", vmpck_id); + return NULL; + } + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); + if (!ctx) + return NULL; + + key = snp_get_vmpck(vmpck_id); + if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { + pr_err("SNP: crypto init failed\n"); + kfree(ctx); + return NULL; + } + + return ctx; +} + +int snp_setup_psp_messaging(struct snp_guest_dev *snp_dev) +{ + struct sev_guest_platform_data *pdata; + int ret; + + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + pr_err("SNP not supported\n"); + return 0; + } + + if (platform_data) { + pr_debug("SNP platform data already initialized.\n"); + goto create_ctx; + } + + if (!secrets_pa) { + pr_err("SNP no secrets page\n"); + return -ENODEV; + } + + pdata = kzalloc(sizeof(struct sev_guest_platform_data), GFP_KERNEL); + if (!pdata) { + pr_err("SNP alloc failed\n"); + return -ENOMEM; + } + + pdata->layout = (__force void *)ioremap_encrypted(secrets_pa, PAGE_SIZE); + if (!pdata->layout) { + pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n"); + goto e_free_pdata; + } + + ret = -ENOMEM; + /* Allocate the shared page used for the request and response message. */ + pdata->request = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->request) + goto e_unmap; + + pdata->response = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->response) + goto e_free_request; + + /* initial the input address for guest request */ + pdata->input.req_gpa = __pa(pdata->request); + pdata->input.resp_gpa = __pa(pdata->response); + platform_data = pdata; + +create_ctx: + ret = -EIO; + snp_dev->ctx = snp_init_crypto(snp_dev->vmpck_id); + if (!snp_dev->ctx) { + pr_err("SNP init crypto failed\n"); + platform_data = NULL; + goto e_free_response; + } + + snp_dev->pdata = platform_data; + return 0; + +e_free_response: + free_shared_pages(pdata->response, sizeof(struct snp_guest_msg)); +e_free_request: + free_shared_pages(pdata->request, sizeof(struct snp_guest_msg)); +e_unmap: + iounmap(pdata->layout); +e_free_pdata: + kfree(pdata); + + return ret; +} +EXPORT_SYMBOL_GPL(snp_setup_psp_messaging); + +static int __enc_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; + + if (WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) + return -EBADMSG; + + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + aesgcm_encrypt(ctx, msg->payload, plaintext, len, &hdr->algo, AAD_LEN, + iv, hdr->authtag); + return 0; +} + +static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_msg_hdr *hdr = &msg->hdr; + u8 iv[GCM_AES_IV_SIZE] = {}; + + memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + if (aesgcm_decrypt(ctx, plaintext, msg->payload, len, &hdr->algo, + AAD_LEN, iv, hdr->authtag)) + return 0; + else + return -EBADMSG; +} + +static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req, + struct sev_guest_platform_data *pdata) +{ + struct snp_guest_msg *resp = &snp_dev->secret_response; + struct snp_guest_msg *req = &snp_dev->secret_request; + struct snp_guest_msg_hdr *req_hdr = &req->hdr; + struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + struct aesgcm_ctx *ctx = snp_dev->ctx; + + pr_debug("response [seqno %lld type %d version %d sz %d]\n", + resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, + resp_hdr->msg_sz); + + /* Copy response from shared memory to encrypted memory. */ + memcpy(resp, pdata->response, sizeof(*resp)); + + /* Verify that the sequence counter is incremented by 1 */ + if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) + return -EBADMSG; + + /* Verify response message type and version number. */ + if (resp_hdr->msg_type != (req_hdr->msg_type + 1) || + resp_hdr->msg_version != req_hdr->msg_version) + return -EBADMSG; + + /* + * If the message size is greater than our buffer length then return + * an error. + */ + if (unlikely((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) + return -EBADMSG; + + return dec_payload(ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); +} + +static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, struct snp_guest_req *req) +{ + struct snp_guest_msg *msg = &snp_dev->secret_request; + struct snp_guest_msg_hdr *hdr = &msg->hdr; + + memset(msg, 0, sizeof(*msg)); + + hdr->algo = SNP_AEAD_AES_256_GCM; + hdr->hdr_version = MSG_HDR_VER; + hdr->hdr_sz = sizeof(*hdr); + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; + hdr->msg_seqno = seqno; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; + + /* Verify the sequence number is non-zero */ + if (!hdr->msg_seqno) + return -ENOSR; + + pr_debug("request [seqno %lld type %d version %d sz %d]\n", + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); + + return __enc_payload(snp_dev->ctx, msg, req->req_buf, req->req_sz); +} + +static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, + struct snp_guest_request_ioctl *rio); + +static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio, + struct sev_guest_platform_data *pdata) +{ + unsigned long req_start = jiffies; + unsigned int override_npages = 0; + u64 override_err = 0; + int rc; + +retry_request: + /* + * Call firmware to process the request. In this function the encrypted + * message enters shared memory with the host. So after this call the + * sequence number must be incremented or the VMPCK must be deleted to + * prevent reuse of the IV. + */ + rc = snp_issue_guest_request(req, &pdata->input, rio); + switch (rc) { + case -ENOSPC: + /* + * If the extended guest request fails due to having too + * small of a certificate data buffer, retry the same + * guest request without the extended data request in + * order to increment the sequence number and thus avoid + * IV reuse. + */ + override_npages = *req->data_npages; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; + + /* + * Override the error to inform callers the given extended + * request buffer size was too small and give the caller the + * required buffer size. + */ + override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); + + /* + * If this call to the firmware succeeds, the sequence number can + * be incremented allowing for continued use of the VMPCK. If + * there is an error reflected in the return value, this value + * is checked further down and the result will be the deletion + * of the VMPCK and the error code being propagated back to the + * user as an ioctl() return code. + */ + goto retry_request; + + /* + * The host may return SNP_GUEST_REQ_ERR_BUSY if the request has been + * throttled. Retry in the driver to avoid returning and reusing the + * message sequence number on a different message. + */ + case -EAGAIN: + if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { + rc = -ETIMEDOUT; + break; + } + schedule_timeout_killable(SNP_REQ_RETRY_DELAY); + goto retry_request; + } + + /* + * Increment the message sequence number. There is no harm in doing + * this now because decryption uses the value stored in the response + * structure and any failure will wipe the VMPCK, preventing further + * use anyway. + */ + snp_inc_msg_seqno(snp_dev); + + if (override_err) { + rio->exitinfo2 = override_err; + + /* + * If an extended guest request was issued and the supplied certificate + * buffer was not large enough, a standard guest request was issued to + * prevent IV reuse. If the standard request was successful, return -EIO + * back to the caller as would have originally been returned. + */ + if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) + rc = -EIO; + } + + if (override_npages) + *req->data_npages = override_npages; + + return rc; +} + +int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) +{ + struct sev_guest_platform_data *pdata; + u64 seqno; + int rc; + + if (!snp_dev || !snp_dev->pdata || !req || !rio) + return -ENODEV; + + pdata = snp_dev->pdata; + + /* Get message sequence and verify that its a non-zero */ + seqno = snp_get_msg_seqno(snp_dev); + if (!seqno) + return -EIO; + + /* Clear shared memory's response for the host to populate. */ + memset(pdata->response, 0, sizeof(struct snp_guest_msg)); + + /* Encrypt the userspace provided payload in pdata->secret_request. */ + rc = enc_payload(snp_dev, seqno, req); + if (rc) + return rc; + + /* + * Write the fully encrypted request to the shared unencrypted + * request page. + */ + memcpy(pdata->request, &snp_dev->secret_request, sizeof(snp_dev->secret_request)); + + rc = __handle_guest_request(snp_dev, req, rio, pdata); + if (rc) { + if (rc == -EIO && + rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) + return rc; + + pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", + rc, rio->exitinfo2); + snp_disable_vmpck(snp_dev); + return rc; + } + + rc = verify_and_dec_payload(snp_dev, req, pdata); + if (rc) { + pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc); + snp_disable_vmpck(snp_dev); + return rc; + } + + return 0; +} +EXPORT_SYMBOL_GPL(snp_send_guest_request); + +bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) +{ + if (WARN_ON(vmpck_id > 3)) + return false; + + dev->vmpck_id = vmpck_id; + + return true; +} +EXPORT_SYMBOL_GPL(snp_assign_vmpck); + static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) { struct sev_es_save_area *cur_vmsa, *vmsa; @@ -2160,8 +2612,8 @@ static int __init init_sev_config(char *str) } __setup("sev=", init_sev_config); -int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio) +static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, + struct snp_guest_request_ioctl *rio) { struct ghcb_state state; struct es_em_ctxt ctxt; @@ -2228,7 +2680,6 @@ int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *inpu return ret; } -EXPORT_SYMBOL_GPL(snp_issue_guest_request); static struct platform_device sev_guest_device = { .name = "sev-guest", @@ -2237,18 +2688,9 @@ static struct platform_device sev_guest_device = { static int __init snp_init_platform_device(void) { - struct sev_guest_platform_data data; - if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - if (!secrets_pa) - return -ENODEV; - - data.secrets_gpa = secrets_pa; - if (platform_device_add_data(&sev_guest_device, &data, sizeof(data))) - return -ENODEV; - if (platform_device_register(&sev_guest_device)) return -ENODEV; diff --git a/drivers/virt/coco/sev-guest/Kconfig b/drivers/virt/coco/sev-guest/Kconfig index bcc760bfb468..c130456ad401 100644 --- a/drivers/virt/coco/sev-guest/Kconfig +++ b/drivers/virt/coco/sev-guest/Kconfig @@ -2,7 +2,6 @@ config SEV_GUEST tristate "AMD SEV Guest driver" default m depends on AMD_MEM_ENCRYPT - select CRYPTO_LIB_AESGCM help SEV-SNP firmware provides the guest a mechanism to communicate with the PSP without risk from a malicious hypervisor who wishes to read, diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 8ad43e007d3b..9733ff348db4 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -27,32 +27,6 @@ #define DEVICE_NAME "sev-guest" -#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) -#define SNP_REQ_RETRY_DELAY (2*HZ) - -struct snp_guest_dev { - struct device *dev; - struct miscdevice misc; - - /* Mutex to serialize the shared buffer access and command handling. */ - struct mutex cmd_mutex; - - void *certs_data; - struct aesgcm_ctx *ctx; - /* request and response are in unencrypted memory */ - struct snp_guest_msg *request, *response; - - /* - * Avoid information leakage by double-buffering shared messages - * in fields that are in regular encrypted memory. - */ - struct snp_guest_msg secret_request, secret_response; - - struct snp_secrets_page_layout *layout; - struct snp_req_data input; - unsigned int vmpck_id; -}; - static u32 vmpck_id; module_param(vmpck_id, uint, 0444); MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP."); @@ -66,98 +40,6 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) return 0; } -static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev) -{ - return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN; -} - -static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev) -{ - return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id; -} - -static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev) -{ - char zero_key[VMPCK_KEY_LEN] = {0}; - u8 *key = snp_get_vmpck(snp_dev); - - if (key) - return !memcmp(key, zero_key, VMPCK_KEY_LEN); - - return true; -} - -/* - * If an error is received from the host or AMD Secure Processor (ASP) there - * are two options. Either retry the exact same encrypted request or discontinue - * using the VMPCK. - * - * This is because in the current encryption scheme GHCB v2 uses AES-GCM to - * encrypt the requests. The IV for this scheme is the sequence number. GCM - * cannot tolerate IV reuse. - * - * The ASP FW v1.51 only increments the sequence numbers on a successful - * guest<->ASP back and forth and only accepts messages at its exact sequence - * number. - * - * So if the sequence number were to be reused the encryption scheme is - * vulnerable. If the sequence number were incremented for a fresh IV the ASP - * will reject the request. - */ -static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) -{ - u8 *key = snp_get_vmpck(snp_dev); - - dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - snp_dev->vmpck_id); - memzero_explicit(key, VMPCK_KEY_LEN); -} - -static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev) -{ - u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); - u64 count; - - lockdep_assert_held(&snp_dev->cmd_mutex); - - /* Read the current message sequence counter from secrets pages */ - count = *os_area_msg_seqno; - - return count + 1; -} - -/* Return a non-zero on success */ -static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev) -{ - u64 count = __snp_get_msg_seqno(snp_dev); - - /* - * The message sequence counter for the SNP guest request is a 64-bit - * value but the version 2 of GHCB specification defines a 32-bit storage - * for it. If the counter exceeds the 32-bit value then return zero. - * The caller should check the return value, but if the caller happens to - * not check the value and use it, then the firmware treats zero as an - * invalid number and will fail the message request. - */ - if (count >= UINT_MAX) { - dev_err(snp_dev->dev, "request message sequence counter overflow\n"); - return 0; - } - - return count; -} - -static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev) -{ - u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev); - - /* - * The counter is also incremented by the PSP, so increment it by 2 - * and save in secrets page. - */ - *os_area_msg_seqno += 2; -} - static inline struct snp_guest_dev *to_snp_dev(struct file *file) { struct miscdevice *dev = file->private_data; @@ -165,255 +47,6 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev) -{ - struct aesgcm_ctx *ctx; - u8 *key; - - if (snp_is_vmpck_empty(snp_dev)) { - pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id); - return NULL; - } - - ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); - if (!ctx) - return NULL; - - key = snp_get_vmpck(snp_dev); - if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) { - pr_err("SNP: crypto init failed\n"); - kfree(ctx); - return NULL; - } - - return ctx; -} - -static int __enc_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, - void *plaintext, size_t len) -{ - struct snp_guest_msg_hdr *hdr = &msg->hdr; - u8 iv[GCM_AES_IV_SIZE] = {}; - - if (WARN_ON((hdr->msg_sz + ctx->authsize) > sizeof(msg->payload))) - return -EBADMSG; - - memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); - aesgcm_encrypt(ctx, msg->payload, plaintext, len, &hdr->algo, AAD_LEN, - iv, hdr->authtag); - return 0; -} - -static int dec_payload(struct aesgcm_ctx *ctx, struct snp_guest_msg *msg, - void *plaintext, size_t len) -{ - struct snp_guest_msg_hdr *hdr = &msg->hdr; - u8 iv[GCM_AES_IV_SIZE] = {}; - - memcpy(iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); - if (aesgcm_decrypt(ctx, plaintext, msg->payload, len, &hdr->algo, - AAD_LEN, iv, hdr->authtag)) - return 0; - else - return -EBADMSG; -} - -static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_req *guest_req) -{ - struct snp_guest_msg *resp = &snp_dev->secret_response; - struct snp_guest_msg *req = &snp_dev->secret_request; - struct snp_guest_msg_hdr *req_hdr = &req->hdr; - struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; - struct aesgcm_ctx *ctx = snp_dev->ctx; - - pr_debug("response [seqno %lld type %d version %d sz %d]\n", - resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, - resp_hdr->msg_sz); - - /* Copy response from shared memory to encrypted memory. */ - memcpy(resp, snp_dev->response, sizeof(*resp)); - - /* Verify that the sequence counter is incremented by 1 */ - if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) - return -EBADMSG; - - /* Verify response message type and version number. */ - if (resp_hdr->msg_type != (req_hdr->msg_type + 1) || - resp_hdr->msg_version != req_hdr->msg_version) - return -EBADMSG; - - /* - * If the message size is greater than our buffer length then return - * an error. - */ - if (unlikely((resp_hdr->msg_sz + ctx->authsize) > guest_req->resp_sz)) - return -EBADMSG; - - /* Decrypt the payload */ - return dec_payload(ctx, resp, guest_req->resp_buf, resp_hdr->msg_sz); -} - -static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, struct snp_guest_req *req) -{ - struct snp_guest_msg *msg = &snp_dev->secret_request; - struct snp_guest_msg_hdr *hdr = &msg->hdr; - - memset(msg, 0, sizeof(*msg)); - - hdr->algo = SNP_AEAD_AES_256_GCM; - hdr->hdr_version = MSG_HDR_VER; - hdr->hdr_sz = sizeof(*hdr); - hdr->msg_type = req->msg_type; - hdr->msg_version = req->msg_version; - hdr->msg_seqno = seqno; - hdr->msg_vmpck = req->vmpck_id; - hdr->msg_sz = req->req_sz; - - /* Verify the sequence number is non-zero */ - if (!hdr->msg_seqno) - return -ENOSR; - - pr_debug("request [seqno %lld type %d version %d sz %d]\n", - hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); - - return __enc_payload(snp_dev->ctx, msg, req->req_buf, req->req_sz); -} - -static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, - struct snp_guest_request_ioctl *rio) -{ - unsigned long req_start = jiffies; - unsigned int override_npages = 0; - u64 override_err = 0; - int rc; - -retry_request: - /* - * Call firmware to process the request. In this function the encrypted - * message enters shared memory with the host. So after this call the - * sequence number must be incremented or the VMPCK must be deleted to - * prevent reuse of the IV. - */ - rc = snp_issue_guest_request(req, &snp_dev->input, rio); - switch (rc) { - case -ENOSPC: - /* - * If the extended guest request fails due to having too - * small of a certificate data buffer, retry the same - * guest request without the extended data request in - * order to increment the sequence number and thus avoid - * IV reuse. - */ - override_npages = *req->data_npages; - req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; - - /* - * Override the error to inform callers the given extended - * request buffer size was too small and give the caller the - * required buffer size. - */ - override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); - - /* - * If this call to the firmware succeeds, the sequence number can - * be incremented allowing for continued use of the VMPCK. If - * there is an error reflected in the return value, this value - * is checked further down and the result will be the deletion - * of the VMPCK and the error code being propagated back to the - * user as an ioctl() return code. - */ - goto retry_request; - - /* - * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been - * throttled. Retry in the driver to avoid returning and reusing the - * message sequence number on a different message. - */ - case -EAGAIN: - if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { - rc = -ETIMEDOUT; - break; - } - schedule_timeout_killable(SNP_REQ_RETRY_DELAY); - goto retry_request; - } - - /* - * Increment the message sequence number. There is no harm in doing - * this now because decryption uses the value stored in the response - * structure and any failure will wipe the VMPCK, preventing further - * use anyway. - */ - snp_inc_msg_seqno(snp_dev); - - if (override_err) { - rio->exitinfo2 = override_err; - - /* - * If an extended guest request was issued and the supplied certificate - * buffer was not large enough, a standard guest request was issued to - * prevent IV reuse. If the standard request was successful, return -EIO - * back to the caller as would have originally been returned. - */ - if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) - rc = -EIO; - } - - if (override_npages) - *req->data_npages = override_npages; - - return rc; -} - -static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req, - struct snp_guest_request_ioctl *rio) -{ - u64 seqno; - int rc; - - /* Get message sequence and verify that its a non-zero */ - seqno = snp_get_msg_seqno(snp_dev); - if (!seqno) - return -EIO; - - /* Clear shared memory's response for the host to populate. */ - memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); - - /* Encrypt the userspace provided payload in snp_dev->secret_request. */ - rc = enc_payload(snp_dev, seqno, req); - if (rc) - return rc; - - /* - * Write the fully encrypted request to the shared unencrypted - * request page. - */ - memcpy(snp_dev->request, &snp_dev->secret_request, - sizeof(snp_dev->secret_request)); - - rc = __handle_guest_request(snp_dev, req, rio); - if (rc) { - if (rc == -EIO && - rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) - return rc; - - dev_alert(snp_dev->dev, - "Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", - rc, rio->exitinfo2); - snp_disable_vmpck(snp_dev); - return rc; - } - - rc = verify_and_dec_payload(snp_dev, req); - if (rc) { - dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); - snp_disable_vmpck(snp_dev); - return rc; - } - - return 0; -} - static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, struct snp_guest_request_ioctl *rio, u8 type, void *req_buf, size_t req_sz, void *resp_buf, @@ -422,7 +55,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, struct snp_guest_req guest_req = { .msg_version = rio->msg_version, .msg_type = type, - .vmpck_id = vmpck_id, + .vmpck_id = snp_dev->vmpck_id, .req_buf = req_buf, .req_sz = req_sz, .resp_buf = resp_buf, @@ -441,7 +74,7 @@ static int handle_guest_request_ext(struct snp_guest_dev *snp_dev, u64 exit_code struct snp_guest_req guest_req = { .msg_version = rio->msg_version, .msg_type = type, - .vmpck_id = vmpck_id, + .vmpck_id = snp_dev->vmpck_id, .req_buf = req_buf, .req_sz = req_sz, .resp_buf = resp_buf, @@ -627,7 +260,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long mutex_lock(&snp_dev->cmd_mutex); /* Check if the VMPCK is not empty */ - if (snp_is_vmpck_empty(snp_dev)) { + if (snp_is_vmpck_empty(snp_dev->vmpck_id)) { dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); mutex_unlock(&snp_dev->cmd_mutex); return -ENOTTY; @@ -655,147 +288,63 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long return ret; } -static void free_shared_pages(void *buf, size_t sz) -{ - unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; - int ret; - - if (!buf) - return; - - ret = set_memory_encrypted((unsigned long)buf, npages); - if (ret) { - WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n"); - return; - } - - __free_pages(virt_to_page(buf), get_order(sz)); -} - -static void *alloc_shared_pages(struct device *dev, size_t sz) -{ - unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; - struct page *page; - int ret; - - page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); - if (!page) - return NULL; - - ret = set_memory_decrypted((unsigned long)page_address(page), npages); - if (ret) { - dev_err(dev, "failed to mark page shared, ret=%d\n", ret); - __free_pages(page, get_order(sz)); - return NULL; - } - - return page_address(page); -} - static const struct file_operations snp_guest_fops = { .owner = THIS_MODULE, .unlocked_ioctl = snp_guest_ioctl, }; -bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) -{ - if (WARN_ON(vmpck_id > 3)) - return false; - - dev->vmpck_id = vmpck_id; - - return true; -} - static int __init sev_guest_probe(struct platform_device *pdev) { - struct snp_secrets_page_layout *layout; - struct sev_guest_platform_data *data; struct device *dev = &pdev->dev; struct snp_guest_dev *snp_dev; struct miscdevice *misc; - void __iomem *mapping; int ret; if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - if (!dev->platform_data) - return -ENODEV; - - data = (struct sev_guest_platform_data *)dev->platform_data; - mapping = ioremap_encrypted(data->secrets_gpa, PAGE_SIZE); - if (!mapping) - return -ENODEV; - - layout = (__force void *)mapping; - - ret = -ENOMEM; snp_dev = devm_kzalloc(&pdev->dev, sizeof(struct snp_guest_dev), GFP_KERNEL); if (!snp_dev) - goto e_unmap; + return -ENOMEM; - ret = -EINVAL; - snp_dev->layout = layout; if (!snp_assign_vmpck(snp_dev, vmpck_id)) { dev_err(dev, "invalid vmpck id %d\n", vmpck_id); - goto e_unmap; + ret = -EINVAL; + goto e_free_snpdev; } - /* Verify that VMPCK is not zero. */ - if (snp_is_vmpck_empty(snp_dev)) { - dev_err(dev, "vmpck id %d is null\n", vmpck_id); - goto e_unmap; + if (snp_setup_psp_messaging(snp_dev)) { + dev_err(dev, "Unable to setup PSP messaging vmpck id %d\n", snp_dev->vmpck_id); + ret = -ENODEV; + goto e_free_snpdev; } mutex_init(&snp_dev->cmd_mutex); platform_set_drvdata(pdev, snp_dev); snp_dev->dev = dev; - /* Allocate the shared page used for the request and response message. */ - snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); - if (!snp_dev->request) - goto e_unmap; - - snp_dev->response = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); - if (!snp_dev->response) - goto e_free_request; - - snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE); + snp_dev->certs_data = alloc_shared_pages(SEV_FW_BLOB_MAX_SIZE); if (!snp_dev->certs_data) - goto e_free_response; - - ret = -EIO; - snp_dev->ctx = snp_init_crypto(snp_dev); - if (!snp_dev->ctx) - goto e_free_cert_data; + goto e_free_ctx; misc = &snp_dev->misc; misc->minor = MISC_DYNAMIC_MINOR; misc->name = DEVICE_NAME; misc->fops = &snp_guest_fops; - /* initial the input address for guest request */ - snp_dev->input.req_gpa = __pa(snp_dev->request); - snp_dev->input.resp_gpa = __pa(snp_dev->response); - ret = misc_register(misc); if (ret) - goto e_free_ctx; + goto e_free_cert_data; - dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id); + dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", snp_dev->vmpck_id); return 0; -e_free_ctx: - kfree(snp_dev->ctx); e_free_cert_data: free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); -e_free_response: - free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); -e_free_request: - free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); -e_unmap: - iounmap(mapping); + e_free_ctx: + kfree(snp_dev->ctx); +e_free_snpdev: + kfree(snp_dev); return ret; } @@ -803,11 +352,9 @@ static int __exit sev_guest_remove(struct platform_device *pdev) { struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev); - free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); - free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); - free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); - kfree(snp_dev->ctx); misc_deregister(&snp_dev->misc); + kfree(snp_dev->ctx); + kfree(snp_dev); return 0; } From patchwork Sat Jul 22 11:19:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124273 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp748779vqg; Sat, 22 Jul 2023 04:25:20 -0700 (PDT) X-Google-Smtp-Source: APBJJlHHwZ7Vhp9SFtprDVg/i5wKaFL3SqeFMPiiSk+cs66BeQu1/BRaSbu13oSpMCKbyn6ZQJ8t X-Received: by 2002:aa7:c558:0:b0:51d:d30c:f1e3 with SMTP id s24-20020aa7c558000000b0051dd30cf1e3mr3737343edr.16.1690025119783; Sat, 22 Jul 2023 04:25:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690025119; cv=pass; d=google.com; s=arc-20160816; b=p0D7j1ZQpxE+QeB6Z+NZFaB1t5XZMfgV2xk21cKCwVbMy1MNB6lBxxWrC0bFn16Uam ZdB7V0eQIxOtaeO0qVe0SXiC16em2TLP2822JXlIs9RP1dGbohCj3KqHMCNEU24aRIog X6cBXR5ADRE6Jidghad2Sxm3ywHDzJQz5mG/g2MpO0N/BldM4cVt1Paj80Vzo63lNqmz p7uhELt15kZzp7f0Xj+4S+PFypystEZ0gHReJzcJyDG4JmaUWl5ix31Z2F+M1rrsVhds WevzxDpqm1ymzfldMGKABnStqTw/IlwE66UTzJQJw91l2hPgN7oHPlQ+pd57XlzkzQ/y VNPw== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=Bo1Evqk/mz2ev36gdunzlVNfClz3K9jqaUfvzUPSVMY=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=DXV5JdtcJBSUFGFH6bUY8y1VI5fpgFg/y2EE+8sg+gxy5dovB4wFTZlYoMr9EhTlQh Y2hobuOs+pJWRygJ+DXo0yvyOGxpBsk9sp2vcdqPIW/pu2FkFX/o7f7M2LM5XNN/utaG 2domZFhFuYpwSCYKyno8wrruHk6dMk5wEB10o3Q6W7gPGkAkwtTFsV6n5TAfkjImoo+d V/bCsF+Db9rsfzf4lIms9nxB/U0OWSai/cgzWXbCvkA/gIhvucqdKO3EJWL+9/kS5DC5 Qku89nzWkTRWUWmlrVg8o4aznhk/6zDnr9Y0/jSVy8DYseD46O2KXX7BpRZhIjoVd3xF Smyg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=tIh12WR0; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v17-20020aa7d651000000b005218c27b18csi3637299edr.498.2023.07.22.04.24.56; Sat, 22 Jul 2023 04:25:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=tIh12WR0; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229588AbjGVLVc (ORCPT + 99 others); Sat, 22 Jul 2023 07:21:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbjGVLVa (ORCPT ); Sat, 22 Jul 2023 07:21:30 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2061.outbound.protection.outlook.com [40.107.93.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB47D35B8 for ; Sat, 22 Jul 2023 04:20:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BsGBmjYvkwqFUq/Az6M7QOqbb1Z66QJIk32mVj9txXMCxms7t5MF0lBrLTjUntB3ZjfDW0zDjyd2MDf5+11y5KKDY21z32/+qimb/9Neytnt72b/4DfL0zGnLZmvN38zZQUdFX6FH/WP87J0Cq+6EnYxxnZnnW5Zya9TJCqF9/85K7+utwFLkANNY8+sTOZdWVyz8tX+o259uZ0wh4cw3qC58j/jL2Efz86bXqZZPkO26ASlNi202oLrvoETqaA6RFXMlH+moJ+EYd7xi0/HK1RJKDrXciDnq2Nhvr0cepUgQS55XEHUL+ZKIQwKPXQJpKHRbDVgzWcqJn6iGcfJHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Bo1Evqk/mz2ev36gdunzlVNfClz3K9jqaUfvzUPSVMY=; b=moZwrNTDCKEZOtCSI7c/eoRmjQZNWBeq3nOBvGf/AebfZOjzFMRy33o8eG9KAcy4Unebdo0yguOFPh25yzQZaGnpGzjUjGifDwaP9xZ6t5z3bk/9mlwkpstCIyJPZOqibGByn+w3RsdA9skMhnJFzqd6II+OiXl+Ez4TjTjVNVwePbmfVGnknefOEgm1nEBzFr/HXf73x4iqn/BBma2su5Hw9aG0VcJmP2nm1ofG5W9n34LdJ9YKm34j373Dc1P/6Hvn2z1cJPO9Ws39Utb/tlcqeVLtwJCRQfPQlY2tVn5rbslwQqFCRJIS++qteSlasXtjqCGfIubBRkZ7T7VwfQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bo1Evqk/mz2ev36gdunzlVNfClz3K9jqaUfvzUPSVMY=; b=tIh12WR04QnvhISjpzHGjq0XVv3IeKiACkdXXs95r9O3Pwa5SQGc1Vh/wyO84PLFJFI0pe1YnVBsInMfNn1F3981kgOh7aEPLFquR9lMfw63YShcyMq5SKlM3tU7sxT+gM6nMv7iEmsj/ecqQqBTGSJKZQAwnyeHvOwpEUNkNjM= Received: from DS7PR03CA0166.namprd03.prod.outlook.com (2603:10b6:5:3b2::21) by DM6PR12MB4169.namprd12.prod.outlook.com (2603:10b6:5:215::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30; Sat, 22 Jul 2023 11:20:41 +0000 Received: from DM6NAM11FT027.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b2:cafe::6) by DS7PR03CA0166.outlook.office365.com (2603:10b6:5:3b2::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT027.mail.protection.outlook.com (10.13.172.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:41 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:38 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 08/14] x86/mm: Add generic guest initialization hook Date: Sat, 22 Jul 2023 16:49:03 +0530 Message-ID: <20230722111909.15166-9-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT027:EE_|DM6PR12MB4169:EE_ X-MS-Office365-Filtering-Correlation-Id: 9261fe10-b69e-4e5a-9f06-08db8aa5af4d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z9wWSlcVFXHaVMlRQjamipPrKUbrXApzkghkAzQKlUaMQojCm9lhJO10oWxz0TqBmIBJoGkvbNHFTTi9buEl/1mzSgKitb2x2pSRY2+US4WCCNy4uuh5jbbDWx7MhUttUV7cNIa2Vj8BEYzkBg+GKa9BiuZzalgnMS2jPKM/xbH0yzTz3crEbs8odirxvoiXMd/4qgQE1QDwFgaEMhWRj7fd1GBNbz9vhXQ8QYKqwk/5la/B2u2/h5FlBbhDQfIOWAxMdA2AY3J4C0NBZdOFsazM1c6ewQG1MnJaDo3nEGHLfggjo4IPnJV7gQi2GYSDZdsohzoezbOpGv9amNurxDKByV6h7xUXdxUdPTyvcJu3+D4HoUQiMfQl21r/DmBCatPMrAmi5QtHeIlxnz080j+fwSKzyaWcbUhIUlOrSXO3g10/Yuw2iCVPrHaoB+eTqeVFNVRL50PG7Rm+PuC9sjTWdK9kBjUg2/1EPRO7dRAVMajDFuo6ZSz7AfVi37YlOeoyjyRb8bTyuu6gmQO1Msjfcvlt6ZCJnsb7GVDH5PqIiuFNLR1PD0rl7wk/Kf+3pi7Mp/gCEplYW6ilOkQgF34pCQoOyt1btM96pUDm/a4llSclcY1PRlvhEqZm9q4U8BeWcLRYeiqvqDCJV2YCtuPUy9ygF/fsqt0HE2PVSX9a3WopyTL5IcSpVcQBCSFDeW8DKdfnov4rtOPwf1xlXebqRfw1dOn3bqLRH4EGNxd1nVzNLQMZeMVkCGuYeeEp3NSclGsdBRANbVM3XKHLcVvHlW+ksIKOrVtaHuXrg84= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199021)(82310400008)(40470700004)(46966006)(36840700001)(40460700003)(81166007)(40480700001)(356005)(36860700001)(36756003)(426003)(2616005)(47076005)(83380400001)(16526019)(8936002)(8676002)(5660300002)(110136005)(478600001)(54906003)(4326008)(316002)(70586007)(70206006)(41300700001)(26005)(186003)(336012)(1076003)(6666004)(7696005)(2906002)(82740400003)(41533002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:41.7612 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9261fe10-b69e-4e5a-9f06-08db8aa5af4d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT027.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4169 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119780067480869 X-GMAIL-MSGID: 1772119780067480869 Add generic enc_init guest hook for performing any type of initialization that is vendor specific. Signed-off-by: Nikunj A Dadhania --- arch/x86/include/asm/x86_init.h | 2 ++ arch/x86/kernel/x86_init.c | 2 ++ arch/x86/mm/mem_encrypt.c | 3 +++ 3 files changed, 7 insertions(+) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 88085f369ff6..5bca02769074 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -148,12 +148,14 @@ struct x86_init_acpi { * @enc_status_change_finish Notify HV after the encryption status of a range is changed * @enc_tlb_flush_required Returns true if a TLB flush is needed before changing page encryption status * @enc_cache_flush_required Returns true if a cache flush is needed before changing page encryption status + * @enc_init Prepare and initialize encryption features */ struct x86_guest { void (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc); bool (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc); bool (*enc_tlb_flush_required)(bool enc); bool (*enc_cache_flush_required)(void); + void (*enc_init)(void); }; /** diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index d82f4fa2f1bf..451e0f39d053 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -135,6 +135,7 @@ static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } static bool is_private_mmio_noop(u64 addr) {return false; } +static void enc_init_noop(void) { } struct x86_platform_ops x86_platform __ro_after_init = { .calibrate_cpu = native_calibrate_cpu_early, @@ -157,6 +158,7 @@ struct x86_platform_ops x86_platform __ro_after_init = { .enc_status_change_finish = enc_status_change_finish_noop, .enc_tlb_flush_required = enc_tlb_flush_required_noop, .enc_cache_flush_required = enc_cache_flush_required_noop, + .enc_init = enc_init_noop, }, }; diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 9f27e14e185f..01abecc9a774 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -84,5 +84,8 @@ void __init mem_encrypt_init(void) /* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes(); + if (x86_platform.guest.enc_init) + x86_platform.guest.enc_init(); + print_mem_encrypt_feature_info(); } From patchwork Sat Jul 22 11:19:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124272 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp748485vqg; Sat, 22 Jul 2023 04:24:32 -0700 (PDT) X-Google-Smtp-Source: APBJJlH5JUIEJPR75lQH2WMhW9KbvXaO0WZuXWklZMvOKrUKqTaEw92MEm5nsPqBMoEOCVNza2HM X-Received: by 2002:a17:906:748b:b0:992:b1b2:9bf4 with SMTP id e11-20020a170906748b00b00992b1b29bf4mr4082428ejl.35.1690025072537; Sat, 22 Jul 2023 04:24:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690025072; cv=pass; d=google.com; s=arc-20160816; b=sjF1eJ2EY3fkyAuDCxxdCofTp0RaIG1Gr0IEDkIymSBLvBAa4uqb524pBeuMKjVGIB nnneIbCErpKcwoXJbkqAo5rSsc+85E5f8QVCw/+8A5Oh6Cicf+GZxQ3iPdCh/KqBprQg uNRg8V/hbHotxPNNX+lO0I5tfd6cuIi/Q8AxDBipkH+TtGkkDjGQlDDZBP08cQ6Cwzjs b5loGNW4sF3f/kQxh8VgK3Ov4L299JfzUUpPY8ePsKrz4f8Vy7x+3k6HkGeD832jQ27Z 4QYT9IxuMiISCm2GaJYzAGXFqiKNs/YirXDN7kw9SPEU2S0npYW+bFORMi7G/LlP81i2 3MuA== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=g8rFfVrUTFoITwQVDTO6F5FOD7whF12hpJmq/8o8lVc=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=Uwol/oFe3HdsHk+xBnlR465vVMiEc+kWYcxqt4qxrdFhjT+SLdUZM/0ecyRjSR8SIW s0B8HSOz7v1SBSSepzDiLSnvJhh+xQ6vxPAIhrVq9FvYJHfPlipWoTqJl5uc+2AIQAJJ 9Jh3ziESYT0013lnHtoTaO72ZwQdTqPfzlmpMo9HRitulBUtxNEs1y8KAuB7DSRlr5Ng AQvjQkm82EcPgEEP9WmuA3bbOwOAI5/VfC846lbEYk7/EZPw6z7rKBYmCkABzrvzgeqr BsqtEYuMcRwFFhcTIY32fijmBgIpLrVU+AuhOFAhmPjQttvMOWkJyixEyqgrQIXI+IkL UNjQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GwDuAPrd; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lv17-20020a170906bc9100b00997eac101c8si3257976ejb.523.2023.07.22.04.24.08; Sat, 22 Jul 2023 04:24:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GwDuAPrd; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229779AbjGVLVn (ORCPT + 99 others); Sat, 22 Jul 2023 07:21:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjGVLVk (ORCPT ); Sat, 22 Jul 2023 07:21:40 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2040.outbound.protection.outlook.com [40.107.93.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACEC83AAD for ; Sat, 22 Jul 2023 04:20:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RHGiHEasmjLAUD1JYQqEepgnmYQt2auhYNuKhPOIGIWpsEnN+Ox8wibF2dTmT99rLihSkq4hvn6pj9XxCq78tT/lsBwOUeYptx0s+6tJmJIIDRRgm5MiCIN2IO6a3joVo1dInvbCn2tvmWbYB9SXCviP5xV6ckvO5TgcNEOXvJo1rV5nZkHhxdBqY/I2p6ur35AFamkRjaFGRoeTnk5UMspWWEfYvIHZD2dVMw/xffQ4hH18CB2VbaxnAH3KVB9xliZlqHyovp15hLfYII62aLqHzMH0UkSrijTBN5cxhDw0bHP9Uk7hz0MMN8i6l1dDsmp2sipsXHRpyKR8uFfIGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=g8rFfVrUTFoITwQVDTO6F5FOD7whF12hpJmq/8o8lVc=; b=ME7eOtt+mlyuG8xQYvBaWitomFYmgLgGLi97vzgI0JvFxYhgeE2B1rWskwQmunPQSDjHdyZI1UYA54g9BEhEV8cPH0qaD/QNJgR66hML/0j90P5KY/N5RqIbRBbOnrKlxgek4ek5yJmKnTN3QvdfJBXT2xVGHPdqw495Rg6BVm4i7PZasaGGBM4ZJl1G9ur7W3irtTwjtl5zcQTFD26TFxI8AlEPXbjt4CvgXdi424nd8BYm72GzzgQugz73QOJYfpfbeCO9CpKf1gTHui7AY7FpDol+ErXYH/WL8qItj/cODWbhmllrFY3LMGRZNUThwrAAYK8thkT1Rw/4T+taiQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=g8rFfVrUTFoITwQVDTO6F5FOD7whF12hpJmq/8o8lVc=; b=GwDuAPrd6aGerXEtBT0f9IjcXMT3jDlmWW5PRNTrEZ79t+zinTPRRu+6gNlELQV9FSKMt7egAI3VRiMBTYO4nzCwXRigXDRT/9u26JMIt2/9yi9cWCHiulU7bPfB8IINv4MQ91zi5Xr672YUtZDjSyvWx6kjULApRypLRKyz+qw= Received: from DM6PR13CA0062.namprd13.prod.outlook.com (2603:10b6:5:134::39) by DM6PR12MB4283.namprd12.prod.outlook.com (2603:10b6:5:211::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.25; Sat, 22 Jul 2023 11:20:45 +0000 Received: from DM6NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:5:134:cafe::b5) by DM6PR13CA0062.outlook.office365.com (2603:10b6:5:134::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT036.mail.protection.outlook.com (10.13.172.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:45 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:41 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 09/14] x86/sev: Add Secure TSC support for SNP guests Date: Sat, 22 Jul 2023 16:49:04 +0530 Message-ID: <20230722111909.15166-10-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT036:EE_|DM6PR12MB4283:EE_ X-MS-Office365-Filtering-Correlation-Id: e3e63ab2-43ee-4523-b0db-08db8aa5b141 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 98fKRmOiYcSAuQN4nnMfuBqeBzYjIqngDf0Pi/9vDs51NqCHWy218wcco5WZYmXqJPA1ljuI9W/iegVURaC/eBa/ZmXibHZmHF+ei2Fp4Vr6Ox5SYQCKBEEuF5W2ks4Rui45s58a38aj3He16RqLY2PUJTqxfwrjPTPbrY7n6mPk1+aMrndetO/+QGKTn/4l34NhOmqJyubXIzYFLlY+/a2Dz7z27C5X4+wtV0OvroRO5s/M4HzOBh8m0EtgBmO0Gi3BPhC2wRRoU0a9yYSYFznXX7gRBWNVisMYdTNI9fe5amyMd6OD7PSO6EAWnjxKQkWz6luvhRVNP0AM0hOasW+FpLDfMwPQJ8YWHxtUFHCbZqc3vw9vEz7WufECJn+JK7SyuMMrcsSzIMReKhbfpAxRCEMK5SAUj7zBNdliT1q9takcUuVUEOHl6/xYYLLkCxzjadfIwZcvCsV/JSsRUb8rL3Y/6iTCEHMtyVK6xgES0bLqhBuG+mK9ZUN8MKqMcK4DF/4KNBU11TrFSGkMXye6st8wtShOjzrjm9QDrBrseOUeUg+AafuP+TJNY+4SIaALd0/pbqdjOR01r0AhkAqE4ZOfCSv/6z+2M0t+HuZPOpMfssJJwyjZephzqEEuwNC1So9btBIF4WHvK+Jf97N4jq2ai2tz1UT3AFpVGpgrqmDDmQm0qD4jyOW5mm58hkDe5SGQwAsHzE234XMRSSAeQ+g1BArt6LmM9nGvpKJ8p7KrkxURlokxcgEKAC2XV/+6MbR8vdJ4NaCH9SOkOA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(136003)(396003)(376002)(39860400002)(451199021)(82310400008)(36840700001)(40470700004)(46966006)(36860700001)(2906002)(47076005)(40460700003)(83380400001)(2616005)(426003)(356005)(81166007)(36756003)(82740400003)(40480700001)(4326008)(70206006)(70586007)(186003)(478600001)(26005)(336012)(1076003)(16526019)(41300700001)(316002)(110136005)(7696005)(6666004)(54906003)(8936002)(8676002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:45.0385 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e3e63ab2-43ee-4523-b0db-08db8aa5b141 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT036.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4283 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119730411529908 X-GMAIL-MSGID: 1772119730411529908 Add support for Secure TSC in SNP enabled guests. Secure TSC allows guest to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. During the boot-up of the secondary cpus, SecureTSC enabled guests need to query TSC info from AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, the hypervisor is just the conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal AES GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP. Signed-off-by: Nikunj A Dadhania --- arch/x86/coco/core.c | 3 ++ arch/x86/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev.c | 82 ++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 ++++ 7 files changed, 123 insertions(+), 2 deletions(-) diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c index 73f83233d25d..1cfb86c6bd78 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -89,6 +89,9 @@ static bool amd_cc_platform_has(enum cc_attr attr) case CC_ATTR_GUEST_SEV_SNP: return sev_status & MSR_AMD64_SEV_SNP_ENABLED; + case CC_ATTR_GUEST_SECURE_TSC: + return sev_status & MSR_AMD64_SNP_SECURE_TSC; + default: return false; } diff --git a/arch/x86/include/asm/sev-guest.h b/arch/x86/include/asm/sev-guest.h index e6f94208173d..58739173eba9 100644 --- a/arch/x86/include/asm/sev-guest.h +++ b/arch/x86/include/asm/sev-guest.h @@ -39,6 +39,8 @@ enum msg_type { SNP_MSG_ABSORB_RSP, SNP_MSG_VMRK_REQ, SNP_MSG_VMRK_RSP, + SNP_MSG_TSC_INFO_REQ = 17, + SNP_MSG_TSC_INFO_RSP, SNP_MSG_TYPE_MAX }; @@ -111,6 +113,22 @@ struct snp_guest_req { u8 msg_type; }; +struct snp_tsc_info_req { +#define SNP_TSC_INFO_REQ_SZ 128 + /* Must be zero filled */ + u8 rsvd[SNP_TSC_INFO_REQ_SZ]; +} __packed; + +struct snp_tsc_info_resp { + /* Status of TSC_INFO message */ + u32 status; + u32 rsvd1; + u64 tsc_scale; + u64 tsc_offset; + u32 tsc_factor; + u8 rsvd2[100]; +} __packed; + int snp_setup_psp_messaging(struct snp_guest_dev *snp_dev); int snp_send_guest_request(struct snp_guest_dev *dev, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio); diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 807f85f8014c..d5b35da1b583 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -189,6 +189,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); +void __init snp_secure_tsc_prepare(void); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -208,6 +209,7 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npag static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } +static inline void __init snp_secure_tsc_prepare(void) { } #endif #endif diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index e7c7379d6ac7..3956c5095109 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -412,7 +412,9 @@ struct sev_es_save_area { u8 reserved_0x298[80]; u32 pkru; u32 tsc_aux; - u8 reserved_0x2f0[24]; + u64 tsc_scale; + u64 tsc_offset; + u8 reserved_0x300[8]; u64 rcx; u64 rdx; u64 rbx; @@ -544,7 +546,7 @@ static inline void __unused_size_checks(void) BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0); BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248); BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298); - BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0); + BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x300); BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320); BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380); BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0); diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 72e76c58aebd..d55562cd395d 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -76,6 +76,10 @@ static u64 sev_hv_features __ro_after_init; /* Secrets page physical address from the CC blob */ static u64 secrets_pa __ro_after_init; +/* Secure TSC values read using TSC_INFO SNP Guest request */ +static u64 guest_tsc_scale __ro_after_init; +static u64 guest_tsc_offset __ro_after_init; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -1411,6 +1415,78 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, unsigned int vmpck_id) } EXPORT_SYMBOL_GPL(snp_assign_vmpck); +static struct snp_guest_dev tsc_snp_dev __initdata; + +static int __init snp_get_tsc_info(void) +{ + static u8 buf[SNP_TSC_INFO_REQ_SZ + AUTHTAG_LEN]; + struct snp_guest_request_ioctl rio; + struct snp_tsc_info_resp tsc_resp; + struct snp_tsc_info_req tsc_req; + struct snp_guest_req req; + int rc, resp_len; + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover the + * authtag. + */ + resp_len = sizeof(tsc_resp) + AUTHTAG_LEN; + if (sizeof(buf) < resp_len) + return -EINVAL; + + memset(&tsc_req, 0, sizeof(tsc_req)); + memset(&req, 0, sizeof(req)); + memset(&rio, 0, sizeof(rio)); + memset(buf, 0, sizeof(buf)); + + if (!snp_assign_vmpck(&tsc_snp_dev, 0)) + return -EINVAL; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(&tsc_snp_dev)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + req.msg_version = MSG_HDR_VER; + req.msg_type = SNP_MSG_TSC_INFO_REQ; + req.vmpck_id = tsc_snp_dev.vmpck_id; + req.req_buf = &tsc_req; + req.req_sz = sizeof(tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.exit_code = SVM_VMGEXIT_GUEST_REQUEST; + rc = snp_send_guest_request(&tsc_snp_dev, &req, &rio); + if (rc) + goto err_req; + + memcpy(&tsc_resp, buf, sizeof(tsc_resp)); + pr_debug("%s: Valid response status %x scale %llx offset %llx factor %x\n", + __func__, tsc_resp.status, tsc_resp.tsc_scale, tsc_resp.tsc_offset, + tsc_resp.tsc_factor); + + guest_tsc_scale = tsc_resp.tsc_scale; + guest_tsc_offset = tsc_resp.tsc_offset; + +err_req: + /* The response buffer contains the sensitive data, explicitly clear it. */ + memzero_explicit(buf, sizeof(buf)); + memzero_explicit(&tsc_resp, sizeof(tsc_resp)); + memzero_explicit(&req, sizeof(req)); + + return rc; +} + +void __init snp_secure_tsc_prepare(void) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) + return; + + if (snp_get_tsc_info()) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + pr_debug("SecureTSC enabled\n"); +} + static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) { struct sev_es_save_area *cur_vmsa, *vmsa; @@ -1511,6 +1587,12 @@ static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) vmsa->vmpl = 0; vmsa->sev_features = sev_status >> 2; + /* Setting Secure TSC parameters */ + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) { + vmsa->tsc_scale = guest_tsc_scale; + vmsa->tsc_offset = guest_tsc_offset; + } + /* Switch the page over to a VMSA page now that it is initialized */ ret = snp_set_vmsa(vmsa, true); if (ret) { diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index e0b51c09109f..fc25749fb2e5 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -215,6 +215,11 @@ void __init sme_map_bootdata(char *real_mode_data) __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true); } +void __init amd_enc_init(void) +{ + snp_secure_tsc_prepare(); +} + void __init sev_setup_arch(void) { phys_addr_t total_mem = memblock_phys_mem_size(); @@ -501,6 +506,7 @@ void __init sme_early_init(void) x86_platform.guest.enc_status_change_finish = amd_enc_status_change_finish; x86_platform.guest.enc_tlb_flush_required = amd_enc_tlb_flush_required; x86_platform.guest.enc_cache_flush_required = amd_enc_cache_flush_required; + x86_platform.guest.enc_init = amd_enc_init; } void __init mem_encrypt_free_decrypted_mem(void) diff --git a/include/linux/cc_platform.h b/include/linux/cc_platform.h index cb0d6cd1c12f..e081ca4d5da2 100644 --- a/include/linux/cc_platform.h +++ b/include/linux/cc_platform.h @@ -90,6 +90,14 @@ enum cc_attr { * Examples include TDX Guest. */ CC_ATTR_HOTPLUG_DISABLED, + + /** + * @CC_ATTR_GUEST_SECURE_TSC: Secure TSC is active. + * + * The platform/OS is running as a guest/virtual machine and actively + * using AMD SEV-SNP Secure TSC feature. + */ + CC_ATTR_GUEST_SECURE_TSC, }; #ifdef CONFIG_ARCH_HAS_CC_PLATFORM From patchwork Sat Jul 22 11:19:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124275 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp753942vqg; Sat, 22 Jul 2023 04:39:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlHHE/GAlEvcZ94dWlYBdX0060ItoSYauQ7OWbbzq75QiXAAkgzljF5XtgcIFVTJ5rpU3jWC X-Received: by 2002:a17:902:e5c4:b0:1b6:92f0:b6f5 with SMTP id u4-20020a170902e5c400b001b692f0b6f5mr7904442plf.14.1690025944717; Sat, 22 Jul 2023 04:39:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690025944; cv=pass; d=google.com; s=arc-20160816; b=FxMxee+IGAcLK/a6W3Q129+TuAH1R9ViHpjy5BggGGelc5RH0u/9JNOlI1KVC4J/9p 2h/P5znhlVmy3CnhCGf1/LyLgcyNTnwrTMbqXUXBY1dgbWMwMVS4FPutOr0jtoUARNmB j1RxzmTkIKnsTBrbJJXOSL9ykDi7If4FQ1fVFXGz86vuSSj2+4rCFfF/WX18RPTahvdy wjsxCp8dX3Dk7H9L+tlpkR/YgPm/Ak2Q9Ay2SVC1DE1DyN+lb/GRCl7MqSEGUEXwxLBy sJSy9h2iEIaDgtyMqHjiqGFVBq+DsicUYClXAxnuienyKsJHszB/JMH+wzDp/7czMW+H SMww== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=rhSrHljLl1rrS996VfK80M41HfQys5Vt16cM10AjRWE=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=AhT3ntko2suSmhGeSIXGurm5v/FRez0Rv2syVdcnNC7Qj7sTbT10ZPHE8+FMynQ1Kz l+TfTVhnf0mkZfWJVB0M/cFUlV7vqD3oVe6VZRdDhzlEAmdxCHTlw5RIg/T8A5tMrqIJ Xg3R4mBZ6wWmiiCVBMAbyREpIQHD4svpAAkkpC64SuUvAuFCZzQFhHHSuks2qfvR7NSR pjygc1Yil6Kak7RMtMD2N4vFs+HwKNB4Lm+g8c5j1D4UJGwlb8o16+QmoLrQW6v2CxC9 qdyGAKOanZMa4VKgqrVsW6DLt4z93NVA0dRIIrJ9iT3KfaeRyzHwlJD/7SncY/HJucph GuAg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=gLkcvAI4; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d18-20020a170903231200b001b8867a7ce2si5427394plh.273.2023.07.22.04.38.34; Sat, 22 Jul 2023 04:39:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=gLkcvAI4; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229994AbjGVLYv (ORCPT + 99 others); Sat, 22 Jul 2023 07:24:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229990AbjGVLYu (ORCPT ); Sat, 22 Jul 2023 07:24:50 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2080.outbound.protection.outlook.com [40.107.237.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5B383AB0 for ; Sat, 22 Jul 2023 04:24:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gbOHSvbu1ccqeP2NM8GAFfh5KtU3b64yijCt841ywPyn6UJy216qR61QH6t4uSB4Ijk1YRcchckgujhx1DStSrQDpilYVOcKpUHGrdKOgJuNkAXbs1nR4ByWDezMMgmHKffW6wIzKy3G38MyBzZ8bOrqH5ZYVrRCIv8cPkrZsnNUxcq/HSuJhn+45rZEOKL8cMlwzKAx7PkPxVgs5Si4U+w4KhgxZUkwLnmNF5hPc79qWtAlB+mz68CrRZR9VozZuHI0BNzr71ZksX0cgvfR1BwZbI896rpkgzG0GMUYnqLLG5nixYA4ppW7deSJHDWOYexUpAiH8krnKEUlxQO6GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rhSrHljLl1rrS996VfK80M41HfQys5Vt16cM10AjRWE=; b=LWEZtz7KfZwepa0PSBu6g8PkerazQU+Z8Vf2N/KRezjVDJqCihmiNnT7ZqFKCduQnz6396Eln5Xe+GJ6WC7pDPv0gbQ13KbQ6Wyj/BPNBDrlWHQgI2V0zeydILCvP6guNQ8IwQUcq1niwhVldr0RJvcYxYb8FSNFVJ5UQY2KIlgz/hfQsEPJa3zUbCa5xHfvH/YP0FbI0n84/3Vc6dkK4d6t/YPDTqUtwxkLrw5l4x9rG1nBv2EXwKBM2Q6sh5CABZlnvLV11Vj/fcTj6CPwC5Er9S3KRYVNc9r2ioqsTJ5GsEcVc9Ht34V486a+qzaw5FFH1KsG1hE7l9z2NYw+jQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rhSrHljLl1rrS996VfK80M41HfQys5Vt16cM10AjRWE=; b=gLkcvAI4wLaU845rjChWQqVRZr+vkHgUfANDaloIVHDrwJvpTpP7OWP4WMykm/NF/SBT4nmHWiavAG+WM6uCbPcxfKEdslECwWwNfHEKrAHDl+hYhJwcGYVpu7vEOOFbi6wwqwQ3AMzjsWs5i6eEDY9gssjnyhrDCAki9AO2Fos= Received: from DM6PR06CA0028.namprd06.prod.outlook.com (2603:10b6:5:120::41) by CH2PR12MB4857.namprd12.prod.outlook.com (2603:10b6:610:64::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.31; Sat, 22 Jul 2023 11:20:48 +0000 Received: from DM6NAM11FT075.eop-nam11.prod.protection.outlook.com (2603:10b6:5:120:cafe::2b) by DM6PR06CA0028.outlook.office365.com (2603:10b6:5:120::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30 via Frontend Transport; Sat, 22 Jul 2023 11:20:48 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT075.mail.protection.outlook.com (10.13.173.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:20:48 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:44 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 10/14] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests Date: Sat, 22 Jul 2023 16:49:05 +0530 Message-ID: <20230722111909.15166-11-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT075:EE_|CH2PR12MB4857:EE_ X-MS-Office365-Filtering-Correlation-Id: f6c0188d-c07d-4b61-7f37-08db8aa5b315 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2aES4z/uDbCjQF3M51FGr2Pnk6su+HFlfPFwzdtwvNkAwhPkD77mrJ/+0nMD1FBR+BI67zCAu6Vg6sF3CyUhccv0us7uM9zaSKk4k4jgnXNasEjksW22egJd1aDKNV25sZL8u+Vnd1XOH0Skx0onpwg0ibvpcgj4gQHj4h/MGxJ5wLkE8nW7vx3Tl6VquhEUoTmEXCidfJ9LuHs7ZHMT2MnFjBLTSHBmtXFs7JLlca2VWMd6DipuS9V8t022JU1RFGuN8GMkyMPXhHRrZmFuHm//cSNcZJ8WidbGhj1EGKhXa6ck6gRGd0ROzvVY2aAcqAzjs6BuTfMWaBeiIeCqVwTue4AmnZ6Gd2EZ0+t8pnGmLhK5aEjC6y3pcPTpF22xCkAa5kzoSx/tX05laU4mTwEpXWAjP/jzihpcqDymYhFb51sej8BaCIOgFGa6HADGDs9tzu0kjhWPHa7vTVlRB12J9HyH/Xi0T6BmXYViuR0fn7dW7Fzfrh3cwJ0cbE7zL1jmv0jOIqWqTRtgLxnh3Ixm0SZotUgecy/Cw0M6JDnKQQ+KQswkq3WVkvk7//H4WMKO6Hiwvgt8KwScO9BnlrV4pSaO7NZbwG6SHYb86ktUG+8V7GLjq/KongplNNNeLhrkhFSHDrFROCy7ha6rOeP3ycEYMuJckuzu+EaWjE6AjcXdoW1+nTjuzkFr6cFrlcjE6YHR8BNw2sHS5KI2mz9QJv32gdfde02cS4vgY3GutUXVZ7ejzpuHfVq5Oo7TBJ/ETx7exWb9Z0QlufVhMw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199021)(82310400008)(40470700004)(46966006)(36840700001)(40460700003)(81166007)(40480700001)(356005)(36860700001)(36756003)(426003)(2616005)(47076005)(83380400001)(16526019)(8936002)(8676002)(5660300002)(110136005)(478600001)(54906003)(4326008)(316002)(70586007)(70206006)(41300700001)(26005)(186003)(336012)(1076003)(6666004)(7696005)(2906002)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:48.1043 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f6c0188d-c07d-4b61-7f37-08db8aa5b315 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT075.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4857 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772120645153356958 X-GMAIL-MSGID: 1772120645153356958 Secure TSC enabled guests should not write MSR_IA32_TSC(10H) register as the subsequent TSC value reads are undefined. MSR_IA32_TSC related accesses should not exit to the hypervisor for such guests. Accesses to MSR_IA32_TSC needs special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- arch/x86/kernel/sev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index d55562cd395d..2d42822fa01c 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1729,6 +1729,30 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) /* Is it a WRMSR? */ exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0; + /* + * TSC related accesses should not exit to the hypervisor when a + * guest is executing with SecureTSC enabled, so special handling + * is required for accesses of MSR_IA32_TSC: + * + * Writes: Writing to MSR_IA32_TSC can cause subsequent reads + * of the TSC to return undefined values, so ignore all + * writes. + * Reads: Reads of MSR_IA32_TSC should return the current TSC + * value, use the value returned by RDTSC. + */ + if (regs->cx == MSR_IA32_TSC && cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) { + u64 tsc; + + if (exit_info_1) + return ES_OK; + + tsc = rdtsc(); + regs->ax = UINT_MAX & tsc; + regs->dx = UINT_MAX & (tsc >> 32); + + return ES_OK; + } + ghcb_set_rcx(ghcb, regs->cx); if (exit_info_1) { ghcb_set_rax(ghcb, regs->ax); From patchwork Sat Jul 22 11:19:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124279 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp759189vqg; Sat, 22 Jul 2023 04:54:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlGmyojM8vEFSRXq3xVaHuJxEMt3ekSDif62yA4piscuI+76ToKHwQFs76VM2XYQn8pqNZyx X-Received: by 2002:a17:906:7386:b0:993:dd1d:824d with SMTP id f6-20020a170906738600b00993dd1d824dmr4555031ejl.19.1690026841356; Sat, 22 Jul 2023 04:54:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690026841; cv=pass; d=google.com; s=arc-20160816; b=A9c8wwHbrc59BAEGPkok+XwNp3QgS0GFSTefwAP2b7g7A+teYVhJJ4rj2JxOok8f5G LEk6LMYSoFIHfbPaAY8FyKB/uSYTSIR81TCnbSNyH2/+Rdpv7IqWA9shpq3hhJgodQWQ E1viewjmk4mG4taDG2Ld9jNIwJ1yo/iiqA/Nnwx1UKCGxhS44mjSLCkpMiCiki1wzIhT I6T0rhg0Taox88V+Q2oWOthYSwLLrVin+Xh6LdDXY+wAz8dZdLmkKZiXF6Gt/Sr75lyr IBVrt1guj6+1OSztz3vo+n6KmDhgr/RvtGkl3fARNy/W7dYv7Pkf1c0k+1IwH9mbNLRO uP+w== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=pP5EnYMWy6h0McrrcwvHxMH9bmi4Ie9BZNOR+xAO3B8=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=K8x8xJln2MJ+pLQzyGdbp7jFcslYFMhe2ijQs72R/2CAJ3I+F/Vfb8azaN+UsDra0S aWBm0Rj0AdKPFJIUJ1pGQtxHwtNNikjHw9jNst+28id/Htbpo1jDtwX3BSYKNHlDvaMB ZpEw4D43CG6fA6rY12VP5TEZJj6WjBi90i8mLk+sz7z9ig+57Wl5CVOMxc01ycP5HlcQ kpl+WHgBvVQWNXsfJF2/pAtsENr5uVBasHywNxCwk32I7MOIFs4nRFdn153zvNEtjLJf ywiQ5vJSMLMLJM2VGBZ2NVl4BkKqE4f4ogPvMq/M77c++rwtU2JrsFDqawhcjNMv7wmd w1vA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=gXwkSp7R; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ha7-20020a170906a88700b009931f44c716si3756842ejb.219.2023.07.22.04.53.36; Sat, 22 Jul 2023 04:54:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=gXwkSp7R; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229897AbjGVLWQ (ORCPT + 99 others); Sat, 22 Jul 2023 07:22:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjGVLWP (ORCPT ); Sat, 22 Jul 2023 07:22:15 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2045C3C39 for ; Sat, 22 Jul 2023 04:21:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HPihWtMzmXCIzUJHL4h8bhSaSZyW1kh6B0Ba7luAl0WzsTChXJ2NdNvSh9+Xha7ackbym6K/IgzaP65GPBw3Py5PffOt+rPJQEDmM2KRZ+OY9gI6N+56q6zd8CnUTx1AqZUtYocNVuy3fKGIEOXIzxQk5HSOCLsS0o2U2gNLlYLbEN7OyacaiDuPl74VRG3AyiLZ9NVm7rVQ/O1V9Y/w+xJkjW2mMHlbyB0fpi4VSMEXKNgl/Ux9mTIxHazprwdjKMyxHiWud16x6lHeYZgo2Fmu0ZUUhIQ1zvmvdAVWk9vpoz/JaQ3kpQiMy1Xp075i6sShStLX770cv0yDrhahOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pP5EnYMWy6h0McrrcwvHxMH9bmi4Ie9BZNOR+xAO3B8=; b=ds+DGYVXi1TP4/g8yhXktZmcPnd0wdvAbFb4rzeyC8PKJk94ljyxEUzqyVdqLaMrey36EHXoyPd0LHX7NN52GwHdX9zfQJJEMwkjvzoTxKRcxg1g+z8faNAeBz0N14tyaX/sfUxZtXOj7f89S8UD0LzV9uvpcLr1g3mBeQ4hDmmm4iaGvsjVF8blAww7/xEIWitfzuDvgMJ12LIVGTpVKLnx2IF84b075VQqpDr2cU3+XmcczA5gyrnSeZGahc9GRMSJ1TtvS6oRPCqP/2ZTXf3swZ2VC6scuwe9wxVhzhPonbfre/r5hcIxrxdM4Gpc+9R0NXUL/am43mLs7rTIEQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pP5EnYMWy6h0McrrcwvHxMH9bmi4Ie9BZNOR+xAO3B8=; b=gXwkSp7RP0CMh56VO7eFTOSRuu1bTFl5fQOx3DmPoOqHLGx3LZv+egzj3v8JNizYbYj/IjyRRfDHIu7C5sQXIb2wW2Zih94qgHa+iXYj/Hd+K69ti/X/MRxSQZh2WNGogrjMUh2pDssMYxDe/a/1rBoTkocHwVKDLIs4zIQFsoo= Received: from SJ0PR03CA0029.namprd03.prod.outlook.com (2603:10b6:a03:33a::34) by CY8PR12MB7243.namprd12.prod.outlook.com (2603:10b6:930:58::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30; Sat, 22 Jul 2023 11:20:52 +0000 Received: from DM6NAM11FT098.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:33a:cafe::3f) by SJ0PR03CA0029.outlook.office365.com (2603:10b6:a03:33a::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30 via Frontend Transport; Sat, 22 Jul 2023 11:20:51 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT098.mail.protection.outlook.com (10.13.173.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:51 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:47 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 11/14] x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests Date: Sat, 22 Jul 2023 16:49:06 +0530 Message-ID: <20230722111909.15166-12-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT098:EE_|CY8PR12MB7243:EE_ X-MS-Office365-Filtering-Correlation-Id: 7beb562b-4885-46ed-b9c0-08db8aa5b4df X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZOeUlzCm249OE9LenRiFmpFmg0+ySm8etvTBiSoj1pIXtzU1p/Lg2Jgze6bu+3kvJwYqOdiaV1YwQByBBjaR6FaIYZwLga91f7UtZM2wnqmHZ8+/DaVdzKmE71txHpXiqozsF6fwWFEWP+rtUupK5wwA/l+LsPkCEK4jeEbGjcG1NCLKiEWHl8e/cUpv7Ft0KhZrks/EKcroMHuUy3A9Tcu7dwyLxioIgr7J65JgwdRiC/FvaQoA32t8BJ4QQb6FQKex6MkaPklYitOPKoSnwkaMuHx5L4v5g7HiD/pBGgLD2sk8iSC9NjNi/E1CVRSMDF6dHwluI6tq0Ecg/pfTW4S+AuFiuSJ8TLZQGZfXj5XJne5833a5wyTJTseyMIdBMIwajvULsBu7GqZ22K9qAkVcFL62jyHqkJzK1Kma3+1D4BXPXNkOihCuEhFBjUz3Xf4Ngg7wRpLp/YT8pZucQ4x/o1VSDHNuOwcu249QPDd+ewWUzhEI8iHy7wpKns6XzG7nzNXkDEDeZeHn4c5BnvDCyQe58pIKsNf3fDK1UY5Qc1NVxfAw9g9AZr/rs05gmRYjamQDWCULep7qYNEt+TkqyEqw6oO7JBiaVoyK3kdUBiFABdVoTD8TNlmSilLgMx01oE0oBSdqfguJvpyyIfwSZmDLW2m5SZospRRuQJ2034KXF/S/1/ufCaqFw2VqRxRtykSakgh4AzrT6bUFafVSWlNxqYiIYQ53TgtJCGYV+5KcQzDV9VO47EgH9gX2lDnp5g+F4Xcr7RfJY9N7LQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(346002)(39860400002)(136003)(451199021)(82310400008)(46966006)(40470700004)(36840700001)(6666004)(7696005)(478600001)(110136005)(54906003)(16526019)(186003)(336012)(2616005)(1076003)(26005)(2906002)(316002)(41300700001)(70586007)(70206006)(4326008)(5660300002)(8676002)(8936002)(356005)(82740400003)(81166007)(36756003)(40460700003)(36860700001)(426003)(47076005)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:51.1073 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7beb562b-4885-46ed-b9c0-08db8aa5b4df X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT098.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7243 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772121585100271709 X-GMAIL-MSGID: 1772121585100271709 The hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, terminate guest execution. Signed-off-by: Nikunj A Dadhania --- arch/x86/kernel/sev-shared.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 3a5b0c9c4fcc..1c22025b298f 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -912,6 +912,13 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, bool rdtscp = (exit_code == SVM_EXIT_RDTSCP); enum es_result ret; + /* + * RDTSC and RDTSCP should not be intercepted when Secure TSC is + * enabled. Terminate the SNP guest when the interception is enabled. + */ + if (sev_status & MSR_AMD64_SNP_SECURE_TSC) + return ES_VMM_ERROR; + ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0); if (ret != ES_OK) return ret; From patchwork Sat Jul 22 11:19:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124282 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp759479vqg; Sat, 22 Jul 2023 04:54:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlHNNRL53TEJNoZA/BhripyWFaQWf1bXmeqdB9f/2sBzZSWbkQWIYqjgjtL336W2RvwbAlOA X-Received: by 2002:aa7:d145:0:b0:51e:5ec8:d2f7 with SMTP id r5-20020aa7d145000000b0051e5ec8d2f7mr3842820edo.30.1690026895801; Sat, 22 Jul 2023 04:54:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690026895; cv=pass; d=google.com; s=arc-20160816; b=qhScc/3FeoafdoWxzxLPxdapG4Xlkiicb6Q0vBnMlqjJhe5tSPD5IiUE/5MeR2//B8 imj54iWQ2SDWys97Le4RVfqFy/KpFQ/DDC1Q8S4SbZfkEk+7B8iuTxK9YTgE6f+kYXJi cJ9lOod+yG5BDExMpeo2F5WMV+3gERNjb7ZO57phUmYpJNfgYlEaOcvczh3haF5pH825 xwzIMTfeptxKOKpcycP091WQFI5VU6/o9SXTXl84CPgy9GZ2e3NVOTLYcpVIxbBUp3Ca G25GpYuqYM+GQ8zvyh8r8fEalHKtDavlh5uXVpAw5Q1E8oiTTmbQfDwya1VdBgvRG9rO rylg== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=O3s6XRk5sDMEp4erAgDEJbeHlmXvoaQdohpW0ELcNPc=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=iTaIS4ultE2uEokAi0UbMmx3QJ+yNTAm5uvMsU8gQzxS94koRV/ZHHyVfW3ThEpxoo tPXvV3iYXXNkTj5HV0D6ZqXiuO2xQVpOsgsmjfEWZ36yXIUrXC8Zkc6wS4sMpE8dvbfL eNGho6OoXZiFgAX6qQiW6xPsYbEmK62N9GaRb/vig/HwSnvo1ApgI4FzFTvR1UiFBkPy ho8oZH1rOUOZOilqy/Jc2z5myyoPQKejRkCf2tA14EXjPJhF54V7Cat42wM6b8YwJXiA WYvmCPozxg1pC0kVDcb0ghFzUDho5mqfUbeGlM1LYKJlpDn3vuXC5tZH46gtHqDo4U6h lxig== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=w6rcvI+W; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q18-20020a056402033200b0051deabf148esi3804018edw.638.2023.07.22.04.54.31; Sat, 22 Jul 2023 04:54:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=w6rcvI+W; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230014AbjGVLZ3 (ORCPT + 99 others); Sat, 22 Jul 2023 07:25:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230002AbjGVLZ2 (ORCPT ); Sat, 22 Jul 2023 07:25:28 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C956F3C0D for ; Sat, 22 Jul 2023 04:24:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ixpMxZ1x4xx3/VVhIspMwJssF2kVFgx3K19HkqmHluulRv7Eeo0aSpdsJbKjjB90dQeCm6olx/0/Btrx1Mb+3QkCcE28Sbay0Tn++UEx5419UloRhyjmSvsAd7rCJovCxDa/o7KIdZUh5icGMuI7aVLbFef8Mpf4yA+TsdBVDfiwdTRQWmHEEWKJm2ckocKzHQi11D/KvNqXEof9mrvNcVWxdusZ4SpH9gMlcn/TxT2wkHzQb/Cjz9Nxt3iOdLM+QfECC206a1pSZ4y01cvDwbzCnY5mij7sIq5xiB530LhRXyrlvNqH8+z+NqD4yCRTlG8hQ7n1hPKdPcAcQdIkmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=O3s6XRk5sDMEp4erAgDEJbeHlmXvoaQdohpW0ELcNPc=; b=HrSUtTd1ExjE+0eP8HBj53PFYKI46WAGZxmP1WoP8GEENtY9rGaWQUZeiasWnv9urSyJxpVvgnCaPHuvJqVAZqrwT/hf9j/PRSOUj+ki/P/8JAO6MU/sStOe5cS9KVMNdMfdnwXjmww/RJ09cytj6mp/GzxTKDSzFWHZzQbb2tjTg+bMJ9XBGkXVtTNCr7dMyKfTk9mptE46UU5y1BjVNartoCUbwAGizWyXRSgB/9gKVbGKZAAFVhaSyOQUYw09XOvJTpmUX0hWZ4QCMmypTWR8g+CAo1xk+/bYTU22fVkUOIl8Bhs7DmfEsahPcIG4p2wPsit1KIHaTBIMRC2yeg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=O3s6XRk5sDMEp4erAgDEJbeHlmXvoaQdohpW0ELcNPc=; b=w6rcvI+WKW4V0+E72mjwmzxvU8kMaBluT/CH7VwMH1aYAoaIMHu6JGa2yvRYriA1Ag4QSrVqOKLGc1vgJeZcQkWZYAPW/DwQH/llA/DiWEQRTiEv59oPkLtn9fVlTDs9pHaw1yEbrOPJvt4EiDsJnI8vXXDTMHOdv0hbn5Z6aWg= Received: from SJ0PR03CA0026.namprd03.prod.outlook.com (2603:10b6:a03:33a::31) by DM4PR12MB7645.namprd12.prod.outlook.com (2603:10b6:8:107::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.30; Sat, 22 Jul 2023 11:20:56 +0000 Received: from DM6NAM11FT098.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:33a:cafe::c4) by SJ0PR03CA0026.outlook.office365.com (2603:10b6:a03:33a::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.24 via Frontend Transport; Sat, 22 Jul 2023 11:20:55 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT098.mail.protection.outlook.com (10.13.173.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:55 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:50 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 12/14] x86/kvmclock: Skip kvmclock when Secure TSC is available Date: Sat, 22 Jul 2023 16:49:07 +0530 Message-ID: <20230722111909.15166-13-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT098:EE_|DM4PR12MB7645:EE_ X-MS-Office365-Filtering-Correlation-Id: 48bec2ad-b712-46fc-c8d6-08db8aa5b784 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ahqkD93P1iqWfg/HD94E4ixuvZyvmqXa8prwRYE9zgoXWyuexk5CCZ95sMsbfkiB2+sYJ6QJQDAyI5L5RSmon7xTKGAvwJg6LKlWArKrYlbN3Zo1iR+1YWVizMOqBoqog3FwjUFAoAj6eMxbUXiUOXO2ZS0wDz+GduoDaO/P3aNLm0aO5TvUkVIWEO2KUNlMDNVBRYo6bUWhpCt6yrP0JpW/j0Ez2Ra68wtsvU/OxX/7b0HpIftq8SEJGu8ftFA5dlmvYz2fn3hlK11p5Vj/Co2V3p/7oT1EnEe1Mu5/YtbreC00xnqEf+8AM+cIRVOkGvvANMgPj4sXBBdvUQN3eGjKeuCPKzhdpakogJCMyg59I3l4zS+m+AOTPfEPf+YTZx6fDbyWA3rDuH+suRa9MnhAJ7a9ujX4JZ2UCuf3H3BpTVm1o3Kpm9aQFDiRJFf2tB4y1iJIYWTC0Byz8xjvIfw83LBFmVcOKZPoVI4JO0tg0/qbmwuPwU4FphKSWem5BCgMLv9vd4C+muaDITXtJEYzkLJHP8uBCo9vWrqWRroYWloOfl2cRPERr/oPPtspmX8I5kPbBdc/Zcon+sAvhHA0LkZTpblF3w9H0ET999wYvzacUy64vtnX7NsGRjCc7AotOimTzaPUDxiG9nm9sr8+tOA4fL9OrxQh3XmXCeIlZGEo5EkmddrsoU4tIoXETWPo4ogwOwe3kSSb+1jFVxMjNlsA2TSRMu56A4JyosZMmAAoXo2UPuHvj2uzYZ+ocdv/Bx3JQHI/3I7CB5FECg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(39860400002)(346002)(376002)(136003)(82310400008)(451199021)(36840700001)(40470700004)(46966006)(4744005)(47076005)(1076003)(41300700001)(426003)(186003)(336012)(83380400001)(478600001)(26005)(6666004)(40480700001)(2616005)(40460700003)(2906002)(16526019)(7696005)(36756003)(5660300002)(70586007)(70206006)(4326008)(81166007)(356005)(82740400003)(8676002)(8936002)(316002)(36860700001)(110136005)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:55.5601 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 48bec2ad-b712-46fc-c8d6-08db8aa5b784 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT098.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7645 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772121642150650685 X-GMAIL-MSGID: 1772121642150650685 For AMD SNP guests having Secure TSC enabled, skip using the kvmclock. The guest kernel will fallback and use Secure TSC based clocksource. Signed-off-by: Nikunj A Dadhania --- arch/x86/kernel/kvmclock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 0f35d44c56fe..1be342064851 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -288,7 +288,7 @@ void __init kvmclock_init(void) { u8 flags; - if (!kvm_para_available() || !kvmclock) + if (!kvm_para_available() || !kvmclock || cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) return; if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) { From patchwork Sat Jul 22 11:19:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124278 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp758799vqg; Sat, 22 Jul 2023 04:52:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlF4IgCqMvtx86QbuG0M0GC+QtFTO5jYhb/FuDb2BTUS799sg6VkESyweaYtBvulwk8lnYYX X-Received: by 2002:aa7:d043:0:b0:522:1d9e:221 with SMTP id n3-20020aa7d043000000b005221d9e0221mr661824edo.38.1690026774318; Sat, 22 Jul 2023 04:52:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690026774; cv=pass; d=google.com; s=arc-20160816; b=mG6r5u1tB1XMlcNUunOGaywZ+WTqzoQLEplvBewWuFtsj07y83Z0V/t4sIu1vNAD8o fffJCOJ0oC20+aGii8jA0hulMRBPy7zGposaNS1icBsmsRTy6aCLeFvsbmhLGydlY8Sa tW9JdLnkLdiB4f2wPjN7tB2VWdK2Phs8G2cg1Q5JUrCKNairT5cLkTtF3Pt+lzzjTPVB E/r3xVOU36AB6xGf5K4Q1qt+Cak+gN5zV+8byWJSudaiF7tTApUJ2pPMEfH1LsnB/jnT t4kgBumzFy5wwR5ARE+blRtSPKCcqg5ewJR2tOZLqsCQNInAC4BAymYxxLFHiYFXfATs +8Qg== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=6y+5SDDPpRulwOlmXO3VSI8qEK9hnC3NPzAIDB6HvJw=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=vPLmPgkxk5S6RkPYH3i3wTjyq1gc1yASMVCZf2787nvghEvOppNuWm7XM7o6ZxfQUo DZ6Y/BV2QSWOSdOl7N9WjX3RQEm+4xweSSjuEEFEudrlj9GhaNy6KgQRBbkUcM5qf7Mm G3W+ZUHmGucFIW8BEV7wWpnE+uLQ+SOJwfwrI7Fj+lhGOIbATs7Vt+sibRXVegDqOzSm 15IoJXPK4CAQ+iy/RhJNWB3h8jfck2xriNjNBKXFJ3pWjghyZMext1+p9uiBafC8iMiB P5BsaFNRVzgVfv2wnioxW52wqsI1LpAX4YdUy4wyHdj6sDtXbOGUlDhX6ASB0BXVQEML wAmA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=yQOmRqFA; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d16-20020aa7ce10000000b005221fc3b76bsi19761edv.126.2023.07.22.04.52.30; Sat, 22 Jul 2023 04:52:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=yQOmRqFA; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229899AbjGVLWY (ORCPT + 99 others); Sat, 22 Jul 2023 07:22:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjGVLWW (ORCPT ); Sat, 22 Jul 2023 07:22:22 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2060d.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::60d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E456358C for ; Sat, 22 Jul 2023 04:21:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Wkq205bdvY1MKoGm9wEAjdewqEUWY+7flbr2WCvbw2rjjMqV65FmyUr7OIYu2Z4ghzmoVzgIQRE7TrIgVruv5HnMgmRVa5DkX6yqVWMT4wuWRN55Of6ZIVXXd2s7oPcY6jUp7XI6+1oAIJjJ6kk95q0tO/5n1UZ8PiWG0b1qOEhsNQOXjfvMoVavcrlcWjL0PykfMtl0gTAUo73KAgUXMUPP8Ld8yuW96RL5SgLsM+Njas79no1/2CQbdy2BvNfN6gMcDrwGDHN5opWA2gqAPFzf4emxCZUH0Cf1XEddsf3GzVyrtvOLAYceLJAQXTnvDhyA5IQUvTGHWBw6/JtKXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=6y+5SDDPpRulwOlmXO3VSI8qEK9hnC3NPzAIDB6HvJw=; b=hilqRGV1CApi7RPpGmyqTnGOL69XwaKZLcOIHZ4CRuV87Ijiif17V2l8d49dacwN7IlJNLFAFClrthrpSLox+vOv7szoOr7EytEs9dA56yqb5xfApFirkmH3wohUUenjoErMSeWl6tVx3F5ThbkClWyj7J93E/lbRrqNO/sAX6XrHafLqfuDDHTMHjG3kkoBYNBD7hozNtTfNrt899mTqh2Ay/1s98fPH5BcoKiTzHGJnxM+zYGA93Xex0B9tRBQ8au4XdCeulFa5YTA2UqNlK0tvxBlHUpRjvNl0baSc8H8LQLMX/OLMHnvBnVgPyzlulapvFqklAV72jCiwyIVcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6y+5SDDPpRulwOlmXO3VSI8qEK9hnC3NPzAIDB6HvJw=; b=yQOmRqFACqKQISTB260AUXhubL8u6BhlkPqoPS/Z1cjRsiN1AO/ddLD+xvLvMQf6jXGF1h3iVClZKpoUzToBmF8BdybP82r0Eg5N2iZQiN5EygnSYwPiMl8SMcYOHiHvQov770AEKf0rz+kpEdVrnnFpG7+GBhu+cuxwdTZQHXU= Received: from SJ0PR03CA0018.namprd03.prod.outlook.com (2603:10b6:a03:33a::23) by MW5PR12MB5622.namprd12.prod.outlook.com (2603:10b6:303:198::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.25; Sat, 22 Jul 2023 11:20:57 +0000 Received: from DM6NAM11FT098.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:33a:cafe::a8) by SJ0PR03CA0018.outlook.office365.com (2603:10b6:a03:33a::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT098.mail.protection.outlook.com (10.13.173.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:20:57 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:54 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 13/14] x86/tsc: Mark Secure TSC as reliable clocksource Date: Sat, 22 Jul 2023 16:49:08 +0530 Message-ID: <20230722111909.15166-14-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT098:EE_|MW5PR12MB5622:EE_ X-MS-Office365-Filtering-Correlation-Id: b087bf29-3428-4239-b709-08db8aa5b89b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ejrI7jBOmnZv2Mn4S7g8UpU3s9FNf22WxToI1niDcr846HoY+mNuB/efz78gL4IMGJ+H1A8Zp2YWjyajAsy+DKSrx3ywPofWCysKFXJXcBollVm4NqRzHk4kOdfzKFo8MdF22GINMbpdYPoouHx/got+KLvmyPEQziE2qR5vgwcJAxIP+XqPZ0Aeodlghuzf4AQSz7cSGQHQEQEO+qbLnV439GTsFvuhvinmvZTjKHrJCRyFacDXVNF3+1jMjXWc4hDwtsNnV/hxi5nnZK9j+03ri9M8llO1Scxp8knKaJY7sjEHWY/Eit9Cj6S+hGgVA7+uHG6+gt6SWvYg+qinDntdrOazF+Fo+7WGF0wrbg8/wF52iwowcgW6vhPSznRg7TP6NQiLHYb4Y95LVQS+ddM6IG3BBJCWQJct37oX8SDZQ8UKb3Hdesskx0Tzh/tm1ig20RdDzaUV0xR+c10meLNBOSKs31ObVdNfXSiBoIRU/d6ct5i5HnIB6/OMzJbsnC5xJZyBTUEg/31fxjLLOuAxCoPWWzfkkO+7PDKwG7et4YWgPrWpmD2kYV3hG5GcfebC1y6qWRd3OEuhWoCbJ3WqCI1RzqcBxr64NYNrBaGuCbHXGtIbfrCk3AohrAHCcSMsGJz+ff9AcUly87UQ7sd3soUTiSnsXnAQXIUBkqhuhD3ogRIcaphkEWrZLumIKGMaGvSWZ53m4ET++UlBiXJmnwgELO4ymN1PKo/bahs88lthfH803Vqjt/2AhBUxrEnd4Z98Fsd5IRKCtCqVPg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(396003)(346002)(376002)(82310400008)(451199021)(46966006)(40470700004)(36840700001)(40460700003)(83380400001)(2616005)(426003)(2906002)(4744005)(47076005)(36860700001)(82740400003)(356005)(81166007)(40480700001)(26005)(70206006)(70586007)(4326008)(316002)(5660300002)(186003)(16526019)(336012)(1076003)(6666004)(41300700001)(7696005)(110136005)(54906003)(478600001)(8936002)(36756003)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:20:57.3725 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b087bf29-3428-4239-b709-08db8aa5b89b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT098.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5622 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772121514774460040 X-GMAIL-MSGID: 1772121514774460040 AMD SNP guests may have Secure TSC feature enabled. Secure TSC as clocksource is wrongly marked as unstable, mark Secure TSC as reliable. Signed-off-by: Nikunj A Dadhania --- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 344698852146..5f1e2b51ae3b 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -1221,7 +1221,7 @@ static void __init check_system_tsc_reliable(void) tsc_clocksource_reliable = 1; } #endif - if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) + if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE) || cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) tsc_clocksource_reliable = 1; /* From patchwork Sat Jul 22 11:19:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 124274 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp749120vqg; Sat, 22 Jul 2023 04:26:20 -0700 (PDT) X-Google-Smtp-Source: APBJJlElcLhakN95bTGZ9R4kq8+LLkF3dFIG4/4NC+sMgRYLST8MBpLCgEFmolIiYYhh5N/rkmbJ X-Received: by 2002:a05:6402:516c:b0:521:942a:f2b7 with SMTP id d12-20020a056402516c00b00521942af2b7mr3574743ede.28.1690025179771; Sat, 22 Jul 2023 04:26:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690025179; cv=pass; d=google.com; s=arc-20160816; b=hP/lNBQwvGk+ZMRjg5oxkSLAfelApS8f0Tp9YU4XFf2nfLwlFZ/+p7NHfy1KrsoXpq 6cLadsWbmbs7CXE/x6C86LtL+RaNue+aB+fCeE100SjYEDd6q087eDs/YeoJqWtBXsls d3+goSqN5c7Vbj/VZxlde47CTu+2SOePo9f59y9oj95ldH7nLRwU26Nrpg1KE/c4lE7W ZJZrtutDnuvwn8/LURqw9KGSertgZBQDpXY+MEi7wR0FI3K0xvRfzR3flss1zeAjtfdH kJPsFQ18O7BezwX9RXklVbpLk1d/OVPQoviwZfmL5h3hRTxudQN8hNzi+kDuEEYFFPR0 TyAA== ARC-Message-Signature: i=2; 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 :dkim-signature; bh=5NiJq6xAI78ib3LcOR1IpI4AF1pAQl6aTlJruCyHLaI=; fh=Tmgh4sO3JYBnv5ss1qimzxXGWNntS96RmnHsBHYYV/U=; b=i9bZREfNHnKUrAGVHuyvll3jc1IbNk3lX2xbE4QsAPOhWnF6AMA354wMPdAs0zt3ab jYkXKT3XqeIz8oUjoeO4lTB1rgumb1tN7MAB3D9jW5t0zAI6OIEFjVMVghng+8rOdsvz xYYlAz9TikZYacp5BLhb4c3sHtOETLg6l53h8gEbpEJ+ZH6M5x2KCcBWBGQsgL5H7axW KO+WDP7ooxQSZ61E70lHAqHQClBANFCUmaBMQ30vZB0hW32eKnE8g044SlLavc6lq5K7 voNFqTsbtSNjy+tFrlIL3bOCyy1rkSKCmVPnJkapAOZYyWGxXPlhzciP6b+UQHG6plJC 04uQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=nJqDKg39; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v17-20020aa7d651000000b005218c27b18csi3637299edr.498.2023.07.22.04.25.55; Sat, 22 Jul 2023 04:26:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=nJqDKg39; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229898AbjGVLXv (ORCPT + 99 others); Sat, 22 Jul 2023 07:23:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229546AbjGVLXu (ORCPT ); Sat, 22 Jul 2023 07:23:50 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2060f.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e83::60f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 047CC3C3E for ; Sat, 22 Jul 2023 04:23:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P6gR72N82FUDXActhsdTo5awGcTwhUpzbW8uf4BZQiYNz6IuM51hQwuHWuqifx3QdRl5o9lsQa3JPjhxeRy7nIQDQcXEict0eP1KcxmnFGxQfAzcHtClchpyrljXe5y159uJEcSF4EP+Ck4eUJ7zM+T4AlrurGrPZwz7ztsRL+BeJThmS3CiYaUhmFRdMt9flzJxb4sMqKKpaoiVbhK8NnBoV21wJ6kjZl1oc+TwPprVIuYQ62MR6RI6Upge2KCrEbMtjBDLJNdcXGq2FEunrKzMf/W0qFA6lyDm6FQ5nXe2lG4DynpLo0n32nqxDzPVdArT4HoVKWa4W0j2jwscOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5NiJq6xAI78ib3LcOR1IpI4AF1pAQl6aTlJruCyHLaI=; b=R3R/qH41lUYf0kJJDNPsLlnFRNvDGdFZKgbboT+B8NUM8U18ESTo4ClslUCwhbI5EhJQQhuB/CIVk2QDi+EIUnCtKdtUa1dvE+Rs0ghb1I4i9jRv+4dfFOCHeRkFJNmWwo+2PqmpPSksQcQSOkZVKJZBqT17G9Nx7sLTTX3d19xb5ETfpVlaiKO8JExyoDtWdHab05HYv7G/iYPj+gZFFpb882r2sWDlSCD8PrHiLH3Y6+auLwGrlYYJXNiG+zDBfEaXkuDLx/u/dp/UIMUwHrsIWghkI7KK+n+aducdEnLJBCqQ1KvYuL3AOxQyNol5pTkQUW0BW3+YbnKz+SNqIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5NiJq6xAI78ib3LcOR1IpI4AF1pAQl6aTlJruCyHLaI=; b=nJqDKg39F9iuPF1VpzkUUuDrm+RZNtVcG9xtnRezr48QIt58lX/Eg0sqOvelwl3L5TLPIHCtmdQWAIJj3eVULToY/A5Di69QRLlgKC9Eyy6LBDq1WZqc8q/3IeJgsFr0mZwKyY5dCaWahkciDDFhaceLb6JNrN45+qJc2BfgvTk= Received: from DS7PR03CA0029.namprd03.prod.outlook.com (2603:10b6:5:3b8::34) by CH0PR12MB5283.namprd12.prod.outlook.com (2603:10b6:610:d6::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.29; Sat, 22 Jul 2023 11:21:00 +0000 Received: from DM6NAM11FT115.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b8:cafe::22) by DS7PR03CA0029.outlook.office365.com (2603:10b6:5:3b8::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.28 via Frontend Transport; Sat, 22 Jul 2023 11:21:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT115.mail.protection.outlook.com (10.13.173.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6631.22 via Frontend Transport; Sat, 22 Jul 2023 11:21:00 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 22 Jul 2023 06:20:57 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , Subject: [PATCH v3 14/14] x86/sev: Enable Secure TSC for SNP guests Date: Sat, 22 Jul 2023 16:49:09 +0530 Message-ID: <20230722111909.15166-15-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230722111909.15166-1-nikunj@amd.com> References: <20230722111909.15166-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT115:EE_|CH0PR12MB5283:EE_ X-MS-Office365-Filtering-Correlation-Id: 31318545-2a7c-47a8-0907-08db8aa5ba90 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ANsmhHXYNyfl9rr49DxDA+1IvVZ9SV3m7tJ1R5U4oLJiBFQ2ROAgxrrXM2NXDKVsmcPu3uKspdQn6Os+gOtUe64oZayeCtuCJyN1la7xWhoCqnHQTvu6fws3kv5Pt5Mj3TTEwuw2pCTv1CfFg1lmYRfrLM66g0pa4N6q4Wcoyez6+SSraG5iykGBk6W/LmoahcWN1Nd+4dW8D3k8VzlffeULJxwdCV9o3Bl9Lb40n63MygxN73fLrcvdGsSt8z9HK1PHwfpTrkvAFNV0GhJihQl/IF5JAyqqDS1KwlU7MgdiBW0q/NT3LiPndoKfNMGUuoDEu5XvyhSefJ5RAlLEeDfWU3C44nIvmQbn0H23aMFQDPJq4uZgQ9LA+BM0UzgfAVXDclISxgq43nWQqOUd64gM54UdLx1j05qX0UnigVS8Ui7LHjQVOqUGotV/whcnVCEM5vU2iCPVnOGE4bE6kZ7ipPZQtGe8EhH59lgWfRcb+1aL8M5PJkJnGiWGMXEBZuG9ItWI9FY1cRh+XfN0HXtqmphzrBkS1JLKYpJW2VM2631mG1E+dZg+4UApuA6Yz2I/gKJTdihRPPvaauYqCWKR6S/m3gzDJDE9shzZTt0lCqCBsCIFp+YIy45KO6dpkd9v1l0wz6upoQGEhJFjRAS3vBNMrIdMp0MiqaoWJIfpvpKu/G8/KOdIAI3tigM3qHYOSCkArW2LKRA8TfPXOkmtg7zz2t/ng6lDAmctNWaaCuwd/VcAceV27xfNJ9SfnNIGEW2q2WC90geE+c1nyQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(396003)(346002)(39860400002)(136003)(451199021)(82310400008)(40470700004)(36840700001)(46966006)(40460700003)(356005)(40480700001)(81166007)(36860700001)(36756003)(2616005)(426003)(47076005)(83380400001)(16526019)(8676002)(8936002)(5660300002)(110136005)(54906003)(478600001)(4326008)(316002)(70206006)(70586007)(41300700001)(26005)(186003)(336012)(1076003)(7696005)(6666004)(2906002)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2023 11:21:00.6514 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 31318545-2a7c-47a8-0907-08db8aa5ba90 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT115.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5283 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772119843089228328 X-GMAIL-MSGID: 1772119843089228328 Now that all the required plumbing is done for enabling SNP Secure TSC feature, add Secure TSC to snp features present list. The CC_ATTR_GUEST_SECURE_TSC can be used by the guest to query whether the SNP guest has Secure TSC feature active. Signed-off-by: Nikunj A Dadhania --- arch/x86/boot/compressed/sev.c | 2 +- arch/x86/mm/mem_encrypt.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 014b89c89088..11f951caf2de 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -313,7 +313,7 @@ static void enforce_vmpl0(void) * by the guest kernel. As and when a new feature is implemented in the * guest kernel, a corresponding bit should be added to the mask. */ -#define SNP_FEATURES_PRESENT (0) +#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_SECURE_TSC) void snp_check_features(void) { diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 01abecc9a774..26608b9f2ca7 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -69,8 +69,14 @@ static void print_mem_encrypt_feature_info(void) pr_cont(" SEV-ES"); /* Secure Nested Paging */ - if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) - pr_cont(" SEV-SNP"); + if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + pr_cont(" SEV-SNP\n"); + pr_cont("SNP Features active: "); + + /* SNP Secure TSC */ + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) + pr_cont(" SECURE-TSC"); + } pr_cont("\n"); }