]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / CpuExceptionCommon.h
CommitLineData
8f07f895 1/** @file\r
2 Common header file for CPU Exception Handler Library.\r
3\r
0d25074c 4 Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8f07f895 6\r
7**/\r
8\r
9#ifndef _CPU_EXCEPTION_COMMON_H_\r
10#define _CPU_EXCEPTION_COMMON_H_\r
11\r
e41aad15
JF
12#include <Ppi/VectorHandoffInfo.h>\r
13#include <Protocol/Cpu.h>\r
8f07f895 14#include <Library/BaseLib.h>\r
15#include <Library/SerialPortLib.h>\r
16#include <Library/PrintLib.h>\r
17#include <Library/LocalApicLib.h>\r
18#include <Library/PeCoffGetEntryPointLib.h>\r
e41aad15
JF
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/SynchronizationLib.h>\r
1b2f7b3e 21#include <Library/CpuExceptionHandlerLib.h>\r
e41aad15
JF
22\r
23#define CPU_EXCEPTION_NUM 32\r
24#define CPU_INTERRUPT_NUM 256\r
25#define HOOKAFTER_STUB_SIZE 16\r
26\r
1b2f7b3e
JF
27//\r
28// Exception Error Code of Page-Fault Exception\r
29//\r
30#define IA32_PF_EC_P BIT0\r
31#define IA32_PF_EC_WR BIT1\r
32#define IA32_PF_EC_US BIT2\r
33#define IA32_PF_EC_RSVD BIT3\r
34#define IA32_PF_EC_ID BIT4\r
35#define IA32_PF_EC_PK BIT5\r
0d25074c 36#define IA32_PF_EC_SS BIT6\r
1b2f7b3e
JF
37#define IA32_PF_EC_SGX BIT15\r
38\r
e41aad15 39#include "ArchInterruptDefs.h"\r
8f07f895 40\r
0ff5aa9c
JW
41#define CPU_STACK_SWITCH_EXCEPTION_NUMBER \\r
42 FixedPcdGetSize (PcdCpuStackSwitchExceptionList)\r
43\r
44#define CPU_STACK_SWITCH_EXCEPTION_LIST \\r
45 FixedPcdGetPtr (PcdCpuStackSwitchExceptionList)\r
46\r
47#define CPU_KNOWN_GOOD_STACK_SIZE \\r
48 FixedPcdGet32 (PcdCpuKnownGoodStackSize)\r
49\r
50#define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE)\r
51\r
8f07f895 52//\r
53// Record exception handler information\r
54//\r
55typedef struct {\r
56 UINTN ExceptionStart;\r
57 UINTN ExceptionStubHeaderSize;\r
e41aad15 58 UINTN HookAfterStubHeaderStart;\r
8f07f895 59} EXCEPTION_HANDLER_TEMPLATE_MAP;\r
60\r
2c5873fe
JF
61typedef struct {\r
62 UINTN IdtEntryCount;\r
63 SPIN_LOCK DisplayMessageSpinLock;\r
64 RESERVED_VECTORS_DATA *ReservedVectors;\r
65 EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;\r
66} EXCEPTION_HANDLER_DATA;\r
67\r
e41aad15 68extern CONST UINT32 mErrorCodeFlag;\r
e41aad15 69extern CONST UINTN mDoFarReturnFlag;\r
8f07f895 70\r
71/**\r
72 Return address map of exception handler template so that C code can generate\r
73 exception tables.\r
74\r
75 @param AddressMap Pointer to a buffer where the address map is returned.\r
76**/\r
77VOID\r
78EFIAPI\r
e41aad15 79AsmGetTemplateAddressMap (\r
8f07f895 80 OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap\r
81 );\r
82\r
83/**\r
e41aad15
JF
84 Return address map of exception handler template so that C code can generate\r
85 exception tables.\r
86\r
87 @param IdtEntry Pointer to IDT entry to be updated.\r
88 @param InterruptHandler IDT handler value.\r
8f07f895 89\r
90**/\r
91VOID\r
e41aad15
JF
92ArchUpdateIdtEntry (\r
93 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry,\r
94 IN UINTN InterruptHandler\r
95 );\r
96\r
97/**\r
98 Read IDT handler value from IDT entry.\r
99\r
100 @param IdtEntry Pointer to IDT entry to be read.\r
101\r
102**/\r
103UINTN\r
104ArchGetIdtHandler (\r
105 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry\r
8f07f895 106 );\r
107\r
108/**\r
109 Prints a message to the serial port.\r
110\r
111 @param Format Format string for the message to print.\r
dd563742 112 @param ... Variable argument list whose contents are accessed\r
8f07f895 113 based on the format string specified by Format.\r
114\r
115**/\r
116VOID\r
117EFIAPI\r
118InternalPrintMessage (\r
119 IN CONST CHAR8 *Format,\r
120 ...\r
121 );\r
122\r
123/**\r
124 Find and display image base address and return image base and its entry point.\r
dd563742 125\r
a9c7ab95 126 @param CurrentEip Current instruction pointer.\r
dd563742 127\r
8f07f895 128**/\r
dd563742 129VOID\r
1b2f7b3e
JF
130DumpModuleImageInfo (\r
131 IN UINTN CurrentEip\r
8f07f895 132 );\r
133\r
134/**\r
135 Display CPU information.\r
136\r
3f25e6ea 137 @param ExceptionType Exception type.\r
8f07f895 138 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
139**/\r
140VOID\r
1b2f7b3e 141DumpImageAndCpuContent (\r
e41aad15 142 IN EFI_EXCEPTION_TYPE ExceptionType,\r
8f07f895 143 IN EFI_SYSTEM_CONTEXT SystemContext\r
144 );\r
145\r
e41aad15
JF
146/**\r
147 Internal worker function to initialize exception handler.\r
148\r
ab95e54d
JF
149 @param[in] VectorInfo Pointer to reserved vector list.\r
150 @param[in, out] ExceptionHandlerData Pointer to exception handler data.\r
dd563742
JF
151\r
152 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
e41aad15
JF
153 with default exception handlers.\r
154 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
155 @retval EFI_UNSUPPORTED This function is not supported.\r
156\r
157**/\r
158EFI_STATUS\r
159InitializeCpuExceptionHandlersWorker (\r
ab95e54d
JF
160 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r
161 IN OUT EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
e41aad15
JF
162 );\r
163\r
164/**\r
165 Registers a function to be called from the processor interrupt handler.\r
166\r
670f13af
JF
167 @param[in] InterruptType Defines which interrupt or exception to hook.\r
168 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
169 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
170 will be uninstalled\r
171 @param[in] ExceptionHandlerData Pointer to exception handler data.\r
e41aad15
JF
172\r
173 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
174 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
175 previously installed.\r
176 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
177 previously installed.\r
178 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
179 or this function is not supported.\r
3f25e6ea 180**/\r
e41aad15
JF
181EFI_STATUS\r
182RegisterCpuInterruptHandlerWorker (\r
183 IN EFI_EXCEPTION_TYPE InterruptType,\r
670f13af
JF
184 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler,\r
185 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
e41aad15
JF
186 );\r
187\r
188/**\r
189 Internal worker function to update IDT entries accordling to vector attributes.\r
190\r
9db15f81
JF
191 @param[in] IdtTable Pointer to IDT table.\r
192 @param[in] TemplateMap Pointer to a buffer where the address map is\r
193 returned.\r
194 @param[in] ExceptionHandlerData Pointer to exception handler data.\r
e41aad15
JF
195\r
196**/\r
197VOID\r
198UpdateIdtTable (\r
199 IN IA32_IDT_GATE_DESCRIPTOR *IdtTable,\r
200 IN EXCEPTION_HANDLER_TEMPLATE_MAP *TemplateMap,\r
9db15f81 201 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
e41aad15
JF
202 );\r
203\r
204/**\r
205 Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r
206\r
81b21fc2
JF
207 @param[in] ExceptionType Exception type.\r
208 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
209 @param[in] ExceptionHandlerData Pointer to exception handler data.\r
e41aad15
JF
210**/\r
211VOID\r
212ArchSaveExceptionContext (\r
81b21fc2
JF
213 IN UINTN ExceptionType,\r
214 IN EFI_SYSTEM_CONTEXT SystemContext,\r
215 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
e41aad15
JF
216 );\r
217\r
218/**\r
219 Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.\r
220\r
368c54e7
JF
221 @param[in] ExceptionType Exception type.\r
222 @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
223 @param[in] ExceptionHandlerData Pointer to exception handler data.\r
e41aad15
JF
224**/\r
225VOID\r
226ArchRestoreExceptionContext (\r
368c54e7
JF
227 IN UINTN ExceptionType,\r
228 IN EFI_SYSTEM_CONTEXT SystemContext,\r
229 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
e41aad15
JF
230 );\r
231\r
232/**\r
07da1ac8 233 Fix up the vector number and function address in the vector code.\r
dd563742 234\r
07da1ac8
AF
235 @param[in] NewVectorAddr New vector handler address.\r
236 @param[in] VectorNum Index of vector.\r
237 @param[in] OldVectorAddr Old vector handler address.\r
e41aad15
JF
238\r
239**/\r
240VOID\r
241EFIAPI\r
242AsmVectorNumFixup (\r
07da1ac8
AF
243 IN VOID *NewVectorAddr,\r
244 IN UINT8 VectorNum,\r
245 IN VOID *OldVectorAddr\r
e41aad15
JF
246 );\r
247\r
248/**\r
249 Read and save reserved vector information\r
dd563742 250\r
e41aad15
JF
251 @param[in] VectorInfo Pointer to reserved vector list.\r
252 @param[out] ReservedVector Pointer to reserved vector data buffer.\r
253 @param[in] VectorCount Vector number to be updated.\r
dd563742 254\r
e41aad15
JF
255 @return EFI_SUCCESS Read and save vector info successfully.\r
256 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
257\r
258**/\r
259EFI_STATUS\r
260ReadAndVerifyVectorInfo (\r
261 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,\r
262 OUT RESERVED_VECTORS_DATA *ReservedVector,\r
263 IN UINTN VectorCount\r
264 );\r
265\r
a51ee144
JF
266/**\r
267 Get ASCII format string exception name by exception type.\r
268\r
269 @param ExceptionType Exception type.\r
270\r
271 @return ASCII format string exception name.\r
272**/\r
273CONST CHAR8 *\r
274GetExceptionNameStr (\r
275 IN EFI_EXCEPTION_TYPE ExceptionType\r
276 );\r
277\r
44ecbc28
JF
278/**\r
279 Internal worker function for common exception handler.\r
280\r
281 @param ExceptionType Exception type.\r
282 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
283 @param ExceptionHandlerData Pointer to exception handler data.\r
284**/\r
285VOID\r
286CommonExceptionHandlerWorker (\r
dd563742 287 IN EFI_EXCEPTION_TYPE ExceptionType,\r
44ecbc28
JF
288 IN EFI_SYSTEM_CONTEXT SystemContext,\r
289 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
290 );\r
291\r
0ff5aa9c
JW
292/**\r
293 Setup separate stack for specific exceptions.\r
294\r
295 @param[in] StackSwitchData Pointer to data required for setuping up\r
296 stack switch.\r
297\r
298 @retval EFI_SUCCESS The exceptions have been successfully\r
299 initialized with new stack.\r
300 @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content.\r
301**/\r
302EFI_STATUS\r
7c4207e9 303ArchSetupExceptionStack (\r
0ff5aa9c
JW
304 IN CPU_EXCEPTION_INIT_DATA *StackSwitchData\r
305 );\r
306\r
307/**\r
308 Return address map of exception handler template so that C code can generate\r
309 exception tables. The template is only for exceptions using task gate instead\r
310 of interrupt gate.\r
311\r
312 @param AddressMap Pointer to a buffer where the address map is returned.\r
313**/\r
314VOID\r
315EFIAPI\r
316AsmGetTssTemplateMap (\r
317 OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap\r
318 );\r
319\r
8f07f895 320#endif\r
e41aad15 321\r