]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugMp.c
UefiCpuPkg/LocalApic.h: Remove duplicated/conflicted definitions
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugMp.c
index 577c6f54c66552576e1c54dfd85e1adecea13ee0..d49d4ee32347f6481f0d320dbb94aeb03b1f57f4 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Multi-Processor support functions implementation.\r
 \r
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2016, 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
 \r
 #include "DebugAgent.h"\r
 \r
-DEBUG_MP_CONTEXT volatile  mDebugMpContext = {0,0,0,0,0,0,0,0,FALSE,FALSE};\r
+GLOBAL_REMOVE_IF_UNREFERENCED DEBUG_MP_CONTEXT volatile  mDebugMpContext = {0,0,0,{0},{0},0,0,0,0,FALSE,FALSE};\r
 \r
-DEBUG_CPU_DATA volatile  mDebugCpuData = {0};\r
+GLOBAL_REMOVE_IF_UNREFERENCED DEBUG_CPU_DATA volatile  mDebugCpuData = {0};\r
 \r
 /**\r
-  Acquire access control on debug port.\r
+  Acquire a spin lock when Multi-processor supported.\r
 \r
   It will block in the function if cannot get the access control.\r
+  If Multi-processor is not supported, return directly.\r
+\r
+  @param[in, out] MpSpinLock      A pointer to the spin lock.\r
 \r
 **/\r
 VOID\r
