From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.86]) by mx.groups.io with SMTP id smtpd.web09.2442.1636582529627020090 for ; Wed, 10 Nov 2021 14:15:29 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=3pUi7jN4; 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.223.86, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fiJpyT9E6YhXhy0zSOY5mqLFTft3/6KJqmMIC2p/8nU4f4Y4VhNT8Ozo4OiggskJuD6qogUyn+d6vl9GngpKC8QkMHngswdzqo/IxnCGxSelI60qhUdlTCBlKN5Az35j6U0xyB/6/JTCa80piF1+X3BdQshMw4xR1fhloJzPLAsTVk7ud8PPIFtzWhsu3Th2eXNbLJl6R+02TH2aArXbKOxkyBf4hM79EuEW5IkbeWzWUAMoC9KUR2FmHH/KPQb5SZ1siUKJFdGcdTQnWWUtIHE3uu5IGFBbCYub+M6X0vazDkIzzy4W6t42I46wfSCjI7uvsZKDjiGfaoO4JczDtw== 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=lByKUDqZ38X4Ml/a+IH9AieqO6OzpJxy2kmUsWNmayGAkvLwvGMZ5XlKQMeMs8IAZJttn5aRz52YiboTOflGvKOecGmzhA+VKfvElDRg7mILG9xjo7Cb/Wr3gjhMF69NB3zqjJ5anDKbDVPnCa4XJBvuHccjHACo27zKTCot+z4Am62NaaO8JVUxbf7pMDFksjLQQXEib+7je52SrTWbBs94IMI2+vlKzXliU0aRM0LYNvVHrZhnBnDIYCIyUCkKspREgs+d9xs5x13TdiYacjytHxCLOjxjQWtaGMTmMdbsm+/QmvUnSQiDmD9+G3iqH1M1F9yast9ZP9BP6dKZQg== 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=3pUi7jN4ZEskcsxEVIoANc2St4sIeIlycHOMZsHNrWLghB37tml97hbs9zg8Yhk0opw83mr9Ez5ccVjA7LxSpOlF2lqc7LxDXeko1196RANmIYNQkP1W9bR2eoS0LDw8KxNALLa83GoHiIXaOnrJtWRT0V93dXkVnrIE0oiGXzM= Received: from BN6PR14CA0027.namprd14.prod.outlook.com (2603:10b6:404:13f::13) by SA0PR12MB4511.namprd12.prod.outlook.com (2603:10b6:806:95::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.13; Wed, 10 Nov 2021 22:15:26 +0000 Received: from BN8NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:404:13f:cafe::e8) by BN6PR14CA0027.outlook.office365.com (2603:10b6:404:13f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4669.11 via Frontend Transport; Wed, 10 Nov 2021 22:15:26 +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 BN8NAM11FT036.mail.protection.outlook.com (10.13.177.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4690.15 via Frontend Transport; Wed, 10 Nov 2021 22:15:26 +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; Wed, 10 Nov 2021 16:15:22 -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 v12 16/32] OvmfPkg/BaseMemEncryptSevLib: skip the pre-validated system RAM Date: Wed, 10 Nov 2021 16:14:41 -0600 Message-ID: <20211110221457.2397234-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211110221457.2397234-1-brijesh.singh@amd.com> References: <20211110221457.2397234-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: c8ef2b66-caba-4ee7-2c38-08d9a4979917 X-MS-TrafficTypeDiagnostic: SA0PR12MB4511: 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: rmBk0qbY2N973/0mlRLSvddGVexbp1pwcTvqKcz4feO9rB61sWxM0vcpsQFPOij+WVMlgg7oPL3bjW9nr0HJXKWIrQ+mIAS8vMgSiMHS92jyHmq4xCNRAW1i63IjygO0iSfqtD6R3xgF3OMb0y7u+Abdb6VkFOTcxhJvGsDW4V+y86/jpPovhOE2Av19aS4c8jbq2QOFtav4puQQy9Mm/qhLP9tmksPuS6vd4AVV18BCSSKxajXOak/ICJl+kVj8RlZX7sZN5Y0AzfTCkMs7tkVkDxGBri8kQ5m5Zl7jPMXOGrjtVB1u9V1hl93ZWxvOeSsJYTo7SZzM7hu8X1QDZH70rdfykUfSErB7PokLpbveRfcx8RJoG8Nph31HzqHoW4PXeSfaV55gn1ULJI1PB5vK7oE3WYb2ImvDPl0o1T6cDvPJHBOfzqsCyBHEQxWa6TodPCMaAP4YDfJbX7F24lQylbY/s0okUiQie4ZkYLBnNtKXYUal6AeCPwgcIDI4eXgEaA3VFY8rBbkOoJ+D3Hq2ee+MHQVZ/STgBwQ87Pbi5ULIbfSkjkr3EaVAFz+isZwMZPtclcGvKodHFqKUj4vZDPuRVyL3wpeT0wVtkHq3rah7PoV+AEnuhLwRxE+Z/9rXq9su9sJg30UdwQDxBbrwYxGHaVl91hBJS10Ab2iwdHT1TGG5emDDo9RDJkb3tnZQW1aWoQol/uxmphxh6gl862oMM77UZ+l7aqdTA/2jWt/KjCWxqAyyTkEom2y5jDat+6luVClB72+2nLdxrs3yZagbqF1oslN0jWvP5cANKChGq/SmdbM88gIojX3lZEb7Rg7kIGiNef1sIZzNs0LsOcjRl349L9ljFHifYKg= 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)(186003)(86362001)(316002)(6666004)(70206006)(1076003)(54906003)(26005)(7696005)(4326008)(5660300002)(36756003)(81166007)(2906002)(44832011)(966005)(83380400001)(47076005)(19627235002)(2616005)(6916009)(16526019)(336012)(8676002)(426003)(70586007)(356005)(8936002)(7416002)(15650500001)(508600001)(36860700001)(82310400003)(213903007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Nov 2021 22:15:26.4645 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c8ef2b66-caba-4ee7-2c38-08d9a4979917 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: BN8NAM11FT036.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4511 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