3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
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
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.
17 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER
*gDebugImageTableHeader
= NULL
;
20 ExceptionHandlersStart (
25 ExceptionHandlersEnd (
30 CommonExceptionEntry (
35 AsmCommonExceptionEntry (
40 EFI_EXCEPTION_CALLBACK gExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
41 EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
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.
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
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
60 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
62 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
66 RegisterInterruptHandler (
67 IN EFI_EXCEPTION_TYPE InterruptType
,
68 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
71 if (InterruptType
> MAX_ARM_EXCEPTION
) {
72 return EFI_UNSUPPORTED
;
75 if ((InterruptHandler
!= NULL
) && (gExceptionHandlers
[InterruptType
] != NULL
)) {
76 return EFI_ALREADY_STARTED
;
79 gExceptionHandlers
[InterruptType
] = InterruptHandler
;
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.
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
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
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.
106 RegisterDebuggerInterruptHandler (
107 IN EFI_EXCEPTION_TYPE InterruptType
,
108 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
111 if (InterruptType
> MAX_ARM_EXCEPTION
) {
112 return EFI_UNSUPPORTED
;
115 if ((InterruptHandler
!= NULL
) && (gDebuggerExceptionHandlers
[InterruptType
] != NULL
)) {
116 return EFI_ALREADY_STARTED
;
119 gDebuggerExceptionHandlers
[InterruptType
] = InterruptHandler
;
129 IN UINT32 FaultAddress
,
130 OUT UINT32
*ImageBase
,
131 OUT UINT32
*PeCoffSizeOfHeaders
134 EFI_DEBUG_IMAGE_INFO
*DebugTable
;
139 DebugTable
= gDebugImageTableHeader
->EfiDebugImageInfoTable
;
140 if (DebugTable
== NULL
) {
144 Address
= (CHAR8
*)(UINTN
)FaultAddress
;
145 for (Entry
= 0; Entry
< gDebugImageTableHeader
->TableSize
; Entry
++, DebugTable
++) {
146 if (DebugTable
->NormalImage
!= NULL
) {
147 if ((DebugTable
->NormalImage
->ImageInfoType
== EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
) &&
148 (DebugTable
->NormalImage
->LoadedImageProtocolInstance
!= NULL
)) {
149 if ((Address
>= (CHAR8
*)DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
) &&
150 (Address
<= ((CHAR8
*)DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
+ DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageSize
))) {
151 *ImageBase
= (UINT32
)DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
;
152 *PeCoffSizeOfHeaders
= PeCoffGetSizeOfHeaders ((VOID
*)(UINTN
)*ImageBase
);
153 return PeCoffLoaderGetPdbPointer (DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
);
167 CPSR_CHAR gCpsrChar
[] = {
189 CHAR8
*Str
= ReturnStr
;
192 for (Index
= 0; gCpsrChar
[Index
].Bit
!= 0; Index
++, Str
++) {
193 *Str
= gCpsrChar
[Index
].Char
;
194 if ((Cpsr
& (1 << gCpsrChar
[Index
].Bit
)) != 0) {
195 // Concert to upper case if bit is set
203 switch (Cpsr
& 0x1f) {
231 AsciiStrCat (Str
, ModeStr
);
235 CHAR8
*gExceptionTypeString
[] = {
237 "Undefined Instruction",
248 CommonCExceptionHandler (
249 IN EFI_EXCEPTION_TYPE ExceptionType
,
250 IN OUT EFI_SYSTEM_CONTEXT SystemContext
253 BOOLEAN Dispatched
= FALSE
;
256 if (ExceptionType
<= MAX_ARM_EXCEPTION
) {
257 if (gDebuggerExceptionHandlers
[ExceptionType
]) {
259 // If DebugSupport hooked the interrupt call the handler. This does not disable
260 // the normal handler.
262 gDebuggerExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
265 if (gExceptionHandlers
[ExceptionType
]) {
266 gExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
270 DEBUG ((EFI_D_ERROR
, "Unknown exception type %d from %08x\n", ExceptionType
, SystemContext
.SystemContextArm
->PC
));
276 // We did work so this was an expected ExceptionType
281 if (ExceptionType
== EXCEPT_ARM_SOFTWARE_INTERRUPT
) {
283 // ARM JTAG debuggers some times use this vector, so it is not an error to get one
289 // Code after here is the default exception handler... Dump the context
291 DEBUG ((EFI_D_ERROR
, "\n%a Exception PC at 0x%08x CPSR 0x%08x ", gExceptionTypeString
[ExceptionType
], SystemContext
.SystemContextArm
->PC
, SystemContext
.SystemContextArm
->CPSR
));
295 UINT32 PeCoffSizeOfHeader
;
297 CHAR8 CpsrStr
[32]; // char per bit. Lower 5-bits are mode that is a 3 char string
299 CpsrString (SystemContext
.SystemContextArm
->CPSR
, CpsrStr
);
300 DEBUG ((EFI_D_ERROR
, "%a\n", CpsrStr
));
302 Pdb
= GetImageName (SystemContext
.SystemContextArm
->PC
, &ImageBase
, &PeCoffSizeOfHeader
);
303 Offset
= SystemContext
.SystemContextArm
->PC
- ImageBase
;
305 DEBUG ((EFI_D_ERROR
, "%a\n", Pdb
));
308 // A PE/COFF image loads its headers into memory so the headers are
309 // included in the linked addressess. ELF and Mach-O images do not
310 // include the headers so the first byte of the image is usually
311 // text (code). If you look at link maps from ELF or Mach-O images
312 // you need to subtact out the size of the PE/COFF header to get
313 // get the offset that matches the link map.
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
));
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
));
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
));
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
));
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
));
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
));
330 InitializeExceptions (
331 IN EFI_CPU_ARCH_PROTOCOL
*Cpu
339 EFI_PHYSICAL_ADDRESS Base
;
341 Status
= EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid
, (VOID
**)&gDebugImageTableHeader
);
342 if (EFI_ERROR (Status
)) {
343 gDebugImageTableHeader
= NULL
;
347 // Disable interrupts
349 Cpu
->GetInterruptState (Cpu
, &Enabled
);
350 Cpu
->DisableInterrupt (Cpu
);
353 // Initialize the C entry points for interrupts
355 for (Index
= 0; Index
<= MAX_ARM_EXCEPTION
; Index
++) {
356 Status
= RegisterInterruptHandler (Index
, NULL
);
357 ASSERT_EFI_ERROR (Status
);
359 Status
= RegisterDebuggerInterruptHandler (Index
, NULL
);
360 ASSERT_EFI_ERROR (Status
);
364 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.
366 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
369 // Reserve space for the exception handlers
371 Base
= (EFI_PHYSICAL_ADDRESS
)PcdGet32 (PcdCpuVectorBaseAddress
);
372 Status
= gBS
->AllocatePages (AllocateAddress
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES (Length
), &Base
);
373 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000
374 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of
375 // EFI_NOT_FOUND, and continue in that case.
376 if (EFI_ERROR(Status
) && (Status
!= EFI_NOT_FOUND
)) {
377 ASSERT_EFI_ERROR (Status
);
380 CopyMem ((VOID
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
), (VOID
*)ExceptionHandlersStart
, Length
);
383 // Patch in the common Assembly exception handler
385 Offset
= (UINTN
)CommonExceptionEntry
- (UINTN
)ExceptionHandlersStart
;
386 *(UINTN
*) ((UINT8
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
) + Offset
) = (UINTN
)AsmCommonExceptionEntry
;
388 // Flush Caches since we updated executable stuff
389 InvalidateInstructionCacheRange ((VOID
*)PcdGet32(PcdCpuVectorBaseAddress
), Length
);
393 // Restore interrupt state
395 Status
= Cpu
->EnableInterrupt (Cpu
);