From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (NAM04-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web08.27.1616599961841997394 for ; Wed, 24 Mar 2021 08:32:42 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=g8lZOnIR; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: , mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gUGkv/eANuZkD5Y/rRyy/05cpLWQOS15SY664x3tL4MtpTfIiXEsqAMtMy+gKBoouqp8k3sWSy+Uif7m+dtClV8kCe4OQwp05reqLbqTWmJs17TgywjMPj4oyc4gi1GXlfCWikra+Qru2G0hel2Y2XfsNtjVhspIZVjCFboCBHG/R+Eh6wFTLeBeYokIHLtKl4FT7NlkWVSC/mjeMLR86k5Bk10vQJYpP+f09lDmhptOF7ae+dRg3TF4HhABOzUm0EZs3m49uQP+mTpwrfFzkx9BCAnvw9tYjWEdNPv2x4fm+YWA9CKxgkqzQzH79wJ0Rqvr06XLBeUv/d5oQ5amHw== 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=JT/o8FrJErOCZSf1akwTxaBymohmGgPWgpCa1Vg6jy4=; b=jxXlKVUXpttm0zRCCBiuxPJ9prSaWHVNSBKt3jZfS0Yr1JVLAaMt2kzAkgs35PF0xI352McK6gONtSobuHg2IqFFheSgWTG4jVrCJpj1KqyWzQgHaensPUnI5KXtLeBQOZx6f3iKn4bIQfzbBX3nl8kTViGUB6b9Y37HoKLqfYg7SRlP/BAPGDvZvEYx7aSgZb/sX7PkHsa3QThbFTmndaDkxXzGX5E38Zt7ZNWYcOsAd23n5wtnWWMQ0jl4LmsMxDAZAPJ9uyIzfcYcKvig9ZKR589ZWKQ08qokrxlifEF87lt1HfjaH/digzPMzMFqZYwCuHI2tMNoBsH+vUu1dA== 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=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JT/o8FrJErOCZSf1akwTxaBymohmGgPWgpCa1Vg6jy4=; b=g8lZOnIR7PzuFgXe+znKwLnOPNiIsD56oVRFijzxYAIYtv1wufE6EjUKy6TMRh9iKZ+pAywCxIlAwFLqFlZpXb1si+5ItWu5KueJjdK4JYwM2eIqupySxORyXpQmruLZ3AwqwlKl0POEy34pMIoapnMLKuGy5Q2JobYvaLCK7+E= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4430.namprd12.prod.outlook.com (2603:10b6:806:70::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18; Wed, 24 Mar 2021 15:32:41 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 15:32:41 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io Cc: Brijesh Singh , James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Laszlo Ersek Subject: [RFC PATCH 19/19] OvmfPkg/MemEncryptSevLib: Skip page state change for non RAM region Date: Wed, 24 Mar 2021 10:32:15 -0500 Message-Id: <20210324153215.17971-20-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324153215.17971-1-brijesh.singh@amd.com> References: <20210324153215.17971-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::11) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9PR11CA0006.namprd11.prod.outlook.com (2603:10b6:806:6e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Wed, 24 Mar 2021 15:32:40 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f7fb067c-7669-4e5b-4980-08d8eeda0ff2 X-MS-TrafficTypeDiagnostic: SA0PR12MB4430: 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: a+KPPk76kBB/EyBEabwRq9zTDsW0ShxXuEuJx8MZdDdhE4cFQkgot0a+BCywThKn61mRMuQx57tr3jd/f9nJYaN0vuBHDOq00h0Bb4/yZiAsoodfVwwr+X2Bfsea1qMeH3n44WQr3AVu3B2HTi3CLyMhUJaqfShoABRBMsicnrksjm/2gorIZKGk7ct8TGa5XHSRKcvy0VbrOQQogrzEa1yIMFAcLr7JA4iGGyYDF4bsU/8+wPidXQ+2wjV5bUpCYAPAu2jcntx4pmZ+g/UeDDwLHlFgVkBGkXUMgCvzItFhCGBbcIYYyJ7USkAtVXR75oD2wouhhyE4EE+BcStn/NdtwgoKLdOIfyeq4jlYbLYnlkgL67sq9ucJmbVgf4wjb3z5ToVBMHJ8zJZdnREBypT2BrtGQb+HFFHSgbXnsofedO+CP1e3DvNxnE7ssX5hl5WyKD/qi9cji3pp6HMvN1ABS19F4t1Z0sL5N4w+fVUZ0aQAM96wIfseMMMpVB7g9D7a9N6XDIH6Ygh0bvycCSubpHigEOCEnqgaGDFUYcrjeCjTsvZS4EnYCIRs3de0Lqk0qD4G33MZJ0TUBz09f17xK6elszKoizw2tcQDaeAG7eFAitxMq2cJyoVCUa0FCG/kyULxi93UFIe6piYEA7IkSUPaDFkY7lZHPBeE0QHKkojbwuXnrNRnPjW/sLTjnX5J3fFKjCPnCHnMMCjX6IIQ04t/xMJb5fXYS1hd2RSn5BOV/fi/BNT39qrgBJLX X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(2616005)(83380400001)(66476007)(4326008)(44832011)(1076003)(6486002)(2906002)(86362001)(66946007)(478600001)(316002)(66556008)(966005)(16526019)(6666004)(26005)(54906003)(19627235002)(6916009)(186003)(5660300002)(8676002)(7696005)(956004)(8936002)(38100700001)(36756003)(52116002)(44824005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?ytFAfOOWRWsQvYONX5f2rMBcjyJJuMBnqd5yw5eqkLJlbeUlFwqO3KCB36Ua?= =?us-ascii?Q?qwQbZSc6M3n94hIvMwwINUlFxKKk020qqwe8nCJanGYwwffLbzGksjAl6y9n?= =?us-ascii?Q?TbIlCADkPemPPsTyQQv7Gieqrzofa4OBN0K+HsA4JSfwqlcmRwtdYpLrWk3u?= =?us-ascii?Q?6nf3fXNEswDoEMnLaQOZVkAYRJ67fOKIQHxx9TC4VS1lilwuMz2cIPB2qjFg?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GaEJISQChSjryVFBuCxcm78qNSJDzubwSu76y4N?= =?us-ascii?Q?HQ6aWrGoakOe56iMuRom+KwoFc3d2IW9yUTEFtn5xEgzGAAz59KNZ6wG09ot?= =?us-ascii?Q?+arFaV2Znx78hqcovklTMHUfwxzFLYSUsZ0l3UQGDNn9zQQJqaOXe0GQ8ssV?= =?us-ascii?Q?7tZ/hKJiYTQ8Hd5X7HGNe+de15UaXUpJMzSxB99s/vHEzXwrmOHFoXDZIFPg?= =?us-ascii?Q?hK7a3hZGIVLOpdEYmooE0dWw?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f7fb067c-7669-4e5b-4980-08d8eeda0ff2 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 15:32:41.6319 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: shh0vHlkCnd2KqCMJYgzfxO/JhLo20M25Vr698KEpLf/gpHboC08v9hcgll4+zrS8qCI0pc75I8DSEEXxz+FVA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4430 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3275 The PEI phases uses MemEncryptSevSnpValidateSystemRam() to validate the system RAM. MemEncryptSev{Set,Clear}PageEncMask() is later used by libraries/drivers to change the page state from private to shared and vice versa. The AmdSevDxe driver calls the MemEncryptSevClearPageEncMask() to remove the encryption attribute from the reserved and non-existent memory regions. Those regions where not part of system RAM, and was not pre-validated during PEI phase, so we fail to change the page state for it. There are multiple approaches to fix it: 1) Add a new parameter to MemEncryptSevClearPageEncMask(), that should be set by the caller to indicate whether the region is RAM. OR 2) Lookup the address in the interval tree maintained by the MemEncryptSevSnpValidateSystemRam() to determine whether we validated the page in the past. OR 3) Iterate through the Memory space GCD to calculate if the address range is RAM. For now, we have chosen #2, it does not require a changes to the caller of MemEncryptSevClearPageEncMask() and lookup routine is already available. Extend the SEV-ES workarea to pass the interval tree root pointer so that we can perform the lookup later. If the specified address was not present in the tree, then do not invalidate the page as it could result in page state change failure. Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Signed-off-by: Brijesh Singh --- OvmfPkg/Include/Library/MemEncryptSevLib.h | 3 ++ OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf | 4 ++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c | 31 +++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c | 42 ++++++++++++-------- 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/Library/MemEncryptSevLib.h index 47d6802b61..712590b64d 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -54,6 +54,9 @@ typedef struct _SEC_SEV_ES_WORK_AREA { UINT64 RandomData; UINT64 EncryptionMask; + + UINT64 SnpSystemRamValidatedRootAddress; + } SEC_SEV_ES_WORK_AREA; // diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf index fa8f7719a7..43b842254f 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf @@ -38,6 +38,7 @@ X64/PeiDxeVirtualMemory.c X64/SnpPageStateChangeInternal.c X64/PeiDxeSnpSetPageState.c + X64/SnpPageStateTrack.c X64/VirtualMemory.c X64/VirtualMemory.h @@ -58,3 +59,6 @@ [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask + +[FixedPcd] + gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c index 0a3d58ac22..9fe6831368 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeSnpSetPageState.c @@ -10,8 +10,12 @@ #include #include +#include +#include +#include #include "PeiSnpPageStateChange.h" +#include "SnpPageStateTrack.h" VOID SnpSetMemoryPrivate ( @@ -28,5 +32,32 @@ SnpSetMemoryShared ( IN UINTN Length ) { + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + SNP_VALIDATED_RANGE *RootNode, *Range; + + // + // Get the Page State tracker root node. The information will be used to lookup + // the address in the page state tracker. + // + SevEsWorkArea = (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAreaBase); + RootNode = (SNP_VALIDATED_RANGE *) SevEsWorkArea->SnpSystemRamValidatedRootAddress; + + // + // Check if the region is validated during the System RAM validation process. + // If region is not validated then do nothing. This typically will happen if + // we are getting called to make the page state change for the MMIO region. + // The MMIO regions fall within reserved memory type and does not require + // page state changes. + // + Range = FindOverlapRange (RootNode, PhysicalAddress, PhysicalAddress + Length); + if (Range == NULL) { + DEBUG ((EFI_D_INFO, "%a:%a %Lx - %Lx is not RAM, skipping it.\n", + gEfiCallerBaseName, + __FUNCTION__, + PhysicalAddress, + PhysicalAddress + Length)); + return; + } + SetPageStateInternal (PhysicalAddress, EFI_SIZE_TO_PAGES (Length), SevSnpPageShared, FALSE); } diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c index 41bf301efe..2e049d3df7 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiSnpSystemRamValidate.c @@ -18,8 +18,6 @@ #include "SnpPageStateTrack.h" #include "VirtualMemory.h" -STATIC SNP_VALIDATED_RANGE *mRootNode; - STATIC SNP_VALIDATED_RANGE * SetPageStateChangeInitialize ( @@ -64,15 +62,34 @@ SevSnpValidateSystemRam ( UINTN EndAddress; SNP_VALIDATED_RANGE *Range; EFI_STATUS Status; + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + SNP_VALIDATED_RANGE *RootNode; EndAddress = BaseAddress + EFI_PAGES_TO_SIZE (NumPages); + // The Root of SNP_VALIDATED_RANGE is saved in the EsWorkArea. + SevEsWorkArea = (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAreaBase); + RootNode = (SNP_VALIDATED_RANGE *) SevEsWorkArea->SnpSystemRamValidatedRootAddress; + + // + // If the Root is NULL then its the first call. Lets initialize the List before + // we process the request. + // + if (RootNode == NULL) { + RootNode = SetPageStateChangeInitialize (); + + // + // Save the RootNode in the workarea + // + SevEsWorkArea->SnpSystemRamValidatedRootAddress = (UINT64) (UINTN) RootNode; + } + // // The page table used in PEI can address up to 4GB memory. If we are asked to validate // a range above the 4GB, then create an identity mapping so that the PVALIDATE instruction - // can execute correctly. If the page table entry is not present then PVALIDATE will - // cause the #GP. // + // + if (BaseAddress >= SIZE_4GB) { Status = InternalMemEncryptSevCreateIdentityMap1G (0, BaseAddress, EFI_PAGES_TO_SIZE (NumPages)); @@ -81,25 +98,16 @@ SevSnpValidateSystemRam ( } } - // - // If the Root is NULL then its the first call. Lets initialize the List before - // we process the request. - // - if (mRootNode == NULL) { - mRootNode = SetPageStateChangeInitialize (); - } - // // Check if the range is already validated // - EndAddress = BaseAddress + EFI_PAGES_TO_SIZE(NumPages); - Range = FindOverlapRange (mRootNode, BaseAddress, EndAddress); + Range = FindOverlapRange (RootNode, BaseAddress, EndAddress); // // Range is not validated if (Range == NULL) { SetPageStateInternal (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); - AddRangeToIntervalTree (mRootNode, BaseAddress, EndAddress); + AddRangeToIntervalTree (RootNode, BaseAddress, EndAddress); return; } @@ -110,12 +118,12 @@ SevSnpValidateSystemRam ( if (BaseAddress < Range->StartAddress) { NumPages = EFI_SIZE_TO_PAGES (Range->StartAddress - BaseAddress); SetPageStateInternal (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); - AddRangeToIntervalTree (mRootNode, BaseAddress, Range->StartAddress); + AddRangeToIntervalTree (RootNode, BaseAddress, Range->StartAddress); } if (EndAddress > Range->EndAddress) { NumPages = EFI_SIZE_TO_PAGES (EndAddress - Range->EndAddress); SetPageStateInternal (Range->EndAddress, NumPages, SevSnpPagePrivate, TRUE); - AddRangeToIntervalTree (mRootNode, Range->StartAddress, EndAddress); + AddRangeToIntervalTree (RootNode, Range->StartAddress, EndAddress); } } -- 2.17.1