[edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting.

Yuanhao Xie posted 16 patches 2 months, 4 weeks ago
Only 11 patches received!
[edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting.
Posted by Yuanhao Xie 2 months, 4 weeks ago
From: Ray Ni <ray.ni@intel.com>

The patch fixes the following issues in the original implementation:

1. MtrrSetting contains random value if MTRR is not supported.
2. Unconditionally access fixed MTRR on CPU that may not support
    fixed MTRR.
3. The maximum number of Variable MTRR entries are initialized, while
the portion exceeding the maximum number remains uninitialized.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>

Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index bd61ffc240..c9440f01ef 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -2824,29 +2824,43 @@ MtrrGetAllMtrrs (
   OUT MTRR_SETTINGS  *MtrrSetting
   )
 {
-  if (!IsMtrrSupported ()) {
+  BOOLEAN                          FixedMtrrSupported;
+  UINT32                           VariableMtrrCount;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  *MtrrDefType;
+
+  ZeroMem (MtrrSetting, sizeof (*MtrrSetting));
+
+  MtrrDefType = (MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType;
+  if (!MtrrLibIsMtrrSupported (&FixedMtrrSupported, &VariableMtrrCount)) {
     return MtrrSetting;
   }
 
+  //
+  // Get MTRR_DEF_TYPE value
+  //
+  MtrrDefType->Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
+
+  //
+  // Enabling the Fixed MTRR bit when unsupported is not allowed.
+  //
+  ASSERT (FixedMtrrSupported || (MtrrDefType->Bits.FE == 0));
+
   //
   // Get fixed MTRRs
   //
-  MtrrGetFixedMtrrWorker (&MtrrSetting->Fixed);
+  if (MtrrDefType->Bits.FE == 1) {
+    MtrrGetFixedMtrrWorker (&MtrrSetting->Fixed);
+  }
 
   //
   // Get variable MTRRs
   //
   MtrrGetVariableMtrrWorker (
     NULL,
-    GetVariableMtrrCountWorker (),
+    VariableMtrrCount,
     &MtrrSetting->Variables
     );
 
-  //
-  // Get MTRR_DEF_TYPE value
-  //
-  MtrrSetting->MtrrDefType = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
-
   return MtrrSetting;
 }
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108562): https://edk2.groups.io/g/devel/message/108562
Mute This Topic: https://groups.io/mt/101331024/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-