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>
31 CopyExceptionHandlers(
32 IN PHYSICAL_ADDRESS BaseAddress
37 RegisterExceptionHandler(
38 IN EFI_EXCEPTION_TYPE ExceptionType
,
39 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
43 ExceptionHandlersStart(
52 RETURN_STATUS
ArchVectorConfig(
53 IN UINTN VectorBaseAddress
56 // these globals are provided by the architecture specific source (Arm or AArch64)
57 extern UINTN gMaxExceptionNumber
;
58 extern EFI_EXCEPTION_CALLBACK gExceptionHandlers
[];
59 extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[];
60 extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask
;
61 extern UINTN gDebuggerNoHandlerValue
;
63 // A compiler flag adjusts the compilation of this library to a variant where
64 // the vectors are relocated (copied) to another location versus using the
65 // vectors in-place. Since this effects an assembly .align directive we must
66 // address this at library build time. Since this affects the build of the
67 // library we cannot represent this in a PCD since PCDs are evaluated on
68 // a per-module basis.
69 #if defined(ARM_RELOCATE_VECTORS)
70 STATIC CONST BOOLEAN gArmRelocateVectorTable
= TRUE
;
72 STATIC CONST BOOLEAN gArmRelocateVectorTable
= FALSE
;
77 Initializes all CPU exceptions entries and provides the default exception handlers.
79 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
80 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
81 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
82 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
84 @param[in] VectorInfo Pointer to reserved vector list.
86 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
87 with default exception handlers.
88 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
89 @retval EFI_UNSUPPORTED This function is not supported.
94 InitializeCpuExceptionHandlers(
95 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
101 Status
= EFI_SUCCESS
;
103 // if we are requested to copy exceptin handlers to another location
104 if (gArmRelocateVectorTable
) {
106 VectorBase
= PcdGet64(PcdCpuVectorBaseAddress
);
107 Status
= CopyExceptionHandlers(VectorBase
);
110 else { // use VBAR to point to where our exception handlers are
112 // The vector table must be aligned for the architecture. If this
113 // assertion fails ensure the appropriate FFS alignment is in effect,
114 // which can be accomplished by ensuring the proper Align=X statement
115 // in the platform packaging rules. For ARM Align=32 is required and
116 // for AArch64 Align=4K is required. Align=Auto can be used but this
117 // is known to cause an issue with populating the reset vector area
118 // for encapsulated FVs.
119 ASSERT(((UINTN
)ExceptionHandlersStart
& gExceptionVectorAlignmentMask
) == 0);
121 // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector
122 // Base Address to point into CpuDxe code.
123 VectorBase
= (UINTN
)ExceptionHandlersStart
;
125 Status
= RETURN_SUCCESS
;
128 if (!RETURN_ERROR(Status
)) {
129 // call the architecture-specific routine to prepare for the new vector
130 // configuration to take effect
131 ArchVectorConfig(VectorBase
);
133 ArmWriteVBar(VectorBase
);
136 return RETURN_SUCCESS
;
140 Copies exception handlers to the speciifed address.
142 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
143 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
144 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
145 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
147 @param[in] VectorInfo Pointer to reserved vector list.
149 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
150 with default exception handlers.
151 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
152 @retval EFI_UNSUPPORTED This function is not supported.
157 CopyExceptionHandlers(
158 IN PHYSICAL_ADDRESS BaseAddress
161 RETURN_STATUS Status
;
166 // ensure that the destination value specifies an address meeting the vector alignment requirements
167 ASSERT ((BaseAddress
& gExceptionVectorAlignmentMask
) == 0);
170 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.
172 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
174 VectorBase
= (UINT32
*)(UINTN
)BaseAddress
;
176 if (FeaturePcdGet(PcdDebuggerExceptionSupport
) == TRUE
) {
177 // Save existing vector table, in case debugger is already hooked in
178 CopyMem((VOID
*)gDebuggerExceptionHandlers
, (VOID
*)VectorBase
, sizeof (EFI_EXCEPTION_CALLBACK
)* (gMaxExceptionNumber
+1));
181 // Copy our assembly code into the page that contains the exception vectors.
182 CopyMem((VOID
*)VectorBase
, (VOID
*)ExceptionHandlersStart
, Length
);
185 // Initialize the C entry points for interrupts
187 for (Index
= 0; Index
<= gMaxExceptionNumber
; Index
++) {
188 if (!FeaturePcdGet(PcdDebuggerExceptionSupport
) ||
189 (gDebuggerExceptionHandlers
[Index
] == 0) || (gDebuggerExceptionHandlers
[Index
] == (VOID
*)gDebuggerNoHandlerValue
)) {
191 Status
= RegisterExceptionHandler(Index
, NULL
);
192 ASSERT_EFI_ERROR(Status
);
195 // If the debugger has already hooked put its vector back
196 VectorBase
[Index
] = (UINT32
)(UINTN
)gDebuggerExceptionHandlers
[Index
];
200 // Flush Caches since we updated executable stuff
201 InvalidateInstructionCacheRange((VOID
*)(UINTN
)BaseAddress
, Length
);
203 return RETURN_SUCCESS
;
208 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
210 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
211 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
212 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
213 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
215 @param[in] VectorInfo Pointer to reserved vector list.
217 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
218 with default interrupt/exception handlers.
219 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
220 @retval EFI_UNSUPPORTED This function is not supported.
225 InitializeCpuInterruptHandlers(
226 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
229 // not needed, this is what the CPU driver is for
230 return EFI_UNSUPPORTED
;
234 Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only
235 provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt
238 This function registers and enables the handler specified by ExceptionHandler for a processor
239 interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the
240 handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.
241 The installed handler is called once for each processor interrupt or exception.
242 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
243 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
245 @param[in] ExceptionType Defines which interrupt or exception to hook.
246 @param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
247 when a processor interrupt occurs. If this parameter is NULL, then the handler
250 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
251 @retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was
252 previously installed.
253 @retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not
254 previously installed.
255 @retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,
256 or this function is not supported.
259 RegisterCpuInterruptHandler(
260 IN EFI_EXCEPTION_TYPE ExceptionType
,
261 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler
263 if (ExceptionType
> gMaxExceptionNumber
) {
264 return RETURN_UNSUPPORTED
;
267 if ((ExceptionHandler
!= NULL
) && (gExceptionHandlers
[ExceptionType
] != NULL
)) {
268 return RETURN_ALREADY_STARTED
;
271 gExceptionHandlers
[ExceptionType
] = ExceptionHandler
;
273 return RETURN_SUCCESS
;
277 Register exception handler.
279 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
280 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
281 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
282 of the UEFI 2.0 specification.
283 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
284 that is called when a processor interrupt occurs.
285 If this parameter is NULL, then the handler will be uninstalled.
287 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
288 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
289 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
290 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
295 RegisterExceptionHandler(
296 IN EFI_EXCEPTION_TYPE ExceptionType
,
297 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
300 return RegisterCpuInterruptHandler(ExceptionType
, InterruptHandler
);
305 CommonCExceptionHandler(
306 IN EFI_EXCEPTION_TYPE ExceptionType
,
307 IN OUT EFI_SYSTEM_CONTEXT SystemContext
310 if (ExceptionType
<= gMaxExceptionNumber
) {
311 if (gExceptionHandlers
[ExceptionType
]) {
312 gExceptionHandlers
[ExceptionType
](ExceptionType
, SystemContext
);
317 DEBUG((EFI_D_ERROR
, "Unknown exception type %d\n", ExceptionType
));
321 DefaultExceptionHandler(ExceptionType
, SystemContext
);
325 Initializes all CPU exceptions entries with optional extra initializations.
327 By default, this method should include all functionalities implemented by
328 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
329 This could be done by calling InitializeCpuExceptionHandlers() directly
330 in this method besides the extra works.
332 InitData is optional and its use and content are processor arch dependent.
333 The typical usage of it is to convey resources which have to be reserved
334 elsewhere and are necessary for the extra initializations of exception.
336 @param[in] VectorInfo Pointer to reserved vector list.
337 @param[in] InitData Pointer to data optional for extra initializations
340 @retval EFI_SUCCESS The exceptions have been successfully
342 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
348 InitializeCpuExceptionHandlersEx (
349 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
350 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
353 return InitializeCpuExceptionHandlers (VectorInfo
);