077c9d
commit cdd927d98cc38acf55e1c6594b5c9451df8f239f
077c9d
Author: Stefan Liebler <stli@linux.ibm.com>
077c9d
Date:   Tue Dec 18 13:57:08 2018 +0100
077c9d
077c9d
    S390: Add z13 memmove ifunc variant.
077c9d
    
077c9d
    This patch introduces a z13 specific ifunc variant for memmove.
077c9d
    As the common code implementation, it checks if we can copy from
077c9d
    the beginning to the end - with z196 memcpy implementation - or
077c9d
    if we have to copy from the end to the beginning.
077c9d
    The latter case is done by using vector load/store instructions.
077c9d
    
077c9d
    If vector instructions are not available, the common-code is
077c9d
    used as fallback.  Therefore it is implemented in memmove-c with
077c9d
    a different name.
077c9d
    Furthermore the ifunc logic decides if we need the common-code
077c9d
    implementation at all.  If vector instructions are supported
077c9d
    due to the minimum architecture level set we can skip the
077c9d
    common-code ifunc variant.
077c9d
    
077c9d
    ChangeLog:
077c9d
    
077c9d
            * sysdeps/s390/Makefile (sysdep_routines): Add memmove-c.
077c9d
            * sysdeps/s390/ifunc-memcpy.h (HAVE_MEMMOVE_IFUNC,
077c9d
            HAVE_MEMMOVE_IFUNC_AND_VX_SUPPORT, MEMMOVE_DEFAULT,
077c9d
            HAVE_MEMMOVE_C, MEMMOVE_C,  HAVE_MEMMOVE_Z13, MEMMOVE_Z13):
077c9d
            New defines.
077c9d
            * sysdeps/s390/memcpy-z900.S: Add z13 memmove implementation.
077c9d
            * sysdeps/s390/memmove-c.c: New file.
077c9d
            * sysdeps/s390/memmove.c: Likewise.
077c9d
            * sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
            (__libc_ifunc_impl_list): Add ifunc variants for memmove.
077c9d
077c9d
diff --git a/sysdeps/s390/Makefile b/sysdeps/s390/Makefile
077c9d
index 838950a5ab958e31..3a7cccdf8f147398 100644
077c9d
--- a/sysdeps/s390/Makefile
077c9d
+++ b/sysdeps/s390/Makefile
077c9d
@@ -33,5 +33,6 @@ endif
077c9d
 ifeq ($(subdir),string)
077c9d
 sysdep_routines += bzero memset memset-z900 \
077c9d
 		   memcmp memcmp-z900 \
077c9d
-		   mempcpy memcpy memcpy-z900
077c9d
+		   mempcpy memcpy memcpy-z900 \
077c9d
+		   memmove memmove-c
077c9d
 endif
077c9d
diff --git a/sysdeps/s390/ifunc-memcpy.h b/sysdeps/s390/ifunc-memcpy.h
077c9d
index 51c71baa2c0b0452..0e701968c8f39014 100644
077c9d
--- a/sysdeps/s390/ifunc-memcpy.h
077c9d
+++ b/sysdeps/s390/ifunc-memcpy.h
077c9d
@@ -43,6 +43,29 @@
077c9d
 # define HAVE_MEMCPY_Z196	HAVE_MEMCPY_IFUNC
077c9d
 #endif
077c9d
 
077c9d
+#if defined SHARED && defined USE_MULTIARCH && IS_IN (libc)	\
077c9d
+  && ! defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
077c9d
+# define HAVE_MEMMOVE_IFUNC	1
077c9d
+#else
077c9d
+# define HAVE_MEMMOVE_IFUNC	0
077c9d
+#endif
077c9d
+
077c9d
+#ifdef HAVE_S390_VX_ASM_SUPPORT
077c9d
+# define HAVE_MEMMOVE_IFUNC_AND_VX_SUPPORT HAVE_MEMMOVE_IFUNC
077c9d
+#else
077c9d
+# define HAVE_MEMMOVE_IFUNC_AND_VX_SUPPORT 0
077c9d
+#endif
077c9d
+
077c9d
+#if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
077c9d
+# define MEMMOVE_DEFAULT	MEMMOVE_Z13
077c9d
+# define HAVE_MEMMOVE_C		0
077c9d
+# define HAVE_MEMMOVE_Z13	1
077c9d
+#else
077c9d
+# define MEMMOVE_DEFAULT	MEMMOVE_C
077c9d
+# define HAVE_MEMMOVE_C		1
077c9d
+# define HAVE_MEMMOVE_Z13	HAVE_MEMMOVE_IFUNC_AND_VX_SUPPORT
077c9d
+#endif
077c9d
+
077c9d
 #if HAVE_MEMCPY_Z900_G5
