2 Common header file for CPU Exception Handler Library.
4 Copyright (c) 2012 - 2017, 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_EXCEPTION_HANDLER_LIB_HOB_GUID \
48 0xb21d9148, 0x9211, 0x4d8f, { 0xad, 0xd3, 0x66, 0xb1, 0x89, 0xc9, 0x2c, 0x83 } \
52 // Record exception handler information
56 UINTN ExceptionStubHeaderSize
;
57 UINTN HookAfterStubHeaderStart
;
58 } EXCEPTION_HANDLER_TEMPLATE_MAP
;
62 SPIN_LOCK DisplayMessageSpinLock
;
63 RESERVED_VECTORS_DATA
*ReservedVectors
;
64 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
65 } EXCEPTION_HANDLER_DATA
;
67 extern CONST UINT32 mErrorCodeFlag
;
68 extern CONST UINTN mDoFarReturnFlag
;
71 Return address map of exception handler template so that C code can generate
74 @param AddressMap Pointer to a buffer where the address map is returned.
78 AsmGetTemplateAddressMap (
79 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap
83 Return address map of exception handler template so that C code can generate
86 @param IdtEntry Pointer to IDT entry to be updated.
87 @param InterruptHandler IDT handler value.
92 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
,
93 IN UINTN InterruptHandler
97 Read IDT handler value from IDT entry.
99 @param IdtEntry Pointer to IDT entry to be read.
104 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
108 Prints a message to the serial port.
110 @param Format Format string for the message to print.
111 @param ... Variable argument list whose contents are accessed
112 based on the format string specified by Format.
117 InternalPrintMessage (
118 IN CONST CHAR8
*Format
,
123 Find and display image base address and return image base and its entry point.
125 @param CurrentEip Current instruction pointer.
129 DumpModuleImageInfo (
134 Display CPU information.
136 @param ExceptionType Exception type.
137 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
140 DumpImageAndCpuContent (
141 IN EFI_EXCEPTION_TYPE ExceptionType
,
142 IN EFI_SYSTEM_CONTEXT SystemContext
146 Internal worker function to initialize exception handler.
148 @param[in] VectorInfo Pointer to reserved vector list.
149 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
151 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
152 with default exception handlers.
153 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
154 @retval EFI_UNSUPPORTED This function is not supported.
158 InitializeCpuExceptionHandlersWorker (
159 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
160 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
164 Registers a function to be called from the processor interrupt handler.
166 @param[in] InterruptType Defines which interrupt or exception to hook.
167 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
168 when a processor interrupt occurs. If this parameter is NULL, then the handler
170 @param[in] ExceptionHandlerData Pointer to exception handler data.
172 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
173 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
174 previously installed.
175 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
176 previously installed.
177 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
178 or this function is not supported.
181 RegisterCpuInterruptHandlerWorker (
182 IN EFI_EXCEPTION_TYPE InterruptType
,
183 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
184 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
188 Internal worker function to update IDT entries accordling to vector attributes.
190 @param[in] IdtTable Pointer to IDT table.
191 @param[in] TemplateMap Pointer to a buffer where the address map is
193 @param[in] ExceptionHandlerData Pointer to exception handler data.
198 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
199 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
200 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
204 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
206 @param[in] ExceptionType Exception type.
207 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
208 @param[in] ExceptionHandlerData Pointer to exception handler data.
211 ArchSaveExceptionContext (
212 IN UINTN ExceptionType
,
213 IN EFI_SYSTEM_CONTEXT SystemContext
,
214 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
218 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
220 @param[in] ExceptionType Exception type.
221 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
222 @param[in] ExceptionHandlerData Pointer to exception handler data.
225 ArchRestoreExceptionContext (
226 IN UINTN ExceptionType
,
227 IN EFI_SYSTEM_CONTEXT SystemContext
,
228 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
232 Fix up the vector number and function address in the vector code.
234 @param[in] NewVectorAddr New vector handler address.
235 @param[in] VectorNum Index of vector.
236 @param[in] OldVectorAddr Old vector handler address.
242 IN VOID
*NewVectorAddr
,
244 IN VOID
*OldVectorAddr
248 Read and save reserved vector information
250 @param[in] VectorInfo Pointer to reserved vector list.
251 @param[out] ReservedVector Pointer to reserved vector data buffer.
252 @param[in] VectorCount Vector number to be updated.
254 @return EFI_SUCCESS Read and save vector info successfully.
255 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
259 ReadAndVerifyVectorInfo (
260 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo
,
261 OUT RESERVED_VECTORS_DATA
*ReservedVector
,
266 Get ASCII format string exception name by exception type.
268 @param ExceptionType Exception type.
270 @return ASCII format string exception name.
273 GetExceptionNameStr (
274 IN EFI_EXCEPTION_TYPE ExceptionType
278 Internal worker function for common exception handler.
280 @param ExceptionType Exception type.
281 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
282 @param ExceptionHandlerData Pointer to exception handler data.
285 CommonExceptionHandlerWorker (
286 IN EFI_EXCEPTION_TYPE ExceptionType
,
287 IN EFI_SYSTEM_CONTEXT SystemContext
,
288 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData