2 * Main file supporting the SEC Phase for Versatile Express
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.
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
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.
19 #include <Library/CpuExceptionHandlerLib.h>
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>
30 CopyExceptionHandlers(
31 IN PHYSICAL_ADDRESS BaseAddress
36 RegisterExceptionHandler(
37 IN EFI_EXCEPTION_TYPE ExceptionType
,
38 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
42 ExceptionHandlersStart(
51 RETURN_STATUS
ArchVectorConfig(
52 IN UINTN VectorBaseAddress
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
;
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
;
71 BOOLEAN gArmRelocateVectorTable
= FALSE
;
76 Initializes all CPU exceptions entries and provides the default exception handlers.
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.
83 @param[in] VectorInfo Pointer to reserved vector list.
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.
93 InitializeCpuExceptionHandlers(
94 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
100 Status
= EFI_SUCCESS
;
102 // if we are requested to copy exceptin handlers to another location
103 if (gArmRelocateVectorTable
) {
105 VectorBase
= PcdGet32(PcdCpuVectorBaseAddress
);
106 Status
= CopyExceptionHandlers(VectorBase
);
109 else { // use VBAR to point to where our exception handlers are
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);
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
;
124 Status
= RETURN_SUCCESS
;
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
);
132 ArmWriteVBar(VectorBase
);
135 return RETURN_SUCCESS
;
139 Copies exception handlers to the speciifed address.
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.
146 @param[in] VectorInfo Pointer to reserved vector list.
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.
155 CopyExceptionHandlers(
156 IN PHYSICAL_ADDRESS BaseAddress
159 RETURN_STATUS Status
;
164 // ensure that the destination value specifies an address meeting the vector alignment requirements
165 ASSERT ((BaseAddress
& gExceptionVectorAlignmentMask
) == 0);
168 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.
170 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
172 VectorBase
= (UINT32
*)(UINTN
)BaseAddress
;
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));
179 // Copy our assembly code into the page that contains the exception vectors.
180 CopyMem((VOID
*)VectorBase
, (VOID
*)ExceptionHandlersStart
, Length
);
183 // Initialize the C entry points for interrupts
185 for (Index
= 0; Index
<= gMaxExceptionNumber
; Index
++) {
186 if (!FeaturePcdGet(PcdDebuggerExceptionSupport
) ||
187 (gDebuggerExceptionHandlers
[Index
] == 0) || (gDebuggerExceptionHandlers
[Index
] == (VOID
*)gDebuggerNoHandlerValue
)) {
189 Status
= RegisterExceptionHandler(Index
, NULL
);
190 ASSERT_EFI_ERROR(Status
);
193 // If the debugger has already hooked put its vector back
194 VectorBase
[Index
] = (UINT32
)(UINTN
)gDebuggerExceptionHandlers
[Index
];
198 // Flush Caches since we updated executable stuff
199 InvalidateInstructionCacheRange((VOID
*)(UINTN
)BaseAddress
, Length
);
201 return RETURN_SUCCESS
;
206 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
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.
213 @param[in] VectorInfo Pointer to reserved vector list.
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.
223 InitializeCpuInterruptHandlers(
224 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
227 // not needed, this is what the CPU driver is for
228 return EFI_UNSUPPORTED
;
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
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.
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
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.
257 RegisterCpuInterruptHandler(
258 IN EFI_EXCEPTION_TYPE ExceptionType
,
259 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler
261 if (ExceptionType
> gMaxExceptionNumber
) {
262 return RETURN_UNSUPPORTED
;
265 if ((ExceptionHandler
!= NULL
) && (gExceptionHandlers
[ExceptionType
] != NULL
)) {
266 return RETURN_ALREADY_STARTED
;
269 gExceptionHandlers
[ExceptionType
] = ExceptionHandler
;
271 return RETURN_SUCCESS
;
275 Register exception handler.
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.
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.
293 RegisterExceptionHandler(
294 IN EFI_EXCEPTION_TYPE ExceptionType
,
295 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
298 return RegisterCpuInterruptHandler(ExceptionType
, InterruptHandler
);
303 CommonCExceptionHandler(
304 IN EFI_EXCEPTION_TYPE ExceptionType
,
305 IN OUT EFI_SYSTEM_CONTEXT SystemContext
308 if (ExceptionType
<= gMaxExceptionNumber
) {
309 if (gExceptionHandlers
[ExceptionType
]) {
310 gExceptionHandlers
[ExceptionType
](ExceptionType
, SystemContext
);
315 DEBUG((EFI_D_ERROR
, "Unknown exception type %d\n", ExceptionType
));
319 DefaultExceptionHandler(ExceptionType
, SystemContext
);