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