+ if (DebugAgentContext != NULL) {\r
+ DebugPortHandle = (UINT64)(UINT32)(Mailbox->DebugPortHandle + DebugAgentContext->StackMigrateOffset);\r
+ UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
+ Mailbox = (DEBUG_AGENT_MAILBOX *) ((UINTN) Mailbox + DebugAgentContext->StackMigrateOffset);\r
+ MailboxLocation = (UINT64)(UINTN)Mailbox;\r
+ //\r
+ // Build mailbox location in HOB and fix-up its address\r
+ //\r
+ MailboxLocationPointer = BuildGuidDataHob (\r
+ &gEfiDebugAgentGuid,\r
+ &MailboxLocation,\r
+ sizeof (UINT64)\r
+ );\r
+ MailboxLocationPointer = (UINT64 *) ((UINTN) MailboxLocationPointer + DebugAgentContext->HeapMigrateOffset);\r
+ } else {\r
+ //\r
+ // DebugAgentContext is NULL. Then, Mailbox can directly be copied into memory.\r
+ // Allocate ACPI NVS memory for new Mailbox and Debug Port Handle buffer\r
+ //\r
+ Status = PeiServicesAllocatePages (\r
+ EfiACPIMemoryNVS,\r
+ EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
+ &Address\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate pages!\n"));\r
+ CpuDeadLoop ();\r
+ }\r
+ NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
+ //\r
+ // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox\r
+ // and Debug Port Handle buffer in the allocated pool that may be marked as free by DXE Core after DXE Core\r
+ // reallocates the HOB.\r
+ //\r
+ CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
+ CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
+ UpdateMailboxContent (NewMailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, (UINT64)(UINTN)(NewMailbox + 1));\r
+ MailboxLocation = (UINT64)(UINTN)NewMailbox;\r
+ //\r
+ // Build mailbox location in HOB\r
+ //\r
+ MailboxLocationPointer = BuildGuidDataHob (\r
+ &gEfiDebugAgentGuid,\r
+ &MailboxLocation,\r
+ sizeof (UINT64)\r
+ );\r
+ }\r
+ //\r
+ // Update IDT entry to save the location saved mailbox pointer\r
+ //\r
+ SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
+ break;\r
+\r
+ case DEBUG_AGENT_INIT_PEI:\r
+ if (Context == NULL) {\r
+ DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
+ CpuDeadLoop ();\r
+ }\r
+ //\r
+ // Check if Debug Agent has initialized before\r
+ //\r
+ if (IsDebugAgentInitialzed()) {\r
+ DEBUG ((EFI_D_WARN, "Debug Agent: It has already initialized in SEC Core!\n"));\r
+ break;\r
+ }\r
+ //\r
+ // Install Vector Handoff Info PPI to persist vectors used by Debug Agent\r
+ //\r
+ Status = PeiServicesInstallPpi (&mVectorHandoffInfoPpiList[0]);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install Vector Handoff Info PPI!\n"));\r
+ CpuDeadLoop ();\r
+ }\r
+ //\r
+ // Set up IDT entries\r
+ //\r
+ InitializeDebugIdt ();\r
+ //\r
+ // Build mailbox in HOB and setup Mailbox Set In Pei flag\r
+ //\r
+ Mailbox = AllocateZeroPool (sizeof (DEBUG_AGENT_MAILBOX));\r
+ if (Mailbox == NULL) {\r
+ DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate memory!\n"));\r
+ CpuDeadLoop ();\r
+ } else {\r
+ MailboxLocation = (UINT64)(UINTN)Mailbox;\r
+ MailboxLocationPointer = BuildGuidDataHob (\r
+ &gEfiDebugAgentGuid,\r
+ &MailboxLocation,\r
+ sizeof (UINT64)\r
+ );\r
+ //\r
+ // Initialize Debug Timer hardware and save its frequency\r
+ //\r
+ InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
+ UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
+ //\r
+ // Update IDT entry to save the location pointer saved mailbox pointer\r
+ //\r
+ SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
+ }\r
+ //\r
+ // Save init arch type when debug agent initialized\r
+ //\r
+ SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
+ //\r
+ // Register for a callback once memory has been initialized.\r
+ // If memory has been ready, the callback function will be invoked immediately\r
+ //\r
+ Status = PeiServicesNotifyPpi (&mDebugAgentMemoryDiscoveredNotifyList[0]);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to register memory discovered callback function!\n"));\r
+ CpuDeadLoop ();\r
+ }\r
+ //\r
+ // Set HOB check flag if memory has not been ready yet\r
+ //\r
+ if (GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY) == 0) {\r
+ SetDebugFlag (DEBUG_AGENT_FLAG_CHECK_MAILBOX_IN_HOB, 1);\r
+ }\r