From patchwork Tue Dec 3 09:00:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891954 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2056.outbound.protection.outlook.com [40.107.93.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D477A1DE4F9; Tue, 3 Dec 2024 09:01:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.56 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216483; cv=fail; b=tgL0RH92TqTzxMWRnHDois6q9cbX5mR0vT+OCDRbD5X4mNXKEmCRYdYgwaOaBbgIszERDhLPH997WfT33iT8JHcQnD+53TnwvBXkIMvf4Ck717TegIZhFgREWgL34LDw9KL17XNwZDkQa4lIMbH0dMzKxYV3b38v+ApEY2DWUik= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216483; c=relaxed/simple; bh=F+foVLc233A2V00/YJkLSr4JmHFptVGwLoedSWjJgm0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WVxGZrapgGgHuJovy1des9wAkxL/8AJYzxydHXWypDfdZyEZR5F3Y47/NzQwyxzESGmDXt970lqGqzMNbU7z2U7U9Mz9TKVdR3hEL2+x7TuclNfN39Hjxt+0U31miKlr5Zw2NUWaaHWoRUbR5YcnDl0id9iRoV7NPhhZwJmRmiQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=bO5EwG/5; arc=fail smtp.client-ip=40.107.93.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="bO5EwG/5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pa1/X/r8eEN66qM59l6Y5AUJlZryv6t5JF5wES1N8bRG9Mn393wmp0g0xOYlbcMJGD5qBEaZGturfwcGsuYQnncY6bsOz8et3/pNBmP1bxDQjDASjclAep/z6tsrkhoC6YWZSUScdZmhHLh6mFEtNF4Zydg4yMralR3YVmc7WIsYt0C3CDKEdrnd6EUB7Qhu3vcBFaTW1P8Y8vtEP3kCNiC00VOtfZdMvuNx2EaIgJn7Q3KH052mx5AG6l+vBTGLHtJsg6ZGBmGW6iIclVPL+XMZ07Yau+gtz0dhzpLO4USaYkWHu4mx1C5SYRvLqYuCJ1QxIScKG7AgeBtsff0plA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=UWva6vLEW8BGE9aXbZ5/KZlsm+l2wyjAj2UdwU7eS8g=; b=cWK+2JwvHQWcL4Uk/ESRMyFBKP2ryzf60gb15Rd8M7+1iSpiYC5UuMC5B8/bT1bQJQ8TpzN1KgwIDL1CFCVBvxX9RvaaEKEjQQVmWRrJ1XIvGwpc4y+nsggCie8LeiriRtPkPYEEwLU93wArRNH1QdJj/B4xYSFYQhHhmB3PClCyS2h4oiLoHpyqZyCutOsgaWLHi+0or+0VIGG/McDAdkT9R687QN6WHuY+/7kT0eW7eFAka1PqH8XUzt8nDV/Rqzn5M5qpD7Efba4Wv0NhlqOxHyiFZnPIt5Na+h8ETTis8E9cS80x/M8lGOMAoi7phlVzYH0U6u0GrbQ0IqtHhQ== 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 (0) 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=UWva6vLEW8BGE9aXbZ5/KZlsm+l2wyjAj2UdwU7eS8g=; b=bO5EwG/5x7/dsoQStL8BonVzw+FO5wtF13aWaxUG9o+82kqgWzgOxmDmm6tRunVXeGxyavZqQxvrjz0jbc+n4bCcQl4Fo6IlCPmNLTj/7reo8OCGImej7IwbFqPUBnUgnkuwdHv9QNyn6s3yjk8MaP3ZMyPMJR4u4Uuwy/cAJDU= Received: from CH2PR16CA0002.namprd16.prod.outlook.com (2603:10b6:610:50::12) by CY8PR12MB7218.namprd12.prod.outlook.com (2603:10b6:930:5a::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.14; Tue, 3 Dec 2024 09:01:13 +0000 Received: from CH1PEPF0000A34C.namprd04.prod.outlook.com (2603:10b6:610:50:cafe::df) by CH2PR16CA0002.outlook.office365.com (2603:10b6:610:50::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:12 +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 CH1PEPF0000A34C.mail.protection.outlook.com (10.167.244.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:12 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:08 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 01/13] x86/sev: Carve out and export SNP guest messaging init routines Date: Tue, 3 Dec 2024 14:30:33 +0530 Message-ID: <20241203090045.942078-2-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A34C:EE_|CY8PR12MB7218:EE_ X-MS-Office365-Filtering-Correlation-Id: 1efccd97-6b69-4b97-55d0-08dd13790989 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|36860700013|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: tRcmu8TBonD+E0h3vGCdeScTji4Ql+LHX6fVjJOWecD4ejVClrBuRjeWawSRdUGu1swARDSzQPE1Ca3I5PHhhdwhiYeDEDFd3amuQ4yUMwSzQYoWGXlsoMT3vKXERAmWD8w84/bI6kw3qd2BEAq6DZsfkR8aDFB67vz03ms104EU4QBPdePYwUYfln0DQMDvMynuX0u9x/K6LbnP7D5Ln2bqDKKKa/GV1dF4sJOWDOuF3xqhq++tGisazUFRp0xY/PzXDrNEJHFOuKNxAGw2sFTuRiyTyHcARoHeLlkEAyyK0dQ2HlhxbetNESYQaLiinuM78AlUMHVz60W9PndgeH/I42e57yry+DayfwsfJIDFsKr5Nf3/6Blh3+Mk2QWf5uTGT1iSGnzB7XT8co8MWZdsGwwe8Ew8/ZTjLr/BwHlTMiztZesGnLjr8XT9rrMzYL/WdOfKY9ZhL2eSYxKnTSTM/bOGrWAV2whPns5l1fatitZGsHuqpkdksUVZDQYiC0z9smm/bElE9SAIfUQ8RENXjMA/7fNCrBAuAqJOafhsFAYZn/ZVSMAk9is81AfWqBV4KZ3tMDdZAqZORpVMd6+zyWPN5hYaGJeswU8r3zI7CMPHAq8q3nml4NlWe2c9Fgj7FIJ5BliS5IlZn3OeP2Tbot8zs0PPyziOpeOl+xejBw/oP27ez9pKt/NVmQkH61ZmClTi/QpEuXI6HopjLdPt5FD1CeNj1tEFqMfLDgMJPlh/M2JMKzj3ZKqtuc69yJtZcw24lkwoKEnf303zZg7fbAC+I7ghBJfBtygQTvB78VXQ8HZjBD0jzXlrqz4dFj8VW9jIwG8Ism/K5ERAcRSBPJumeFrzGY+hgWV7L3U5Y5ollpXGrcgINb2g1FY/vA+skzLqLza23fXkNNAQC6zFkcdzCZONOPjeVKaucX9b3CYtrJznA3N0v+1yKf1OQl1IFKegamdtQLv//by5yGvOhpgoCDl0WVMkP0DKkRLISEvHTNt+H3K8H0K+Lu7+xSuyk2FyYuZVa36TmDJz28KrTFYxXAERNzFpMcNoKCSvxKsY6ULsyH32r1NvFUi5itM5bQJBmK6OkVSPZGUsGC0e2FyUgRFUlczdOnupDyGmUFuL+dOutPMp5bYfFuus1CzK4ICFsr5g4hvCNVQJG1xU7FwPYaucY451ru5k37AgRT+vjUCAPDt6PZnrguVEXA2I2yXJv4bYKdpSa4xnCpiTgZXDZ3yxfCTCD65Beo5V7cOKoMk8TtKiFC1yvbowRxtmTj3PTvKCE3FIBUXAtSLUlHpbn+8OcT99g9XFYBoeqCfWxETqWJJrJehLhscR12tBfMiMz5I7eqgZXKjWRJUhGqY2uCQbc2TZmddUebf0Idi/5cSDSp00IXpGm0N8pZo8kx+a+wJ22DCsujMQ/F6MbjrFBvBxDIB++b8o8TkQCtzjtXgXIZ4p+fSUhRgL 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:(13230040)(7416014)(376014)(36860700013)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:12.7508 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1efccd97-6b69-4b97-55d0-08dd13790989 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: CH1PEPF0000A34C.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7218 Currently, the sev-guest driver is the only user of SNP guest messaging. All routines for initializing SNP guest messaging are implemented within the sev-guest driver and are not available during early boot. In prepratation for adding Secure TSC guest support, carve out APIs to allocate and initialize guest messaging descriptor context and make it part of coco/sev/core.c. As there is no user of sev_guest_platform_data anymore, remove the structure. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky Signed-off-by: Nikunj A Dadhania --- arch/x86/include/asm/sev.h | 24 ++- arch/x86/coco/sev/core.c | 183 +++++++++++++++++++++- drivers/virt/coco/sev-guest/sev-guest.c | 197 +++--------------------- arch/x86/Kconfig | 1 + drivers/virt/coco/sev-guest/Kconfig | 1 - 5 files changed, 220 insertions(+), 186 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 91f08af31078..f78c94e29c74 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -14,6 +14,7 @@ #include #include #include +#include #define GHCB_PROTOCOL_MIN 1ULL #define GHCB_PROTOCOL_MAX 2ULL @@ -170,10 +171,6 @@ struct snp_guest_msg { u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)]; } __packed; -struct sev_guest_platform_data { - u64 secrets_gpa; -}; - struct snp_guest_req { void *req_buf; size_t req_sz; @@ -253,6 +250,7 @@ struct snp_msg_desc { u32 *os_area_msg_seqno; u8 *vmpck; + int vmpck_id; }; /* @@ -458,6 +456,20 @@ void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot); void snp_kexec_finish(void); void snp_kexec_begin(void); +static inline bool snp_is_vmpck_empty(struct snp_msg_desc *mdesc) +{ + static const char zero_key[VMPCK_KEY_LEN] = {0}; + + if (mdesc->vmpck) + return !memcmp(mdesc->vmpck, zero_key, VMPCK_KEY_LEN); + + return true; +} + +int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id); +struct snp_msg_desc *snp_msg_alloc(void); +void snp_msg_free(struct snp_msg_desc *mdesc); + #else /* !CONFIG_AMD_MEM_ENCRYPT */ #define snp_vmpl 0 @@ -498,6 +510,10 @@ static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; } static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { } static inline void snp_kexec_finish(void) { } static inline void snp_kexec_begin(void) { } +static inline bool snp_is_vmpck_empty(struct snp_msg_desc *mdesc) { return false; } +static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; } +static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; } +static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index c5b0148b8c0a..3cc741eefd06 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -2580,15 +2581,9 @@ static struct platform_device sev_guest_device = { static int __init snp_init_platform_device(void) { - struct sev_guest_platform_data data; - if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - data.secrets_gpa = secrets_pa; - if (platform_device_add_data(&sev_guest_device, &data, sizeof(data))) - return -ENODEV; - if (platform_device_register(&sev_guest_device)) return -ENODEV; @@ -2667,3 +2662,179 @@ static int __init sev_sysfs_init(void) } arch_initcall(sev_sysfs_init); #endif // CONFIG_SYSFS + +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("failed to mark page shared, ret=%d\n", ret); + __free_pages(page, get_order(sz)); + return NULL; + } + + return page_address(page); +} + +static u8 *get_vmpck(int id, struct snp_secrets_page *secrets, u32 **seqno) +{ + u8 *key = NULL; + + switch (id) { + case 0: + *seqno = &secrets->os_area.msg_seqno_0; + key = secrets->vmpck0; + break; + case 1: + *seqno = &secrets->os_area.msg_seqno_1; + key = secrets->vmpck1; + break; + case 2: + *seqno = &secrets->os_area.msg_seqno_2; + key = secrets->vmpck2; + break; + case 3: + *seqno = &secrets->os_area.msg_seqno_3; + key = secrets->vmpck3; + break; + default: + break; + } + + return key; +} + +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("Crypto context initialization failed\n"); + kfree(ctx); + return NULL; + } + + return ctx; +} + +int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) +{ + /* Adjust the default VMPCK key based on the executing VMPL level */ + if (vmpck_id == -1) + vmpck_id = snp_vmpl; + + mdesc->vmpck = get_vmpck(vmpck_id, mdesc->secrets, &mdesc->os_area_msg_seqno); + if (!mdesc->vmpck) { + pr_err("Invalid VMPCK%d communication key\n", vmpck_id); + return -EINVAL; + } + + /* Verify that VMPCK is not zero. */ + if (snp_is_vmpck_empty(mdesc)) { + pr_err("Empty VMPCK%d communication key\n", vmpck_id); + return -EINVAL; + } + + mdesc->vmpck_id = vmpck_id; + + mdesc->ctx = snp_init_crypto(mdesc->vmpck, VMPCK_KEY_LEN); + if (!mdesc->ctx) + return -ENOMEM; + + return 0; +} +EXPORT_SYMBOL_GPL(snp_msg_init); + +struct snp_msg_desc *snp_msg_alloc(void) +{ + struct snp_msg_desc *mdesc; + void __iomem *mem; + + BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE); + + mdesc = kzalloc(sizeof(struct snp_msg_desc), GFP_KERNEL); + if (!mdesc) + return ERR_PTR(-ENOMEM); + + mem = ioremap_encrypted(secrets_pa, PAGE_SIZE); + if (!mem) + goto e_free_mdesc; + + mdesc->secrets = (__force struct snp_secrets_page *)mem; + + /* Allocate the shared page used for the request and response message. */ + mdesc->request = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!mdesc->request) + goto e_unmap; + + mdesc->response = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (!mdesc->response) + goto e_free_request; + + mdesc->certs_data = alloc_shared_pages(SEV_FW_BLOB_MAX_SIZE); + if (!mdesc->certs_data) + goto e_free_response; + + /* initial the input address for guest request */ + mdesc->input.req_gpa = __pa(mdesc->request); + mdesc->input.resp_gpa = __pa(mdesc->response); + mdesc->input.data_gpa = __pa(mdesc->certs_data); + + return mdesc; + +e_free_response: + free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg)); +e_free_request: + free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); +e_unmap: + iounmap(mem); +e_free_mdesc: + kfree(mdesc); + + return ERR_PTR(-ENOMEM); +} +EXPORT_SYMBOL_GPL(snp_msg_alloc); + +void snp_msg_free(struct snp_msg_desc *mdesc) +{ + if (!mdesc) + return; + + mdesc->vmpck = NULL; + mdesc->os_area_msg_seqno = NULL; + kfree(mdesc->ctx); + + free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg)); + free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); + iounmap((__force void __iomem *)mdesc->secrets); + kfree(mdesc); +} +EXPORT_SYMBOL_GPL(snp_msg_free); diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index b699771be029..5268511bc9b8 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -63,16 +63,6 @@ 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 bool is_vmpck_empty(struct snp_msg_desc *mdesc) -{ - char zero_key[VMPCK_KEY_LEN] = {0}; - - if (mdesc->vmpck) - return !memcmp(mdesc->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 @@ -93,7 +83,7 @@ static bool is_vmpck_empty(struct snp_msg_desc *mdesc) static void snp_disable_vmpck(struct snp_msg_desc *mdesc) { pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n", - vmpck_id); + mdesc->vmpck_id); memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN); mdesc->vmpck = NULL; } @@ -147,23 +137,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("Crypto context initialization failed\n"); - kfree(ctx); - return NULL; - } - - return ctx; -} - static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req) { struct snp_guest_msg *resp_msg = &mdesc->secret_response; @@ -335,7 +308,7 @@ static int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_r guard(mutex)(&snp_cmd_mutex); /* Check if the VMPCK is not empty */ - if (is_vmpck_empty(mdesc)) { + if (snp_is_vmpck_empty(mdesc)) { pr_err_ratelimited("VMPCK is disabled\n"); return -ENOTTY; } @@ -414,7 +387,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io req.msg_version = arg->msg_version; req.msg_type = SNP_MSG_REPORT_REQ; - req.vmpck_id = vmpck_id; + req.vmpck_id = mdesc->vmpck_id; req.req_buf = report_req; req.req_sz = sizeof(*report_req); req.resp_buf = report_resp->data; @@ -461,7 +434,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque req.msg_version = arg->msg_version; req.msg_type = SNP_MSG_KEY_REQ; - req.vmpck_id = vmpck_id; + req.vmpck_id = mdesc->vmpck_id; req.req_buf = derived_key_req; req.req_sz = sizeof(*derived_key_req); req.resp_buf = buf; @@ -539,7 +512,7 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques req.msg_version = arg->msg_version; req.msg_type = SNP_MSG_REPORT_REQ; - req.vmpck_id = vmpck_id; + req.vmpck_id = mdesc->vmpck_id; req.req_buf = &report_req->data; req.req_sz = sizeof(report_req->data); req.resp_buf = report_resp->data; @@ -616,76 +589,11 @@ 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, }; -static u8 *get_vmpck(int id, struct snp_secrets_page *secrets, u32 **seqno) -{ - u8 *key = NULL; - - switch (id) { - case 0: - *seqno = &secrets->os_area.msg_seqno_0; - key = secrets->vmpck0; - break; - case 1: - *seqno = &secrets->os_area.msg_seqno_1; - key = secrets->vmpck1; - break; - case 2: - *seqno = &secrets->os_area.msg_seqno_2; - key = secrets->vmpck2; - break; - case 3: - *seqno = &secrets->os_area.msg_seqno_3; - key = secrets->vmpck3; - break; - default: - break; - } - - return key; -} - struct snp_msg_report_resp_hdr { u32 status; u32 report_size; @@ -979,13 +887,10 @@ static void unregister_sev_tsm(void *data) static int __init sev_guest_probe(struct platform_device *pdev) { - struct sev_guest_platform_data *data; - struct snp_secrets_page *secrets; struct device *dev = &pdev->dev; struct snp_guest_dev *snp_dev; struct snp_msg_desc *mdesc; struct miscdevice *misc; - void __iomem *mapping; int ret; BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE); @@ -993,115 +898,57 @@ static int __init sev_guest_probe(struct platform_device *pdev) if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return -ENODEV; - if (!dev->platform_data) - return -ENODEV; - - data = (struct sev_guest_platform_data *)dev->platform_data; - mapping = ioremap_encrypted(data->secrets_gpa, PAGE_SIZE); - if (!mapping) - return -ENODEV; - - secrets = (__force void *)mapping; - - ret = -ENOMEM; snp_dev = devm_kzalloc(&pdev->dev, sizeof(struct snp_guest_dev), GFP_KERNEL); if (!snp_dev) - goto e_unmap; - - mdesc = devm_kzalloc(&pdev->dev, sizeof(struct snp_msg_desc), GFP_KERNEL); - if (!mdesc) - goto e_unmap; - - /* Adjust the default VMPCK key based on the executing VMPL level */ - if (vmpck_id == -1) - vmpck_id = snp_vmpl; + return -ENOMEM; - ret = -EINVAL; - mdesc->vmpck = get_vmpck(vmpck_id, secrets, &mdesc->os_area_msg_seqno); - if (!mdesc->vmpck) { - dev_err(dev, "Invalid VMPCK%d communication key\n", vmpck_id); - goto e_unmap; - } + mdesc = snp_msg_alloc(); + if (IS_ERR_OR_NULL(mdesc)) + return -ENOMEM; - /* Verify that VMPCK is not zero. */ - if (is_vmpck_empty(mdesc)) { - dev_err(dev, "Empty VMPCK%d communication key\n", vmpck_id); - goto e_unmap; - } + ret = snp_msg_init(mdesc, vmpck_id); + if (ret) + return -EIO; platform_set_drvdata(pdev, snp_dev); snp_dev->dev = dev; - mdesc->secrets = secrets; - - /* Allocate the shared page used for the request and response message. */ - mdesc->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); - if (!mdesc->request) - goto e_unmap; - - mdesc->response = alloc_shared_pages(dev, sizeof(struct snp_guest_msg)); - if (!mdesc->response) - goto e_free_request; - - mdesc->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE); - if (!mdesc->certs_data) - goto e_free_response; - - ret = -EIO; - mdesc->ctx = snp_init_crypto(mdesc->vmpck, VMPCK_KEY_LEN); - if (!mdesc->ctx) - goto e_free_cert_data; misc = &snp_dev->misc; misc->minor = MISC_DYNAMIC_MINOR; misc->name = DEVICE_NAME; misc->fops = &snp_guest_fops; - /* Initialize the input addresses for guest request */ - mdesc->input.req_gpa = __pa(mdesc->request); - mdesc->input.resp_gpa = __pa(mdesc->response); - mdesc->input.data_gpa = __pa(mdesc->certs_data); - /* Set the privlevel_floor attribute based on the vmpck_id */ - sev_tsm_ops.privlevel_floor = vmpck_id; + sev_tsm_ops.privlevel_floor = mdesc->vmpck_id; ret = tsm_register(&sev_tsm_ops, snp_dev); if (ret) - goto e_free_cert_data; + goto e_msg_init; ret = devm_add_action_or_reset(&pdev->dev, unregister_sev_tsm, NULL); if (ret) - goto e_free_cert_data; + goto e_msg_init; ret = misc_register(misc); if (ret) - goto e_free_ctx; + goto e_msg_init; snp_dev->msg_desc = mdesc; - dev_info(dev, "Initialized SEV guest driver (using VMPCK%d communication key)\n", vmpck_id); + dev_info(dev, "Initialized SEV guest driver (using VMPCK%d communication key)\n", + mdesc->vmpck_id); return 0; -e_free_ctx: - kfree(mdesc->ctx); -e_free_cert_data: - free_shared_pages(mdesc->certs_data, SEV_FW_BLOB_MAX_SIZE); -e_free_response: - free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg)); -e_free_request: - free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); -e_unmap: - iounmap(mapping); +e_msg_init: + snp_msg_free(mdesc); + return ret; } static void __exit sev_guest_remove(struct platform_device *pdev) { struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev); - struct snp_msg_desc *mdesc = snp_dev->msg_desc; - free_shared_pages(mdesc->certs_data, SEV_FW_BLOB_MAX_SIZE); - free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg)); - free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg)); - kfree(mdesc->ctx); + snp_msg_free(snp_dev->msg_desc); misc_deregister(&snp_dev->misc); } diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 9d7bd0ae48c4..0f7e3acf37e3 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1559,6 +1559,7 @@ config AMD_MEM_ENCRYPT select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select UNACCEPTED_MEMORY + 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/Kconfig b/drivers/virt/coco/sev-guest/Kconfig index 0b772bd921d8..a6405ab6c2c3 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 select TSM_REPORTS help SEV-SNP firmware provides the guest a mechanism to communicate with From patchwork Tue Dec 3 09:00:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891956 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2085.outbound.protection.outlook.com [40.107.94.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A44C1DEFF6; Tue, 3 Dec 2024 09:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216490; cv=fail; b=JLGVSuhy/nmsZYgkSx96EhD3FxY/GYMB7Pu5jxbnq/FOJq1z5w2WedjsnPRH+SHGLd62IctthFmZQfi48WXDNOlAnLGOAcNzgdFbLvZP0+a+Dex7R0b2694WO3dAE3bWyuxS/ve4IFuGW3V+4mI8Sq0Mj5Mz+yXBXjKHZLE6B3U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216490; c=relaxed/simple; bh=eLog3o8vSBxUEAKEkvPNMlFe3ZhkGPS/y+UsBOYQQE8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hafFsStZeVtJgj2mEp9dZTdw6/e2D4eDxlrE+jtzBf53dfom8dfEDInEXVdx3+bJIBYjv4m3cwFS076vvPtqXljBhOxTgyb1B/jjqe0IOjqvKkmPEi7T0khVFY1igi1MUch6XBezlYaLoyVUVlrzBnKHoGcQt8dae01l65VqAnc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=a4lxIPlu; arc=fail smtp.client-ip=40.107.94.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="a4lxIPlu" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QG2kdnEjqFoytAskyQeuzS2SucxhU+inbnN25NIsCOkWecgh4ifFpejnN9uEBHMqZH2CjfOab83CRKXt0cqBR0rrJGB3BVzmlAgs7E/k8BQLy3iEsbnj7rMLMXwlBa58gF3KNKtEu2TIbNTHy8MmEU/9zg1ozbuhqsP1Yf5QT5trESxgw1D99141j1xvUBAOJuhTm/OGaLUq8M+oPApaE/L8grjsmV20+YxbkTXIUjevdlKZU23GvwpPgZVVL+5+L+KJZo7AuBmH47jakqjFpqbOLnVLK/218u2LnmudY1/KGcGZYFrklHKGVzqG4Ge3mHZ6xI6fbrJaGuL3nOlngg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=gX+/9Xiqg7oUVKQmndZaU4wqVbeedgI/ega53R01FuI=; b=e+DhDqqcKUfzzi7ovj8zhzxUFRtAu0cGjJzAdpg+lfRCfbjP1SX7dTY1SDWxWuAGfxyLEsbu84Vj01h/diRKoOuiFKE9XFY0ABdZ4yv4wWCOi6ne/Uf6yzPUX/g7hKEI5YNT9Ksk/AXVq/Nq2ecZkO8gbfaXswUfq2v5GUOJD+aM52Udoql9hC6uW9eBWGkYrZUNCgFs8ydz/LOZtx31zHNkRGuB78XuONL9E/1QGCcQv+uG/2rPE7metCXW+P5jDywHwSrWvu4yhQM5OLx/Rr0/zrfpJJdLSUYuSRRSBqrLaiU//6Sjg594to4cbxtjKof2y2ptlXRrtQgULEPrBw== 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 (0) 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=gX+/9Xiqg7oUVKQmndZaU4wqVbeedgI/ega53R01FuI=; b=a4lxIPluJwUb7rV/tvozRnIu3c9zcmLexeSsn89p1PslsMHg7ePRUWbLbGq0WcWFBuvpZI3zkiatSFeAEKIQ4h8HUWhyx4pa7hQ4gp3mtLPqTtcD1IezTgmCnRcwUqaDzOab/ilNaEgxvIxOMSdLPCckST4iqFgsW5ozQlFXd60= Received: from CH2PR16CA0002.namprd16.prod.outlook.com (2603:10b6:610:50::12) by MW3PR12MB4412.namprd12.prod.outlook.com (2603:10b6:303:58::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.19; Tue, 3 Dec 2024 09:01:17 +0000 Received: from CH1PEPF0000A34C.namprd04.prod.outlook.com (2603:10b6:610:50:cafe::bb) by CH2PR16CA0002.outlook.office365.com (2603:10b6:610:50::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:16 +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 CH1PEPF0000A34C.mail.protection.outlook.com (10.167.244.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:16 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:12 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 02/13] x86/sev: Relocate SNP guest messaging routines to common code Date: Tue, 3 Dec 2024 14:30:34 +0530 Message-ID: <20241203090045.942078-3-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A34C:EE_|MW3PR12MB4412:EE_ X-MS-Office365-Filtering-Correlation-Id: ef4f31f3-c9a8-4179-3873-08dd13790bf5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|1800799024|376014|7416014; X-Microsoft-Antispam-Message-Info: FGa1sSWPimQE/NC3HktIBr5vBY+rEe5k9PYBAir/K9GBmJW6g42SgPysSOYYFwQF6jM1ruQ+sagCNjnHGHj4kYPDlxhLSSRDRHZuQpXwO9Q/i5wvOuDfBoUYStHh8UCdyZMjWWflW+afsQaj+ut5/+QVxste5mNGjGN+SuI9ZBRS4FD0c9VvdffM52fwY5qaqzBDgeFeJSzhyxW2eHOuiyO9lFiIQ9PXAuARNOjt1psNFr/AFuLzx2iyu2cmOZvD7KXG1U7SOreJ4QsWTr6OhZJkivIGUvM4MeK9nzkLI+3xxrh6/S1MCSIMa8snYcGmjAQffxHwySbXPFNqfrqSgdc4h4F8tf1I0YfITV33SKR2Cd/Kr3c3xAf9Asot6yCh2Nu6ZHn8g65JpHWGC/oSWTsd0UZnTJa41eC6KMsd+EM9q19Cq+1zPJ3U+UrILe0LoEBVpASQQy/YGb+9R8bFdl505i1tAitbDkD/hqye8KyG4nsAkOT+IavWsLcAmqQUBC+ZxHarpdQGe8QcCRQBqdKXvmWsR74YwxYtgwXAvI0G4Mm+IwCg2O+ZBx1cMLs+P3qmBRx7mDaAbKiQBmb6G9HIRyhFCnnsQO9ce9lKW9yIqkUTierpUiZOnqgr7JvZD9jnT/vY2t0LBAcinoucOTmZzyK/t85jTkbiLtxHRpvYx43FOOErfFx2QzzUTTrqyfUFDf8Owgr90N3kyhZipMTAMucWk8Fv2zQ5Pg66YxuXE9Q2pwimveLOO9tgs8+6gxzsIfizUBGngXA6jhi44UZUIJtAiYHxMHYCrMVjtgi58ZfFANEub5pq1pbZkX+zc8QXAJWcIs1M25K7zSPr3NKXLXYxQND48NyOjUNhRroibmax51qHIQHLp6+LAXxrjixd4LyFlB7mM+W9o5Fy+s31Azc9RHtTTjvw8PjfjHjxf+bXi9T/8gBQQs1LRHZiUTukUyUxUwCjJrh28DBSHemQ7zoLTZMiohTE/v8PFsW8j89DnqqkspP3l7qybC5upv25YsiNOcFumqYe5x1p8wpzOV/SD9d0i9t6j90AW/IMvej0IWmCZmifIxq4Mrb3Rv98VM7gelLgsuDaYcbl9q+1HvXnA4C0mMaxB7nGD5lWubcsquORHvyb/UTurDtS7s3p300o4eGgBoPJB05GgotD4AHWImqVWLPcJRK+q2+m6uCjG4JIebWoREJW2rB5C9SJqeVnoom7fH0JCAbJ8XCc5ZYG3olzinHGY6mMAhdz1nI9afdlmjsa4f3103htsfpHNlFZZcEaAlftW50sETFJuzHAbt3oeGNpdF5N2SjTtFtbhM4L/6h7ToPSdIOMWvS06nRaW5J8pNcRz+AmdJX8jw4pKtXmmvkTzpDVneVBjziAOJS797jJd5hEjJF+JevZgUekL9RyonIZeZoQZXCci1HSXjjSFSH2vcjo88WSJ3XLXmrAg4ScSpoPQ3fZ 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:(13230040)(82310400026)(36860700013)(1800799024)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:16.8287 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ef4f31f3-c9a8-4179-3873-08dd13790bf5 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: CH1PEPF0000A34C.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4412 At present, the SEV guest driver exclusively handles SNP guest messaging. All routines for sending guest messages are embedded within the guest driver. To support Secure TSC, SEV-SNP guests must communicate with the AMD Security Processor during early boot. However, these guest messaging functions are not accessible during early boot since they are currently part of the guest driver. Hence, relocate the core SNP guest messaging functions to SEV common code and provide an API for sending SNP guest messages. No functional change, but just an export symbol added for snp_send_guest_request() and dropped the export symbol on snp_issue_guest_request() and made it static. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 14 +- arch/x86/coco/sev/core.c | 295 +++++++++++++++++++++++- drivers/virt/coco/sev-guest/sev-guest.c | 292 ----------------------- 3 files changed, 299 insertions(+), 302 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index f78c94e29c74..53f3048f484e 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -125,6 +125,9 @@ struct snp_req_data { #define AAD_LEN 48 #define MSG_HDR_VER 1 +#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) +#define SNP_REQ_RETRY_DELAY (2*HZ) + /* See SNP spec SNP_GUEST_REQUEST section for the structure */ enum msg_type { SNP_MSG_TYPE_INVALID = 0, @@ -443,8 +446,6 @@ void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __noreturn snp_abort(void); void snp_dmi_setup(void); -int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio); int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input); void snp_accept_memory(phys_addr_t start, phys_addr_t end); u64 snp_get_unsupported_features(u64 status); @@ -469,6 +470,8 @@ static inline bool snp_is_vmpck_empty(struct snp_msg_desc *mdesc) int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id); struct snp_msg_desc *snp_msg_alloc(void); void snp_msg_free(struct snp_msg_desc *mdesc); +int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio); #else /* !CONFIG_AMD_MEM_ENCRYPT */ @@ -492,11 +495,6 @@ static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } static inline void snp_dmi_setup(void) { } -static inline int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio) -{ - return -ENOTTY; -} static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input) { return -ENOTTY; @@ -514,6 +512,8 @@ static inline bool snp_is_vmpck_empty(struct snp_msg_desc *mdesc) { return false static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; } static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; } static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } +static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) { return -ENODEV; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 3cc741eefd06..a61898c7f114 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -2509,8 +2509,8 @@ int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, } EXPORT_SYMBOL_GPL(snp_issue_svsm_attest_req); -int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, - struct snp_guest_request_ioctl *rio) +static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input, + struct snp_guest_request_ioctl *rio) { struct ghcb_state state; struct es_em_ctxt ctxt; @@ -2572,7 +2572,6 @@ int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *inpu return ret; } -EXPORT_SYMBOL_GPL(snp_issue_guest_request); static struct platform_device sev_guest_device = { .name = "sev-guest", @@ -2838,3 +2837,293 @@ void snp_msg_free(struct snp_msg_desc *mdesc) kfree(mdesc); } EXPORT_SYMBOL_GPL(snp_msg_free); + +/* Mutex to serialize the shared buffer access and command handling. */ +static DEFINE_MUTEX(snp_cmd_mutex); + +/* + * 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_msg_desc *mdesc) +{ + pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n", + mdesc->vmpck_id); + memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN); + mdesc->vmpck = NULL; +} + +static inline u64 __snp_get_msg_seqno(struct snp_msg_desc *mdesc) +{ + u64 count; + + lockdep_assert_held(&snp_cmd_mutex); + + /* Read the current message sequence counter from secrets pages */ + count = *mdesc->os_area_msg_seqno; + + return count + 1; +} + +/* Return a non-zero on success */ +static u64 snp_get_msg_seqno(struct snp_msg_desc *mdesc) +{ + u64 count = __snp_get_msg_seqno(mdesc); + + /* + * 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("request message sequence counter overflow\n"); + return 0; + } + + return count; +} + +static void snp_inc_msg_seqno(struct snp_msg_desc *mdesc) +{ + /* + * The counter is also incremented by the PSP, so increment it by 2 + * and save in secrets page. + */ + *mdesc->os_area_msg_seqno += 2; +} + +static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req) +{ + struct snp_guest_msg *resp_msg = &mdesc->secret_response; + struct snp_guest_msg *req_msg = &mdesc->secret_request; + struct snp_guest_msg_hdr *req_msg_hdr = &req_msg->hdr; + struct snp_guest_msg_hdr *resp_msg_hdr = &resp_msg->hdr; + struct aesgcm_ctx *ctx = mdesc->ctx; + u8 iv[GCM_AES_IV_SIZE] = {}; + + pr_debug("response [seqno %lld type %d version %d sz %d]\n", + resp_msg_hdr->msg_seqno, resp_msg_hdr->msg_type, resp_msg_hdr->msg_version, + resp_msg_hdr->msg_sz); + + /* Copy response from shared memory to encrypted memory. */ + memcpy(resp_msg, mdesc->response, sizeof(*resp_msg)); + + /* Verify that the sequence counter is incremented by 1 */ + if (unlikely(resp_msg_hdr->msg_seqno != (req_msg_hdr->msg_seqno + 1))) + return -EBADMSG; + + /* Verify response message type and version number. */ + if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) || + resp_msg_hdr->msg_version != req_msg_hdr->msg_version) + return -EBADMSG; + + /* + * If the message size is greater than our buffer length then return + * an error. + */ + if (unlikely((resp_msg_hdr->msg_sz + ctx->authsize) > req->resp_sz)) + return -EBADMSG; + + /* Decrypt the payload */ + memcpy(iv, &resp_msg_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_msg_hdr->msg_seqno))); + if (!aesgcm_decrypt(ctx, req->resp_buf, resp_msg->payload, resp_msg_hdr->msg_sz, + &resp_msg_hdr->algo, AAD_LEN, iv, resp_msg_hdr->authtag)) + return -EBADMSG; + + return 0; +} + +static int enc_payload(struct snp_msg_desc *mdesc, u64 seqno, struct snp_guest_req *req) +{ + struct snp_guest_msg *msg = &mdesc->secret_request; + struct snp_guest_msg_hdr *hdr = &msg->hdr; + struct aesgcm_ctx *ctx = mdesc->ctx; + u8 iv[GCM_AES_IV_SIZE] = {}; + + memset(msg, 0, sizeof(*msg)); + + hdr->algo = SNP_AEAD_AES_256_GCM; + hdr->hdr_version = MSG_HDR_VER; + hdr->hdr_sz = sizeof(*hdr); + hdr->msg_type = req->msg_type; + hdr->msg_version = req->msg_version; + hdr->msg_seqno = seqno; + hdr->msg_vmpck = req->vmpck_id; + hdr->msg_sz = req->req_sz; + + /* Verify the sequence number is non-zero */ + if (!hdr->msg_seqno) + return -ENOSR; + + pr_debug("request [seqno %lld type %d version %d sz %d]\n", + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); + + if (WARN_ON((req->req_sz + ctx->authsize) > sizeof(msg->payload))) + return -EBADMSG; + + memcpy(iv, &hdr->msg_seqno, min(sizeof(iv), sizeof(hdr->msg_seqno))); + aesgcm_encrypt(ctx, msg->payload, req->req_buf, req->req_sz, &hdr->algo, + AAD_LEN, iv, hdr->authtag); + + return 0; +} + +static int __handle_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) +{ + unsigned long req_start = jiffies; + unsigned int override_npages = 0; + u64 override_err = 0; + int rc; + +retry_request: + /* + * Call firmware to process the request. In this function the encrypted + * message enters shared memory with the host. So after this call the + * sequence number must be incremented or the VMPCK must be deleted to + * prevent reuse of the IV. + */ + rc = snp_issue_guest_request(req, &mdesc->input, rio); + switch (rc) { + case -ENOSPC: + /* + * If the extended guest request fails due to having too + * small of a certificate data buffer, retry the same + * guest request without the extended data request in + * order to increment the sequence number and thus avoid + * IV reuse. + */ + override_npages = mdesc->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_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); + + /* + * If this call to the firmware succeeds, the sequence number can + * be incremented allowing for continued use of the VMPCK. If + * there is an error reflected in the return value, this value + * is checked further down and the result will be the deletion + * of the VMPCK and the error code being propagated back to the + * user as an ioctl() return code. + */ + goto retry_request; + + /* + * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been + * throttled. Retry in the driver to avoid returning and reusing the + * message sequence number on a different message. + */ + case -EAGAIN: + if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { + rc = -ETIMEDOUT; + break; + } + schedule_timeout_killable(SNP_REQ_RETRY_DELAY); + goto retry_request; + } + + /* + * Increment the message sequence number. There is no harm in doing + * this now because decryption uses the value stored in the response + * structure and any failure will wipe the VMPCK, preventing further + * use anyway. + */ + snp_inc_msg_seqno(mdesc); + + if (override_err) { + rio->exitinfo2 = override_err; + + /* + * If an extended guest request was issued and the supplied certificate + * buffer was not large enough, a standard guest request was issued to + * prevent IV reuse. If the standard request was successful, return -EIO + * back to the caller as would have originally been returned. + */ + if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) + rc = -EIO; + } + + if (override_npages) + mdesc->input.data_npages = override_npages; + + return rc; +} + +int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, + struct snp_guest_request_ioctl *rio) +{ + u64 seqno; + int rc; + + guard(mutex)(&snp_cmd_mutex); + + /* Check if the VMPCK is not empty */ + if (snp_is_vmpck_empty(mdesc)) { + pr_err_ratelimited("VMPCK is disabled\n"); + return -ENOTTY; + } + + /* Get message sequence and verify that its a non-zero */ + seqno = snp_get_msg_seqno(mdesc); + if (!seqno) + return -EIO; + + /* Clear shared memory's response for the host to populate. */ + memset(mdesc->response, 0, sizeof(struct snp_guest_msg)); + + /* Encrypt the userspace provided payload in mdesc->secret_request. */ + rc = enc_payload(mdesc, seqno, req); + if (rc) + return rc; + + /* + * Write the fully encrypted request to the shared unencrypted + * request page. + */ + memcpy(mdesc->request, &mdesc->secret_request, sizeof(mdesc->secret_request)); + + rc = __handle_guest_request(mdesc, req, rio); + if (rc) { + if (rc == -EIO && + rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) + return rc; + + pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", + rc, rio->exitinfo2); + + snp_disable_vmpck(mdesc); + return rc; + } + + rc = verify_and_dec_payload(mdesc, req); + if (rc) { + pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc); + snp_disable_vmpck(mdesc); + return rc; + } + + return 0; +} +EXPORT_SYMBOL_GPL(snp_send_guest_request); + diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 5268511bc9b8..08706229e410 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -31,9 +31,6 @@ #define DEVICE_NAME "sev-guest" -#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) -#define SNP_REQ_RETRY_DELAY (2*HZ) - #define SVSM_MAX_RETRIES 3 struct snp_guest_dev { @@ -60,76 +57,6 @@ static int vmpck_id = -1; module_param(vmpck_id, int, 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); - -/* - * 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_msg_desc *mdesc) -{ - pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n", - mdesc->vmpck_id); - memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN); - mdesc->vmpck = NULL; -} - -static inline u64 __snp_get_msg_seqno(struct snp_msg_desc *mdesc) -{ - u64 count; - - lockdep_assert_held(&snp_cmd_mutex); - - /* Read the current message sequence counter from secrets pages */ - count = *mdesc->os_area_msg_seqno; - - return count + 1; -} - -/* Return a non-zero on success */ -static u64 snp_get_msg_seqno(struct snp_msg_desc *mdesc) -{ - u64 count = __snp_get_msg_seqno(mdesc); - - /* - * 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("request message sequence counter overflow\n"); - return 0; - } - - return count; -} - -static void snp_inc_msg_seqno(struct snp_msg_desc *mdesc) -{ - /* - * The counter is also incremented by the PSP, so increment it by 2 - * and save in secrets page. - */ - *mdesc->os_area_msg_seqno += 2; -} - static inline struct snp_guest_dev *to_snp_dev(struct file *file) { struct miscdevice *dev = file->private_data; @@ -137,225 +64,6 @@ static inline struct snp_guest_dev *to_snp_dev(struct file *file) return container_of(dev, struct snp_guest_dev, misc); } -static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req) -{ - struct snp_guest_msg *resp_msg = &mdesc->secret_response; - struct snp_guest_msg *req_msg = &mdesc->secret_request; - struct snp_guest_msg_hdr *req_msg_hdr = &req_msg->hdr; - struct snp_guest_msg_hdr *resp_msg_hdr = &resp_msg->hdr; - struct aesgcm_ctx *ctx = mdesc->ctx; - u8 iv[GCM_AES_IV_SIZE] = {}; - - pr_debug("response [seqno %lld type %d version %d sz %d]\n", - resp_msg_hdr->msg_seqno, resp_msg_hdr->msg_type, resp_msg_hdr->msg_version, - resp_msg_hdr->msg_sz); - - /* Copy response from shared memory to encrypted memory. */ - memcpy(resp_msg, mdesc->response, sizeof(*resp_msg)); - - /* Verify that the sequence counter is incremented by 1 */ - if (unlikely(resp_msg_hdr->msg_seqno != (req_msg_hdr->msg_seqno + 1))) - return -EBADMSG; - - /* Verify response message type and version number. */ - if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) || - resp_msg_hdr->msg_version != req_msg_hdr->msg_version) - return -EBADMSG; - - /* - * If the message size is greater than our buffer length then return - * an error. - */ - if (unlikely((resp_msg_hdr->msg_sz + ctx->authsize) > req->resp_sz)) - return -EBADMSG; - - /* Decrypt the payload */ - memcpy(iv, &resp_msg_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_msg_hdr->msg_seqno))); - if (!aesgcm_decrypt(ctx, req->resp_buf, resp_msg->payload, resp_msg_hdr->msg_sz, - &resp_msg_hdr->algo, AAD_LEN, iv, resp_msg_hdr->authtag)) - return -EBADMSG; - - return 0; -} - -static int enc_payload(struct snp_msg_desc *mdesc, u64 seqno, struct snp_guest_req *req) -{ - struct snp_guest_msg *msg = &mdesc->secret_request; - struct snp_guest_msg_hdr *hdr = &msg->hdr; - struct aesgcm_ctx *ctx = mdesc->ctx; - u8 iv[GCM_AES_IV_SIZE] = {}; - - memset(msg, 0, sizeof(*msg)); - - hdr->algo = SNP_AEAD_AES_256_GCM; - hdr->hdr_version = MSG_HDR_VER; - hdr->hdr_sz = sizeof(*hdr); - hdr->msg_type = req->msg_type; - hdr->msg_version = req->msg_version; - hdr->msg_seqno = seqno; - hdr->msg_vmpck = req->vmpck_id; - hdr->msg_sz = req->req_sz; - - /* Verify the sequence number is non-zero */ - if (!hdr->msg_seqno) - return -ENOSR; - - pr_debug("request [seqno %lld type %d version %d sz %d]\n", - hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); - - if (WARN_ON((req->req_sz + ctx->authsize) > sizeof(msg->payload))) - return -EBADMSG; - - memcpy(iv, &hdr->msg_seqno, min(sizeof(iv), sizeof(hdr->msg_seqno))); - aesgcm_encrypt(ctx, msg->payload, req->req_buf, req->req_sz, &hdr->algo, - AAD_LEN, iv, hdr->authtag); - - return 0; -} - -static int __handle_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, - struct snp_guest_request_ioctl *rio) -{ - unsigned long req_start = jiffies; - unsigned int override_npages = 0; - u64 override_err = 0; - int rc; - -retry_request: - /* - * Call firmware to process the request. In this function the encrypted - * message enters shared memory with the host. So after this call the - * sequence number must be incremented or the VMPCK must be deleted to - * prevent reuse of the IV. - */ - rc = snp_issue_guest_request(req, &mdesc->input, rio); - switch (rc) { - case -ENOSPC: - /* - * If the extended guest request fails due to having too - * small of a certificate data buffer, retry the same - * guest request without the extended data request in - * order to increment the sequence number and thus avoid - * IV reuse. - */ - override_npages = mdesc->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_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN); - - /* - * If this call to the firmware succeeds, the sequence number can - * be incremented allowing for continued use of the VMPCK. If - * there is an error reflected in the return value, this value - * is checked further down and the result will be the deletion - * of the VMPCK and the error code being propagated back to the - * user as an ioctl() return code. - */ - goto retry_request; - - /* - * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been - * throttled. Retry in the driver to avoid returning and reusing the - * message sequence number on a different message. - */ - case -EAGAIN: - if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { - rc = -ETIMEDOUT; - break; - } - schedule_timeout_killable(SNP_REQ_RETRY_DELAY); - goto retry_request; - } - - /* - * Increment the message sequence number. There is no harm in doing - * this now because decryption uses the value stored in the response - * structure and any failure will wipe the VMPCK, preventing further - * use anyway. - */ - snp_inc_msg_seqno(mdesc); - - if (override_err) { - rio->exitinfo2 = override_err; - - /* - * If an extended guest request was issued and the supplied certificate - * buffer was not large enough, a standard guest request was issued to - * prevent IV reuse. If the standard request was successful, return -EIO - * back to the caller as would have originally been returned. - */ - if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) - rc = -EIO; - } - - if (override_npages) - mdesc->input.data_npages = override_npages; - - return rc; -} - -static int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, - struct snp_guest_request_ioctl *rio) -{ - u64 seqno; - int rc; - - guard(mutex)(&snp_cmd_mutex); - - /* Check if the VMPCK is not empty */ - if (snp_is_vmpck_empty(mdesc)) { - pr_err_ratelimited("VMPCK is disabled\n"); - return -ENOTTY; - } - - /* Get message sequence and verify that its a non-zero */ - seqno = snp_get_msg_seqno(mdesc); - if (!seqno) - return -EIO; - - /* Clear shared memory's response for the host to populate. */ - memset(mdesc->response, 0, sizeof(struct snp_guest_msg)); - - /* Encrypt the userspace provided payload in mdesc->secret_request. */ - rc = enc_payload(mdesc, seqno, req); - if (rc) - return rc; - - /* - * Write the fully encrypted request to the shared unencrypted - * request page. - */ - memcpy(mdesc->request, &mdesc->secret_request, - sizeof(mdesc->secret_request)); - - rc = __handle_guest_request(mdesc, req, rio); - if (rc) { - if (rc == -EIO && - rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN)) - return rc; - - pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n", - rc, rio->exitinfo2); - - snp_disable_vmpck(mdesc); - return rc; - } - - rc = verify_and_dec_payload(mdesc, req); - if (rc) { - pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc); - snp_disable_vmpck(mdesc); - return rc; - } - - return 0; -} - struct snp_req_resp { sockptr_t req_data; sockptr_t resp_data; From patchwork Tue Dec 3 09:00:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891957 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2072.outbound.protection.outlook.com [40.107.92.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75CF61DED7B; Tue, 3 Dec 2024 09:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.72 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216491; cv=fail; b=mEHmLgoD9NAtFvyq6Hc3cQUYTvAeE5AlOdzxUANvMlLCUItzMn0/REKR1HQnJvTnTLb8G//jpq+JgwPKL2OmL+au2y2hcR6l+2kuzRhEZmYTchPiYNMq+4K4h1sFgLooQUBmYXU+1WQY4j5k13QCAS56eoOcGiX89Ep07P3id+I= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216491; c=relaxed/simple; bh=IXLNWsV9sKb5nADPd61OXSqhkiGqNvdMP/TJnGan+GQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CbL9qF65IycVlq4gnjPWBtZgiu6cBsfSCWqLXXURal6NlfG/AM07BTVtkl0Q7tvIKCIVA58J+mevsGgtj85+1Lv77uJHaTGMQkloqbMsJ7adgn8vf3CCscb6NGOxWbtfivurmT5GIjbE5gpz0UvWb9uDOCtKvesI7GFmTo7c75k= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=m3o7GOfi; arc=fail smtp.client-ip=40.107.92.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="m3o7GOfi" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UVhhRgUhFx1NIbdEPiJOVtPOrVCjfF+sKFCUtw6NZUJgWsh5bFORjA5bnzIdxzz8zofTfCpfspLbigPFNfjOyzjnSuWmxBmHNDbewa8ohFD9ow7I1yID4Lje9hCB8Yw2N5WHHBGuOndica1SRu1wIZ3J133nACb9PrzvqyWwEHiT3CmVa8Bl4LCm0CCr0hxCZGGbeGQnsHEOsRLCWLFZ708U/4br6RUjEoBRpHK7tqNNTgCHs/+jhQypsE0+IouM2ZjpeHEKQPXV/T/9ie+jbIptnOb8/eNKsmLUIKeNZ9l5upULKjXfV0dhdb4GB3oJRai8xlEcPd4xDzvooKa1qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=yw9z+VxqVynV0S6nQaMMizVq7s1ywCddudECnIr6VtY=; b=zPfPs61IM//ARTKAdLZL7GMqjNsunNtxF6IR4fvsTah7wZ+frwv1wUcH0TrGvhK+34YQYJBHxxjDvIJB/jD0CSYo7AUQ0Zrh0gwT6kq1LUwIihRhK3q0HAxUnJzBATCpYQoORYPb5128gL7z0r8t6ZY0zVUFke6ArxvkbCDR/kFIWumG4n35ylhH12vdDIKS1ZWhMrOXsCWpBQcLuwe4xfuNRnsDiInZvXd4AQuYe0esH5sdqabYj7oFXKG6clqrFlCjxQ4UoaWvj9IB4YK+Xrd8c/ZlmkeC82jGBaSe0lb0S6fe1kXJI7qWc0QFJjowEw2UKbF/IrlX67omYpjFyQ== 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 (0) 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=yw9z+VxqVynV0S6nQaMMizVq7s1ywCddudECnIr6VtY=; b=m3o7GOfiHu4SD9pns5qfEH6FWzUZidsFqOZNAF2qVJ4bPQt7we9E3RRQTGJQ50CDtDBJ/WPGNv8k/YYBXgpcLWvSXJsJqqjnkJrV40d3yDno16V4kQwUzYL5jm1Q6IFmn87Q3Ue0cHStxViGb0VfWSNwM4rGllAF6r6a+eIv7tE= Received: from CH0P221CA0029.NAMP221.PROD.OUTLOOK.COM (2603:10b6:610:11d::7) by CY5PR12MB6203.namprd12.prod.outlook.com (2603:10b6:930:24::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:20 +0000 Received: from CH1PEPF0000A349.namprd04.prod.outlook.com (2603:10b6:610:11d:cafe::40) by CH0P221CA0029.outlook.office365.com (2603:10b6:610:11d::7) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.19 via Frontend Transport; Tue, 3 Dec 2024 09:01:19 +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 CH1PEPF0000A349.mail.protection.outlook.com (10.167.244.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:19 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:15 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 03/13] x86/sev: Add Secure TSC support for SNP guests Date: Tue, 3 Dec 2024 14:30:35 +0530 Message-ID: <20241203090045.942078-4-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A349:EE_|CY5PR12MB6203:EE_ X-MS-Office365-Filtering-Correlation-Id: fdfc8240-0ca6-4183-99ac-08dd13790db1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: +uIrslOAvpgiKWBPyOPJj40WYLPz0Tpe5VaXBejVvYPDyJawdKktqJtt6V86VSh1w8IDXg7nvYSUIfD4zegtJZxcxxdo8msO4uROPe4NfVU9XsTOW9zP+MOWK6F4EAko66R/WzuU6TpCGKChcZlD2lavTH02F78e2L3YrCISf/UTqto53OlF4TV5HZEjRzuz4NY3qMI2g7q4iF5+c4TPQEfYHXqtwNywrwmulFdx8M4+a2QB5mjTgOMtRsFxXEmoybEDTVKzMnc1Cnb8m4EXoy3B2bNkHB6wc76izeIjevxfWWCmqnvPeJJuGXSJZyIEq2TBM+394+11gJDggPzrb93Ut5dM5bV1CViv35S7LMrxxw9BpRGj4WK9Ke2E/QlprgRnQVuYIs4rzoo0Icq5mHOFRFBALoGFpzX8FMsGEV0XLpHJpl994cbUOHZx7gy2/SjXNWhf059IWj4eXqauqORLkLNIoWtc+v/unIxMdM69SThb5yXV6uMT2kqiSQgahlTp2wd+vDs9W2gDuyvYgaNJs/8Eehhdfkhp/MZux5aPLLfv/nOPzV1pPT0j2eJvwQXjLA239+rFK2b7LZPnYG/Xx5CJQmliREkWuvSr9p7kc7WHXyMX4woi13ciLJSGRx/EQiDskrt4h26OPRFOfyh+yNcNaP02iQ2Fiy0QU/GsIeyHOaD+vJoW3WF9LggwX+XKGMDj5di4XUPqrcA96FRIVzSB3MUlcnFxaLsYoMrXwOpFIxsqpT885IG6j+TYqZ4kxIa8XBPtt1jzigIm4KYGDn/H9KWewD0HVli6AgalAB5rpltstYuViE1jTrup4BWJoyRKV3EKo7QuYR+dSwcqqE8T5KKEREjzNBXs5UTJrUJgfzg8spF2Pn1BLZf68QRDjLTF1Xx60xnHxFck3xOxfebnTIzgWdVLpb+CthAEW4HyJT2b11IDlmHK+jwOIcszT67KMJU8YKPRHciib2kecfLm1fuRYZllvJQlsgKCrCBzyEuKQhIvF/ZGCgoXiCpC8KBcTCP1hKlvnmqcVw4kIBHJgguF536KU/sZILzRD6GfnCD88DroAkUa+2pcTzMJndkCHU4Uui44Sfmp/V0BxqCQ/P3eVr7l5Wf6g30/CQtSuhsjdHaHwf84J5LKVwtrTeQCDCdYL+zVKqAKILuett5FFvlLH/kCU8K706eSdJNC/NaXefJDlfn5+le8DpzmxOsUNQ5eGUETE4DSR1Q1ppeatBEltqsCLlc1nxd0gp4nxXhw13mOxHTZxhKKcN3vdSSc/zWR22so/lDIvg48MTa7SLmiDT5JKNuV2DjqVpkxl4GukbTdkdH3yWxTKe59lu+jCe/hvaT2XFGfdNKeeKudaHmaxco6mD+Ap2IXmVaVKHPPql8jBUNfYM2W98HZlPrhnJ2b0A0xbZkJxPWIq8XoCJhVV5Z1PjdbMJwBIfgE9mxtMJunnB8sEQRQ 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:(13230040)(36860700013)(1800799024)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:19.7410 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fdfc8240-0ca6-4183-99ac-08dd13790db1 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: CH1PEPF0000A349.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6203 Add support for Secure TSC in SNP-enabled guests. Secure TSC allows guests to securely use RDTSC/RDTSCP instructions, ensuring that the parameters used cannot be altered by the hypervisor once the guest is launched. Secure TSC-enabled guests need to query TSC information from the AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, with the hypervisor acting merely as a conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with AEAD (AES-256 GCM). Signed-off-by: Nikunj A Dadhania Tested-by: Peter Gonda Reviewed-by: Tom Lendacky --- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 22 ++++++ arch/x86/include/asm/svm.h | 6 +- include/linux/cc_platform.h | 8 +++ arch/x86/coco/core.c | 3 + arch/x86/coco/sev/core.c | 116 ++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 2 + 7 files changed, 156 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 50f5666938c0..6ef92432a5ce 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -206,6 +206,7 @@ struct snp_psc_desc { #define GHCB_TERM_NO_SVSM 7 /* SVSM is not advertised in the secrets page */ #define GHCB_TERM_SVSM_VMPL0 8 /* SVSM is present but has set VMPL to 0 */ #define GHCB_TERM_SVSM_CAA 9 /* SVSM is present but CAA is not page aligned */ +#define GHCB_TERM_SECURE_TSC 10 /* Secure TSC initialization failed */ #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 53f3048f484e..9fd02efef08e 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -146,6 +146,9 @@ enum msg_type { SNP_MSG_VMRK_REQ, SNP_MSG_VMRK_RSP, + SNP_MSG_TSC_INFO_REQ = 17, + SNP_MSG_TSC_INFO_RSP, + SNP_MSG_TYPE_MAX }; @@ -174,6 +177,22 @@ struct snp_guest_msg { u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)]; } __packed; +#define SNP_TSC_INFO_REQ_SZ 128 +#define SNP_TSC_INFO_RESP_SZ 128 + +struct snp_tsc_info_req { + u8 rsvd[SNP_TSC_INFO_REQ_SZ]; +} __packed; + +struct snp_tsc_info_resp { + u32 status; + u32 rsvd1; + u64 tsc_scale; + u64 tsc_offset; + u32 tsc_factor; + u8 rsvd2[100]; +} __packed; + struct snp_guest_req { void *req_buf; size_t req_sz; @@ -473,6 +492,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc); int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio); +void __init snp_secure_tsc_prepare(void); + #else /* !CONFIG_AMD_MEM_ENCRYPT */ #define snp_vmpl 0 @@ -514,6 +535,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; } static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio) { return -ENODEV; } +static inline void __init snp_secure_tsc_prepare(void) { } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 2b59b9951c90..92e18798f197 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -417,7 +417,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; @@ -564,7 +566,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/include/linux/cc_platform.h b/include/linux/cc_platform.h index caa4b4430634..cb7103dc124f 100644 --- a/include/linux/cc_platform.h +++ b/include/linux/cc_platform.h @@ -88,6 +88,14 @@ enum cc_attr { * enabled to run SEV-SNP guests. */ CC_ATTR_HOST_SEV_SNP, + + /** + * @CC_ATTR_GUEST_SNP_SECURE_TSC: SNP 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_SNP_SECURE_TSC, }; #ifdef CONFIG_ARCH_HAS_CC_PLATFORM diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c index 0f81f70aca82..5b9a358a3254 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -100,6 +100,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr) case CC_ATTR_HOST_SEV_SNP: return cc_flags.host_sev_snp; + case CC_ATTR_GUEST_SNP_SECURE_TSC: + return sev_status & MSR_AMD64_SNP_SECURE_TSC; + default: return false; } diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index a61898c7f114..39683101b526 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -96,6 +96,14 @@ static u64 sev_hv_features __ro_after_init; /* Secrets page physical address from the CC blob */ static u64 secrets_pa __ro_after_init; +/* + * For Secure TSC guests, the BP fetches TSC_INFO using SNP guest messaging and + * initializes snp_tsc_scale and snp_tsc_offset. These values are replicated + * across the APs VMSA fields (TSC_SCALE and TSC_OFFSET). + */ +static u64 snp_tsc_scale __ro_after_init; +static u64 snp_tsc_offset __ro_after_init; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -1277,6 +1285,12 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip) vmsa->vmpl = snp_vmpl; vmsa->sev_features = sev_status >> 2; + /* Populate AP's TSC scale/offset to get accurate TSC values. */ + if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) { + vmsa->tsc_scale = snp_tsc_scale; + vmsa->tsc_offset = snp_tsc_offset; + } + /* Switch the page over to a VMSA page now that it is initialized */ ret = snp_set_vmsa(vmsa, caa, apic_id, true); if (ret) { @@ -3127,3 +3141,105 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req } EXPORT_SYMBOL_GPL(snp_send_guest_request); +static int __init snp_get_tsc_info(void) +{ + struct snp_guest_request_ioctl *rio; + struct snp_tsc_info_resp *tsc_resp; + struct snp_tsc_info_req *tsc_req; + struct snp_msg_desc *mdesc; + struct snp_guest_req *req; + unsigned char *buf; + int rc = -ENOMEM; + + tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL); + if (!tsc_req) + return rc; + + tsc_resp = kzalloc(sizeof(*tsc_resp), GFP_KERNEL); + if (!tsc_resp) + goto e_free_tsc_req; + + req = kzalloc(sizeof(*req), GFP_KERNEL); + if (!req) + goto e_free_tsc_resp; + + rio = kzalloc(sizeof(*rio), GFP_KERNEL); + if (!rio) + goto e_free_req; + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover + * the authtag. + */ + buf = kzalloc(SNP_TSC_INFO_RESP_SZ + AUTHTAG_LEN, GFP_KERNEL); + if (!buf) + goto e_free_rio; + + mdesc = snp_msg_alloc(); + if (IS_ERR_OR_NULL(mdesc)) + goto e_free_buf; + + rc = snp_msg_init(mdesc, snp_vmpl); + if (rc) + goto e_free_mdesc; + + req->msg_version = MSG_HDR_VER; + req->msg_type = SNP_MSG_TSC_INFO_REQ; + req->vmpck_id = snp_vmpl; + req->req_buf = tsc_req; + req->req_sz = sizeof(*tsc_req); + req->resp_buf = buf; + req->resp_sz = sizeof(*tsc_resp) + AUTHTAG_LEN; + req->exit_code = SVM_VMGEXIT_GUEST_REQUEST; + + rc = snp_send_guest_request(mdesc, req, rio); + if (rc) + goto e_request; + + memcpy(tsc_resp, buf, sizeof(*tsc_resp)); + pr_debug("%s: response status 0x%x scale 0x%llx offset 0x%llx factor 0x%x\n", + __func__, tsc_resp->status, tsc_resp->tsc_scale, tsc_resp->tsc_offset, + tsc_resp->tsc_factor); + + if (tsc_resp->status == 0) { + snp_tsc_scale = tsc_resp->tsc_scale; + snp_tsc_offset = tsc_resp->tsc_offset; + } else { + pr_err("Failed to get TSC info, response status 0x%x\n", tsc_resp->status); + rc = -EIO; + } + +e_request: + /* The response buffer contains sensitive data, explicitly clear it. */ + memzero_explicit(buf, sizeof(buf)); + memzero_explicit(tsc_resp, sizeof(*tsc_resp)); +e_free_mdesc: + snp_msg_free(mdesc); +e_free_buf: + kfree(buf); +e_free_rio: + kfree(rio); +e_free_req: + kfree(req); + e_free_tsc_resp: + kfree(tsc_resp); +e_free_tsc_req: + kfree(tsc_req); + + return rc; +} + +void __init snp_secure_tsc_prepare(void) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) || + !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) + return; + + if (snp_get_tsc_info()) { + pr_alert("Unable to retrieve Secure TSC info from ASP\n"); + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC); + } + + pr_debug("SecureTSC enabled"); +} diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 0a120d85d7bb..95bae74fdab2 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -94,6 +94,8 @@ void __init mem_encrypt_init(void) /* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes(); + snp_secure_tsc_prepare(); + print_mem_encrypt_feature_info(); } From patchwork Tue Dec 3 09:00:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891955 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2049.outbound.protection.outlook.com [40.107.244.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45AD81DE3CF; Tue, 3 Dec 2024 09:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.49 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216490; cv=fail; b=ey0ZWbeAnLim7H2OrCyTX3clcH5v1P4q8GDswd+1AwnnkkpWgPJ119S0EgzfcR5tSlFyEo/FzZBO6JnxHuc7BICEh+vm/iaPxJZ6lLVeiiI2S3LmuCXxs9bmZxvxNw/BX/OPJl7czrkUuMF5gaTWIotifIdHPPH8I8qfRkx80hI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216490; c=relaxed/simple; bh=xD//zptdKAscnlm2Vf8WtRMspIq0l/OHD/3bYxoonP0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=o9hVEe5U4ryHtMhtCxS5uIJRt66UJKX7fPwrmThotk9LP5SGfawYTkyJEDE9U8daB7FtRX1wPWL8bXvPdc3O2QzpKahgO6ToLH5DU0Ei7JoTf5TK/0ckJUKMtOmtbgrYU9kTNiUKg5gN2hS6QP5MUNM8nT42bQt8kGhfx6fvMJU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=la9HxfgK; arc=fail smtp.client-ip=40.107.244.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="la9HxfgK" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=s2Rplf7Qf/Ong2sJDi8maO1w4NmyW6Ms7+/gPRWukI7V+OJS8zvm56d5bx1K5HNDc/BAc22wuw464gzqFIbDzAXGZwDm4kac51RVJcDklRHIQYF9e/ZuAZErR1+dTTYi/KultR2C3GhCoRw/Xw+tKHRLJz2/n+SJ+nFNYFJIcg5lRM0FjdF5Z9i8hjWF0m4DQiBYJu1tAn74gcczyvgoNLROsPl8jnbSlBu4DDzbg3D0eKnxHnlT7Qj1RCwQRk3dK3wsi/sSjnVCHgUDzj3jrJPhYN1kvfN0pYzYr/IR+UN6LsXMPl0CAaqqSuDoD0joe7Jn/uSbmlaS411O49VX4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=UP99uhyNsG10l+T2I2+7VWdX+j6FML3l2skCSpWWdZ8=; b=AYa+2KdoKQVMpemHsbnq4fijp060bhttuGZsYDmFXky8SbWKqf5adAD5SYMdJCKDQtzDvHhSlReBrNI3zcid2yIHOiHbN6DCIVPY4V+RgLhMD/pu/WoPCLBC4JIXmfyF8opN6bjWr8HtBxgw+YeHcB0CyKG46WD7mGQ/ndbQYjx/TmIRBxfVxXsP3sBTrntJ8lz9dbMo/gLmYyuJxO8eYQN/VEsBnVfKU6CLiAl1Ptb/G9mFUWOy3NBVW8crcLnN+viqxYQJmWCCOo0Ye1sVtYSDzPFlaa96/HjUGjbnnJz/UbmQGcleNUvCUvc8B3M7g1NUqkf9ZXMlAYnBVXcNQA== 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 (0) 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=UP99uhyNsG10l+T2I2+7VWdX+j6FML3l2skCSpWWdZ8=; b=la9HxfgKp6MYgRoCS2tFudbdxbFE2YNUMvsh6wzvJRxkPAHshJijbBTuV4MQRwuMxy1B+W9WcWK3+iATf1b7hDM7IfMe1UdW49fGk2MvqvXZo6mMpTJxDjn3TzQMi+O4jWGlCsBmOLrADHEfcQJxpkj+QbnldQcYh/N5b7EaXTo= Received: from CH2PR12CA0024.namprd12.prod.outlook.com (2603:10b6:610:57::34) by DM4PR12MB6183.namprd12.prod.outlook.com (2603:10b6:8:a7::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.17; Tue, 3 Dec 2024 09:01:23 +0000 Received: from CH1PEPF0000A346.namprd04.prod.outlook.com (2603:10b6:610:57:cafe::1d) by CH2PR12CA0024.outlook.office365.com (2603:10b6:610:57::34) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CH1PEPF0000A346.mail.protection.outlook.com (10.167.244.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:23 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:19 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 04/13] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests Date: Tue, 3 Dec 2024 14:30:36 +0530 Message-ID: <20241203090045.942078-5-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A346:EE_|DM4PR12MB6183:EE_ X-MS-Office365-Filtering-Correlation-Id: ac567751-33ad-4c27-b633-08dd13790fc5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|36860700013|7416014|376014; X-Microsoft-Antispam-Message-Info: B6ICRwRIFnJPoylZMebXYTnUexwnvg4g/hwPvtNWw7Pw2PVjjedaUeDsk/xffl67IZEbadAuEV/3gvbFpJCcKsW+utxlzpo0vgUSxM5S9oUm+bHSAztkP1kd9RQQwkEcL+neCcL//8HmVpU9XIOn02bDxY2UWx6wzRG/ujnucbILBJbtys8UX7z189iAdhCZ1okU9tAHkkjr8obIs2EEmdDWnFQmFIen0J54DfKA21FVkH6GvoCXIVlhR31CdPNYo1D41uGN1g3wowu5vJeXznTZv4TZfotK4QKnD6nj0GUyqd61bcmqMyaBy2BdDVCYIfnNmtJ6FpmcEKm7oIlLm58XHLudm5K/NlEzwm+hxMO8cLDm5giGubau6e0oFHFoH/HJeQDB1ryMhDFHxThYjptIMCNzDjS6/f7vdU4QlMuBiqwQejBYEsUMCpDOneCCrh4FMUu9ZOLj9I+Mo0tfHlqAkQF6djYDN2dLbq0XYPuCa6N8vBiyRW9DHhhGeCR13qykZIIEENF6MTZgIKG8MWT5Mt/fywVEud2Dfte4wKRuzKzWzQHYGZL0i6jKjbCQfB0wypD1r6ZRqF6x3B7qVjf1VJ8N0NiGrPq9Bz6ThR4vPkh9Ji4hKD55A4ZoHO9UsOjGReMmhQ3dT1PHu7wP2q/IiB0zflLbOg6xfTcKyBKGbDdVxIdb0f2wt26TgyudiK9qEoMgc5uNIp2shVb62rVIPeNDIHmJFSxWan3FN37DH4uEdq/SMpg+nbB01+LLkPeAfjQoh0mnTqsI9IfF2WghdS9QyAVImcEmLObTTzhIDASOfdp7OBh98ZWWkn3xsVWlTk+QgjmljKAMC9M/tLiBf9FyjApD8wjJ2k9IgaKY4C25jMZCV6ftTv6wkWc3NRtQMrVyp3phkKUqfKFvbPbAcO/EaAqFrUoBgEIqNqsYRs6LGfgwvxkrAiCGfyNQWBl/qYnXp0Dp+IIRFkXoi7+vEwzMF/vFFwdazhkh0ZTP9KCSoByHZ2PRclneqNQp+nZpT8Lide9AxcaVO5WMpPSM/4osQdvYpiQZ99/oo1Qwm+BBu1rUSSxLHRtBHumpEJ1zFxJ4pImpUR/0ZAEn1ucUw5rBMHVWXWM1Laj2HncvwM48DiHIMxPd2flVZWhUJfFuGCH8FwYIWDAslFRLHUfYeuSTDhfHcGqg2MS4rj/3MOnTxuHlXzW0e56gwKgJW5ncCoRFW9xVe8eOa88abjPqL53VHy/m9+EWsmUY7Qss30uUjP0hnxki2p6jIi/GYi1yVcSZWuq1DEPYrIwzuLu2cru6WBvljcV24z06f58k6IzYdba2lnSIz/yYirSFEeAQVDCz9wlrtORkNs0dYEtm+7QcwWeJEmro3saYCRAqvpDRMvSs4QjwerGGfBvO4xmZSuaSK709e79EKIshOQ6MxTzQF37l73hA+409v7/Rucqr4GtxmEYF/U4pmzLu 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:(13230040)(82310400026)(1800799024)(36860700013)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:23.2126 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ac567751-33ad-4c27-b633-08dd13790fc5 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: CH1PEPF0000A346.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6183 Secure TSC enabled guests should not write to MSR_IA32_TSC(10H) register as the subsequent TSC value reads are undefined. For AMD platform, MSR_IA32_TSC is intercepted by the hypervisor, MSR_IA32_TSC read/write 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 Tested-by: Peter Gonda --- arch/x86/coco/sev/core.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 39683101b526..af28fb962309 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -1433,6 +1433,31 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write) return ES_OK; } +/* + * 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. + */ +static enum es_result __vc_handle_msr_tsc(struct pt_regs *regs, bool write) +{ + u64 tsc; + + if (write) + return ES_OK; + + tsc = rdtsc_ordered(); + regs->ax = lower_32_bits(tsc); + regs->dx = upper_32_bits(tsc); + + return ES_OK; +} + static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) { struct pt_regs *regs = ctxt->regs; @@ -1445,6 +1470,9 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) if (regs->cx == MSR_SVSM_CAA) return __vc_handle_msr_caa(regs, write); + if (regs->cx == MSR_IA32_TSC && (sev_status & MSR_AMD64_SNP_SECURE_TSC)) + return __vc_handle_msr_tsc(regs, write); + ghcb_set_rcx(ghcb, regs->cx); if (write) { ghcb_set_rax(ghcb, regs->ax); From patchwork Tue Dec 3 09:00:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891958 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2082.outbound.protection.outlook.com [40.107.101.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D767D1DFD95; Tue, 3 Dec 2024 09:01:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216493; cv=fail; b=l+Zuf+r5eSheKEAVQ1Krz3x77JukpXrEZjVuf51S31nxhh74wc6QjvYou8K4Dp94Zs0IYQ/ODmm9O6lTpSVjP2UeE6CbaqSYnKVGeyn9oo0Q7bTBgGaI5z/OeppGTvAeA/YUULgDShCrghsxrTts2NKY0YjxZiKGvIeZnQ9Tq64= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216493; c=relaxed/simple; bh=xxgGhhE7eqLY0qKIzlmwkzy8uGXHWchoatB47ooDhUw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=meHZSKVLq3Na3VqbIMt272dzPQQJL955dgWioQUUJbem1JD2Fc+p46BcEUV/7+3TFrE+rkgIkr16DHLyG+4fKR9b1rTxya/DsdIZh4ovOKXAEkZhVZ3VJBsY2WyprvSbO1btx7As3QMTrIKGOkz2F3x8GRzP55/GPxPmsFPx+II= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=BFXvB2ZW; arc=fail smtp.client-ip=40.107.101.82 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="BFXvB2ZW" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Qu2HSwh5bfj5q25RQainpCfHRZFOa5VONdoCrv/TfiHiYTF31sC8+V2Bao8UxHU9io092hF+VZZSYh80XlrV2ow6hVFbfHr6slkVbX/4NxtElyCqlqs4m2ZhVSEg3i4PF3EAWcNy6THyCaSI8qG6A0Mih7+EUqr9VIUm4aMjNeXYOCuffZvcVlCrefgxlYytRGsxWaTlseILiJ7EuMFQaG9SgI1bW1IbAYXjVZPXHYo4EnFp7OXeUtXk64NSp+Weqb6WKiEmFxkqxGo8TU5Fhm8XiNbDkgyEWim4GYnQ8ykl0A3QLrP1Nypc7FYoWuevL3VgZzzuBB8SjPfVAIYFzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=Hus6KSWzfISOR78FRMjPuc7o5ht2CzZat74hItkA+L0=; b=kx3fPu67N5Ub4Sk3Ac/kYTobduZatL+pUHC4TI8ZqP7opGKZx/SK1uCxFCqHT9uAkvUQ+bgpU2EBlx9Ow/bT7M96nQ7FaTXsJ0JrZ9QEPHJYYRkJS+ubh7Pv+BuyEonj/xmxlzaB2vENzkh79c4Fvou07QV4Az3sUFnRtUIPpE/cCN9TLIOcSJBklFr8MXYPSX8tafYAoihC5S87furHNFmcOQpvUJV5wDFKXWgYn7hgEDbuuokoKBPdmrNWIOCauZdVyzGglfiOOB1sLktMiBQAxU5JF5aokT6phG9x/bAw43Dix0xOreTR+3WN0IvCGJmtrjSO0R1n0pY2ue5IHg== 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 (0) 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=Hus6KSWzfISOR78FRMjPuc7o5ht2CzZat74hItkA+L0=; b=BFXvB2ZWpRjEHr0MUDny7GXrMHdfNxod4T9LWLqFX/o69I2t+I6o0Pir/cOuckrxVEfVN+SO4h2jnvaXtorO60VCllhHxHBeIJg74hguT79PhgnLJCbBDu1vHUhsX8M0Wr/SY7Pl5mjJW42G17Brca6ZuppOg3CQqWNlI2J/wW8= Received: from CH2PR12CA0014.namprd12.prod.outlook.com (2603:10b6:610:57::24) by DM3PR12MB9434.namprd12.prod.outlook.com (2603:10b6:0:4b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:26 +0000 Received: from CH1PEPF0000A348.namprd04.prod.outlook.com (2603:10b6:610:57:cafe::22) by CH2PR12CA0014.outlook.office365.com (2603:10b6:610:57::24) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01: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 CH1PEPF0000A348.mail.protection.outlook.com (10.167.244.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:26 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:23 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 05/13] x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests Date: Tue, 3 Dec 2024 14:30:37 +0530 Message-ID: <20241203090045.942078-6-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A348:EE_|DM3PR12MB9434:EE_ X-MS-Office365-Filtering-Correlation-Id: b043dfa9-b086-4392-12fb-08dd137911e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|82310400026|1800799024|376014|7416014; X-Microsoft-Antispam-Message-Info: y4qJuQdGrmEfRpMs+JV5B0WM26HmynjiSifxvZXOmmce5Iwy/0P0+FSQy1ZfrOMFVChPeK81B2H0FiwHnYD9gpIb0mQ0KaoAFBZQIiDJAfHewpJD5rBvzzySVC6jnm8jAhnL0slOQ8iN2dEVATc7pkZZRNkUSTh3uJaAclBdVozaCO/pik9fe2Hzy51Sj7phSKi3nnGEf2v8BeX/wIpkikDb3Hb3OlV+NLiBe3X6TCKDWlfc2yLojj8M14Wxr4lsHn2//AC+mSGs0dmOhZF5UMwIB+K/DM/7t/a3Xs0noWclEtmyYCLih8IcpcaJLv4NDOw2m07U6TXxrKJunya0rzeUBW+K0If+EQbqKgfmzn/UukNMegLJPckMqntqVqw1T857zJ7tE/YDExgx9eMAcNG0tAJ0g4Dcga1YySQuuHR3W5UGYZKoQCDDTBoIQZslThnaHOtn8tXDXHL/SHkjtXcMfk2k7P2Fq9Q4btTz2XHitBHEvJyAwRZXq8fndhbM/2CCK+t4Erk8A7q0Z6cuOf8+xcHo3BC8nu9RTyM+DQ8Y98yl3OUxAVutiCrXFKUwfwXWhLmhqfDhFw+0sdB+shc6Z48kPEOv7jAyvWl7617fKMF3ZTa78Mk9ksmuhNic+eFUTc/oGkemDEO3Oas7QIDOF3Tm+4zXcg0L3Hz+Xu2nlhnH6Bw8WEc5bQs8P3tUwt6MdEQdX/abjElKFYtgqxWhqgUWgddXKxWMJofDXwOvjyzYdMi9Jqe+9px4RqfPPU56iMLgWEOdCIfG84flbMYrxLPbYm/ZjlbvBNXfUrnDmrNfIK3HJ+tPYPUl4hLsK7fOLxn0C2j+9z5YMhG0PdIxdlNnIVeXm8IRr3HNMG6Sudj51UOYvVKWSBLD6V/AYvOncqaygcPe2F6jl6+WfO0+KsdDxdUOOcAxmh6cJLl8lIEk6gk90steSNxRqSJjLsGeeb21WN7uzUt4ZWRBTr/Wja85nVM2an3YctHNflka9Rz4X7rk2TKHRCDCQqdOhw/A8aa0jN/o37IPsr9LK4qggqT5MCexM2jvkuYdVk5y75OU2m+Ulnj6df/FQ7rd3oLHVFCo+bKHxhTBsVeybPtOPSou2TPPl6KLF/mHiPcKuqVjwq21+xh9JkJMTliIdVHbXrETMP9vDUD8VHmbyEx6equweEUHpfQsrJIpIqQn0L5KPaO1ijZN0UpO/m+xL6Zy+igY6fypusHC62+BWTBToWXtBxJu4aB9lNkdN4DJ4t/TG8pqThIljZTGi2boF5cfcCkEhgUEjS76gWdfgwW4OTb6pbQ6HqbOWVRJuAvcSdBXll/EG50Rv+OEtUpL1sSMJ7nzNfi1G499MexViRBeqH4MdMBeKmu2a3AxKBUj58Er3gEMvIsykFPpGZl1GtDiYc8HqOGIKTKZnHyp++p3odM/ph/NOuZuyJBrIwsWeNganCtzzy2nYAdXbXYv 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:(13230040)(36860700013)(82310400026)(1800799024)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:26.7893 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b043dfa9-b086-4392-12fb-08dd137911e5 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: CH1PEPF0000A348.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR12MB9434 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, guest execution should be terminated as the guest cannot rely on the TSC value provided by the hypervisor. Signed-off-by: Nikunj A Dadhania Tested-by: Peter Gonda Reviewed-by: Tom Lendacky --- arch/x86/coco/sev/shared.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/x86/coco/sev/shared.c b/arch/x86/coco/sev/shared.c index 71de53194089..879ab48b705c 100644 --- a/arch/x86/coco/sev/shared.c +++ b/arch/x86/coco/sev/shared.c @@ -1140,6 +1140,20 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, bool rdtscp = (exit_code == SVM_EXIT_RDTSCP); enum es_result ret; + /* + * 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, guest execution should be terminated as the guest + * cannot rely on the TSC value provided by the hypervisor. + * + * This file is included from kernel/sev.c and boot/compressed/sev.c, + * use sev_status here as cc_platform_has() is not available when + * compiling boot/compressed/sev.c. + */ + if (sev_status & MSR_AMD64_SNP_SECURE_TSC) + return ES_VMM_ERROR; + ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0); if (ret != ES_OK) return ret; From patchwork Tue Dec 3 09:00:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891959 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2041.outbound.protection.outlook.com [40.107.101.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC69E1DFE2A; Tue, 3 Dec 2024 09:01:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216495; cv=fail; b=RV4XQvTpdaNSAtTjXxIrowTJtSCc200kOgNIxs8cQyXGeAo5MJF3BEI/D4BmMefocxuzf0bIxalYGR1IJKQHQ50yxSwKSXXREVDF1rAoucRwI2cJ+9HawkO6RlZPlhwcGt2CNdua6ud7pOaeUA6Hp2mP0o809wC+WJsTMArKicI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216495; c=relaxed/simple; bh=QCM9U1F4ZFnANebZbS2eTDfs1zWxRA26l6GKwbr9O94=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pHK0NaMfeTEVkqLee8fk/j95JHFvoNIVeo4tpKSeM9hh1LDMzeOpu9Z0e+47XJANZ+GuwyXkeGRsKJcYcc/sWhR7YQWyFjDsZLyS7CN8ybs0c9czdAYGMU7wiYkjyixhksGXkbzsST5lKUCGafdCFdyUW8iDgblKW1PjFVgQ854= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=PYjwMPd6; arc=fail smtp.client-ip=40.107.101.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="PYjwMPd6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GpOhPJXPaPu03CQ12cj1jtsHGiQbeaBioWNXooQA5XwKwAi2swJXfRM2L1sWtTqwk/Fj8GXKTNxRsk2oNnudQxs3F3FQFBpxGgX/XyYDiaQK8wGqqGx/szFYEfI5ps4Crw4HzR692q2xrbg0UtiyXVJ5PImAgxhn+7k5wdWdlKEE3yvCDUSwU4sAo+p2NCP5O3NTnckjBEwizhjn21UoCRK6V1EAayxAFQj1yifkbVyGR/9qaZOIlV3bwmy8K9NzJj2gLFrrnK7Fov/rSvTqAGjR1p392mPQZ3jWWP5zL6AUg50GlrnPXvTsZ+Rpe5RrOrx7Uda3s+hIiPjLgQlHLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=JfFSubw7wupOReCs6a+4PGtnwt2udDILBgj4QoHT2k8=; b=S1+oArrLWAdPi/NMQcfNLgzl0t9pj1J0AwEGTjJiis2a7F0NXtrGg5jbDAJH01za0HkzpW+/khsX+bkgrrTy+IQOz8GtxeGCTI9YwyafHI4M3M12jb3iWAHOOsuDIYfevFLG24rF9OiEfKVRCaM2SGjiGFZNQ0/PgRDPVH0nUcX8/03+w8EJngaq6RwB9HW5o4f+3u7A9l0faCPTSR+8dfqD96qwPBqu0x0Mz6cf3bdQ/UDX1JhRpqz+XEGG2UMtPp/jEr7GwMijWb5zSNGXFv5bO3NiSxLB7ktg3GNBBIJkW4ovfg4s0po4BldZj6TfpvmZGH9Vrwh9HjYCvkuqgA== 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 (0) 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=JfFSubw7wupOReCs6a+4PGtnwt2udDILBgj4QoHT2k8=; b=PYjwMPd69pnGeVkNdxVUTWMcgAMo78EEjAhoGt6O7I7xMOcIfxTXwAyp4IIWq6I1xm12QYsGNfFYqNU8UIALb/0hHKKIr8UnXFvBF0zsXMmnlEnayxnCAtmc/UfjYON2P3UNn9w38cpMphbJfsK50fe8VHOrK9i/6jzbe4OLsmo= Received: from CH5P223CA0018.NAMP223.PROD.OUTLOOK.COM (2603:10b6:610:1f3::25) by PH7PR12MB7968.namprd12.prod.outlook.com (2603:10b6:510:272::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:30 +0000 Received: from CH1PEPF0000A34B.namprd04.prod.outlook.com (2603:10b6:610:1f3:cafe::53) by CH5P223CA0018.outlook.office365.com (2603:10b6:610:1f3::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:30 +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 CH1PEPF0000A34B.mail.protection.outlook.com (10.167.244.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:30 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:26 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 06/13] x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC enabled guests Date: Tue, 3 Dec 2024 14:30:38 +0530 Message-ID: <20241203090045.942078-7-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A34B:EE_|PH7PR12MB7968:EE_ X-MS-Office365-Filtering-Correlation-Id: f72db9c6-6609-4ec0-8f14-08dd13791416 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7416014|376014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: xuv+y/iTBiMfuIzUBdyRZ8dOT5IysP5C/YkJpSCzw4hwFsifsUw5fnG9d0jTjtnpqTGrtT5G4KWTyV2WVFEGcQmUR35dgxcdCip+mCu5jPmE8ElwbFOGoD6Tju5UACBiQcruuNMwFDszmOlxmptkCgMUMGOGL8+8SIvEJILfBnh+gbpWFYZEs8o55ej4hjb3GG5CuLOFFLrgsJdpaZJ3OXi7PmnKIg5tfS2KU7HtrWU6y4PgAKOXOmk9y1aaTelzNhrfaWRieHi7M/SQVzhQSxQ6M8r51exS+n8ft0x1O4Ic/yXtVQYzJREGZoBQfUni2OCf9K3AQ/XvEH7SyqrNDimyCnvc70d1acnXRTi7tBUZUWwbAN41Tah68o4hTwgDNOBd1ULVdpYk5JcpBSwv41kEUVkwBL26wrIPhd4qHi3kSkE0C/Q2IFAvPeZaBLsjc0r6yzTtArjuhzGUe9zsvrSkCdkZBq7agKHWhVkXm7aA31lKvYTf8Ksl5bXdwl/xekmDmvX02rzr1GjtsC5bhHkFWEOYcU4tmbWu5uh0d+i8DArSk2aeyUNk/Nh6XCFYOZl6agSwfzYk54vPrNB6EIQ7ILCSy5q11H9nmCGUTYtz1ViWs6E8Pcu64p4lQdGBQJVdJT9nlDmYntm98uuA+U1PInytETub1tvbo+omPl/sLJYu378kIVSX7I4znizxvlSoPylNnehKo1ZTqiuMUV79YrWeVkkfKk47j8Pmc4rP0Utfy8hdQbzRixf5LBFl3a2DfiOSlquITpXY8o8w+UeI/zROvsh0Ei4dtUnZq1uFx0lIrZcNSuOqDOmfyqjIkVhc6wlAHFEfqMt1TKJxkcGH7j5nqobW4SoL9/XJwQnkzxW+hdWUulH7WWyiURlIBhLvbpwRJJ697tAr1jHiXBcaesR/i/IfwM7SRLMVdN5nIkh/ygguM/VWBpIIfKAiBWQxoSz0VBZxIuoTIVCDdnWWIrFxhJgBoUvKwrw3QyfhONOOE2DUpuMf4LSQ0W18wmWaPj5YDAaPt5otHKmJiY7clx1X3Be8CT3IHhyA1ltg9B9GwQeyRlIPRw/6l4a5TOzSsiUUC7CXTudo8SrYJnblzhNEWxwgk0xZR8iUV0Szq9T2C/aPyx5hbPHOVjde5aCKY9gg9iAE3jwqxRnVmRbqvze7ebxPv0S+12HIAukA154n80s94OfWALYSp+F9/macahrU9oxsGMcIUeEcECK/iIqPbqMxKLKCPolH1epSzaPdrFjCr9wAEdaQJ7n51Fv50Yy4cG0pzq0hPWDGmCA4FLgj9FBrxp+yZ6pkrGFGq0Qx5L9XEcdh7wXvq5qie7lEfxsgd/4DgESu4dkIHG4OnVxP9PXlJb+EuV7O2Cq90cetwVynK8UQYD5ff8QLP5/5S3pmswkr8hy9cw31PAU9Dtz53VZib13eIT6mz2af9AcpiY/Axo1ttv68oXav 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:(13230040)(36860700013)(7416014)(376014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:30.4534 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f72db9c6-6609-4ec0-8f14-08dd13791416 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: CH1PEPF0000A34B.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7968 The hypervisor should not be intercepting GUEST_TSC_FREQ MSR(0xcOO10134) when Secure TSC is enabled. A #VC exception will be generated if the GUEST_TSC_FREQ MSR is being intercepted. If this should occur and SecureTSC is enabled, terminate guest execution. Signed-off-by: Nikunj A Dadhania Reviewed-by: Tom Lendacky --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/coco/sev/core.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 3ae84c3b8e6d..233be13cc21f 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -608,6 +608,7 @@ #define MSR_AMD_PERF_CTL 0xc0010062 #define MSR_AMD_PERF_STATUS 0xc0010063 #define MSR_AMD_PSTATE_DEF_BASE 0xc0010064 +#define MSR_AMD64_GUEST_TSC_FREQ 0xc0010134 #define MSR_AMD64_OSVW_ID_LENGTH 0xc0010140 #define MSR_AMD64_OSVW_STATUS 0xc0010141 #define MSR_AMD_PPIN_CTL 0xc00102f0 diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index af28fb962309..59c5e716fdd1 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -1473,6 +1473,14 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) if (regs->cx == MSR_IA32_TSC && (sev_status & MSR_AMD64_SNP_SECURE_TSC)) return __vc_handle_msr_tsc(regs, write); + /* + * GUEST_TSC_FREQ should not be intercepted when Secure TSC is + * enabled. Terminate the SNP guest when the interception is enabled. + */ + if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ && (sev_status & MSR_AMD64_SNP_SECURE_TSC)) + return ES_VMM_ERROR; + + ghcb_set_rcx(ghcb, regs->cx); if (write) { ghcb_set_rax(ghcb, regs->ax); From patchwork Tue Dec 3 09:00:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891960 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2083.outbound.protection.outlook.com [40.107.237.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A822D1E0B7F; Tue, 3 Dec 2024 09:01:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.83 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216501; cv=fail; b=AjlUfTNtqoM5b0Ywey4FE5SDr5kVqqw0sr3apYEUxvA2P6CEtWZSka+L+uSq9Whp0ZrqrT7ZdC05fWpP4rct529r4GCWSXxLcW3i38y1JsXQnpVg3LOw6FKxam2lN6/O9xVo0Rzy0P1d/X/gTkGF1cVf/zpQvDSGKZG8/aJACdE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216501; c=relaxed/simple; bh=f6vubs2pvTm3eVh/13qnd4PIdAdCj7J+u1+haCnCZe8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aDe20Ap9sOgszNBEAxfTf50NXU/JApNcvZSuPQMsjyWTWoIbB+AixA2L9lFq3HhqHHXoCxhC+H6wuqpiHNvEoytvtaKkm7fmg2zwgAk0kOQGRmzJUmErZa7LKU3JagruXpjXA0wm+Fq4P2rE+8iog7t3TpHA+CBdOS8GFs0O1oI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=G4kX5rgy; arc=fail smtp.client-ip=40.107.237.83 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="G4kX5rgy" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Z6zDC6uguGTIBPPp/jzyZsJL2C9HFQ7oOLrT2FyGwzgVJmBPg0yJn4dIk28k0gsoF883aH3egjDwWilvM9GnnVsU7nPC4CLvZ04k2NqgQxf03Btr6MPus2gA3bEkDvum2yRhc9fmB3HR5yQT5QX8aEXRfHeJCiQQgmFPCwJ8nqSfxGXOrU7eL4OQFS7MrJ9kV1Jl0t7Id83vi/HkZL4UEBHKBfTAGJ+GZU4jnhmjS5TtRvv5gZ1LOpGCyAo6DViY9Y3suC2lBQnWbSB10KWPRerzaTb4NGvQgBuvSJ1htgDWy0gb5+SfensPzaAJ+oLtKwhQ88LI13jwGQs1tS0mdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=QqfnoyTzYJGKUa3O0n3sbxeLFTWLytTIonx8E3wq6Yo=; b=rFKuriVOajD9aWAhh751l8yu+nk+N0iiDBOLvTgLxyT+ZZEjQ22uiRZgZ6UMhkklDPHhL145JaLcj5G8h1ZYQJcuRNlk/JjwSiGz1ZF2FSoJ8sENR8v03yiKHlq19se+zOdnwJ9Kj7S+efrCMkK/Fde8eV3w8jH0d8lcHYGa6elMkjaAVgnE0KbLYUk5dSSuVvufe4OqGSIXp2UBFVuuU4BEEGuy98Ub1aShjvmIi0Glj7kKUQbKqw2ES4flb7p75WkWhdEKHI7X/SosAywHZbTbuY6JHdIqCZWt6H5PBxqYsYDPTKEXQVUQFD3aHtRp4BwfzrK97sCOWn/lItETfg== 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 (0) 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=QqfnoyTzYJGKUa3O0n3sbxeLFTWLytTIonx8E3wq6Yo=; b=G4kX5rgyJiYaokBLh8X4zjs/3LZ+w/JgFFcbMQuuYIOoXk7ZISS/yMfzXo6KPw1jaYBjRZgUX29v7Xqx9rQWsqhBXHq0gHj3JUwNXH0iIO16Z6tdrx6kcgj9QHx+1BRWQvms5+IBzizsItLslV9j+GnJePE+ovQBwcg6GbHh6RE= Received: from CH0PR03CA0120.namprd03.prod.outlook.com (2603:10b6:610:cd::35) by PH8PR12MB6940.namprd12.prod.outlook.com (2603:10b6:510:1bf::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:34 +0000 Received: from CH1PEPF0000A347.namprd04.prod.outlook.com (2603:10b6:610:cd:cafe::70) by CH0PR03CA0120.outlook.office365.com (2603:10b6:610:cd::35) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.19 via Frontend Transport; Tue, 3 Dec 2024 09:01:34 +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 CH1PEPF0000A347.mail.protection.outlook.com (10.167.244.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:33 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:30 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 07/13] x86/sev: Mark Secure TSC as reliable clocksource Date: Tue, 3 Dec 2024 14:30:39 +0530 Message-ID: <20241203090045.942078-8-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A347:EE_|PH8PR12MB6940:EE_ X-MS-Office365-Filtering-Correlation-Id: 86ba68f0-7b72-4e7f-2563-08dd13791624 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|7416014|376014|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: 0etoc8hubzlFNREKmHjZxmugrindgNLUt9l6XNhPhCoVXxTRspBQ+Cbph64xGAYzkHy38tBz76TtXzsJn4Z6GYTC3/QGrSMJ7FwP2EGOxqg75IgPYPnVP9O1REuu06AkOy8qQcMtT2SV5MMEpwmt5rioGYnGhZHopEAzDQXRdmlNF/2HSknyTT+9aOHwULDzj0nQBigo60cSMAhpVJvqKUnx/Bk5qIsPFsHg5O8oN7s9YfvF7TXula7uQMkwWlYtwjysZvkJ8VGrvjEbUcv0pVSgEmrt8RPil0Qdn1Ddsab6ucYXTS5H4wl9/9R9uhFNUWcF1GpMQqNr5JzGvP78xGLpjsfVzNKkfl6Vrh4LrUehbjZjXM7kHmW8mbwNXfExroT6qAtqCcGsBpnq6FCtJP2UY963I+X4DrNEohvmmRlskcCFgMDxzbv4FuDpTkWt18n291HWgUMPt6QRbx23T/DxbWplmrWR3tp4y9SXLJP7DfrmA/lB88RbKg6AYSIQgJHMDcdzdMeB5C8MfQ1FutQ8jWv5SEchya6Xw9f4FUjEemMX1Jwbm0pih02BHq6xUwA9m39Lsib93aPeRP7hM6wdM4E+x7uQuoh5lUJsTAhqWWSDn0VLVmS1Nzhr71S0rGTafXi/e+QZ+FBFGNRfm0XyJNFZ2zimZI6TDh77o5ACa0MuaxNKNwvPJjLGtzbAkWYTFGQNC81yGm5qRbvkOALG61BCYcQ8lmfy4b9Zh+mEjYu0lLz866zI68q6St8rGtuMn+3rB0Ri02ttx0Px7edOmjCs7xhNBvLBdRtgWCoARNZSWdaqB8RgKxzA86ytdAaQ4WLWy5VPTzxpNVt8PdtHhXnFzmyH7qRIgPb5SCPxLR2ORj41StnDdzjcjnF/zLtFczmIS0yLkokSgPr+a5KIhasjaC6j5gHdZZaAKR5YDXprlSnWWT6nquZdtRvRjCIQZmOfAX7nvNdfEbjieM+sbc6gEKyqDxs5JaDQ9wntOStkzl+cC1yJu6/sUUAGr1QqCsIb8exTTBSJvjeoBY2dQHBEbZQDE8oF1yNTav4y30BZYjzDIuELXe9lMhs08tu67eSTlvc8layfzw/BKgm17Yf6dTjmuswMp2ZX1WptWzOLuCOJsSyAR9B2FzC5DTdpCvjabtygTAvr05iFga6NCPnx8/eHl6s2SqfBIt41F+UWHCkBAUhGgrjOHY21FoJrk0wms7HocWjBfH4LlOA+UcTj1Bpmppyn/bUpLZAy3WR8BXmkQm1tm3wrduSNBlhQi+IwJP4E1Z2taum0gtuMk5o+xYazv7iNfa6z0Gcp/iuaFNPa/bNDEPaC/t1+PTr9zXjdKuRJeSDdyhLZUXaeGi94igfjtL0tDYSuGj6RHztTUPQZH/kI1MIWxBKBmY2QhTrkHSFbBiXgo5UW/Jm3M0NUcO9GY4zIawtFF8VEGsc+1vGtbr0yplOHEr4i 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:(13230040)(82310400026)(7416014)(376014)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:33.9447 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 86ba68f0-7b72-4e7f-2563-08dd13791624 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: CH1PEPF0000A347.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6940 In SNP guest environment with Secure TSC enabled, unlike other clock sources (such as HPET, ACPI timer, APIC, etc.), the RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. Hence, mark Secure TSC as the only reliable clock source, bypassing unstable calibration. Signed-off-by: Nikunj A Dadhania Tested-by: Peter Gonda Reviewed-by: Tom Lendacky --- arch/x86/mm/mem_encrypt_amd.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index 774f9677458f..fa0bc52ef707 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -541,6 +541,10 @@ void __init sme_early_init(void) * kernel mapped. */ snp_update_svsm_ca(); + + /* Mark the TSC as reliable when Secure TSC is enabled */ + if (sev_status & MSR_AMD64_SNP_SECURE_TSC) + setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); } void __init mem_encrypt_free_decrypted_mem(void) From patchwork Tue Dec 3 09:00:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891961 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2057.outbound.protection.outlook.com [40.107.223.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4D341BD9EB; Tue, 3 Dec 2024 09:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216504; cv=fail; b=PPQdCnpgC8ts//X93jr76zkbOgn829LbcDKWwuIwcS6KiTH73p5rr0ee8Fg3kCJ1/hEvdp4K0wIB5ydvl6mAYL0Eq4/obc7QXJbMsuWGEOAWCtb4sbHbvzSISvmePgeRuFtJihhAL/bJFSMzgBtbEp5HisKD2Qi00HSWZfG8zo0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216504; c=relaxed/simple; bh=pSEt+x5pK7qLR8kzWGpKtymdDI3tYGaJc3XCrr4HDZo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JPZD5NWI8tqZAAjGyZpEjw/M0CENYEKC9mCVVyZmF49pmnfRfBRiOWaPsSkdhkqUqNaZ3HGHt6P//9+gKarEwbsO+v2/+S5E+fHfjVB2iy5dCn/PCj6OLI4VdXbUUQpC/0hPS3o10gN5ALHs0MCLSv06Okv3yuR9QEHw4IDZ1z0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=Pe9WpBoV; arc=fail smtp.client-ip=40.107.223.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="Pe9WpBoV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hUsahnoa++wQmtIL6r8amaNEAGuTxbolAy+8Nv261ag6vZeKIhf+2M5qwc9F5SQBc+jXTtKrCD/M0999VtlLZM302ScKZPEXbdSAtRbez5jsVlIPywqI9+UovU573h3OoYb7X6VkR3Kg40a+6iCNF3bIQ/jyF+7mXKBiNN2aaxZstxMdR/gTPg1hUBg4cH2qzEovSMmH54yQ5rOZgY54H4OT86ew3fkDQeiBZMRDhM6eki9qmD7K/McA/920/CQccHNK6YHrJaDfn8OjpnH/bYPam4RwQ8605yDHvEUzHNZwMgQbrSxcQNPftNO77NNuzgj+60NWV/wYisq4bT6/6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=gRCKVDfmEN69C8vV943le0g8cl+S2x3eSRUWNJEc5Xc=; b=TwqqdG13io8D0/Kw9RP0vBORVxHvr7XqNo5spJum7HwUkvmOwMsqAspbaW+hB24T/fnXaRRLdUZIwdrY9/YOdwe7nva8Af1R7QOFSJm1l++r3yp8vRR7V1iQWC/aVFCVxsLP9UIxu/u7f6Su154itkynqqsUtL5ItnLQtwA6rL79v6R0L96h2ezLenYTgJopoBXuA3dRYy3eHFvNvBu8S/wfxI9ccozm9KwJ3NItJZIzVSlYxCxhCyVzIbAKgpHv6KTnJykSHa5YP10jHaI7oAOgo0SzsqD7cKkrREYQS9BHexoNqzw+JIfkC1Y4ijHzRz6yS6oTNJ70PiWx58lIRA== 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 (0) 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=gRCKVDfmEN69C8vV943le0g8cl+S2x3eSRUWNJEc5Xc=; b=Pe9WpBoVV1n4vwPH+TMCWgvcAvxyMVDTdub6vtWQVfoLwRYFF3AAtS/2GqbfzjrGuIxAQkjU1PsIalif/xrjFRtNeQ4IlGEyb3XIhbp1iUSN7M53xBPhx+cA0JHgHBbLUFDfcUW9u+PrZpd4n3HOnAKWYF9rz7uU+XPewoQUBAk= Received: from CH0PR04CA0044.namprd04.prod.outlook.com (2603:10b6:610:77::19) by PH7PR12MB7916.namprd12.prod.outlook.com (2603:10b6:510:26a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:37 +0000 Received: from CH1PEPF0000A345.namprd04.prod.outlook.com (2603:10b6:610:77:cafe::f1) by CH0PR04CA0044.outlook.office365.com (2603:10b6:610:77::19) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:37 +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 CH1PEPF0000A345.mail.protection.outlook.com (10.167.244.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:37 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:33 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 08/13] x86/cpu/amd: Do not print FW_BUG for Secure TSC Date: Tue, 3 Dec 2024 14:30:40 +0530 Message-ID: <20241203090045.942078-9-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A345:EE_|PH7PR12MB7916:EE_ X-MS-Office365-Filtering-Correlation-Id: 714e3424-fde9-421b-f8a1-08dd1379185b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7416014|376014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: O7gm1yuRtIG2wNCN/FdxkqTEXWhiabPr31jExJRLRSlFn93q1npqMVBBevlTdAbND6WhE0oZBcINUICX6xJ1vPfMZloIjr6mmUzPzbpKTQiUpG3bW4wXUrcZHQjlqUFzhlf4Jjoo0VAnR8sW/huXhjpB/WHhGTvtE7dzrXMaFv1N5Cc0WWafczc3BclWr4gopj2mUbjdHzfwNHs75GIH3ieMHmzR5ywFM9IcuO70uuQT4c/eSZisJfNr4TF+nsajEemL8W+JoBP6WYnO+ghIaMl8+aBUAH08OYdob/j3ylhUP46v4141+Gvu9CE1I40hp7GhRst9/cbgYbf+zJicKiOw4NbHs/mUpWeFZjfMmjQQnNja3+h+DzcuF5YQj6Iw5Nsmqfb+rdUjZefjlyOkFFZTPdktoSTvITPtzYg0eiXqp19CnYdwgInwgylnk/qEGE40g0B9S25LacEoBJ1zbs7ztVx6NP36UrTxN2RpFhcKSep1NttXCsjr/Wc4wNGSiD+h9WcxK1f2WwuWcifFgCfzngaMdVhovyyVi9HWkj5cdusGMoDX240Oi2N8MJgGdHJI8CePi4Q6qMkaDtZwi0hnm4+NOFwGt69ZZ5WTCbMgXxUFAx7oVgZo9/aJwzP34HFS/HTKSBneVYaq2nn4G2xpAfQqmbigm55JnfXZW48ZT6v++CycwSd/5Q8t1zD0aE/9gstsyBj84pTEegE0kOIO3nEMpERy43ErsH+LoCALIq5lDnE5S6t18HhVn3Rv45SUAbncWXF/vo2RQNS+MzEIk0hR2fcf37a3RjJ/e5yhEGU2XNabgkV0jbDnPDoTHeYkTa3lbnC/lH3rBTAH/cogXDsceWjOni0Okj8vsvlnoVJrGtXmpVndpCLPBdK2R2jebQ1CX6w1i025u6elfJHabgk7EAFd4tBFPi05N6IcS3MUy9vFudAifuDZy2vGnmxkOzDZ6/Sx83AzqKo6VtIHk0QroJMI+ptqwCr98cZ/U/BRZrZ/ZstZL5maYnkSiLfwB8ftNCHi2UUXc4BYjJ8Fge4zJnUDgIrWIK2IJpY+MrM+k4VxJo+nRxZdSd3XzqxlcSp9hzYk/IEo+xJgCtEr+yGIcNWPtjIAGC6Vf72UoBqTRx+LbxdRxl8OVJtD3zAzC+lNNiOrKiZ5UYBViF61ZH43kpN58JOw7o3fxiA8GkO9K7JSdP1X0N6lyxtcZ7EJvWfpUVJJ3LvTsgK21a+SjXNjV01YBR4/zJMjjSfJVcBsn+VAw013SdFG0A0k9F5RYqhXy2Lm0hcx3R7ZbZDterudNnE5QMEHuSZrnHUfGyFt4N35zvofpezx4RRnnRh6z5fNO+4OMxa5df+Lr/p5PtTjE5UlSYnIe2S/k09ev4TvvaIT5/F2fp5EpdkJpJjJk4s4PL6RCLkVsyw4gXKLvWFUY1PHsvJlG2mfxpiqOd5kWFJEzKK1T6w60TT1 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:(13230040)(36860700013)(7416014)(376014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:37.6348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 714e3424-fde9-421b-f8a1-08dd1379185b 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: CH1PEPF0000A345.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7916 When Secure TSC is enabled and TscInvariant (bit 8) in CPUID_8000_0007_edx is set, the kernel complains with the below firmware bug: [Firmware Bug]: TSC doesn't count with P0 frequency! Secure TSC does not need to run at P0 frequency; the TSC frequency is set by the VMM as part of the SNP_LAUNCH_START command. Skip this check when Secure TSC is enabled Signed-off-by: Nikunj A Dadhania Tested-by: Peter Gonda Reviewed-by: Tom Lendacky --- arch/x86/kernel/cpu/amd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index d8408aafeed9..af9dd545f0ec 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -370,7 +370,8 @@ static void bsp_determine_snp(struct cpuinfo_x86 *c) static void bsp_init_amd(struct cpuinfo_x86 *c) { - if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) { + if (cpu_has(c, X86_FEATURE_CONSTANT_TSC) && + !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) { if (c->x86 > 0x10 || (c->x86 == 0x10 && c->x86_model >= 0x2)) { From patchwork Tue Dec 3 09:00:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891962 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2074.outbound.protection.outlook.com [40.107.95.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 915E01E0DBE; Tue, 3 Dec 2024 09:01:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216506; cv=fail; b=jYuVJ2BLEZMoTdm/Xcfn29vEthXxOrWGPlz2n8uVq/JYJqGX+2hDQGCjssqTeN598ot1GU+FX2IqHyRwIm1ifa008DiZnWqKLw8X9zhHp1nx7JnPjEeUrOyxXXDum0WuWTrZEIo8LNfhDf+dPZl5oKnuE01cLMq4A2KTMALov/U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216506; c=relaxed/simple; bh=P1u32rIZ+FqWAL216aVVfOa7lDFV7oeVfPh5C68lBK4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e9NrqyzQD1kXidR3wm63pkzC8/DC6ocbc4hJgtYCDOwj1WJeOaoobKd5JBDgT9wlQn/rgsamW3HGERTmSvRDTQIxh07n221uFEPzfFozj7Vp6dh9Dr6STrxUn4cqZ8CM9d294N/X9mjxW03USRse2ilD/hE1dwo9ENauuxEB5os= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=2Ar98UrJ; arc=fail smtp.client-ip=40.107.95.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="2Ar98UrJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jr2Jelpn2wnzOUY0zXfAuNNDbJKAY5Xucp33RcrZnj2RIZZ5QpOtl/CuxkwEM/BBJIB4yegFokOMNqh2q+SAEx+f84EEdOabGjJ6T770vSsKLjhtOWPJ+tQFawYg0vfB5xAQpTtsJtRePJ3zJClo1nHpU4ZLq4oiJ4Vtd15WZdzpiW2p5bOFVKmE/uIetup/NgpjtfkgFvtc22qNyvg0Ct+mIhaQUD451E6YJ9YqK06auQsMx2F67xcq30X1UpsvCe+yadddr3doC2MbJe3asL9DkR6E+VkgFNIa69HIyDxMw7olSiw0a3RbZG0Mv8x2T8pp8NLlidVnq06hV3HM2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=TnFNgpFnVxXh6rkEe/mdjR31OUGytIueqp5ARvjA/74=; b=gPbpon0QC8G6zs+FPiaV0HaeV2aXAfFYfkDeIaCahkZOGsOAnCCYBhpaWRsG3DmEbpo2v3hYMafeypug67Q7Qn9EEWljXGDAL9P+BmaAiUoyOOcGLbjEzjOtN6Zk1R88fiYAZWjUz+gZ6tQHJeAAT0MJfMhqq+7a5ecuvlM7go3oYvVsLiMLwLviw8fVq7XAJlb2MHNMwrBPY7Uy0Ruwksl4rIF8gqhpyjYHOpGzxKWakH62Glb/keQcQzquPpJejuoaFcphTaAyrzOuPmepbXPQjgGfF6KO/EKptub3ky4X1FQKvxN9V1ib1UKfaB88i+8hQUblZed7Ura2TyD1KA== 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 (0) 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=TnFNgpFnVxXh6rkEe/mdjR31OUGytIueqp5ARvjA/74=; b=2Ar98UrJvNKGWKSVW1GIw0kHiu+264m+ciUd4RLN0czgIbGqnG4oTxpVcRg5SWWniqYusa3lRRfUhCRkfJIxKMCfhjlRk7ELgkWzAHmwbQLPUj+no+22lGhB4+f/VSHbqd1QLDZw7kAGZ75cw3IjhHdl92f1a0jimeKqwcadH4o= Received: from CH0P221CA0036.NAMP221.PROD.OUTLOOK.COM (2603:10b6:610:11d::14) by CH3PR12MB7548.namprd12.prod.outlook.com (2603:10b6:610:144::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.19; Tue, 3 Dec 2024 09:01:41 +0000 Received: from CH1PEPF0000A349.namprd04.prod.outlook.com (2603:10b6:610:11d:cafe::be) by CH0P221CA0036.outlook.office365.com (2603:10b6:610:11d::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CH1PEPF0000A349.mail.protection.outlook.com (10.167.244.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:41 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:37 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 09/13] tsc: Use the GUEST_TSC_FREQ MSR for discovering TSC frequency Date: Tue, 3 Dec 2024 14:30:41 +0530 Message-ID: <20241203090045.942078-10-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A349:EE_|CH3PR12MB7548:EE_ X-MS-Office365-Filtering-Correlation-Id: a98a3ae7-a62b-481e-8db2-08dd13791a71 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: zQ2Wmrfr6yRmIj5a5ePXd2dusK9+QG+JA4+0psTDlw73rXcv4ddFZShjraUXq9YTwPnVFygsTnlrmr/fUxRYP3HkXVZbxZAcHU3yPikiEjS6pY9BxvL9U/1/+QgnsNp6ZoTw17E+copnFCQ75Geps+wTiOZwWvYkiEd/mFiCTxbvXj3c/pnO8q5X8F3r3uHfkrMnwWFYYciNDQwBZKP5uHdqDMXC5zw9mQWmwLz+8S6xOublzWVtHHr3iIlIss4eeCuErKIIrwS2u6pVX0Q0wp464WIcq8QUTUcPppnkKpOfu35Ktizn04GZYeM21Np70pofn2JfsROY3rYsn4wzryHd0K3H4RbxBJz7knk1YAqC3csMCQFcuTPnLUgcvDKawTJInRBGBJMa4G0A49lfL98CdK4VOXVpT+O5P1DJpsWafTGoSf2EEZcfMKqBKjTDHR3sodAMFUYy+n6BPptLdDUokO/brstWEl1Nk0mYwQ98uxynROzZrwSiSHNZx0xZPiRi+keN7rC/nPuOwypl65lr1NwVi8ZKE6b2/U+q+P/haXXWn0VVJdhOQq4h1JFJ0EHcjGohv95XBS2mI3P/Y80Z6ckO/YbZ/lKcgVknYR+IXywzLNlgi14mFB9YzUKnBSTpXoQ7bAyhES031dAMmDBKFqLAGc0bXNPa0eS0u1W36WlJEiRy+JaE8O7kxeLmzu08eTTqYElqJsyrIbuMuWzMuYeXsdTsJoDpsj9PnYKKguTv+caf8tqM9E24Xim2BaxiEBqRwRv8d1ocu23KctTZcI8loz9ReZ3FbShzkbFis4Hg8R1jnVYiRFXsduHu7/CUx5AJfNWYdD1JcQ0aB9cphdG4hl0ielmpQtCATbN6O506WfJejpchehOwDEpPitS37XiXfCMIu12Cu/ggDepn3dGsPCt5gGqoaeVI9TWFceAmEKn4AiaGHXn+qiiy5xNpIAAiz2akF6gjM8H6HB3TDjZQZ5JG3vxVtxVONg0qja8kbbpb/md2kIXR8bhm/inGA+Tz6lTIPuF28ZOr+fjqmfhjx03O1mUSiCGSRINxGGfbQpzh1vyOEcuMKJyBDl7dqM/pyCDrqRyIpo84NzxREV7ZPLnxORXcVxPt65zozkjueeJ5h0jDqWkt8hPETQ9mPyHNWoJyFsXhv2JaQ3ogRe3PytT3Q7MrSAzKlkxl6ywZBE+jU70OZ0xCrudcHQGZhQaCYbTNI6VklKAXlH/7eFVXCyrwHeqS6LFZ18M8zR/QyI4+Y4AkspVJpf5DqHvG4cDRpyhthWv90g4+ZC5pS2yRiX0qEtGgGD23Rr8pGl9Mi1/lig7lVkb1i42WGhWHXX4P6Y1euRv+7ePCBgpoqvINCI9Xaw2WQc9SmKKB0VHARaI5bfWb1g6tP69qI9l4d5M0Wnr2+iagvnzavjKPOlQ35AF37k/c2h4xc2bA9+e/zGtu1RSFoGTP3+jj 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:(13230040)(1800799024)(36860700013)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:41.1317 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a98a3ae7-a62b-481e-8db2-08dd13791a71 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: CH1PEPF0000A349.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB7548 Calibrating the TSC frequency using the kvmclock is not correct for SecureTSC enabled guests. Use the platform provided TSC frequency via the GUEST_TSC_FREQ MSR (C001_0134h). Signed-off-by: Nikunj A Dadhania --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/coco/sev/core.c | 16 ++++++++++++++++ arch/x86/kernel/tsc.c | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 9fd02efef08e..c4dca06b3b01 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -493,6 +493,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req struct snp_guest_request_ioctl *rio); void __init snp_secure_tsc_prepare(void); +void __init snp_secure_tsc_init(void); #else /* !CONFIG_AMD_MEM_ENCRYPT */ @@ -536,6 +537,7 @@ static inline void snp_msg_free(struct snp_msg_desc *mdesc) { } static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req, struct snp_guest_request_ioctl *rio) { return -ENODEV; } static inline void __init snp_secure_tsc_prepare(void) { } +static inline void __init snp_secure_tsc_init(void) { } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 59c5e716fdd1..1bc668883058 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -3279,3 +3279,19 @@ void __init snp_secure_tsc_prepare(void) pr_debug("SecureTSC enabled"); } + +static unsigned long securetsc_get_tsc_khz(void) +{ + unsigned long long tsc_freq_mhz; + + setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); + rdmsrl(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz); + + return (unsigned long)(tsc_freq_mhz * 1000); +} + +void __init snp_secure_tsc_init(void) +{ + x86_platform.calibrate_cpu = securetsc_get_tsc_khz; + x86_platform.calibrate_tsc = securetsc_get_tsc_khz; +} diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 67aeaba4ba9c..c0eef924b84e 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -30,6 +30,7 @@ #include #include #include +#include unsigned int __read_mostly cpu_khz; /* TSC clocks / usec, not used here */ EXPORT_SYMBOL(cpu_khz); @@ -1515,6 +1516,10 @@ void __init tsc_early_init(void) /* Don't change UV TSC multi-chassis synchronization */ if (is_early_uv_system()) return; + + if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) + snp_secure_tsc_init(); + if (!determine_cpu_tsc_frequencies(true)) return; tsc_enable_sched_clock(); From patchwork Tue Dec 3 09:00:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891963 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89B961E1028; Tue, 3 Dec 2024 09:01:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.71 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216510; cv=fail; b=QWs7aZHAMgOksKbSSRTPD7qExe8TIWgDWDek7bhF1l3aG6FWwRrt/Zk51sz7lC2Ll1RFjPykVz+5yOzSSHmjKpWETHBlIFBTH8JOGFspIq8NRKnqbk+kEXvfEQIg/b/m4AQuJo3UrWi7oaLP5VDAvNov7Uz0xXwfEQ7K+w4SKrA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216510; c=relaxed/simple; bh=j+FEN0yRJGy4p/21ziBMRJ9XzjjBvYRFpKkjqXPHsQo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lSXqCDrCPExCvT0X7qSl9fUx8g4RlO7r8yMFNMGSHbA9/eQBhpEZRF/dBdsJUAhPQnk6Vv77DjWof98w+JeFjLwfL2OzYBl+LWN5DUCYeRm+B8y+9h+VSVU9an7Y/cfjWsqWHqDM7vQX7ozuNL3C8UEJ8IxN8v2VtJTL6GLVZS0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=cjJJ5/ng; arc=fail smtp.client-ip=40.107.93.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="cjJJ5/ng" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lOdBInXbiIVsIf2UJZZTDlt41XaA/YPh9XAbjiquDkjkNN5dptHbz966XgO7VvWnew9CEwVA1sawgBtANk8kOMS9QotFW6uGsPbn8jxxE8B//4tHr5vhxuIRe/sBW7JzhHJRCMnOhG+Z+XmHcqIL5XUtVTnLMXS2qIQY0L4Y7c+BkSIrw8eYMn4exvILMYGIdlGA9cx8tGzfGdXbQr4qPYO5lkSKhmy7QXWigzPv5nJuTDYScC/AkUb3wd+kXfr1c52MXq/zc1Gr/wMdw1jWMNhlsMA5gzDhQkqkBB2De8MYPofME5WGi42aQRRWQAh24OLvZwzolYaAJhhwrqeSQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=kZfRjAV92vPxcDMdOyZ3ULgVmfzT+uIw2EJDl1J3ZIo=; b=icBjrMPTnjqkjxLZrP4zxt9y9V8syY0CF9WwLSZX6hK7/sZMhhdNle6aYTkdDmW65hsr05eEfVscJHzYbUO4nYiRcDEB1BksMKWKfQhlSf5DqYG1bp3qapmuVWko9gnMFbMC1kPcQpjffXU4dZ2XqnXL1HgmKlZPvlNsL7d6PqfKgvftqrhzePAPTJ8DmhwFEX7GONGrSj+CnZE02x171cfcFBa3uMebo8z9IUXdL6TXcOhDETUGUcydKH9Cl5RuZ/3BkR1sZTGxlKHp+2j+LvkoKWYKyy2YWe0SBnkr2F5pG9V5bs+UPu3tncFuYn784yr73MpWEH6Ake4UraZ8bw== 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 (0) 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=kZfRjAV92vPxcDMdOyZ3ULgVmfzT+uIw2EJDl1J3ZIo=; b=cjJJ5/ngTv/DawgF3HrdnLDEjhnRJfbz8zEVy4RmwlAtFRptstJHU3+3UP6D2AoAIPt2ax/VhpPcnha6bpu0wyI7tNdLrw/zGnErkMUc+rKBCSgAAFmm0lkItfDqi2t/l2PbqSJevnfVACZTcnW+s8W0bSJ8qgmQrcVowezcXUQ= Received: from CH0PR04CA0048.namprd04.prod.outlook.com (2603:10b6:610:77::23) by SJ1PR12MB6027.namprd12.prod.outlook.com (2603:10b6:a03:48a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.14; Tue, 3 Dec 2024 09:01:45 +0000 Received: from CH1PEPF0000A345.namprd04.prod.outlook.com (2603:10b6:610:77:cafe::54) by CH0PR04CA0048.outlook.office365.com (2603:10b6:610:77::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.19 via Frontend Transport; Tue, 3 Dec 2024 09:01:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CH1PEPF0000A345.mail.protection.outlook.com (10.167.244.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:45 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:40 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , , Alexey Makhalov , "Juergen Gross" , Boris Ostrovsky Subject: [PATCH v15 10/13] tsc: Upgrade TSC clocksource rating Date: Tue, 3 Dec 2024 14:30:42 +0530 Message-ID: <20241203090045.942078-11-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A345:EE_|SJ1PR12MB6027:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a8a9058-e891-4475-1221-08dd13791ce5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: bK6Gjc8GAnVjD3q+olOuIqMRlIHQBQ2I99Q3/YntczQJVzHm11iEMd8et8nRniOOmH1JsJzICeTvazkIWnfKRiMZnzlsyv535psEFuufCERoW0y2IMSlsALJ7MEb/PKo5x8NzD3e3yINVHjDdXwH8RjsDbDyTEw0JOcE5LnhFE8MFDU3fn6El7Y6thEFBrcio2rI0aI87Czrl3HEJrFCpVbutt8lznJwnDeHbzrfZOzCc1e5J7ThJTpBqQ7Mc9atjzSD2o3unY+ifz2nJnZyNpgRcNNmJEnppXrmOYzZp8BdScrENgjrnsjerIjgkwvDNlpLFxqm74hyn62OCPmwCrNetvOXNluNZez3UigEOxvZ6Xv9aUVpp9Lf/r/fc5rr7oJETF0m3S/a/zXoGhUPQ39HDuABaKYcHtZqSgQeApNGNrQBjJa98DEjr3vom88cDvZ0Xu8jrwoDzLOgSLmaoiv/nUA9lZ/Xf8fbRUF3nJjo3gHEah4G+IBUJ8ytuEq6OEj3Zn/bEzrFT6Mt/OSx3Kh/5T4Y+gifxRfvnIbxxePIiixgIF12dO5uezCPyRrMlghSvG93CStEd0CVmmux4bnSbQI2x5Zf6BlUOba2Ybk97sYS6g+ZUoX5ahDt0cJQLOhLAT8b1RnzjH5S7Eipqzn0/DmnNw+iDp5/Ln4DSiJBwrJSqy5eAw9ixgUVKe3F3cV4XUQCBns4PyTasKSjvn5fos6rQPLXzgIavYgTPLg1MIYorRgpBtz7kwxWKRplvb15vU0FcvrQjkML5qAbrhyJA38pLppaqx1ltlkcEM2o1gaVlQfCMwvhfS1OtC3nXyinzSYVsi0Td7eEmGuXUGXBuqO4cYujKGrlT85lGLmQ1T6DMchTJk3fLGNMVTymIRGjKfEMLD+wSAy5GNGs2BNPUZyw0ClAyfvOvfESVGqRdJvuoTx0G589rawM8VvXeMwcyKlWFn/06vHo6F4HslxIGWMbZPzheFV/ogZdtprsWS0/5SetVJ6WEkqlnlK9zJeG90ycrfro5CTeA1fU0kybj+V904VAlnxzt4ZKGsID+JS0xkGJ8skKYehPyLHebdOIIKISyFR+GmKf9mp+FZn6MMugO+vs/oyP3vN8M6u4myUKaXYfkpBmcSAEo1Uxj9KLb/YwdSDBTNFkj0LXuDfQTxQ3ov3Vv1SxE44Rj9kVlYbxJjySJXr9VmGnFBmhHEr3EoX9ulCuHWrOtngtKd6iJ5CO4/NcYXKkA52TwmQ1OqxDwA+V6yg0PX9JjmZbEhF//xnCBESw5Jhtk4kD3ga6ODbs9AujoKKNOOeqcrl0WozxtCqYkzxHX2ENXFkgNZA0CZvaIgyvZcb+bui5Jd39A0rB+s9oXpXpjEomWHuKOF63OZDBIEPf6VdY6Co9wU2PvEXeGYtdU/sPIR1ip5E3d9QqmB1ju+6YNpbLElkZ/FxOLMiMcQQaSaKwrita 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:(13230040)(82310400026)(36860700013)(376014)(7416014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:45.2754 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1a8a9058-e891-4475-1221-08dd13791ce5 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: CH1PEPF0000A345.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6027 In virtualized environments running on modern CPUs, the underlying platforms guarantees to have a stable, always running TSC, i.e. that the TSC is a superior timesource as compared to other clock sources (such as kvmclock, HPET, ACPI timer, APIC, etc.). Upgrade the rating of the early and regular clock source to prefer TSC over other clock sources when TSC is invariant, non-stop and stable. Cc: Alexey Makhalov Cc: Juergen Gross Cc: Boris Ostrovsky Suggested-by: Thomas Gleixner Signed-off-by: Nikunj A Dadhania --- arch/x86/kernel/tsc.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index c0eef924b84e..900edcde0c9e 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -1265,6 +1265,21 @@ static void __init check_system_tsc_reliable(void) tsc_disable_clocksource_watchdog(); } +static void __init upgrade_clock_rating(struct clocksource *tsc_early, + struct clocksource *tsc) +{ + /* + * Upgrade the clock rating for TSC early and regular clocksource when + * the underlying platform provides non-stop, invaraint and stable TSC. + */ + if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && + boot_cpu_has(X86_FEATURE_NONSTOP_TSC) && + !tsc_unstable) { + tsc_early->rating = 449; + tsc->rating = 450; + } +} + /* * Make an educated guess if the TSC is trustworthy and synchronized * over all CPUs. @@ -1566,6 +1581,8 @@ void __init tsc_init(void) if (tsc_clocksource_reliable || no_tsc_watchdog) tsc_disable_clocksource_watchdog(); + upgrade_clock_rating(&clocksource_tsc_early, &clocksource_tsc); + clocksource_register_khz(&clocksource_tsc_early, tsc_khz); detect_art(); } From patchwork Tue Dec 3 09:00:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891964 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2085.outbound.protection.outlook.com [40.107.102.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7349B1DE3B9; Tue, 3 Dec 2024 09:01:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216515; cv=fail; b=Yl37pZYFWug6ZMQyBJJDCZk5dCnjyMf0vlKfXGsaUFQ5mN0HCU29yeixksNneGduFs/FYJeQMMSKdfpEvzqneIa/wsdzi0B8WIs63L9Kd07gXF+mNxcnge1t3fwxAVXZTMSUY16RsHQ8LanxYeq7WFQqARApXI6Rt5fFn2mlsF0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216515; c=relaxed/simple; bh=Tz6CZU9AlSBuu4PVH1vfwh24LbRJxkEZIvcaSBZcDkA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TKX/CP9aqZ+zz0R5FL6g75aGn2D0WIdcwQBzA7PYnaUdNoVsH1crZmE+XRGOTeeFNeqMkCbD1k94Vh1HvXTtahH0EoH/Ylro6vE7apUWaxh+AW3rHyGRLttTpmgPGAWJ7CkG7a/+A9dzIJlO8VetTkShCDUXfYLqJsLEpWzh6O0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=x2+TXyV1; arc=fail smtp.client-ip=40.107.102.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="x2+TXyV1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=BDBKZpxazekaGrZYrVHHuvvdmSFVZ0llhJnA9LmV8Z00bGpbjcKuqCJVXZhwcd6Wg+BGqq0myGu+zZk0pD3z7PYytZmVw8N8We15uM8Udl0nNZEe6GHvQa55kCVfxPfVWQthBAHFQxOUKepSbSlr4bIF6bAdwuVDqhFgDzJ9IiXihaq5KVJCYbQRw5VtluCDW1WFFj8YiURvY4/KqYdmeoaIDTzyafQDqls1nBxIXwvOokmtfTa40nWPu8VbfVwgv4qv9b7uyJ0YmpM29iHZvRsLZNhZL4y8Ois68LejSvG3LcuTLolFyrGzizVDmQtFM3roD76l8S19RIDHymJUyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=ANGx2jD0891ExjocqhNzetd//WRjNa2RbPQ36Hour8Q=; b=MrF/Br3Z2Cv/hdSJCbPAvIhNPjwJw/BO901CMK9tgHNBv59hwoFbYGdrMDoP21qypyqLieJkjDhpFdG5nV++pZEbXA6NlxKZD1tCWl6fTyDUsqDTxJZIeojXZmtT2QW3yoSgi83/KevA2AdB/QKAPBvTJeTBlz77sUjBIX6MmbTcNAUBfsfV9htl1bM6WGPRM6ie24b+/XNifY41SwhGRSV24e6mQlEnVGdBj3FIfrieqfObC5MZxG6BtuPJQOtkTXXlLQd6HP6x1pmNk+k+BN6quCFoBRRQKmtLB9CR8aOc2U+vC0eCph0VuZztWJFpfrxxoy6JvGWdyGtYanhVVw== 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 (0) 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=ANGx2jD0891ExjocqhNzetd//WRjNa2RbPQ36Hour8Q=; b=x2+TXyV1r5ALC71F5e66pAa0s8V0auwttgx4LqFPJP+05JRQtUjCSL2gKeVHJ3VGYUDtAgz/Bh4v+IECban+vLyasXj7CPhO+Og9TvKcG7zGF9ykLe4VqtQkuZaZOM/FjGBZ2mZznZOl9uoC4JJbevAZot4X0tiARWOFLEU7H8I= Received: from CH2PR16CA0016.namprd16.prod.outlook.com (2603:10b6:610:50::26) by DM4PR12MB6088.namprd12.prod.outlook.com (2603:10b6:8:af::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.17; Tue, 3 Dec 2024 09:01:49 +0000 Received: from CH1PEPF0000A34C.namprd04.prod.outlook.com (2603:10b6:610:50:cafe::cd) by CH2PR16CA0016.outlook.office365.com (2603:10b6:610:50::26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01: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 CH1PEPF0000A34C.mail.protection.outlook.com (10.167.244.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01: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.2507.39; Tue, 3 Dec 2024 03:01:45 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , , Alexey Makhalov , "Juergen Gross" , Boris Ostrovsky Subject: [PATCH v15 11/13] tsc: Switch to native sched clock Date: Tue, 3 Dec 2024 14:30:43 +0530 Message-ID: <20241203090045.942078-12-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A34C:EE_|DM4PR12MB6088:EE_ X-MS-Office365-Filtering-Correlation-Id: b61b4ab8-f2cc-4940-4de4-08dd13791f54 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|7416014|376014; X-Microsoft-Antispam-Message-Info: FhzQ8bkNPBBSoBzSxegdaxsWqKs63GNzY9ZeNAtJ3ibQMD8+l9gz2YzQoxRcM4EbrCzspYGpyLAO4/rbpqxWb4a/OODkAH3/piFYQYcX3fgZG4S8WCDOSfoEBcP+pY5rWBvs9S/9IvYccXR7IptFtJpmEF5lzi8EyiJm6JcVjJsEdCdMUa8GnUFIVLgI+TbbJqsBrR5rWjOTObhHLHGeY+UO8CdAaCu9GlJBf6Eqww2/YXq2UQKxbIz4v5L6sTPQYsovv7TdSJqzxo5Mz/H1nLbhOQiuWWlP95tuWGhYsIilPLQP0Az2hA/GX5jn+zlolW5SfsOJfWgsgTGy2gyGGU53Zd2vqUvlPlsjZXyqroTHHs2kYXTWQhae9Dh6cyu/0/qzCq+oGr12F1j/ZodfpSQnd6+0H0v7B9Fi3vmbd/G38kyIybOaD3BeDMPO6H4NRVWdJhC/zC4Ora2wO7/WQqjroGS6M7UUCaQ3oZ7E6QZVyx94/60SpRqmmqfSy6wniP6OxRrlevQOVYiF5D8oeEbvdJE4GxhYenAoGR9toBIuaCvDSdnuYv0cCxz5/+UtTmxpA/X4YT5Dtn11bTmpA/TlkNhxsbiRRZcxpc9SgVv9uJonmKP3voJTJ5pTaDDTTDoSUuAruvT5H0rMXxgI/RISMFdCYWfYfPlXwCaQ7z87lG0++7eRTUDSTJuz2dqd73PW/5IFPNotTT2D+tRchskF7ptiH/iZEsNmglyb2TB+Oxz2mdEZt6VrNl2cc6Cg85/DfbpMAIheLhHIDCLrgK89v1yNv/WgUp64P0o4LyWBPiKSzCcvRIxHzMm3I5Y9R6FdsUrp1y06tB24XMWieLPYklQ3u2/hBKFjeT/EqYF66V6IcmIgsHJuIzQwktG5zVa8G7x8LLzd57pOAO42R9CAiKWcMg74MGxJ14W1WhXmowtjCOdpEuM5JPXOuGFJND8QC9EkeQFLok2tqTK22scG037DwjS+Qow/oPiI2KLWhqV2RncVX/c3nNGT4KvFv/QsI6mdv/U78OwLtHSFFlOJZfBtIm7rmRWyCB+xTPC/71h2F345AraZ5C1CX90Pt+Qd63pOP9Dj9i9pipeBgkGnWVc4Gvvb5kODWKTmcbKNAYgZ6wg89xYPqWo338SLzJTVvSqSiqLV7pM/kSrlJacCpae/LA/ZtQXsEk2N+g9EfKVdGuzPIDocpQGR2SPSlekyGqTWTzkRsgtYSXDd2Cmc7/FgBF9xYq6GWpeBZ2zs8qzNUxMPQVV5yo0gNw6rZOOAGzL80MqKMpiN/NhOB80krg8ivrP3eGQ12pLnRiWWo+X9/eUZEcXZ/hife8dKZig23IGfsccx/F+ig4oUKUjh8qShDu31z4mYBniWCva5EgRS02VQrdNhwImsJBLdrqB7JQtEQzGA4HxwXgUoH5WcgOMdJ8abNIMCmP0isxSb5VjrouaAadp30zcbx+nR 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:(13230040)(36860700013)(1800799024)(82310400026)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:49.3442 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b61b4ab8-f2cc-4940-4de4-08dd13791f54 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: CH1PEPF0000A34C.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6088 Although the kernel switches over to stable TSC clocksource instead of PV clocksource, the scheduler still keeps on using PV clocks as the sched clock source. This is because KVM, Xen and VMWare, switch the paravirt sched clock handler in their init routines. HyperV is the only PV clock source that checks if the platform provides an invariant TSC and does not switch to PV sched clock. When switching back to stable TSC, restore the scheduler clock to native_sched_clock(). As the clock selection happens in the stop machine context, schedule delayed work to update the static_call() Cc: Alexey Makhalov Cc: Juergen Gross Cc: Boris Ostrovsky Signed-off-by: Nikunj A Dadhania --- arch/x86/kernel/tsc.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 900edcde0c9e..a337c7e0b26e 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -273,10 +273,25 @@ bool using_native_sched_clock(void) { return static_call_query(pv_sched_clock) == native_sched_clock; } + +static void enable_native_sc_work(struct work_struct *work) +{ + pr_info("using native sched clock\n"); + paravirt_set_sched_clock(native_sched_clock); +} +static DECLARE_DELAYED_WORK(enable_native_sc, enable_native_sc_work); + +static void enable_native_sched_clock(void) +{ + if (!using_native_sched_clock()) + schedule_delayed_work(&enable_native_sc, 0); +} #else u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock"))); bool using_native_sched_clock(void) { return true; } + +static void enable_native_sched_clock(void) { } #endif notrace u64 sched_clock(void) @@ -1158,6 +1173,10 @@ static void tsc_cs_tick_stable(struct clocksource *cs) static int tsc_cs_enable(struct clocksource *cs) { vclocks_set_used(VDSO_CLOCKMODE_TSC); + + /* Restore native_sched_clock() when switching to TSC */ + enable_native_sched_clock(); + return 0; } From patchwork Tue Dec 3 09:00:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891965 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2059.outbound.protection.outlook.com [40.107.243.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 728D51DE3C9; Tue, 3 Dec 2024 09:01:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216519; cv=fail; b=ppeAm20PwImINMyCuVRs/IKhLNFjFENi0yxNPp9exxgP5OwHuvg4fendtDQOsFfmjalxdUG0xTmNf6D58W3XOb506E4GajgyDBBtAItSrk2IlXWV0905wZQeNF0+x4KObnnyq6u2czsq+YX1auBeENWlx2HsJVCLgdqNyfy32k0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216519; c=relaxed/simple; bh=4a2XzODFuQVyOytOE8rsg5rO3D9l8qwZh8Vb+pmqUpY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EoAlkXA92JETobTO7w9LZRsUmnKT3FIe/atJ1dX8lLXrzvPZFwK00UFCMZwB/9uQda5asFwBsy8w0Qu3a9jJ68J50WyelVe7i9x/P3JdaH2XtncW+eoy7es6VrRHgjg3Y3f4033+7Zzln7y38HiCy3DhlN9o2YNG67C/6I6AInE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=RnRlB27J; arc=fail smtp.client-ip=40.107.243.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="RnRlB27J" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xi3FRUz3UDRuuA5F5J4Rqgrq03kRUvmtjZBf+oPDvhI3/oCDxdXFXu7v/EtwAUWy+lI4qKvehkfr+IHkzGjvADc8TQIsVzE7QMKWPDce3zcrDtZ6yLQl/TuSfQW0+v8RwcH25D+x3Kq84FBWO/RqNKqZWuXW4mdh/dPyi8YcTwFtXnxgNXM18CtgbtnjbZsbc0/GsxOwQVpdTJxYj0YlICJsh6k+nTYPOTA0Qnk1/ejXI2a5vt/EpGkHMwW1PkK9QFXXQKfzpDjHx/vyO4+gxw9yDTP3ANu9YdTaWke/aWmHGQvjyU3xVyig8mmierK24aSkL8Fq3XEVxKg7gmxhZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=y08IGbt4QaGBoYCCEYQTD2cNqgyyVQ7RxEiJnvhiAcg=; b=T1y/8PlnyE1EdnF9cZ92uXnexVdiDzwdQTHp6MF1fUjLmxbK9J2a4Jncgat6DodeK0ZXjn2ID6pUa3WrC17cijsuaqMrEydyzRwGVbicoq9ACsw4qNp4j2qwAHusMQi3dgDVWH7xx9cmvi+fUzGcR5ItNEKAsGevCko62cWdjVHLAQ9VN2JpaporFgsUrnzig3q41G/dVmJtHw2VJ6YczFGMVgH7Or0DNU5pXrPtga0f3BJhEbapWInAbnJxi1WIMitpgconZNzUG/IcHANCm2p6sIqL/HhYHn5hzIYy2cLNNi5uV5IZ+JwQpD5chvLiqzEVIGpoLRu+fNQgCMiEjA== 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 (0) 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=y08IGbt4QaGBoYCCEYQTD2cNqgyyVQ7RxEiJnvhiAcg=; b=RnRlB27JE1riCbcT3R9fr+/CtFZL40Z0jjH8VWSzVzhTKlsrxe0FeiHs+46LltTrJZ6Sb8AJGZLk3bXblVV0dnXeRo8dHxVPlitPwvv4ERQy2VxTelNUgQG0TVpMqK3eAcE+G0RIz2Z3qPU1eQOiO7vUEba3h0laqkyqQbhQ2Kc= Received: from CH0PR04CA0033.namprd04.prod.outlook.com (2603:10b6:610:77::8) by PH7PR12MB7985.namprd12.prod.outlook.com (2603:10b6:510:27b::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.18; Tue, 3 Dec 2024 09:01:53 +0000 Received: from CH1PEPF0000A345.namprd04.prod.outlook.com (2603:10b6:610:77:cafe::73) by CH0PR04CA0033.outlook.office365.com (2603:10b6:610:77::8) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.19 via Frontend Transport; Tue, 3 Dec 2024 09:01:53 +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 CH1PEPF0000A345.mail.protection.outlook.com (10.167.244.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01:53 +0000 Received: from gomati.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 3 Dec 2024 03:01:49 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 12/13] x86/kvmclock: Abort SecureTSC enabled guest when kvmclock is selected Date: Tue, 3 Dec 2024 14:30:44 +0530 Message-ID: <20241203090045.942078-13-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A345:EE_|PH7PR12MB7985:EE_ X-MS-Office365-Filtering-Correlation-Id: 49316b85-5b3b-4bdd-b391-08dd13792196 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: oeHuFinbAzP5fd01M5nG6AxjIoiW7m5JjyA6MR0MeMJKiFhnpME/Lsg69By77gwmggUIBm954pi/lFp8paVo7bolv5gpT5c8SjbCrbQ83SkcSlvQKwPAWjt+OeFHrye2r2PWpLzs87ddAqrg03MZZ/CsHAPC4B1RWtvHUgliUwq5ADEyiw52up09TCPhSfOtP3M5Ekj7QZoJ9zMs18vHqgAuxJH71g6rUahBvyPmTrRqtU8QGh9ZeWxtorrmrgaK59U4Ko2optar4YCV6lS8KIttLdYFbmRUJ8riUzaBC2TiZToa7XDVoJAyuPCMkacw7ULVDrPsgKI6VBKunoOdI+IIx4qKO/aKBs6377CIFjKVBymQyGu3/s5DhrVwIwDObqWfUYSHvvph+SKL1mVprsUIq+CyAO90FyaO18tMcdMFy4jW9OkT4SouaZJez111P/2NtbUZA1So6u0s0uJCpZNeXN5+wJnjZohcv0QptRqAoc9oqgY0yxE7d0jT25Ldr4Ucf8s1ov/BMjSCE3lZsqWzs2t17Y/vEZloOVuWBMbRzOlAs1AQ/qZAF7lrJQr5OKtkRtxRzX9kD8YgE97YYD9GS63bw3ad2k2/PxGbEDIT5hGIUJGllIXqfHuB/6OlRAX48589sKv2gOWVFhYEfG430Jz+gvCbKlC6xN2VZxhg/02tKp2bDhJlu3iMICwnghpAZl36YODdg5w3WC/2HiiK24ijK59aKqWA2FnLt49OAnwesjQFSIIavPAX6X6/73eQG4Jz/YdjAjqPFUGCFsoMfBE/mSpmTR9seVowM2Mb4iYagrAyBLtsqRYPooyEDegv3XxYpfsfdCv5su73aESkT7ouIBvEFzlxjwRs+O+T8zkGKS8YcsEZr8mv4m29lA3KQQUGgG/HEm1bzgdPuPEDAX4OT7yZjLKC0hf7fwIpn8W9BzqX6S05oUrSmfu1RyH30qWDgDOEUR8qd7TyJndrkXQPxH9ZFSp4w46F/K1mY1vs4oDZMie2Pa7t5+aLs3TeLhJ8uYn9F+EuTy157ZJ6Yuw0CSg7C/pm1HOFMVMT2oEYiEcWvA+FPNIxoDCns2zERmd9oFNf/3zSwbyqn9SFPXH1AUwGkXU45ZKqSDfbb23qb+K6dTCJDWJkLvgW+oA7foHpoTYh5NJFC7/LmRarPdzf2rQh8el0rRolps968Ukz/JZBCK8mVxyf7A6If0P/oTz6hHaAMVdLvC8yM5IC+n83VsnMy/ZjgRCBi86i5uI5f31qkxXrMoxrMs52909jx2LXonWIOMeSHnbr/xERVawkAkJelbg9c9bHFwLdmzg+rJiJQ1Et99ReDjbgtbYG2ACC46UNNgzU+lMp4O/9EaPWqHeA0uu26wnLYcOUDeV2dv1sfVwIApIqr8sDGAKJjv8lsOi8zCIteZGQMuesJhwly60JzoxsQ6rrvTk31diwtuCmQxCqJkm7y0No 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:(13230040)(36860700013)(1800799024)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:53.1192 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 49316b85-5b3b-4bdd-b391-08dd13792196 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: CH1PEPF0000A345.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7985 SecureTSC enabled guests should use TSC as the only clock source, terminate the guest with appropriate code when clock source switches to hypervisor controlled kvmclock. Signed-off-by: Nikunj A Dadhania --- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 2 ++ arch/x86/coco/sev/shared.c | 3 +-- arch/x86/kernel/kvmclock.c | 9 +++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 6ef92432a5ce..ad0743800b0e 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -207,6 +207,7 @@ struct snp_psc_desc { #define GHCB_TERM_SVSM_VMPL0 8 /* SVSM is present but has set VMPL to 0 */ #define GHCB_TERM_SVSM_CAA 9 /* SVSM is present but CAA is not page aligned */ #define GHCB_TERM_SECURE_TSC 10 /* Secure TSC initialization failed */ +#define GHCB_TERM_SECURE_TSC_KVMCLOCK 11 /* KVM clock selected instead of Secure TSC */ #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index c4dca06b3b01..12b167fd6475 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -494,6 +494,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req void __init snp_secure_tsc_prepare(void); void __init snp_secure_tsc_init(void); +void __noreturn sev_es_terminate(unsigned int set, unsigned int reason); #else /* !CONFIG_AMD_MEM_ENCRYPT */ @@ -538,6 +539,7 @@ static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_ struct snp_guest_request_ioctl *rio) { return -ENODEV; } static inline void __init snp_secure_tsc_prepare(void) { } static inline void __init snp_secure_tsc_init(void) { } +static inline void sev_es_terminate(unsigned int set, unsigned int reason) { } #endif /* CONFIG_AMD_MEM_ENCRYPT */ diff --git a/arch/x86/coco/sev/shared.c b/arch/x86/coco/sev/shared.c index 879ab48b705c..840149556241 100644 --- a/arch/x86/coco/sev/shared.c +++ b/arch/x86/coco/sev/shared.c @@ -117,8 +117,7 @@ static bool __init sev_es_check_cpu_features(void) return true; } -static void __head __noreturn -sev_es_terminate(unsigned int set, unsigned int reason) +void __head __noreturn sev_es_terminate(unsigned int set, unsigned int reason) { u64 val = GHCB_MSR_TERM_REQ; diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 5b2c15214a6b..39dda04b5ba0 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -21,6 +21,7 @@ #include #include #include +#include static int kvmclock __initdata = 1; static int kvmclock_vsyscall __initdata = 1; @@ -150,6 +151,14 @@ bool kvm_check_and_clear_guest_paused(void) static int kvm_cs_enable(struct clocksource *cs) { + /* + * For a guest with SecureTSC enabled, the TSC should be the only clock + * source. Abort the guest when kvmclock is selected as the clock + * source. + */ + if (WARN_ON(cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC_KVMCLOCK); + vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK); return 0; } From patchwork Tue Dec 3 09:00:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13891966 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2077.outbound.protection.outlook.com [40.107.236.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E91CD1E1C1B; Tue, 3 Dec 2024 09:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216523; cv=fail; b=GeIX7QDkIYsh9gUAjhY9MA/zRhB2f7ghU2IXrwu/PhyY8GKjO5KsnUnknenFkcL7sl905xT8SKmX/P4m8dDxw1a6FhjrW/8s7MP5i2U9jnHY82aIysn8cCaoaUxllM4ToyFavq0W21OzRT5PKgCeIEd0sL82TsDt4ATl1Hd+i+o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733216523; c=relaxed/simple; bh=J7ITyZv4fTZN+ZfiudI2wlQZ6XlSSdAR/6O3kPxxatE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CrQGNwriHtpuE2L7+dMhjiHUsbjqMgEOgB9wzf4oOA/Cs0M1YgVC3k1xrrdgIU0D3cVD4Eadxpv5yGrB7hT16FY8bJ4md6yzzq1MFtPFMX7JbdsbRcO5CzzmpTGfudlR63TRHTDyeWJq7pvwHNP0I34nHGNd+hMTLrcXXJwvJ38= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=pXs58p5h; arc=fail smtp.client-ip=40.107.236.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="pXs58p5h" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=btZZ+1d5f7VklRyBBK1XydR76IZhZqBBGArUuItPIrHRD5UGtNVc7/nxex356HMGTMZBpw5TBGwji0oyQtXTt6GpiBAfWTH2PAKTOvxY/NJdnKza0MvjjCdR87SCacfv/TldddFnQFZ9QcJON6tSb/EOVSZfhS98Pvl0xhc5xt9chy0g8cCLL8h1BlftASr4vOyYpZrhCRTYFbQTipfzYSDnTTZyk/t4onlgbNuVBb4j3uVzdByp9jj0a2LLxNnUqEHWJNssGnw58oUd0Ps9X4cvhb+coLITMCEalOtVQERyhzFjXdFbKmh8v+0XWitG473jjtshxhqHDR8KTEK2Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=atjBLt16IpratFsJbg/6ZonMoxx3kjKCKv65wzEaGEI=; b=I/kQsgACSeHW9zqe1wGr6qh0Qtqiw61LTKX53c5lQeMspIUXaBtKjBuzEn1wB3y2P/lwLYtqQsMuquyZzwWjtcaMsfkwb4YxHHZPM3lN7rKrAIeErXr+8h7PawKK53WKkdr4Q7q1tFiiiYxkoN8YQ16nnTocLFTaPH8qOmUip7At7jLLNoIB7u4m459mi+10VfeFpiO2jUrXZ8xCFhIt5gw2PCMXprqHibnVeKM7PbuV84dvvq2UTtPVIyPL5rnszbTVy3zwB5b4zhvowcTRGyG5ljfpURyzdxdzfRj+g7eutiqT8T/Qbx4tTfbuat5QZDKtx3bmNLcrFpyhTI1zwQ== 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 (0) 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=atjBLt16IpratFsJbg/6ZonMoxx3kjKCKv65wzEaGEI=; b=pXs58p5h+hES5wfhoFKVO715+nUxxbHMUJzvcONqnuDxjdS3t9udWN2NM8D02ZQZ6Gsv/9yvQzJmZl4FUqYNuEnOZ/GIXZM/9+ME0rmcg/fTijQLzVjXTnnVHQc3BByoTS7QGjWPYpfutZWTqwPQzVS3fBvWsEGNd1rKtO0DFSE= Received: from CH2PR12CA0013.namprd12.prod.outlook.com (2603:10b6:610:57::23) by CYYPR12MB8749.namprd12.prod.outlook.com (2603:10b6:930:c6::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.19; Tue, 3 Dec 2024 09:01:56 +0000 Received: from CH1PEPF0000A348.namprd04.prod.outlook.com (2603:10b6:610:57:cafe::2e) by CH2PR12CA0013.outlook.office365.com (2603:10b6:610:57::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.18 via Frontend Transport; Tue, 3 Dec 2024 09:01: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 CH1PEPF0000A348.mail.protection.outlook.com (10.167.244.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 09:01: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.2507.39; Tue, 3 Dec 2024 03:01:52 -0600 From: Nikunj A Dadhania To: , , , , CC: , , , , , , Subject: [PATCH v15 13/13] x86/sev: Allow Secure TSC feature for SNP guests Date: Tue, 3 Dec 2024 14:30:45 +0530 Message-ID: <20241203090045.942078-14-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241203090045.942078-1-nikunj@amd.com> References: <20241203090045.942078-1-nikunj@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000A348:EE_|CYYPR12MB8749:EE_ X-MS-Office365-Filtering-Correlation-Id: fe929ffd-8079-4e7e-197a-08dd137923c9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|82310400026|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: 7j9rKRFBmVwF95DXZ1w5wClf+76RK87sM6mazqe+khsH4IshnyPJv0h/T91oWsRyj2mAbgFylCB3jLYaV9KhOgLlshgz7QtnDQ9QQdDk5LEkZ6I4Wy1WC/nkmPBO3Gw80pQjxUrXnqnSCLW2SBarL4nNQ2Fy2J86mcZp9UgLVVr0ewlkkA7dlEm4pO9tMlGvpZ7kk+PB+EcFxMX6mIQi7zEIYEalIGX6DQ7zKTjd/72mcy6PHE/LfWdmHEu1R45+k5q1EZ4qd0sfpaXVWigdLgfWzDxfOVTVe/5laK6GoYxKNL2T54688rH09iQAxrLYc83dQqOESg6jL0UnxS44ZdDPd+Ksa2y9kY0tTbk+K2SwNegkTMDSMeN0G/ZFQON3f14RIo1LYd6/+QaM4Ra2W1qd8hwPOjCUO127FN7vOJQaFORsFFKuXLq118hrIYM5U2VAzxO9HrQ+cckuW6KowevTworyvOtMzCYXn3aPkZCiVV2TBCYKndugewGA75iv1dR/DtnSP3vp3ybFqQ9yHZfvEF18IAfZP4ts98NNDm+g+5lXvdma5i6WRiymUOG0Ci93bTuRW/82vQ/blmP72cuXocJPSloe761A4hrpdCPQ0zZJ0nLRWIsisQh9npoLr/4vmmlke7o5V7IZ4j6vHFUPkg5VkXwnVBUjnBf+IvPb1mXN4tF3VRvUTWEgWlhb36bvPwXdigwnJu4sI2Ri0h+/YkXPbDbPC89PDiZiWM0xkbMDWebFCKWa18/aN5euOh/sW3JITbKdY1/RGLD1bB9Gr10dGZzZe0swGT2T1iOkQhG+UexDJQObDCqGv2puJS1gyDx/+qifWEfV3lCd+QJcRVBWBKNDjeFT43TpMKO0uFm+FikkidAz6tVPoqLYiEIrYIarSz3G5/paDbIpiqWejGRvhijsgXYAiaWJnPvs86tbJPh3eC8eX1MNAT4g7q2kaVPtu0n081lDkNYwg00PPeRZQKO9Olcj6cn4xBzvBpfeyNwYFLgz6yqx7kCFRmDoeKFayKAxsi7in9RvtTAiMSkbAgqD5mPnkBhkQHpVu5ZBujMFKGQma9esqH5cVeNFjIchr8McjnOAk9T6YTdXEdTC5qMn0JgkfoLzw65qCSYx4JRSM/x/p/Et/LkVWv6lERoXkrZLRsyJXTXfSJbkYf+q516+lOWEO3DlJmJqiVI6OwvuIRg48hnn0UTNKd3vTP1L4YbhHL1dc/l29iha9UeSUVcGiCiRN7ncJnt04QrI9XR3yFS8W1Ou3jHL81g81lVL+1u+ed21JZHoIBPsPzkaERMWqp8y2mkeN3egPXRiaJNVDqs14MT/Lrb8IkdZTJpl2jRSkzFKCdvWygPD9tHG2gAgRQ//NGiuzmJS2lw0uvKIeSs0HlBfr7rrQQeDLMsfvcEjVfX5SFPD/bph8RmqAGDowmhkuZKBLdQrrIQKV1FMpaYG/J7BU8d3 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:(13230040)(7416014)(376014)(82310400026)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 09:01:56.7893 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fe929ffd-8079-4e7e-197a-08dd137923c9 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: CH1PEPF0000A348.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8749 Now that all the required plumbing is done for enabling SNP Secure TSC feature, add Secure TSC to SNP features present list. Signed-off-by: Nikunj A Dadhania Tested-by: Peter Gonda Reviewed-by: Tom Lendacky --- arch/x86/boot/compressed/sev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index cd44e120fe53..bb55934c1cee 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -401,7 +401,8 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) * 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 MSR_AMD64_SNP_DEBUG_SWAP +#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_DEBUG_SWAP | \ + MSR_AMD64_SNP_SECURE_TSC) u64 snp_get_unsupported_features(u64 status) {