]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/Exception.c
Fix Duet broken caused by LzmaUefiDecompress's interface is changed in IntelFramework...
[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
162\r
8a4d81e6
A
163CHAR8 *gExceptionTypeString[] = {\r
164 "Reset",\r
165 "Undefined Instruction",\r
166 "SWI",\r
167 "Prefetch Abort",\r
168 "Data Abort",\r
169 "Undefined",\r
170 "IRQ",\r
171 "FIQ"\r
172};\r
2ef2b01e
A
173\r
174VOID\r
175EFIAPI\r
176CommonCExceptionHandler (\r
177 IN EFI_EXCEPTION_TYPE ExceptionType,\r
178 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
179 )\r
180{\r
181 BOOLEAN Dispatched = FALSE;\r
8a4d81e6 182 \r
d213712d 183 \r
2ef2b01e
A
184 if (ExceptionType <= MAX_ARM_EXCEPTION) {\r
185 if (gDebuggerExceptionHandlers[ExceptionType]) {\r
186 //\r
187 // If DebugSupport hooked the interrupt call the handler. This does not disable \r
188 // the normal handler.\r
189 //\r
190 gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
191 Dispatched = TRUE;\r
192 }\r
193 if (gExceptionHandlers[ExceptionType]) {\r
194 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
195 Dispatched = TRUE;\r
196 }\r
8a4d81e6
A
197 } else {\r
198 DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));\r
199 ASSERT (FALSE);\r
2ef2b01e
A
200 }\r
201\r
202 if (Dispatched) {\r
203 //\r
204 // We did work so this was an expected ExceptionType\r
205 //\r
206 return;\r
207 }\r
208 \r
209 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {\r
210 //\r
211 // ARM JTAG debuggers some times use this vector, so it is not an error to get one\r
212 //\r
213 return;\r
214 }\r
215\r
216 //\r
f659880b 217 // Code after here is the default exception handler... Dump the context\r
2ef2b01e 218 //\r
f659880b
A
219 DEBUG ((EFI_D_ERROR, "\n%a Exception from instruction at 0x%08x CPSR 0x%08x\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC, SystemContext.SystemContextArm->CPSR));\r
220 DEBUG_CODE_BEGIN ();\r
62e797a4
A
221 CHAR8 *Pdb;\r
222 UINT32 ImageBase;\r
223 UINT32 PeCoffSizeOfHeader;\r
224 UINT32 Offset;\r
f659880b
A
225 \r
226 Pdb = GetImageName (SystemContext.SystemContextArm->PC, &ImageBase, &PeCoffSizeOfHeader);\r
227 Offset = SystemContext.SystemContextArm->PC - ImageBase;\r
228 if (Pdb != NULL) {\r
229 DEBUG ((EFI_D_ERROR, "%a\n", Pdb));\r
2ef2b01e 230\r
f659880b
A
231 //\r
232 // A PE/COFF image loads its headers into memory so the headers are \r
233 // included in the linked addressess. ELF and Mach-O images do not\r
234 // include the headers so the first byte of the image is usually\r
235 // text (code). If you look at link maps from ELF or Mach-O images\r
236 // you need to subtact out the size of the PE/COFF header to get\r
237 // get the offset that matches the link map. \r
238 //\r
239 DEBUG ((EFI_D_ERROR, "loadded at 0x%08x (PE/COFF offset) 0x%08x (ELF or Mach-O offset) 0x%08x\n", ImageBase, Offset, Offset - PeCoffSizeOfHeader));\r
240 }\r
241 DEBUG_CODE_END ();\r
242 DEBUG ((EFI_D_ERROR, " 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
243 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
244 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
245 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
246 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
247\r
248 ASSERT (FALSE);\r
249// while (TRUE) {\r
250// CpuSleep ();\r
251// }\r
2ef2b01e
A
252}\r
253\r
254\r
255\r
256EFI_STATUS\r
257InitializeExceptions (\r
258 IN EFI_CPU_ARCH_PROTOCOL *Cpu\r
259 )\r
260{\r
261 EFI_STATUS Status;\r
262 UINTN Offset;\r
263 UINTN Length;\r
264 UINTN Index;\r
265 BOOLEAN Enabled;\r
266 EFI_PHYSICAL_ADDRESS Base;\r
267\r
62e797a4
A
268 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&gDebugImageTableHeader);\r
269 if (EFI_ERROR (Status)) {\r
270 gDebugImageTableHeader = NULL;\r
271 }\r
f659880b 272\r
2ef2b01e
A
273 //\r
274 // Disable interrupts\r
275 //\r
276 Cpu->GetInterruptState (Cpu, &Enabled);\r
277 Cpu->DisableInterrupt (Cpu);\r
278\r
279 //\r
280 // Initialize the C entry points for interrupts\r
281 //\r
282 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r
283 Status = RegisterInterruptHandler (Index, NULL);\r
284 ASSERT_EFI_ERROR (Status);\r
285 \r
286 Status = RegisterDebuggerInterruptHandler (Index, NULL);\r
287 ASSERT_EFI_ERROR (Status);\r
288 }\r
289 \r
290 //\r
8a4d81e6 291 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.\r
2ef2b01e
A
292 //\r
293 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
294\r
295 //\r
296 // Reserve space for the exception handlers\r
297 //\r
298 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r
299 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r
300 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r
301 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of \r
302 // EFI_NOT_FOUND, and continue in that case.\r
303 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r
8a4d81e6 304 ASSERT_EFI_ERROR (Status);\r
2ef2b01e
A
305 }\r
306\r
307 CopyMem ((VOID *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress), (VOID *)ExceptionHandlersStart, Length);\r
308\r
309 //\r
310 // Patch in the common Assembly exception handler\r
311 //\r
312 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r
313 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
314\r
315 // Flush Caches since we updated executable stuff\r
8a4d81e6 316 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
2ef2b01e
A
317\r
318 if (Enabled) {\r
319 // \r
320 // Restore interrupt state\r
321 //\r
322 Status = Cpu->EnableInterrupt (Cpu);\r
323 }\r
324\r
325 return Status;\r
326}\r