2010-09-12 08:43:36 +02:00
|
|
|
/** @file
|
|
|
|
Debug Agent library implementition.
|
|
|
|
|
2018-06-27 15:14:00 +02:00
|
|
|
Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
|
2010-09-12 08:43:36 +02:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php.
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "SmmDebugAgentLib.h"
|
|
|
|
|
|
|
|
DEBUG_AGENT_MAILBOX *mMailboxPointer = NULL;
|
2010-11-17 02:25:34 +01:00
|
|
|
DEBUG_AGENT_MAILBOX mLocalMailbox;
|
|
|
|
UINTN mSavedDebugRegisters[6];
|
2013-01-25 03:36:18 +01:00
|
|
|
IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable[33];
|
|
|
|
BOOLEAN mSkipBreakpoint = FALSE;
|
2016-11-29 07:51:18 +01:00
|
|
|
BOOLEAN mSmmDebugIdtInitFlag = FALSE;
|
2017-10-10 12:04:12 +02:00
|
|
|
BOOLEAN mApicTimerRestore = FALSE;
|
|
|
|
BOOLEAN mPeriodicMode;
|
|
|
|
UINT32 mTimerCycle;
|
|
|
|
UINTN mApicTimerDivisor;
|
|
|
|
UINT8 mVector;
|
2013-01-25 03:36:18 +01:00
|
|
|
|
|
|
|
CHAR8 mWarningMsgIgnoreSmmEntryBreak[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check if debug agent support multi-processor.
|
|
|
|
|
|
|
|
@retval TRUE Multi-processor is supported.
|
|
|
|
@retval FALSE Multi-processor is not supported.
|
|
|
|
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
MultiProcessorDebugSupport (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-09-12 08:43:36 +02:00
|
|
|
|
2012-06-11 04:15:11 +02:00
|
|
|
/**
|
|
|
|
Read the Attach/Break-in symbols from the debug port.
|
|
|
|
|
|
|
|
@param[in] Handle Pointer to Debug Port handle.
|
|
|
|
@param[out] BreakSymbol Returned break symbol.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Read the symbol in BreakSymbol.
|
|
|
|
@retval EFI_NOT_FOUND No read the break symbol.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
DebugReadBreakSymbol (
|
|
|
|
IN DEBUG_PORT_HANDLE Handle,
|
|
|
|
OUT UINT8 *BreakSymbol
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Smm instance has no debug timer to poll break symbol.
|
|
|
|
//
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
2010-09-12 08:43:36 +02:00
|
|
|
|
2013-01-25 03:36:18 +01:00
|
|
|
/**
|
|
|
|
Get the pointer to Mailbox from the GUIDed HOB.
|
|
|
|
|
|
|
|
@return Pointer to Mailbox.
|
|
|
|
|
|
|
|
**/
|
|
|
|
DEBUG_AGENT_MAILBOX *
|
|
|
|
GetMailboxFromHob (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_HOB_GUID_TYPE *GuidHob;
|
2018-06-27 15:14:00 +02:00
|
|
|
UINT64 *MailboxLocation;
|
2013-01-25 03:36:18 +01:00
|
|
|
DEBUG_AGENT_MAILBOX *Mailbox;
|
|
|
|
|
|
|
|
GuidHob = GetFirstGuidHob (&gEfiDebugAgentGuid);
|
|
|
|
if (GuidHob == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MailboxLocation = (UINT64 *) (GET_GUID_HOB_DATA(GuidHob));
|
|
|
|
Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);
|
|
|
|
VerifyMailboxChecksum (Mailbox);
|
2018-06-27 15:14:00 +02:00
|
|
|
|
2013-01-25 03:36:18 +01:00
|
|
|
return Mailbox;
|
|
|
|
}
|
|
|
|
|
2010-09-12 08:43:36 +02:00
|
|
|
/**
|
|
|
|
Get Debug Agent Mailbox pointer.
|
|
|
|
|
|
|
|
@return Mailbox pointer.
|
|
|
|
|
|
|
|
**/
|
|
|
|
DEBUG_AGENT_MAILBOX *
|
|
|
|
GetMailboxPointer (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2013-01-25 03:36:18 +01:00
|
|
|
VerifyMailboxChecksum (mMailboxPointer);
|
2010-09-12 08:43:36 +02:00
|
|
|
return mMailboxPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get debug port handle.
|
|
|
|
|
|
|
|
@return Debug port handle.
|
|
|
|
|
|
|
|
**/
|
|
|
|
DEBUG_PORT_HANDLE
|
|
|
|
GetDebugPortHandle (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2013-01-25 03:36:18 +01:00
|
|
|
return (DEBUG_PORT_HANDLE) (UINTN)(GetMailboxPointer()->DebugPortHandle);
|
2010-09-12 08:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Store debug register when SMI exit.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SaveDebugRegister (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
mSavedDebugRegisters[0] = AsmReadDr0 ();
|
|
|
|
mSavedDebugRegisters[1] = AsmReadDr1 ();
|
|
|
|
mSavedDebugRegisters[2] = AsmReadDr2 ();
|
|
|
|
mSavedDebugRegisters[3] = AsmReadDr3 ();
|
|
|
|
mSavedDebugRegisters[4] = AsmReadDr6 ();
|
|
|
|
mSavedDebugRegisters[5] = AsmReadDr7 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Restore debug register when SMI exit.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
RestoreDebugRegister (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
AsmWriteDr7 (0);
|
|
|
|
AsmWriteDr0 (mSavedDebugRegisters[0]);
|
|
|
|
AsmWriteDr1 (mSavedDebugRegisters[1]);
|
|
|
|
AsmWriteDr2 (mSavedDebugRegisters[2]);
|
|
|
|
AsmWriteDr3 (mSavedDebugRegisters[3]);
|
|
|
|
AsmWriteDr6 (mSavedDebugRegisters[4]);
|
|
|
|
AsmWriteDr7 (mSavedDebugRegisters[5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize debug agent.
|
|
|
|
|
|
|
|
This function is used to set up debug enviroment for source level debug
|
|
|
|
in SMM code.
|
|
|
|
|
|
|
|
If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries
|
|
|
|
and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,
|
|
|
|
it it exists, debug agent wiil copied it into the local Mailbox in SMM space.
|
|
|
|
it will overirde IDT table entries and initialize debug port. Context will be
|
|
|
|
NULL.
|
|
|
|
If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug
|
|
|
|
Registers and get local Mailbox in SMM space. Context will be NULL.
|
|
|
|
If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug
|
|
|
|
Registers. Context will be NULL.
|
|
|
|
|
|
|
|
@param[in] InitFlag Init flag is used to decide initialize process.
|
|
|
|
@param[in] Context Context needed according to InitFlag.
|
|
|
|
@param[in] Function Continue function called by debug agent library; it was
|
|
|
|
optional.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
InitializeDebugAgent (
|
|
|
|
IN UINT32 InitFlag,
|
|
|
|
IN VOID *Context, OPTIONAL
|
|
|
|
IN DEBUG_AGENT_CONTINUE Function OPTIONAL
|
|
|
|
)
|
|
|
|
{
|
2013-01-25 03:36:18 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT64 DebugPortHandle;
|
|
|
|
IA32_IDT_GATE_DESCRIPTOR IdtEntry[33];
|
|
|
|
IA32_DESCRIPTOR IdtDescriptor;
|
|
|
|
IA32_DESCRIPTOR *Ia32Idtr;
|
|
|
|
IA32_IDT_ENTRY *Ia32IdtEntry;
|
|
|
|
IA32_DESCRIPTOR Idtr;
|
|
|
|
UINT16 IdtEntryCount;
|
|
|
|
DEBUG_AGENT_MAILBOX *Mailbox;
|
|
|
|
UINT64 *MailboxLocation;
|
2015-04-01 09:51:15 +02:00
|
|
|
UINT32 DebugTimerFrequency;
|
2010-09-12 08:43:36 +02:00
|
|
|
|
|
|
|
switch (InitFlag) {
|
|
|
|
case DEBUG_AGENT_INIT_SMM:
|
2013-11-22 07:30:01 +01:00
|
|
|
//
|
|
|
|
// Install configuration table for persisted vector handoff info
|
|
|
|
//
|
|
|
|
Status = gSmst->SmmInstallConfigurationTable (
|
|
|
|
gSmst,
|
|
|
|
&gEfiVectorHandoffTableGuid,
|
|
|
|
(VOID *) &mVectorHandoffInfoDebugAgent[0],
|
|
|
|
sizeof (EFI_VECTOR_HANDOFF_INFO) * mVectorHandoffInfoCount
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
|
|
|
|
CpuDeadLoop ();
|
|
|
|
}
|
2013-01-25 03:36:18 +01:00
|
|
|
//
|
|
|
|
// Check if Debug Agent initialized in DXE phase
|
|
|
|
//
|
|
|
|
Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &Mailbox);
|
|
|
|
if (Status == EFI_SUCCESS && Mailbox != NULL) {
|
|
|
|
VerifyMailboxChecksum (Mailbox);
|
|
|
|
mMailboxPointer = Mailbox;
|
|
|
|
break;
|
2010-09-12 08:43:36 +02:00
|
|
|
}
|
2013-01-25 03:36:18 +01:00
|
|
|
//
|
|
|
|
// Check if Debug Agent initialized in SEC/PEI phase
|
|
|
|
//
|
2018-06-27 15:14:00 +02:00
|
|
|
Mailbox = GetMailboxFromHob ();
|
2013-01-25 03:36:18 +01:00
|
|
|
if (Mailbox != NULL) {
|
|
|
|
mMailboxPointer = Mailbox;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//
|
2013-11-22 07:30:01 +01:00
|
|
|
// Debug Agent was not initialized before, use the local mailbox.
|
2013-01-25 03:36:18 +01:00
|
|
|
//
|
|
|
|
ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));
|
|
|
|
Mailbox = &mLocalMailbox;
|
|
|
|
//
|
|
|
|
// Save original IDT entries
|
|
|
|
//
|
2018-06-27 15:14:00 +02:00
|
|
|
AsmReadIdtr (&IdtDescriptor);
|
2013-01-25 03:36:18 +01:00
|
|
|
CopyMem (&IdtEntry, (VOID *)IdtDescriptor.Base, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR));
|
|
|
|
//
|
|
|
|
// Initialized Debug Agent
|
|
|
|
//
|
|
|
|
InitializeDebugIdt ();
|
2015-04-01 09:51:15 +02:00
|
|
|
//
|
|
|
|
// Initialize Debug Timer hardware and save its frequency
|
|
|
|
//
|
2015-05-25 04:48:00 +02:00
|
|
|
InitializeDebugTimer (&DebugTimerFrequency, TRUE);
|
2016-04-01 06:53:06 +02:00
|
|
|
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);
|
2015-04-01 09:51:15 +02:00
|
|
|
|
2013-01-25 03:36:18 +01:00
|
|
|
DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle, NULL);
|
|
|
|
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);
|
|
|
|
mMailboxPointer = Mailbox;
|
|
|
|
//
|
|
|
|
// Trigger one software interrupt to inform HOST
|
|
|
|
//
|
|
|
|
TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);
|
|
|
|
|
|
|
|
SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);
|
|
|
|
//
|
|
|
|
// Memory has been ready
|
|
|
|
//
|
|
|
|
if (IsHostAttached ()) {
|
|
|
|
//
|
|
|
|
// Trigger one software interrupt to inform HOST
|
|
|
|
//
|
|
|
|
TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Find and report PE/COFF image info to HOST
|
2018-06-27 15:14:00 +02:00
|
|
|
//
|
2013-01-25 03:36:18 +01:00
|
|
|
FindAndReportModuleImageInfo (SIZE_4KB);
|
|
|
|
//
|
|
|
|
// Restore saved IDT entries
|
2018-06-27 15:14:00 +02:00
|
|
|
//
|
2013-01-25 03:36:18 +01:00
|
|
|
CopyMem ((VOID *)IdtDescriptor.Base, &IdtEntry, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR));
|
2010-09-12 08:43:36 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEBUG_AGENT_INIT_ENTER_SMI:
|
|
|
|
SaveDebugRegister ();
|
2016-11-29 07:51:18 +01:00
|
|
|
if (!mSmmDebugIdtInitFlag) {
|
|
|
|
//
|
|
|
|
// We only need to initialize Debug IDT table at first SMI entry
|
|
|
|
// after SMM relocation.
|
|
|
|
//
|
|
|
|
InitializeDebugIdt ();
|
|
|
|
mSmmDebugIdtInitFlag = TRUE;
|
|
|
|
}
|
2015-05-25 04:46:11 +02:00
|
|
|
//
|
|
|
|
// Check if CPU APIC Timer is working, otherwise initialize it.
|
|
|
|
//
|
2015-10-30 18:53:53 +01:00
|
|
|
InitializeLocalApicSoftwareEnable (TRUE);
|
2017-10-10 12:04:12 +02:00
|
|
|
GetApicTimerState (&mApicTimerDivisor, &mPeriodicMode, &mVector);
|
|
|
|
mTimerCycle = GetApicTimerInitCount ();
|
|
|
|
if (!mPeriodicMode || mTimerCycle == 0) {
|
|
|
|
mApicTimerRestore = TRUE;
|
2015-05-25 04:48:00 +02:00
|
|
|
InitializeDebugTimer (NULL, FALSE);
|
2015-05-25 04:46:11 +02:00
|
|
|
}
|
2013-01-25 03:36:18 +01:00
|
|
|
Mailbox = GetMailboxPointer ();
|
|
|
|
if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {
|
2010-09-12 08:43:36 +02:00
|
|
|
//
|
2013-01-25 03:36:18 +01:00
|
|
|
// If Debug Agent has been communicaton state with HOST, we need skip
|
|
|
|
// any break points set in SMM, set Skip Breakpoint flag
|
2010-09-12 08:43:36 +02:00
|
|
|
//
|
2013-01-25 03:36:18 +01:00
|
|
|
mSkipBreakpoint = TRUE;
|
|
|
|
}
|
|
|
|
if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI) == 1) {
|
|
|
|
if (mSkipBreakpoint) {
|
|
|
|
//
|
|
|
|
// Print warning message if ignore smm entry break
|
|
|
|
//
|
|
|
|
DebugPortWriteBuffer ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle,
|
|
|
|
(UINT8 *)mWarningMsgIgnoreSmmEntryBreak,
|
|
|
|
AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak)
|
|
|
|
);
|
|
|
|
} else {
|
2010-09-12 08:43:36 +02:00
|
|
|
//
|
|
|
|
// If SMM entry break is set, SMM code will be break at here.
|
|
|
|
//
|
|
|
|
CpuBreakpoint ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEBUG_AGENT_INIT_EXIT_SMI:
|
2013-01-25 03:36:18 +01:00
|
|
|
Mailbox = GetMailboxPointer ();
|
|
|
|
//
|
|
|
|
// Clear Skip Breakpoint flag
|
|
|
|
//
|
|
|
|
mSkipBreakpoint = FALSE;
|
2010-09-12 08:43:36 +02:00
|
|
|
RestoreDebugRegister ();
|
2017-10-10 12:04:12 +02:00
|
|
|
//
|
|
|
|
// Restore APIC Timer
|
|
|
|
//
|
|
|
|
if (mApicTimerRestore) {
|
|
|
|
InitializeApicTimer (mApicTimerDivisor, mTimerCycle, mPeriodicMode, mVector);
|
|
|
|
mApicTimerRestore = FALSE;
|
|
|
|
}
|
2010-09-12 08:43:36 +02:00
|
|
|
break;
|
2013-01-25 03:36:18 +01:00
|
|
|
|
|
|
|
case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:
|
|
|
|
if (Context == NULL) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));
|
|
|
|
CpuDeadLoop ();
|
|
|
|
} else {
|
|
|
|
Ia32Idtr = (IA32_DESCRIPTOR *) Context;
|
|
|
|
Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);
|
2017-02-17 04:44:04 +01:00
|
|
|
MailboxLocation = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +
|
|
|
|
((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));
|
2013-01-25 03:36:18 +01:00
|
|
|
mMailboxPointer = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);
|
|
|
|
VerifyMailboxChecksum (mMailboxPointer);
|
|
|
|
//
|
|
|
|
// Get original IDT address and size.
|
|
|
|
//
|
|
|
|
AsmReadIdtr ((IA32_DESCRIPTOR *) &Idtr);
|
|
|
|
IdtEntryCount = (UINT16) ((Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR));
|
|
|
|
if (IdtEntryCount < 33) {
|
|
|
|
Idtr.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * 33 - 1);
|
|
|
|
Idtr.Base = (UINTN) &mIdtEntryTable;
|
|
|
|
ZeroMem (&mIdtEntryTable, Idtr.Limit + 1);
|
|
|
|
AsmWriteIdtr ((IA32_DESCRIPTOR *) &Idtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeDebugIdt ();
|
|
|
|
//
|
2015-04-01 09:51:15 +02:00
|
|
|
// Initialize Debug Timer hardware and save its frequency
|
2013-01-25 03:36:18 +01:00
|
|
|
//
|
2015-05-25 04:48:00 +02:00
|
|
|
InitializeDebugTimer (&DebugTimerFrequency, TRUE);
|
2015-04-01 09:51:15 +02:00
|
|
|
UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);
|
2015-06-08 08:36:41 +02:00
|
|
|
//
|
|
|
|
// Enable Debug Timer interrupt and CPU interrupt
|
|
|
|
//
|
|
|
|
SaveAndSetDebugTimerInterrupt (TRUE);
|
2013-01-25 03:36:18 +01:00
|
|
|
EnableInterrupts ();
|
|
|
|
|
|
|
|
FindAndReportModuleImageInfo (SIZE_4KB);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
//
|
2018-06-27 15:14:00 +02:00
|
|
|
// Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
|
2013-01-25 03:36:18 +01:00
|
|
|
// Debug Agent library instance.
|
|
|
|
//
|
|
|
|
DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));
|
|
|
|
CpuDeadLoop ();
|
2018-06-27 15:14:00 +02:00
|
|
|
break;
|
2010-09-12 08:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|