548bcb
commit 38560563587ad8eafa700c56800ff844f18fbad1
548bcb
Author: Naohiro Tamura <naohirot@fujitsu.com>
548bcb
Date:   Thu May 20 07:34:37 2021 +0000
548bcb
548bcb
    aarch64: Added Vector Length Set test helper script
548bcb
    
548bcb
    This patch is a test helper script to change Vector Length for child
548bcb
    process. This script can be used as test-wrapper for 'make check'.
548bcb
    
548bcb
    Usage examples:
548bcb
    
548bcb
    ~/build$ make check subdirs=string \
548bcb
    test-wrapper='~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16'
548bcb
    
548bcb
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16 \
548bcb
    make test t=string/test-memcpy
548bcb
    
548bcb
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 32 \
548bcb
    ./debugglibc.sh string/test-memmove
548bcb
    
548bcb
    ~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 64 \
548bcb
    ./testrun.sh string/test-memset
548bcb
548bcb
diff --git a/INSTALL b/INSTALL
548bcb
index 065565093bd76d5b..b3a4370f592c5047 100644
548bcb
--- a/INSTALL
548bcb
+++ b/INSTALL
548bcb
@@ -387,6 +387,10 @@ the same syntax as 'test-wrapper-env', the only difference in its
548bcb
 semantics being starting with an empty set of environment variables
548bcb
 rather than the ambient set.
548bcb
 
548bcb
+   For AArch64 with SVE, when testing the GNU C Library, 'test-wrapper'
548bcb
+may be set to "SRCDIR/sysdeps/unix/sysv/linux/aarch64/vltest.py
548bcb
+VECTOR-LENGTH" to change Vector Length.
548bcb
+
548bcb
 Installing the C Library
548bcb
 ========================
548bcb
 
548bcb
diff --git a/manual/install.texi b/manual/install.texi
548bcb
index 7e9f2be150e6f98a..c262fd56d0cef67b 100644
548bcb
--- a/manual/install.texi
548bcb
+++ b/manual/install.texi
548bcb
@@ -425,6 +425,9 @@ use has the same syntax as @samp{test-wrapper-env}, the only
548bcb
 difference in its semantics being starting with an empty set of
548bcb
 environment variables rather than the ambient set.
548bcb
 
548bcb
+For AArch64 with SVE, when testing @theglibc{}, @samp{test-wrapper}
548bcb
+may be set to "@var{srcdir}/sysdeps/unix/sysv/linux/aarch64/vltest.py
548bcb
+@var{vector-length}" to change Vector Length.
548bcb
 
548bcb
 @node Running make install
548bcb
 @appendixsec Installing the C Library
548bcb
diff --git a/sysdeps/unix/sysv/linux/aarch64/vltest.py b/sysdeps/unix/sysv/linux/aarch64/vltest.py
548bcb
new file mode 100755
548bcb
index 0000000000000000..bed62ad151e06868
548bcb
--- /dev/null
548bcb
+++ b/sysdeps/unix/sysv/linux/aarch64/vltest.py
548bcb
@@ -0,0 +1,82 @@
548bcb
+#!/usr/bin/python3
548bcb
+# Set Scalable Vector Length test helper
548bcb
+# Copyright (C) 2021 Free Software Foundation, Inc.
548bcb
+# This file is part of the GNU C Library.
548bcb
+#
548bcb
+# The GNU C Library is free software; you can redistribute it and/or
548bcb
+# modify it under the terms of the GNU Lesser General Public
548bcb
+# License as published by the Free Software Foundation; either
548bcb
+# version 2.1 of the License, or (at your option) any later version.
548bcb
+#
548bcb
+# The GNU C Library is distributed in the hope that it will be useful,
548bcb
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
548bcb
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
548bcb
+# Lesser General Public License for more details.
548bcb
+#
548bcb
+# You should have received a copy of the GNU Lesser General Public
548bcb
+# License along with the GNU C Library; if not, see
548bcb
+# <https://www.gnu.org/licenses/>.
548bcb
+"""Set Scalable Vector Length test helper.
548bcb
+
548bcb
+Set Scalable Vector Length for child process.
548bcb
+
548bcb
+examples:
548bcb
+
548bcb
+~/build$ make check subdirs=string \
548bcb
+test-wrapper='~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16'
548bcb
+
548bcb
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 16 \
548bcb
+make test t=string/test-memcpy
548bcb
+
548bcb
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 32 \
548bcb
+./debugglibc.sh string/test-memmove
548bcb
+
548bcb
+~/build$ ~/glibc/sysdeps/unix/sysv/linux/aarch64/vltest.py 64 \
548bcb
+./testrun.sh string/test-memset
548bcb
+"""
548bcb
+import argparse
548bcb
+from ctypes import cdll, CDLL
548bcb
+import os
548bcb
+import sys
548bcb
+
548bcb
+EXIT_SUCCESS = 0
548bcb
+EXIT_FAILURE = 1
548bcb
+EXIT_UNSUPPORTED = 77
548bcb
+
548bcb
+AT_HWCAP = 16
548bcb
+HWCAP_SVE = (1 << 22)
548bcb
+
548bcb
+PR_SVE_GET_VL = 51
548bcb
+PR_SVE_SET_VL = 50
548bcb
+PR_SVE_SET_VL_ONEXEC = (1 << 18)
548bcb
+PR_SVE_VL_INHERIT = (1 << 17)
548bcb
+PR_SVE_VL_LEN_MASK = 0xffff
548bcb
+
548bcb
+def main(args):
548bcb
+    libc = CDLL("libc.so.6")
548bcb
+    if not libc.getauxval(AT_HWCAP) & HWCAP_SVE:
548bcb
+        print("CPU doesn't support SVE")
548bcb
+        sys.exit(EXIT_UNSUPPORTED)
548bcb
+
548bcb
+    libc.prctl(PR_SVE_SET_VL,
548bcb
+               args.vl[0] | PR_SVE_SET_VL_ONEXEC | PR_SVE_VL_INHERIT)
548bcb
+    os.execvp(args.args[0], args.args)
548bcb
+    print("exec system call failure")
548bcb
+    sys.exit(EXIT_FAILURE)
548bcb
+
548bcb
+if __name__ == '__main__':
548bcb
+    parser = argparse.ArgumentParser(description=
548bcb
+            "Set Scalable Vector Length test helper",
548bcb
+            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
548bcb
+
548bcb
+    # positional argument
548bcb
+    parser.add_argument("vl", nargs=1, type=int,
548bcb
+                        choices=range(16, 257, 16),
548bcb
+                        help=('vector length '\
548bcb
+                              'which is multiples of 16 from 16 to 256'))
548bcb
+    # remainDer arguments
548bcb
+    parser.add_argument('args', nargs=argparse.REMAINDER,
548bcb
+                        help=('args '\
548bcb
+                              'which is passed to child process'))
548bcb
+    args = parser.parse_args()
548bcb
+    main(args)