From nobody Mon Feb 9 00:01:43 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+89579+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+89579+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1651936406; cv=none; d=zohomail.com; s=zohoarc; b=N7acEiQoNzlDmY750O5rQGPJVWJ3u+rJ/Ro8ExFhXKgDNdPGeVoNfSafAFS19eKxl/JSCXvbu7vkgUaYKPSITm0Fei0jEq3Bm1aXh83QeakmRx9u7peImVgBHtc0gQUcjD+94xkstW4w518l5lZQWS+gqQe+p+dAxCAZRyOAFoQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651936406; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=/yHNEDA/oXVG0AzcjMx2wCjOWpdkxYxrr8nlXRgEMMw=; b=FBvBw8OANh0TG13lxrm0FAM6+SxhMjud2ZPvy70ylO1cRPMCpcev9hGJyN2zzA0nYAuCSGgBSEkY98NTFrbd1WtFhvVUSEIyJGnVgmXWXucLB4uYHU6guXtpFZbC/+V33D27zUqVATq2o2t6GM0rCOXcNdmRdjfZ21PvBk1F3Vc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+89579+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1651936406389772.4895262372756; Sat, 7 May 2022 08:13:26 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 6At1YY1788612xoexSULHvxG; Sat, 07 May 2022 08:13:26 -0700 X-Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web10.10776.1651936400565355060 for ; Sat, 07 May 2022 08:13:25 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10340"; a="268345284" X-IronPort-AV: E=Sophos;i="5.91,207,1647327600"; d="scan'208";a="268345284" X-Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2022 08:13:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,207,1647327600"; d="scan'208";a="601004388" X-Received: from shwdeopenlab706.ccr.corp.intel.com ([10.239.183.102]) by orsmga001.jf.intel.com with ESMTP; 07 May 2022 08:13:23 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Eric Dong Subject: [edk2-devel] [PATCH 4/4] MpInitLib: Only allocate below 1MB memory for 16bit code Date: Sat, 7 May 2022 23:13:13 +0800 Message-Id: <20220507151313.115-5-ray.ni@intel.com> In-Reply-To: <20220507151313.115-1-ray.ni@intel.com> References: <20220507151313.115-1-ray.ni@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ray.ni@intel.com X-Gm-Message-State: q6214yp4V1UpKBW6qFpdMts8x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1651936406; bh=35m5gVsMzLW87n/SVafypJc4sTw3gXx2pyWu8OiQ7co=; h=Cc:Date:From:Reply-To:Subject:To; b=mnEEPeCVVkQT1vlcNISBSfJ/ECSBRsGQ2rmHeMt5R4RPMkrl/a8pt04fi+fZ60jjccW oiofR/25KRe3YxCXfl/PjyUboE0UnrQjyjqjsf20XN+D+g2MOWNi0TOp0Fvltu0gGH8bR Y01wFAsadKmS7xiFJN/NCZ4Em8PkEfJsykM= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1651936407822100019 Content-Type: text/plain; charset="utf-8" Today's implementation allocates below 1MB memory for the 16bit, 32bit and 64bit code. But it's not necessary since now the 32bit and 64bit code run at high memory no matter in PEI and DXE phase. The patch simplifies the logic to remove the code that handles the case when WakeupBufferHigh is 0. It also reduce the memory foot print under 1MB by allocating memory for 16bit code only. MP_CPU_EXCHANGE_INFO is still under 1MB which is immediate after the 16bit code. Signed-off-by: Ray Ni Cc: Eric Dong --- UefiCpuPkg/Library/MpInitLib/AmdSev.c | 6 +- UefiCpuPkg/Library/MpInitLib/MpEqu.inc | 2 +- UefiCpuPkg/Library/MpInitLib/MpLib.c | 94 ++++++++++++-------------- 3 files changed, 46 insertions(+), 56 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/AmdSev.c b/UefiCpuPkg/Library/MpI= nitLib/AmdSev.c index b4a344ee6b..4e4c63a52d 100644 --- a/UefiCpuPkg/Library/MpInitLib/AmdSev.c +++ b/UefiCpuPkg/Library/MpInitLib/AmdSev.c @@ -110,11 +110,7 @@ MpInitLibSevEsAPReset ( Code16 =3D GetProtectedMode16CS (); Code32 =3D GetProtectedMode32CS (); =20 - if (CpuMpData->WakeupBufferHigh !=3D 0) { - APResetFn =3D (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->Ad= dressMap.SwitchToRealNoNxOffset); - } else { - APResetFn =3D (AP_RESET *)(CpuMpData->MpCpuExchangeInfo->BufferStart += CpuMpData->AddressMap.SwitchToRealOffset); - } + APResetFn =3D (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->Addr= essMap.SwitchToRealNoNxOffset); =20 BufferStart =3D CpuMpData->MpCpuExchangeInfo->BufferStart; StackStart =3D CpuMpData->SevEsAPResetStackStart - diff --git a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc b/UefiCpuPkg/Library/Mp= InitLib/MpEqu.inc index aba53f5720..831fcc2cc7 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc +++ b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc @@ -97,5 +97,5 @@ struc MP_CPU_EXCHANGE_INFO .ExtTopoAvail: CTYPE_BOOLEAN 1 endstruc =20 -MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart) +MP_CPU_EXCHANGE_INFO_OFFSET equ (Flat32Start - RendezvousFunnelProcStart) %define MP_CPU_EXCHANGE_INFO_FIELD(Field) (MP_CPU_EXCHANGE_INFO_OFFSET + M= P_CPU_EXCHANGE_INFO. %+ Field) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 722ff3fd42..df297b5bf5 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -850,6 +850,30 @@ WaitApWakeup ( } } =20 +/** + Calculate the size of the reset vector. + + @param[in] AddressMap The pointer to Address Map structure. + + @return Total amount of memory required for the AP reset= area +**/ +STATIC +VOID +GetApResetVectorSize ( + IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap, + OUT UINTN *SizeBelow1Mb OPTIONAL, + OUT UINTN *SizeAbove1Mb OPTIONAL + ) +{ + if (SizeBelow1Mb !=3D NULL) { + *SizeBelow1Mb =3D AddressMap->ModeTransitionOffset + sizeof (MP_CPU_EX= CHANGE_INFO); + } + + if (SizeAbove1Mb !=3D NULL) { + *SizeAbove1Mb =3D AddressMap->RendezvousFunnelSize - AddressMap->ModeT= ransitionOffset; + } +} + /** This function will fill the exchange info structure. =20 @@ -937,21 +961,15 @@ FillExchangeInfoData ( // Copy all 32-bit code and 64-bit code into memory with type of // EfiBootServicesCode to avoid page fault if NX memory protection is en= abled. // - if (CpuMpData->WakeupBufferHigh !=3D 0) { - Size =3D CpuMpData->AddressMap.RendezvousFunnelSize - - CpuMpData->AddressMap.ModeTransitionOffset; - CopyMem ( - (VOID *)CpuMpData->WakeupBufferHigh, - CpuMpData->AddressMap.RendezvousFunnelAddress + - CpuMpData->AddressMap.ModeTransitionOffset, - Size - ); + GetApResetVectorSize (&CpuMpData->AddressMap, NULL, &Size); + CopyMem ( + (VOID *)CpuMpData->WakeupBufferHigh, + CpuMpData->AddressMap.RendezvousFunnelAddress + + CpuMpData->AddressMap.ModeTransitionOffset, + Size + ); =20 - ExchangeInfo->ModeTransitionMemory =3D (UINT32)CpuMpData->WakeupBuffer= High; - } else { - ExchangeInfo->ModeTransitionMemory =3D (UINT32) - (ExchangeInfo->BufferStart + CpuM= pData->AddressMap.ModeTransitionOffset); - } + ExchangeInfo->ModeTransitionMemory =3D (UINT32)CpuMpData->WakeupBufferHi= gh; =20 ExchangeInfo->ModeHighMemory =3D ExchangeInfo->ModeTransitionMemory + (UINT32)ExchangeInfo->ModeOffset - @@ -992,7 +1010,7 @@ BackupAndPrepareWakeupBuffer ( CopyMem ( (VOID *)CpuMpData->WakeupBuffer, (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress, - CpuMpData->AddressMap.RendezvousFunnelSize + CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO) ); } =20 @@ -1013,27 +1031,6 @@ RestoreWakeupBuffer ( ); } =20 -/** - Calculate the size of the reset vector. - - @param[in] AddressMap The pointer to Address Map structure. - - @return Total amount of memory required for the AP reset= area -**/ -STATIC -UINTN -GetApResetVectorSize ( - IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap - ) -{ - UINTN Size; - - Size =3D AddressMap->RendezvousFunnelSize + - sizeof (MP_CPU_EXCHANGE_INFO); - - return Size; -} - /** Allocate reset vector buffer. =20 @@ -1044,20 +1041,17 @@ AllocateResetVector ( IN OUT CPU_MP_DATA *CpuMpData ) { - UINTN ApResetVectorSize; + UINTN ApResetVectorSizeBelow1Mb; + UINTN ApResetVectorSizeAbove1Mb; UINTN ApResetStackSize; =20 if (CpuMpData->WakeupBuffer =3D=3D (UINTN)-1) { - ApResetVectorSize =3D GetApResetVectorSize (&CpuMpData->AddressMap); + GetApResetVectorSize (&CpuMpData->AddressMap, &ApResetVectorSizeBelow1= Mb, &ApResetVectorSizeAbove1Mb); =20 - CpuMpData->WakeupBuffer =3D GetWakeupBuffer (ApResetVectorSize); + CpuMpData->WakeupBuffer =3D GetWakeupBuffer (ApResetVectorSizeBel= ow1Mb); CpuMpData->MpCpuExchangeInfo =3D (MP_CPU_EXCHANGE_INFO *)(UINTN) - (CpuMpData->WakeupBuffer + - CpuMpData->AddressMap.RendezvousFunnel= Size); - CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer ( - CpuMpData->AddressMap.RendezvousFunnel= Size - - CpuMpData->AddressMap.ModeTransitionOf= fset - ); + (CpuMpData->WakeupBuffer + ApResetVecto= rSizeBelow1Mb - sizeof (MP_CPU_EXCHANGE_INFO)); + CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer (ApResetVectorSizeA= bove1Mb); // // The AP reset stack is only used by SEV-ES guests. Do not allocate it // if SEV-ES is not enabled. An SEV-SNP guest is also considered @@ -1796,7 +1790,7 @@ MpInitLibInitialize ( UINT8 ApLoopMode; UINT8 *MonitorBuffer; UINTN Index; - UINTN ApResetVectorSize; + UINTN ApResetVectorSizeBelow1Mb; UINTN BackupBufferAddr; UINTN ApIdtBase; =20 @@ -1814,7 +1808,7 @@ MpInitLibInitialize ( ASSERT (MaxLogicalProcessorNumber !=3D 0); =20 AsmGetAddressMap (&AddressMap); - ApResetVectorSize =3D GetApResetVectorSize (&AddressMap); + GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, NULL); ApStackSize =3D PcdGet32 (PcdCpuApStackSize); ApLoopMode =3D GetApLoopMode (&MonitorFilterSize); =20 @@ -1825,7 +1819,7 @@ MpInitLibInitialize ( =20 BufferSize =3D ApStackSize * MaxLogicalProcessorNumber; BufferSize +=3D MonitorFilterSize * MaxLogicalProcessorNumber; - BufferSize +=3D ApResetVectorSize; + BufferSize +=3D ApResetVectorSizeBelow1Mb; BufferSize =3D ALIGN_VALUE (BufferSize, 8); BufferSize +=3D VolatileRegisters.Idtr.Limit + 1; BufferSize +=3D sizeof (CPU_MP_DATA); @@ -1858,12 +1852,12 @@ MpInitLibInitialize ( // MonitorBuffer =3D (UINT8 *)(Buffer + ApStackSize * MaxLogi= calProcessorNumber); BackupBufferAddr =3D (UINTN)MonitorBuffer + MonitorFilterSize= * MaxLogicalProcessorNumber; - ApIdtBase =3D ALIGN_VALUE (BackupBufferAddr + ApResetV= ectorSize, 8); + ApIdtBase =3D ALIGN_VALUE (BackupBufferAddr + ApResetV= ectorSizeBelow1Mb, 8); CpuMpData =3D (CPU_MP_DATA *)(ApIdtBase + VolatileRegi= sters.Idtr.Limit + 1); CpuMpData->Buffer =3D Buffer; CpuMpData->CpuApStackSize =3D ApStackSize; CpuMpData->BackupBuffer =3D BackupBufferAddr; - CpuMpData->BackupBufferSize =3D ApResetVectorSize; + CpuMpData->BackupBufferSize =3D ApResetVectorSizeBelow1Mb; CpuMpData->WakeupBuffer =3D (UINTN)-1; CpuMpData->CpuCount =3D 1; CpuMpData->BspNumber =3D 0; --=20 2.32.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#89579): https://edk2.groups.io/g/devel/message/89579 Mute This Topic: https://groups.io/mt/90954629/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-