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.web09.26058.1652685263921678640 for ; Mon, 16 May 2022 00:14:28 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=WTeaw3UV; 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=1652685267; x=1684221267; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nlBn9qDyuU5nty5L6xUNgiJSQLtOgO80FY88uc/LMQE=; b=WTeaw3UVb2RajmnGNbBN0TOa1FdrKjmq/XQDzRIZIvGxYS4mP+6mx+o4 7I8f8th1W+QCutCRW1WsvPSVYpHZRFOiiMhvHq1IM3GI68oFSngsHLPP3 ZTTL0FenmtyteC58NoZZ5dYtielBoF1WTI2Lmax21Eb4OZs76BDggh7Gu Tk5RPkItUBExvmfIC7bAvOMQZvdNrdardyMaczJCvecBLcgLY6E5quJXc e0NDuiwvxcERvaP3RrAANgr/AYUjQ4YbIdM5MaQ/zwthow9tButU01BI5 mEdjbVzXwmA2Re/bYHgzPQm8lQGlM58nOiwCR05wAG5zm9ggrMzNZlsqM w==; X-IronPort-AV: E=McAfee;i="6400,9594,10348"; a="270451791" X-IronPort-AV: E=Sophos;i="5.91,229,1647327600"; d="scan'208";a="270451791" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2022 00:14:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,229,1647327600"; d="scan'208";a="555129046" Received: from shwdeopenlab706.ccr.corp.intel.com ([10.239.183.102]) by orsmga002.jf.intel.com with ESMTP; 16 May 2022 00:14:25 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Eric Dong Subject: [PATCH v3 4/5] MpInitLib: Only allocate below 1MB memory for 16bit code Date: Mon, 16 May 2022 15:14:11 +0800 Message-Id: <20220516071412.359-5-ray.ni@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 In-Reply-To: <20220516071412.359-1-ray.ni@intel.com> References: <20220516071412.359-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 1cc071cf7b..ebadcc6fb3 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc +++ b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc @@ -95,5 +95,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 aa0eb9a70b..e4edbb618d 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -848,6 +848,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 @@ -935,21 +959,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 @@ -990,7 +1008,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 @@ -1011,27 +1029,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 @@ -1042,20 +1039,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 @@ -1794,7 +1788,7 @@ MpInitLibInitialize ( UINT8 ApLoopMode;=0D UINT8 *MonitorBuffer;=0D UINTN Index;=0D - UINTN ApResetVectorSize;=0D + UINTN ApResetVectorSizeBelow1Mb;=0D UINTN BackupBufferAddr;=0D UINTN ApIdtBase;=0D =0D @@ -1808,7 +1802,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 @@ -1819,7 +1813,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 @@ -1852,12 +1846,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.35.1.windows.2