c1f23d63 |
1 | /**@file\r |
2 | X64 specific debug support functions\r |
3 | \r |
4 | Copyright (c) 2006 - 2007, Intel Corporation\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 | //\r |
16 | // private header files\r |
17 | //\r |
18 | #include "plDebugSupport.h"\r |
19 | \r |
20 | //\r |
21 | // This the global main table to keep track of the interrupts\r |
22 | //\r |
23 | IDT_ENTRY *IdtEntryTable = NULL;\r |
24 | DESCRIPTOR NullDesc = {0, 0};\r |
25 | \r |
26 | STATIC\r |
27 | EFI_STATUS\r |
28 | CreateEntryStub (\r |
29 | IN EFI_EXCEPTION_TYPE ExceptionType,\r |
30 | OUT VOID **Stub\r |
31 | )\r |
32 | /*++\r |
33 | \r |
34 | Routine Description: Allocate pool for a new IDT entry stub. Copy the generic\r |
35 | stub into the new buffer and fixup the vector number and jump target address.\r |
36 | \r |
37 | Arguments:\r |
38 | ExceptionType - This is the exception type that the new stub will be created\r |
39 | for.\r |
40 | Stub - On successful exit, *Stub contains the newly allocated entry stub.\r |
41 | Returns:\r |
42 | Typically EFI_SUCCESS\r |
43 | other possibilities are passed through from AllocatePool\r |
44 | \r |
45 | --*/\r |
46 | {\r |
47 | UINT8 *StubCopy;\r |
48 | \r |
49 | StubCopy = *Stub;\r |
50 | \r |
51 | //\r |
52 | // Fixup the stub code for this vector\r |
53 | //\r |
54 | \r |
55 | // The stub code looks like this:\r |
56 | //\r |
57 | // 00000000 6A 00 push 0 ; push vector number - will be modified before installed\r |
58 | // 00000002 E9 db 0e9h ; jump rel32\r |
59 | // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry\r |
60 | //\r |
61 | \r |
62 | //\r |
63 | // poke in the exception type so the second push pushes the exception type\r |
64 | //\r |
65 | StubCopy[0x1] = (UINT8) ExceptionType;\r |
66 | \r |
67 | //\r |
68 | // fixup the jump target to point to the common entry\r |
69 | //\r |
70 | *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);\r |
71 | \r |
72 | return EFI_SUCCESS;\r |
73 | }\r |
74 | \r |
75 | STATIC\r |
76 | EFI_STATUS\r |
77 | HookEntry (\r |
78 | IN EFI_EXCEPTION_TYPE ExceptionType,\r |
79 | IN VOID (*NewCallback) ()\r |
80 | )\r |
81 | /*++\r |
82 | \r |
83 | Routine Description:\r |
84 | Creates a nes entry stub. Then saves the current IDT entry and replaces it\r |
85 | with an interrupt gate for the new entry point. The IdtEntryTable is updated\r |
86 | with the new registered function.\r |
87 | \r |
88 | This code executes in boot services context. The stub entry executes in interrupt\r |
89 | context.\r |
90 | \r |
91 | Arguments:\r |
92 | ExceptionType - specifies which vector to hook.\r |
93 | NewCallback - a pointer to the new function to be registered.\r |
94 | \r |
95 | Returns:\r |
96 | EFI_SUCCESS\r |
97 | Other possibilities are passed through by CreateEntryStub\r |
98 | \r |
99 | --*/\r |
100 | {\r |
101 | BOOLEAN OldIntFlagState;\r |
102 | EFI_STATUS Status;\r |
103 | \r |
104 | Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);\r |
105 | if (Status == EFI_SUCCESS) {\r |
106 | OldIntFlagState = WriteInterruptFlag (0);\r |
107 | ReadIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r |
108 | \r |
109 | ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[0];\r |
110 | ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[3];\r |
111 | ((UINT32 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT32 *) &IdtEntryTable[ExceptionType].OrigDesc.High)[0];\r |
112 | \r |
113 | Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r |
114 | IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r |
115 | WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r |
116 | WriteInterruptFlag (OldIntFlagState);\r |
117 | }\r |
118 | \r |
119 | return Status;\r |
120 | }\r |
121 | \r |
122 | STATIC\r |
123 | EFI_STATUS\r |
124 | UnhookEntry (\r |
125 | IN EFI_EXCEPTION_TYPE ExceptionType\r |
126 | )\r |
127 | /*++\r |
128 | \r |
129 | Routine Description:\r |
130 | Undoes HookEntry. This code executes in boot services context.\r |
131 | \r |
132 | Arguments:\r |
133 | ExceptionType - specifies which entry to unhook\r |
134 | \r |
135 | Returns:\r |
136 | EFI_SUCCESS\r |
137 | \r |
138 | --*/\r |
139 | {\r |
140 | BOOLEAN OldIntFlagState;\r |
141 | \r |
142 | OldIntFlagState = WriteInterruptFlag (0);\r |
143 | WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r |
144 | WriteInterruptFlag (OldIntFlagState);\r |
145 | \r |
146 | return EFI_SUCCESS;\r |
147 | }\r |
148 | \r |
149 | EFI_STATUS\r |
150 | ManageIdtEntryTable (\r |
151 | VOID (*NewCallback)(),\r |
152 | EFI_EXCEPTION_TYPE ExceptionType\r |
153 | )\r |
154 | /*++\r |
155 | \r |
156 | Routine Description:\r |
157 | This is the main worker function that manages the state of the interrupt\r |
158 | handlers. It both installs and uninstalls interrupt handlers based on the\r |
159 | value of NewCallback. If NewCallback is NULL, then uninstall is indicated.\r |
160 | If NewCallback is non-NULL, then install is indicated.\r |
161 | \r |
162 | Arguments:\r |
163 | NewCallback - If non-NULL, NewCallback specifies the new handler to register.\r |
164 | If NULL, specifies that the previously registered handler should\r |
165 | be uninstalled.\r |
166 | ExceptionType - Indicates which entry to manage\r |
167 | \r |
168 | Returns:\r |
169 | EFI_SUCCESS\r |
170 | EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r |
171 | no handler registered for it\r |
172 | EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r |
173 | \r |
174 | Other possible return values are passed through from UnHookEntry and HookEntry.\r |
175 | \r |
176 | --*/\r |
177 | {\r |
178 | EFI_STATUS Status;\r |
179 | \r |
180 | Status = EFI_SUCCESS;\r |
181 | \r |
182 | if (CompareDescriptor (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {\r |
183 | //\r |
184 | // we've already installed to this vector\r |
185 | //\r |
186 | if (NewCallback != NULL) {\r |
187 | //\r |
188 | // if the input handler is non-null, error\r |
189 | //\r |
190 | Status = EFI_ALREADY_STARTED;\r |
191 | } else {\r |
192 | Status = UnhookEntry (ExceptionType);\r |
193 | }\r |
194 | } else {\r |
195 | //\r |
196 | // no user handler installed on this vector\r |
197 | //\r |
198 | if (NewCallback == NULL) {\r |
199 | //\r |
200 | // if the input handler is null, error\r |
201 | //\r |
202 | Status = EFI_INVALID_PARAMETER;\r |
203 | } else {\r |
204 | Status = HookEntry (ExceptionType, NewCallback);\r |
205 | }\r |
206 | }\r |
207 | \r |
208 | return Status;\r |
209 | }\r |
210 | \r |
211 | EFI_STATUS\r |
212 | EFIAPI\r |
213 | GetMaximumProcessorIndex (\r |
214 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r |
215 | OUT UINTN *MaxProcessorIndex\r |
216 | )\r |
217 | /*++\r |
218 | \r |
219 | Routine Description: This is a DebugSupport protocol member function.\r |
220 | \r |
221 | Arguments:\r |
222 | This - The DebugSupport instance\r |
223 | MaxProcessorIndex - The maximuim supported processor index\r |
224 | \r |
225 | Returns:\r |
226 | Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0\r |
227 | \r |
228 | --*/\r |
229 | {\r |
230 | *MaxProcessorIndex = 0;\r |
231 | return (EFI_SUCCESS);\r |
232 | }\r |
233 | \r |
234 | EFI_STATUS\r |
235 | EFIAPI\r |
236 | RegisterPeriodicCallback (\r |
237 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r |
238 | IN UINTN ProcessorIndex,\r |
239 | IN EFI_PERIODIC_CALLBACK PeriodicCallback\r |
240 | )\r |
241 | /*++\r |
242 | \r |
243 | Routine Description: This is a DebugSupport protocol member function.\r |
244 | \r |
245 | Arguments:\r |
246 | This - The DebugSupport instance\r |
247 | ProcessorIndex - Which processor the callback applies to.\r |
248 | PeriodicCallback - Callback function\r |
249 | \r |
250 | Returns:\r |
251 | \r |
252 | EFI_SUCCESS\r |
253 | EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r |
254 | no handler registered for it\r |
255 | EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r |
256 | \r |
257 | Other possible return values are passed through from UnHookEntry and HookEntry.\r |
258 | \r |
259 | --*/\r |
260 | {\r |
261 | return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);\r |
262 | }\r |
263 | \r |
264 | EFI_STATUS\r |
265 | EFIAPI\r |
266 | RegisterExceptionCallback (\r |
267 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r |
268 | IN UINTN ProcessorIndex,\r |
269 | IN EFI_EXCEPTION_CALLBACK NewCallback,\r |
270 | IN EFI_EXCEPTION_TYPE ExceptionType\r |
271 | )\r |
272 | /*++\r |
273 | \r |
274 | Routine Description:\r |
275 | This is a DebugSupport protocol member function.\r |
276 | \r |
277 | This code executes in boot services context.\r |
278 | \r |
279 | Arguments:\r |
280 | This - The DebugSupport instance\r |
281 | ProcessorIndex - Which processor the callback applies to.\r |
282 | NewCallback - Callback function\r |
283 | ExceptionType - Which exception to hook\r |
284 | \r |
285 | Returns:\r |
286 | \r |
287 | EFI_SUCCESS\r |
288 | EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r |
289 | no handler registered for it\r |
290 | EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r |
291 | \r |
292 | Other possible return values are passed through from UnHookEntry and HookEntry.\r |
293 | \r |
294 | --*/\r |
295 | {\r |
296 | return ManageIdtEntryTable (NewCallback, ExceptionType);\r |
297 | }\r |
298 | \r |
299 | EFI_STATUS\r |
300 | EFIAPI\r |
301 | InvalidateInstructionCache (\r |
302 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r |
303 | IN UINTN ProcessorIndex,\r |
304 | IN VOID *Start,\r |
305 | IN UINT64 Length\r |
306 | )\r |
307 | /*++\r |
308 | \r |
309 | Routine Description:\r |
310 | This is a DebugSupport protocol member function.\r |
311 | Calls assembly routine to flush cache.\r |
312 | \r |
313 | Arguments:\r |
314 | This - The DebugSupport instance\r |
315 | ProcessorIndex - Which processor the callback applies to.\r |
316 | Start - Physical base of the memory range to be invalidated\r |
317 | Length - mininum number of bytes in instruction cache to invalidate\r |
318 | \r |
319 | Returns:\r |
320 | \r |
321 | EFI_SUCCESS - always return success\r |
322 | \r |
323 | --*/\r |
324 | {\r |
325 | AsmWbinvd ();\r |
326 | return EFI_SUCCESS;\r |
327 | }\r |
328 | \r |
329 | EFI_STATUS\r |
330 | plInitializeDebugSupportDriver (\r |
331 | VOID\r |
332 | )\r |
333 | /*++\r |
334 | \r |
335 | Routine Description:\r |
336 | Initializes driver's handler registration database.\r |
337 | \r |
338 | This code executes in boot services context.\r |
339 | \r |
340 | Arguments:\r |
341 | None\r |
342 | \r |
343 | Returns:\r |
344 | EFI_SUCCESS\r |
345 | EFI_UNSUPPORTED - if X64 processor does not support FXSTOR/FXRSTOR instructions,\r |
346 | the context save will fail, so these processor's are not supported.\r |
347 | EFI_OUT_OF_RESOURCES - not resource to finish initialization\r |
348 | \r |
349 | --*/\r |
350 | {\r |
351 | EFI_EXCEPTION_TYPE ExceptionType;\r |
352 | \r |
353 | if (!FxStorSupport ()) {\r |
354 | return EFI_UNSUPPORTED;\r |
355 | }\r |
356 | \r |
357 | IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);\r |
358 | if (IdtEntryTable == NULL) {\r |
359 | return EFI_OUT_OF_RESOURCES;\r |
360 | }\r |
361 | \r |
362 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r |
363 | IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);\r |
364 | if (IdtEntryTable[ExceptionType].StubEntry == NULL) {\r |
365 | goto ErrorCleanup;\r |
366 | }\r |
367 | \r |
368 | CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);\r |
369 | }\r |
370 | return EFI_SUCCESS;\r |
371 | \r |
372 | ErrorCleanup:\r |
373 | \r |
374 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r |
375 | if (IdtEntryTable[ExceptionType].StubEntry != NULL) {\r |
376 | FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r |
377 | }\r |
378 | }\r |
379 | FreePool (IdtEntryTable);\r |
380 | \r |
381 | return EFI_OUT_OF_RESOURCES;\r |
382 | }\r |
383 | \r |
384 | EFI_STATUS\r |
385 | EFIAPI\r |
386 | plUnloadDebugSupportDriver (\r |
387 | IN EFI_HANDLE ImageHandle\r |
388 | )\r |
389 | /*++\r |
390 | \r |
391 | Routine Description:\r |
392 | This is the callback that is written to the LoadedImage protocol instance\r |
393 | on the image handle. It uninstalls all registered handlers and frees all entry\r |
394 | stub memory.\r |
395 | \r |
396 | This code executes in boot services context.\r |
397 | \r |
398 | Arguments:\r |
399 | ImageHandle - The image handle of the unload handler\r |
400 | \r |
401 | Returns:\r |
402 | \r |
403 | EFI_SUCCESS - always return success\r |
404 | \r |
405 | --*/\r |
406 | {\r |
407 | EFI_EXCEPTION_TYPE ExceptionType;\r |
408 | \r |
409 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r |
410 | ManageIdtEntryTable (NULL, ExceptionType);\r |
411 | }\r |
412 | \r |
413 | FreePool (IdtEntryTable);\r |
414 | return EFI_SUCCESS;\r |
415 | }\r |
416 | \r |
417 | VOID\r |
418 | InterruptDistrubutionHub (\r |
419 | EFI_EXCEPTION_TYPE ExceptionType,\r |
420 | EFI_SYSTEM_CONTEXT_IA32 *ContextRecord\r |
421 | )\r |
422 | /*++\r |
423 | \r |
424 | Routine Description: Common piece of code that invokes the registered handlers.\r |
425 | \r |
426 | This code executes in exception context so no efi calls are allowed.\r |
427 | \r |
428 | Arguments:\r |
429 | ExceptionType - exception type\r |
430 | ContextRecord - system context\r |
431 | \r |
432 | Returns:\r |
433 | \r |
434 | None\r |
435 | \r |
436 | --*/\r |
437 | {\r |
438 | if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r |
439 | if (ExceptionType != SYSTEM_TIMER_VECTOR) {\r |
440 | IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);\r |
441 | } else {\r |
442 | OrigVector = IdtEntryTable[ExceptionType].OrigVector;\r |
443 | IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);\r |
444 | }\r |
445 | }\r |
446 | }\r |