From patchwork Sun Mar 26 14:46:51 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: 75098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp983128vqo; Sun, 26 Mar 2023 08:03:54 -0700 (PDT) X-Google-Smtp-Source: AKy350Zs9R9IcZZ8QK5+C8SionXgQf3qPwrfF04LeZhK33B8eb72VI1vh628+4PPmEDCBJH/fipH X-Received: by 2002:aa7:d584:0:b0:4fa:ba60:8188 with SMTP id r4-20020aa7d584000000b004faba608188mr9105668edq.8.1679843034132; Sun, 26 Mar 2023 08:03:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679843034; cv=pass; d=google.com; s=arc-20160816; b=BJwA8frUfUBOOdVAY2dk2kjJPTCWe9mLH65QLZzD9vudaFvqJJwqHfj1YB6WN9vZbP E2qfxABerkzIwsb4/5yqJVRDj4t4Abqps9UAvaxS4+gUiSbWOKk234q+y97X+TkTA+CK Pgzo5yKezFaI457c7N1NcSd9szNalBnjwKmVnqhWPymrnHO4aPzfeROHigQB6H5Oy1/e KBo+waZI0mlPu+upceOFNMN+LMncxpNbzGzjnjvEzue3mUZvq98pgwhCp74aGf2X/4vZ CdvkmbiSN2w/2XAQVlPJ+A9/qZX5XYtgDnMtHwGIKqzaBNnhlOr2Eao3f8Az9MkZKrwF kuQg== 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=7mZRboiz92GLRRtXzyURdQDY1BRyRzr9vrBUiKUV6iE=; b=I8/XYIXRdMKEBgXua7zUl91vos3r07/ReZF5pEXBeISeYnRBQDXPccIy9OSX7LSNtJ uaucKtfXWhHODsG21CandVE2bcH4JtHHSGg7Ka0f94E+73/6XrtQ4aN/kBJy9mSkgB7l VovNeQ0xtBo26tlti0Fa60FFwcSJtCJetQoBWPcs9VbpsTyEv5HWkXN/dgAD5nNJUqDU gUlb1/eyhrW+KubXaR9VgeNZfMlX6X3bYnO6k+oA4LiDEnIKFAZw2EmIwQPF6I7TDgbU KmvhMtI2qiXSRJz9qwJNomk9+bLUJFf0qjm4zPISzlw7crW/2ksbHshjWI6zZVJcVdZS HsuQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZxjvrEJ6; 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 i20-20020a50fc14000000b004fa5cf7608esi24495160edr.258.2023.03.26.08.03.30; Sun, 26 Mar 2023 08:03: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=ZxjvrEJ6; 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 S232159AbjCZOro (ORCPT + 99 others); Sun, 26 Mar 2023 10:47:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232043AbjCZOrk (ORCPT ); Sun, 26 Mar 2023 10:47:40 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2040.outbound.protection.outlook.com [40.107.237.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1CA446A3 for ; Sun, 26 Mar 2023 07:47:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cK8g1qNjgRsxlamRGaUU6vAsffgE5DEl8tXGaZllYrSztYzCajeYnq/glKgGytbuHrLuDUDvKSVJ4pMAXRD8b0d+1dAf+bhzh5tNWHGv1KhgWv54cx/YSAkIGoWv5J8T+q5jVMoncI8bZ2G5QYm/kxY5GXgnll5rCPY8HsgEuUsfaShgxGjZmUVpC/6yXPUXLG0br7xazhN5Dz0KNhcG1vpttDtNB0ayC1DUb24yHqO+eX4PppegqUFEiM9JNhTvk3q3r7QXrdIQXdxabJtb/RUqOh2t6dcP/8Z+ECfBjh+VqaOWXTgvWBl34el6QZIKJIbszT8xsW66bIrnCwNoHQ== 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=7mZRboiz92GLRRtXzyURdQDY1BRyRzr9vrBUiKUV6iE=; b=VlaA7CgKMM4FYvrgYu1bzkvTSx9q/Xb8LPCkyKX8nXfn2+wMJNn8TupS4aAgVIH6CQ/teGbGLQ4mciqGhjjZXNXRfkGnMpIbdxO5Q6Ay1QexWpwPTfERTkiPRLHvT5bWuwpVcRnJlfGJp4yTP+WPLNi4AiwL3mjq6ZhIkAPTw5G1ZrZiGz6UiZE6po6a+wf4rZXsVUvFtaEIk+In0UfUv3jdsdc+2tvr733FcDiZHs+I5l4uur1fogCfpdkTgEFDyoam9ito/7sMmGrfiEfpt03NodS6Ut/VWeVnS/112wPRRE+ynUiILr3X7GUnhynd+PHZZ5kJizrgvCyJcnpLMw== 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=7mZRboiz92GLRRtXzyURdQDY1BRyRzr9vrBUiKUV6iE=; b=ZxjvrEJ6/o52YlSoPICK8Iv6iI2/P8GuSlw4r0HylEdoNFJ77i7srCI5y+pAqj+n/V/pycoGOaZdbrPIgq28yE+8ekrHhkD0AfU86JOfqqAzBI2nUIYg7xpXNMe34Q40mhpedPzTRRPSb2SmWIRHqPRDWx01+PA4JRXHjnxZREA= Received: from MN2PR02CA0021.namprd02.prod.outlook.com (2603:10b6:208:fc::34) by PH8PR12MB8608.namprd12.prod.outlook.com (2603:10b6:510:1bc::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sun, 26 Mar 2023 14:47:26 +0000 Received: from BL02EPF000100D3.namprd05.prod.outlook.com (2603:10b6:208:fc:cafe::3b) by MN2PR02CA0021.outlook.office365.com (2603:10b6:208:fc::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Sun, 26 Mar 2023 14:47: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 BL02EPF000100D3.mail.protection.outlook.com (10.167.241.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47: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.2375.34; Sun, 26 Mar 2023 09:47:22 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 01/11] virt: sev-guest: Use AES GCM crypto library Date: Sun, 26 Mar 2023 20:16:51 +0530 Message-ID: <20230326144701.3039598-2-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D3:EE_|PH8PR12MB8608:EE_ X-MS-Office365-Filtering-Correlation-Id: b73f654c-d0e4-48dd-57c1-08db2e090412 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Rw/bab2ByTDd5IA/AwY4ur7Dn4X2XJER5Tnmw///rAYjKWo/cSuSPAnUypRR3J/14/zpoFpM2DpjnYZDAntcOGdE9/TILYh+X8XIt0JPWi8G/H5EzbNJMUgcg58dcw5+aAgJpJnvPXmp41m5CDrnNdfeHojleZNzWe3xXGxJw08w+Us91HK3YB67hAATVrdxNDwh0nUrjjOU44Zpm1dVU+KidP9wqV/e/tYQgutDE3WR3FdqGbbhIoKTsKFI3n3PJAcyhaznHUVukr5Cq8qI/p+S2JsAKL5jMMkrgH+QCr2AeIEN7t4QcI4PRABH19QwcXZ49ZGBMUYEOPHIokd0tvswRqdmnoe9s8XqHBFwhFuAFrK1slXR3vIVAV8jQ0rVT7GEd9giKYVp7AbgKIr1iuyy2nlq8YWSg37BItaV7ASczZRxe4p+S1cOa4nLyo2BTUU2cNdUXIvntzAwcShiHarNwyF7lEnKzKl0NzYHyD6pVm6FSGCsEceKbg1PSfHZ+3Ly8sUiijFQGbvJTYyJpASAvYscnwlY5vZ+PIFXQCuWjcCptRcMbD76gYTTcyh7QMjH7Z6yQzTvX8EYtmXij/dvBODzAgqxCXE6h/OUYMDOYChB4b6dRxVSIooLjhs0Q79BUSGMmpkXuDNhkWXePzhkBkpVjJbuk8GBXfG2dKQs0AuYDSvCBprjOKXsysxQIpTx8Dk6+oKCMgVd3rXPYZ6m5041Y8Cb/CMp/KLjfm8= 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)(136003)(39860400002)(376002)(346002)(396003)(451199021)(36840700001)(40470700004)(46966006)(16526019)(186003)(6666004)(2906002)(26005)(1076003)(83380400001)(40480700001)(356005)(82310400005)(36756003)(36860700001)(82740400003)(81166007)(336012)(40460700003)(426003)(2616005)(47076005)(41300700001)(30864003)(8936002)(5660300002)(7696005)(54906003)(70206006)(110136005)(70586007)(316002)(478600001)(4326008)(8676002)(966005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:26.0482 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b73f654c-d0e4-48dd-57c1-08db2e090412 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: BL02EPF000100D3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB8608 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761443089340804978?= X-GMAIL-MSGID: =?utf-8?q?1761443089340804978?= SEV-SNP guests with SecureTSC enabled need to send a TSC_INFO SNP Guest message to the AMD security processor before the smpboot phase starts. Details from the TSC_INFO response have to be programmed in the VMSA before the secondary CPUs are brought up. Start using the AES GCM library implementation as the crypto API is not available yet this early. Link: https://lore.kernel.org/all/20221103192259.2229-1-ardb@kernel.org CC: Ard Biesheuvel Signed-off-by: Nikunj A Dadhania --- 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 46f1a8d558b0..57af908bafba 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; struct snp_guest_msg *request, *response; struct snp_secrets_page_layout *layout; struct snp_req_data input; @@ -60,6 +51,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}; @@ -144,132 +144,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->response; struct snp_guest_msg *req = snp_dev->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); @@ -287,11 +214,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, @@ -318,7 +245,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, __u64 *fw_err) @@ -446,7 +373,6 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in 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; @@ -464,7 +390,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; @@ -485,7 +411,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; @@ -502,7 +427,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; @@ -527,7 +452,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; @@ -565,7 +489,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; @@ -777,8 +701,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; @@ -793,11 +717,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: @@ -816,7 +742,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 Sun Mar 26 14:46:52 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: 75088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976011vqo; Sun, 26 Mar 2023 07:49:17 -0700 (PDT) X-Google-Smtp-Source: AKy350aoW2pc1VEe4/hFoHpshYAz5zB7u+y1o5WnU69JFYKH5pKunYVCJnb6VQDZnuk2pijik1ly X-Received: by 2002:a17:902:daca:b0:19f:3d59:e0ac with SMTP id q10-20020a170902daca00b0019f3d59e0acmr10967169plx.44.1679842157093; Sun, 26 Mar 2023 07:49:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842157; cv=pass; d=google.com; s=arc-20160816; b=hAITt7rWkJUui+dTS83LYLvV7cBLb4eWVoC6Q1LcWH3hhDItuSBOR52MQ+Xq13cmTh SXJQ+SbMhzzLLWMSGx5spcXV3dEy82W5mZyrUewwpBysteSbUD0K5n981w14HOpUjxSE LAYuqkBSglRzNKuIYokr74gOQe0V7PkQp2OL8leInwkoyrSNl8nFhUm51h5rhw7/B3bM xqgUeCKBb0pNKWw7Qliis/97t/zerYqFS2DpEjW5x8L7DKjALD7sK+jZW2LV+cr8o6lZ xeZPsM9e1qmFQUEg2JqRBMNUgxvHLm4wuYxSFeEwAKnWkdC/VYbncjXak4M6xc9QRgBU 4tlw== 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=6l9l4MSPMGVG3GgDFqDSeKfsbvGphG5pxVBc850DdwE=; b=Oa07XQVlaWOTUeWE2vCKg+CFtAyW5F+lJ+u2EufHCICMOlVQKIXVYyFc8oTRMByX6c O6m73kFc0lJ5ZWEs56lZL2ygj+kDP+abVu58OXbzvEC6rtWp8Gplrgbsbdp4rAKeIkIh qfe54GCouxqm9K2482qn+CXnwtnU8eUPPum2HdyoHY4Ol9fNUmEARysA+Th3B5FsqNll zdk1SL16+1E7qaBnmp3/vALhSmPX3vUYIFbt1zN2q0RMNcS7Oj+uoJL//h9vkhEmIXOc xOSrTMj1/2oZyRRprD7yAL/gvGfUgYPT1dct322gRdP/fRcrH8QPGiNIAOBY7IZoJFlb 7omg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=jDtUFSbY; 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 b10-20020a170903228a00b001a19cbd4fb9si27345718plh.244.2023.03.26.07.49.04; Sun, 26 Mar 2023 07:49:17 -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=jDtUFSbY; 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 S232122AbjCZOrr (ORCPT + 99 others); Sun, 26 Mar 2023 10:47:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232136AbjCZOrl (ORCPT ); Sun, 26 Mar 2023 10:47:41 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2071.outbound.protection.outlook.com [40.107.237.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DA6B46AC for ; Sun, 26 Mar 2023 07:47:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mi9P0Ivsyh+976CwprI0wsXEzUXMlaTD0SEfOYzyvdZKsSrKYJYZWYaQtYr7QILMY/dtUZx2fDNw6q+G8zemw8vf/j5SXKzjkz8p0zcLyC4IgmVM1oXX5sAsS+79qdgNHkK3BV3TfXYA9WoOqeCW2yvPWhemEUwrV/ibzHS/O9BTrus4TSrGCl1NGt0MEJElgxWzmKIl1aZdp2bsyGE6fdIBkD2MUNrSzrdfYx5SaBvLX/BCMEIkpZnrtXmfwgblOkCW/duh8vtJfAuFN3PQZonFVJEwp1OjtZc3ORwYb8doyQPIR8clKH1/1HI0/pu3XpRrBqCRC6VQ+irvRjIDsg== 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=6l9l4MSPMGVG3GgDFqDSeKfsbvGphG5pxVBc850DdwE=; b=a8dmZQ7pTLPF8udRZ6VZwKdojuROMV2rmHmi+uQnyLlcPkfbhG2kVHWC4IqOGE9yOysyFbHjBrmNCX/VpZfzdr7bDSHGGKA5GLhoFmirfIM9vwnqtTAYXNfi5dcU3r9L+RQax8mDz+edGfSR9UAJCPjMVhIRKt3FC0cecO9fJ6cIQ13c5buhmA0DxEYm8uzo9CNk7zRewnXJyhU4FK+13kYQPnHglY3XELAisGtjQYDEoa/E/FzRiIjbM2zeeQh4P6kpE+6N0erYn4LXsUQ+wgogoOGb3xghO6GqbVRCn+bYhe1n25HVnXS8EA2WRDYLk5j/0pgb+DtXcSLUbtYePA== 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=6l9l4MSPMGVG3GgDFqDSeKfsbvGphG5pxVBc850DdwE=; b=jDtUFSbYz3EA95IykH7+XBlWmF8pW1gbWJMZdiuJjflB7EjxAQ1MOBQtxVCfZIWTSAMgChh3VzH3OopPtZTbJ1bBTvNis4HV6irV74Rk1uerrmOP04lJ9YvJxhUFp6GLfiC1cJ+ZLm+CW1OvG89HMZlk2Tm9evEruBvgDETd++w= Received: from BL1PR13CA0127.namprd13.prod.outlook.com (2603:10b6:208:2bb::12) by MN0PR12MB5740.namprd12.prod.outlook.com (2603:10b6:208:373::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Sun, 26 Mar 2023 14:47:29 +0000 Received: from BL02EPF000100D0.namprd05.prod.outlook.com (2603:10b6:208:2bb:cafe::c9) by BL1PR13CA0127.outlook.office365.com (2603:10b6:208:2bb::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.13 via Frontend Transport; Sun, 26 Mar 2023 14:47:29 +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 BL02EPF000100D0.mail.protection.outlook.com (10.167.241.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:29 +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.2375.34; Sun, 26 Mar 2023 09:47:25 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 02/11] virt: sev-guest: Move mutex to SNP guest device structure Date: Sun, 26 Mar 2023 20:16:52 +0530 Message-ID: <20230326144701.3039598-3-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D0:EE_|MN0PR12MB5740:EE_ X-MS-Office365-Filtering-Correlation-Id: d6a8e165-9efd-4183-35f2-08db2e090606 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DIZpMFk6O5PoyqBtURbTp0n2pQs0/l5UJfVfJBSdM9nAL3OpZoMVYU1FTOSYZPFYDn741sQzmlCN9kVGYKMxK1kkRxlmsAec9E8a6J6W5RgqRBPJxbCVZaZj3H0TCTjlrKnjzkAKcwcvLkrI+aJs0IhGQo9YIAz7BgHROA5JAvAddtjwkRQeWElPnKBJEDMimLG2j2qiy7B9Sg8IaCuJmjv2YJL39OdyLFffQjJoLvmys3RfBsj1pAKbKXlK+tNfhzzXIwENHEHKmqQZM3OKCFzuztsNsXGz585776/AnhmGeFDAyGJqvYZNdiHqSKK295d0mAF5IVPVja5DaWdmWQKzdcP2Crnb8f6pRlxlE8BftUqP/KYB6/UWa4malh4tubP7XwTYHitb8v3iT7ADQSwMwJIGCyqLh8FtRmtJsZQcrFOnu3TVUoqvDrIa34Uut+sRflEyR0a6QedzsKZxt3DRb2e9NCWMrZaqBOi5FFvV263kxATJYFvi7dFvknIwq0i+NG6GQxDZdGj7kdTXyN//iDWkqXyqZLSbGurS9iuscxexUezi2vbngs9Jt5ZlOMJtLbHRFnOXMHbHOpn3bR1YIzhQqz7iWfFXcg5uTRL0sVDsnLmBVPH3NxVCIjskH96cDgKNnmaDq2M5vafRbKjfr0ZyWPn0R8yhUR3XAS098gc5aIRUVgrEjsyq4xfTqvbuB8QAHjg15VsYRdZkkoth7kZTpwEGI54EEpuJgUQ= 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)(346002)(376002)(39860400002)(451199021)(46966006)(40470700004)(36840700001)(83380400001)(36756003)(8676002)(16526019)(356005)(316002)(40480700001)(70206006)(54906003)(70586007)(5660300002)(2906002)(82740400003)(81166007)(26005)(36860700001)(8936002)(41300700001)(1076003)(6666004)(47076005)(2616005)(4326008)(426003)(110136005)(82310400005)(7696005)(336012)(478600001)(186003)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:29.3398 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6a8e165-9efd-4183-35f2-08db2e090606 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: BL02EPF000100D0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5740 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442169783522685?= X-GMAIL-MSGID: =?utf-8?q?1761442169783522685?= In preparation to provide new API for sending SNP guest message to the sev-guest driver, 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 57af908bafba..6ae197b57644 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; struct snp_guest_msg *request, *response; @@ -48,9 +51,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) @@ -99,7 +99,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; @@ -377,7 +377,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; @@ -417,7 +417,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; @@ -456,7 +456,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; @@ -541,12 +541,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; } @@ -564,7 +564,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.fw_err && copy_to_user(argp, &input, sizeof(input))) return -EFAULT; @@ -683,6 +683,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 Sun Mar 26 14:46:53 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: 75089 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976015vqo; Sun, 26 Mar 2023 07:49:18 -0700 (PDT) X-Google-Smtp-Source: AK7set8OaKe8YApneQU9TWJYhqNYruMaPCH2+QaTj1bKvXaUuQN2kkma3tIu9Ip9n23XjSuvqrfn X-Received: by 2002:a05:6a20:c26:b0:d9:a5e9:4b4a with SMTP id bw38-20020a056a200c2600b000d9a5e94b4amr6566268pzb.62.1679842158074; Sun, 26 Mar 2023 07:49:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842158; cv=pass; d=google.com; s=arc-20160816; b=S7b5elNrdyQNHcZOb5yXyQExv1vJ+Z3s48ttj0xeeSvws+df3ay1V2nvni4/1dapd6 WBg9dSZ7YW8mtDlvTCdRIpzRJGOjqF9LRrVXXtAE5GVYKvVJAV4pIJu5eguaJxyWnDJQ Hn/xV7ib3NSieTHa2OWRTapDz4bPFimYVxiwrJKCu+PyWY2EXcal5yxO0ks0OuOjKY3H w3ZpT9S5bzdW+g7iaZ6BRqm6iKAkTvnWu/hxkVeEEUV3BQjxz9n+TrpaPeAYtso7OcAz F1pdxpcuomxvuUgXoeeO9Y7jwrJ87GO4PZ3p4g24FxTXSX++inDs1lsW8B63bdE1ubIk Ehzg== 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=U31Vby/Fqrrgw0Ivkecm0SEmjf6GjxWhTpGVfrqw+7s=; b=tgKChV6FDpZk7xGhfSY0v5RrIQtpFrnW+yXQca4srKVxs8w6VDdwvQNnYURjPvk6xQ IQMGBH6HEKBRxa8jH7hvOvZlMlJPzxLOjOOb90Wl4Hbfs5+zV05UMeqzNnUaTU1r87vU F/eEl0Dlfnxwf/5kffDbyTqo46fm0sgdGLlTd9xnpch4bIv6IrjE0gUBeuzoD3yrqu60 UphLg/jdQVbtKbLNzplH4DGBivWZJdh14LLipo0VDwpM3GB6EIYS4TVtr9qPwqVqLvS3 5PPd8w6BtJ+XXhU4didpQwtC60GnbYBkBG1ON6zm8BQuvooG/TSMPRoT9n8Lw31VTUdP 0ilg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=3993hK5U; 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 e26-20020a056a0000da00b0062d09848024si2490863pfj.347.2023.03.26.07.49.05; Sun, 26 Mar 2023 07:49:18 -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=3993hK5U; 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 S232136AbjCZOrw (ORCPT + 99 others); Sun, 26 Mar 2023 10:47:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232259AbjCZOrm (ORCPT ); Sun, 26 Mar 2023 10:47:42 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2051.outbound.protection.outlook.com [40.107.223.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EE1C61B1 for ; Sun, 26 Mar 2023 07:47:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JDjHmXIDHwSN80jeur6m8uAcaebFAZSQWWwFYAiIWvd1/HY/mS7ddF9UEx96xNvAO6AtBUDd2NKwR1glBhv1rrRVWkQEjJXlPpnEcK0sD6C4AycumIPGwZx0QMQ7A3BiU6CH4zmDv0G445ijN5IQPDwtVDyiGKmZO7u4jpUsyUBnWzLOfNwsJi3arycruQB7QDxbEG5ginnaTUYMNuwuWOMS7MebyyOJElw2aeKX8ZbsoJIJP3wggl2h/CSdGxjvwBWB+TsmyW7TQQVsfTiepDXyTWECNI4f65vjo8OkJtClIVt4+3xpyuBPWE7B26nHUPi8VoMaUKfgEjEQ37zLCw== 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=U31Vby/Fqrrgw0Ivkecm0SEmjf6GjxWhTpGVfrqw+7s=; b=QQ7ZYngvHGZO+gZiNNn+/A30N/n17IFwK5IdmpmwE/nNHkGP/hHiW1BW4Yc/RbFXbOUAte5FtaRpoR9Tb8Wa2ip1OxNPZejg/Ord+f32jYNsYGqJodRJzwJOqzlo2wxntQxyV9jI9+r1Ls9ZAfosP4M/17acFtLB2DcSRPCwfjcWfwgaH88i+PYbvJ85mNIda/ODTcE+cPwo6fBqjIvMsmu//WVVJJEhaqC906ijCJ/199k+W2a0s0oS+8c97w2WErlzKo7LvFzuT8ZsB4d8Pqa51RVxdyzJjReI/Ifp5Kf4siAzbH6hgl9b11G5ixlDiCrghuICxFORwZ8lowle5w== 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=U31Vby/Fqrrgw0Ivkecm0SEmjf6GjxWhTpGVfrqw+7s=; b=3993hK5USVHHL7He5HlFIMw/hNlyi0DAXZLU4L3wIJMDcPrqh2vqq0RyV8gLDqHhhBJ+rwCHcnJNEkY8fFg3xM4Bbaia4MRJENv8CN9V6KeUvcKw87inmpyi3nAEp7DHLfyg6y6HfNYtquqktVBaBeGhLuDYESWOllZiIKs4KBw= Received: from BL1PR13CA0149.namprd13.prod.outlook.com (2603:10b6:208:2bb::34) by SN7PR12MB7811.namprd12.prod.outlook.com (2603:10b6:806:34f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Sun, 26 Mar 2023 14:47:33 +0000 Received: from BL02EPF000100D0.namprd05.prod.outlook.com (2603:10b6:208:2bb:cafe::e4) by BL1PR13CA0149.outlook.office365.com (2603:10b6:208:2bb::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.16 via Frontend Transport; Sun, 26 Mar 2023 14:47: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 BL02EPF000100D0.mail.protection.outlook.com (10.167.241.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:32 +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.2375.34; Sun, 26 Mar 2023 09:47:29 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 03/11] virt: sev-guest: Add snp_guest_req structure Date: Sun, 26 Mar 2023 20:16:53 +0530 Message-ID: <20230326144701.3039598-4-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D0:EE_|SN7PR12MB7811:EE_ X-MS-Office365-Filtering-Correlation-Id: e08e9568-c3c4-4dda-cffb-08db2e090806 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LaCg7yq6KnMRh3OwqCKoC4Hysl0dBk8zDp4bbXQI7UIhqi3C/Sn+MWIWrz6sDe9+SUHFkizYatKl6EPmmlIHRo2R+N4SQE5y9JY6v//UrDycDFsjNtYR9FLeA1IxiwNCN7yEvOnKRzWDAzMHor584GlYPy0yWBF6Kuu3Iw6jwtyb1hhQ6eYmM2RUCsr3koAGGNysA8exWsgYEFp28yyj1SLcn77gPURh8d+96nTyM2rVy+Pp09r5zznbw78H4nQEQA9o8PWCwlDD9Vz5i6k9fUBYhejODZP1H+5HAQBepUE9D3bLwweUw+JSHBcqkufBBV1quw3gZdQupi/yoOoRMRMJgC7US2FnhZ6LRqNsBJK0UBKKKXi5MitlBD4y9NjjlhSZm00Tkoxea6PTbJgbQQ4XqTkFsZ9//kLMDyDPq+sge3S6i4kGuCmfPOEPEYO8ftSoaancQc9KV2UxPIzPfCL9Re7cvv3HJYJTQFKpfGBk+UzfJUuzK/i8Ukwexg+MQ/ackraUyUXU09TK5rynXkiBAwB7rSjU+OUjTNw0NG/XOKX59Zw8fyGgEBC5o/A1P9bp4FwhuyzS9s91MThC7B//n8mmgdlv6S158QOFNFTh/85Dz/uoR87+CK6znRou55u/9Ma9WYc6LJ+cN3sRuqDcuolDTws2ZCm2XDjef1e1SdvvgQNnR8YKtPGjSyQY0w+ZUE5ZZY1TQeBmhRpXxgDy6oiwGJPcGy0re5DjTXI= 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)(39860400002)(136003)(346002)(451199021)(36840700001)(40470700004)(46966006)(70586007)(47076005)(8936002)(8676002)(6666004)(2616005)(356005)(82310400005)(16526019)(26005)(83380400001)(1076003)(336012)(426003)(186003)(41300700001)(478600001)(36756003)(36860700001)(4326008)(7696005)(70206006)(2906002)(5660300002)(81166007)(40480700001)(110136005)(82740400003)(316002)(40460700003)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:32.6835 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e08e9568-c3c4-4dda-cffb-08db2e090806 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: BL02EPF000100D0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7811 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442170300660404?= X-GMAIL-MSGID: =?utf-8?q?1761442170300660404?= 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. Signed-off-by: Nikunj A Dadhania --- drivers/virt/coco/sev-guest/sev-guest.c | 87 ++++++++++++++----------- drivers/virt/coco/sev-guest/sev-guest.h | 19 ++++++ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 6ae197b57644..ec93dee330f2 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -60,16 +60,6 @@ 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) -{ - char zero_key[VMPCK_KEY_LEN] = {0}; - - if (snp_dev->vmpck) - return !memcmp(snp_dev->vmpck, 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 @@ -198,8 +188,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); /* Verify that the sequence counter is incremented by 1 */ if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) @@ -221,34 +212,34 @@ static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, 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, - void *payload, size_t sz) +static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, + struct snp_guest_req *req, u8 __vmpck_id) { - struct snp_guest_msg *req = snp_dev->request; - struct snp_guest_msg_hdr *hdr = &req->hdr; + struct snp_guest_msg *msg = snp_dev->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 = __vmpck_id; + hdr->msg_sz = req->req_sz; /* Verify the sequence number is non-zero */ if (!hdr->msg_seqno) return -ENOSR; - dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", + 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, __u64 *fw_err) +static int __handle_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req) { unsigned long err = 0xff, override_err = 0; unsigned long req_start = jiffies; @@ -262,7 +253,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, &err); + rc = snp_issue_guest_request(req->exit_code, &snp_dev->input, &err); switch (rc) { case -ENOSPC: /* @@ -273,7 +264,7 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, * IV reuse. */ override_npages = snp_dev->input.data_npages; - exit_code = SVM_VMGEXIT_GUEST_REQUEST; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; /* * Override the error to inform callers the given extended @@ -314,8 +305,8 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, */ snp_inc_msg_seqno(snp_dev); - if (fw_err) - *fw_err = override_err ?: err; + if (req->fw_err) + *req->fw_err = override_err ?: err; if (override_npages) snp_dev->input.data_npages = override_npages; @@ -332,13 +323,14 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, return rc; } -static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver, - u8 type, void *req_buf, size_t req_sz, void *resp_buf, - u32 resp_sz, __u64 *fw_err) +static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req) { u64 seqno; int rc; + if (!snp_dev || !req) + return -ENODEV; + /* Get message sequence and verify that its a non-zero */ seqno = snp_get_msg_seqno(snp_dev); if (!seqno) @@ -347,21 +339,22 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, msg_ver, type, req_buf, req_sz); + rc = enc_payload(snp_dev, seqno, req, vmpck_id); if (rc) return rc; - rc = __handle_guest_request(snp_dev, exit_code, fw_err); + rc = __handle_guest_request(snp_dev, req); if (rc) { - if (rc == -EIO && *fw_err == SNP_GUEST_REQ_INVALID_LEN) + if (rc == -EIO && *req->fw_err == SNP_GUEST_REQ_INVALID_LEN) return rc; - dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", rc, *fw_err); + dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", + rc, *req->fw_err); 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->resp_buf, req->resp_sz); if (rc) { dev_alert(snp_dev->dev, "Detected unexpected decode failure from ASP. rc: %d\n", rc); snp_disable_vmpck(snp_dev); @@ -371,6 +364,24 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in return 0; } + +static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, u8 msg_version, + u8 msg_type, void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz, __u64 *fw_err) +{ + struct snp_guest_req guest_req = { + .msg_version = msg_version, + .msg_type = msg_type, + .req_buf = req_buf, + .req_sz = req_sz, + .resp_buf = resp_buf, + .resp_sz = resp_sz, + .fw_err = fw_err, + .exit_code = exit_code, + }; + return snp_send_guest_request(snp_dev, &guest_req); +} + static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg) { struct snp_report_resp *resp; @@ -544,7 +555,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 (is_vmpck_empty(snp_dev->vmpck)) { dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n"); mutex_unlock(&snp_dev->cmd_mutex); return -ENOTTY; @@ -678,7 +689,7 @@ static int __init sev_guest_probe(struct platform_device *pdev) } /* Verify that VMPCK is not zero. */ - if (is_vmpck_empty(snp_dev)) { + if (is_vmpck_empty(snp_dev->vmpck)) { dev_err(dev, "vmpck id %d is null\n", vmpck_id); goto e_unmap; } diff --git a/drivers/virt/coco/sev-guest/sev-guest.h b/drivers/virt/coco/sev-guest/sev-guest.h index ceb798a404d6..d245578d988e 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.h +++ b/drivers/virt/coco/sev-guest/sev-guest.h @@ -63,4 +63,23 @@ struct snp_guest_msg { u8 payload[4000]; } __packed; +struct snp_guest_req { + void *req_buf, *resp_buf; + size_t req_sz, resp_sz; + u64 exit_code; + u64 *fw_err; + u8 msg_version; + u8 msg_type; +}; + +static inline bool is_vmpck_empty(u8 *vmpck) +{ + char zero_key[VMPCK_KEY_LEN] = {0}; + + if (vmpck) + return !memcmp(vmpck, zero_key, VMPCK_KEY_LEN); + + return true; +} + #endif /* __VIRT_SEVGUEST_H__ */ From patchwork Sun Mar 26 14:46:54 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: 75090 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976035vqo; Sun, 26 Mar 2023 07:49:21 -0700 (PDT) X-Google-Smtp-Source: AKy350ZOq79Fcr9YjKnMnFafnLWYq1CXhE4q9WVyBei91Yhxz6LIQeYtMi+WbLOUqbc3zs5Zas5P X-Received: by 2002:a17:90b:4d0d:b0:233:cd29:f168 with SMTP id mw13-20020a17090b4d0d00b00233cd29f168mr9945443pjb.24.1679842161288; Sun, 26 Mar 2023 07:49:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842161; cv=pass; d=google.com; s=arc-20160816; b=qC7MR9/dYzdi65tqMDusesoDaMYOW4SsAikI53EB7am90J85CPHdUE1MyEKNvxfyrM 889Cn2rRXLcDfEmZKSFes4eBEDWpfizCeOEwS0iv+xF8D/I4jCUQWmoo3cgHUitBmqdn f8vd93/WQ+ceMZPgGR5NR+yoOaROaOTilHrXpju03a7SW4SNvTJJlnxP9iyUid8wL0u0 500PorVDtEBzg6qMlvJDdOMhBV/l79UMAub4Y682ArDULCn+oSewhHro39C+Mf1axTPv UC7QwF1l93elkz7OJ2COSqa+NUhHtVn04usRAssbbQx6Jymnfdj/iPGlNa/5v8R9BVoW 2Yug== 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=6vKQXI/UcLoINMgy3KgFkyPhc0bu3CZ+oU0nhIZo/RI=; b=f5LbtiZwvwfWE8upjrZgM4SBKTJmu7IDs5wuBnyYLwfmr+JaqroA4YmjteHAYBQz2e OHeZKtuUGnSqMAjGtR0uqFLHOTXGwtfWx+OsFZFH9ICtflQ6hNjUberkE72Bf3xHbbvu AG5bAkACbuDRqj4C9RWKd/UdkBohoQEelYu8EYrzNr3TJAOEOFTgDTUlTgCdqR18syBV R9JBdB22541uz00jsMSSZePqqF56Y3BmV7M3qnmKFa0PxseBjlHrFjfiYES4FOb8lXaq XMisY/1ucWrs9bKp7+z7dRFy2ZCOxKJF58M9TGz+p08QRePuTJD6IZu6SJf/14+7yPsl O4Mg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=O7h7teTI; 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 27-20020a63145b000000b0050bf5a43800si24181165pgu.242.2023.03.26.07.49.08; Sun, 26 Mar 2023 07:49:21 -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=O7h7teTI; 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 S230495AbjCZOsT (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232269AbjCZOrn (ORCPT ); Sun, 26 Mar 2023 10:47:43 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFDDF5FDB for ; Sun, 26 Mar 2023 07:47:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gtYC7CDhj1hqanwK8bCci73ATKIRvLle84lxx0Dnn+3yVhpDEuZH+f+uuoNCvZTz7dk+iqMsNZLTJ2Lig/Etk/VkgUpDtum1PVOtpQgfGKZDCPAHFA9uUDIU+XwOxQA3KJAs+f3R8kFeX/mhMdYlPN5vbyvSeH/hP2xR4hlBqUsqiPiNNJg2brrL7sgdW0LJlxh9tgl039tsNcmmD9M0GIlXPsmfKH69KadFKPdKwuBOS6XOxNjv7zgJ5cU1Sdi7/4isrFh6rl5LcIb9z2lWDJB26a4RJchfLbPVMbcah5cmlJKJQQDhxEaR1Wo9KgyJZbnxEeNcjsSgz4RTg9qfNg== 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=6vKQXI/UcLoINMgy3KgFkyPhc0bu3CZ+oU0nhIZo/RI=; b=itJXOdD5xPuXlPlosnoptswPoPyyLXuxs1tezWo0RgLAKZkZrvpWkMhyACoFi7M8LXD6/kuFehncjSvjYNCf5aacYkNfrDX+pRLlJpgMvv4qxWwFhsD9pQIFLmPRnk3sbfxtpbP/ifP+N34ZWZzaacFn7G1cG5T3Ea2vUKDBw2YlqcRad8lOsZoyhWVXzM63XfU0LIgHyYKYlO3ODQJ+sp7cWXpPbqW3kACXtBEx9xXn6nXKXAHzJj4DFizYBAkwd+5m0BRRwwxFWmQPWl2yqcIoGWhE1CUP5MErFob1dOmBeBtD0w7u7Mypy9PcEi7txnCwtC5TZbyIRMCZugLYjQ== 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=6vKQXI/UcLoINMgy3KgFkyPhc0bu3CZ+oU0nhIZo/RI=; b=O7h7teTIbNykqZRgBEZih8OBhF52hFhHlXP0xoVkOBBVGf4YzSTWZJNxhg3AF+tQ/hatqMeggK1Zvk98gnkIky3hdiNXX/dHMs7Nh1jWmShJ0o/CHCm6LBVMxIuE/Suh8XaKlH7YPRqk324aIFQmNQo7c4FHPnCWJceiQv8YfAo= Received: from BL1PR13CA0150.namprd13.prod.outlook.com (2603:10b6:208:2bb::35) by PH0PR12MB5404.namprd12.prod.outlook.com (2603:10b6:510:d7::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sun, 26 Mar 2023 14:47:36 +0000 Received: from BL02EPF000100D0.namprd05.prod.outlook.com (2603:10b6:208:2bb:cafe::fb) by BL1PR13CA0150.outlook.office365.com (2603:10b6:208:2bb::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.15 via Frontend Transport; Sun, 26 Mar 2023 14:47:36 +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 BL02EPF000100D0.mail.protection.outlook.com (10.167.241.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:36 +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.2375.34; Sun, 26 Mar 2023 09:47:32 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 04/11] virt: sev-guest: Add simplified helper to assign vmpck Date: Sun, 26 Mar 2023 20:16:54 +0530 Message-ID: <20230326144701.3039598-5-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D0:EE_|PH0PR12MB5404:EE_ X-MS-Office365-Filtering-Correlation-Id: b1f14eec-677c-4fc2-2eb6-08db2e090a0c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yZ0iOqcmYZXydtXhcMzg+i9ylLHiYgr3PbFIWZAb53ynLT+7fHXvMrECYVUq8svA0u2FlHT9BfUkHra+XV5fNIMCJ/ilRmhdi68weORL/rCO6m6qSK5b1EDWmFs1ojzt66gbrpFf9GL950lDHXF6oPStob+v+AhSmH0EprHcaMOaaH7+dh0Ipo995Iagwvuwet0Hr+eAxTg6Fc7Qx/5o/+GtPHnXYUo2cra7X3/IDmrnvXNKKMgnIOQ4wGYMmo9C+d55WSDXdep7LRTMDWfnG09Cjm03cYscFZGVRfr7zT9nITLsH4+ReLUrfyKbVXlBXp/+V+bkkWFzLubOpZFEthAeHTWnpzRkduTsJa+np7wvow/R6Fj/yP0GnRhz2ORZ3OZ1dd7FkH104g0PFmxd26Fhj3jL8dqOKmAajrL4vj1f1tKEKx+FV9RDVrAHupscR5PvEYArpG89HIR3wd0CQvgscsLVBRZBVsmKew64IUlz68YqWeZN2TCoZXhgZvN4pdr3MRWiFmX2rvPt2gemQxTqDVcgM7S4vnJEkzq6ftsrN/fVSXeouIKf9+OS3V9MXQ3Jdn2/4nA8N3QnQibFXmITN694C7aoOnu1KE0gMVVmgMXDr+pkre23scoNAQXYOhUQvjZ0sWYRLuPbZOHVbU2w6UAhwUYlDzFyPieE/C9P/smYIV0OpyhzeJq6UCrcdSgcKaFxjU5GQypzHRYZ8FZ3SBQKIdKn4571n8O+yVk72VRc/EoB2we0Nf3REf4D9l/OPgZknGyQ1yZ7xbgnng== 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)(136003)(39860400002)(376002)(396003)(346002)(451199021)(36840700001)(46966006)(40470700004)(5660300002)(110136005)(316002)(6666004)(8676002)(70586007)(4326008)(336012)(83380400001)(81166007)(1076003)(8936002)(26005)(36756003)(41300700001)(82310400005)(36860700001)(82740400003)(2616005)(2906002)(70206006)(54906003)(47076005)(426003)(478600001)(356005)(40460700003)(186003)(7696005)(40480700001)(16526019)(17423001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:36.0898 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b1f14eec-677c-4fc2-2eb6-08db2e090a0c 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: BL02EPF000100D0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB5404 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442174411650100?= X-GMAIL-MSGID: =?utf-8?q?1761442174411650100?= Simplify get_vmpck and prepare it to be used as an API. Update the snp_guest_dev structure in snp_assign_vmpck(). Added vmpck_id to the snp_guest_dev structure which can be used in SNP guest request API and will remove direct use of vmpck_id command line parameter. Signed-off-by: Nikunj A Dadhania --- drivers/virt/coco/sev-guest/sev-guest.c | 41 ++++++++----------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index ec93dee330f2..4901ebc8fa1a 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -45,6 +45,7 @@ struct snp_guest_dev { struct snp_req_data input; u32 *os_area_msg_seqno; u8 *vmpck; + u8 vmpck_id; }; static u32 vmpck_id; @@ -80,7 +81,7 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) static void snp_disable_vmpck(struct snp_guest_dev *snp_dev) { dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - vmpck_id); + snp_dev->vmpck_id); memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); snp_dev->vmpck = NULL; } @@ -339,7 +340,7 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_gues memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, req, vmpck_id); + rc = enc_payload(snp_dev, seqno, req, snp_dev->vmpck_id); if (rc) return rc; @@ -364,7 +365,6 @@ static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_gues return 0; } - static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, u8 msg_version, u8 msg_type, void *req_buf, size_t req_sz, void *resp_buf, u32 resp_sz, __u64 *fw_err) @@ -625,32 +625,16 @@ 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; + dev->vmpck = dev->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; + dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + vmpck_id; - return key; + return true; } static int __init sev_guest_probe(struct platform_device *pdev) @@ -682,8 +666,8 @@ 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; } @@ -697,7 +681,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)); From patchwork Sun Mar 26 14:46:55 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: 75092 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976058vqo; Sun, 26 Mar 2023 07:49:25 -0700 (PDT) X-Google-Smtp-Source: AKy350ZAYT8siZoqQ0ATcCWeBJElwA9/1LPk2YNqb+ae93VQq1I95w/2LcGZGZ8TjGmKpaW4mop8 X-Received: by 2002:a62:6441:0:b0:625:764a:5fc3 with SMTP id y62-20020a626441000000b00625764a5fc3mr8416795pfb.0.1679842164602; Sun, 26 Mar 2023 07:49:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842164; cv=pass; d=google.com; s=arc-20160816; b=rztVLbDEm4XGTQN1MNnS/nYPI+rrZHnnyLA8XcO7vzls+pKLFOLV3hEjMIVMYgr1EW 3lEtPc+zZilUuSKuozVCfJpLjPGQRsz6bw8Wz0NKf4FfNBmjrpykgxegKHzrL4DZ65fk 8PSgia0xbSGVTWViBrDU5awjYAKYWmvYQyOdq3f5HfoN3Yj21joBEM5QbHJPO3ArtEC4 NvQwOZ46h36oksg4MmFTBZPbnl4Fgbbflkc5VoEeUt4jkaaSCqz9F5M6YMVGjJyBrutE H67E/hNWslzBSGlf1urPToe4o4e+FFulEcjcsmwqSY5HsRrcICQx6vcGcAn/4lUlz9g6 kwyw== 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=rBgZ5AfHYV8og/BNoh2GHCBR4RG3Io4yIMhVBgEYJqs=; b=JtAigodkHTd+1LXAhZGR4crgHzuln/Dn7wf8yTSv3zvr323xD0zW+9eYIi039w2+UK LjcK89pYtFGZ0vXB/0KqMxBrkf0TmpYYhzrNXU8wTPHcvLtbu4RvU+yAp42V68XnOqtw +5x1uhJOZWEakd9u+WQZ/6oLWfR1E/ntNldkaMTWDWhdH81LskxgKrWvHQD0c9iTVRhX CMoSCGbdAPHJVBI6xuIcwNfD7CAiK35MPf2/AMin8DzX/yTRD/qaBjE77SlTOxc1wZtd UooA6WLqB8mzS1qBWsU9CSGqx2P+evrqkF480aMEcOQRZMjBzrcQfUEdb0qcPkxcqdil 0Muw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=nqSEl7ZD; 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 ei26-20020a056a0080da00b00627f0f8d49fsi18887398pfb.373.2023.03.26.07.49.12; Sun, 26 Mar 2023 07:49: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=nqSEl7ZD; 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 S229483AbjCZOs2 (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232314AbjCZOr4 (ORCPT ); Sun, 26 Mar 2023 10:47:56 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2061e.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::61e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 386B065BB for ; Sun, 26 Mar 2023 07:47:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e48weKmyM/fi/tUJG7Buam/XlPUbQKjyJFi29GAzMcjaVLyWYwA4TbAqdImAnpEODFNMVAWWFsOAD3Npv2UYb4evOxGdE3cFam0bYp0cGjxt+EoaYKuWW1zNLgoFqbPSCjnusK0rMN/Xik9H/HlUIw4thqRrigHtiHcIkPAYgglZwr4khiL9hXz4y+4piJsmhql5GLzcMtCx9EGGdkuaIaJoR6yIWq8w/YuMuw2VlbxZ86kocMp7Wm7+wZKopjmjPe3yUXVNy/9XZq/WwvQbgtwxGhFtwlIxyRCc7CAgFwC9ZtMFhS1vVvVuj3cF6jJfvl7yKarDF47ltBtXkl15Pw== 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=rBgZ5AfHYV8og/BNoh2GHCBR4RG3Io4yIMhVBgEYJqs=; b=nJkghFD9MB9bkuAUKPzS5Lv0VraV5D3umtCe83lvjBjvovj9U+47eKJLlamfhA3oa6U7T75U517FPK+SUNj1fJitcv1wamCO815WvNVT+At371ZbJI7JwoumeA0Za1BHfmlpTyVGIXUBOAf1nLveQBw9gBoxAzyQV71TCcHrCEvvBrq8hG/rNXPl0Xd1h6A+toIBLhvBt6AlcRxZ6mwB9biKlwWA8cvRfx2eBT2+a1R0hWMMxacZrJb37QzmNVpOL5+Dtunv7V1B0nPtVc22UjwrjYT13aI/TRa6/a2GWQ+t5T5XgCC6qzd1kSx//AlOLG5nsY08I35iW39cHc+Kfw== 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=rBgZ5AfHYV8og/BNoh2GHCBR4RG3Io4yIMhVBgEYJqs=; b=nqSEl7ZDdNItKOrQlNr8DEx/IC1tneiw9H+MblrKb9cIcq6ZqUFFA7D560vMHiA6ZWJJk0x2iAQk8wToezkGkOjz2VhQDoM5X7xAE4NnySlPp54saxr9KOaZjfyTyYv5dF+C8TmwOUG2Z6iBNzsLIcr91952YDNE0jSAyWNjZnU= Received: from MN2PR20CA0009.namprd20.prod.outlook.com (2603:10b6:208:e8::22) by MW6PR12MB8759.namprd12.prod.outlook.com (2603:10b6:303:243::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sun, 26 Mar 2023 14:47:40 +0000 Received: from BL02EPF000100D2.namprd05.prod.outlook.com (2603:10b6:208:e8:cafe::2a) by MN2PR20CA0009.outlook.office365.com (2603:10b6:208:e8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Sun, 26 Mar 2023 14:47: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 BL02EPF000100D2.mail.protection.outlook.com (10.167.241.206) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:39 +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.2375.34; Sun, 26 Mar 2023 09:47:35 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 05/11] x86/sev: Move and reorganize sev guest request api Date: Sun, 26 Mar 2023 20:16:55 +0530 Message-ID: <20230326144701.3039598-6-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D2:EE_|MW6PR12MB8759:EE_ X-MS-Office365-Filtering-Correlation-Id: 5c0fb150-5b55-457a-bbd0-08db2e090c37 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6wu80f6skWiyWmpl/ZcvUay2jVy6ZuQtTYsJa5OQoP7kkOEUXySsd682JgLevR8mo5+f5z4yWWCLKiowlgqyKc/z6ticOdzD/u3s5Em2V5I1BBhuDrh7lvx04uLHEBRRno2EaDU5M8rGtTOOGWGBgPt11MBm51S7ZdQn5a+cTPdtNqRzlYFvV2xfbMjC2NfJb0asv5yLscusijMnnQJTfGrKKeNsnyqbfhnB6S2BvsCeFmzAL+yfMqqWwYx6OEBa1kSUFJk/KVDEPa8XkxwwiqWvu5SWfTrgU/SvPNLlfvUeopoUVuhL3fRsaqOxN8Qz5uoqfU4FNDdNr6DdWscqDbbqtYt9mND8oQix1uL4DkfAevapBfthoS5kEojD2d/CykgXf1WdA/58Q18zCqUr/ZKmSKdrcg3537W2Lv/8agzQpfZvg6I0+IpUc1G8Fo3bIdwcgM6t6ekqYiDzvt1x1re94vxGm0vuWWlEIoGBjAwtfFJYDl3tbWi1B+YIrtroZWMeXCXr6/na9Lf0xLrA4udwYqGjT6sQyIcVKn9ALguKa600iP6Ljz9KUGn2s47ItK6RxsBX1vZ4JDoepDil6me0/mBf458daK+tfK/u2uvWI+Nyvs8gEkaQuIrquj+v1zLi+QMnquwCV6tgqr/5HFHBlYPPl1BJMT2es5U2fFCneIg2sNYPOubBlY48HQptW8cG96kSAy5iy8Rhy+MkLw8Ughlo0OSQZUX4LDnsXNM= 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)(39860400002)(136003)(346002)(451199021)(46966006)(36840700001)(40470700004)(36756003)(1076003)(6666004)(82310400005)(40480700001)(16526019)(186003)(26005)(426003)(2616005)(47076005)(336012)(40460700003)(83380400001)(4326008)(81166007)(5660300002)(8936002)(82740400003)(8676002)(70586007)(70206006)(41300700001)(2906002)(30864003)(7696005)(36860700001)(478600001)(316002)(110136005)(54906003)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:39.7150 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5c0fb150-5b55-457a-bbd0-08db2e090c37 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: BL02EPF000100D2.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8759 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442177400335638?= X-GMAIL-MSGID: =?utf-8?q?1761442177400335638?= For enabling Secure TSC, SEV-SNP guests need to communicate with the security coprocessor really early during boot. Lot of the required functions are implemented in the sev-guest driver. Move the required functions and provide API to the driver to assign VM communications key and send guest request. Signed-off-by: Nikunj A Dadhania --- arch/x86/Kconfig | 1 + .../x86/include/asm}/sev-guest.h | 6 + arch/x86/include/asm/sev.h | 22 + arch/x86/kernel/sev.c | 437 ++++++++++++++++- drivers/virt/coco/sev-guest/Kconfig | 1 - drivers/virt/coco/sev-guest/sev-guest.c | 455 ++---------------- 6 files changed, 487 insertions(+), 435 deletions(-) rename {drivers/virt/coco/sev-guest => arch/x86/include/asm}/sev-guest.h (86%) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index a825bf031f49..42c52bfd2bac 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1545,6 +1545,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/drivers/virt/coco/sev-guest/sev-guest.h b/arch/x86/include/asm/sev-guest.h similarity index 86% rename from drivers/virt/coco/sev-guest/sev-guest.h rename to arch/x86/include/asm/sev-guest.h index d245578d988e..834cdae302ad 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.h +++ b/arch/x86/include/asm/sev-guest.h @@ -12,6 +12,9 @@ #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 #define AAD_LEN 48 @@ -72,6 +75,9 @@ struct snp_guest_req { u8 msg_type; }; +int snp_send_guest_request(struct snp_guest_dev *dev, struct snp_guest_req *req); +bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id); + static inline bool is_vmpck_empty(u8 *vmpck) { char zero_key[VMPCK_KEY_LEN] = {0}; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..36868e21c3e0 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -9,6 +9,7 @@ #define __ASM_ENCRYPTED_STATE_H #include +#include #include #include #include @@ -96,6 +97,27 @@ struct snp_req_data { struct sev_guest_platform_data { u64 secrets_gpa; + + void *certs_data; + struct aesgcm_ctx *ctx; + struct snp_guest_msg *req, *resp; + struct snp_secrets_page_layout *layout; + struct snp_req_data input; + u8 *vmpck0; +}; + +struct snp_guest_dev { + struct device *dev; + struct miscdevice misc; + + /* Mutex to serialize the shared buffer access and command handling. */ + struct mutex cmd_mutex; + + struct sev_guest_platform_data *pdata; + + u32 *os_area_msg_seqno; + u8 *vmpck; + u8 vmpck_id; }; /* diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 3f664ab277c4..c5ca97aab8c1 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -22,10 +22,13 @@ #include #include #include +#include +#include #include #include #include +#include #include #include #include @@ -982,6 +985,427 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) free_page((unsigned long)vmsa); } +static struct sev_guest_platform_data *platform_data; + +/* + * 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) +{ + pr_alert("Disabling vmpck_id %d to prevent IV reuse.\n", snp_dev->vmpck_id); + memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); + snp_dev->vmpck = NULL; +} + +static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *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; + + 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) +{ + /* + * 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; +} + +static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen) +{ + struct aesgcm_ctx *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); + if (!ctx) + return NULL; + + if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) { + pr_err("SNP: crypto init failed\n"); + kfree(ctx); + return NULL; + } + + return ctx; +} + +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); +} + +static int snp_setup_psp_messaging(struct sev_guest_platform_data *pdata) +{ + u64 gpa; + int ret; + + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return -ENODEV; + + gpa = get_secrets_page(); + if (!gpa) + return -ENODEV; + + pdata->layout = (__force void *)ioremap_encrypted(gpa, PAGE_SIZE); + if (!pdata->layout) { + pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n"); + return -ENODEV; + } + + ret = -EINVAL; + pdata->vmpck0 = pdata->layout->vmpck0; + + /* Verify that VMPCK is not zero. */ + if (is_vmpck_empty(pdata->vmpck0)) { + pr_err("vmpck id 0 is null\n"); + goto e_unmap; + } + + ret = -ENOMEM; + /* Allocate the shared page used for the request and response message. */ + pdata->req = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->req) + goto e_unmap; + + pdata->resp = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!pdata->resp) + goto e_free_request; + + pdata->certs_data = alloc_shared_pages(SEV_FW_BLOB_MAX_SIZE); + if (!pdata->certs_data) + goto e_free_response; + + ret = -EIO; + pdata->ctx = snp_init_crypto(pdata->vmpck0, VMPCK_KEY_LEN); + if (!pdata->ctx) + goto e_free_cert_data; + + /* initial the input address for guest request */ + pdata->input.req_gpa = __pa(pdata->req); + pdata->input.resp_gpa = __pa(pdata->resp); + pdata->input.data_gpa = __pa(pdata->certs_data); + + return 0; + +e_free_cert_data: + free_shared_pages(pdata->certs_data, SEV_FW_BLOB_MAX_SIZE); +e_free_response: + free_shared_pages(pdata->resp, sizeof(struct snp_guest_msg)); +e_free_request: + free_shared_pages(pdata->req, sizeof(struct snp_guest_msg)); +e_unmap: + iounmap(pdata->layout); + + return ret; +} + +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 sev_guest_platform_data *pdata, + void *payload, u32 sz) +{ + struct snp_guest_msg *resp = pdata->resp; + struct snp_guest_msg *req = pdata->req; + struct snp_guest_msg_hdr *req_hdr = &req->hdr; + struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + struct aesgcm_ctx *ctx = pdata->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); + + /* 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) > sz)) + return -EBADMSG; + + return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); +} + +static int enc_payload(struct sev_guest_platform_data *pdata, u64 seqno, + struct snp_guest_req *req, u8 vmpck_id) +{ + struct snp_guest_msg *msg = pdata->req; + 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 = 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(pdata->ctx, msg, req->req_buf, req->req_sz); +} + +static int __handle_guest_request(struct snp_guest_dev *snp_dev, + struct sev_guest_platform_data *pdata, + struct snp_guest_req *req) +{ + unsigned long err = 0xff, override_err = 0; + unsigned long req_start = jiffies; + unsigned int override_npages = 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->exit_code, &pdata->input, &err); + 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 = pdata->input.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_REQ_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_EBUSY 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 (req->fw_err) + *req->fw_err = override_err ?: err; + + if (override_npages) + pdata->input.data_npages = override_npages; + + /* + * 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_REQ_INVALID_LEN) + return -EIO; + + return rc; +} + +int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req) +{ + struct sev_guest_platform_data *pdata; + u64 seqno; + int rc; + + if (!snp_dev || !snp_dev->pdata || !req) + 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; + + memset(pdata->resp, 0, sizeof(struct snp_guest_msg)); + + /* Encrypt the provided payload */ + rc = enc_payload(pdata, seqno, req, snp_dev->vmpck_id); + if (rc) + return rc; + + rc = __handle_guest_request(snp_dev, pdata, req); + if (rc) { + if (rc == -EIO && *req->fw_err == SNP_GUEST_REQ_INVALID_LEN) + return rc; + + pr_alert("Detected error from ASP request. rc: %d, fw_err: %llu\n", + rc, *req->fw_err); + snp_disable_vmpck(snp_dev); + return rc; + } + + rc = verify_and_dec_payload(pdata, req->resp_buf, req->resp_sz); + 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, int vmpck_id) +{ + struct sev_guest_platform_data *pdata = dev->pdata; + + if (!pdata || WARN_ON(vmpck_id > 3)) + return false; + + dev->vmpck_id = vmpck_id; + dev->vmpck = pdata->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; + dev->os_area_msg_seqno = &pdata->layout->os_area.msg_seqno_0 + 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; @@ -2247,18 +2671,17 @@ 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) + platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return -ENOMEM; + + if (snp_setup_psp_messaging(platform_data)) return -ENODEV; - data.secrets_gpa = gpa; - if (platform_device_add_data(&sev_guest_device, &data, sizeof(data))) + if (platform_device_add_data(&sev_guest_device, platform_data, sizeof(*platform_data))) return -ENODEV; if (platform_device_register(&sev_guest_device)) 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 4901ebc8fa1a..a011df21c03a 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -24,110 +24,25 @@ #include #include -#include "sev-guest.h" +#include #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; - struct snp_guest_msg *request, *response; - struct snp_secrets_page_layout *layout; - struct snp_req_data input; - u32 *os_area_msg_seqno; - u8 *vmpck; - u8 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."); static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev) { - if (snp_dev && snp_dev->ctx) - return snp_dev->ctx->authsize; + struct sev_guest_platform_data *pdata = snp_dev->pdata; + + if (pdata && pdata->ctx) + return pdata->ctx->authsize; WARN_ONCE(1, "Unable to get crypto authsize\n"); return 0; } -/* - * 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) -{ - dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n", - snp_dev->vmpck_id); - memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN); - snp_dev->vmpck = NULL; -} - -static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *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; - - 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) -{ - /* - * 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; -} - static inline struct snp_guest_dev *to_snp_dev(struct file *file) { struct miscdevice *dev = file->private_data; @@ -135,236 +50,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(u8 *key, size_t keylen) -{ - struct aesgcm_ctx *ctx; - - ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); - if (!ctx) - return NULL; - - if (aesgcm_expandkey(ctx, key, keylen, 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, void *payload, u32 sz) -{ - struct snp_guest_msg *resp = snp_dev->response; - struct snp_guest_msg *req = snp_dev->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); - - /* 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) > sz)) - return -EBADMSG; - - /* Decrypt the payload */ - return dec_payload(ctx, resp, payload, resp_hdr->msg_sz); -} - -static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, - struct snp_guest_req *req, u8 __vmpck_id) -{ - struct snp_guest_msg *msg = snp_dev->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 = __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) -{ - unsigned long err = 0xff, override_err = 0; - unsigned long req_start = jiffies; - unsigned int override_npages = 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->exit_code, &snp_dev->input, &err); - 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 = snp_dev->input.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_REQ_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_EBUSY 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 (req->fw_err) - *req->fw_err = override_err ?: err; - - if (override_npages) - snp_dev->input.data_npages = override_npages; - - /* - * 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_REQ_INVALID_LEN) - return -EIO; - - return rc; -} - -static int snp_send_guest_request(struct snp_guest_dev *snp_dev, struct snp_guest_req *req) -{ - u64 seqno; - int rc; - - if (!snp_dev || !req) - return -ENODEV; - - /* Get message sequence and verify that its a non-zero */ - seqno = snp_get_msg_seqno(snp_dev); - if (!seqno) - return -EIO; - - memset(snp_dev->response, 0, sizeof(struct snp_guest_msg)); - - /* Encrypt the userspace provided payload */ - rc = enc_payload(snp_dev, seqno, req, snp_dev->vmpck_id); - if (rc) - return rc; - - rc = __handle_guest_request(snp_dev, req); - if (rc) { - if (rc == -EIO && *req->fw_err == SNP_GUEST_REQ_INVALID_LEN) - return rc; - - dev_alert(snp_dev->dev, "Detected error from ASP request. rc: %d, fw_err: %llu\n", - rc, *req->fw_err); - snp_disable_vmpck(snp_dev); - return rc; - } - - rc = verify_and_dec_payload(snp_dev, req->resp_buf, req->resp_sz); - 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, u8 msg_version, u8 msg_type, void *req_buf, size_t req_sz, void *resp_buf, u32 resp_sz, __u64 *fw_err) @@ -463,12 +148,14 @@ 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 sev_guest_platform_data *pdata; struct snp_ext_report_req req; struct snp_report_resp *resp; int ret, npages = 0, resp_len; lockdep_assert_held(&snp_dev->cmd_mutex); + pdata = snp_dev->pdata; if (!arg->req_data || !arg->resp_data) return -EINVAL; @@ -492,7 +179,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques * the host. If host does not supply any certs in it, then copy * zeros to indicate that certificate data was not provided. */ - memset(snp_dev->certs_data, 0, req.certs_len); + memset(pdata->certs_data, 0, req.certs_len); npages = req.certs_len >> PAGE_SHIFT; cmd: /* @@ -505,14 +192,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; + pdata->input.data_npages = npages; ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg->msg_version, SNP_MSG_REPORT_REQ, &req.data, sizeof(req.data), resp->data, resp_len, &arg->fw_err); /* If certs length is invalid then copy the returned length */ if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) { - req.certs_len = snp_dev->input.data_npages << PAGE_SHIFT; + req.certs_len = pdata->input.data_npages << PAGE_SHIFT; if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) ret = -EFAULT; @@ -522,7 +209,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques goto e_free; if (npages && - copy_to_user((void __user *)req.certs_address, snp_dev->certs_data, + copy_to_user((void __user *)req.certs_address, pdata->certs_data, req.certs_len)) { ret = -EFAULT; goto e_free; @@ -583,68 +270,17 @@ 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; - dev->vmpck = dev->layout->vmpck0 + vmpck_id * VMPCK_KEY_LEN; - dev->os_area_msg_seqno = &dev->layout->os_area.msg_seqno_0 + 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 sev_guest_platform_data *pdata; 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)) @@ -653,80 +289,48 @@ static int __init sev_guest_probe(struct platform_device *pdev) 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; + pdata = (struct sev_guest_platform_data *)dev->platform_data; 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; + snp_dev->pdata = pdata; if (!snp_assign_vmpck(snp_dev, vmpck_id)) { dev_err(dev, "invalid vmpck id %d\n", vmpck_id); - goto e_unmap; + goto e_free_snpdev; } /* Verify that VMPCK is not zero. */ if (is_vmpck_empty(snp_dev->vmpck)) { dev_err(dev, "vmpck id %d is null\n", vmpck_id); - goto e_unmap; + goto e_free_snpdev; + } + + /* Skip VMPCK0 initialization as the key is already initialized during early boot */ + if (vmpck_id && aesgcm_expandkey(pdata->ctx, snp_dev->vmpck, VMPCK_KEY_LEN, AUTHTAG_LEN)) { + 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); - if (!snp_dev->certs_data) - goto e_free_response; - - ret = -EIO; - 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; 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); - snp_dev->input.data_gpa = __pa(snp_dev->certs_data); - - ret = misc_register(misc); + ret = misc_register(misc); if (ret) - goto e_free_ctx; + goto e_free_snpdev; 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: - 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_snpdev: + kfree(snp_dev); return ret; } @@ -734,11 +338,8 @@ 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); return 0; } From patchwork Sun Mar 26 14:46: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: 75091 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976036vqo; Sun, 26 Mar 2023 07:49:21 -0700 (PDT) X-Google-Smtp-Source: AK7set/6x46KrEEOoluiYXgJXyvb3b5OEeu4j0ngreql+HJgDjWnautIff11zc8vGW1Fc8rAU4S8 X-Received: by 2002:a05:6a20:1b10:b0:da:38b3:74e3 with SMTP id ch16-20020a056a201b1000b000da38b374e3mr8001708pzb.34.1679842161593; Sun, 26 Mar 2023 07:49:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842161; cv=pass; d=google.com; s=arc-20160816; b=nDPXXIkHXJHvSyHBiVYKETjeA1v6XqWHM90jAfEsVF4Q0uyaYs5yIua39sh1eYLomX Y3A6dGJL0+7NhcZvXVGsFRCmpwP6tN8rBnajRQUb/ahCVjE2GtWLmY08OepdtP09ARyo ftmlV1qv+jZVtRcZL0C2Yw4pAwYhZFSbeaZ1glTUOns5Pm5inNF6EzyGaxvpVlnfr0Ql GE+zp1gGSaAt2HDeoS1J95JOGKcd9CgHG6h3nDfI6PLGL8hSz+TSwkSQ4cCowJtY7oiW NYMEVEQhW8kaLhaP2IqIsc9ajVE0zL1rzcvg8ZvCqazg7FZlMwbxU5GiT8henrYLiIis JLKQ== 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=iZOd8+VWlBTQm3ysuapRL8TUJrtzoFTQJXHfy5b7Lis=; b=mA1Ohh222sOgkTFPXZcFDR5gCNPyfU3hUYc2fWtYryekLPhsZcS3NWi2qA1Dw3FqNS lzB5CwZbkFZo8wnV8+Ly1dXmmK+GIXYMLWytcZ9YPUxgymk/2skysnVll5HEbnrAzJ+5 Ni2ulabLPdm3CK06VFasFBQA7Bd2rcdnTTsH6x4AvIDQaOHpCuQ+E2iVgcmvFBOAIUXL XYYiK17nehtpRnjryggWwsrFTjitCnCC7jwLFcrpu8pmRBkKuaja5C7qYFlz1e58yfeJ RO1Rl5stjm+2vsp6dVc5n1Wp2IR+UYG/QEFWQYidnlJI3tWBhEo9i9uu6Cq72OTIcCnE lmMw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=QWh81SVQ; 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 r23-20020a63e517000000b004fb64c27835si25318125pgh.524.2023.03.26.07.49.09; Sun, 26 Mar 2023 07:49:21 -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=QWh81SVQ; 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 S232018AbjCZOsX (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232208AbjCZOrx (ORCPT ); Sun, 26 Mar 2023 10:47:53 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2089.outbound.protection.outlook.com [40.107.94.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 479826A4B for ; Sun, 26 Mar 2023 07:47:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=j4SEpAZ3Co48l0WtBJogj3lgjXv6FH1i+TsoTsFNkCq6XQg8slMT1XhrACd7q2DSevMzqK1LWcrEIhPa/dCyJOX8DcTIoKDu60NsGr+4J+Y7Brk5Fc20m1m+HgBfHoLfdyVwcXRM11pNTWbYR2FM4KcsM7gTBL0jkElPKdsqaAKrzsm2HYT9CBE1Fj20c3oa7hiMUua+7isSQXomzJx+OUc4GZorj29Qsygtko4GJapXwfqY6bhHGDBcxKGJpJmnw3/o12QCAKaz4B7aiKOMl5b3PU1qn3StBGVzi6AdO62ku/b1h8Yc2Pf5ayucegQI0kxWLEcCyW31N2I++6QHNQ== 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=iZOd8+VWlBTQm3ysuapRL8TUJrtzoFTQJXHfy5b7Lis=; b=n9L7Qos1ldu9I7iVIJ5+3fhtWhk0JL+PJhfqQGhrSwtT+SrLzU7uVwqTkLfMJ8n+M7Lp3LYzgQ6wOaAStRDTlgv78Z/cxF6p53pLLgSMIirUafK3Aot3VoAMXMcxv+3xffRQYlms0X0JiStCwcwdyffN+bnopjuPsh26HVHRzmmZkSx1IgGK+HRQoGpRgDJPGnIKWKpzmMpFVtSNGFQDI/sasxgaa30ioBbYfJrMwoVqrhuidfRIle/dyds2ZnDWgPuHcHSNMlhlsNku7Q1+QmQbe+HvzissPMa3n+I4/Utv5W5qE1fDxbNRUImZ+6sfimJJpFupqYkx1SXoon8QnA== 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=iZOd8+VWlBTQm3ysuapRL8TUJrtzoFTQJXHfy5b7Lis=; b=QWh81SVQCj6VhSfeCCnSF/0ljp5+Yi1rjJBol2c5M0tROZzlW4GGh++4Oat+hsGobET5vhqCN41ds99PsFzRc+CHkW9tkMfvJFxFr5i5VB5hK9J3Hd31Mjp9OxxyQX5jInOSu8PBQ5vNOqoGKjVe9EcOwN/LIpCZ5HTlcUeAsjo= Received: from BL1PR13CA0132.namprd13.prod.outlook.com (2603:10b6:208:2bb::17) by SN7PR12MB8819.namprd12.prod.outlook.com (2603:10b6:806:32a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.28; Sun, 26 Mar 2023 14:47:43 +0000 Received: from BL02EPF000100D0.namprd05.prod.outlook.com (2603:10b6:208:2bb:cafe::7c) by BL1PR13CA0132.outlook.office365.com (2603:10b6:208:2bb::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.13 via Frontend Transport; Sun, 26 Mar 2023 14:47:43 +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 BL02EPF000100D0.mail.protection.outlook.com (10.167.241.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:42 +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.2375.34; Sun, 26 Mar 2023 09:47:39 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 06/11] x86/mm: Add generic guest initialization hook Date: Sun, 26 Mar 2023 20:16:56 +0530 Message-ID: <20230326144701.3039598-7-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D0:EE_|SN7PR12MB8819:EE_ X-MS-Office365-Filtering-Correlation-Id: b9674fdd-f6a7-498b-3a28-08db2e090e25 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bJLwUFnX5QueNxKQeFVJ4veDiDUsT+ui7BMQHcdb6+T9k12AmLn6pwi7CVE7epQX2RPnwPETZLj6jW4MbT9Fm2kvUjXtzPHdDJsdzytsJpyLENLlRfMbZQlYx2f1pup3+PCtEOMAMfhXda8JJyikncQ1Ex8MaJQxtmybqYH69I5Ct3p4fXQsG0cnHZWt8Y0ulHIKpsb6Ih8qzvr4jFHtg/sksoBJb412Vxq4RExE+pT/9EG59lxHPZ2ZKfRRD5UDP0CVaCwsAVfI9IshAYkn557RnvB2o05lxGNMw4pXFhstudivPpgS3osRzc1rZK24oe42p8PQxvcO+ktWzqs2vNu3F8UR/c1pxvtjkrlAKG5uI3b/Bxvqq3/KF9Y2Q2kfk0dV39y7lSOXR+gf5j/Mks8UjGnkMtnrAygqXzY1ePIfpesU6X/u77rrodsrjiIiea7dwyZsW8HsMuW2aVdA28iWwgrQhh2XzSwGDaC3g20BaaIvTIYV9yFg2ccvcZHdMxCYR8XBiuAJMP8dDGqH7jqNqLf2MNcyveTNWoDtmEBYO9iO2zLb/RuHswwDxMXAHQLi3LRPlYb/aH79RP7+Z9uqOowhOrWL15/Mu8NnlqTJVnzqCuBfrx51DWyi+TUKR486u4gQFrs7+khTSMRGf29NaDUtNZfmgRfvYoN/sxBfxsUJVy2MWeKjC9MkQsOUwNRM+dIGsBss/9rB+AcWMbv7xLkYU9fmD/JFPIw7ChujzWjZpfpqPegm++7d4X/Y 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)(136003)(376002)(346002)(39860400002)(396003)(451199021)(40470700004)(36840700001)(46966006)(47076005)(83380400001)(426003)(336012)(2616005)(4326008)(70206006)(70586007)(8676002)(54906003)(7696005)(478600001)(1076003)(26005)(16526019)(186003)(110136005)(316002)(6666004)(82310400005)(2906002)(40480700001)(36756003)(40460700003)(41300700001)(36860700001)(5660300002)(82740400003)(8936002)(81166007)(356005)(41533002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:42.9648 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b9674fdd-f6a7-498b-3a28-08db2e090e25 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: BL02EPF000100D0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8819 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442174068527345?= X-GMAIL-MSGID: =?utf-8?q?1761442174068527345?= 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 c1c8c581759d..4026ac30a79f 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 ef80d361b463..a71996e51a9c 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -134,6 +134,7 @@ static void enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return false; } static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } +static void enc_init_noop(void) { } struct x86_platform_ops x86_platform __ro_after_init = { .calibrate_cpu = native_calibrate_cpu_early, @@ -155,6 +156,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 Sun Mar 26 14:46: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: 75093 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976067vqo; Sun, 26 Mar 2023 07:49:28 -0700 (PDT) X-Google-Smtp-Source: AK7set9XePOaBoa76VB6ogTDdb7hJiAXQR9403RsFUnus6AV2kpLoc45t2rjSAbkyVr9v+abTqbL X-Received: by 2002:a05:6a20:c28a:b0:c7:770a:557f with SMTP id bs10-20020a056a20c28a00b000c7770a557fmr7765619pzb.50.1679842168093; Sun, 26 Mar 2023 07:49:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842168; cv=pass; d=google.com; s=arc-20160816; b=VAt0DOwU2LudXlk5uD8HiYumOCJvEC4cb+E3by3D0sKGWIVW5kWh43pG1DyHxFLBFT 4GJVd0ZQgePHgXXDPHEchVBp8O0flcJuDtPI+wF7QlraHozbC4lVXqJ+hunk/jJy+bDs JqlLnczrmBED1okQYFbf4aC9VdTWPGAMDSjc2mOT/lZCayfAQF6flpiyGCgDg7IPIPDm HInbV/kIFzxuSYQTksN/oG0b7v/nhIfRKZp9geEXW+x2DHBypylqx6xcWDJ3TANgKpy7 5qrlSBMnlISPtR2Fh2Buu7z9QVkykBmWU3NYV98pLQw23J2r+V5oDhcxlFM+lYmkAAFx CXfg== 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=l2HoLyHFCavrP2EYHHagnBQpLIhkR5uHWiKsotNsC5k=; b=mfDaycgiYu18Y2yrT65lDVjdsyZzHVoi7UoR9KUsdSTJWZMINPfJ09RFB5ItGZGSqf pJHsN0101HdvLHHDTDOu+05FVPbkEf5s0/wMZanRZ/xykzzGi5+T9aNNdEwEz8snjhKk 1ePufa9llJ+s3RQRiQPzgZx6tUx1AeLUirbLv0TWTZeaIlFIUtxV1zwu7iV5wKS/L297 kDqxnYQXRt+UL1Tsgee2HEjAF4JUABxXCt3Bc3FIgp6tWp8YyEtMUq3JScrn+52J2ZGu PAEDzVOzp7+mO/vqOXQ6+R/VXtQCp3lh/OuEqfBGPDyxfIN+I3FCzjN87u5zTC1M6+xn 0LVQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Z5oAlUKe; 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 u11-20020a6540cb000000b005034a5a0a58si24835373pgp.434.2023.03.26.07.49.15; Sun, 26 Mar 2023 07:49:28 -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=Z5oAlUKe; 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 S232134AbjCZOsc (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232323AbjCZOr5 (ORCPT ); Sun, 26 Mar 2023 10:47:57 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2067.outbound.protection.outlook.com [40.107.92.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D7B97A88 for ; Sun, 26 Mar 2023 07:47:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PxycigX4V25dMjoswm9lzFwIrJDh2gB1PndVR6h7PqmRBDKSwPcLCzfLQl+INH03xDp/K6P1fJu/faoYQY/xQUMmit212V6gqN0Crz33LLPD4r7BzrRNLhlSJfDgwpZ/n0x6UZvUuT3uRDcAAB2c3aWSGAxejnQ88ABQJQyPbebOSgCOyNI3k08fUcUYpuXLKEDA1MOYTuRA+izR7rHtmQkpgaO91/tzENeqQt9k/vu5+V5qyfz6sXpfUXojZiP8LqfA91vmJtdq4Ehq5qmnLwBSeMZIHfOn0YKRKuv+fWZwCxmkY4wSZuzR3KIrypFnToAmCeWP2Fadqm1UAFeI2Q== 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=l2HoLyHFCavrP2EYHHagnBQpLIhkR5uHWiKsotNsC5k=; b=l7EIO5bSvNHi9DIpo+/zsLQbiIQSC2oYMDsSU4LWhgvUJbE2NXyoovdNndx+jdXwSThFmNV9Mtrv+g+x+hh/F1XiNg6z9C70ylme8xgF7CLkKdLdFG6MhMxOHgn2sKuDXnpkDoe8dZioDvKEyhGLXtiz0Hb4Bt2A068VwQif1WNRDGtuzSJY1VCFDhmfWCin+8nOlSxSKd5pnEa86qocdxrPt+E0I0GDQLG4/Rn+EZlzzQncb+wrxZuyAiUc2Rhu7kMKFH2aYgaDd7Pk6wbCbxhStt+mV9y3bCL/cvjsB5OE/HYZSgJrQFyC58zcZtz4Gxx0hpNX3V1Kf0co+029mQ== 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=l2HoLyHFCavrP2EYHHagnBQpLIhkR5uHWiKsotNsC5k=; b=Z5oAlUKeL8c78KRRnniUtVW9h1TEGypjc1e+pRi7O2dnI7CVaUwArHN1IAzlM0oeCbvnlRSqz4reIPf5pNmBPD9tGS4H/eQt76wIk2IQhUJOgmzrrC/KEZLCe0H4UN87VlMxFDrau92onDfJs8etIpuiE1NM0dAd0hKTAH/EY4g= Received: from MN2PR02CA0015.namprd02.prod.outlook.com (2603:10b6:208:fc::28) by MN2PR12MB4206.namprd12.prod.outlook.com (2603:10b6:208:1d5::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sun, 26 Mar 2023 14:47:46 +0000 Received: from BL02EPF000100D3.namprd05.prod.outlook.com (2603:10b6:208:fc:cafe::93) by MN2PR02CA0015.outlook.office365.com (2603:10b6:208:fc::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.40 via Frontend Transport; Sun, 26 Mar 2023 14:47:46 +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 BL02EPF000100D3.mail.protection.outlook.com (10.167.241.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:46 +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.2375.34; Sun, 26 Mar 2023 09:47:42 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 07/11] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests Date: Sun, 26 Mar 2023 20:16:57 +0530 Message-ID: <20230326144701.3039598-8-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D3:EE_|MN2PR12MB4206:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ff4fcaa-967b-4f6c-06a6-08db2e091029 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hVgTUKceVo6YdObVEQ4JQdnu7qK3xQP0BVnhJ0eN2Q+lXfAq+m0c4gwWGtDrAVbMM0ttfhlGbJomdH2YPE5CH9O7Ro+5pQBAepp+Hj3IOEshg3MFmgOM6QI+Q31x6TvNJCzyiwDyZvfWCStem1gVdHH5F1JjqlFQ0Zad99F5loKl40B5u1NKG2EsuDYCBDrPOSDtLxYglZ7+0g9ui8xruuIkAdFiSMHi5tFu7B+DbPidZyXMekOUiiGyM3R5GCNz4g+S9xUgME77/AqkMfxHVxbE/Q5EtjmQEgqvyWGPUp7HbrDUKzmLTtAQKU0HymhgKSp6/mLsvpD1EH/TcXj1zzj3Q1cvWCi7UXNtbbsbKpXJVRnsWWIS9hzH0aN+NFWwUcfrsJk4n137ih38aYiWjfrFL8i4e6xK0BRE+Ij/vCSjenD271d6/rUg6s8GjhOSfCrbmKMPxd5BJnEYDdNXaGiAS2/sDfS4NxEAKVNor+gnfxSVHXmBqHsFprBMmW40kKiD9WHU+iB/gel04l8dPef1lUYMFget96t2feknkFmyvsBfDLdLgxy9A8jg05VpLFT/1KiXCjFT9Ccw/u/37xT+q4+5qo3QhSxCCEieZwpaRNHM6Z5GJECVQcx9Fo3wBd+Hx9UhTc1dyaNXdn5/Rjwak2qDFZUd+w8kTj0kyexK9xloEd7AS/kKP3n9zqmV6jXIGM+o42iVpuFB/ZQJhlyeWdgtl62+58l/0oUGHFU= 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)(39860400002)(136003)(346002)(451199021)(36840700001)(40470700004)(46966006)(82310400005)(2906002)(426003)(47076005)(83380400001)(40460700003)(186003)(16526019)(336012)(2616005)(40480700001)(356005)(36860700001)(8936002)(82740400003)(81166007)(5660300002)(4326008)(8676002)(41300700001)(70206006)(70586007)(36756003)(26005)(1076003)(316002)(110136005)(6666004)(478600001)(7696005)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:46.3295 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1ff4fcaa-967b-4f6c-06a6-08db2e091029 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: BL02EPF000100D3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4206 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442181108320306?= X-GMAIL-MSGID: =?utf-8?q?1761442181108320306?= 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 c5ca97aab8c1..3750e545d688 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1642,6 +1642,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 && (sev_status & MSR_AMD64_SNP_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 Sun Mar 26 14:46: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: 75097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp978992vqo; Sun, 26 Mar 2023 07:57:25 -0700 (PDT) X-Google-Smtp-Source: AKy350Zt5qjqb0ua1bHF2W7zdaxpS/A8e0cq6MYA/O5pdoAqVW9rhQrlYhZH40TiydGBXlERDu5I X-Received: by 2002:a17:906:3118:b0:930:e495:b1cb with SMTP id 24-20020a170906311800b00930e495b1cbmr9800219ejx.75.1679842645211; Sun, 26 Mar 2023 07:57:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842645; cv=pass; d=google.com; s=arc-20160816; b=MRCQMn3ZxmpPKqj32flWD6N4osBB8I6rn21cElSS0Qhar8n4+zVKjNJWPfK/upXPle oyCd8v3RNns/XG35mR3aiqGn5avw/erLSCH+p6IcVoEh+W3Lu1s05s8j0LlWlTUX+XFV MaQZ2c9c9H0m4ZEEMumoHlljBR4LgYZctAxLPfSbaBgANWx8WW3Z9C7tscjF7BKv+ajr irglCuX0qqin6nLiCn3Z0/YEDKBwGMoYKRgfJlPX0ibmCUuzPH5Og7sc2hIvvdNkqNXZ LWIgzmsEJwxcl+E5PsBjwb0JrA3zh3qJ0W9FO542RedJ9iOxrSwM4+X8xG+PyD7dgCSM L2MQ== 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=ze86LUa0Gg0kC2N5eSAqM4UWybZ3yRCrNeXFDD+tz2E=; b=SgV1oyiQgyTEjRiCJHoRloTG2LdOm2wSzPJXM0Qe3fhxAKIf49LPUcbVCVj1t8peGm IqA7DuMSXnys0eSOpjZ+Z7uR18RX8msDVVG+xcbxzGv9v3jE6wsiiD93skEbVAF7FfiV MVYOfBqjC+3Ce0bvleIa3Wv2VsgezXzxqASe2zuJXa8ENf03P8B5KfqunKF9nLo6TmCA 0uYFAJshnKikrOxi0au80bT1IUkv34/96iiDIknIMm4ADOCTbg3KUdz0lM1xDRPEf6oT oYrpB2ImEjlPoWn7bK4GHFJpwVu6mFoPXTFZcMBxW/krUngh5we35AFilEx8LLz1K2w3 ztnQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=4EIp+EKC; 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 r26-20020a1709064d1a00b008cdf4a9c9a1si16090905eju.622.2023.03.26.07.57.01; Sun, 26 Mar 2023 07:57:25 -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=4EIp+EKC; 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 S232178AbjCZOsj (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232401AbjCZOsB (ORCPT ); Sun, 26 Mar 2023 10:48:01 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2064.outbound.protection.outlook.com [40.107.95.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A4B865B2 for ; Sun, 26 Mar 2023 07:47:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SpaJKreZt4cH292xJtun1lwA1L7YWJKE+fwCtuzmRoiIx1RBSfVwWq9bJFcF/goTKZweRyc/u8k2d9C7LbN2znCDWY2VTs+uQXE9EdQT68eTYseVE3+OeoVi4lKTmcBo8XGxIwfsHu4K9VAwtJGoBegcr4QhRViuQyqkbhDeP9A/2lwsSsqOmdFTMMqxUCTUEDigclexorFAXolN3M+XJ0ro4uf9DUkJtynGikaRVwFI3IHH+DvIU8F1SSfxdrFgdSBy9OpcdNZtbAYoROeuAqfFfbBEdvFvqLnqkHuPFSmE/VyqgKKaAL3VWxaT1Pdf2I/kuZ7kopibX5vW7V8Rxw== 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=ze86LUa0Gg0kC2N5eSAqM4UWybZ3yRCrNeXFDD+tz2E=; b=EVG3VBZqrQ4IZCiADU2oasJ4RLi07USDllN7WgdH65gCJW4r+uRAwAJE8CnuqVGWJDKLNZzk1IOEaVaFaHXQtmrfFm6w16TI/LxDcAXd5DUrAVWx/sNNKmGOC2LnYLPJCsQGdubO7dnS4Cd6N9ztff3M1JAE51Rc+4lbiFbSnQSn2MLZfateO2JgoCzz4pkMD/3gWTf3p+ttRr65hV2kdlOYfo4DYyyn/wg1xoZEqMUcNZMXxZ9u5SzFBwZW/QSMJ7utYw0lmMu72oy/msDjRLU3nlhyN8oMmT7vSAX9S73Dk4FtVXlnuVk6op8YK+xMofbm8NPH45vFwDJtkznRqg== 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=ze86LUa0Gg0kC2N5eSAqM4UWybZ3yRCrNeXFDD+tz2E=; b=4EIp+EKCKdtGUc43fbroBKOa8hlVUwK5jCbGymrVB+64I0uq62l8p4uNVh1yIeR9ZL6fDt7Gv64/fn0Io6EZgZhli5thJ76XpygwIRvgkrT61vB28GJFwphbIyKrobmBgixqAFEPHqggFI4IB1XPLxiD0GJnrO/ipSkJ3GbNeBE= Received: from MN2PR20CA0024.namprd20.prod.outlook.com (2603:10b6:208:e8::37) by SA3PR12MB7904.namprd12.prod.outlook.com (2603:10b6:806:320::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.28; Sun, 26 Mar 2023 14:47:49 +0000 Received: from BL02EPF000100D2.namprd05.prod.outlook.com (2603:10b6:208:e8:cafe::8d) by MN2PR20CA0024.outlook.office365.com (2603:10b6:208:e8::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Sun, 26 Mar 2023 14:47:49 +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 BL02EPF000100D2.mail.protection.outlook.com (10.167.241.206) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:49 +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.2375.34; Sun, 26 Mar 2023 09:47:46 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 08/11] x86/sev: Add Secure TSC support for SNP guests Date: Sun, 26 Mar 2023 20:16:58 +0530 Message-ID: <20230326144701.3039598-9-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D2:EE_|SA3PR12MB7904:EE_ X-MS-Office365-Filtering-Correlation-Id: 8cef3afc-b41d-48a0-2834-08db2e091218 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Jn2iC7BOyJuphTaZNMEks3PldhyOddFeaTEO30vZMc9+VNZKBta+sY41Ry1vEQ7zSwcQg+CmAa7SOgwrvoxsGNhNmEu6JI8C6C2i2HQXj2uWbeFYRkVZpbe/N/j9EbZY/K+qLlgPSDm7md2pKhY9FpD38a2OiuTgJ9GH458zKrWVAKHiHghqz4erMqx6AovBfuvMnMHPz2hXXxQVpB8cdeywyRYG2Md/DDgvAFLckp/8lOsvAbxS/SixORh7zGna/FBCW2UfU+gerqTMNp53rIWvZo0guiwquxMnZ7L3U9y4mBYWtrylgntebWYVgiOrP+nXJYiZAu21ZggRj+rkCoG7mxO2O7qSb+MwQRKfAO2xTv6hUtaGWfL4CbfJLdqsB0pssN2PlC/OziJRRGzj2kFuoBdBOzeU8L6VIw5X//bDXOB5gFtHkkMFzuoqqi5WqZJ/TA2ac0zn4O1OLbTMjgAV2xuSC/HJUp+WY6uiJWg1+7C0aagTEXLTd1CM7mWGgRQE+Cjk0PNPSlu3SZ4XkY190GdqV3NYMbb75ynFF3mIUz+Q1WiLxbFtGqOo21UXWBPJ3xKVsTtCTjN7cSPT0Qd2L+5AjQw+VGFfTHoeYdwqhiQJja1ZarIFZQJWLLuWz87msSOvr8jktlgBJ4KbIGzO6ghtMjBy9kTU5risGDVi6VTVAHfRdyprq0R+9hb8YBx7cRh8RAtSCEdhCIMgKPbYfnzln4euVerPx2t6c84= 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)(136003)(376002)(346002)(39860400002)(396003)(451199021)(40470700004)(36840700001)(46966006)(47076005)(83380400001)(426003)(336012)(2616005)(4326008)(70206006)(70586007)(8676002)(54906003)(7696005)(478600001)(1076003)(26005)(16526019)(186003)(110136005)(316002)(6666004)(82310400005)(2906002)(40480700001)(30864003)(36756003)(40460700003)(41300700001)(36860700001)(5660300002)(82740400003)(8936002)(81166007)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:49.5744 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8cef3afc-b41d-48a0-2834-08db2e091218 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: BL02EPF000100D2.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7904 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442681474285556?= X-GMAIL-MSGID: =?utf-8?q?1761442681474285556?= 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 Security processor (PSP). This communication channel is encrypted between the security processor and the guest, hypervisor is just the conduit to deliver the guest messages to the security processor. Each message is protected with an AEAD (AES-256 GCM). Use minimal GCM library to encrypt/decrypt SNP Guest messages to communicate with the PSP. Moreover, 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/include/asm/sev-guest.h | 18 +++++++ arch/x86/include/asm/sev.h | 2 + arch/x86/include/asm/svm.h | 6 ++- arch/x86/kernel/sev-shared.c | 7 +++ arch/x86/kernel/sev.c | 92 +++++++++++++++++++++++++++++--- arch/x86/mm/mem_encrypt_amd.c | 6 +++ include/linux/cc_platform.h | 8 +++ 7 files changed, 131 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sev-guest.h b/arch/x86/include/asm/sev-guest.h index 834cdae302ad..d5ed041ce06b 100644 --- a/arch/x86/include/asm/sev-guest.h +++ b/arch/x86/include/asm/sev-guest.h @@ -37,6 +37,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 }; @@ -75,6 +77,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; + u64 tsc_factor; + u8 rsvd2[96]; +} __packed; + int snp_send_guest_request(struct snp_guest_dev *dev, struct snp_guest_req *req); bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id); diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 36868e21c3e0..d05cbab5e9e0 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -218,6 +218,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); +bool __init snp_secure_tsc_prepare(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } @@ -238,6 +239,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 bool __init snp_secure_tsc_prepare(void) { return false; } static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err) { diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 770dcf75eaa9..c781f8e0aae4 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -404,7 +404,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; @@ -536,7 +538,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-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; diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 3750e545d688..280aaa1e6aad 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -72,6 +72,10 @@ static struct ghcb *boot_ghcb __section(".data"); /* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features __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; @@ -1107,7 +1111,7 @@ static void *alloc_shared_pages(size_t sz) return page_address(page); } -static int snp_setup_psp_messaging(struct sev_guest_platform_data *pdata) +static int __init snp_setup_psp_messaging(struct sev_guest_platform_data *pdata) { u64 gpa; int ret; @@ -1406,6 +1410,80 @@ bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id) } EXPORT_SYMBOL_GPL(snp_assign_vmpck); +static int __init snp_get_tsc_info(void) +{ + u8 buf[SNP_TSC_INFO_REQ_SZ + AUTHTAG_LEN]; + struct snp_tsc_info_resp tsc_resp = {0}; + struct snp_tsc_info_req tsc_req; + struct snp_guest_req req; + struct snp_guest_dev dev; + 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; + + /* Zero the tsc_info_req */ + memzero_explicit(&tsc_req, sizeof(tsc_req)); + memzero_explicit(&req, sizeof(req)); + + dev.pdata = platform_data; + if (!snp_assign_vmpck(&dev, 0)) + return -EINVAL; + + req.msg_version = MSG_HDR_VER; + req.msg_type = SNP_MSG_TSC_INFO_REQ; + req.req_buf = &tsc_req; + req.req_sz = sizeof(tsc_req); + req.resp_buf = buf; + req.resp_sz = resp_len; + req.fw_err = NULL; + req.exit_code = SVM_VMGEXIT_GUEST_REQUEST; + rc = snp_send_guest_request(&dev, &req); + if (rc) + goto err_req; + + memcpy(&tsc_resp, buf, sizeof(tsc_resp)); + pr_debug("%s: Valid response status %x scale %llx offset %llx factor %llx\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; +} + +bool __init snp_secure_tsc_prepare(void) +{ + platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); + if (!platform_data) + return false; + + /* Initialize the PSP channel to send snp messages */ + if (snp_setup_psp_messaging(platform_data)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + if (cc_platform_has(CC_ATTR_GUEST_SECURE_TSC)) { + if (snp_get_tsc_info()) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + pr_info("SecureTSC enabled\n"); + } + return true; +} + static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) { struct sev_es_save_area *cur_vmsa, *vmsa; @@ -1506,6 +1584,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) { @@ -2698,11 +2782,7 @@ static int __init snp_init_platform_device(void) if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - platform_data = kzalloc(sizeof(*platform_data), GFP_KERNEL); - if (!platform_data) - return -ENOMEM; - - if (snp_setup_psp_messaging(platform_data)) + if (!platform_data->ctx) return -ENODEV; if (platform_device_add_data(&sev_guest_device, platform_data, sizeof(*platform_data))) diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index 9c4d8dbcb129..7d2388e52b8f 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 Sun Mar 26 14:46: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: 75095 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976146vqo; Sun, 26 Mar 2023 07:49:40 -0700 (PDT) X-Google-Smtp-Source: AKy350bTqgu0hW6Mw7sLkUWfyEXJ4WP9D8n2z+y+zGFgXKAlj9ErAgc/d/2OwBwiSmuJy1RbXv9g X-Received: by 2002:a17:90b:3910:b0:23f:e653:4a5a with SMTP id ob16-20020a17090b391000b0023fe6534a5amr10343053pjb.39.1679842179800; Sun, 26 Mar 2023 07:49:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842179; cv=pass; d=google.com; s=arc-20160816; b=oDhWznmChCFcV+yZpOCaGz/v/n9z4MJrWshfXMjCDS9v+1mit94y/p7Gf4E0xl0j9E /QQZjkTex4iVf9yufEQ/bV7WkhnjfyWc7+vUC4rgyuO0xMzVuAweSlluQBlCt2hVga33 3alEJH/E5QBuZPdEPaSYm/tYkF8D7UP2PHKPD93P8pqpasGqb9DGUOxI5DLTrM383GVF tSnKuHX61OCOSH0imGfYZNfI3RV3FQ0DiYmwkBvj9xrOT84MFO6malLiRBysptGfXMVO PxF9+hXE3pgUeUaU+W4jW6/LY1B6pGgAvbSLeNy2CmOpcjqHmUDdj4h+CpMATveU7o0/ B/OA== 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=NFu5SRsi1J2C+cEif7W5W5/+jg0BMdRNnahFkdxqaw4=; b=JrEX2czZrcz2m4eoVKHzGw6hGbV/UxQ6Yf7SIclbMV0n1ojgCFaLUZgXpGOVfyY0iQ k6RbzvxRmPP6VyO4FWVqDNhQ4WlKeJbhrJhVQTCrZDHc7yBbplov0zNFA15TrU2pLBDi HQL0TMgf8Uf6Rtx8JNSG2W7tZ3gYj47LEBIL+I6guHeZJ0Ab1Ggz1Q51nicIjONsDrkA ah1XsiB2lb0Yaqeam5wcCEGiwaSukIORqo0sqOGv1jvTck5xmYB4KoYXfCSnbr1hvzoe D7WaCZBsjiLU9YNVLDBt1RZiA1Kziw7I74Hpdoj6PEs50YVifD6UA+lqwparWrPMqTyn uUqA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Kegt4REZ; 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 v66-20020a632f45000000b004fb11698712si25972009pgv.332.2023.03.26.07.49.27; Sun, 26 Mar 2023 07:49:39 -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=Kegt4REZ; 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 S232384AbjCZOs7 (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231671AbjCZOs1 (ORCPT ); Sun, 26 Mar 2023 10:48:27 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2060.outbound.protection.outlook.com [40.107.220.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E9DD30E9 for ; Sun, 26 Mar 2023 07:47:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KlMiBHJLanl9bzP8PguXQ4mPf5Izd/odCyOsrVG5joqXb4AKv59sHXSyLBQxB+umWpyWq6iSZ7DeVrwli2/Wv7gNsnJ9PEQtiO8e6JbqsS1pR2k3Kh4GVK00WSFKAAKER6zqH6tKEasayLvMDsRfT3fb+c0rdKOVJOQsVwsGGeI/r7e3+gDR7W/zjmMEcNk7liPLW59g2Jt3+r2Q67Z7ND3QW/y7KvHkNstuio4JOtJmc+dhiA5bJN96Xe2tFj21xfxSMHm7JHJVBVyLQdB5BpOYKKb+uyzV69xb+3b65vuPULe/FvCFU59KhO9l+6wS6/lbaRUx6biPXJwl6Xt0Yg== 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=NFu5SRsi1J2C+cEif7W5W5/+jg0BMdRNnahFkdxqaw4=; b=PSr0OdH85vcD8RHPmEwTiy8K8V+fMXrQI4owDaQk5LqPIV3I9YRrVE8dYf3cfRaSk3Hxxtz0VHfjEh3D8uvSBi11BKTLktUIhy2eD3UxLokte6MCeDp3A1pO00ID59HtNe/kjEK1ulCyC1h1tkIlTjVQ3XNZYdNEBB2TzEjxTlTAmTmYMqDjANjn9YbrCwFCS06crq1PCIY6O55o2efZSyX7gHqjE7UynvCh4amotMMnQjbgTwMjFvm7QsEmW4Ls49BBVox8KtCI+gZBSeEdX3LLDeDmc4ND07PxC1L/bM2OVt8G7vu9ymyZMvhI54SysHVM381P58B2XWupdbWOww== 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=NFu5SRsi1J2C+cEif7W5W5/+jg0BMdRNnahFkdxqaw4=; b=Kegt4REZ5DLk8JUCDdBs/Mb5kJi83zi8qu+Lmj9sk0q7MmMAzst9sRwVetVmPb75gOvpdkSynSRDn9UlzrtZa3xDvOyQorO+8IaenK67Eg8TAqKnPJSwEoxU/S0Fo8KzYj6He7bOivR6VAYmUv3jalSQpeE4l3KqSvTWjD8/fo8= Received: from MN2PR02CA0011.namprd02.prod.outlook.com (2603:10b6:208:fc::24) by MW4PR12MB5642.namprd12.prod.outlook.com (2603:10b6:303:187::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Sun, 26 Mar 2023 14:47:54 +0000 Received: from BL02EPF000100D3.namprd05.prod.outlook.com (2603:10b6:208:fc:cafe::82) by MN2PR02CA0011.outlook.office365.com (2603:10b6:208:fc::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Sun, 26 Mar 2023 14:47:52 +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 BL02EPF000100D3.mail.protection.outlook.com (10.167.241.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:52 +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.2375.34; Sun, 26 Mar 2023 09:47:49 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 09/11] x86/kvmclock: Use Secure TSC as clock if available Date: Sun, 26 Mar 2023 20:16:59 +0530 Message-ID: <20230326144701.3039598-10-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D3:EE_|MW4PR12MB5642:EE_ X-MS-Office365-Filtering-Correlation-Id: 247b6730-66c0-4292-3afe-08db2e09140b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lZ/Ct+nFtwmXkRduVx9WTqee0FAnYHJZf72iX29PmSEypCkdBTzTlehjCBr4hzMFFyKd+uhHGCXugpu8LTr2fJjCObtAAwajF5UWAxVSVpdyAK3kChVEn5I7GwM8/TXK+LlBCqLVBUcgSb+Y2tdGeadeGxh8imUbdfqf473vZ7sHRWBX82suYARGSQpIM6KH349jlD1N++v3gX4ui7W6PkVTCF6d5f6YffisH/9J9GtXW5E29Ofg7OMAcr0Jh6piGn/7EvFzc6NejVVNwVqEnqWADk/KGG+nGTU5iiklXT38MtzLLSll31HZPwOlel5DtbD1KQ+WnOTJ/wZSVpXC6XR406NAC2DhFSxcDxNacec2oT0eFlbN2mGXfgViIxgqMLmPe/ee9LTbLdgtmlDsuRdOO3Wxm8qqpAD6C7gpd5NyaQpSrIHaNKkixUdXfxNuPfRcPPGE4GbFqO9uMPQfwPxMc0C4QNAGv0UZb/ChZk84A7LmpJMZFs8s5tSin/ZjhSEmmz6Df+SLhVqt9ueVhr1ryH+Wo7i+NaxQ+XOPwb3HfNDLUcCD5l7TlNugqEGoV0hC1hC/qu7xllsF4qDhaStbB/fuK5TPF7mVQtoB07jnPBpQgA6GWDicvBN4vY+Ord80RKn9WQcHbgNjtGkDl5/AeJ0T4Oz08Oke2SvQtCj6y6SOpu7DQrB2df/BlUt9J/pLSBLk+8qVZ+7zlNpkZeQeBEX7zZVTlAP010ww9dw= 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)(39860400002)(346002)(136003)(451199021)(40470700004)(36840700001)(46966006)(316002)(4326008)(5660300002)(8676002)(70586007)(40460700003)(70206006)(2616005)(82740400003)(26005)(40480700001)(47076005)(426003)(356005)(36756003)(336012)(41300700001)(2906002)(54906003)(478600001)(110136005)(83380400001)(81166007)(6666004)(36860700001)(16526019)(1076003)(186003)(4744005)(7696005)(8936002)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:52.8452 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 247b6730-66c0-4292-3afe-08db2e09140b 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: BL02EPF000100D3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5642 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442193363590438?= X-GMAIL-MSGID: =?utf-8?q?1761442193363590438?= For AMD SNP guests having Secure TSC enabled, use Secure TSC based clocksource instead of kvmclock. 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 Sun Mar 26 14:47: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: 75094 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976114vqo; Sun, 26 Mar 2023 07:49:34 -0700 (PDT) X-Google-Smtp-Source: AKy350YlEhELTvDekCsR+PA0hyAhifQvwKw+xlni2cnOFgUoQc1fCXfCOs6UnMEDKkuHC7FuEAd/ X-Received: by 2002:a17:90b:38cf:b0:23d:3878:781e with SMTP id nn15-20020a17090b38cf00b0023d3878781emr8405972pjb.21.1679842174271; Sun, 26 Mar 2023 07:49:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842174; cv=pass; d=google.com; s=arc-20160816; b=UKDqLH3r5+5brPsyQ+0VWFH9D8wfe6bP4v7QrTTiwb1tC2zBdZ0mnbkWWaqAsDkhGr RjG3rDA8yyIm+33c6ZtqW3a0T7TRXSFwwAuHuu56Zye2KOZDOtf69avnYsgnAqyJ4sp0 yUuFpDxKlvbEUXDDs49PS4RA32ahKBBaa0sjeqNGwOvUQyO3NRc1RDrVWvVs5qowUWGZ UlixKSGFUdxT81LN7kJn/MsNrudhE6zLfB5iCrYVsTgj07NkMqFnp0lvpe7mpVzIBi30 dYqAgaFa0emZAEBkf3/LpDS97pflImf8IpaW5WPqJ7/z326csX/fnjfMRYXubdtXBwmq acOg== 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=; b=U/oolC86+yBUtXviSjRbxwnYkrhnTzSeduGEYctgu5KQEOSrkWmZrZeHEYfV6AL8DW zZmjXVqLMjgnImM+2e7ELD1aDeYLlskOpCHkCLHe47CAC2sGU1GwkThRjB2AMKtH+o04 Mh88Htm8rOyjfi6+r1kzQG4gLAFKUiBrkema8UewCpvmSAB5hkyl0H1JyuNHnDZ4XfNC /aVFFRzTAqmFBlfGmFFW4SORJFPzdCqX7IJsUzo87v8N47Vz7NvprvXVQdwMjmbQf3FG IZtPnCupKn/XU7McCyXPtEP3URElAa6ggmcg4eG7o8628c7K/rFueeYpD2qUsw8fIz8u vA0A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=YYUlMbrD; 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 6-20020a17090a190600b002308ac7a1b0si8682338pjg.117.2023.03.26.07.49.21; Sun, 26 Mar 2023 07:49:34 -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=YYUlMbrD; 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 S232106AbjCZOs4 (ORCPT + 99 others); Sun, 26 Mar 2023 10:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232083AbjCZOs0 (ORCPT ); Sun, 26 Mar 2023 10:48:26 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe59::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47BA03A8E for ; Sun, 26 Mar 2023 07:48:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RbIJ0EdclACJ737HO61KoPikIlgx1CTHPRxG77mVtRoE6ufB5WdNH4osjAyxD99nGkDrsBylg5+vsDtCA/JsQz2IEpXhxfGLoEVqTmp6CFwdWNYHf2OKb5phrXsxzYjNOnr5bhIt0CbkKPy/ypvVnf4khidCm37YQkn6mrKjS5mcvKpPP3r6M7ulYxB762hia/rmnSm8lKA1SZV3MrvgJez/gbvzB09UlBlExmCIJ6ZzN3Jk2BsUYscS35Hr606580a6YIATLB6uak6rT0oUrem10nez/Kg662kA3ra6B6gzre4EiQSPPstoypyYjE7NsZR9Slhy8JVLQEgHieLrZA== 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=Q3YMgFbeM8HrHPlonEsoXUdzAuv07czZmWWY/5T335rnqmKYErTsrBHKlWT3KSSge27euFapSwyLlopSUio8zh1Tm6DdCmthwawxjHYV9FMg4aDsO5i5XG69/QI26fBYxIv+KT3cUg8nLY8nsRC1F1QJ6pDO0rPhbIxV0xNLjt63tnR0e67k8V5qPGCxft8pCGP3femcno5guDArbRlvKgBbqSDnM8CPJ30h+iT3QbdtxKwAgp8FWFDQa+MeWTuQuEn3tLytT6sa++SDr36LUHmJGnGcDFl2a0Z3G8DFEa4PKS5Hw19wtHvHtbYb0rC1AR5tanUhsAvnFYPHI9DBow== 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=YYUlMbrDLcHE30HJ30/F9RNblwGHbTDe97D31Yk1UtY0Pm6/zbQgD/PfYYJzAJZv3rrer99KL38nhN2NNwSq3Dvh7HWuXYGZ0heBIV1zQRVZWX///qZGDHvxHFKCzWGYvpulF/nJtiaw1Q8upFWBD74TgYa4oRhEeu6oAY3cqms= Received: from MN2PR02CA0003.namprd02.prod.outlook.com (2603:10b6:208:fc::16) by SJ0PR12MB5422.namprd12.prod.outlook.com (2603:10b6:a03:3ac::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sun, 26 Mar 2023 14:47:56 +0000 Received: from BL02EPF000100D3.namprd05.prod.outlook.com (2603:10b6:208:fc:cafe::96) by MN2PR02CA0003.outlook.office365.com (2603:10b6:208:fc::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Sun, 26 Mar 2023 14:47:56 +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 BL02EPF000100D3.mail.protection.outlook.com (10.167.241.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:47:56 +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.2375.34; Sun, 26 Mar 2023 09:47:52 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 10/11] x86/tsc: Mark Secure TSC as reliable clocksource Date: Sun, 26 Mar 2023 20:17:00 +0530 Message-ID: <20230326144701.3039598-11-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D3:EE_|SJ0PR12MB5422:EE_ X-MS-Office365-Filtering-Correlation-Id: 2d6289bf-f5e9-4e79-c0bf-08db2e091617 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hDs5mVcPw5lzNOmwGrd8XYNC0eDInXRcv1iFnSEvwHNrgpYQcvax+5wDFZCDiAaBcKfqvu1JsN3Cax9M9wzEkcSlbZE5iQDjHFoFWKtIA0sKhaQ7aWvxwnFUeYFU6PcANnsPO3F0NY+w9lr9iByZyt4ExfNQ2Y67R2Sjd1HuKSP6nkOSTeOsKEgZkCorVD8I7FvpsVM1IUywfoHMa7XcrdorN/LZ7XVs+KNS2Q7uda/rK0GcDgxUZ7noISGsTbloQLS4xZgDgnHyExUZihz3GpViOaOwxpBFSr3+0yub0nxX7AoN1OFFLmvdJ2V8GlPT9bOCn6VWUZYE+fnwmgALQC6quzVQgZjVP8UA7oMcFNnydITnkrmCNVqRLPo+6wvWOXE90AHLOhpdap7FS7X7rWQHCfhoamvufE8Dzy293wXv5to49+3vZr1XZVmU58tBztIu2AaEyu4JjMJJS6DLk9yblUp1bnUjgENkrAqwfSa7mCEIEgaCTkjAnmqFzu2YgiyUz1N6dYwgceL1EnTd50p0MlZodk9ikMzrxg1K3x10eQYkvXcr+nvOY6QdfSC/kyCUEAYRgXrZV0HitlGY1TkCveKkOTlxnekR0omjOvkR1ZezsC3P1VTMZzSJpNcntPUv6dCtsWe3kqR42eaoUaN2gdbkNL0mDYJtG0yG3hLAhrDfN+T9PHUQxtcuXf1VYafzeFAJ6OcbopMgpAMiZ+nSXkq9LV4J4mlPZ7sp31g= 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)(39860400002)(376002)(136003)(451199021)(46966006)(40470700004)(36840700001)(336012)(16526019)(2616005)(186003)(4326008)(40480700001)(41300700001)(7696005)(47076005)(83380400001)(6666004)(1076003)(26005)(426003)(82740400003)(478600001)(316002)(110136005)(54906003)(36860700001)(40460700003)(2906002)(4744005)(8676002)(70206006)(70586007)(356005)(82310400005)(36756003)(81166007)(5660300002)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:47:56.2202 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2d6289bf-f5e9-4e79-c0bf-08db2e091617 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: BL02EPF000100D3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5422 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442187868623491?= X-GMAIL-MSGID: =?utf-8?q?1761442187868623491?= 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 Sun Mar 26 14:47: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: 75096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp976198vqo; Sun, 26 Mar 2023 07:49:44 -0700 (PDT) X-Google-Smtp-Source: AKy350asnxdZRAiZtrfAlgPMhCmt7s42frchZtjAgPTXKXAjC+NbJoajjN48SQHSxd16WyCwqex7 X-Received: by 2002:aa7:8bc1:0:b0:627:fd1b:5fe4 with SMTP id s1-20020aa78bc1000000b00627fd1b5fe4mr9378833pfd.9.1679842184210; Sun, 26 Mar 2023 07:49:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679842184; cv=pass; d=google.com; s=arc-20160816; b=0IH/9ynzZ+JxwM9eGeetWW3Oy/onG6oGlK5UnxqHiZdxR9CFk9dTkdY6lVDQDlJ1Dt 0b5q9sM4a/VqVLE2o8tUMErPNQbE/QvcuhjYpPkKBYvBw+o2wjirHozRpC9fyzwKGltr 2hwkwgRATZoAPRytW+UjeVloTgsjRZarPdCy4qUDB2Y1LJZIAuluSM3ssnVWiuesNuJ0 dkwU7fkwJfbIwfOy2227wU/yfxMVfTi/sJlQlPZygMDOGifhKHOfF9lqxAzlcFtoeS7x TKCshnv/1kTsV4aoPW8sAxAxsNXLBk3O0B8Rkg9Swm20ZX5zHgiG/DDnPqkuvYI+RfON ahSw== 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=hatQwVALEOXgXnuAEwDqSmbsKdsvwSWxgQtiCpU1jwo=; b=rdVeh8Obif7qeUWNDopGu9zaqpI5GT32ENo7kVUztN52Do/Hl/uVmDPCxasnjBwpV5 iKn718IwykK4hVSzgdWq337xPbeCujjFSQ1x8XPtjmHLHvQYVx1QOogbFM3pSPulMBe6 nk9R75UM6TPR74uwSSf2lXm1tQ6WpUuFfqg04q2v65MJTx7UklXB60apXG2hL+vrhZ/t /H73ZLZs+9PpcJkogRDY0BaUHcvRSmPEqVG2kj20pI8KLbv7+8BZyaOqO14Aq1UgXGWp ZrTJKpR3FUIDYfxi63Acas1xiTr89DN8N+SGs9qgqDoUn6/wfHon2EYHn3j/cSnXsBbV aKdA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=NS1q5hpH; 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 2-20020a631142000000b0050be5b718ebsi24673094pgr.272.2023.03.26.07.49.31; Sun, 26 Mar 2023 07:49:44 -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=NS1q5hpH; 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 S231925AbjCZOtJ (ORCPT + 99 others); Sun, 26 Mar 2023 10:49:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232267AbjCZOsx (ORCPT ); Sun, 26 Mar 2023 10:48:53 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 368C0468E for ; Sun, 26 Mar 2023 07:48:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hq0p9iEYBimKsK8xJ2T7D+LzOBIyVwWcAjE/liq1xewabc1Rp1k+rg4Ats9FPl8DDWcnX6LLu6OHmeQZYCtrzP/X7fFRsZ1lpRuV3TE89TmIfCBdzdRwRlJhPrMl9QIUr2iK8AzQfI0cCE6/xGym9guvnhIwyhEgzvAW8sywQgl//GFQxQe25v4Vtcw0g5DNvG4jlfKLItb8vl9OwirNadrS7P+opTx4FP/oS+tGku7zJM22MuuOhW5cSaTTyEnOM3rTIFJshmj3/GMmkykR7YXP0vX0tPc+5gEvyVuMjSe4oxrjPyFvExq78FGFAlpGhPFaimcYZzwgNenk0r6y5g== 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=hatQwVALEOXgXnuAEwDqSmbsKdsvwSWxgQtiCpU1jwo=; b=gmXjpAtEv9UHvxvqDDOUNNKK87wZkyy0MKUVTwpEGqkGYn4TrQ+3VKIpkD3NSIFNLowlQLSeN5cQ0Jr3WZ9Kxqb5n0QKqcEalMa+dZ+v5/jy6ZIMbHMfHx/bRYRlRh2wCi699q/IP1ozINXZC2cMpycC6rCTywTKRF4auzJO65cKVCqv6rroHcLT5zJDVYNQRkxm+u01XypgAcziY87IX6USitojm2pq8aVDqwR/TG8HoB6P6vqVMXFodqAW/e7aC6YvP+whCu2xlTTA1+V86Z35Z6TsqVAyHRfqIzBGK0nsHmHUs3Tkk2fr4xWCrlWcasdxIARYMurVEVt3w5U23w== 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=hatQwVALEOXgXnuAEwDqSmbsKdsvwSWxgQtiCpU1jwo=; b=NS1q5hpHRIZaiPjjvzT2ewq77qAjWVY3l3f0D4egOU7RtfbSG6Pu/xab1cNT8LxcahW5KNB4iXVa5SrylpHAC+lk5cQlI0F6p80+MBPxNXkGUYnwF+AzycE+Vj6XRdYoN5Ty4rlo1IOn0iQbdMEIVldy6rZj9cs5jLu9FcKJ+/4= Received: from BLAPR03CA0040.namprd03.prod.outlook.com (2603:10b6:208:32d::15) by CH3PR12MB7643.namprd12.prod.outlook.com (2603:10b6:610:152::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.28; Sun, 26 Mar 2023 14:48:01 +0000 Received: from BL02EPF000100D1.namprd05.prod.outlook.com (2603:10b6:208:32d:cafe::5d) by BLAPR03CA0040.outlook.office365.com (2603:10b6:208:32d::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Sun, 26 Mar 2023 14:48:01 +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 BL02EPF000100D1.mail.protection.outlook.com (10.167.241.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Sun, 26 Mar 2023 14:48:01 +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.2375.34; Sun, 26 Mar 2023 09:47:56 -0500 From: Nikunj A Dadhania To: , CC: , , , , , , , , Subject: [PATCH v2 11/11] x86/sev: Enable Secure TSC for SNP guests Date: Sun, 26 Mar 2023 20:17:01 +0530 Message-ID: <20230326144701.3039598-12-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230326144701.3039598-1-nikunj@amd.com> References: <20230326144701.3039598-1-nikunj@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF000100D1:EE_|CH3PR12MB7643:EE_ X-MS-Office365-Filtering-Correlation-Id: 2103e54a-175b-455f-7917-08db2e0918f5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xEXiwU5XqHA70wIY1hqwWCmj6r0By/PJNSa6t1FqW+1UmaykJiZ7U0XO5kla+SUE1Fc095ikwLSDAC6+rngwyc+0mDUwU71MoPGN54dSu30+W0iubGPLjZ5DMjLxSoHkobQkT7n9zMRD0g0euMnzlblVj4UjBes6gNlPRjvwKkhnkJtpCcAfutVlDcast7YHHiE7xf/1m5FQYlX6B4XJmgwLqOad1qKRb3kY8dQIDkFJmLkg/lu7lQH/DYrTcWGx2XX/erKDQ6108VORzDkk4mWgSWpZ6yWQJ8+yLwAYW1cynQyZdAW3xhOHYuwQ87HsL34sRtIgvb0hhCHlpve3kmIfh3IXcWha4ivnfqPWpN7K9jzw4KE1VzYjZq5AVhwZv5TQE9o1D/NCYRU8GAKsLsGdZSv4ODX+en0gOVDLbO3oTssuNWo3drKKgapc3k6S10ePCO14b2QVWU5QWqUWn71Duf5Xpvqi5wVW/+1I+wyFxksJ2Iu7VhgZWeCnJstr+cHX/NmP0sSQ3TmTbaybW5GC5xi134WSVNHV0+McBA49W9YO7XI22Hi8WG8alRcVTZZqBg8+X9pGyNmnq+LKbtogvE/2Ely0CnXm+18sN0KuEuhzhoTji/0FoF1PXwYdWknRW6+I+HgFbZowEbjbJC4fuAwiGXAJGBCDKP10UpFTEJOvAI/8pc5kLcvnGYZZMidCz3fNM5BUbCIYcz4c7zwkW1CXmsvN1y++4KuNhnk= 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)(136003)(376002)(346002)(39860400002)(396003)(451199021)(40470700004)(36840700001)(46966006)(47076005)(83380400001)(426003)(336012)(2616005)(4326008)(70206006)(70586007)(8676002)(54906003)(7696005)(478600001)(1076003)(26005)(16526019)(186003)(110136005)(316002)(6666004)(82310400005)(2906002)(40480700001)(36756003)(40460700003)(41300700001)(36860700001)(5660300002)(82740400003)(8936002)(81166007)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Mar 2023 14:48:01.0912 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2103e54a-175b-455f-7917-08db2e0918f5 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: BL02EPF000100D1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB7643 X-Spam-Status: No, score=-0.2 required=5.0 tests=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 autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761442197817663127?= X-GMAIL-MSGID: =?utf-8?q?1761442197817663127?= 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/coco/core.c | 3 +++ arch/x86/mm/mem_encrypt.c | 10 ++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index d63ad8f99f83..13b6ff192c3e 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -315,7 +315,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/coco/core.c b/arch/x86/coco/core.c index 49b44f881484..a3d93ed722c9 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -68,6 +68,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/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"); }