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