2 Debug Agent library implementition for Dxe Core and Dxr modules.
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 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.
15 #include "DxeDebugAgentLib.h"
17 DEBUG_AGENT_MAILBOX mMailbox
;
18 DEBUG_AGENT_MAILBOX
*mMailboxPointer
= NULL
;
19 IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable
[33];
20 BOOLEAN mDxeCoreFlag
= FALSE
;
21 BOOLEAN mMultiProcessorDebugSupport
= FALSE
;
22 VOID
*mSavedIdtTable
= NULL
;
23 UINTN mSaveIdtTableSize
= 0;
24 BOOLEAN mDebugAgentInitialized
= FALSE
;
25 BOOLEAN mSkipBreakpoint
= FALSE
;
28 Check if debug agent support multi-processor.
30 @retval TRUE Multi-processor is supported.
31 @retval FALSE Multi-processor is not supported.
35 MultiProcessorDebugSupport (
39 return mMultiProcessorDebugSupport
;
43 Internal constructor worker function.
45 It will register one callback function on EFI PCD Protocol.
46 It will allocate the NVS memory to store Mailbox and install configuration table
47 in system table to store its pointer.
51 InternalConstructorWorker (
56 EFI_PHYSICAL_ADDRESS Address
;
57 BOOLEAN DebugTimerInterruptState
;
58 DEBUG_AGENT_MAILBOX
*Mailbox
;
59 DEBUG_AGENT_MAILBOX
*NewMailbox
;
60 EFI_HOB_GUID_TYPE
*GuidHob
;
61 EFI_VECTOR_HANDOFF_INFO
*VectorHandoffInfo
;
64 // Check persisted vector handoff info
67 GuidHob
= GetFirstGuidHob (&gEfiVectorHandoffInfoPpiGuid
);
68 if (GuidHob
!= NULL
&& !mDxeCoreFlag
) {
70 // Check if configuration table is installed or not if GUIDed HOB existed,
71 // only when Debug Agent is not linked by DXE Core
73 Status
= EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
**) &VectorHandoffInfo
);
75 if (GuidHob
== NULL
|| Status
!= EFI_SUCCESS
) {
77 // Install configuration table for persisted vector handoff info if GUIDed HOB cannot be found or
78 // configuration table does not exist
80 Status
= gBS
->InstallConfigurationTable (&gEfiVectorHandoffTableGuid
, (VOID
*) &mVectorHandoffInfoDebugAgent
[0]);
81 if (EFI_ERROR (Status
)) {
82 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
88 // Install EFI Serial IO protocol on debug port
93 Status
= gBS
->AllocatePages (
96 EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX
) + PcdGet16(PcdDebugPortHandleBufferSize
)),
99 if (EFI_ERROR (Status
)) {
100 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for mailbox!\n"));
104 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (FALSE
);
106 NewMailbox
= (DEBUG_AGENT_MAILBOX
*) (UINTN
) Address
;
108 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox
109 // and Debug Port Handle buffer may be free at runtime, SMM debug agent needs to access them
111 Mailbox
= GetMailboxPointer ();
112 CopyMem (NewMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
113 CopyMem (NewMailbox
+ 1, (VOID
*)(UINTN
)Mailbox
->DebugPortHandle
, PcdGet16(PcdDebugPortHandleBufferSize
));
115 // Update Debug Port Handle in new Mailbox
117 UpdateMailboxContent (NewMailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, (UINT64
)(UINTN
)(NewMailbox
+ 1));
118 mMailboxPointer
= NewMailbox
;
120 DebugTimerInterruptState
= SaveAndSetDebugTimerInterrupt (DebugTimerInterruptState
);
122 Status
= gBS
->InstallConfigurationTable (&gEfiDebugAgentGuid
, (VOID
*) mMailboxPointer
);
123 if (EFI_ERROR (Status
)) {
124 DEBUG ((EFI_D_ERROR
, "DebugAgent: Failed to install configuration for mailbox!\n"));
130 Debug Agent constructor function.
132 @param[in] ImageHandle The firmware allocated handle for the EFI image.
133 @param[in] SystemTable A pointer to the EFI System Table.
135 @retval RETURN_SUCCESS When this function completed.
140 DxeDebugAgentLibConstructor (
141 IN EFI_HANDLE ImageHandle
,
142 IN EFI_SYSTEM_TABLE
*SystemTable
147 // Invoke internal constructor function only when DXE core links this library instance
149 InternalConstructorWorker ();
152 return RETURN_SUCCESS
;
156 Get the pointer to Mailbox from the configuration table.
158 @return Pointer to Mailbox.
161 DEBUG_AGENT_MAILBOX
*
162 GetMailboxFromConfigurationTable (
167 DEBUG_AGENT_MAILBOX
*Mailbox
;
169 Status
= EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid
, (VOID
**) &Mailbox
);
170 if (Status
== EFI_SUCCESS
&& Mailbox
!= NULL
) {
171 VerifyMailboxChecksum (Mailbox
);
179 Get the pointer to Mailbox from the GUIDed HOB.
181 @param[in] HobStart The starting HOB pointer to search from.
183 @return Pointer to Mailbox.
186 DEBUG_AGENT_MAILBOX
*
191 EFI_HOB_GUID_TYPE
*GuidHob
;
192 UINT64
*MailboxLocation
;
193 DEBUG_AGENT_MAILBOX
*Mailbox
;
195 GuidHob
= GetNextGuidHob (&gEfiDebugAgentGuid
, HobStart
);
196 if (GuidHob
== NULL
) {
199 MailboxLocation
= (UINT64
*) (GET_GUID_HOB_DATA(GuidHob
));
200 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
201 VerifyMailboxChecksum (Mailbox
);
207 Get Debug Agent Mailbox pointer.
209 @return Mailbox pointer.
212 DEBUG_AGENT_MAILBOX
*
217 AcquireMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
218 VerifyMailboxChecksum (mMailboxPointer
);
219 ReleaseMpSpinLock (&mDebugMpContext
.MailboxSpinLock
);
220 return mMailboxPointer
;
224 Get debug port handle.
226 @return Debug port handle.
234 return (DEBUG_PORT_HANDLE
) (UINTN
)(GetMailboxPointer ()->DebugPortHandle
);
238 Worker function to setup IDT table and initialize the IDT entries.
240 @param[in] Mailbox Pointer to Mailbox.
244 SetupDebugAgentEnviroment (
245 IN DEBUG_AGENT_MAILBOX
*Mailbox
248 IA32_DESCRIPTOR Idtr
;
249 UINT16 IdtEntryCount
;
250 UINT64 DebugPortHandle
;
252 if (mMultiProcessorDebugSupport
) {
253 InitializeSpinLock (&mDebugMpContext
.MpContextSpinLock
);
254 InitializeSpinLock (&mDebugMpContext
.DebugPortSpinLock
);
255 InitializeSpinLock (&mDebugMpContext
.MailboxSpinLock
);
257 // Clear Break CPU index value
259 mDebugMpContext
.BreakAtCpuIndex
= (UINT32
) -1;
263 // Get original IDT address and size.
265 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
266 IdtEntryCount
= (UINT16
) ((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
267 if (IdtEntryCount
< 33) {
268 ZeroMem (&mIdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33);
270 // Copy original IDT table into new one
272 CopyMem (&mIdtEntryTable
, (VOID
*) Idtr
.Base
, Idtr
.Limit
+ 1);
274 // Load new IDT table
276 Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
277 Idtr
.Base
= (UINTN
) &mIdtEntryTable
;
278 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
282 // Initialize the IDT table entries to support source level debug.
284 InitializeDebugIdt ();
287 // If mMailboxPointer is not set before, set it
289 if (mMailboxPointer
== NULL
) {
290 if (Mailbox
!= NULL
) {
292 // If Mailbox exists, copy it into one global variable
294 CopyMem (&mMailbox
, Mailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
296 ZeroMem (&mMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
298 mMailboxPointer
= &mMailbox
;
302 // Initialize debug communication port
304 DebugPortHandle
= (UINT64
) (UINTN
)DebugPortInitialize ((VOID
*)(UINTN
)mMailboxPointer
->DebugPortHandle
, NULL
);
305 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
307 if (Mailbox
== NULL
) {
309 // Trigger one software interrupt to inform HOST
311 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
312 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
314 // Memory has been ready
316 if (IsHostAttached ()) {
318 // Trigger one software interrupt to inform HOST
320 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
327 Initialize debug agent.
329 This function is used to set up debug enviroment for DXE phase.
331 If this function is called by DXE Core, Context must be the pointer
332 to HOB list which will be used to get GUIDed HOB. It will enable
333 interrupt to support break-in feature.
334 If this function is called by DXE module, Context must be NULL. It
335 will enable interrupt to support break-in feature.
337 @param[in] InitFlag Init flag is used to decide initialize process.
338 @param[in] Context Context needed according to InitFlag.
339 @param[in] Function Continue function called by debug agent library; it was
345 InitializeDebugAgent (
347 IN VOID
*Context
, OPTIONAL
348 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
351 UINT64
*MailboxLocation
;
352 DEBUG_AGENT_MAILBOX
*Mailbox
;
353 BOOLEAN InterruptStatus
;
355 IA32_DESCRIPTOR IdtDescriptor
;
356 IA32_DESCRIPTOR
*Ia32Idtr
;
357 IA32_IDT_ENTRY
*Ia32IdtEntry
;
358 UINT32 DebugTimerFrequency
;
360 if (InitFlag
== DEBUG_AGENT_INIT_DXE_AP
) {
362 // Invoked by AP, enable interrupt to let AP could receive IPI from other processors
369 // Disable Debug Timer interrupt
371 SaveAndSetDebugTimerInterrupt (FALSE
);
373 // Save and disable original interrupt status
375 InterruptStatus
= SaveAndDisableInterrupts ();
378 // Try to get mailbox firstly
382 MailboxLocation
= NULL
;
386 case DEBUG_AGENT_INIT_DXE_LOAD
:
388 // Check if Debug Agent has been initialized before
390 if (IsDebugAgentInitialzed ()) {
391 DEBUG ((EFI_D_INFO
, "Debug Agent: The former agent will be overwritten by the new one!\n"));
394 mMultiProcessorDebugSupport
= TRUE
;
396 // Save original IDT table
398 AsmReadIdtr (&IdtDescriptor
);
399 mSaveIdtTableSize
= IdtDescriptor
.Limit
+ 1;
400 mSavedIdtTable
= AllocateCopyPool (mSaveIdtTableSize
, (VOID
*) IdtDescriptor
.Base
);
402 // Check if Debug Agent initialized in DXE phase
404 Mailbox
= GetMailboxFromConfigurationTable ();
405 if (Mailbox
== NULL
) {
407 // Try to get mailbox from GUIDed HOB build in PEI
409 HobList
= GetHobList ();
410 Mailbox
= GetMailboxFromHob (HobList
);
413 // Set up IDT table and prepare for IDT entries
415 SetupDebugAgentEnviroment (Mailbox
);
417 // Initialize Debug Timer hardware and save its initial count and frequency
419 mDebugMpContext
.DebugTimerInitCount
= InitializeDebugTimer (&DebugTimerFrequency
);
420 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
422 // For DEBUG_AGENT_INIT_S3, needn't to install configuration table and EFI Serial IO protocol
423 // For DEBUG_AGENT_INIT_DXE_CORE, InternalConstructorWorker() will invoked in Constructor()
425 InternalConstructorWorker ();
427 // Enable interrupt to receive Debug Timer interrupt
431 mDebugAgentInitialized
= TRUE
;
432 FindAndReportModuleImageInfo (SIZE_4KB
);
434 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
436 if (gST
->ConOut
!= NULL
) {
437 Print (L
"Debug Agent: Initialized successfully!\r\n");
438 Print (L
"If the Debug Port is serial port, please make sure this serial port isn't connected by ISA Serial driver\r\n");
439 Print (L
"You could do the following steps to disconnect the serial port:\r\n");
440 Print (L
"1: Shell> drivers\r\n");
442 Print (L
" V VERSION E G G #D #C DRIVER NAME IMAGE NAME\r\n");
443 Print (L
" == ======== = = = == == =================================== ===================\r\n");
444 Print (L
" 8F 0000000A B - - 1 14 PCI Bus Driver PciBusDxe\r\n");
445 Print (L
" 91 00000010 ? - - - - ATA Bus Driver AtaBusDxe\r\n");
447 Print (L
" A7 0000000A B - - 1 1 ISA Serial Driver IsaSerialDxe\r\n");
449 Print (L
"2: Shell> dh -d A7\r\n");
450 Print (L
" A7: Image(IsaSerialDxe) ImageDevPath (..9FB3-11D4-9A3A-0090273FC14D))DriverBinding ComponentName ComponentName2\r\n");
451 Print (L
" Driver Name : ISA Serial Driver\r\n");
452 Print (L
" Image Name : FvFile(93B80003-9FB3-11D4-9A3A-0090273FC14D)\r\n");
453 Print (L
" Driver Version : 0000000A\r\n");
454 Print (L
" Driver Type : BUS\r\n");
455 Print (L
" Configuration : NO\r\n");
456 Print (L
" Diagnostics : NO\r\n");
457 Print (L
" Managing :\r\n");
458 Print (L
" Ctrl[EA] : PciRoot(0x0)/Pci(0x1F,0x0)/Serial(0x0)\r\n");
459 Print (L
" Child[EB] : PciRoot(0x0)/Pci(0x1F,0x0)/Serial(0x0)/Uart(115200,8,N,1)\r\n");
460 Print (L
"3: Shell> disconnect EA\r\n");
461 Print (L
"4: Shell> load -nc DebugAgentDxe.efi\r\n\r\n");
465 case DEBUG_AGENT_INIT_DXE_UNLOAD
:
466 if (mDebugAgentInitialized
) {
467 if (IsHostAttached ()) {
468 Print (L
"Debug Agent: Host is still connected, please de-attach TARGET firstly!\r\n");
469 *(EFI_STATUS
*)Context
= EFI_ACCESS_DENIED
;
471 // Enable Debug Timer interrupt again
473 SaveAndSetDebugTimerInterrupt (TRUE
);
476 // Restore original IDT table
478 AsmReadIdtr (&IdtDescriptor
);
479 IdtDescriptor
.Limit
= (UINT16
) (mSaveIdtTableSize
- 1);
480 CopyMem ((VOID
*) IdtDescriptor
.Base
, mSavedIdtTable
, mSaveIdtTableSize
);
481 AsmWriteIdtr (&IdtDescriptor
);
482 FreePool (mSavedIdtTable
);
483 mDebugAgentInitialized
= FALSE
;
484 *(EFI_STATUS
*)Context
= EFI_SUCCESS
;
487 Print (L
"Debug Agent: It hasn't been initialized, cannot unload it!\r\n");
488 *(EFI_STATUS
*)Context
= EFI_NOT_STARTED
;
492 // Restore interrupt state.
494 SetInterruptState (InterruptStatus
);
497 case DEBUG_AGENT_INIT_DXE_CORE
:
499 mMultiProcessorDebugSupport
= TRUE
;
501 // Try to get mailbox from GUIDed HOB build in PEI
504 Mailbox
= GetMailboxFromHob (HobList
);
506 // Set up IDT table and prepare for IDT entries
508 SetupDebugAgentEnviroment (Mailbox
);
510 // Initialize Debug Timer hardware and save its initial count and frequency
512 mDebugMpContext
.DebugTimerInitCount
= InitializeDebugTimer (&DebugTimerFrequency
);
513 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
515 // Enable interrupt to receive Debug Timer interrupt
521 case DEBUG_AGENT_INIT_S3
:
523 if (Context
!= NULL
) {
524 Ia32Idtr
= (IA32_DESCRIPTOR
*) Context
;
525 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
526 MailboxLocation
= (UINT64
*) (UINTN
) (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
527 (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
528 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
529 VerifyMailboxChecksum (Mailbox
);
532 // Save Mailbox pointer in global variable
534 mMailboxPointer
= Mailbox
;
536 // Set up IDT table and prepare for IDT entries
538 SetupDebugAgentEnviroment (Mailbox
);
542 DisableInterrupts ();
543 FindAndReportModuleImageInfo (SIZE_4KB
);
544 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT
) == 1) {
546 // If Boot Script entry break is set, code will be break at here.
554 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
555 // Debug Agent library instance.
557 DEBUG ((EFI_D_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));