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