00db10
From cddea07761373ce92dc75e8306212d71fa2043ba Mon Sep 17 00:00:00 2001
00db10
From: Stefan Liebler <stli@linux.vnet.ibm.com>
00db10
Date: Mon, 7 Nov 2016 17:18:39 +0100
00db10
Subject: [PATCH 15/17] S390: Fix utf32 to utf16 handling of low surrogates
00db10
 (disable cu42).
00db10
00db10
Upstream commit a42a95c43133d69b1108f582cffa0f6986a9c3da
00db10
00db10
According to the latest Unicode standard, a conversion from/to UTF-xx has
00db10
to report an error if the character value is in range of an utf16 surrogate
00db10
(0xd800..0xdfff). See https://sourceware.org/ml/libc-help/2015-12/msg00015.html.
00db10
00db10
Thus the cu42 instruction, which converts from utf32 to utf16,  has to be
00db10
disabled because it does not report an error in case of a value in range of
00db10
a low surrogate (0xdc00..0xdfff). The etf3eh variant is removed and the c,
00db10
vector variant is adjusted to handle the value in range of an utf16 low
00db10
surrogate correctly.
00db10
00db10
ChangeLog:
00db10
00db10
	* sysdeps/s390/utf16-utf32-z9.c: Disable cu42 instruction and report
00db10
	an error in case of a value in range of an utf16 low surrogate.
00db10
---
00db10
 sysdeps/s390/utf16-utf32-z9.c | 155 +++++++++++++++++-------------------------
00db10
 1 file changed, 62 insertions(+), 93 deletions(-)
00db10
00db10
diff --git a/sysdeps/s390/utf16-utf32-z9.c b/sysdeps/s390/utf16-utf32-z9.c
00db10
index 8d42ab8..5d2ac44 100644
00db10
--- a/sysdeps/s390/utf16-utf32-z9.c
00db10
+++ b/sysdeps/s390/utf16-utf32-z9.c
00db10
@@ -145,42 +145,6 @@ gconv_end (struct __gconv_step *data)
00db10
   free (data->__data);
00db10
 }
00db10
 
00db10
-/* The macro for the hardware loop.  This is used for both
00db10
-   directions.  */
00db10
-#define HARDWARE_CONVERT(INSTRUCTION)					\
00db10
-  {									\
00db10
-    register const unsigned char* pInput __asm__ ("8") = inptr;		\
00db10
-    register size_t inlen __asm__ ("9") = inend - inptr;		\
00db10
-    register unsigned char* pOutput __asm__ ("10") = outptr;		\
00db10
-    register size_t outlen __asm__("11") = outend - outptr;		\
00db10
-    unsigned long cc = 0;						\
00db10
-									\
00db10
-    __asm__ __volatile__ (".machine push       \n\t"			\
00db10
-			  ".machine \"z9-109\" \n\t"			\
00db10
-			  ".machinemode \"zarch_nohighgprs\"\n\t"	\
00db10
-			  "0: " INSTRUCTION "  \n\t"			\
00db10
-			  ".machine pop        \n\t"			\
00db10
-			  "   jo     0b        \n\t"			\
00db10
-			  "   ipm    %2        \n"			\
00db10
-			  : "+a" (pOutput), "+a" (pInput), "+d" (cc),	\
00db10
-			    "+d" (outlen), "+d" (inlen)			\
00db10
-			  :						\
00db10
-			  : "cc", "memory");				\
00db10
-									\
00db10
-    inptr = pInput;							\
00db10
-    outptr = pOutput;							\
00db10
-    cc >>= 28;								\
00db10
-									\
00db10
-    if (cc == 1)							\
00db10
-      {									\
00db10
-	result = __GCONV_FULL_OUTPUT;					\
00db10
-      }									\
00db10
-    else if (cc == 2)							\
00db10
-      {									\
00db10
-	result = __GCONV_ILLEGAL_INPUT;					\
00db10
-      }									\
00db10
-  }
00db10
-
00db10
 #define PREPARE_LOOP							\
