public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib
@ 2022-05-20 14:15 Ni, Ray
  2022-05-20 14:15 ` [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance Ni, Ray
                   ` (4 more replies)
  0 siblings, 5 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel


Ray Ni (5):
  CpuException: Avoid allocating code pages for DXE instance
  CpuException: Init global variables in-place
  CpuException: Avoid allocating page but using global variables
  CpuException: Remove InitializeCpuInterruptHandlers
  CpuException: Add InitializeSeparateExceptionStacks

 MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c       |   2 +-
 .../Include/Library/CpuExceptionHandlerLib.h  |  52 +----
 .../CpuExceptionHandlerLibNull.c              |  57 +----
 UefiCpuPkg/CpuDxe/CpuDxe.c                    |  33 ++-
 UefiCpuPkg/CpuDxe/CpuMp.c                     |   6 +-
 UefiCpuPkg/CpuMpPei/CpuMpPei.c                |   4 +-
 .../CpuExceptionHandlerLib/DxeException.c     | 218 ++++--------------
 .../Ia32/ExceptionHandlerAsm.nasm             |   4 +-
 .../CpuExceptionHandlerLib/PeiCpuException.c  | 112 +--------
 .../PeiDxeSmmCpuException.c                   |  19 +-
 .../SecPeiCpuException.c                      |  58 +----
 .../CpuExceptionHandlerLib/SmmException.c     |  72 ++----
 .../X64/ExceptionHandlerAsm.nasm              |   2 +
 .../X64/Xcode5ExceptionHandlerAsm.nasm        |   9 +-
 14 files changed, 149 insertions(+), 499 deletions(-)

-- 
2.35.1.windows.2


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance
  2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
@ 2022-05-20 14:15 ` Ni, Ray
  2022-05-22 16:40   ` [edk2-devel] " Wang, Jian J
  2022-05-20 14:15 ` [PATCH 2/5] CpuException: Init global variables in-place Ni, Ray
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong

Today the DXE instance allocates code page and then copies the IDT
vectors to the allocated code page. Then it fixes up the vector number
in the IDT vector.

But if we update the NASM file to generate 256 IDT vectors, there is
no need to do the copy and fix-up.

A side effect is up to 4096 bytes (HOOKAFTER_STUB_SIZE * 256) is
used for 256 IDT vectors. While 32 IDT vectors only require 512 bytes.

But considering the code logic simplification, 3.5K space is not a big
deal. SEC instance still generates 32 IDT vectors so no impact to SEC.
If 3.5K is too much a waste in PEI phase, we can enhance the code
further to generate 32 vectors for PEI.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../CpuExceptionHandlerLib/DxeException.c     | 22 -------------------
 .../Ia32/ExceptionHandlerAsm.nasm             |  4 ++--
 .../X64/ExceptionHandlerAsm.nasm              |  2 ++
 .../X64/Xcode5ExceptionHandlerAsm.nasm        |  9 ++++----
 4 files changed, 9 insertions(+), 28 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index 61f11e98f8..5083c4b8e8 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -95,9 +95,6 @@ InitializeCpuInterruptHandlers (
   IA32_DESCRIPTOR                 IdtDescriptor;
   UINTN                           IdtEntryCount;
   EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
-  UINTN                           Index;
-  UINTN                           InterruptEntry;
-  UINT8                           *InterruptEntryCode;
   RESERVED_VECTORS_DATA           *ReservedVectors;
   EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
 
@@ -138,25 +135,6 @@ InitializeCpuInterruptHandlers (
   AsmGetTemplateAddressMap (&TemplateMap);
   ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
 
-  Status = gBS->AllocatePool (
-                  EfiBootServicesCode,
-                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
-                  (VOID **)&InterruptEntryCode
-                  );
-  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
-
-  InterruptEntry = (UINTN)InterruptEntryCode;
-  for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
-    CopyMem (
-      (VOID *)InterruptEntry,
-      (VOID *)TemplateMap.ExceptionStart,
-      TemplateMap.ExceptionStubHeaderSize
-      );
-    AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID *)TemplateMap.ExceptionStart);
-    InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
-  }
-
-  TemplateMap.ExceptionStart                     = (UINTN)InterruptEntryCode;
   mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
   mExceptionHandlerData.ReservedVectors          = ReservedVectors;
   mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nasm
index 3fe9aed1e8..8ed2b8f455 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nasm
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nasm
@@ -33,7 +33,7 @@ ALIGN   8
 ;
 AsmIdtVectorBegin:
 %assign Vector 0
-%rep  32
+%rep  256
     push    byte %[Vector];
     push    eax
     mov     eax, ASM_PFX(CommonInterruptEntry)
@@ -439,7 +439,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
 
     mov ebx, dword [ebp + 0x8]
     mov dword [ebx],      AsmIdtVectorBegin
-    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
+    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
     mov dword [ebx + 0x8], HookAfterStubBegin
 
     popad
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
index 9a806d1f86..aaf8d622e6 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
@@ -31,6 +31,8 @@ SECTION .text
 
 ALIGN   8
 
+; Generate 32 IDT vectors.
+; 32 IDT vectors are enough because interrupts (32+) are not enabled in SEC and PEI phase.
 AsmIdtVectorBegin:
 %assign Vector 0
 %rep  32
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAsm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAsm.nasm
index 9c72fa5815..7c0e3d3b0b 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAsm.nasm
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAsm.nasm
@@ -53,9 +53,10 @@ SECTION .text
 
 ALIGN   8
 
+; Generate 256 IDT vectors.
 AsmIdtVectorBegin:
 %assign Vector 0
-%rep  32
+%rep  256
     push    byte %[Vector]
     push    rax
     mov     rax, strict qword 0 ;    mov     rax, ASM_PFX(CommonInterruptEntry)
@@ -453,16 +454,16 @@ global ASM_PFX(AsmGetTemplateAddressMap)
 ASM_PFX(AsmGetTemplateAddressMap):
     lea     rax, [AsmIdtVectorBegin]
     mov     qword [rcx], rax
-    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
+    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
     lea     rax, [HookAfterStubHeaderBegin]
     mov     qword [rcx + 0x10], rax
 
 ; Fix up CommonInterruptEntry address
     lea    rax, [ASM_PFX(CommonInterruptEntry)]
     lea    rcx, [AsmIdtVectorBegin]
-%rep  32
+%rep  256
     mov    qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)], rax
-    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
+    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
 %endrep
 ; Fix up HookAfterStubHeaderEnd
     lea    rax, [HookAfterStubHeaderEnd]
-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/5] CpuException: Init global variables in-place
  2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
  2022-05-20 14:15 ` [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance Ni, Ray
@ 2022-05-20 14:15 ` Ni, Ray
  2022-05-22 16:32   ` [edk2-devel] " Wang, Jian J
  2022-06-06  7:46   ` Dong, Eric
  2022-05-20 14:15 ` [PATCH 3/5] CpuException: Avoid allocating page but using global variables Ni, Ray
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong

Additionally removed two useless global variables:
"SPIN_LOCK  mDisplayMessageSpinLock" from SMM instance.
"UINTN mEnabledInterruptNum" from DXE instance.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../Library/CpuExceptionHandlerLib/DxeException.c  | 11 ++++++-----
 .../Library/CpuExceptionHandlerLib/SmmException.c  | 14 ++++++--------
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index 5083c4b8e8..da5b96d6c6 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -16,9 +16,12 @@ CONST UINTN  mDoFarReturnFlag = 0;
 
 RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
 EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
-UINTN                      mEnabledInterruptNum = 0;
-
-EXCEPTION_HANDLER_DATA  mExceptionHandlerData;
+EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
+  0,   // To be fixed
+  0,   // To be fixed
+  mReservedVectorsData,
+  mExternalInterruptHandlerTable
+};
 
 UINT8  mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *
                  CPU_KNOWN_GOOD_STACK_SIZE];
@@ -62,8 +65,6 @@ InitializeCpuExceptionHandlers (
   IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
   )
 {
-  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;
-  mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;
   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
   return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
 }
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 77ee74579f..9f0af4120a 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -11,14 +11,14 @@
 
 CONST UINTN  mDoFarReturnFlag = 1;
 
-//
-// Spin lock for CPU information display
-//
-SPIN_LOCK  mDisplayMessageSpinLock;
-
 RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
 EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
