[PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers


Ni, Ray
 

InitializeCpuExceptionHandlers() expects caller allocates IDT while
InitializeCpuInterruptHandlers() allocates 256 IDT entries itself.

InitializeCpuExceptionHandlers() fills max 32 IDT entries allocated
by caller. If caller allocates 10 entries, the API just fills 10 IDT
entries.

The inconsistency between the two APIs makes code hard to
unerstand and hard to share.

Because there is only one caller (CpuDxe) for
InitializeCpuInterruptHandler(), this patch updates CpuDxe driver
to allocates 256 IDT entries then call
InitializeCpuExceptionHandlers().

With this change, InitializeCpuInterruptHandlers() is removed
completely.

And InitializeCpuExceptionHandlers() fills max 32 entries for PEI
and SMM instance, max 256 entries for DXE instance.
Such behavior matches to the original one.

Signed-off-by: Ray Ni <ray.ni@...>
Cc: Eric Dong <eric.dong@...>
---
.../Include/Library/CpuExceptionHandlerLib.h | 28 +------
.../CpuExceptionHandlerLibNull.c | 31 +------
UefiCpuPkg/CpuDxe/CpuDxe.c | 33 ++++++--
.../CpuExceptionHandlerLib/DxeException.c | 80 ++-----------------
.../CpuExceptionHandlerLib/PeiCpuException.c | 61 +-------------
.../PeiDxeSmmCpuException.c | 19 ++---
.../SecPeiCpuException.c | 31 +------
.../CpuExceptionHandlerLib/SmmException.c | 35 ++------
8 files changed, 56 insertions(+), 262 deletions(-)

diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h b/MdeMod=
ulePkg/Include/Library/CpuExceptionHandlerLib.h
index 22a4408f9f..d4649bebe1 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -2,7 +2,7 @@
CPU Exception library provides the default CPU interrupt/exception handl=
er.=0D
It also provides capability to register user interrupt/exception handler=
.=0D
=0D
- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -132,28 +132,6 @@ InitializeCpuExceptionHandlersEx (
IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL=0D
);=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- );=0D
-=0D
/**=0D
Registers a function to be called from the processor interrupt handler.=
=0D
=0D
@@ -161,8 +139,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
+ NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() is invoked,=0D
+ otherwise EFI_UNSUPPORTED returned.=0D
=0D
@param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
diff --git a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHa=
ndlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExcepti=
onHandlerLibNull.c
index 35ab5a8db5..54f38788fe 100644
--- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi=
bNull.c
+++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi=
bNull.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU Exception Handler library implementition with empty functions.=0D
=0D
- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -33,31 +33,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;=0D
}=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- )=0D
-{=0D
- return EFI_SUCCESS;=0D
-}=0D
-=0D
/**=0D
Registers a function to be called from the processor interrupt handler.=
=0D
=0D
@@ -65,8 +40,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
+ NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() is invoked,=0D
+ otherwise EFI_UNSUPPORTED returned.=0D
=0D
@param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
index 00f3cb0957..a6a91507f6 100644
--- a/UefiCpuPkg/CpuDxe/CpuDxe.c
+++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU DXE Module to produce CPU ARCH Protocol.=0D
=0D
- Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+ Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -10,6 +10,8 @@
#include "CpuMp.h"=0D
#include "CpuPageTable.h"=0D
=0D
+#define CPU_INTERRUPT_NUM 256=0D
+=0D
//=0D
// Global Variables=0D
//=0D
@@ -924,9 +926,12 @@ InitInterruptDescriptorTable (
VOID=0D
)=0D
{=0D
- EFI_STATUS Status;=0D
- EFI_VECTOR_HANDOFF_INFO *VectorInfoList;=0D
- EFI_VECTOR_HANDOFF_INFO *VectorInfo;=0D
+ EFI_STATUS Status;=0D
+ EFI_VECTOR_HANDOFF_INFO *VectorInfoList;=0D
+ EFI_VECTOR_HANDOFF_INFO *VectorInfo;=0D
+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;=0D
+ IA32_DESCRIPTOR IdtDescriptor;=0D
+ UINTN IdtEntryCount;=0D
=0D
VectorInfo =3D NULL;=0D
Status =3D EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGu=
id, (VOID **)&VectorInfoList);=0D
@@ -934,7 +939,25 @@ InitInterruptDescriptorTable (
VectorInfo =3D VectorInfoList;=0D
}=0D
=0D
- Status =3D InitializeCpuInterruptHandlers (VectorInfo);=0D
+ AsmReadIdtr (&IdtDescriptor);=0D
+ IdtEntryCount =3D (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESC=
RIPTOR);=0D
+ if (IdtEntryCount < CPU_INTERRUPT_NUM) {=0D
+ //=0D
+ // Increase Interrupt Descriptor Table and Copy the old IDT table in=0D
+ //=0D
+ IdtTable =3D AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU=
_INTERRUPT_NUM);=0D
+ ASSERT (IdtTable !=3D NULL);=0D
+ CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_D=
ESCRIPTOR) * IdtEntryCount);=0D
+=0D
+ //=0D
+ // Load Interrupt Descriptor Table=0D
+ //=0D
+ IdtDescriptor.Base =3D (UINTN)IdtTable;=0D
+ IdtDescriptor.Limit =3D (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * C=
PU_INTERRUPT_NUM - 1);=0D
+ AsmWriteIdtr (&IdtDescriptor);=0D
+ }=0D
+=0D
+ Status =3D InitializeCpuExceptionHandlers (VectorInfo);=0D
ASSERT_EFI_ERROR (Status);=0D
}=0D
=0D
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/Uef=
iCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index f139131a7c..c7c1fe31d2 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU exception handler library implemenation for DXE modules.=0D
=0D
- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>=0D
+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -17,8 +17,8 @@ CONST UINTN mDoFarReturnFlag =3D 0;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_INTERRUPT_NUM];=0D
EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_INTERRUPT_NU=
M];=0D
EXCEPTION_HANDLER_DATA mExceptionHandlerData =3D {=0D
- 0, // To be fixed=0D
- 0, // To be fixed=0D
+ CPU_INTERRUPT_NUM,=0D
+ 0, // To be fixed=0D
mReservedVectorsData,=0D
mExternalInterruptHandlerTable=0D
};=0D
@@ -69,76 +69,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHand=
lerData);=0D
}=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- )=0D
-{=0D
- EFI_STATUS Status;=0D
- IA32_IDT_GATE_DESCRIPTOR *IdtTable;=0D
- IA32_DESCRIPTOR IdtDescriptor;=0D
- UINTN IdtEntryCount;=0D
- EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;=0D
-=0D
- SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA) * C=
PU_INTERRUPT_NUM, 0xff);=0D
- if (VectorInfo !=3D NULL) {=0D
- Status =3D ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData, =
CPU_INTERRUPT_NUM);=0D
- if (EFI_ERROR (Status)) {=0D
- return EFI_INVALID_PARAMETER;=0D
- }=0D
- }=0D
-=0D
- //=0D
- // Read IDT descriptor and calculate IDT size=0D
- //=0D
- AsmReadIdtr (&IdtDescriptor);=0D
- IdtEntryCount =3D (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESC=
RIPTOR);=0D
- if (IdtEntryCount > CPU_INTERRUPT_NUM) {=0D
- IdtEntryCount =3D CPU_INTERRUPT_NUM;=0D
- }=0D
-=0D
- //=0D
- // Create Interrupt Descriptor Table and Copy the old IDT table in=0D
- //=0D
- IdtTable =3D AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_I=
NTERRUPT_NUM);=0D
- ASSERT (IdtTable !=3D NULL);=0D
- CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DES=
CRIPTOR) * IdtEntryCount);=0D
-=0D
- AsmGetTemplateAddressMap (&TemplateMap);=0D
- ASSERT (TemplateMap.ExceptionStubHeaderSize <=3D HOOKAFTER_STUB_SIZE);=0D
-=0D
- mExceptionHandlerData.IdtEntryCount =3D CPU_INTERRUPT_NUM;=0D
- InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);=0D
-=0D
- UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);=0D
-=0D
- //=0D
- // Load Interrupt Descriptor Table=0D
- //=0D
- IdtDescriptor.Base =3D (UINTN)IdtTable;=0D
- IdtDescriptor.Limit =3D (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU=
_INTERRUPT_NUM - 1);=0D
- AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);=0D
-=0D
- return EFI_SUCCESS;=0D
-}=0D
-=0D
/**=0D
Registers a function to be called from the processor interrupt handler.=
=0D
=0D
@@ -146,8 +76,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
+ NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() is invoked,=0D
+ otherwise EFI_UNSUPPORTED returned.=0D
=0D
@param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c b/=
UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 687fc4177f..1ae611c75e 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU exception handler library implementation for PEIM module.=0D
=0D
-Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -133,6 +133,7 @@ InitializeCpuExceptionHandlers (
=0D
ExceptionHandlerData =3D AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));=
=0D
ASSERT (ExceptionHandlerData !=3D NULL);=0D
+ ExceptionHandlerData->IdtEntryCount =3D CPU_EXCEPTION_NUM;=0D
ExceptionHandlerData->ReservedVectors =3D ReservedVectors;=0D
ExceptionHandlerData->ExternalInterruptHandler =3D NULL;=0D
InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);=0D
@@ -148,64 +149,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;=0D
}=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- )=0D
-{=0D
- return EFI_UNSUPPORTED;=0D
-}=0D
-=0D
-/**=0D
- Registers a function to be called from the processor interrupt handler.=
=0D
-=0D
- This function registers and enables the handler specified by InterruptHa=
ndler for a processor=0D
- interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
- handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
- The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
-=0D
- @param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
- @param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
- when a processor interrupt occurs. If this=
parameter is NULL, then the handler=0D
- will be uninstalled.=0D
-=0D
- @retval EFI_SUCCESS The handler for the processor interrupt wa=
s successfully installed or uninstalled.=0D
- @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handle=
r for InterruptType was=0D
- previously installed.=0D
- @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler fo=
r InterruptType was not=0D
- previously installed.=0D
- @retval EFI_UNSUPPORTED The interrupt specified by InterruptType i=
s not supported,=0D
- or this function is not supported.=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-RegisterCpuInterruptHandler (=0D
- IN EFI_EXCEPTION_TYPE InterruptType,=0D
- IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler=0D
- )=0D
-{=0D
- return EFI_UNSUPPORTED;=0D
-}=0D
-=0D
/**=0D
Initializes all CPU exceptions entries with optional extra initializatio=
ns.=0D
=0D
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuExceptio=
n.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
index f47a80dcab..a7d0897ef1 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.=
=0D
=0D
-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -261,31 +261,26 @@ InitializeCpuExceptionHandlersWorker (
RESERVED_VECTORS_DATA *ReservedVectors;=0D
=0D
ReservedVectors =3D ExceptionHandlerData->ReservedVectors;=0D
- SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_EX=
CEPTION_NUM, 0xff);=0D
+ SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * Except=
ionHandlerData->IdtEntryCount, 0xff);=0D
if (VectorInfo !=3D NULL) {=0D
- Status =3D ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_E=
XCEPTION_NUM);=0D
+ Status =3D ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, Excep=
tionHandlerData->IdtEntryCount);=0D
if (EFI_ERROR (Status)) {=0D
return EFI_INVALID_PARAMETER;=0D
}=0D
}=0D
=0D
//=0D
- // Read IDT descriptor and calculate IDT size=0D
+ // Setup the exception handlers according to IDT size, but no more than=
=0D
+ // ExceptionHandlerData->IdtEntryCount (32 in PEI and SMM, 256 in DXE)=
handlers.=0D
//=0D
AsmReadIdtr (&IdtDescriptor);=0D
- IdtEntryCount =3D (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESC=
RIPTOR);=0D
- if (IdtEntryCount > CPU_EXCEPTION_NUM) {=0D
- //=0D
- // CPU exception library only setup CPU_EXCEPTION_NUM exception handle=
r at most=0D
- //=0D
- IdtEntryCount =3D CPU_EXCEPTION_NUM;=0D
- }=0D
+ IdtEntryCount =3D (IdtDescriptor.Limit + 1) / size=
of (IA32_IDT_GATE_DESCRIPTOR);=0D
+ ExceptionHandlerData->IdtEntryCount =3D MIN (IdtEntryCount, ExceptionHan=
dlerData->IdtEntryCount);=0D
=0D
IdtTable =3D (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;=0D
AsmGetTemplateAddressMap (&TemplateMap);=0D
ASSERT (TemplateMap.ExceptionStubHeaderSize <=3D HOOKAFTER_STUB_SIZE);=0D
=0D
- ExceptionHandlerData->IdtEntryCount =3D IdtEntryCount;=0D
UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);=0D
=0D
return EFI_SUCCESS;=0D
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c=
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index 6e5216380d..e894ead612 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU exception handler library implemenation for SEC/PEIM modules.=0D
=0D
-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>=0D
+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -166,31 +166,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;=0D
}=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- )=0D
-{=0D
- return EFI_UNSUPPORTED;=0D
-}=0D
-=0D
/**=0D
Registers a function to be called from the processor interrupt handler.=
=0D
=0D
@@ -198,8 +173,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
+ NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() is invoked,=0D
+ otherwise EFI_UNSUPPORTED returned.=0D
=0D
@param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/Uef=
iCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 9f0af4120a..ec643556c7 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -1,7 +1,7 @@
/** @file=0D
CPU exception handler library implementation for SMM modules.=0D
=0D
- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>=0D
+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
**/=0D
@@ -14,8 +14,8 @@ CONST UINTN mDoFarReturnFlag =3D 1;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];=0D
EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NU=
M];=0D
EXCEPTION_HANDLER_DATA mExceptionHandlerData =3D {=0D
- 0, // To be fixed=0D
- 0, // To be fixed=0D
+ CPU_EXCEPTION_NUM,=0D
+ 0, // To be fixed=0D
mReservedVectorsData,=0D
mExternalInterruptHandlerTable=0D
};=0D
@@ -62,31 +62,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHand=
lerData);=0D
}=0D
=0D
-/**=0D
- Initializes all CPU interrupt/exceptions entries and provides the defaul=
t interrupt/exception handlers.=0D
-=0D
- Caller should try to get an array of interrupt and/or exception vectors =
that are in use and need to=0D
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.=0D
- If caller cannot get reserved vector list or it does not exists, set Vec=
torInfo to NULL.=0D
- If VectorInfo is not NULL, the exception vectors will be initialized per=
vector attribute accordingly.=0D
-=0D
- @param[in] VectorInfo Pointer to reserved vector list.=0D
-=0D
- @retval EFI_SUCCESS All CPU interrupt/exception entries have b=
een successfully initialized=0D
- with default interrupt/exception handlers.=
=0D
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if=
VectorInfo is not NULL.=0D
- @retval EFI_UNSUPPORTED This function is not supported.=0D
-=0D
-**/=0D
-EFI_STATUS=0D
-EFIAPI=0D
-InitializeCpuInterruptHandlers (=0D
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL=0D
- )=0D
-{=0D
- return EFI_UNSUPPORTED;=0D
-}=0D
-=0D
/**=0D
Registers a function to be called from the processor interrupt handler.=
=0D
=0D
@@ -94,8 +69,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHand=
ler is NULL, then the=0D
handler for the processor interrupt or exception type specified by Inter=
ruptType is uninstalled.=0D
The installed handler is called once for each processor interrupt or exc=
eption.=0D
- NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() or=0D
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED retu=
rned.=0D
+ NOTE: This function should be invoked after InitializeCpuExceptionHandle=
rs() is invoked,=0D
+ otherwise EFI_UNSUPPORTED returned.=0D
=0D
@param[in] InterruptType Defines which interrupt or exception to ho=
ok.=0D
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_IN=
TERRUPT_HANDLER that is called=0D
--=20
2.35.1.windows.2


Wang, Jian J
 

Hi Ray,

Both CpuDxe.c and CpuExceptionCommon.h have CPU_INTERRUPT_NUM defined.
I'd suggest to move it to a common place, such as BaseLib.h. I don't see any issue
if they are defined to different value. It just gives me a feeling that it might cause
potential problems sometimes in the future.


Regards,
Jian

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Ni, Ray
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@...>
Subject: [edk2-devel] [PATCH 4/5] CpuException: Remove
InitializeCpuInterruptHandlers

InitializeCpuExceptionHandlers() expects caller allocates IDT while
InitializeCpuInterruptHandlers() allocates 256 IDT entries itself.

InitializeCpuExceptionHandlers() fills max 32 IDT entries allocated
by caller. If caller allocates 10 entries, the API just fills 10 IDT
entries.

The inconsistency between the two APIs makes code hard to
unerstand and hard to share.

Because there is only one caller (CpuDxe) for
InitializeCpuInterruptHandler(), this patch updates CpuDxe driver
to allocates 256 IDT entries then call
InitializeCpuExceptionHandlers().

With this change, InitializeCpuInterruptHandlers() is removed
completely.

And InitializeCpuExceptionHandlers() fills max 32 entries for PEI
and SMM instance, max 256 entries for DXE instance.
Such behavior matches to the original one.

Signed-off-by: Ray Ni <ray.ni@...>
Cc: Eric Dong <eric.dong@...>
---
.../Include/Library/CpuExceptionHandlerLib.h | 28 +------
.../CpuExceptionHandlerLibNull.c | 31 +------
UefiCpuPkg/CpuDxe/CpuDxe.c | 33 ++++++--
.../CpuExceptionHandlerLib/DxeException.c | 80 ++-----------------
.../CpuExceptionHandlerLib/PeiCpuException.c | 61 +-------------
.../PeiDxeSmmCpuException.c | 19 ++---
.../SecPeiCpuException.c | 31 +------
.../CpuExceptionHandlerLib/SmmException.c | 35 ++------
8 files changed, 56 insertions(+), 262 deletions(-)

diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
index 22a4408f9f..d4649bebe1 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -2,7 +2,7 @@
CPU Exception library provides the default CPU interrupt/exception handler.

It also provides capability to register user interrupt/exception handler.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -132,28 +132,6 @@ InitializeCpuExceptionHandlersEx (
IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL

);



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- );

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -161,8 +139,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
index 35ab5a8db5..54f38788fe 100644
---
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
+++
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Handler library implementition with empty functions.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -33,31 +33,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -65,8 +40,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
index 00f3cb0957..a6a91507f6 100644
--- a/UefiCpuPkg/CpuDxe/CpuDxe.c
+++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
@@ -1,7 +1,7 @@
/** @file

CPU DXE Module to produce CPU ARCH Protocol.



- Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -10,6 +10,8 @@
#include "CpuMp.h"

#include "CpuPageTable.h"



+#define CPU_INTERRUPT_NUM 256

+

//

// Global Variables

//

@@ -924,9 +926,12 @@ InitInterruptDescriptorTable (
VOID

)

{

- EFI_STATUS Status;

- EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

- EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ EFI_STATUS Status;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;

+ IA32_DESCRIPTOR IdtDescriptor;

+ UINTN IdtEntryCount;



VectorInfo = NULL;

Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid,
(VOID **)&VectorInfoList);

@@ -934,7 +939,25 @@ InitInterruptDescriptorTable (
VectorInfo = VectorInfoList;

}



- Status = InitializeCpuInterruptHandlers (VectorInfo);

+ AsmReadIdtr (&IdtDescriptor);

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ if (IdtEntryCount < CPU_INTERRUPT_NUM) {

+ //

+ // Increase Interrupt Descriptor Table and Copy the old IDT table in

+ //

+ IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

+ ASSERT (IdtTable != NULL);

+ CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

+

+ //

+ // Load Interrupt Descriptor Table

+ //

+ IdtDescriptor.Base = (UINTN)IdtTable;

+ IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

+ AsmWriteIdtr (&IdtDescriptor);

+ }

+

+ Status = InitializeCpuExceptionHandlers (VectorInfo);

ASSERT_EFI_ERROR (Status);

}



diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index f139131a7c..c7c1fe31d2 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for DXE modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -17,8 +17,8 @@ CONST UINTN mDoFarReturnFlag = 0;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_INTERRUPT_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_INTERRUPT_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -69,76 +69,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- EFI_STATUS Status;

- IA32_IDT_GATE_DESCRIPTOR *IdtTable;

- IA32_DESCRIPTOR IdtDescriptor;

- UINTN IdtEntryCount;

- EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;

-

- SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA)
* CPU_INTERRUPT_NUM, 0xff);

- if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData,
CPU_INTERRUPT_NUM);

- if (EFI_ERROR (Status)) {

- return EFI_INVALID_PARAMETER;

- }

- }

-

- //

- // Read IDT descriptor and calculate IDT size

- //

- AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_INTERRUPT_NUM) {

- IdtEntryCount = CPU_INTERRUPT_NUM;

- }

-

- //

- // Create Interrupt Descriptor Table and Copy the old IDT table in

- //

- IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

- ASSERT (IdtTable != NULL);

- CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

-

- AsmGetTemplateAddressMap (&TemplateMap);

- ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);

-

- mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;

- InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);

-

- UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);

-

- //

- // Load Interrupt Descriptor Table

- //

- IdtDescriptor.Base = (UINTN)IdtTable;

- IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

- AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);

-

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -146,8 +76,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 687fc4177f..1ae611c75e 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for PEIM module.



-Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -133,6 +133,7 @@ InitializeCpuExceptionHandlers (


ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));

ASSERT (ExceptionHandlerData != NULL);

+ ExceptionHandlerData->IdtEntryCount = CPU_EXCEPTION_NUM;

ExceptionHandlerData->ReservedVectors = ReservedVectors;

ExceptionHandlerData->ExternalInterruptHandler = NULL;

InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);

@@ -148,64 +149,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

-/**

- Registers a function to be called from the processor interrupt handler.

-

- This function registers and enables the handler specified by InterruptHandler
for a processor

- interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

- handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

- The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

-

- @param[in] InterruptType Defines which interrupt or exception to hook.

- @param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

- when a processor interrupt occurs. If this parameter is NULL,
then the handler

- will be uninstalled.

-

- @retval EFI_SUCCESS The handler for the processor interrupt was
successfully installed or uninstalled.

- @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
for InterruptType was

- previously installed.

- @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
InterruptType was not

- previously installed.

- @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not
supported,

- or this function is not supported.

-**/

