446cf2
commit 04bba1e5d84b6fd8d3a3b006bc240cd5d241ee30
446cf2
Author: H.J. Lu <hjl.tools@gmail.com>
446cf2
Date:   Wed Aug 5 13:51:56 2020 -0700
446cf2
446cf2
    x86: Set CPU usable feature bits conservatively [BZ #26552]
446cf2
    
446cf2
    Set CPU usable feature bits only for CPU features which are usable in
446cf2
    user space and whose usability can be detected from user space, excluding
446cf2
    features like FSGSBASE whose enable bit can only be checked in the kernel.
446cf2
446cf2
diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c
446cf2
index f13a1df4555c7000..6954728c47d0126b 100644
446cf2
--- a/sysdeps/x86/cpu-features.c
446cf2
+++ b/sysdeps/x86/cpu-features.c
446cf2
@@ -44,107 +44,55 @@ extern void TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *)
446cf2
 static void
446cf2
 update_usable (struct cpu_features *cpu_features)
446cf2
 {
446cf2
-  /* Before COMMON_CPUID_INDEX_80000001, copy the cpuid array elements to
446cf2
-     the usable array.  */
446cf2
-  unsigned int i;
446cf2
-  for (i = 0; i < COMMON_CPUID_INDEX_80000001; i++)
446cf2
-    cpu_features->features[i].usable = cpu_features->features[i].cpuid;
446cf2
-
446cf2
-  /* Before COMMON_CPUID_INDEX_80000001, clear the unknown usable bits
446cf2
-     and the always zero bits.  */
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_1_ECX_16);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_1_ECX_31);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_1_EDX_10);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_1_EDX_20);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_1_EDX_30);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EBX_6);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EBX_22);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_13);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_15);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_16);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_23);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_24);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_ECX_26);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_0);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_1);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_5);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_6);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_7);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_9);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_11);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_12);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_13);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_17);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_19);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_21);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, INDEX_7_EDX_23);
446cf2
-
446cf2
-  /* EAX/EBX from COMMON_CPUID_INDEX_1 and EAX from COMMON_CPUID_INDEX_7
446cf2
-     aren't used for CPU feature detection.  */
446cf2
-  cpu_features->features[COMMON_CPUID_INDEX_1].usable.eax = 0;
446cf2
-  cpu_features->features[COMMON_CPUID_INDEX_1].usable.ebx = 0;
446cf2
-  cpu_features->features[COMMON_CPUID_INDEX_7].usable.eax = 0;
446cf2
-
446cf2
-  /* Starting from COMMON_CPUID_INDEX_80000001, copy the cpuid bits to
446cf2
-     usable bits.  */
446cf2
+  /* Copy the cpuid bits to usable bits for CPU featuress whose usability
446cf2
+     in user space can be detected without additonal OS support.  */
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSE3);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, PCLMULQDQ);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSSE3);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CMPXCHG16B);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSE4_1);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSE4_2);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, MOVBE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, POPCNT);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, AES);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, OSXSAVE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, TSC);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CX8);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CMOV);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CLFSH);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, MMX);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, FXSR);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SSE2);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, HTT);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, BMI1);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, HLE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, BMI2);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, ERMS);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, RTM);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, RDSEED);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, ADX);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CLFLUSHOPT);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CLWB);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SHA);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, PREFETCHWT1);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, OSPKE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, WAITPKG);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, GFNI);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, RDPID);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, CLDEMOTE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, MOVDIRI);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, MOVDIR64B);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, FSRM);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, SERIALIZE);
446cf2
+  CPU_FEATURE_SET_USABLE (cpu_features, TSXLDTRK);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, LAHF64_SAHF64);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, SVM);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, LZCNT);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, SSE4A);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, PREFETCHW);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XOP);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, LWP);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, FMA4);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, TBM);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, SYSCALL_SYSRET);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, NX);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, PAGE1GB);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, RDTSCP);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, LM);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XSAVEOPT);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XSAVEC);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XGETBV_ECX_1);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XSAVES);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, XFD);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, INVARIANT_TSC);
446cf2
   CPU_FEATURE_SET_USABLE (cpu_features, WBNOINVD);
446cf2
-  CPU_FEATURE_SET_USABLE (cpu_features, AVX512_BF16);
446cf2
-
446cf2
-  /* MPX has been deprecated.  */
446cf2
-  CPU_FEATURE_UNSET (cpu_features, MPX);
446cf2
-
446cf2
-  /* Clear the usable bits which require OS support.  */
446cf2
-  CPU_FEATURE_UNSET (cpu_features, FMA);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, F16C);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX2);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512F);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512DQ);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_IFMA);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512PF);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512ER);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512CD);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512BW);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512VL);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_VBMI);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, PKU);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_VBMI2);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, VAES);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, VPCLMULQDQ);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_VNNI);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_BITALG);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_VPOPCNTDQ);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_4VNNIW);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_4FMAPS);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_VP2INTERSECT);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AMX_BF16);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AMX_TILE);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AMX_INT8);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, XOP);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, FMA4);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, XSAVEC);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, XFD);
446cf2
-  CPU_FEATURE_UNSET (cpu_features, AVX512_BF16);
446cf2
 
446cf2
   /* Can we call xgetbv?  */
446cf2
   if (CPU_FEATURES_CPU_P (cpu_features, OSXSAVE))
446cf2
@@ -243,8 +191,11 @@ update_usable (struct cpu_features *cpu_features)
446cf2
 	  CPU_FEATURE_SET_USABLE (cpu_features, AMX_INT8);
446cf2
 	}
446cf2
 
446cf2
-
446cf2
-      /* XFD is usable only when OSXSAVE is enabled.  */
446cf2
+      /* These features are usable only when OSXSAVE is enabled.  */
446cf2
+      CPU_FEATURE_SET (cpu_features, XSAVE);
446cf2
+      CPU_FEATURE_SET_USABLE (cpu_features, XSAVEOPT);
446cf2
+      CPU_FEATURE_SET_USABLE (cpu_features, XSAVEC);
446cf2
+      CPU_FEATURE_SET_USABLE (cpu_features, XGETBV_ECX_1);
446cf2
       CPU_FEATURE_SET_USABLE (cpu_features, XFD);
446cf2
 
446cf2
       /* For _dl_runtime_resolve, set xsave_state_size to xsave area