From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (NAM12-BN8-obe.outbound.protection.outlook.com [40.107.237.89]) by mx.groups.io with SMTP id smtpd.web10.3988.1680027610384891042 for ; Tue, 28 Mar 2023 11:20:10 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=fh/QEjvF; 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.237.89, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=m+HFZZ0TdQe43t7H60XPs//nxhbhaXOKqtBERLVoRUFhCM1h69mHS1D54DJtnmJFY/jIVnmNM0ZWR/YklhggiOq7PWABwylCxy8L99qZ01aRTL883tyykxiq8l1oNo8rrm1b9knQC2Rx2XPJSlhSMcomOWkwV5GiZ6ckkUJQSVPN5dkqxEHcdH8A3cayG8qRtzZmN1Yc3/ENc2oHztUssRt0tNaqfGh4Gdiw57Nb3ooUlr9IZu7/kdf27a7bOSPq1H6ErlK9Pv3D48ZzsTsLOsJ9jJWozOY5oY71FpFQ9+rrF11AbCERLN4an39N4vVRqGD7Ki/oHuzW3DODcOpzMA== 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=NFmmStOnHB6D2EfDqFnNqFmPWaqAeSITyl6TwGaCuEo=; b=SWN2ZVcKE1DSicAX29VWqNb5MZiOkuzTxngrJhvRHQR5C0jEczGxkQU7xPLrzotcx7TogKEX4y/Hdo2lnlMIwCNpe9QJUoarxTVx9nmvTvPI3GOq81nEbmPg3YAP3x0nzJRT4Pu+zo8isDf9tB/0DVqh/Zya4tYb4isTmvgJzde4dG6vvhzV6IWcbsKCndyU3DSQPCzVffHsUuG3t2XLG+PjsG/R38t2OeQmSveMN7TZw2x+L8mdcq2Si4+p4PQrU/VcGblq2OpGX01aFLoOchxSNDTE4OsuiyZuGb95riwvCes2k4x3X2sSS0ZQgiwqo7h8zR9tbpf1JmDvncZlFw== 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=NFmmStOnHB6D2EfDqFnNqFmPWaqAeSITyl6TwGaCuEo=; b=fh/QEjvF4gXfuPOMjX4pRZxmk+SdR5ThZDMdSdyoA+EprDiESkjXD3KfP69F4ojtkyMoE4N08z5VXTfuIlNQ00wPnT5ym+JpcnkjSf6T8ZymzR0IYr5bWz7Pv+NaXscIY1oAoTF2sjiWW5DOVJUjhrwxNzsBAHjdf5n+h0fWpzA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) by MN0PR12MB5907.namprd12.prod.outlook.com (2603:10b6:208:37b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Tue, 28 Mar 2023 18:20:07 +0000 Received: from DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::5b56:bf13:70be:ea60]) by DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::5b56:bf13:70be:ea60%6]) with mapi id 15.20.6222.033; Tue, 28 Mar 2023 18:20:07 +0000 Message-ID: Date: Tue, 28 Mar 2023 13:20:05 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [edk2-devel] [PATCH v2 2/2] UefiCpuPkg/MpInitLib: Ensure SEV-SNP VMSA allocations are not 2MB aligned To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann , Michael Roth , Ashish Kalra References: <1750A7A753390E6E.29160@groups.io> From: "Lendacky, Thomas" In-Reply-To: <1750A7A753390E6E.29160@groups.io> X-ClientProxiedBy: SA0PR12CA0027.namprd12.prod.outlook.com (2603:10b6:806:6f::32) To DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) Return-Path: Thomas.Lendacky@amd.com MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR12MB5229:EE_|MN0PR12MB5907:EE_ X-MS-Office365-Filtering-Correlation-Id: f82175fd-ae41-45c6-d09d-08db2fb90f48 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YiduuOBVCJvKNI/iBLsQk2v1i4MMYHLH36Uh5GZUb7sk1nt/RCr8eRoF38ijiKg05N/f9/wPkmtsr++DbXZbkAEbLSwC6+QLptqcchGsAUmdf9ATrvigbhty6t5kPp7zZ7xi/4F6hHdwDAVIdVL9FfO4spcFvnpAUSHidxrsprkNxO9HXPuQwRaVjPl+mWu1EEMfI8z+Yw7xE1R00Pe5uDgRnRpNq4xjlFLz7MlhY/apQqubDMYYKvpdnfhaAz5LxMs0cEjlEg/1WyaR7/2+Bd1khjflUrIszse+i19ubOsfmHPvOL/YT2D1tHxnNv52cOyF8KH+QKDKdIGfMWRWrInwPTOzO6X8+xL6XIVNp2i9IkpbXb242xRXqcbEExUjjgsO7L4Y3omqgEnfYLiRZSh+AxNemWHc7dfoD9UJ4TUgaB5hBdlS1e0bVtbuxlaA7Qa0zNFNwy82jDugVTjCrHwvlwFB474XBTU/zAcsWkBKJqmmZNMuDFnkYlttnqZRkgmyRQa//D95KMMryOSf+aORDsnXK8m6HymtngIQX4ZcyvTWYqXeM5KevPuB3Hwdcdh3utP+J864+SgG6G6hrMlLkEnF4JyMmU6/VYuQPYLpHNGvRIVQ3JSNO0H9heftOYAUgl1mLN1TNwjJ4Qnb4w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR12MB5229.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(6029001)(4636009)(39860400002)(366004)(136003)(346002)(376002)(396003)(451199021)(31696002)(86362001)(66946007)(8936002)(66556008)(66476007)(8676002)(2906002)(478600001)(19627235002)(316002)(54906003)(6486002)(36756003)(966005)(5660300002)(38100700002)(26005)(6916009)(4326008)(53546011)(31686004)(83380400001)(186003)(41300700001)(6512007)(6506007)(2616005)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?cEdrTjV6djYzMCtPNFQ4YzNNOGlZQmczM2FDNmIrYnNmbUNyQVJVdUVPM0lm?= =?utf-8?B?dk54c3dPSHlVNUhxK21nZWpxOW92c1NiSUZvZGpQMGQrd2Z4c2lKRkR3ZE10?= =?utf-8?B?cGVPeDhrdVViZGZGM0Y4a0JmUmJaOUp3TnpHMFh6MlFyc2VETXVRL3p0R0tr?= =?utf-8?B?dkFPN3pYNUtHRWZJdXZoZWtjZTlxNkp5QnFkVFF1cy9qK2VybEpneDhqK3Jr?= =?utf-8?B?U0hHdWR1VlRML1h0QVRQVzlBNEFkb1JQdCt2WXhjT3ozQjd3UE1McHg0M01V?= =?utf-8?B?bE4wdi9rV0lEYUNuQnJUN1BmT0s5dTNldEdoSUNDb2hYTEdkZ2tiVTV3RW90?= =?utf-8?B?Yllya2h3SjJmVi9Wb2IzMHEyUkhWWlpRUy9RQXkwZUM1ek1tVGlFTnpLZ0xI?= =?utf-8?B?TVZWYkZhVjMw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-CrossTenant-AuthSource: DM4PR12MB5229.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Mar 2023 18:20:07.6691 (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: 6j9XlMTYkJyxCNDGJY89d3CWpL+jUoDDV7EDn5qT1OOsEAzE7t1v9iFmeAQxhdflrpGHFvO8YErTmiPvLmK0pg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5907 Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/28/23 13:09, Lendacky, Thomas via groups.io wrote: > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4353 > > Due to AMD erratum #1467, an SEV-SNP VMSA should not be 2MB aligned. To > work around this issue, allocate two pages instead of one. Because of the > way that page allocation is implemented, always try to use the second > page. If the second page is not 2MB aligned, free the first page and use > the second page. If the second page is 2MB aligned, free the second page > and use the first page. Freeing in this way reduces holes in the memory > map. > > Fixes: 06544455d0d4 ("UefiCpuPkg/MpInitLib: Use SEV-SNP AP Creation ...") > Signed-off-by: Tom Lendacky > --- > UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c | 26 ++++++++++++++++++++--- > 1 file changed, 23 insertions(+), 3 deletions(-) > > diff --git a/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c b/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c > index 509be9b41757..c9f0984f41a2 100644 > --- a/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c > +++ b/UefiCpuPkg/Library/MpInitLib/X64/AmdSev.c > @@ -13,6 +13,8 @@ > #include > #include > > +#define _IS_ALIGNED(x, y) (ALIGN_POINTER((x), (y)) == (x)) I haven't seen any movement on Gerd's series to create common macros for alignment checks, but can't wait forever. So here I use an underscore in front of IS_ALIGNED() so that there won't be a conflict either before or after Gerd's series is applied. Once Gerd's series is applied, I can go back and submit another patch to use the new macros. Thanks, Tom > + > /** > Perform the requested AP Creation action. > > @@ -121,6 +123,7 @@ SevSnpCreateSaveArea ( > UINT32 ApicId > ) > { > + UINT8 *Pages; > SEV_ES_SAVE_AREA *SaveArea; > IA32_CR0 ApCr0; > IA32_CR0 ResetCr0; > @@ -131,13 +134,30 @@ SevSnpCreateSaveArea ( > > if (CpuData->SevEsSaveArea == NULL) { > // > - // Allocate a single page for the SEV-ES Save Area and initialize it. > + // Allocate a page for the SEV-ES Save Area and initialize it. Due to AMD > + // erratum #1467 (VMSA cannot be on a 2MB boundary), allocate an extra page > + // to choose from to work around the issue. > // > - SaveArea = AllocateReservedPages (1); > - if (!SaveArea) { > + Pages = AllocateReservedPages (2); > + if (!Pages) { > return; > } > > + // > + // Since page allocation works by allocating downward in the address space, > + // try to always free the first (lower address) page to limit possible holes > + // in the memory map. So, if the address of the second page is 2MB aligned, > + // then use the first page and free the second page. Otherwise, free the > + // first page and use the second page. > + // > + if (_IS_ALIGNED (Pages + EFI_PAGE_SIZE, SIZE_2MB)) { > + SaveArea = (SEV_ES_SAVE_AREA *)Pages; > + FreePages (Pages + EFI_PAGE_SIZE, 1); > + } else { > + SaveArea = (SEV_ES_SAVE_AREA *)(Pages + EFI_PAGE_SIZE); > + FreePages (Pages, 1); > + } > + > CpuData->SevEsSaveArea = SaveArea; > } else { > SaveArea = CpuData->SevEsSaveArea;