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-2021, Arm Limited. All rights reserved.<BR>
6 * Copyright (c) 2016 HP Development Company, L.P.
8 * SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/CpuExceptionHandlerLib.h>
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>
25 CopyExceptionHandlers (
26 IN PHYSICAL_ADDRESS BaseAddress
31 RegisterExceptionHandler (
32 IN EFI_EXCEPTION_TYPE ExceptionType
,
33 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
37 ExceptionHandlersStart (
42 ExceptionHandlersEnd (
48 IN UINTN VectorBaseAddress
51 // these globals are provided by the architecture specific source (Arm or AArch64)
52 extern UINTN gMaxExceptionNumber
;
53 extern EFI_EXCEPTION_CALLBACK gExceptionHandlers
[];
54 extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[];
55 extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask
;
56 extern UINTN gDebuggerNoHandlerValue
;
58 // A compiler flag adjusts the compilation of this library to a variant where
59 // the vectors are relocated (copied) to another location versus using the
60 // vectors in-place. Since this effects an assembly .align directive we must
61 // address this at library build time. Since this affects the build of the
62 // library we cannot represent this in a PCD since PCDs are evaluated on
63 // a per-module basis.
64 #if defined (ARM_RELOCATE_VECTORS)
65 STATIC CONST BOOLEAN gArmRelocateVectorTable
= TRUE
;
67 STATIC CONST BOOLEAN gArmRelocateVectorTable
= FALSE
;
71 Initializes all CPU exceptions entries and provides the default exception handlers.
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.
78 @param[in] VectorInfo Pointer to reserved vector list.
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.
88 InitializeCpuExceptionHandlers (
89 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
97 // if we are requested to copy exception handlers to another location
98 if (gArmRelocateVectorTable
) {
99 VectorBase
= PcdGet64 (PcdCpuVectorBaseAddress
);
100 Status
= CopyExceptionHandlers (VectorBase
);
102 // use VBAR to point to where our exception handlers are
104 // The vector table must be aligned for the architecture. If this
105 // assertion fails ensure the appropriate FFS alignment is in effect,
106 // which can be accomplished by ensuring the proper Align=X statement
107 // in the platform packaging rules. For ARM Align=32 is required and
108 // for AArch64 Align=4K is required. Align=Auto can be used but this
109 // is known to cause an issue with populating the reset vector area
110 // for encapsulated FVs.
111 ASSERT (((UINTN
)ExceptionHandlersStart
& gExceptionVectorAlignmentMask
) == 0);
113 // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector
114 // Base Address to point into CpuDxe code.
115 VectorBase
= (UINTN
)ExceptionHandlersStart
;
117 Status
= RETURN_SUCCESS
;
120 if (!RETURN_ERROR (Status
)) {
121 // call the architecture-specific routine to prepare for the new vector
122 // configuration to take effect
123 ArchVectorConfig (VectorBase
);
125 ArmWriteVBar (VectorBase
);
128 return RETURN_SUCCESS
;
132 Copies exception handlers to the specified address.
134 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
135 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
136 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
137 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
139 @param[in] VectorInfo Pointer to reserved vector list.
141 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
142 with default exception handlers.
143 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
144 @retval EFI_UNSUPPORTED This function is not supported.
149 CopyExceptionHandlers (
150 IN PHYSICAL_ADDRESS BaseAddress
153 RETURN_STATUS Status
;
158 // ensure that the destination value specifies an address meeting the vector alignment requirements
159 ASSERT ((BaseAddress
& gExceptionVectorAlignmentMask
) == 0);
162 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.
164 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
166 VectorBase
= (UINT32
*)(UINTN
)BaseAddress
;
168 if (FeaturePcdGet (PcdDebuggerExceptionSupport
) == TRUE
) {
169 // Save existing vector table, in case debugger is already hooked in
170 CopyMem ((VOID
*)gDebuggerExceptionHandlers
, (VOID
*)VectorBase
, sizeof (EFI_EXCEPTION_CALLBACK
)* (gMaxExceptionNumber
+1));
173 // Copy our assembly code into the page that contains the exception vectors.
174 CopyMem ((VOID
*)VectorBase
, (VOID
*)ExceptionHandlersStart
, Length
);
177 // Initialize the C entry points for interrupts
179 for (Index
= 0; Index
<= gMaxExceptionNumber
; Index
++) {
180 if (!FeaturePcdGet (PcdDebuggerExceptionSupport
) ||
181 (gDebuggerExceptionHandlers
[Index
] == 0) || (gDebuggerExceptionHandlers
[Index
] == (VOID
*)gDebuggerNoHandlerValue
))
183 Status
= RegisterExceptionHandler (Index
, NULL
);
184 ASSERT_EFI_ERROR (Status
);
186 // If the debugger has already hooked put its vector back
187 VectorBase
[Index
] = (UINT32
)(UINTN
)gDebuggerExceptionHandlers
[Index
];
191 // Flush Caches since we updated executable stuff
192 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)BaseAddress
, Length
);
194 return RETURN_SUCCESS
;
198 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
200 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
201 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
202 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
203 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
205 @param[in] VectorInfo Pointer to reserved vector list.
207 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
208 with default interrupt/exception handlers.
209 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
210 @retval EFI_UNSUPPORTED This function is not supported.
215 InitializeCpuInterruptHandlers (
216 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
219 // not needed, this is what the CPU driver is for
220 return EFI_UNSUPPORTED
;
224 Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only
225 provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt
228 This function registers and enables the handler specified by ExceptionHandler for a processor
229 interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the
230 handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.
231 The installed handler is called once for each processor interrupt or exception.
232 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
233 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
235 @param[in] ExceptionType Defines which interrupt or exception to hook.
236 @param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
237 when a processor interrupt occurs. If this parameter is NULL, then the handler
240 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
241 @retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was
242 previously installed.
243 @retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not
244 previously installed.
245 @retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,
246 or this function is not supported.
249 RegisterCpuInterruptHandler (
250 IN EFI_EXCEPTION_TYPE ExceptionType
,
251 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler
254 if (ExceptionType
> gMaxExceptionNumber
) {
255 return RETURN_UNSUPPORTED
;
258 if ((ExceptionHandler
!= NULL
) && (gExceptionHandlers
[ExceptionType
] != NULL
)) {
259 return RETURN_ALREADY_STARTED
;
262 gExceptionHandlers
[ExceptionType
] = ExceptionHandler
;
264 return RETURN_SUCCESS
;
268 Register exception handler.
270 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
271 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
272 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
273 of the UEFI 2.0 specification.
274 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
275 that is called when a processor interrupt occurs.
276 If this parameter is NULL, then the handler will be uninstalled.
278 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
279 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
280 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
281 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
286 RegisterExceptionHandler (
287 IN EFI_EXCEPTION_TYPE ExceptionType
,
288 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
291 return RegisterCpuInterruptHandler (ExceptionType
, InterruptHandler
);
296 CommonCExceptionHandler (
297 IN EFI_EXCEPTION_TYPE ExceptionType
,
298 IN OUT EFI_SYSTEM_CONTEXT SystemContext
301 if (ExceptionType
<= gMaxExceptionNumber
) {
302 if (gExceptionHandlers
[ExceptionType
]) {
303 gExceptionHandlers
[ExceptionType
](ExceptionType
, SystemContext
);
307 DEBUG ((DEBUG_ERROR
, "Unknown exception type %d\n", ExceptionType
));
311 DefaultExceptionHandler (ExceptionType
, SystemContext
);
315 Initializes all CPU exceptions entries with optional extra initializations.
317 By default, this method should include all functionalities implemented by
318 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
319 This could be done by calling InitializeCpuExceptionHandlers() directly
320 in this method besides the extra works.
322 InitData is optional and its use and content are processor arch dependent.
323 The typical usage of it is to convey resources which have to be reserved
324 elsewhere and are necessary for the extra initializations of exception.
326 @param[in] VectorInfo Pointer to reserved vector list.
327 @param[in] InitData Pointer to data optional for extra initializations
330 @retval EFI_SUCCESS The exceptions have been successfully
332 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
338 InitializeCpuExceptionHandlersEx (
339 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
,
340 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
343 return InitializeCpuExceptionHandlers (VectorInfo
);