]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgentLib.c
SourceLevelDebugPkg: Refine casting expression result to bigger size
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DxeDebugAgent / DxeDebugAgentLib.c
index d560b5235993e18a9070a910816537ac09fccc64..c74a1f6be3fb097277a24111e9a7ace3581dfa40 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Agent library implementition for Dxe Core and Dxr modules.\r
 \r
-  Copyright (c) 2010 - 2013, 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
@@ -57,6 +57,32 @@ InternalConstructorWorker (
   BOOLEAN                     DebugTimerInterruptState;\r
   DEBUG_AGENT_MAILBOX         *Mailbox;\r
   DEBUG_AGENT_MAILBOX         *NewMailbox;\r
+  EFI_HOB_GUID_TYPE           *GuidHob;\r
+  EFI_VECTOR_HANDOFF_INFO     *VectorHandoffInfo;\r
+\r
+  //\r
+  // Check persisted vector handoff info\r
+  //\r
+  Status = EFI_SUCCESS;\r
+  GuidHob = GetFirstGuidHob (&gEfiVectorHandoffInfoPpiGuid);\r
+  if (GuidHob != NULL && !mDxeCoreFlag) {\r
+    //\r
+    // Check if configuration table is installed or not if GUIDed HOB existed,\r
+    // only when Debug Agent is not linked by DXE Core\r
+    //\r
+    Status = EfiGetSystemConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID **) &VectorHandoffInfo);\r
+  }\r
+  if (GuidHob == NULL || Status != EFI_SUCCESS) {\r
+    //\r
+    // Install configuration table for persisted vector handoff info if GUIDed HOB cannot be found or\r
+    // configuration table does not exist\r
+    //\r
+    Status = gBS->InstallConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID *) &mVectorHandoffInfoDebugAgent[0]);\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
 \r
   //\r
   // Install EFI Serial IO protocol on debug port\r
@@ -70,7 +96,10 @@ InternalConstructorWorker (
                   EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
                   &Address\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "DebugAgent: Cannot install configuration table for mailbox!\n"));\r
+    CpuDeadLoop ();\r
+  }\r
 \r
   DebugTimerInterruptState = SaveAndSetDebugTimerInterrupt (FALSE);\r
 \r
@@ -91,7 +120,10 @@ InternalConstructorWorker (
   DebugTimerInterruptState = SaveAndSetDebugTimerInterrupt (DebugTimerInterruptState);\r
 \r
   Status = gBS->InstallConfigurationTable (&gEfiDebugAgentGuid, (VOID *) mMailboxPointer);\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install configuration for mailbox!\n"));\r
+    CpuDeadLoop ();\r
+  }\r
 }\r
 \r
 /**\r
@@ -133,7 +165,7 @@ GetMailboxFromConfigurationTable (
 {\r
   EFI_STATUS               Status;\r
   DEBUG_AGENT_MAILBOX      *Mailbox;\r
-  \r
+\r
   Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &Mailbox);\r
   if (Status == EFI_SUCCESS && Mailbox != NULL) {\r
     VerifyMailboxChecksum (Mailbox);\r
@@ -203,19 +235,24 @@ GetDebugPortHandle (
 }\r
 \r
 /**\r
-  Worker function to setup IDT table and initialize the IDT entries.\r
+  Worker function to set up Debug Agent environment.\r
+\r
+  This function will set up IDT table and initialize the IDT entries and \r
+  initialize CPU LOCAL APIC timer.\r
+  It also tries to connect HOST if Debug Agent was not initialized before.\r
 \r
   @param[in] Mailbox        Pointer to Mailbox.\r
 \r
 **/\r
 VOID\r
