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