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
);
69 if (GuidHob
== NULL
|| Status
!= EFI_SUCCESS
) {
71 // Install configuration table for persisted vector handoff info if GUIDed HOB cannot be found or
72 // configuration table does not exist
74 Status
= gBS
->InstallConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
*) &mVectorHandoffInfoDebugAgent
[0]);
75 if (EFI_ERROR (Status
)) {
76 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
82 // Install EFI Serial IO protocol on debug port
87 Status
= gBS
->AllocatePages (
90 EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX
) + PcdGet16(PcdDebugPortHandleBufferSize
)),
93 if (EFI_ERROR (Status
)) {
94 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for mailbox!\n"));
98 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (FALSE
);
100 NewMailbox
= (DEBUG_AGENT_MAILBOX
*) (UINTN
) Address
;
102 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox
103 // and Debug Port Handle buffer may be free at runtime, SMM debug agent needs to access them
105 Mailbox
= GetMailboxPointer ();
106 CopyMem (NewMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
107 CopyMem (NewMailbox
+ 1, (VOID
*)(UINTN
)Mailbox
->DebugPortHandle
, PcdGet16(PcdDebugPortHandleBufferSize
));
109 // Update Debug Port Handle in new Mailbox
111 UpdateMailboxContent (NewMailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, (UINT64
)(UINTN
)(NewMailbox
+ 1));
112 mMailboxPointer
= NewMailbox
;
114 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (DebugTimerInterruptState
);
116 Status
= gBS
->InstallConfigurationTable (&gEfiDebugAgentGuid
, (VOID
*) mMailboxPointer
);
117 if (EFI_ERROR (Status
)) {
118 DEBUG ((EFI_D_ERROR
, "DebugAgent: Failed to install configuration for mailbox!\n"));
124 Debug Agent constructor function.
126 @param[in] ImageHandle The firmware allocated handle for the EFI image.
127 @param[in] SystemTable A pointer to the EFI System Table.
129 @retval RETURN_SUCCESS When this function completed.
134 DxeDebugAgentLibConstructor (
135 IN EFI_HANDLE ImageHandle
,
136 IN EFI_SYSTEM_TABLE
*SystemTable
141 // Invoke internal constructor function only when DXE core links this library instance
143 InternalConstructorWorker ();
146 return RETURN_SUCCESS
;
150 Get the pointer to Mailbox from the configuration table.
152 @return Pointer to Mailbox.
155 DEBUG_AGENT_MAILBOX
*
156 GetMailboxFromConfigurationTable (
161 DEBUG_AGENT_MAILBOX
*Mailbox
;
163 Status
= EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid
, (VOID
**) &Mailbox
);
164 if (Status
== EFI_SUCCESS
&& Mailbox
!= NULL
) {
165 VerifyMailboxChecksum (Mailbox
);
173 Get the pointer to Mailbox from the GUIDed HOB.
175 @param[in] HobStart The starting HOB pointer to search from.
177 @return Pointer to Mailbox.
180 DEBUG_AGENT_MAILBOX
*
185 EFI_HOB_GUID_TYPE
*GuidHob
;
186 UINT64
*MailboxLocation
;
187 DEBUG_AGENT_MAILBOX
*Mailbox
;
189 GuidHob
= GetNextGuidHob (&gEfiDebugAgentGuid
, HobStart
);
190 if (GuidHob
== NULL
) {
193 MailboxLocation
= (UINT64
*) (GET_GUID_HOB_DATA(GuidHob
));
194 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
195 VerifyMailboxChecksum (Mailbox
);
201 Get Debug Agent Mailbox pointer.
203 @return Mailbox pointer.
206 DEBUG_AGENT_MAILBOX
*
211 AcquireMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
212 VerifyMailboxChecksum (mMailboxPointer
);
213 ReleaseMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
214 return mMailboxPointer
;
218 Get debug port handle.
220 @return Debug port handle.
228 return (DEBUG_PORT_HANDLE
) (UINTN
)(GetMailboxPointer ()->DebugPortHandle
);
232 Worker function to set up Debug Agent environment.
234 This function will set up IDT table and initialize the IDT entries and
235 initialize CPU LOCAL APIC timer.
236 It also tries to connect HOST if Debug Agent was not initialized before.
238 @param[in] Mailbox Pointer to Mailbox.
242 SetupDebugAgentEnvironment (
243 IN DEBUG_AGENT_MAILBOX
*Mailbox
246 IA32_DESCRIPTOR Idtr
;
247 UINT16 IdtEntryCount
;
248 UINT64 DebugPortHandle
;
249 UINT32 DebugTimerFrequency
;
251 if (mMultiProcessorDebugSupport
) {
252 InitializeSpinLock (&mDebugMpContext
.MpContextSpinLock
);
253 InitializeSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
254 InitializeSpinLock (&mDebugMpContext
.MailboxSpinLock
);
256 // Clear Break CPU index value
258 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) -1;
262 // Get original IDT address and size.
264 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
265 IdtEntryCount
= (UINT16
) ((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
266 if (IdtEntryCount
< 33) {
267 ZeroMem (&mIdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33);
269 // Copy original IDT table into new one
271 CopyMem (&mIdtEntryTable
, (VOID
*) Idtr
.Base
, Idtr
.Limit
+ 1);
273 // Load new IDT table
275 Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
276 Idtr
.Base
= (UINTN
) &mIdtEntryTable
;
277 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
281 // Initialize the IDT table entries to support source level debug.
283 InitializeDebugIdt ();
286 // If mMailboxPointer is not set before, set it
288 if (mMailboxPointer
== NULL
) {
289 if (Mailbox
!= NULL
) {
291 // If Mailbox exists, copy it into one global variable
293 CopyMem (&mMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
295 ZeroMem (&mMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
297 mMailboxPointer
= &mMailbox
;
301 // Initialize Debug Timer hardware and save its initial count and frequency
303 mDebugMpContext
.DebugTimerInitCount
= InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
304 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
306 // Initialize debug communication port
308 DebugPortHandle
= (UINT64
) (UINTN
)DebugPortInitialize ((VOID
*)(UINTN
)mMailboxPointer
->DebugPortHandle
, NULL
);
309 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
311 if (Mailbox
== NULL
) {
313 // Trigger one software interrupt to inform HOST
315 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
316 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
318 // Memory has been ready
320 if (IsHostAttached ()) {
322 // Trigger one software interrupt to inform HOST
324 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
331 Initialize debug agent.
333 This function is used to set up debug environment for DXE phase.
335 If this function is called by DXE Core, Context must be the pointer
336 to HOB list which will be used to get GUIDed HOB. It will enable
337 interrupt to support break-in feature.
338 If this function is called by DXE module, Context must be NULL. It
339 will enable interrupt to support break-in feature.
341 @param[in] InitFlag Init flag is used to decide initialize process.
342 @param[in] Context Context needed according to InitFlag.
343 @param[in] Function Continue function called by debug agent library; it was
349 InitializeDebugAgent (
351 IN VOID
*Context
, OPTIONAL
352 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
355 UINT64
*MailboxLocation
;
356 DEBUG_AGENT_MAILBOX
*Mailbox
;
357 BOOLEAN InterruptStatus
;
359 IA32_DESCRIPTOR IdtDescriptor
;
360 IA32_DESCRIPTOR
*Ia32Idtr
;
361 IA32_IDT_ENTRY
*Ia32IdtEntry
;
362 BOOLEAN PeriodicMode
;
365 if (InitFlag
== DEBUG_AGENT_INIT_DXE_AP
) {
367 // Check if CPU APIC Timer is working, otherwise initialize it.
369 InitializeLocalApicSoftwareEnable (TRUE
);
370 GetApicTimerState (NULL
, &PeriodicMode
, NULL
);
371 TimerCycle
= GetApicTimerInitCount ();
372 if (!PeriodicMode
|| TimerCycle
== 0) {
373 InitializeDebugTimer (NULL
, FALSE
);
376 // Invoked by AP, enable interrupt to let AP could receive IPI from other processors
383 // Disable Debug Timer interrupt
385 SaveAndSetDebugTimerInterrupt (FALSE
);
387 // Save and disable original interrupt status
389 InterruptStatus
= SaveAndDisableInterrupts ();
392 // Try to get mailbox firstly
396 MailboxLocation
= NULL
;
400 case DEBUG_AGENT_INIT_DXE_LOAD
:
402 // Check if Debug Agent has been initialized before
404 if (IsDebugAgentInitialzed ()) {
405 DEBUG ((EFI_D_INFO
, "Debug Agent: The former agent will be overwritten by the new one!\n"));
408 mMultiProcessorDebugSupport
= TRUE
;
410 // Save original IDT table
412 AsmReadIdtr (&IdtDescriptor
);
413 mSaveIdtTableSize
= IdtDescriptor
.Limit
+ 1;
414 mSavedIdtTable
= AllocateCopyPool (mSaveIdtTableSize
, (VOID
*) IdtDescriptor
.Base
);
416 // Check if Debug Agent initialized in DXE phase
418 Mailbox
= GetMailboxFromConfigurationTable ();
419 if (Mailbox
== NULL
) {
421 // Try to get mailbox from GUIDed HOB build in PEI
423 HobList
= GetHobList ();
424 Mailbox
= GetMailboxFromHob (HobList
);
427 // Set up Debug Agent Environment and try to connect HOST if required
429 SetupDebugAgentEnvironment (Mailbox
);
431 // For DEBUG_AGENT_INIT_S3, needn't to install configuration table and EFI Serial IO protocol
432 // For DEBUG_AGENT_INIT_DXE_CORE, InternalConstructorWorker() will invoked in Constructor()
434 InternalConstructorWorker ();
436 // Enable Debug Timer interrupt
438 SaveAndSetDebugTimerInterrupt (TRUE
);
440 // Enable interrupt to receive Debug Timer interrupt
444 mDebugAgentInitialized
= TRUE
;
445 FindAndReportModuleImageInfo (SIZE_4KB
);
447 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
451 case DEBUG_AGENT_INIT_DXE_UNLOAD
:
452 if (mDebugAgentInitialized
) {
453 if (IsHostAttached ()) {
454 *(EFI_STATUS
*)Context
= EFI_ACCESS_DENIED
;
456 // Enable Debug Timer interrupt again
458 SaveAndSetDebugTimerInterrupt (TRUE
);
461 // Restore original IDT table
463 AsmReadIdtr (&IdtDescriptor
);
464 IdtDescriptor
.Limit
= (UINT16
) (mSaveIdtTableSize
- 1);
465 CopyMem ((VOID
*) IdtDescriptor
.Base
, mSavedIdtTable
, mSaveIdtTableSize
);
466 AsmWriteIdtr (&IdtDescriptor
);
467 FreePool (mSavedIdtTable
);
468 mDebugAgentInitialized
= FALSE
;
469 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
472 *(EFI_STATUS
*)Context
= EFI_NOT_STARTED
;
476 // Restore interrupt state.
478 SetInterruptState (InterruptStatus
);
481 case DEBUG_AGENT_INIT_DXE_CORE
:
483 mMultiProcessorDebugSupport
= TRUE
;
485 // Try to get mailbox from GUIDed HOB build in PEI
488 Mailbox
= GetMailboxFromHob (HobList
);
490 // Set up Debug Agent Environment and try to connect HOST if required
492 SetupDebugAgentEnvironment (Mailbox
);
494 // Enable Debug Timer interrupt
496 SaveAndSetDebugTimerInterrupt (TRUE
);
498 // Enable interrupt to receive Debug Timer interrupt
504 case DEBUG_AGENT_INIT_S3
:
506 if (Context
!= NULL
) {
507 Ia32Idtr
= (IA32_DESCRIPTOR
*) Context
;
508 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
509 MailboxLocation
= (UINT64
*) ((UINTN
) Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
510 ((UINTN
) Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
511 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
512 VerifyMailboxChecksum (Mailbox
);
515 // Save Mailbox pointer in global variable
517 mMailboxPointer
= Mailbox
;
519 // Set up Debug Agent Environment and try to connect HOST if required
521 SetupDebugAgentEnvironment (Mailbox
);
525 DisableInterrupts ();
526 FindAndReportModuleImageInfo (SIZE_4KB
);
527 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT
) == 1) {
529 // If Boot Script entry break is set, code will be break at here.
537 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
538 // Debug Agent library instance.
540 DEBUG ((EFI_D_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));