2 Common header file for CPU Exception Handler Library.
4 Copyright (c) 2012 - 2019, 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_SS BIT6
43 #define IA32_PF_EC_SGX BIT15
45 #include "ArchInterruptDefs.h"
47 #define CPU_STACK_SWITCH_EXCEPTION_NUMBER \
48 FixedPcdGetSize (PcdCpuStackSwitchExceptionList)
50 #define CPU_STACK_SWITCH_EXCEPTION_LIST \
51 FixedPcdGetPtr (PcdCpuStackSwitchExceptionList)
53 #define CPU_KNOWN_GOOD_STACK_SIZE \
54 FixedPcdGet32 (PcdCpuKnownGoodStackSize)
56 #define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE)
59 // Record exception handler information
63 UINTN ExceptionStubHeaderSize
;
64 UINTN HookAfterStubHeaderStart
;
65 } EXCEPTION_HANDLER_TEMPLATE_MAP
;
69 SPIN_LOCK DisplayMessageSpinLock
;
70 RESERVED_VECTORS_DATA
*ReservedVectors
;
71 EFI_CPU_INTERRUPT_HANDLER
*ExternalInterruptHandler
;
72 } EXCEPTION_HANDLER_DATA
;
74 extern CONST UINT32 mErrorCodeFlag
;
75 extern CONST UINTN mDoFarReturnFlag
;
78 Return address map of exception handler template so that C code can generate
81 @param AddressMap Pointer to a buffer where the address map is returned.
85 AsmGetTemplateAddressMap (
86 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap
90 Return address map of exception handler template so that C code can generate
93 @param IdtEntry Pointer to IDT entry to be updated.
94 @param InterruptHandler IDT handler value.
99 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
,
100 IN UINTN InterruptHandler
104 Read IDT handler value from IDT entry.
106 @param IdtEntry Pointer to IDT entry to be read.
111 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
115 Prints a message to the serial port.
117 @param Format Format string for the message to print.
118 @param ... Variable argument list whose contents are accessed
119 based on the format string specified by Format.
124 InternalPrintMessage (
125 IN CONST CHAR8
*Format
,
130 Find and display image base address and return image base and its entry point.
132 @param CurrentEip Current instruction pointer.
136 DumpModuleImageInfo (
141 Display CPU information.
143 @param ExceptionType Exception type.
144 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
147 DumpImageAndCpuContent (
148 IN EFI_EXCEPTION_TYPE ExceptionType
,
149 IN EFI_SYSTEM_CONTEXT SystemContext
153 Internal worker function to initialize exception handler.
155 @param[in] VectorInfo Pointer to reserved vector list.
156 @param[in, out] ExceptionHandlerData Pointer to exception handler data.
158 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
159 with default exception handlers.
160 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
161 @retval EFI_UNSUPPORTED This function is not supported.
165 InitializeCpuExceptionHandlersWorker (
166 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
167 IN OUT EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
171 Registers a function to be called from the processor interrupt handler.
173 @param[in] InterruptType Defines which interrupt or exception to hook.
174 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
175 when a processor interrupt occurs. If this parameter is NULL, then the handler
177 @param[in] ExceptionHandlerData Pointer to exception handler data.
179 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
180 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
181 previously installed.
182 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
183 previously installed.
184 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
185 or this function is not supported.
188 RegisterCpuInterruptHandlerWorker (
189 IN EFI_EXCEPTION_TYPE InterruptType
,
190 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
,
191 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
195 Internal worker function to update IDT entries accordling to vector attributes.
197 @param[in] IdtTable Pointer to IDT table.
198 @param[in] TemplateMap Pointer to a buffer where the address map is
200 @param[in] ExceptionHandlerData Pointer to exception handler data.
205 IN IA32_IDT_GATE_DESCRIPTOR
*IdtTable
,
206 IN EXCEPTION_HANDLER_TEMPLATE_MAP
*TemplateMap
,
207 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
211 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
213 @param[in] ExceptionType Exception type.
214 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
215 @param[in] ExceptionHandlerData Pointer to exception handler data.
218 ArchSaveExceptionContext (
219 IN UINTN ExceptionType
,
220 IN EFI_SYSTEM_CONTEXT SystemContext
,
221 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
225 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
227 @param[in] ExceptionType Exception type.
228 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.
229 @param[in] ExceptionHandlerData Pointer to exception handler data.
232 ArchRestoreExceptionContext (
233 IN UINTN ExceptionType
,
234 IN EFI_SYSTEM_CONTEXT SystemContext
,
235 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
239 Fix up the vector number and function address in the vector code.
241 @param[in] NewVectorAddr New vector handler address.
242 @param[in] VectorNum Index of vector.
243 @param[in] OldVectorAddr Old vector handler address.
249 IN VOID
*NewVectorAddr
,
251 IN VOID
*OldVectorAddr
255 Read and save reserved vector information
257 @param[in] VectorInfo Pointer to reserved vector list.
258 @param[out] ReservedVector Pointer to reserved vector data buffer.
259 @param[in] VectorCount Vector number to be updated.
261 @return EFI_SUCCESS Read and save vector info successfully.
262 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
266 ReadAndVerifyVectorInfo (
267 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo
,
268 OUT RESERVED_VECTORS_DATA
*ReservedVector
,
273 Get ASCII format string exception name by exception type.
275 @param ExceptionType Exception type.
277 @return ASCII format string exception name.
280 GetExceptionNameStr (
281 IN EFI_EXCEPTION_TYPE ExceptionType
285 Internal worker function for common exception handler.
287 @param ExceptionType Exception type.
288 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
289 @param ExceptionHandlerData Pointer to exception handler data.
292 CommonExceptionHandlerWorker (
293 IN EFI_EXCEPTION_TYPE ExceptionType
,
294 IN EFI_SYSTEM_CONTEXT SystemContext
,
295 IN EXCEPTION_HANDLER_DATA
*ExceptionHandlerData
299 Setup separate stack for specific exceptions.
301 @param[in] StackSwitchData Pointer to data required for setuping up
304 @retval EFI_SUCCESS The exceptions have been successfully
305 initialized with new stack.
306 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.
309 ArchSetupExceptionStack (
310 IN CPU_EXCEPTION_INIT_DATA
*StackSwitchData
314 Return address map of exception handler template so that C code can generate
315 exception tables. The template is only for exceptions using task gate instead
318 @param AddressMap Pointer to a buffer where the address map is returned.
322 AsmGetTssTemplateMap (
323 OUT EXCEPTION_HANDLER_TEMPLATE_MAP
*AddressMap