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.
16 #include <Library/CacheMaintenanceLib.h>
19 ExceptionHandlersStart (
24 ExceptionHandlersEnd (
29 CommonExceptionEntry (
34 AsmCommonExceptionEntry (
39 EFI_EXCEPTION_CALLBACK gExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
40 EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
45 This function registers and enables the handler specified by InterruptHandler for a processor
46 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
47 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
48 The installed handler is called once for each processor interrupt or exception.
50 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts
51 are enabled and FALSE if interrupts are disabled.
52 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
53 when a processor interrupt occurs. If this parameter is NULL, then the handler
56 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
57 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
59 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
61 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
65 RegisterInterruptHandler (
66 IN EFI_EXCEPTION_TYPE InterruptType
,
67 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
70 if (InterruptType
> MAX_ARM_EXCEPTION
) {
71 return EFI_UNSUPPORTED
;
74 if ((InterruptHandler
!= NULL
) && (gExceptionHandlers
[InterruptType
] != NULL
)) {
75 return EFI_ALREADY_STARTED
;
78 gExceptionHandlers
[InterruptType
] = InterruptHandler
;
85 This function registers and enables the handler specified by InterruptHandler for a processor
86 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
87 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
88 The installed handler is called once for each processor interrupt or exception.
90 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts
91 are enabled and FALSE if interrupts are disabled.
92 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
93 when a processor interrupt occurs. If this parameter is NULL, then the handler
96 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
97 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
99 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
100 previously installed.
101 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
105 RegisterDebuggerInterruptHandler (
106 IN EFI_EXCEPTION_TYPE InterruptType
,
107 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
110 if (InterruptType
> MAX_ARM_EXCEPTION
) {
111 return EFI_UNSUPPORTED
;
114 if ((InterruptHandler
!= NULL
) && (gDebuggerExceptionHandlers
[InterruptType
] != NULL
)) {
115 return EFI_ALREADY_STARTED
;
118 gDebuggerExceptionHandlers
[InterruptType
] = InterruptHandler
;
123 CHAR8
*gExceptionTypeString
[] = {
125 "Undefined Instruction",
136 CommonCExceptionHandler (
137 IN EFI_EXCEPTION_TYPE ExceptionType
,
138 IN OUT EFI_SYSTEM_CONTEXT SystemContext
141 BOOLEAN Dispatched
= FALSE
;
144 if (ExceptionType
<= MAX_ARM_EXCEPTION
) {
145 if (gDebuggerExceptionHandlers
[ExceptionType
]) {
147 // If DebugSupport hooked the interrupt call the handler. This does not disable
148 // the normal handler.
150 gDebuggerExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
153 if (gExceptionHandlers
[ExceptionType
]) {
154 gExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
158 DEBUG ((EFI_D_ERROR
, "Unknown exception type %d from %08x\n", ExceptionType
, SystemContext
.SystemContextArm
->PC
));
164 // We did work so this was an expected ExceptionType
169 if (ExceptionType
== EXCEPT_ARM_SOFTWARE_INTERRUPT
) {
171 // ARM JTAG debuggers some times use this vector, so it is not an error to get one
177 // Code after here is the default exception handler...
179 DEBUG ((EFI_D_ERROR
, "%a Exception from %08x\n", gExceptionTypeString
[ExceptionType
], SystemContext
.SystemContextArm
->PC
));
187 InitializeExceptions (
188 IN EFI_CPU_ARCH_PROTOCOL
*Cpu
196 EFI_PHYSICAL_ADDRESS Base
;
199 // Disable interrupts
201 Cpu
->GetInterruptState (Cpu
, &Enabled
);
202 Cpu
->DisableInterrupt (Cpu
);
205 // Initialize the C entry points for interrupts
207 for (Index
= 0; Index
<= MAX_ARM_EXCEPTION
; Index
++) {
208 Status
= RegisterInterruptHandler (Index
, NULL
);
209 ASSERT_EFI_ERROR (Status
);
211 Status
= RegisterDebuggerInterruptHandler (Index
, NULL
);
212 ASSERT_EFI_ERROR (Status
);
216 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.
218 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
221 // Reserve space for the exception handlers
223 Base
= (EFI_PHYSICAL_ADDRESS
)PcdGet32 (PcdCpuVectorBaseAddress
);
224 Status
= gBS
->AllocatePages (AllocateAddress
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES (Length
), &Base
);
225 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000
226 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of
227 // EFI_NOT_FOUND, and continue in that case.
228 if (EFI_ERROR(Status
) && (Status
!= EFI_NOT_FOUND
)) {
229 ASSERT_EFI_ERROR (Status
);
232 CopyMem ((VOID
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
), (VOID
*)ExceptionHandlersStart
, Length
);
235 // Patch in the common Assembly exception handler
237 Offset
= (UINTN
)CommonExceptionEntry
- (UINTN
)ExceptionHandlersStart
;
238 *(UINTN
*) ((UINT8
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
) + Offset
) = (UINTN
)AsmCommonExceptionEntry
;
240 // Flush Caches since we updated executable stuff
241 InvalidateInstructionCacheRange ((VOID
*)PcdGet32(PcdCpuVectorBaseAddress
), Length
);
245 // Restore interrupt state
247 Status
= Cpu
->EnableInterrupt (Cpu
);