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.57]) by mx.groups.io with SMTP id smtpd.web11.118.1634144282905702923 for ; Wed, 13 Oct 2021 09:58:03 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=k5JtuGvv; 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.57, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KmS8vbWEIKuehqjMPprlIDpTXhGn8Nx1uy30dW7DJ/VEO5NQ+Gh2qk5lX0YNsAbhyvWGu+HB9dArN/ap24IBl8B+O7UVGCGMx+dSUk5Uwc5toR9+Qoe4IPZI/Amk4Kx5HVbB2cR+Z4WkVLG8SFzKnLgvOlhaTVjn82R3nzBUyzKoIy96kSUlxaATQQ2i5CoCcbQdaZk40f0TqlCfjy2E/+ED5VKDnSxTGBfJ7Lvk+X9PdvB0TsrK7ePVOqPvm9eWtff07UP3jewheA1Jcxl1fNkqzdsD/jxdt7km1ldO44+LFJ94PBt0mTD1TqfFTHRkMrEm7QdfnliMOVCWyeMnUQ== 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=K+RAaUEZPM3Oj8CKnpLf/nq1jec9JbPs9dxE2pXN2WM=; b=R/U2i0e0naXhls9IRPEMeYhjb8QhWS18W1Rqrkq+Ljx0OYXv6st2JD7nqSZzc2RdHFoM1PHLbyXhj+Ru/p1JeJnQpED7YChAEOPRSfWs1S2eh/tM1NSPGJfVdGdoEfcX8a+LovQWgwqFL4fi1aks+ai/tIeWtsDj1GgF7wfHl0APVcZchqtc9rQzL9HoVhf3zAzr0aX9jSg3vFIB2wEjQR8W0KYM2Di7xzyhaLypeSJ3BHOKq26vdbVxrMvDBQ1Xx23V4D4CxjfNln/1T2ADCmuG1ePnguJVPI4ZYF/pXzcD29L5xFABAcbrr6inI2pP+U04ecMpPS+yICl792K9zw== 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=K+RAaUEZPM3Oj8CKnpLf/nq1jec9JbPs9dxE2pXN2WM=; b=k5JtuGvvbRZynrtNfnSx6id9n30IBVEwr4tk7i3fpy5C/yK7j3U3AWR2lkSaj3ZjDVMV20QDx84ABaKWkzvYGEnWUDnwEFEZypzGJRjzArmn3h3ic+qj6PA0RloFR7JtJj+u0QAd+fya18MrkqZ7N8znnhV3wO15D7vRsXVLg5k= Received: from MW4PR04CA0054.namprd04.prod.outlook.com (2603:10b6:303:6a::29) by CH2PR12MB3781.namprd12.prod.outlook.com (2603:10b6:610:27::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.14; Wed, 13 Oct 2021 16:57:55 +0000 Received: from CO1NAM11FT039.eop-nam11.prod.protection.outlook.com (2603:10b6:303:6a:cafe::3b) by MW4PR04CA0054.outlook.office365.com (2603:10b6:303:6a::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.14 via Frontend Transport; Wed, 13 Oct 2021 16:57:55 +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 CO1NAM11FT039.mail.protection.outlook.com (10.13.174.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 16:57:55 +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.2308.8; Wed, 13 Oct 2021 11:57:47 -0500 From: "Brijesh Singh" To: 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 v9 15/32] OvmfPkg/MemEncryptSevLib: add function to check the VMPL0 Date: Wed, 13 Oct 2021 11:56:56 -0500 Message-ID: <20211013165713.727815-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211013165713.727815-1-brijesh.singh@amd.com> References: <20211013165713.727815-1-brijesh.singh@amd.com> MIME-Version: 1.0 Return-Path: brijesh.singh@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d4ae7c3c-e9c1-4ba8-8099-08d98e6a9a25 X-MS-TrafficTypeDiagnostic: CH2PR12MB3781: 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: 2cdnUCHrbM8JFiLQSPBWVFQmZcQYLtWkRQ4gyKbwzd8ytsDKVNkIUlLIZ9OKZZssUyQfJkodJE5xXBCTyCZy8PUy+mjfLoPTiz+Zme69vW1KX0ft5I+aJ8DNIZqYn5hMZ9KgMu3PhnqFwWaawAtaIR8iD/3DJ/7w4sK38NHN7zdaxfMjTdUX1iEnC34xsY/hS/vGk9YerkPqeyAqdJf9XEzzYWKzNAFlN1Biw/CwrmPYqNF+Kgz4RsiYancZb7QdPt2y9/ATxNt/nSjUCHyL/QSfDUdy0eUv8pwSuphKVBXF7DdYCMrA7AVchBGjk8AvlBRpjeN3MqiKqURQFx48pInGWDo2zNmou2VvHFPT+0n28lAU1sVK90DW2sr9osLHdSIqQGGtM6uxmoqg4pVmE7h8uPIyZZG/mvJaEpb/hhrWL3AQYGBwBILoazI/2T9+ABghfqwSWA4nvkQI32PMxOIwyP4zGeJbuV8mOZe17TXS5icNhZ90GDL0sGwF/vtjqaR9RiYCfg2y6Wuj3eMMAGqJfmjAkpw4LD7t2dp6bQMGOmXVQT6vU/nMVB/9a1uTy6cIcR1LGn5V8K0SDvnv/oLylYd+BWhe3LHQ0gfdjhf/0rV5kwN+jbEbnZv+MtTmGsHD69dECcV7/oMUmEr7C4oH9bpMSMGDHdjnkY8rUDD52+wEb1q61EZ/F8dpMDK/pSb7OyK18EqoPpZIogS5itpSQAvarhy5JSSauSdkUiJBnviLXJE+any6O1I4crcQ+ajHzNan/Krzl/b4Jtr3dYnO7gNLRk7T0QUghuZEeEq2cWHh2mn9TORc4HoSlAV5 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)(70206006)(4326008)(426003)(2616005)(1076003)(6916009)(70586007)(44832011)(36756003)(186003)(2906002)(16526019)(26005)(336012)(6666004)(356005)(47076005)(83380400001)(81166007)(82310400003)(36860700001)(86362001)(508600001)(966005)(316002)(54906003)(7696005)(19627235002)(8676002)(8936002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 16:57:55.1953 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d4ae7c3c-e9c1-4ba8-8099-08d98e6a9a25 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: CO1NAM11FT039.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB3781 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.25.1