-EFI_STATUS

-EFIAPI

-RegisterCpuInterruptHandler (

- IN EFI_EXCEPTION_TYPE InterruptType,

- IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Initializes all CPU exceptions entries with optional extra initializations.



diff --git
a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
index f47a80dcab..a7d0897ef1 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -261,31 +261,26 @@ InitializeCpuExceptionHandlersWorker (
RESERVED_VECTORS_DATA *ReservedVectors;



ReservedVectors = ExceptionHandlerData->ReservedVectors;

- SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
CPU_EXCEPTION_NUM, 0xff);

+ SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
ExceptionHandlerData->IdtEntryCount, 0xff);

if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
CPU_EXCEPTION_NUM);

+ Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
ExceptionHandlerData->IdtEntryCount);

if (EFI_ERROR (Status)) {

return EFI_INVALID_PARAMETER;

}

}



//

- // Read IDT descriptor and calculate IDT size

+ // Setup the exception handlers according to IDT size, but no more than

+ // ExceptionHandlerData->IdtEntryCount (32 in PEI and SMM, 256 in DXE)
handlers.

//

AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_EXCEPTION_NUM) {

- //

- // CPU exception library only setup CPU_EXCEPTION_NUM exception handler
at most

- //

- IdtEntryCount = CPU_EXCEPTION_NUM;

- }

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ ExceptionHandlerData->IdtEntryCount = MIN (IdtEntryCount,
ExceptionHandlerData->IdtEntryCount);



IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;

AsmGetTemplateAddressMap (&TemplateMap);

ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);



- ExceptionHandlerData->IdtEntryCount = IdtEntryCount;

UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);



return EFI_SUCCESS;

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index 6e5216380d..e894ead612 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for SEC/PEIM modules.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -166,31 +166,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -198,8 +173,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 9f0af4120a..ec643556c7 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for SMM modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -14,8 +14,8 @@ CONST UINTN mDoFarReturnFlag = 1;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_EXCEPTION_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -62,31 +62,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -94,8 +69,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

--
2.35.1.windows.2



-=-=-=-=-=-=
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#89919): https://edk2.groups.io/g/devel/message/89919
Mute This Topic: https://groups.io/mt/91231770/1768734
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@...]
-=-=-=-=-=-=


Ni, Ray
 

Jian,
I think we need discussion on where to put the common CPU_INTERRUPT_NUM definition.

Do you agree that we can leave that to another patch?

________________________________________
From: Wang, Jian J <jian.j.wang@...>
Sent: Monday, May 23, 2022 0:27
To: devel@edk2.groups.io; Ni, Ray
Cc: Dong, Eric
Subject: RE: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers

Hi Ray,

Both CpuDxe.c and CpuExceptionCommon.h have CPU_INTERRUPT_NUM defined.
I'd suggest to move it to a common place, such as BaseLib.h. I don't see any issue
if they are defined to different value. It just gives me a feeling that it might cause
potential problems sometimes in the future.


