]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c
ECC clean up.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / PlDebugSupport.c
CommitLineData
9e604fe4 1/** @file\r
c1f23d63 2 IA32 specific debug support functions\r
3\r
9e604fe4 4Copyright (c) 2006 - 2008, Intel Corporation\r
c1f23d63 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
e9f9d09a 18#include "PlDebugSupport.h"\r
c1f23d63 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
9e604fe4 26/**\r
27 Allocate pool for a new IDT entry stub.\r
28\r
29 Copy the generic stub into the new buffer and fixup the vector number\r
30 and jump target address.\r
31\r
32 @param ExceptionType This is the exception type that the new stub will be created\r
33 for.\r
34 @param Stub On successful exit, *Stub contains the newly allocated entry stub.\r
35\r
36 @retval EFI_SUCCESS Always.\r
37\r
38**/\r
c1f23d63 39EFI_STATUS\r
40CreateEntryStub (\r
41 IN EFI_EXCEPTION_TYPE ExceptionType,\r
42 OUT VOID **Stub\r
43 )\r
c1f23d63 44{\r
45 UINT8 *StubCopy;\r
46\r
47 StubCopy = *Stub;\r
48\r
49 //\r
50 // Fixup the stub code for this vector\r
51 //\r
52\r
53 // The stub code looks like this:\r
54 //\r
55 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top\r
56 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack\r
57 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed\r
58 // 0000000D E9 db 0e9h ; jump rel32\r
59 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry\r
60 //\r
61\r
62 //\r
63 // poke in the exception type so the second push pushes the exception type\r
64 //\r
65 StubCopy[0x0c] = (UINT8) ExceptionType;\r
66\r
67 //\r
68 // fixup the jump target to point to the common entry\r
69 //\r
70 *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];\r
71\r
72 return EFI_SUCCESS;\r
73}\r
74\r
9e604fe4 75/**\r
c1f23d63 76 Creates a nes entry stub. Then saves the current IDT entry and replaces it\r
77 with an interrupt gate for the new entry point. The IdtEntryTable is updated\r
78 with the new registered function.\r
79\r
80 This code executes in boot services context. The stub entry executes in interrupt\r
81 context.\r
82\r
9e604fe4 83 @param ExceptionType Specifies which vector to hook.\r
84 @param NewCallback A pointer to the new function to be registered.\r
c1f23d63 85\r
9e604fe4 86 @retval EFI_SUCCESS Always.\r
c1f23d63 87\r
9e604fe4 88**/\r
89EFI_STATUS\r
90HookEntry (\r
91 IN EFI_EXCEPTION_TYPE ExceptionType,\r
92 IN VOID (*NewCallback) ()\r
93 )\r
c1f23d63 94{\r
95 BOOLEAN OldIntFlagState;\r
96 EFI_STATUS Status;\r
97\r
98 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);\r
99 if (Status == EFI_SUCCESS) {\r
100 OldIntFlagState = WriteInterruptFlag (0);\r
9e604fe4 101 READ_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
c1f23d63 102\r
103 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[0];\r
104 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];\r
105\r
106 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r
107 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r
9e604fe4 108 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r
c1f23d63 109 WriteInterruptFlag (OldIntFlagState);\r
110 }\r
111\r
112 return Status;\r
113}\r
114\r
9e604fe4 115/**\r
c1f23d63 116 Undoes HookEntry. This code executes in boot services context.\r
117\r
9e604fe4 118 @param ExceptionType Specifies which entry to unhook\r
c1f23d63 119\r
9e604fe4 120 @retval EFI_SUCCESS Always.\r
c1f23d63 121\r
9e604fe4 122**/\r
123EFI_STATUS\r
124UnhookEntry (\r
125 IN EFI_EXCEPTION_TYPE ExceptionType\r
126 )\r
c1f23d63 127{\r
128 BOOLEAN OldIntFlagState;\r
129\r
130 OldIntFlagState = WriteInterruptFlag (0);\r
9e604fe4 131 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
c1f23d63 132 WriteInterruptFlag (OldIntFlagState);\r
133\r
134 return EFI_SUCCESS;\r
135}\r
136\r
9e604fe4 137/**\r
c1f23d63 138 This is the main worker function that manages the state of the interrupt\r
139 handlers. It both installs and uninstalls interrupt handlers based on the\r
140 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.\r
141 If NewCallback is non-NULL, then install is indicated.\r
142\r
9e604fe4 143 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.\r
144 If NULL, specifies that the previously registered handler should\r
145 be uninstalled.\r
146 @param ExceptionType Indicates which entry to manage.\r
c1f23d63 147\r
9e604fe4 148 @retval EFI_SUCCESS Process is ok.\r
149 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has\r
150 no handler registered for it\r
151 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.\r
152 @retval others Possible return values are passed through from UnHookEntry and HookEntry.\r
c1f23d63 153\r
9e604fe4 154**/\r
155EFI_STATUS\r
156ManageIdtEntryTable (\r
157 VOID (*NewCallback)(),\r
158 EFI_EXCEPTION_TYPE ExceptionType\r
159 )\r
c1f23d63 160{\r
161 EFI_STATUS Status;\r
162\r
163 Status = EFI_SUCCESS;\r
164\r
631c1991 165 if (!FeaturePcdGet (PcdNtEmulatorEnable)) {\r
9e604fe4 166 if (COMPARE_DESCRIPTOR (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {\r
c1f23d63 167 //\r
168 // we've already installed to this vector\r
169 //\r
170 if (NewCallback != NULL) {\r
171 //\r
172 // if the input handler is non-null, error\r
173 //\r
174 Status = EFI_ALREADY_STARTED;\r
175 } else {\r
176 Status = UnhookEntry (ExceptionType);\r
177 }\r
178 } else {\r
179 //\r
180 // no user handler installed on this vector\r
181 //\r
182 if (NewCallback == NULL) {\r
183 //\r
184 // if the input handler is null, error\r
185 //\r
186 Status = EFI_INVALID_PARAMETER;\r
187 } else {\r
188 Status = HookEntry (ExceptionType, NewCallback);\r
189 }\r
190 }\r
191 }\r
192\r
193 return Status;\r
194}\r
195\r
9e604fe4 196/**\r
197 This is a DebugSupport protocol member function, hard\r
198 coded to support only 1 processor for now.\r
199\r
200 @param This The DebugSupport instance\r
201 @param MaxProcessorIndex The maximuim supported processor index\r
202\r
203 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.\r
204\r
205**/\r
c1f23d63 206EFI_STATUS\r
207EFIAPI\r
208GetMaximumProcessorIndex (\r
209 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
210 OUT UINTN *MaxProcessorIndex\r
211 )\r
c1f23d63 212{\r
213 *MaxProcessorIndex = 0;\r
214 return (EFI_SUCCESS);\r
215}\r
216\r
9e604fe4 217/**\r
218 DebugSupport protocol member function.\r
219\r
220 @param This The DebugSupport instance\r
221 @param ProcessorIndex Which processor the callback applies to.\r
222 @param PeriodicCallback Callback function\r
223\r
224 @retval EFI_SUCCESS Indicates the callback was registered.\r
225 @retval others Callback was not registered.\r
226\r
227**/\r
c1f23d63 228EFI_STATUS\r
229EFIAPI\r
230RegisterPeriodicCallback (\r
231 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
232 IN UINTN ProcessorIndex,\r
233 IN EFI_PERIODIC_CALLBACK PeriodicCallback\r
234 )\r
9e604fe4 235{\r
236 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);\r
237}\r
c1f23d63 238\r
9e604fe4 239/**\r
240 DebugSupport protocol member function.\r
c1f23d63 241\r
9e604fe4 242 This code executes in boot services context.\r
c1f23d63 243\r
9e604fe4 244 @param This The DebugSupport instance\r
245 @param ProcessorIndex Which processor the callback applies to.\r
246 @param NewCallback Callback function\r
247 @param ExceptionType Which exception to hook\r
c1f23d63 248\r
9e604fe4 249 @retval EFI_SUCCESS Indicates the callback was registered.\r
250 @retval others Callback was not registered.\r
c1f23d63 251\r
9e604fe4 252**/\r
c1f23d63 253EFI_STATUS\r
254EFIAPI\r
255RegisterExceptionCallback (\r
256 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
257 IN UINTN ProcessorIndex,\r
258 IN EFI_EXCEPTION_CALLBACK NewCallback,\r
259 IN EFI_EXCEPTION_TYPE ExceptionType\r
260 )\r
c1f23d63 261{\r
262 return ManageIdtEntryTable (NewCallback, ExceptionType);\r
263}\r
264\r
9e604fe4 265/**\r
266 DebugSupport protocol member function. Calls assembly routine to flush cache.\r
267\r
268 @param This The DebugSupport instance\r
269 @param ProcessorIndex Which processor the callback applies to.\r
270 @param Start Physical base of the memory range to be invalidated\r
271 @param Length mininum number of bytes in instruction cache to invalidate\r
272\r
273 @retval EFI_SUCCESS Always returned.\r
274\r
275**/\r
c1f23d63 276EFI_STATUS\r
277EFIAPI\r
278InvalidateInstructionCache (\r
279 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
280 IN UINTN ProcessorIndex,\r
281 IN VOID *Start,\r
282 IN UINT64 Length\r
283 )\r
c1f23d63 284{\r
285 AsmWbinvd ();\r
286 return EFI_SUCCESS;\r
287}\r
288\r
9e604fe4 289/**\r
290 Initializes driver's handler registration databas. \r
291 \r
292 This code executes in boot services context\r
293 Must be public because it's referenced from DebugSupport.c\r
294\r
295 @retval EFI_UNSUPPORTED If IA32 processor does not support FXSTOR/FXRSTOR instructions,\r
296 the context save will fail, so these processor's are not supported.\r
297 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.\r
298 @retval EFI_SUCCESS Initializes successfully.\r
299\r
300**/\r
c1f23d63 301EFI_STATUS\r
9e604fe4 302PlInitializeDebugSupportDriver (\r
c1f23d63 303 VOID\r
304 )\r
c1f23d63 305{\r
306 EFI_EXCEPTION_TYPE ExceptionType;\r
307\r
308 if (!FxStorSupport ()) {\r
309 return EFI_UNSUPPORTED;\r
310 }\r
311\r
312 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);\r
313 if (IdtEntryTable == NULL) {\r
314 return EFI_OUT_OF_RESOURCES;\r
315 }\r
316\r
317 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
318 IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);\r
319 if (IdtEntryTable[ExceptionType].StubEntry == NULL) {\r
320 goto ErrorCleanup;\r
321 }\r
322\r
323 CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);\r
324 }\r
325 return EFI_SUCCESS;\r
326\r
327ErrorCleanup:\r
328\r
329 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
330 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {\r
331 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r
332 }\r
333 }\r
334 FreePool (IdtEntryTable);\r
335\r
336 return EFI_OUT_OF_RESOURCES;\r
337}\r
338\r
9e604fe4 339/**\r
c1f23d63 340 This is the callback that is written to the LoadedImage protocol instance\r
341 on the image handle. It uninstalls all registered handlers and frees all entry\r
342 stub memory.\r
343\r
9e604fe4 344 @param ImageHandle The firmware allocated handle for the EFI image.\r
c1f23d63 345\r
9e604fe4 346 @retval EFI_SUCCESS Always.\r
c1f23d63 347\r
9e604fe4 348**/\r
349EFI_STATUS\r
350EFIAPI\r
351PlUnloadDebugSupportDriver (\r
352 IN EFI_HANDLE ImageHandle\r
353 )\r
c1f23d63 354{\r
355 EFI_EXCEPTION_TYPE ExceptionType;\r
356\r
357 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
358 ManageIdtEntryTable (NULL, ExceptionType);\r
359 }\r
360\r
361 FreePool (IdtEntryTable);\r
362 return EFI_SUCCESS;\r
363}\r
364\r
9e604fe4 365/**\r
366 Common piece of code that invokes the registered handlers.\r
367\r
368 This code executes in exception context so no efi calls are allowed.\r
369\r
370 @param ExceptionType Exception type\r
371 @param ContextRecord System context\r
372\r
373**/\r
c1f23d63 374VOID\r
375InterruptDistrubutionHub (\r
376 EFI_EXCEPTION_TYPE ExceptionType,\r
377 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord\r
378 )\r
c1f23d63 379{\r
380 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r
381 if (ExceptionType != SYSTEM_TIMER_VECTOR) {\r
382 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);\r
383 } else {\r
384 OrigVector = IdtEntryTable[ExceptionType].OrigVector;\r
385 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);\r
386 }\r
387 }\r
388}\r