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