fa256e60f84fd549387c9b511c3fa9c792b8b945
[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 #include <Library/CacheMaintenanceLib.h>
17
18 VOID
19 ExceptionHandlersStart (
20 VOID
21 );
22
23 VOID
24 ExceptionHandlersEnd (
25 VOID
26 );
27
28 VOID
29 CommonExceptionEntry (
30 VOID
31 );
32
33 VOID
34 AsmCommonExceptionEntry (
35 VOID
36 );
37
38
39 EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];
40 EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];
41
42
43
44 /**
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.
49
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
54 will be uninstalled.
55
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
58 previously installed.
59 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
60 previously installed.
61 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
62
63 **/
64 EFI_STATUS
65 RegisterInterruptHandler (
66 IN EFI_EXCEPTION_TYPE InterruptType,
67 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
68 )
69 {
70 if (InterruptType > MAX_ARM_EXCEPTION) {
71 return EFI_UNSUPPORTED;
72 }
73
74 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {
75 return EFI_ALREADY_STARTED;
76 }
77
78 gExceptionHandlers[InterruptType] = InterruptHandler;
79
80 return EFI_SUCCESS;
81 }
82
83
84 /**
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.
89
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
94 will be uninstalled.
95
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
98 previously installed.
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.
102
103 **/
104 EFI_STATUS
105 RegisterDebuggerInterruptHandler (
106 IN EFI_EXCEPTION_TYPE InterruptType,
107 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
108 )
109 {
110 if (InterruptType > MAX_ARM_EXCEPTION) {
111 return EFI_UNSUPPORTED;
112 }
113
114 if ((InterruptHandler != NULL) && (gDebuggerExceptionHandlers[InterruptType] != NULL)) {
115 return EFI_ALREADY_STARTED;
116 }
117
118 gDebuggerExceptionHandlers[InterruptType] = InterruptHandler;
119
120 return EFI_SUCCESS;
121 }
122
123
124
125 VOID
126 EFIAPI
127 CommonCExceptionHandler (
128 IN EFI_EXCEPTION_TYPE ExceptionType,
129 IN OUT EFI_SYSTEM_CONTEXT SystemContext
130 )
131 {
132 BOOLEAN Dispatched = FALSE;
133
134 if (ExceptionType <= MAX_ARM_EXCEPTION) {
135 if (gDebuggerExceptionHandlers[ExceptionType]) {
136 //
137 // If DebugSupport hooked the interrupt call the handler. This does not disable
138 // the normal handler.
139 //
140 gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);
141 Dispatched = TRUE;
142 }
143 if (gExceptionHandlers[ExceptionType]) {
144 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);
145 Dispatched = TRUE;
146 }
147 }
148
149 if (Dispatched) {
150 //
151 // We did work so this was an expected ExceptionType
152 //
153 return;
154 }
155
156 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {
157 //
158 // ARM JTAG debuggers some times use this vector, so it is not an error to get one
159 //
160 return;
161 }
162
163 //
164 // Code after here is the default exception handler...
165 //
166 DEBUG ((EFI_D_ERROR, "Exception %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));
167 ASSERT (FALSE);
168
169 }
170
171
172
173 EFI_STATUS
174 InitializeExceptions (
175 IN EFI_CPU_ARCH_PROTOCOL *Cpu
176 )
177 {
178 EFI_STATUS Status;
179 UINTN Offset;
180 UINTN Length;
181 UINTN Index;
182 BOOLEAN Enabled;
183 EFI_PHYSICAL_ADDRESS Base;
184
185 //
186 // Disable interrupts
187 //
188 Cpu->GetInterruptState (Cpu, &Enabled);
189 Cpu->DisableInterrupt (Cpu);
190
191 //
192 // Initialize the C entry points for interrupts
193 //
194 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {
195 Status = RegisterInterruptHandler (Index, NULL);
196 ASSERT_EFI_ERROR (Status);
197
198 Status = RegisterDebuggerInterruptHandler (Index, NULL);
199 ASSERT_EFI_ERROR (Status);
200 }
201
202 //
203 // Copy an implementation of the ARM exception vectors to 0x0.
204 //
205 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;
206
207 //
208 // Reserve space for the exception handlers
209 //
210 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);
211 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);
212 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000
213 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of
214 // EFI_NOT_FOUND, and continue in that case.
215 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {
216 ASSERT_EFI_ERROR (Status);
217 }
218
219 CopyMem ((VOID *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress), (VOID *)ExceptionHandlersStart, Length);
220
221 //
222 // Patch in the common Assembly exception handler
223 //
224 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;
225 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;
226
227 // Flush Caches since we updated executable stuff
228 InvalidateInstructionCacheRange((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);
229
230 if (Enabled) {
231 //
232 // Restore interrupt state
233 //
234 Status = Cpu->EnableInterrupt (Cpu);
235 }
236
237 return Status;
238 }