2 X64 specific debug support functions
4 Copyright (c) 2006 - 2007, 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};
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 6A 00 push 0 ; push vector number - will be modified before installed
56 // 00000002 E9 db 0e9h ; jump rel32
57 // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
61 // poke in the exception type so the second push pushes the exception type
63 StubCopy
[0x1] = (UINT8
) ExceptionType
;
66 // fixup the jump target to point to the common entry
68 *(UINT32
*) &StubCopy
[0x3] = (UINT32
)((UINTN
) CommonIdtEntry
- (UINTN
) &StubCopy
[StubSize
]);
74 Creates a nes entry stub. Then saves the current IDT entry and replaces it
75 with an interrupt gate for the new entry point. The IdtEntryTable is updated
76 with the new registered function.
78 This code executes in boot services context. The stub entry executes in interrupt
81 @param ExceptionType Specifies which vector to hook.
82 @param NewCallback A pointer to the new function to be registered.
84 @retval EFI_SUCCESS Always.
89 IN EFI_EXCEPTION_TYPE ExceptionType
,
90 IN
VOID (*NewCallback
) ()
93 BOOLEAN OldIntFlagState
;
96 Status
= CreateEntryStub (ExceptionType
, (VOID
**) &IdtEntryTable
[ExceptionType
].StubEntry
);
97 if (Status
== EFI_SUCCESS
) {
98 OldIntFlagState
= WriteInterruptFlag (0);
99 READ_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
101 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[0] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.Low
)[0];
102 ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT16
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.Low
)[3];
103 ((UINT32
*) &IdtEntryTable
[ExceptionType
].OrigVector
)[1] = ((UINT32
*) &IdtEntryTable
[ExceptionType
].OrigDesc
.High
)[0];
105 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
106 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
107 WRITE_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
108 WriteInterruptFlag (OldIntFlagState
);
115 Undoes HookEntry. This code executes in boot services context.
117 @param ExceptionType Specifies which entry to unhook
119 @retval EFI_SUCCESS Always.
124 IN EFI_EXCEPTION_TYPE ExceptionType
127 BOOLEAN OldIntFlagState
;
129 OldIntFlagState
= WriteInterruptFlag (0);
130 WRITE_IDT (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
131 WriteInterruptFlag (OldIntFlagState
);
137 This is the main worker function that manages the state of the interrupt
138 handlers. It both installs and uninstalls interrupt handlers based on the
139 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
140 If NewCallback is non-NULL, then install is indicated.
142 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
143 If NULL, specifies that the previously registered handler should
145 @param ExceptionType Indicates which entry to manage.
147 @retval EFI_SUCCESS Process is ok.
148 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
149 no handler registered for it
150 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
151 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
155 ManageIdtEntryTable (
156 VOID (*NewCallback
)(),
157 EFI_EXCEPTION_TYPE ExceptionType
162 Status
= EFI_SUCCESS
;
164 if (COMPARE_DESCRIPTOR (&IdtEntryTable
[ExceptionType
].NewDesc
, &NullDesc
)) {
166 // we've already installed to this vector
168 if (NewCallback
!= NULL
) {
170 // if the input handler is non-null, error
172 Status
= EFI_ALREADY_STARTED
;
174 Status
= UnhookEntry (ExceptionType
);
178 // no user handler installed on this vector
180 if (NewCallback
== NULL
) {
182 // if the input handler is null, error
184 Status
= EFI_INVALID_PARAMETER
;
186 Status
= HookEntry (ExceptionType
, NewCallback
);
194 This is a DebugSupport protocol member function, hard
195 coded to support only 1 processor for now.
197 @param This The DebugSupport instance
198 @param MaxProcessorIndex The maximuim supported processor index
200 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
205 GetMaximumProcessorIndex (
206 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
207 OUT UINTN
*MaxProcessorIndex
210 *MaxProcessorIndex
= 0;
211 return (EFI_SUCCESS
);
215 DebugSupport protocol member function.
217 @param This The DebugSupport instance
218 @param ProcessorIndex Which processor the callback applies to.
219 @param PeriodicCallback Callback function
221 @retval EFI_SUCCESS Indicates the callback was registered.
222 @retval others Callback was not registered.
227 RegisterPeriodicCallback (
228 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
229 IN UINTN ProcessorIndex
,
230 IN EFI_PERIODIC_CALLBACK PeriodicCallback
233 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
237 DebugSupport protocol member function.
239 This code executes in boot services context.
241 @param This The DebugSupport instance
242 @param ProcessorIndex Which processor the callback applies to.
243 @param NewCallback Callback function
244 @param ExceptionType Which exception to hook
246 @retval EFI_SUCCESS Indicates the callback was registered.
247 @retval others Callback was not registered.
252 RegisterExceptionCallback (
253 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
254 IN UINTN ProcessorIndex
,
255 IN EFI_EXCEPTION_CALLBACK NewCallback
,
256 IN EFI_EXCEPTION_TYPE ExceptionType
259 return ManageIdtEntryTable (NewCallback
, ExceptionType
);
263 DebugSupport protocol member function. Calls assembly routine to flush cache.
265 @param This The DebugSupport instance
266 @param ProcessorIndex Which processor the callback applies to.
267 @param Start Physical base of the memory range to be invalidated
268 @param Length mininum number of bytes in instruction cache to invalidate
270 @retval EFI_SUCCESS Always returned.
275 InvalidateInstructionCache (
276 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
277 IN UINTN ProcessorIndex
,
287 Initializes driver's handler registration databas.
289 This code executes in boot services context
290 Must be public because it's referenced from DebugSupport.c
292 @retval EFI_UNSUPPORTED If x64 processor does not support FXSTOR/FXRSTOR instructions,
293 the context save will fail, so these processor's are not supported.
294 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
295 @retval EFI_SUCCESS Initializes successfully.
299 PlInitializeDebugSupportDriver (
303 EFI_EXCEPTION_TYPE ExceptionType
;
305 if (!FxStorSupport ()) {
306 return EFI_UNSUPPORTED
;
309 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
310 if (IdtEntryTable
== NULL
) {
311 return EFI_OUT_OF_RESOURCES
;
314 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
315 IdtEntryTable
[ExceptionType
].StubEntry
= (DEBUG_PROC
) (UINTN
) AllocatePool (StubSize
);
316 if (IdtEntryTable
[ExceptionType
].StubEntry
== NULL
) {
320 CopyMem ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
, InterruptEntryStub
, StubSize
);
326 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
327 if (IdtEntryTable
[ExceptionType
].StubEntry
!= NULL
) {
328 FreePool ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
);
331 FreePool (IdtEntryTable
);
333 return EFI_OUT_OF_RESOURCES
;
337 This is the callback that is written to the LoadedImage protocol instance
338 on the image handle. It uninstalls all registered handlers and frees all entry
341 @param ImageHandle The firmware allocated handle for the EFI image.
343 @retval EFI_SUCCESS Always.
348 PlUnloadDebugSupportDriver (
349 IN EFI_HANDLE ImageHandle
352 EFI_EXCEPTION_TYPE ExceptionType
;
354 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
355 ManageIdtEntryTable (NULL
, ExceptionType
);
358 FreePool (IdtEntryTable
);
363 Common piece of code that invokes the registered handlers.
365 This code executes in exception context so no efi calls are allowed.
367 @param ExceptionType Exception type
368 @param ContextRecord System context
372 InterruptDistrubutionHub (
373 EFI_EXCEPTION_TYPE ExceptionType
,
374 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
377 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
378 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
379 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
381 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
382 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);