]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/MtrrLib: Fix a bug that may wrongly set memory <1MB to UC
authorRuiyu Ni <ruiyu.ni@intel.com>
Mon, 21 Jan 2019 15:16:22 +0000 (23:16 +0800)
committerRay Ni <ray.ni@intel.com>
Tue, 19 Feb 2019 09:14:34 +0000 (17:14 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1481

Today's MtrrLib contains a bug, for example:
 when the original cache setting is WB for [0xF_0000, 0xF_8000) and,
 a new request to set [0xF_0000, 0xF_4000) to WP,
 the cache setting for [0xF_4000, 0xF_8000) is reset to UC.

The reason is when MtrrLibSetBelow1MBMemoryAttribute() is called the
WorkingFixedSettings doesn't contain the actual MSR value stored in
hardware, but when writing the fixed MTRRs, the code logic assumes
WorkingFixedSettings contains the actual MSR value.

The new fix is to change MtrrLibSetBelow1MBMemoryAttribute() to
calculate the correct ClearMasks[] and OrMasks[], and use them
directly when writing the fixed MTRRs.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ray Ni <ray.ni@intel.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Chasel Chiu <chasel.chiu@intel.com>
UefiCpuPkg/Library/MtrrLib/MtrrLib.c

index 086f7ad8f051bb6cd02750bd8576b138a90db18b..590b91abf5d565cb8b730f0923fe4346cdce00b5 100644 (file)
@@ -5,7 +5,7 @@
     Most of services in this library instance are suggested to be invoked by BSP only,\r
     except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.\r
 \r
     Most of services in this library instance are suggested to be invoked by BSP only,\r
     except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.\r
 \r
-  Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2019, 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
   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
@@ -2099,8 +2099,8 @@ MtrrLibSetMemoryRanges (
   Set the below-1MB memory attribute to fixed MTRR buffer.\r
   Modified flag array indicates which fixed MTRR is modified.\r
 \r
   Set the below-1MB memory attribute to fixed MTRR buffer.\r
   Modified flag array indicates which fixed MTRR is modified.\r
 \r
-  @param [in, out] FixedSettings Fixed MTRR buffer.\r
-  @param [out]     Modified      Flag array indicating which MTRR is modified.\r
+  @param [in, out] ClearMasks    The bits (when set) to clear in the fixed MTRR MSR.\r
+  @param [in, out] OrMasks       The bits to set in the fixed MTRR MSR.\r
   @param [in]      BaseAddress   Base address.\r
   @param [in]      Length        Length.\r
   @param [in]      Type          Memory type.\r
   @param [in]      BaseAddress   Base address.\r
   @param [in]      Length        Length.\r
   @param [in]      Type          Memory type.\r
@@ -2111,8 +2111,8 @@ MtrrLibSetMemoryRanges (
 **/\r
 RETURN_STATUS\r
 MtrrLibSetBelow1MBMemoryAttribute (\r
 **/\r
 RETURN_STATUS\r
 MtrrLibSetBelow1MBMemoryAttribute (\r
-  IN OUT MTRR_FIXED_SETTINGS     *FixedSettings,\r
-  OUT BOOLEAN                    *Modified,\r
+  IN OUT UINT64                  *ClearMasks,\r
+  IN OUT UINT64                  *OrMasks,\r
   IN PHYSICAL_ADDRESS            BaseAddress,\r
   IN UINT64                      Length,\r
   IN MTRR_MEMORY_CACHE_TYPE      Type\r
   IN PHYSICAL_ADDRESS            BaseAddress,\r
   IN UINT64                      Length,\r
   IN MTRR_MEMORY_CACHE_TYPE      Type\r
@@ -2122,36 +2122,17 @@ MtrrLibSetBelow1MBMemoryAttribute (
   UINT32                    MsrIndex;\r
   UINT64                    ClearMask;\r
   UINT64                    OrMask;\r
   UINT32                    MsrIndex;\r
   UINT64                    ClearMask;\r
   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
 \r
   ASSERT (BaseAddress < BASE_1MB);\r
 \r
-  SetMem (LocalModified, sizeof (LocalModified), FALSE);\r
-\r
-  //\r
-  // (Value & ~0 | 0) still equals to (Value)\r
-  //\r
-  SetMem (ClearMasks, sizeof (ClearMasks), 0);\r
-  SetMem (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
   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
-    LocalModified[MsrIndex] = TRUE;\r
-  }\r
-\r
-  for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mMtrrLibFixedMtrrTable); MsrIndex++) {\r
-    if (LocalModified[MsrIndex]) {\r
-      FixedSettings->Mtrr[MsrIndex] = (FixedSettings->Mtrr[MsrIndex] & ~ClearMasks[MsrIndex]) | OrMasks[MsrIndex];\r
-    }\r
+    ClearMasks[MsrIndex] = ClearMasks[MsrIndex] | ClearMask;\r
+    OrMasks[MsrIndex]    = (OrMasks[MsrIndex] & ~ClearMask) | OrMask;\r
   }\r
   return RETURN_SUCCESS;\r
 }\r
   }\r
   return RETURN_SUCCESS;\r
 }\r
@@ -2213,8 +2194,8 @@ MtrrSetMemoryAttributesInMtrrSettings (
   MTRR_MEMORY_RANGE         WorkingVariableMtrr[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];\r
   BOOLEAN                   VariableSettingModified[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];\r
 \r
   MTRR_MEMORY_RANGE         WorkingVariableMtrr[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];\r
   BOOLEAN                   VariableSettingModified[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];\r
 \r
-  BOOLEAN                   FixedSettingsModified[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
-  MTRR_FIXED_SETTINGS       WorkingFixedSettings;\r
+  UINT64                    ClearMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
+  UINT64                    OrMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];\r
 \r
   MTRR_CONTEXT              MtrrContext;\r
   BOOLEAN                   MtrrContextValid;\r
 \r
   MTRR_CONTEXT              MtrrContext;\r
   BOOLEAN                   MtrrContextValid;\r
@@ -2226,10 +2207,6 @@ MtrrSetMemoryAttributesInMtrrSettings (
   // TRUE indicating the accordingly Variable setting needs modificaiton in OriginalVariableMtrr.\r
   //\r
   SetMem (VariableSettingModified, ARRAY_SIZE (VariableSettingModified), FALSE);\r
   // TRUE indicating the accordingly Variable setting needs modificaiton in OriginalVariableMtrr.\r
   //\r
   SetMem (VariableSettingModified, ARRAY_SIZE (VariableSettingModified), FALSE);\r
-  //\r
-  // TRUE indicating the accordingly Fixed setting needs modification in WorkingFixedSettings.\r
-  //\r
-  SetMem (FixedSettingsModified, ARRAY_SIZE (FixedSettingsModified), FALSE);\r
 \r
   //\r
   // TRUE indicating the caller requests to set variable MTRRs.\r
 \r
   //\r
   // TRUE indicating the caller requests to set variable MTRRs.\r
@@ -2405,14 +2382,17 @@ MtrrSetMemoryAttributesInMtrrSettings (
   //\r
   // 3. Apply the below-1MB memory attribute settings.\r
   //\r
   //\r
   // 3. Apply the below-1MB memory attribute settings.\r
   //\r
-  ZeroMem (WorkingFixedSettings.Mtrr, sizeof (WorkingFixedSettings.Mtrr));\r
+  // (Value & ~0 | 0) still equals to (Value)\r
+  //\r
+  ZeroMem (ClearMasks, sizeof (ClearMasks));\r
+  ZeroMem (OrMasks, sizeof (OrMasks));\r
   for (Index = 0; Index < RangeCount; Index++) {\r
     if (Ranges[Index].BaseAddress >= BASE_1MB) {\r
       continue;\r
     }\r
 \r
     Status = MtrrLibSetBelow1MBMemoryAttribute (\r
   for (Index = 0; Index < RangeCount; Index++) {\r
     if (Ranges[Index].BaseAddress >= BASE_1MB) {\r
       continue;\r
     }\r
 \r
     Status = MtrrLibSetBelow1MBMemoryAttribute (\r
-               &WorkingFixedSettings, FixedSettingsModified,\r
+               ClearMasks, OrMasks,\r
                Ranges[Index].BaseAddress, Ranges[Index].Length, Ranges[Index].Type\r
                );\r
     if (RETURN_ERROR (Status)) {\r
                Ranges[Index].BaseAddress, Ranges[Index].Length, Ranges[Index].Type\r
                );\r
     if (RETURN_ERROR (Status)) {\r
@@ -2424,19 +2404,16 @@ MtrrSetMemoryAttributesInMtrrSettings (
   //\r
   // 4. Write fixed MTRRs that have been modified\r
   //\r
   //\r
   // 4. Write fixed MTRRs that have been modified\r
   //\r
-  for (Index = 0; Index < ARRAY_SIZE (FixedSettingsModified); Index++) {\r
-    if (FixedSettingsModified[Index]) {\r
+  for (Index = 0; Index < ARRAY_SIZE (ClearMasks); Index++) {\r
+    if (ClearMasks[Index] != 0) {\r
       if (MtrrSetting != NULL) {\r
       if (MtrrSetting != NULL) {\r
-        MtrrSetting->Fixed.Mtrr[Index] = WorkingFixedSettings.Mtrr[Index];\r
+        MtrrSetting->Fixed.Mtrr[Index] = (MtrrSetting->Fixed.Mtrr[Index] & ~ClearMasks[Index]) | OrMasks[Index];\r
       } else {\r
         if (!MtrrContextValid) {\r
           MtrrLibPreMtrrChange (&MtrrContext);\r
           MtrrContextValid = TRUE;\r
         }\r
       } else {\r
         if (!MtrrContextValid) {\r
           MtrrLibPreMtrrChange (&MtrrContext);\r
           MtrrContextValid = TRUE;\r
         }\r
-        AsmWriteMsr64 (\r
-          mMtrrLibFixedMtrrTable[Index].Msr,\r
-          WorkingFixedSettings.Mtrr[Index]\r
-        );\r
+        AsmMsrAndThenOr64 (mMtrrLibFixedMtrrTable[Index].Msr, ~ClearMasks[Index], OrMasks[Index]);\r
       }\r
     }\r
   }\r
       }\r
     }\r
   }\r