From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com [40.107.236.71]) by mx.groups.io with SMTP id smtpd.web10.6827.1639020521896368542 for ; Wed, 08 Dec 2021 19:28:42 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=Rn+DcmZX; 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.236.71, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XZG0BZWql7RvkHcNkynWO8K/0vcFibVTwZOuTkK9kr3s0ncV+Va19kTXiwyoved6qZ9JrC/lR1QRDCuZ0Dip32iqWl3A3ev7HHSNRtRgbYP0ZwHd/P/C6V7WcD0meU3gqpUzXaCB0i6739UVZG+puG8Zkw6f3zbcZWm+VVv+lLsqnXj0zjBSRq/gm2eTiW6tt0KzoJp7qKnT8TiqSUHFaYpWmu5BNkC3Q68ZCWl2rHPSzF1IyOlcFsowqjGSL+zidZuSy976f+w9GsjEEhqGW8sEPL4baE/wZOv1y4IE/07vfHo+YoKlya2ke1yBioq0qd7jMK9P8THu3f4mA9TkWQ== 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=33a8S+jyym0cjXHDRsIAsIrMlMGxq8eT0b4QuzHYm88=; b=hLta/DT2348CaJ8XS7yFgntgf4u8FxGzuYLD7u7YmYIKn77kqqkfuJiHzR0Uaj4xxhpAok/bd2gIjHpNYks+1cqLxPz5K9HzpOMlZubuN9GsfSS7ALmBJHEKVzoxfUsPVawBdwJxCzcLG7sy9ENcGVs6zIHT9oddh4ORVqCLmb4Ty2HfG+WKk6eLvplfgcWqAs3pJ8AeNhZWJI3oouMrHKZhe1MYTFsj6vRfZOuXKMjgOXWmBF0K8wLoyFcAVgm/xabq/9VA6OSnVqwlco4u1TYlhvBlqs/U9to33DrLsJUDsKa0NDEtld1w2689vee2cKWNZxIlA8LK3WAQwnNNjQ== 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=33a8S+jyym0cjXHDRsIAsIrMlMGxq8eT0b4QuzHYm88=; b=Rn+DcmZX8CmTqwI0wzNzwZp9gsiEV1attvdqt/1ScQsTuHlHxd6C0eFCvgTp6n3lhT5OBfG/7/i4CjuamWyP9DMyKyg5rT4WL6izz2c1ruDA6MF6npPqArnJZJlT0FISjvrnwKaUL0jMFtfEVFCuhPt6botgmoVKrWu32iu+86E= Received: from MW4P223CA0015.NAMP223.PROD.OUTLOOK.COM (2603:10b6:303:80::20) by BL0PR12MB5508.namprd12.prod.outlook.com (2603:10b6:208:1c1::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Thu, 9 Dec 2021 03:28:40 +0000 Received: from CO1NAM11FT012.eop-nam11.prod.protection.outlook.com (2603:10b6:303:80:cafe::5a) by MW4P223CA0015.outlook.office365.com (2603:10b6:303:80::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11 via Frontend Transport; Thu, 9 Dec 2021 03:28:39 +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=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1NAM11FT012.mail.protection.outlook.com (10.13.175.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4755.13 via Frontend Transport; Thu, 9 Dec 2021 03:28:39 +0000 Received: from sbrijesh-desktop.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Wed, 8 Dec 2021 21:28:37 -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 v14 16/32] OvmfPkg/BaseMemEncryptSevLib: skip the pre-validated system RAM Date: Wed, 8 Dec 2021 21:27:44 -0600 Message-ID: <20211209032800.3802995-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211209032800.3802995-1-brijesh.singh@amd.com> References: <20211209032800.3802995-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 SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c3f8554d-efb3-4b8d-e053-08d9bac3fe34 X-MS-TrafficTypeDiagnostic: BL0PR12MB5508:EE_ 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: +vy4KWv/XO+sHWpy7sf4KMFdzh/HQfJ51QlDVYyNy/zYrNveOMqVU9VWQdgZPc8qfOhFKyskpB2jbTfakw7bZf50bzk9T6rFpOYmPKsZONzY18p3G3Zo591W35XUeZ1SfB8n8v/m8haT6prxmCBU9ZMiWj3AJggNZWLIFICqGQxU4TP/6Z7XTquWfxwyqNZVqGpf+5nxwbZouUapXBICuzLNuRth4dCiUtw6yTdd/oB4vMu4xc1bdmT2VAnuyYYu86gXyo3ij956fYkFRIfiPCvPbAfG7sSv3m/J35uJGY8Y7RQWjmR2nTmtrrh/5s4TtHxpSkoFj30M8VlO8doFXvuGCIOzGkvciHN9K539VpDwWzzkM10lXplsS5k/BXoGF7RENRtQvxVbtTJjF1OQp25+Xw1JcDcyZ8mBWtfOr2ZAmyafTuoa9AOmLqmifo9+qa1etl56Z0U/9CcqkJrygiZYkDiPInQ2ef/Y7bMk6+yMqgYgi3sIflLWslYNqdaqGuqMdLo8B+XuO85ZfLU+CVimZOuMvizceVv3F9Vc0x+uf1SCBx4HI1enBoIiAOD3OrRYJLZ2PKxNFRIh7rWvvOtwCAOH6c8KrrSZpFqNiWZoifBQrX4DA1TSDnE3c6dYbypRbKsC+93LQ3HGP+WK37MYAmZpRfouM7OE+R4DTzKGSXfNNb2VHiAlaDN7jUtrHfvA0Tuvl6XvnDDUW7b9HiU+zvMqv1K9YXulaFhRVAAg8/yRP8L/qoFchPXjZa0skgwemmNc79Tr2EcazVV1snkAxtD1DFLcgcrtuWmraFHqpBoju0YXXHk1PMW8HYQnnAHmtC3P1NxH4wOTpBwjoQJIJl7acCcLf+KGIX3z2fSPPtK7xT2VU75GBFMytrQ7ciU7fClYcVTveMv9dkrGnw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(83380400001)(4326008)(36756003)(966005)(7696005)(47076005)(6916009)(86362001)(508600001)(54906003)(356005)(36860700001)(6666004)(15650500001)(81166007)(316002)(82310400004)(336012)(2616005)(8676002)(26005)(70586007)(16526019)(8936002)(40460700001)(7416002)(5660300002)(2906002)(70206006)(426003)(186003)(19627235002)(1076003)(44832011)(213903007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2021 03:28:39.4226 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c3f8554d-efb3-4b8d-e053-08d9bac3fe34 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=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT012.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB5508 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 | 67 ++++++++++++++++++- 2 files changed, 68 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 bc891c2636d6..2d2136f8054c 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -14,6 +14,46 @@ =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 +68,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