2 IA32 specific debug support functions
4 Copyright (c) 2006 - 2008, 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;
27 Allocate pool for a new IDT entry stub.
29 Copy the generic stub into the new buffer and fixup the vector number
30 and jump target address.
32 @param ExceptionType This is the exception type that the new stub will be created
34 @param Stub On successful exit, *Stub contains the newly allocated entry stub.
36 @retval EFI_SUCCESS Always.
41 IN EFI_EXCEPTION_TYPE ExceptionType
,
50 // Fixup the stub code for this vector
53 // The stub code looks like this:
55 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top
56 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
57 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
58 // 0000000D E9 db 0e9h ; jump rel32
59 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
63 // poke in the exception type so the second push pushes the exception type
65 StubCopy
[0x0c] = (UINT8
) ExceptionType
;
68 // fixup the jump target to point to the common entry
70 *(UINT32
*) &StubCopy
[0x0e] = (UINT32
) CommonIdtEntry
- (UINT32
) &StubCopy
[StubSize
];
76 Creates a nes entry stub. Then saves the current IDT entry and replaces it
77 with an interrupt gate for the new entry point. The IdtEntryTable is updated
78 with the new registered function.
80 This code executes in boot services context. The stub entry executes in interrupt
83 @param ExceptionType Specifies which vector to hook.
84 @param NewCallback A pointer to the new function to be registered.
86 @retval EFI_SUCCESS Always.
91 IN EFI_EXCEPTION_TYPE ExceptionType
,
92 IN
VOID (*NewCallback
) ()
95 BOOLEAN OldIntFlagState
;
98 Status
= CreateEntryStub (ExceptionType
, (VOID
**) &IdtEntryTable
[ExceptionType
].StubEntry
);
99 if (Status
== EFI_SUCCESS
) {
100 OldIntFlagState
= WriteInterruptFlag (0);
101 READ_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
103 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[0] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[0];
104 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
)[3];
106 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
107 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
108 WRITE_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
109 WriteInterruptFlag (OldIntFlagState
);
116 Undoes HookEntry. This code executes in boot services context.
118 @param ExceptionType Specifies which entry to unhook
120 @retval EFI_SUCCESS Always.
125 IN EFI_EXCEPTION_TYPE ExceptionType
128 BOOLEAN OldIntFlagState
;
130 OldIntFlagState
= WriteInterruptFlag (0);
131 WRITE_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
132 WriteInterruptFlag (OldIntFlagState
);
138 This is the main worker function that manages the state of the interrupt
139 handlers. It both installs and uninstalls interrupt handlers based on the
140 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
141 If NewCallback is non-NULL, then install is indicated.
143 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
144 If NULL, specifies that the previously registered handler should
146 @param ExceptionType Indicates which entry to manage.
148 @retval EFI_SUCCESS Process is ok.
149 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
150 no handler registered for it
151 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
152 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
156 ManageIdtEntryTable (
157 VOID (*NewCallback
)(),
158 EFI_EXCEPTION_TYPE ExceptionType
163 Status
= EFI_SUCCESS
;
165 if (!FeaturePcdGet (PcdNtEmulatorEnable
)) {
166 if (COMPARE_DESCRIPTOR (&IdtEntryTable
[ExceptionType
].NewDesc
, &NullDesc
)) {
168 // we've already installed to this vector
170 if (NewCallback
!= NULL
) {
172 // if the input handler is non-null, error
174 Status
= EFI_ALREADY_STARTED
;
176 Status
= UnhookEntry (ExceptionType
);
180 // no user handler installed on this vector
182 if (NewCallback
== NULL
) {
184 // if the input handler is null, error
186 Status
= EFI_INVALID_PARAMETER
;
188 Status
= HookEntry (ExceptionType
, NewCallback
);
197 This is a DebugSupport protocol member function, hard
198 coded to support only 1 processor for now.
200 @param This The DebugSupport instance
201 @param MaxProcessorIndex The maximuim supported processor index
203 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
208 GetMaximumProcessorIndex (
209 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
210 OUT UINTN
*MaxProcessorIndex
213 *MaxProcessorIndex
= 0;
214 return (EFI_SUCCESS
);
218 DebugSupport protocol member function.
220 @param This The DebugSupport instance
221 @param ProcessorIndex Which processor the callback applies to.
222 @param PeriodicCallback Callback function
224 @retval EFI_SUCCESS Indicates the callback was registered.
225 @retval others Callback was not registered.
230 RegisterPeriodicCallback (
231 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
232 IN UINTN ProcessorIndex
,
233 IN EFI_PERIODIC_CALLBACK PeriodicCallback
236 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
240 DebugSupport protocol member function.
242 This code executes in boot services context.
244 @param This The DebugSupport instance
245 @param ProcessorIndex Which processor the callback applies to.
246 @param NewCallback Callback function
247 @param ExceptionType Which exception to hook
249 @retval EFI_SUCCESS Indicates the callback was registered.
250 @retval others Callback was not registered.
255 RegisterExceptionCallback (
256 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
257 IN UINTN ProcessorIndex
,
258 IN EFI_EXCEPTION_CALLBACK NewCallback
,
259 IN EFI_EXCEPTION_TYPE ExceptionType
262 return ManageIdtEntryTable (NewCallback
, ExceptionType
);
266 DebugSupport protocol member function. Calls assembly routine to flush cache.
268 @param This The DebugSupport instance
269 @param ProcessorIndex Which processor the callback applies to.
270 @param Start Physical base of the memory range to be invalidated
271 @param Length mininum number of bytes in instruction cache to invalidate
273 @retval EFI_SUCCESS Always returned.
278 InvalidateInstructionCache (
279 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
280 IN UINTN ProcessorIndex
,
290 Initializes driver's handler registration databas.
292 This code executes in boot services context
293 Must be public because it's referenced from DebugSupport.c
295 @retval EFI_UNSUPPORTED If IA32 processor does not support FXSTOR/FXRSTOR instructions,
296 the context save will fail, so these processor's are not supported.
297 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
298 @retval EFI_SUCCESS Initializes successfully.
302 PlInitializeDebugSupportDriver (
306 EFI_EXCEPTION_TYPE ExceptionType
;
308 if (!FxStorSupport ()) {
309 return EFI_UNSUPPORTED
;
312 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
313 if (IdtEntryTable
== NULL
) {
314 return EFI_OUT_OF_RESOURCES
;
317 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
318 IdtEntryTable
[ExceptionType
].StubEntry
= (DEBUG_PROC
) (UINTN
) AllocatePool (StubSize
);
319 if (IdtEntryTable
[ExceptionType
].StubEntry
== NULL
) {
323 CopyMem ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
, InterruptEntryStub
, StubSize
);
329 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
330 if (IdtEntryTable
[ExceptionType
].StubEntry
!= NULL
) {
331 FreePool ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
);
334 FreePool (IdtEntryTable
);
336 return EFI_OUT_OF_RESOURCES
;
340 This is the callback that is written to the LoadedImage protocol instance
341 on the image handle. It uninstalls all registered handlers and frees all entry
344 @param ImageHandle The firmware allocated handle for the EFI image.
346 @retval EFI_SUCCESS Always.
351 PlUnloadDebugSupportDriver (
352 IN EFI_HANDLE ImageHandle
355 EFI_EXCEPTION_TYPE ExceptionType
;
357 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
358 ManageIdtEntryTable (NULL
, ExceptionType
);
361 FreePool (IdtEntryTable
);
366 Common piece of code that invokes the registered handlers.
368 This code executes in exception context so no efi calls are allowed.
370 @param ExceptionType Exception type
371 @param ContextRecord System context
375 InterruptDistrubutionHub (
376 EFI_EXCEPTION_TYPE ExceptionType
,
377 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
380 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
381 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
382 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
384 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
385 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);