6c0556
commit 38560563587ad8eafa700c56800ff844f18fbad1
6c0556
Author: Naohiro Tamura <naohirot@fujitsu.com>
6c0556
Date:   Thu May 20 07:34:37 2021 +0000
6c0556
6c0556
    aarch64: Added Vector Length Set test helper script
6c0556
    
6c0556
    This patch is a test helper script to change Vector Length for child
6c0556
    process. This script can be used as test-wrapper for 'make check'.
6c0556
    
6c0556
    Usage examples:
6c0556
    
6c0556
    ~/build$ make check subdirs=string \
6c0556
    test-wrapper='~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16'
6c0556
    
6c0556
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16 \
6c0556
    make test t=string/test-memcpy
6c0556
    
6c0556
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 32 \
6c0556
    ./debugglibc.sh string/test-memmove
6c0556
    
6c0556
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 64 \
6c0556
    ./testrun.sh string/test-memset
6c0556
6c0556
diff --git a/INSTALL b/INSTALL
6c0556
index 065565093bd76d5b..b3a4370f592c5047 100644
6c0556
--- a/INSTALL
6c0556
+++ b/INSTALL
6c0556
@@ -387,6 +387,10 @@ the same syntax as 'test-wrapper-env', the only difference in its
6c0556
 semantics being starting with an empty set of environment variables
6c0556
 rather than the ambient set.
6c0556
 
6c0556
+   For AArch64 with SVE, when testing the GNU C Library, 'test-wrapper'
6c0556
+may be set to "SRCDIR/sysdeps/unix/sysv/linux/aarch64/vltest.py
6c0556
+VECTOR-LENGTH" to change Vector Length.
6c0556
+
6c0556
 Installing the C Library
6c0556
 ========================
6c0556
 
6c0556
diff --git a/manual/install.texi b/manual/install.texi
6c0556
index 7e9f2be150e6f98a..c262fd56d0cef67b 100644
6c0556
--- a/manual/install.texi
6c0556
+++ b/manual/install.texi
6c0556
@@ -425,6 +425,9 @@ use has the same syntax as @samp{test-wrapper-env}, the only
6c0556
 difference in its semantics being starting with an empty set of
6c0556
 environment variables rather than the ambient set.
6c0556
 
6c0556
+For AArch64 with SVE, when testing @theglibc{}, @samp{test-wrapper}
6c0556
+may be set to "@var{srcdir}/sysdeps/unix/sysv/linux/aarch64/vltest.py
6c0556
+@var{vector-length}" to change Vector Length.
6c0556
 
6c0556
 @node Running make install
6c0556
 @appendixsec Installing the C Library
6c0556
diff --git a/sysdeps/unix/sysv/linux/aarch64/vltest.py b/sysdeps/unix/sysv/linux/aarch64/vltest.py
6c0556
new file mode 100755
6c0556
index 0000000000000000..bed62ad151e06868
6c0556
--- /dev/null
6c0556
+++ b/sysdeps/unix/sysv/linux/aarch64/vltest.py
6c0556
@@ -0,0 +1,82 @@
6c0556
+#!/usr/bin/python3
6c0556
+# Set Scalable Vector Length test helper
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
+"""Set Scalable Vector Length test helper.
6c0556
+
6c0556
+Set Scalable Vector Length for child process.
6c0556
+
6c0556
+examples:
6c0556
+
6c0556
+~/build$ make check subdirs=string \
6c0556
+test-wrapper='~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16'
6c0556
+
6c0556
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16 \
6c0556
+make test t=string/test-memcpy
6c0556
+
6c0556
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 32 \
6c0556
+./debugglibc.sh string/test-memmove
6c0556
+
6c0556
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 64 \
6c0556
+./testrun.sh string/test-memset
6c0556
+"""
6c0556
+import argparse
6c0556
+from ctypes import cdll, CDLL
6c0556
+import os
6c0556
+import sys
6c0556
+
6c0556
+EXIT_SUCCESS = 0
6c0556
+EXIT_FAILURE = 1
6c0556
+EXIT_UNSUPPORTED = 77
6c0556
+
6c0556
+AT_HWCAP = 16
6c0556
+HWCAP_SVE = (1 << 22)
6c0556
+
6c0556
+PR_SVE_GET_VL = 51
6c0556
+PR_SVE_SET_VL = 50
6c0556
+PR_SVE_SET_VL_ONEXEC = (1 << 18)
6c0556
+PR_SVE_VL_INHERIT = (1 << 17)
6c0556
+PR_SVE_VL_LEN_MASK = 0xffff
6c0556
+
6c0556
+def main(args):
6c0556
+    libc = CDLL("libc.so.6")
6c0556
+    if not libc.getauxval(AT_HWCAP) & HWCAP_SVE:
6c0556
+        print("CPU doesn't support SVE")
6c0556
+        sys.exit(EXIT_UNSUPPORTED)
6c0556
+
6c0556
+    libc.prctl(PR_SVE_SET_VL,
6c0556
+               args.vl[0] | PR_SVE_SET_VL_ONEXEC | PR_SVE_VL_INHERIT)
6c0556
+    os.execvp(args.args[0], args.args)
6c0556
+    print("exec system call failure")
6c0556
+    sys.exit(EXIT_FAILURE)
6c0556
+
6c0556
+if __name__ == '__main__':
6c0556
+    parser = argparse.ArgumentParser(description=
6c0556
+            "Set Scalable Vector Length test helper",
6c0556
+            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
6c0556
+
6c0556
+    # positional argument
6c0556
+    parser.add_argument("vl", nargs=1, type=int,
6c0556
+                        choices=range(16, 257, 16),
6c0556
+                        help=('vector length '\
6c0556
+                              'which is multiples of 16 from 16 to 256'))
6c0556
+    # remainDer arguments
6c0556
+    parser.add_argument('args', nargs=argparse.REMAINDER,
6c0556
+                        help=('args '\
6c0556
+                              'which is passed to child process'))
6c0556
+    args = parser.parse_args()
6c0556
+    main(args)