-EXCEPTION_HANDLER_DATA     mExceptionHandlerData;
+EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
+  0,   // To be fixed
+  0,   // To be fixed
+  mReservedVectorsData,
+  mExternalInterruptHandlerTable
+};
 
 /**
   Common exception handler.
@@ -58,8 +58,6 @@ InitializeCpuExceptionHandlers (
   IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
   )
 {
-  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;
-  mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;
   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
   return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
 }
-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/5] CpuException: Avoid allocating page but using global variables
  2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
  2022-05-20 14:15 ` [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance Ni, Ray
  2022-05-20 14:15 ` [PATCH 2/5] CpuException: Init global variables in-place Ni, Ray
@ 2022-05-20 14:15 ` Ni, Ray
  2022-05-22 16:30   ` [edk2-devel] " Wang, Jian J
  2022-06-06  7:47   ` Dong, Eric
  2022-05-20 14:15 ` [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers Ni, Ray
  2022-05-20 14:15 ` [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks Ni, Ray
  4 siblings, 2 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../CpuExceptionHandlerLib/DxeException.c     | 24 ++++---------------
 1 file changed, 5 insertions(+), 19 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index da5b96d6c6..f139131a7c 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -14,8 +14,8 @@
 
 CONST UINTN  mDoFarReturnFlag = 0;
 
-RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
-EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
+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
@@ -96,27 +96,15 @@ InitializeCpuInterruptHandlers (
   IA32_DESCRIPTOR                 IdtDescriptor;
   UINTN                           IdtEntryCount;
   EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
-  RESERVED_VECTORS_DATA           *ReservedVectors;
-  EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
-
-  Status = gBS->AllocatePool (
-                  EfiBootServicesCode,
-                  sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,
-                  (VOID **)&ReservedVectors
-                  );
-  ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);
-  SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);
+
+  SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);
   if (VectorInfo != NULL) {
-    Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);
+    Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData, CPU_INTERRUPT_NUM);
     if (EFI_ERROR (Status)) {
-      FreePool (ReservedVectors);
       return EFI_INVALID_PARAMETER;
     }
   }
 
-  ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);
-  ASSERT (ExternalInterruptHandler != NULL);
-
   //
   // Read IDT descriptor and calculate IDT size
   //
@@ -137,8 +125,6 @@ InitializeCpuInterruptHandlers (
   ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
 
   mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
-  mExceptionHandlerData.ReservedVectors          = ReservedVectors;
-  mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
 
   UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers
  2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
                   ` (2 preceding siblings ...)
  2022-05-20 14:15 ` [PATCH 3/5] CpuException: Avoid allocating page but using global variables Ni, Ray
@ 2022-05-20 14:15 ` Ni, Ray
  2022-05-22 16:27   ` [edk2-devel] " Wang, Jian J
  2022-05-20 14:15 ` [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks Ni, Ray
  4 siblings, 1 reply; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong

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@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../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/CpuExceptionHandlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
index 35ab5a8db5..54f38788fe 100644
--- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
+++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.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


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks
  2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
                   ` (3 preceding siblings ...)
  2022-05-20 14:15 ` [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers Ni, Ray
@ 2022-05-20 14:15 ` Ni, Ray
  2022-05-22 16:05   ` Wang, Jian J
  2022-06-06  7:47   ` Dong, Eric
  4 siblings, 2 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-20 14:15 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong, Jian J Wang

Today InitializeCpuExceptionHandlersEx is called from three modules:
1. DxeCore (links to DxeCpuExceptionHandlerLib)
    DxeCore expects it initializes the IDT entries as well as
    assigning separate stacks for #DF and #PF.
2. CpuMpPei (links to PeiCpuExceptionHandlerLib)
   and CpuDxe (links to DxeCpuExceptionHandlerLib)
    It's called for each thread for only assigning separate stacks for
    #DF and #PF. The IDT entries initialization is skipped because
    caller sets InitData->X64.InitDefaultHandlers to FALSE.

Additionally, SecPeiCpuExceptionHandlerLib, SmmCpuExceptionHandlerLib
also implement such API and the behavior of the API is simply to initialize
IDT entries only.

Because it mixes the IDT entries initialization and separate stacks
assignment for certain exception handlers together, in order to know
whether the function call only initializes IDT entries, or assigns stacks,
we need to check:
1. value of InitData->X64.InitDefaultHandlers
2. library instance

This patch cleans up the code to separate the stack assignment to a new API:
InitializeSeparateExceptionStacks().

Only when caller calls the new API, the separate stacks are assigned.
With this change, the SecPei and Smm instance can return unsupported which
gives caller a very clear status.

The old API InitializeCpuExceptionHandlersEx() is removed in this patch.
Because no platform module is consuming the old API, the impact is none.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c       |  2 +-
 .../Include/Library/CpuExceptionHandlerLib.h  | 24 ++---
 .../CpuExceptionHandlerLibNull.c              | 26 ++----
 UefiCpuPkg/CpuDxe/CpuMp.c                     |  6 +-
 UefiCpuPkg/CpuMpPei/CpuMpPei.c                |  4 +-
 .../CpuExceptionHandlerLib/DxeException.c     | 91 ++++++-------------
 .../CpuExceptionHandlerLib/PeiCpuException.c  | 51 ++---------
 .../SecPeiCpuException.c                      | 27 ++----
 .../CpuExceptionHandlerLib/SmmException.c     | 27 ++----
 9 files changed, 74 insertions(+), 184 deletions(-)

diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
index 2c27fc0695..83f49d7c00 100644
--- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
+++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
@@ -253,7 +253,7 @@ DxeMain (
     VectorInfoList = (EFI_VECTOR_HANDOFF_INFO *)(GET_GUID_HOB_DATA (GuidHob));
   }
 
-  Status = InitializeCpuExceptionHandlersEx (VectorInfoList, NULL);
+  Status = InitializeCpuExceptionHandlers (VectorInfoList);
   ASSERT_EFI_ERROR (Status);
 
   //
diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
index d4649bebe1..9a495081f7 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -103,32 +103,20 @@ InitializeCpuExceptionHandlers (
   );
 
 /**
-  Initializes all CPU exceptions entries with optional extra initializations.
+  Setup separate stacks for certain exception handlers.
 
-  By default, this method should include all functionalities implemented by
-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
-  This could be done by calling InitializeCpuExceptionHandlers() directly
-  in this method besides the extra works.
+  InitData is optional and processor arch dependent.
 
-  InitData is optional and its use and content are processor arch dependent.
-  The typical usage of it is to convey resources which have to be reserved
-  elsewhere and are necessary for the extra initializations of exception.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data optional for extra initializations
-                            of exception.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
   @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   );
 
diff --git a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
index 54f38788fe..8aeedcb4d1 100644
--- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
+++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
@@ -82,34 +82,22 @@ DumpCpuContext (
 }
 
 /**
-  Initializes all CPU exceptions entries with optional extra initializations.
+  Setup separate stacks for certain exception handlers.
 
-  By default, this method should include all functionalities implemented by
-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
-  This could be done by calling InitializeCpuExceptionHandlers() directly
-  in this method besides the extra works.
+  InitData is optional and processor arch dependent.
 
-  InitData is optional and its use and content are processor arch dependent.
-  The typical usage of it is to convey resources which have to be reserved
-  elsewhere and are necessary for the extra initializations of exception.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data optional for extra initializations
-                            of exception.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
   @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   )
 {
-  return InitializeCpuExceptionHandlers (VectorInfo);
+  return EFI_UNSUPPORTED;
 }
diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c
index 1f218367b3..e385f585c7 100644
--- a/UefiCpuPkg/CpuDxe/CpuMp.c
+++ b/UefiCpuPkg/CpuDxe/CpuMp.c
@@ -1,7 +1,7 @@
 /** @file
   CPU DXE Module to produce CPU MP Protocol.
 
-  Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -617,7 +617,7 @@ GetGdtr (
 /**
   Initializes CPU exceptions handlers for the sake of stack switch requirement.
 
-  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
+  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly
   for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
 
   @param[in,out] Buffer  The pointer to private data buffer.
@@ -641,7 +641,7 @@ InitializeExceptionStackSwitchHandlers (
   AsmReadIdtr (&Idtr);
   EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;
   EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
-  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);
+  Status                     = InitializeSeparateExceptionStacks (EssData);
   ASSERT_EFI_ERROR (Status);
 }
 
diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
index 1e68c91d95..d4786979fa 100644
--- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
+++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
@@ -432,7 +432,7 @@ GetGdtr (
 /**
   Initializes CPU exceptions handlers for the sake of stack switch requirement.
 
-  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
+  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly
   for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
 
   @param[in,out] Buffer  The pointer to private data buffer.
@@ -456,7 +456,7 @@ InitializeExceptionStackSwitchHandlers (
   AsmReadIdtr (&Idtr);
   EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;
   EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
-  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);
+  Status                     = InitializeSeparateExceptionStacks (EssData);
   ASSERT_EFI_ERROR (Status);
 }
 
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index c7c1fe31d2..e62bb5e6c0 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -103,82 +103,49 @@ RegisterCpuInterruptHandler (
 }
 
 /**
-  Initializes CPU exceptions entries and setup stack switch for given exceptions.
+  Setup separate stacks for certain exception handlers.
 
-  This method will call InitializeCpuExceptionHandlers() to setup default
-  exception handlers unless indicated not to do it explicitly.
+  InitData is optional and processor arch dependent.
 
-  If InitData is passed with NULL, this method will use the resource reserved
-  by global variables to initialize it; Otherwise it will use data in InitData
-  to setup stack switch. This is for the different use cases in DxeCore and
-  Cpu MP exception initialization.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data required to setup stack switch for
-                            given exceptions.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
+  @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   )
 {
-  EFI_STATUS               Status;
   CPU_EXCEPTION_INIT_DATA  EssData;
   IA32_DESCRIPTOR          Idtr;
   IA32_DESCRIPTOR          Gdtr;
 
-  //
-  // To avoid repeat initialization of default handlers, the caller should pass
-  // an extended init data with InitDefaultHandlers set to FALSE. There's no
-  // need to call this method to just initialize default handlers. Call non-ex
-  // version instead; or this method must be implemented as a simple wrapper of
-  // non-ex version of it, if this version has to be called.
-  //
-  if ((InitData == NULL) || InitData->X64.InitDefaultHandlers) {
-    Status = InitializeCpuExceptionHandlers (VectorInfo);
-  } else {
-    Status = EFI_SUCCESS;
-  }
-
-  if (!EFI_ERROR (Status)) {
-    //
-    // Initializing stack switch is only necessary for Stack Guard functionality.
-    //
-    if (PcdGetBool (PcdCpuStackGuard)) {
-      if (InitData == NULL) {
-        SetMem (mNewGdt, sizeof (mNewGdt), 0);
-
-        AsmReadIdtr (&Idtr);
-        AsmReadGdtr (&Gdtr);
-
-        EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;
-        EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof (mNewStack);
-        EssData.X64.KnownGoodStackSize         = CPU_KNOWN_GOOD_STACK_SIZE;
-        EssData.X64.StackSwitchExceptions      = CPU_STACK_SWITCH_EXCEPTION_LIST;
-        EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;
-        EssData.X64.IdtTable                   = (VOID *)Idtr.Base;
-        EssData.X64.IdtTableSize               = Idtr.Limit + 1;
-        EssData.X64.GdtTable                   = mNewGdt;
-        EssData.X64.GdtTableSize               = sizeof (mNewGdt);
-        EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;
-        EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;
-        EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;
-        EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;
-
-        InitData = &EssData;
-      }
-
-      Status = ArchSetupExceptionStack (InitData);
-    }
+  if (InitData == NULL) {
+    SetMem (mNewGdt, sizeof (mNewGdt), 0);
+
+    AsmReadIdtr (&Idtr);
+    AsmReadGdtr (&Gdtr);
+
+    EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;
+    EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof (mNewStack);
+    EssData.X64.KnownGoodStackSize         = CPU_KNOWN_GOOD_STACK_SIZE;
+    EssData.X64.StackSwitchExceptions      = CPU_STACK_SWITCH_EXCEPTION_LIST;
+    EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;
+    EssData.X64.IdtTable                   = (VOID *)Idtr.Base;
+    EssData.X64.IdtTableSize               = Idtr.Limit + 1;
+    EssData.X64.GdtTable                   = mNewGdt;
+    EssData.X64.GdtTableSize               = sizeof (mNewGdt);
+    EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;
+    EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;
+    EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;
+    EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;
+
+    InitData = &EssData;
   }
 
-  return Status;
+  return ArchSetupExceptionStack (InitData);
 }
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 1ae611c75e..494c2ab433 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -150,57 +150,26 @@ InitializeCpuExceptionHandlers (
 }
 
 /**
-  Initializes all CPU exceptions entries with optional extra initializations.
+  Setup separate stacks for certain exception handlers.
 
-  By default, this method should include all functionalities implemented by
-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
-  This could be done by calling InitializeCpuExceptionHandlers() directly
-  in this method besides the extra works.
+  InitData is optional and processor arch dependent.
 
-  InitData is optional and its use and content are processor arch dependent.
-  The typical usage of it is to convey resources which have to be reserved
-  elsewhere and are necessary for the extra initializations of exception.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data optional for extra initializations
-                            of exception.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
+  @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   )
 {
-  EFI_STATUS  Status;
-
-  //
-  // To avoid repeat initialization of default handlers, the caller should pass
-  // an extended init data with InitDefaultHandlers set to FALSE. There's no
-  // need to call this method to just initialize default handlers. Call non-ex
-  // version instead; or this method must be implemented as a simple wrapper of
-  // non-ex version of it, if this version has to be called.
-  //
-  if ((InitData == NULL) || InitData->Ia32.InitDefaultHandlers) {
-    Status = InitializeCpuExceptionHandlers (VectorInfo);
-  } else {
-    Status = EFI_SUCCESS;
-  }
-
-  if (!EFI_ERROR (Status)) {
-    //
-    // Initializing stack switch is only necessary for Stack Guard functionality.
-    //
-    if (PcdGetBool (PcdCpuStackGuard) && (InitData != NULL)) {
-      Status = ArchSetupExceptionStack (InitData);
-    }
+  if (InitData == NULL) {
+    return EFI_UNSUPPORTED;
   }
 
-  return Status;
+  return ArchSetupExceptionStack (InitData);
 }
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index e894ead612..4313cc5582 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -200,33 +200,22 @@ RegisterCpuInterruptHandler (
 }
 
 /**
-  Initializes all CPU exceptions entries with optional extra initializations.
+  Setup separate stacks for certain exception handlers.
 
-  By default, this method should include all functionalities implemented by
-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
-  This could be done by calling InitializeCpuExceptionHandlers() directly
-  in this method besides the extra works.
+  InitData is optional and processor arch dependent.
 
-  InitData is optional and its use and content are processor arch dependent.
-  The typical usage of it is to convey resources which have to be reserved
-  elsewhere and are necessary for the extra initializations of exception.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data optional for extra initializations
-                            of exception.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
+  @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   )
 {
-  return InitializeCpuExceptionHandlers (VectorInfo);
+  return EFI_UNSUPPORTED;
 }
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index ec643556c7..1c97dab926 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -96,33 +96,22 @@ RegisterCpuInterruptHandler (
 }
 
 /**
-  Initializes all CPU exceptions entries with optional extra initializations.
+  Setup separate stacks for certain exception handlers.
 
-  By default, this method should include all functionalities implemented by
-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
-  This could be done by calling InitializeCpuExceptionHandlers() directly
-  in this method besides the extra works.
+  InitData is optional and processor arch dependent.
 
-  InitData is optional and its use and content are processor arch dependent.
-  The typical usage of it is to convey resources which have to be reserved
-  elsewhere and are necessary for the extra initializations of exception.
+  @param[in]  InitData      Pointer to data optional for information about how
+                            to assign stacks for certain exception handlers.
 
-  @param[in]  VectorInfo    Pointer to reserved vector list.
-  @param[in]  InitData      Pointer to data optional for extra initializations
-                            of exception.
-
-  @retval EFI_SUCCESS             The exceptions have been successfully
-                                  initialized.
-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
-                                  content.
+  @retval EFI_SUCCESS             The stacks are assigned successfully.
+  @retval EFI_UNSUPPORTED         This function is not supported.
 
 **/
 EFI_STATUS
 EFIAPI