Regards,
Jian

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Ni, Ray
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@...>
Subject: [edk2-devel] [PATCH 4/5] CpuException: Remove
InitializeCpuInterruptHandlers

InitializeCpuExceptionHandlers() expects caller allocates IDT while
InitializeCpuInterruptHandlers() allocates 256 IDT entries itself.

InitializeCpuExceptionHandlers() fills max 32 IDT entries allocated
by caller. If caller allocates 10 entries, the API just fills 10 IDT
entries.

The inconsistency between the two APIs makes code hard to
unerstand and hard to share.

Because there is only one caller (CpuDxe) for
InitializeCpuInterruptHandler(), this patch updates CpuDxe driver
to allocates 256 IDT entries then call
InitializeCpuExceptionHandlers().

With this change, InitializeCpuInterruptHandlers() is removed
completely.

And InitializeCpuExceptionHandlers() fills max 32 entries for PEI
and SMM instance, max 256 entries for DXE instance.
Such behavior matches to the original one.

Signed-off-by: Ray Ni <ray.ni@...>
Cc: Eric Dong <eric.dong@...>
---
.../Include/Library/CpuExceptionHandlerLib.h | 28 +------
.../CpuExceptionHandlerLibNull.c | 31 +------
UefiCpuPkg/CpuDxe/CpuDxe.c | 33 ++++++--
.../CpuExceptionHandlerLib/DxeException.c | 80 ++-----------------
.../CpuExceptionHandlerLib/PeiCpuException.c | 61 +-------------
.../PeiDxeSmmCpuException.c | 19 ++---
.../SecPeiCpuException.c | 31 +------
.../CpuExceptionHandlerLib/SmmException.c | 35 ++------
8 files changed, 56 insertions(+), 262 deletions(-)

diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
index 22a4408f9f..d4649bebe1 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -2,7 +2,7 @@
CPU Exception library provides the default CPU interrupt/exception handler.

It also provides capability to register user interrupt/exception handler.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -132,28 +132,6 @@ InitializeCpuExceptionHandlersEx (
IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL

);



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- );

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -161,8 +139,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
index 35ab5a8db5..54f38788fe 100644
---
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
+++
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Handler library implementition with empty functions.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -33,31 +33,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -65,8 +40,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
index 00f3cb0957..a6a91507f6 100644
--- a/UefiCpuPkg/CpuDxe/CpuDxe.c
+++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
@@ -1,7 +1,7 @@
/** @file

CPU DXE Module to produce CPU ARCH Protocol.



- Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -10,6 +10,8 @@
#include "CpuMp.h"

#include "CpuPageTable.h"



+#define CPU_INTERRUPT_NUM 256

+

//

// Global Variables

//

@@ -924,9 +926,12 @@ InitInterruptDescriptorTable (
VOID

)

{

- EFI_STATUS Status;

- EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

- EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ EFI_STATUS Status;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;

+ IA32_DESCRIPTOR IdtDescriptor;

+ UINTN IdtEntryCount;



VectorInfo = NULL;

Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid,
(VOID **)&VectorInfoList);

@@ -934,7 +939,25 @@ InitInterruptDescriptorTable (
VectorInfo = VectorInfoList;

}



- Status = InitializeCpuInterruptHandlers (VectorInfo);

+ AsmReadIdtr (&IdtDescriptor);

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ if (IdtEntryCount < CPU_INTERRUPT_NUM) {

+ //

+ // Increase Interrupt Descriptor Table and Copy the old IDT table in

+ //

+ IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

+ ASSERT (IdtTable != NULL);

+ CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

+

+ //

+ // Load Interrupt Descriptor Table

+ //

+ IdtDescriptor.Base = (UINTN)IdtTable;

+ IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

+ AsmWriteIdtr (&IdtDescriptor);

+ }

+

+ Status = InitializeCpuExceptionHandlers (VectorInfo);

ASSERT_EFI_ERROR (Status);

}



diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index f139131a7c..c7c1fe31d2 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for DXE modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -17,8 +17,8 @@ CONST UINTN mDoFarReturnFlag = 0;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_INTERRUPT_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_INTERRUPT_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -69,76 +69,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- EFI_STATUS Status;

- IA32_IDT_GATE_DESCRIPTOR *IdtTable;

- IA32_DESCRIPTOR IdtDescriptor;

- UINTN IdtEntryCount;

- EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;

-

- SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA)
* CPU_INTERRUPT_NUM, 0xff);

- if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData,
CPU_INTERRUPT_NUM);

- if (EFI_ERROR (Status)) {

- return EFI_INVALID_PARAMETER;

- }

- }

-

- //

- // Read IDT descriptor and calculate IDT size

- //

- AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_INTERRUPT_NUM) {

- IdtEntryCount = CPU_INTERRUPT_NUM;

- }

-

- //

- // Create Interrupt Descriptor Table and Copy the old IDT table in

- //

- IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

- ASSERT (IdtTable != NULL);

- CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

-

- AsmGetTemplateAddressMap (&TemplateMap);

- ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);

-

- mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;

- InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);

-

- UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);

-

- //

- // Load Interrupt Descriptor Table

- //

- IdtDescriptor.Base = (UINTN)IdtTable;

- IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

- AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);

-

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -146,8 +76,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 687fc4177f..1ae611c75e 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for PEIM module.



-Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -133,6 +133,7 @@ InitializeCpuExceptionHandlers (


ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));

ASSERT (ExceptionHandlerData != NULL);

+ ExceptionHandlerData->IdtEntryCount = CPU_EXCEPTION_NUM;

ExceptionHandlerData->ReservedVectors = ReservedVectors;

ExceptionHandlerData->ExternalInterruptHandler = NULL;

InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);

@@ -148,64 +149,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

-/**

- Registers a function to be called from the processor interrupt handler.

-

- This function registers and enables the handler specified by InterruptHandler
for a processor

- interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

- handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

- The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

-

- @param[in] InterruptType Defines which interrupt or exception to hook.

- @param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

- when a processor interrupt occurs. If this parameter is NULL,
then the handler

- will be uninstalled.

-

- @retval EFI_SUCCESS The handler for the processor interrupt was
successfully installed or uninstalled.

- @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler
for InterruptType was

- previously installed.

- @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
InterruptType was not

- previously installed.

- @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not
supported,

- or this function is not supported.

-**/

-EFI_STATUS

-EFIAPI

-RegisterCpuInterruptHandler (

- IN EFI_EXCEPTION_TYPE InterruptType,

- IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Initializes all CPU exceptions entries with optional extra initializations.



diff --git
a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
index f47a80dcab..a7d0897ef1 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -261,31 +261,26 @@ InitializeCpuExceptionHandlersWorker (
RESERVED_VECTORS_DATA *ReservedVectors;



ReservedVectors = ExceptionHandlerData->ReservedVectors;

- SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
CPU_EXCEPTION_NUM, 0xff);

+ SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
ExceptionHandlerData->IdtEntryCount, 0xff);

if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
CPU_EXCEPTION_NUM);

+ Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
ExceptionHandlerData->IdtEntryCount);

