Adding support for a single stack, GCC check in will follow
[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 extern BOOLEAN gExceptionContext;
19
20 VOID
21 ExceptionHandlersStart (
22 VOID
23 );
24
25 VOID
26 ExceptionHandlersEnd (
27 VOID
28 );
29
30 VOID
31 CommonExceptionEntry (
32 VOID
33 );
34
35 VOID
36 AsmCommonExceptionEntry (
37 VOID
38 );
39
40
41 EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];
42 EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];
43
44
45
46 /**
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.
51
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
56 will be uninstalled.
57
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
60 previously installed.
61 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
62 previously installed.
63 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
64
65 **/
66 EFI_STATUS
67 RegisterInterruptHandler (
68 IN EFI_EXCEPTION_TYPE InterruptType,
69 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
70 )
71 {
72 if (InterruptType > MAX_ARM_EXCEPTION) {
73 return EFI_UNSUPPORTED;
74 }
75
76 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {
77 return EFI_ALREADY_STARTED;
78 }
79
80 gExceptionHandlers[InterruptType] = InterruptHandler;
81
82 return EFI_SUCCESS;
83 }
84
85
86 /**
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.
91
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
96 will be uninstalled.
97
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.
104
105 **/
106 EFI_STATUS
107 RegisterDebuggerInterruptHandler (
108 IN EFI_EXCEPTION_TYPE InterruptType,
109 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
110 )
111 {
112 if (InterruptType > MAX_ARM_EXCEPTION) {
113 return EFI_UNSUPPORTED;
114 }
115
116 if ((InterruptHandler != NULL) && (gDebuggerExceptionHandlers[InterruptType] != NULL)) {
117 return EFI_ALREADY_STARTED;
118 }
119
120 gDebuggerExceptionHandlers[InterruptType] = InterruptHandler;
121
122 return EFI_SUCCESS;
123 }
124
125 CHAR8 *gExceptionTypeString[] = {
126 "Reset",
127 "Undefined Instruction",
128 "SWI",
129 "Prefetch Abort",
130 "Data Abort",
131 "Undefined",
132 "IRQ",
133 "FIQ"
134 };
135
136 VOID
137 EFIAPI
138 CommonCExceptionHandler (
139 IN EFI_EXCEPTION_TYPE ExceptionType,
140 IN OUT EFI_SYSTEM_CONTEXT SystemContext
141 )
142 {
143 BOOLEAN Dispatched = FALSE;
144
145 gExceptionContext = TRUE;
146
147 if (ExceptionType <= MAX_ARM_EXCEPTION) {
148 if (gDebuggerExceptionHandlers[ExceptionType]) {
149 //
150 // If DebugSupport hooked the interrupt call the handler. This does not disable
151 // the normal handler.
152 //
153 gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);
154 Dispatched = TRUE;
155 }
156 if (gExceptionHandlers[ExceptionType]) {
157 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);
158 Dispatched = TRUE;
159 }
160 } else {
161 DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));
162 ASSERT (FALSE);
163 }
164
165 gExceptionContext = FALSE;
166
167 if (Dispatched) {
168 //
169 // We did work so this was an expected ExceptionType
170 //
171 return;
172 }
173
174 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {
175 //
176 // ARM JTAG debuggers some times use this vector, so it is not an error to get one
177 //
178 return;
179 }
180
181 //
182 // Code after here is the default exception handler...
183 //
184 DEBUG ((EFI_D_ERROR, "%a Exception from %08x\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC));
185 ASSERT (FALSE);
186
187 }
188
189
190
191 EFI_STATUS
192 InitializeExceptions (
193 IN EFI_CPU_ARCH_PROTOCOL *Cpu
194 )
195 {
196 EFI_STATUS Status;
197 UINTN Offset;
198 UINTN Length;
199 UINTN Index;
200 BOOLEAN Enabled;
201 EFI_PHYSICAL_ADDRESS Base;
202
203 //
204 // Disable interrupts
205 //
206 Cpu->GetInterruptState (Cpu, &Enabled);
207 Cpu->DisableInterrupt (Cpu);
208
209 //
210 // Initialize the C entry points for interrupts
211 //
212 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {
213 Status = RegisterInterruptHandler (Index, NULL);
214 ASSERT_EFI_ERROR (Status);
215
216 Status = RegisterDebuggerInterruptHandler (Index, NULL);
217 ASSERT_EFI_ERROR (Status);
218 }
219
220 //
221 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.
222 //
223 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;
224
225 //
226 // Reserve space for the exception handlers
227 //
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);
235 }
236
237 CopyMem ((VOID *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress), (VOID *)ExceptionHandlersStart, Length);
238
239 //
240 // Patch in the common Assembly exception handler
241 //
242 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;
243 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;
244
245 // Flush Caches since we updated executable stuff
246 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);
247
248 if (Enabled) {
249 //
250 // Restore interrupt state
251 //
252 Status = Cpu->EnableInterrupt (Cpu);
253 }
254
255 return Status;
256 }