-SetupDebugAgentEnviroment (\r
+SetupDebugAgentEnvironment (\r
   IN DEBUG_AGENT_MAILBOX       *Mailbox\r
   )\r
 {\r
   IA32_DESCRIPTOR              Idtr;\r
   UINT16                       IdtEntryCount;\r
   UINT64                       DebugPortHandle;\r
+  UINT32                       DebugTimerFrequency;\r
 \r
   if (mMultiProcessorDebugSupport) {\r
     InitializeSpinLock (&mDebugMpContext.MpContextSpinLock);\r
@@ -233,9 +270,16 @@ SetupDebugAgentEnviroment (
   AsmReadIdtr ((IA32_DESCRIPTOR *) &Idtr);\r
   IdtEntryCount = (UINT16) ((Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
   if (IdtEntryCount < 33) {\r
+    ZeroMem (&mIdtEntryTable, sizeof (IA32_IDT_GATE_DESCRIPTOR) * 33);\r
+    //\r
+    // Copy original IDT table into new one\r
+    //\r
+    CopyMem (&mIdtEntryTable, (VOID *) Idtr.Base, Idtr.Limit + 1);\r
+    //\r
+    // Load new IDT table\r
+    //\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
@@ -244,16 +288,26 @@ SetupDebugAgentEnviroment (
   //\r
   InitializeDebugIdt ();\r
 \r
-  if (Mailbox != NULL) {\r
-    //\r
-    // If Mailbox exists, copy it into one global variable,\r
-    //\r
-    CopyMem (&mMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
-  } else {\r
-    ZeroMem (&mMailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
-  }  \r
+  //\r
+  // If mMailboxPointer is not set before, set it\r
+  //\r
+  if (mMailboxPointer == NULL) {\r
+    if (Mailbox != NULL) {\r
+      //\r
+      // If Mailbox exists, copy it into one global variable\r
+      //\r
+      CopyMem (&mMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
+    } else {\r
+      ZeroMem (&mMailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
+    }\r
+    mMailboxPointer = &mMailbox;\r
+  }\r
 \r
-  mMailboxPointer = &mMailbox;\r
+  //\r
+  // Initialize Debug Timer hardware and save its initial count and frequency\r
+  //\r
+  mDebugMpContext.DebugTimerInitCount = InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
+  UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
   //\r
   // Initialize debug communication port\r
   //\r
@@ -282,7 +336,7 @@ SetupDebugAgentEnviroment (
 /**\r
   Initialize debug agent.\r
 \r
-  This function is used to set up debug enviroment for DXE phase.\r
+  This function is used to set up debug environment for DXE phase.\r
 \r
   If this function is called by DXE Core, Context must be the pointer\r
   to HOB list which will be used to get GUIDed HOB. It will enable\r
@@ -311,8 +365,19 @@ InitializeDebugAgent (
   IA32_DESCRIPTOR              IdtDescriptor;\r
   IA32_DESCRIPTOR              *Ia32Idtr;\r
   IA32_IDT_ENTRY               *Ia32IdtEntry;\r
+  BOOLEAN                      PeriodicMode;\r
+  UINTN                        TimerCycle;\r
 \r
   if (InitFlag == DEBUG_AGENT_INIT_DXE_AP) {\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
     //\r
     // Invoked by AP, enable interrupt to let AP could receive IPI from other processors\r
     //\r
@@ -320,7 +385,7 @@ InitializeDebugAgent (
     return ;\r
   }\r
 \r
-  // \r
+  //\r
   // Disable Debug Timer interrupt\r
   //\r
   SaveAndSetDebugTimerInterrupt (FALSE);\r
@@ -354,30 +419,30 @@ InitializeDebugAgent (
     mSaveIdtTableSize = IdtDescriptor.Limit + 1;\r
     mSavedIdtTable    = AllocateCopyPool (mSaveIdtTableSize, (VOID *) IdtDescriptor.Base);\r
     //\r
-    // Initialize Debug Timer hardware\r
-    //\r
-    InitializeDebugTimer ();\r
-    //\r
     // Check if Debug Agent initialized in DXE phase\r
     //\r
     Mailbox = GetMailboxFromConfigurationTable ();\r
     if (Mailbox == NULL) {\r
       //\r
-      // Try to get mailbox from GUIDed HOB build in PEI \r
+      // Try to get mailbox from GUIDed HOB build in PEI\r
       //\r
       HobList = GetHobList ();\r
       Mailbox = GetMailboxFromHob (HobList);\r
     }\r
     //\r
-    // Set up IDT table and prepare for IDT entries\r
+    // Set up Debug Agent Environment and try to connect HOST if required\r
     //\r
-    SetupDebugAgentEnviroment (Mailbox);\r
+    SetupDebugAgentEnvironment (Mailbox);\r
     //\r
     // For DEBUG_AGENT_INIT_S3, needn't to install configuration table and EFI Serial IO protocol\r
     // For DEBUG_AGENT_INIT_DXE_CORE, InternalConstructorWorker() will invoked in Constructor()\r
     //\r
     InternalConstructorWorker ();\r
     //\r
+    // Enable Debug Timer interrupt\r
+    //\r
+    SaveAndSetDebugTimerInterrupt (TRUE);\r
+    //\r
     // Enable interrupt to receive Debug Timer interrupt\r
     //\r
     EnableInterrupts ();\r
@@ -387,41 +452,13 @@ InitializeDebugAgent (
 \r
     *(EFI_STATUS *)Context = EFI_SUCCESS;\r
 \r
-    if (gST->ConOut != NULL) {\r
-      Print (L"Debug Agent: Initialized successfully!\r\n");\r
-      Print (L"If the Debug Port is serial port, please make sure this serial port isn't connected by ISA Serial driver\r\n");\r
-      Print (L"You could do the following steps to disconnect the serial port:\r\n");\r
-      Print (L"1: Shell> drivers\r\n");\r
-      Print (L"   ...\r\n");\r
-      Print (L"   V  VERSION  E G G #D #C DRIVER NAME                         IMAGE NAME\r\n");\r
-      Print (L"   == ======== = = = == == =================================== ===================\r\n");\r
-      Print (L"   8F 0000000A B - -  1 14 PCI Bus Driver                      PciBusDxe\r\n");\r
-      Print (L"   91 00000010 ? - -  -  - ATA Bus Driver                      AtaBusDxe\r\n");\r
-      Print (L"   ...\r\n");\r
-      Print (L"   A7 0000000A B - -  1  1 ISA Serial Driver                   IsaSerialDxe\r\n");\r
-      Print (L"   ...\r\n");\r
-      Print (L"2: Shell> dh -d A7\r\n");\r
-      Print (L"   A7: Image(IsaSerialDxe) ImageDevPath (..9FB3-11D4-9A3A-0090273FC14D))DriverBinding ComponentName ComponentName2\r\n");\r
-      Print (L"        Driver Name    : ISA Serial Driver\r\n");\r
-      Print (L"        Image Name     : FvFile(93B80003-9FB3-11D4-9A3A-0090273FC14D)\r\n");\r
-      Print (L"        Driver Version : 0000000A\r\n");\r
-      Print (L"        Driver Type    : BUS\r\n");\r
-      Print (L"        Configuration  : NO\r\n");\r
-      Print (L"        Diagnostics    : NO\r\n");\r
-      Print (L"        Managing       :\r\n");\r
-      Print (L"          Ctrl[EA] : PciRoot(0x0)/Pci(0x1F,0x0)/Serial(0x0)\r\n");\r
-      Print (L"            Child[EB] : PciRoot(0x0)/Pci(0x1F,0x0)/Serial(0x0)/Uart(115200,8,N,1)\r\n");\r
-      Print (L"3: Shell> disconnect EA\r\n");\r
-      Print (L"4: Shell> load -nc DebugAgentDxe.efi\r\n\r\n");\r
-    }\r
     break;\r
 \r
   case DEBUG_AGENT_INIT_DXE_UNLOAD:\r
     if (mDebugAgentInitialized) {\r
       if (IsHostAttached ()) {\r
-        Print (L"Debug Agent: Host is still connected, please de-attach TARGET firstly!\r\n");\r
         *(EFI_STATUS *)Context = EFI_ACCESS_DENIED;\r
-        // \r
+        //\r
         // Enable Debug Timer interrupt again\r
         //\r
         SaveAndSetDebugTimerInterrupt (TRUE);\r
@@ -438,7 +475,6 @@ InitializeDebugAgent (
         *(EFI_STATUS *)Context = EFI_SUCCESS;\r
       }\r
     } else {\r
-      Print (L"Debug Agent: It hasn't been initialized, cannot unload it!\r\n");\r
       *(EFI_STATUS *)Context = EFI_NOT_STARTED;\r
     }\r
 \r
@@ -452,18 +488,18 @@ InitializeDebugAgent (
     mDxeCoreFlag                = TRUE;\r
     mMultiProcessorDebugSupport = TRUE;\r
     //\r
-    // Initialize Debug Timer hardware\r
-    //\r
-    InitializeDebugTimer ();\r
-    //\r
-    // Try to get mailbox from GUIDed HOB build in PEI \r
+    // Try to get mailbox from GUIDed HOB build in PEI\r
     //\r
     HobList = Context;\r
     Mailbox = GetMailboxFromHob (HobList);\r
     //\r
-    // Set up IDT table and prepare for IDT entries\r
+    // Set up Debug Agent Environment and try to connect HOST if required\r
+    //\r
+    SetupDebugAgentEnvironment (Mailbox);\r
     //\r
-    SetupDebugAgentEnviroment (Mailbox);\r
+    // Enable Debug Timer interrupt\r
+    //\r
+    SaveAndSetDebugTimerInterrupt (TRUE);\r
     //\r
     // Enable interrupt to receive Debug Timer interrupt\r
     //\r
@@ -476,15 +512,19 @@ InitializeDebugAgent (
     if (Context != NULL) {\r
       Ia32Idtr =  (IA32_DESCRIPTOR *) Context;\r
       Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
-      MailboxLocation = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow + \r
-                                           (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
+      MailboxLocation = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
+                                   ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
       Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
       VerifyMailboxChecksum (Mailbox);\r
     }\r
     //\r
-    // Set up IDT table and prepare for IDT entries\r
+    // Save Mailbox pointer in global variable\r
+    //\r
+    mMailboxPointer = Mailbox;\r
+    //\r
+    // Set up Debug Agent Environment and try to connect HOST if required\r
     //\r
-    SetupDebugAgentEnviroment (Mailbox);\r
+    SetupDebugAgentEnvironment (Mailbox);\r
     //\r
     // Disable interrupt\r
     //\r
@@ -500,7 +540,7 @@ InitializeDebugAgent (
 \r
   default:\r
     //\r
-    // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this \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