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