e354a5
commit f267e1c9dd7fb8852cc32d6eafd96bbcfd5cbb2b
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Fri Dec 4 09:13:43 2020 +0100
e354a5
e354a5
    x86_64: Add glibc-hwcaps support
e354a5
    
e354a5
    The subdirectories match those in the x86-64 psABI:
e354a5
    
e354a5
    https://gitlab.com/x86-psABIs/x86-64-ABI/-/commit/77566eb03bc6a326811cb7e9a6b9396884b67c7c
e354a5
    
e354a5
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
e354a5
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index f67b231c0f8e3aff..7f2fc73877f0a4c8 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -1751,7 +1751,7 @@ $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
e354a5
 # glibc-hwcaps mechanism for this architecture).  Used to obtain test
e354a5
 # coverage for some glibc-hwcaps tests for the widest possible range
e354a5
 # of systems.
e354a5
-glibc-hwcaps-first-subdirs-for-tests =
e354a5
+glibc-hwcaps-first-subdirs-for-tests = x86-64-v2
e354a5
 
e354a5
 # The test modules are parameterized by preprocessor macros.
e354a5
 LDFLAGS-libmarkermod1-1.so += -Wl,-soname,libmarkermod1.so
e354a5
diff --git a/elf/tst-glibc-hwcaps-cache.script b/elf/tst-glibc-hwcaps-cache.script
e354a5
index 6356d152089cdd9a..66d6942402b7233b 100644
e354a5
--- a/elf/tst-glibc-hwcaps-cache.script
e354a5
+++ b/elf/tst-glibc-hwcaps-cache.script
e354a5
@@ -4,3 +4,13 @@
e354a5
 cp $B/elf/libmarkermod2-1.so $L/libmarkermod2.so
e354a5
 cp $B/elf/libmarkermod3-1.so $L/libmarkermod3.so
e354a5
 cp $B/elf/libmarkermod4-1.so $L/libmarkermod4.so
e354a5
+
e354a5
+mkdirp 0770 $L/glibc-hwcaps/x86-64-v2
e354a5
+cp $B/elf/libmarkermod2-2.so $L/glibc-hwcaps/x86-64-v2/libmarkermod2.so
e354a5
+mkdirp 0770 $L/glibc-hwcaps/x86-64-v3
e354a5
+cp $B/elf/libmarkermod3-2.so $L/glibc-hwcaps/x86-64-v2/libmarkermod3.so
e354a5
+cp $B/elf/libmarkermod3-3.so $L/glibc-hwcaps/x86-64-v3/libmarkermod3.so
e354a5
+mkdirp 0770 $L/glibc-hwcaps/x86-64-v4
e354a5
+cp $B/elf/libmarkermod4-2.so $L/glibc-hwcaps/x86-64-v2/libmarkermod4.so
e354a5
+cp $B/elf/libmarkermod4-3.so $L/glibc-hwcaps/x86-64-v3/libmarkermod4.so
e354a5
+cp $B/elf/libmarkermod4-4.so $L/glibc-hwcaps/x86-64-v4/libmarkermod4.so
e354a5
diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
e354a5
index 42b97c5cc73892cc..d1d7cb9d2eeca9c5 100644
e354a5
--- a/sysdeps/x86_64/Makefile
e354a5
+++ b/sysdeps/x86_64/Makefile
e354a5
@@ -144,8 +144,47 @@ CFLAGS-tst-auditmod10b.c += $(AVX512-CFLAGS)
e354a5
 CFLAGS-tst-avx512-aux.c += $(AVX512-CFLAGS)
e354a5
 CFLAGS-tst-avx512mod.c += $(AVX512-CFLAGS)
e354a5
 endif
e354a5
+
e354a5
+$(objpfx)tst-glibc-hwcaps: $(objpfx)libmarkermod2-1.so \
e354a5
+  $(objpfx)libmarkermod3-1.so $(objpfx)libmarkermod4-1.so
e354a5
+$(objpfx)tst-glibc-hwcaps.out: \
e354a5
+  $(objpfx)libmarkermod2.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod2.so \
e354a5
+  $(objpfx)libmarkermod3.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod3.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v3/libmarkermod3.so \
e354a5
+  $(objpfx)libmarkermod4.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod4.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v3/libmarkermod4.so \
e354a5
+    $(objpfx)glibc-hwcaps/x86-64-v4/libmarkermod4.so \
e354a5
+
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod2.so: $(objpfx)libmarkermod2-2.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod3.so: $(objpfx)libmarkermod3-2.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v3/libmarkermod3.so: $(objpfx)libmarkermod3-3.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v2/libmarkermod4.so: $(objpfx)libmarkermod4-2.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v3/libmarkermod4.so: $(objpfx)libmarkermod4-3.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+$(objpfx)glibc-hwcaps/x86-64-v4/libmarkermod4.so: $(objpfx)libmarkermod4-4.so
e354a5
+	$(make-target-directory)
e354a5
+	cp $< $@
e354a5
+
e354a5
+ifeq (no,$(build-hardcoded-path-in-tests))
e354a5
+# This is an ld.so.cache test, and RPATH/RUNPATH in the executable
e354a5
+# interferes with its test objectives.
e354a5
+tests-container += tst-glibc-hwcaps-cache
e354a5
 endif
