2 Debug Agent library implementation for Dxe Core and Dxr modules.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DxeDebugAgentLib.h"
11 DEBUG_AGENT_MAILBOX mMailbox
;
12 DEBUG_AGENT_MAILBOX
*mMailboxPointer
= NULL
;
13 IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable
[33];
14 BOOLEAN mDxeCoreFlag
= FALSE
;
15 BOOLEAN mMultiProcessorDebugSupport
= FALSE
;
16 VOID
*mSavedIdtTable
= NULL
;
17 UINTN mSaveIdtTableSize
= 0;
18 BOOLEAN mDebugAgentInitialized
= FALSE
;
19 BOOLEAN mSkipBreakpoint
= FALSE
;
22 Check if debug agent support multi-processor.
24 @retval TRUE Multi-processor is supported.
25 @retval FALSE Multi-processor is not supported.
29 MultiProcessorDebugSupport (
33 return mMultiProcessorDebugSupport
;
37 Internal constructor worker function.
39 It will register one callback function on EFI PCD Protocol.
40 It will allocate the NVS memory to store Mailbox and install configuration table
41 in system table to store its pointer.
45 InternalConstructorWorker (
50 EFI_PHYSICAL_ADDRESS Address
;
51 BOOLEAN DebugTimerInterruptState
;
52 DEBUG_AGENT_MAILBOX
*Mailbox
;
53 DEBUG_AGENT_MAILBOX
*NewMailbox
;
54 EFI_HOB_GUID_TYPE
*GuidHob
;
55 EFI_VECTOR_HANDOFF_INFO
*VectorHandoffInfo
;
58 // Check persisted vector handoff info
61 GuidHob
= GetFirstGuidHob (&gEfiVectorHandoffInfoPpiGuid
);
62 if ((GuidHob
!= NULL
) && !mDxeCoreFlag
) {
64 // Check if configuration table is installed or not if GUIDed HOB existed,
65 // only when Debug Agent is not linked by DXE Core
67 Status
= EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
**)&VectorHandoffInfo
);
70 if ((GuidHob
== NULL
) || (Status
!= EFI_SUCCESS
)) {
72 // Install configuration table for persisted vector handoff info if GUIDed HOB cannot be found or
73 // configuration table does not exist
75 Status
= gBS
->InstallConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
*)&mVectorHandoffInfoDebugAgent
[0]);
76 if (EFI_ERROR (Status
)) {
77 DEBUG ((DEBUG_ERROR
, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
83 // Install EFI Serial IO protocol on debug port
88 Status
= gBS
->AllocatePages (
91 EFI_SIZE_TO_PAGES (sizeof (DEBUG_AGENT_MAILBOX
) + PcdGet16 (PcdDebugPortHandleBufferSize
)),
94 if (EFI_ERROR (Status
)) {
95 DEBUG ((DEBUG_ERROR
, "DebugAgent: Cannot install configuration table for mailbox!\n"));
99 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (FALSE
);
101 NewMailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)Address
;
103 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox
104 // and Debug Port Handle buffer may be free at runtime, SMM debug agent needs to access them
106 Mailbox
= GetMailboxPointer ();
107 CopyMem (NewMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
108 CopyMem (NewMailbox
+ 1, (VOID
*)(UINTN
)Mailbox
->DebugPortHandle
, PcdGet16 (PcdDebugPortHandleBufferSize
));
110 // Update Debug Port Handle in new Mailbox
112 UpdateMailboxContent (NewMailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, (UINT64
)(UINTN
)(NewMailbox
+ 1));
113 mMailboxPointer
= NewMailbox
;
115 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (DebugTimerInterruptState
);
117 Status
= gBS
->InstallConfigurationTable (&gEfiDebugAgentGuid
, (VOID
*)mMailboxPointer
);
118 if (EFI_ERROR (Status
)) {
119 DEBUG ((DEBUG_ERROR
, "DebugAgent: Failed to install configuration for mailbox!\n"));
125 Debug Agent constructor function.
127 @param[in] ImageHandle The firmware allocated handle for the EFI image.
128 @param[in] SystemTable A pointer to the EFI System Table.
130 @retval RETURN_SUCCESS When this function completed.
135 DxeDebugAgentLibConstructor (
136 IN EFI_HANDLE ImageHandle
,
137 IN EFI_SYSTEM_TABLE
*SystemTable
142 // Invoke internal constructor function only when DXE core links this library instance
144 InternalConstructorWorker ();
147 return RETURN_SUCCESS
;
151 Get the pointer to Mailbox from the configuration table.
153 @return Pointer to Mailbox.
156 DEBUG_AGENT_MAILBOX
*
157 GetMailboxFromConfigurationTable (
162 DEBUG_AGENT_MAILBOX
*Mailbox
;
164 Status
= EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid
, (VOID
**)&Mailbox
);
165 if ((Status
== EFI_SUCCESS
) && (Mailbox
!= NULL
)) {
166 VerifyMailboxChecksum (Mailbox
);
174 Get the pointer to Mailbox from the GUIDed HOB.
176 @param[in] HobStart The starting HOB pointer to search from.
178 @return Pointer to Mailbox.
181 DEBUG_AGENT_MAILBOX
*
186 EFI_HOB_GUID_TYPE
*GuidHob
;
187 UINT64
*MailboxLocation
;
188 DEBUG_AGENT_MAILBOX
*Mailbox
;
190 GuidHob
= GetNextGuidHob (&gEfiDebugAgentGuid
, HobStart
);
191 if (GuidHob
== NULL
) {
195 MailboxLocation
= (UINT64
*)(GET_GUID_HOB_DATA (GuidHob
));
196 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
197 VerifyMailboxChecksum (Mailbox
);
203 Get Debug Agent Mailbox pointer.
205 @return Mailbox pointer.
208 DEBUG_AGENT_MAILBOX
*
213 AcquireMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
214 VerifyMailboxChecksum (mMailboxPointer
);
215 ReleaseMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
216 return mMailboxPointer
;
220 Get debug port handle.
222 @return Debug port handle.
230 return (DEBUG_PORT_HANDLE
)(UINTN
)(GetMailboxPointer ()->DebugPortHandle
);
234 Worker function to set up Debug Agent environment.
236 This function will set up IDT table and initialize the IDT entries and
237 initialize CPU LOCAL APIC timer.
238 It also tries to connect HOST if Debug Agent was not initialized before.
240 @param[in] Mailbox Pointer to Mailbox.
244 SetupDebugAgentEnvironment (
245 IN DEBUG_AGENT_MAILBOX
*Mailbox
248 IA32_DESCRIPTOR Idtr
;
249 UINT16 IdtEntryCount
;
250 UINT64 DebugPortHandle
;
251 UINT32 DebugTimerFrequency
;
253 if (mMultiProcessorDebugSupport
) {
254 InitializeSpinLock (&mDebugMpContext
.MpContextSpinLock
);
255 InitializeSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
256 InitializeSpinLock (&mDebugMpContext
.MailboxSpinLock
);
258 // Clear Break CPU index value
260 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
)-1;
264 // Get original IDT address and size.
266 AsmReadIdtr ((IA32_DESCRIPTOR
*)&Idtr
);
267 IdtEntryCount
= (UINT16
)((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
268 if (IdtEntryCount
< 33) {
269 ZeroMem (&mIdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33);
271 // Copy original IDT table into new one
273 CopyMem (&mIdtEntryTable
, (VOID
*)Idtr
.Base
, Idtr
.Limit
+ 1);
275 // Load new IDT table
277 Idtr
.Limit
= (UINT16
)(sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
278 Idtr
.Base
= (UINTN
)&mIdtEntryTable
;
279 AsmWriteIdtr ((IA32_DESCRIPTOR
*)&Idtr
);
283 // Initialize the IDT table entries to support source level debug.
285 InitializeDebugIdt ();
288 // If mMailboxPointer is not set before, set it
290 if (mMailboxPointer
== NULL
) {
291 if (Mailbox
!= NULL
) {
293 // If Mailbox exists, copy it into one global variable
295 CopyMem (&mMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
297 ZeroMem (&mMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
300 mMailboxPointer
= &mMailbox
;
304 // Initialize Debug Timer hardware and save its initial count and frequency
306 mDebugMpContext
.DebugTimerInitCount
= InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
307 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
309 // Initialize debug communication port
311 DebugPortHandle
= (UINT64
)(UINTN
)DebugPortInitialize ((VOID
*)(UINTN
)mMailboxPointer
->DebugPortHandle
, NULL
);
312 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
314 if (Mailbox
== NULL
) {
316 // Trigger one software interrupt to inform HOST
318 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
319 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
321 // Memory has been ready
323 if (IsHostAttached ()) {
325 // Trigger one software interrupt to inform HOST
327 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
333 Initialize debug agent.
335 This function is used to set up debug environment for DXE phase.
337 If this function is called by DXE Core, Context must be the pointer
338 to HOB list which will be used to get GUIDed HOB. It will enable
339 interrupt to support break-in feature.
340 If this function is called by DXE module, Context must be NULL. It
341 will enable interrupt to support break-in feature.
343 @param[in] InitFlag Init flag is used to decide initialize process.
344 @param[in] Context Context needed according to InitFlag.
345 @param[in] Function Continue function called by debug agent library; it was
351 InitializeDebugAgent (
353 IN VOID
*Context OPTIONAL
,
354 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
357 UINT64
*MailboxLocation
;
358 DEBUG_AGENT_MAILBOX
*Mailbox
;
359 BOOLEAN InterruptStatus
;
361 IA32_DESCRIPTOR IdtDescriptor
;
362 IA32_DESCRIPTOR
*Ia32Idtr
;
363 IA32_IDT_ENTRY
*Ia32IdtEntry
;
364 BOOLEAN PeriodicMode
;
367 if (InitFlag
== DEBUG_AGENT_INIT_DXE_AP
) {
369 // Check if CPU APIC Timer is working, otherwise initialize it.
371 InitializeLocalApicSoftwareEnable (TRUE
);
372 GetApicTimerState (NULL
, &PeriodicMode
, NULL
);
373 TimerCycle
= GetApicTimerInitCount ();
374 if (!PeriodicMode
|| (TimerCycle
== 0)) {
375 InitializeDebugTimer (NULL
, FALSE
);
379 // Invoked by AP, enable interrupt to let AP could receive IPI from other processors
386 // Disable Debug Timer interrupt
388 SaveAndSetDebugTimerInterrupt (FALSE
);
390 // Save and disable original interrupt status
392 InterruptStatus
= SaveAndDisableInterrupts ();
395 // Try to get mailbox firstly
399 MailboxLocation
= NULL
;
402 case DEBUG_AGENT_INIT_DXE_LOAD
:
404 // Check if Debug Agent has been initialized before
406 if (IsDebugAgentInitialzed ()) {
407 DEBUG ((DEBUG_INFO
, "Debug Agent: The former agent will be overwritten by the new one!\n"));
410 mMultiProcessorDebugSupport
= TRUE
;
412 // Save original IDT table
414 AsmReadIdtr (&IdtDescriptor
);
415 mSaveIdtTableSize
= IdtDescriptor
.Limit
+ 1;
416 mSavedIdtTable
= AllocateCopyPool (mSaveIdtTableSize
, (VOID
*)IdtDescriptor
.Base
);
418 // Check if Debug Agent initialized in DXE phase
420 Mailbox
= GetMailboxFromConfigurationTable ();
421 if (Mailbox
== NULL
) {
423 // Try to get mailbox from GUIDed HOB build in PEI
425 HobList
= GetHobList ();
426 Mailbox
= GetMailboxFromHob (HobList
);
430 // Set up Debug Agent Environment and try to connect HOST if required
432 SetupDebugAgentEnvironment (Mailbox
);
434 // For DEBUG_AGENT_INIT_S3, needn't to install configuration table and EFI Serial IO protocol
435 // For DEBUG_AGENT_INIT_DXE_CORE, InternalConstructorWorker() will invoked in Constructor()
437 InternalConstructorWorker ();
439 // Enable Debug Timer interrupt
441 SaveAndSetDebugTimerInterrupt (TRUE
);
443 // Enable interrupt to receive Debug Timer interrupt
447 mDebugAgentInitialized
= TRUE
;
448 FindAndReportModuleImageInfo (SIZE_4KB
);
450 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
454 case DEBUG_AGENT_INIT_DXE_UNLOAD
:
455 if (mDebugAgentInitialized
) {
456 if (IsHostAttached ()) {
457 *(EFI_STATUS
*)Context
= EFI_ACCESS_DENIED
;
459 // Enable Debug Timer interrupt again
461 SaveAndSetDebugTimerInterrupt (TRUE
);
464 // Restore original IDT table
466 AsmReadIdtr (&IdtDescriptor
);
467 IdtDescriptor
.Limit
= (UINT16
)(mSaveIdtTableSize
- 1);
468 CopyMem ((VOID
*)IdtDescriptor
.Base
, mSavedIdtTable
, mSaveIdtTableSize
);
469 AsmWriteIdtr (&IdtDescriptor
);
470 FreePool (mSavedIdtTable
);
471 mDebugAgentInitialized
= FALSE
;
472 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
475 *(EFI_STATUS
*)Context
= EFI_NOT_STARTED
;
479 // Restore interrupt state.
481 SetInterruptState (InterruptStatus
);
484 case DEBUG_AGENT_INIT_DXE_CORE
:
486 mMultiProcessorDebugSupport
= TRUE
;
488 // Try to get mailbox from GUIDed HOB build in PEI
491 Mailbox
= GetMailboxFromHob (HobList
);
493 // Set up Debug Agent Environment and try to connect HOST if required
495 SetupDebugAgentEnvironment (Mailbox
);
497 // Enable Debug Timer interrupt
499 SaveAndSetDebugTimerInterrupt (TRUE
);
501 // Enable interrupt to receive Debug Timer interrupt
507 case DEBUG_AGENT_INIT_S3
:
509 if (Context
!= NULL
) {
510 Ia32Idtr
= (IA32_DESCRIPTOR
*)Context
;
511 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
512 MailboxLocation
= (UINT64
*)((UINTN
)Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
513 ((UINTN
)Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
514 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
515 VerifyMailboxChecksum (Mailbox
);
519 // Save Mailbox pointer in global variable
521 mMailboxPointer
= Mailbox
;
523 // Set up Debug Agent Environment and try to connect HOST if required
525 SetupDebugAgentEnvironment (Mailbox
);
529 DisableInterrupts ();
530 FindAndReportModuleImageInfo (SIZE_4KB
);
531 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT
) == 1) {
533 // If Boot Script entry break is set, code will be break at here.
542 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
543 // Debug Agent library instance.
545 DEBUG ((DEBUG_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));