]>
Commit | Line | Data |
---|---|---|
c1f23d63 | 1 | /**@file\r |
2 | IA32 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 | |
e9f9d09a | 18 | #include "PlDebugSupport.h"\r |
c1f23d63 | 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;\r | |
25 | \r | |
c1f23d63 | 26 | EFI_STATUS\r |
27 | CreateEntryStub (\r | |
28 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
29 | OUT VOID **Stub\r | |
30 | )\r | |
31 | /*++\r | |
32 | \r | |
33 | Routine Description: Allocate pool for a new IDT entry stub. Copy the generic\r | |
34 | stub into the new buffer and fixup the vector number and jump target address.\r | |
35 | \r | |
36 | Arguments:\r | |
37 | ExceptionType - This is the exception type that the new stub will be created\r | |
38 | for.\r | |
39 | Stub - On successful exit, *Stub contains the newly allocated entry stub.\r | |
40 | Returns:\r | |
41 | Typically EFI_SUCCESS\r | |
42 | other possibilities are passed through from AllocatePool\r | |
43 | \r | |
44 | --*/\r | |
45 | {\r | |
46 | UINT8 *StubCopy;\r | |
47 | \r | |
48 | StubCopy = *Stub;\r | |
49 | \r | |
50 | //\r | |
51 | // Fixup the stub code for this vector\r | |
52 | //\r | |
53 | \r | |
54 | // The stub code looks like this:\r | |
55 | //\r | |
56 | // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top\r | |
57 | // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack\r | |
58 | // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed\r | |
59 | // 0000000D E9 db 0e9h ; jump rel32\r | |
60 | // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry\r | |
61 | //\r | |
62 | \r | |
63 | //\r | |
64 | // poke in the exception type so the second push pushes the exception type\r | |
65 | //\r | |
66 | StubCopy[0x0c] = (UINT8) ExceptionType;\r | |
67 | \r | |
68 | //\r | |
69 | // fixup the jump target to point to the common entry\r | |
70 | //\r | |
71 | *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];\r | |
72 | \r | |
73 | return EFI_SUCCESS;\r | |
74 | }\r | |
75 | \r | |
c1f23d63 | 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)[0];\r | |
110 | ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];\r | |
111 | \r | |
112 | Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r | |
113 | IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r | |
114 | WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r | |
115 | WriteInterruptFlag (OldIntFlagState);\r | |
116 | }\r | |
117 | \r | |
118 | return Status;\r | |
119 | }\r | |
120 | \r | |
c1f23d63 | 121 | EFI_STATUS\r |
122 | UnhookEntry (\r | |
123 | IN EFI_EXCEPTION_TYPE ExceptionType\r | |
124 | )\r | |
125 | /*++\r | |
126 | \r | |
127 | Routine Description:\r | |
128 | Undoes HookEntry. This code executes in boot services context.\r | |
129 | \r | |
130 | Arguments:\r | |
131 | ExceptionType - specifies which entry to unhook\r | |
132 | \r | |
133 | Returns:\r | |
134 | EFI_SUCCESS\r | |
135 | \r | |
136 | --*/\r | |
137 | {\r | |
138 | BOOLEAN OldIntFlagState;\r | |
139 | \r | |
140 | OldIntFlagState = WriteInterruptFlag (0);\r | |
141 | WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r | |
142 | WriteInterruptFlag (OldIntFlagState);\r | |
143 | \r | |
144 | return EFI_SUCCESS;\r | |
145 | }\r | |
146 | \r | |
147 | EFI_STATUS\r | |
148 | ManageIdtEntryTable (\r | |
149 | VOID (*NewCallback)(),\r | |
150 | EFI_EXCEPTION_TYPE ExceptionType\r | |
151 | )\r | |
152 | /*++\r | |
153 | \r | |
154 | Routine Description:\r | |
155 | This is the main worker function that manages the state of the interrupt\r | |
156 | handlers. It both installs and uninstalls interrupt handlers based on the\r | |
157 | value of NewCallback. If NewCallback is NULL, then uninstall is indicated.\r | |
158 | If NewCallback is non-NULL, then install is indicated.\r | |
159 | \r | |
160 | Arguments:\r | |
161 | NewCallback - If non-NULL, NewCallback specifies the new handler to register.\r | |
162 | If NULL, specifies that the previously registered handler should\r | |
163 | be uninstalled.\r | |
164 | ExceptionType - Indicates which entry to manage\r | |
165 | \r | |
166 | Returns:\r | |
167 | EFI_SUCCESS\r | |
168 | EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r | |
169 | no handler registered for it\r | |
170 | EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r | |
171 | \r | |
172 | Other possible return values are passed through from UnHookEntry and HookEntry.\r | |
173 | \r | |
174 | --*/\r | |
175 | {\r | |
176 | EFI_STATUS Status;\r | |
177 | \r | |
178 | Status = EFI_SUCCESS;\r | |
179 | \r | |
631c1991 | 180 | if (!FeaturePcdGet (PcdNtEmulatorEnable)) {\r |
c1f23d63 | 181 | if (CompareDescriptor (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {\r |
182 | //\r | |
183 | // we've already installed to this vector\r | |
184 | //\r | |
185 | if (NewCallback != NULL) {\r | |
186 | //\r | |
187 | // if the input handler is non-null, error\r | |
188 | //\r | |
189 | Status = EFI_ALREADY_STARTED;\r | |
190 | } else {\r | |
191 | Status = UnhookEntry (ExceptionType);\r | |
192 | }\r | |
193 | } else {\r | |
194 | //\r | |
195 | // no user handler installed on this vector\r | |
196 | //\r | |
197 | if (NewCallback == NULL) {\r | |
198 | //\r | |
199 | // if the input handler is null, error\r | |
200 | //\r | |
201 | Status = EFI_INVALID_PARAMETER;\r | |
202 | } else {\r | |
203 | Status = HookEntry (ExceptionType, NewCallback);\r | |
204 | }\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 IA32 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 |