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