-InitializeCpuExceptionHandlersEx (
-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
+InitializeSeparateExceptionStacks (
   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
   )
 {
-  return InitializeCpuExceptionHandlers (VectorInfo);
+  return EFI_UNSUPPORTED;
 }
-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks
  2022-05-20 14:15 ` [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks Ni, Ray
@ 2022-05-22 16:05   ` Wang, Jian J
  2022-06-06  7:47   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Wang, Jian J @ 2022-05-22 16:05 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io; +Cc: Dong, Eric


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

Regards,
Jian

> -----Original Message-----
> From: Ni, Ray <ray.ni@intel.com>
> Sent: Friday, May 20, 2022 10:16 PM
> To: devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@intel.com>; Wang, Jian J <jian.j.wang@intel.com>
> Subject: [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks
> 
> Today InitializeCpuExceptionHandlersEx is called from three modules:
> 1. DxeCore (links to DxeCpuExceptionHandlerLib)
>     DxeCore expects it initializes the IDT entries as well as
>     assigning separate stacks for #DF and #PF.
> 2. CpuMpPei (links to PeiCpuExceptionHandlerLib)
>    and CpuDxe (links to DxeCpuExceptionHandlerLib)
>     It's called for each thread for only assigning separate stacks for
>     #DF and #PF. The IDT entries initialization is skipped because
>     caller sets InitData->X64.InitDefaultHandlers to FALSE.
> 
> Additionally, SecPeiCpuExceptionHandlerLib, SmmCpuExceptionHandlerLib
> also implement such API and the behavior of the API is simply to initialize
> IDT entries only.
> 
> Because it mixes the IDT entries initialization and separate stacks
> assignment for certain exception handlers together, in order to know
> whether the function call only initializes IDT entries, or assigns stacks,
> we need to check:
> 1. value of InitData->X64.InitDefaultHandlers
> 2. library instance
> 
> This patch cleans up the code to separate the stack assignment to a new API:
> InitializeSeparateExceptionStacks().
> 
> Only when caller calls the new API, the separate stacks are assigned.
> With this change, the SecPei and Smm instance can return unsupported which
> gives caller a very clear status.
> 
> The old API InitializeCpuExceptionHandlersEx() is removed in this patch.
> Because no platform module is consuming the old API, the impact is none.
> 
> Signed-off-by: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> ---
>  MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c       |  2 +-
>  .../Include/Library/CpuExceptionHandlerLib.h  | 24 ++---
>  .../CpuExceptionHandlerLibNull.c              | 26 ++----
>  UefiCpuPkg/CpuDxe/CpuMp.c                     |  6 +-
>  UefiCpuPkg/CpuMpPei/CpuMpPei.c                |  4 +-
>  .../CpuExceptionHandlerLib/DxeException.c     | 91 ++++++-------------
>  .../CpuExceptionHandlerLib/PeiCpuException.c  | 51 ++---------
>  .../SecPeiCpuException.c                      | 27 ++----
>  .../CpuExceptionHandlerLib/SmmException.c     | 27 ++----
>  9 files changed, 74 insertions(+), 184 deletions(-)
> 
> diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
> b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
> index 2c27fc0695..83f49d7c00 100644
> --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
> +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
> @@ -253,7 +253,7 @@ DxeMain (
>      VectorInfoList = (EFI_VECTOR_HANDOFF_INFO *)(GET_GUID_HOB_DATA
> (GuidHob));
> 
>    }
> 
> 
> 
> -  Status = InitializeCpuExceptionHandlersEx (VectorInfoList, NULL);
> 
> +  Status = InitializeCpuExceptionHandlers (VectorInfoList);
> 
>    ASSERT_EFI_ERROR (Status);
> 
> 
> 
>    //
> 
> diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> index d4649bebe1..9a495081f7 100644
> --- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> +++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
> @@ -103,32 +103,20 @@ InitializeCpuExceptionHandlers (
>    );
> 
> 
> 
>  /**
> 
> -  Initializes all CPU exceptions entries with optional extra initializations.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  By default, this method should include all functionalities implemented by
> 
> -  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
> 
> -  This could be done by calling InitializeCpuExceptionHandlers() directly
> 
> -  in this method besides the extra works.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  InitData is optional and its use and content are processor arch dependent.
> 
> -  The typical usage of it is to convey resources which have to be reserved
> 
> -  elsewhere and are necessary for the extra initializations of exception.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data optional for extra initializations
> 
> -                            of exception.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
>    @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    );
> 
> 
> 
> diff --git
> a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> index 54f38788fe..8aeedcb4d1 100644
> ---
> a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> +++
> b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLib
> Null.c
> @@ -82,34 +82,22 @@ DumpCpuContext (
>  }
> 
> 
> 
>  /**
> 
> -  Initializes all CPU exceptions entries with optional extra initializations.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  By default, this method should include all functionalities implemented by
> 
> -  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
> 
> -  This could be done by calling InitializeCpuExceptionHandlers() directly
> 
> -  in this method besides the extra works.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  InitData is optional and its use and content are processor arch dependent.
> 
> -  The typical usage of it is to convey resources which have to be reserved
> 
> -  elsewhere and are necessary for the extra initializations of exception.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data optional for extra initializations
> 
> -                            of exception.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
>    @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    )
> 
>  {
> 
> -  return InitializeCpuExceptionHandlers (VectorInfo);
> 
> +  return EFI_UNSUPPORTED;
> 
>  }
> 
> diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c
> index 1f218367b3..e385f585c7 100644
> --- a/UefiCpuPkg/CpuDxe/CpuMp.c
> +++ b/UefiCpuPkg/CpuDxe/CpuMp.c
> @@ -1,7 +1,7 @@
>  /** @file
> 
>    CPU DXE Module to produce CPU MP Protocol.
> 
> 
> 
> -  Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
> 
> +  Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>
> 
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> 
> 
>  **/
> 
> @@ -617,7 +617,7 @@ GetGdtr (
>  /**
> 
>    Initializes CPU exceptions handlers for the sake of stack switch requirement.
> 
> 
> 
> -  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
> 
> +  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly
> 
>    for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
> 
> 
> 
>    @param[in,out] Buffer  The pointer to private data buffer.
> 
> @@ -641,7 +641,7 @@ InitializeExceptionStackSwitchHandlers (
>    AsmReadIdtr (&Idtr);
> 
>    EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;
> 
>    EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
> 
> -  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);
> 
> +  Status                     = InitializeSeparateExceptionStacks (EssData);
> 
>    ASSERT_EFI_ERROR (Status);
> 
>  }
> 
> 
> 
> diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
> b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
> index 1e68c91d95..d4786979fa 100644
> --- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
> +++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
> @@ -432,7 +432,7 @@ GetGdtr (
>  /**
> 
>    Initializes CPU exceptions handlers for the sake of stack switch requirement.
> 
> 
> 
> -  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
> 
> +  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly
> 
>    for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
> 
> 
> 
>    @param[in,out] Buffer  The pointer to private data buffer.
> 
> @@ -456,7 +456,7 @@ InitializeExceptionStackSwitchHandlers (
>    AsmReadIdtr (&Idtr);
> 
>    EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;
> 
>    EssData->Ia32.IdtTableSize = Idtr.Limit + 1;
> 
> -  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);
> 
> +  Status                     = InitializeSeparateExceptionStacks (EssData);
> 
>    ASSERT_EFI_ERROR (Status);
> 
>  }
> 
> 
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index c7c1fe31d2..e62bb5e6c0 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -103,82 +103,49 @@ RegisterCpuInterruptHandler (
>  }
> 
> 
> 
>  /**
> 
> -  Initializes CPU exceptions entries and setup stack switch for given exceptions.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  This method will call InitializeCpuExceptionHandlers() to setup default
> 
> -  exception handlers unless indicated not to do it explicitly.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  If InitData is passed with NULL, this method will use the resource reserved
> 
> -  by global variables to initialize it; Otherwise it will use data in InitData
> 
> -  to setup stack switch. This is for the different use cases in DxeCore and
> 
> -  Cpu MP exception initialization.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data required to setup stack switch for
> 
> -                            given exceptions.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
> +  @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    )
> 
>  {
> 
> -  EFI_STATUS               Status;
> 
>    CPU_EXCEPTION_INIT_DATA  EssData;
> 
>    IA32_DESCRIPTOR          Idtr;
> 
>    IA32_DESCRIPTOR          Gdtr;
> 
> 
> 
> -  //
> 
> -  // To avoid repeat initialization of default handlers, the caller should pass
> 
> -  // an extended init data with InitDefaultHandlers set to FALSE. There's no
> 
> -  // need to call this method to just initialize default handlers. Call non-ex
> 
> -  // version instead; or this method must be implemented as a simple wrapper
> of
> 
> -  // non-ex version of it, if this version has to be called.
> 
> -  //
> 
> -  if ((InitData == NULL) || InitData->X64.InitDefaultHandlers) {
> 
> -    Status = InitializeCpuExceptionHandlers (VectorInfo);
> 
> -  } else {
> 
> -    Status = EFI_SUCCESS;
> 
> -  }
> 
> -
> 
> -  if (!EFI_ERROR (Status)) {
> 
> -    //
> 
> -    // Initializing stack switch is only necessary for Stack Guard functionality.
> 
> -    //
> 
> -    if (PcdGetBool (PcdCpuStackGuard)) {
> 
> -      if (InitData == NULL) {
> 
> -        SetMem (mNewGdt, sizeof (mNewGdt), 0);
> 
> -
> 
> -        AsmReadIdtr (&Idtr);
> 
> -        AsmReadGdtr (&Gdtr);
> 
> -
> 
> -        EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;
> 
> -        EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof
> (mNewStack);
> 
> -        EssData.X64.KnownGoodStackSize         =
> CPU_KNOWN_GOOD_STACK_SIZE;
> 
> -        EssData.X64.StackSwitchExceptions      =
> CPU_STACK_SWITCH_EXCEPTION_LIST;
> 
> -        EssData.X64.StackSwitchExceptionNumber =
> CPU_STACK_SWITCH_EXCEPTION_NUMBER;
> 
> -        EssData.X64.IdtTable                   = (VOID *)Idtr.Base;
> 
> -        EssData.X64.IdtTableSize               = Idtr.Limit + 1;
> 
> -        EssData.X64.GdtTable                   = mNewGdt;
> 
> -        EssData.X64.GdtTableSize               = sizeof (mNewGdt);
> 
> -        EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;
> 
> -        EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;
> 
> -        EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 +
> CPU_TSS_DESC_SIZE;
> 
> -        EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;
> 
> -
> 
> -        InitData = &EssData;
> 
> -      }
> 
> -
> 
> -      Status = ArchSetupExceptionStack (InitData);
> 
> -    }
> 
> +  if (InitData == NULL) {
> 
> +    SetMem (mNewGdt, sizeof (mNewGdt), 0);
> 
> +
> 
> +    AsmReadIdtr (&Idtr);
> 
> +    AsmReadGdtr (&Gdtr);
> 
> +
> 
> +    EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;
> 
> +    EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof
> (mNewStack);
> 
> +    EssData.X64.KnownGoodStackSize         = CPU_KNOWN_GOOD_STACK_SIZE;
> 
> +    EssData.X64.StackSwitchExceptions      =
> CPU_STACK_SWITCH_EXCEPTION_LIST;
> 
> +    EssData.X64.StackSwitchExceptionNumber =
> CPU_STACK_SWITCH_EXCEPTION_NUMBER;
> 
> +    EssData.X64.IdtTable                   = (VOID *)Idtr.Base;
> 
> +    EssData.X64.IdtTableSize               = Idtr.Limit + 1;
> 
> +    EssData.X64.GdtTable                   = mNewGdt;
> 
> +    EssData.X64.GdtTableSize               = sizeof (mNewGdt);
> 
> +    EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;
> 
> +    EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;
> 
> +    EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 +
> CPU_TSS_DESC_SIZE;
> 
> +    EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;
> 
> +
> 
> +    InitData = &EssData;
> 
>    }
> 
> 
> 
> -  return Status;
> 
> +  return ArchSetupExceptionStack (InitData);
> 
>  }
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> index 1ae611c75e..494c2ab433 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
> @@ -150,57 +150,26 @@ InitializeCpuExceptionHandlers (
>  }
> 
> 
> 
>  /**
> 
> -  Initializes all CPU exceptions entries with optional extra initializations.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  By default, this method should include all functionalities implemented by
> 
> -  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
> 
> -  This could be done by calling InitializeCpuExceptionHandlers() directly
> 
> -  in this method besides the extra works.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  InitData is optional and its use and content are processor arch dependent.
> 
> -  The typical usage of it is to convey resources which have to be reserved
> 
> -  elsewhere and are necessary for the extra initializations of exception.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data optional for extra initializations
> 
> -                            of exception.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
> +  @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    )
> 
>  {
> 
> -  EFI_STATUS  Status;
> 
> -
> 
> -  //
> 
> -  // To avoid repeat initialization of default handlers, the caller should pass
> 
> -  // an extended init data with InitDefaultHandlers set to FALSE. There's no
> 
> -  // need to call this method to just initialize default handlers. Call non-ex
> 
> -  // version instead; or this method must be implemented as a simple wrapper
> of
> 
> -  // non-ex version of it, if this version has to be called.
> 
> -  //
> 
> -  if ((InitData == NULL) || InitData->Ia32.InitDefaultHandlers) {
> 
> -    Status = InitializeCpuExceptionHandlers (VectorInfo);
> 
> -  } else {
> 
> -    Status = EFI_SUCCESS;
> 
> -  }
> 
> -
> 
> -  if (!EFI_ERROR (Status)) {
> 
> -    //
> 
> -    // Initializing stack switch is only necessary for Stack Guard functionality.
> 
> -    //
> 
> -    if (PcdGetBool (PcdCpuStackGuard) && (InitData != NULL)) {
> 
> -      Status = ArchSetupExceptionStack (InitData);
> 
> -    }
> 
> +  if (InitData == NULL) {
> 
> +    return EFI_UNSUPPORTED;
> 
>    }
> 
> 
> 
> -  return Status;
> 
> +  return ArchSetupExceptionStack (InitData);
> 
>  }
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> index e894ead612..4313cc5582 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
> @@ -200,33 +200,22 @@ RegisterCpuInterruptHandler (
>  }
> 
> 
> 
>  /**
> 
> -  Initializes all CPU exceptions entries with optional extra initializations.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  By default, this method should include all functionalities implemented by
> 
> -  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
> 
> -  This could be done by calling InitializeCpuExceptionHandlers() directly
> 
> -  in this method besides the extra works.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  InitData is optional and its use and content are processor arch dependent.
> 
> -  The typical usage of it is to convey resources which have to be reserved
> 
> -  elsewhere and are necessary for the extra initializations of exception.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data optional for extra initializations
> 
> -                            of exception.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
> +  @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    )
> 
>  {
> 
> -  return InitializeCpuExceptionHandlers (VectorInfo);
> 
> +  return EFI_UNSUPPORTED;
> 
>  }
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> index ec643556c7..1c97dab926 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> @@ -96,33 +96,22 @@ RegisterCpuInterruptHandler (
>  }
> 
> 
> 
>  /**
> 
> -  Initializes all CPU exceptions entries with optional extra initializations.
> 
> +  Setup separate stacks for certain exception handlers.
> 
> 
> 
> -  By default, this method should include all functionalities implemented by
> 
> -  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
> 
> -  This could be done by calling InitializeCpuExceptionHandlers() directly
> 
> -  in this method besides the extra works.
> 
> +  InitData is optional and processor arch dependent.
> 
> 
> 
> -  InitData is optional and its use and content are processor arch dependent.
> 
> -  The typical usage of it is to convey resources which have to be reserved
> 
> -  elsewhere and are necessary for the extra initializations of exception.
> 
> +  @param[in]  InitData      Pointer to data optional for information about how
> 
> +                            to assign stacks for certain exception handlers.
> 
> 
> 
> -  @param[in]  VectorInfo    Pointer to reserved vector list.
> 
> -  @param[in]  InitData      Pointer to data optional for extra initializations
> 
> -                            of exception.
> 
> -
> 
> -  @retval EFI_SUCCESS             The exceptions have been successfully
> 
> -                                  initialized.
> 
> -  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
> 
> -                                  content.
> 
> +  @retval EFI_SUCCESS             The stacks are assigned successfully.
> 
> +  @retval EFI_UNSUPPORTED         This function is not supported.
> 
> 
> 
>  **/
> 
>  EFI_STATUS
> 
>  EFIAPI
> 
> -InitializeCpuExceptionHandlersEx (
> 
> -  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,
> 
> +InitializeSeparateExceptionStacks (
> 
>    IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL
> 
>    )
> 
>  {
> 
> -  return InitializeCpuExceptionHandlers (VectorInfo);
> 
> +  return EFI_UNSUPPORTED;
> 
>  }
> 
> --
> 2.35.1.windows.2


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers
  2022-05-20 14:15 ` [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers Ni, Ray
@ 2022-05-22 16:27   ` Wang, Jian J
  2022-05-24  8:04     ` Ni, Ray
  0 siblings, 1 reply; 20+ messages in thread
From: Wang, Jian J @ 2022-05-22 16:27 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ni, Ray; +Cc: Dong, Eric

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@intel.com>
> 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@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../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@intel.com]
> -=-=-=-=-=-=
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 3/5] CpuException: Avoid allocating page but using global variables
  2022-05-20 14:15 ` [PATCH 3/5] CpuException: Avoid allocating page but using global variables Ni, Ray
@ 2022-05-22 16:30   ` Wang, Jian J
  2022-06-06  7:47   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Wang, Jian J @ 2022-05-22 16:30 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ni, Ray; +Cc: Dong, Eric


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

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@intel.com>
> Subject: [edk2-devel] [PATCH 3/5] CpuException: Avoid allocating page but using
> global variables
> 
> Signed-off-by: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../CpuExceptionHandlerLib/DxeException.c     | 24 ++++---------------
>  1 file changed, 5 insertions(+), 19 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index da5b96d6c6..f139131a7c 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -14,8 +14,8 @@
> 
> 
>  CONST UINTN  mDoFarReturnFlag = 0;
> 
> 
> 
> -RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
> 
> -EFI_CPU_INTERRUPT_HANDLER
> mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
> 
> +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
> 
> @@ -96,27 +96,15 @@ InitializeCpuInterruptHandlers (
>    IA32_DESCRIPTOR                 IdtDescriptor;
> 
>    UINTN                           IdtEntryCount;
> 
>    EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
> 
> -  RESERVED_VECTORS_DATA           *ReservedVectors;
> 
> -  EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
> 
> -
> 
> -  Status = gBS->AllocatePool (
> 
> -                  EfiBootServicesCode,
> 
> -                  sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,
> 
> -                  (VOID **)&ReservedVectors
> 
> -                  );
> 
> -  ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);
> 
> -  SetMem ((VOID *)ReservedVectors, sizeof (RESERVED_VECTORS_DATA) *
> CPU_INTERRUPT_NUM, 0xff);
> 
> +
> 
> +  SetMem ((VOID *)mReservedVectorsData, sizeof (RESERVED_VECTORS_DATA)
> * CPU_INTERRUPT_NUM, 0xff);
> 
>    if (VectorInfo != NULL) {
> 
> -    Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors,
> CPU_INTERRUPT_NUM);
> 
> +    Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectorsData,
> CPU_INTERRUPT_NUM);
> 
>      if (EFI_ERROR (Status)) {
> 
> -      FreePool (ReservedVectors);
> 
>        return EFI_INVALID_PARAMETER;
> 
>      }
> 
>    }
> 
> 
> 
> -  ExternalInterruptHandler = AllocateZeroPool (sizeof
> (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);
> 
> -  ASSERT (ExternalInterruptHandler != NULL);
> 
> -
> 
>    //
> 
>    // Read IDT descriptor and calculate IDT size
> 
>    //
> 
> @@ -137,8 +125,6 @@ InitializeCpuInterruptHandlers (
>    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
> 
> 
> 
>    mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
> 
> -  mExceptionHandlerData.ReservedVectors          = ReservedVectors;
> 
> -  mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
> 
>    InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
> 
> 
> 
>    UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
> 
> --
> 2.35.1.windows.2
> 
> 
> 
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89918): https://edk2.groups.io/g/devel/message/89918
> Mute This Topic: https://groups.io/mt/91231769/1768734
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 2/5] CpuException: Init global variables in-place
  2022-05-20 14:15 ` [PATCH 2/5] CpuException: Init global variables in-place Ni, Ray
@ 2022-05-22 16:32   ` Wang, Jian J
  2022-06-06  7:46   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Wang, Jian J @ 2022-05-22 16:32 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ni, Ray; +Cc: Dong, Eric


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

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@intel.com>
> Subject: [edk2-devel] [PATCH 2/5] CpuException: Init global variables in-place
> 
> Additionally removed two useless global variables:
> "SPIN_LOCK  mDisplayMessageSpinLock" from SMM instance.
> "UINTN mEnabledInterruptNum" from DXE instance.
> 
> Signed-off-by: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../Library/CpuExceptionHandlerLib/DxeException.c  | 11 ++++++-----
>  .../Library/CpuExceptionHandlerLib/SmmException.c  | 14 ++++++--------
>  2 files changed, 12 insertions(+), 13 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index 5083c4b8e8..da5b96d6c6 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -16,9 +16,12 @@ CONST UINTN  mDoFarReturnFlag = 0;
> 
> 
>  RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
> 
>  EFI_CPU_INTERRUPT_HANDLER
> mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
> 
> -UINTN                      mEnabledInterruptNum = 0;
> 
> -
> 
> -EXCEPTION_HANDLER_DATA  mExceptionHandlerData;
> 
> +EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
> 
> +  0,   // To be fixed
> 
> +  0,   // To be fixed
> 
> +  mReservedVectorsData,
> 
> +  mExternalInterruptHandlerTable
> 
> +};
> 
> 
> 
>  UINT8  mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *
> 
>                   CPU_KNOWN_GOOD_STACK_SIZE];
> 
> @@ -62,8 +65,6 @@ InitializeCpuExceptionHandlers (
>    IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
> 
>    )
> 
>  {
> 
> -  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;
> 
> -  mExceptionHandlerData.ExternalInterruptHandler =
> mExternalInterruptHandlerTable;
> 
>    InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
> 
>    return InitializeCpuExceptionHandlersWorker (VectorInfo,
> &mExceptionHandlerData);
> 
>  }
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> index 77ee74579f..9f0af4120a 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
> @@ -11,14 +11,14 @@
> 
> 
>  CONST UINTN  mDoFarReturnFlag = 1;
> 
> 
> 
> -//
> 
> -// Spin lock for CPU information display
> 
> -//
> 
> -SPIN_LOCK  mDisplayMessageSpinLock;
> 
> -
> 
>  RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];
> 
>  EFI_CPU_INTERRUPT_HANDLER
> mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
> 
> -EXCEPTION_HANDLER_DATA     mExceptionHandlerData;
> 
> +EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {
> 
> +  0,   // To be fixed
> 
> +  0,   // To be fixed
> 
> +  mReservedVectorsData,
> 
> +  mExternalInterruptHandlerTable
> 
> +};
> 
> 
> 
>  /**
> 
>    Common exception handler.
> 
> @@ -58,8 +58,6 @@ InitializeCpuExceptionHandlers (
>    IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
> 
>    )
> 
>  {
> 
> -  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;
> 
> -  mExceptionHandlerData.ExternalInterruptHandler =
> mExternalInterruptHandlerTable;
> 
>    InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
> 
>    return InitializeCpuExceptionHandlersWorker (VectorInfo,
> &mExceptionHandlerData);
> 
>  }
> 
> --
> 2.35.1.windows.2
> 
> 
> 
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89917): https://edk2.groups.io/g/devel/message/89917
> Mute This Topic: https://groups.io/mt/91231768/1768734
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance
  2022-05-20 14:15 ` [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance Ni, Ray
@ 2022-05-22 16:40   ` Wang, Jian J
  2022-05-24  8:01     ` Ni, Ray
  0 siblings, 1 reply; 20+ messages in thread
From: Wang, Jian J @ 2022-05-22 16:40 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ni, Ray; +Cc: Dong, Eric

Ray,

You changed "%rep 32" to "%rep 256" in Ia32/ExceptionHandlerAsm.nasm.
According to my understanding and your comments, this should be done
only to X64 code, right?

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@intel.com>
> Subject: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages
> for DXE instance
> 
> Today the DXE instance allocates code page and then copies the IDT
> vectors to the allocated code page. Then it fixes up the vector number
> in the IDT vector.
> 
> But if we update the NASM file to generate 256 IDT vectors, there is
> no need to do the copy and fix-up.
> 
> A side effect is up to 4096 bytes (HOOKAFTER_STUB_SIZE * 256) is
> used for 256 IDT vectors. While 32 IDT vectors only require 512 bytes.
> 
> But considering the code logic simplification, 3.5K space is not a big
> deal. SEC instance still generates 32 IDT vectors so no impact to SEC.
> If 3.5K is too much a waste in PEI phase, we can enhance the code
> further to generate 32 vectors for PEI.
> 
> Signed-off-by: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../CpuExceptionHandlerLib/DxeException.c     | 22 -------------------
>  .../Ia32/ExceptionHandlerAsm.nasm             |  4 ++--
>  .../X64/ExceptionHandlerAsm.nasm              |  2 ++
>  .../X64/Xcode5ExceptionHandlerAsm.nasm        |  9 ++++----
>  4 files changed, 9 insertions(+), 28 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index 61f11e98f8..5083c4b8e8 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -95,9 +95,6 @@ InitializeCpuInterruptHandlers (
>    IA32_DESCRIPTOR                 IdtDescriptor;
> 
>    UINTN                           IdtEntryCount;
> 
>    EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
> 
> -  UINTN                           Index;
> 
> -  UINTN                           InterruptEntry;
> 
> -  UINT8                           *InterruptEntryCode;
> 
>    RESERVED_VECTORS_DATA           *ReservedVectors;
> 
>    EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
> 
> 
> 
> @@ -138,25 +135,6 @@ InitializeCpuInterruptHandlers (
>    AsmGetTemplateAddressMap (&TemplateMap);
> 
>    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
> 
> 
> 
> -  Status = gBS->AllocatePool (
> 
> -                  EfiBootServicesCode,
> 
> -                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
> 
> -                  (VOID **)&InterruptEntryCode
> 
> -                  );
> 
> -  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
> 
> -
> 
> -  InterruptEntry = (UINTN)InterruptEntryCode;
> 
> -  for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
> 
> -    CopyMem (
> 
> -      (VOID *)InterruptEntry,
> 
> -      (VOID *)TemplateMap.ExceptionStart,
> 
> -      TemplateMap.ExceptionStubHeaderSize
> 
> -      );
> 
> -    AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID
> *)TemplateMap.ExceptionStart);
> 
> -    InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
> 
> -  }
> 
> -
> 
> -  TemplateMap.ExceptionStart                     = (UINTN)InterruptEntryCode;
> 
>    mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
> 
>    mExceptionHandlerData.ReservedVectors          = ReservedVectors;
> 
>    mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
> 
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> index 3fe9aed1e8..8ed2b8f455 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> @@ -33,7 +33,7 @@ ALIGN   8
>  ;
> 
>  AsmIdtVectorBegin:
> 
>  %assign Vector 0
> 
> -%rep  32
> 
> +%rep  256
> 
>      push    byte %[Vector];
> 
>      push    eax
> 
>      mov     eax, ASM_PFX(CommonInterruptEntry)
> 
> @@ -439,7 +439,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
> 
> 
>      mov ebx, dword [ebp + 0x8]
> 
>      mov dword [ebx],      AsmIdtVectorBegin
> 
> -    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> 
> +    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> 
>      mov dword [ebx + 0x8], HookAfterStubBegin
> 
> 
> 
>      popad
> 
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> index 9a806d1f86..aaf8d622e6 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> @@ -31,6 +31,8 @@ SECTION .text
> 
> 
>  ALIGN   8
> 
> 
> 
> +; Generate 32 IDT vectors.
> 
> +; 32 IDT vectors are enough because interrupts (32+) are not enabled in SEC and
> PEI phase.
> 
>  AsmIdtVectorBegin:
> 
>  %assign Vector 0
> 
>  %rep  32
> 
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> index 9c72fa5815..7c0e3d3b0b 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> @@ -53,9 +53,10 @@ SECTION .text
> 
> 
>  ALIGN   8
> 
> 
> 
> +; Generate 256 IDT vectors.
> 
>  AsmIdtVectorBegin:
> 
>  %assign Vector 0
> 
> -%rep  32
> 
> +%rep  256
> 
>      push    byte %[Vector]
> 
>      push    rax
> 
>      mov     rax, strict qword 0 ;    mov     rax, ASM_PFX(CommonInterruptEntry)
> 
> @@ -453,16 +454,16 @@ global ASM_PFX(AsmGetTemplateAddressMap)
>  ASM_PFX(AsmGetTemplateAddressMap):
> 
>      lea     rax, [AsmIdtVectorBegin]
> 
>      mov     qword [rcx], rax
> 
> -    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> 
> +    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> 
>      lea     rax, [HookAfterStubHeaderBegin]
> 
>      mov     qword [rcx + 0x10], rax
> 
> 
> 
>  ; Fix up CommonInterruptEntry address
> 
>      lea    rax, [ASM_PFX(CommonInterruptEntry)]
> 
>      lea    rcx, [AsmIdtVectorBegin]
> 
> -%rep  32
> 
> +%rep  256
> 
>      mov    qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)],
> rax
> 
> -    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> 
> +    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> 
>  %endrep
> 
>  ; Fix up HookAfterStubHeaderEnd
> 
>      lea    rax, [HookAfterStubHeaderEnd]
> 
> --
> 2.35.1.windows.2
> 
> 
> 
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89916): https://edk2.groups.io/g/devel/message/89916
> Mute This Topic: https://groups.io/mt/91231767/1768734
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
> 


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance
  2022-05-22 16:40   ` [edk2-devel] " Wang, Jian J
@ 2022-05-24  8:01     ` Ni, Ray
  2022-05-24 15:18       ` Wang, Jian J
  2022-06-06  7:46       ` Dong, Eric
  0 siblings, 2 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-24  8:01 UTC (permalink / raw)
  To: Wang, Jian J, devel@edk2.groups.io; +Cc: Dong, Eric

Jian,
Ia32/ExceptionHandlerAsm.nasm is used by 32bit DxeCpuExceptionHandlerLib instance.

I agree the commit message is not correct. The commit message says
SEC still creates 32 entries but 32bit SEC creates 256 entries.

I will update the commit message to align to code behavior.

Thanks,
Ray

________________________________________
From: Wang, Jian J <jian.j.wang@intel.com>
Sent: Monday, May 23, 2022 0:40
To: devel@edk2.groups.io; Ni, Ray
Cc: Dong, Eric
Subject: RE: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance

Ray,

You changed "%rep 32" to "%rep 256" in Ia32/ExceptionHandlerAsm.nasm.
According to my understanding and your comments, this should be done
only to X64 code, right?

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@intel.com>
> Subject: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages
> for DXE instance
>
> Today the DXE instance allocates code page and then copies the IDT
> vectors to the allocated code page. Then it fixes up the vector number
> in the IDT vector.
>
> But if we update the NASM file to generate 256 IDT vectors, there is
> no need to do the copy and fix-up.
>
> A side effect is up to 4096 bytes (HOOKAFTER_STUB_SIZE * 256) is
> used for 256 IDT vectors. While 32 IDT vectors only require 512 bytes.
>
> But considering the code logic simplification, 3.5K space is not a big
> deal. SEC instance still generates 32 IDT vectors so no impact to SEC.
> If 3.5K is too much a waste in PEI phase, we can enhance the code
> further to generate 32 vectors for PEI.
>
> Signed-off-by: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../CpuExceptionHandlerLib/DxeException.c     | 22 -------------------
>  .../Ia32/ExceptionHandlerAsm.nasm             |  4 ++--
>  .../X64/ExceptionHandlerAsm.nasm              |  2 ++
>  .../X64/Xcode5ExceptionHandlerAsm.nasm        |  9 ++++----
>  4 files changed, 9 insertions(+), 28 deletions(-)
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index 61f11e98f8..5083c4b8e8 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -95,9 +95,6 @@ InitializeCpuInterruptHandlers (
>    IA32_DESCRIPTOR                 IdtDescriptor;
>
>    UINTN                           IdtEntryCount;
>
>    EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
>
> -  UINTN                           Index;
>
> -  UINTN                           InterruptEntry;
>
> -  UINT8                           *InterruptEntryCode;
>
>    RESERVED_VECTORS_DATA           *ReservedVectors;
>
>    EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
>
>
>
> @@ -138,25 +135,6 @@ InitializeCpuInterruptHandlers (
>    AsmGetTemplateAddressMap (&TemplateMap);
>
>    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
>
>
>
> -  Status = gBS->AllocatePool (
>
> -                  EfiBootServicesCode,
>
> -                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
>
> -                  (VOID **)&InterruptEntryCode
>
> -                  );
>
> -  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
>
> -
>
> -  InterruptEntry = (UINTN)InterruptEntryCode;
>
> -  for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
>
> -    CopyMem (
>
> -      (VOID *)InterruptEntry,
>
> -      (VOID *)TemplateMap.ExceptionStart,
>
> -      TemplateMap.ExceptionStubHeaderSize
>
> -      );
>
> -    AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID
> *)TemplateMap.ExceptionStart);
>
> -    InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
>
> -  }
>
> -
>
> -  TemplateMap.ExceptionStart                     = (UINTN)InterruptEntryCode;
>
>    mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
>
>    mExceptionHandlerData.ReservedVectors          = ReservedVectors;
>
>    mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> index 3fe9aed1e8..8ed2b8f455 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> @@ -33,7 +33,7 @@ ALIGN   8
>  ;
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
> -%rep  32
>
> +%rep  256
>
>      push    byte %[Vector];
>
>      push    eax
>
>      mov     eax, ASM_PFX(CommonInterruptEntry)
>
> @@ -439,7 +439,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
>
>
>      mov ebx, dword [ebp + 0x8]
>
>      mov dword [ebx],      AsmIdtVectorBegin
>
> -    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>      mov dword [ebx + 0x8], HookAfterStubBegin
>
>
>
>      popad
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> index 9a806d1f86..aaf8d622e6 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> @@ -31,6 +31,8 @@ SECTION .text
>
>
>  ALIGN   8
>
>
>
> +; Generate 32 IDT vectors.
>
> +; 32 IDT vectors are enough because interrupts (32+) are not enabled in SEC and
> PEI phase.
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
>  %rep  32
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> index 9c72fa5815..7c0e3d3b0b 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> @@ -53,9 +53,10 @@ SECTION .text
>
>
>  ALIGN   8
>
>
>
> +; Generate 256 IDT vectors.
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
> -%rep  32
>
> +%rep  256
>
>      push    byte %[Vector]
>
>      push    rax
>
>      mov     rax, strict qword 0 ;    mov     rax, ASM_PFX(CommonInterruptEntry)
>
> @@ -453,16 +454,16 @@ global ASM_PFX(AsmGetTemplateAddressMap)
>  ASM_PFX(AsmGetTemplateAddressMap):
>
>      lea     rax, [AsmIdtVectorBegin]
>
>      mov     qword [rcx], rax
>
> -    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>      lea     rax, [HookAfterStubHeaderBegin]
>
>      mov     qword [rcx + 0x10], rax
>
>
>
>  ; Fix up CommonInterruptEntry address
>
>      lea    rax, [ASM_PFX(CommonInterruptEntry)]
>
>      lea    rcx, [AsmIdtVectorBegin]
>
> -%rep  32
>
> +%rep  256
>
>      mov    qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)],
> rax
>
> -    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>  %endrep
>
>  ; Fix up HookAfterStubHeaderEnd
>
>      lea    rax, [HookAfterStubHeaderEnd]
>
> --
> 2.35.1.windows.2
>
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89916): https://edk2.groups.io/g/devel/message/89916
> Mute This Topic: https://groups.io/mt/91231767/1768734
> Group Owner: devel+owner@edk2.groups.io
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers
  2022-05-22 16:27   ` [edk2-devel] " Wang, Jian J
@ 2022-05-24  8:04     ` Ni, Ray
  2022-05-24 15:17       ` Wang, Jian J
  2022-06-06  7:47       ` Dong, Eric
  0 siblings, 2 replies; 20+ messages in thread