if (EFI_ERROR (Status)) {

return EFI_INVALID_PARAMETER;

}

}



//

- // Read IDT descriptor and calculate IDT size

+ // Setup the exception handlers according to IDT size, but no more than

+ // ExceptionHandlerData->IdtEntryCount (32 in PEI and SMM, 256 in DXE)
handlers.

//

AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_EXCEPTION_NUM) {

- //

- // CPU exception library only setup CPU_EXCEPTION_NUM exception handler
at most

- //

- IdtEntryCount = CPU_EXCEPTION_NUM;

- }

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ ExceptionHandlerData->IdtEntryCount = MIN (IdtEntryCount,
ExceptionHandlerData->IdtEntryCount);



IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;

AsmGetTemplateAddressMap (&TemplateMap);

ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);



- ExceptionHandlerData->IdtEntryCount = IdtEntryCount;

UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);



return EFI_SUCCESS;

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index 6e5216380d..e894ead612 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for SEC/PEIM modules.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -166,31 +166,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -198,8 +173,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 9f0af4120a..ec643556c7 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for SMM modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -14,8 +14,8 @@ CONST UINTN mDoFarReturnFlag = 1;
RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_EXCEPTION_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -62,31 +62,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -94,8 +69,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

--
2.35.1.windows.2



-=-=-=-=-=-=
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#89919): https://edk2.groups.io/g/devel/message/89919
Mute This Topic: https://groups.io/mt/91231770/1768734
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@...]
-=-=-=-=-=-=


Wang, Jian J
 

Another patch works for me.

Reviewed-by: Jian J Wang <jian.j.wang@...>

Regards,
Jian

-----Original Message-----
From: Ni, Ray <ray.ni@...>
Sent: Tuesday, May 24, 2022 4:04 PM
To: Wang, Jian J <jian.j.wang@...>; devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@...>
Subject: Re: [edk2-devel] [PATCH 4/5] CpuException: Remove
InitializeCpuInterruptHandlers

Jian,
I think we need discussion on where to put the common CPU_INTERRUPT_NUM
definition.

Do you agree that we can leave that to another patch?

________________________________________
From: Wang, Jian J <jian.j.wang@...>
Sent: Monday, May 23, 2022 0:27
To: devel@edk2.groups.io; Ni, Ray
Cc: Dong, Eric
Subject: RE: [edk2-devel] [PATCH 4/5] CpuException: Remove
InitializeCpuInterruptHandlers

Hi Ray,

Both CpuDxe.c and CpuExceptionCommon.h have CPU_INTERRUPT_NUM
defined.
I'd suggest to move it to a common place, such as BaseLib.h. I don't see any
issue
if they are defined to different value. It just gives me a feeling that it might
cause
potential problems sometimes in the future.


Regards,
Jian

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Ni, Ray
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@...>
Subject: [edk2-devel] [PATCH 4/5] CpuException: Remove
InitializeCpuInterruptHandlers

InitializeCpuExceptionHandlers() expects caller allocates IDT while
InitializeCpuInterruptHandlers() allocates 256 IDT entries itself.

InitializeCpuExceptionHandlers() fills max 32 IDT entries allocated
by caller. If caller allocates 10 entries, the API just fills 10 IDT
entries.

The inconsistency between the two APIs makes code hard to
unerstand and hard to share.

Because there is only one caller (CpuDxe) for
InitializeCpuInterruptHandler(), this patch updates CpuDxe driver
to allocates 256 IDT entries then call
InitializeCpuExceptionHandlers().

With this change, InitializeCpuInterruptHandlers() is removed
completely.

And InitializeCpuExceptionHandlers() fills max 32 entries for PEI
and SMM instance, max 256 entries for DXE instance.
Such behavior matches to the original one.

Signed-off-by: Ray Ni <ray.ni@...>
Cc: Eric Dong <eric.dong@...>
---
.../Include/Library/CpuExceptionHandlerLib.h | 28 +------
.../CpuExceptionHandlerLibNull.c | 31 +------
UefiCpuPkg/CpuDxe/CpuDxe.c | 33 ++++++--
.../CpuExceptionHandlerLib/DxeException.c | 80 ++-----------------
.../CpuExceptionHandlerLib/PeiCpuException.c | 61 +-------------
.../PeiDxeSmmCpuException.c | 19 ++---
.../SecPeiCpuException.c | 31 +------
.../CpuExceptionHandlerLib/SmmException.c | 35 ++------
8 files changed, 56 insertions(+), 262 deletions(-)

diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
index 22a4408f9f..d4649bebe1 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -2,7 +2,7 @@
CPU Exception library provides the default CPU interrupt/exception handler.

It also provides capability to register user interrupt/exception handler.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -132,28 +132,6 @@ InitializeCpuExceptionHandlersEx (
IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL

);



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- );

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -161,8 +139,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
index 35ab5a8db5..54f38788fe 100644
---
a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
+++
b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
Null.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Handler library implementition with empty functions.



- Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -33,31 +33,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -65,8 +40,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
index 00f3cb0957..a6a91507f6 100644
--- a/UefiCpuPkg/CpuDxe/CpuDxe.c
+++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
@@ -1,7 +1,7 @@
/** @file

CPU DXE Module to produce CPU ARCH Protocol.



- Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -10,6 +10,8 @@
#include "CpuMp.h"

#include "CpuPageTable.h"



+#define CPU_INTERRUPT_NUM 256

+

//

// Global Variables

//

@@ -924,9 +926,12 @@ InitInterruptDescriptorTable (
VOID

)

{

- EFI_STATUS Status;

- EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

- EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ EFI_STATUS Status;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfoList;

+ EFI_VECTOR_HANDOFF_INFO *VectorInfo;

+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;

+ IA32_DESCRIPTOR IdtDescriptor;

+ UINTN IdtEntryCount;



VectorInfo = NULL;

Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid,
(VOID **)&VectorInfoList);

@@ -934,7 +939,25 @@ InitInterruptDescriptorTable (
VectorInfo = VectorInfoList;

}



- Status = InitializeCpuInterruptHandlers (VectorInfo);

+ AsmReadIdtr (&IdtDescriptor);

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ if (IdtEntryCount < CPU_INTERRUPT_NUM) {

+ //

+ // Increase Interrupt Descriptor Table and Copy the old IDT table in

+ //

+ IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

+ ASSERT (IdtTable != NULL);

+ CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

+

+ //

+ // Load Interrupt Descriptor Table

+ //

+ IdtDescriptor.Base = (UINTN)IdtTable;

+ IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

+ AsmWriteIdtr (&IdtDescriptor);

+ }

+

+ Status = InitializeCpuExceptionHandlers (VectorInfo);

ASSERT_EFI_ERROR (Status);

}



diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index f139131a7c..c7c1fe31d2 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for DXE modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -17,8 +17,8 @@ CONST UINTN mDoFarReturnFlag = 0;
RESERVED_VECTORS_DATA
mReservedVectorsData[CPU_INTERRUPT_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_INTERRUPT_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -69,76 +69,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- EFI_STATUS Status;

- IA32_IDT_GATE_DESCRIPTOR *IdtTable;

- IA32_DESCRIPTOR IdtDescriptor;

- UINTN IdtEntryCount;

- EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;

-

- SetMem ((VOID *)mReservedVectorsData, sizeof
(RESERVED_VECTORS_DATA)
* CPU_INTERRUPT_NUM, 0xff);

- if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData,
CPU_INTERRUPT_NUM);

- if (EFI_ERROR (Status)) {

- return EFI_INVALID_PARAMETER;

- }

- }

-

- //

- // Read IDT descriptor and calculate IDT size

- //

- AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_INTERRUPT_NUM) {

- IdtEntryCount = CPU_INTERRUPT_NUM;

- }

-

- //

- // Create Interrupt Descriptor Table and Copy the old IDT table in

- //

- IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM);

- ASSERT (IdtTable != NULL);

- CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
(IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);

-

- AsmGetTemplateAddressMap (&TemplateMap);

- ASSERT (TemplateMap.ExceptionStubHeaderSize <=
HOOKAFTER_STUB_SIZE);

-

- mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;

- InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);

-

- UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);

-

- //

- // Load Interrupt Descriptor Table

- //

- IdtDescriptor.Base = (UINTN)IdtTable;

- IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
CPU_INTERRUPT_NUM - 1);

- AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);

-

- return EFI_SUCCESS;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -146,8 +76,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 687fc4177f..1ae611c75e 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for PEIM module.



-Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -133,6 +133,7 @@ InitializeCpuExceptionHandlers (


ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));

ASSERT (ExceptionHandlerData != NULL);

+ ExceptionHandlerData->IdtEntryCount = CPU_EXCEPTION_NUM;

ExceptionHandlerData->ReservedVectors = ReservedVectors;

ExceptionHandlerData->ExternalInterruptHandler = NULL;

InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);

@@ -148,64 +149,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

-/**

- Registers a function to be called from the processor interrupt handler.

-

- This function registers and enables the handler specified by InterruptHandler
for a processor

- interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

- handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

- The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

-

- @param[in] InterruptType Defines which interrupt or exception to hook.

- @param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

- when a processor interrupt occurs. If this parameter is NULL,
then the handler

- will be uninstalled.

-

- @retval EFI_SUCCESS The handler for the processor interrupt was
successfully installed or uninstalled.

- @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a
handler
for InterruptType was

- previously installed.

- @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler
for
InterruptType was not

- previously installed.

- @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is
not
supported,

- or this function is not supported.

-**/

