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