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