From patchwork Tue Sep 10 06:03:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797982 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2085.outbound.protection.outlook.com [40.107.96.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 997B813AD09; Tue, 10 Sep 2024 06:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.96.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948261; cv=fail; b=iz00pFhpIV0FuB3yIE55lTJNyW5hjzIscgDjmxL7dwZQdILDojhE7RP+u6HUxbYHmgwupNQi08HIeQv20navXWcpKBQVbonow98PExOlPnNei5it6OsRNHT5ZDwOOGjHoaLfxJi0QNTaf+JGpxhg71WXzGjelUvGkSVvAilRQ0o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948261; c=relaxed/simple; bh=QI8lcM155gvsN/0vpySkRHE5Uso3hpCbCzm1Za6gDHw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lDe66z+dCDNoC1GmnH8tWFSmhRF1FO+0zOvOKFOjzeAJYgJdW5T8brcFs8PGQh2ch4eJc26Eo0RFBWTXWal+hy5bu935JQO9uat5S6bRl+lIle3fqNocGvcc7mcwHNdVBq8nbCbpZPqIPP8DdeqCbvT5fcsfYjtIMVjeI/U7zrY= 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=KBrW0Kew; arc=fail smtp.client-ip=40.107.96.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="KBrW0Kew" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=x8V5o1WdjWKjKvfk9JzFo2knPV0L8cYJLuMzLTXh2pZBpD8J42N+/mYfXdPmpqaQRTzZWelGun4BgdtzJjnYQzoF1mFGkLrhUpQQORPh+Y0d/GsjhCk1DZLqZL3nniBH57o6TPF9tR5WUm4Jpkd3Z47JywmPyeblRI82aNnxvBibWG4GCAD332Qrjp43+WzwNAkpwXFfLyJwMie3i8k3WVMtimny1ZTf17O5QMraIIwVCj8hookkbFc8Mdc5/rs/q8ndxF/YdcfBSKvNToEimJzEyV8v9sSvyYViNVDgSKsf39Wja8jZuqKKcxT30m+DL0pkHuTPaP9lM7kQWBh6CQ== 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=tpm1292gyqgs6s3vVvds7fgnNH/TOdvo/ndkaO/+jLE=; b=VyGndk0seSXTy5C0GcaIwUVPTfZUrkUV0g029vdkXQ6Yh6fjTQC2/VAJcIalmtjp9/qdaj72LuO073OltFu/AflcFqcxpaVyLuDfoaPnji1L/DuSERAymq1ozP/j3y8PgwkSStwJvAouh5DnBBv2fIdTAMOyU9sfF5jw+RIaJBk70MwY1akcfzsdsqn7Ej8hIYcQmF09NPGP5tPIysNxZabdyL5Mnf8oIPC/lN863f6JZcmpSOIeGvbMiIJ2vp0Ol1eCWbWH1NbcStStykFcbjdc62JlesO/CM6NGeJbXaFfdK/bqjolF7GeQtTLBS6JRBQRvpPEGvPtw5sI4sDhuA== 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=tpm1292gyqgs6s3vVvds7fgnNH/TOdvo/ndkaO/+jLE=; b=KBrW0KewhOKhy/s88we4PFcda0ZFFmg+TQApbnadHq6xXMWH0UutZc/iCTifmLywS25l+CJs3osoJAF029ULBW+30yzSrdL4my1qNmFQWq76rBUmM4d/Dhah7OndSkMiAXtgbeOHjR++vSfGRtF9Zd2PncJK5aoaENyxx2a886o= Received: from SJ0PR03CA0378.namprd03.prod.outlook.com (2603:10b6:a03:3a1::23) by LV3PR12MB9120.namprd12.prod.outlook.com (2603:10b6:408:1a3::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.27; Tue, 10 Sep 2024 06:04:15 +0000 Received: from CO1PEPF000075F2.namprd03.prod.outlook.com (2603:10b6:a03:3a1:cafe::e5) by SJ0PR03CA0378.outlook.office365.com (2603:10b6:a03:3a1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Tue, 10 Sep 2024 06:04:15 +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=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075F2.mail.protection.outlook.com (10.167.249.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:04:15 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:04:13 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 1/6] x86/sev: Define the #HV doorbell page structure Date: Tue, 10 Sep 2024 06:03:31 +0000 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075F2:EE_|LV3PR12MB9120:EE_ X-MS-Office365-Filtering-Correlation-Id: 053a64e3-318c-4101-bba9-08dcd15e665e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|376014|1800799024; X-Microsoft-Antispam-Message-Info: 3dkERxObV1ad87RHiOv1BBEpC0QVuTfdPYMr4VeF95WKYI9bF3dwkDOHPLLtViixYxAsYex/2nWbuBWRWFS0e7F41VkvDnQTEJ2/jeJdGy7vxv61x8gXbji4DTgF1WS1VEeIDiJSjj2R8cJ9+/MaMsy+DAcWHTROw7h2shVT8mwRzh+4YiUUhw4e9MoeyfXK3Y5X65tDL4aVkVQYjTenTK+yg1xtGfh6KkU2Pfj9gtm2faT/pWdFDu1zNteZO1Ajl/TWS1flumXsPpseWv2JBN3QpGCH3lggUtMxhvvrFmf8DRNp97NlnvrvEAHqevaPJ+Clwsk1JdlikshIB02d7Av9d2JdxURdhlpM+5ox6FZ0ySe6SH20OnOhomA7t7aZ7gdZhm02oXfQ3SQYVKNzsyUuHw3y0e5XcKYr1z/nTuU3/cfpDjfmYJE7ZmHM+8FwvqH6Z8B5VELSAsXxRLyBWwX7fO4F6cllw6SGhMboi2HxqZyVAAe8TyqL4G/0UVAa/Dhwxbuq+EvGSbYcVWApEKTenFzVLsjscYVoywyv1fmLxJdtUnrq35HU+5R8dlZc2QrTc0fsw0i0wZiYpL68maOtQXzvRamzQterrsqnPeyjbUrfpzJPCIFhg0w9D78ViLStdKagh7qQfOOMp2Bb+oc8h7ewSWTQBoYYbj8XX2I9H0Wmd70L73B5G1J/mcfRcR/Sa6+dUoW6A3SSTmHBp+FfEeuW2oYDp2XoOrmmrd5X6Dxf0j5kAPyFPnEnZFOLKUmPklm5WDAuPcXHLga/EqbMnJC+E8Bs+I7UlItd/pC2xUgudlTxfM62Pv6Dii0IUQWvxnblpPijX3aYvAEb78n06QCvZ7Ftjr726k6VgktNU2LNwTYY29FdwkNdZEjU3HWrJ1pQHrjT1A6HvFcMi8vNZaRgfygqUhgZqbki9GMyjbDWcF7wH/PxgqFXYmHwvyvQnD7IyQtz7WWR+fV+QXdnz6VjdMIxVo1so1JASY85cIGcR+byPkqinmXMJLX9WeP5pm7ouMUl72FoJtmO0UFDifsims577NjaFnLG7UJl+/LJUCLquFw0QCJOQ/FW0o3Z8iwb82hBoz1INh9mmpaUAJYy9CU1mYivIi+VqRTalEIbp1zpZuauEkMrlDjQBBRHL1J0sKp9DThwcninQ9Ne5625d30uv92aLSc87bRv4k6RL+A4rHSlit1kZGCnzxhwQA0QgKo+3yDStJH1oWkPve1lnSHLHybFAi+9YtuieJsbUBmz5ViHS1RHmf6xg0iDY0Q+hwSwgHURHDFJU/mIuW6iuc+MnBmAK/H9eh8Vn8DHjjnmjhh3RefLLP7UqQSgeqW5fOLAM6j3oydFLcOssh16keBdhOlDbdtqsGPTCx9Wq0qPWYUWMGkeg+VswJe9UufTnvM+thZ0XkusKlNPoDaf55B4XOtsM+KolB4+dC6t3/qSDNgKPjRg/ESw X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(376014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:04:15.2508 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 053a64e3-318c-4101-bba9-08dcd15e665e 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075F2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR12MB9120 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 | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index f0dea3750ca9..2b1f4c8daf19 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -516,6 +516,47 @@ struct ghcb { u32 ghcb_usage; } __packed; +/* + * 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 { + /* Interrupt vector being injected */ + u8 vector; + + /* Non-maskable event field (NMI, etc.) */ + u8 nm_events; + }; + + struct { + /* Non-maskable event indicators */ + u16 reserved1: 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)]; +}; #define EXPECTED_VMCB_SAVE_AREA_SIZE 744 #define EXPECTED_GHCB_SAVE_AREA_SIZE 1032 From patchwork Tue Sep 10 06:03:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797983 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2073.outbound.protection.outlook.com [40.107.96.73]) (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 95DFF166F00; Tue, 10 Sep 2024 06:04:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.96.73 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948281; cv=fail; b=DpmZUQ/NZy5snSIQ0zd6zOxE50yBMmr4wwfX76TH/x8QD+lZbpiNaE1q17P2Viww6KWlWvYF1vbCEfbO6lrmFMP6IpdqijAbnVBB4t2tUTF3WA6TZZyAx04/o1hUMhss4t3zSPokLSutp99yfpw+SpR6cAuTfL4YJOItg2KszKU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948281; c=relaxed/simple; bh=HM0Nw/nuSh4b4fFMRD90TjYXag3URCUYfwI0v0CRX0I=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s5PqtPZAmtBRkQq6/v2EPX5Z9TKacTSl7DpwktJ/KRKnvi2RsLb4EXvNlnLQQOvYJ1ObyVA6d3JXVKcWQcKr5fcKN97chfvHDtZ/glXUbCH98tJCbQqsCLZaq8RCZmBYoy279ziHtyMbJe5CzcjWi0/QzWA/aDv7KddwyOx9Mh4= 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=hNbBn+bR; arc=fail smtp.client-ip=40.107.96.73 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="hNbBn+bR" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KNSUfX4kCaEY+vh33vGRcT/bbORy6xsvnnen2cFIuAah+Wb7cMKXgteKEn3kBsJHPqqnLdge/PtCkFIAqpUPSqf7tuZxBQX7eY2hlhb30jWj9gUMc8Ur+EqzHdFvgwRMsc5zVbsnPrybulVl6WVP6h4a9yBhq+BKvrWXBHbqd25vUPwwfR+cS36FreX1LR78SJQfqpYJArmstj7PrYA2ybB6RL+UkYNO0HeX2OB1GWzRlqxXjogDHeCgPIN+iLcO2h3T344M5AeR0NFy6nVYuqqtYwXCGHmtCKUtsk4x/uBTI9WUINT+kxjWTuC7jS+Zqp++R+r48HAeppU8VU7Yjw== 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=ClOfLecC5uGwl0NQkTC/DUjjaa/7TNWCiWpJu3L8ajc=; b=YLRqnp+RAVe7XZVqpKiJkeTk9tSuJnn3XtlmBR1tNqzvOAmBj02ypTLtBwrXLOxTz8JLTn6n+VzOaDA8OJclLqjA350NpzG4+u5/9yLNFVPmA/EDS+JlFTCBqnxO9FUe+GjTmUBvEVutJt7hUVJeidif6gjXja6yPBV6eA4Wsi3hGShxmPRwuHkm255LyxZNz/mPfCS9WD5vG2t8+6gMTmG6tSJWDL8647gi9+BEVDelYAtzb94RFjoia/oZ/lK8XXPZnugsk5TFIpWA9ypxkd2jY6PYNcDZIAdorvHqqd1X64OhKCud4wkV5+URbpuxWODz7tQbaVgLYqthNvXtIQ== 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=ClOfLecC5uGwl0NQkTC/DUjjaa/7TNWCiWpJu3L8ajc=; b=hNbBn+bRCh3b9rWt+j2SXk5+iSrem+iqFuZYqFMm0atRSe2/1oAHmBQylFQQHarjJz2O2uoSk2mz68gYl0Xi25l5vq0t2IqOKZZpiDu1nazgEkB12h+jUer+7v+RR9t/lZsz8+MrN4irb/OxNUlycFxmONs9hyNzmGP8at2R9wU= Received: from BYAPR05CA0035.namprd05.prod.outlook.com (2603:10b6:a03:c0::48) by SJ2PR12MB8134.namprd12.prod.outlook.com (2603:10b6:a03:4fa::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.25; Tue, 10 Sep 2024 06:04:35 +0000 Received: from CO1PEPF000075F3.namprd03.prod.outlook.com (2603:10b6:a03:c0:cafe::5e) by BYAPR05CA0035.outlook.office365.com (2603:10b6:a03:c0::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.23 via Frontend Transport; Tue, 10 Sep 2024 06:04:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075F3.mail.protection.outlook.com (10.167.249.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:04:34 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:04:33 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 2/6] KVM: SVM: Add support for the SEV-SNP #HV doorbell page NAE event Date: Tue, 10 Sep 2024 06:03:32 +0000 Message-ID: <7a04e3f07965a09d79bbcc067ed943ea0a4c7b7c.1725945912.git.huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075F3:EE_|SJ2PR12MB8134:EE_ X-MS-Office365-Filtering-Correlation-Id: 89b76146-8f53-4e75-bf2a-08dcd15e7217 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|82310400026|1800799024|376014; X-Microsoft-Antispam-Message-Info: 6d0Num+t3+vY0KHSM8NzBMPkncCyd7qxQbLr7jqljEnoTDrcha1t1AUq9Y+qIq9kQTKG/IHfZP8teW2+eYha6bhz9uVne0SJlo/cJwDpZDAiW6qnrmUs9A0l1qjyBlivTczQ5GvXVw4KCTk86Sx2q9/zG83pvDKEU7Bt3oibb3HHC19bzPOHYGBgCupOu+Z6JRuZ89vZ9ascqfWg46i/Hga22SlfAY/Yxm3NY6dkVuwRKW3G0+hxifxxZ4lzOkzm2pL2dSPWdVWEeVYDe6vzGxIacszi3i7atd95r/RF7LHOnWeV0Y+57nqg5ZhuLMlHh7fwjVSh7kbLCoHOxf190pVA+U4rX2d9t4M6t51y3h4WZthf8TlXk4dk1qs2a+t3rFM81Yqnh+neTj2H2J3opG02UcWQqcj3kxPDbNGvlBFSXIojG4SYm5QnWJdj3H07is2pPxZYObmPG4bwb+p7rMpXL/EGq6JMUZpKW9RpDqzkopeaa5cHt2M771EjCU2JQfjsyQQkfoflU4Qw0VulzOdc777FbO8seZfblWzWyy/nqC52Qd2pS54z+yp117AXq3kcpo4L1QAWPRr66GZsBIJK9sV3vC8GG63rtNCxtFp6ZV8x3eI6YN6hTsDdDe0NaU7yJEfYNWxVoKJUCWLd5wAtOfkOmJMIlmMrldCSv6s/AZWw5dY99Xk1RZ9yCC4b434qUnE0saJERxgXfwGNCpTy7cjg/OWJVqL1/C0qdb5C4touRVuIlDWjs3kz/x26WkYErXpkg10vR+xLZRxaS1Qojlyk/PkYKW7B2HEurvF5QAQVKwwu3Dpu4GXVItiHlSWzTDMUT9XfLzfwBwravdn4T8lg5Z4NfrX1oMRSHz3Nl9RP3s+lN7l1MNLpDOXmskQRpQmXLmiCIUHvtupk/gLfRsMvFBJcoWQa8MY++6ADqCr7un/n2FoRBpQu2kJhoSW6cG22BS8Dlmw7rSVj9Pw+lRdiOsXuMQWvVFu1NvTwa/d02me8MljEeibCXoAxO/xzQrOknhg3kF3tiaTLShQMoZ5b8GX+7m05Etkr+0hqE4p21wsSBxL1vXmcuzB6iXuQIEUnm3F2gndxf+WDZE6nuFEl76othgEpXowFG8PsiFi8jz308MHeIv9iPdnsdsybqpz6eaOvYgHCu2nX9tINseCAMI0NfF23LUL1Gp60u4342LMhwH4ibw9HOKtYvLBWlRGxGhOHdhK1mcNk+dflIQH/1XfVUCysbTSFoZEGlPkZ/g6++e5EOk1W+iSx8MIpMV5qH4xfYDv4pNpxMMcxTXzHuYPGjEbDNgF/8qWm6ecDAM5la6tqTxv0nqO/CM63E2khso7wBcYdrDDHPUYAgz5OhcF9xkjsB6Ax6tNUKtVyEDnx+iXI6AsF47KxOu6efiy8R4ZYJ4K+fyIhkenBtv3ZPivniST3TZyfLmsGpVeKFDJ9CEK+gnLX7qtx X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(82310400026)(1800799024)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:04:34.9338 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 89b76146-8f53-4e75-bf2a-08dcd15e7217 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075F3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8134 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 714c517dd4b7..e65867ea768d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3410,6 +3410,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; @@ -4125,6 +4129,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, true); + + 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; @@ -4405,6 +4469,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", @@ -4572,6 +4644,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 76107c7d0595..f0f14801e122 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -225,6 +225,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 Tue Sep 10 06:03:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797984 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) (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 DF8AF16F27F; Tue, 10 Sep 2024 06:05:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948307; cv=fail; b=UoSTIF45+7gPrDnnygGSdaf6jXeKgk8mTIM+Q3DOirFFxQb26weoT421ew9ghmWqAogopOKILDgHk57Z5xEsIWZ6UoijR0Xq7kFneiWRNPvEJG90UjsS8J+OYK1QT4QjSfQcI4/unFNvlCDesS0LRNWCknpGJON0me9nMvVuhvU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948307; c=relaxed/simple; bh=/iZcmVHvLp87cV5AdKdsq72/WWP3MMEvAOMYhuHyG6U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lclc48LjJdmlHrPXrXbgXWg2ov2kbXbjw81zIvTJFEvTlAW5df1fPEJYB2Uxgf3nJ5o0cB/XXlb/JVxZs5mvpETT7fmn5pI+eAW41cpNaiFEE3HKOkptI0tiZOS7pA/cvwgwoaCwfMgsmrZC2/JmOxR/W5jHWuFFLL1E5Cx4Wf0= 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=bM/E1Yc2; arc=fail smtp.client-ip=40.107.236.40 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="bM/E1Yc2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YJF0dy1//Fucub6SG8iwacYJHkNYI0bXyvX5MopjBfCGyxbKSg0LIV5P4tV5X6fKMu134FY/WJ4UqNAYmpY/xdHmVTGssWaC8maR0T1OrrZqqINUKTuGusGsW9+6M70th/Ex2G6HZIe6dT1dQhjrfrJs8dv0QexpR8JHQzZIkysdbxFd3ZD2HdesaV5j/1x7lHUN9t0tK0Ynh6Nb7r8//bjrngHEMhLY0MOf4r8TO4XumEwL+RSj5kjM/eCvexlhssj5ZlUk9fJz1jfxeZwN1masb5tWn7NDyGeZL4Ep0+swmRslxKKHW0yFYW7eOSm/HOoHTGXOMZ1x7JDwNrbOow== 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=wgBrHR3vovwPjkVSH4po1UMbWgN5ps6EnO53x6EYXKY=; b=AybOk5deGXUYicxQThtWxrC11Sw60enbxJB4XR3V6S+GkJUAB9BvwxUBSEVaNLb/zO+etQNQFAlSaiWQ8g0UcLUApyeS7xEuym82UJMwoweUcgENoAnNMjsTpLnqSaus8FC7j6y6kbhOO1cYZ955tN9j++KeVCalpQVkdaR6ucTcs85+DGeQ+wlKhvk+Z+kBlEgs75Hag1bsBVHjN1ORcVfG99WPhZ2haEXUIb+mLbxKxYh/GJuy0szRoWiXbDjP33EZ5uILIC4zTMnbmeHN+BprhYwk8/u7XVdS+6yV0G9d1z8/MQ0OldW5Ildye70+NsbQJCKdh8ebEQQm35m+NQ== 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=wgBrHR3vovwPjkVSH4po1UMbWgN5ps6EnO53x6EYXKY=; b=bM/E1Yc2Ac5i0XeDFeUICDsm1Ufzc31cioWYsOso1rJQcOmltjYi/sIbrX55+fVTR99u/ZS7kzfSco4y1dCFL8kHQm2r/Nl0DmfyUyNhwrp3JhwhGjUmIU2ihxoQHnyLBZ7ciFm5EuvxzRdyRBKouIfi5jx+RkH6fU/msTXK710= Received: from SJ0PR03CA0389.namprd03.prod.outlook.com (2603:10b6:a03:3a1::34) by MN0PR12MB6080.namprd12.prod.outlook.com (2603:10b6:208:3c8::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.25; Tue, 10 Sep 2024 06:05:00 +0000 Received: from CO1PEPF000075F2.namprd03.prod.outlook.com (2603:10b6:a03:3a1:cafe::e4) by SJ0PR03CA0389.outlook.office365.com (2603:10b6:a03:3a1::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Tue, 10 Sep 2024 06:04:59 +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=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075F2.mail.protection.outlook.com (10.167.249.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:04:59 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:04:57 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 3/6] KVM: SVM: Inject #HV when restricted injection is active Date: Tue, 10 Sep 2024 06:03:33 +0000 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075F2:EE_|MN0PR12MB6080:EE_ X-MS-Office365-Filtering-Correlation-Id: cd37f508-6bfc-44d3-4cd3-08dcd15e80a9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|376014|36860700013; X-Microsoft-Antispam-Message-Info: MPMua0iO4DU5KE/+vwYz60X0NKHnDJp/yxONfbd63zz/MzikcwhnfWWrgTmRmhSByMBIC/65BtOMJyHaE/AEcx6u3wmg6GrPe1ZhsPgFHsWjI80OiYamE+6IfXhnitYv47OFtf/vWKAQeDbcwahXnhI02zDCU2PB1DlsdPbPWpLOwUNhLO8G+wXvShjbxd2h6u9FCx/mnHbJEXAPS5aHRWGxKxXzs3LlqUvieIyIIg8deWpKk1I9m5dSGAbGMeogAtKYIz0BLaSqIbq4lQwcLjnFHnFnl3sGZqI5cfIfohgQvnU6a8owhxjwx7cWr484h/+2vfMBqSMTo2UT9U4oUlNuS2p11nq8lEZrIfXeGybms8J5cwDCwX4pPuAqACsdoew2Qg53zBqdeJrmj2qc/DJ9Z6UmFR38dSzVDqqTjdX+vFcCWe4ZVuXFos5N6/xXT8HQSIml4pt53fNJk2+ApvVIssrYKeqm6gBPGLCf7qEvZvQli71Ts+nVHxhO9AjS8OXwBmtn2HHQlkX7dgBMEXb85qJ5n9WnieKX3klgfC8CdhUTLGsT9XQmfM1F1RTbOGmh5YFvWUhp4z1ijdfuvi8PV/KOVxTgFlaolOScHU2mF6EzcPIYARUqjqoSqaNgoWXIbNviplqcBPgf6IMbP5NKTfAYUcGq4fszR8q8eEN8xT0w8pkCi47Fx+euXY2W4jTBpc2KweExohmPj0jyfYK1WmvMuEcpIJXKFuvMoGD1f3U95O7wQ4zHlY1GENCNcmSE/0gtNz5I4eaij1r/Kn7BSxO5MlvBvuTVEaKlCN/GKCJ2WgMJOe2fvfNUndMg5cO3/5/CofY/ZWcJ5dAyhSGD+HQVXOJelPP7JwhoBVokcO8K6pgBv43dCnwvM03NBcbYylwG08OEXWUNt5+Vg7BQh1nk3ljjXSDm9heHa17vAJbW7/dsGGO2WHhxQnw+joQ4jYi8nZhb4AWRcpRKkbOJQRjSKo07Mhn191SRBhlkDnZChcQH4nu8w/fkcancb4Hf7RuIy97+oBB184NhkbgxDZ7Y1AfR+dsNglVudOECd2pLOPpXLJIPOcTsPsK9Y/6xgE9uPjOGcSCx3nNBokR2G93RYW18WePquNlRibjx/vTUSfZcbNPo+mZC66jX7FiaaOS0XKmOcorPCConVuRYHYiB6+dWhlAMsOQlFaDQ8CBQZrMhk7ATCR+dTXaECgdQZrvDMhkLQ5rVHnOU8bcPx2MbA5dKMwplPKTpnzVBaU18uj5Jkz5KKHNZuz1ofW3xA0uDQjeKXfuSuutG7RusUetzFu8jifCy8Rj3T8/YMb7+zuXrE3ThzQIy9F6/V2eAMBzT67Y/c5kxMNm7sVcmXDrSLtDV9M/3QnSKoV0fu8c3cVC0Ak3gVxgC+M1IgoBhvDwqz8I/DmLAMe/FGGBjRM0/u1N9WIy/xodU5Jj7IVGr1i+k1PS0D8tv91+n X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:04:59.3758 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cd37f508-6bfc-44d3-4cd3-08dcd15e80a9 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075F2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6080 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 | 153 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 19 +++- arch/x86/kvm/svm/svm.h | 21 ++++- 4 files changed, 190 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index bf57a824f722..f5d85174e658 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 e65867ea768d..f7623fa64307 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5023,3 +5023,156 @@ 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, true); +} + +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 bool __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) + return false; + + hvdb->events.vector = vcpu->arch.interrupt.nr; + + prepare_hv_injection(svm, hvdb); + + unmap_hvdb(vcpu, &hvdb_map); + + return true; +} + +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 %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; + + return __sev_snp_inject(type, vcpu); +} + +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 ((svm->vmcb->control.event_inj & SVM_EVTINJ_VEC_MASK) != HV_VECTOR) + 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); +} + +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 d6f252555ab3..a48388d99c97 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -447,6 +447,9 @@ static int svm_update_soft_interrupt_rip(struct kvm_vcpu *vcpu) svm->soft_int_old_rip = old_rip; svm->soft_int_next_rip = rip; + if (sev_snp_queue_exception(vcpu)) + return 0; + if (nrips) kvm_rip_write(vcpu, old_rip); @@ -467,6 +470,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) @@ -3662,10 +3668,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, @@ -3810,6 +3818,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) @@ -4128,6 +4139,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 f0f14801e122..95c0a7070bd1 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -41,6 +41,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 @@ -751,6 +755,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) { @@ -781,7 +796,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 Tue Sep 10 06:03:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797985 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2076.outbound.protection.outlook.com [40.107.223.76]) (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 4E790166F00; Tue, 10 Sep 2024 06:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948344; cv=fail; b=lNNg+3BdXPaxGYRwOagsDjY9+Xru2Q6rgsMdwJyAGmzrLLBNWOllnwKn3w8P6oIsnvtVvqUGNbX8GcB08sztWzwdPsIj27esSNU3YYe9NG8qLYWAYJSRDHldRtC5XUmEKaJ4YBtnSOEjh1XJiKIzmCI8QjUp1jdU9eE+6y3KqYg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948344; c=relaxed/simple; bh=1RvH64giLzsAFCBJEwnxMeF9iIP+bM0o2di3sz1EdXQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ltKZtmIBTK6MUm/qtijqJUNMtrk08nYYGK597Daq7p6MGQ//aT7yigK0xPUkeO0BNau1Ld3zd3Z/VZcJwubhxswXcc2GK5RFeJKUWMahlyXyZxZsg5+qvzkvCfItxZgZrKgWnLJ2WFQSFunHahopItkQUo3nyA73OoSjecJ0RcA= 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=3RowBa7L; arc=fail smtp.client-ip=40.107.223.76 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="3RowBa7L" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qLWCIXYehhnsJAl36EI+5YY61vEb8sBelCaoqKAeZSccVfQ0Zt7CN3dgRA+aFZlGIXliBYwEbmszZIQzw7UxWjjxyJL7YC0jfzwK4Ct8jneSYuNZZF8QdHdaUyKpVjTLr13N/L0BF5Q+xpd4e+vC9jMYw5hijkkfqBPh/+kQ9qTdQMKi/f0Zr6fXDczNPyvdfAnmagfI+3kQDINFzM02wZWFYY740aQ+V+y7vYhEqgF6snLkB2EUpYqIoadtbGto5ltqC+Gic0JzvOp9g2es3ibeYmj/sy9TANs3fs2AyMWXegE3Vipez+DrK+eB/grAtQ/n5Z3TuX8S2tNEvd98lA== 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=mXskL7azb5JqpARC1VesGy6K3lPsq/FoQJe8AVkcByI=; b=B/DX8DIX3V2jc5mSXiXeD6jPONCSrnOO4MeErn8QyF2x84Z1fwMdM54155fw2IGshlbGdBHpbAR/tPV6ehqFSVQw8RdfDWUgHdnK2TfX9uTTQhiH1PBtHI9clLV5IMMDYcO97v9X04/DS6t+BS7ZjSFw+8jqH8CDLtbQKMtTMFG7/G0GwcY8LSxfIdz7RHq44dC8PK4fely+1mgxLAmv8t29v0P56OF0ziCizOVC4abb86Kuz4C20wqJh+R2RRLIpsKa7GexajrFow0huo5y5cNwxMx3MILhGWSqpyx9d/FcgczbbHqSDiB0YujvNWlO/BIixaN9tNtkBVVMsc0bcw== 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=mXskL7azb5JqpARC1VesGy6K3lPsq/FoQJe8AVkcByI=; b=3RowBa7LM0Pbe9x9KFb2geAqppis2TZnrQbCM8vUZEBXw2LOgLyFnBs413KMhCA1I8kvZ+hvNsJOxzzHEmLf6u2/6RUmfXTdKWOQlqFrrfgMfgPSzYtRxrVtzBOcNYHgHs6HP9mILpOYLrmYKcN9HK1l8yj1n+ixh8n75S5TqK0= Received: from MW3PR06CA0023.namprd06.prod.outlook.com (2603:10b6:303:2a::28) by CH3PR12MB8459.namprd12.prod.outlook.com (2603:10b6:610:139::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.28; Tue, 10 Sep 2024 06:05:24 +0000 Received: from CO1PEPF000075ED.namprd03.prod.outlook.com (2603:10b6:303:2a:cafe::e) by MW3PR06CA0023.outlook.office365.com (2603:10b6:303:2a::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.25 via Frontend Transport; Tue, 10 Sep 2024 06:05:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075ED.mail.protection.outlook.com (10.167.249.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:05:23 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:05:21 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 4/6] KVM: SVM: Inject NMIs when restricted injection is active Date: Tue, 10 Sep 2024 06:03:34 +0000 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075ED:EE_|CH3PR12MB8459:EE_ X-MS-Office365-Filtering-Correlation-Id: 9280c4e8-defd-48cd-2b0e-08dcd15e8f08 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014; X-Microsoft-Antispam-Message-Info: GQ4SXD0aqoGcYckGdtczib2+hXBFBTScYbEOQGQ7zUj55tfiBUk0antegaGiiBG7k05v4xdp00MYRgKW2zdbRSkTLDMJBZU530kC7gciXZvMUtDafO1/vYUHu7oUhwkFtb8eHJhgtSy00DLsrVYufhdypY8yF9UG8DskJjMGHeBdWXpwfPtgd7JLMHek2NG7kchGeYlKUhXa9SUth+JgRuFKPXOsS2nf964N6XOkWCxyBoLN2FP9I0UOJVJeWj7viDZn1kc2gumF0qHG759m7C0aNC4HLBTo5hOhyZeaAq+QuevJ0Rxo1/3UoMEHtwuDAftnkNRTvh+fP1eytTRfse1CXkXzk/WkoRtqvry1+5ECvBwm/nxMe3uEfIkaH1mlkzyhff/w43jPEIQQOJoQkJXUBHp6A8UfXYZUzDbGtNsu2pTdHfx8u0ZfY1Hil8rph8TKehO9xQDhtSkvKKGUGSAoZoniF3IQiF4g+dTQ2EMG3p8lMNOgUYx3r+p1qD2y6ZOpIDDWgC8Se84gZvforBDAdTM7SmUDpWPM/R44IhKEt+dLIICtfcKDfDg3sqQkki7LvL0GL9krtLjEiPkv7vHDZRbxyWdZiNkNRseL3OnJdLJ2cGk1mn1keThI0Wn7giCRQZr5q6SDeLPutOeg/2mxXSwXK9Rt3l3g+nVoIq6Z6yOBudjdicI89a0iiZ10h8AlCfna8Z9d5N1aFNt8BPwdo39TMUm2GuG6rQpfqvgJaD5F2l5k38tIsIL6SweMjsXXxL9AZIzOqJSbmTBxgGOyo6x8rfj1gYEoUp9v/weI9S7tvZ/EKXmwIxMNzYzevD/7+Le5ZPxnPZhhvccLf4UCdoXySmYNntaV3RaLv8yARJ/HVGKb7v/gX2rJ0Wwta6bsQMPpJLc7J+KMfY5Y4cEcaLTZGhtwpOJ+wdEWLrtLQBqFuTgp3SSJhkP6NilORds5hgzZjOFPRJkjVPcUSz+cRnihkFjFsGSEHqug90WMmWMQtNUpRevBrmpYMWx6YTZYa3M+xLx69EYIq+9CWRuLzpBlulB759druNs0RJxmszHtQIVH1EdWtxbK2erAHG30sqLwzIyOOZIOr++9WXpPEjMTO2LfwX0NwzV2i6WMtRlVKNqCIETJZRlOvEXa7SuRJ0ItEcnwLvKhdALmU18LptI+0EOVDJmmx3Q+HNOitk9IRWIleR4ATiCRp7PMi93PT+00FGo+k89AfV49rkF36G1PD37NtOLWkXTe8hpji8kupvSYF9NXYCXohYwLWuCiV8WzWZECudDrsYrwaYFEPrl1cKWanCsR7CPJQ7tvXiDUZ7uprdFnc6d+yR9q5vPiejSp6hLVAFt8ej3snBlLV6ESCqqPuzRrs4J9a7v9ebwXncNKKYv5oWXie085LVGta3mT+dh/A7Rc5S7GdXNHFiFGaAfik95citNq7JlgmDTha+IYnz5dRB92EcoI X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:05:23.4701 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9280c4e8-defd-48cd-2b0e-08dcd15e8f08 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075ED.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8459 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 f7623fa64307..9102f7e39c52 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5070,7 +5070,10 @@ static bool __sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) if (!hvdb) return false; - 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); @@ -5148,10 +5151,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: @@ -5169,8 +5179,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 a48388d99c97..d9c572344f0c 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3602,6 +3602,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) @@ -3616,6 +3619,8 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) svm->nmi_masked = true; svm_set_iret_intercept(svm); } + +status: ++vcpu->stat.nmi_injections; } @@ -3786,6 +3791,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 95c0a7070bd1..f60ff6229ff4 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -43,6 +43,7 @@ extern int lbrv; enum inject_type { INJECT_IRQ, + INJECT_NMI, }; /* From patchwork Tue Sep 10 06:03:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797986 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2047.outbound.protection.outlook.com [40.107.243.47]) (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 ED56D175D5A; Tue, 10 Sep 2024 06:05:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948347; cv=fail; b=i7kZKo6O4w77ggPw5UeEmAduR9d/uOSS+JOvbmeJYMeuzVym0TuKntWAMBRIKUHxCUwv7DjAkkggZio9XJczIbJQwIz8tAJ4muvvkPfJFGylI+3rmeWkDtLdwlxs3Z4zaIiCIoTel1R1jFeGjcNugPkpgA8RUuLbNusI1dzWTZc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948347; c=relaxed/simple; bh=2nraB2Z3/crlRxkcbr/hMqKCOwwWC042q2JUz7F0PT8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DJn0jJnhfP0Y8rEveT/JlXSBGRYcwLJOvDu1AnGXEMIEZftylZ5A6L+luItwcmrqjI/ojORtSR+HfEsyIoEOaVlbrqPxy1fpfTChOLWNMbpOd42JxIClXPviv0d4KsMvQxeFwpZGbKCGL0ePLPVVHgUn7e8bUXA8cAg5tIyr5hw= 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=duaWbJN0; arc=fail smtp.client-ip=40.107.243.47 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="duaWbJN0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Qd6oGM+SGEs3BTx3c51mUTb8np6VVIEj4q7RfwmdYVdngao6/HLyJB2bTTRG1oHLLrEMVrmQGwakrOpr2Y0ss+26u1+U7MHASotJ9KJEJOKGaUlgGV3eqTjV8OeFzR4PwXOZ1Xky3+CPKNFwj8pcYtE8uNS72Gh9078gR/S8qypCSnaeF5jZZA3JvsLbH4xiNeYsY22Fueh4rbdD92g9HkanTDLAls0GXEVF56FPVFHEhseUfH9Vi1DfNkMVOcy1viviLkVfJojp3LL11VuXv8y/xWXHKq18wbAwtbMnr2ya8KNy/1hpM7zjjNl1R6PgKZifipixytXS1I4z518b3A== 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=x4m2uRvXtfBuum/oRe5e5uAr00h6qkG0CTEBwXFmvAE=; b=GON7zNqTd8U9516frNJYgYrl8hnMy7YYe6NbK9UxZ7UAIoi3OZrqldeGoGklveG7LK1Yw7cIFPuEelvydRaOUxqlQrNddlcmZ7OKTgoIvU6YNA7uNGjuqbjCCYJRP9XPCPIpZcUH1v6mowcDouS/UmPHaAI6cNe09oKHh4J5Yo+2Co+KORGFUDAYbI5aJzCpZ3qcWMaLLX2jRjudyZJTk1iFy5HhVcc4qeZBv1mEvAuno0GEStVgn3jhc9XbND/u0hOYEKrWz1m6ZkcHlcFsSb9xXgFsBREt/tiT4e/dpgN82RDGTM9sW3QnAJufD4IYcNbLuyqpoUpg8vuDfNEBlg== 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=x4m2uRvXtfBuum/oRe5e5uAr00h6qkG0CTEBwXFmvAE=; b=duaWbJN059BqnEbzLuq2G6H7w0EyI2nLNCoXo23cLASJLsN300hrF5vk7hEoMAveeBLHLAGifnkM/LsxPXJskpviFG233GgFktfpIkuo9c54kEXri8sVeTV49AIzxkqxqahmU071O9pTIHy+OBIjfR/wbVIWnMtQAKFVzcOD6aQ= Received: from BYAPR08CA0020.namprd08.prod.outlook.com (2603:10b6:a03:100::33) by PH7PR12MB7259.namprd12.prod.outlook.com (2603:10b6:510:207::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.17; Tue, 10 Sep 2024 06:05:41 +0000 Received: from CO1PEPF000075F4.namprd03.prod.outlook.com (2603:10b6:a03:100:cafe::2e) by BYAPR08CA0020.outlook.office365.com (2603:10b6:a03:100::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Tue, 10 Sep 2024 06:05:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075F4.mail.protection.outlook.com (10.167.249.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:05:41 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:05:39 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 5/6] KVM: SVM: Inject MCEs when restricted injection is active Date: Tue, 10 Sep 2024 06:03:35 +0000 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075F4:EE_|PH7PR12MB7259:EE_ X-MS-Office365-Filtering-Correlation-Id: fd731b2b-6f35-439c-5c22-08dcd15e997b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: 1HL9QZ1HMc15uaj3sGyHhOja3gGT8lgVusY8xy6GJ6SZ9bWh1bb/O8lju8FU/jLSaHGzTIovx0JTH5KCmYsDiM4eHq7EndMR+KHvrp5iFhnitrdWO7ZKpt4zhX47rxYyCEKQNZmGUksmnZZ1nRdtyd4DoYL/Z8eBP+zsqjTo7SmGP2EC+a7aWyGVYuo275AdVM7LuKPYi/7Bb3oc7fFdp5Aym/sXqex+FqhFp6t9o0bH6q954yDl80wTVDnIXswQaNtJqnL7yDzsZanH3w69ZCNngR2SeAg9KZbcd4ZKtrnVIfNpWCS0psLrXTqhjH8m+etvw5siGOBSnl2/TkP1ur8H1W1Mchb9XrxJQkYED4vxyv2g31ZmPYJFSqnmW+b/cBv1ajZPMoRo3iB1ybJsC/vFT/hvGGuoMj8E7H25ufHQpwWnU7J2LrNybUymAHtJrRnQ9t/eTpWKdwrUCDEHXjBKOfHbJz06Vm02kRHODLKBCiNzpztekSVlkc3nsGUjr4u63wYyFenHtWbrNWtxzzYLpSAAeZI1J+krXJ/qPzrxmIZwuh317dc9cISfQJc/epUxXU/ZmpfVJ3pb9xCFZToVrmbap4dzhKMxbDao9NT8Pm+ijF8ZBrEyHW8SZ6zDit+csrLCaFTnUhJdEtui5CCgx42Rk+Kv56NAM35JjXxgqFe8rGee/hE/rkBYdcKAWaweuwwMbHLAQoEIR8ztOOrGvmmSDDcdSlCLhVJehZvvE229e43mAiK2TU4JptmlO+cSxdc/gyWdBoHJ9PQClYRW7MCVik9PCx/ywwwTttF5a2lOnYlCHEO0K9+wrv7Hef7vjnhuvgSYoqd7TNgoY7sL7lzNTR1GgHO0988DB8S+7gFOo9Hn57+PiAKxPVtPrwAr84MeKpysL9vQKd1uz0Wgih5kCax6+57t5R5HBsBJIJ92JUIJNncqIDfnomjOw2kF46v3IW8LaHO2+f68UqiX8q1Ydakf3oG/c+65vVM4X2BrCjHoym8nPvtExp8DSRu9pNaIsrnCAC95a496Fds64Eszk6TLkj5yHd1EpR5Pl8Hyfudy3Zem0GVys1hSsep2EYAp2q7IVz89e2Hah88APvKs6aGsQSY0ZBQ2sdr3ue+bTU7BdlGQRZQthtvegzY0BfrGQnGv91TOqa5ZHXFKkbj6n7EVMFRDq/GASUmuMw0FV3LzGIzYrlrlmzk00G7+0nPNjDnauu3GygVhN6Gz40sBwrtNxzrLBFwHH4x7wDn3vTwo+U+s5DhNekCnYcuGO8YCIjwb2XrdN3FoSxQNTtPDpDFABKfthNxg0/saCe1Z67jaY07DUOXXT40N2NSK+YoqhX4lrs+SQFFmRKbTvY7zsgo0XwvNxFvMeNRVWQJrAdHHO+37dtcP5nTQheEe+VIrFG7+KVrT2FXlWrQlVc46ITjxsUdA/ArSxSXxcVYYzoxPRVO4+VaIvaKS X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(376014)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:05:41.0334 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fd731b2b-6f35-439c-5c22-08dcd15e997b 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075F4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7259 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 | 8 ++++++++ 9 files changed, 50 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 68ad4f923664..9e5764a8e031 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -76,6 +76,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 94e7b5a4fafe..cb1608a69144 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1715,6 +1715,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 9102f7e39c52..0b898b16026b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -5072,6 +5072,8 @@ static bool __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; @@ -5089,6 +5091,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) { + if (__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, @@ -5153,7 +5160,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 | @@ -5162,6 +5169,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: @@ -5179,9 +5189,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 d9c572344f0c..1c13c5da6eea 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3867,6 +3867,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); @@ -5066,6 +5082,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 f60ff6229ff4..0cf32954589f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -44,6 +44,7 @@ extern int lbrv; enum inject_type { INJECT_IRQ, INJECT_NMI, + INJECT_MCE, }; /* @@ -602,6 +603,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 0bf35ebe8a1b..c3a49a3b7f21 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -84,6 +84,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 f18c2d8c7476..b3dce5d95329 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5093,6 +5093,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 ce3221cd1d01..b2b1a3bb4eb3 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -92,6 +92,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 70219e406987..2007598af873 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10485,6 +10485,12 @@ 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; @@ -10493,6 +10499,8 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, can_inject = false; } +out_except: + /* Don't inject interrupts if the user asked to avoid doing so */ if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) return 0; From patchwork Tue Sep 10 06:03:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Melody (Huibo) Wang" X-Patchwork-Id: 13797987 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2084.outbound.protection.outlook.com [40.107.243.84]) (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 5F59516BE23; Tue, 10 Sep 2024 06:06:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948365; cv=fail; b=Nx2jJ4wmgKFdO/jH6a+atwIaLJnjKFB9otR2Wx2qeVfAG2Dp9qBtsRohcaJFjIoW5Dh6frGBJb3jexjUNycbTtAvj8k8heOxK3XRoy/uBinhcVhPiM6avUD/eoc8buVa/9SkaeU3VhDeqhQXDpcNycP2wmhl6fOVFUWaXCYoue4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725948365; c=relaxed/simple; bh=70zXwsnB3QtCcer74A8rFuSJb4ncMblcC9Plyu+SNBU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DVE+tvmS9Nfj/lPPvk2wY/s4g0DQp1xNYE4GfoJ/WiywHVXROIY6lrCnCslQodlmsN5RNaH40TLNaagKk1EqlVg16Bzi427jySW9Op5D7ifqF4TkgR3Kg/FvC2fWpEZ+IVfQKG8Rynl8ohTLUyJCgqS+mgASzuOVKLvp5GDNDF4= 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=MDBggCPH; arc=fail smtp.client-ip=40.107.243.84 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="MDBggCPH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QLkWmzQI/ork0C90BzjWQVMeww3BlPmM6vmLJ5FwZbVbrPXwzICw+EPeMTa1iwOjyBwSuy+jqyGIF9tL9BfPx/qUISzueuhqYea5418hrXWl2ApxMvMqUiWG4+r4B4i7xMqz4/Neqchoj0XwcYRbjM764/IcCaFPxFl+k8jZZjX15KHrSEAPzNFGSAiggtvFv44+KBX6ThU7HhJ4plpckBOIwKOu1M8XkZ3Ih4/KmY8TMfu8W+SMSY/ZG5jrQzPtE3oByYV97HHbN9KXV8Og5/Qsp2QgUgUnR4akj2ByC/hOpJobViYuUjZuv3qnkijzDTNgrbw2RTw2RWEQF6oZhA== 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=BoN70gbK2KET3WII/s2tvefJhIeJN066xQyyKOwUROs=; b=I7amdkIXzCP9LK+epQ9ejWuZxg37/xFnUq6H9PCcJSWX8NKp4mP6Xc35Z2YUW51tTsyVGtAfo2BxR4UL+CGyefTPe0UaiJQT55fdP4ZFQDtTXRtlVXtdxVsZC3HZeE8GeWeV6EbFiLQ9lmPmph8wzQRYiNid+998+/oYMlv99Vv/KS7hnUUtjq7T+5HyHfgri5pPQuoMyxOJFIHzrolwT8HHrElg2Ci+dj4rY9iBbRJoL/3p6p3b4J6KF3nDC/cXPvLqIndz112xlFLWPLURchTcn18EFZVffhs1J1MajpTSihTVRWco/3e6Ox59dzKTzAtc1XDZ8gAsKRo/ks/w8Q== 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=BoN70gbK2KET3WII/s2tvefJhIeJN066xQyyKOwUROs=; b=MDBggCPHXdaKUqwzIjSLd6C6JnulQMTw9rFeBI6ZZnYvKenbBX4bI4/ZUWGHNzFDG25wpOJuWQ+gx5d71XAZQWUu7ZLdLKBFS6h7rt9+AzxGat10sbSeuh+RVkeKnLYmnqD9tetaPIRmuW4POSXU2RYYLzJ6X95NTIVoTY8K05g= Received: from SJ0PR03CA0377.namprd03.prod.outlook.com (2603:10b6:a03:3a1::22) by MN2PR12MB4422.namprd12.prod.outlook.com (2603:10b6:208:265::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.25; Tue, 10 Sep 2024 06:06:00 +0000 Received: from CO1PEPF000075F2.namprd03.prod.outlook.com (2603:10b6:a03:3a1:cafe::e2) by SJ0PR03CA0377.outlook.office365.com (2603:10b6:a03:3a1::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Tue, 10 Sep 2024 06:05:59 +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=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1PEPF000075F2.mail.protection.outlook.com (10.167.249.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Tue, 10 Sep 2024 06:05:59 +0000 Received: from ruby-9130host.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 10 Sep 2024 01:05:57 -0500 From: Melody Wang To: , , CC: Sean Christopherson , Paolo Bonzini , Tom Lendacky , Ashish Kalra , Michael Roth , Melody Wang Subject: [PATCH v2 6/6] KVM: SVM: Enable restricted injection for an SEV-SNP guest Date: Tue, 10 Sep 2024 06:03:36 +0000 Message-ID: <82c10ab0083736024a916db479517154f7a01373.1725945912.git.huibo.wang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000075F2:EE_|MN2PR12MB4422:EE_ X-MS-Office365-Filtering-Correlation-Id: 0ffd7be6-eaec-45af-abd5-08dcd15ea441 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: yAyUvcpV48OabhhCKMv+BmXqr7Z5so/YQIyiBYXKms+0ZypokszVTeEeaTZUfa6zwL/y7sSrkvysfaVokAfPMLabPuhEiXv4MnhudX8yXrbrbuIfsGCcqFPs1pKQcBOIOMHxWY5H8m45HC2PAMpZ5Z21HLKlJl2mE6NHvTsm9xNg6P+ij/YhSLok8icsIez9AXsWKG1sG9UwDrk2mmxKTetOiBybFtCjX83UydHMN1Bd6+BmLRd9Acm2sjzkA1RdSOhxsyhRtF5glGO0VxMamNBBYwLV1wLpflv3s6bqy0prtIhJfn89o+NKGlGivM7QgcmoQgOey7WrmIxnnEBlRh/CnpJ9TnL3ERtW7glZPa+y7QEGWLNNImezgRSWeTVcAbWo82dp7FNnkuMSdWtcGZKSve+u7z2Q4/6HjRZuSicYglWO7GzjxarSyADZ/wwhXS9dheXqX4qF7AKM7PFvYLmg0Iunv+tH1FuTWgmO5QGhoOExVA5NhqM+byHFJmRYaYoLCPGb4Ydbm17JIBB1ueFOGZYN6jslTTTcXJNsu33fM1ihPzrb5CTmU7iqfz1yBOv4Dk0+srnJQDtCdr1VjZUMR5QToxubK81UJKyYowF46Kq+93Rf+q8Dz9uDW+JcHvd7SN1Nayyzxbe87HS19z+RFaJzgEtvPUXHXGKPUWCLm/HGKtxxzTrV8wm5UrJihnVhjP5PWDHAcQFSdYgQqdlTDU4YTwqIf6HIrGf6FQpX+rGw0Y1vQE0iDa7Sm0VPGy1Z7k9CLQYHOULW3LPLWKKwUvbYLya9g4ErVTXlkJw3Ezu7SXu57M6ZfgYQbietKYzuhbwhlvTqBQ4NFbdM5pAlOFCTMDH1El8QJc2iOojzmPPUKHjRjVieSl1M6ookDNlUIiQ+qGIiPVIXe9+3meFp24bt63kN8pWWsahAJOnSm6ZfSeqVZA1w+phaKFmj5NbQxNMML0utZgCyVn7fwyflQKd2yPSO1P7oOm9SCbA2TUFnq867QPE0Jx2k0IHx0lRkpWkgmrSnU2ORO3fx4GTHpqJLUgXj8SdmONtuyXHWbgGEF4tmuFan3whav8fnDx9eKWgi951DeDjmFoW889ss0dggpNScSoX4Lj3EJm45EWtYsN3LGh2piXm2JVZDwc6XWg1LIcO+t5AbK3d9/5at9P6wzLSAuvWHvZeF3Ieqt8m71OwH4h20mKvlQrUK9x4zJvwtf/XcjLXhWltJEbC3gKmtOybXWQxzA5pz+kMDenMlskqw8sHAA+zct8oynIebSv/z+gHHCbscP5pxl9RqKMB5pjNnKUgQY2BdCai3o/BNKWuKhtSN605wwQe5bMn7oUd6WFXo5OZLeaN7kPRAb1fCL8RegbNnPKO1iLEVRu6TvTAKXW4Hiv1MrLQF+7OB6DvK9Mce8NEz3IzaqrnUH88wdX8m/rxMv4RcgLVt+4HA8vqnqCdmpVKEC5KI X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(376014)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Sep 2024 06:05:59.0791 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0ffd7be6-eaec-45af-abd5-08dcd15ea441 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000075F2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4422 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 dd4682857c12..ff8466405409 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -446,6 +446,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 0b898b16026b..bad50378c898 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -39,7 +39,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; @@ -56,6 +58,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 @@ -3079,6 +3085,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) @@ -4556,6 +4568,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; @@ -4613,6 +4634,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)