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