2 CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "CpuExceptionCommon.h"
10 #include <Library/DebugLib.h>
13 Internal worker function for common exception handler.
15 @param ExceptionType Exception type.
16 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
17 @param ExceptionHandlerData Pointer to exception handler data.
20 CommonExceptionHandlerWorker (
21 IN EFI_EXCEPTION_TYPE ExceptionType
,
22 IN EFI_SYSTEM_CONTEXT SystemContext
,
23 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
26 EXCEPTION_HANDLER_CONTEXT
*ExceptionHandlerContext
;
27 RESERVED_VECTORS_DATA
*ReservedVectors
;
28 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
30 ExceptionHandlerContext
= (EXCEPTION_HANDLER_CONTEXT
*) (UINTN
) (SystemContext
.SystemContextIa32
);
31 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
32 ExternalInterruptHandler
= ExceptionHandlerData
->ExternalInterruptHandler
;
34 switch (ReservedVectors
[ExceptionType
].Attribute
) {
35 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
37 // The new exception handler registered by RegisterCpuInterruptHandler() is executed BEFORE original handler.
38 // Save the original handler to stack so the assembly code can jump to it instead of returning from handler.
40 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
41 ExceptionHandlerContext
->OldIdtHandler
= ReservedVectors
[ExceptionType
].ExceptonHandler
;
43 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
46 // If spin-lock can be acquired, it's the first time entering here.
48 if (AcquireSpinLockOrFail (&ReservedVectors
[ExceptionType
].SpinLock
)) {
50 // The new exception handler registered by RegisterCpuInterruptHandler() is executed AFTER original handler.
51 // Save the original handler to stack but skip running the new handler so the original handler is executed
54 ReservedVectors
[ExceptionType
].ApicId
= GetApicId ();
55 ArchSaveExceptionContext (ExceptionType
, SystemContext
, ExceptionHandlerData
);
56 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
57 ExceptionHandlerContext
->OldIdtHandler
= ReservedVectors
[ExceptionType
].ExceptonHandler
;
61 // If spin-lock cannot be acquired, it's the second time entering here.
62 // 'break' instead of 'return' is used so the new exception handler can be executed.
64 if (ReservedVectors
[ExceptionType
].ApicId
== GetApicId ()) {
66 // Old IDT handler has been executed, then restore CPU exception content to
67 // run new exception handler.
69 ArchRestoreExceptionContext (ExceptionType
, SystemContext
, ExceptionHandlerData
);
71 // Release spin lock for ApicId
73 ReleaseSpinLock (&ReservedVectors
[ExceptionType
].SpinLock
);
83 // It should never reach here
89 if (ExternalInterruptHandler
!= NULL
&&
90 ExternalInterruptHandler
[ExceptionType
] != NULL
) {
91 (ExternalInterruptHandler
[ExceptionType
]) (ExceptionType
, SystemContext
);
92 } else if (ExceptionType
< CPU_EXCEPTION_NUM
) {
94 // Get Spinlock to display CPU information
96 while (!AcquireSpinLockOrFail (&ExceptionHandlerData
->DisplayMessageSpinLock
)) {
100 // Initialize the serial port before dumping.
102 SerialPortInitialize ();
104 // Display ExceptionType, CPU information and Image information
106 DumpImageAndCpuContent (ExceptionType
, SystemContext
);
108 // Release Spinlock of output message
110 ReleaseSpinLock (&ExceptionHandlerData
->DisplayMessageSpinLock
);
112 // Enter a dead loop if needn't to execute old IDT handler further
114 if (ReservedVectors
[ExceptionType
].Attribute
!= EFI_VECTOR_HANDOFF_HOOK_BEFORE
) {
121 Internal worker function to update IDT entries accordling to vector attributes.
123 @param[in] IdtTable Pointer to IDT table.
124 @param[in] TemplateMap Pointer to a buffer where the address map is
126 @param[in] ExceptionHandlerData Pointer to exception handler data.
131 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
132 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
133 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
138 UINTN InterruptHandler
;
139 RESERVED_VECTORS_DATA
*ReservedVectors
;
141 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
143 // Use current CS as the segment selector of interrupt gate in IDT
145 CodeSegment
= AsmReadCs ();
147 for (Index
= 0; Index
< ExceptionHandlerData
->IdtEntryCount
; Index
++) {
148 IdtTable
[Index
].Bits
.Selector
= CodeSegment
;
150 // Check reserved vectors attributes
152 switch (ReservedVectors
[Index
].Attribute
) {
153 case EFI_VECTOR_HANDOFF_DO_NOT_HOOK
:
155 // Keep original IDT entry
158 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
159 InitializeSpinLock (&ReservedVectors
[Index
].SpinLock
);
161 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
162 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
,
163 TemplateMap
->ExceptionStubHeaderSize
166 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
168 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
171 // Go on the following code
173 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
175 // Save original IDT handler address
177 ReservedVectors
[Index
].ExceptonHandler
= ArchGetIdtHandler (&IdtTable
[Index
]);
179 // Go on the following code
183 // Update new IDT entry
185 InterruptHandler
= TemplateMap
->ExceptionStart
+ Index
* TemplateMap
->ExceptionStubHeaderSize
;
186 ArchUpdateIdtEntry (&IdtTable
[Index
], InterruptHandler
);
193 Internal worker function to initialize exception handler.
195 @param[in] VectorInfo Pointer to reserved vector list.
196 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
198 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
199 with default exception handlers.
200 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
201 @retval EFI_UNSUPPORTED This function is not supported.
205 InitializeCpuExceptionHandlersWorker (
206 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
207 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
211 IA32_DESCRIPTOR IdtDescriptor
;
213 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
214 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
215 RESERVED_VECTORS_DATA
*ReservedVectors
;
217 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
218 SetMem ((VOID
*) ReservedVectors
, sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
, 0xff);
219 if (VectorInfo
!= NULL
) {
220 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectors
, CPU_EXCEPTION_NUM
);
221 if (EFI_ERROR (Status
)) {
222 return EFI_INVALID_PARAMETER
;
227 // Read IDT descriptor and calculate IDT size
229 AsmReadIdtr (&IdtDescriptor
);
230 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
231 if (IdtEntryCount
> CPU_EXCEPTION_NUM
) {
233 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
235 IdtEntryCount
= CPU_EXCEPTION_NUM
;
238 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
239 AsmGetTemplateAddressMap (&TemplateMap
);
240 ASSERT (TemplateMap
.ExceptionStubHeaderSize
<= HOOKAFTER_STUB_SIZE
);
242 ExceptionHandlerData
->IdtEntryCount
= IdtEntryCount
;
243 UpdateIdtTable (IdtTable
, &TemplateMap
, ExceptionHandlerData
);
249 Registers a function to be called from the processor interrupt handler.
251 @param[in] InterruptType Defines which interrupt or exception to hook.
252 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
253 when a processor interrupt occurs. If this parameter is NULL, then the handler
255 @param[in] ExceptionHandlerData Pointer to exception handler data.
257 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
258 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
259 previously installed.
260 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
261 previously installed.
262 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
263 or this function is not supported.
266 RegisterCpuInterruptHandlerWorker (
267 IN EFI_EXCEPTION_TYPE InterruptType
,
268 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
269 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
272 UINTN EnabledInterruptNum
;
273 RESERVED_VECTORS_DATA
*ReservedVectors
;
274 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
276 EnabledInterruptNum
= ExceptionHandlerData
->IdtEntryCount
;
277 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
278 ExternalInterruptHandler
= ExceptionHandlerData
->ExternalInterruptHandler
;
280 if (InterruptType
< 0 || InterruptType
>= (EFI_EXCEPTION_TYPE
)EnabledInterruptNum
||
281 ReservedVectors
[InterruptType
].Attribute
== EFI_VECTOR_HANDOFF_DO_NOT_HOOK
) {
282 return EFI_UNSUPPORTED
;
285 if (InterruptHandler
== NULL
&& ExternalInterruptHandler
[InterruptType
] == NULL
) {
286 return EFI_INVALID_PARAMETER
;
289 if (InterruptHandler
!= NULL
&& ExternalInterruptHandler
[InterruptType
] != NULL
) {
290 return EFI_ALREADY_STARTED
;
293 ExternalInterruptHandler
[InterruptType
] = InterruptHandler
;