From: Ni, Ray @ 2022-05-24  8:04 UTC (permalink / raw)
  To: Wang, Jian J, devel@edk2.groups.io; +Cc: Dong, Eric

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@intel.com>
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@intel.com>
> 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@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  .../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@intel.com]
> -=-=-=-=-=-=
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers
  2022-05-24  8:04     ` Ni, Ray
@ 2022-05-24 15:17       ` Wang, Jian J
  2022-06-06  7:47       ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Wang, Jian J @ 2022-05-24 15:17 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io; +Cc: Dong, Eric

Another patch works for me.

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

Regards,
Jian

> -----Original Message-----
> From: Ni, Ray <ray.ni@intel.com>
> Sent: Tuesday, May 24, 2022 4:04 PM
> To: Wang, Jian J <jian.j.wang@intel.com>; devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@intel.com>
> 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@intel.com>
> 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@intel.com>
> > 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@intel.com>
> > Cc: Eric Dong <eric.dong@intel.com>
> > ---
> >  .../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@intel.com]
> > -=-=-=-=-=-=
> >


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance
  2022-05-24  8:01     ` Ni, Ray
@ 2022-05-24 15:18       ` Wang, Jian J
  2022-06-06  7:46       ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Wang, Jian J @ 2022-05-24 15:18 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io; +Cc: Dong, Eric