-AcquireDebugPortControl (\r
-  VOID\r
+AcquireMpSpinLock (\r
+  IN OUT SPIN_LOCK           *MpSpinLock\r
   )\r
 {\r
-  if (!MultiProcessorDebugSupport) {\r
+  if (!MultiProcessorDebugSupport()) {\r
     return;\r
   }\r
 \r
   while (TRUE) {\r
-    if (AcquireSpinLockOrFail (&mDebugMpContext.DebugPortSpinLock)) {\r
+    if (AcquireSpinLockOrFail (MpSpinLock)) {\r
       break;\r
     }\r
     CpuPause ();\r
@@ -43,51 +46,21 @@ AcquireDebugPortControl (
 }\r
 \r
 /**\r
-  Release access control on debug port.\r
-\r
-**/\r
-VOID\r
-ReleaseDebugPortControl (\r
-  VOID\r
-  )\r
-{\r
-  if (!MultiProcessorDebugSupport) {\r
-    return;\r
-  }\r
-\r
-  ReleaseSpinLock (&mDebugMpContext.DebugPortSpinLock);\r
-}\r
-\r
-/**\r
-  Acquire access control on MP context.\r
+  Release a spin lock when Multi-processor supported.\r
 \r
-  It will block in the function if cannot get the access control.\r
+  @param[in, out] MpSpinLock      A pointer to the spin lock.\r
 \r
 **/\r
 VOID\r
-AcquireMpContextControl (\r
-  VOID\r
+ReleaseMpSpinLock (\r
+  IN OUT SPIN_LOCK           *MpSpinLock\r
   )\r
 {\r
-  while (TRUE) {\r
-    if (AcquireSpinLockOrFail (&mDebugMpContext.MpContextSpinLock)) {\r
-      break;\r
-    }\r
-    CpuPause ();\r
-    continue;\r
+  if (!MultiProcessorDebugSupport()) {\r
+    return;\r
   }\r
-}\r
 \r
-/**\r
-  Release access control on MP context.\r
-\r
-**/\r
-VOID\r
-ReleaseMpContextControl (\r
-  VOID\r
-  )\r
-{\r
-  ReleaseSpinLock (&mDebugMpContext.MpContextSpinLock);\r
+  ReleaseSpinLock (MpSpinLock);\r
 }\r
 \r
 /**\r
@@ -101,7 +74,7 @@ HaltOtherProcessors (
   IN UINT32             CurrentProcessorIndex\r
   )\r
 {\r
-\r
+  DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Try to halt other processors.\n", CurrentProcessorIndex);\r
   if (!IsBsp (CurrentProcessorIndex)) {\r
     SetIpiSentByApFlag (TRUE);;\r
   }\r
@@ -117,7 +90,7 @@ HaltOtherProcessors (
   // Send fixed IPI to other processors.\r
   //\r
   SendFixedIpiAllExcludingSelf (DEBUG_TIMER_VECTOR);\r
-  \r
+\r
 }\r
 \r
 /**\r
@@ -136,7 +109,7 @@ GetProcessorIndex (
 \r
   LocalApicID = (UINT16) GetApicId ();\r
 \r
-  AcquireMpContextControl ();\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 \r
   for (Index = 0; Index < mDebugCpuData.CpuCount; Index ++) {\r
     if (mDebugCpuData.ApicID[Index] == LocalApicID) {\r
@@ -149,7 +122,7 @@ GetProcessorIndex (
     mDebugCpuData.CpuCount ++ ;\r
   }\r
 \r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 \r
   return Index;\r
 }\r
@@ -165,14 +138,26 @@ GetProcessorIndex (
 **/\r
 BOOLEAN\r
 IsBsp (\r
-  IN UINT32             ProcessorIndex\r
+  IN UINT32  ProcessorIndex\r
   )\r
 {\r
-  if (AsmMsrBitFieldRead64 (MSR_IA32_APIC_BASE_ADDRESS, 8, 8) == 1) {\r
+  MSR_IA32_APIC_BASE_REGISTER  MsrApicBase;\r
+  \r
+  //\r
+  // If there are less than 2 CPUs detected, then the currently executing CPU\r
+  // must be the BSP.  This avoids an access to an MSR that may not be supported \r
+  // on single core CPUs.\r
+  //\r
+  if (mDebugCpuData.CpuCount < 2) {\r
+    return TRUE;\r
+  }\r
+\r
+  MsrApicBase.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
+  if (MsrApicBase.Bits.BSP == 1) {\r
     if (mDebugMpContext.BspIndex != ProcessorIndex) {\r
-      AcquireMpContextControl ();\r
+      AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
       mDebugMpContext.BspIndex = ProcessorIndex;\r
-      ReleaseMpContextControl ();\r
+      ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
     }\r
     return TRUE;\r
   } else {\r
@@ -197,7 +182,7 @@ SetCpuStopFlagByIndex (
   UINT8                 Value;\r
   UINTN                 Index;\r
 \r
-  AcquireMpContextControl ();\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 \r
   Value = mDebugMpContext.CpuStopStatusMask[ProcessorIndex / 8];\r
   Index = ProcessorIndex % 8;\r
@@ -208,7 +193,7 @@ SetCpuStopFlagByIndex (
   }\r
   mDebugMpContext.CpuStopStatusMask[ProcessorIndex / 8] = Value;\r
 \r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
@@ -228,7 +213,7 @@ SetCpuBreakFlagByIndex (
   UINT8                 Value;\r
   UINTN                 Index;\r
 \r
-  AcquireMpContextControl ();\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 \r
   Value = mDebugMpContext.CpuBreakMask[ProcessorIndex / 8];\r
   Index = ProcessorIndex % 8;\r
@@ -239,7 +224,7 @@ SetCpuBreakFlagByIndex (
   }\r
   mDebugMpContext.CpuBreakMask[ProcessorIndex / 8] = Value;\r
 \r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
@@ -279,11 +264,9 @@ SetCpuRunningFlag (
   IN BOOLEAN            RunningFlag\r
   )\r
 {\r
-  AcquireMpContextControl ();\r
-\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
   mDebugMpContext.RunCommandSet = RunningFlag;\r
-\r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
@@ -297,15 +280,13 @@ SetDebugViewPoint (
   IN UINT32             ProcessorIndex\r
   )\r
 {\r
-  AcquireMpContextControl ();\r
-\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
   mDebugMpContext.ViewPointIndex = ProcessorIndex;\r
-\r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
-  Initialize debug timer.\r
+  Set the IPI send by BPS/AP flag.\r
 \r
   @param[in] IpiSentByApFlag   TRUE means this IPI is sent by AP.\r
                                FALSE means this IPI is sent by BSP.\r
@@ -316,15 +297,13 @@ SetIpiSentByApFlag (
   IN BOOLEAN            IpiSentByApFlag\r
   )\r
 {\r
-  AcquireMpContextControl ();\r
-\r
+  AcquireMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
   mDebugMpContext.IpiSentByAp = IpiSentByApFlag;\r
-\r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
-  Check if any processor breaks.\r
+  Check the next pending breaking CPU.\r
 \r
   @retval others      There is at least one processor broken, the minimum\r
                       index number of Processor returned.\r
@@ -332,12 +311,12 @@ SetIpiSentByApFlag (
 \r
 **/\r
 UINT32\r
-FindCpuNotRunning (\r
+FindNextPendingBreakCpu (\r
   VOID\r
   )\r
 {\r
   UINT32               Index;\r
-  \r
+\r
   for (Index = 0; Index < DEBUG_CPU_MAX_COUNT / 8; Index ++) {\r
     if (mDebugMpContext.CpuBreakMask[Index] != 0) {\r
       return  (UINT32) LowBitSet32 (mDebugMpContext.CpuBreakMask[Index]) + Index * 8;\r
@@ -345,7 +324,7 @@ FindCpuNotRunning (
   }\r
   return (UINT32)-1;\r
 }\r
-  \r
+\r
 /**\r
   Check if all processors are in running status.\r
 \r
@@ -359,7 +338,7 @@ IsAllCpuRunning (
   )\r
 {\r
   UINTN              Index;\r
-  \r
+\r
   for (Index = 0; Index < DEBUG_CPU_MAX_COUNT / 8; Index ++) {\r
     if (mDebugMpContext.CpuStopStatusMask[Index] != 0) {\r
       return FALSE;\r
@@ -368,3 +347,37 @@ IsAllCpuRunning (
   return TRUE;\r
 }\r
 \r
+/**\r
+  Check if the current processor is the first breaking processor.\r
+\r
+  If yes, halt other processors.\r
+\r
+  @param[in] ProcessorIndex   Processor index value.\r
+\r
+  @return TRUE       This processor is the first breaking processor.\r
+  @return FALSE      This processor is not the first breaking processor.\r
+\r
+**/\r
+BOOLEAN\r
+IsFirstBreakProcessor (\r
+  IN UINT32              ProcessorIndex\r
+  )\r
+{\r
+  if (MultiProcessorDebugSupport()) {\r
+    if (mDebugMpContext.BreakAtCpuIndex != (UINT32) -1) {\r
+      //\r
+      // The current processor is not the first breaking one.\r
+      //\r
+      SetCpuBreakFlagByIndex (ProcessorIndex, TRUE);\r
+      return FALSE;\r
+    } else {\r
+      //\r
+      // If no any processor breaks, try to halt other processors\r
+      //\r
+      HaltOtherProcessors (ProcessorIndex);\r
+      return TRUE;\r
+    }\r
+  }\r
+  return TRUE;\r
+}\r
+\r