]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/Exception.c
Updated ARM Exception handler to print out text values for CPSR register
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / Exception.c
CommitLineData
2ef2b01e
A
1/** @file\r
2\r
3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r
4 \r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "CpuDxe.h" \r
f659880b 16\r
62e797a4 17EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *gDebugImageTableHeader = NULL;\r
2ef2b01e
A
18\r
19VOID\r
20ExceptionHandlersStart (\r
21 VOID\r
22 );\r
23\r
24VOID\r
25ExceptionHandlersEnd (\r
26 VOID\r
27 );\r
28\r
29VOID\r
30CommonExceptionEntry (\r
31 VOID\r
32 );\r
33\r
34VOID\r
35AsmCommonExceptionEntry (\r
36 VOID\r
37 );\r
38\r
39\r
40EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
41EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
42\r
43\r
44\r
45/**\r
46 This function registers and enables the handler specified by InterruptHandler for a processor \r
47 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
48 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
49 The installed handler is called once for each processor interrupt or exception.\r
50\r
51 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r
52 are enabled and FALSE if interrupts are disabled.\r
53 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
54 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
55 will be uninstalled.\r
56\r
57 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
58 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
59 previously installed.\r
60 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
61 previously installed.\r
62 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
63\r
64**/\r
65EFI_STATUS\r
66RegisterInterruptHandler (\r
67 IN EFI_EXCEPTION_TYPE InterruptType,\r
68 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
69 )\r
70{\r
71 if (InterruptType > MAX_ARM_EXCEPTION) {\r
72 return EFI_UNSUPPORTED;\r
73 }\r
74\r
75 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {\r
76 return EFI_ALREADY_STARTED;\r
77 }\r
78\r
79 gExceptionHandlers[InterruptType] = InterruptHandler;\r
80\r
81 return EFI_SUCCESS;\r
82}\r
83\r
84\r
85/**\r
86 This function registers and enables the handler specified by InterruptHandler for a processor \r
87 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
88 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
89 The installed handler is called once for each processor interrupt or exception.\r
90\r
91 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r
92 are enabled and FALSE if interrupts are disabled.\r
93 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
94 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
95 will be uninstalled.\r
96\r
97 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
98 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
99 previously installed.\r
100 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
101 previously installed.\r
102 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
103\r
104**/\r
105EFI_STATUS\r
106RegisterDebuggerInterruptHandler (\r
107 IN EFI_EXCEPTION_TYPE InterruptType,\r
108 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
109 )\r
110{\r
111 if (InterruptType > MAX_ARM_EXCEPTION) {\r
112 return EFI_UNSUPPORTED;\r
113 }\r
114\r
115 if ((InterruptHandler != NULL) && (gDebuggerExceptionHandlers[InterruptType] != NULL)) {\r
116 return EFI_ALREADY_STARTED;\r
117 }\r
118\r
119 gDebuggerExceptionHandlers[InterruptType] = InterruptHandler;\r
120\r
121 return EFI_SUCCESS;\r
122}\r
123\r
f659880b 124\r
f659880b
A
125\r
126\r
127CHAR8 *\r
128GetImageName (\r
129 IN UINT32 FaultAddress,\r
130 OUT UINT32 *ImageBase,\r
131 OUT UINT32 *PeCoffSizeOfHeaders\r
132 )\r
133{\r
62e797a4
A
134 EFI_DEBUG_IMAGE_INFO *DebugTable;\r
135 UINTN Entry;\r
136 CHAR8 *Address;\r
137\r
f659880b
A
138 \r
139 DebugTable = gDebugImageTableHeader->EfiDebugImageInfoTable;\r
140 if (DebugTable == NULL) {\r
141 return NULL;\r
142 }\r
143\r
144 Address = (CHAR8 *)(UINTN)FaultAddress;\r
62e797a4
A
145 for (Entry = 0; Entry < gDebugImageTableHeader->TableSize; Entry++, DebugTable++) {\r
146 if (DebugTable->NormalImage != NULL) {\r
147 if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) && \r
148 (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {\r
149 if ((Address >= (CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase) &&\r
150 (Address <= ((CHAR8 *)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase + DebugTable->NormalImage->LoadedImageProtocolInstance->ImageSize))) {\r
151 *ImageBase = (UINT32)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;\r
152 *PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)*ImageBase);\r
153 return PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);\r
154 } \r
155 }\r
156 } \r
f659880b
A
157 }\r
158\r
159 return NULL;\r
160}\r
161\r
235605b5
A
162typedef struct {\r
163 UINT32 Bit;\r
164 CHAR8 Char;\r
165} CPSR_CHAR;\r
166\r
167CPSR_CHAR gCpsrChar[] = {\r
168 { 31, 'n' },\r
169 { 30, 'z' },\r
170 { 29, 'c' },\r
171 { 28, 'v' },\r
172 { 27, 'q' },\r
173\r
174 { 8, 'a' },\r
175 { 7, 'i' },\r
176 { 6, 'f' },\r
177 { 5, 't' },\r
178 { 0, '?' }\r
179};\r
180\r
f659880b 181\r
235605b5
A
182VOID\r
183CpsrString (\r
184 IN UINT32 Cpsr,\r
185 OUT CHAR8 *ReturnStr\r
186 )\r
187{\r
188 UINTN Index;\r
189 CHAR8 *Str = ReturnStr;\r
190 CHAR8 *ModeStr;\r
191 \r
192 for (Index = 0; gCpsrChar[Index].Bit != 0; Index++, Str++) {\r
193 *Str = gCpsrChar[Index].Char;\r
194 if ((Cpsr & (1 << gCpsrChar[Index].Bit)) != 0) {\r
195 // Concert to upper case if bit is set\r
196 *Str &= ~0x20;\r
197 }\r
198 }\r
199 \r
200 *Str++ = '_';\r
201 *Str = '\0';\r
202 \r
203 switch (Cpsr & 0x1f) {\r
204 case 0x17:\r
205 ModeStr = "abt";\r
206 break;\r
207 case 0x011:\r
208 ModeStr = "fiq";\r
209 break;\r
210 case 0x12:\r
211 ModeStr = "irq";\r
212 break;\r
213 case 0x13:\r
214 ModeStr = "svc";\r
215 break;\r
216 case 0x1f:\r
217 ModeStr = "sys";\r
218 break;\r
219 case 0x1b:\r
220 ModeStr = "und";\r
221 break;\r
222 case 0x10:\r
223 ModeStr = "usr";\r
224 break;\r
225 \r
226 default:\r
227 ModeStr = "???";\r
228 break;\r
229 }\r
230 \r
231 AsciiStrCat (Str, ModeStr);\r
232 return;\r
233} \r
234 \r
8a4d81e6
A
235CHAR8 *gExceptionTypeString[] = {\r
236 "Reset",\r
237 "Undefined Instruction",\r
238 "SWI",\r
239 "Prefetch Abort",\r
240 "Data Abort",\r
241 "Undefined",\r
242 "IRQ",\r
243 "FIQ"\r
244};\r
2ef2b01e
A
245\r
246VOID\r
247EFIAPI\r
248CommonCExceptionHandler (\r
249 IN EFI_EXCEPTION_TYPE ExceptionType,\r
250 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
251 )\r
252{\r
253 BOOLEAN Dispatched = FALSE;\r
8a4d81e6 254 \r
d213712d 255 \r
2ef2b01e
A
256 if (ExceptionType <= MAX_ARM_EXCEPTION) {\r
257 if (gDebuggerExceptionHandlers[ExceptionType]) {\r
258 //\r
259 // If DebugSupport hooked the interrupt call the handler. This does not disable \r
260 // the normal handler.\r
261 //\r
262 gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
263 Dispatched = TRUE;\r
264 }\r
265 if (gExceptionHandlers[ExceptionType]) {\r
266 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
267 Dispatched = TRUE;\r
268 }\r
8a4d81e6
A
269 } else {\r
270 DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));\r
271 ASSERT (FALSE);\r
2ef2b01e
A
272 }\r
273\r
274 if (Dispatched) {\r
275 //\r
276 // We did work so this was an expected ExceptionType\r
277 //\r
278 return;\r
279 }\r
280 \r
281 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {\r
282 //\r
283 // ARM JTAG debuggers some times use this vector, so it is not an error to get one\r
284 //\r
285 return;\r
286 }\r
287\r
288 //\r
f659880b 289 // Code after here is the default exception handler... Dump the context\r
2ef2b01e 290 //\r
235605b5 291 DEBUG ((EFI_D_ERROR, "\n%a Exception PC at 0x%08x CPSR 0x%08x ", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR));\r
f659880b 292 DEBUG_CODE_BEGIN ();\r
62e797a4
A
293 CHAR8 *Pdb;\r
294 UINT32 ImageBase;\r
295 UINT32 PeCoffSizeOfHeader;\r
296 UINT32 Offset;\r
235605b5
A
297 CHAR8 CpsrStr[32]; // char per bit. Lower 5-bits are mode that is a 3 char string\r
298 \r
299 CpsrString (SystemContext.SystemContextArm->CPSR, CpsrStr);\r
300 DEBUG ((EFI_D_ERROR, "%a\n", CpsrStr));\r
f659880b
A
301 \r
302 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);\r
303 Offset = SystemContext.SystemContextArm->PC - ImageBase;\r
304 if (Pdb != NULL) {\r
305 DEBUG ((EFI_D_ERROR, "%a\n", Pdb));\r
2ef2b01e 306\r
f659880b
A
307 //\r
308 // A PE/COFF image loads its headers into memory so the headers are \r
309 // included in the linked addressess. ELF and Mach-O images do not\r
310 // include the headers so the first byte of the image is usually\r
311 // text (code). If you look at link maps from ELF or Mach-O images\r
312 // you need to subtact out the size of the PE/COFF header to get\r
313 // get the offset that matches the link map. \r
314 //\r
235605b5 315 DEBUG ((EFI_D_ERROR, "loadded at 0x%08x (PE/COFF offset) 0x%x (ELF or Mach-O offset) 0x%x", ImageBase, Offset, Offset - PeCoffSizeOfHeader));\r
f659880b
A
316 }\r
317 DEBUG_CODE_END ();\r
235605b5
A
318 DEBUG ((EFI_D_ERROR, "\n R0 0x%08x R1 0x%08x R2 0x%08x R3 0x%08x\n", SystemContext.SystemContextArm->R0, SystemContext.SystemContextArm->R1, SystemContext.SystemContextArm->R2, SystemContext.SystemContextArm->R3));\r
319 DEBUG ((EFI_D_ERROR, " R4 0x%08x R5 0x%08x R6 0x%08x R7 0x%08x\n", SystemContext.SystemContextArm->R4, SystemContext.SystemContextArm->R5, SystemContext.SystemContextArm->R6, SystemContext.SystemContextArm->R7));\r
320 DEBUG ((EFI_D_ERROR, " R8 0x%08x R9 0x%08x R10 0x%08x R11 0x%08x\n", SystemContext.SystemContextArm->R8, SystemContext.SystemContextArm->R9, SystemContext.SystemContextArm->R10, SystemContext.SystemContextArm->R11));\r
321 DEBUG ((EFI_D_ERROR, " R12 0x%08x SP 0x%08x LR 0x%08x PC 0x%08x\n", SystemContext.SystemContextArm->R12, SystemContext.SystemContextArm->SP, SystemContext.SystemContextArm->LR, SystemContext.SystemContextArm->PC));\r
322 DEBUG ((EFI_D_ERROR, "DFSR 0x%08x DFAR 0x%08x IFSR 0x%08x IFAR 0x%08x\n\n", SystemContext.SystemContextArm->DFSR, SystemContext.SystemContextArm->DFAR, SystemContext.SystemContextArm->IFSR, SystemContext.SystemContextArm->IFAR));\r
f659880b
A
323\r
324 ASSERT (FALSE);\r
2ef2b01e
A
325}\r
326\r
327\r
328\r
329EFI_STATUS\r
330InitializeExceptions (\r
331 IN EFI_CPU_ARCH_PROTOCOL *Cpu\r
332 )\r
333{\r
334 EFI_STATUS Status;\r
335 UINTN Offset;\r
336 UINTN Length;\r
337 UINTN Index;\r
338 BOOLEAN Enabled;\r
339 EFI_PHYSICAL_ADDRESS Base;\r
340\r
62e797a4
A
341 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&gDebugImageTableHeader);\r
342 if (EFI_ERROR (Status)) {\r
343 gDebugImageTableHeader = NULL;\r
344 }\r
f659880b 345\r
2ef2b01e
A
346 //\r
347 // Disable interrupts\r
348 //\r
349 Cpu->GetInterruptState (Cpu, &Enabled);\r
350 Cpu->DisableInterrupt (Cpu);\r
351\r
352 //\r
353 // Initialize the C entry points for interrupts\r
354 //\r
355 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r
356 Status = RegisterInterruptHandler (Index, NULL);\r
357 ASSERT_EFI_ERROR (Status);\r
358 \r
359 Status = RegisterDebuggerInterruptHandler (Index, NULL);\r
360 ASSERT_EFI_ERROR (Status);\r
361 }\r
362 \r
363 //\r
8a4d81e6 364 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.\r
2ef2b01e
A
365 //\r
366 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
367\r
368 //\r
369 // Reserve space for the exception handlers\r
370 //\r
371 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r
372 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r
373 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r
374 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of \r
375 // EFI_NOT_FOUND, and continue in that case.\r
376 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r
8a4d81e6 377 ASSERT_EFI_ERROR (Status);\r
2ef2b01e
A
378 }\r
379\r
380 CopyMem ((VOID *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress), (VOID *)ExceptionHandlersStart, Length);\r
381\r
382 //\r
383 // Patch in the common Assembly exception handler\r
384 //\r
385 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r
386 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
387\r
388 // Flush Caches since we updated executable stuff\r
8a4d81e6 389 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
2ef2b01e
A
390\r
391 if (Enabled) {\r
392 // \r
393 // Restore interrupt state\r
394 //\r
395 Status = Cpu->EnableInterrupt (Cpu);\r
396 }\r
397\r
398 return Status;\r
399}\r