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