2 IA32/x64 generic functions to support Debug Support protocol.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DebugSupport.h"
12 // This the global main table to keep track of the interrupts
14 IDT_ENTRY
*IdtEntryTable
= NULL
;
17 Read IDT Gate Descriptor from IDT Table.
19 @param Vector Specifies vector number.
20 @param IdtGateDescriptor Pointer to IDT Gate Descriptor read from IDT Table.
24 ReadIdtGateDescriptor (
25 IN EFI_EXCEPTION_TYPE Vector
,
26 OUT IA32_IDT_GATE_DESCRIPTOR
*IdtGateDescriptor
29 IA32_DESCRIPTOR IdtrValue
;
30 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
32 AsmReadIdtr (&IdtrValue
);
33 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtrValue
.Base
;
35 CopyMem ((VOID
*)IdtGateDescriptor
, (VOID
*)&(IdtTable
)[Vector
], sizeof (IA32_IDT_GATE_DESCRIPTOR
));
39 Write IDT Gate Descriptor into IDT Table.
41 @param Vector Specifies vector number.
42 @param IdtGateDescriptor Pointer to IDT Gate Descriptor written into IDT Table.
46 WriteIdtGateDescriptor (
47 EFI_EXCEPTION_TYPE Vector
,
48 IA32_IDT_GATE_DESCRIPTOR
*IdtGateDescriptor
51 IA32_DESCRIPTOR IdtrValue
;
52 IA32_IDT_GATE_DESCRIPTOR
*IdtTable
;
54 AsmReadIdtr (&IdtrValue
);
55 IdtTable
= (IA32_IDT_GATE_DESCRIPTOR
*)IdtrValue
.Base
;
57 CopyMem ((VOID
*)&(IdtTable
)[Vector
], (VOID
*)IdtGateDescriptor
, sizeof (IA32_IDT_GATE_DESCRIPTOR
));
61 Creates a nes entry stub. Then saves the current IDT entry and replaces it
62 with an interrupt gate for the new entry point. The IdtEntryTable is updated
63 with the new registered function.
65 This code executes in boot services context. The stub entry executes in interrupt
68 @param ExceptionType Specifies which vector to hook.
69 @param NewCallback A pointer to the new function to be registered.
74 IN EFI_EXCEPTION_TYPE ExceptionType
,
75 IN CALLBACK_FUNC NewCallback
78 BOOLEAN OldIntFlagState
;
80 CreateEntryStub (ExceptionType
, (VOID
**)&IdtEntryTable
[ExceptionType
].StubEntry
);
83 // Disables CPU interrupts and returns the previous interrupt state
85 OldIntFlagState
= SaveAndDisableInterrupts ();
88 // gets IDT Gate descriptor by index
90 ReadIdtGateDescriptor (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
92 // stores orignal interrupt handle
94 IdtEntryTable
[ExceptionType
].OrigVector
= (DEBUG_PROC
)GetInterruptHandleFromIdt (&(IdtEntryTable
[ExceptionType
].OrigDesc
));
97 // encodes new IDT Gate descriptor by stub entry
99 Vect2Desc (&IdtEntryTable
[ExceptionType
].NewDesc
, IdtEntryTable
[ExceptionType
].StubEntry
);
101 // stores NewCallback
103 IdtEntryTable
[ExceptionType
].RegisteredCallback
= NewCallback
;
106 // writes back new IDT Gate descriptor
108 WriteIdtGateDescriptor (ExceptionType
, &(IdtEntryTable
[ExceptionType
].NewDesc
));
111 // restore interrupt state
113 SetInterruptState (OldIntFlagState
);
119 Undoes HookEntry. This code executes in boot services context.
121 @param ExceptionType Specifies which entry to unhook
126 IN EFI_EXCEPTION_TYPE ExceptionType
129 BOOLEAN OldIntFlagState
;
132 // Disables CPU interrupts and returns the previous interrupt state
134 OldIntFlagState
= SaveAndDisableInterrupts ();
137 // restore the default IDT Date Descriptor
139 WriteIdtGateDescriptor (ExceptionType
, &(IdtEntryTable
[ExceptionType
].OrigDesc
));
142 // restore interrupt state
144 SetInterruptState (OldIntFlagState
);
150 Returns the maximum value that may be used for the ProcessorIndex parameter in
151 RegisterPeriodicCallback() and RegisterExceptionCallback().
153 Hard coded to support only 1 processor for now.
155 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
156 @param MaxProcessorIndex Pointer to a caller-allocated UINTN in which the maximum supported
157 processor index is returned. Always 0 returned.
159 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
164 GetMaximumProcessorIndex (
165 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
166 OUT UINTN
*MaxProcessorIndex
169 *MaxProcessorIndex
= 0;
174 Registers a function to be called back periodically in interrupt context.
176 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
177 @param ProcessorIndex Specifies which processor the callback function applies to.
178 @param PeriodicCallback A pointer to a function of type PERIODIC_CALLBACK that is the main
179 periodic entry point of the debug agent.
181 @retval EFI_SUCCESS The function completed successfully.
182 @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
183 function was previously registered.
184 @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
189 RegisterPeriodicCallback (
190 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
191 IN UINTN ProcessorIndex
,
192 IN EFI_PERIODIC_CALLBACK PeriodicCallback
195 return ManageIdtEntryTable (PeriodicCallback
, SYSTEM_TIMER_VECTOR
);
199 Registers a function to be called when a given processor exception occurs.
201 This code executes in boot services context.
203 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
204 @param ProcessorIndex Specifies which processor the callback function applies to.
205 @param ExceptionCallback A pointer to a function of type EXCEPTION_CALLBACK that is called
206 when the processor exception specified by ExceptionType occurs.
207 @param ExceptionType Specifies which processor exception to hook.
209 @retval EFI_SUCCESS The function completed successfully.
210 @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
211 function was previously registered.
212 @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
217 RegisterExceptionCallback (
218 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
219 IN UINTN ProcessorIndex
,
220 IN EFI_EXCEPTION_CALLBACK ExceptionCallback
,
221 IN EFI_EXCEPTION_TYPE ExceptionType
224 return ManageIdtEntryTable (ExceptionCallback
, ExceptionType
);
228 Invalidates processor instruction cache for a memory range. Subsequent execution in this range
229 causes a fresh memory fetch to retrieve code to be executed.
231 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
232 @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.
233 @param Start Specifies the physical base of the memory range to be invalidated.
234 @param Length Specifies the minimum number of bytes in the processor's instruction
237 @retval EFI_SUCCESS Always returned.
242 InvalidateInstructionCache (
243 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
244 IN UINTN ProcessorIndex
,
254 Common piece of code that invokes the registered handlers.
256 This code executes in exception context so no efi calls are allowed.
257 This code is called from assembly file.
259 @param ExceptionType Exception type
260 @param ContextRecord System context
264 InterruptDistrubutionHub (
265 EFI_EXCEPTION_TYPE ExceptionType
,
266 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
269 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
270 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
271 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
273 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
274 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);
280 This is the callback that is written to the Loaded Image protocol instance
281 on the image handle. It uninstalls all registered handlers and frees all entry
284 @param ImageHandle The firmware allocated handle for the EFI image.
286 @retval EFI_SUCCESS Always.
291 PlUnloadDebugSupportDriver (
292 IN EFI_HANDLE ImageHandle
295 EFI_EXCEPTION_TYPE ExceptionType
;
297 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
298 ManageIdtEntryTable (NULL
, ExceptionType
);
300 // Free space for each Interrupt Stub precedure.
302 if (IdtEntryTable
[ExceptionType
].StubEntry
!= NULL
) {
303 FreePool ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
);
307 FreePool (IdtEntryTable
);
313 Initializes driver's handler registration database.
315 This code executes in boot services context.
316 Must be public because it's referenced from DebugSupport.c
318 @retval EFI_UNSUPPORTED If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,
319 the context save will fail, so these processors are not supported.
320 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
321 @retval EFI_SUCCESS Initializes successfully.
325 PlInitializeDebugSupportDriver (
329 EFI_EXCEPTION_TYPE ExceptionType
;
332 // Check whether FxStor instructions are supported.
334 if (!FxStorSupport ()) {
335 return EFI_UNSUPPORTED
;
338 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
339 if (IdtEntryTable
== NULL
) {
340 return EFI_OUT_OF_RESOURCES
;
343 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
344 IdtEntryTable
[ExceptionType
].StubEntry
= (DEBUG_PROC
)(UINTN
)AllocatePool (StubSize
);
345 if (IdtEntryTable
[ExceptionType
].StubEntry
== NULL
) {
350 // Copy Interrupt stub code.
352 CopyMem ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
, InterruptEntryStub
, StubSize
);
359 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
360 if (IdtEntryTable
[ExceptionType
].StubEntry
!= NULL
) {
361 FreePool ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
);
365 FreePool (IdtEntryTable
);
367 return EFI_OUT_OF_RESOURCES
;