2 CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "CpuExceptionCommon.h"
16 #include <Library/DebugLib.h>
19 Internal worker function for common exception handler.
21 @param ExceptionType Exception type.
22 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
23 @param ExceptionHandlerData Pointer to exception handler data.
26 CommonExceptionHandlerWorker (
27 IN EFI_EXCEPTION_TYPE ExceptionType
,
28 IN EFI_SYSTEM_CONTEXT SystemContext
,
29 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
32 EXCEPTION_HANDLER_CONTEXT
*ExceptionHandlerContext
;
33 RESERVED_VECTORS_DATA
*ReservedVectors
;
34 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
36 ExceptionHandlerContext
= (EXCEPTION_HANDLER_CONTEXT
*) (UINTN
) (SystemContext
.SystemContextIa32
);
37 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
38 ExternalInterruptHandler
= ExceptionHandlerData
->ExternalInterruptHandler
;
40 switch (ReservedVectors
[ExceptionType
].Attribute
) {
41 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
43 // Need to jmp to old IDT handler after this exception handler
45 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
46 ExceptionHandlerContext
->OldIdtHandler
= ReservedVectors
[ExceptionType
].ExceptonHandler
;
48 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
51 // If if anyone has gotten SPIN_LOCK for owner running hook after
53 if (AcquireSpinLockOrFail (&ReservedVectors
[ExceptionType
].SpinLock
)) {
55 // Need to execute old IDT handler before running this exception handler
57 ReservedVectors
[ExceptionType
].ApicId
= GetApicId ();
58 ArchSaveExceptionContext (ExceptionType
, SystemContext
, ExceptionHandlerData
);
59 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
60 ExceptionHandlerContext
->OldIdtHandler
= ReservedVectors
[ExceptionType
].ExceptonHandler
;
64 // If failed to acquire SPIN_LOCK, check if it was locked by processor itself
66 if (ReservedVectors
[ExceptionType
].ApicId
== GetApicId ()) {
68 // Old IDT handler has been executed, then restore CPU exception content to
69 // run new exception handler.
71 ArchRestoreExceptionContext (ExceptionType
, SystemContext
, ExceptionHandlerData
);
73 // Rlease spin lock for ApicId
75 ReleaseSpinLock (&ReservedVectors
[ExceptionType
].SpinLock
);
85 // It should never reach here
91 if (ExternalInterruptHandler
!= NULL
&&
92 ExternalInterruptHandler
[ExceptionType
] != NULL
) {
93 (ExternalInterruptHandler
[ExceptionType
]) (ExceptionType
, SystemContext
);
94 } else if (ExceptionType
< CPU_EXCEPTION_NUM
) {
96 // Get Spinlock to display CPU information
98 while (!AcquireSpinLockOrFail (&ExceptionHandlerData
->DisplayMessageSpinLock
)) {
102 // Initialize the serial port before dumping.
104 SerialPortInitialize ();
106 // Display ExceptionType, CPU information and Image information
108 DumpImageAndCpuContent (ExceptionType
, SystemContext
);
110 // Release Spinlock of output message
112 ReleaseSpinLock (&ExceptionHandlerData
->DisplayMessageSpinLock
);
114 // Enter a dead loop if needn't to execute old IDT handler further
116 if (ReservedVectors
[ExceptionType
].Attribute
!= EFI_VECTOR_HANDOFF_HOOK_BEFORE
) {
123 Internal worker function to update IDT entries accordling to vector attributes.
125 @param[in] IdtTable Pointer to IDT table.
126 @param[in] TemplateMap Pointer to a buffer where the address map is
128 @param[in] ExceptionHandlerData Pointer to exception handler data.
133 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
134 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
135 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
140 UINTN InterruptHandler
;
141 RESERVED_VECTORS_DATA
*ReservedVectors
;
143 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
145 // Use current CS as the segment selector of interrupt gate in IDT
147 CodeSegment
= AsmReadCs ();
149 for (Index
= 0; Index
< ExceptionHandlerData
->IdtEntryCount
; Index
++) {
150 IdtTable
[Index
].Bits
.Selector
= CodeSegment
;
152 // Check reserved vectors attributes
154 switch (ReservedVectors
[Index
].Attribute
) {
155 case EFI_VECTOR_HANDOFF_DO_NOT_HOOK
:
157 // Keep original IDT entry
160 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
161 InitializeSpinLock (&ReservedVectors
[Index
].SpinLock
);
163 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
164 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
,
165 TemplateMap
->ExceptionStubHeaderSize
168 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
170 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
173 // Go on the following code
175 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
177 // Save original IDT handler address
179 ReservedVectors
[Index
].ExceptonHandler
= ArchGetIdtHandler (&IdtTable
[Index
]);
181 // Go on the following code
185 // Update new IDT entry
187 InterruptHandler
= TemplateMap
->ExceptionStart
+ Index
* TemplateMap
->ExceptionStubHeaderSize
;
188 ArchUpdateIdtEntry (&IdtTable
[Index
], InterruptHandler
);
195 Internal worker function to initialize exception handler.
197 @param[in] VectorInfo Pointer to reserved vector list.
198 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
200 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
201 with default exception handlers.
202 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
203 @retval EFI_UNSUPPORTED This function is not supported.
207 InitializeCpuExceptionHandlersWorker (
208 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
209 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
213 IA32_DESCRIPTOR IdtDescriptor
;
215 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
216 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
217 RESERVED_VECTORS_DATA
*ReservedVectors
;
219 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
220 SetMem ((VOID
*) ReservedVectors
, sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
, 0xff);
221 if (VectorInfo
!= NULL
) {
222 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectors
, CPU_EXCEPTION_NUM
);
223 if (EFI_ERROR (Status
)) {
224 return EFI_INVALID_PARAMETER
;
229 // Read IDT descriptor and calculate IDT size
231 AsmReadIdtr (&IdtDescriptor
);
232 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
233 if (IdtEntryCount
> CPU_EXCEPTION_NUM
) {
235 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
237 IdtEntryCount
= CPU_EXCEPTION_NUM
;
240 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
241 AsmGetTemplateAddressMap (&TemplateMap
);
242 ASSERT (TemplateMap
.ExceptionStubHeaderSize
<= HOOKAFTER_STUB_SIZE
);
244 ExceptionHandlerData
->IdtEntryCount
= IdtEntryCount
;
245 UpdateIdtTable (IdtTable
, &TemplateMap
, ExceptionHandlerData
);
251 Registers a function to be called from the processor interrupt handler.
253 @param[in] InterruptType Defines which interrupt or exception to hook.
254 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
255 when a processor interrupt occurs. If this parameter is NULL, then the handler
257 @param[in] ExceptionHandlerData Pointer to exception handler data.
259 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
260 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
261 previously installed.
262 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
263 previously installed.
264 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
265 or this function is not supported.
268 RegisterCpuInterruptHandlerWorker (
269 IN EFI_EXCEPTION_TYPE InterruptType
,
270 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
271 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
274 UINTN EnabledInterruptNum
;
275 RESERVED_VECTORS_DATA
*ReservedVectors
;
276 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
278 EnabledInterruptNum
= ExceptionHandlerData
->IdtEntryCount
;
279 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
280 ExternalInterruptHandler
= ExceptionHandlerData
->ExternalInterruptHandler
;
282 if (InterruptType
< 0 || InterruptType
>= (EFI_EXCEPTION_TYPE
)EnabledInterruptNum
||
283 ReservedVectors
[InterruptType
].Attribute
== EFI_VECTOR_HANDOFF_DO_NOT_HOOK
) {
284 return EFI_UNSUPPORTED
;
287 if (InterruptHandler
== NULL
&& ExternalInterruptHandler
[InterruptType
] == NULL
) {
288 return EFI_INVALID_PARAMETER
;
291 if (InterruptHandler
!= NULL
&& ExternalInterruptHandler
[InterruptType
] != NULL
) {
292 return EFI_ALREADY_STARTED
;
295 ExternalInterruptHandler
[InterruptType
] = InterruptHandler
;