077c9d
 # define MEMCPY_Z900_G5		__memcpy_default
077c9d
 # define MEMPCPY_Z900_G5	__mempcpy_default
077c9d
@@ -66,3 +89,15 @@
077c9d
 # define MEMCPY_Z196		NULL
077c9d
 # define MEMPCPY_Z196		NULL
077c9d
 #endif
077c9d
+
077c9d
+#if HAVE_MEMMOVE_C
077c9d
+# define MEMMOVE_C		__memmove_c
077c9d
+#else
077c9d
+# define MEMMOVE_C		NULL
077c9d
+#endif
077c9d
+
077c9d
+#if HAVE_MEMMOVE_Z13
077c9d
+# define MEMMOVE_Z13		__memmove_z13
077c9d
+#else
077c9d
+# define MEMMOVE_Z13		NULL
077c9d
+#endif
077c9d
diff --git a/sysdeps/s390/memcpy-z900.S b/sysdeps/s390/memcpy-z900.S
077c9d
index 3a50cf44d85d2417..bd3b1950ee442c0c 100644
077c9d
--- a/sysdeps/s390/memcpy-z900.S
077c9d
+++ b/sysdeps/s390/memcpy-z900.S
077c9d
@@ -182,6 +182,7 @@ ENTRY(MEMCPY_Z196)
077c9d
 # endif /* !defined __s390x__  */
077c9d
 	ltgr    %r4,%r4
077c9d
 	je      .L_Z196_4
077c9d
+.L_Z196_start2:
077c9d
 	aghi    %r4,-1
077c9d
 	srlg    %r5,%r4,8
077c9d
 	ltgr    %r5,%r5
077c9d
@@ -207,6 +208,75 @@ ENTRY(MEMCPY_Z196)
077c9d
 END(MEMCPY_Z196)
077c9d
 #endif /* HAVE_MEMCPY_Z196  */
077c9d
 
