From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (NAM04-BN8-obe.outbound.protection.outlook.com [40.107.100.77]) by mx.groups.io with SMTP id smtpd.web12.2361.1636582529222515883 for ; Wed, 10 Nov 2021 14:15:29 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=L4ldE0i/; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.100.77, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O9aAvCgCdjRlqueJq0OFU/PiRfJHauMvONSj3ThsuW6xNsx0IyqazvUMi9bPXsO3vuKyvYu2EGm32khQDRYxGb8gmTlnfFzGk1+qUdTDwgKJa3yAqRB7GuP3cSqjRHaQd3hMVa8SSdg0lHVPJ9lDWKsk9t1z0bnD6SdCgeIROghZrzbaQVSQ+PwODm3nQI7f4zaSGkDuMUQYWL3BxbI76e5jvXGaK68CxsLyChiY1oIG/IaxfZ6vRSYs1EL49JT1yKG8EoEo6cfLbEQxLQXMsMC5aBm02/+ECzgSip6D/DcdedBN70HDpJ7XVQNwqgwYed6JB9Aiam9GPy7T+e2IsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gqmcH/XH1iKjEQq4jwe5bvc0FS4PxpOdaseRluLWgDg=; b=SdAdOThIUwIVDD7UkDf6fG5XO9z0r+U4lYmywa/1AiVxZMqTFu7r/axQNj1FvO/GKO2+t9SIkiDkgI5XYhEx9zVKWfDVDb0ETnmYv1i8MgRxyxvqcQ1d1zNmDjOJYQLQwmlB5fiMuJp1vAd0iVk2UgMVPXddxYFY/xCG9Pdl2LtmWQrcgwl5Z8kYG1FuAO2HrcBpt/n3dXzFb3U2lOK+M7VttksoTt0OuCMNb5Z15gOTinor56thitX0HJC1ygZbLnR5tFLmuLug6nWIaIWUqNULYaeV8kMH6vxkqXptaZBGQd+gixHO+pS12x/eiDn/nueRJkHyQP/UiJpoWj2M5A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gqmcH/XH1iKjEQq4jwe5bvc0FS4PxpOdaseRluLWgDg=; b=L4ldE0i/YZvBHHNy6265BQ/WsUcG7lxoyNME+y1YDAYjZY4o6gRHol5XBsXEJHfa6U3KfTerA4aCWmIF7m5b81mm0UtZc3CtDRdUwelsim3gtFzDz+zQq7aSVuvEj0G7beXYVSbUwy5GEELNUupFEZ0F6xZF2gZzrg5YgNeOmSc= Received: from BN6PR2001CA0008.namprd20.prod.outlook.com (2603:10b6:404:b4::18) by MN2PR12MB4237.namprd12.prod.outlook.com (2603:10b6:208:1d6::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.13; Wed, 10 Nov 2021 22:15:26 +0000 Received: from BN8NAM11FT066.eop-nam11.prod.protection.outlook.com (2603:10b6:404:b4:cafe::8) by BN6PR2001CA0008.outlook.office365.com (2603:10b6:404:b4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.13 via Frontend Transport; Wed, 10 Nov 2021 22:15:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; 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; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT066.mail.protection.outlook.com (10.13.177.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4690.15 via Frontend Transport; Wed, 10 Nov 2021 22:15:26 +0000 Received: from sbrijesh-desktop.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Wed, 10 Nov 2021 16:15:21 -0600 From: "Brijesh Singh" To: CC: James Bottomley , Min Xu , "Jiewen Yao" , Tom Lendacky , "Jordan Justen" , Ard Biesheuvel , Erdem Aktas , "Michael Roth" , Gerd Hoffmann , "Michael D Kinney" , Liming Gao , Zhiguang Liu , Ray Ni , Rahul Kumar , Eric Dong , Brijesh Singh , Michael Roth Subject: [PATCH v12 15/32] OvmfPkg/MemEncryptSevLib: add function to check the VMPL0 Date: Wed, 10 Nov 2021 16:14:40 -0600 Message-ID: <20211110221457.2397234-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211110221457.2397234-1-brijesh.singh@amd.com> References: <20211110221457.2397234-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] 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-Office365-Filtering-Correlation-Id: 9b0e1e5e-6b2b-408c-d14e-08d9a4979903 X-MS-TrafficTypeDiagnostic: MN2PR12MB4237: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eENArKzpy0K4LTC0I06rLXf8bQGEbZadQCr4oZvTS0Xdee2cOdTxYvZGRXssJd2bIxz47RWbiRpoFVXxaWbZYMAihpcMXxQVcqQO2ne/zJjWK85r6C8UVNf88BNEXbFLYl7V3F3HTvEQpk83m7jj6gaG479e8wBV9bd7LUDiyNcESfOVWPLuerFw1PDriXYXWPR3QaEC8b9RUREKF0btybkx3DZ47dlE/EsIpZ/D4Zpm1rvB+dysy6mVzJzMXeueI5Lrp4HMn6c6aFBlTF/oU92Gala07ik3U+xGJzERr3axZByh/smtrhs5PZE7TFdFvUX06DT1X7/0xRNhrR27UCFQZVuCvqO6szNfCSyJfoPUuWoWe2rfuMpyGaxUu8jkt3ZRhsZoy9vXBP0l4dvKzQL5ATKKg+E/Y2wx9GZiazN16qy+UCbR6UNOaZANBnNH1QJiEQ8E3s0CD0vEfMFeQ6GGUoPDQRzyh5sq53r3TqvP/3N66j3J4k84KsaMkn9B3nlmjU5uxxK6TtAdp6SuQLia2tas25ScEfv5YIOkaWZxEmjUB96sNV/J5Ka/7hqeGnreGKDRX7By/wLAiS9bZ7W2p2e2Xz5tcZZmnF7vf1Z7DApFF5KMfcn/wQZTlgdDnLO5kNKj2aI4X7c4iZJjdoTPb0zdnDIGACXUEGKgyBvWsBFnqYD2X5svlASOJ8dgzIT47raFA/I02c4t/Jh9v7sKeiuiNvwiHGYR0tccqW6NxJmZNrbxS21Rxx2IShve9RQPoVBXiO5drWR1fea7tjxZHrUoqzDlZWeySDi+xJ0il61WF2gbLsAVw3s17Uu1 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:(4636009)(36840700001)(46966006)(7416002)(2906002)(6666004)(82310400003)(6916009)(54906003)(356005)(966005)(8676002)(316002)(26005)(186003)(19627235002)(16526019)(5660300002)(8936002)(86362001)(81166007)(70206006)(7696005)(4326008)(336012)(70586007)(83380400001)(508600001)(36860700001)(426003)(2616005)(1076003)(44832011)(36756003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Nov 2021 22:15:26.3377 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9b0e1e5e-6b2b-408c-d14e-08d9a4979903 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: BN8NAM11FT066.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4237 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 Virtual Machine Privilege Level (VMPL) feature in the SEV-SNP architecture allows a guest VM to divide its address space into four levels. The level can be used to provide the hardware isolated abstraction layers with a VM. The VMPL0 is the highest privilege, and VMPL3 is the least privilege. Certain operations must be done by the VMPL0 software, such as: * Validate or invalidate memory range (PVALIDATE instruction) * Allocate VMSA page (RMPADJUST instruction when VMSA=3D1) The initial SEV-SNP support assumes that the guest is running on VMPL0. Let's add function in the MemEncryptSevLib that can be used for checking whether guest is booted under the VMPL0. Cc: Michael Roth Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Brijesh Singh --- .../X64/SnpPageStateChange.h | 5 ++ .../X64/SecSnpSystemRamValidate.c | 46 +++++++++++++++++++ .../X64/SnpPageStateChangeInternal.c | 1 - 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h = b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h index 8bbdf06468b9..cc1318075523 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h @@ -28,4 +28,9 @@ InternalSetPageState ( IN BOOLEAN UseLargeEntry ); =20 +VOID +SnpPageStateFailureTerminate ( + VOID + ); + #endif diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecSnpSystemRamValida= te.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecSnpSystemRamValidate.c index 64aab7f45b6d..3394094a65e5 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecSnpSystemRamValidate.c @@ -14,6 +14,43 @@ =20 #include "SnpPageStateChange.h" =20 +// +// The variable used for the VMPL check. +// +STATIC UINT8 gVmpl0Data[4096]; + +/** + The function checks whether SEV-SNP guest is booted under VMPL0. + + @retval TRUE The guest is booted under VMPL0 + @retval FALSE The guest is not booted under VMPL0 + **/ +STATIC +BOOLEAN +SevSnpIsVmpl0 ( + VOID + ) +{ + UINT64 Rdx; + EFI_STATUS Status; + + // + // There is no straightforward way to query the current VMPL level. + // The simplest method is to use the RMPADJUST instruction to change + // a page permission to a VMPL level-1, and if the guest kernel is + // launched at a level <=3D 1, then RMPADJUST instruction will return + // an error. + // + Rdx =3D 1; + + Status =3D AsmRmpAdjust ((UINT64) gVmpl0Data, 0, Rdx); + if (EFI_ERROR (Status)) { + return FALSE; + } + + return TRUE; +} + /** Pre-validate the system RAM when SEV-SNP is enabled in the guest VM. =20 @@ -32,5 +69,14 @@ MemEncryptSevSnpPreValidateSystemRam ( return; } =20 + // + // The page state change uses the PVALIDATE instruction. The instruction + // can be run on VMPL-0 only. If its not VMPL-0 guest then terminate + // the boot. + // + if (!SevSnpIsVmpl0 ()) { + SnpPageStateFailureTerminate (); + } + InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); } diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInt= ernal.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeIntern= al.c index f9ab804a7edc..cffe703de9dd 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c @@ -40,7 +40,6 @@ MemoryStateToGhcbOp ( return Cmd; } =20 -STATIC VOID SnpPageStateFailureTerminate ( VOID --=20 2.25.1