1. Added PcdNtEmulator and Removed MACRO EFI_NT_EMULATOR
[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
49 //\r
50 // First, allocate a new buffer and copy the stub code into it\r
51 //\r
2c3b5ec5
LG
52 *Stub = AllocatePool (StubSize);\r
53 if (*Stub != NULL) {\r
878ddf1f 54 StubCopy = *Stub;\r
2c3b5ec5 55 CopyMem (StubCopy, InterruptEntryStub, StubSize);\r
878ddf1f 56\r
57 //\r
58 // Next fixup the stub code for this vector\r
59 //\r
60\r
61 // The stub code looks like this:\r
62 //\r
63 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top\r
64 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack\r
65 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed\r
66 // 0000000D E9 db 0e9h ; jump rel32\r
67 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry\r
68 //\r
69\r
70 //\r
71 // poke in the exception type so the second push pushes the exception type\r
72 //\r
73 StubCopy[0x0c] = (UINT8) ExceptionType;\r
74\r
75 //\r
76 // fixup the jump target to point to the common entry\r
77 //\r
78 *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];\r
7af45bf1 79\r
2c3b5ec5 80 return EFI_SUCCESS;\r
878ddf1f 81 }\r
82\r
2c3b5ec5 83 return EFI_OUT_OF_RESOURCES;\r
878ddf1f 84}\r
85\r
86STATIC\r
87EFI_STATUS\r
88HookEntry (\r
89 IN EFI_EXCEPTION_TYPE ExceptionType,\r
90 IN VOID (*NewCallback) ()\r
91 )\r
92/*++\r
93\r
94Routine Description:\r
95 Creates a nes entry stub. Then saves the current IDT entry and replaces it\r
96 with an interrupt gate for the new entry point. The IdtEntryTable is updated\r
97 with the new registered function.\r
98\r
99 This code executes in boot services context. The stub entry executes in interrupt\r
100 context.\r
101\r
102Arguments:\r
103 ExceptionType - specifies which vector to hook.\r
104 NewCallback - a pointer to the new function to be registered.\r
105\r
106Returns:\r
107 EFI_SUCCESS\r
108 Other possibilities are passed through by CreateEntryStub\r
109\r
110--*/\r
878ddf1f 111{\r
112 BOOLEAN OldIntFlagState;\r
113 EFI_STATUS Status;\r
114\r
115 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);\r
116 if (Status == EFI_SUCCESS) {\r
117 OldIntFlagState = WriteInterruptFlag (0);\r
118 ReadIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
119\r
120 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[0];\r
121 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];\r
122\r
123 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r
124 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r
125 WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r
126 WriteInterruptFlag (OldIntFlagState);\r
127 }\r
128\r
129 return Status;\r
130}\r
131\r
132STATIC\r
133EFI_STATUS\r
134UnhookEntry (\r
135 IN EFI_EXCEPTION_TYPE ExceptionType\r
136 )\r
137/*++\r
138\r
139Routine Description:\r
140 Undoes HookEntry. This code executes in boot services context.\r
141\r
142Arguments:\r
143 ExceptionType - specifies which entry to unhook\r
144\r
145Returns:\r
146 EFI_SUCCESS\r
878ddf1f 147\r
148--*/\r
149{\r
150 BOOLEAN OldIntFlagState;\r
878ddf1f 151\r
152 OldIntFlagState = WriteInterruptFlag (0);\r
153 WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
2c3b5ec5 154 FreePool ((VOID *) (UINTN) IdtEntryTable[ExceptionType].StubEntry);\r
878ddf1f 155 ZeroMem (&IdtEntryTable[ExceptionType], sizeof (IDT_ENTRY));\r
156 WriteInterruptFlag (OldIntFlagState);\r
157\r
2c3b5ec5 158 return EFI_SUCCESS;\r
878ddf1f 159}\r
878ddf1f 160\r
161EFI_STATUS\r
162ManageIdtEntryTable (\r
163 VOID (*NewCallback)(),\r
164 EFI_EXCEPTION_TYPE ExceptionType\r
165 )\r
166/*++\r
167\r
168Routine Description:\r
169 This is the main worker function that manages the state of the interrupt\r
170 handlers. It both installs and uninstalls interrupt handlers based on the\r
171 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.\r
172 If NewCallback is non-NULL, then install is indicated.\r
173\r
174Arguments:\r
175 NewCallback - If non-NULL, NewCallback specifies the new handler to register.\r
176 If NULL, specifies that the previously registered handler should\r
177 be uninstalled.\r
178 ExceptionType - Indicates which entry to manage\r
179\r
180Returns:\r
181 EFI_SUCCESS\r
182 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
183 no handler registered for it\r
184 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r
185\r
186 Other possible return values are passed through from UnHookEntry and HookEntry.\r
187\r
188--*/\r
878ddf1f 189{\r
190 EFI_STATUS Status;\r
191\r
192 Status = EFI_SUCCESS;\r
193\r
7af45bf1 194 if (FeaturePcdGet (PcdNtEmulatorEnable)) {\r
195 if (CompareDescriptor (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {\r
878ddf1f 196 //\r
7af45bf1 197 // we've already installed to this vector\r
878ddf1f 198 //\r
7af45bf1 199 if (NewCallback != NULL) {\r
200 //\r
201 // if the input handler is non-null, error\r
202 //\r
203 Status = EFI_ALREADY_STARTED;\r
204 } else {\r
205 Status = UnhookEntry (ExceptionType);\r
206 }\r
878ddf1f 207 } else {\r
878ddf1f 208 //\r
7af45bf1 209 // no user handler installed on this vector\r
878ddf1f 210 //\r
7af45bf1 211 if (NewCallback == NULL) {\r
212 //\r
213 // if the input handler is null, error\r
214 //\r
215 Status = EFI_INVALID_PARAMETER;\r
216 } else {\r
217 Status = HookEntry (ExceptionType, NewCallback);\r
218 }\r
878ddf1f 219 }\r
220 }\r
7af45bf1 221\r
878ddf1f 222 return Status;\r
223}\r
224\r
225EFI_STATUS\r
226EFIAPI\r
227GetMaximumProcessorIndex (\r
228 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
229 OUT UINTN *MaxProcessorIndex\r
230 )\r
231/*++\r
232\r
233Routine Description: This is a DebugSupport protocol member function.\r
234\r
235Arguments:\r
5fd59c65 236 This - The DebugSupport instance\r
237 MaxProcessorIndex - The maximuim supported processor index\r
878ddf1f 238\r
5fd59c65 239Returns:\r
240 Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0\r
878ddf1f 241\r
242--*/\r
878ddf1f 243{\r
244 *MaxProcessorIndex = 0;\r
245 return (EFI_SUCCESS);\r
246}\r
247\r
248EFI_STATUS\r
249EFIAPI\r
250RegisterPeriodicCallback (\r
251 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
252 IN UINTN ProcessorIndex,\r
253 IN EFI_PERIODIC_CALLBACK PeriodicCallback\r
254 )\r
255/*++\r
256\r
257Routine Description: This is a DebugSupport protocol member function.\r
258\r
259Arguments:\r
5fd59c65 260 This - The DebugSupport instance\r
261 ProcessorIndex - Which processor the callback applies to.\r
262 PeriodicCallback - Callback function\r
878ddf1f 263\r
264Returns:\r
265\r
5fd59c65 266 EFI_SUCCESS\r
267 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
268 no handler registered for it\r
269 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r
270\r
271 Other possible return values are passed through from UnHookEntry and HookEntry.\r
272\r
878ddf1f 273--*/\r
878ddf1f 274{\r
275 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);\r
276}\r
277\r
278EFI_STATUS\r
279EFIAPI\r
280RegisterExceptionCallback (\r
281 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
282 IN UINTN ProcessorIndex,\r
283 IN EFI_EXCEPTION_CALLBACK NewCallback,\r
284 IN EFI_EXCEPTION_TYPE ExceptionType\r
285 )\r
286/*++\r
287\r
288Routine Description:\r
289 This is a DebugSupport protocol member function.\r
290\r
291 This code executes in boot services context.\r
292\r
293Arguments:\r
5fd59c65 294 This - The DebugSupport instance\r
295 ProcessorIndex - Which processor the callback applies to.\r
296 NewCallback - Callback function\r
297 ExceptionType - Which exception to hook\r
878ddf1f 298\r
299Returns:\r
300\r
5fd59c65 301 EFI_SUCCESS\r
302 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
303 no handler registered for it\r
304 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.\r
305\r
306 Other possible return values are passed through from UnHookEntry and HookEntry.\r
878ddf1f 307\r
308--*/\r
878ddf1f 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
5fd59c65 325 Calls assembly routine to flush cache.\r
878ddf1f 326\r
327Arguments:\r
5fd59c65 328 This - The DebugSupport instance\r
329 ProcessorIndex - Which processor the callback applies to.\r
330 Start - Physical base of the memory range to be invalidated\r
331 Length - mininum number of bytes in instruction cache to invalidate\r
878ddf1f 332\r
333Returns:\r
334\r
5fd59c65 335 EFI_SUCCESS - always return success\r
878ddf1f 336\r
337--*/\r
878ddf1f 338{\r
5fd59c65 339 AsmWbinvd ();\r
878ddf1f 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
5fd59c65 361 EFI_OUT_OF_RESOURCES - not resource to finish initialization\r
878ddf1f 362\r
363--*/\r
878ddf1f 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
5fd59c65 396 EFI_SUCCESS - always return success\r
878ddf1f 397\r
398--*/\r
878ddf1f 399{\r
400 EFI_EXCEPTION_TYPE ExceptionType;\r
401\r
402 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
403 ManageIdtEntryTable (NULL, ExceptionType);\r
404 }\r
405\r
2c3b5ec5 406 FreePool (IdtEntryTable);\r
878ddf1f 407 return EFI_SUCCESS;\r
408}\r
409\r
410VOID\r
411InterruptDistrubutionHub (\r
412 EFI_EXCEPTION_TYPE ExceptionType,\r
413 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord\r
414 )\r
415/*++\r
416\r
417Routine Description: Common piece of code that invokes the registered handlers.\r
418\r
419 This code executes in exception context so no efi calls are allowed.\r
420\r
421Arguments:\r
5fd59c65 422 ExceptionType - exception type\r
423 ContextRecord - system context\r
878ddf1f 424\r
425Returns:\r
426\r
427 None\r
428\r
429--*/\r
878ddf1f 430{\r
431 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r
432 if (ExceptionType != SYSTEM_TIMER_VECTOR) {\r
433 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);\r
434 } else {\r
435 OrigVector = IdtEntryTable[ExceptionType].OrigVector;\r
436 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);\r
437 }\r
438 }\r
439}\r