-EFI_STATUS

-EFIAPI

-RegisterCpuInterruptHandler (

- IN EFI_EXCEPTION_TYPE InterruptType,

- IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Initializes all CPU exceptions entries with optional extra initializations.



diff --git
a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
index f47a80dcab..a7d0897ef1 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
+++
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU Exception Library provides PEI/DXE/SMM CPU common exception
handler.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -261,31 +261,26 @@ InitializeCpuExceptionHandlersWorker (
RESERVED_VECTORS_DATA *ReservedVectors;



ReservedVectors = ExceptionHandlerData->ReservedVectors;

- SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
CPU_EXCEPTION_NUM, 0xff);

+ SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
ExceptionHandlerData->IdtEntryCount, 0xff);

if (VectorInfo != NULL) {

- Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
CPU_EXCEPTION_NUM);

+ Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
ExceptionHandlerData->IdtEntryCount);

if (EFI_ERROR (Status)) {

return EFI_INVALID_PARAMETER;

}

}



//

- // Read IDT descriptor and calculate IDT size

+ // Setup the exception handlers according to IDT size, but no more than

+ // ExceptionHandlerData->IdtEntryCount (32 in PEI and SMM, 256 in DXE)
handlers.

//

AsmReadIdtr (&IdtDescriptor);

- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

- if (IdtEntryCount > CPU_EXCEPTION_NUM) {

- //

- // CPU exception library only setup CPU_EXCEPTION_NUM exception
handler
at most

- //

- IdtEntryCount = CPU_EXCEPTION_NUM;

- }

+ IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
(IA32_IDT_GATE_DESCRIPTOR);

+ ExceptionHandlerData->IdtEntryCount = MIN (IdtEntryCount,
ExceptionHandlerData->IdtEntryCount);



IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;

AsmGetTemplateAddressMap (&TemplateMap);

ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);



- ExceptionHandlerData->IdtEntryCount = IdtEntryCount;

UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);



return EFI_SUCCESS;

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index 6e5216380d..e894ead612 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implemenation for SEC/PEIM modules.



-Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -166,31 +166,6 @@ InitializeCpuExceptionHandlers (
return EFI_SUCCESS;

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -198,8 +173,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 9f0af4120a..ec643556c7 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -1,7 +1,7 @@
/** @file

CPU exception handler library implementation for SMM modules.



- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent



**/

@@ -14,8 +14,8 @@ CONST UINTN mDoFarReturnFlag = 1;
RESERVED_VECTORS_DATA
mReservedVectorsData[CPU_EXCEPTION_NUM];

EFI_CPU_INTERRUPT_HANDLER
mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];

EXCEPTION_HANDLER_DATA mExceptionHandlerData = {

- 0, // To be fixed

- 0, // To be fixed

+ CPU_EXCEPTION_NUM,

+ 0, // To be fixed

mReservedVectorsData,

mExternalInterruptHandlerTable

};

@@ -62,31 +62,6 @@ InitializeCpuExceptionHandlers (
return InitializeCpuExceptionHandlersWorker (VectorInfo,
&mExceptionHandlerData);

}



-/**

- Initializes all CPU interrupt/exceptions entries and provides the default
interrupt/exception handlers.

-

- Caller should try to get an array of interrupt and/or exception vectors that
are
in use and need to

- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.

- If caller cannot get reserved vector list or it does not exists, set VectorInfo
to
NULL.

- If VectorInfo is not NULL, the exception vectors will be initialized per vector
attribute accordingly.

-

- @param[in] VectorInfo Pointer to reserved vector list.

-

- @retval EFI_SUCCESS All CPU interrupt/exception entries have been
successfully initialized

- with default interrupt/exception handlers.

- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
VectorInfo is not NULL.

- @retval EFI_UNSUPPORTED This function is not supported.

-

-**/

-EFI_STATUS

-EFIAPI

-InitializeCpuInterruptHandlers (

- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL

- )

-{

- return EFI_UNSUPPORTED;

-}

-

