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