00db10
   enum direction dir = ((struct utf16_data *) step->__data)->dir;	\
00db10
   int emit_bom = ((struct utf16_data *) step->__data)->emit_bom;	\
00db10
@@ -310,7 +274,7 @@ gconv_end (struct __gconv_step *data)
00db10
 		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
00db10
 		  /* Calculate remaining uint16_t values in loaded vrs.  */ \
00db10
 		  "12: lghi %[R_TMP2],16\n\t"				\
00db10
-		  "    sgr %[R_TMP2],%[R_TMP]\n\t"			\
00db10
+		  "    slgr %[R_TMP2],%[R_TMP]\n\t"			\
00db10
 		  "    srl %[R_TMP2],1\n\t"				\
00db10
 		  "    llh %[R_TMP],0(%[R_IN])\n\t"			\
00db10
 		  "    aghi %[R_OUTLEN],-4\n\t"				\
00db10
@@ -437,7 +401,7 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
     uint32_t c = get32 (inptr);						\
00db10
 									\
00db10
     if (__builtin_expect (c <= 0xd7ff, 1)				\
00db10
-	|| (c >=0xdc00 && c <= 0xffff))					\
00db10
+	|| (c > 0xdfff && c <= 0xffff))					\
00db10
       {									\
00db10
 	/* Two UTF-16 chars.  */					\
00db10
 	put16 (outptr, c);						\
00db10
@@ -475,29 +439,10 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
     inptr += 4;								\
00db10
   }
00db10
 
00db10
-#define BODY_TO_ETF3EH							\
00db10
-  {									\
00db10
-    HARDWARE_CONVERT ("cu42 %0, %1");					\
00db10
-									\
00db10
-    if (__glibc_likely (inptr == inend)					\
00db10
-	|| result == __GCONV_FULL_OUTPUT)				\
00db10
-      break;								\
00db10
-									\
00db10
-    if (inptr + 4 > inend)						\
00db10
-      {									\
00db10
-	result = __GCONV_INCOMPLETE_INPUT;				\
00db10
-	break;								\
00db10
-      }									\
00db10
-									\
00db10
-    STANDARD_TO_LOOP_ERR_HANDLER (4);					\
00db10
-  }
00db10
-
00db10
 #define BODY_TO_VX							\
