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