b1dca6
commit fdf8fbca455ca3ef57235bde907bcc6a624ac5aa
b1dca6
Author: Florian Weimer <fweimer@redhat.com>
b1dca6
Date:   Thu Dec 10 13:51:18 2020 +0100
b1dca6
b1dca6
    s390x: Add glibc-hwcaps support
b1dca6
    
b1dca6
    Subdirectories z13, z14, z15 can be selected, mostly based on the
b1dca6
    level of support for vector instructions.
b1dca6
    
b1dca6
    Co-Authored-By: Stefan Liebler <stli@linux.ibm.com>
b1dca6
b1dca6
diff --git a/elf/Makefile b/elf/Makefile
b1dca6
index 63c61ad63677ec63..67029930dd2cb461 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 = power9 x86-64-v2
b1dca6
+glibc-hwcaps-first-subdirs-for-tests = power9 x86-64-v2 z13
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 6a4675f9bd30e02f..c3271f61f9e50f2e 100644
b1dca6
--- a/elf/tst-glibc-hwcaps-cache.script
b1dca6
+++ b/elf/tst-glibc-hwcaps-cache.script
b1dca6
@@ -11,6 +11,16 @@ mkdirp 0770 $L/glibc-hwcaps/power10
b1dca6
 cp $B/elf/libmarkermod3-2.so $L/glibc-hwcaps/power9/libmarkermod3.so
b1dca6
 cp $B/elf/libmarkermod3-3.so $L/glibc-hwcaps/power10/libmarkermod3.so
b1dca6
 
b1dca6
+mkdirp 0770 $L/glibc-hwcaps/z13
b1dca6
+cp $B/elf/libmarkermod2-2.so $L/glibc-hwcaps/z13/libmarkermod2.so
b1dca6
+mkdirp 0770 $L/glibc-hwcaps/z14
b1dca6
+cp $B/elf/libmarkermod3-2.so $L/glibc-hwcaps/z13/libmarkermod3.so
b1dca6
+cp $B/elf/libmarkermod3-3.so $L/glibc-hwcaps/z14/libmarkermod3.so
b1dca6
+mkdirp 0770 $L/glibc-hwcaps/z15
b1dca6
+cp $B/elf/libmarkermod4-2.so $L/glibc-hwcaps/z13/libmarkermod4.so
b1dca6
+cp $B/elf/libmarkermod4-3.so $L/glibc-hwcaps/z14/libmarkermod4.so
b1dca6
+cp $B/elf/libmarkermod4-4.so $L/glibc-hwcaps/z15/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
diff --git a/sysdeps/s390/s390-64/Makefile b/sysdeps/s390/s390-64/Makefile
b1dca6
index b4d793bb3dd1f703..e5da26871c862e63 100644
b1dca6
--- a/sysdeps/s390/s390-64/Makefile
b1dca6
+++ b/sysdeps/s390/s390-64/Makefile
b1dca6
@@ -6,4 +6,43 @@ ifeq ($(subdir),elf)
b1dca6
 CFLAGS-rtld.c += -Wno-uninitialized -Wno-unused
b1dca6
 CFLAGS-dl-load.c += -Wno-unused
