]>
Commit | Line | Data |
---|---|---|
2939c778 EC |
1 | /* @file\r |
2 | * Main file supporting the SEC Phase for Versatile Express\r | |
3 | *\r | |
4 | * Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
5 | * Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r | |
6 | * Copyright (c) 2016 HP Development Company, L.P.\r | |
7 | *\r | |
8 | * This program and the accompanying materials\r | |
9 | * are licensed and made available under the terms and conditions of the BSD License\r | |
10 | * which accompanies this distribution. The full text of the license may be found at\r | |
11 | * http://opensource.org/licenses/bsd-license.php\r | |
12 | *\r | |
13 | * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | *\r | |
16 | **/\r | |
17 | \r | |
18 | #include <Uefi.h>\r | |
19 | #include <Library/CpuExceptionHandlerLib.h>\r | |
20 | \r | |
21 | #include <Library/ArmLib.h>\r | |
22 | #include <Library/PcdLib.h>\r | |
23 | #include <Library/CacheMaintenanceLib.h>\r | |
24 | #include <Library/BaseLib.h>\r | |
25 | #include <Library/BaseMemoryLib.h>\r | |
26 | #include <Library/DebugLib.h>\r | |
27 | #include <Library/DefaultExceptionHandlerLib.h>\r | |
28 | \r | |
8a771a2e | 29 | STATIC\r |
2939c778 EC |
30 | RETURN_STATUS\r |
31 | CopyExceptionHandlers(\r | |
32 | IN PHYSICAL_ADDRESS BaseAddress\r | |
33 | );\r | |
34 | \r | |
35 | EFI_STATUS\r | |
36 | EFIAPI\r | |
37 | RegisterExceptionHandler(\r | |
38 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
39 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
40 | );\r | |
41 | \r | |
42 | VOID\r | |
43 | ExceptionHandlersStart(\r | |
44 | VOID\r | |
45 | );\r | |
46 | \r | |
47 | VOID\r | |
48 | ExceptionHandlersEnd(\r | |
49 | VOID\r | |
50 | );\r | |
51 | \r | |
52 | RETURN_STATUS ArchVectorConfig(\r | |
53 | IN UINTN VectorBaseAddress\r | |
54 | );\r | |
55 | \r | |
56 | // these globals are provided by the architecture specific source (Arm or AArch64)\r | |
57 | extern UINTN gMaxExceptionNumber;\r | |
58 | extern EFI_EXCEPTION_CALLBACK gExceptionHandlers[];\r | |
59 | extern EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[];\r | |
60 | extern PHYSICAL_ADDRESS gExceptionVectorAlignmentMask;\r | |
61 | extern UINTN gDebuggerNoHandlerValue;\r | |
62 | \r | |
63 | // A compiler flag adjusts the compilation of this library to a variant where\r | |
64 | // the vectors are relocated (copied) to another location versus using the\r | |
65 | // vectors in-place. Since this effects an assembly .align directive we must\r | |
66 | // address this at library build time. Since this affects the build of the\r | |
67 | // library we cannot represent this in a PCD since PCDs are evaluated on\r | |
68 | // a per-module basis.\r | |
69 | #if defined(ARM_RELOCATE_VECTORS)\r | |
8a771a2e | 70 | STATIC CONST BOOLEAN gArmRelocateVectorTable = TRUE;\r |
2939c778 | 71 | #else\r |
8a771a2e | 72 | STATIC CONST BOOLEAN gArmRelocateVectorTable = FALSE;\r |
2939c778 EC |
73 | #endif\r |
74 | \r | |
75 | \r | |
76 | /**\r | |
77 | Initializes all CPU exceptions entries and provides the default exception handlers.\r | |
78 | \r | |
79 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
80 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
81 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
82 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
83 | \r | |
84 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
85 | \r | |
86 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
87 | with default exception handlers.\r | |
88 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
89 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
90 | \r | |
91 | **/\r | |
92 | EFI_STATUS\r | |
93 | EFIAPI\r | |
94 | InitializeCpuExceptionHandlers(\r | |
95 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
96 | )\r | |
97 | {\r | |
98 | RETURN_STATUS Status;\r | |
99 | UINTN VectorBase;\r | |
100 | \r | |
101 | Status = EFI_SUCCESS;\r | |
102 | \r | |
103 | // if we are requested to copy exceptin handlers to another location\r | |
104 | if (gArmRelocateVectorTable) {\r | |
105 | \r | |
f0bbcdf8 | 106 | VectorBase = PcdGet64(PcdCpuVectorBaseAddress);\r |
2939c778 EC |
107 | Status = CopyExceptionHandlers(VectorBase);\r |
108 | \r | |
109 | }\r | |
110 | else { // use VBAR to point to where our exception handlers are\r | |
111 | \r | |
112 | // The vector table must be aligned for the architecture. If this\r | |
113 | // assertion fails ensure the appropriate FFS alignment is in effect,\r | |
114 | // which can be accomplished by ensuring the proper Align=X statement\r | |
115 | // in the platform packaging rules. For ARM Align=32 is required and\r | |
116 | // for AArch64 Align=4K is required. Align=Auto can be used but this\r | |
117 | // is known to cause an issue with populating the reset vector area\r | |
118 | // for encapsulated FVs.\r | |
119 | ASSERT(((UINTN)ExceptionHandlersStart & gExceptionVectorAlignmentMask) == 0);\r | |
120 | \r | |
f0bbcdf8 | 121 | // We do not copy the Exception Table at PcdGet64(PcdCpuVectorBaseAddress). We just set Vector\r |
2939c778 EC |
122 | // Base Address to point into CpuDxe code.\r |
123 | VectorBase = (UINTN)ExceptionHandlersStart;\r | |
124 | \r | |
125 | Status = RETURN_SUCCESS;\r | |
126 | }\r | |
127 | \r | |
128 | if (!RETURN_ERROR(Status)) {\r | |
129 | // call the architecture-specific routine to prepare for the new vector\r | |
130 | // configuration to take effect\r | |
131 | ArchVectorConfig(VectorBase);\r | |
132 | \r | |
133 | ArmWriteVBar(VectorBase);\r | |
134 | }\r | |
135 | \r | |
136 | return RETURN_SUCCESS;\r | |
137 | }\r | |
138 | \r | |
139 | /**\r | |
140 | Copies exception handlers to the speciifed address.\r | |
141 | \r | |
142 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
143 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
144 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
145 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
146 | \r | |
147 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
148 | \r | |
149 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r | |
150 | with default exception handlers.\r | |
151 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
152 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
153 | \r | |
154 | **/\r | |
8a771a2e | 155 | STATIC\r |
2939c778 EC |
156 | RETURN_STATUS\r |
157 | CopyExceptionHandlers(\r | |
158 | IN PHYSICAL_ADDRESS BaseAddress\r | |
159 | )\r | |
160 | {\r | |
161 | RETURN_STATUS Status;\r | |
162 | UINTN Length;\r | |
163 | UINTN Index;\r | |
164 | UINT32 *VectorBase;\r | |
165 | \r | |
166 | // ensure that the destination value specifies an address meeting the vector alignment requirements\r | |
167 | ASSERT ((BaseAddress & gExceptionVectorAlignmentMask) == 0);\r | |
168 | \r | |
169 | //\r | |
170 | // Copy an implementation of the exception vectors to PcdCpuVectorBaseAddress.\r | |
171 | //\r | |
172 | Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r | |
173 | \r | |
174 | VectorBase = (UINT32 *)(UINTN)BaseAddress;\r | |
175 | \r | |
176 | if (FeaturePcdGet(PcdDebuggerExceptionSupport) == TRUE) {\r | |
177 | // Save existing vector table, in case debugger is already hooked in\r | |
178 | CopyMem((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (EFI_EXCEPTION_CALLBACK)* (gMaxExceptionNumber+1));\r | |
179 | }\r | |
180 | \r | |
181 | // Copy our assembly code into the page that contains the exception vectors.\r | |
182 | CopyMem((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r | |
183 | \r | |
184 | //\r | |
185 | // Initialize the C entry points for interrupts\r | |
186 | //\r | |
187 | for (Index = 0; Index <= gMaxExceptionNumber; Index++) {\r | |
188 | if (!FeaturePcdGet(PcdDebuggerExceptionSupport) ||\r | |
189 | (gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)gDebuggerNoHandlerValue)) {\r | |
190 | \r | |
191 | Status = RegisterExceptionHandler(Index, NULL);\r | |
192 | ASSERT_EFI_ERROR(Status);\r | |
193 | }\r | |
194 | else {\r | |
195 | // If the debugger has already hooked put its vector back\r | |
196 | VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];\r | |
197 | }\r | |
198 | }\r | |
199 | \r | |
200 | // Flush Caches since we updated executable stuff\r | |
201 | InvalidateInstructionCacheRange((VOID *)(UINTN)BaseAddress, Length);\r | |
202 | \r | |
203 | return RETURN_SUCCESS;\r | |
204 | }\r | |
205 | \r | |
206 | \r | |
207 | /**\r | |
208 | Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r | |
209 | \r | |
210 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
211 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
212 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r | |
213 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
214 | \r | |
215 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
216 | \r | |
217 | @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r | |
218 | with default interrupt/exception handlers.\r | |
219 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
220 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
221 | \r | |
222 | **/\r | |
223 | EFI_STATUS\r | |
224 | EFIAPI\r | |
225 | InitializeCpuInterruptHandlers(\r | |
226 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
227 | )\r | |
228 | {\r | |
229 | // not needed, this is what the CPU driver is for\r | |
230 | return EFI_UNSUPPORTED;\r | |
231 | }\r | |
232 | \r | |
233 | /**\r | |
234 | Registers a function to be called from the processor exception handler. (On ARM/AArch64 this only\r | |
235 | provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt\r | |
236 | Protocol.)\r | |
237 | \r | |
238 | This function registers and enables the handler specified by ExceptionHandler for a processor\r | |
239 | interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the\r | |
240 | handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.\r | |
241 | The installed handler is called once for each processor interrupt or exception.\r | |
242 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r | |
243 | InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r | |
244 | \r | |
245 | @param[in] ExceptionType Defines which interrupt or exception to hook.\r | |
246 | @param[in] ExceptionHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
247 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
248 | will be uninstalled.\r | |
249 | \r | |
250 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
251 | @retval EFI_ALREADY_STARTED ExceptionHandler is not NULL, and a handler for ExceptionType was\r | |
252 | previously installed.\r | |
253 | @retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not\r | |
254 | previously installed.\r | |
255 | @retval EFI_UNSUPPORTED The interrupt specified by ExceptionType is not supported,\r | |
256 | or this function is not supported.\r | |
257 | **/\r | |
258 | RETURN_STATUS\r | |
259 | RegisterCpuInterruptHandler(\r | |
260 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
261 | IN EFI_CPU_INTERRUPT_HANDLER ExceptionHandler\r | |
262 | ) {\r | |
263 | if (ExceptionType > gMaxExceptionNumber) {\r | |
264 | return RETURN_UNSUPPORTED;\r | |
265 | }\r | |
266 | \r | |
267 | if ((ExceptionHandler != NULL) && (gExceptionHandlers[ExceptionType] != NULL)) {\r | |
268 | return RETURN_ALREADY_STARTED;\r | |
269 | }\r | |
270 | \r | |
271 | gExceptionHandlers[ExceptionType] = ExceptionHandler;\r | |
272 | \r | |
273 | return RETURN_SUCCESS;\r | |
274 | }\r | |
275 | \r | |
276 | /**\r | |
277 | Register exception handler.\r | |
278 | \r | |
279 | @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.\r | |
280 | @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and\r | |
281 | the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL\r | |
282 | of the UEFI 2.0 specification.\r | |
283 | @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER\r | |
284 | that is called when a processor interrupt occurs.\r | |
285 | If this parameter is NULL, then the handler will be uninstalled.\r | |
286 | \r | |
287 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
288 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.\r | |
289 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.\r | |
290 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r | |
291 | \r | |
292 | **/\r | |
293 | EFI_STATUS\r | |
294 | EFIAPI\r | |
295 | RegisterExceptionHandler(\r | |
296 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
297 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
298 | )\r | |
299 | {\r | |
300 | return RegisterCpuInterruptHandler(ExceptionType, InterruptHandler);\r | |
301 | }\r | |
302 | \r | |
303 | VOID\r | |
304 | EFIAPI\r | |
305 | CommonCExceptionHandler(\r | |
306 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
307 | IN OUT EFI_SYSTEM_CONTEXT SystemContext\r | |
308 | )\r | |
309 | {\r | |
310 | if (ExceptionType <= gMaxExceptionNumber) {\r | |
311 | if (gExceptionHandlers[ExceptionType]) {\r | |
312 | gExceptionHandlers[ExceptionType](ExceptionType, SystemContext);\r | |
313 | return;\r | |
314 | }\r | |
315 | }\r | |
316 | else {\r | |
317 | DEBUG((EFI_D_ERROR, "Unknown exception type %d\n", ExceptionType));\r | |
318 | ASSERT(FALSE);\r | |
319 | }\r | |
320 | \r | |
321 | DefaultExceptionHandler(ExceptionType, SystemContext);\r | |
322 | }\r |