ce426f
From cddea07761373ce92dc75e8306212d71fa2043ba Mon Sep 17 00:00:00 2001
ce426f
From: Stefan Liebler <stli@linux.vnet.ibm.com>
ce426f
Date: Mon, 7 Nov 2016 17:18:39 +0100
ce426f
Subject: [PATCH 15/17] S390: Fix utf32 to utf16 handling of low surrogates
ce426f
 (disable cu42).
ce426f
ce426f
Upstream commit a42a95c43133d69b1108f582cffa0f6986a9c3da
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 cu42 instruction, which converts from utf32 to utf16,  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/utf16-utf32-z9.c: Disable cu42 instruction and report
ce426f
	an error in case of a value in range of an utf16 low surrogate.
ce426f
---
ce426f
 sysdeps/s390/utf16-utf32-z9.c | 155 +++++++++++++++++-------------------------
ce426f
 1 file changed, 62 insertions(+), 93 deletions(-)
ce426f
ce426f
diff --git a/sysdeps/s390/utf16-utf32-z9.c b/sysdeps/s390/utf16-utf32-z9.c
ce426f
index 8d42ab8..5d2ac44 100644
ce426f
--- a/sysdeps/s390/utf16-utf32-z9.c
ce426f
+++ b/sysdeps/s390/utf16-utf32-z9.c
ce426f
@@ -145,42 +145,6 @@ gconv_end (struct __gconv_step *data)
ce426f
   free (data->__data);
ce426f
 }
ce426f
 
ce426f
-/* The macro for the hardware loop.  This is used for both
ce426f
-   directions.  */
ce426f
-#define HARDWARE_CONVERT(INSTRUCTION)					\
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 cc = 0;						\
ce426f
-									\
ce426f
-    __asm__ __volatile__ (".machine push       \n\t"			\
ce426f
-			  ".machine \"z9-109\" \n\t"			\
ce426f
-			  ".machinemode \"zarch_nohighgprs\"\n\t"	\
ce426f
-			  "0: " INSTRUCTION "  \n\t"			\
ce426f
-			  ".machine pop        \n\t"			\
ce426f
-			  "   jo     0b        \n\t"			\
ce426f
-			  "   ipm    %2        \n"			\
ce426f
-			  : "+a" (pOutput), "+a" (pInput), "+d" (cc),	\
ce426f
-			    "+d" (outlen), "+d" (inlen)			\
ce426f
-			  :						\
ce426f
-			  : "cc", "memory");				\
ce426f
-									\
ce426f
-    inptr = pInput;							\
ce426f
-    outptr = pOutput;							\
ce426f
-    cc >>= 28;								\
ce426f
-									\
ce426f
-    if (cc == 1)							\
ce426f
-      {									\
ce426f
-	result = __GCONV_FULL_OUTPUT;					\
ce426f
-      }									\
ce426f
-    else if (cc == 2)							\
ce426f
-      {									\
ce426f
-	result = __GCONV_ILLEGAL_INPUT;					\
ce426f
-      }									\
ce426f
-  }
ce426f
-
ce426f
 #define PREPARE_LOOP							\
ce426f
   enum direction dir = ((struct utf16_data *) step->__data)->dir;	\
ce426f
   int emit_bom = ((struct utf16_data *) step->__data)->emit_bom;	\
ce426f
@@ -310,7 +274,7 @@ gconv_end (struct __gconv_step *data)
ce426f
 		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
ce426f
 		  /* Calculate remaining uint16_t values in loaded vrs.  */ \
ce426f
 		  "12: lghi %[R_TMP2],16\n\t"				\
ce426f
-		  "    sgr %[R_TMP2],%[R_TMP]\n\t"			\
ce426f
+		  "    slgr %[R_TMP2],%[R_TMP]\n\t"			\
ce426f
 		  "    srl %[R_TMP2],1\n\t"				\
ce426f
 		  "    llh %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
 		  "    aghi %[R_OUTLEN],-4\n\t"				\
ce426f
@@ -437,7 +401,7 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
     uint32_t c = get32 (inptr);						\
ce426f
 									\
ce426f
     if (__builtin_expect (c <= 0xd7ff, 1)				\
ce426f
-	|| (c >=0xdc00 && c <= 0xffff))					\
ce426f
+	|| (c > 0xdfff && c <= 0xffff))					\
ce426f
       {									\
ce426f
 	/* Two UTF-16 chars.  */					\
ce426f
 	put16 (outptr, c);						\
ce426f
@@ -475,29 +439,10 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
     inptr += 4;								\
ce426f
   }
ce426f
 
ce426f
-#define BODY_TO_ETF3EH							\
ce426f
-  {									\
ce426f
-    HARDWARE_CONVERT ("cu42 %0, %1");					\
ce426f
-									\
ce426f
-    if (__glibc_likely (inptr == inend)					\
ce426f
-	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
-      break;								\
ce426f
-									\
ce426f
-    if (inptr + 4 > inend)						\
ce426f
-      {									\
ce426f
-	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
-	break;								\
ce426f
-      }									\
ce426f
-									\
ce426f
-    STANDARD_TO_LOOP_ERR_HANDLER (4);					\
ce426f
-  }
ce426f
-
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
+    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
@@ -509,8 +454,8 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
 		  CONVERT_32BIT_SIZE_T ([R_OUTLEN])			\
ce426f
 		  /* Loop which handles UTF-16 chars			\
ce426f
 		     ch < 0xd800 || (ch > 0xdfff && ch < 0x10000).  */	\
ce426f
-		  "0:  clgijl %[R_INLEN],32,20f\n\t"			\
ce426f
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "0:  clgijl %[R_INLEN],32,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
 		  "1:  vlm %%v16,%%v17,0(%[R_IN])\n\t"			\
ce426f
 		  "    lghi %[R_TMP2],0\n\t"				\
ce426f
 		  /* Shorten to UTF-16.  */				\
ce426f
@@ -526,9 +471,15 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
 		  "    aghi %[R_INLEN],-32\n\t"				\
ce426f
 		  "    aghi %[R_OUTLEN],-16\n\t"			\
ce426f
 		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
ce426f
-		  "    clgijl %[R_INLEN],32,20f\n\t"			\
ce426f
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],32,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
 		  "    j 1b\n\t"					\
