ce426f
From 1ae5597025b342ee8fec59e04970b44fc1361744 Mon Sep 17 00:00:00 2001
ce426f
From: Stefan Liebler <stli@linux.vnet.ibm.com>
ce426f
Date: Mon, 7 Nov 2016 17:17:51 +0100
ce426f
Subject: [PATCH 14/17] S390: Fix utf32 to utf8 handling of low surrogates
ce426f
 (disable cu41).
ce426f
ce426f
Upstream commit 52f8a48e24563daa807f94824ce9782b9a9eece9
ce426f
ce426f
According to the latest Unicode standard, a conversion from/to UTF-xx has
ce426f
to report an error if the character value is in range of an utf16 surrogate
ce426f
(0xd800..0xdfff). See https://sourceware.org/ml/libc-help/2015-12/msg00015.html.
ce426f
ce426f
Thus the cu41 instruction, which converts from utf32 to utf8,  has to be
ce426f
disabled because it does not report an error in case of a value in range of
ce426f
a low surrogate (0xdc00..0xdfff). The etf3eh variant is removed and the c,
ce426f
vector variant is adjusted to handle the value in range of an utf16 low
ce426f
surrogate correctly.
ce426f
ce426f
ChangeLog:
ce426f
ce426f
	* sysdeps/s390/utf8-utf32-z9.c: Disable cu41 instruction and report
ce426f
	an error in case of a value in range of an utf16 low surrogate.
ce426f
---
ce426f
 sysdeps/s390/utf8-utf32-z9.c | 188 ++++++++++++++++++++++++++-----------------
ce426f
 1 file changed, 115 insertions(+), 73 deletions(-)
ce426f
ce426f
diff --git a/sysdeps/s390/utf8-utf32-z9.c b/sysdeps/s390/utf8-utf32-z9.c
ce426f
index e39e0a7..efae745 100644
ce426f
--- a/sysdeps/s390/utf8-utf32-z9.c
ce426f
+++ b/sysdeps/s390/utf8-utf32-z9.c
ce426f
@@ -572,28 +572,6 @@ __from_utf8_loop_resolver (unsigned long int dl_hwcap)
ce426f
 
ce426f
 strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 
