/** @file\r
CPU exception handler library implemenation for DXE modules.\r
\r
- Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r
- This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+ Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
\r
**/\r
\r
#include <Library/MemoryAllocationLib.h>\r
#include <Library/UefiBootServicesTableLib.h>\r
\r
-CONST UINTN mDoFarReturnFlag = 0;\r
-\r
-RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];\r
-EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];\r
-UINTN mEnabledInterruptNum = 0;\r
+CONST UINTN mDoFarReturnFlag = 0;\r
\r
-EXCEPTION_HANDLER_DATA mExceptionHandlerData;\r
+RESERVED_VECTORS_DATA mReservedVectorsData[CPU_INTERRUPT_NUM];\r
+EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];\r
+EXCEPTION_HANDLER_DATA mExceptionHandlerData = {\r
+ CPU_INTERRUPT_NUM,\r
+ 0, // To be fixed\r
+ mReservedVectorsData,\r
+ mExternalInterruptHandlerTable\r
+};\r
\r
-UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *\r
- CPU_KNOWN_GOOD_STACK_SIZE];\r
-UINT8 mNewGdt[CPU_TSS_GDT_SIZE];\r
+UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *\r
+ CPU_KNOWN_GOOD_STACK_SIZE];\r
+UINT8 mNewGdt[CPU_TSS_GDT_SIZE];\r
\r
/**\r
Common exception handler.\r
VOID\r
EFIAPI\r
CommonExceptionHandler (\r
- IN EFI_EXCEPTION_TYPE ExceptionType,\r
- IN EFI_SYSTEM_CONTEXT SystemContext\r
+ IN EFI_EXCEPTION_TYPE ExceptionType,\r
+ IN EFI_SYSTEM_CONTEXT SystemContext\r
)\r
{\r
CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);\r
EFI_STATUS\r
EFIAPI\r
InitializeCpuExceptionHandlers (\r
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
+ IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
)\r
{\r
- mExceptionHandlerData.ReservedVectors = mReservedVectorsData;\r
- mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;\r
InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);\r
}\r
\r
-/**\r
- Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r
-\r
- Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
- persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
- If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
- If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
-\r
- @param[in] VectorInfo Pointer to reserved vector list.\r
-\r
- @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r
- with default interrupt/exception handlers.\r
- @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
- @retval EFI_UNSUPPORTED This function is not supported.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeCpuInterruptHandlers (\r
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
- )\r
-{\r
- EFI_STATUS Status;\r
- IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r
- IA32_DESCRIPTOR IdtDescriptor;\r
- UINTN IdtEntryCount;\r
- EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;\r
- UINTN Index;\r
- UINTN InterruptEntry;\r
- UINT8 *InterruptEntryCode;\r
- RESERVED_VECTORS_DATA *ReservedVectors;\r
- EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;\r
-\r
- Status = gBS->AllocatePool (\r
- EfiBootServicesCode,\r
- sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,\r
- (VOID **)&ReservedVectors\r
- );\r
- ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);\r
- SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r
- if (VectorInfo != NULL) {\r
- Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);\r
- if (EFI_ERROR (Status)) {\r
- FreePool (ReservedVectors);\r
- return EFI_INVALID_PARAMETER;\r
- }\r
- }\r
-\r
- ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r
- ASSERT (ExternalInterruptHandler != NULL);\r
-\r
- //\r
- // Read IDT descriptor and calculate IDT size\r
- //\r
- AsmReadIdtr (&IdtDescriptor);\r
- IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);\r
- if (IdtEntryCount > CPU_INTERRUPT_NUM) {\r
- IdtEntryCount = CPU_INTERRUPT_NUM;\r
- }\r
- //\r
- // Create Interrupt Descriptor Table and Copy the old IDT table in\r
- //\r
- IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);\r
- ASSERT (IdtTable != NULL);\r
- CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);\r
-\r
- AsmGetTemplateAddressMap (&TemplateMap);\r
- ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);\r
-\r
- Status = gBS->AllocatePool (\r
- EfiBootServicesCode,\r
- TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,\r
- (VOID **)&InterruptEntryCode\r
- );\r
- ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);\r
-\r
- InterruptEntry = (UINTN) InterruptEntryCode;\r
- for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {\r
- CopyMem (\r
- (VOID *) InterruptEntry,\r
- (VOID *) TemplateMap.ExceptionStart,\r
- TemplateMap.ExceptionStubHeaderSize\r
- );\r
- AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);\r
- InterruptEntry += TemplateMap.ExceptionStubHeaderSize;\r
- }\r
-\r
- TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;\r
- mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;\r
- mExceptionHandlerData.ReservedVectors = ReservedVectors;\r
- mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;\r
- InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
-\r
- UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);\r
-\r
- //\r
- // Load Interrupt Descriptor Table\r
- //\r
- IdtDescriptor.Base = (UINTN) IdtTable;\r
- IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);\r
- AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
/**\r
Registers a function to be called from the processor interrupt handler.\r
\r
interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
The installed handler is called once for each processor interrupt or exception.\r
- NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
- InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
+ NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,\r
+ otherwise EFI_UNSUPPORTED returned.\r
\r
@param[in] InterruptType Defines which interrupt or exception to hook.\r
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
EFI_STATUS\r
EFIAPI\r
RegisterCpuInterruptHandler (\r
- IN EFI_EXCEPTION_TYPE InterruptType,\r
- IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
+ IN EFI_EXCEPTION_TYPE InterruptType,\r
+ IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
)\r
{\r
return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);\r
}\r
\r
/**\r
- Initializes CPU exceptions entries and setup stack switch for given exceptions.\r
+ Setup separate stacks for certain exception handlers.\r
\r
- This method will call InitializeCpuExceptionHandlers() to setup default\r
- exception handlers unless indicated not to do it explicitly.\r
+ InitData is optional and processor arch dependent.\r
\r
- If InitData is passed with NULL, this method will use the resource reserved\r
- by global variables to initialize it; Otherwise it will use data in InitData\r
- to setup stack switch. This is for the different use cases in DxeCore and\r
- Cpu MP exception initialization.\r
+ @param[in] InitData Pointer to data optional for information about how\r
+ to assign stacks for certain exception handlers.\r
\r
- @param[in] VectorInfo Pointer to reserved vector list.\r
- @param[in] InitData Pointer to data required to setup stack switch for\r
- given exceptions.\r
-\r
- @retval EFI_SUCCESS The exceptions have been successfully\r
- initialized.\r
- @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r
- content.\r
+ @retval EFI_SUCCESS The stacks are assigned successfully.\r
+ @retval EFI_UNSUPPORTED This function is not supported.\r
\r
**/\r
EFI_STATUS\r
EFIAPI\r
-InitializeCpuExceptionHandlersEx (\r
- IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r
- IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
+InitializeSeparateExceptionStacks (\r
+ IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
)\r
{\r
- EFI_STATUS Status;\r
- CPU_EXCEPTION_INIT_DATA EssData;\r
- IA32_DESCRIPTOR Idtr;\r
- IA32_DESCRIPTOR Gdtr;\r
-\r
- //\r
- // To avoid repeat initialization of default handlers, the caller should pass\r
- // an extended init data with InitDefaultHandlers set to FALSE. There's no\r
- // need to call this method to just initialize default handlers. Call non-ex\r
- // version instead; or this method must be implemented as a simple wrapper of\r
- // non-ex version of it, if this version has to be called.\r
- //\r
- if (InitData == NULL || InitData->X64.InitDefaultHandlers) {\r
- Status = InitializeCpuExceptionHandlers (VectorInfo);\r
- } else {\r
- Status = EFI_SUCCESS;\r
- }\r
-\r
- if (!EFI_ERROR (Status)) {\r
- //\r
- // Initializing stack switch is only necessary for Stack Guard functionality.\r
- //\r
- if (PcdGetBool (PcdCpuStackGuard)) {\r
- if (InitData == NULL) {\r
- SetMem (mNewGdt, sizeof (mNewGdt), 0);\r
-\r
- AsmReadIdtr (&Idtr);\r
- AsmReadGdtr (&Gdtr);\r
-\r
- EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
- EssData.X64.KnownGoodStackTop = (UINTN)mNewStack;\r
- EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;\r
- EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;\r
- EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;\r
- EssData.X64.IdtTable = (VOID *)Idtr.Base;\r
- EssData.X64.IdtTableSize = Idtr.Limit + 1;\r
- EssData.X64.GdtTable = mNewGdt;\r
- EssData.X64.GdtTableSize = sizeof (mNewGdt);\r
- EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;\r
- EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;\r
- EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;\r
- EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;\r
-\r
- InitData = &EssData;\r
- }\r
- Status = ArchSetupExcpetionStack (InitData);\r
- }\r
+ CPU_EXCEPTION_INIT_DATA EssData;\r
+ IA32_DESCRIPTOR Idtr;\r
+ IA32_DESCRIPTOR Gdtr;\r
+\r
+ if (InitData == NULL) {\r
+ SetMem (mNewGdt, sizeof (mNewGdt), 0);\r
+\r
+ AsmReadIdtr (&Idtr);\r
+ AsmReadGdtr (&Gdtr);\r
+\r
+ EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
+ EssData.X64.KnownGoodStackTop = (UINTN)mNewStack + sizeof (mNewStack);\r
+ EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;\r
+ EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;\r
+ EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;\r
+ EssData.X64.IdtTable = (VOID *)Idtr.Base;\r
+ EssData.X64.IdtTableSize = Idtr.Limit + 1;\r
+ EssData.X64.GdtTable = mNewGdt;\r
+ EssData.X64.GdtTableSize = sizeof (mNewGdt);\r
+ EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;\r
+ EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;\r
+ EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;\r
+ EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;\r
+\r
+ InitData = &EssData;\r
}\r
\r
- return Status;\r
+ return ArchSetupExceptionStack (InitData);\r
}\r