00db10
   {									\
00db10
-    register const unsigned char* pInput asm ("8") = inptr;		\
00db10
-    register size_t inlen asm ("9") = inend - inptr;			\
00db10
-    register unsigned char* pOutput asm ("10") = outptr;		\
00db10
-    register size_t outlen asm("11") = outend - outptr;			\
00db10
+    size_t inlen = inend - inptr;					\
00db10
+    size_t outlen = outend - outptr;					\
00db10
     unsigned long tmp, tmp2, tmp3;					\
00db10
     asm volatile (".machine push\n\t"					\
00db10
 		  ".machine \"z13\"\n\t"				\
00db10
@@ -509,8 +454,8 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
 		  CONVERT_32BIT_SIZE_T ([R_OUTLEN])			\
00db10
 		  /* Loop which handles UTF-16 chars			\
00db10
 		     ch < 0xd800 || (ch > 0xdfff && ch < 0x10000).  */	\
00db10
-		  "0:  clgijl %[R_INLEN],32,20f\n\t"			\
00db10
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
00db10
+		  "0:  clgijl %[R_INLEN],32,2f\n\t"			\
00db10
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
00db10
 		  "1:  vlm %%v16,%%v17,0(%[R_IN])\n\t"			\
00db10
 		  "    lghi %[R_TMP2],0\n\t"				\
00db10
 		  /* Shorten to UTF-16.  */				\
00db10
@@ -526,9 +471,15 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
 		  "    aghi %[R_INLEN],-32\n\t"				\
00db10
 		  "    aghi %[R_OUTLEN],-16\n\t"			\
00db10
 		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
00db10
-		  "    clgijl %[R_INLEN],32,20f\n\t"			\
00db10
-		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
00db10
+		  "    clgijl %[R_INLEN],32,2f\n\t"			\
00db10
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
00db10
 		  "    j 1b\n\t"					\
00db10
+		  /* Calculate remaining uint32_t values in inptr.  */	\
00db10
+		  "2:  \n\t"						\
00db10
+		  "    clgije %[R_INLEN],0,99f\n\t"			\
00db10
+		  "    clgijl %[R_INLEN],4,92f\n\t"			\
00db10
+		  "    srlg %[R_TMP2],%[R_INLEN],2\n\t"			\
00db10
+		  "    j 20f\n\t"					\
00db10
 		  /* Setup to check for ch >= 0xd800 && ch <= 0xdfff	\
00db10
 		     and check for ch >= 0x10000. (v30, v31)  */	\
00db10
 		  "9:  .long 0xd800,0xdfff,0x10000,0x10000\n\t"		\
00db10
@@ -540,21 +491,59 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
 		  "    agr %[R_TMP],%[R_TMP2]\n\t"			\
00db10
 		  "    srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes.  */ \
00db10
 		  "    ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store.  */ \
00db10
-		  "    jl 20f\n\t"					\
00db10
+		  "    jl 12f\n\t"					\
00db10
 		  "    vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t"		\
00db10
 		  /* Update pointers.  */				\
00db10
 		  "    la %[R_IN],0(%[R_TMP],%[R_IN])\n\t"		\
00db10
 		  "    slgr %[R_INLEN],%[R_TMP]\n\t"			\
00db10
 		  "    la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t"		\
00db10
 		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
00db10
-		  /* Handles UTF16 surrogates with convert instruction.  */ \
00db10
-		  "20: cu42 %[R_OUT],%[R_IN]\n\t"			\
00db10
-		  "    jo 0b\n\t" /* Try vector implemenation again.  */ \
00db10
-		  "    lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1.  */ \
00db10
-		  "    lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2.  */ \
00db10
+		  /* Calculate remaining uint32_t values in vrs.  */	\
00db10
+		  "12: lghi %[R_TMP2],8\n\t"				\
00db10
+		  "    srlg %[R_TMP3],%[R_TMP3],1\n\t"			\
00db10
+		  "    slgr %[R_TMP2],%[R_TMP3]\n\t"			\
00db10
+		  /* Handle remaining UTF-32 characters.  */		\
00db10
+		  "20: l %[R_TMP],0(%[R_IN])\n\t"			\
00db10
+		  "    aghi %[R_INLEN],-4\n\t"				\
00db10
+		  /* Test if ch is 2byte UTF-16 char. */		\
00db10
+		  "    clfi %[R_TMP],0xffff\n\t"			\
00db10
+		  "    jh 21f\n\t"					\
00db10
+		  /* Handle 2 byte UTF16 char.  */			\
00db10
+		  "    lgr %[R_TMP3],%[R_TMP]\n\t"			\
00db10
+		  "    nilf %[R_TMP],0xf800\n\t"			\
00db10
+		  "    clfi %[R_TMP],0xd800\n\t"			\
00db10
+		  "    je 91f\n\t" /* Do not accept UTF-16 surrogates.  */ \
00db10
+		  "    slgfi %[R_OUTLEN],2\n\t"				\
00db10
+		  "    jl 90f \n\t"					\
00db10
+		  "    sth %[R_TMP3],0(%[R_OUT])\n\t"			\
00db10
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
00db10
+		  "    la %[R_OUT],2(%[R_OUT])\n\t"			\
00db10
+		  "    brctg %[R_TMP2],20b\n\t"				\
00db10
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
00db10
+		  /* Test if ch is 4byte UTF-16 char. */		\
00db10
+		  "21: clfi %[R_TMP],0x10ffff\n\t"			\
00db10
+		  "    jh 91f\n\t" /* ch > 0x10ffff is not allowed!  */	\
00db10
+		  /* Handle 4 byte UTF16 char.  */			\
00db10
+		  "    slgfi %[R_OUTLEN],4\n\t"				\
00db10
+		  "    jl 90f \n\t"					\
00db10
+		  "    slfi %[R_TMP],0x10000\n\t" /* zabcd = uvwxy - 1.  */ \
00db10
+		  "    llilf %[R_TMP3],0xd800dc00\n\t"			\
00db10
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
00db10
+		  "    risbgn %[R_TMP3],%[R_TMP],38,47,6\n\t" /* High surrogate.  */ \
00db10
+		  "    risbgn %[R_TMP3],%[R_TMP],54,63,0\n\t" /* Low surrogate.  */ \
00db10
+		  "    st %[R_TMP3],0(%[R_OUT])\n\t"			\
00db10
+		  "    la %[R_OUT],4(%[R_OUT])\n\t"			\
00db10
+		  "    brctg %[R_TMP2],20b\n\t"				\
00db10
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
00db10
+		  "92: lghi %[R_RES],%[RES_IN_FULL]\n\t"		\
00db10
+		  "    j 99f\n\t"					\
00db10
+		  "91: lghi %[R_RES],%[RES_IN_ILL]\n\t"			\
00db10
+		  "    j 99f\n\t"					\
00db10
+		  "90: lghi %[R_RES],%[RES_OUT_FULL]\n\t"		\
00db10
+		  "99: \n\t"						\
00db10
 		  ".machine pop"					\
00db10
-		  : /* outputs */ [R_IN] "+a" (pInput)			\
00db10
-		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput)	\
00db10
+		  : /* outputs */ [R_IN] "+a" (inptr)			\
00db10
+		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (outptr)	\
00db10
 		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp)	\
