From: "Ni, Ray" <ray.ni@intel.com>
To: devel@edk2.groups.io
Cc: Eric Dong <eric.dong@intel.com>
Subject: [PATCH v3 4/5] MpInitLib: Only allocate below 1MB memory for 16bit code
Date: Mon, 16 May 2022 15:14:11 +0800 [thread overview]
Message-ID: <20220516071412.359-5-ray.ni@intel.com> (raw)
In-Reply-To: <20220516071412.359-1-ray.ni@intel.com>
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@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
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/MpInitLib/AmdSev.c
index b4a344ee6b..4e4c63a52d 100644
--- a/UefiCpuPkg/Library/MpInitLib/AmdSev.c
+++ b/UefiCpuPkg/Library/MpInitLib/AmdSev.c
@@ -110,11 +110,7 @@ MpInitLibSevEsAPReset (
Code16 = GetProtectedMode16CS ();
Code32 = GetProtectedMode32CS ();
- if (CpuMpData->WakeupBufferHigh != 0) {
- APResetFn = (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->AddressMap.SwitchToRealNoNxOffset);
- } else {
- APResetFn = (AP_RESET *)(CpuMpData->MpCpuExchangeInfo->BufferStart + CpuMpData->AddressMap.SwitchToRealOffset);
- }
+ APResetFn = (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->AddressMap.SwitchToRealNoNxOffset);
BufferStart = CpuMpData->MpCpuExchangeInfo->BufferStart;
StackStart = CpuMpData->SevEsAPResetStackStart -
diff --git a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc b/UefiCpuPkg/Library/MpInitLib/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
endstruc
-MP_CPU_EXCHANGE_INFO_OFFSET equ (SwitchToRealProcEnd - RendezvousFunnelProcStart)
+MP_CPU_EXCHANGE_INFO_OFFSET equ (Flat32Start - RendezvousFunnelProcStart)
%define MP_CPU_EXCHANGE_INFO_FIELD(Field) (MP_CPU_EXCHANGE_INFO_OFFSET + MP_CPU_EXCHANGE_INFO. %+ Field)
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index aa0eb9a70b..e4edbb618d 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -848,6 +848,30 @@ WaitApWakeup (
}
}
+/**
+ 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 != NULL) {
+ *SizeBelow1Mb = AddressMap->ModeTransitionOffset + sizeof (MP_CPU_EXCHANGE_INFO);
+ }
+
+ if (SizeAbove1Mb != NULL) {
+ *SizeAbove1Mb = AddressMap->RendezvousFunnelSize - AddressMap->ModeTransitionOffset;
+ }
+}
+
/**
This function will fill the exchange info structure.
@@ -935,21 +959,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 enabled.
//
- if (CpuMpData->WakeupBufferHigh != 0) {
- Size = 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
+ );
- ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh;
- } else {
- ExchangeInfo->ModeTransitionMemory = (UINT32)
- (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset);
- }
+ ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh;
ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory +
(UINT32)ExchangeInfo->ModeOffset -
@@ -990,7 +1008,7 @@ BackupAndPrepareWakeupBuffer (
CopyMem (
(VOID *)CpuMpData->WakeupBuffer,
(VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress,
- CpuMpData->AddressMap.RendezvousFunnelSize
+ CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO)
);
}
@@ -1011,27 +1029,6 @@ RestoreWakeupBuffer (
);
}
-/**
- 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 = AddressMap->RendezvousFunnelSize +
- sizeof (MP_CPU_EXCHANGE_INFO);
-
- return Size;
-}
-
/**
Allocate reset vector buffer.
@@ -1042,20 +1039,17 @@ AllocateResetVector (
IN OUT CPU_MP_DATA *CpuMpData
)
{
- UINTN ApResetVectorSize;
+ UINTN ApResetVectorSizeBelow1Mb;
+ UINTN ApResetVectorSizeAbove1Mb;
UINTN ApResetStackSize;
if (CpuMpData->WakeupBuffer == (UINTN)-1) {
- ApResetVectorSize = GetApResetVectorSize (&CpuMpData->AddressMap);
+ GetApResetVectorSize (&CpuMpData->AddressMap, &ApResetVectorSizeBelow1Mb, &ApResetVectorSizeAbove1Mb);
- CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize);
+ CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSizeBelow1Mb);
CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN)
- (CpuMpData->WakeupBuffer +
- CpuMpData->AddressMap.RendezvousFunnelSize);
- CpuMpData->WakeupBufferHigh = AllocateCodeBuffer (
- CpuMpData->AddressMap.RendezvousFunnelSize -
- CpuMpData->AddressMap.ModeTransitionOffset
- );
+ (CpuMpData->WakeupBuffer + ApResetVectorSizeBelow1Mb - sizeof (MP_CPU_EXCHANGE_INFO));
+ CpuMpData->WakeupBufferHigh = AllocateCodeBuffer (ApResetVectorSizeAbove1Mb);
//
// 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
@@ -1794,7 +1788,7 @@ MpInitLibInitialize (
UINT8 ApLoopMode;
UINT8 *MonitorBuffer;
UINTN Index;
- UINTN ApResetVectorSize;
+ UINTN ApResetVectorSizeBelow1Mb;
UINTN BackupBufferAddr;
UINTN ApIdtBase;
@@ -1808,7 +1802,7 @@ MpInitLibInitialize (
ASSERT (MaxLogicalProcessorNumber != 0);
AsmGetAddressMap (&AddressMap);
- ApResetVectorSize = GetApResetVectorSize (&AddressMap);
+ GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, NULL);
ApStackSize = PcdGet32 (PcdCpuApStackSize);
ApLoopMode = GetApLoopMode (&MonitorFilterSize);
@@ -1819,7 +1813,7 @@ MpInitLibInitialize (
BufferSize = ApStackSize * MaxLogicalProcessorNumber;
BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
- BufferSize += ApResetVectorSize;
+ BufferSize += ApResetVectorSizeBelow1Mb;
BufferSize = ALIGN_VALUE (BufferSize, 8);
BufferSize += VolatileRegisters.Idtr.Limit + 1;
BufferSize += sizeof (CPU_MP_DATA);
@@ -1852,12 +1846,12 @@ MpInitLibInitialize (
//
MonitorBuffer = (UINT8 *)(Buffer + ApStackSize * MaxLogicalProcessorNumber);
BackupBufferAddr = (UINTN)MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
- ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8);
+ ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSizeBelow1Mb, 8);
CpuMpData = (CPU_MP_DATA *)(ApIdtBase + VolatileRegisters.Idtr.Limit + 1);
CpuMpData->Buffer = Buffer;
CpuMpData->CpuApStackSize = ApStackSize;
CpuMpData->BackupBuffer = BackupBufferAddr;
- CpuMpData->BackupBufferSize = ApResetVectorSize;
+ CpuMpData->BackupBufferSize = ApResetVectorSizeBelow1Mb;
CpuMpData->WakeupBuffer = (UINTN)-1;
CpuMpData->CpuCount = 1;
CpuMpData->BspNumber = 0;
--
2.35.1.windows.2
next prev parent reply other threads:[~2022-05-16 7:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-16 7:14 [PATCH v3 0/5] MpInitLib code refactoring Ni, Ray
2022-05-16 7:14 ` [PATCH v3 1/5] MpInitLib: Allocate code buffer for PEI phase Ni, Ray
2022-05-16 7:14 ` [PATCH v3 2/5] MpInitLib: remove unneeded global ASM_PFX Ni, Ray
2022-05-16 7:14 ` [PATCH v3 3/5] MpInitLib: Put SEV logic in separate file Ni, Ray
2022-05-16 13:41 ` Lendacky, Thomas
2022-05-16 7:14 ` Ni, Ray [this message]
2022-05-16 7:14 ` [PATCH v3 5/5] MpInitLib: Move the Above1Mb vector allocation to MpInitLibInitialize Ni, Ray
2022-06-10 8:19 ` [edk2-devel] [PATCH v3 0/5] MpInitLib code refactoring Dong, Eric
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220516071412.359-5-ray.ni@intel.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox