From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (NAM12-BN8-obe.outbound.protection.outlook.com [40.107.237.61]) by mx.groups.io with SMTP id smtpd.web12.852.1631557211439854610 for ; Mon, 13 Sep 2021 11:20:12 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=ZAiXl2hU; 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.237.61, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WcKgRCq3gEl8yYig8OH9bTTXm9Bs0/yu13HXLbjY8RRexnqjPUQy10+mF4ipaj6cm+U/km+ufeiWZutRUFLw17IItWlki4r9eUHq6BQhi8D0WFaUauc0lbEqp3xnQNXkRKkuKZMdMul09cI2WTCkp/Yh/yVAK1zgyRoS1/YjhZPrU3TnOUkCmpr6Q6joTS/A9JypkmWLD+ySVUWrPqswCNz7FGq+qbPMTWhJNsscb8sdYQLvirq0S8pAYqmag2Uw4s5xl4kBe4qOIBltukghecWLRFBLSmF/oTZpEI4VKovO7v0Y8oHJcQjelvcqra5RBf1n4xIXw/hvlM0+X3WpLg== 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; bh=RY0QGL8s+FwFu5CprFJKwG6HDp706QJHBanrP3OOQ78=; b=FwgvmpZqUWGOvTuh5D9OrLwx1MssYpOK2cClnN1nJnnkZgyZf1VjdzYKtLZetbpbSQsTx7Kc4f/49+9wT7cdTr1knkphzBu1uj+KC9Ase0ti0QUAzmtOWqskVJVZwxJ1TiPQ4ltBjqu+z0PYrsV7tnzLcFL4r00TUiToWbvkFEZiiBsKWrzJbAoN+5st/TLYmsFNKlNA1GVYU8gYZmbfkq5P8zdkrRSEVsZ3qpX7m/tb2WxzVdGIc+ekCBhH7srT/JPa1wIRvHqgWsj55ZKfJDLkK+3z7aQ1CVyEXYGHEt1EVql9/lHefzFHRfN/Rb1gsL6gqZxDhGv42GjNYnoTpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=RY0QGL8s+FwFu5CprFJKwG6HDp706QJHBanrP3OOQ78=; b=ZAiXl2hURGDr2JJUo+t+VF2wCKpPGruIlyuOj3J+FrsLLqoAbDUPJYQ8rzao0exTOd9wUlLaJnnNYitphemMutaNJa8urLiNk6uqbbBXgyG73KCKkpyn7svko5lCagVqOLaLB4XdEQnSgGc4gEZBUTYr80njYIwU8T4nBvf0xgM= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2512.namprd12.prod.outlook.com (2603:10b6:802:31::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14; Mon, 13 Sep 2021 18:20:10 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3%6]) with mapi id 15.20.4500.019; Mon, 13 Sep 2021 18:20:09 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io CC: James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Erdem Aktas , Michael Roth , Gerd Hoffmann , Brijesh Singh , Michael Roth Subject: [PATCH v7 14/31] OvmfPkg/MemEncryptSevLib: add function to check the VMPL0 Date: Mon, 13 Sep 2021 13:19:24 -0500 Message-ID: <20210913181941.23405-15-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210913181941.23405-1-brijesh.singh@amd.com> References: <20210913181941.23405-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA9P221CA0012.NAMP221.PROD.OUTLOOK.COM (2603:10b6:806:25::17) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9P221CA0012.NAMP221.PROD.OUTLOOK.COM (2603:10b6:806:25::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14 via Frontend Transport; Mon, 13 Sep 2021 18:20:09 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 022a9012-698f-4b39-4523-08d976e31ec6 X-MS-TrafficTypeDiagnostic: SN1PR12MB2512: X-MS-Exchange-Transport-Forked: True 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: o31HQrd5/B89bzYYq0/WiQymF+bSRZ5hWkT9Gbk71d0OrUMfNQ22HxekI595bUUm28u6/PcMOyqS3Z2IhnC1BAYLYXsBlsY9kr8vCAj4ltkRn6R8mzLzaKGlJHZWcFWM4ixLAEywDF07xFodzOky6fXMZp/LB1oEpVCWXJEx/KtIx4+go5HkEcLNH8nEhWsU3ig2z8wqysLssci0Gr/irjGTBFuDcnX08dJtO89O1vJ4EKSHIKB5DRJbQaFL9ERgEVwdcxIjQ+WE6Eyhvt0Qpt7LxEpu1y9F6W6idVCMh1IMrs5834cUZIR9vMkaCjdjHON+qBPAD4aiSNOy0urySfiAz4KE0tvWhSVYC33uPhbCxM3QpEbhHXY5ch7oWfHpi4f8YaNjCNDc4y3P+svwCYZb00hmfGU4NY92SG7jDVNeVAPQgGjFVblyOW8HptvB5C2PAV5hAPWD3ipKRfH5jlVW9ZPiaFjS715TrVDtUdFuzhIrcJ8FatovPBPxOJttJwjzSNrV6DvLYC4MtWRPjQwWRiwsda5uUJfji8zeWNNPVrXKB/nD0tsgoX0uhp3XOSVad0XY5yRK2t/hei/Y4ZjRbUaPDLcIGskDvSD+Otc0SOpClBS61rSpw/UJ6r5WXVVDmwjiCsRgt2sBfFvn0gmlJkWEAdvmtk8CCQHw7cWRsOxA7qiSj0CKyszWuyLjmOE+2EXUoDIruO8HYS+lhjpS6B7NWuS1zBxQctWlHlX5LiE+FflhRG1uOZzMtPE3QAx524DMhcxmbQ+42yu8TWvIXsWls9nyE9cIHcnRlVA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(39860400002)(366004)(376002)(136003)(83380400001)(478600001)(86362001)(8936002)(966005)(6486002)(8676002)(186003)(6916009)(2906002)(38350700002)(38100700002)(26005)(36756003)(19627235002)(956004)(2616005)(66946007)(6666004)(66556008)(54906003)(5660300002)(52116002)(44832011)(66476007)(316002)(4326008)(1076003)(7696005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?js+tp9UiRRbT2Impklku2LOchJed2EwMb8zTUDwGM8mNu+R3Swu1L5qNkpJ7?= =?us-ascii?Q?xngmrgllU1kdm4Rf6lqPbbXfs9ceheKvUBtRVyiXBbD6vgbAMvLyZ9gGfRMr?= =?us-ascii?Q?cSZ5DW4i0oxFZNUfzJInpGc09PiABCMpdn3TVxBq/UjA85s5hV5uwpT+cA60?= =?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au82NQ20L5sNHMzL4XmKyLnWcOkpk?= =?us-ascii?Q?ReCityAOXuTgaU9ENCDMBInIT/c7UxFc1MumsERLPX4zFOPMBn+yGntUW6KR?= =?us-ascii?Q?6skMyQt0hN9+aUdt0mymdewd7zQTd7awUzDHB2dZ0ykX4GHXCPOMlj/IA+i3?= =?us-ascii?Q?5He5g4yxvFP2I3FWuJ+6GcGAcXLYvWHLp47NZl7vgJ6S9z/MmgYj/e9XhvmX?= =?us-ascii?Q?4KqcMCR0jXUS2xRgPz9IDFT1iiPVtjDdhw7uNEuJ1mJAwLBQhGqO12YeWhaV?= =?us-ascii?Q?uryNefhQG+mehq2d5xbss5UT?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 022a9012-698f-4b39-4523-08d976e31ec6 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Sep 2021 18:20:09.7798 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: D3hUphRagJSW6w7cGnVTktK7Olx7xKoMfNic9Lq3iBS81fFhPWyDGjhnNWQtXLOje+1e8deIR+BtwSC0+o/n3A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2512 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 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 506df12d4e51..653151d4a422 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.17.1