2 Common header file for CPU Exception Handler Library.
4 Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #ifndef _CPU_EXCEPTION_COMMON_H_
10 #define _CPU_EXCEPTION_COMMON_H_
12 #include <Ppi/VectorHandoffInfo.h>
13 #include <Protocol/Cpu.h>
14 #include <Library/BaseLib.h>
15 #include <Library/SerialPortLib.h>
16 #include <Library/PrintLib.h>
17 #include <Library/LocalApicLib.h>
18 #include <Library/PeCoffGetEntryPointLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/SynchronizationLib.h>
21 #include <Library/CpuExceptionHandlerLib.h>
23 #define CPU_EXCEPTION_NUM 32
24 #define CPU_INTERRUPT_NUM 256
25 #define HOOKAFTER_STUB_SIZE 16
28 // Exception Error Code of Page-Fault Exception
30 #define IA32_PF_EC_P BIT0
31 #define IA32_PF_EC_WR BIT1
32 #define IA32_PF_EC_US BIT2
33 #define IA32_PF_EC_RSVD BIT3
34 #define IA32_PF_EC_ID BIT4
35 #define IA32_PF_EC_PK BIT5
36 #define IA32_PF_EC_SS BIT6
37 #define IA32_PF_EC_SGX BIT15
39 #include "ArchInterruptDefs.h"
41 #define CPU_STACK_SWITCH_EXCEPTION_NUMBER \
42 FixedPcdGetSize (PcdCpuStackSwitchExceptionList)
44 #define CPU_STACK_SWITCH_EXCEPTION_LIST \
45 FixedPcdGetPtr (PcdCpuStackSwitchExceptionList)
47 #define CPU_KNOWN_GOOD_STACK_SIZE \
48 FixedPcdGet32 (PcdCpuKnownGoodStackSize)
50 #define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE)
53 // Record exception handler information
57 UINTN ExceptionStubHeaderSize
;
58 UINTN HookAfterStubHeaderStart
;
59 } EXCEPTION_HANDLER_TEMPLATE_MAP
;
63 SPIN_LOCK DisplayMessageSpinLock
;
64 RESERVED_VECTORS_DATA
*ReservedVectors
;
65 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
66 } EXCEPTION_HANDLER_DATA
;
68 extern CONST UINT32 mErrorCodeFlag
;
69 extern CONST UINTN mDoFarReturnFlag
;
72 Return address map of exception handler template so that C code can generate
75 @param AddressMap Pointer to a buffer where the address map is returned.
79 AsmGetTemplateAddressMap (
80 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap
84 Return address map of exception handler template so that C code can generate
87 @param IdtEntry Pointer to IDT entry to be updated.
88 @param InterruptHandler IDT handler value.
93 OUT IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
,
94 IN UINTN InterruptHandler
98 Read IDT handler value from IDT entry.
100 @param IdtEntry Pointer to IDT entry to be read.
105 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
109 Prints a message to the serial port.
111 @param Format Format string for the message to print.
112 @param ... Variable argument list whose contents are accessed
113 based on the format string specified by Format.
118 InternalPrintMessage (
119 IN CONST CHAR8
*Format
,
124 Find and display image base address and return image base and its entry point.
126 @param CurrentEip Current instruction pointer.
130 DumpModuleImageInfo (
135 Display CPU information.
137 @param ExceptionType Exception type.
138 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
141 DumpImageAndCpuContent (
142 IN EFI_EXCEPTION_TYPE ExceptionType
,
143 IN EFI_SYSTEM_CONTEXT SystemContext
147 Internal worker function to initialize exception handler.
149 @param[in] VectorInfo Pointer to reserved vector list.
150 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
152 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
153 with default exception handlers.
154 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
155 @retval EFI_UNSUPPORTED This function is not supported.
159 InitializeCpuExceptionHandlersWorker (
160 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
161 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
165 Registers a function to be called from the processor interrupt handler.
167 @param[in] InterruptType Defines which interrupt or exception to hook.
168 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
169 when a processor interrupt occurs. If this parameter is NULL, then the handler
171 @param[in] ExceptionHandlerData Pointer to exception handler data.
173 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
174 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
175 previously installed.
176 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
177 previously installed.
178 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
179 or this function is not supported.
182 RegisterCpuInterruptHandlerWorker (
183 IN EFI_EXCEPTION_TYPE InterruptType
,
184 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
185 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
189 Internal worker function to update IDT entries accordling to vector attributes.
191 @param[in] IdtTable Pointer to IDT table.
192 @param[in] TemplateMap Pointer to a buffer where the address map is
194 @param[in] ExceptionHandlerData Pointer to exception handler data.
199 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
200 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
201 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
205 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
207 @param[in] ExceptionType Exception type.
208 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
209 @param[in] ExceptionHandlerData Pointer to exception handler data.
212 ArchSaveExceptionContext (
213 IN UINTN ExceptionType
,
214 IN EFI_SYSTEM_CONTEXT SystemContext
,
215 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
219 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
221 @param[in] ExceptionType Exception type.
222 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
223 @param[in] ExceptionHandlerData Pointer to exception handler data.
226 ArchRestoreExceptionContext (
227 IN UINTN ExceptionType
,
228 IN EFI_SYSTEM_CONTEXT SystemContext
,
229 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
233 Fix up the vector number and function address in the vector code.
235 @param[in] NewVectorAddr New vector handler address.
236 @param[in] VectorNum Index of vector.
237 @param[in] OldVectorAddr Old vector handler address.
243 IN VOID
*NewVectorAddr
,
245 IN VOID
*OldVectorAddr
249 Read and save reserved vector information
251 @param[in] VectorInfo Pointer to reserved vector list.
252 @param[out] ReservedVector Pointer to reserved vector data buffer.
253 @param[in] VectorCount Vector number to be updated.
255 @return EFI_SUCCESS Read and save vector info successfully.
256 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
260 ReadAndVerifyVectorInfo (
261 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo
,
262 OUT RESERVED_VECTORS_DATA
*ReservedVector
,
267 Get ASCII format string exception name by exception type.
269 @param ExceptionType Exception type.
271 @return ASCII format string exception name.
274 GetExceptionNameStr (
275 IN EFI_EXCEPTION_TYPE ExceptionType
279 Internal worker function for common exception handler.
281 @param ExceptionType Exception type.
282 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
283 @param ExceptionHandlerData Pointer to exception handler data.
286 CommonExceptionHandlerWorker (
287 IN EFI_EXCEPTION_TYPE ExceptionType
,
288 IN EFI_SYSTEM_CONTEXT SystemContext
,
289 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
293 Setup separate stack for specific exceptions.
295 @param[in] StackSwitchData Pointer to data required for setuping up
298 @retval EFI_SUCCESS The exceptions have been successfully
299 initialized with new stack.
300 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
303 ArchSetupExceptionStack (
304 IN CPU_EXCEPTION_INIT_DATA
*StackSwitchData
308 Return address map of exception handler template so that C code can generate
309 exception tables. The template is only for exceptions using task gate instead
312 @param AddressMap Pointer to a buffer where the address map is returned.
316 AsmGetTssTemplateMap (
317 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap