From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.71]) by mx.groups.io with SMTP id smtpd.web12.6758.1639020521061239946 for ; Wed, 08 Dec 2021 19:28:41 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=LQ/K+YgG; 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.92.71, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qvpk9RTWfMfWjHJwYF109LLP7b+TynQjOj69eAPBDqgYWi2ntGxbPqacjhc8MAC1ovNsgke1SOT6bUkQTLHE1v12hM2quygwvAijG0C07IvLOZFbYePAKa1fL4AYhKOlP9oUSReuZ5cjwE+bXNnZnsCwUnYwoKKE3Vlkiatnj0YxGRcdaVnFvqxQz/7zw77weTzIM4a8XPJAH9NOZpqVEyQCl/Xi5+/QLhhJBe6w7sBRiUA+12jxr9m0rS5q7YGND9fh5TjzSd3yIx0ijgThx+BY7a1aGWpj5Se1tuf1vN4reE9Vj2Fo8wneCeq2MIJ4vB0onIbNYpLW8PF0b7mmSQ== 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=aXvVEzyHGTZIdoV2SshsD5xZqtL7LpgyPP2I8GyuDaI=; b=OK4OvEcRxPkD1LnRHhMyb+SlpQCG2JejmYhOxvqTSaLCIzwDd+R3mPO3kPevOW7f6/r5yNPtnpAnOOWCwBAftYR9h1PYKWns2D7dBebObkIxXQ2r9/6M3PmAy6z7kSMN1rX1N+8SxulUfJapziFm25IyeCUARWD14Zy290X9dVensi8ZkxAP9nlSmQAhUDAe1CB5bKd6a4sdE0fhVQzbfhnoN5L7VoFjgqT9ED3OAW/FbnETrgrMO9z6bmCLEjzs4iF2rXZtIuik6baYAgH9XJl5uTZoapV06/EIYutHCoPlMibzO3585bsSdj71cB9ESapxo9G94++46XpO0+p8qg== 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=aXvVEzyHGTZIdoV2SshsD5xZqtL7LpgyPP2I8GyuDaI=; b=LQ/K+YgG2UFO9nN5KCZm4DAQs1FWzsO0C22idbf4Mld0/xNs5m/0FzA7DiLcuxaFuodq4/T69iwzhOZ28bHW7RFrMSxUe5eDe24sc9/FYvT6AObTaSSmbKHRg/U+WsTbGbtDHYCb5mjhBD0oiMyV7bwiEjns8K3jvaHGYZwVm80= Received: from MW4P223CA0018.NAMP223.PROD.OUTLOOK.COM (2603:10b6:303:80::23) by DM6PR12MB4028.namprd12.prod.outlook.com (2603:10b6:5:1ce::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Thu, 9 Dec 2021 03:28:38 +0000 Received: from CO1NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:303:80:cafe::b2) by MW4P223CA0018.outlook.office365.com (2603:10b6:303:80::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.20 via Frontend Transport; Thu, 9 Dec 2021 03:28: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=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1NAM11FT012.mail.protection.outlook.com (10.13.175.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4755.13 via Frontend Transport; Thu, 9 Dec 2021 03:28:38 +0000 Received: from sbrijesh-desktop.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.2375.17; Wed, 8 Dec 2021 21:28:35 -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 v14 15/32] OvmfPkg/MemEncryptSevLib: add function to check the VMPL0 Date: Wed, 8 Dec 2021 21:27:43 -0600 Message-ID: <20211209032800.3802995-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211209032800.3802995-1-brijesh.singh@amd.com> References: <20211209032800.3802995-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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a769e35a-0109-4a15-c61b-08d9bac3fd97 X-MS-TrafficTypeDiagnostic: DM6PR12MB4028:EE_ 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: pV3HMLxbDj7u3rRHVMP9LVNM6z76/EYPtYsjibSWWZr8o3/mKUK0CcUQt8iFA1cZFuaYvYonWwgiKN4rMCaedZy5d4LRQ1XFFLiO+NbNTqj9O1zoKpUBsU0aiunYRvTTwaevE9Xw+6sHCIYf1L4jQWevRBU5ho23eS1qZEqB7cYp4Em8wb4NdJivf+Uwlw/RedjZ4vOV7NNXzqxV/KO/xM0Vg8FQNffVl4q17/sp0IohS/ZY4H7lKvUA/yEiMLF+/OUfeiqXxqR/JoAjNmjYe4q10HLcj6E2fEiUOQl+/AulBwcPO1LjCIVPj2gpTay7RLUJJcjjff/l/3eq7t4d3Os5ruZVWxXKAyIlyjr6N+c4rpJWSc/AExpm6i5TeUo2C5IKU5ehpG9BON7W4HzGBuvdWvi/gmFvKxGFIR6kAtYWSdJMrTIrRbE52LLaUzU7agwrzv4nbzKZNp/ZwcjI/dbDo6VdJGe7Rq2ONNynxYY2FeEfFVibovsYz37eKz+ctD9tpiuXJ8PTVQ4GN+gNpvnRTmOLDVNCv0KxcYzVoTgPh4Z7vJNfCa/ZcnAssjyMAVb3mF4yJsU621gwukvxNSEf6OEaTgNbTUZSRtsOCtf1x4Jn+E7f7ZpA97J1CPUEf32YsAOJon1VAj6v5qrEsxHqa2eUEEaaUufdfFUNbaf2Q9IvQG+2312kfo4erAAjBXyPaKsGrsXmFzz2vPh09eK2Vfqv3qA8HbV1kxcW+YYInoalDAro2SC18hpXEYmvurWrvcCDrrYcA2AjDihGKepDy4cv4+NcGlLEG4TVLmoDCqQJzpNvufngaw35YhbuNvHxvoMGg3yCHPm5/LJB/gf5xWs9FYl8Lws4m+3DM1Fug2jwoHzEF9oWfDBw0vnC 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:(4636009)(36840700001)(46966006)(40470700001)(8936002)(81166007)(16526019)(70586007)(966005)(6916009)(1076003)(26005)(6666004)(186003)(70206006)(508600001)(2906002)(83380400001)(7416002)(8676002)(19627235002)(356005)(47076005)(316002)(86362001)(336012)(40460700001)(426003)(4326008)(36756003)(2616005)(7696005)(36860700001)(5660300002)(54906003)(44832011)(82310400004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2021 03:28:38.3935 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a769e35a-0109-4a15-c61b-08d9bac3fd97 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: CO1NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4028 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 b396f0ffbd75..43319cc9ed17 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChange.h @@ -27,4 +27,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 bc891c2636d6..7797febb8ac6 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 9c552ef5c7b1..d11aafae8472 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c @@ -42,7 +42,6 @@ MemoryStateToGhcbOp ( return Cmd; } =20 -STATIC VOID SnpPageStateFailureTerminate ( VOID --=20 2.25.1