e354a5
 
e354a5
+endif # $(subdir) == elf
e354a5
+
e354a5
 ifeq ($(subdir),csu)
e354a5
 gen-as-const-headers += tlsdesc.sym rtld-offsets.sym
e354a5
 endif
e354a5
diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..8810a822efe36962
e354a5
--- /dev/null
e354a5
+++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c
e354a5
@@ -0,0 +1,66 @@
e354a5
+/* Architecture-specific glibc-hwcaps subdirectories.  x86 version.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <dl-hwcaps.h>
e354a5
+#include <cpu-features.h>
e354a5
+
e354a5
+const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2";
e354a5
+enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs.  */
e354a5
+
e354a5
+uint32_t
e354a5
+_dl_hwcaps_subdirs_active (void)
e354a5
+{
e354a5
+  int active = 0;
e354a5
+
e354a5
+  /* Test in reverse preference order.  */
e354a5
+
e354a5
+  /* x86-64-v2.  */
e354a5
+  if (!(CPU_FEATURE_USABLE (CMPXCHG16B)
e354a5
+        && CPU_FEATURE_USABLE (LAHF64_SAHF64)
e354a5
+        && CPU_FEATURE_USABLE (POPCNT)
e354a5
+        && CPU_FEATURE_USABLE (SSE3)
e354a5
+        && CPU_FEATURE_USABLE (SSE4_1)
e354a5
+        && CPU_FEATURE_USABLE (SSE4_2)
e354a5
+        && CPU_FEATURE_USABLE (SSSE3)))
e354a5
+    return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
e354a5
+  ++active;
e354a5
+
e354a5
+  /* x86-64-v3.  */
e354a5
+  if (!(CPU_FEATURE_USABLE (AVX)
e354a5
+        && CPU_FEATURE_USABLE (AVX2)
e354a5
+        && CPU_FEATURE_USABLE (BMI1)
e354a5
+        && CPU_FEATURE_USABLE (BMI2)
e354a5
+        && CPU_FEATURE_USABLE (F16C)
e354a5
+        && CPU_FEATURE_USABLE (FMA)
e354a5
+        && CPU_FEATURE_USABLE (LZCNT)
e354a5
+        && CPU_FEATURE_USABLE (MOVBE)
e354a5
+        && CPU_FEATURE_USABLE (OSXSAVE)))
e354a5
+    return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
e354a5
+  ++active;
e354a5
+
e354a5
+ /* x86-64-v4.  */
e354a5
+  if (!(CPU_FEATURE_USABLE (AVX512F)
e354a5
+        && CPU_FEATURE_USABLE (AVX512BW)
e354a5
+        && CPU_FEATURE_USABLE (AVX512CD)
e354a5
+        && CPU_FEATURE_USABLE (AVX512DQ)
e354a5
+        && CPU_FEATURE_USABLE (AVX512VL)))
e354a5
+    return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
e354a5
+  ++active;
e354a5
+
e354a5
+  return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
e354a5
+}
e354a5
diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..3075a8286dc30768
e354a5
--- /dev/null
e354a5
+++ b/sysdeps/x86_64/tst-glibc-hwcaps.c
e354a5
@@ -0,0 +1,76 @@
e354a5
+/* glibc-hwcaps subdirectory test.  x86_64 version.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <stdio.h>
e354a5
+#include <support/check.h>
e354a5
+#include <sys/param.h>
e354a5
+#include <sys/platform/x86.h>
e354a5
+
e354a5
+extern int marker2 (void);
e354a5
+extern int marker3 (void);
e354a5
+extern int marker4 (void);
e354a5
+
e354a5
+/* Return the x86-64-vN level, 1 for the baseline.  */
e354a5
+static int
e354a5
+compute_level (void)
e354a5
+{
e354a5
+  const struct cpu_features *cpu_features
e354a5
+    = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX);
e354a5
+
e354a5
+ if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, POPCNT)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, MMX)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSE)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSE2)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSE3)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSSE3)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)))
e354a5
+   return 1;
e354a5
+ if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, AVX2)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, BMI1)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, BMI2)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, F16C)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, FMA)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, LZCNT)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE)))
e354a5
+   return 2;
e354a5
+ if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ)
e354a5
+       && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)))
e354a5
+   return 3;
e354a5
+ return 4;
e354a5
+}
e354a5
+
e354a5
+static int
e354a5
+do_test (void)
e354a5
+{
e354a5
+  int level = compute_level ();
e354a5
+  printf ("info: detected x86-64 micro-architecture level: %d\n", level);
e354a5
+  TEST_COMPARE (marker2 (), MIN (level, 2));
e354a5
+  TEST_COMPARE (marker3 (), MIN (level, 3));
e354a5
+  TEST_COMPARE (marker4 (), MIN (level, 4));
e354a5
+  return 0;
e354a5
+}
e354a5
+
e354a5
+#include <support/test-driver.c>