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