[PATCH v3 4/5] MpInitLib: Only allocate below 1MB memory for 16bit code


Ni, Ray
 

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 <ray.ni@...>
Cc: Eric Dong <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