]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c
SourceLevelDebugPkg: Refine casting expression result to bigger size
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SmmDebugAgent / SmmDebugAgentLib.c
index fa42311659e7f22e555bacb1bc7979296b626105..11afd329fad334c6f572fe0b9f5725dd4158ca54 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Agent library implementition.\r
 \r
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, 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
 #include "SmmDebugAgentLib.h"\r
 \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
 \r
-GLOBAL_REMOVE_IF_UNREFERENCED DEBUG_AGENT_MAILBOX         mLocalMailbox;\r
+CHAR8 mWarningMsgIgnoreSmmEntryBreak[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";\r
 \r
-GLOBAL_REMOVE_IF_UNREFERENCED UINTN    mSavedDebugRegisters[6];\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
-CONST BOOLEAN                    MultiProcessorDebugSupport = FALSE;\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
+\r
+  @param[in]  Handle         Pointer to Debug Port handle.\r
+  @param[out] BreakSymbol    Returned break symbol.\r
+\r
+  @retval EFI_SUCCESS        Read the symbol in BreakSymbol.\r
+  @retval EFI_NOT_FOUND      No read the break symbol.\r
+\r
+**/\r
+EFI_STATUS\r
+DebugReadBreakSymbol (\r
+  IN  DEBUG_PORT_HANDLE      Handle,\r
+  OUT UINT8                  *BreakSymbol\r
+  )\r
+{\r
+  //\r
+  // Smm instance has no debug timer to poll break symbol.\r
+  //\r
+  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
+  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
@@ -34,6 +97,7 @@ GetMailboxPointer (
   VOID\r
   )\r
 {\r
+  VerifyMailboxChecksum (mMailboxPointer);\r
   return mMailboxPointer;\r
 }\r
 \r
@@ -48,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
@@ -116,31 +180,138 @@ InitializeDebugAgent (
   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
+  BOOLEAN                       PeriodicMode;\r
+  UINTN                         TimerCycle;\r
 \r
   switch (InitFlag) {\r
   case DEBUG_AGENT_INIT_SMM:\r
-    Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &mMailboxPointer);\r
-    if (EFI_ERROR (Status) || mMailboxPointer == NULL) {\r
-      ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
-      mMailboxPointer = &mLocalMailbox;\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 ((EFI_D_ERROR, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));\r
+      CpuDeadLoop ();\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
+    // 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
+    // Debug Agent was not initialized before, use the local mailbox.\r
+    //\r
+    ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));\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
+    // 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
 \r
   case DEBUG_AGENT_INIT_ENTER_SMI:\r
     SaveDebugRegister ();\r
-    InitializeDebugIdt ();\r
-\r
-    if (mMailboxPointer != NULL) {\r
+    if (!mSmmDebugIdtInitFlag) {\r
       //\r
-      // Initialize debug communication port\r
+      // We only need to initialize Debug IDT table at first SMI entry\r
+      // after SMM relocation.\r
       //\r
-      DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)mMailboxPointer->DebugPortHandle, NULL);\r
-      mMailboxPointer->DebugPortHandle = DebugPortHandle;\r
-\r
-      if (mMailboxPointer->DebugFlag.Bits.BreakOnNextSmi == 1) {\r
+      InitializeDebugIdt ();\r
+      mSmmDebugIdtInitFlag = TRUE;\r
+    }\r
+    //\r
+    // Check if CPU APIC Timer is working, otherwise initialize it.\r
+    //\r
+    InitializeLocalApicSoftwareEnable (TRUE);\r
+    GetApicTimerState (NULL, &PeriodicMode, NULL);\r
+    TimerCycle = GetApicTimerInitCount ();\r
+    if (!PeriodicMode || TimerCycle == 0) {\r
+      InitializeDebugTimer (NULL, FALSE);\r
+    }\r
+    Mailbox = GetMailboxPointer ();\r
+    if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {\r
+      //\r
+      // If Debug Agent has been communicaton state with HOST, we need skip\r
+      // any break points set in SMM, set Skip Breakpoint flag\r
+      //\r
+      mSkipBreakpoint = TRUE;\r
+    }\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 ((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
@@ -150,8 +321,61 @@ InitializeDebugAgent (
     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
     break;\r
+\r
+  case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
+    if (Context == NULL) {\r
+      DEBUG ((EFI_D_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
+    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 ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
+    CpuDeadLoop ();\r
+    break;    \r
   }\r
 }\r
 \r