From patchwork Mon Jan 6 12:46:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nikunj A. Dadhania" X-Patchwork-Id: 13927387 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2050.outbound.protection.outlook.com [40.107.244.50]) (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 080BF1EA90; Mon, 6 Jan 2025 12:47:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736167625; cv=fail; b=F7hTmJm7wIUc1AQrvwIHJf4ikGm9JeKpm90uJkrfwGLS23vtyzFlooixDCehLcJQZna7j91NzDJ95zDH+s1OcKmZcblz185C9P8Dsj7YzvWl+aItJl5UCH7Tp9PX5M9k8REdkDxJ0JfhKu5yILTTOwR22CQjU8wBse6rXHTZwvA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736167625; c=relaxed/simple; bh=z1x6jgOZT8hU761fzzQ4KDU3eN3rnyG2K8DeedNrpqo=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=VzchyjPmxfo3WssuJtBYLqda0wEbh4bKwp4DO2nni5mj1UhweClCd+uvq22wCFQDK1KUGXGFrtllIpTPAS94rAAv0eVJ2cPtygxYePyQ9z6oQJMzoKHN35ko1tZ7HlbHXjDhxnj44flz7kGF10r0B8U5JfKaMPR4kiOdhaNmV7Y= 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=uowM9MTx; arc=fail smtp.client-ip=40.107.244.50 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="uowM9MTx" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Qr6N5jyHHqeS+3DH7HNx880eNoGh0SKizntg6dO0he3017EGjhiI/3rCs5cP/1v5UGTucJ21dddXh1sV7fHnfNdp3NHAagu2VPjnjgjjNtyLGSNsyCSaQOLizEzMuJB8yACSk7h9w0a/UWRr8Jhjn8vJUXBuqqgvT4gilOoycQqnvrIv4TZ8qBY+2hhOfdXxkxftxXTqTZxU9Sy/bDwD3DY+911X+Ordm9Bse/BT6V7Y4RX0EwJiDuK8VwYn5SPh4P01H7D3Nc09r6lP8KGCsXgu8p9bx8tJRgR0e7ca5KG46sjxAiuHG7rKwfUSGw4z3J7HqSSvtkWhpRKARC3Rmw== 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=FRM2IJVw/3C+Kvw9ajfQSWI3P1MhfXtRRcb5rhgs5KQ=; b=n6lwnTFZ/XQ1EJjAAhP2eNPM0sI+MkhKq4EoFH3v6uzTYnu991mHM0w1KPD6WAMEf1g8VWNUf2lBH7zOFBn7HjRY4kMEx5x4XKIhZJX6WYD6BSuxz7HrkDYCHbjWst6K+Z6P2Z/jNTCVpS8/jzUWq8GYIDAQ3RBNq2rATltk4ZEte39A5W429SK+5M8xQvtgVGHbsH5Hwds5PhR1OEEP0QppyaJZ0EdcrXjEx21fi3udkcNonb4bXRy0LBPwEUpkTbLfEIxkzuHqI+CQxqe5Uyvhirh+aN7YUTlyYPfBo7HllpIqtWZhCeT4GuiM8HHKiCAde/E0czyVRjNSopJCxQ== 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=FRM2IJVw/3C+Kvw9ajfQSWI3P1MhfXtRRcb5rhgs5KQ=; b=uowM9MTxLuz0tM4wm/Ijvn45Kzg4QVeDgAgqUWPkKdm7jp1xqcwLx0QScDHLrvFc7FKbL/OnRqULDoUP4F8upRZFgWsoMRZAi/sN74df14CoxQD74DLPyB/Q4zwdy/jY71s1tB4A6gHqeUq+du9ewH2nwGEWw4GytnsXZlWPRHo= Received: from CY5PR22CA0009.namprd22.prod.outlook.com (2603:10b6:930:16::20) by SN7PR12MB7953.namprd12.prod.outlook.com (2603:10b6:806:345::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8314.17; Mon, 6 Jan 2025 12:46:55 +0000 Received: from CY4PEPF0000EDD3.namprd03.prod.outlook.com (2603:10b6:930:16:cafe::1f) by CY5PR22CA0009.outlook.office365.com (2603:10b6:930:16::20) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8314.17 via Frontend Transport; Mon, 6 Jan 2025 12:46:54 +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 CY4PEPF0000EDD3.mail.protection.outlook.com (10.167.241.199) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8335.7 via Frontend Transport; Mon, 6 Jan 2025 12:46:54 +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; Mon, 6 Jan 2025 06:46:49 -0600 From: Nikunj A Dadhania To: , , , CC: , , , , , , , , Subject: [PATCH v16 00/13] Add Secure TSC support for SNP guests Date: Mon, 6 Jan 2025 18:16:20 +0530 Message-ID: <20250106124633.1418972-1-nikunj@amd.com> X-Mailer: git-send-email 2.34.1 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: CY4PEPF0000EDD3:EE_|SN7PR12MB7953:EE_ X-MS-Office365-Filtering-Correlation-Id: f8fa9db3-cff5-49c4-8d69-08dd2e50332d 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: pYxqq/Bqaz1T3zsyIZUeNdRU2W7JYnXnYqb+N/KQ1GpqDCJhVx8t+8iYPNaONuQm6gSZJD+l+GMF5VYvbIUWve57qBuh33jsk5gDSd80kxWc02r1BSf2D3wHy4nJacwmEY1/vZlkMlbw0ZghgTsTUQtKexHEzxXjMmSQEUN7RayFH69poVv/Ew94PYdm4wxFeHcztZwmHTvuoOV2tgqaW5c6EcUctOwH9+/tLSefYzjyxv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8fmi6hqSTT1+DFdQjv+3SbHUpjsDN639bcx95qX3GkkWydXLSRKOTHqnXNcpbSAy4+T5N+mDscvQq8r/0XV5skJtlljoeLjkKZdeVAS+/MkD5WKOLzuIvWWnAJM2YBqc8YfVTVqBhq0I9wS5baRnan+7pGnPx0ZpQBRZ9WTPbxu/x2CDmV4RgHI4lLJbdweXOcrDAQ= 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: 06 Jan 2025 12:46:54.5644 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f8fa9db3-cff5-49c4-8d69-08dd2e50332d 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: CY4PEPF0000EDD3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7953 This patchset is also available at: https://github.com/AMDESE/linux-kvm/tree/sectsc-guest-latest and is based on tip/master Overview -------- Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC". In order to enable secure TSC, SEV-SNP guests need to send a TSC_INFO guest message before the APs are booted. Details from the TSC_INFO response will then be used to program the VMSA before the APs are brought up. See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info" SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before APs are started, move the guest messaging code from sev-guest driver to sev/core.c and provide well defined APIs to the sev-guest driver. Patches: 01-02: Prepatch 03-04: Patches moving SNP guest messaging code from SEV guest driver to SEV common code 05-10: SecureTSC enablement patches 11-12: Generic TSC improvements 15: SecureTSC enablement. Testing SecureTSC ----------------- SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series: https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest QEMU changes: https://github.com/nikunjad/qemu/tree/snp-securetsc-latest QEMU commandline SEV-SNP with SecureTSC: qemu-system-x86_64 -cpu EPYC-Milan-v2 -smp 4 \ -object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \ -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \ -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \ ... Changelog: ---------- v16: * Rebased on latest tip/master * prepatch: Remove is_vmpck_empty() and use memchr_inv() instead (Boris) * prepatch: Uses GFP_KERNEL for snp_init_crypto() (Boris) * Use memset during cleanup and fix memory leak (Boris/Francesco) * Use tsp_resp with extra allocation and get rid of buf variable (Francesco) * Drop RoBs and TBs, comment/commit message update * Add switch case for #VC Secure TSC MSRs handling (Boris) * Move CC_ATTR_GUEST_SNP_SECURE_TSC before host defines to group guest defines(Tom) * Cache the TSC frequency in khz to avoid reading MSR and multiply every time (Tom) * Drop the patch preventing FW_BUG for not running at P0 frequency (Boris) * Limit clock rating upgrade to virtualized environments as part of CONFIG_PARAVIRT (Boris) * Squash patch 9 and 12 to logically merge changes that switches to TSC from kvm-clock (Boris) v15: https://lore.kernel.org/kvm/20241203090045.942078-1-nikunj@amd.com/ * Rebased on latest tip/master * Update commits/comments (Boris) * Add snp_msg_free() to free allocated buffers (Boris) * Dynamically allocate buffers for sending TSC_INFO (Boris) * Fix the build issue at patch#1 (Boris) * Carve out tsc handling in __vc_handle_msr_tsc() (Boris) Nikunj A Dadhania (13): virt: sev-guest: Remove is_vmpck_empty() helper virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL x86/sev: Carve out and export SNP guest messaging init routines x86/sev: Relocate SNP guest messaging routines to common code x86/sev: Add Secure TSC support for SNP guests x86/sev: Change TSC MSR behavior for Secure TSC enabled guests x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC enabled guests x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests x86/sev: Mark Secure TSC as reliable clocksource x86/tsc: Switch Secure TSC guests away from kvm-clock x86/tsc: Upgrade TSC clocksource rating for guests x86/tsc: Switch to native sched clock x86/sev: Allow Secure TSC feature for SNP guests arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 48 +- arch/x86/include/asm/svm.h | 6 +- include/linux/cc_platform.h | 8 + arch/x86/boot/compressed/sev.c | 3 +- arch/x86/coco/core.c | 4 + arch/x86/coco/sev/core.c | 652 +++++++++++++++++++++++- arch/x86/coco/sev/shared.c | 10 + arch/x86/kernel/kvmclock.c | 11 + arch/x86/kernel/tsc.c | 43 ++ arch/x86/mm/mem_encrypt.c | 2 + arch/x86/mm/mem_encrypt_amd.c | 4 + drivers/virt/coco/sev-guest/sev-guest.c | 485 +----------------- arch/x86/Kconfig | 1 + drivers/virt/coco/sev-guest/Kconfig | 1 - 16 files changed, 791 insertions(+), 489 deletions(-) base-commit: af2c8596bd2e455ae350ba1585bc938ee85aa38d