548bcb
commit 01a5746b6c8a44dc29d33e056b63485075a6a3cc
548bcb
Author: Florian Weimer <fweimer@redhat.com>
548bcb
Date:   Wed Feb 24 13:12:04 2021 +0100
548bcb
548bcb
    x86: Add CPU-specific diagnostics to ld.so --list-diagnostics
548bcb
548bcb
Conflicts:
548bcb
	sysdeps/x86/dl-diagnostics-cpu.c
548bcb
	  (reworked due to struct differences, different knobs
548bcb
	  downstream)
548bcb
548bcb
diff --git a/sysdeps/x86/dl-diagnostics-cpu.c b/sysdeps/x86/dl-diagnostics-cpu.c
548bcb
new file mode 100644
548bcb
index 0000000000000000..0ba286a828b69937
548bcb
--- /dev/null
548bcb
+++ b/sysdeps/x86/dl-diagnostics-cpu.c
548bcb
@@ -0,0 +1,101 @@
548bcb
+/* Print CPU diagnostics data in ld.so.  x86 version.
548bcb
+   Copyright (C) 2021 Free Software Foundation, Inc.
548bcb
+   This file is part of the GNU C Library.
548bcb
+
548bcb
+   The GNU C Library is free software; you can redistribute it and/or
548bcb
+   modify it under the terms of the GNU Lesser General Public
548bcb
+   License as published by the Free Software Foundation; either
548bcb
+   version 2.1 of the License, or (at your option) any later version.
548bcb
+
548bcb
+   The GNU C Library is distributed in the hope that it will be useful,
548bcb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
548bcb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
548bcb
+   Lesser General Public License for more details.
548bcb
+
548bcb
+   You should have received a copy of the GNU Lesser General Public
548bcb
+   License along with the GNU C Library; if not, see
548bcb
+   <https://www.gnu.org/licenses/>.  */
548bcb
+
548bcb
+#include <dl-diagnostics.h>
548bcb
+#include <ldsodefs.h>
548bcb
+
548bcb
+static void
548bcb
+print_cpu_features_value (const char *label, uint64_t value)
548bcb
+{
548bcb
+  _dl_printf ("x86.cpu_features.");
548bcb
+  _dl_diagnostics_print_labeled_value (label, value);
548bcb
+}
548bcb
+
548bcb
+static void
548bcb
+print_cpu_feature_internal (unsigned int index, const char *kind,
548bcb
+                            unsigned int reg, uint32_t value)
548bcb
+{
548bcb
+  _dl_printf ("x86.cpu_features.features[0x%x].%s[0x%x]=0x%x\n",
548bcb
+              index, kind, reg, value);
548bcb
+}
548bcb
+
548bcb
+static void
548bcb
+print_cpu_feature_preferred (const char *label, unsigned int flag)
548bcb
+{
548bcb
+  _dl_printf("x86.cpu_features.preferred.%s=0x%x\n", label, flag);
548bcb
+}
548bcb
+
548bcb
+void
548bcb
+_dl_diagnostics_cpu (void)
548bcb
+{
548bcb
+  const struct cpu_features *cpu_features = __get_cpu_features ();
548bcb
+
548bcb
+  print_cpu_features_value ("basic.kind", cpu_features->basic.kind);
548bcb
+  print_cpu_features_value ("basic.max_cpuid", cpu_features->basic.max_cpuid);
548bcb
+  print_cpu_features_value ("basic.family", cpu_features->basic.family);
548bcb
+  print_cpu_features_value ("basic.model", cpu_features->basic.model);
548bcb
+  print_cpu_features_value ("basic.stepping", cpu_features->basic.stepping);
548bcb
+
548bcb
+  for (unsigned int index = 0; index < COMMON_CPUID_INDEX_MAX; ++index)
548bcb
+    {
548bcb
+      /* Downstream, these constants are not part of the ABI yet, so
548bcb
+         analysis needs to take the precise glibc version into
548bcb
+         account.  */
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "cpuid", 0, cpu_features->features[index].cpuid.eax);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "cpuid", 1, cpu_features->features[index].cpuid.ebx);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "cpuid", 2, cpu_features->features[index].cpuid.ecx);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "cpuid", 3, cpu_features->features[index].cpuid.edx);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "usable", 0, cpu_features->features[index].usable.eax);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "usable", 1, cpu_features->features[index].usable.ebx);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "usable", 2, cpu_features->features[index].usable.ecx);
548bcb
+      print_cpu_feature_internal
548bcb
+        (index, "usable", 3, cpu_features->features[index].usable.edx);
548bcb
+    }
548bcb
+
548bcb
+  /* The preferred indicators are not part of the ABI and need to be
548bcb
+     translated.  */
548bcb
+#define BIT(x) \
548bcb
+  print_cpu_feature_preferred (#x, CPU_FEATURE_PREFERRED_P (cpu_features, x));
548bcb
+#include "cpu-features-preferred_feature_index_1.def"
548bcb
+#undef BIT
548bcb
+
548bcb
+  print_cpu_features_value ("xsave_state_size",
548bcb
+                            cpu_features->xsave_state_size);
548bcb
+  print_cpu_features_value ("xsave_state_full_size",
548bcb
+                            cpu_features->xsave_state_full_size);
548bcb
+  print_cpu_features_value ("data_cache_size", cpu_features->data_cache_size);
548bcb
+  print_cpu_features_value ("shared_cache_size",
548bcb
+                            cpu_features->shared_cache_size);
548bcb
+  print_cpu_features_value ("non_temporal_threshold",
548bcb
+                            cpu_features->non_temporal_threshold);
548bcb
+  print_cpu_features_value ("rep_movsb_threshold",
548bcb
+                            cpu_features->rep_movsb_threshold);
548bcb
+  print_cpu_features_value ("rep_stosb_threshold",
548bcb
+                            cpu_features->rep_stosb_threshold);
548bcb
+  _Static_assert (offsetof (struct cpu_features, rep_stosb_threshold)
548bcb
+                  + sizeof (cpu_features->rep_stosb_threshold)
548bcb
+                  == sizeof (*cpu_features),
548bcb
+                  "last cpu_features field has been printed");
548bcb
+}
548bcb
diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h
548bcb
index f43e22f677b249a9..536643b209425198 100644
548bcb
--- a/sysdeps/x86/include/cpu-features.h
548bcb
+++ b/sysdeps/x86/include/cpu-features.h
548bcb
@@ -107,6 +107,8 @@ enum
548bcb
 # define bit_XTILECFG_state	(1u << 17)
548bcb
 # define bit_XTILEDATA_state	(1u << 18)
548bcb
 
548bcb
+/* NB: When adding new fields, update sysdeps/x86/dl-diagnostics-cpu.c
548bcb
+   to print them.  */
548bcb
 struct cpu_features
548bcb
 {
548bcb
   struct cpu_features_basic basic;