]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugMp.c
MdePkg/DebugAgent: Support IA32 processors without MSR_IA32_APIC_BASE_ADDRESS
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugMp.c
index 5275b096bb190c4a5ce8c5414d195a731e5fef71..bdb6742811ea03f47ffea623b8480f1ef9f800eb 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Multi-Processor support functions implementation.\r
 \r
-  Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2015, 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
+  Release a spin lock when Multi-processor supported.\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
-\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
@@ -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,23 @@ GetProcessorIndex (
 **/\r
 BOOLEAN\r
 IsBsp (\r
-  IN UINT32             ProcessorIndex\r
+  IN UINT32  ProcessorIndex\r
   )\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
   if (AsmMsrBitFieldRead64 (MSR_IA32_APIC_BASE_ADDRESS, 8, 8) == 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 +179,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 +190,7 @@ SetCpuStopFlagByIndex (
   }\r
   mDebugMpContext.CpuStopStatusMask[ProcessorIndex / 8] = Value;\r
 \r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
@@ -228,7 +210,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 +221,7 @@ SetCpuBreakFlagByIndex (
   }\r
   mDebugMpContext.CpuBreakMask[ProcessorIndex / 8] = Value;\r
 \r
-  ReleaseMpContextControl ();\r
+  ReleaseMpSpinLock (&mDebugMpContext.MpContextSpinLock);\r
 }\r
 \r
 /**\r
@@ -279,11 +261,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,11 +277,9 @@ 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
@@ -316,11 +294,9 @@ 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
@@ -337,7 +313,7 @@ FindNextPendingBreakCpu (
   )\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 +321,7 @@ FindNextPendingBreakCpu (
   }\r
   return (UINT32)-1;\r
 }\r
-  \r
+\r
 /**\r
   Check if all processors are in running status.\r
 \r
@@ -359,7 +335,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
@@ -371,20 +347,20 @@ IsAllCpuRunning (
 /**\r
   Check if the current processor is the first breaking processor.\r
 \r
-  If yes, halt other processors.  \r
-  \r
+  If yes, halt other processors.\r
+\r
   @param[in] ProcessorIndex   Processor index value.\r
-  \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
 **/\r
 BOOLEAN\r
 IsFirstBreakProcessor (\r
   IN UINT32              ProcessorIndex\r
   )\r
 {\r
-  if (MultiProcessorDebugSupport) {\r
+  if (MultiProcessorDebugSupport()) {\r
     if (mDebugMpContext.BreakAtCpuIndex != (UINT32) -1) {\r
       //\r
       // The current processor is not the first breaking one.\r
@@ -400,5 +376,5 @@ IsFirstBreakProcessor (
     }\r
   }\r
   return TRUE;\r
-} \r
+}\r
 \r