Adding support for a single stack, GCC check in will follow
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / Exception.c
CommitLineData
2ef2b01e
A
1/** @file\r
2\r
3 Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r
4 \r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "CpuDxe.h" \r
16#include <Library/CacheMaintenanceLib.h>\r
17\r
8a4d81e6
A
18extern BOOLEAN gExceptionContext;\r
19\r
2ef2b01e
A
20VOID\r
21ExceptionHandlersStart (\r
22 VOID\r
23 );\r
24\r
25VOID\r
26ExceptionHandlersEnd (\r
27 VOID\r
28 );\r
29\r
30VOID\r
31CommonExceptionEntry (\r
32 VOID\r
33 );\r
34\r
35VOID\r
36AsmCommonExceptionEntry (\r
37 VOID\r
38 );\r
39\r
40\r
41EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
42EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
43\r
44\r
45\r
46/**\r
47 This function registers and enables the handler specified by InterruptHandler for a processor \r
48 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
49 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
50 The installed handler is called once for each processor interrupt or exception.\r
51\r
52 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r
53 are enabled and FALSE if interrupts are disabled.\r
54 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
55 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
56 will be uninstalled.\r
57\r
58 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
59 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
60 previously installed.\r
61 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
62 previously installed.\r
63 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
64\r
65**/\r
66EFI_STATUS\r
67RegisterInterruptHandler (\r
68 IN EFI_EXCEPTION_TYPE InterruptType,\r
69 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
70 )\r
71{\r
72 if (InterruptType > MAX_ARM_EXCEPTION) {\r
73 return EFI_UNSUPPORTED;\r
74 }\r
75\r
76 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {\r
77 return EFI_ALREADY_STARTED;\r
78 }\r
79\r
80 gExceptionHandlers[InterruptType] = InterruptHandler;\r
81\r
82 return EFI_SUCCESS;\r
83}\r
84\r
85\r
86/**\r
87 This function registers and enables the handler specified by InterruptHandler for a processor \r
88 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
89 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
90 The installed handler is called once for each processor interrupt or exception.\r
91\r
92 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r
93 are enabled and FALSE if interrupts are disabled.\r
94 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
95 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
96 will be uninstalled.\r
97\r
98 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
99 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
100 previously installed.\r
101 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
102 previously installed.\r
103 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
104\r
105**/\r
106EFI_STATUS\r
107RegisterDebuggerInterruptHandler (\r
108 IN EFI_EXCEPTION_TYPE InterruptType,\r
109 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
110 )\r
111{\r
112 if (InterruptType > MAX_ARM_EXCEPTION) {\r
113 return EFI_UNSUPPORTED;\r
114 }\r
115\r
116 if ((InterruptHandler != NULL) && (gDebuggerExceptionHandlers[InterruptType] != NULL)) {\r
117 return EFI_ALREADY_STARTED;\r
118 }\r
119\r
120 gDebuggerExceptionHandlers[InterruptType] = InterruptHandler;\r
121\r
122 return EFI_SUCCESS;\r
123}\r
124\r
8a4d81e6
A
125CHAR8 *gExceptionTypeString[] = {\r
126 "Reset",\r
127 "Undefined Instruction",\r
128 "SWI",\r
129 "Prefetch Abort",\r
130 "Data Abort",\r
131 "Undefined",\r
132 "IRQ",\r
133 "FIQ"\r
134};\r
2ef2b01e
A
135\r
136VOID\r
137EFIAPI\r
138CommonCExceptionHandler (\r
139 IN EFI_EXCEPTION_TYPE ExceptionType,\r
140 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
141 )\r
142{\r
143 BOOLEAN Dispatched = FALSE;\r
8a4d81e6
A
144 \r
145 gExceptionContext = TRUE; \r
2ef2b01e
A
146 \r
147 if (ExceptionType <= MAX_ARM_EXCEPTION) {\r
148 if (gDebuggerExceptionHandlers[ExceptionType]) {\r
149 //\r
150 // If DebugSupport hooked the interrupt call the handler. This does not disable \r
151 // the normal handler.\r
152 //\r
153 gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
154 Dispatched = TRUE;\r
155 }\r
156 if (gExceptionHandlers[ExceptionType]) {\r
157 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
158 Dispatched = TRUE;\r
159 }\r
8a4d81e6
A
160 } else {\r
161 DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));\r
162 ASSERT (FALSE);\r
2ef2b01e
A
163 }\r
164\r
8a4d81e6
A
165 gExceptionContext = FALSE; \r
166\r
2ef2b01e
A
167 if (Dispatched) {\r
168 //\r
169 // We did work so this was an expected ExceptionType\r
170 //\r
171 return;\r
172 }\r
173 \r
174 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {\r
175 //\r
176 // ARM JTAG debuggers some times use this vector, so it is not an error to get one\r
177 //\r
178 return;\r
179 }\r
180\r
181 //\r
182 // Code after here is the default exception handler...\r
183 //\r
8a4d81e6 184 DEBUG ((EFI_D_ERROR, "%a Exception from %08x\n", gExceptionTypeString[ExceptionType], SystemContext.SystemContextArm->PC));\r
2ef2b01e
A
185 ASSERT (FALSE);\r
186\r
187}\r
188\r
189\r
190\r
191EFI_STATUS\r
192InitializeExceptions (\r
193 IN EFI_CPU_ARCH_PROTOCOL *Cpu\r
194 )\r
195{\r
196 EFI_STATUS Status;\r
197 UINTN Offset;\r
198 UINTN Length;\r
199 UINTN Index;\r
200 BOOLEAN Enabled;\r
201 EFI_PHYSICAL_ADDRESS Base;\r
202\r
203 //\r
204 // Disable interrupts\r
205 //\r
206 Cpu->GetInterruptState (Cpu, &Enabled);\r
207 Cpu->DisableInterrupt (Cpu);\r
208\r
209 //\r
210 // Initialize the C entry points for interrupts\r
211 //\r
212 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r
213 Status = RegisterInterruptHandler (Index, NULL);\r
214 ASSERT_EFI_ERROR (Status);\r
215 \r
216 Status = RegisterDebuggerInterruptHandler (Index, NULL);\r
217 ASSERT_EFI_ERROR (Status);\r
218 }\r
219 \r
220 //\r
8a4d81e6 221 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.\r
2ef2b01e
A
222 //\r
223 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
224\r
225 //\r
226 // Reserve space for the exception handlers\r
227 //\r
228 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r
229 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r
230 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r
231 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of \r
232 // EFI_NOT_FOUND, and continue in that case.\r
233 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r
8a4d81e6 234 ASSERT_EFI_ERROR (Status);\r
2ef2b01e
A
235 }\r
236\r
237 CopyMem ((VOID *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress), (VOID *)ExceptionHandlersStart, Length);\r
238\r
239 //\r
240 // Patch in the common Assembly exception handler\r
241 //\r
242 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r
243 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
244\r
245 // Flush Caches since we updated executable stuff\r
8a4d81e6 246 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
2ef2b01e
A
247\r
248 if (Enabled) {\r
249 // \r
250 // Restore interrupt state\r
251 //\r
252 Status = Cpu->EnableInterrupt (Cpu);\r
253 }\r
254\r
255 return Status;\r
256}\r