From patchwork Wed Nov 27 22:55:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887413 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) (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 AC3F42036F6; Wed, 27 Nov 2024 22:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748191; cv=fail; b=HFDWkvTx5OWGtfr/iVMpdQvp3De7Vdyrlfi8lpfMjU/Yfel37OtLgCdsBtpkmh8PkLA80Jd4AHI7BWHvGR914mOm1uSanCgp4Q6yLasqeIpU5G75rjQFfwWQrgy1a+lHPCJqZyrjf3r9VBORAW/IA6f4hjxm5ikyezmr1mpiiwQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748191; c=relaxed/simple; bh=O/uDhENTpi2yI4Pfiz8Mw6hgm/uWiWDHHbqWulVsEVY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=A9Zd7WjqaFsGfHq1a/Oyu2AsL+ESQB0HCvScgzfPbHF8eb+f8bXBpOJbV9NtvRTkAMoZ/IiM8alrM/H4jKJgrC5IcOPZWYHMvfA3XHwmoZEhwJstJl3vMEQ2AIdMPNwy0wkbImYBmmU9bM5XQo01SeQhxAVEL1mHyuma0IPEGx0= 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=qHpYD1g+; arc=fail smtp.client-ip=40.107.237.60 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="qHpYD1g+" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KYkw4XkPOBzk+knHoi6cj+GIj8hMcskff7PNHZ2xmKYPPquVWrmMGnNxSyiJ49JLpPUTw9f6R7E2ZtRnYukOmVcboWPSNt/X/hyzall3mLvFh3eXF9ICCVUWHbRGsEkCdJC/evtkI12OYHlcJ3Cldps7lSvpO/WBqa/Y7Gl2vYzhm2HsonYDsXo+2FCOv3qGnWFM7AConE/KbOiNMLeC+SFwy7f39KkWRFAbgVyOBhbp9fSB/4UZYXpST1scE39dk25lx/Y9n+bzXV/UYssh7ewsdc18XZX8TUIDkkwUOyL6Y+5Or4rXo1QdITLz/g3Fda+Hf+qRwapXPXD6c3DtTg== 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=6E1RYwEXT4MapY4L7CuHPs3znYJIC+82CrH4S+JkdqA=; b=lf46gnU590FesPohAIe2UW3nGzw1TtSOzdb28nMLFtekWQsgFu097DNkIXXa2FBPuBQCpVRxoI2MTsXX9h0Msq2RK4hahV1sdlDZgOSrIUbTsYgMJDlmH1laGxn0FfPNqhNVE1fHKxF3fo9Go3qSkUVX/rvYF4dN9xQ5ULg41NHcVV29iFP+DVXerTfRToEw4hBB8Va78Qyo5lYHGudkCGFeum0FG0GggijIvjl7H4qNNG82lrzScVoJf916gE66D/HUABSkYBjCUYPaqFcIoy2jqd5on0fNKOFtkMx9xvBBhz4Z/7/NS5mviZpQbK0tElbALQk5RdgUHu4/VSHnqw== 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=6E1RYwEXT4MapY4L7CuHPs3znYJIC+82CrH4S+JkdqA=; b=qHpYD1g+2IQ11NnZOGnu6WXUjMy1Dd+aYz2+xiYXyk+1F2EpV+TOo8MFAB9d0OT9XT7aeFnzWamnNiCmc3WD8+PulbhSisy5GLkEMOFB9OMcVGIE27qJvpNQo1G/6SUCM9fOjemkAetV9EeQ5je7rYQo0hCdcyYFro5V7GssCiU= Received: from BL0PR02CA0115.namprd02.prod.outlook.com (2603:10b6:208:35::20) by SJ0PR12MB6685.namprd12.prod.outlook.com (2603:10b6:a03:478::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.13; Wed, 27 Nov 2024 22:56:24 +0000 Received: from BN2PEPF000055DC.namprd21.prod.outlook.com (2603:10b6:208:35:cafe::89) by BL0PR02CA0115.outlook.office365.com (2603:10b6:208:35::20) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8182.21 via Frontend Transport; Wed, 27 Nov 2024 22:56:24 +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 BN2PEPF000055DC.mail.protection.outlook.com (10.167.245.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:56:24 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:56:23 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 1/7] x86/sev: Define the #HV doorbell page structure Date: Wed, 27 Nov 2024 22:55:33 +0000 Message-ID: <20241127225539.5567-2-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055DC:EE_|SJ0PR12MB6685:EE_ X-MS-Office365-Filtering-Correlation-Id: dc4a332c-3b8c-4d26-0400-08dd0f36b801 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700013|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: XwosRrwvqVvjL0EEyu8FqRK3s3oQIDxRakZgimyag+X3/26UswZcbjVphb6dfMutG4DdJ4XfIXcErkEfHioNQudeLl6zpciiHkwDalB/m6t3xiBaTMbsezbFsEO1ZA1NZeqAGkuzst9Wbpnm7BoySrrNPgs9P6XgGrsLVOdTIK0IRIzfeaO550e9SfoVdEgPXxX/8c3hpparzYwwzDCk9/AezcxhjquOnA7aoTo+/gFBHhYA0wFIyaYrsBKdKPst1iqIVxd5+WOeO3nT3x+cv1fW7uhVbwkWobMz2tsr3/hm1mf7u+2Wp8l6J0bTHbXBDb+MaBN+pSqulkuLour+q0kKqV1CzeaW82JYzOCLHLMyVrmcjtKzbkx8kWFbq255okDqueRzqDELA6wFgG0RlEoq1YD+QMi3UvLlkjRo6vSccctZwf9yrcTf5Kbm+0ebJ8ki1DRf6poMCsPCU4wsuXizcIeO7GSztjIgrVjL8tlHmi7ERZh90SFv2TU7HnPAsjeKbed4MZNKUL0aAq5pzVM2V2/UU7xWDHyy/vp9CwW8Sx6Gl1cGbWYV3ziwUNC6mUzcQJk4f5q1/wh5VwDGabH89xiPUVXreoU1lNwNZwJYCOj91umv4rpu4UyleeD2JM6tRo1LyUgKp6YpRF9SIyQZXN1cwzY5DH24+EP25lOSbIBwvlBNdNfdDsxoxrEiggba/hIuC2ec4EFu2RmNSRWYFdJih4yUY/qcbcD1py7ZUlh7iF7BDo2zwjC3GMEYGlWbyO0oGxe4bKKHgXs0JvQkC1bJbUuHkFTv6vjk3on6Ubyu/6N8EP9wOKQlPYC+AK3tit3rdr5FCP3nzqnRE08086ZVCjkoac0JpueLK5BAbnAGB1xs1BkLipJlfF0S6zuh5QAi2WB8/iixrkq3TfIzArSIys02FE82sJJBzPHn0NmWcFq/Z6Y4xBQoxkhcVLavyvh413ucITvxkJSPRCMtVnZ0fBKdfN/zfi4Qd4RA8mUHroQ7f+4LhzX/ml6bewrqZCqRAaBKMxKg+/BjpwPxBt685+vSBsS1vgzju1HJ3sPbw//BvR6Jnm/Dgs8kGp9GTa5MrogYR47G/EaDOmI0KGshSpQmiqnwxoFj2mzSiW68oscokH/ct6HiB5riCVmPqFlONmPs/mEdFFUipQydxTu5AATNtUdmgw3QFGEMsero1qtaCS0r0QDLZFmOktB+g20nqUt9Dyv1rLCD5R+0qZgjy0Zsl9y0n4GwMmELTD38mwiT4uLZHNFXaaQvafJGnHVUJM/sEWIf081xlq20rkAeCyuCddDsXs2aiV8ylFOXfOylzVKpRSrjkSUKkeCgehWfoHx6rToZAUCwX0HvaZHOZaaVyl8GMDmekExmbdezaDw0x0JbS3Qb7rnMAEOTpAEeoiRGsDNoPXTw9UyyHLIa7jGieSkRHd7ZLyde1bp2WdocsQjPyO6D7y6t 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)(376014)(36860700013)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:56:24.5422 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dc4a332c-3b8c-4d26-0400-08dd0f36b801 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: BN2PEPF000055DC.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6685 Restricted injection is a feature which enforces additional interrupt and event injection security protections for a SEV-SNP guest. It disables all hypervisor-based interrupt queuing and event injection of all vectors except a new exception vector, #HV (28), which is reserved for SNP guest use, but never generated by hardware. #HV is only allowed to be injected into VMSAs that execute with Restricted Injection. The guests running with the SNP restricted injection feature active limit the host to ringing a doorbell with a #HV exception. Define two fields in the #HV doorbell page: a pending event field, and an EOI assist. Create the structure definition for the #HV doorbell page as per GHCB specification. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/asm/svm.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 2b59b9951c90..95cb9a62f477 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -239,6 +239,39 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_TSC_RATIO_MAX 0x000000ffffffffffULL #define SVM_TSC_RATIO_DEFAULT 0x0100000000ULL +/* + * Hypervisor doorbell page: + * + * Used when restricted injection is enabled for a VM. One page in size that + * is shared between the guest and hypervisor to communicate exception and + * interrupt events. + */ +struct hvdb_events { + /* First 64 bytes of HV doorbell page defined in GHCB specification */ + union { + struct { + /* Non-maskable event indicators */ + u16 vector: 8, + nmi: 1, + mce: 1, + reserved2: 5, + no_further_signal: 1; + }; + + u16 pending_events; + }; + + u8 no_eoi_required; + + u8 reserved3[61]; +}; + +struct hvdb { + struct hvdb_events events; + + /* Remainder of the page is for software use */ + u8 reserved[PAGE_SIZE - sizeof(struct hvdb_events)]; +}; /* AVIC */ #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFFULL) From patchwork Wed Nov 27 22:55:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887414 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2064.outbound.protection.outlook.com [40.107.95.64]) (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 30F0C13BC35; Wed, 27 Nov 2024 22:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748213; cv=fail; b=qToWJ3Wo3zifHvkNrEXfjFGRqjSqXmcsQSrO/gRz6DSn+XfSyAnuhbIWoaY4u822dab/wMrBkyQjTElNp/XY9ipV/jlyhX5X/vxk+XnRCo2nhuEj1iWksF4ljLXmjuYKFiStt5oqqMzos/JOS4BFeX3i8c8siSaN3kAuhFy5X6I= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748213; c=relaxed/simple; bh=xip+nJAoKbEYFWXuVQEWfiZW5IdFYH3aHqOvAp7sAIc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VT/4SB9ZiWcIcfQr7yP4h5R7Yp/AQ7OdZOWjHZcNaGgt5YHO8e2rUdY1OkVymGt+cY6mmLfWRIMV8fOKFSgt9jVB94daGIbxRHSaoc/f9l7Pre9pUYYRxds7/ODNuAnBke4fIgVElDYFui2tsIQyjnzmt8U4qaHRw55xQ9vtrmw= 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=xW61D/oo; arc=fail smtp.client-ip=40.107.95.64 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="xW61D/oo" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mZdQ8A48txSDt+EXFGxHZmdUy08AiV4vusAx8sg0c4xW+3sbqvvLRZTd/oAzPZma7YzuRJ7eW5PlFjlxKCNiV9NkDM+6z6/boMpdgcn2CRC88z+7kMIYT00eKPN2MJSl+d1UQT3cQwHRVwPjlzWqwW6gYpcSRR4LIRENBCeBkx3mjvCYOOapSG/ABt9Aq0Z5nEokt50yRjzOaEwlEh9vDV3zRMIe0IaV+olukM1Ox2ml4ZPvjpfsLpE0Vey9xmE5YPB/xsk/c7Zgrz+JQfganWewVC11ogNRlagrwfVgkqRZRZjE1XcnoimWP9ciFNG8WJ1WblxTfqZqIU7yzdYycA== 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=f0LH54jEbq2oagAvJ281mIIieXRjTN4sISx9lTdni8Y=; b=wbRFVEsrFYLAjAq65P8eod+xtPlugaFFx7Lp18s7pagjmVb5sf98ownh8bLRJNdtaA7tBUvuyopZ588f9YJZMddyJsObxtfhLs5k8Yj6SjXwf9WKLXJEtv260V0864P96KloccNfdI7YPNM+7a9AA3w0f8gTqiffipY5t66Ao98fyZFYP4f1pYcgv3L9SaSydGvHMKvYBFBGNhMinRThh9N6ImdU3UoDGCyoGVOS6F3YSRhkhhsHsm/yrVAtVeeLyUXZzpCHSu8G5/PyqMC8la7YJYrIBtB5QsSJWI7sHlKbNoXSKdsUGoit43j5VA9q75K/ms6RgoAfJdswBUkPZg== 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=f0LH54jEbq2oagAvJ281mIIieXRjTN4sISx9lTdni8Y=; b=xW61D/ooRXM6kebZkKVy73qVSV0B5kgHHzVTYCQ3myNFbopJZ+hMTe0dJRvXMIm/DID1P7EdjNHm/IVZkdUfBgi85v6SCAD8an2IlJFAIiirWoNvJK8CsS4cN0IPK8p5QyZAD5QiFUy45z5QzlaHK6zESTZYD5M002Eh32YSCN8= Received: from BN9P223CA0009.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::14) by PH8PR12MB6964.namprd12.prod.outlook.com (2603:10b6:510:1bf::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.13; Wed, 27 Nov 2024 22:56:45 +0000 Received: from BN2PEPF000055DD.namprd21.prod.outlook.com (2603:10b6:408:10b:cafe::10) by BN9P223CA0009.outlook.office365.com (2603:10b6:408:10b::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.13 via Frontend Transport; Wed, 27 Nov 2024 22:56: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 BN2PEPF000055DD.mail.protection.outlook.com (10.167.245.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:56:44 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:56:43 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 2/7] KVM: SVM: Add support for the SEV-SNP #HV doorbell page NAE event Date: Wed, 27 Nov 2024 22:55:34 +0000 Message-ID: <20241127225539.5567-3-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055DD:EE_|PH8PR12MB6964:EE_ X-MS-Office365-Filtering-Correlation-Id: 2c5a2035-6ce7-4eb0-4cf0-08dd0f36c42e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|1800799024|376014; X-Microsoft-Antispam-Message-Info: 8OFkjUbmDXXP/zCmEAmkabllrcPpdbZZ0wF3W6BvYDG9KrRyqwKYa8vfgKsPdUk+JPB2XHfzOS09oNyv+o/qj3aHlXjCB3IJuOEOfN1CxMdDmxsLfh+FZBKEt09HN9FGuJiGO2jB0K8/dSSDen0lUGwIux0dJwJI13RM/f0bnVLogsD73sXopZCob3inQseJkozOdvYsQ6pgP1XMbDzQN9oUiOMFAosBvC+ONrGylkK2/2CBFueE9D/z89HlwhPyg3PtuZQ9qswVnCi8LdAfst0MKmbr2UO6qcwbIXcsFES1k+wVSJZN2zVaqT9Uzjq68roCKGPeO++yIGaIn93eXdDodiB4mGIZxT/mDTEnCyfLhudb8QZGdCZbShAG8vDM+rdkEh9fqoiN6BFRJBzUINWja1aW/ID1wF8hIVEaFUswg0eTzd/c+N7sCjLZ+YJAA0msWaOqKT2HahukHvM+2YU69JDxp3GveOMB9TfLeO8COwS56uXPGSnu2Mf7gxI6OgZAlcKBULEE+HUwqb1DYZjbnoFBq/olRUioHTMIf4sQX+Mgy+C//QK2H9fA3rA5sCfOEMf7Sn4RLuUkwGLs9wYSRfKcgxdGBapDE0BXhP+H+llYaC1MjbC8g8H65dJRMOt9OPrhazYL710QhabVTsm/uo7+v1Iu3C3xCIn5GehpYOBtSx6sXuObjOJlDIzaeoIPOJavIKatcEtl3twsAGByvUONekG1nUf4qu1FdGbg7PEg5O+3xPN/QwXDfDW+QTv/9lffisyrdb9AEsKDULGxHDBhdAbIKZ+91C+P61W96gRIjuG/zVcVriBKOIaMJPVykt7hoB3RlRLrTltU3au+aNUigMVFjhyKcCpME31tqpdpaoNVOHlBsjm1yhGyEUIC0ocNBUCkXPlJOxbvlEVHdWBcNPFeU/0Y7OT4/65PeYMRglAMO4fA3A8pNhiyCEqsvLUfWrWNxhwP7lyM/hd0FpI4xRrlXbkVEMiTeZzGj6KAT16wPoDIEeTFVNZWe6e2g+SNGwyykrdclEr/QiSHEEpx400N17ZE9m60eecO8/AKriuiJ/sAFi0XsKACnGmxJz9xr4WLojczmpNFU8HGIyvwcT9oo+iVqiOubUJ4e34wP4QXR73wAHOjvIPiE5dhv0fXJsdh0khhxZAW14uufAsvZkkAId3HV49oF1GugoWxmQSQRojN/xbAitfZ7NUPYIfqmXjSoXp6xf9fYn5zf6NiBDxS2BeMta+U3nSg+VSSM6fUA1XvdCrJju4NY/sYHbRl+a+pu6EZ9FT+LWhBkxydb7s1uNfVkOghvQXhVuD+oFPBaA6ETXTksJoP2nWb6Ij2iaqsfs8byLfXXhoVa/GkafpBruC2a2x25OQABnBv64aTzDuIuYA6Bc9ZIm8Ht7jy16SOUXy68n5WLehCDdJcorpcFj+zvp5mQLFZoD4grvvyClXgks74kAm0 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);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:56:44.9872 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2c5a2035-6ce7-4eb0-4cf0-08dd0f36c42e 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: BN2PEPF000055DD.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6964 To support the SEV-SNP Restricted Injection feature, the SEV-SNP guest must register a #HV doorbell page for use with the #HV. The #HV doorbell page NAE event allows the guest to register a #HV doorbell page. The NAE event consists of four actions: GET_PREFERRED, SET, QUERY, CLEAR. Implement the NAE event as per GHCB specification. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/uapi/asm/svm.h | 5 +++ arch/x86/kvm/svm/sev.c | 73 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 + 3 files changed, 80 insertions(+) diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 1814b413fd57..7905c9be44d1 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -115,6 +115,11 @@ #define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 #define SVM_VMGEXIT_AP_CREATE 1 #define SVM_VMGEXIT_AP_DESTROY 2 +#define SVM_VMGEXIT_HVDB_PAGE 0x80000014 +#define SVM_VMGEXIT_HVDB_GET_PREFERRED 0 +#define SVM_VMGEXIT_HVDB_SET 1 +#define SVM_VMGEXIT_HVDB_QUERY 2 +#define SVM_VMGEXIT_HVDB_CLEAR 3 #define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018 #define SVM_VMGEXIT_HV_FEATURES 0x8000fffd #define SVM_VMGEXIT_TERM_REQUEST 0x8000fffe diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 72674b8825c4..7cd1c0652d15 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3413,6 +3413,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) control->exit_info_1 == control->exit_info_2) goto vmgexit_err; break; + case SVM_VMGEXIT_HVDB_PAGE: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -4129,6 +4133,66 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r return 1; /* resume guest */ } +static int sev_snp_hv_doorbell_page(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_host_map hvdb_map; + gpa_t hvdb_gpa; + u64 request; + + if (!sev_snp_guest(vcpu->kvm)) + return -EINVAL; + + request = svm->vmcb->control.exit_info_1; + hvdb_gpa = svm->vmcb->control.exit_info_2; + + switch (request) { + case SVM_VMGEXIT_HVDB_GET_PREFERRED: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, ~0ULL); + break; + case SVM_VMGEXIT_HVDB_SET: + svm->sev_es.hvdb_gpa = INVALID_PAGE; + + if (!PAGE_ALIGNED(hvdb_gpa)) { + vcpu_unimpl(vcpu, "vmgexit: unaligned #HV doorbell page address [%#llx] from guest\n", + hvdb_gpa); + return -EINVAL; + } + + if (!page_address_valid(vcpu, hvdb_gpa)) { + vcpu_unimpl(vcpu, "vmgexit: invalid #HV doorbell page address [%#llx] from guest\n", + hvdb_gpa); + return -EINVAL; + } + + /* Map and unmap the GPA just to be sure the GPA is valid */ + if (kvm_vcpu_map(vcpu, gpa_to_gfn(hvdb_gpa), &hvdb_map)) { + /* Unable to map #HV doorbell page from guest */ + vcpu_unimpl(vcpu, "vmgexit: error mapping #HV doorbell page [%#llx] from guest\n", + hvdb_gpa); + return -EINVAL; + } + kvm_vcpu_unmap(vcpu, &hvdb_map); + + svm->sev_es.hvdb_gpa = hvdb_gpa; + fallthrough; + case SVM_VMGEXIT_HVDB_QUERY: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, svm->sev_es.hvdb_gpa); + break; + case SVM_VMGEXIT_HVDB_CLEAR: + svm->sev_es.hvdb_gpa = INVALID_PAGE; + break; + default: + svm->sev_es.hvdb_gpa = INVALID_PAGE; + + vcpu_unimpl(vcpu, "vmgexit: invalid #HV doorbell page request [%#llx] from guest\n", + request); + return -EINVAL; + } + + return 0; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4409,6 +4473,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) case SVM_VMGEXIT_EXT_GUEST_REQUEST: ret = snp_handle_ext_guest_req(svm, control->exit_info_1, control->exit_info_2); break; + case SVM_VMGEXIT_HVDB_PAGE: + if (sev_snp_hv_doorbell_page(svm)) { + ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2); + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); + } + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -4576,6 +4648,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) sev_enc_bit)); mutex_init(&svm->sev_es.snp_vmsa_mutex); + svm->sev_es.hvdb_gpa = INVALID_PAGE; } void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 43fa6a16eb19..161bd32b87ad 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -239,6 +239,8 @@ struct vcpu_sev_es_state { gpa_t snp_vmsa_gpa; bool snp_ap_waiting_for_reset; bool snp_has_guest_vmsa; + + gpa_t hvdb_gpa; }; struct vcpu_svm { From patchwork Wed Nov 27 22:55:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887415 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2082.outbound.protection.outlook.com [40.107.93.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 28BD6203704; Wed, 27 Nov 2024 22:57:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748255; cv=fail; b=U/f0PwdZhCcx1AYigpW2qJ4AgYU1B8hm1/e7zFdx3pdQLbspAzFLKAs0DR8G+sKw5Mup2+d+XVnHZc+L640v4SNURBdwBqTLS7iNPjiGxrGx2WgLRrXgNp16PT2rJ44Bu9uYf71Yf7VPZlVSeLSrZxv/S0vMohecdOiCB6TD0ts= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748255; c=relaxed/simple; bh=74XSjuNJR4yfTOXlTu0upmcncP1IauW8MVha0A7XLac=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CEEyR+xHcoh7NWmhwZ8LYAQuyi3wyo1IFPJhje4+BiKX5oTREjyQMIxFJZhB+zlpFu4CR5PaFpwxnMtISaZ7rpElABq0WpdWj8hEd89CNzzSVQb42C/8efUcc5rTfYev4RS97ohf5nW5EBCxBhyMxVbxCVb+q9trF3FgbN7LRX0= 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=vzaqpsDA; arc=fail smtp.client-ip=40.107.93.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="vzaqpsDA" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=zEXTfgJlh+R743GTHtcXkRyF04wozHYVsUtBZGSYGd8VJ7Fqkyoc9Gcr96e+8LhfhqA3a6LjHeb3o91XhYEhcl/YGPR58n6hVYpaix5PcnB2sm6GlZOa5VwgKIUQoTB2Jq5DhbqpS0EMPSVKrEmXkT7Sfbe1BKBq/LpigG6HeubokLQGCbdOToeRmvuPPhlWXfAfTqvgxdPVooo6D8vJxEB9ldS9DWswap5EfWCouZjiUX/QIZDASUfPqpbP9MyRv/UiA4KBFYaU4VROjYsvAeb6WehSfZNxNylcfi1ozQfOdhGh0S3cQISH462C5WO7U3qrVnO0PaNfR52uaAnQfA== 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=UN7UVbzhY6KoiLPYLqE9n+rTEZUrpnHPIyIodxuy54k=; b=QbjlARhOJQuiyuyDmRcTuOIuJXaEn1Y2hlWR738ZUQMaHtJoEz5Wk7Hs+SsmG6WQOhy2aqEarWszx9WdR8LEgjG2JNJ1pWkRgLQZe1Y41pGMvhQx7wRycq7bcihD7xM+8l+JXKe5PoMyEW8EXXWgHPKQoJl1eNj13wCmmbkjCKd+Q7wykP72l2J+OYkVynmSoOZ89zSdLbd2Y0MZp+iyHrwnAA9OfVMw3YIKbKD8T9Is92Mun+HJThiakplkl2UDERVInTJWmB4j5wsr+FRM88lwuChUNkK3Ut+sAlcK6InT6AKUtJpVsXdapI+R7aU9MX2fAmyB2lEdkcC2mWnVzA== 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=UN7UVbzhY6KoiLPYLqE9n+rTEZUrpnHPIyIodxuy54k=; b=vzaqpsDAYN6jdi07QJqtxJn98+yrw9z/m+wD5RRkZXcqd62GuKdWGwpNGAl0QmYp2sVpBN9La7T5R9byieA8cxOsEvMv06Xs5jwRwDPz8bXvWftnYhodrWwtvGo9e45CxFPXmoLokf7aVo5uBSPs1552IrTnvHp/UqdgZmeu4Js= Received: from BL0PR1501CA0012.namprd15.prod.outlook.com (2603:10b6:207:17::25) by SJ0PR12MB6831.namprd12.prod.outlook.com (2603:10b6:a03:47d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8182.23; Wed, 27 Nov 2024 22:57:25 +0000 Received: from BN2PEPF000055DF.namprd21.prod.outlook.com (2603:10b6:207:17:cafe::4c) by BL0PR1501CA0012.outlook.office365.com (2603:10b6:207:17::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8182.17 via Frontend Transport; Wed, 27 Nov 2024 22:57:24 +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 BN2PEPF000055DF.mail.protection.outlook.com (10.167.245.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:57:24 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:57:23 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 3/7] KVM: SVM: Inject #HV when restricted injection is active Date: Wed, 27 Nov 2024 22:55:35 +0000 Message-ID: <20241127225539.5567-4-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055DF:EE_|SJ0PR12MB6831:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f39c428-e135-4914-fba7-08dd0f36dbd6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|36860700013|376014; X-Microsoft-Antispam-Message-Info: CkS8PlDG8bOpHnSZVrgmrush2kdtlw6ex9CDkvPh3N8obD7y1/v1HMmtVuIu8WkR0Gw3ptPgxwQg8aFQqwJdS9tSwLbPBbnpUAcQvdxdjFYT8sPwEj0zI5uyGYBUGByOwypatjohL+6TW2z8ELX3C7YmrkGf9kEIzN+L6Fa6y0+UybO4Hlfy3oP2oCXbblHuqoKmYKJ+KP3g4HLAcOIvchTOGbrV8xaDnoh7LHVLAHQFvo6Y23ipHA0k2fFkZ5PyGDXZoTYAdhF98JrySsl6BUOFZnGm0uitxFviJ9O7/RVACDFk15WwjQhpYx9qYiYl0/OM9yyPuca3pReOTtyo7wADlD3S3hQrk1zsbbtsju7j0sMT34M+HldjH9q4PAQKaTQGxWvTxTcVPrwNEIsixhkhyOYuAEiO1bqkVRXT4d4VdtjTTh6zUMke2Zx+VNBhOHTovLDbfK90/gjp0DG0H+YxcLgXgyqF93DcgxzXK7g9xJd52eNACt07CLJ3RSsaIqOEITiKpXr/IFUeb4aN1aGWh14dnQ/0IZ39d5QJpBt6vXD9a5qHJgbcGrBOLinObTjh5F20rdh5LVfjcsYoxQnEL1p1jOUvIiyisSNj/DUDHxjtYKZywbCMxeNEXug2SlPNar4I9mgA23Ay756P/FIJg8TzNTsmTZslNUczD1iXHkacyEtJBpTpykVOxEIvZs+350yEO7q1kEg6ayWM2jUI/EfgDkjid9YdrfSku6wEPOkbGyCfnIBmLvJYutJvFZYcYxwqwYeqVKds8FtrbIl1lXYcS2Qs6NbSIp/MHPUOip80TIERnAzUOzwmqJ82Jr4UL2LDKbdxvaYW5xGgbs9GuD1ua3Fp/3I41WkozpNC7cWX/qQFwSrN9h4dSsdYTUXJYeQqi/J2l+E3keC7UOJn4sUIPt4VsLGApVcoDAY1DxaRVWgpkL0tRfbY4h8V50v1o/TFXlfuCNBhEEA56p0u6LDa2nsZc6GYUY6dZValjV7f/KVRs00zU0Y9BTpwrueCZz64r/e1Sz5CqG+qx/wxhmAKXkcSpQZuAUiJAUN1S2KDQcefZobkBgqp/6DsWkOhul49x0xDDNzHnTMD1PmVaoZrM/zPzIkM6NCb6vMnNWvHmfbQvZNXW2eKIomJhSI+QgjDGTQkGn8OzwkyI69+9NeYsUrG+0gk+rXWJpTBSZH3Aonh5Fmq5mx8vBFGwbsUZzGm9/g/NG3uaMpxC2SiKX3oVwLEbpBzn23a4pGE/dEQEfoB6a455g0TLm42eXBQ5lopmSc7ggHRRguAkUrwyMXzox8FEBOtZBF/e6tfD06oq+FR/PtADXfs08u9XcfzsFSxvesAU0NSXbquj3XYgpqZQOV03kue+4r+MaizUTvAfxKRppXJ269Tue0T/iH0GHqWxsuudUOaOg6f47SraRJcOdJIhKBDI5dPqGA/XY1yxAIIj+y56apgHZFN 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)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:57:24.6803 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f39c428-e135-4914-fba7-08dd0f36dbd6 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: BN2PEPF000055DF.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6831 When restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest. Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject the interrupt or exception. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 165 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 +++- arch/x86/kvm/svm/svm.h | 21 +++- 4 files changed, 199 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 88585c1de416..ec82ab4ef70c 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -35,6 +35,7 @@ #define MC_VECTOR 18 #define XM_VECTOR 19 #define VE_VECTOR 20 +#define HV_VECTOR 28 /* Select x86 specific features in */ #define __KVM_HAVE_PIT diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7cd1c0652d15..77dbc7dea974 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5028,3 +5028,168 @@ int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) return level; } + +static void prepare_hv_injection(struct vcpu_svm *svm, struct hvdb *hvdb) +{ + if (hvdb->events.no_further_signal) + return; + + svm->vmcb->control.event_inj = HV_VECTOR | + SVM_EVTINJ_TYPE_EXEPT | + SVM_EVTINJ_VALID; + svm->vmcb->control.event_inj_err = 0; + + hvdb->events.no_further_signal = 1; +} + +static void unmap_hvdb(struct kvm_vcpu *vcpu, struct kvm_host_map *map) +{ + kvm_vcpu_unmap(vcpu, map); +} + +static struct hvdb *map_hvdb(struct kvm_vcpu *vcpu, struct kvm_host_map *map) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!VALID_PAGE(svm->sev_es.hvdb_gpa)) + return NULL; + + if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->sev_es.hvdb_gpa), map)) { + /* Unable to map #HV doorbell page from guest */ + vcpu_unimpl(vcpu, "snp: error mapping #HV doorbell page [%#llx] from guest\n", + svm->sev_es.hvdb_gpa); + + return NULL; + } + + return map->hva; +} + +static void __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct kvm_host_map hvdb_map; + struct hvdb *hvdb; + + hvdb = map_hvdb(vcpu, &hvdb_map); + if (!hvdb) { + WARN_ONCE(1, "restricted injection enabled, hvdb page mapping failed\n"); + return; + } + + hvdb->events.vector = vcpu->arch.interrupt.nr; + + prepare_hv_injection(svm, hvdb); + + unmap_hvdb(vcpu, &hvdb_map); +} + +bool sev_snp_queue_exception(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!sev_snp_is_rinj_active(vcpu)) + return false; + + /* + * Restricted injection is enabled, only #HV is supported. + * If the vector is not HV_VECTOR, do not inject the exception, + * then return true to skip the original injection path. + */ + if (WARN_ONCE(vcpu->arch.exception.vector != HV_VECTOR, + "restricted injection enabled, exception vector %u injection not supported\n", + vcpu->arch.exception.vector)) + return true; + + /* + * An intercept likely occurred during #HV delivery, so re-inject it + * using the current HVDB pending event values. + */ + svm->vmcb->control.event_inj = HV_VECTOR | + SVM_EVTINJ_TYPE_EXEPT | + SVM_EVTINJ_VALID; + svm->vmcb->control.event_inj_err = 0; + + return true; +} + +bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) +{ + if (!sev_snp_is_rinj_active(vcpu)) + return false; + + __sev_snp_inject(type, vcpu); + + return true; +} + +void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct kvm_host_map hvdb_map; + struct hvdb *hvdb; + + if (!sev_snp_is_rinj_active(vcpu)) + return; + + if (!svm->vmcb->control.event_inj) + return; + + if (WARN_ONCE((svm->vmcb->control.event_inj & SVM_EVTINJ_VEC_MASK) != HV_VECTOR, + "restricted injection enabled, %u vector not supported\n", + svm->vmcb->control.event_inj & SVM_EVTINJ_VEC_MASK)) + return; + + /* + * Copy the information in the doorbell page into the event injection + * fields to complete the cancellation flow. + */ + hvdb = map_hvdb(vcpu, &hvdb_map); + if (!hvdb) + return; + + if (!hvdb->events.pending_events) { + /* No pending events, then event_inj field should be 0 */ + WARN_ON_ONCE(svm->vmcb->control.event_inj); + goto out; + } + + /* Copy info back into event_inj field (replaces #HV) */ + svm->vmcb->control.event_inj = SVM_EVTINJ_VALID; + + if (hvdb->events.vector) + svm->vmcb->control.event_inj |= hvdb->events.vector | + SVM_EVTINJ_TYPE_INTR; + + hvdb->events.pending_events = 0; + +out: + unmap_hvdb(vcpu, &hvdb_map); +} + +/* +* sev_snp_blocked() is for each vector - interrupt, nmi and mce, +* for example, it is checking if there is an interrupt handled or not by +* the guest when another interrupt is pending. So hvdb->events.vector will +* be used for checking. While no_further_signal is signaling to the guest +* that a #HV is presented by the hypervisor. So no_further_signal is checked +* when a #HV needs to be presented to the guest. +*/ +bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) +{ + struct kvm_host_map hvdb_map; + struct hvdb *hvdb; + bool blocked; + + /* Indicate interrupts are blocked if doorbell page can't be mapped */ + hvdb = map_hvdb(vcpu, &hvdb_map); + if (!hvdb) + return true; + + /* Indicate interrupts blocked based on guest acknowledgment */ + blocked = !!hvdb->events.vector; + + unmap_hvdb(vcpu, &hvdb_map); + + return blocked; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index dd15cc635655..99f35a54b6ad 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -467,6 +467,9 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu) svm_update_soft_interrupt_rip(vcpu)) return; + if (sev_snp_queue_exception(vcpu)) + return; + svm->vmcb->control.event_inj = ex->vector | SVM_EVTINJ_VALID | (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0) @@ -3679,10 +3682,12 @@ static void svm_inject_irq(struct kvm_vcpu *vcpu, bool reinjected) trace_kvm_inj_virq(vcpu->arch.interrupt.nr, vcpu->arch.interrupt.soft, reinjected); - ++vcpu->stat.irq_injections; - svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | - SVM_EVTINJ_VALID | type; + if (!sev_snp_inject(INJECT_IRQ, vcpu)) + svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | + SVM_EVTINJ_VALID | type; + + ++vcpu->stat.irq_injections; } void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode, @@ -3827,6 +3832,9 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) if (!gif_set(svm)) return true; + if (sev_snp_is_rinj_active(vcpu)) + return sev_snp_blocked(INJECT_IRQ, vcpu); + if (is_guest_mode(vcpu)) { /* As long as interrupts are being delivered... */ if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) @@ -4145,6 +4153,8 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu) struct vcpu_svm *svm = to_svm(vcpu); struct vmcb_control_area *control = &svm->vmcb->control; + sev_snp_cancel_injection(vcpu); + control->exit_int_info = control->event_inj; control->exit_int_info_err = control->event_inj_err; control->event_inj = 0; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 161bd32b87ad..724e0b197b2c 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -55,6 +55,10 @@ extern bool x2avic_enabled; extern bool vnmi; extern int lbrv; +enum inject_type { + INJECT_IRQ, +}; + /* * Clean bits in VMCB. * VMCB_ALL_CLEAN_MASK might also need to @@ -765,6 +769,17 @@ void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn); +bool sev_snp_queue_exception(struct kvm_vcpu *vcpu); +bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu); +void sev_snp_cancel_injection(struct kvm_vcpu *vcpu); +bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu); +static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) +{ + struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; + + return sev_snp_guest(vcpu->kvm) && + (sev->vmsa_features & SVM_SEV_FEAT_RESTRICTED_INJECTION); +}; #else static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp) { @@ -795,7 +810,11 @@ static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) { return 0; } - +static inline bool sev_snp_queue_exception(struct kvm_vcpu *vcpu) { return false; } +static inline bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) { return false; } +static inline void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) {} +static inline bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) { return false; } +static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) { return false; } #endif /* vmenter.S */ From patchwork Wed Nov 27 22:55:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887416 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2052.outbound.protection.outlook.com [40.107.95.52]) (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 5A79B203700; Wed, 27 Nov 2024 22:57:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748270; cv=fail; b=d4C3GYLXT1LsC8qeybOPbHyjFD120MmOLfrdh1bgMmWywU9n9XMCShvO4fMGlg10RuERWyaAGAXIgkC7Vs19guxr3Z251LCx8fulSYEYwzRUKHogRx0ZY//wMp7zeQs2OUEGUZ18BDU6mP2j1XkwDRTmYtI46u47Ac8PY1H9Tg0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748270; c=relaxed/simple; bh=Y/9eQjOU5dMKQ7vZmXycvpKETvdAx/g//U2KNfpXaI0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uQYgWCAWxoJSwuZjoyNtGrMWjwrNXnZYwhSykuPNIl06tSwYNa5JyjEiK8MEreSuwTC/l5FCL6UqAFmo3NQ7Z4pEYlrNYhmshAmNluJJ546YsamOV1fUEevkfQi/0UjAZIb74sLTfQEpTsWcvCcyrI3grUOMj2K1DYOWh3pWums= 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=emx8wPen; arc=fail smtp.client-ip=40.107.95.52 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="emx8wPen" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=S0fOnxFoB+dIyNl7bMuhKoZ5yzJo4QQqq0FSdkTO74PSvVQGtfR+ys/8dcEOcIMiW7mh5m3J29y55L0KQOBIGXRSHc4K8Bgu/HoEZGScq3Aj6aPkWDIunxGNw/pfRJbcq6NR0xFNYGPtFLoaba0LFv8Gbay8fQq7/cLBaZN5WvmpZY4eN4x9kNEaMfS3UWsI9HQrV+zUHz5Scp7vLIjLYjW3vKCHooXc3axAxKWMsbcxo95iYmtGHQvQ7Fq90Fs8EAByILfRub8HIo0zJVp6wuXnBpy0T40cS8pegEazYT5I5ATQvP8BqwDfHFtIbksN1Hw/rbteT1hAD4pe24UkcQ== 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=g8YuRrwVkvuTsVH22qQw+nA2hfm2QMcLjWyNqIGXfyM=; b=FPrW8gj5rCch9Df2Q6R3WfzRjyYafR2iLYKIxZyAL+doDDj8qepMJqQ9tSkH934mS9H6w6Nmd2eSmMk5IQHZU/g4KZRnA+GxAIXqY9Mb8QCzrPJyn6cxJiWVfzemxNdaPkj7Gb0b65VjnBwIfHyG41MHM/jWfe6nXGzmVBj+PSQ2AILQneN6+dSVrD+Gb9cRn802qWZtd0M69X0ruNI8z8Fmk1oUV0IFE/uSh/Ym4Nhl5tOXSR3HbJ2G0raio3MFTgGXn9qvwW9kbWFleax1X+R3YuJNKpf3HlPoXDUkn1UlUhMddTH6iCc4D1CxOR94UhBjUlKhQ3HUzwEkvrxpmg== 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=g8YuRrwVkvuTsVH22qQw+nA2hfm2QMcLjWyNqIGXfyM=; b=emx8wPen/vXeNyzC65MGf2XEA5VVuarTIWTpXNLOjzpEeiP0HWpY20o6DLh4505/ig8y9yE+Ikj4WOEwvcYFYUT5KcSSvrpv5ne5cGNaMH9+maCEY2qhj6oCHtVvv8BkxqX7vFyjFp00LhNUl4bgkGxrgia/UziCMtK4Flwok0c= Received: from BN9P223CA0012.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::17) by SA3PR12MB7784.namprd12.prod.outlook.com (2603:10b6:806:317::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.13; Wed, 27 Nov 2024 22:57:44 +0000 Received: from BN2PEPF000055DD.namprd21.prod.outlook.com (2603:10b6:408:10b:cafe::10) by BN9P223CA0012.outlook.office365.com (2603:10b6:408:10b::17) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8182.20 via Frontend Transport; Wed, 27 Nov 2024 22:57:43 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN2PEPF000055DD.mail.protection.outlook.com (10.167.245.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:57:43 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:57:42 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 4/7] KVM: SVM: Inject NMIs when restricted injection is active Date: Wed, 27 Nov 2024 22:55:36 +0000 Message-ID: <20241127225539.5567-5-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055DD:EE_|SA3PR12MB7784:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ae5c23b-3747-433a-1026-08dd0f36e74d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: Ev6Bhqqia2JYE2yjtKuh7FmXAX0Visss5D/GTX+MXBW5yM+lbJnZa7sBUose8561TLDC/BukK5RyOt3Oa+KAXRNpVqYZx2IptiWB9Iu/8iC6ZKn5oY4SKTLH9lNAp00yQP9Fz4WrAT8o/n+w6VimNyxHZsRzNPtv0JktLGiC6lOWWpswoxITQd1WiOrr38TaZoqRSDWsfZhjAbHSqfWc7V+n4/OpoEpUBUbj/yYs19lNBEts4s5nVBbAkntU7n6H5J5Mc9weOPiuX5y1RHYbYOy5sHyf/6zN6rqZkCmbFYlZmwWmhQGsMMZa4PDaqg0+GoChhxFvJ+iYPKANNATWp4itu7oGAO/uavFaSxgsJOOuaWtEs82XzK+5vnU9SjcI6tiIk31+YqT06MavGDOhh3a6m3Z4RNkrI1q+NZ2ZpIDAAj4o8btqBd/3bjZWawf4YTg8aBcYqoyYnjXhBOKI5v4/Ui5wMmHaRiC1wE7FJNzoedvfgG6aQaDHlN5VKUejowxRevGgz1wSo3P5MHIAJWgHUfACcKGMqWQW3YdL/My+FGApII/h/Yr19ApEB8/RhJtfZMvGllSsYq4AGz7vxi/9xP4mxZa7Vn/1700gdi6dy+82Ws4ikNTpdefTnBatiS7/IsEloheEcj6DRFAZ4AZIzSpZG+oRS/dOaYoImP9YT4YlUyejxfqortzAtdTLDJBg4FHEtkpWOm5fLA69dnEuJG8GHqYUbJil2tFDNBefO8TvtklaxUlMktItqMuI/Xk1qcpm4LOrWZTLzBD2Tdf/P20TI8YmVy3fopwGTLbFQiQYORRnUefVxNXxp2Zz26J+HgzsgOos6NxO72eaYeba4AgWjdxIZIYzxcy4cH/cJBuBzTH5Kw2stwfnIsHmoesr9UnJgukWXoWXGcRDVAX8KBiztjLoLBlVvLt9RmTO7+pHl1gy1TOBXhysKiPnjElOOz821nGPll+q5yWJaqwIzKx4aqf+DhwNU2KnScEkjh9uEW8Z1ZRDsfB6Tetkw/fTpxWmYSX2EMfaWnldXMoqrXjn3IW0rO8BrF1n8LY9CNdXB6nApVJ1wg1n0AhTraNnaLozMvdFcCwDO1mvBVojdlOAnSv+Nnqwz9+vCpryE3zWzP6H+cM/fGFPioBZXdDT52IqgFwHL7/plcAq648QEhRZ1J3UGhV2vPyVTgLuYMHUsIpyl63QMY8UlmkEYmiz6fTFsRRko/l4obsoXyNDFQvgLj6DbVrLPYzJlqpAclUI94mrMsroj080FJp6mwmIZpebsUwG2JXayy2EzPflXXIvw8oc377d4FHPLMBaf7o07Ri6s5mRG0Mv/E07hN6+coudnp91Anb2omh9/lA9BT54rpKwORxKNGAPGz5ZOb611L9G6laaUsS/WrNOnSDnOA06Rk+85t9lUcmR6IFMRRMmlE/UUia3NmXX4l4A5/Z95c4CVCwcLB8NoDIW 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)(376014)(1800799024)(36860700013)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:57:43.9091 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2ae5c23b-3747-433a-1026-08dd0f36e74d 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: BN2PEPF000055DD.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7784 When restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest. Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject NMIs. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/kvm/svm/sev.c | 19 ++++++++++++++++--- arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 77dbc7dea974..00d1f620d14a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5077,7 +5077,10 @@ static void __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) return; } - hvdb->events.vector = vcpu->arch.interrupt.nr; + if (type == INJECT_NMI) + hvdb->events.nmi = 1; + else + hvdb->events.vector = vcpu->arch.interrupt.nr; prepare_hv_injection(svm, hvdb); @@ -5157,10 +5160,17 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) /* Copy info back into event_inj field (replaces #HV) */ svm->vmcb->control.event_inj = SVM_EVTINJ_VALID; + /* + * KVM only injects a single event each time (prepare_hv_injection), + * so when events.nmi is true, the vector will be zero + */ if (hvdb->events.vector) svm->vmcb->control.event_inj |= hvdb->events.vector | SVM_EVTINJ_TYPE_INTR; + if (hvdb->events.nmi) + svm->vmcb->control.event_inj |= SVM_EVTINJ_TYPE_NMI; + hvdb->events.pending_events = 0; out: @@ -5186,8 +5196,11 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) if (!hvdb) return true; - /* Indicate interrupts blocked based on guest acknowledgment */ - blocked = !!hvdb->events.vector; + /* Indicate NMIs and interrupts blocked based on guest acknowledgment */ + if (type == INJECT_NMI) + blocked = hvdb->events.nmi; + else + blocked = !!hvdb->events.vector; unmap_hvdb(vcpu, &hvdb_map); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 99f35a54b6ad..91bf17684bc8 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3616,6 +3616,9 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + if (sev_snp_inject(INJECT_NMI, vcpu)) + goto status; + svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; if (svm->nmi_l1_to_l2) @@ -3630,6 +3633,8 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) svm->nmi_masked = true; svm_set_iret_intercept(svm); } + +status: ++vcpu->stat.nmi_injections; } @@ -3800,6 +3805,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu) if (!gif_set(svm)) return true; + if (sev_snp_is_rinj_active(vcpu)) + return sev_snp_blocked(INJECT_NMI, vcpu); + if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) return false; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 724e0b197b2c..b6e833f455ae 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -57,6 +57,7 @@ extern int lbrv; enum inject_type { INJECT_IRQ, + INJECT_NMI, }; /* From patchwork Wed Nov 27 22:55:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887417 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2049.outbound.protection.outlook.com [40.107.220.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 05719203719; Wed, 27 Nov 2024 22:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.49 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748286; cv=fail; b=U/WmXCYmE+j8lwiDWRdphBt5S8VzXaVtHajXZLxPWJh1Z+R4M1d0NyQMJKPrq+eLRValJc4JUYLbwvT36Mnfzgd0qU402jMmSVryTDbkub0LVZH1jxKRNy+FvfSA3HnljbaRsr0yLo+YLsmNv/5A5nwR5Mzmilac8PRHkyaqovw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748286; c=relaxed/simple; bh=WdFpuSTP1DKP83gnf5AbkV5PdOIFQXK1T/NUmEGh+9w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=meccym7v6qPygdgC+U7+4s54CEg466hmlkDFhs2FGD490pB8+l5oBTVjHmdfyeQmbCicES/KmOxPltsObewF1hinBRBHLnbrI1LYv834GbplQ38bJDa4NIg93ZmuuSKUO9VrgWFnDwickssXDhCaKGbBxQUF8UTIIWPUWerQIes= 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=LmfFXDQ/; arc=fail smtp.client-ip=40.107.220.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="LmfFXDQ/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PrTBNFadKhzT43GwD3Lpvbve3ayh9m+BPprICy7bahsMs+cQV8/maED+MLzooGQup7yZ9ABVU87s0gwnqDSy9qDx4lf1f1yfrOGArBVKzZ0AQExVOyMVcF1ico50LoNH3AfNaHp4bMjLY52dyoHIZl4Zl9+BLKm30XzVV1fcs/lzY/hjznPrDZXTkr82n+KEsyVeJWoxqR0UWrfTYBiVogjlCpgwaYze93L9jjMaWDm7RvUx7AAfi9u/qwlAIfP5BAJfr1JEreSCCmphTO/UhZsZt6r8rM100yj15xsv9MGA3EPW+m7WCo6Tc2KMv96yWTaB/AoQjngh7wdLIGcAVw== 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=qvqg7PyITE+1JiitYiwoDkp34LN0+a0PWcsztV/wUdw=; b=dpDsGIg/JsMcj5tYwizczxGxYZ/fEKJObZW0mpiHIcd5o8jzKKoup0WbKXSS70CZcb5YzuVmZdljiauSWuiJf0y4VsejU1TbWIXYIP630pKEod/8E3QH3+rIHgLZs0gsK5mzFNF27rh5TaruPhclZqbcgNo1hsQIRevxLUKLAZ1xPp2QURDWx/WToW10vHuBSwfLTz+EI2k01rgoT0DqqyFk0kmbHuYW4k7ZfvzUJfmnI1QntvOQjg8QRhlo2kJOyb0uiRg2nRJJ+3qLIvLPfzgySFhWrHw3Dtbkficea3+7z2USAWkS6xPNwYxZZ/UFHdt+vuf8zqL1krO0lH9Cow== 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=qvqg7PyITE+1JiitYiwoDkp34LN0+a0PWcsztV/wUdw=; b=LmfFXDQ/1bVZ9tsyAxqwvTzQDkAaO4G1WetrS1ESIk5T8SfduOtm7Je1iKO4NwJCEDRpwOrugNUuyvIVVcjhSzbBRIjcbGNiHto/7Nrt74c/w4ytXcUrfwol3bkxthouUnLRUkLPU91ZOBV0HKl2XBR0EK6Vl/Yqcd0fMj6Y0Fw= Received: from BN9PR03CA0630.namprd03.prod.outlook.com (2603:10b6:408:106::35) by PH0PR12MB7982.namprd12.prod.outlook.com (2603:10b6:510:28d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.13; Wed, 27 Nov 2024 22:58:01 +0000 Received: from BN2PEPF000055E1.namprd21.prod.outlook.com (2603:10b6:408:106:cafe::4f) by BN9PR03CA0630.outlook.office365.com (2603:10b6:408:106::35) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.12 via Frontend Transport; Wed, 27 Nov 2024 22:58:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN2PEPF000055E1.mail.protection.outlook.com (10.167.245.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:58:00 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:57:59 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 5/7] KVM: SVM: Inject MCEs when restricted injection is active Date: Wed, 27 Nov 2024 22:55:37 +0000 Message-ID: <20241127225539.5567-6-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055E1:EE_|PH0PR12MB7982:EE_ X-MS-Office365-Filtering-Correlation-Id: 52576278-38ba-4949-93f1-08dd0f36f13b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: 4lGnbFZgBkr90XOQ1XK98l/zlfH+TYuTORHU8mu6kmlo15le4PEgftfVb8MHedKgP9UzvDnKlS4VpW3Ib++9dLg6sIa1zsxngTNUTXlnyDvPGc/MUuT64m85r00tGa+C+wqFJUn1QA+yM/1mSiZBqdBUTTDN82GeJ8TJfg1qR5CURLDNUp2RbWxJMV7ToG5XzCdaW2HeZ3CNyG8UzTnt7V764RfsujP6upjjBhq3FIjMzbRGmC7kidNOuMN+tNoQDUJE6W6Sr1kSpxlhn2/SnYP/zi+SwD3bDbfYt4rFxQmaZZ+yLn/1bAirSZmSNj+zzz6xjhQpgEby9N+x6h0kkqfxMy11NniCu8XYQIAY11x3aY4UM3RQeNXDf8lglndwPyj/JjU/di7hKPIFJtrUgTKLlHp9kNUuQb2UvMzXVOcA+vndqAxU4b6/LG+eBZGZtcFQyzG3TkeXXTCC3kUMKNVPmYVoyYPo+0zv8EZbBJpr/GAAkkilgTV4SoMpfhSPTwrH7NiSqvrNrEVRpVhb0E3AVyNmMFd7DaOTwBlGQOhM7pfE2IOWleyQ665fLcUdhOX9S/K4nakT4cgiZBBc1S7VDbWYLznW0bw3rlxvPQAQsVJ1jhBIyoSft4tac8RfPKG3uX1mStMWe22km7wh4DqEs/UDYijRcSpcCrLIq58TEgzCoW0bGj1aXXqidcdCsLDpHKrk6YrtENq8Z7aIf8DFOscTJgbYUjOaAPFH3junX1d/tkwE6w4ETnz75wtDutI4gDDImA/wlV5f1cCH5QlgOZrK6lJWQP+GQlvg7d3deWDth3npi3C9mMXEtkjT0m/djzNpDFEwUO0MeK21vMgwdrUUI+V7ixDZ4oB1aZCm1yRhSz/zQsO302pRWxiDj482RRHjRYGtqL9MZU6M5kYGyJSoeJz+StqUCB4bIEshKBZud+ZjRlnjqnLlH+4sC9HYhsF5v86myXhwdcee4PVsgFMQpv0UlHvw6fqpsBtYahFo4cXROq6Ytg/nMin3AwDy+6EWdlorcpORApZR4y9Lnx63waLQGhHrgijIUvgQBEw/OW0M78g1s69CFZaagP+lZIlv4cn7SWmF0WHUwbKKAibrFMfhfKTtQtrw640sinGO4SmMD8z2b26LtlX0aRwLuh3c/F5LLBal0iKm81Xnv8JdGWe51Eu+szB/YcsaWMzQ0PZNNdt29zLzjS8C3WvjLKoXzjzXUVNSW/g7CQfEdphiUxJv0d/I7awM3DB814uYZdJoDQYX7QjlG3R7LO4GeUx5M4HGjzLi74Sz7Bww6Crd1Y5l0Tab5jf6xPbtRVmrlyiAeOTgDMnBqWT4uaXGMbJTHkSpWmvVmIAb1Qp5wujjXaonHikf5tBxesFhtnegH0+stq3BKiBwSS5k4/k/J8YzYjI/242JS36o6QvEabp8tLu0VShQkQKA/wXmW6V8q4T4c9xUI3veeWqf 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)(376014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:58:00.5561 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 52576278-38ba-4949-93f1-08dd0f36f13b 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: BN2PEPF000055E1.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7982 When restricted injection is active, only #HV exceptions can be injected into the SEV-SNP guest. Detect that restricted injection feature is active for the guest, and then follow the #HV doorbell communication from the GHCB specification to inject the MCEs. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/sev.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.c | 17 +++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 ++ arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 7 +++++++ 9 files changed, 49 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 5aff7222e40f..07fb1e2f59cb 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -77,6 +77,7 @@ KVM_X86_OP(inject_exception) KVM_X86_OP(cancel_injection) KVM_X86_OP(interrupt_allowed) KVM_X86_OP(nmi_allowed) +KVM_X86_OP_OPTIONAL(mce_allowed) KVM_X86_OP(get_nmi_mask) KVM_X86_OP(set_nmi_mask) KVM_X86_OP(enable_nmi_window) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index e159e44a6a1b..288b826e384c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1717,6 +1717,7 @@ struct kvm_x86_ops { void (*cancel_injection)(struct kvm_vcpu *vcpu); int (*interrupt_allowed)(struct kvm_vcpu *vcpu, bool for_injection); int (*nmi_allowed)(struct kvm_vcpu *vcpu, bool for_injection); + int (*mce_allowed)(struct kvm_vcpu *vcpu); bool (*get_nmi_mask)(struct kvm_vcpu *vcpu); void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked); /* Whether or not a virtual NMI is pending in hardware. */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 00d1f620d14a..19fcb0ddcff0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5079,6 +5079,8 @@ static void __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) if (type == INJECT_NMI) hvdb->events.nmi = 1; + else if (type == INJECT_MCE) + hvdb->events.mce = 1; else hvdb->events.vector = vcpu->arch.interrupt.nr; @@ -5094,6 +5096,11 @@ bool sev_snp_queue_exception(struct kvm_vcpu *vcpu) if (!sev_snp_is_rinj_active(vcpu)) return false; + if (vcpu->arch.exception.vector == MC_VECTOR) { + __sev_snp_inject(INJECT_MCE, vcpu); + return true; + } + /* * Restricted injection is enabled, only #HV is supported. * If the vector is not HV_VECTOR, do not inject the exception, @@ -5162,7 +5169,7 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) /* * KVM only injects a single event each time (prepare_hv_injection), - * so when events.nmi is true, the vector will be zero + * so when events.nmi is true, the mce and vector will be zero */ if (hvdb->events.vector) svm->vmcb->control.event_inj |= hvdb->events.vector | @@ -5171,6 +5178,9 @@ void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) if (hvdb->events.nmi) svm->vmcb->control.event_inj |= SVM_EVTINJ_TYPE_NMI; + if (hvdb->events.mce) + svm->vmcb->control.event_inj |= MC_VECTOR | SVM_EVTINJ_TYPE_EXEPT; + hvdb->events.pending_events = 0; out: @@ -5196,9 +5206,11 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) if (!hvdb) return true; - /* Indicate NMIs and interrupts blocked based on guest acknowledgment */ + /* Indicate NMIs, MCEs and interrupts blocked based on guest acknowledgment */ if (type == INJECT_NMI) blocked = hvdb->events.nmi; + else if (type == INJECT_MCE) + blocked = hvdb->events.mce; else blocked = !!hvdb->events.vector; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 91bf17684bc8..696653269c55 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3881,6 +3881,22 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) return 1; } +bool svm_mce_blocked(struct kvm_vcpu *vcpu) +{ + if (sev_snp_is_rinj_active(vcpu)) + return sev_snp_blocked(INJECT_MCE, vcpu); + + return false; +} + +static int svm_mce_allowed(struct kvm_vcpu *vcpu) +{ + if (svm_mce_blocked(vcpu)) + return 0; + + return 1; +} + static void svm_enable_irq_window(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -5091,6 +5107,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .cancel_injection = svm_cancel_injection, .interrupt_allowed = svm_interrupt_allowed, .nmi_allowed = svm_nmi_allowed, + .mce_allowed = svm_mce_allowed, .get_nmi_mask = svm_get_nmi_mask, .set_nmi_mask = svm_set_nmi_mask, .enable_nmi_window = svm_enable_nmi_window, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index b6e833f455ae..9c71bf01729b 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -58,6 +58,7 @@ extern int lbrv; enum inject_type { INJECT_IRQ, INJECT_NMI, + INJECT_MCE, }; /* @@ -616,6 +617,7 @@ void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); void disable_nmi_singlestep(struct vcpu_svm *svm); bool svm_smi_blocked(struct kvm_vcpu *vcpu); bool svm_nmi_blocked(struct kvm_vcpu *vcpu); +bool svm_mce_blocked(struct kvm_vcpu *vcpu); bool svm_interrupt_blocked(struct kvm_vcpu *vcpu); void svm_set_gif(struct vcpu_svm *svm, bool value); int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code); diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 92d35cc6cd15..036f750c53c5 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -87,6 +87,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .cancel_injection = vmx_cancel_injection, .interrupt_allowed = vmx_interrupt_allowed, .nmi_allowed = vmx_nmi_allowed, + .mce_allowed = vmx_mce_allowed, .get_nmi_mask = vmx_get_nmi_mask, .set_nmi_mask = vmx_set_nmi_mask, .enable_nmi_window = vmx_enable_nmi_window, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 893366e53732..afa6d126324c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5103,6 +5103,11 @@ int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) return !vmx_interrupt_blocked(vcpu); } +int vmx_mce_allowed(struct kvm_vcpu *vcpu) +{ + return 1; +} + int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) { void __user *ret; diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index a55981c5216e..8607ef20897d 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -93,6 +93,7 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu); void vmx_cancel_injection(struct kvm_vcpu *vcpu); int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection); int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection); +int vmx_mce_allowed(struct kvm_vcpu *vcpu); bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); void vmx_enable_nmi_window(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2e713480933a..a76ce35c5b93 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10357,12 +10357,19 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, } } + if (vcpu->arch.exception.vector == MC_VECTOR) { + r = static_call(kvm_x86_mce_allowed)(vcpu); + if (!r) + goto out_except; + } + kvm_inject_exception(vcpu); vcpu->arch.exception.pending = false; vcpu->arch.exception.injected = true; can_inject = false; +out_except: } /* Don't inject interrupts if the user asked to avoid doing so */ From patchwork Wed Nov 27 22:55:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887418 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2070.outbound.protection.outlook.com [40.107.244.70]) (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 36CDB2036E3; Wed, 27 Nov 2024 22:58:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748308; cv=fail; b=cqWCxrprmU+tVg4/RDCK0ggrBOOynp62nDy17teOPZZ108uADXWCkNb95IK5G0g/Ncl1Co6PD4h0pFdObmU7Bndo2mV23Eg13QdF5TVnZsdMIQRMEd+iSrwZ3FDxqJz05HHYhlr2USgIvHTDpH8pxqSFMzlHA1KEf/4H+AMVZUM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748308; c=relaxed/simple; bh=hMf8Xy8uCOdFVm8cjVZtD2mvkfJo9Q40MXlQx8l7PbI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Y9uY/moZisttJoGS3wjgyt2tGcSoElDFgLNEta630+xrCzVBwGe67lJRJMRAVCI6ak1NQWzwSB3aGmNqCmrl1oOtCf/mcDr5g18onk8GmvU9uQmXv/n0F3ObgR0HgYZnoLs1s0eVQYDxTdN0dmtCSrAPxmNqqsg0rgg39wGG9LU= 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=3YvJoZZ5; arc=fail smtp.client-ip=40.107.244.70 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="3YvJoZZ5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=prHaFSHUdCE5R/Tm7uIhkSEkAtQ+EBuQu9uu/fDoBEz2L/xfxjclYPy1wmMR3hrzDzI/UJVLPoTTfaKIe4GVySOiYQCnP2x/41TBNoeHuHDgS8w5AcFoFAFoQO3qvjcJImkg6ZhRBgrefMxGx5R8BG8HJN48C2h3e9VNR+/62xiMAPyec/nuHBmBLxiIgadv4KxD33FS1X7M5eGANpUaj3h12LUaloOx0t8gCh56RtpmZ5Fo9PZ2HPF4NxzAXtxGKLDRlrXqUn7uyMo2mnL7fCyEIy51HKSCUFHBVVkOVoq8RpzG5YE7xAmdt7n5xxuwHk6Ee3cktruu7moAN7qbjQ== 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=epAxFeTsCYoLZvYhuWzuuX5wZ3QKXW3RxkhG4dQUlAw=; b=mWaUte+IDgjYpzK97cywMP8C92B5V3bGI8nfRv124FN5RpangRJyef//rxcvoUBRwcN8wh2inJ3rR+5rD5GWJGZxIpeaM4Z9ljSixfIGwbleaAfaZ6tVzeFuH5uXpPbj2fUFGlXs9sHCnclYmb9tz5ZBaS1GUQ7h5yW077pEnMx13BAl50unbNpGwsmZC2u8/jdRRYd7zrfHp+S6dWJY8ZK/TQIZjFTVlkE5D55JIKiI98I89lWgbS70MN8f6QGYyjfxNraR2WsP0eB8vd7h6ziSgJ77gRGljkkHZpZRpOvVZ/QICJO+R3aKuv1530ZYXp1+tGflzDK/8XhM0dNWMA== 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=epAxFeTsCYoLZvYhuWzuuX5wZ3QKXW3RxkhG4dQUlAw=; b=3YvJoZZ5HTpOGtAjN1PV0dgkohdMPt85+dP5teKQrctRGdeVIAlKIpFELYQR3T51HyArmvahhI+R0ovCvkh204vRPOgTD+UFwFvlF2O6r/Q/rtvZypsTAaiiPULstpJ/fP4MLwo62IUHjpWgkxNYOGit+d1VLhpBQFsI3DZfTzY= Received: from BN9P223CA0029.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::34) by MW5PR12MB5623.namprd12.prod.outlook.com (2603:10b6:303:199::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.13; Wed, 27 Nov 2024 22:58:22 +0000 Received: from BN2PEPF000055DD.namprd21.prod.outlook.com (2603:10b6:408:10b:cafe::52) by BN9P223CA0029.outlook.office365.com (2603:10b6:408:10b::34) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8182.21 via Frontend Transport; Wed, 27 Nov 2024 22:58:22 +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 BN2PEPF000055DD.mail.protection.outlook.com (10.167.245.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:58:21 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:58:20 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 6/7] KVM: SVM: Add support for the SEV-SNP #HV IPI NAE event Date: Wed, 27 Nov 2024 22:55:38 +0000 Message-ID: <20241127225539.5567-7-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055DD:EE_|MW5PR12MB5623:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f442843-1de0-430c-f017-08dd0f36fdfc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|36860700013|376014; X-Microsoft-Antispam-Message-Info: 0yqcJaso/eRuM5KsS2Z6FMN9/qHyFCl1VN38xX8c7eNHxf3KTUQ7S813YbQ6UD5daU887qRjPu7PGfmHbHPN6FhYKN2+v83bSpSV5BNOddCaNDO74tEBjbHCJ0yCtY2oMBrSqkM7GViUVTm1p4+hUwIfjx45XHg5ZRW/IGRs0QMwGEehfHMM73CJ2KNnvj0+C6NEPGd0GVeb9WCb26HC2cvMHTmFh6IZsVwNkR89cJIzS8Y59KNiuTwUGplI6wd1UdH238Qen2WDKY+TuylZwHTTuJaVD8RXBSYzR2Suc9dvFGV6dv/a6u+NC+kbwXnfrKhqTH+fQgDO1De5ThL+DB11kU9N1unB+ao1oFT3togRf0tL89DS/NEPjXY8lxhGYjDNTdZy6snotQDNrYxDZ1m3ooPfnSHwPTWRVegPOwmKDNpHQRQzAW1ixfH7uVdAGNNRuitSyZcRalvsrVCcuXZ2X5VmoI4InaQHrEIZ+9Tu5SX1O3/bkLcXsc6w5j4bJ4JbPcDuNajxxnn9H9aJnplv/fJDN2S8P5AqHZFB5V6hp8w7D+T8IH9DitFGRdf+FBhoy5DJ5xIyCvPoViN71nDiMUYbjsV8tZRaJonbOQxF9U/BcSchlrnvmtLfK+X5dAdF82+PQPFWz+vJZivEmXdylOAR9j8homC0nSrh+tb1Di7CO68JNhcrrVJ60EjJ5iY/xOfWTzSPYvgxF8n6w0yM1NUAbTLkeb3fol9qhGwQO3NTlm60zcNNGmX82ZuhDQWEU0V4GLEkm/x0+35KAsDwKxVJgJoN6NhuBQadLz6b0AsSsBXjI7E6NdV14Z1g0P6uy7tuc8p3BzCybOoTQ4G7jr/rsCi/Rs3Uq3XunUeikoWTqyFKSZxQ2GFFpn+kjbRdjbO1W5RDiOL+THikYZIuQBR2/OvacmX/6xWeEvCsg9a3xjoHjIyfQ0pQtndqaExKN7WWhsjD7Q13Z/aBN8rxNvBSHZzL0N0MqbFmxCX4n15bh04fPZ00hlFYMQ1ZBQeG+uVW7h/NczUOrbE9PNudf7vr3Kf+HX4lJdgSawBrdTL0mrULZXJEqW5kD7UQ4ggUNgFUD908BQwTP6aAyKkv86Rskk/0ud8OClHwx2Q3Evre4Jyna+ojSlwjib72mcILK612jQACAifKs9yXiS1Yi2ewzhm01OnrriDGY+SgYKI4V+VFqihnHJYahbL4FXwYp68AgXMlyc3WSBJErJlOVIL25He/+Q5b11dAgTezvllIpixE/mB9NGjsys3OZPkZu9SH/F2NIXF8/uQWjBljpnA1uJakI4f9NtUBHvcHb3h05r83QzlYWRIJY/PR4rNYIM91ok/tkAv5eKEvb6/++jXaLCDKr9gA5ULf6CbjFF1E/E1MKIUONy4pdps+sHJaGSzfRfeyUbq8uF+ZL/rAqdYJmjBn+WKR9QTU0WAtKhdiu+HEDY77XtJMo6/e 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)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:58:21.9560 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3f442843-1de0-430c-f017-08dd0f36fdfc 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: BN2PEPF000055DD.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5623 The #HV IPI NAE event allows the guest to send an IPI to other vCPUs in the guest when the Restricted Injection feature is enabled. Implement the NAE event as per GHCB specification. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kvm/lapic.c | 24 +++++++++++++++++++++++- arch/x86/kvm/lapic.h | 2 ++ arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 7905c9be44d1..7a3a599d3df8 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -120,6 +120,7 @@ #define SVM_VMGEXIT_HVDB_SET 1 #define SVM_VMGEXIT_HVDB_QUERY 2 #define SVM_VMGEXIT_HVDB_CLEAR 3 +#define SVM_VMGEXIT_HV_IPI 0x80000015 #define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018 #define SVM_VMGEXIT_HV_FEATURES 0x8000fffd #define SVM_VMGEXIT_TERM_REQUEST 0x8000fffe diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 3c83951c619e..99a45ba1b637 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2417,7 +2417,7 @@ static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t address, int len, const void *data) { - struct kvm_lapic *apic = to_lapic(this); + struct kvm_lapic *apic = this ? to_lapic(this) : vcpu->arch.apic; unsigned int offset = address - apic->base_address; u32 val; @@ -3416,3 +3416,25 @@ void kvm_lapic_exit(void) static_key_deferred_flush(&apic_sw_disabled); WARN_ON(static_branch_unlikely(&apic_sw_disabled.key)); } + +/* Send IPI by writing ICR with MSR write when X2APIC enabled, with mmio write when XAPIC enabled */ +int kvm_xapic_x2apic_send_ipi(struct kvm_vcpu *vcpu, u64 data) +{ + u32 icr_msr_addr = APIC_BASE_MSR + (APIC_ICR >> 4); + struct kvm_lapic *apic = vcpu->arch.apic; + gpa_t gpa = apic->base_address + APIC_ICR; + + if (!kvm_lapic_enabled(vcpu)) + return 1; + + if (vcpu->arch.apic_base & X2APIC_ENABLE) { + if (!kvm_x2apic_msr_write(vcpu, icr_msr_addr, data)) + return 0; + } else { + if (!apic_mmio_write(vcpu, NULL, gpa, 4, &data)) + return 0; + } + + return 1; +} +EXPORT_SYMBOL_GPL(kvm_xapic_x2apic_send_ipi); diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 24add38beaf0..29c55f35f889 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -140,6 +140,8 @@ int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len); void kvm_lapic_exit(void); +int kvm_xapic_x2apic_send_ipi(struct kvm_vcpu *vcpu, u64 data); + u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic); #define VEC_POS(v) ((v) & (32 - 1)) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 19fcb0ddcff0..5e8fc8cf2d0d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -34,6 +34,7 @@ #include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "lapic.h" #define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_DEFAULT 2ULL @@ -3417,6 +3418,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!sev_snp_guest(vcpu->kvm)) goto vmgexit_err; break; + case SVM_VMGEXIT_HV_IPI: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -4193,6 +4198,22 @@ static int sev_snp_hv_doorbell_page(struct vcpu_svm *svm) return 0; } +static int sev_snp_hv_ipi(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + u64 icr_info; + + if (!sev_snp_guest(vcpu->kvm)) + return -EINVAL; + + icr_info = svm->vmcb->control.exit_info_1; + + if (kvm_xapic_x2apic_send_ipi(vcpu, icr_info)) + return -EINVAL; + + return 0; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4479,6 +4500,14 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); } + ret = 1; + break; + case SVM_VMGEXIT_HV_IPI: + if (sev_snp_hv_ipi(svm)) { + ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2); + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); + } + ret = 1; break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: From patchwork Wed Nov 27 22:55:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Melody Wang X-Patchwork-Id: 13887419 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2069.outbound.protection.outlook.com [40.107.101.69]) (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 6A42620371F; Wed, 27 Nov 2024 22:58:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748325; cv=fail; b=sr7M7xiOn5NEQ12bB/Mit4fcRNUacYIeDm3zpzt66ehgRlWvxI6H0G4ArVutDYtWP48ushu53UyHlR3wY89DY7FL03Os6mm/Fj/dLU8woWtLy1leSRdC7ErvEYcYHYW2hYUt9xYxswa76N50IlbHNVU6uH4HxmbSjrRSw2l8q0s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732748325; c=relaxed/simple; bh=k02qQbwB4qeqHAIi8WoCdm30xubS/qPPyzaEMZ3mqco=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LZMmbw3sMu11DZlCybeTHnlaylh/gnFT4PJKYJ5DTDS4foX2GyqCk8Pbln2sWClLDdGhDkEwWp+Y/Y/yeyZW42N0Bf9kaFD8Shdm98bkvVveCix7b6ReaOo6W0keZ5v0I4out4gb/8Kla/IjNyG5FyGabOiQCRwT1G7TbikOIrw= 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=lPWF/0/Z; arc=fail smtp.client-ip=40.107.101.69 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="lPWF/0/Z" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LSYXZTlgcTCV2YIHtKxXej4e+T5Pm+vBN2fUeX9Y4dQoIOUamthU0vv1BCLpbGyoBCMNxSYtCdukThjqFNUmJnbuop7MFPUK9f8M9X8K+BKZ4DpXD62Ugcz/6kiRxu9BF1qyUp2E1jKrZu+5BK9RNfcyt++/oPTn8YU5HiI0WNRd8LKsCHOWgZPB6P3bF16RBo1em75dH9S2YqseqNRbDYCmoxZ5AHeDZs9CmpymFalL6y8DwHpy4C1jUjre996Gm9DO9UY47fSgYh1eVdVxaHTne617eZg318iWUoJrGGc7qjkgN/GTSqO6w+MTgwuVcILh/eDEyf2yCbTBHHWoAg== 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=bgetm8luVMgzUT2SoVPewyHupRCIScJgEk/GjtcktRY=; b=iMo2AjvuOkB1ejLVZfIyDJu1S3JagLMQzUijglN57RWSIY8DVULhfIV/mBOQ5MntuBUVpMqXfTueUdtRfIBmFRbH+11rWUhGz0mxCs9GEYS5JKI9XHz8GLcDOsb9kOe6OMG7NPhQov86IBm9XU4qDm1YVQupTPV9gCmqcBvBilLhrsfiXfd7fdL7kJh4B2L2p8LB56ib8aZ/jWx5YVr+/jpieQ45yQL9QZkZNhni5SseH6d4+AuY7ZxtI0TvMld5Dg4FAK9hN5fqOSIUdXThlkWqTyKxwEpw4wbp/zWlzBAznSszxhJ2YA46CB5RzEB7XtxbUgopHHqIxP1hOldgYg== 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=bgetm8luVMgzUT2SoVPewyHupRCIScJgEk/GjtcktRY=; b=lPWF/0/ZNVppBYK43qxk/9ghY+3YS+gsYmUcqC5+D8TxP9uDUWIYUtfUKalbCrTe7RjhjJrkX8tUO4tSb7upzvOyLWX3hbPXj5cZN0cL/HY91/iH906GKYdGWiOcCZzTeOXWkhi8ZWWua1ffZb2GylBKh62zZ58Ps/BSVy/9OGU= Received: from BN9PR03CA0441.namprd03.prod.outlook.com (2603:10b6:408:113::26) by DM6PR12MB4057.namprd12.prod.outlook.com (2603:10b6:5:213::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.12; Wed, 27 Nov 2024 22:58:39 +0000 Received: from BN2PEPF000055E0.namprd21.prod.outlook.com (2603:10b6:408:113:cafe::e) by BN9PR03CA0441.outlook.office365.com (2603:10b6:408:113::26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.13 via Frontend Transport; Wed, 27 Nov 2024 22:58:38 +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 BN2PEPF000055E0.mail.protection.outlook.com (10.167.245.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8230.0 via Frontend Transport; Wed, 27 Nov 2024 22:58:38 +0000 Received: from ruby-9130host.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; Wed, 27 Nov 2024 16:58:37 -0600 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , "Neeraj Upadhyay" , Ashish Kalra , Michael Roth , Pankaj Gupta , Melody Wang Subject: [PATCH v3 7/7] KVM: SVM: Enable restricted injection for an SEV-SNP guest Date: Wed, 27 Nov 2024 22:55:39 +0000 Message-ID: <20241127225539.5567-8-huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241127225539.5567-1-huibo.wang@amd.com> References: <20241127225539.5567-1-huibo.wang@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: BN2PEPF000055E0:EE_|DM6PR12MB4057:EE_ X-MS-Office365-Filtering-Correlation-Id: b9ef6a4a-082b-4e2b-2003-08dd0f3707f2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700013|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: kTAO/x/IZVjoqP/GqQU4gqNLNjsGvnRgOfZxPYdEsdnNH7T+w8Ahb1/nyvV+4RFT7yOcMUS1iuUrQ1lE+Xi98Z2AkvfJMEB3DvRnj72/OuehfPWkQiBgCkoEpL9IJyA2Ba4YBquSa7VaCjylrpQIyCcXMO5oha0FwRSZtBqtVMqK+enEXuCPp0Y/HtA+HJpgIbyGLP4mJE85reuqSWtGJWwzcTs5jXvuOhZvozv31teQ2kNwH1OkEUdhN9tGRT3V1jz4NNtX1KxHJTVZNiqnBKOg9lkTXJLdTOz/ERc3PLvsSCzFOKOd8NZIG8wWVRj5lW5Rmw0oFHUq5XrscQ7va+baMp2fxED0kyRhx0VcPUaAbyjEmgwq++wYok4/4r9lwOEKe/1p/Swn/+aisqKMutzMyd2HvtGRP5jsf+g4AwQgSCg/8kDHsF87lRcGzwnBKlytSHfHw874TEx3iOC8WqFIi9F4FD0GTdpQP0oz+XE4oyaCxba4NZ2//m6eBQK5Ma0Iey1KlyBE2lv4vyprA1CaFhK4ULhhBJXsuc3Tl6pVPRaF10ziNxor4O7TEMZmvVyNqWlbAl2wsuJqbYZlj56Igj7SMA4+4NQl9gL7VhZOsfWvlrwNZyBdFUOFejp7u7Mb4SQt501K1KrwkX7eNUyPtgLHuPVbPexYhBkYAPddoJzfHyduhUN8pnD1Z7toz/CoMsCTg6C7BT+XBT/w3kzniSAGSQiwxaof8ensFMW3Dvh7LllyGwbXq/d0J8iZmh7+wBDBf9DYv69AZOyf1dZgL6BmA1fY5FJsUq2+Hj84NbE3GKJAtoECGSlUXf0jVMQxhQFDJDuTEEbsU2xJdcovSS4EE1KPsuPtn/wvU5KVXhOCKz/TKZcZHAUQUIJhDf0e9rIRAEMqEUElxT8nbwPOO0Df9S59bprhLIjw5CgLu1JuxiXTQfexG5YVWOe85JI6amqhKKqdmC+rkqYmLQQAfn8ZOsNpwE4ECNKW2HXhv2n5SF1fKSPF73sc2UCc9KMFzAaKQOWDSLAaA5NwWN8u2Ro0K/KXiHiBKAx8HO5ioNj5nn9PvgC9T2rVWGkb8xHupjzdE7tbC/b084BeNBUwjVEHqCQ415uiKx9j45xQaVyfFGCY7vYjohfJCqBSuqqnNi5kXRp336x9Wgk/l/NKt0A5CCMHu7ded4/YsQ4E9uRUGgm/daw4PElMaQKBavp6l/vu0mlOILoRPqgXKASa6JVjjHSTFvCjoCVsfqC6D6yMEuc791A7ghvKLW0iXn+i67skKBwu31xkvr/vGuPOmYy5Bs2FoeXKsIdjk8nSbfcjHJFFoT02sLdoxFbEUBqrThIPkPghA52e5UQMx5Q/2w6632zfDLGWlTObmTqOF3aoRSDocn9XFUrsWWnTKpOB1APVmdLJcGB0XyeAWa98Odqn6OXJhgKNCwuPMBr9SZkEKfsJ3js2BrEseh+f 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)(376014)(36860700013)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Nov 2024 22:58:38.6779 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b9ef6a4a-082b-4e2b-2003-08dd0f3707f2 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: BN2PEPF000055E0.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4057 Enable the restricted injection in an SEV-SNP guest by setting the restricted injection bit in the VMSA SEV features field (SEV_FEATURES[3]) from QEMU. Add the restricted injection supporting the hypervisor advertised features. Co-developed-by: Thomas Lendacky Signed-off-by: Thomas Lendacky Signed-off-by: Melody Wang --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/sev-common.h | 1 + arch/x86/kvm/svm/sev.c | 26 +++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index d96277dceabf..c0a409ac1ea3 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -450,6 +450,7 @@ #define X86_FEATURE_SEV_SNP (19*32+ 4) /* "sev_snp" AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* AMD hardware-enforced cache coherency */ +#define X86_FEATURE_RESTRICTED_INJECTION (19*32+12) /* AMD SEV Restricted Injection */ #define X86_FEATURE_DEBUG_SWAP (19*32+14) /* "debug_swap" AMD SEV-ES full debug state swap support */ #define X86_FEATURE_SVSM (19*32+28) /* "svsm" SVSM present */ diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 98726c2b04f8..f409893ad1a5 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -136,6 +136,7 @@ enum psc_op { #define GHCB_HV_FT_SNP BIT_ULL(0) #define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) +#define GHCB_HV_FT_SNP_RINJ (BIT_ULL(2) | GHCB_HV_FT_SNP_AP_CREATION) #define GHCB_HV_FT_SNP_MULTI_VMPL BIT_ULL(5) /* diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5e8fc8cf2d0d..d2a1b4304e41 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -40,7 +40,9 @@ #define GHCB_VERSION_DEFAULT 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | \ + GHCB_HV_FT_SNP_AP_CREATION | \ + GHCB_HV_FT_SNP_RINJ) /* enable/disable SEV support */ static bool sev_enabled = true; @@ -57,6 +59,10 @@ module_param_named(sev_snp, sev_snp_enabled, bool, 0444); /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444); + +/* enable/disable SEV-SNP Restricted Injection support */ +static bool sev_snp_restricted_injection_enabled = true; +module_param_named(restricted_injection, sev_snp_restricted_injection_enabled, bool, 0444); static u64 sev_supported_vmsa_features; #define AP_RESET_HOLD_NONE 0 @@ -3083,6 +3089,12 @@ void __init sev_hardware_setup(void) sev_supported_vmsa_features = 0; if (sev_es_debug_swap_enabled) sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP; + + if (!sev_snp_enabled || !cpu_feature_enabled(X86_FEATURE_RESTRICTED_INJECTION)) + sev_snp_restricted_injection_enabled = false; + + if (sev_snp_restricted_injection_enabled) + sev_supported_vmsa_features |= SVM_SEV_FEAT_RESTRICTED_INJECTION; } void sev_hardware_unsetup(void) @@ -4589,6 +4601,15 @@ void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm) sev_es_vcpu_after_set_cpuid(svm); } +static void sev_snp_init_vmcb(struct vcpu_svm *svm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; + + /* V_NMI is not supported when Restricted Injection is enabled */ + if (sev->vmsa_features & SVM_SEV_FEAT_RESTRICTED_INJECTION) + svm->vmcb->control.int_ctl &= ~V_NMI_ENABLE_MASK; +} + static void sev_es_init_vmcb(struct vcpu_svm *svm) { struct vmcb *vmcb = svm->vmcb01.ptr; @@ -4646,6 +4667,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) /* Clear intercepts on selected MSRs */ set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1); set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1); + + if (sev_snp_guest(vcpu->kvm)) + sev_snp_init_vmcb(svm); } void sev_init_vmcb(struct vcpu_svm *svm)