From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (NAM10-MW2-obe.outbound.protection.outlook.com [40.107.94.44]) by mx.groups.io with SMTP id smtpd.web11.484.1609968186306194133 for ; Wed, 06 Jan 2021 13:23:06 -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=sHxpZIoN; 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.94.44, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QTCPjCA5rRnaUk6xcosit2tSUI3EEzbYan+wbgvyvPCiv0OReauQtkS5SCo9IOkHbLLA/cQLQli8qJJV+DePa0w7lWnILafj+CtF6EK/tRTprlEQJlfPtvkwo50nUmbJlz5Y3+jQscctzpeokgj3BSCJyPgcEaTFFk/nUJTgvqxCZQ36/zg9nM72X+Rop18B3yPruG5M+/Oee645Obv9x3Bp/da7CYPN21fV9s24tgKZQFc0gKGlV0zkRPc1sJ8+71M2UGU3xUKDrPSVXvQeUqA9d0GW0NBOpqhWwN79eQHNYSsAiUbdl2pIADY4G6A+b/V9y2Bys3IRWqUzQflV1w== 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=7l8Nd60Z0LXaRUDH9ZwcqF3uWycE9AZQtLcTrj/CPD4=; b=QVzHqND0EZt8n1foR+TtrwBYwyC2WbFbQuUzvpkuvAvO+83Rllci9egMmGg3CFiDhRlJsXU9/Zr+JIHw6r4i6OJZoY0bCXwvP73COIo3z4jDdBSU1rA6AIu4XHh1gHVFsucf95/d96n+8HKs1pFqh6X/qlsKFC/w2g6ce8bzEWlxRGy+7evMVIwh63c5G0uxMvIkY29TaIQLSf2lFnZSTBvjR5u/l8OPtFXABsoonfU1QvOgQgJkLiQ+ZqkKjSguODTAjN1fLT1H2pbCZfxB+PL9e0oWRsF86xzkxgwXrnONDhDTN5GR0Ch519yKpinx7wDlzn2VPc3aHHlddaEqEw== 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=7l8Nd60Z0LXaRUDH9ZwcqF3uWycE9AZQtLcTrj/CPD4=; b=sHxpZIoNY+ttSW4o3Q4KIvj7NpskutJvyZvZ7rO2ff1sWmzgsTpxkKlxs6cgUd/BKyqYskoqRsxj9v2vJqRRu8y4HxZX9LlnAVTsMg19VBgsiGEUKCGlJaF0bqT7ZixLs1QDHHnbjZWWU7+NJB1G7ML0cppVYRbKcq3stKyh1dM= 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 (2603:10b6:3:6e::7) by DM5PR12MB1259.namprd12.prod.outlook.com (2603:10b6:3:75::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6; Wed, 6 Jan 2021 21:23:03 +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.3721.024; Wed, 6 Jan 2021 21:23:03 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Rebecca Cran , Peter Grehan , Anthony Perard , Julien Grall Subject: [PATCH v2 06/15] OvmfPkg/MemEncryptSevLib: Add an interface to retrieve the encryption mask Date: Wed, 6 Jan 2021 15:21:32 -0600 Message-ID: X-Mailer: git-send-email 2.30.0 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0032.namprd11.prod.outlook.com (2603:10b6:806:d0::7) 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 SA0PR11CA0032.namprd11.prod.outlook.com (2603:10b6:806:d0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Wed, 6 Jan 2021 21:23:02 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 1f1c0e19-7710-4389-2ec5-08d8b2894074 X-MS-TrafficTypeDiagnostic: DM5PR12MB1259: 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: fcn2lMsOHvCvXjbPcMAMc9fW945PeqXfuCwR4Otd1gaI2FzeKDjBPbxsUIinyJ+eExkvm80ImWKcUnknM5z5bjLHU8PVr0tsENouSVYpX+4mWlOT01pOjqjW+VfuY5YbtAzGFcIKjrMUCRRWk4vGGgqWE+ch+z05aWhJQmCbPTiWchTPqeQ3XKaBJmwQhucuXezl0Z0gu+SVxP+0pZX1eG/5GbCvoSfITpwJL+ZLRRPiwMJU4NoZRy+m54g9LYg5EF+I2O5EKB0rMCtqf/atgzKW4hIOkcMmRcYu4l5ir73Vc70zn5JPIKZM9ucCoFv/rxH1k0X6YfZ8w9iYrjEccHCTGOO/YT+AXY+dqo4Q7o2EN4QNzet6V14ZufMIdQx1ijB8E9L4CIxkFzIrHrGk2+Rg9ian+evXGZDQF9SBsrZnOP92HVvcVN90pN2lGu1iBNLPq/tXHGBJMzaNfLdanw== 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)(136003)(366004)(346002)(39860400002)(396003)(19627235002)(6666004)(316002)(83380400001)(5660300002)(2906002)(52116002)(7696005)(36756003)(8676002)(8936002)(478600001)(54906003)(30864003)(966005)(66946007)(86362001)(2616005)(186003)(16526019)(4326008)(66476007)(26005)(6486002)(956004)(6916009)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?yJOdBSJuZV1rLSSV0CxnQUA7PoXtOqj5oi+FOnA3xyGbaCd7wK9oP23sjW45?= =?us-ascii?Q?Fnph+51b2ZIKD+j6Dwl8k2NvFtpn4tZWmW70RBqf7Mqj8+KvQhpRJBrzncnX?= =?us-ascii?Q?rUUTAP+JmRORKdEib9fShf9qRQaOdqX/6oE94iEgSDhpx+nq/567YLj87t3w?= =?us-ascii?Q?EfL59vxCU2NkqPbzxV3+zZu9Amb7qQf1T17v3ffuGXu4PH6WqF3DKrhjVxtp?= =?us-ascii?Q?KfSsHxKaXMp7B8UT5kqq3vmu8bmecQ+k54V1ROOiA6ZFdk9XrFtKbbpmGFIS?= =?us-ascii?Q?Qng60SSPX7NQPzK8BzSwe+UiZJfK2OmjNgE/evkGgilW0NhU5lEKHeCOb3wl?= =?us-ascii?Q?jUFb05K6jFTIarBT6WnpBfi8LVtwiWc8tEigH8ImGRTPUGKmk3SsdBAaQ6N9?= =?us-ascii?Q?ZUNSY8CYSwcDa2IuEhGlI7QO9KZHmPk9xqi8PqnE/js8BLipeKzM100cAf5F?= =?us-ascii?Q?i7sTX3amX5xOy0mo8zJPEr9RbOrBrTlsWtzpPjCPM6lVAAGV5D4nVNd1k6Se?= =?us-ascii?Q?G6D6PDu/IwmJkSpCf7GrilAqkgXcai5AMgA6qubdhFlOGD+DabxYDzvbCwpj?= =?us-ascii?Q?GFEdqc6Czio/7JYrO8GJiP1boE0OW7Qfqfzrt2OsKK7snj8CjnQyoXkKwme/?= =?us-ascii?Q?16yNNq24PExJTsNIs398OXOYxiXLm7hoKWvupOo0l+njd49dFM30pwMZ+Ooj?= =?us-ascii?Q?iX7BZAPfcg/JJq6+GhKc5SIj0AHS0+BlOx6pfLWkvbqdCAu4qqsmgb3DeeaE?= =?us-ascii?Q?VrqCZ4yZDsE17d1EndDiPbw4e/Afa61CRWuE7PJpHo9nlNiDrUn9kjRiCYF1?= =?us-ascii?Q?yeGdb8HO7fFctJzPnH76hfygwjbNwEUEoEVVqVubQVTTkThLdxfqv3Fp8YoD?= =?us-ascii?Q?PlBUQaO0kXjhXyjvxEEHZPff/DcbjZ8iOjup96LtI+agWKiJFnwYQJ3kRwUS?= =?us-ascii?Q?QZHJddRGUEhLTUlZT9aXNYVR4olqvzKmihzcV/ytseSIW3piWd0MHn6Sbl8A?= =?us-ascii?Q?NnM2?= 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: 06 Jan 2021 21:23:03.3365 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-Network-Message-Id: 1f1c0e19-7710-4389-2ec5-08d8b2894074 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qMqaw5uyVG0nNb45x4mfq98pYcUfBbrQaFhdjnPdQq1ko8DFhxAfrRXzx58ATHlOOVv22gkVMWMs82ssPAu8rw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1259 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 To ensure that we always use a validated encryption mask for an SEV-ES guest, create a new interface in the MemEncryptSevLib library to return the encryption mask. This can be used in place of the multiple locations where CPUID is used to retrieve the value (which would require validation again) and allows the validated mask to be returned. The PEI phase will use the value from the SEV-ES work area. Since the SEV-ES work area isn't valid in the DXE phase, the DXE phase will use the PcdPteMemoryEncryptionAddressOrMask PCD which is set during PEI. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Rebecca Cran Cc: Peter Grehan Cc: Anthony Perard Cc: Julien Grall Cc: Brijesh Singh Signed-off-by: Tom Lendacky --- OvmfPkg/AmdSev/AmdSevX64.dsc = | 4 +- OvmfPkg/Bhyve/BhyveX64.dsc = | 4 +- OvmfPkg/OvmfPkgIa32.dsc = | 4 +- OvmfPkg/OvmfPkgIa32X64.dsc = | 4 +- OvmfPkg/OvmfPkgX64.dsc = | 4 +- OvmfPkg/OvmfXen.dsc = | 3 +- OvmfPkg/Library/BaseMemEncryptSevLib/{BaseMemEncryptSevLib.inf =3D> DxeMem= EncryptSevLib.inf} | 15 +- OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf = | 56 +++++++ OvmfPkg/Include/Library/MemEncryptSevLib.h = | 12 ++ OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLibInternal.c = | 145 ++++++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c = | 94 +----------- OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLibInternal.c = | 159 ++++++++++++++++++++ 12 files changed, 400 insertions(+), 104 deletions(-) diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index bb7697eb324b..d39436e7afc9 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -164,7 +164,7 @@ [LibraryClasses] QemuFwCfgSimpleParserLib|OvmfPkg/Library/QemuFwCfgSimpleParserLib/QemuFw= CfgSimpleParserLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf @@ -285,6 +285,8 @@ [LibraryClasses.common.PEIM] Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.in= f !endif =20 + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc index b93fe30ae4e0..33edf3d2d6b5 100644 --- a/OvmfPkg/Bhyve/BhyveX64.dsc +++ b/OvmfPkg/Bhyve/BhyveX64.dsc @@ -163,7 +163,7 @@ [LibraryClasses] QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/BaseQemuFwCfgS3LibNull.inf BhyveFwCtlLib|OvmfPkg/Library/BhyveFwCtlLib/BhyveFwCtlLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf =20 CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf @@ -292,6 +292,8 @@ [LibraryClasses.common.PEIM] Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.in= f !endif =20 + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 26a013ec353e..75c5f46a7786 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -170,7 +170,7 @@ [LibraryClasses] QemuFwCfgSimpleParserLib|OvmfPkg/Library/QemuFwCfgSimpleParserLib/QemuFw= CfgSimpleParserLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif @@ -310,6 +310,8 @@ [LibraryClasses.common.PEIM] Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.in= f !endif =20 + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 10579fe46c5b..8693248b4ea0 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -174,7 +174,7 @@ [LibraryClasses] QemuFwCfgSimpleParserLib|OvmfPkg/Library/QemuFwCfgSimpleParserLib/QemuFw= CfgSimpleParserLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif @@ -314,6 +314,8 @@ [LibraryClasses.common.PEIM] Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.in= f !endif =20 + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index c9235e48ad62..799468e08e92 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -174,7 +174,7 @@ [LibraryClasses] QemuFwCfgSimpleParserLib|OvmfPkg/Library/QemuFwCfgSimpleParserLib/QemuFw= CfgSimpleParserLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif @@ -314,6 +314,8 @@ [LibraryClasses.common.PEIM] Tpm2DeviceLib|SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.in= f !endif =20 + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 12b7a87ee877..c13388ba5a05 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -161,7 +161,7 @@ [LibraryClasses] SerializeVariablesLib|OvmfPkg/Library/SerializeVariablesLib/SerializeVar= iablesLib.inf QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgDxeLib.inf QemuLoadImageLib|OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoad= ImageLib.inf - MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevL= ib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf @@ -273,6 +273,7 @@ [LibraryClasses.common.PEIM] QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/PeiQemuFwCfgS3LibFwCfg.inf PcdLib|MdePkg/Library/PeiPcdLib/PeiPcdLib.inf QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLi= b.inf =20 [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf = b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf similarity index 68% rename from OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf rename to OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf index 7c44d0952815..837db0876184 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf @@ -1,7 +1,7 @@ ## @file # Library provides the helper functions for SEV guest # -# Copyright (c) 2017 Advanced Micro Devices. All rights reserved.
+# Copyright (c) 2017 - 2020, Advanced Micro Devices. All rights reserved.<= BR> # # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -10,11 +10,11 @@ =20 [Defines] INF_VERSION =3D 1.25 - BASE_NAME =3D MemEncryptSevLib + BASE_NAME =3D DxeMemEncryptSevLib FILE_GUID =3D c1594631-3888-4be4-949f-9c630dbc842b MODULE_TYPE =3D BASE VERSION_STRING =3D 1.0 - LIBRARY_CLASS =3D MemEncryptSevLib|PEIM DXE_DRIVER DXE_= RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER + LIBRARY_CLASS =3D MemEncryptSevLib|DXE_DRIVER DXE_RUNTI= ME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER =20 # # The following information is for reference only and not required by the = build @@ -29,15 +29,17 @@ [Packages] OvmfPkg/OvmfPkg.dec UefiCpuPkg/UefiCpuPkg.dec =20 +[Sources] + DxeMemEncryptSevLibInternal.c + MemEncryptSevLibInternal.c + [Sources.X64] - MemEncryptSevLibInternal.c X64/MemEncryptSevLib.c X64/VirtualMemory.c X64/VirtualMemory.h =20 [Sources.IA32] Ia32/MemEncryptSevLib.c - MemEncryptSevLibInternal.c =20 [LibraryClasses] BaseLib @@ -49,3 +51,6 @@ [LibraryClasses] =20 [FeaturePcd] gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b= /OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf new file mode 100644 index 000000000000..7c29d14039d6 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf @@ -0,0 +1,56 @@ +## @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 PeiMemEncryptSevLib + FILE_GUID =3D 15d9a694-3d2a-4184-9672-ba55c3070e07 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D MemEncryptSevLib|PEIM + +# +# 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] + PeiMemEncryptSevLibInternal.c + MemEncryptSevLibInternal.c + +[Sources.X64] + X64/MemEncryptSevLib.c + X64/VirtualMemory.c + X64/VirtualMemory.h + +[Sources.IA32] + Ia32/MemEncryptSevLib.c + +[LibraryClasses] + BaseLib + CacheMaintenanceLib + CpuLib + DebugLib + MemoryAllocationLib + PcdLib + +[FeaturePcd] + gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire + +[FixedPcd] + gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/L= ibrary/MemEncryptSevLib.h index a2c70aa550fe..872abe6725dc 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -135,4 +135,16 @@ MemEncryptSevLocateInitialSmramSaveStateMapPages ( OUT UINTN *BaseAddress, OUT UINTN *NumberOfPages ); + +/** + Returns the SEV encryption mask. + + @return The SEV pagetable encryption mask +**/ +UINT64 +EFIAPI +MemEncryptSevGetEncryptionMask ( + VOID + ); + #endif // _MEM_ENCRYPT_SEV_LIB_H_ diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLibIntern= al.c b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLibInternal.c new file mode 100644 index 000000000000..2816f859a0c4 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLibInternal.c @@ -0,0 +1,145 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +STATIC BOOLEAN mSevStatus =3D FALSE; +STATIC BOOLEAN mSevEsStatus =3D FALSE; +STATIC BOOLEAN mSevStatusChecked =3D FALSE; + +STATIC UINT64 mSevEncryptionMask =3D 0; +STATIC BOOLEAN mSevEncryptionMaskSaved =3D FALSE; + +/** + Reads and sets the status of SEV features. + + **/ +STATIC +VOID +EFIAPI +InternalMemEncryptSevStatus ( + VOID + ) +{ + UINT32 RegEax; + MSR_SEV_STATUS_REGISTER Msr; + CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax; + BOOLEAN ReadSevMsr; + UINT64 EncryptionMask; + + ReadSevMsr =3D FALSE; + + EncryptionMask =3D PcdGet64 (PcdPteMemoryEncryptionAddressOrMask); + if (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; + } + } + } + + if (ReadSevMsr) { + // + // Check MSR_0xC0010131 Bit 0 (Sev Enabled) + // + Msr.Uint32 =3D AsmReadMsr32 (MSR_SEV_STATUS); + if (Msr.Bits.SevBit) { + mSevStatus =3D TRUE; + } + + // + // Check MSR_0xC0010131 Bit 1 (Sev-Es Enabled) + // + if (Msr.Bits.SevEsBit) { + mSevEsStatus =3D TRUE; + } + } + + mSevStatusChecked =3D TRUE; +} + +/** + 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 + ) +{ + if (!mSevStatusChecked) { + InternalMemEncryptSevStatus (); + } + + return mSevEsStatus; +} + +/** + Returns a boolean to indicate whether SEV is enabled. + + @retval TRUE SEV is enabled + @retval FALSE SEV is not enabled +**/ +BOOLEAN +EFIAPI +MemEncryptSevIsEnabled ( + VOID + ) +{ + if (!mSevStatusChecked) { + InternalMemEncryptSevStatus (); + } + + return mSevStatus; +} + +/** + Returns the SEV encryption mask. + + @return The SEV pagtable encryption mask +**/ +UINT64 +EFIAPI +MemEncryptSevGetEncryptionMask ( + VOID + ) +{ + if (!mSevEncryptionMaskSaved) { + mSevEncryptionMask =3D PcdGet64 (PcdPteMemoryEncryptionAddressOrMask); + mSevEncryptionMaskSaved =3D TRUE; + } + + return mSevEncryptionMask; +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.= c b/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c index 02b8eb225d81..b4a9f464e268 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c @@ -2,7 +2,7 @@ =20 Secure Encrypted Virtualization (SEV) library helper function =20 - Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -12,102 +12,10 @@ #include #include #include -#include -#include -#include #include #include #include =20 -STATIC BOOLEAN mSevStatus =3D FALSE; -STATIC BOOLEAN mSevEsStatus =3D FALSE; -STATIC BOOLEAN mSevStatusChecked =3D FALSE; - -/** - Reads and sets the status of SEV features. - - **/ -STATIC -VOID -EFIAPI -InternalMemEncryptSevStatus ( - VOID - ) -{ - UINT32 RegEax; - MSR_SEV_STATUS_REGISTER Msr; - CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax; - - // - // 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, NULL)= ; - - if (Eax.Bits.SevBit) { - // - // Check MSR_0xC0010131 Bit 0 (Sev Enabled) - // - Msr.Uint32 =3D AsmReadMsr32 (MSR_SEV_STATUS); - if (Msr.Bits.SevBit) { - mSevStatus =3D TRUE; - } - - // - // Check MSR_0xC0010131 Bit 1 (Sev-Es Enabled) - // - if (Msr.Bits.SevEsBit) { - mSevEsStatus =3D TRUE; - } - } - } - - mSevStatusChecked =3D TRUE; -} - -/** - 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 - ) -{ - if (!mSevStatusChecked) { - InternalMemEncryptSevStatus (); - } - - return mSevEsStatus; -} - -/** - Returns a boolean to indicate whether SEV is enabled. - - @retval TRUE SEV is enabled - @retval FALSE SEV is not enabled -**/ -BOOLEAN -EFIAPI -MemEncryptSevIsEnabled ( - VOID - ) -{ - if (!mSevStatusChecked) { - InternalMemEncryptSevStatus (); - } - - return mSevStatus; -} - - /** Locate the page range that covers the initial (pre-SMBASE-relocation) SM= RAM Save State Map. diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLibIntern= al.c b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLibInternal.c new file mode 100644 index 000000000000..e2fd109d120f --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLibInternal.c @@ -0,0 +1,159 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2020, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +STATIC BOOLEAN mSevStatus =3D FALSE; +STATIC BOOLEAN mSevEsStatus =3D FALSE; +STATIC BOOLEAN mSevStatusChecked =3D FALSE; + +STATIC UINT64 mSevEncryptionMask =3D 0; +STATIC BOOLEAN mSevEncryptionMaskSaved =3D FALSE; + +/** + Reads and sets the status of SEV features. + + **/ +STATIC +VOID +EFIAPI +InternalMemEncryptSevStatus ( + VOID + ) +{ + UINT32 RegEax; + MSR_SEV_STATUS_REGISTER Msr; + 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; + } + } + } + + if (ReadSevMsr) { + // + // Check MSR_0xC0010131 Bit 0 (Sev Enabled) + // + Msr.Uint32 =3D AsmReadMsr32 (MSR_SEV_STATUS); + if (Msr.Bits.SevBit) { + mSevStatus =3D TRUE; + } + + // + // Check MSR_0xC0010131 Bit 1 (Sev-Es Enabled) + // + if (Msr.Bits.SevEsBit) { + mSevEsStatus =3D TRUE; + } + } + + mSevStatusChecked =3D TRUE; +} + +/** + 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 + ) +{ + if (!mSevStatusChecked) { + InternalMemEncryptSevStatus (); + } + + return mSevEsStatus; +} + +/** + Returns a boolean to indicate whether SEV is enabled. + + @retval TRUE SEV is enabled + @retval FALSE SEV is not enabled +**/ +BOOLEAN +EFIAPI +MemEncryptSevIsEnabled ( + VOID + ) +{ + if (!mSevStatusChecked) { + InternalMemEncryptSevStatus (); + } + + return mSevStatus; +} + +/** + Returns the SEV encryption mask. + + @return The SEV pagtable encryption mask +**/ +UINT64 +EFIAPI +MemEncryptSevGetEncryptionMask ( + VOID + ) +{ + if (!mSevEncryptionMaskSaved) { + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + + SevEsWorkArea =3D (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWork= AreaBase); + if (SevEsWorkArea !=3D NULL) { + mSevEncryptionMask =3D SevEsWorkArea->EncryptionMask; + } else { + CPUID_MEMORY_ENCRYPTION_INFO_EBX Ebx; + + // + // CPUID Fn8000_001F[EBX] Bit 0:5 (memory encryption bit position) + // + AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, NULL, &Ebx.Uint32, NULL, NUL= L); + mSevEncryptionMask =3D LShiftU64 (1, Ebx.Bits.PtePosBits); + } + + mSevEncryptionMaskSaved =3D TRUE; + } + + return mSevEncryptionMask; +} --=20 2.30.0