077c9d
+#if HAVE_MEMMOVE_Z13
077c9d
+ENTRY(MEMMOVE_Z13)
077c9d
+	.machine "z13"
077c9d
+	.machinemode "zarch_nohighgprs"
077c9d
+# if !defined __s390x__
077c9d
+	/* Note: The 31bit dst and src pointers are prefixed with zeroes.  */
077c9d
+	llgfr	%r4,%r4
077c9d
+	llgfr	%r3,%r3
077c9d
+	llgfr	%r2,%r2
077c9d
+# endif /* !defined __s390x__ */
077c9d
+	sgrk	%r0,%r2,%r3
077c9d
+	clgijh	%r4,16,.L_MEMMOVE_Z13_LARGE
077c9d
+	aghik	%r5,%r4,-1
077c9d
+.L_MEMMOVE_Z13_SMALL:
077c9d
+	jl .L_MEMMOVE_Z13_END		/* Jump away if len was zero.  */
077c9d
+	/* Store up to 16 bytes with vll/vstl which needs the index
077c9d
+	   instead of lengths.  */
077c9d
+	vll	%v16,%r5,0(%r3)
077c9d
+	vstl	%v16,%r5,0(%r2)
077c9d
+.L_MEMMOVE_Z13_END:
077c9d
+	br      %r14
077c9d
+.L_MEMMOVE_Z13_LARGE:
077c9d
+	lgr     %r1,%r2			/* For memcpy: r1: Use as dest ;
077c9d
+					   r2: Return dest  */
077c9d
+	/* The unsigned comparison (dst - src >= len) determines if we can
077c9d
+	   execute the forward case with memcpy.  */
077c9d
+#if ! HAVE_MEMCPY_Z196
077c9d
+# error The z13 variant of memmove needs the z196 variant of memcpy!
077c9d
+#endif
077c9d
+	clgrjhe %r0,%r4,.L_Z196_start2
077c9d
+	risbgn	%r5,%r4,4,128+63,60	/* r5 = r4 / 16  */
077c9d
+	aghi	%r4,-16
077c9d
+	clgijhe	%r5,8,.L_MEMMOVE_Z13_LARGE_64B
077c9d
+.L_MEMMOVE_Z13_LARGE_16B_LOOP:
077c9d
+	/* Store at least 16 bytes with vl/vst. The number of 16byte blocks
077c9d
+	   is stored in r5.  */
077c9d
+	vl	%v16,0(%r4,%r3)
077c9d
+	vst	%v16,0(%r4,%r2)
077c9d
+	aghi	%r4,-16
077c9d
+	brctg	%r5,.L_MEMMOVE_Z13_LARGE_16B_LOOP
077c9d
+	aghik	%r5,%r4,15
077c9d
+	j	.L_MEMMOVE_Z13_SMALL
077c9d
+.L_MEMMOVE_Z13_LARGE_64B:
077c9d
+	/* Store at least 128 bytes with 4x vl/vst. The number of 64byte blocks
077c9d
+	   will be stored in r0.  */
077c9d
+	aghi	%r4,-48
077c9d
+	srlg	%r0,%r5,2		/* r5 = %r0 / 4
077c9d
+					   => Number of 64byte blocks.  */
077c9d
+.L_MEMMOVE_Z13_LARGE_64B_LOOP:
077c9d
+	vl	%v20,48(%r4,%r3)
077c9d
+	vl	%v19,32(%r4,%r3)
077c9d
+	vl	%v18,16(%r4,%r3)
077c9d
+	vl	%v17,0(%r4,%r3)
077c9d
+	vst	%v20,48(%r4,%r2)
077c9d
+	vst	%v19,32(%r4,%r2)
077c9d
+	vst	%v18,16(%r4,%r2)
077c9d
+	vst	%v17,0(%r4,%r2)
077c9d
+	aghi	%r4,-64
077c9d
+	brctg	%r0,.L_MEMMOVE_Z13_LARGE_64B_LOOP
077c9d
+	aghi	%r4,48
077c9d
+	/* Recalculate the number of 16byte blocks.  */
077c9d
+	risbg	%r5,%r5,62,128+63,0	/* r5 = r5 & 3
077c9d
+					   => Remaining 16byte blocks.  */
077c9d
+	jne	.L_MEMMOVE_Z13_LARGE_16B_LOOP
077c9d
+	aghik	%r5,%r4,15
077c9d
+	j	.L_MEMMOVE_Z13_SMALL
077c9d
+END(MEMMOVE_Z13)
077c9d
+#endif /* HAVE_MEMMOVE_Z13  */
077c9d
+
077c9d
 #if ! HAVE_MEMCPY_IFUNC
077c9d
 /* If we don't use ifunc, define an alias for mem[p]cpy here.
077c9d
    Otherwise see sysdeps/s390/mem[p]cpy.c.  */
077c9d
@@ -215,10 +285,27 @@ strong_alias (MEMPCPY_DEFAULT, __mempcpy)
077c9d
 weak_alias (__mempcpy, mempcpy)
077c9d
 #endif
077c9d
 
077c9d
+#if ! HAVE_MEMMOVE_IFUNC
077c9d
+/* If we don't use ifunc, define an alias for memmove here.
077c9d
+   Otherwise see sysdeps/s390/memmove.c.  */
077c9d
+# if ! HAVE_MEMMOVE_C
077c9d
+/* If the c variant is needed, then sysdeps/s390/memmove-c.c
077c9d
+   defines memmove.
077c9d
+   Otherwise MEMMOVE_DEFAULT is implemented here and we have to define it.  */
077c9d
+strong_alias (MEMMOVE_DEFAULT, memmove)
077c9d
+# endif
077c9d
+#endif
077c9d
+
077c9d
 #if defined SHARED && IS_IN (libc)
077c9d
 /* Defines the internal symbols.
077c9d
    Compare to libc_hidden_[builtin_]def (mem[p]cpy) in string/mem[p]cpy.c.  */
077c9d
 strong_alias (MEMCPY_DEFAULT, __GI_memcpy)
077c9d
 strong_alias (MEMPCPY_DEFAULT, __GI_mempcpy)
077c9d
 strong_alias (MEMPCPY_DEFAULT, __GI___mempcpy)
077c9d
+# if ! HAVE_MEMMOVE_C
077c9d
+/* If the c variant is needed, then sysdeps/s390/memmove-c.c
077c9d
+   defines the internal symbol.
077c9d
+   Otherwise MEMMOVE_DEFAULT is implemented here and we have to define it.  */
077c9d
+strong_alias (MEMMOVE_DEFAULT, __GI_memmove)
077c9d
+# endif
077c9d
 #endif
