]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c
SourceLevelDebugPkg: Apply uncrustify changes
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SmmDebugAgent / SmmDebugAgentLib.c
index 3326c0a05fb4c0bae0d9c359b96722c8065ce417..f49a592d276292b1f7a9f2f9e240f546099a9467 100644 (file)
@@ -1,23 +1,41 @@
 /** @file\r
-  Debug Agent library implementition.\r
+  Debug Agent library implementation.\r
 \r
-  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php.\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "SmmDebugAgentLib.h"\r
 \r
-DEBUG_AGENT_MAILBOX         *mMailboxPointer = NULL;\r
-DEBUG_AGENT_MAILBOX         mLocalMailbox;\r
-UINTN                       mSavedDebugRegisters[6];\r
-CONST BOOLEAN               MultiProcessorDebugSupport = FALSE;\r
+DEBUG_AGENT_MAILBOX       *mMailboxPointer = NULL;\r
+DEBUG_AGENT_MAILBOX       mLocalMailbox;\r
+UINTN                     mSavedDebugRegisters[6];\r
+IA32_IDT_GATE_DESCRIPTOR  mIdtEntryTable[33];\r
+BOOLEAN                   mSkipBreakpoint      = FALSE;\r
+BOOLEAN                   mSmmDebugIdtInitFlag = FALSE;\r
+BOOLEAN                   mApicTimerRestore    = FALSE;\r
+BOOLEAN                   mPeriodicMode;\r
+UINT32                    mTimerCycle;\r
+UINTN                     mApicTimerDivisor;\r
+UINT8                     mVector;\r
+\r
+CHAR8  mWarningMsgIgnoreSmmEntryBreak[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";\r
+\r
+/**\r
+  Check if debug agent support multi-processor.\r
+\r
+  @retval TRUE    Multi-processor is supported.\r
+  @retval FALSE   Multi-processor is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+MultiProcessorDebugSupport (\r
+  VOID\r
+  )\r
+{\r
+  return FALSE;\r
+}\r
 \r
 /**\r
   Read the Attach/Break-in symbols from the debug port.\r
@@ -31,8 +49,8 @@ CONST BOOLEAN               MultiProcessorDebugSupport = FALSE;
 **/\r
 EFI_STATUS\r
 DebugReadBreakSymbol (\r
-  IN  DEBUG_PORT_HANDLE      Handle,\r
-  OUT UINT8                  *BreakSymbol\r
+  IN  DEBUG_PORT_HANDLE  Handle,\r
+  OUT UINT8              *BreakSymbol\r
   )\r
 {\r
   //\r
@@ -41,6 +59,33 @@ DebugReadBreakSymbol (
   return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  Get the pointer to Mailbox from the GUIDed HOB.\r
+\r
+  @return Pointer to Mailbox.\r
+\r
+**/\r
+DEBUG_AGENT_MAILBOX *\r
+GetMailboxFromHob (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE    *GuidHob;\r
+  UINT64               *MailboxLocation;\r
+  DEBUG_AGENT_MAILBOX  *Mailbox;\r
+\r
+  GuidHob = GetFirstGuidHob (&gEfiDebugAgentGuid);\r
+  if (GuidHob == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  MailboxLocation = (UINT64 *)(GET_GUID_HOB_DATA (GuidHob));\r
+  Mailbox         = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
+  VerifyMailboxChecksum (Mailbox);\r
+\r
+  return Mailbox;\r
+}\r
+\r
 /**\r
   Get Debug Agent Mailbox pointer.\r
 \r
@@ -52,6 +97,7 @@ GetMailboxPointer (
   VOID\r
   )\r
 {\r
+  VerifyMailboxChecksum (mMailboxPointer);\r
   return mMailboxPointer;\r
 }\r
 \r
@@ -66,7 +112,7 @@ GetDebugPortHandle (
   VOID\r
   )\r
 {\r
-  return (DEBUG_PORT_HANDLE) (UINTN)(mMailboxPointer->DebugPortHandle);\r
+  return (DEBUG_PORT_HANDLE)(UINTN)(GetMailboxPointer ()->DebugPortHandle);\r
 }\r
 \r
 /**\r
@@ -107,13 +153,13 @@ RestoreDebugRegister (
 /**\r
   Initialize debug agent.\r
 \r
-  This function is used to set up debug enviroment for source level debug\r
+  This function is used to set up debug environment for source level debug\r
   in SMM code.\r
 \r
-  If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries\r
+  If InitFlag is DEBUG_AGENT_INIT_SMM, it will override IDT table entries\r
   and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,\r
   it it exists, debug agent wiil copied it into the local Mailbox in SMM space.\r
-  it will overirde IDT table entries and initialize debug port. Context will be\r
+  it will override IDT table entries and initialize debug port. Context will be\r
   NULL.\r
   If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug\r
   Registers and get local Mailbox in SMM space. Context will be NULL.\r
@@ -130,46 +176,222 @@ VOID
 EFIAPI\r
 InitializeDebugAgent (\r
   IN UINT32                InitFlag,\r
-  IN VOID                  *Context, OPTIONAL\r
+  IN VOID                  *Context  OPTIONAL,\r
   IN DEBUG_AGENT_CONTINUE  Function  OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS               Status;\r
-  UINT64                   DebugPortHandle;\r
+  EFI_STATUS                Status;\r
+  UINT64                    DebugPortHandle;\r
+  IA32_IDT_GATE_DESCRIPTOR  IdtEntry[33];\r
+  IA32_DESCRIPTOR           IdtDescriptor;\r
+  IA32_DESCRIPTOR           *Ia32Idtr;\r
+  IA32_IDT_ENTRY            *Ia32IdtEntry;\r
+  IA32_DESCRIPTOR           Idtr;\r
+  UINT16                    IdtEntryCount;\r
+  DEBUG_AGENT_MAILBOX       *Mailbox;\r
+  UINT64                    *MailboxLocation;\r
+  UINT32                    DebugTimerFrequency;\r
 \r
   switch (InitFlag) {\r
-  case DEBUG_AGENT_INIT_SMM:\r
-    Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &mMailboxPointer);\r
-    if (EFI_ERROR (Status) || mMailboxPointer == NULL) {\r
+    case DEBUG_AGENT_INIT_SMM:\r
+      //\r
+      // Install configuration table for persisted vector handoff info\r
+      //\r
+      Status = gSmst->SmmInstallConfigurationTable (\r
+                        gSmst,\r
+                        &gEfiVectorHandoffTableGuid,\r
+                        (VOID *)&mVectorHandoffInfoDebugAgent[0],\r
+                        sizeof (EFI_VECTOR_HANDOFF_INFO) * mVectorHandoffInfoCount\r
+                        );\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((DEBUG_ERROR, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));\r
+        CpuDeadLoop ();\r
+      }\r
+\r
+      //\r
+      // Check if Debug Agent initialized in DXE phase\r
+      //\r
+      Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **)&Mailbox);\r
+      if ((Status == EFI_SUCCESS) && (Mailbox != NULL)) {\r
+        VerifyMailboxChecksum (Mailbox);\r
+        mMailboxPointer = Mailbox;\r
+        break;\r
+      }\r
+\r
+      //\r
+      // Check if Debug Agent initialized in SEC/PEI phase\r
+      //\r
+      Mailbox = GetMailboxFromHob ();\r
+      if (Mailbox != NULL) {\r
+        mMailboxPointer = Mailbox;\r
+        break;\r
+      }\r
+\r
+      //\r
+      // Debug Agent was not initialized before, use the local mailbox.\r
+      //\r
       ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
-      mMailboxPointer = &mLocalMailbox;\r
-    }\r
+      Mailbox = &mLocalMailbox;\r
+      //\r
+      // Save original IDT entries\r
+      //\r
+      AsmReadIdtr (&IdtDescriptor);\r
+      CopyMem (&IdtEntry, (VOID *)IdtDescriptor.Base, 33 * sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
+      //\r
+      // Initialized Debug Agent\r
+      //\r
+      InitializeDebugIdt ();\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
+      DebugPortHandle = (UINT64)(UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE)(UINTN)Mailbox->DebugPortHandle, NULL);\r
+      UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
+      mMailboxPointer = Mailbox;\r
+      //\r
+      // Trigger one software interrupt to inform HOST\r
+      //\r
+      TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
+\r
+      SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
+      //\r
+      // Memory has been ready\r
+      //\r
+      if (IsHostAttached ()) {\r
+        //\r
+        // Trigger one software interrupt to inform HOST\r
+        //\r
+        TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
+      }\r
+\r
+      //\r
+      // Find and report PE/COFF image info to HOST\r
+      //\r
+      FindAndReportModuleImageInfo (SIZE_4KB);\r
+      //\r
+      // Restore saved IDT entries\r
+      //\r
+      CopyMem ((VOID *)IdtDescriptor.Base, &IdtEntry, 33 * sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
 \r
-    break;\r
+      break;\r
 \r
-  case DEBUG_AGENT_INIT_ENTER_SMI:\r
-    SaveDebugRegister ();\r
-    InitializeDebugIdt ();\r
+    case DEBUG_AGENT_INIT_ENTER_SMI:\r
+      SaveDebugRegister ();\r
+      if (!mSmmDebugIdtInitFlag) {\r
+        //\r
+        // We only need to initialize Debug IDT table at first SMI entry\r
+        // after SMM relocation.\r
+        //\r
+        InitializeDebugIdt ();\r
+        mSmmDebugIdtInitFlag = TRUE;\r
+      }\r
 \r
-    if (mMailboxPointer != NULL) {\r
       //\r
-      // Initialize debug communication port\r
+      // Check if CPU APIC Timer is working, otherwise initialize it.\r
       //\r
-      DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)mMailboxPointer->DebugPortHandle, NULL);\r
-      mMailboxPointer->DebugPortHandle = DebugPortHandle;\r
+      InitializeLocalApicSoftwareEnable (TRUE);\r
+      GetApicTimerState (&mApicTimerDivisor, &mPeriodicMode, &mVector);\r
+      mTimerCycle = GetApicTimerInitCount ();\r
+      if (!mPeriodicMode || (mTimerCycle == 0)) {\r
+        mApicTimerRestore = TRUE;\r
+        InitializeDebugTimer (NULL, FALSE);\r
+      }\r
 \r
-      if (mMailboxPointer->DebugFlag.BreakOnNextSmi == 1) {\r
+      Mailbox = GetMailboxPointer ();\r
+      if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {\r
         //\r
-        // If SMM entry break is set, SMM code will be break at here.\r
+        // If Debug Agent has been communication state with HOST, we need skip\r
+        // any break points set in SMM, set Skip Breakpoint flag\r
         //\r
-        CpuBreakpoint ();\r
+        mSkipBreakpoint = TRUE;\r
       }\r
-    }\r
-    break;\r
 \r
-  case DEBUG_AGENT_INIT_EXIT_SMI:\r
-    RestoreDebugRegister ();\r
-    break;\r
+      if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI) == 1) {\r
+        if (mSkipBreakpoint) {\r
+          //\r
+          // Print warning message if ignore smm entry break\r
+          //\r
+          DebugPortWriteBuffer (\r
+            (DEBUG_PORT_HANDLE)(UINTN)Mailbox->DebugPortHandle,\r
+            (UINT8 *)mWarningMsgIgnoreSmmEntryBreak,\r
+            AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak)\r
+            );\r
+        } else {\r
+          //\r
+          // If SMM entry break is set, SMM code will be break at here.\r
+          //\r
+          CpuBreakpoint ();\r
+        }\r
+      }\r
+\r
+      break;\r
+\r
+    case DEBUG_AGENT_INIT_EXIT_SMI:\r
+      Mailbox = GetMailboxPointer ();\r
+      //\r
+      // Clear Skip Breakpoint flag\r
+      //\r
+      mSkipBreakpoint = FALSE;\r
+      RestoreDebugRegister ();\r
+      //\r
+      // Restore APIC Timer\r
+      //\r
+      if (mApicTimerRestore) {\r
+        InitializeApicTimer (mApicTimerDivisor, mTimerCycle, mPeriodicMode, mVector);\r
+        mApicTimerRestore = FALSE;\r
+      }\r
+\r
+      break;\r
+\r
+    case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
+      if (Context == NULL) {\r
+        DEBUG ((DEBUG_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
+        CpuDeadLoop ();\r
+      } else {\r
+        Ia32Idtr        =  (IA32_DESCRIPTOR *)Context;\r
+        Ia32IdtEntry    = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
+        MailboxLocation = (UINT64 *)((UINTN)Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
+                                     ((UINTN)Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
+        mMailboxPointer = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
+        VerifyMailboxChecksum (mMailboxPointer);\r
+        //\r
+        // Get original IDT address and size.\r
+        //\r
+        AsmReadIdtr ((IA32_DESCRIPTOR *)&Idtr);\r
+        IdtEntryCount = (UINT16)((Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
+        if (IdtEntryCount < 33) {\r
+          Idtr.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * 33 - 1);\r
+          Idtr.Base  = (UINTN)&mIdtEntryTable;\r
+          ZeroMem (&mIdtEntryTable, Idtr.Limit + 1);\r
+          AsmWriteIdtr ((IA32_DESCRIPTOR *)&Idtr);\r
+        }\r
+\r
+        InitializeDebugIdt ();\r
+        //\r
+        // Initialize Debug Timer hardware and save its frequency\r
+        //\r
+        InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
+        UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
+        //\r
+        // Enable Debug Timer interrupt and CPU interrupt\r
+        //\r
+        SaveAndSetDebugTimerInterrupt (TRUE);\r
+        EnableInterrupts ();\r
+\r
+        FindAndReportModuleImageInfo (SIZE_4KB);\r
+      }\r
+\r
+      break;\r
+\r
+    default:\r
+      //\r
+      // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this\r
+      // Debug Agent library instance.\r
+      //\r
+      DEBUG ((DEBUG_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
+      CpuDeadLoop ();\r
+      break;\r
   }\r
 }\r
-\r