ce426f
-
ce426f
-/* Conversion from UTF-32 internal/BE to UTF-8.  */
ce426f
-#define BODY_TO_HW(ASM)							\
ce426f
-  {									\
ce426f
-    ASM;								\
ce426f
-    if (__glibc_likely (inptr == inend)					\
ce426f
-	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
-      break;								\
ce426f
-    if (inptr + 4 > inend)						\
ce426f
-      {									\
ce426f
-	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
-	break;								\
ce426f
-      }									\
ce426f
-    STANDARD_TO_LOOP_ERR_HANDLER (4);					\
ce426f
-  }
ce426f
-
ce426f
-/* The hardware routine uses the S/390 cu41 instruction.  */
ce426f
-#define BODY_TO_ETF3EH BODY_TO_HW (HARDWARE_CONVERT ("cu41 %0, %1"))
ce426f
-
ce426f
-/* The hardware routine uses the S/390 vector and cu41 instructions.  */
ce426f
-#define BODY_TO_VX BODY_TO_HW (HW_TO_VX)
ce426f
-
ce426f
 /* The software routine mimics the S/390 cu41 instruction.  */
ce426f
 #define BODY_TO_C						\
ce426f
   {								\
ce426f
@@ -632,7 +610,7 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 	    result = __GCONV_FULL_OUTPUT;			\
ce426f
 	    break;						\
ce426f
 	  }							\
ce426f
-	if (wc >= 0xd800 && wc < 0xdc00)			\
ce426f
+	if (wc >= 0xd800 && wc <= 0xdfff)			\
ce426f
 	  {							\
ce426f
 	    /* Do not accept UTF-16 surrogates.   */		\
ce426f
 	    result = __GCONV_ILLEGAL_INPUT;			\
ce426f
@@ -679,13 +657,12 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
     inptr += 4;							\
ce426f
   }
ce426f
 
ce426f
-#define HW_TO_VX							\
ce426f
+/* The hardware routine uses the S/390 vector instructions.  */
ce426f
+#define BODY_TO_VX							\
ce426f
   {									\
ce426f
-    register const unsigned char* pInput asm ("8") = inptr;		\
ce426f
-    register size_t inlen asm ("9") = inend - inptr;			\
ce426f
-    register unsigned char* pOutput asm ("10") = outptr;		\
ce426f
-    register size_t outlen asm("11") = outend - outptr;			\
ce426f
-    unsigned long tmp, tmp2;						\
ce426f
+    size_t inlen = inend - inptr;					\
ce426f
+    size_t outlen = outend - outptr;					\
ce426f
+    unsigned long tmp, tmp2, tmp3;					\
ce426f
     asm volatile (".machine push\n\t"					\
ce426f
 		  ".machine \"z13\"\n\t"				\
ce426f
 		  ".machinemode \"zarch_nohighgprs\"\n\t"		\
ce426f
@@ -696,10 +673,10 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 		  CONVERT_32BIT_SIZE_T ([R_INLEN])			\
ce426f
 		  CONVERT_32BIT_SIZE_T ([R_OUTLEN])			\
ce426f
 		  /* Loop which handles UTF-32 chars <=0x7f.  */	\
ce426f
-		  "0:  clgijl %[R_INLEN],64,20f\n\t"			\
ce426f
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "0:  clgijl %[R_INLEN],64,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
 		  "1:  vlm %%v16,%%v19,0(%[R_IN])\n\t"			\
ce426f
-		  "    lghi %[R_TMP],0\n\t"				\
ce426f
+		  "    lghi %[R_TMP2],0\n\t"				\
ce426f
 		  /* Shorten to byte values.  */			\
ce426f
 		  "    vpkf %%v23,%%v16,%%v17\n\t"			\
ce426f
 		  "    vpkf %%v24,%%v18,%%v19\n\t"			\
ce426f
@@ -719,41 +696,116 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 		  "    aghi %[R_OUTLEN],-16\n\t"			\
ce426f
 		  "    la %[R_IN],64(%[R_IN])\n\t"			\
ce426f
 		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
ce426f
-		  "    clgijl %[R_INLEN],64,20f\n\t"			\
ce426f
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],64,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
 		  "    j 1b\n\t"					\
ce426f
 		  /* Found a value > 0x7f.  */				\
ce426f
-		  "13: ahi %[R_TMP],4\n\t"				\
ce426f
-		  "12: ahi %[R_TMP],4\n\t"				\
ce426f
-		  "11: ahi %[R_TMP],4\n\t"				\
ce426f
-		  "10: vlgvb %[R_I],%%v22,7\n\t"			\
ce426f
-		  "    srlg %[R_I],%[R_I],2\n\t"			\
ce426f
-		  "    agr %[R_I],%[R_TMP]\n\t"				\
ce426f
-		  "    je 20f\n\t"					\
ce426f
+		  "13: ahi %[R_TMP2],4\n\t"				\
ce426f
+		  "12: ahi %[R_TMP2],4\n\t"				\
ce426f
+		  "11: ahi %[R_TMP2],4\n\t"				\
ce426f
+		  "10: vlgvb %[R_TMP],%%v22,7\n\t"			\
ce426f
+		  "    srlg %[R_TMP],%[R_TMP],2\n\t"			\
ce426f
+		  "    agr %[R_TMP],%[R_TMP2]\n\t"			\
ce426f
+		  "    je 16f\n\t"					\
ce426f
 		  /* Store characters before invalid one...  */		\
ce426f
-		  "    slgr %[R_OUTLEN],%[R_I]\n\t"			\
ce426f
-		  "15: aghi %[R_I],-1\n\t"				\
ce426f
-		  "    vstl %%v23,%[R_I],0(%[R_OUT])\n\t"		\
ce426f
+		  "    slgr %[R_OUTLEN],%[R_TMP]\n\t"			\
ce426f
+		  "15: aghi %[R_TMP],-1\n\t"				\
ce426f
+		  "    vstl %%v23,%[R_TMP],0(%[R_OUT])\n\t"		\
ce426f
 		  /* ... and update pointers.  */			\
ce426f
-		  "    aghi %[R_I],1\n\t"				\
ce426f
-		  "    la %[R_OUT],0(%[R_I],%[R_OUT])\n\t"		\
ce426f
-		  "    sllg %[R_I],%[R_I],2\n\t"			\
ce426f
-		  "    la %[R_IN],0(%[R_I],%[R_IN])\n\t"		\
ce426f
-		  "    slgr %[R_INLEN],%[R_I]\n\t"			\
ce426f
-		  /* Handle multibyte utf8-char with convert instruction. */ \
ce426f
-		  "20: cu41 %[R_OUT],%[R_IN]\n\t"			\
ce426f
-		  "    jo 0b\n\t" /* Try vector implemenation again.  */ \
ce426f
-		  "    lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1.  */ \
ce426f
-		  "    lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2.  */ \
ce426f
+		  "    aghi %[R_TMP],1\n\t"				\
ce426f
+		  "    la %[R_OUT],0(%[R_TMP],%[R_OUT])\n\t"		\
ce426f
+		  "    sllg %[R_TMP2],%[R_TMP],2\n\t"			\
ce426f
+		  "    la %[R_IN],0(%[R_TMP2],%[R_IN])\n\t"		\
ce426f
+		  "    slgr %[R_INLEN],%[R_TMP2]\n\t"			\
ce426f
+		  /* Calculate remaining uint32_t values in loaded vrs.  */ \
ce426f
+		  "16: lghi %[R_TMP2],16\n\t"				\
ce426f
+		  "    sgr %[R_TMP2],%[R_TMP]\n\t"			\
ce426f
+		  "    l %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-4\n\t"				\
ce426f
+		  "    j 22f\n\t"					\
ce426f
+		  /* Handle remaining bytes.  */			\
ce426f
+		  "2:  clgije %[R_INLEN],0,99f\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],4,92f\n\t"			\
ce426f
+		  /* Calculate remaining uint32_t values in inptr.  */	\
ce426f
+		  "    srlg %[R_TMP2],%[R_INLEN],2\n\t"			\
ce426f
+		  /* Handle multibyte utf8-char. */			\
ce426f
+		  "20: l %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-4\n\t"				\
ce426f
+		  /* Test if ch is 1byte UTF-8 char. */			\
ce426f
+		  "21: clijh %[R_TMP],0x7f,22f\n\t"			\
ce426f
+		  /* Handle 1-byte UTF-8 char.  */			\
ce426f
+		  "31: slgfi %[R_OUTLEN],1\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    stc %[R_TMP],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],1(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 2byte UTF-8 char. */			\
ce426f
+		  "22: clfi %[R_TMP],0x7ff\n\t"				\
ce426f
+		  "    jh 23f\n\t"					\
ce426f
+		  /* Handle 2-byte UTF-8 char.  */			\
ce426f
+		  "32: slgfi %[R_OUTLEN],2\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    llill %[R_TMP3],0xc080\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],51,55,2\n\t" /* 1. byte.   */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 2. byte.   */ \
ce426f
+		  "    sth %[R_TMP3],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],2(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 3-byte UTF-8 char.  */		\
ce426f
+		  "23: clfi %[R_TMP],0xffff\n\t"			\
ce426f
+		  "    jh 24f\n\t"					\
ce426f
+		  /* Handle 3-byte UTF-8 char.  */			\
ce426f
+		  "33: slgfi %[R_OUTLEN],3\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    llilf %[R_TMP3],0xe08080\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],44,47,4\n\t" /* 1. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],50,55,2\n\t" /* 2. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 3. byte.  */ \
ce426f
+		  /* Test if ch is a UTF-16 surrogate: ch & 0xf800 == 0xd800  */ \
ce426f
+		  "    nilf %[R_TMP],0xf800\n\t"			\
ce426f
+		  "    clfi %[R_TMP],0xd800\n\t"			\
ce426f
+		  "    je 91f\n\t" /* Do not accept UTF-16 surrogates.  */ \
ce426f
+		  "    stcm %[R_TMP3],7,0(%[R_OUT])\n\t"		\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],3(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 4-byte UTF-8 char.  */		\
ce426f
+		  "24: clfi %[R_TMP],0x10ffff\n\t"			\
ce426f
+		  "    jh 91f\n\t" /* ch > 0x10ffff is not allowed!  */	\
ce426f
+		  /* Handle 4-byte UTF-8 char.  */			\
ce426f
+		  "34: slgfi %[R_OUTLEN],4\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    llilf %[R_TMP3],0xf0808080\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],37,39,6\n\t" /* 1. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],42,47,4\n\t" /* 2. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],50,55,2\n\t" /* 3. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 4. byte.  */ \
ce426f
+		  "    st %[R_TMP3],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],4(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  "92: lghi %[R_RES],%[RES_IN_FULL]\n\t"		\
ce426f
+		  "    j 99f\n\t"					\
ce426f
+		  "91: lghi %[R_RES],%[RES_IN_ILL]\n\t"			\
ce426f
+		  "    j 99f\n\t"					\
ce426f
+		  "90: lghi %[R_RES],%[RES_OUT_FULL]\n\t"		\
ce426f
+		  "99: \n\t"						\
ce426f
 		  ".machine pop"					\
ce426f
-		  : /* outputs */ [R_IN] "+a" (pInput)			\
ce426f
-		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput)	\
ce426f
-		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=d" (tmp)	\
ce426f
-		    , [R_I] "=a" (tmp2)					\
ce426f
+		  : /* outputs */ [R_IN] "+a" (inptr)			\
ce426f
+		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (outptr)	\
ce426f
+		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp)	\
ce426f
+		    , [R_TMP2] "=a" (tmp2), [R_TMP3] "=d" (tmp3)	\
ce426f
 		    , [R_RES] "+d" (result)				\
