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