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