ce426f
 		  : /* inputs */					\
ce426f
 		    [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT)		\
ce426f
 		    , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT)		\
ce426f
+		    , [RES_IN_FULL] "i" (__GCONV_INCOMPLETE_INPUT)	\
ce426f
 		  : /* clobber list */ "memory", "cc"			\
ce426f
 		    ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17")	\
ce426f
 		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
ce426f
@@ -761,8 +813,11 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 		    ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23")	\
ce426f
 		    ASM_CLOBBER_VR ("v24")				\
ce426f
 		  );							\
ce426f
-    inptr = pInput;							\
ce426f
-    outptr = pOutput;							\
ce426f
+    if (__glibc_likely (inptr == inend)					\
ce426f
+	|| result != __GCONV_ILLEGAL_INPUT)				\
ce426f
+      break;								\
ce426f
+									\
ce426f
+    STANDARD_TO_LOOP_ERR_HANDLER (4);					\
ce426f
   }
ce426f
 
ce426f
 /* Generate loop-function with software routing.  */
ce426f
@@ -774,15 +829,6 @@ strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
 #define LOOP_NEED_FLAGS
ce426f
 #include <iconv/loop.c>
ce426f
 
ce426f
-/* Generate loop-function with hardware utf-convert instruction.  */
ce426f
-#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
-#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
-#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
ce426f
-#define LOOPFCT			__to_utf8_loop_etf3eh
ce426f
-#define LOOP_NEED_FLAGS
ce426f
-#define BODY			BODY_TO_ETF3EH
ce426f
-#include <iconv/loop.c>
ce426f
-
ce426f
 #if defined HAVE_S390_VX_ASM_SUPPORT
ce426f
 /* Generate loop-function with hardware vector and utf-convert instructions.  */
ce426f
 # define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
@@ -807,10 +853,6 @@ __to_utf8_loop_resolver (unsigned long int dl_hwcap)
ce426f
     return __to_utf8_loop_vx;
ce426f
   else
ce426f
 #endif
ce426f
-  if (dl_hwcap & HWCAP_S390_ZARCH && dl_hwcap & HWCAP_S390_HIGH_GPRS
ce426f
-      && dl_hwcap & HWCAP_S390_ETF3EH)
ce426f
-    return __to_utf8_loop_etf3eh;
ce426f
-  else
ce426f
     return __to_utf8_loop_c;
ce426f
 }
ce426f
 
ce426f
-- 
ce426f
1.8.3.1
ce426f