[PATCH v3 5/5] MpInitLib: Move the Above1Mb vector allocation to MpInitLibInitialize


Ni, Ray
 

The AP vector consists of 2 parts:
1. the initial 16-bit code that should be under 1MB and page aligned.
2. the 32-bit/64-bit code that can be anywhere in the memory with any
alignment.

The need of part #2 is because the memory under 1MB is temporary
"stolen" for use and will "give" back after all AP wake up. The range
of memory is not marked as code page in page table. CPU may trigger
exception as soon as NX is enabled.

The part #2 memory allocation can be done in the MpInitLibInitialize.

Signed-off-by: Ray Ni <ray.ni@...>
Cc: Eric Dong <eric.dong@...>
---
UefiCpuPkg/Library/MpInitLib/MpLib.c | 53 +++++++++++++++-------------
1 file changed, 29 insertions(+), 24 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn=
itLib/MpLib.c
index e4edbb618d..66e0f94f03 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -955,18 +955,6 @@ FillExchangeInfoData (
Size -=3D sizeof (IA32_SEGMENT_DESCRIPTOR);=0D
}=0D
=0D
- //=0D
- // 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
- 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->WakeupBufferHi=
gh;=0D
=0D
ExchangeInfo->ModeHighMemory =3D ExchangeInfo->ModeTransitionMemory +=0D
@@ -1035,21 +1023,24 @@ RestoreWakeupBuffer (
@param[in, out] CpuMpData The pointer to CPU MP Data structure.=0D
**/=0D
VOID=0D
-AllocateResetVector (=0D
+AllocateResetVectorBelow1Mb (=0D
IN OUT CPU_MP_DATA *CpuMpData=0D
)=0D
{=0D
- UINTN ApResetVectorSizeBelow1Mb;=0D
- UINTN ApResetVectorSizeAbove1Mb;=0D
UINTN ApResetStackSize;=0D
=0D
if (CpuMpData->WakeupBuffer =3D=3D (UINTN)-1) {=0D
- GetApResetVectorSize (&CpuMpData->AddressMap, &ApResetVectorSizeBelow1=
Mb, &ApResetVectorSizeAbove1Mb);=0D
-=0D
- CpuMpData->WakeupBuffer =3D GetWakeupBuffer (ApResetVectorSizeBel=
ow1Mb);=0D
+ CpuMpData->WakeupBuffer =3D GetWakeupBuffer (CpuMpData->BackupBuf=
ferSize);=0D
CpuMpData->MpCpuExchangeInfo =3D (MP_CPU_EXCHANGE_INFO *)(UINTN)=0D
- (CpuMpData->WakeupBuffer + ApResetVecto=
rSizeBelow1Mb - sizeof (MP_CPU_EXCHANGE_INFO));=0D
- CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer (ApResetVectorSizeA=
bove1Mb);=0D
+ (CpuMpData->WakeupBuffer + CpuMpData->B=
ackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO));=0D
+ DEBUG ((=0D
+ DEBUG_INFO,=0D
+ "AP Vector: 16-bit =3D %p/%x, ExchangeInfo =3D %p/%x\n",=0D
+ CpuMpData->WakeupBuffer,=0D
+ CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO),=0D
+ CpuMpData->MpCpuExchangeInfo,=0D
+ sizeof (MP_CPU_EXCHANGE_INFO)=0D
+ ));=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
@@ -1148,7 +1139,7 @@ WakeUpAP (
(CpuMpData->InitFlag !=3D ApInitDone))=0D
{=0D
ResetVectorRequired =3D TRUE;=0D
- AllocateResetVector (CpuMpData);=0D
+ AllocateResetVectorBelow1Mb (CpuMpData);=0D
AllocateSevEsAPMemory (CpuMpData);=0D
FillExchangeInfoData (CpuMpData);=0D
SaveLocalApicTimerSetting (CpuMpData);=0D
@@ -1789,6 +1780,7 @@ MpInitLibInitialize (
UINT8 *MonitorBuffer;=0D
UINTN Index;=0D
UINTN ApResetVectorSizeBelow1Mb;=0D
+ UINTN ApResetVectorSizeAbove1Mb;=0D
UINTN BackupBufferAddr;=0D
UINTN ApIdtBase;=0D
=0D
@@ -1802,9 +1794,9 @@ MpInitLibInitialize (
ASSERT (MaxLogicalProcessorNumber !=3D 0);=0D
=0D
AsmGetAddressMap (&AddressMap);=0D
- GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, NULL);=0D
- ApStackSize =3D PcdGet32 (PcdCpuApStackSize);=0D
- ApLoopMode =3D GetApLoopMode (&MonitorFilterSize);=0D
+ GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, &ApResetV=
ectorSizeAbove1Mb);=0D
+ ApStackSize =3D PcdGet32 (PcdCpuApStackSize);=0D
+ ApLoopMode =3D GetApLoopMode (&MonitorFilterSize);=0D
=0D
//=0D
// Save BSP's Control registers for APs.=0D
@@ -1913,6 +1905,19 @@ MpInitLibInitialize (
(UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);=0D
}=0D
=0D
+ //=0D
+ // 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
+ CpuMpData->WakeupBufferHigh =3D AllocateCodeBuffer (ApResetVectorSizeAbo=
ve1Mb);=0D
+ CopyMem (=0D
+ (VOID *)CpuMpData->WakeupBufferHigh,=0D
+ CpuMpData->AddressMap.RendezvousFunnelAddress +=0D
+ CpuMpData->AddressMap.ModeTransitionOffset,=0D
+ ApResetVectorSizeAbove1Mb=0D
+ );=0D
+ DEBUG ((DEBUG_INFO, "AP Vector: non-16-bit =3D %p/%x\n", CpuMpData->Wake=
upBufferHigh, ApResetVectorSizeAbove1Mb));=0D
+=0D
//=0D
// Enable the local APIC for Virtual Wire Mode.=0D
//=0D
--=20
2.35.1.windows.2