2 IA32 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;
26 #ifndef EFI_NT_EMULATOR
30 IN EFI_EXCEPTION_TYPE ExceptionType
,
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.
39 ExceptionType - This is the exception type that the new stub will be created
41 Stub - On successful exit, *Stub contains the newly allocated entry stub.
44 other possibilities are passed through from AllocatePool
52 // First, allocate a new buffer and copy the stub code into it
54 Status
= gBS
->AllocatePool (EfiBootServicesData
, StubSize
, Stub
);
55 if (Status
== EFI_SUCCESS
) {
57 gBS
->CopyMem (StubCopy
, InterruptEntryStub
, StubSize
);
60 // Next fixup the stub code for this vector
63 // The stub code looks like this:
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
73 // poke in the exception type so the second push pushes the exception type
75 StubCopy
[0x0c] = (UINT8
) ExceptionType
;
78 // fixup the jump target to point to the common entry
80 *(UINT32
*) &StubCopy
[0x0e] = (UINT32
) CommonIdtEntry
- (UINT32
) &StubCopy
[StubSize
];
89 IN EFI_EXCEPTION_TYPE ExceptionType
,
90 IN
VOID (*NewCallback
) ()
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.
99 This code executes in boot services context. The stub entry executes in interrupt
103 ExceptionType - specifies which vector to hook.
104 NewCallback - a pointer to the new function to be registered.
108 Other possibilities are passed through by CreateEntryStub
112 BOOLEAN OldIntFlagState
;
115 Status
= CreateEntryStub (ExceptionType
, (VOID
**) &IdtEntryTable
[ExceptionType
].StubEntry
);
116 if (Status
== EFI_SUCCESS
) {
117 OldIntFlagState
= WriteInterruptFlag (0);
118 ReadIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
120 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[0] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[0];
121 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[3];
123 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
124 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
125 WriteIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
126 WriteInterruptFlag (OldIntFlagState
);
135 IN EFI_EXCEPTION_TYPE ExceptionType
140 Undoes HookEntry. This code executes in boot services context.
143 ExceptionType - specifies which entry to unhook
147 Other values are passed through from FreePool
151 BOOLEAN OldIntFlagState
;
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
);
165 ManageIdtEntryTable (
166 VOID (*NewCallback
)(),
167 EFI_EXCEPTION_TYPE ExceptionType
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.
178 NewCallback - If non-NULL, NewCallback specifies the new handler to register.
179 If NULL, specifies that the previously registered handler should
181 ExceptionType - Indicates which entry to manage
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.
189 Other possible return values are passed through from UnHookEntry and HookEntry.
195 Status
= EFI_SUCCESS
;
197 #ifndef EFI_NT_EMULATOR
198 if (CompareDescriptor (&IdtEntryTable
[ExceptionType
].NewDesc
, &NullDesc
)) {
200 // we've already installed to this vector
202 if (NewCallback
!= NULL
) {
204 // if the input handler is non-null, error
206 Status
= EFI_ALREADY_STARTED
;
208 Status
= UnhookEntry (ExceptionType
);
212 // no user handler installed on this vector
214 if (NewCallback
== NULL
) {
216 // if the input handler is null, error
218 Status
= EFI_INVALID_PARAMETER
;
220 Status
= HookEntry (ExceptionType
, NewCallback
);
229 GetMaximumProcessorIndex (
230 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
231 OUT UINTN
*MaxProcessorIndex
235 Routine Description: This is a DebugSupport protocol member function.
238 This - The DebugSupport instance
239 MaxProcessorIndex - The maximuim supported processor index
242 Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0
246 *MaxProcessorIndex
= 0;
247 return (EFI_SUCCESS
);
252 RegisterPeriodicCallback (
253 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
254 IN UINTN ProcessorIndex
,
255 IN EFI_PERIODIC_CALLBACK PeriodicCallback
259 Routine Description: This is a DebugSupport protocol member function.
262 This - The DebugSupport instance
263 ProcessorIndex - Which processor the callback applies to.
264 PeriodicCallback - Callback function
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.
273 Other possible return values are passed through from UnHookEntry and HookEntry.
277 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
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
291 This is a DebugSupport protocol member function.
293 This code executes in boot services context.
296 This - The DebugSupport instance
297 ProcessorIndex - Which processor the callback applies to.
298 NewCallback - Callback function
299 ExceptionType - Which exception to hook
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.
308 Other possible return values are passed through from UnHookEntry and HookEntry.
312 return ManageIdtEntryTable (NewCallback
, ExceptionType
);
317 InvalidateInstructionCache (
318 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
319 IN UINTN ProcessorIndex
,
326 This is a DebugSupport protocol member function.
327 Calls assembly routine to flush cache.
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
337 EFI_SUCCESS - always return success
346 plInitializeDebugSupportDriver (
352 Initializes driver's handler registration database.
354 This code executes in boot services context.
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
367 if (!FxStorSupport ()) {
368 return EFI_UNSUPPORTED
;
370 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
371 if (IdtEntryTable
!= NULL
) {
374 return EFI_OUT_OF_RESOURCES
;
381 plUnloadDebugSupportDriver (
382 IN EFI_HANDLE ImageHandle
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
391 This code executes in boot services context.
394 ImageHandle - The image handle of the unload handler
398 EFI_SUCCESS - always return success
402 EFI_EXCEPTION_TYPE ExceptionType
;
404 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
405 ManageIdtEntryTable (NULL
, ExceptionType
);
408 gBS
->FreePool (IdtEntryTable
);
413 InterruptDistrubutionHub (
414 EFI_EXCEPTION_TYPE ExceptionType
,
415 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
419 Routine Description: Common piece of code that invokes the registered handlers.
421 This code executes in exception context so no efi calls are allowed.
424 ExceptionType - exception type
425 ContextRecord - system context
433 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
434 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
435 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
437 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
438 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);