]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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-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>
8 *
9 * SPDX-License-Identifier: BSD-2-Clause-Patent
10 *
11 **/
12
13 #include <Uefi.h>
14 #include <Library/CpuExceptionHandlerLib.h>
15
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>
23
24 STATIC
25 RETURN_STATUS
26 CopyExceptionHandlers (
27 IN PHYSICAL_ADDRESS BaseAddress
28 );
29
30 EFI_STATUS
31 EFIAPI
32 RegisterExceptionHandler (
33 IN EFI_EXCEPTION_TYPE ExceptionType,
34 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
35 );
36
37 VOID
38 ExceptionHandlersStart (
39 VOID
40 );
41
42 VOID
43 ExceptionHandlersEnd (
44 VOID
45 );
46
47 RETURN_STATUS
48 ArchVectorConfig (
49 IN UINTN VectorBaseAddress
50 );
51
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;
58
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;
67 #else
68 STATIC CONST BOOLEAN gArmRelocateVectorTable = FALSE;
69 #endif
70
71 /**
72 Initializes all CPU exceptions entries and provides the default exception handlers.
73
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.
78
79 @param[in] VectorInfo Pointer to reserved vector list.
80
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.
85
86 **/
87 EFI_STATUS
88 EFIAPI
89 InitializeCpuExceptionHandlers (
90 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
91 )
92 {
93 RETURN_STATUS Status;
94 UINTN VectorBase;
95
96 Status = EFI_SUCCESS;
97
98 // if we are requested to copy exception handlers to another location
99 if (gArmRelocateVectorTable) {
100 VectorBase = PcdGet64 (PcdCpuVectorBaseAddress);
101 Status = CopyExceptionHandlers (VectorBase);
102 } else {
103 // use VBAR to point to where our exception handlers are
104
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);
113
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;
117
118 Status = RETURN_SUCCESS;
119 }
120
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);
125
126 ArmWriteVBar (VectorBase);
127 }
128
129 return RETURN_SUCCESS;
130 }
131
132 /**
133 Copies exception handlers to the specified address.
134
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.
139
140 @param[in] VectorInfo Pointer to reserved vector list.
141
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.
146
147 **/
148 STATIC
149 RETURN_STATUS
150 CopyExceptionHandlers (
151 IN PHYSICAL_ADDRESS BaseAddress
152 )
153 {
154 RETURN_STATUS Status;
155 UINTN Length;
156 UINTN Index;
157 UINT32 *VectorBase;
158
159 // ensure that the destination value specifies an address meeting the vector alignment requirements
160 ASSERT ((BaseAddress & gExceptionVectorAlignmentMask) == 0);
161
162 //
163 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.
164 //
165 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;
166
167 VectorBase = (UINT32 *)(UINTN)BaseAddress;
168
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));
172 }
173
174 // Copy our assembly code into the page that contains the exception vectors.
175 CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);
176
177 //
178 // Initialize the C entry points for interrupts
179 //
180 for (Index = 0; Index <= gMaxExceptionNumber; Index++) {
181 if (!FeaturePcdGet (PcdDebuggerExceptionSupport) ||
182 (gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)gDebuggerNoHandlerValue))
183 {
184 Status = RegisterExceptionHandler (Index, NULL);
185 ASSERT_EFI_ERROR (Status);
186 } else {
187 // If the debugger has already hooked put its vector back
188 VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];
189 }
190 }
191
192 // Flush Caches since we updated executable stuff
193 InvalidateInstructionCacheRange ((VOID *)(UINTN)BaseAddress, Length);
194
195 return RETURN_SUCCESS;
196 }
197
198 /**
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
201 Protocol.)
202
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.
209
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
213 will be uninstalled.
214
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.
222 **/
223 RETURN_STATUS
224 RegisterCpuInterruptHandler (
225 IN EFI_EXCEPTION_TYPE ExceptionType,
226 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler
227 )
228 {
229 if (ExceptionType > gMaxExceptionNumber) {
230 return RETURN_UNSUPPORTED;
231 }
232
233 if ((ExceptionHandler != NULL) && (gExceptionHandlers[ExceptionType] != NULL)) {
234 return RETURN_ALREADY_STARTED;
235 }
236
237 gExceptionHandlers[ExceptionType] = ExceptionHandler;
238
239 return RETURN_SUCCESS;
240 }
241
242 /**
243 Register exception handler.
244
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.
252
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.
257
258 **/
259 EFI_STATUS
260 EFIAPI
261 RegisterExceptionHandler (
262 IN EFI_EXCEPTION_TYPE ExceptionType,
263 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
264 )
265 {
266 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);
267 }
268
269 VOID
270 EFIAPI
271 CommonCExceptionHandler (
272 IN EFI_EXCEPTION_TYPE ExceptionType,
273 IN OUT EFI_SYSTEM_CONTEXT SystemContext
274 )
275 {
276 if (ExceptionType <= gMaxExceptionNumber) {
277 if (gExceptionHandlers[ExceptionType]) {
278 gExceptionHandlers[ExceptionType](ExceptionType, SystemContext);
279 return;
280 }
281 } else {
282 DEBUG ((DEBUG_ERROR, "Unknown exception type %d\n", ExceptionType));
283 ASSERT (FALSE);
284 }
285
286 DefaultExceptionHandler (ExceptionType, SystemContext);
287 }
288
289 /**
290 Setup separate stacks for certain exception handlers.
291 If the input Buffer and BufferSize are both NULL, use global variable if possible.
292
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.
298
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.
302 **/
303 EFI_STATUS
304 EFIAPI
305 InitializeSeparateExceptionStacks (
306 IN VOID *Buffer,
307 IN OUT UINTN *BufferSize
308 )
309 {
310 return EFI_SUCCESS;
311 }