]>
Commit | Line | Data |
---|---|---|
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 | |
f659880b | 16 | \r |
2ef2b01e A |
17 | \r |
18 | VOID\r | |
19 | ExceptionHandlersStart (\r | |
20 | VOID\r | |
21 | );\r | |
22 | \r | |
23 | VOID\r | |
24 | ExceptionHandlersEnd (\r | |
25 | VOID\r | |
26 | );\r | |
27 | \r | |
28 | VOID\r | |
29 | CommonExceptionEntry (\r | |
30 | VOID\r | |
31 | );\r | |
32 | \r | |
33 | VOID\r | |
34 | AsmCommonExceptionEntry (\r | |
35 | VOID\r | |
36 | );\r | |
37 | \r | |
38 | \r | |
39 | EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r | |
40 | EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r | |
41 | \r | |
42 | \r | |
43 | \r | |
44 | /**\r | |
45 | This function registers and enables the handler specified by InterruptHandler for a processor \r | |
46 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r | |
47 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r | |
48 | The installed handler is called once for each processor interrupt or exception.\r | |
49 | \r | |
50 | @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r | |
51 | are enabled and FALSE if interrupts are disabled.\r | |
52 | @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
53 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
54 | will be uninstalled.\r | |
55 | \r | |
56 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
57 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r | |
58 | previously installed.\r | |
59 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r | |
60 | previously installed.\r | |
61 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r | |
62 | \r | |
63 | **/\r | |
64 | EFI_STATUS\r | |
65 | RegisterInterruptHandler (\r | |
66 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
67 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
68 | )\r | |
69 | {\r | |
70 | if (InterruptType > MAX_ARM_EXCEPTION) {\r | |
71 | return EFI_UNSUPPORTED;\r | |
72 | }\r | |
73 | \r | |
74 | if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {\r | |
75 | return EFI_ALREADY_STARTED;\r | |
76 | }\r | |
77 | \r | |
78 | gExceptionHandlers[InterruptType] = InterruptHandler;\r | |
79 | \r | |
80 | return EFI_SUCCESS;\r | |
81 | }\r | |
82 | \r | |
83 | \r | |
84 | /**\r | |
85 | This function registers and enables the handler specified by InterruptHandler for a processor \r | |
86 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r | |
87 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r | |
88 | The installed handler is called once for each processor interrupt or exception.\r | |
89 | \r | |
90 | @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r | |
91 | are enabled and FALSE if interrupts are disabled.\r | |
92 | @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
93 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
94 | will be uninstalled.\r | |
95 | \r | |
96 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
97 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r | |
98 | previously installed.\r | |
99 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r | |
100 | previously installed.\r | |
101 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r | |
102 | \r | |
103 | **/\r | |
104 | EFI_STATUS\r | |
105 | RegisterDebuggerInterruptHandler (\r | |
106 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
107 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
108 | )\r | |
109 | {\r | |
110 | if (InterruptType > MAX_ARM_EXCEPTION) {\r | |
111 | return EFI_UNSUPPORTED;\r | |
112 | }\r | |
113 | \r | |
114 | if ((InterruptHandler != NULL) && (gDebuggerExceptionHandlers[InterruptType] != NULL)) {\r | |
115 | return EFI_ALREADY_STARTED;\r | |
116 | }\r | |
117 | \r | |
118 | gDebuggerExceptionHandlers[InterruptType] = InterruptHandler;\r | |
119 | \r | |
120 | return EFI_SUCCESS;\r | |
121 | }\r | |
122 | \r | |
f659880b | 123 | \r |
f659880b | 124 | \r |
2ef2b01e A |
125 | VOID\r |
126 | EFIAPI\r | |
127 | CommonCExceptionHandler (\r | |
128 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
129 | IN OUT EFI_SYSTEM_CONTEXT SystemContext\r | |
130 | )\r | |
131 | {\r | |
132 | BOOLEAN Dispatched = FALSE;\r | |
8a4d81e6 | 133 | \r |
d213712d | 134 | \r |
2ef2b01e A |
135 | if (ExceptionType <= MAX_ARM_EXCEPTION) {\r |
136 | if (gDebuggerExceptionHandlers[ExceptionType]) {\r | |
137 | //\r | |
138 | // If DebugSupport hooked the interrupt call the handler. This does not disable \r | |
139 | // the normal handler.\r | |
140 | //\r | |
141 | gDebuggerExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r | |
142 | Dispatched = TRUE;\r | |
143 | }\r | |
144 | if (gExceptionHandlers[ExceptionType]) {\r | |
145 | gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r | |
146 | Dispatched = TRUE;\r | |
147 | }\r | |
8a4d81e6 A |
148 | } else {\r |
149 | DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));\r | |
150 | ASSERT (FALSE);\r | |
2ef2b01e A |
151 | }\r |
152 | \r | |
153 | if (Dispatched) {\r | |
154 | //\r | |
155 | // We did work so this was an expected ExceptionType\r | |
156 | //\r | |
157 | return;\r | |
158 | }\r | |
159 | \r | |
160 | if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {\r | |
161 | //\r | |
162 | // ARM JTAG debuggers some times use this vector, so it is not an error to get one\r | |
163 | //\r | |
164 | return;\r | |
165 | }\r | |
166 | \r | |
6f72e28d | 167 | DefaultExceptionHandler (ExceptionType, SystemContext);\r |
2ef2b01e A |
168 | }\r |
169 | \r | |
170 | \r | |
171 | \r | |
172 | EFI_STATUS\r | |
173 | InitializeExceptions (\r | |
174 | IN EFI_CPU_ARCH_PROTOCOL *Cpu\r | |
175 | )\r | |
176 | {\r | |
177 | EFI_STATUS Status;\r | |
178 | UINTN Offset;\r | |
179 | UINTN Length;\r | |
180 | UINTN Index;\r | |
0416278c | 181 | BOOLEAN IrqEnabled;\r |
182 | BOOLEAN FiqEnabled;\r | |
2ef2b01e | 183 | EFI_PHYSICAL_ADDRESS Base;\r |
41d47802 | 184 | UINT32 *VectorBase;\r |
2ef2b01e A |
185 | \r |
186 | //\r | |
187 | // Disable interrupts\r | |
188 | //\r | |
0416278c | 189 | Cpu->GetInterruptState (Cpu, &IrqEnabled);\r |
2ef2b01e A |
190 | Cpu->DisableInterrupt (Cpu);\r |
191 | \r | |
0416278c | 192 | //\r |
193 | // EFI does not use the FIQ, but a debugger might so we must disable \r | |
194 | // as we take over the exception vectors. \r | |
195 | //\r | |
196 | FiqEnabled = ArmGetFiqState ();\r | |
197 | ArmDisableFiq ();\r | |
198 | \r | |
2ef2b01e | 199 | //\r |
8a4d81e6 | 200 | // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.\r |
2ef2b01e A |
201 | //\r |
202 | Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r | |
203 | \r | |
204 | //\r | |
205 | // Reserve space for the exception handlers\r | |
206 | //\r | |
207 | Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r | |
41d47802 | 208 | VectorBase = (UINT32 *)(UINTN)Base;\r |
2ef2b01e A |
209 | Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r |
210 | // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r | |
211 | // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of \r | |
212 | // EFI_NOT_FOUND, and continue in that case.\r | |
213 | if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r | |
8a4d81e6 | 214 | ASSERT_EFI_ERROR (Status);\r |
2ef2b01e A |
215 | }\r |
216 | \r | |
41d47802 | 217 | // Save existing vector table, in case debugger is already hooked in\r |
218 | CopyMem ((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (gDebuggerExceptionHandlers));\r | |
219 | \r | |
220 | //\r | |
221 | // Initialize the C entry points for interrupts\r | |
222 | //\r | |
223 | for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r | |
224 | Status = RegisterInterruptHandler (Index, NULL);\r | |
225 | ASSERT_EFI_ERROR (Status);\r | |
226 | \r | |
227 | if (VectorBase[Index] == 0xEAFFFFFE) {\r | |
228 | // Exception handler contains branch to vector location (jmp $) so no handler\r | |
229 | // NOTE: This code assumes vectors are ARM and not Thumb code\r | |
230 | gDebuggerExceptionHandlers[Index] = NULL;\r | |
231 | }\r | |
232 | }\r | |
233 | \r | |
234 | // Copy our assembly code into the page that contains the exception vectors. \r | |
235 | CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r | |
2ef2b01e A |
236 | \r |
237 | //\r | |
238 | // Patch in the common Assembly exception handler\r | |
239 | //\r | |
240 | Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r | |
241 | *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r | |
242 | \r | |
243 | // Flush Caches since we updated executable stuff\r | |
8a4d81e6 | 244 | InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r |
2ef2b01e | 245 | \r |
0416278c | 246 | if (FiqEnabled) {\r |
247 | ArmEnableFiq ();\r | |
248 | }\r | |
249 | \r | |
250 | if (IrqEnabled) {\r | |
2ef2b01e A |
251 | // \r |
252 | // Restore interrupt state\r | |
253 | //\r | |
254 | Status = Cpu->EnableInterrupt (Cpu);\r | |
255 | }\r | |
256 | \r | |
257 | return Status;\r | |
258 | }\r |