]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MtrrLib/MtrrLib.c
UefiCpuPkg/MtrrLib: Fix bug that may incorrectly set <1MB attribute
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / MtrrLib.c
index a7adbafae397b215752f663ef90c9e6db3a7dd87..200becdd4a2500eb3145788bc2f97a19a47c3aae 100644 (file)
@@ -449,10 +449,13 @@ MtrrGetVariableMtrrWorker (
 \r
   for (Index = 0; Index < VariableMtrrCount; Index++) {\r
     if (MtrrSetting == NULL) {\r
-      VariableSettings->Mtrr[Index].Base =\r
-        AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1));\r
-      VariableSettings->Mtrr[Index].Mask =\r
-        AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1));\r
+      VariableSettings->Mtrr[Index].Mask = AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1));\r
+      //\r
+      // Skip to read the Base MSR when the Mask.V is not set.\r
+      //\r
+      if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&VariableSettings->Mtrr[Index].Mask)->Bits.V != 0) {\r
+        VariableSettings->Mtrr[Index].Base = AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1));\r
+      }\r
     } else {\r
       VariableSettings->Mtrr[Index].Base = MtrrSetting->Variables.Mtrr[Index].Base;\r
       VariableSettings->Mtrr[Index].Mask = MtrrSetting->Variables.Mtrr[Index].Mask;\r
@@ -2111,22 +2114,32 @@ MtrrLibSetBelow1MBMemoryAttribute (
   UINT64                    OrMask;\r
   UINT64                    ClearMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
   UINT64                    OrMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
+  BOOLEAN                   LocalModified[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
 \r
   ASSERT (BaseAddress < BASE_1MB);\r
 \r
+  SetMem (LocalModified, sizeof (LocalModified), FALSE);\r
+\r
+  //\r
+  // (Value & ~0 | 0) still equals to (Value)\r
+  //\r
+  SetMem64 (ClearMasks, sizeof (ClearMasks), 0);\r
+  SetMem64 (OrMasks, sizeof (OrMasks), 0);\r
+\r
   MsrIndex = (UINT32)-1;\r
   while ((BaseAddress < BASE_1MB) && (Length != 0)) {\r
     Status = MtrrLibProgramFixedMtrr (Type, &BaseAddress, &Length, &MsrIndex, &ClearMask, &OrMask);\r
     if (RETURN_ERROR (Status)) {\r
       return Status;\r
     }\r
-    ClearMasks[MsrIndex] = ClearMask;\r
-    OrMasks[MsrIndex]    = OrMask;\r
-    Modified[MsrIndex]   = TRUE;\r
+    ClearMasks[MsrIndex]    = ClearMask;\r
+    OrMasks[MsrIndex]       = OrMask;\r
+    Modified[MsrIndex]      = TRUE;\r
+    LocalModified[MsrIndex] = TRUE;\r
   }\r
 \r
   for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mMtrrLibFixedMtrrTable); MsrIndex++) {\r
-    if (Modified[MsrIndex]) {\r
+    if (LocalModified[MsrIndex]) {\r
       FixedSettings->Mtrr[MsrIndex] = (FixedSettings->Mtrr[MsrIndex] & ~ClearMasks[MsrIndex]) | OrMasks[MsrIndex];\r
     }\r
   }\r
@@ -2351,6 +2364,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
   //\r
   // 3. Apply the below-1MB memory attribute settings.\r
   //\r
+  ZeroMem (WorkingFixedSettings.Mtrr, sizeof (WorkingFixedSettings.Mtrr));\r
   for (Index = 0; Index < RangeCount; Index++) {\r
     if (Ranges[Index].BaseAddress >= BASE_1MB) {\r
       continue;\r
@@ -2540,14 +2554,14 @@ MtrrSetVariableMtrrWorker (
   ASSERT (VariableMtrrCount <= ARRAY_SIZE (VariableSettings->Mtrr));\r
 \r
   for (Index = 0; Index < VariableMtrrCount; Index++) {\r
-    AsmWriteMsr64 (\r
-      MSR_IA32_MTRR_PHYSBASE0 + (Index << 1),\r
-      VariableSettings->Mtrr[Index].Base\r
-      );\r
-    AsmWriteMsr64 (\r
-      MSR_IA32_MTRR_PHYSMASK0 + (Index << 1),\r
-      VariableSettings->Mtrr[Index].Mask\r
-      );\r
+    //\r
+    // Mask MSR is always updated since caller might need to invalidate the MSR pair.\r
+    // Base MSR is skipped when Mask.V is not set.\r
+    //\r
+    AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableSettings->Mtrr[Index].Mask);\r
+    if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&VariableSettings->Mtrr[Index].Mask)->Bits.V != 0) {\r
+      AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableSettings->Mtrr[Index].Base);\r
+    }\r
   }\r
 }\r
 \r
@@ -2773,6 +2787,7 @@ MtrrDebugPrintAllMtrrsWorker (
     UINTN             RangeCount;\r
     UINT64            MtrrValidBitsMask;\r
     UINT64            MtrrValidAddressMask;\r
+    UINT32            VariableMtrrCount;\r
     MTRR_MEMORY_RANGE Ranges[\r
       ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1\r
       ];\r
@@ -2782,6 +2797,8 @@ MtrrDebugPrintAllMtrrsWorker (
       return;\r
     }\r
 \r
+    VariableMtrrCount = GetVariableMtrrCountWorker ();\r
+\r
     if (MtrrSetting != NULL) {\r
       Mtrrs = MtrrSetting;\r
     } else {\r
@@ -2799,8 +2816,8 @@ MtrrDebugPrintAllMtrrsWorker (
       DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d]   : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index]));\r
     }\r
 \r
-    for (Index = 0; Index < ARRAY_SIZE (Mtrrs->Variables.Mtrr); Index++) {\r
-      if ((Mtrrs->Variables.Mtrr[Index].Mask & BIT11) == 0) {\r
+    for (Index = 0; Index < VariableMtrrCount; Index++) {\r
+      if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&Mtrrs->Variables.Mtrr[Index].Mask)->Bits.V == 0) {\r
         //\r
         // If mask is not valid, then do not display range\r
         //\r
@@ -2826,11 +2843,11 @@ MtrrDebugPrintAllMtrrsWorker (
     RangeCount = 1;\r
 \r
     MtrrLibGetRawVariableRanges (\r
-      &Mtrrs->Variables, ARRAY_SIZE (Mtrrs->Variables.Mtrr),\r
+      &Mtrrs->Variables, VariableMtrrCount,\r
       MtrrValidBitsMask, MtrrValidAddressMask, RawVariableRanges\r
       );\r
     MtrrLibApplyVariableMtrrs (\r
-      RawVariableRanges, ARRAY_SIZE (RawVariableRanges),\r
+      RawVariableRanges, VariableMtrrCount,\r
       Ranges, ARRAY_SIZE (Ranges), &RangeCount\r
       );\r
 \r