I see. With its addressed,

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

Regards,
Jian

> -----Original Message-----
> From: Ni, Ray <ray.ni@intel.com>
> Sent: Tuesday, May 24, 2022 4:02 PM
> To: Wang, Jian J <jian.j.wang@intel.com>; devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@intel.com>
> Subject: Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code
> pages for DXE instance
> 
> Jian,
> Ia32/ExceptionHandlerAsm.nasm is used by 32bit DxeCpuExceptionHandlerLib
> instance.
> 
> I agree the commit message is not correct. The commit message says
> SEC still creates 32 entries but 32bit SEC creates 256 entries.
> 
> I will update the commit message to align to code behavior.
> 
> Thanks,
> Ray
> 
> ________________________________________
> From: Wang, Jian J <jian.j.wang@intel.com>
> Sent: Monday, May 23, 2022 0:40
> To: devel@edk2.groups.io; Ni, Ray
> Cc: Dong, Eric
> Subject: RE: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code
> pages for DXE instance
> 
> Ray,
> 
> You changed "%rep 32" to "%rep 256" in Ia32/ExceptionHandlerAsm.nasm.
> According to my understanding and your comments, this should be done
> only to X64 code, right?
> 
> 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@intel.com>
> > Subject: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages
> > for DXE instance
> >
> > Today the DXE instance allocates code page and then copies the IDT
> > vectors to the allocated code page. Then it fixes up the vector number
> > in the IDT vector.
> >
> > But if we update the NASM file to generate 256 IDT vectors, there is
> > no need to do the copy and fix-up.
> >
> > A side effect is up to 4096 bytes (HOOKAFTER_STUB_SIZE * 256) is
> > used for 256 IDT vectors. While 32 IDT vectors only require 512 bytes.
> >
> > But considering the code logic simplification, 3.5K space is not a big
> > deal. SEC instance still generates 32 IDT vectors so no impact to SEC.
> > If 3.5K is too much a waste in PEI phase, we can enhance the code
> > further to generate 32 vectors for PEI.
> >
> > Signed-off-by: Ray Ni <ray.ni@intel.com>
> > Cc: Eric Dong <eric.dong@intel.com>
> > ---
> >  .../CpuExceptionHandlerLib/DxeException.c     | 22 -------------------
> >  .../Ia32/ExceptionHandlerAsm.nasm             |  4 ++--
> >  .../X64/ExceptionHandlerAsm.nasm              |  2 ++
> >  .../X64/Xcode5ExceptionHandlerAsm.nasm        |  9 ++++----
> >  4 files changed, 9 insertions(+), 28 deletions(-)
> >
> > diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> > b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> > index 61f11e98f8..5083c4b8e8 100644
> > --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> > +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> > @@ -95,9 +95,6 @@ InitializeCpuInterruptHandlers (
> >    IA32_DESCRIPTOR                 IdtDescriptor;
> >
> >    UINTN                           IdtEntryCount;
> >
> >    EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
> >
> > -  UINTN                           Index;
> >
> > -  UINTN                           InterruptEntry;
> >
> > -  UINT8                           *InterruptEntryCode;
> >
> >    RESERVED_VECTORS_DATA           *ReservedVectors;
> >
> >    EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
> >
> >
> >
> > @@ -138,25 +135,6 @@ InitializeCpuInterruptHandlers (
> >    AsmGetTemplateAddressMap (&TemplateMap);
> >
> >    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
> >
> >
> >
> > -  Status = gBS->AllocatePool (
> >
> > -                  EfiBootServicesCode,
> >
> > -                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
> >
> > -                  (VOID **)&InterruptEntryCode
> >
> > -                  );
> >
> > -  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
> >
> > -
> >
> > -  InterruptEntry = (UINTN)InterruptEntryCode;
> >
> > -  for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
> >
> > -    CopyMem (
> >
> > -      (VOID *)InterruptEntry,
> >
> > -      (VOID *)TemplateMap.ExceptionStart,
> >
> > -      TemplateMap.ExceptionStubHeaderSize
> >
> > -      );
> >
> > -    AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID
> > *)TemplateMap.ExceptionStart);
> >
> > -    InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
> >
> > -  }
> >
> > -
> >
> > -  TemplateMap.ExceptionStart                     = (UINTN)InterruptEntryCode;
> >
> >    mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
> >
> >    mExceptionHandlerData.ReservedVectors          = ReservedVectors;
> >
> >    mExceptionHandlerData.ExternalInterruptHandler =
> ExternalInterruptHandler;
> >
> > diff --git
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> > m
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> > m
> > index 3fe9aed1e8..8ed2b8f455 100644
> > ---
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> > m
> > +++
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> > m
> > @@ -33,7 +33,7 @@ ALIGN   8
> >  ;
> >
> >  AsmIdtVectorBegin:
> >
> >  %assign Vector 0
> >
> > -%rep  32
> >
> > +%rep  256
> >
> >      push    byte %[Vector];
> >
> >      push    eax
> >
> >      mov     eax, ASM_PFX(CommonInterruptEntry)
> >
> > @@ -439,7 +439,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
> >
> >
> >      mov ebx, dword [ebp + 0x8]
> >
> >      mov dword [ebx],      AsmIdtVectorBegin
> >
> > -    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> >
> > +    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> >
> >      mov dword [ebx + 0x8], HookAfterStubBegin
> >
> >
> >
> >      popad
> >
> > diff --git
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> > index 9a806d1f86..aaf8d622e6 100644
> > ---
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> > +++
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> > @@ -31,6 +31,8 @@ SECTION .text
> >
> >
> >  ALIGN   8
> >
> >
> >
> > +; Generate 32 IDT vectors.
> >
> > +; 32 IDT vectors are enough because interrupts (32+) are not enabled in SEC
> and
> > PEI phase.
> >
> >  AsmIdtVectorBegin:
> >
> >  %assign Vector 0
> >
> >  %rep  32
> >
> > diff --git
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> > m.nasm
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> > m.nasm
> > index 9c72fa5815..7c0e3d3b0b 100644
> > ---
> >
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> > m.nasm
> > +++
> >
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> > m.nasm
> > @@ -53,9 +53,10 @@ SECTION .text
> >
> >
> >  ALIGN   8
> >
> >
> >
> > +; Generate 256 IDT vectors.
> >
> >  AsmIdtVectorBegin:
> >
> >  %assign Vector 0
> >
> > -%rep  32
> >
> > +%rep  256
> >
> >      push    byte %[Vector]
> >
> >      push    rax
> >
> >      mov     rax, strict qword 0 ;    mov     rax, ASM_PFX(CommonInterruptEntry)
> >
> > @@ -453,16 +454,16 @@ global ASM_PFX(AsmGetTemplateAddressMap)
> >  ASM_PFX(AsmGetTemplateAddressMap):
> >
> >      lea     rax, [AsmIdtVectorBegin]
> >
> >      mov     qword [rcx], rax
> >
> > -    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> >
> > +    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> >
> >      lea     rax, [HookAfterStubHeaderBegin]
> >
> >      mov     qword [rcx + 0x10], rax
> >
> >
> >
> >  ; Fix up CommonInterruptEntry address
> >
> >      lea    rax, [ASM_PFX(CommonInterruptEntry)]
> >
> >      lea    rcx, [AsmIdtVectorBegin]
> >
> > -%rep  32
> >
> > +%rep  256
> >
> >      mov    qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)],
> > rax
> >
> > -    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
> >
> > +    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
> >
> >  %endrep
> >
> >  ; Fix up HookAfterStubHeaderEnd
> >
> >      lea    rax, [HookAfterStubHeaderEnd]
> >
> > --
> > 2.35.1.windows.2
> >
> >
> >
> > -=-=-=-=-=-=
> > Groups.io Links: You receive all messages sent to this group.
> > View/Reply Online (#89916): https://edk2.groups.io/g/devel/message/89916
> > Mute This Topic: https://groups.io/mt/91231767/1768734
> > Group Owner: devel+owner@edk2.groups.io
> > Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> > -=-=-=-=-=-=
> >


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance
  2022-05-24  8:01     ` Ni, Ray
  2022-05-24 15:18       ` Wang, Jian J
@ 2022-06-06  7:46       ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Dong, Eric @ 2022-06-06  7:46 UTC (permalink / raw)
  To: Ni, Ray, Wang, Jian J, devel@edk2.groups.io

[-- Attachment #1: Type: text/plain, Size: 8632 bytes --]

Acked-by: Eric Dong <eric.dong@intel.com>

From: Ni, Ray <ray.ni@intel.com>
Sent: Tuesday, May 24, 2022 4:02 PM
To: Wang, Jian J <jian.j.wang@intel.com>; devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@intel.com>
Subject: Re: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance

Jian,
Ia32/ExceptionHandlerAsm.nasm is used by 32bit DxeCpuExceptionHandlerLib instance.

I agree the commit message is not correct. The commit message says
SEC still creates 32 entries but 32bit SEC creates 256 entries.

I will update the commit message to align to code behavior.

Thanks,
Ray

________________________________________
From: Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
Sent: Monday, May 23, 2022 0:40
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Ni, Ray
Cc: Dong, Eric
Subject: RE: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance

Ray,

You changed "%rep 32" to "%rep 256" in Ia32/ExceptionHandlerAsm.nasm.
According to my understanding and your comments, this should be done
only to X64 code, right?

Regards,
Jian

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Ni, Ray
> Sent: Friday, May 20, 2022 10:16 PM
> To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>
> Subject: [edk2-devel] [PATCH 1/5] CpuException: Avoid allocating code pages
> for DXE instance
>
> Today the DXE instance allocates code page and then copies the IDT
> vectors to the allocated code page. Then it fixes up the vector number
> in the IDT vector.
>
> But if we update the NASM file to generate 256 IDT vectors, there is
> no need to do the copy and fix-up.
>
> A side effect is up to 4096 bytes (HOOKAFTER_STUB_SIZE * 256) is
> used for 256 IDT vectors. While 32 IDT vectors only require 512 bytes.
>
> But considering the code logic simplification, 3.5K space is not a big
> deal. SEC instance still generates 32 IDT vectors so no impact to SEC.
> If 3.5K is too much a waste in PEI phase, we can enhance the code
> further to generate 32 vectors for PEI.
>
> Signed-off-by: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Cc: Eric Dong <eric.dong@intel.com<mailto:eric.dong@intel.com>>
> ---
>  .../CpuExceptionHandlerLib/DxeException.c     | 22 -------------------
>  .../Ia32/ExceptionHandlerAsm.nasm             |  4 ++--
>  .../X64/ExceptionHandlerAsm.nasm              |  2 ++
>  .../X64/Xcode5ExceptionHandlerAsm.nasm        |  9 ++++----
>  4 files changed, 9 insertions(+), 28 deletions(-)
>
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> index 61f11e98f8..5083c4b8e8 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
> @@ -95,9 +95,6 @@ InitializeCpuInterruptHandlers (
>    IA32_DESCRIPTOR                 IdtDescriptor;
>
>    UINTN                           IdtEntryCount;
>
>    EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;
>
> -  UINTN                           Index;
>
> -  UINTN                           InterruptEntry;
>
> -  UINT8                           *InterruptEntryCode;
>
>    RESERVED_VECTORS_DATA           *ReservedVectors;
>
>    EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;
>
>
>
> @@ -138,25 +135,6 @@ InitializeCpuInterruptHandlers (
>    AsmGetTemplateAddressMap (&TemplateMap);
>
>    ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
>
>
>
> -  Status = gBS->AllocatePool (
>
> -                  EfiBootServicesCode,
>
> -                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
>
> -                  (VOID **)&InterruptEntryCode
>
> -                  );
>
> -  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
>
> -
>
> -  InterruptEntry = (UINTN)InterruptEntryCode;
>
> -  for (Index = 0; Index < CPU_INTERRUPT_NUM; Index++) {
>
> -    CopyMem (
>
> -      (VOID *)InterruptEntry,
>
> -      (VOID *)TemplateMap.ExceptionStart,
>
> -      TemplateMap.ExceptionStubHeaderSize
>
> -      );
>
> -    AsmVectorNumFixup ((VOID *)InterruptEntry, (UINT8)Index, (VOID
> *)TemplateMap.ExceptionStart);
>
> -    InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
>
> -  }
>
> -
>
> -  TemplateMap.ExceptionStart                     = (UINTN)InterruptEntryCode;
>
>    mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
>
>    mExceptionHandlerData.ReservedVectors          = ReservedVectors;
>
>    mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> index 3fe9aed1e8..8ed2b8f455 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.nas
> m
> @@ -33,7 +33,7 @@ ALIGN   8
>  ;
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
> -%rep  32
>
> +%rep  256
>
>      push    byte %[Vector];
>
>      push    eax
>
>      mov     eax, ASM_PFX(CommonInterruptEntry)
>
> @@ -439,7 +439,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
>
>
>      mov ebx, dword [ebp + 0x8]
>
>      mov dword [ebx],      AsmIdtVectorBegin
>
> -    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    mov dword [ebx + 0x4], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>      mov dword [ebx + 0x8], HookAfterStubBegin
>
>
>
>      popad
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> index 9a806d1f86..aaf8d622e6 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
> @@ -31,6 +31,8 @@ SECTION .text
>
>
>  ALIGN   8
>
>
>
> +; Generate 32 IDT vectors.
>
> +; 32 IDT vectors are enough because interrupts (32+) are not enabled in SEC and
> PEI phase.
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
>  %rep  32
>
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> index 9c72fa5815..7c0e3d3b0b 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAs
> m.nasm
> @@ -53,9 +53,10 @@ SECTION .text
>
>
>  ALIGN   8
>
>
>
> +; Generate 256 IDT vectors.
>
>  AsmIdtVectorBegin:
>
>  %assign Vector 0
>
> -%rep  32
>
> +%rep  256
>
>      push    byte %[Vector]
>
>      push    rax
>
>      mov     rax, strict qword 0 ;    mov     rax, ASM_PFX(CommonInterruptEntry)
>
> @@ -453,16 +454,16 @@ global ASM_PFX(AsmGetTemplateAddressMap)
>  ASM_PFX(AsmGetTemplateAddressMap):
>
>      lea     rax, [AsmIdtVectorBegin]
>
>      mov     qword [rcx], rax
>
> -    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    mov     qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>      lea     rax, [HookAfterStubHeaderBegin]
>
>      mov     qword [rcx + 0x10], rax
>
>
>
>  ; Fix up CommonInterruptEntry address
>
>      lea    rax, [ASM_PFX(CommonInterruptEntry)]
>
>      lea    rcx, [AsmIdtVectorBegin]
>
> -%rep  32
>
> +%rep  256
>
>      mov    qword [rcx + (JmpAbsoluteAddress - 8 - HookAfterStubHeaderBegin)],
> rax
>
> -    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
>
> +    add    rcx, (AsmIdtVectorEnd - AsmIdtVectorBegin) / 256
>
>  %endrep
>
>  ; Fix up HookAfterStubHeaderEnd
>
>      lea    rax, [HookAfterStubHeaderEnd]
>
> --
> 2.35.1.windows.2
>
>
>
> -=-=-=-=-=-=
> Groups.io Links: You receive all messages sent to this group.
> View/Reply Online (#89916): https://edk2.groups.io/g/devel/message/89916
> Mute This Topic: https://groups.io/mt/91231767/1768734
> Group Owner: devel+owner@edk2.groups.io<mailto:devel+owner@edk2.groups.io>
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
>

[-- Attachment #2: Type: text/html, Size: 29064 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/5] CpuException: Init global variables in-place
  2022-05-20 14:15 ` [PATCH 2/5] CpuException: Init global variables in-place Ni, Ray
  2022-05-22 16:32   ` [edk2-devel] " Wang, Jian J
@ 2022-06-06  7:46   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Dong, Eric @ 2022-06-06  7:46 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io

Acked-by: Eric Dong <eric.dong@intel.com>

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@intel.com>
Subject: [PATCH 2/5] CpuException: Init global variables in-place

Additionally removed two useless global variables:
"SPIN_LOCK  mDisplayMessageSpinLock" from SMM instance.
"UINTN mEnabledInterruptNum" from DXE instance.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../Library/CpuExceptionHandlerLib/DxeException.c  | 11 ++++++-----  .../Library/CpuExceptionHandlerLib/SmmException.c  | 14 ++++++--------
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index 5083c4b8e8..da5b96d6c6 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -16,9 +16,12 @@ CONST UINTN  mDoFarReturnFlag = 0;
  RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM]; EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];-UINTN                      mEnabledInterruptNum = 0;--EXCEPTION_HANDLER_DATA  mExceptionHandlerData;+EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {+  0,   // To be fixed+  0,   // To be fixed+  mReservedVectorsData,+  mExternalInterruptHandlerTable+};  UINT8  mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *                  CPU_KNOWN_GOOD_STACK_SIZE];@@ -62,8 +65,6 @@ InitializeCpuExceptionHandlers (
   IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL   ) {-  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;-  mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);   return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData); }diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index 77ee74579f..9f0af4120a 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -11,14 +11,14 @@
  CONST UINTN  mDoFarReturnFlag = 1; -//-// Spin lock for CPU information display-//-SPIN_LOCK  mDisplayMessageSpinLock;- RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM]; EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];-EXCEPTION_HANDLER_DATA     mExceptionHandlerData;+EXCEPTION_HANDLER_DATA     mExceptionHandlerData = {+  0,   // To be fixed+  0,   // To be fixed+  mReservedVectorsData,+  mExternalInterruptHandlerTable+};  /**   Common exception handler.@@ -58,8 +58,6 @@ InitializeCpuExceptionHandlers (
   IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL   ) {-  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;-  mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);   return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData); }-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/5] CpuException: Avoid allocating page but using global variables
  2022-05-20 14:15 ` [PATCH 3/5] CpuException: Avoid allocating page but using global variables Ni, Ray
  2022-05-22 16:30   ` [edk2-devel] " Wang, Jian J
