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>
18 extern BOOLEAN gExceptionContext
;
21 ExceptionHandlersStart (
26 ExceptionHandlersEnd (
31 CommonExceptionEntry (
36 AsmCommonExceptionEntry (
41 EFI_EXCEPTION_CALLBACK gExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
42 EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers
[MAX_ARM_EXCEPTION
+ 1];
47 This function registers and enables the handler specified by InterruptHandler for a processor
48 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
49 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
50 The installed handler is called once for each processor interrupt or exception.
52 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts
53 are enabled and FALSE if interrupts are disabled.
54 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
55 when a processor interrupt occurs. If this parameter is NULL, then the handler
58 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
59 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
61 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
63 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
67 RegisterInterruptHandler (
68 IN EFI_EXCEPTION_TYPE InterruptType
,
69 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
72 if (InterruptType
> MAX_ARM_EXCEPTION
) {
73 return EFI_UNSUPPORTED
;
76 if ((InterruptHandler
!= NULL
) && (gExceptionHandlers
[InterruptType
] != NULL
)) {
77 return EFI_ALREADY_STARTED
;
80 gExceptionHandlers
[InterruptType
] = InterruptHandler
;
87 This function registers and enables the handler specified by InterruptHandler for a processor
88 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
89 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
90 The installed handler is called once for each processor interrupt or exception.
92 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts
93 are enabled and FALSE if interrupts are disabled.
94 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
95 when a processor interrupt occurs. If this parameter is NULL, then the handler
98 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
99 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
100 previously installed.
101 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
102 previously installed.
103 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
107 RegisterDebuggerInterruptHandler (
108 IN EFI_EXCEPTION_TYPE InterruptType
,
109 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
112 if (InterruptType
> MAX_ARM_EXCEPTION
) {
113 return EFI_UNSUPPORTED
;
116 if ((InterruptHandler
!= NULL
) && (gDebuggerExceptionHandlers
[InterruptType
] != NULL
)) {
117 return EFI_ALREADY_STARTED
;
120 gDebuggerExceptionHandlers
[InterruptType
] = InterruptHandler
;
125 CHAR8
*gExceptionTypeString
[] = {
127 "Undefined Instruction",
138 CommonCExceptionHandler (
139 IN EFI_EXCEPTION_TYPE ExceptionType
,
140 IN OUT EFI_SYSTEM_CONTEXT SystemContext
143 BOOLEAN Dispatched
= FALSE
;
145 gExceptionContext
= TRUE
;
147 if (ExceptionType
<= MAX_ARM_EXCEPTION
) {
148 if (gDebuggerExceptionHandlers
[ExceptionType
]) {
150 // If DebugSupport hooked the interrupt call the handler. This does not disable
151 // the normal handler.
153 gDebuggerExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
156 if (gExceptionHandlers
[ExceptionType
]) {
157 gExceptionHandlers
[ExceptionType
] (ExceptionType
, SystemContext
);
161 DEBUG ((EFI_D_ERROR
, "Unknown exception type %d from %08x\n", ExceptionType
, SystemContext
.SystemContextArm
->PC
));
165 gExceptionContext
= FALSE
;
169 // We did work so this was an expected ExceptionType
174 if (ExceptionType
== EXCEPT_ARM_SOFTWARE_INTERRUPT
) {
176 // ARM JTAG debuggers some times use this vector, so it is not an error to get one
182 // Code after here is the default exception handler...
184 DEBUG ((EFI_D_ERROR
, "%a Exception from %08x\n", gExceptionTypeString
[ExceptionType
], SystemContext
.SystemContextArm
->PC
));
192 InitializeExceptions (
193 IN EFI_CPU_ARCH_PROTOCOL
*Cpu
201 EFI_PHYSICAL_ADDRESS Base
;
204 // Disable interrupts
206 Cpu
->GetInterruptState (Cpu
, &Enabled
);
207 Cpu
->DisableInterrupt (Cpu
);
210 // Initialize the C entry points for interrupts
212 for (Index
= 0; Index
<= MAX_ARM_EXCEPTION
; Index
++) {
213 Status
= RegisterInterruptHandler (Index
, NULL
);
214 ASSERT_EFI_ERROR (Status
);
216 Status
= RegisterDebuggerInterruptHandler (Index
, NULL
);
217 ASSERT_EFI_ERROR (Status
);
221 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.
223 Length
= (UINTN
)ExceptionHandlersEnd
- (UINTN
)ExceptionHandlersStart
;
226 // Reserve space for the exception handlers
228 Base
= (EFI_PHYSICAL_ADDRESS
)PcdGet32 (PcdCpuVectorBaseAddress
);
229 Status
= gBS
->AllocatePages (AllocateAddress
, EfiBootServicesCode
, EFI_SIZE_TO_PAGES (Length
), &Base
);
230 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000
231 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of
232 // EFI_NOT_FOUND, and continue in that case.
233 if (EFI_ERROR(Status
) && (Status
!= EFI_NOT_FOUND
)) {
234 ASSERT_EFI_ERROR (Status
);
237 CopyMem ((VOID
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
), (VOID
*)ExceptionHandlersStart
, Length
);
240 // Patch in the common Assembly exception handler
242 Offset
= (UINTN
)CommonExceptionEntry
- (UINTN
)ExceptionHandlersStart
;
243 *(UINTN
*) ((UINT8
*)(UINTN
)PcdGet32 (PcdCpuVectorBaseAddress
) + Offset
) = (UINTN
)AsmCommonExceptionEntry
;
245 // Flush Caches since we updated executable stuff
246 InvalidateInstructionCacheRange ((VOID
*)PcdGet32(PcdCpuVectorBaseAddress
), Length
);
250 // Restore interrupt state
252 Status
= Cpu
->EnableInterrupt (Cpu
);