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