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.
7 * Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
9 * SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/CpuExceptionHandlerLib.h>
16 #include <Library/ArmLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/CacheMaintenanceLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/DefaultExceptionHandlerLib.h>
26 CopyExceptionHandlers (
27 IN PHYSICAL_ADDRESS BaseAddress
32 RegisterExceptionHandler (
33 IN EFI_EXCEPTION_TYPE ExceptionType
,
34 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
38 ExceptionHandlersStart (
43 ExceptionHandlersEnd (
49 IN UINTN VectorBaseAddress
52 // these globals are provided by the architecture specific source (Arm or AArch64)
53 extern UINTN gMaxExceptionNumber
;
54 extern EFI_EXCEPTION_CALLBACK gExceptionHandlers
[];
55 extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[];
56 extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask
;
57 extern UINTN gDebuggerNoHandlerValue
;
59 // A compiler flag adjusts the compilation of this library to a variant where
60 // the vectors are relocated (copied) to another location versus using the
61 // vectors in-place. Since this effects an assembly .align directive we must
62 // address this at library build time. Since this affects the build of the
63 // library we cannot represent this in a PCD since PCDs are evaluated on
64 // a per-module basis.
65 #if defined (ARM_RELOCATE_VECTORS)
66 STATIC CONST BOOLEAN gArmRelocateVectorTable
= TRUE
;
68 STATIC CONST BOOLEAN gArmRelocateVectorTable
= FALSE
;
72 Initializes all CPU exceptions entries and provides the default exception handlers.
74 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
75 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
76 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
77 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
79 @param[in] VectorInfo Pointer to reserved vector list.
81 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
82 with default exception handlers.
83 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
84 @retval EFI_UNSUPPORTED This function is not supported.
89 InitializeCpuExceptionHandlers (
90 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
98 // if we are requested to copy exception handlers to another location
99 if (gArmRelocateVectorTable
) {
100 VectorBase
= PcdGet64 (PcdCpuVectorBaseAddress
);
101 Status
= CopyExceptionHandlers (VectorBase
);
103 // use VBAR to point to where our exception handlers are
105 // The vector table must be aligned for the architecture. If this
106 // assertion fails ensure the appropriate FFS alignment is in effect,
107 // which can be accomplished by ensuring the proper Align=X statement
108 // in the platform packaging rules. For ARM Align=32 is required and
109 // for AArch64 Align=4K is required. Align=Auto can be used but this
110 // is known to cause an issue with populating the reset vector area
111 // for encapsulated FVs.
112 ASSERT (((UINTN
)ExceptionHandlersStart
& gExceptionVectorAlignmentMask
) == 0);
114 // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector
115 // Base Address to point into CpuDxe code.
116 VectorBase
= (UINTN
)ExceptionHandlersStart
;
118 Status
= RETURN_SUCCESS
;
121 if (!RETURN_ERROR (Status
)) {
122 // call the architecture-specific routine to prepare for the new vector
123 // configuration to take effect
124 ArchVectorConfig (VectorBase
);
126 ArmWriteVBar (VectorBase
);
129 return RETURN_SUCCESS
;
133 Copies exception handlers to the specified address.
135 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
136 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
137 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
138 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
140 @param[in] VectorInfo Pointer to reserved vector list.
142 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
143 with default exception handlers.
144 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
145 @retval EFI_UNSUPPORTED This function is not supported.
150 CopyExceptionHandlers (
151 IN PHYSICAL_ADDRESS BaseAddress
154 RETURN_STATUS Status
;
159 // ensure that the destination value specifies an address meeting the vector alignment requirements
160 ASSERT ((BaseAddress
& gExceptionVectorAlignmentMask
) == 0);
163 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.
165 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
167 VectorBase
= (UINT32
*)(UINTN
)BaseAddress
;
169 if (FeaturePcdGet (PcdDebuggerExceptionSupport
) == TRUE
) {
170 // Save existing vector table, in case debugger is already hooked in
171 CopyMem ((VOID
*)gDebuggerExceptionHandlers
, (VOID
*)VectorBase
, sizeof (EFI_EXCEPTION_CALLBACK
)* (gMaxExceptionNumber
+1));
174 // Copy our assembly code into the page that contains the exception vectors.
175 CopyMem ((VOID
*)VectorBase
, (VOID
*)ExceptionHandlersStart
, Length
);
178 // Initialize the C entry points for interrupts
180 for (Index
= 0; Index
<= gMaxExceptionNumber
; Index
++) {
181 if (!FeaturePcdGet (PcdDebuggerExceptionSupport
) ||
182 (gDebuggerExceptionHandlers
[Index
] == 0) || (gDebuggerExceptionHandlers
[Index
] == (VOID
*)gDebuggerNoHandlerValue
))
184 Status
= RegisterExceptionHandler (Index
, NULL
);
185 ASSERT_EFI_ERROR (Status
);
187 // If the debugger has already hooked put its vector back
188 VectorBase
[Index
] = (UINT32
)(UINTN
)gDebuggerExceptionHandlers
[Index
];
192 // Flush Caches since we updated executable stuff
193 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)BaseAddress
, Length
);
195 return RETURN_SUCCESS
;
199 Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only
200 provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt
203 This function registers and enables the handler specified by ExceptionHandler for a processor
204 interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the
205 handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.
206 The installed handler is called once for each processor interrupt or exception.
207 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
208 otherwise EFI_UNSUPPORTED returned.
210 @param[in] ExceptionType Defines which interrupt or exception to hook.
211 @param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
212 when a processor interrupt occurs. If this parameter is NULL, then the handler
215 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
216 @retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was
217 previously installed.
218 @retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not
219 previously installed.
220 @retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,
221 or this function is not supported.
224 RegisterCpuInterruptHandler (
225 IN EFI_EXCEPTION_TYPE ExceptionType
,
226 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler
229 if (ExceptionType
> gMaxExceptionNumber
) {
230 return RETURN_UNSUPPORTED
;
233 if ((ExceptionHandler
!= NULL
) && (gExceptionHandlers
[ExceptionType
] != NULL
)) {
234 return RETURN_ALREADY_STARTED
;
237 gExceptionHandlers
[ExceptionType
] = ExceptionHandler
;
239 return RETURN_SUCCESS
;
243 Register exception handler.
245 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
246 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
247 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
248 of the UEFI 2.0 specification.
249 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
250 that is called when a processor interrupt occurs.
251 If this parameter is NULL, then the handler will be uninstalled.
253 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
254 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
255 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
256 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
261 RegisterExceptionHandler (
262 IN EFI_EXCEPTION_TYPE ExceptionType
,
263 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
266 return RegisterCpuInterruptHandler (ExceptionType
, InterruptHandler
);
271 CommonCExceptionHandler (
272 IN EFI_EXCEPTION_TYPE ExceptionType
,
273 IN OUT EFI_SYSTEM_CONTEXT SystemContext
276 if (ExceptionType
<= gMaxExceptionNumber
) {
277 if (gExceptionHandlers
[ExceptionType
]) {
278 gExceptionHandlers
[ExceptionType
](ExceptionType
, SystemContext
);
282 DEBUG ((DEBUG_ERROR
, "Unknown exception type %d\n", ExceptionType
));
286 DefaultExceptionHandler (ExceptionType
, SystemContext
);
290 Setup separate stacks for certain exception handlers.
291 If the input Buffer and BufferSize are both NULL, use global variable if possible.
293 @param[in] Buffer Point to buffer used to separate exception stack.
294 @param[in, out] BufferSize On input, it indicates the byte size of Buffer.
295 If the size is not enough, the return status will
296 be EFI_BUFFER_TOO_SMALL, and output BufferSize
297 will be the size it needs.
299 @retval EFI_SUCCESS The stacks are assigned successfully.
300 @retval EFI_UNSUPPORTED This function is not supported.
301 @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.
305 InitializeSeparateExceptionStacks (
307 IN OUT UINTN
*BufferSize