ce426f
+		  /* Calculate remaining uint32_t values in inptr.  */	\
ce426f
+		  "2:  \n\t"						\
ce426f
+		  "    clgije %[R_INLEN],0,99f\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],4,92f\n\t"			\
ce426f
+		  "    srlg %[R_TMP2],%[R_INLEN],2\n\t"			\
ce426f
+		  "    j 20f\n\t"					\
ce426f
 		  /* Setup to check for ch >= 0xd800 && ch <= 0xdfff	\
ce426f
 		     and check for ch >= 0x10000. (v30, v31)  */	\
ce426f
 		  "9:  .long 0xd800,0xdfff,0x10000,0x10000\n\t"		\
ce426f
@@ -540,21 +491,59 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
 		  "    agr %[R_TMP],%[R_TMP2]\n\t"			\
ce426f
 		  "    srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes.  */ \
ce426f
 		  "    ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store.  */ \
ce426f
-		  "    jl 20f\n\t"					\
ce426f
+		  "    jl 12f\n\t"					\
ce426f
 		  "    vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t"		\
ce426f
 		  /* Update pointers.  */				\
ce426f
 		  "    la %[R_IN],0(%[R_TMP],%[R_IN])\n\t"		\
ce426f
 		  "    slgr %[R_INLEN],%[R_TMP]\n\t"			\
ce426f
 		  "    la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t"		\
ce426f
 		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
ce426f
-		  /* Handles UTF16 surrogates with convert instruction.  */ \
ce426f
-		  "20: cu42 %[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
+		  /* Calculate remaining uint32_t values in vrs.  */	\
ce426f
+		  "12: lghi %[R_TMP2],8\n\t"				\
ce426f
+		  "    srlg %[R_TMP3],%[R_TMP3],1\n\t"			\
ce426f
+		  "    slgr %[R_TMP2],%[R_TMP3]\n\t"			\
ce426f
+		  /* Handle remaining UTF-32 characters.  */		\
ce426f
+		  "20: l %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-4\n\t"				\
ce426f
+		  /* Test if ch is 2byte UTF-16 char. */		\
ce426f
+		  "    clfi %[R_TMP],0xffff\n\t"			\
ce426f
+		  "    jh 21f\n\t"					\
ce426f
+		  /* Handle 2 byte UTF16 char.  */			\
ce426f
+		  "    lgr %[R_TMP3],%[R_TMP]\n\t"			\
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
+		  "    slgfi %[R_OUTLEN],2\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
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 4byte UTF-16 char. */		\
ce426f
+		  "21: clfi %[R_TMP],0x10ffff\n\t"			\
ce426f
+		  "    jh 91f\n\t" /* ch > 0x10ffff is not allowed!  */	\
ce426f
+		  /* Handle 4 byte UTF16 char.  */			\
ce426f
+		  "    slgfi %[R_OUTLEN],4\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    slfi %[R_TMP],0x10000\n\t" /* zabcd = uvwxy - 1.  */ \
ce426f
+		  "    llilf %[R_TMP3],0xd800dc00\n\t"			\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],38,47,6\n\t" /* High surrogate.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],54,63,0\n\t" /* Low surrogate.  */ \
ce426f
+		  "    st %[R_TMP3],0(%[R_OUT])\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
+		  : /* 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] "=d" (tmp2), [R_TMP3] "=a" (tmp3)	\
ce426f
 		    , [R_RES] "+d" (result)				\
ce426f
@@ -567,17 +556,10 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
 		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
ce426f
 		    ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31")	\
ce426f
 		  );							\
ce426f
-    inptr = pInput;							\
ce426f
-    outptr = pOutput;							\
ce426f
-									\
ce426f
     if (__glibc_likely (inptr == inend)					\
ce426f
-	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
+	|| result != __GCONV_ILLEGAL_INPUT)				\
ce426f
       break;								\
ce426f
-    if (inptr + 4 > inend)						\
ce426f
-      {									\
ce426f
-	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
-	break;								\
ce426f
-      }									\
ce426f
+									\
ce426f
     STANDARD_TO_LOOP_ERR_HANDLER (4);					\
ce426f
   }
ce426f
 
ce426f
@@ -590,15 +572,6 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
ce426f
 #define BODY			BODY_TO_C
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_utf16_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 instructions.  */
ce426f
 # define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
@@ -623,10 +596,6 @@ __to_utf16_loop_resolver (unsigned long int dl_hwcap)
ce426f
     return __to_utf16_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_utf16_loop_etf3eh;
ce426f
-  else
ce426f
     return __to_utf16_loop_c;
ce426f
 }
ce426f
 
ce426f
-- 
ce426f
1.8.3.1
ce426f