2 CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.
4 Copyright (c) 2012 - 2016, 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 // Spinlock for CPU information display
21 SPIN_LOCK mDisplayMessageSpinLock
;
24 // Image align size for DXE/SMM
26 CONST UINTN mImageAlignSize
= SIZE_4KB
;
28 RESERVED_VECTORS_DATA mReservedVectorsData
[CPU_EXCEPTION_NUM
];
29 EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable
[CPU_EXCEPTION_NUM
];
30 EFI_CPU_INTERRUPT_HANDLER
*mExternalInterruptHandler
= NULL
;
31 UINTN mEnabledInterruptNum
= 0;
34 Common exception handler.
36 @param ExceptionType Exception type.
37 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
41 CommonExceptionHandler (
42 IN EFI_EXCEPTION_TYPE ExceptionType
,
43 IN EFI_SYSTEM_CONTEXT SystemContext
46 EXCEPTION_HANDLER_CONTEXT
*ExceptionHandlerContext
;
48 ExceptionHandlerContext
= (EXCEPTION_HANDLER_CONTEXT
*) (UINTN
) (SystemContext
.SystemContextIa32
);
50 switch (mReservedVectors
[ExceptionType
].Attribute
) {
51 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
53 // Need to jmp to old IDT handler after this exception handler
55 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
56 ExceptionHandlerContext
->OldIdtHandler
= mReservedVectors
[ExceptionType
].ExceptonHandler
;
58 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
61 // If if anyone has gotten SPIN_LOCK for owner running hook after
63 if (AcquireSpinLockOrFail (&mReservedVectors
[ExceptionType
].SpinLock
)) {
65 // Need to execute old IDT handler before running this exception handler
67 mReservedVectors
[ExceptionType
].ApicId
= GetApicId ();
68 ArchSaveExceptionContext (ExceptionType
, SystemContext
);
69 ExceptionHandlerContext
->ExceptionDataFlag
= (mErrorCodeFlag
& (1 << ExceptionType
)) ? TRUE
: FALSE
;
70 ExceptionHandlerContext
->OldIdtHandler
= mReservedVectors
[ExceptionType
].ExceptonHandler
;
74 // If failed to acquire SPIN_LOCK, check if it was locked by processor itself
76 if (mReservedVectors
[ExceptionType
].ApicId
== GetApicId ()) {
78 // Old IDT handler has been executed, then retore CPU exception content to
79 // run new exception handler.
81 ArchRestoreExceptionContext (ExceptionType
, SystemContext
);
83 // Rlease spin lock for ApicId
85 ReleaseSpinLock (&mReservedVectors
[ExceptionType
].SpinLock
);
95 // It should never reach here
101 if (mExternalInterruptHandler
[ExceptionType
] != NULL
) {
102 (mExternalInterruptHandler
[ExceptionType
]) (ExceptionType
, SystemContext
);
103 } else if (ExceptionType
< CPU_EXCEPTION_NUM
) {
105 // Get Spinlock to display CPU information
107 while (!AcquireSpinLockOrFail (&mDisplayMessageSpinLock
)) {
111 // Display ExceptionType, CPU information and Image information
113 DumpCpuContent (ExceptionType
, SystemContext
);
115 // Release Spinlock of output message
117 ReleaseSpinLock (&mDisplayMessageSpinLock
);
119 // Enter a dead loop if needn't to execute old IDT handler further
121 if (mReservedVectors
[ExceptionType
].Attribute
!= EFI_VECTOR_HANDOFF_HOOK_BEFORE
) {
128 Internal worker function to update IDT entries accordling to vector attributes.
130 @param[in] IdtTable Pointer to IDT table.
131 @param[in] TemplateMap Pointer to a buffer where the address map is
133 @param[in] ExceptionHandlerData Pointer to exception handler data.
138 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
139 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
140 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
145 UINTN InterruptHandler
;
146 RESERVED_VECTORS_DATA
*ReservedVectors
;
148 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
150 // Use current CS as the segment selector of interrupt gate in IDT
152 CodeSegment
= AsmReadCs ();
154 for (Index
= 0; Index
< ExceptionHandlerData
->IdtEntryCount
; Index
++) {
155 IdtTable
[Index
].Bits
.Selector
= CodeSegment
;
157 // Check reserved vectors attributes
159 switch (ReservedVectors
[Index
].Attribute
) {
160 case EFI_VECTOR_HANDOFF_DO_NOT_HOOK
:
162 // Keep original IDT entry
165 case EFI_VECTOR_HANDOFF_HOOK_AFTER
:
166 InitializeSpinLock (&ReservedVectors
[Index
].SpinLock
);
168 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
169 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
,
170 TemplateMap
->ExceptionStubHeaderSize
173 (VOID
*) ReservedVectors
[Index
].HookAfterStubHeaderCode
,
175 (VOID
*) TemplateMap
->HookAfterStubHeaderStart
178 // Go on the following code
180 case EFI_VECTOR_HANDOFF_HOOK_BEFORE
:
182 // Save original IDT handler address
184 ReservedVectors
[Index
].ExceptonHandler
= ArchGetIdtHandler (&IdtTable
[Index
]);
186 // Go on the following code
190 // Update new IDT entry
192 InterruptHandler
= TemplateMap
->ExceptionStart
+ Index
* TemplateMap
->ExceptionStubHeaderSize
;
193 ArchUpdateIdtEntry (&IdtTable
[Index
], InterruptHandler
);
199 // Save Interrupt number to global variable used for RegisterCpuInterruptHandler ()
201 mEnabledInterruptNum
= ExceptionHandlerData
->IdtEntryCount
;
205 Internal worker function to initialize exception handler.
207 @param[in] VectorInfo Pointer to reserved vector list.
208 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
210 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
211 with default exception handlers.
212 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
213 @retval EFI_UNSUPPORTED This function is not supported.
217 InitializeCpuExceptionHandlersWorker (
218 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
219 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
223 IA32_DESCRIPTOR IdtDescriptor
;
225 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap
;
226 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
227 RESERVED_VECTORS_DATA
*ReservedVectors
;
229 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
230 SetMem ((VOID
*) ReservedVectors
, sizeof (RESERVED_VECTORS_DATA
) * CPU_EXCEPTION_NUM
, 0xff);
231 if (VectorInfo
!= NULL
) {
232 Status
= ReadAndVerifyVectorInfo (VectorInfo
, ReservedVectors
, CPU_EXCEPTION_NUM
);
233 if (EFI_ERROR (Status
)) {
234 return EFI_INVALID_PARAMETER
;
237 InitializeSpinLock (&mDisplayMessageSpinLock
);
239 mExternalInterruptHandler
= mExternalInterruptHandlerTable
;
241 // Read IDT descriptor and calculate IDT size
243 AsmReadIdtr (&IdtDescriptor
);
244 IdtEntryCount
= (IdtDescriptor
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
);
245 if (IdtEntryCount
> CPU_EXCEPTION_NUM
) {
247 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
249 IdtEntryCount
= CPU_EXCEPTION_NUM
;
252 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
253 AsmGetTemplateAddressMap (&TemplateMap
);
254 ASSERT (TemplateMap
.ExceptionStubHeaderSize
<= HOOKAFTER_STUB_SIZE
);
256 ExceptionHandlerData
->IdtEntryCount
= IdtEntryCount
;
257 UpdateIdtTable (IdtTable
, &TemplateMap
, ExceptionHandlerData
);
258 mEnabledInterruptNum
= IdtEntryCount
;
263 Registers a function to be called from the processor interrupt handler.
265 @param[in] InterruptType Defines which interrupt or exception to hook.
266 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
267 when a processor interrupt occurs. If this parameter is NULL, then the handler
269 @param[in] ExceptionHandlerData Pointer to exception handler data.
271 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
272 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
273 previously installed.
274 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
275 previously installed.
276 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
277 or this function is not supported.
280 RegisterCpuInterruptHandlerWorker (
281 IN EFI_EXCEPTION_TYPE InterruptType
,
282 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
283 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
286 UINTN EnabledInterruptNum
;
287 RESERVED_VECTORS_DATA
*ReservedVectors
;
288 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
290 EnabledInterruptNum
= ExceptionHandlerData
->IdtEntryCount
;
291 ReservedVectors
= ExceptionHandlerData
->ReservedVectors
;
292 ExternalInterruptHandler
= ExceptionHandlerData
->ExternalInterruptHandler
;
294 if (InterruptType
< 0 || InterruptType
>= (EFI_EXCEPTION_TYPE
)EnabledInterruptNum
||
295 ReservedVectors
[InterruptType
].Attribute
== EFI_VECTOR_HANDOFF_DO_NOT_HOOK
) {
296 return EFI_UNSUPPORTED
;
299 if (InterruptHandler
== NULL
&& ExternalInterruptHandler
[InterruptType
] == NULL
) {
300 return EFI_INVALID_PARAMETER
;
303 if (InterruptHandler
!= NULL
&& ExternalInterruptHandler
[InterruptType
] != NULL
) {
304 return EFI_ALREADY_STARTED
;
307 ExternalInterruptHandler
[InterruptType
] = InterruptHandler
;