]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPkg / Library / ArmExceptionLib / ArmExceptionLib.c
CommitLineData
2939c778
EC
1/* @file\r
2* Main file supporting the SEC Phase for Versatile Express\r
3*\r
4* Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
8b8b52ff 5* Copyright (c) 2011-2021, Arm Limited. All rights reserved.<BR>\r
2939c778 6* Copyright (c) 2016 HP Development Company, L.P.\r
e2ae0bed 7* Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
2939c778 8*\r
4059386c 9* SPDX-License-Identifier: BSD-2-Clause-Patent\r
2939c778
EC
10*\r
11**/\r
12\r
13#include <Uefi.h>\r
14#include <Library/CpuExceptionHandlerLib.h>\r
15\r
16#include <Library/ArmLib.h>\r
17#include <Library/PcdLib.h>\r
18#include <Library/CacheMaintenanceLib.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/DefaultExceptionHandlerLib.h>\r
23\r
8a771a2e 24STATIC\r
2939c778 25RETURN_STATUS\r
429309e0
MK
26CopyExceptionHandlers (\r
27 IN PHYSICAL_ADDRESS BaseAddress\r
2939c778
EC
28 );\r
29\r
30EFI_STATUS\r
31EFIAPI\r
429309e0
MK
32RegisterExceptionHandler (\r
33 IN EFI_EXCEPTION_TYPE ExceptionType,\r
34 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
2939c778
EC
35 );\r
36\r
37VOID\r
429309e0 38ExceptionHandlersStart (\r
2939c778
EC
39 VOID\r
40 );\r
41\r
42VOID\r
429309e0 43ExceptionHandlersEnd (\r
2939c778
EC
44 VOID\r
45 );\r
46\r
429309e0
MK
47RETURN_STATUS\r
48ArchVectorConfig (\r
49 IN UINTN VectorBaseAddress\r
2939c778
EC
50 );\r
51\r
52// these globals are provided by the architecture specific source (Arm or AArch64)\r
429309e0
MK
53extern UINTN gMaxExceptionNumber;\r
54extern EFI_EXCEPTION_CALLBACK gExceptionHandlers[];\r
55extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[];\r
56extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask;\r
57extern UINTN gDebuggerNoHandlerValue;\r
2939c778
EC
58\r
59// A compiler flag adjusts the compilation of this library to a variant where\r
60// the vectors are relocated (copied) to another location versus using the\r
61// vectors in-place. Since this effects an assembly .align directive we must\r
62// address this at library build time. Since this affects the build of the\r
63// library we cannot represent this in a PCD since PCDs are evaluated on\r
64// a per-module basis.\r
429309e0
MK
65#if defined (ARM_RELOCATE_VECTORS)\r
66STATIC CONST BOOLEAN gArmRelocateVectorTable = TRUE;\r
2939c778 67#else\r
429309e0 68STATIC CONST BOOLEAN gArmRelocateVectorTable = FALSE;\r
2939c778
EC
69#endif\r
70\r
2939c778
EC
71/**\r
72Initializes all CPU exceptions entries and provides the default exception handlers.\r
73\r
74Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
75persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
76If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
77If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
78\r
79@param[in] VectorInfo Pointer to reserved vector list.\r
80\r
81@retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
82with default exception handlers.\r
83@retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
84@retval EFI_UNSUPPORTED This function is not supported.\r
85\r
86**/\r
87EFI_STATUS\r
88EFIAPI\r
429309e0
MK
89InitializeCpuExceptionHandlers (\r
90 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
2939c778
EC
91 )\r
92{\r
429309e0
MK
93 RETURN_STATUS Status;\r
94 UINTN VectorBase;\r
2939c778
EC
95\r
96 Status = EFI_SUCCESS;\r
97\r
ff5fef14 98 // if we are requested to copy exception handlers to another location\r
2939c778 99 if (gArmRelocateVectorTable) {\r
429309e0
MK
100 VectorBase = PcdGet64 (PcdCpuVectorBaseAddress);\r
101 Status = CopyExceptionHandlers (VectorBase);\r
102 } else {\r
103 // use VBAR to point to where our exception handlers are\r
2939c778
EC
104\r
105 // The vector table must be aligned for the architecture. If this\r
106 // assertion fails ensure the appropriate FFS alignment is in effect,\r
107 // which can be accomplished by ensuring the proper Align=X statement\r
108 // in the platform packaging rules. For ARM Align=32 is required and\r
109 // for AArch64 Align=4K is required. Align=Auto can be used but this\r
110 // is known to cause an issue with populating the reset vector area\r
111 // for encapsulated FVs.\r
429309e0 112 ASSERT (((UINTN)ExceptionHandlersStart & gExceptionVectorAlignmentMask) == 0);\r
2939c778 113\r
f0bbcdf8 114 // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector\r
2939c778
EC
115 // Base Address to point into CpuDxe code.\r
116 VectorBase = (UINTN)ExceptionHandlersStart;\r
117\r
118 Status = RETURN_SUCCESS;\r
119 }\r
120\r
429309e0 121 if (!RETURN_ERROR (Status)) {\r
2939c778
EC
122 // call the architecture-specific routine to prepare for the new vector\r
123 // configuration to take effect\r
429309e0 124 ArchVectorConfig (VectorBase);\r
2939c778 125\r
429309e0 126 ArmWriteVBar (VectorBase);\r
2939c778
EC
127 }\r
128\r
129 return RETURN_SUCCESS;\r
130}\r
131\r
132/**\r
ff5fef14 133Copies exception handlers to the specified address.\r
2939c778
EC
134\r
135Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
136persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
137If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
138If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
139\r
140@param[in] VectorInfo Pointer to reserved vector list.\r
141\r
142@retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
143with default exception handlers.\r
144@retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
145@retval EFI_UNSUPPORTED This function is not supported.\r
146\r
147**/\r
8a771a2e 148STATIC\r
2939c778 149RETURN_STATUS\r
429309e0
MK
150CopyExceptionHandlers (\r
151 IN PHYSICAL_ADDRESS BaseAddress\r
2939c778
EC
152 )\r
153{\r
429309e0
MK
154 RETURN_STATUS Status;\r
155 UINTN Length;\r
156 UINTN Index;\r
157 UINT32 *VectorBase;\r
2939c778
EC
158\r
159 // ensure that the destination value specifies an address meeting the vector alignment requirements\r
160 ASSERT ((BaseAddress & gExceptionVectorAlignmentMask) == 0);\r
161\r
162 //\r
163 // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.\r
164 //\r
165 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
166\r
167 VectorBase = (UINT32 *)(UINTN)BaseAddress;\r
168\r
429309e0 169 if (FeaturePcdGet (PcdDebuggerExceptionSupport) == TRUE) {\r
2939c778 170 // Save existing vector table, in case debugger is already hooked in\r
429309e0 171 CopyMem ((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (EFI_EXCEPTION_CALLBACK)* (gMaxExceptionNumber+1));\r
2939c778
EC
172 }\r
173\r
174 // Copy our assembly code into the page that contains the exception vectors.\r
429309e0 175 CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r
2939c778
EC
176\r
177 //\r
178 // Initialize the C entry points for interrupts\r
179 //\r
180 for (Index = 0; Index <= gMaxExceptionNumber; Index++) {\r
429309e0
MK
181 if (!FeaturePcdGet (PcdDebuggerExceptionSupport) ||\r
182 (gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)gDebuggerNoHandlerValue))\r
183 {\r
184 Status = RegisterExceptionHandler (Index, NULL);\r
185 ASSERT_EFI_ERROR (Status);\r
186 } else {\r
2939c778
EC
187 // If the debugger has already hooked put its vector back\r
188 VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];\r
189 }\r
190 }\r
191\r
192 // Flush Caches since we updated executable stuff\r
429309e0 193 InvalidateInstructionCacheRange ((VOID *)(UINTN)BaseAddress, Length);\r
2939c778
EC
194\r
195 return RETURN_SUCCESS;\r
196}\r
197\r
2939c778
EC
198/**\r
199Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only\r
200provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt\r
201Protocol.)\r
202\r
203This function registers and enables the handler specified by ExceptionHandler for a processor\r
204interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the\r
205handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.\r
206The installed handler is called once for each processor interrupt or exception.\r
e2ae0bed
RN
207NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,\r
208otherwise EFI_UNSUPPORTED returned.\r
2939c778
EC
209\r
210@param[in] ExceptionType Defines which interrupt or exception to hook.\r
211@param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
212when a processor interrupt occurs. If this parameter is NULL, then the handler\r
213will be uninstalled.\r
214\r
215@retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
216@retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was\r
217previously installed.\r
218@retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not\r
219previously installed.\r
220@retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,\r
221or this function is not supported.\r
222**/\r
223RETURN_STATUS\r
429309e0
MK
224RegisterCpuInterruptHandler (\r
225 IN EFI_EXCEPTION_TYPE ExceptionType,\r
226 IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler\r
8b8b52ff
PG
227 )\r
228{\r
2939c778
EC
229 if (ExceptionType > gMaxExceptionNumber) {\r
230 return RETURN_UNSUPPORTED;\r
231 }\r
232\r
233 if ((ExceptionHandler != NULL) && (gExceptionHandlers[ExceptionType] != NULL)) {\r
234 return RETURN_ALREADY_STARTED;\r
235 }\r
236\r
237 gExceptionHandlers[ExceptionType] = ExceptionHandler;\r
238\r
239 return RETURN_SUCCESS;\r
240}\r
241\r
242/**\r
243Register exception handler.\r
244\r
245@param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.\r
246@param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and\r
247the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL\r
248of the UEFI 2.0 specification.\r
249@param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER\r
250that is called when a processor interrupt occurs.\r
251If this parameter is NULL, then the handler will be uninstalled.\r
252\r
253@retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
254@retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.\r
255@retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.\r
256@retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
257\r
258**/\r
259EFI_STATUS\r
260EFIAPI\r
429309e0
MK
261RegisterExceptionHandler (\r
262 IN EFI_EXCEPTION_TYPE ExceptionType,\r
263 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
2939c778
EC
264 )\r
265{\r
429309e0 266 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);\r
2939c778
EC
267}\r
268\r
269VOID\r
270EFIAPI\r
429309e0
MK
271CommonCExceptionHandler (\r
272 IN EFI_EXCEPTION_TYPE ExceptionType,\r
273 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
2939c778
EC
274 )\r
275{\r
276 if (ExceptionType <= gMaxExceptionNumber) {\r
277 if (gExceptionHandlers[ExceptionType]) {\r
278 gExceptionHandlers[ExceptionType](ExceptionType, SystemContext);\r
279 return;\r
280 }\r
429309e0
MK
281 } else {\r
282 DEBUG ((DEBUG_ERROR, "Unknown exception type %d\n", ExceptionType));\r
283 ASSERT (FALSE);\r
2939c778
EC
284 }\r
285\r
429309e0 286 DefaultExceptionHandler (ExceptionType, SystemContext);\r
2939c778 287}\r
4cb21e1e
JW
288\r
289/**\r
e2ae0bed 290 Setup separate stacks for certain exception handlers.\r
0f7bccf5 291 If the input Buffer and BufferSize are both NULL, use global variable if possible.\r
4cb21e1e 292\r
0f7bccf5
ZL
293 @param[in] Buffer Point to buffer used to separate exception stack.\r
294 @param[in, out] BufferSize On input, it indicates the byte size of Buffer.\r
295 If the size is not enough, the return status will\r
296 be EFI_BUFFER_TOO_SMALL, and output BufferSize\r
297 will be the size it needs.\r
4cb21e1e 298\r
e2ae0bed
RN
299 @retval EFI_SUCCESS The stacks are assigned successfully.\r
300 @retval EFI_UNSUPPORTED This function is not supported.\r
0f7bccf5 301 @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.\r
4cb21e1e
JW
302**/\r
303EFI_STATUS\r
304EFIAPI\r
e2ae0bed 305InitializeSeparateExceptionStacks (\r
0f7bccf5
ZL
306 IN VOID *Buffer,\r
307 IN OUT UINTN *BufferSize\r
4cb21e1e
JW
308 )\r
309{\r
e2ae0bed 310 return EFI_SUCCESS;\r
4cb21e1e 311}\r