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