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