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