From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com [40.107.244.85]) by mx.groups.io with SMTP id smtpd.web10.2819.1608065553325342555 for ; Tue, 15 Dec 2020 12:52:33 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=cRqadjcw; 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.244.85, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RgjeS9Qhba7yXKvgI2g7hBXABcLrwRfWJ3yU1UMEAlm3vilsUjQRYHVV7LCWsCFIpt0ccyHXA+puGPiMnS0hXI5Mt6HdXhnUI1cYth9RzdtZuT4G2BnxgAi+sk3kq7LwsDn/P9OHTavyjRpe5ybKRT8nZlf1IEz6RvJPfDrv/2WTZ0weABp9/Uot5A6F6QTlG6TKAlTDaH8qAKg0BggiqmokSo0F/F8+JWyvwAeLMF8JO48UFUzr/jP1u/l3QzNOdS399ZeIoCbq/Sfys7m4TKScediQUhCW5APy9vVEUKnORbUqVzJcmTvebSZHIGu2vQ+qKIK1njE7aucOMET3og== 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-SenderADCheck; bh=fs5vg0Dzj+E6kqDtTd74gz4qJXbn1XPKSNWq6ts4b6k=; b=aEZ4rkqS/3h3lKase/1ToK1VUTD8P4eDuEG0AXe4E7z0uL3Pj6wANqo5vycCS4Ti0jY6BPNa7xaIZbK/Ax6o3I+WcMHMFHU/zoCY7nqLqdkehkE65GlA5YkPhsTEaZvAYXxXgruZXFl/vhQZNBkVRJFXA3ctQS4rfeTziVqr9ulQ12xmPrjnvX0eRjrocuNABqUhdl3peK+lbdG9w048ADKxUOoKzZxeGgHYd+xzz43Ft2VYmfEzDo/fFKwodxupRC5+AC3kdbp2ItiIho5eiAaKEwTKk3z1uCjrBhb61XK4/ZiraB80j1CEHaOcJ8my6CQ6Cy5pNEd8iV2xaql7tw== 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=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fs5vg0Dzj+E6kqDtTd74gz4qJXbn1XPKSNWq6ts4b6k=; b=cRqadjcwTNvYeqT8EX4tkHJyfb63UsB+XguP7ZE3RVbo8IK+vRm4+lO98GMjyhrG8ue1In1pE8RMq88E1FaMGxR4qcRtUGvOCM2ppbm1jaqK9oZH5Rg50b+/84utmTfhL5XvOEQYz3TBMFVYNoHZ9G/B8enagCTl33tq49No578= 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 DM5PR12MB1355.namprd12.prod.outlook.com (10.168.234.7) by DM6PR12MB4155.namprd12.prod.outlook.com (10.141.8.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.15; Tue, 15 Dec 2020 20:52:30 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845%12]) with mapi id 15.20.3654.025; Tue, 15 Dec 2020 20:52:30 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Jordan Justen , Laszlo Ersek , Ard Biesheuvel Subject: [PATCH 08/12] OvmfPkg/MemEncryptSevLib: Make the MemEncryptSevLib available for SEC Date: Tue, 15 Dec 2020 14:51:07 -0600 Message-ID: <6b14be9c75dd31656e986c8e7611b739c2b22a9e.1608065471.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CH2PR03CA0001.namprd03.prod.outlook.com (2603:10b6:610:59::11) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from tlendack-t1.amd.com (165.204.77.1) by CH2PR03CA0001.namprd03.prod.outlook.com (2603:10b6:610:59::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Tue, 15 Dec 2020 20:52:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 4729cff1-26c6-42fa-5c40-08d8a13b56ff X-MS-TrafficTypeDiagnostic: DM6PR12MB4155: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0tEHwOjqrZT0fqwacvsyI487IHh818B3BKXtTp0ntT+65j311T7DBPfHdX+o5nWEE3KxTy9K9TL32k/64Oa625wk0tIYgckMH7d8lX3jyVIk56NQNC2BA3Bivx80Kbiklv2Nmj0Z+JNLGOeFkLM6GyvV3R38rgqKIAD5Q0Gy3C8eWM4/3UxOC/EqNE7dWvcAPPbs2md4Ga70Mvk/3xYOg9F9VeFKFedYKM7A+UJDxssYhYIGVWqQHzThjh409sz7XrOkmMbp0nrJWtWcWzWdFcpgy+5a6l6YTsRKdmUmLAQ2QWXpatdpfK5Lb8yuWKfO4fQ0AbpDKpQHCQ4D1grpo9l1KarI1n4hfHvU7IHyFdzPIdrjxE7E0kPx2jLx+rFiX9cJBya+ZrZQUco9WA7waPPPcZJVRdayMqZkpzPzvjTp31QZTJgrmlwM59PX9Vt0uTE3TZraLtvBKgeZKV8TwA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(366004)(136003)(346002)(66946007)(26005)(4326008)(19627235002)(16526019)(6916009)(8676002)(36756003)(508600001)(52116002)(30864003)(6666004)(83380400001)(5660300002)(8936002)(966005)(186003)(54906003)(956004)(2616005)(66476007)(2906002)(86362001)(34490700003)(6486002)(66556008)(7696005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?knpfY2sx4sPHW4CPsxz4+3ZydgfztGfw+0/ynIKT4291rtU2PLC9yzOxByeI?= =?us-ascii?Q?subCQHu2Um2o0fd6S7YIR3U1J7ME6H8j2HLpxv/PacdSMLzuDVvMOc6eOUIX?= =?us-ascii?Q?WqjUK5xln/1I9a4/VSMXwvoJ5D9Zr36Qg7YxhQ2fiMgJxAPDzBNhOJSl+1RY?= =?us-ascii?Q?YlCgUirCBwG4/Eivsfj7xR6i/xUJHdpwS18M1N+RbbOkmKLt5MsAQSLi1TDE?= =?us-ascii?Q?OO4X+EDwGg7qiiL04snQDfhj/TVZeoQkh4k6QAmA+QJ401Odif9JBui6LOQY?= =?us-ascii?Q?3SEtv8qg+pIndcoANaBW6nUhU30gzpAAW4E3c61oxYtvwchKgmoiCKjd9ebu?= =?us-ascii?Q?MluH9SyOS1GzXf63gnWYXwxp6U7Y+E00+Be1D7nuB+mx7HI+yxfbPtv4kmD/?= =?us-ascii?Q?0GLv11rb7ihEEbMQn4HMgka4mJDqmmmDTiloz3IX6CgMEVWpPSzIGrb5kXHh?= =?us-ascii?Q?/A3VMFLaW7WHK9C9C90LXVIkM4ObULBjbTiJgQs3/tP4YBhKp3HvRKUllRUA?= =?us-ascii?Q?9jLVtQ1/e9oWGZF2+tmCByDlIMca6p5WQGdBhhBuRtWj2wSnvr5oC5N2z0Tj?= =?us-ascii?Q?bbTJg5duNya+oBZRYVCu2K+tln68GG7vyoOw8ATLSHvURteDr4kZH7mfRMMg?= =?us-ascii?Q?j1fXfgaXX+J9M/Nd62InA1+DF0bEqCXbHp9/WxFdPJLT10oMzUmEWVZbYk2B?= =?us-ascii?Q?2vLn8PpueKXUPp/6Oe8kXEFuK41GQcQThzv5cO/l88RVuFPkXu++2Vz0g+w5?= =?us-ascii?Q?FoqtX47i9N9DzHwy3MbFCyJqB7HAPmXklPd+IXCtdLN9jGwZ6i79GGBvHUhs?= =?us-ascii?Q?4NtFH5L3FthZ1ShgqeTMa2837P7JxPKgmryIXlDnalt/W6vCV/AWoxG8atyc?= =?us-ascii?Q?jR3L3qmGKPfvFgWqju5VCFj+69riBWukYM91oE0KTYQ8sGCWjF3kNfcxLfJP?= =?us-ascii?Q?9S0AEYz1ST3mxGMk+PxxCh/TmA8wQm6sauFDP7wtmAvVngsRxU6lLa7e89Ms?= =?us-ascii?Q?zuQi?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Dec 2020 20:52:30.5914 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-Network-Message-Id: 4729cff1-26c6-42fa-5c40-08d8a13b56ff X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: F4a4IjxVZWJgTkC4qORvr4Aop4ez0pbZ10C1oh5HHEndv8ZJppcE8ExcT3prC4w6lpW+UIeBGGBb3LfhD4fRuA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4155 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3108 In preparation for a new interface to be added to the MemEncryptSevLib library that will be used in SEC, create an SEC version of the library. This requires the creation of SEC specific files. Some of the current MemEncryptSevLib functions perform memory allocations which cannot be performed in SEC, so these interfaces will return an error during SEC. Also, the current MemEncryptSevLib library uses some static variables to optimize access to variables, which cannot be used in SEC. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Brijesh Singh Signed-off-by: Tom Lendacky --- .../DxeBaseMemEncryptSevLib.inf | 2 +- .../PeiBaseMemEncryptSevLib.inf | 2 +- .../SecBaseMemEncryptSevLib.inf | 54 ++++++++ .../SecMemEncryptSevLibInternal.c | 130 ++++++++++++++++++ ...{VirtualMemory.c =3D> PeiDxeVirtualMemory.c} | 12 +- .../X64/SecVirtualMemory.c | 80 +++++++++++ 6 files changed, 272 insertions(+), 8 deletions(-) create mode 100644 OvmfPkg/Library/BaseMemEncryptSevLib/SecBaseMemEncryptS= evLib.inf create mode 100644 OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLi= bInternal.c rename OvmfPkg/Library/BaseMemEncryptSevLib/X64/{VirtualMemory.c =3D> PeiD= xeVirtualMemory.c} (95%) create mode 100644 OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecVirtualMemo= ry.c diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeBaseMemEncryptSevLib.i= nf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeBaseMemEncryptSevLib.inf index 2be6ca1fa737..390f2d60677f 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeBaseMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeBaseMemEncryptSevLib.inf @@ -33,7 +33,7 @@ [Sources.X64] DxeMemEncryptSevLibInternal.c MemEncryptSevLibInternal.c X64/MemEncryptSevLib.c - X64/VirtualMemory.c + X64/PeiDxeVirtualMemory.c X64/VirtualMemory.h =20 [Sources.IA32] diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiBaseMemEncryptSevLib.i= nf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiBaseMemEncryptSevLib.inf index 7bdf8cb5210d..cb973fdeb868 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiBaseMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiBaseMemEncryptSevLib.inf @@ -33,7 +33,7 @@ [Sources.X64] PeiMemEncryptSevLibInternal.c MemEncryptSevLibInternal.c X64/MemEncryptSevLib.c - X64/VirtualMemory.c + X64/PeiDxeVirtualMemory.c X64/VirtualMemory.h =20 [Sources.IA32] diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/SecBaseMemEncryptSevLib.i= nf b/OvmfPkg/Library/BaseMemEncryptSevLib/SecBaseMemEncryptSevLib.inf new file mode 100644 index 000000000000..b26f739d69fd --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/SecBaseMemEncryptSevLib.inf @@ -0,0 +1,54 @@ +## @file +# Library provides the helper functions for SEV guest +# +# Copyright (c) 2020 Advanced Micro Devices. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 1.25 + BASE_NAME =3D SecMemEncryptSevLib + FILE_GUID =3D 046388b4-430e-4e61-88f6-51ea21db2632 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D MemEncryptSevLib|SEC + +# +# The following information is for reference only and not required by the = build +# tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[Sources.X64] + SecMemEncryptSevLibInternal.c + MemEncryptSevLibInternal.c + X64/MemEncryptSevLib.c + X64/SecVirtualMemory.c + X64/VirtualMemory.h + +[Sources.IA32] + SecMemEncryptSevLibInternal.c + MemEncryptSevLibInternal.c + Ia32/MemEncryptSevLib.c + +[LibraryClasses] + BaseLib + CpuLib + DebugLib + PcdLib + +[FeaturePcd] + gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire + +[FixedPcd] + gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibIntern= al.c b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c new file mode 100644 index 000000000000..30d2ebe1d6e9 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c @@ -0,0 +1,130 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + Reads and sets the status of SEV features. + + **/ +STATIC +UINT32 +EFIAPI +InternalMemEncryptSevStatus ( + VOID + ) +{ + UINT32 RegEax; + CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax; + BOOLEAN ReadSevMsr; + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + + ReadSevMsr =3D FALSE; + + SevEsWorkArea =3D (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAr= eaBase); + if (SevEsWorkArea !=3D NULL && SevEsWorkArea->EncryptionMask !=3D 0) { + // + // The MSR has been read before, so it is safe to read it again and av= oid + // having to validate the CPUID information. + // + ReadSevMsr =3D TRUE; + } else { + // + // Check if memory encryption leaf exist + // + AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL); + if (RegEax >=3D CPUID_MEMORY_ENCRYPTION_INFO) { + // + // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported) + // + AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &Eax.Uint32, NULL, NULL, NUL= L); + + if (Eax.Bits.SevBit) { + ReadSevMsr =3D TRUE; + } + } + } + + return ReadSevMsr ? AsmReadMsr32 (MSR_SEV_STATUS) : 0; +} + +/** + Returns a boolean to indicate whether SEV-ES is enabled. + + @retval TRUE SEV-ES is enabled + @retval FALSE SEV-ES is not enabled +**/ +BOOLEAN +EFIAPI +MemEncryptSevEsIsEnabled ( + VOID + ) +{ + MSR_SEV_STATUS_REGISTER Msr; + + Msr.Uint32 =3D InternalMemEncryptSevStatus (); + + return Msr.Bits.SevEsBit ? TRUE : FALSE; +} + +/** + Returns a boolean to indicate whether SEV is enabled. + + @retval TRUE SEV is enabled + @retval FALSE SEV is not enabled +**/ +BOOLEAN +EFIAPI +MemEncryptSevIsEnabled ( + VOID + ) +{ + MSR_SEV_STATUS_REGISTER Msr; + + Msr.Uint32 =3D InternalMemEncryptSevStatus (); + + return Msr.Bits.SevBit ? TRUE : FALSE; +} + +/** + Returns the SEV encryption mask. + + @return The SEV pagtable encryption mask +**/ +UINT64 +EFIAPI +MemEncryptSevGetEncryptionMask ( + VOID + ) +{ + CPUID_MEMORY_ENCRYPTION_INFO_EBX Ebx; + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + UINT64 EncryptionMask; + + SevEsWorkArea =3D (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAr= eaBase); + if (SevEsWorkArea !=3D NULL) { + EncryptionMask =3D SevEsWorkArea->EncryptionMask; + } else { + // + // CPUID Fn8000_001F[EBX] Bit 0:5 (memory encryption bit position) + // + AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, NULL, &Ebx.Uint32, NULL, NULL)= ; + EncryptionMask =3D LShiftU64 (1, Ebx.Bits.PtePosBits); + } + + return EncryptionMask; +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c b/Ovm= fPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c similarity index 95% rename from OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c rename to OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c index 6422bc53bd5d..3a5bab657bd7 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c @@ -192,7 +192,8 @@ Split2MPageTo4K ( { PHYSICAL_ADDRESS PhysicalAddress4K; UINTN IndexOfPageTableEntries; - PAGE_TABLE_4K_ENTRY *PageTableEntry, *PageTableEntry1; + PAGE_TABLE_4K_ENTRY *PageTableEntry; + PAGE_TABLE_4K_ENTRY *PageTableEntry1; UINT64 AddressEncMask; =20 PageTableEntry =3D AllocatePageTableMemory(1); @@ -472,7 +473,7 @@ Split1GPageTo2M ( /** Set or Clear the memory encryption bit =20 - @param[in] PagetablePoint Page table entry pointer (PTE). + @param[in, out] PageTablePointer Page table entry pointer (PTE). @param[in] Mode Set or Clear encryption bit =20 **/ @@ -562,7 +563,6 @@ EnableReadOnlyPageWriteProtect ( @retval RETURN_UNSUPPORTED Setting the memory encyrption attrib= ute is not supported **/ - STATIC RETURN_STATUS EFIAPI @@ -635,7 +635,7 @@ SetMemoryEncDec ( =20 Status =3D EFI_SUCCESS; =20 - while (Length) + while (Length !=3D 0) { // // If Cr3BaseAddress is not specified then read the current CR3 @@ -683,7 +683,7 @@ SetMemoryEncDec ( // Valid 1GB page // If we have at least 1GB to go, we can just update this entry // - if (!(PhysicalAddress & (BIT30 - 1)) && Length >=3D BIT30) { + if ((PhysicalAddress & (BIT30 - 1)) =3D=3D 0 && Length >=3D BIT30) { SetOrClearCBit(&PageDirectory1GEntry->Uint64, Mode); DEBUG (( DEBUG_VERBOSE, @@ -744,7 +744,7 @@ SetMemoryEncDec ( // Valid 2MB page // If we have at least 2MB left to go, we can just update this ent= ry // - if (!(PhysicalAddress & (BIT21-1)) && Length >=3D BIT21) { + if ((PhysicalAddress & (BIT21-1)) =3D=3D 0 && Length >=3D BIT21) { SetOrClearCBit (&PageDirectory2MEntry->Uint64, Mode); PhysicalAddress +=3D BIT21; Length -=3D BIT21; diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecVirtualMemory.c b/= OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecVirtualMemory.c new file mode 100644 index 000000000000..5c337ea0b820 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecVirtualMemory.c @@ -0,0 +1,80 @@ +/** @file + + Virtual Memory Management Services to set or clear the memory encryption= bit + + Copyright (c) 2020, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include "VirtualMemory.h" + +/** + This function clears memory encryption bit for the memory region specifi= ed by + PhysicalAddress and Length from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use + current CR3) + @param[in] PhysicalAddress The physical address that is the sta= rt + address of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the + memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing the memory encyrption attri= bute + is not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryDecrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length, + IN BOOLEAN Flush + ) +{ + // + // This function is not available during SEC. + // + return RETURN_UNSUPPORTED; +} + +/** + This function sets memory encryption bit for the memory region specified= by + PhysicalAddress and Length from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use + current CR3) + @param[in] PhysicalAddress The physical address that is the sta= rt + address of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were set for the memo= ry + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attrib= ute + is not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryEncrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length, + IN BOOLEAN Flush + ) +{ + // + // This function is not available during SEC. + // + return RETURN_UNSUPPORTED; +} --=20 2.28.0