077c9d
diff --git a/sysdeps/s390/memmove-c.c b/sysdeps/s390/memmove-c.c
077c9d
new file mode 100644
077c9d
index 0000000000000000..be571093e019a38d
077c9d
--- /dev/null
077c9d
+++ b/sysdeps/s390/memmove-c.c
077c9d
@@ -0,0 +1,37 @@
077c9d
+/* Fallback C version of memmove.
077c9d
+   Copyright (C) 2018 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <ifunc-memcpy.h>
077c9d
+
077c9d
+#if HAVE_MEMMOVE_C
077c9d
+# if HAVE_MEMMOVE_IFUNC
077c9d
+/* If we use ifunc, then the memmove symbol is defined
077c9d
+   in sysdeps/s390/memmove.c and we use a different name here.
077c9d
+   Otherwise, we have to define memmove here or in
077c9d
+   sysdeps/s390/memcpy.S depending on the used default implementation.  */
077c9d
+#  define MEMMOVE MEMMOVE_C
077c9d
+#  if defined SHARED && IS_IN (libc)
077c9d
+/* Define the internal symbol.  */
077c9d
+#   undef libc_hidden_builtin_def
077c9d
+#   define libc_hidden_builtin_def(name)			\
077c9d
+  __hidden_ver1 (__memmove_c, __GI_memmove, __memmove_c);
077c9d
+#  endif
077c9d
+# endif
077c9d
+
077c9d
+# include <string/memmove.c>
077c9d
+#endif
077c9d
diff --git a/sysdeps/s390/memmove.c b/sysdeps/s390/memmove.c
077c9d
new file mode 100644
077c9d
index 0000000000000000..ac34edf80f2678cd
077c9d
--- /dev/null
077c9d
+++ b/sysdeps/s390/memmove.c
077c9d
@@ -0,0 +1,44 @@
077c9d
+/* Multiple versions of memmove.
077c9d
+   Copyright (C) 2016-2018 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <ifunc-memcpy.h>
077c9d
+
077c9d
+#if HAVE_MEMMOVE_IFUNC
077c9d
+/* If we don't use ifunc, an alias is defined for memmove
077c9d
+   in sysdeps/s390/memmove-c.c or sysdeps/s390/memcpy.S
077c9d
+   depending on the used default implementation.  */
077c9d
+# undef memmove
077c9d
+# define memmove __redirect_memmove
077c9d
+# include <string.h>
077c9d
+# include <ifunc-resolve.h>
077c9d
+# undef memmove
077c9d
+
077c9d
+# if HAVE_MEMMOVE_C
077c9d
+extern __typeof (__redirect_memmove) MEMMOVE_C attribute_hidden;
077c9d
+# endif
077c9d
+
077c9d
+# if HAVE_MEMMOVE_Z13
077c9d
+extern __typeof (__redirect_memmove) MEMMOVE_Z13 attribute_hidden;
077c9d
+# endif
077c9d
+
077c9d
+s390_libc_ifunc_expr (__redirect_memmove, memmove,
077c9d
+		      (HAVE_MEMMOVE_Z13 && (hwcap & HWCAP_S390_VX))
077c9d
+		      ? MEMMOVE_Z13
077c9d
+		      : MEMMOVE_DEFAULT
077c9d
+		      )
077c9d
+#endif
077c9d
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
index 6969c480cc40e0e2..c05c63e00608dcd7 100644
077c9d
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
@@ -126,6 +126,18 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
077c9d
 	      )
077c9d
 #endif /* HAVE_MEMCPY_IFUNC  */
077c9d
 
077c9d
+#if HAVE_MEMMOVE_IFUNC
077c9d
+    IFUNC_IMPL (i, name, memmove,
077c9d
+# if HAVE_MEMMOVE_Z13
077c9d
+		IFUNC_IMPL_ADD (array, i, memmove,
077c9d
+				dl_hwcap & HWCAP_S390_VX, MEMMOVE_Z13)
077c9d
+# endif
077c9d
+# if HAVE_MEMMOVE_C
077c9d
+		IFUNC_IMPL_ADD (array, i, memmove, 1, MEMMOVE_C)
077c9d
+# endif
077c9d
+		)
077c9d
+#endif /* HAVE_MEMMOVE_IFUNC  */
077c9d
+
077c9d
 #ifdef HAVE_S390_VX_ASM_SUPPORT
077c9d
 
077c9d
 # define IFUNC_VX_IMPL(FUNC)						\