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