]>
Commit | Line | Data |
---|---|---|
2939c778 EC |
1 | /* @file\r |
2 | * Main file supporting the SEC Phase for Versatile Express\r | |
3 | *\r | |
4 | * Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
8b8b52ff | 5 | * Copyright (c) 2011-2021, Arm Limited. All rights reserved.<BR>\r |
2939c778 EC |
6 | * Copyright (c) 2016 HP Development Company, L.P.\r |
7 | *\r | |
4059386c | 8 | * SPDX-License-Identifier: BSD-2-Clause-Patent\r |
2939c778 EC |
9 | *\r |
10 | **/\r | |
11 | \r | |
12 | #include <Uefi.h>\r | |
13 | #include <Library/CpuExceptionHandlerLib.h>\r | |
14 | \r | |
15 | #include <Library/ArmLib.h>\r | |
16 | #include <Library/PcdLib.h>\r | |
17 | #include <Library/CacheMaintenanceLib.h>\r | |
18 | #include <Library/BaseLib.h>\r | |
19 | #include <Library/BaseMemoryLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/DefaultExceptionHandlerLib.h>\r | |
22 | \r | |
8a771a2e | 23 | STATIC\r |
2939c778 | 24 | RETURN_STATUS\r |
429309e0 MK |
25 | CopyExceptionHandlers (\r |
26 | IN PHYSICAL_ADDRESS BaseAddress\r | |
2939c778 EC |
27 | );\r |
28 | \r | |
29 | EFI_STATUS\r | |
30 | EFIAPI\r | |
429309e0 MK |
31 | RegisterExceptionHandler (\r |
32 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
33 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
2939c778 EC |
34 | );\r |
35 | \r | |
36 | VOID\r | |
429309e0 | 37 | ExceptionHandlersStart (\r |
2939c778 EC |
38 | VOID\r |
39 | );\r | |
40 | \r | |
41 | VOID\r | |
429309e0 | 42 | ExceptionHandlersEnd (\r |
2939c778 EC |
43 | VOID\r |
44 | );\r | |
45 | \r | |
429309e0 MK |
46 | RETURN_STATUS\r |
47 | ArchVectorConfig (\r | |
48 | IN UINTN VectorBaseAddress\r | |
2939c778 EC |
49 | );\r |
50 | \r | |
51 | // these globals are provided by the architecture specific source (Arm or AArch64)\r | |
429309e0 MK |
52 | extern UINTN gMaxExceptionNumber;\r |
53 | extern EFI_EXCEPTION_CALLBACK gExceptionHandlers[];\r | |
54 | extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[];\r | |
55 | extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask;\r | |
56 | extern UINTN gDebuggerNoHandlerValue;\r | |
2939c778 EC |
57 | \r |
58 | // A compiler flag adjusts the compilation of this library to a variant where\r | |
59 | // the vectors are relocated (copied) to another location versus using the\r | |
60 | // vectors in-place. Since this effects an assembly .align directive we must\r | |
61 | // address this at library build time. Since this affects the build of the\r | |
62 | // library we cannot represent this in a PCD since PCDs are evaluated on\r | |
63 | // a per-module basis.\r | |
429309e0 MK |
64 | #if defined (ARM_RELOCATE_VECTORS)\r |
65 | STATIC CONST BOOLEAN gArmRelocateVectorTable = TRUE;\r | |
2939c778 | 66 | #else\r |
429309e0 | 67 | STATIC CONST BOOLEAN gArmRelocateVectorTable = FALSE;\r |
2939c778 EC |
68 | #endif\r |
69 | \r | |
2939c778 EC |
70 | /**\r |
71 | Initializes all CPU exceptions entries and provides the default exception handlers.\r | |
72 | \r | |
73 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
74 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
75 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
76 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
77 | \r | |
78 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
79 | \r | |
80 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
81 | with default exception handlers.\r | |
82 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
83 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
84 | \r | |
85 | **/\r | |
86 | EFI_STATUS\r | |
87 | EFIAPI\r | |
429309e0 MK |
88 | InitializeCpuExceptionHandlers (\r |
89 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
2939c778 EC |
90 | )\r |
91 | {\r | |
429309e0 MK |
92 | RETURN_STATUS Status;\r |
93 | UINTN VectorBase;\r | |
2939c778 EC |
94 | \r |
95 | Status = EFI_SUCCESS;\r | |
96 | \r | |
ff5fef14 | 97 | // if we are requested to copy exception handlers to another location\r |
2939c778 | 98 | if (gArmRelocateVectorTable) {\r |
429309e0 MK |
99 | VectorBase = PcdGet64 (PcdCpuVectorBaseAddress);\r |
100 | Status = CopyExceptionHandlers (VectorBase);\r | |
101 | } else {\r | |
102 | // use VBAR to point to where our exception handlers are\r | |
2939c778 EC |
103 | \r |
104 | // The vector table must be aligned for the architecture. If this\r | |
105 | // assertion fails ensure the appropriate FFS alignment is in effect,\r | |
106 | // which can be accomplished by ensuring the proper Align=X statement\r | |
107 | // in the platform packaging rules. For ARM Align=32 is required and\r | |
108 | // for AArch64 Align=4K is required. Align=Auto can be used but this\r | |
109 | // is known to cause an issue with populating the reset vector area\r | |
110 | // for encapsulated FVs.\r | |
429309e0 | 111 | ASSERT (((UINTN)ExceptionHandlersStart & gExceptionVectorAlignmentMask) == 0);\r |
2939c778 | 112 | \r |
f0bbcdf8 | 113 | // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector\r |
2939c778 EC |
114 | // Base Address to point into CpuDxe code.\r |
115 | VectorBase = (UINTN)ExceptionHandlersStart;\r | |
116 | \r | |
117 | Status = RETURN_SUCCESS;\r | |
118 | }\r | |
119 | \r | |
429309e0 | 120 | if (!RETURN_ERROR (Status)) {\r |
2939c778 EC |
121 | // call the architecture-specific routine to prepare for the new vector\r |
122 | // configuration to take effect\r | |
429309e0 | 123 | ArchVectorConfig (VectorBase);\r |
2939c778 | 124 | \r |
429309e0 | 125 | ArmWriteVBar (VectorBase);\r |
2939c778 EC |
126 | }\r |
127 | \r | |
128 | return RETURN_SUCCESS;\r | |
129 | }\r | |
130 | \r | |
131 | /**\r | |
ff5fef14 | 132 | Copies exception handlers to the specified address.\r |
2939c778 EC |
133 | \r |
134 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
135 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
136 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
137 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
138 | \r | |
139 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
140 | \r | |
141 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
142 | with default exception handlers.\r | |
143 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
144 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
145 | \r | |
146 | **/\r | |
8a771a2e | 147 | STATIC\r |
2939c778 | 148 | RETURN_STATUS\r |
429309e0 MK |
149 | CopyExceptionHandlers (\r |
150 | IN PHYSICAL_ADDRESS BaseAddress\r | |
2939c778 EC |
151 | )\r |
152 | {\r | |
429309e0 MK |
153 | RETURN_STATUS Status;\r |
154 | UINTN Length;\r | |
155 | UINTN Index;\r | |
156 | UINT32 *VectorBase;\r | |
2939c778 EC |
157 | \r |
158 | // ensure that the destination value specifies an address meeting the vector alignment requirements\r | |
159 | ASSERT ((BaseAddress & gExceptionVectorAlignmentMask) == 0);\r | |
160 | \r | |
161 | //\r | |
162 | // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.\r | |
163 | //\r | |
164 | Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r | |
165 | \r | |
166 | VectorBase = (UINT32 *)(UINTN)BaseAddress;\r | |
167 | \r | |
429309e0 | 168 | if (FeaturePcdGet (PcdDebuggerExceptionSupport) == TRUE) {\r |
2939c778 | 169 | // Save existing vector table, in case debugger is already hooked in\r |
429309e0 | 170 | CopyMem ((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (EFI_EXCEPTION_CALLBACK)* (gMaxExceptionNumber+1));\r |
2939c778 EC |
171 | }\r |
172 | \r | |
173 | // Copy our assembly code into the page that contains the exception vectors.\r | |
429309e0 | 174 | CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r |
2939c778 EC |
175 | \r |
176 | //\r | |
177 | // Initialize the C entry points for interrupts\r | |
178 | //\r | |
179 | for (Index = 0; Index <= gMaxExceptionNumber; Index++) {\r | |
429309e0 MK |
180 | if (!FeaturePcdGet (PcdDebuggerExceptionSupport) ||\r |
181 | (gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)gDebuggerNoHandlerValue))\r | |
182 | {\r | |
183 | Status = RegisterExceptionHandler (Index, NULL);\r | |
184 | ASSERT_EFI_ERROR (Status);\r | |
185 | } else {\r | |
2939c778 EC |
186 | // If the debugger has already hooked put its vector back\r |
187 | VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];\r | |
188 | }\r | |
189 | }\r | |
190 | \r | |
191 | // Flush Caches since we updated executable stuff\r | |
429309e0 | 192 | InvalidateInstructionCacheRange ((VOID *)(UINTN)BaseAddress, Length);\r |
2939c778 EC |
193 | \r |
194 | return RETURN_SUCCESS;\r | |
195 | }\r | |
196 | \r | |
2939c778 EC |
197 | /**\r |
198 | Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r | |
199 | \r | |
200 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
201 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
202 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
203 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
204 | \r | |
205 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
206 | \r | |
207 | @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r | |
208 | with default interrupt/exception handlers.\r | |
209 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
210 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
211 | \r | |
212 | **/\r | |
213 | EFI_STATUS\r | |
214 | EFIAPI\r | |
429309e0 MK |
215 | InitializeCpuInterruptHandlers (\r |
216 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
217 | )\r | |
2939c778 EC |
218 | {\r |
219 | // not needed, this is what the CPU driver is for\r | |
220 | return EFI_UNSUPPORTED;\r | |
221 | }\r | |
222 | \r | |
223 | /**\r | |
224 | Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only\r | |
225 | provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt\r | |
226 | Protocol.)\r | |
227 | \r | |
228 | This function registers and enables the handler specified by ExceptionHandler for a processor\r | |
229 | interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the\r | |
230 | handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.\r | |
231 | The installed handler is called once for each processor interrupt or exception.\r | |
232 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r | |
233 | InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r | |
234 | \r | |
235 | @param[in] ExceptionType Defines which interrupt or exception to hook.\r | |
236 | @param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
237 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
238 | will be uninstalled.\r | |
239 | \r | |
240 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
241 | @retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was\r | |
242 | previously installed.\r | |
243 | @retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not\r | |
244 | previously installed.\r | |
245 | @retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,\r | |
246 | or this function is not supported.\r | |
247 | **/\r | |
248 | RETURN_STATUS\r | |
429309e0 MK |
249 | RegisterCpuInterruptHandler (\r |
250 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
251 | IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler\r | |
8b8b52ff PG |
252 | )\r |
253 | {\r | |
2939c778 EC |
254 | if (ExceptionType > gMaxExceptionNumber) {\r |
255 | return RETURN_UNSUPPORTED;\r | |
256 | }\r | |
257 | \r | |
258 | if ((ExceptionHandler != NULL) && (gExceptionHandlers[ExceptionType] != NULL)) {\r | |
259 | return RETURN_ALREADY_STARTED;\r | |
260 | }\r | |
261 | \r | |
262 | gExceptionHandlers[ExceptionType] = ExceptionHandler;\r | |
263 | \r | |
264 | return RETURN_SUCCESS;\r | |
265 | }\r | |
266 | \r | |
267 | /**\r | |
268 | Register exception handler.\r | |
269 | \r | |
270 | @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.\r | |
271 | @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and\r | |
272 | the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL\r | |
273 | of the UEFI 2.0 specification.\r | |
274 | @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER\r | |
275 | that is called when a processor interrupt occurs.\r | |
276 | If this parameter is NULL, then the handler will be uninstalled.\r | |
277 | \r | |
278 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
279 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.\r | |
280 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.\r | |
281 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r | |
282 | \r | |
283 | **/\r | |
284 | EFI_STATUS\r | |
285 | EFIAPI\r | |
429309e0 MK |
286 | RegisterExceptionHandler (\r |
287 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
288 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
2939c778 EC |
289 | )\r |
290 | {\r | |
429309e0 | 291 | return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);\r |
2939c778 EC |
292 | }\r |
293 | \r | |
294 | VOID\r | |
295 | EFIAPI\r | |
429309e0 MK |
296 | CommonCExceptionHandler (\r |
297 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
298 | IN OUT EFI_SYSTEM_CONTEXT SystemContext\r | |
2939c778 EC |
299 | )\r |
300 | {\r | |
301 | if (ExceptionType <= gMaxExceptionNumber) {\r | |
302 | if (gExceptionHandlers[ExceptionType]) {\r | |
303 | gExceptionHandlers[ExceptionType](ExceptionType, SystemContext);\r | |
304 | return;\r | |
305 | }\r | |
429309e0 MK |
306 | } else {\r |
307 | DEBUG ((DEBUG_ERROR, "Unknown exception type %d\n", ExceptionType));\r | |
308 | ASSERT (FALSE);\r | |
2939c778 EC |
309 | }\r |
310 | \r | |
429309e0 | 311 | DefaultExceptionHandler (ExceptionType, SystemContext);\r |
2939c778 | 312 | }\r |
4cb21e1e JW |
313 | \r |
314 | /**\r | |
315 | Initializes all CPU exceptions entries with optional extra initializations.\r | |
316 | \r | |
317 | By default, this method should include all functionalities implemented by\r | |
318 | InitializeCpuExceptionHandlers(), plus extra initialization works, if any.\r | |
319 | This could be done by calling InitializeCpuExceptionHandlers() directly\r | |
320 | in this method besides the extra works.\r | |
321 | \r | |
322 | InitData is optional and its use and content are processor arch dependent.\r | |
323 | The typical usage of it is to convey resources which have to be reserved\r | |
324 | elsewhere and are necessary for the extra initializations of exception.\r | |
325 | \r | |
326 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
327 | @param[in] InitData Pointer to data optional for extra initializations\r | |
328 | of exception.\r | |
329 | \r | |
330 | @retval EFI_SUCCESS The exceptions have been successfully\r | |
331 | initialized.\r | |
332 | @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r | |
333 | content.\r | |
334 | \r | |
335 | **/\r | |
336 | EFI_STATUS\r | |
337 | EFIAPI\r | |
338 | InitializeCpuExceptionHandlersEx (\r | |
429309e0 MK |
339 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r |
340 | IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r | |
4cb21e1e JW |
341 | )\r |
342 | {\r | |
343 | return InitializeCpuExceptionHandlers (VectorInfo);\r | |
344 | }\r |