00db10
 		    , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3)	\
00db10
 		    , [R_RES] "+d" (result)				\
00db10
@@ -567,17 +556,10 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
 		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
00db10
 		    ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31")	\
00db10
 		  );							\
00db10
-    inptr = pInput;							\
00db10
-    outptr = pOutput;							\
00db10
-									\
00db10
     if (__glibc_likely (inptr == inend)					\
00db10
-	|| result == __GCONV_FULL_OUTPUT)				\
00db10
+	|| result != __GCONV_ILLEGAL_INPUT)				\
00db10
       break;								\
00db10
-    if (inptr + 4 > inend)						\
00db10
-      {									\
00db10
-	result = __GCONV_INCOMPLETE_INPUT;				\
00db10
-	break;								\
00db10
-      }									\
00db10
+									\
00db10
     STANDARD_TO_LOOP_ERR_HANDLER (4);					\
00db10
   }
00db10
 
00db10
@@ -590,15 +572,6 @@ strong_alias (__from_utf16_loop_c_single, __from_utf16_loop_single)
00db10
 #define BODY			BODY_TO_C
00db10
 #include <iconv/loop.c>
00db10
 
00db10
-/* Generate loop-function with hardware utf-convert instruction.  */
00db10
-#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
00db10
-#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
00db10
-#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
00db10
-#define LOOPFCT			__to_utf16_loop_etf3eh
00db10
-#define LOOP_NEED_FLAGS
00db10
-#define BODY			BODY_TO_ETF3EH
00db10
-#include <iconv/loop.c>
00db10
-
00db10
 #if defined HAVE_S390_VX_ASM_SUPPORT
00db10
 /* Generate loop-function with hardware vector instructions.  */
00db10
 # define MIN_NEEDED_INPUT	MIN_NEEDED_TO
00db10
@@ -623,10 +596,6 @@ __to_utf16_loop_resolver (unsigned long int dl_hwcap)
00db10
     return __to_utf16_loop_vx;
00db10
   else
00db10
 #endif
00db10
-  if (dl_hwcap & HWCAP_S390_ZARCH && dl_hwcap & HWCAP_S390_HIGH_GPRS
00db10
-      && dl_hwcap & HWCAP_S390_ETF3EH)
00db10
-    return __to_utf16_loop_etf3eh;
00db10
-  else
00db10
     return __to_utf16_loop_c;
00db10
 }
00db10
 
00db10
-- 
00db10
1.8.3.1
00db10