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