2 Common header file for CPU Exception Handler Library.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. 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 #ifndef _CPU_EXCEPTION_COMMON_H_
16 #define _CPU_EXCEPTION_COMMON_H_
18 #include <Ppi/VectorHandoffInfo.h>
19 #include <Protocol/Cpu.h>
20 #include <Library/BaseLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/PrintLib.h>
23 #include <Library/LocalApicLib.h>
24 #include <Library/PeCoffGetEntryPointLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/SynchronizationLib.h>
27 #include <Library/CpuExceptionHandlerLib.h>
29 #define CPU_EXCEPTION_NUM 32
30 #define CPU_INTERRUPT_NUM 256
31 #define HOOKAFTER_STUB_SIZE 16
34 // Exception Error Code of Page-Fault Exception
36 #define IA32_PF_EC_P BIT0
37 #define IA32_PF_EC_WR BIT1
38 #define IA32_PF_EC_US BIT2
39 #define IA32_PF_EC_RSVD BIT3
40 #define IA32_PF_EC_ID BIT4
41 #define IA32_PF_EC_PK BIT5
42 #define IA32_PF_EC_SGX BIT15
44 #include "ArchInterruptDefs.h"
46 #define CPU_STACK_SWITCH_EXCEPTION_NUMBER \
47 FixedPcdGetSize (PcdCpuStackSwitchExceptionList)
49 #define CPU_STACK_SWITCH_EXCEPTION_LIST \
50 FixedPcdGetPtr (PcdCpuStackSwitchExceptionList)
52 #define CPU_KNOWN_GOOD_STACK_SIZE \
53 FixedPcdGet32 (PcdCpuKnownGoodStackSize)
55 #define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE)
58 // Record exception handler information
62 UINTN ExceptionStubHeaderSize
;
63 UINTN HookAfterStubHeaderStart
;
64 } EXCEPTION_HANDLER_TEMPLATE_MAP
;
68 SPIN_LOCK DisplayMessageSpinLock
;
69 RESERVED_VECTORS_DATA
*ReservedVectors
;
70 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
71 } EXCEPTION_HANDLER_DATA
;
73 extern CONST UINT32 mErrorCodeFlag
;
74 extern CONST UINTN mDoFarReturnFlag
;
77 Return address map of exception handler template so that C code can generate
80 @param AddressMap Pointer to a buffer where the address map is returned.
84 AsmGetTemplateAddressMap (
85 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap
89 Return address map of exception handler template so that C code can generate
92 @param IdtEntry Pointer to IDT entry to be updated.
93 @param InterruptHandler IDT handler value.
98 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
,
99 IN UINTN InterruptHandler
103 Read IDT handler value from IDT entry.
105 @param IdtEntry Pointer to IDT entry to be read.
110 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
114 Prints a message to the serial port.
116 @param Format Format string for the message to print.
117 @param ... Variable argument list whose contents are accessed
118 based on the format string specified by Format.
123 InternalPrintMessage (
124 IN CONST CHAR8
*Format
,
129 Find and display image base address and return image base and its entry point.
131 @param CurrentEip Current instruction pointer.
135 DumpModuleImageInfo (
140 Display CPU information.
142 @param ExceptionType Exception type.
143 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
146 DumpImageAndCpuContent (
147 IN EFI_EXCEPTION_TYPE ExceptionType
,
148 IN EFI_SYSTEM_CONTEXT SystemContext
152 Internal worker function to initialize exception handler.
154 @param[in] VectorInfo Pointer to reserved vector list.
155 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
157 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
158 with default exception handlers.
159 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
160 @retval EFI_UNSUPPORTED This function is not supported.
164 InitializeCpuExceptionHandlersWorker (
165 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
166 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
170 Registers a function to be called from the processor interrupt handler.
172 @param[in] InterruptType Defines which interrupt or exception to hook.
173 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
174 when a processor interrupt occurs. If this parameter is NULL, then the handler
176 @param[in] ExceptionHandlerData Pointer to exception handler data.
178 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
179 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
180 previously installed.
181 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
182 previously installed.
183 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
184 or this function is not supported.
187 RegisterCpuInterruptHandlerWorker (
188 IN EFI_EXCEPTION_TYPE InterruptType
,
189 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
190 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
194 Internal worker function to update IDT entries accordling to vector attributes.
196 @param[in] IdtTable Pointer to IDT table.
197 @param[in] TemplateMap Pointer to a buffer where the address map is
199 @param[in] ExceptionHandlerData Pointer to exception handler data.
204 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
205 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
206 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
210 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
212 @param[in] ExceptionType Exception type.
213 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
214 @param[in] ExceptionHandlerData Pointer to exception handler data.
217 ArchSaveExceptionContext (
218 IN UINTN ExceptionType
,
219 IN EFI_SYSTEM_CONTEXT SystemContext
,
220 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
224 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
226 @param[in] ExceptionType Exception type.
227 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
228 @param[in] ExceptionHandlerData Pointer to exception handler data.
231 ArchRestoreExceptionContext (
232 IN UINTN ExceptionType
,
233 IN EFI_SYSTEM_CONTEXT SystemContext
,
234 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
238 Fix up the vector number and function address in the vector code.
240 @param[in] NewVectorAddr New vector handler address.
241 @param[in] VectorNum Index of vector.
242 @param[in] OldVectorAddr Old vector handler address.
248 IN VOID
*NewVectorAddr
,
250 IN VOID
*OldVectorAddr
254 Read and save reserved vector information
256 @param[in] VectorInfo Pointer to reserved vector list.
257 @param[out] ReservedVector Pointer to reserved vector data buffer.
258 @param[in] VectorCount Vector number to be updated.
260 @return EFI_SUCCESS Read and save vector info successfully.
261 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
265 ReadAndVerifyVectorInfo (
266 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo
,
267 OUT RESERVED_VECTORS_DATA
*ReservedVector
,
272 Get ASCII format string exception name by exception type.
274 @param ExceptionType Exception type.
276 @return ASCII format string exception name.
279 GetExceptionNameStr (
280 IN EFI_EXCEPTION_TYPE ExceptionType
284 Internal worker function for common exception handler.
286 @param ExceptionType Exception type.
287 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
288 @param ExceptionHandlerData Pointer to exception handler data.
291 CommonExceptionHandlerWorker (
292 IN EFI_EXCEPTION_TYPE ExceptionType
,
293 IN EFI_SYSTEM_CONTEXT SystemContext
,
294 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
298 Setup separate stack for specific exceptions.
300 @param[in] StackSwitchData Pointer to data required for setuping up
303 @retval EFI_SUCCESS The exceptions have been successfully
304 initialized with new stack.
305 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
308 ArchSetupExceptionStack (
309 IN CPU_EXCEPTION_INIT_DATA
*StackSwitchData
313 Return address map of exception handler template so that C code can generate
314 exception tables. The template is only for exceptions using task gate instead
317 @param AddressMap Pointer to a buffer where the address map is returned.
321 AsmGetTssTemplateMap (
322 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap