From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=A5aF+8H9; spf=pass (domain: intel.com, ip: 134.134.136.24, mailfrom: ray.ni@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1651936405; x=1683472405; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dMw1sdOLJh/ppLYC7ft5TMmU6exynLxsMO2+A6VhQAY=; b=A5aF+8H9a1t4LNMVhrCXVNlCD/1R0CdL4Z8fnkXqEvncvvHUVg3TlBOJ 9uDBbLL0hz8ldKOkXo+v5flkR0FtowZSNXGoDnPZ0p5vpePt7o9ci9PBk mnDwS6jJ1wl6NPKkxLfiqGmUGh+Toe2ovSM33wFuDB2MDWc4ZY9VYVnto PfO6DbfAmNw3im9zHTto6tg9YlEXV5m+5c7hkCcVPoSPBWeZjdOhHMRRC E5b9a72x8qAhxnHsZtObsyBHkpV8GU0ELd01fxDiX2DOR0YRzkr2cG3gB z8RGdHuCkYbesPKxgjkmMteANx+HdXFmvaWAxDMnIBLtWAQk/ZXbk1ugo w==; 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" 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" 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: [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> X-Mailer: git-send-email 2.32.0.windows.1 In-Reply-To: <20220507151313.115-1-ray.ni@intel.com> References: <20220507151313.115-1-ray.ni@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 ();=0D Code32 =3D GetProtectedMode32CS ();=0D =0D - if (CpuMpData->WakeupBufferHigh !=3D 0) {=0D - APResetFn =3D (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->Ad= dressMap.SwitchToRealNoNxOffset);=0D - } else {=0D - APResetFn =3D (AP_RESET *)(CpuMpData->MpCpuExchangeInfo->BufferStart += CpuMpData->AddressMap.SwitchToRealOffset);=0D - }=0D + APResetFn =3D (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->Addr= essMap.SwitchToRealNoNxOffset);=0D =0D BufferStart =3D CpuMpData->MpCpuExchangeInfo->BufferStart;=0D StackStart =3D CpuMpData->SevEsAPResetStackStart -=0D 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=0D endstruc=0D =0D -MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelPro= cStart)=0D +MP_CPU_EXCHANGE_INFO_OFFSET equ (Flat32Start - RendezvousFunnelProcStart)= =0D %define MP_CPU_EXCHANGE_INFO_FIELD(Field) (MP_CPU_EXCHANGE_INFO_OFFSET + M= P_CPU_EXCHANGE_INFO. %+ Field)=0D 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 ( }=0D }=0D =0D +/**=0D + Calculate the size of the reset vector.=0D +=0D + @param[in] AddressMap The pointer to Address Map structure.=0D +=0D + @return Total amount of memory required for the AP reset= area=0D +**/=0D +STATIC=0D +VOID=0D +GetApResetVectorSize (=0D + IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap,=0D + OUT UINTN *SizeBelow1Mb OPTIONAL,=0D + OUT UINTN *SizeAbove1Mb OPTIONAL=0D + )=0D +{=0D + if (SizeBelow1Mb !=3D NULL) {=0D + *SizeBelow1Mb =3D AddressMap->ModeTransitionOffset + sizeof (MP_CPU_EX= CHANGE_INFO);=0D + }=0D +=0D + if (SizeAbove1Mb !=3D NULL) {=0D + *SizeAbove1Mb =3D AddressMap->RendezvousFunnelSize - AddressMap->ModeT= ransitionOffset;=0D + }=0D +}=0D +=0D /**=0D This function will fill the exchange info structure.=0D =0D @@ -937,21 +961,15 @@ FillExchangeInfoData ( // Copy all 32-bit code and 64-bit code into memory with type of=0D // EfiBootServicesCode to avoid page fault if NX memory protection is en= abled.=0D //=0D - if (CpuMpData->WakeupBufferHigh !=3D 0) {=0D - Size =3D CpuMpData->AddressMap.RendezvousFunnelSize -=0D - CpuMpData->AddressMap.ModeTransitionOffset;=0D - CopyMem (=0D - (VOID *)CpuMpData->WakeupBufferHigh,=0D - CpuMpData->AddressMap.RendezvousFunnelAddress +=0D - CpuMpData->AddressMap.ModeTransitionOffset,=0D - Size=0D - );=0D + GetApResetVectorSize (&CpuMpData->AddressMap, NULL, &Size);=0D + CopyMem (=0D + (VOID *)CpuMpData->WakeupBufferHigh,=0D + CpuMpData->AddressMap.RendezvousFunnelAddress +=0D + CpuMpData->AddressMap.ModeTransitionOffset,=0D + Size=0D + );=0D =0D - ExchangeInfo->ModeTransitionMemory =3D (UINT32)CpuMpData->WakeupBuffer= High;=0D - } else {=0D - ExchangeInfo->ModeTransitionMemory =3D (UINT32)=0D - (ExchangeInfo->BufferStart + CpuM= pData->AddressMap.ModeTransitionOffset);=0D - }=0D + ExchangeInfo->ModeTransitionMemory =3D (UINT32)CpuMpData->WakeupBufferHi= gh;=0D =0D ExchangeInfo->ModeHighMemory =3D ExchangeInfo->ModeTransitionMemory +=0D (UINT32)ExchangeInfo->ModeOffset -=0D @@ -992,7 +1010,7 @@ BackupAndPrepareWakeupBuffer ( CopyMem (=0D (VOID *)CpuMpData->WakeupBuffer,=0D (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress,=0D - CpuMpData->AddressMap.RendezvousFunnelSize=0D + CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO)=0D );=0D }=0D =0D @@ -1013,27 +1031,6 @@ RestoreWakeupBuffer ( );=0D }=0D =0D -/**=0D - Calculate the size of the reset vector.=0D -=0D - @param[in] AddressMap The pointer to Address Map structure.=0D -=0D - @return Total amount of memory required for the AP reset= area=0D -**/=0D -STATIC=0D -UINTN=0D -GetApResetVectorSize (=0D - IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap=0D - )=0D -{=0D - UINTN Size;=0D -=0D - Size =3D AddressMap->RendezvousFunnelSize +=0D - sizeof (MP_CPU_EXCHANGE_INFO);=0D -=0D - return Size;=0D -}=0D -=0D /**=0D Allocate reset vector buffer.=0D =0D @@ -1044,20 +1041,17 @@ AllocateResetVector ( IN OUT CPU_MP_DATA *CpuMpData=0D )=0D {=0D - UINTN ApResetVectorSize;=0D + UINTN ApResetVectorSizeBelow1Mb;=0D + UINTN ApResetVectorSizeAbove1Mb;=0D UINTN ApResetStackSize;=0D =0D if (CpuMpData->WakeupBuffer =3D=3D (UINTN)-1) {=0D - ApResetVectorSize =3D GetApResetVectorSize (&CpuMpData->AddressMap);=0D + GetApResetVectorSize (&CpuMpData->AddressMap, &ApResetVectorSizeBelow1= Mb, &ApResetVectorSizeAbove1Mb);=0D =0D - CpuMpData->WakeupBuffer =3D GetWakeupBuffer (ApResetVectorSize);= =0D + CpuMpData->WakeupBuffer =3D GetWakeupBuffer (ApResetVectorSizeBel= ow1Mb);=0D CpuMpData->MpCpuExchangeInfo =3D (MP_CPU_EXCHANGE_INFO *)(UINTN)=0D - (CpuMpData->WakeupBuffer +=0D - CpuMpData->AddressMap.RendezvousFunnel= Size);=0D - CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer (=0D - CpuMpData->AddressMap.RendezvousFunnel= Size -=0D - CpuMpData->AddressMap.ModeTransitionOf= fset=0D - );=0D + (CpuMpData->WakeupBuffer + ApResetVecto= rSizeBelow1Mb - sizeof (MP_CPU_EXCHANGE_INFO));=0D + CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer (ApResetVectorSizeA= bove1Mb);=0D //=0D // The AP reset stack is only used by SEV-ES guests. Do not allocate i= t=0D // if SEV-ES is not enabled. An SEV-SNP guest is also considered=0D @@ -1796,7 +1790,7 @@ MpInitLibInitialize ( UINT8 ApLoopMode;=0D UINT8 *MonitorBuffer;=0D UINTN Index;=0D - UINTN ApResetVectorSize;=0D + UINTN ApResetVectorSizeBelow1Mb;=0D UINTN BackupBufferAddr;=0D UINTN ApIdtBase;=0D =0D @@ -1814,7 +1808,7 @@ MpInitLibInitialize ( ASSERT (MaxLogicalProcessorNumber !=3D 0);=0D =0D AsmGetAddressMap (&AddressMap);=0D - ApResetVectorSize =3D GetApResetVectorSize (&AddressMap);=0D + GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, NULL);=0D ApStackSize =3D PcdGet32 (PcdCpuApStackSize);=0D ApLoopMode =3D GetApLoopMode (&MonitorFilterSize);=0D =0D @@ -1825,7 +1819,7 @@ MpInitLibInitialize ( =0D BufferSize =3D ApStackSize * MaxLogicalProcessorNumber;=0D BufferSize +=3D MonitorFilterSize * MaxLogicalProcessorNumber;=0D - BufferSize +=3D ApResetVectorSize;=0D + BufferSize +=3D ApResetVectorSizeBelow1Mb;=0D BufferSize =3D ALIGN_VALUE (BufferSize, 8);=0D BufferSize +=3D VolatileRegisters.Idtr.Limit + 1;=0D BufferSize +=3D sizeof (CPU_MP_DATA);=0D @@ -1858,12 +1852,12 @@ MpInitLibInitialize ( //=0D MonitorBuffer =3D (UINT8 *)(Buffer + ApStackSize * MaxLogi= calProcessorNumber);=0D BackupBufferAddr =3D (UINTN)MonitorBuffer + MonitorFilterSize= * MaxLogicalProcessorNumber;=0D - ApIdtBase =3D ALIGN_VALUE (BackupBufferAddr + ApResetV= ectorSize, 8);=0D + ApIdtBase =3D ALIGN_VALUE (BackupBufferAddr + ApResetV= ectorSizeBelow1Mb, 8);=0D CpuMpData =3D (CPU_MP_DATA *)(ApIdtBase + VolatileRegi= sters.Idtr.Limit + 1);=0D CpuMpData->Buffer =3D Buffer;=0D CpuMpData->CpuApStackSize =3D ApStackSize;=0D CpuMpData->BackupBuffer =3D BackupBufferAddr;=0D - CpuMpData->BackupBufferSize =3D ApResetVectorSize;=0D + CpuMpData->BackupBufferSize =3D ApResetVectorSizeBelow1Mb;=0D CpuMpData->WakeupBuffer =3D (UINTN)-1;=0D CpuMpData->CpuCount =3D 1;=0D CpuMpData->BspNumber =3D 0;=0D --=20 2.32.0.windows.1