@ 2022-06-06  7:47   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Dong, Eric @ 2022-06-06  7:47 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io

Acked-by: Eric Dong <eric.dong@intel.com>

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@intel.com>
Subject: [PATCH 3/5] CpuException: Avoid allocating page but using global variables

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 .../CpuExceptionHandlerLib/DxeException.c     | 24 ++++---------------
 1 file changed, 5 insertions(+), 19 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index da5b96d6c6..f139131a7c 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -14,8 +14,8 @@
 

 CONST UINTN  mDoFarReturnFlag = 0;

 

-RESERVED_VECTORS_DATA      mReservedVectorsData[CPU_EXCEPTION_NUM];

-EFI_CPU_INTERRUPT_HANDLER  mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];

+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

@@ -96,27 +96,15 @@ InitializeCpuInterruptHandlers (
   IA32_DESCRIPTOR                 IdtDescriptor;

   UINTN                           IdtEntryCount;

   EXCEPTION_HANDLER_TEMPLATE_MAP  TemplateMap;

-  RESERVED_VECTORS_DATA           *ReservedVectors;

-  EFI_CPU_INTERRUPT_HANDLER       *ExternalInterruptHandler;

-

-  Status = gBS->AllocatePool (

-                  EfiBootServicesCode,

-                  sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,

-                  (VOID **)&ReservedVectors

-                  );

-  ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);

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

