]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c
ArmPkg: Replace BSD License with BSD+Patent License
[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 * 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 exceptin 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 speciifed 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 if (ExceptionType > gMaxExceptionNumber) {
258 return RETURN_UNSUPPORTED;
259 }
260
261 if ((ExceptionHandler != NULL) && (gExceptionHandlers[ExceptionType] != NULL)) {
262 return RETURN_ALREADY_STARTED;
263 }
264
265 gExceptionHandlers[ExceptionType] = ExceptionHandler;
266
267 return RETURN_SUCCESS;
268 }
269
270 /**
271 Register exception handler.
272
273 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
274 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
275 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
276 of the UEFI 2.0 specification.
277 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
278 that is called when a processor interrupt occurs.
279 If this parameter is NULL, then the handler will be uninstalled.
280
281 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
282 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
283 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
284 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 RegisterExceptionHandler(
290 IN EFI_EXCEPTION_TYPE ExceptionType,
291 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
292 )
293 {
294 return RegisterCpuInterruptHandler(ExceptionType, InterruptHandler);
295 }
296
297 VOID
298 EFIAPI
299 CommonCExceptionHandler(
300 IN EFI_EXCEPTION_TYPE ExceptionType,
301 IN OUT EFI_SYSTEM_CONTEXT SystemContext
302 )
303 {
304 if (ExceptionType <= gMaxExceptionNumber) {
305 if (gExceptionHandlers[ExceptionType]) {
306 gExceptionHandlers[ExceptionType](ExceptionType, SystemContext);
307 return;
308 }
309 }
310 else {
311 DEBUG((EFI_D_ERROR, "Unknown exception type %d\n", ExceptionType));
312 ASSERT(FALSE);
313 }
314
315 DefaultExceptionHandler(ExceptionType, SystemContext);
316 }
317
318 /**
319 Initializes all CPU exceptions entries with optional extra initializations.
320
321 By default, this method should include all functionalities implemented by
322 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
323 This could be done by calling InitializeCpuExceptionHandlers() directly
324 in this method besides the extra works.
325
326 InitData is optional and its use and content are processor arch dependent.
327 The typical usage of it is to convey resources which have to be reserved
328 elsewhere and are necessary for the extra initializations of exception.
329
330 @param[in] VectorInfo Pointer to reserved vector list.
331 @param[in] InitData Pointer to data optional for extra initializations
332 of exception.
333
334 @retval EFI_SUCCESS The exceptions have been successfully
335 initialized.
336 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
337 content.
338
339 **/
340 EFI_STATUS
341 EFIAPI
342 InitializeCpuExceptionHandlersEx (
343 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
344 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
345 )
346 {
347 return InitializeCpuExceptionHandlers (VectorInfo);
348 }
349