2 X64 specific debug support functions
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
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.
16 // private header files
18 #include "plDebugSupport.h"
21 // This the global main table to keep track of the interrupts
23 IDT_ENTRY
*IdtEntryTable
= NULL
;
24 DESCRIPTOR NullDesc
= {0, 0};
29 IN EFI_EXCEPTION_TYPE ExceptionType
,
34 Routine Description: Allocate pool for a new IDT entry stub. Copy the generic
35 stub into the new buffer and fixup the vector number and jump target address.
38 ExceptionType - This is the exception type that the new stub will be created
40 Stub - On successful exit, *Stub contains the newly allocated entry stub.
43 other possibilities are passed through from AllocatePool
50 // First, allocate a new buffer and copy the stub code into it
52 *Stub
= AllocatePool (StubSize
);
55 CopyMem (StubCopy
, InterruptEntryStub
, StubSize
);
58 // Next fixup the stub code for this vector
61 // The stub code looks like this:
63 // 00000000 6A 00 push 0 ; push vector number - will be modified before installed
64 // 00000002 E9 db 0e9h ; jump rel32
65 // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
69 // poke in the exception type so the second push pushes the exception type
71 StubCopy
[0x1] = (UINT8
) ExceptionType
;
74 // fixup the jump target to point to the common entry
76 *(UINT32
*) &StubCopy
[0x3] = (UINT32
)((UINTN
) CommonIdtEntry
- (UINTN
) &StubCopy
[StubSize
]);
81 return EFI_OUT_OF_RESOURCES
;
87 IN EFI_EXCEPTION_TYPE ExceptionType
,
88 IN
VOID (*NewCallback
) ()
93 Creates a nes entry stub. Then saves the current IDT entry and replaces it
94 with an interrupt gate for the new entry point. The IdtEntryTable is updated
95 with the new registered function.
97 This code executes in boot services context. The stub entry executes in interrupt
101 ExceptionType - specifies which vector to hook.
102 NewCallback - a pointer to the new function to be registered.
106 Other possibilities are passed through by CreateEntryStub
110 BOOLEAN OldIntFlagState
;
113 Status
= CreateEntryStub (ExceptionType
, (VOID
**) &IdtEntryTable
[ExceptionType
].StubEntry
);
114 if (Status
== EFI_SUCCESS
) {
115 OldIntFlagState
= WriteInterruptFlag (0);
116 ReadIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
118 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[0] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.Low
)[0];
119 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.Low
)[3];
120 ((UINT32
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT32
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.High
)[0];
122 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
123 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
124 WriteIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
125 WriteInterruptFlag (OldIntFlagState
);
134 IN EFI_EXCEPTION_TYPE ExceptionType
139 Undoes HookEntry. This code executes in boot services context.
142 ExceptionType - specifies which entry to unhook
149 BOOLEAN OldIntFlagState
;
151 OldIntFlagState
= WriteInterruptFlag (0);
152 WriteIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
153 FreePool ((VOID
*) (UINTN
) IdtEntryTable
[ExceptionType
].StubEntry
);
154 ZeroMem (&IdtEntryTable
[ExceptionType
], sizeof (IDT_ENTRY
));
155 WriteInterruptFlag (OldIntFlagState
);
161 ManageIdtEntryTable (
162 VOID (*NewCallback
)(),
163 EFI_EXCEPTION_TYPE ExceptionType
168 This is the main worker function that manages the state of the interrupt
169 handlers. It both installs and uninstalls interrupt handlers based on the
170 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
171 If NewCallback is non-NULL, then install is indicated.
174 NewCallback - If non-NULL, NewCallback specifies the new handler to register.
175 If NULL, specifies that the previously registered handler should
177 ExceptionType - Indicates which entry to manage
181 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
182 no handler registered for it
183 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
185 Other possible return values are passed through from UnHookEntry and HookEntry.
191 Status
= EFI_SUCCESS
;
193 if (CompareDescriptor (&IdtEntryTable
[ExceptionType
].NewDesc
, &NullDesc
)) {
195 // we've already installed to this vector
197 if (NewCallback
!= NULL
) {
199 // if the input handler is non-null, error
201 Status
= EFI_ALREADY_STARTED
;
203 Status
= UnhookEntry (ExceptionType
);
207 // no user handler installed on this vector
209 if (NewCallback
== NULL
) {
211 // if the input handler is null, error
213 Status
= EFI_INVALID_PARAMETER
;
215 Status
= HookEntry (ExceptionType
, NewCallback
);
224 GetMaximumProcessorIndex (
225 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
226 OUT UINTN
*MaxProcessorIndex
230 Routine Description: This is a DebugSupport protocol member function.
233 This - The DebugSupport instance
234 MaxProcessorIndex - The maximuim supported processor index
237 Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0
241 *MaxProcessorIndex
= 0;
242 return (EFI_SUCCESS
);
247 RegisterPeriodicCallback (
248 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
249 IN UINTN ProcessorIndex
,
250 IN EFI_PERIODIC_CALLBACK PeriodicCallback
254 Routine Description: This is a DebugSupport protocol member function.
257 This - The DebugSupport instance
258 ProcessorIndex - Which processor the callback applies to.
259 PeriodicCallback - Callback function
264 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
265 no handler registered for it
266 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
268 Other possible return values are passed through from UnHookEntry and HookEntry.
272 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
277 RegisterExceptionCallback (
278 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
279 IN UINTN ProcessorIndex
,
280 IN EFI_EXCEPTION_CALLBACK NewCallback
,
281 IN EFI_EXCEPTION_TYPE ExceptionType
286 This is a DebugSupport protocol member function.
288 This code executes in boot services context.
291 This - The DebugSupport instance
292 ProcessorIndex - Which processor the callback applies to.
293 NewCallback - Callback function
294 ExceptionType - Which exception to hook
299 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
300 no handler registered for it
301 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
303 Other possible return values are passed through from UnHookEntry and HookEntry.
307 return ManageIdtEntryTable (NewCallback
, ExceptionType
);
312 InvalidateInstructionCache (
313 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
314 IN UINTN ProcessorIndex
,
321 This is a DebugSupport protocol member function.
322 Calls assembly routine to flush cache.
325 This - The DebugSupport instance
326 ProcessorIndex - Which processor the callback applies to.
327 Start - Physical base of the memory range to be invalidated
328 Length - mininum number of bytes in instruction cache to invalidate
332 EFI_SUCCESS - always return success
341 plInitializeDebugSupportDriver (
347 Initializes driver's handler registration database.
349 This code executes in boot services context.
356 EFI_UNSUPPORTED - if X64 processor does not support FXSTOR/FXRSTOR instructions,
357 the context save will fail, so these processor's are not supported.
358 EFI_OUT_OF_RESOURCES - not resource to finish initialization
362 if (!FxStorSupport ()) {
363 return EFI_UNSUPPORTED
;
365 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
366 if (IdtEntryTable
!= NULL
) {
369 return EFI_OUT_OF_RESOURCES
;
376 plUnloadDebugSupportDriver (
377 IN EFI_HANDLE ImageHandle
382 This is the callback that is written to the LoadedImage protocol instance
383 on the image handle. It uninstalls all registered handlers and frees all entry
386 This code executes in boot services context.
389 ImageHandle - The image handle of the unload handler
393 EFI_SUCCESS - always return success
397 EFI_EXCEPTION_TYPE ExceptionType
;
399 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
400 ManageIdtEntryTable (NULL
, ExceptionType
);
403 FreePool (IdtEntryTable
);
408 InterruptDistrubutionHub (
409 EFI_EXCEPTION_TYPE ExceptionType
,
410 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
414 Routine Description: Common piece of code that invokes the registered handlers.
416 This code executes in exception context so no efi calls are allowed.
419 ExceptionType - exception type
420 ContextRecord - system context
428 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
429 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
430 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
432 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
433 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);