On 12/13/2017 10:35 AM, Song, BinX wrote:
V2:
Update function name, add more detail description.
V1:
Check and assert invalid RegisterCpuFeature function parameter

Cc: Eric Dong <eric.d...@intel.com>
Cc: Laszlo Ersek <ler...@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Bell Song <binx.s...@intel.com>
---
  .../Include/Library/RegisterCpuFeaturesLib.h       |  5 ++++
  .../RegisterCpuFeaturesLib.c                       | 29 ++++++++++++++++++++++
  2 files changed, 34 insertions(+)

diff --git a/UefiCpuPkg/Include/Library/RegisterCpuFeaturesLib.h 
b/UefiCpuPkg/Include/Library/RegisterCpuFeaturesLib.h
index 9331e49..fc3ccda 100644
--- a/UefiCpuPkg/Include/Library/RegisterCpuFeaturesLib.h
+++ b/UefiCpuPkg/Include/Library/RegisterCpuFeaturesLib.h
@@ -71,6 +71,11 @@
  #define CPU_FEATURE_APIC_TPR_UPDATE_MESSAGE         (32+9)
  #define CPU_FEATURE_ENERGY_PERFORMANCE_BIAS         (32+10)
  #define CPU_FEATURE_PPIN                            (32+11)
+//
+// Currently, CPU_FEATURE_PROC_TRACE is the MAX feature we support.
+// If you define a feature bigger than it, please also replace it
+// in RegisterCpuFeatureLibIsFeatureValid function.
+//
  #define CPU_FEATURE_PROC_TRACE                      (32+12)
#define CPU_FEATURE_BEFORE_ALL BIT27
diff --git a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeaturesLib.c 
b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeaturesLib.c
index dd6a82b..6ec26e1 100644
--- a/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeaturesLib.c
+++ b/UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeaturesLib.c
@@ -81,6 +81,34 @@ DumpCpuFeature (
  }
/**
+  Determines if the CPU feature is valid.
+
+  @param[in]  Feature        Pointer to CPU feature
+
+  @retval TRUE  The CPU feature is valid.
+  @retval FALSE The CPU feature is invalid.
+**/
+BOOLEAN
+RegisterCpuFeatureLibIsFeatureValid (
+  IN UINT32        Feature
+  )
+{
+  UINT32      Data;
+
+  Data = Feature;
+  Data &= ~(CPU_FEATURE_BEFORE | CPU_FEATURE_AFTER | CPU_FEATURE_BEFORE_ALL | 
CPU_FEATURE_AFTER_ALL);
+  //
+  // Currently, CPU_FEATURE_PROC_TRACE is the MAX feature we support.
+  // If you define a feature bigger than it, please replace it at below.
+  //
+  if (Data > CPU_FEATURE_PROC_TRACE) {
+    DEBUG ((DEBUG_ERROR, "Invalid CPU feature: 0x%x ", Feature));
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/**
    Determines if the feature bit mask is in dependent CPU feature bit mask 
buffer.
@param[in] FeatureMask Pointer to CPU feature bit mask
@@ -444,6 +472,7 @@ RegisterCpuFeature (
VA_START (Marker, InitializeFunc);
    Feature = VA_ARG (Marker, UINT32);
+  ASSERT (RegisterCpuFeatureLibIsFeatureValid(Feature));
    while (Feature != CPU_FEATURE_END) {
      ASSERT ((Feature & (CPU_FEATURE_BEFORE | CPU_FEATURE_AFTER))
                      != (CPU_FEATURE_BEFORE | CPU_FEATURE_AFTER));

Reviewed-by: Ruiyu Ni <ruiyu...@intel.com>

--
Thanks,
Ray
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to