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