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