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
);
229 Invalidates processor instruction cache for a memory range. Subsequent execution in this range
230 causes a fresh memory fetch to retrieve code to be executed.
232 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
233 @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.
234 @param Start Specifies the physical base of the memory range to be invalidated.
235 @param Length Specifies the minimum number of bytes in the processor's instruction
238 @retval EFI_SUCCESS Always returned.
243 InvalidateInstructionCache (
244 IN EFI_DEBUG_SUPPORT_PROTOCOL
*This
,
245 IN UINTN ProcessorIndex
,
255 Common piece of code that invokes the registered handlers.
257 This code executes in exception context so no efi calls are allowed.
258 This code is called from assembly file.
260 @param ExceptionType Exception type
261 @param ContextRecord System context
265 InterruptDistrubutionHub (
266 EFI_EXCEPTION_TYPE ExceptionType
,
267 EFI_SYSTEM_CONTEXT_IA32
*ContextRecord
270 if (IdtEntryTable
[ExceptionType
].RegisteredCallback
!= NULL
) {
271 if (ExceptionType
!= SYSTEM_TIMER_VECTOR
) {
272 IdtEntryTable
[ExceptionType
].RegisteredCallback (ExceptionType
, ContextRecord
);
274 OrigVector
= IdtEntryTable
[ExceptionType
].OrigVector
;
275 IdtEntryTable
[ExceptionType
].RegisteredCallback (ContextRecord
);
281 This is the callback that is written to the Loaded Image protocol instance
282 on the image handle. It uninstalls all registered handlers and frees all entry
285 @param ImageHandle The firmware allocated handle for the EFI image.
287 @retval EFI_SUCCESS Always.
292 PlUnloadDebugSupportDriver (
293 IN EFI_HANDLE ImageHandle
296 EFI_EXCEPTION_TYPE ExceptionType
;
298 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
299 ManageIdtEntryTable (NULL
, ExceptionType
);
301 // Free space for each Interrupt Stub precedure.
303 if (IdtEntryTable
[ExceptionType
].StubEntry
!= NULL
) {
304 FreePool ((VOID
*)(UINTN
)IdtEntryTable
[ExceptionType
].StubEntry
);
308 FreePool (IdtEntryTable
);
314 Initializes driver's handler registration database.
316 This code executes in boot services context.
317 Must be public because it's referenced from DebugSupport.c
319 @retval EFI_UNSUPPORTED If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,
320 the context save will fail, so these processors are not supported.
321 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
322 @retval EFI_SUCCESS Initializes successfully.
326 PlInitializeDebugSupportDriver (
330 EFI_EXCEPTION_TYPE ExceptionType
;
333 // Check whether FxStor instructions are supported.
335 if (!FxStorSupport ()) {
336 return EFI_UNSUPPORTED
;
339 IdtEntryTable
= AllocateZeroPool (sizeof (IDT_ENTRY
) * NUM_IDT_ENTRIES
);
340 if (IdtEntryTable
== NULL
) {
341 return EFI_OUT_OF_RESOURCES
;
344 for (ExceptionType
= 0; ExceptionType
< NUM_IDT_ENTRIES
; ExceptionType
++) {
345 IdtEntryTable
[ExceptionType
].StubEntry
= (DEBUG_PROC
) (UINTN
) AllocatePool (StubSize
);
346 if (IdtEntryTable
[ExceptionType
].StubEntry
== NULL
) {
351 // Copy Interrupt stub code.
353 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
);
364 FreePool (IdtEntryTable
);
366 return EFI_OUT_OF_RESOURCES
;