/**

Registers a function to be called from the processor interrupt handler.



@@ -94,8 +69,8 @@ InitializeCpuInterruptHandlers (
interrupt or exception type specified by InterruptType. If InterruptHandler is
NULL, then the

handler for the processor interrupt or exception type specified by
InterruptType is uninstalled.

The installed handler is called once for each processor interrupt or exception.

- NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
or

- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
returned.

+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
is invoked,

+ otherwise EFI_UNSUPPORTED returned.



@param[in] InterruptType Defines which interrupt or exception to hook.

@param[in] InterruptHandler A pointer to a function of type
EFI_CPU_INTERRUPT_HANDLER that is called

--
2.35.1.windows.2



-=-=-=-=-=-=
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#89919): https://edk2.groups.io/g/devel/message/89919
Mute This Topic: https://groups.io/mt/91231770/1768734
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@...]
-=-=-=-=-=-=


Dong, Eric
 

Acked-by: Eric Dong eric.dong@...

 

From: Ni, Ray <ray.ni@...>
Sent: Tuesday, May 24, 2022 4:04 PM
To: Wang, Jian J <jian.j.wang@...>; devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@...>
Subject: Re: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers

 

Jian,
I think we need discussion on where to put the common CPU_INTERRUPT_NUM definition.

Do you agree that we can leave that to another patch?

________________________________________
From: Wang, Jian J <jian.j.wang@...>
Sent: Monday, May 23, 2022 0:27
To: devel@edk2.groups.io; Ni, Ray
Cc: Dong, Eric
Subject: RE: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers

Hi Ray,

Both CpuDxe.c and CpuExceptionCommon.h have CPU_INTERRUPT_NUM defined.
I'd suggest to move it to a common place, such as BaseLib.h. I don't see any issue
if they are defined to different value. It just gives me a feeling that it might cause
potential problems sometimes in the future.


Regards,
Jian

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Ni, Ray
> Sent: Friday, May 20, 2022 10:16 PM
> To: devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@...>
> Subject: [edk2-devel] [PATCH 4/5] CpuException: Remove
> InitializeCpuInterruptHandlers
>
> InitializeCpuExceptionHandlers() expects caller allocates IDT while
> InitializeCpuInterruptHandlers() allocates 256 IDT entries itself.
>
> InitializeCpuExceptionHandlers() fills max 32 IDT entries allocated
> by caller. If caller allocates 10 entries, the API just fills 10 IDT
> entries.
>
> The inconsistency between the two APIs makes code hard to
> unerstand and hard to share.
>
> Because there is only one caller (CpuDxe) for
> InitializeCpuInterruptHandler(), this patch updates CpuDxe driver
> to allocates 256 IDT entries then call
> InitializeCpuExceptionHandlers().
>
> With this change, InitializeCpuInterruptHandlers() is removed
> completely.
>
> And InitializeCpuExceptionHandlers() fills max 32 entries for PEI
> and SMM instance, max 256 entries for DXE instance.
> Such behavior matches to the original one.
>
> Signed-off-by: Ray Ni <ray.ni@...>
> Cc: Eric Dong <eric.dong@...>
> ---
>  .../Include/Library/CpuExceptionHandlerLib.h  | 28 +------
>  .../CpuExceptionHandlerLibNull.c              | 31 +------
>  UefiCpuPkg/CpuDxe/CpuDxe.c                    | 33 ++++++--
>  .../CpuExceptionHandlerLib/DxeException.c     | 80 ++-----------------
>  .../CpuExceptionHandlerLib/PeiCpuException.c  | 61 +-------------
>  .../PeiDxeSmmCpuException.c                   | 19 ++---
>  .../SecPeiCpuException.c                      | 31 +------
>  .../CpuExceptionHandlerLib/SmmException.c     | 35 ++------
>  8 files changed, 56 insertions(+), 262 deletions(-)
>
> diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> index 22a4408f9f..d4649bebe1 100644
> --- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> +++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> @@ -2,7 +2,7 @@
>    CPU Exception library provides the default CPU interrupt/exception handler.
>
>    It also provides capability to register user interrupt/exception handler.
>
>
>
> -  Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +  Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -132,28 +132,6 @@ InitializeCpuExceptionHandlersEx (
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
>
>    );
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  );
>
> -
>
>  /**
>
>    Registers a function to be called from the processor interrupt handler.
>
>
>
> @@ -161,8 +139,8 @@ InitializeCpuInterruptHandlers (
>    interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
>    handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
>    The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> +  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> is invoked,
>
> +  otherwise EFI_UNSUPPORTED returned.
>
>
>
>    @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
>    @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> diff --git
> a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> index 35ab5a8db5..54f38788fe 100644
> ---
> a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> +++
> b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU Exception Handler library implementition with empty functions.
>
>
>
> -  Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +  Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -33,31 +33,6 @@ InitializeCpuExceptionHandlers (
>    return EFI_SUCCESS;
>
>  }
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  )
>
> -{
>
> -  return EFI_SUCCESS;
>
> -}
>
> -
>
>  /**
>
>    Registers a function to be called from the processor interrupt handler.
>
>
>
> @@ -65,8 +40,8 @@ InitializeCpuInterruptHandlers (
>    interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
>    handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
>    The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> +  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> is invoked,
>
> +  otherwise EFI_UNSUPPORTED returned.
>
>
>
>    @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
>    @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
> index 00f3cb0957..a6a91507f6 100644
> --- a/UefiCpuPkg/CpuDxe/CpuDxe.c
> +++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU DXE Module to produce CPU ARCH Protocol.
>
>
>
> -  Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +  Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -10,6 +10,8 @@
>  #include "CpuMp.h"
>
>  #include "CpuPageTable.h"
>
>
>
> +#define CPU_INTERRUPT_NUM  256
>
> +
>
>  //
>
>  // Global Variables
>
>  //
>
> @@ -924,9 +926,12 @@ InitInterruptDescriptorTable (
>    VOID
>
>    )
>
>  {
>
> -  EFI_STATUS               Status;
>
> -  EFI_VECTOR_HANDOFF_INFO  *VectorInfoList;
>
> -  EFI_VECTOR_HANDOFF_INFO  *VectorInfo;
>
> +  EFI_STATUS                Status;
>
> +  EFI_VECTOR_HANDOFF_INFO   *VectorInfoList;
>
> +  EFI_VECTOR_HANDOFF_INFO   *VectorInfo;
>
> +  IA32_IDT_GATE_DESCRIPTOR  *IdtTable;
>
> +  IA32_DESCRIPTOR           IdtDescriptor;
>
> +  UINTN                     IdtEntryCount;
>
>
>
>    VectorInfo = NULL;
>
>    Status     = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid,
> (VOID **)&VectorInfoList);
>
> @@ -934,7 +939,25 @@ InitInterruptDescriptorTable (
>      VectorInfo = VectorInfoList;
>
>    }
>
>
>
> -  Status = InitializeCpuInterruptHandlers (VectorInfo);
>
> +  AsmReadIdtr (&IdtDescriptor);
>
> +  IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
> (IA32_IDT_GATE_DESCRIPTOR);
>
> +  if (IdtEntryCount < CPU_INTERRUPT_NUM) {
>
> +    //
>
> +    // Increase Interrupt Descriptor Table and Copy the old IDT table in
>
> +    //
>
> +    IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
> CPU_INTERRUPT_NUM);
>
> +    ASSERT (IdtTable != NULL);
>
> +    CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
> (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);
>
> +
>
> +    //
>
> +    // Load Interrupt Descriptor Table
>
> +    //
>
> +    IdtDescriptor.Base  = (UINTN)IdtTable;
>
> +    IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
> CPU_INTERRUPT_NUM - 1);
>
> +    AsmWriteIdtr (&IdtDescriptor);
>
> +  }
>
> +
>
> +  Status = InitializeCpuExceptionHandlers (VectorInfo);
>
>    ASSERT_EFI_ERROR (Status);
>
>  }
>
>
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index f139131a7c..c7c1fe31d2 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU exception handler library implemenation for DXE modules.
>
>
>
> -  Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
>
> +  Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -17,8 +17,8 @@ CONST UINTN  mDoFarReturnFlag = 0;
>  RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_INTERRUPT_NUM];
>
>  EFI_CPU_INTERRUPT_HANDLER
> mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];
>
>  EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
>
> -  0,   // To be fixed
>
> -  0,   // To be fixed
>
> +  CPU_INTERRUPT_NUM,
>
> +  0,                     // To be fixed
>
>    mReservedVectorsData,
>
>    mExternalInterruptHandlerTable
>
>  };
>
> @@ -69,76 +69,6 @@ InitializeCpuExceptionHandlers (
>    return InitializeCpuExceptionHandlersWorker (VectorInfo,
> &mExceptionHandlerData);
>
>  }
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  )
>
> -{
>
> -  EFI_STATUS                      Status;
>
> -  IA32_IDT_GATE_DESCRIPTOR        *IdtTable;
>
> -  IA32_DESCRIPTOR                 IdtDescriptor;
>
> -  UINTN                           IdtEntryCount;
>
> -  EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
>
> -
>
> -  SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA)
> * CPU_INTERRUPT_NUM, 0xff);
>
> -  if (VectorInfo != NULL) {
>
> -    Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData,
> CPU_INTERRUPT_NUM);
>
> -    if (EFI_ERROR (Status)) {
>
> -      return EFI_INVALID_PARAMETER;
>
> -    }
>
> -  }
>
> -
>
> -  //
>
> -  // Read IDT descriptor and calculate IDT size
>
> -  //
>
> -  AsmReadIdtr (&IdtDescriptor);
>
> -  IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
> (IA32_IDT_GATE_DESCRIPTOR);
>
> -  if (IdtEntryCount > CPU_INTERRUPT_NUM) {
>
> -    IdtEntryCount = CPU_INTERRUPT_NUM;
>
> -  }
>
> -
>
> -  //
>
> -  // Create Interrupt Descriptor Table and Copy the old IDT table in
>
> -  //
>
> -  IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) *
> CPU_INTERRUPT_NUM);
>
> -  ASSERT (IdtTable != NULL);
>
> -  CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof
> (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);
>
> -
>
> -  AsmGetTemplateAddressMap (&TemplateMap);
>
> -  ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
>
> -
>
> -  mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
>
> -  InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
>
> -
>
> -  UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
>
> -
>
> -  //
>
> -  // Load Interrupt Descriptor Table
>
> -  //
>
> -  IdtDescriptor.Base  = (UINTN)IdtTable;
>
> -  IdtDescriptor.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) *
> CPU_INTERRUPT_NUM - 1);
>
> -  AsmWriteIdtr ((IA32_DESCRIPTOR *)&IdtDescriptor);
>
> -
>
> -  return EFI_SUCCESS;
>
> -}
>
> -
>
>  /**
>
>    Registers a function to be called from the processor interrupt handler.
>
>
>
> @@ -146,8 +76,8 @@ InitializeCpuInterruptHandlers (
>    interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
>    handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
>    The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> +  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> is invoked,
>
> +  otherwise EFI_UNSUPPORTED returned.
>
>
>
>    @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
>    @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> index 687fc4177f..1ae611c75e 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU exception handler library implementation for PEIM module.
>
>
>
> -Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -133,6 +133,7 @@ InitializeCpuExceptionHandlers (
>
>
>    ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));
>
>    ASSERT (ExceptionHandlerData != NULL);
>
> +  ExceptionHandlerData->IdtEntryCount            = CPU_EXCEPTION_NUM;
>
>    ExceptionHandlerData->ReservedVectors          = ReservedVectors;
>
>    ExceptionHandlerData->ExternalInterruptHandler = NULL;
>
>    InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
>
> @@ -148,64 +149,6 @@ InitializeCpuExceptionHandlers (
>    return EFI_SUCCESS;
>
>  }
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  )
>
> -{
>
> -  return EFI_UNSUPPORTED;
>
> -}
>
> -
>
> -/**
>
> -  Registers a function to be called from the processor interrupt handler.
>
> -
>
> -  This function registers and enables the handler specified by InterruptHandler
> for a processor
>
> -  interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
> -  handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
> -  The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> -
>
> -  @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
> -  @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> -                                when a processor interrupt occurs. If this parameter is NULL,
> then the handler
>
> -                                will be uninstalled.
>
> -
>
> -  @retval EFI_SUCCESS           The handler for the processor interrupt was
> successfully installed or uninstalled.
>
> -  @retval EFI_ALREADY_STARTED   InterruptHandler is not NULL, and a handler
> for InterruptType was
>
> -                                previously installed.
>
> -  @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for
> InterruptType was not
>
> -                                previously installed.
>
> -  @retval EFI_UNSUPPORTED       The interrupt specified by InterruptType is not
> supported,
>
> -                                or this function is not supported.
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -RegisterCpuInterruptHandler (
>
> -  IN EFI_EXCEPTION_TYPE         InterruptType,
>
> -  IN EFI_CPU_INTERRUPT_HANDLER  InterruptHandler
>
> -  )
>
> -{
>
> -  return EFI_UNSUPPORTED;
>
> -}
>
> -
>
>  /**
>
>    Initializes all CPU exceptions entries with optional extra initializations.
>
>
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
> index f47a80dcab..a7d0897ef1 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.
>
>
>
> -Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -261,31 +261,26 @@ InitializeCpuExceptionHandlersWorker (
>    RESERVED_VECTORS_DATA           *ReservedVectors;
>
>
>
>    ReservedVectors = ExceptionHandlerData->ReservedVectors;
>
> -  SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
> CPU_EXCEPTION_NUM, 0xff);
>
> +  SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
> ExceptionHandlerData->IdtEntryCount, 0xff);
>
>    if (VectorInfo != NULL) {
>
> -    Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
> CPU_EXCEPTION_NUM);
>
> +    Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
> ExceptionHandlerData->IdtEntryCount);
>
>      if (EFI_ERROR (Status)) {
>
>        return EFI_INVALID_PARAMETER;
>
>      }
>
>    }
>
>
>
>    //
>
> -  // Read IDT descriptor and calculate IDT size
>
> +  // Setup the exception handlers according to IDT size, but no more than
>
> +  //   ExceptionHandlerData->IdtEntryCount (32 in PEI and SMM, 256 in DXE)
> handlers.
>
>    //
>
>    AsmReadIdtr (&IdtDescriptor);
>
> -  IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof
> (IA32_IDT_GATE_DESCRIPTOR);
>
> -  if (IdtEntryCount > CPU_EXCEPTION_NUM) {
>
> -    //
>
> -    // CPU exception library only setup CPU_EXCEPTION_NUM exception handler
> at most
>
> -    //
>
> -    IdtEntryCount = CPU_EXCEPTION_NUM;
>
> -  }
>
> +  IdtEntryCount                       = (IdtDescriptor.Limit + 1) / sizeof
> (IA32_IDT_GATE_DESCRIPTOR);
>
> +  ExceptionHandlerData->IdtEntryCount = MIN (IdtEntryCount,
> ExceptionHandlerData->IdtEntryCount);
>
>
>
>    IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
>
>    AsmGetTemplateAddressMap (&TemplateMap);
>
>    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
>
>
>
> -  ExceptionHandlerData->IdtEntryCount = IdtEntryCount;
>
>    UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);
>
>
>
>    return EFI_SUCCESS;
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> index 6e5216380d..e894ead612 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU exception handler library implemenation for SEC/PEIM modules.
>
>
>
> -Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
>
> +Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -166,31 +166,6 @@ InitializeCpuExceptionHandlers (
>    return EFI_SUCCESS;
>
>  }
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  )
>
> -{
>
> -  return EFI_UNSUPPORTED;
>
> -}
>
> -
>
>  /**
>
>    Registers a function to be called from the processor interrupt handler.
>
>
>
> @@ -198,8 +173,8 @@ InitializeCpuInterruptHandlers (
>    interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
>    handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
>    The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> +  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> is invoked,
>
> +  otherwise EFI_UNSUPPORTED returned.
>
>
>
>    @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
>    @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> index 9f0af4120a..ec643556c7 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> @@ -1,7 +1,7 @@
>  /** @file
>
>    CPU exception handler library implementation for SMM modules.
>
>
>
> -  Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
>
> +  Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>
>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
>  **/
>
> @@ -14,8 +14,8 @@ CONST UINTN  mDoFarReturnFlag = 1;
>  RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
>
>  EFI_CPU_INTERRUPT_HANDLER
> mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
>
>  EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
>
> -  0,   // To be fixed
>
> -  0,   // To be fixed
>
> +  CPU_EXCEPTION_NUM,
>
> +  0,                     // To be fixed
>
>    mReservedVectorsData,
>
>    mExternalInterruptHandlerTable
>
>  };
>
> @@ -62,31 +62,6 @@ InitializeCpuExceptionHandlers (
>    return InitializeCpuExceptionHandlersWorker (VectorInfo,
> &mExceptionHandlerData);
>
>  }
>
>
>
> -/**
>
> -  Initializes all CPU interrupt/exceptions entries and provides the default
> interrupt/exception handlers.
>
> -
>
> -  Caller should try to get an array of interrupt and/or exception vectors that are
> in use and need to
>
> -  persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
>
> -  If caller cannot get reserved vector list or it does not exists, set VectorInfo to
> NULL.
>
> -  If VectorInfo is not NULL, the exception vectors will be initialized per vector
> attribute accordingly.
>
> -
>
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
>
> -
>
> -  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been
> successfully initialized
>
> -                                with default interrupt/exception handlers.
>
> -  @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if
> VectorInfo is not NULL.
>
> -  @retval EFI_UNSUPPORTED       This function is not supported.
>
> -
>
> -**/
>
> -EFI_STATUS
>
> -EFIAPI
>
> -InitializeCpuInterruptHandlers (
>
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
>
> -  )
>
> -{
>
> -  return EFI_UNSUPPORTED;
>
> -}
>
> -
>
>  /**
>
>    Registers a function to be called from the processor interrupt handler.
>
>
>
> @@ -94,8 +69,8 @@ InitializeCpuInterruptHandlers (
>    interrupt or exception type specified by InterruptType. If InterruptHandler is
> NULL, then the
>
>    handler for the processor interrupt or exception type specified by
> InterruptType is uninstalled.
>
>    The installed handler is called once for each processor interrupt or exception.
>
> -  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> or
>
> -  InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED
> returned.
>
> +  NOTE: This function should be invoked after InitializeCpuExceptionHandlers()
> is invoked,
>
> +  otherwise EFI_UNSUPPORTED returned.
>
>
>
>    @param[in]  InterruptType     Defines which interrupt or exception to hook.
>
>    @param[in]  InterruptHandler  A pointer to a function of type
> EFI_CPU_INTERRUPT_HANDLER that is called
>
> --
> 2.35.1.windows.2
>
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89919): https://edk2.groups.io/g/devel/message/89919
> Mute This Topic: https://groups.io/mt/91231770/1768734
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@...]
> -=-=-=-=-=-=
>