b1dca6
 CFLAGS-dl-reloc.c += -Wno-unused
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/z13/libmarkermod2.so \
b1dca6
+  $(objpfx)libmarkermod3.so \
b1dca6
+    $(objpfx)glibc-hwcaps/z13/libmarkermod3.so \
b1dca6
+    $(objpfx)glibc-hwcaps/z14/libmarkermod3.so \
b1dca6
+  $(objpfx)libmarkermod4.so \
b1dca6
+    $(objpfx)glibc-hwcaps/z13/libmarkermod4.so \
b1dca6
+    $(objpfx)glibc-hwcaps/z14/libmarkermod4.so \
b1dca6
+    $(objpfx)glibc-hwcaps/z15/libmarkermod4.so \
b1dca6
+
b1dca6
+$(objpfx)glibc-hwcaps/z13/libmarkermod2.so: $(objpfx)libmarkermod2-2.so
b1dca6
+	$(make-target-directory)
b1dca6
+	cp $< $@
b1dca6
+$(objpfx)glibc-hwcaps/z13/libmarkermod3.so: $(objpfx)libmarkermod3-2.so
b1dca6
+	$(make-target-directory)
b1dca6
+	cp $< $@
b1dca6
+$(objpfx)glibc-hwcaps/z14/libmarkermod3.so: $(objpfx)libmarkermod3-3.so
b1dca6
+	$(make-target-directory)
b1dca6
+	cp $< $@
b1dca6
+$(objpfx)glibc-hwcaps/z13/libmarkermod4.so: $(objpfx)libmarkermod4-2.so
b1dca6
+	$(make-target-directory)
b1dca6
+	cp $< $@
b1dca6
+$(objpfx)glibc-hwcaps/z14/libmarkermod4.so: $(objpfx)libmarkermod4-3.so
b1dca6
+	$(make-target-directory)
b1dca6
+	cp $< $@
b1dca6
+$(objpfx)glibc-hwcaps/z15/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
diff --git a/sysdeps/s390/s390-64/dl-hwcaps-subdirs.c b/sysdeps/s390/s390-64/dl-hwcaps-subdirs.c
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..3673808a458350ad
b1dca6
--- /dev/null
b1dca6
+++ b/sysdeps/s390/s390-64/dl-hwcaps-subdirs.c
b1dca6
@@ -0,0 +1,54 @@
b1dca6
+/* Architecture-specific glibc-hwcaps subdirectories.  s390x 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 <ldsodefs.h>
b1dca6
+
b1dca6
+const char _dl_hwcaps_subdirs[] = "z15:z14:z13";
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
+  /* z13.  */
b1dca6
+  if (!(GLRO (dl_hwcap) & HWCAP_S390_VX))
b1dca6
+    return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
b1dca6
+  ++active;
b1dca6
+
b1dca6
+  /* z14.  */
b1dca6
+  if (!((GLRO (dl_hwcap) & HWCAP_S390_VXD)
b1dca6
+        && (GLRO (dl_hwcap) & HWCAP_S390_VXE)
b1dca6
+        && (GLRO (dl_hwcap) & HWCAP_S390_GS)))
b1dca6
+    return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active);
b1dca6
+  ++active;
b1dca6
+
b1dca6
+  /* z15.
b1dca6
+     Note: We do not list HWCAP_S390_SORT and HWCAP_S390_DFLT here as,
b1dca6
+     according to the Principles of Operation, those may be replaced or removed
b1dca6
+     in future.  */
b1dca6
+  if (!((GLRO (dl_hwcap) & HWCAP_S390_VXRS_EXT2)
b1dca6
+        && (GLRO (dl_hwcap) & HWCAP_S390_VXRS_PDE)))
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/s390/s390-64/tst-glibc-hwcaps.c b/sysdeps/s390/s390-64/tst-glibc-hwcaps.c
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..690f0d5fab36eb59
b1dca6
--- /dev/null
b1dca6
+++ b/sysdeps/s390/s390-64/tst-glibc-hwcaps.c
b1dca6
@@ -0,0 +1,82 @@
b1dca6
+/* glibc-hwcaps subdirectory test.  s390x 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 <string.h>
b1dca6
+#include <support/check.h>
b1dca6
+#include <sys/auxv.h>
b1dca6
+#include <sys/param.h>
b1dca6
+
b1dca6
+extern int marker2 (void);
b1dca6
+extern int marker3 (void);
b1dca6
+extern int marker4 (void);
b1dca6
+
b1dca6
+/* Return the arch level, 10 for the baseline libmarkermod*.so's.  */
b1dca6
+static int
b1dca6
+compute_level (void)
b1dca6
+{
b1dca6
+  const char *platform = (const char *) getauxval (AT_PLATFORM);
b1dca6
+
b1dca6
+  /* The arch* versions refer to the edition of the Principles of
b1dca6
+     Operation, and they are off by two when compared with the recent
b1dca6
+     product names.  (The code below should not be considered an
b1dca6
+     accurate mapping to Principles of Operation editions for earlier
b1dca6
+     AT_PLATFORM strings).  */
b1dca6
+  if (strcmp (platform, "z900") == 0)
b1dca6
+    return 10;
b1dca6
+  if (strcmp (platform, "z990") == 0)
b1dca6
+    return 10;
b1dca6
+  if (strcmp (platform, "z9-109") == 0)
b1dca6
+    return 10;
b1dca6
+  if (strcmp (platform, "z10") == 0)
b1dca6
+    return 10;
b1dca6
+  if (strcmp (platform, "z196") == 0)
b1dca6
+    return 10;
b1dca6
+  if (strcmp (platform, "zEC12") == 0)
b1dca6
+    return 10;
b1dca6
+
b1dca6
+  /* If we are running on z13 or newer and the kernel was booted with novx,
b1dca6
+     then AT_PLATFORM is z13 or newer, but _dl_hwcaps_subdirs_active will
b1dca6
+     return zero and the _dl_hwcaps_subdirs are not searched.  */
b1dca6
+  const unsigned long int hwcap = getauxval (AT_HWCAP);
b1dca6
+  if ((hwcap & HWCAP_S390_VX) == 0)
b1dca6
+    return 10;
b1dca6
+
b1dca6
+  if (strcmp (platform, "z13") == 0)
b1dca6
+    return 11;
b1dca6
+  if (strcmp (platform, "z14") == 0)
b1dca6
+    return 12;
b1dca6
+  if (strcmp (platform, "z15") == 0)
b1dca6
+    return 13;
b1dca6
+  printf ("warning: unrecognized AT_PLATFORM value: %s\n", platform);
b1dca6
+  /* Assume that the new platform supports z15.  */
b1dca6
+  return 13;
b1dca6
+}
b1dca6
+
b1dca6
+static int
b1dca6
+do_test (void)
b1dca6
+{
b1dca6
+  int level = compute_level ();
b1dca6
+  printf ("info: detected architecture level: arch%d\n", level);
b1dca6
+  TEST_COMPARE (marker2 (), MIN (level - 9, 2));
b1dca6
+  TEST_COMPARE (marker3 (), MIN (level - 9, 3));
b1dca6
+  TEST_COMPARE (marker4 (), MIN (level - 9, 4));
b1dca6
+  return 0;
b1dca6
+}
b1dca6
+
b1dca6
+#include <support/test-driver.c>