3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 IA32 specific debug support functions
25 // private header files
27 #include "plDebugSupport.h"
30 // This the global main table to keep track of the interrupts
32 IDT_ENTRY
*IdtEntryTable
= NULL
;
33 DESCRIPTOR NullDesc
= 0;
35 #ifndef EFI_NT_EMULATOR
39 IN EFI_EXCEPTION_TYPE ExceptionType
,
44 Routine Description: Allocate pool for a new IDT entry stub. Copy the generic
45 stub into the new buffer and fixup the vector number and jump target address.
48 ExceptionType - This is the exception type that the new stub will be created
50 Stub - On successful exit, *Stub contains the newly allocated entry stub.
53 other possibilities are passed through from AllocatePool
61 // First, allocate a new buffer and copy the stub code into it
63 Status
= gBS
->AllocatePool (EfiBootServicesData
, StubSize
, Stub
);
64 if (Status
== EFI_SUCCESS
) {
66 gBS
->CopyMem (StubCopy
, InterruptEntryStub
, StubSize
);
69 // Next fixup the stub code for this vector
72 // The stub code looks like this:
74 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top
75 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
76 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
77 // 0000000D E9 db 0e9h ; jump rel32
78 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
82 // poke in the exception type so the second push pushes the exception type
84 StubCopy
[0x0c] = (UINT8
) ExceptionType
;
87 // fixup the jump target to point to the common entry
89 *(UINT32
*) &StubCopy
[0x0e] = (UINT32
) CommonIdtEntry
- (UINT32
) &StubCopy
[StubSize
];
98 IN EFI_EXCEPTION_TYPE ExceptionType
,
99 IN
VOID (*NewCallback
) ()
104 Creates a nes entry stub. Then saves the current IDT entry and replaces it
105 with an interrupt gate for the new entry point. The IdtEntryTable is updated
106 with the new registered function.
108 This code executes in boot services context. The stub entry executes in interrupt
112 ExceptionType - specifies which vector to hook.
113 NewCallback - a pointer to the new function to be registered.
117 Other possibilities are passed through by CreateEntryStub
120 // TODO: ) - add argument and description to function comment
122 BOOLEAN OldIntFlagState
;
125 Status
= CreateEntryStub (ExceptionType
, (VOID
**) &IdtEntryTable
[ExceptionType
].StubEntry
);
126 if (Status
== EFI_SUCCESS
) {
127 OldIntFlagState
= WriteInterruptFlag (0);
128 ReadIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
130 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[0] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[0];
131 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[3];
133 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
134 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
135 WriteIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
136 WriteInterruptFlag (OldIntFlagState
);
145 IN EFI_EXCEPTION_TYPE ExceptionType
150 Undoes HookEntry. This code executes in boot services context.
153 ExceptionType - specifies which entry to unhook
157 Other values are passed through from FreePool
161 BOOLEAN OldIntFlagState
;
164 OldIntFlagState
= WriteInterruptFlag (0);
165 WriteIdt (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
166 Status
= gBS
->FreePool ((VOID
*) (UINTN
) IdtEntryTable
[ExceptionType
].StubEntry
);
167 ZeroMem (&IdtEntryTable
[ExceptionType
], sizeof (IDT_ENTRY
));
168 WriteInterruptFlag (OldIntFlagState
);
175 ManageIdtEntryTable (
176 VOID (*NewCallback
)(),
177 EFI_EXCEPTION_TYPE ExceptionType
182 This is the main worker function that manages the state of the interrupt
183 handlers. It both installs and uninstalls interrupt handlers based on the
184 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
185 If NewCallback is non-NULL, then install is indicated.
188 NewCallback - If non-NULL, NewCallback specifies the new handler to register.
189 If NULL, specifies that the previously registered handler should
191 ExceptionType - Indicates which entry to manage
195 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
196 no handler registered for it
197 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
199 Other possible return values are passed through from UnHookEntry and HookEntry.
202 // TODO: ) - add argument and description to function comment
206 Status
= EFI_SUCCESS
;
208 #ifndef EFI_NT_EMULATOR
209 if (CompareDescriptor (&IdtEntryTable
[ExceptionType
].NewDesc
, &NullDesc
)) {
211 // we've already installed to this vector
213 if (NewCallback
!= NULL
) {
215 // if the input handler is non-null, error
217 Status
= EFI_ALREADY_STARTED
;
219 Status
= UnhookEntry (ExceptionType
);
223 // no user handler installed on this vector
225 if (NewCallback
== NULL
) {
227 // if the input handler is null, error
229 Status
= EFI_INVALID_PARAMETER
;
231 Status
= HookEntry (ExceptionType
, NewCallback
);
240 GetMaximumProcessorIndex (
241 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
242 OUT UINTN
*MaxProcessorIndex
246 Routine Description: This is a DebugSupport protocol member function.
250 Returns: Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0
253 // TODO: This - add argument and description to function comment
254 // TODO: MaxProcessorIndex - add argument and description to function comment
256 *MaxProcessorIndex
= 0;
257 return (EFI_SUCCESS
);
262 RegisterPeriodicCallback (
263 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
264 IN UINTN ProcessorIndex
,
265 IN EFI_PERIODIC_CALLBACK PeriodicCallback
269 Routine Description: This is a DebugSupport protocol member function.
276 // TODO: This - add argument and description to function comment
277 // TODO: ProcessorIndex - add argument and description to function comment
278 // TODO: PeriodicCallback - add argument and description to function comment
280 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
285 RegisterExceptionCallback (
286 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
287 IN UINTN ProcessorIndex
,
288 IN EFI_EXCEPTION_CALLBACK NewCallback
,
289 IN EFI_EXCEPTION_TYPE ExceptionType
294 This is a DebugSupport protocol member function.
296 This code executes in boot services context.
305 // TODO: This - add argument and description to function comment
306 // TODO: ProcessorIndex - add argument and description to function comment
307 // TODO: NewCallback - add argument and description to function comment
308 // TODO: ExceptionType - add argument and description to function comment
310 return ManageIdtEntryTable (NewCallback
, ExceptionType
);
315 InvalidateInstructionCache (
316 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
317 IN UINTN ProcessorIndex
,
324 This is a DebugSupport protocol member function.
325 For IA32, this is a no-op since the instruction and data caches are coherent.
334 // TODO: This - add argument and description to function comment
335 // TODO: ProcessorIndex - add argument and description to function comment
336 // TODO: Start - add argument and description to function comment
337 // TODO: Length - add argument and description to function comment
338 // TODO: EFI_SUCCESS - add return value to function comment
344 plInitializeDebugSupportDriver (
350 Initializes driver's handler registration database.
352 This code executes in boot services context.
359 EFI_UNSUPPORTED - if IA32 processor does not support FXSTOR/FXRSTOR instructions,
360 the context save will fail, so these processor's are not supported.
363 // TODO: EFI_OUT_OF_RESOURCES - add return value to function comment
365 if (!FxStorSupport ()) {
366 return EFI_UNSUPPORTED
;
368 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
369 if (IdtEntryTable
!= NULL
) {
372 return EFI_OUT_OF_RESOURCES
;
379 plUnloadDebugSupportDriver (
380 IN EFI_HANDLE ImageHandle
385 This is the callback that is written to the LoadedImage protocol instance
386 on the image handle. It uninstalls all registered handlers and frees all entry
389 This code executes in boot services context.
392 ImageHandle - The image handle of the unload handler
399 // TODO: EFI_SUCCESS - add return value to function comment
401 EFI_EXCEPTION_TYPE ExceptionType
;
403 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
404 ManageIdtEntryTable (NULL
, ExceptionType
);
407 gBS
->FreePool (IdtEntryTable
);
412 InterruptDistrubutionHub (
413 EFI_EXCEPTION_TYPE ExceptionType
,
414 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
418 Routine Description: Common piece of code that invokes the registered handlers.
420 This code executes in exception context so no efi calls are allowed.
429 // TODO: ExceptionType - add argument and description to function comment
430 // TODO: ContextRecord - add argument and description to function comment
432 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
433 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
434 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
436 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
437 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);