+

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

   if (VectorInfo != NULL) {

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

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

     if (EFI_ERROR (Status)) {

-      FreePool (ReservedVectors);

       return EFI_INVALID_PARAMETER;

     }

   }

 

-  ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);

-  ASSERT (ExternalInterruptHandler != NULL);

-

   //

   // Read IDT descriptor and calculate IDT size

   //

@@ -137,8 +125,6 @@ InitializeCpuInterruptHandlers (
   ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);

 

   mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;

-  mExceptionHandlerData.ReservedVectors          = ReservedVectors;

-  mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;

   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);

 

   UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);

-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [edk2-devel] [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers
  2022-05-24  8:04     ` Ni, Ray
  2022-05-24 15:17       ` Wang, Jian J
@ 2022-06-06  7:47       ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Dong, Eric @ 2022-06-06  7:47 UTC (permalink / raw)
  To: Ni, Ray, Wang, Jian J, devel@edk2.groups.io

[-- Attachment #1: Type: text/plain, Size: 31322 bytes --]

Acked-by: Eric Dong eric.dong@intel.com<mailto:eric.dong@intel.com>

From: Ni, Ray <ray.ni@intel.com>
Sent: Tuesday, May 24, 2022 4:04 PM
To: Wang, Jian J <jian.j.wang@intel.com>; devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@intel.com>
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@intel.com<mailto:jian.j.wang@intel.com>>
Sent: Monday, May 23, 2022 0:27
To: devel@edk2.groups.io<mailto: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<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Ni, Ray
> Sent: Friday, May 20, 2022 10:16 PM
> To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>
> 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@intel.com<mailto:ray.ni@intel.com>>
> Cc: Eric Dong <eric.dong@intel.com<mailto:eric.dong@intel.com>>
> ---
>  .../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<mailto:devel+owner@edk2.groups.io>
> Unsubscribe: https://edk2.groups.io/g/devel/unsub [jian.j.wang@intel.com]
> -=-=-=-=-=-=
>

[-- Attachment #2: Type: text/html, Size: 106600 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks
  2022-05-20 14:15 ` [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks Ni, Ray
  2022-05-22 16:05   ` Wang, Jian J
@ 2022-06-06  7:47   ` Dong, Eric
  1 sibling, 0 replies; 20+ messages in thread
From: Dong, Eric @ 2022-06-06  7:47 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io; +Cc: Wang, Jian J

Acked-by: Eric Dong <eric.dong@intel.com>

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Friday, May 20, 2022 10:16 PM
To: devel@edk2.groups.io
Cc: Dong, Eric <eric.dong@intel.com>; Wang, Jian J <jian.j.wang@intel.com>
Subject: [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks

Today InitializeCpuExceptionHandlersEx is called from three modules:
1. DxeCore (links to DxeCpuExceptionHandlerLib)
    DxeCore expects it initializes the IDT entries as well as
    assigning separate stacks for #DF and #PF.
2. CpuMpPei (links to PeiCpuExceptionHandlerLib)
   and CpuDxe (links to DxeCpuExceptionHandlerLib)
    It's called for each thread for only assigning separate stacks for
    #DF and #PF. The IDT entries initialization is skipped because
    caller sets InitData->X64.InitDefaultHandlers to FALSE.

Additionally, SecPeiCpuExceptionHandlerLib, SmmCpuExceptionHandlerLib also implement such API and the behavior of the API is simply to initialize IDT entries only.

Because it mixes the IDT entries initialization and separate stacks assignment for certain exception handlers together, in order to know whether the function call only initializes IDT entries, or assigns stacks, we need to check:
1. value of InitData->X64.InitDefaultHandlers 2. library instance

This patch cleans up the code to separate the stack assignment to a new API:
InitializeSeparateExceptionStacks().

Only when caller calls the new API, the separate stacks are assigned.
With this change, the SecPei and Smm instance can return unsupported which gives caller a very clear status.

The old API InitializeCpuExceptionHandlersEx() is removed in this patch.
Because no platform module is consuming the old API, the impact is none.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c       |  2 +-
 .../Include/Library/CpuExceptionHandlerLib.h  | 24 ++---
 .../CpuExceptionHandlerLibNull.c              | 26 ++----
 UefiCpuPkg/CpuDxe/CpuMp.c                     |  6 +-
 UefiCpuPkg/CpuMpPei/CpuMpPei.c                |  4 +-
 .../CpuExceptionHandlerLib/DxeException.c     | 91 ++++++-------------
 .../CpuExceptionHandlerLib/PeiCpuException.c  | 51 ++---------
 .../SecPeiCpuException.c                      | 27 ++----
 .../CpuExceptionHandlerLib/SmmException.c     | 27 ++----
 9 files changed, 74 insertions(+), 184 deletions(-)

diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
index 2c27fc0695..83f49d7c00 100644
--- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
+++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
@@ -253,7 +253,7 @@ DxeMain (
     VectorInfoList = (EFI_VECTOR_HANDOFF_INFO *)(GET_GUID_HOB_DATA (GuidHob));   } -  Status = InitializeCpuExceptionHandlersEx (VectorInfoList, NULL);+  Status = InitializeCpuExceptionHandlers (VectorInfoList);   ASSERT_EFI_ERROR (Status);    //diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
index d4649bebe1..9a495081f7 100644
--- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
+++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h
@@ -103,32 +103,20 @@ InitializeCpuExceptionHandlers (
   );  /**-  Initializes all CPU exceptions entries with optional extra initializations.+  Setup separate stacks for certain exception handlers. -  By default, this method should include all functionalities implemented by-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.-  This could be done by calling InitializeCpuExceptionHandlers() directly-  in this method besides the extra works.+  InitData is optional and processor arch dependent. -  InitData is optional and its use and content are processor arch dependent.-  The typical usage of it is to convey resources which have to be reserved-  elsewhere and are necessary for the extra initializations of exception.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data optional for extra initializations-                            of exception.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.   @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ); diff --git a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
index 54f38788fe..8aeedcb4d1 100644
--- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.c
+++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandle
+++ rLibNull.c
@@ -82,34 +82,22 @@ DumpCpuContext (
 }  /**-  Initializes all CPU exceptions entries with optional extra initializations.+  Setup separate stacks for certain exception handlers. -  By default, this method should include all functionalities implemented by-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.-  This could be done by calling InitializeCpuExceptionHandlers() directly-  in this method besides the extra works.+  InitData is optional and processor arch dependent. -  InitData is optional and its use and content are processor arch dependent.-  The typical usage of it is to convey resources which have to be reserved-  elsewhere and are necessary for the extra initializations of exception.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data optional for extra initializations-                            of exception.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.   @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ) {-  return InitializeCpuExceptionHandlers (VectorInfo);+  return EFI_UNSUPPORTED; }diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c
index 1f218367b3..e385f585c7 100644
--- a/UefiCpuPkg/CpuDxe/CpuMp.c
+++ b/UefiCpuPkg/CpuDxe/CpuMp.c
@@ -1,7 +1,7 @@
 /** @file   CPU DXE Module to produce CPU MP Protocol. -  Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>+  Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>   SPDX-License-Identifier: BSD-2-Clause-Patent  **/@@ -617,7 +617,7 @@ GetGdtr (
 /**   Initializes CPU exceptions handlers for the sake of stack switch requirement. -  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly+  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly   for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.    @param[in,out] Buffer  The pointer to private data buffer.@@ -641,7 +641,7 @@ InitializeExceptionStackSwitchHandlers (
   AsmReadIdtr (&Idtr);   EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;   EssData->Ia32.IdtTableSize = Idtr.Limit + 1;-  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);+  Status                     = InitializeSeparateExceptionStacks (EssData);   ASSERT_EFI_ERROR (Status); } diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
index 1e68c91d95..d4786979fa 100644
--- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
+++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
@@ -432,7 +432,7 @@ GetGdtr (
 /**   Initializes CPU exceptions handlers for the sake of stack switch requirement. -  This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly+  This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly   for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.    @param[in,out] Buffer  The pointer to private data buffer.@@ -456,7 +456,7 @@ InitializeExceptionStackSwitchHandlers (
   AsmReadIdtr (&Idtr);   EssData->Ia32.IdtTable     = (VOID *)Idtr.Base;   EssData->Ia32.IdtTableSize = Idtr.Limit + 1;-  Status                     = InitializeCpuExceptionHandlersEx (NULL, EssData);+  Status                     = InitializeSeparateExceptionStacks (EssData);   ASSERT_EFI_ERROR (Status); } diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
index c7c1fe31d2..e62bb5e6c0 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
@@ -103,82 +103,49 @@ RegisterCpuInterruptHandler (
 }  /**-  Initializes CPU exceptions entries and setup stack switch for given exceptions.+  Setup separate stacks for certain exception handlers. -  This method will call InitializeCpuExceptionHandlers() to setup default-  exception handlers unless indicated not to do it explicitly.+  InitData is optional and processor arch dependent. -  If InitData is passed with NULL, this method will use the resource reserved-  by global variables to initialize it; Otherwise it will use data in InitData-  to setup stack switch. This is for the different use cases in DxeCore and-  Cpu MP exception initialization.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data required to setup stack switch for-                            given exceptions.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.+  @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ) {-  EFI_STATUS               Status;   CPU_EXCEPTION_INIT_DATA  EssData;   IA32_DESCRIPTOR          Idtr;   IA32_DESCRIPTOR          Gdtr; -  //-  // To avoid repeat initialization of default handlers, the caller should pass-  // an extended init data with InitDefaultHandlers set to FALSE. There's no-  // need to call this method to just initialize default handlers. Call non-ex-  // version instead; or this method must be implemented as a simple wrapper of-  // non-ex version of it, if this version has to be called.-  //-  if ((InitData == NULL) || InitData->X64.InitDefaultHandlers) {-    Status = InitializeCpuExceptionHandlers (VectorInfo);-  } else {-    Status = EFI_SUCCESS;-  }--  if (!EFI_ERROR (Status)) {-    //-    // Initializing stack switch is only necessary for Stack Guard functionality.-    //-    if (PcdGetBool (PcdCpuStackGuard)) {-      if (InitData == NULL) {-        SetMem (mNewGdt, sizeof (mNewGdt), 0);--        AsmReadIdtr (&Idtr);-        AsmReadGdtr (&Gdtr);--        EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;-        EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof (mNewStack);-        EssData.X64.KnownGoodStackSize         = CPU_KNOWN_GOOD_STACK_SIZE;-        EssData.X64.StackSwitchExceptions      = CPU_STACK_SWITCH_EXCEPTION_LIST;-        EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;-        EssData.X64.IdtTable                   = (VOID *)Idtr.Base;-        EssData.X64.IdtTableSize               = Idtr.Limit + 1;-        EssData.X64.GdtTable                   = mNewGdt;-        EssData.X64.GdtTableSize               = sizeof (mNewGdt);-        EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;-        EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;-        EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;-        EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;--        InitData = &EssData;-      }--      Status = ArchSetupExceptionStack (InitData);-    }+  if (InitData == NULL) {+    SetMem (mNewGdt, sizeof (mNewGdt), 0);++    AsmReadIdtr (&Idtr);+    AsmReadGdtr (&Gdtr);++    EssData.X64.Revision                   = CPU_EXCEPTION_INIT_DATA_REV;+    EssData.X64.KnownGoodStackTop          = (UINTN)mNewStack + sizeof (mNewStack);+    EssData.X64.KnownGoodStackSize         = CPU_KNOWN_GOOD_STACK_SIZE;+    EssData.X64.StackSwitchExceptions      = CPU_STACK_SWITCH_EXCEPTION_LIST;+    EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;+    EssData.X64.IdtTable                   = (VOID *)Idtr.Base;+    EssData.X64.IdtTableSize               = Idtr.Limit + 1;+    EssData.X64.GdtTable                   = mNewGdt;+    EssData.X64.GdtTableSize               = sizeof (mNewGdt);+    EssData.X64.ExceptionTssDesc           = mNewGdt + Gdtr.Limit + 1;+    EssData.X64.ExceptionTssDescSize       = CPU_TSS_DESC_SIZE;+    EssData.X64.ExceptionTss               = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;+    EssData.X64.ExceptionTssSize           = CPU_TSS_SIZE;++    InitData = &EssData;   } -  return Status;+  return ArchSetupExceptionStack (InitData); }diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
index 1ae611c75e..494c2ab433 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
@@ -150,57 +150,26 @@ InitializeCpuExceptionHandlers (
 }  /**-  Initializes all CPU exceptions entries with optional extra initializations.+  Setup separate stacks for certain exception handlers. -  By default, this method should include all functionalities implemented by-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.-  This could be done by calling InitializeCpuExceptionHandlers() directly-  in this method besides the extra works.+  InitData is optional and processor arch dependent. -  InitData is optional and its use and content are processor arch dependent.-  The typical usage of it is to convey resources which have to be reserved-  elsewhere and are necessary for the extra initializations of exception.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data optional for extra initializations-                            of exception.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.+  @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ) {-  EFI_STATUS  Status;--  //-  // To avoid repeat initialization of default handlers, the caller should pass-  // an extended init data with InitDefaultHandlers set to FALSE. There's no-  // need to call this method to just initialize default handlers. Call non-ex-  // version instead; or this method must be implemented as a simple wrapper of-  // non-ex version of it, if this version has to be called.-  //-  if ((InitData == NULL) || InitData->Ia32.InitDefaultHandlers) {-    Status = InitializeCpuExceptionHandlers (VectorInfo);-  } else {-    Status = EFI_SUCCESS;-  }--  if (!EFI_ERROR (Status)) {-    //-    // Initializing stack switch is only necessary for Stack Guard functionality.-    //-    if (PcdGetBool (PcdCpuStackGuard) && (InitData != NULL)) {-      Status = ArchSetupExceptionStack (InitData);-    }+  if (InitData == NULL) {+    return EFI_UNSUPPORTED;   } -  return Status;+  return ArchSetupExceptionStack (InitData); }diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
index e894ead612..4313cc5582 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
@@ -200,33 +200,22 @@ RegisterCpuInterruptHandler (
 }  /**-  Initializes all CPU exceptions entries with optional extra initializations.+  Setup separate stacks for certain exception handlers. -  By default, this method should include all functionalities implemented by-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.-  This could be done by calling InitializeCpuExceptionHandlers() directly-  in this method besides the extra works.+  InitData is optional and processor arch dependent. -  InitData is optional and its use and content are processor arch dependent.-  The typical usage of it is to convey resources which have to be reserved-  elsewhere and are necessary for the extra initializations of exception.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data optional for extra initializations-                            of exception.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.+  @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ) {-  return InitializeCpuExceptionHandlers (VectorInfo);+  return EFI_UNSUPPORTED; }diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
index ec643556c7..1c97dab926 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c
@@ -96,33 +96,22 @@ RegisterCpuInterruptHandler (
 }  /**-  Initializes all CPU exceptions entries with optional extra initializations.+  Setup separate stacks for certain exception handlers. -  By default, this method should include all functionalities implemented by-  InitializeCpuExceptionHandlers(), plus extra initialization works, if any.-  This could be done by calling InitializeCpuExceptionHandlers() directly-  in this method besides the extra works.+  InitData is optional and processor arch dependent. -  InitData is optional and its use and content are processor arch dependent.-  The typical usage of it is to convey resources which have to be reserved-  elsewhere and are necessary for the extra initializations of exception.+  @param[in]  InitData      Pointer to data optional for information about how+                            to assign stacks for certain exception handlers. -  @param[in]  VectorInfo    Pointer to reserved vector list.-  @param[in]  InitData      Pointer to data optional for extra initializations-                            of exception.--  @retval EFI_SUCCESS             The exceptions have been successfully-                                  initialized.-  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid-                                  content.+  @retval EFI_SUCCESS             The stacks are assigned successfully.+  @retval EFI_UNSUPPORTED         This function is not supported.  **/ EFI_STATUS EFIAPI-InitializeCpuExceptionHandlersEx (-  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL,+InitializeSeparateExceptionStacks (   IN CPU_EXCEPTION_INIT_DATA  *InitData OPTIONAL   ) {-  return InitializeCpuExceptionHandlers (VectorInfo);+  return EFI_UNSUPPORTED; }-- 
2.35.1.windows.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-06-06  7:47 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-20 14:15 [PATCH 0/5] Code refactoring in CpuExceptionHandlerLib Ni, Ray
2022-05-20 14:15 ` [PATCH 1/5] CpuException: Avoid allocating code pages for DXE instance Ni, Ray
2022-05-22 16:40   ` [edk2-devel] " Wang, Jian J
2022-05-24  8:01     ` Ni, Ray
2022-05-24 15:18       ` Wang, Jian J
2022-06-06  7:46       ` Dong, Eric
2022-05-20 14:15 ` [PATCH 2/5] CpuException: Init global variables in-place Ni, Ray
2022-05-22 16:32   ` [edk2-devel] " Wang, Jian J
2022-06-06  7:46   ` Dong, Eric
2022-05-20 14:15 ` [PATCH 3/5] CpuException: Avoid allocating page but using global variables Ni, Ray
2022-05-22 16:30   ` [edk2-devel] " Wang, Jian J
2022-06-06  7:47   ` Dong, Eric
2022-05-20 14:15 ` [PATCH 4/5] CpuException: Remove InitializeCpuInterruptHandlers Ni, Ray
2022-05-22 16:27   ` [edk2-devel] " Wang, Jian J
2022-05-24  8:04     ` Ni, Ray
2022-05-24 15:17       ` Wang, Jian J
2022-06-06  7:47       ` Dong, Eric
2022-05-20 14:15 ` [PATCH 5/5] CpuException: Add InitializeSeparateExceptionStacks Ni, Ray
2022-05-22 16:05   ` Wang, Jian J
2022-06-06  7:47   ` Dong, Eric

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox