From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.67]) by mx.groups.io with SMTP id smtpd.web10.394.1636738832129386378 for ; Fri, 12 Nov 2021 09:40:32 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=2Yzq9p7O; 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.220.67, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WqjrrQNq+GlRpcmpxt1IvTM1DtoEoXOW9rgC/CX0/vBcZKl6Ow2PHRpu75psrqLFqoqIv1wVrG8ttO3esNtdNWQDtjfNMTN+mRxiKsQCPeMvabIKC9JBr+c7CmiqS+cnsTToQGkud/1mMwb+cBbEkeZ4vK1guS+ZfzgQguPXZnUDNZ2j10OTOxQQGhP6RzGVTw8ZBLOzXwzAK6uNhi50blMK6jY8N/da1NJC3865++zLx0ZO7H90Mwg6uqT2KxiJ2UcPK6LHy29wzZpRIoTQEMuviGazfUACLKB6EDa4Y3+5vbeCix9k3feRCtxiQe+rHxY3rzLcc7w9YLP1zw0fLQ== 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=Q78Y9dUTMjc1Mm3ohrlMeD11Wx1hYFU9rEyg7kTg4Eg=; b=kikudA5zyXzuPIBQvlas9K1hHXjgsR94tALkNkxzl3qmfIHPJ5RjvYXCNGqSa8nfFIfwA/p4gHU8zoVkFUNS0fqgxIMDi5pgttJ/ZRXh7kWjnfTL00FSNwlvKyZR2+toMuiiSYYpU+w2QSgyeQp8wYSu/n/aAsQAYX1kha9GtNdy3xRuHC6k/ruLzJmbKWPO21NAOmVb+t8mMuz7F4CYN7l28C3OPFrvC2Tn3S7E2Hlffji7Ut1R3Xn+7wZI+E0ZKtVlT7em2LlmtQDSu/POo4PKIiSEJygp3C7qlH6kJvlXeU7U0eeqF6+MD2O4MKDjJU3OUFOwGFlyohLa6fvx+Q== 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=Q78Y9dUTMjc1Mm3ohrlMeD11Wx1hYFU9rEyg7kTg4Eg=; b=2Yzq9p7OoCMw+IL9JKoS5OpnkY8iFnfiO41cySfmAYIRRYRxGWeOKS87aTHOPPuT1Q9x38CTe6kN98W43Ndqz/flr/lNArv4oFDFxRq4XaaaALEEsQJZWQVG7X1jmkUWnX/CjVKeCEPWMgr2R1TU6r0HoE2pVdOT0+wYfAWNBcE= Received: from BN9PR03CA0315.namprd03.prod.outlook.com (2603:10b6:408:112::20) by DM6PR12MB4434.namprd12.prod.outlook.com (2603:10b6:5:2ad::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4690.17; Fri, 12 Nov 2021 17:40:30 +0000 Received: from BN8NAM11FT009.eop-nam11.prod.protection.outlook.com (2603:10b6:408:112:cafe::c4) by BN9PR03CA0315.outlook.office365.com (2603:10b6:408:112::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.11 via Frontend Transport; Fri, 12 Nov 2021 17:40:30 +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=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT009.mail.protection.outlook.com (10.13.176.65) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4690.15 via Frontend Transport; Fri, 12 Nov 2021 17:40:30 +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.2375.17; Fri, 12 Nov 2021 11:40:29 -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 , Jiewen Yao Subject: [PATCH v13 16/32] OvmfPkg/BaseMemEncryptSevLib: skip the pre-validated system RAM Date: Fri, 12 Nov 2021 11:39:43 -0600 Message-ID: <20211112173959.2505972-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112173959.2505972-1-brijesh.singh@amd.com> References: <20211112173959.2505972-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 SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a85a7f79-86f6-46bc-6ede-08d9a6038593 X-MS-TrafficTypeDiagnostic: DM6PR12MB4434: 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: v+tS2Im4Nba4s7ATI+TtkO5ObUqLMJYe8hedi9aK9vOSIP5BqMuXc+vZpEukk7Qjo+g+4z3h5dQ9m7sYwKBNkAfK2DxkIyWB6u7C/3ERXXNGcqDYnoeVP7TIkmmdcv6ouJZUPpeXC+rKbyUW5ScXJKs598a9UqYdFGVQTd/76vwL3qJN1AU7C1E3XcGoDgx2IZsrCj8KKQuxINMkoIQKCeScmhDYyxsXPDAfv2q24yRnDbZ+JRRtW3Xg4dEHRKZ4aMEewBQbtGK6ctRKO9LlhAXC5NzPBVUWwlIA0vaPStsn2NolTPwBP+1eoxDtZur3RpSINX2a6T8r3JCzda4D87qGhCLKv3kNPuZEGw/qS/HwqzThNU1JTv+7cpKIN2e6McsdVtiygqtO9X7MbROtfIUV5StY+p0GKSHSlHTKjxhPCrVZyUUF3cehygC8qTcM4g1qSit1AbVzAiSVb2j0Gs8XGRJA6hGXMrlz4vJg3DrK3ADHRz8HvkkTS6wKeDchxKNaCxoM0Gc8JBvcoFOVx3x5P5+AGS8C8afEDzZeI4BaEJPb8pYnn0RzOLx2quxnyThNm1XLzMcol2bNC0U9XHS91aqzS7PEQZ2+YcFMPQksW2slIumNDs66IV0KOty6Zw8V8/fDPX43epTA0xE/kJ4xuOGRS3o4o+ZwaAlCpywx56eqya749Aw8/sUIAu5YmBf9hTiyhary0HnQfif4RnJbsEka27qc2B/RSfvrOmIYGaXNqMtlz9/zL3JDHVNa0sIFfiUpCiihMJuKMJZg9xK7611UTeJMC3IBLzHxfyWZfpwDQl7XXEY61vn25mibPaSAizBgQNBexNLooMQN2qUQl2zPZPWpiyqpsyW7DYQ= 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)(2616005)(70586007)(4326008)(316002)(83380400001)(426003)(356005)(47076005)(19627235002)(2906002)(70206006)(6666004)(7696005)(5660300002)(36756003)(186003)(81166007)(8676002)(54906003)(86362001)(336012)(15650500001)(26005)(8936002)(1076003)(82310400003)(36860700001)(44832011)(966005)(6916009)(508600001)(16526019)(7416002)(213903007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Nov 2021 17:40:30.5350 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a85a7f79-86f6-46bc-6ede-08d9a6038593 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: BN8NAM11FT009.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4434 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The MemEncryptSevSnpPreValidateSystemRam() is used for pre-validating the system RAM. As the boot progress, each phase validates a fixed region of the RAM. In the PEI phase, the PlatformPei detects all the available RAM and calls to pre-validate the detected system RAM. While validating the system RAM in PEI phase, we must skip previously validated system RAM to avoid the double validation. 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: Jiewen Yao Acked-by: Gerd Hoffmann Signed-off-by: Brijesh Singh --- .../PeiMemEncryptSevLib.inf | 2 + .../X64/PeiSnpSystemRamValidate.c | 66 ++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b= /OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf index 0402e49a1028..49d5bd1beff1 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf @@ -58,3 +58,5 @@ [FeaturePcd] =20 [FixedPcd] gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValida= te.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c index 64aab7f45b6d..cea7ecf96563 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -14,6 +14,45 @@ =20 #include "SnpPageStateChange.h" =20 +typedef struct { + UINT64 StartAddress; + UINT64 EndAddress; +} SNP_PRE_VALIDATED_RANGE; + +STATIC SNP_PRE_VALIDATED_RANGE mPreValidatedRange[] =3D { + // The below address range was part of the SEV OVMF metadata, and range + // should be pre-validated by the Hypervisor. + { + FixedPcdGet32 (PcdOvmfSecPageTablesBase), + FixedPcdGet32 (PcdOvmfPeiMemFvBase), + }, +}; + +STATIC +BOOLEAN +DetectPreValidatedOverLap ( + IN PHYSICAL_ADDRESS StartAddress, + IN PHYSICAL_ADDRESS EndAddress, + OUT SNP_PRE_VALIDATED_RANGE *OverlapRange + ) +{ + UINTN i; + + // + // Check if the specified address range exist in pre-validated array. + // + for (i =3D 0; i < ARRAY_SIZE (mPreValidatedRange); i++) { + if ((mPreValidatedRange[i].StartAddress < EndAddress) && + (StartAddress < mPreValidatedRange[i].EndAddress)) { + OverlapRange->StartAddress =3D mPreValidatedRange[i].StartAddress; + OverlapRange->EndAddress =3D mPreValidatedRange[i].EndAddress; + return TRUE; + } + } + + return FALSE; +} + /** Pre-validate the system RAM when SEV-SNP is enabled in the guest VM. =20 @@ -28,9 +67,34 @@ MemEncryptSevSnpPreValidateSystemRam ( IN UINTN NumPages ) { + PHYSICAL_ADDRESS EndAddress; + SNP_PRE_VALIDATED_RANGE OverlapRange; + if (!MemEncryptSevSnpIsEnabled ()) { return; } =20 - InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); + EndAddress =3D BaseAddress + EFI_PAGES_TO_SIZE (NumPages); + + while (BaseAddress < EndAddress) { + // + // Check if the range overlaps with the pre-validated ranges. + // + if (DetectPreValidatedOverLap (BaseAddress, EndAddress, &OverlapRange)= ) { + // Validate the non-overlap regions. + if (BaseAddress < OverlapRange.StartAddress) { + NumPages =3D EFI_SIZE_TO_PAGES (OverlapRange.StartAddress - BaseAd= dress); + + InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TR= UE); + } + + BaseAddress =3D OverlapRange.EndAddress; + continue; + } + + // Validate the remaining pages. + NumPages =3D EFI_SIZE_TO_PAGES (EndAddress - BaseAddress); + InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); + BaseAddress =3D EndAddress; + } } --=20 2.25.1