60c5b2
* configure.ac (AC_CHECK_HEADERS_ONCE): Add feature detection for
60c5b2
sys/sdt.h probes.
60c5b2
* dfltcc.c (dfltcc_cc): Minor formatting improvements.
60c5b2
(HB_BITS): Remove.
60c5b2
(HB_SIZE): Likewise.
60c5b2
(is_dfltcc_enabled): Fix buffer overrun on newer models and incomplete
60c5b2
initialization on older models.
60c5b2
Add machine mode hint.
60c5b2
(dfltcc): Use sys/sdt.h feature detection.
60c5b2
(bi_load): New function.
60c5b2
(bi_close_block): Use bi_load.
60c5b2
(close_stream): Fix overwriting the End-of-block Symbol.
60c5b2
(dfltcc_deflate): Fix losing partial byte on flush.
60c5b2
Fix setting Block-Continuation Flag when DFLTCC-CMPR outputs 0 bits and
60c5b2
requests a retry.
60c5b2
Minor formatting improvements.
60c5b2
(dfltcc_inflate): Retry immediately if requested.
60c5b2
Print the hardware error code and flush the output buffer on error.
60c5b2
Minor formatting improvements.
60c5b2
* tests/hufts: Ignore the hardware error code.
60c5b2
---
60c5b2
 configure.ac |  2 +-
60c5b2
 dfltcc.c     | 76 ++++++++++++++++++++++++++++++++++++----------------
60c5b2
 tests/hufts  |  2 ++
60c5b2
 3 files changed, 56 insertions(+), 24 deletions(-)
60c5b2
60c5b2
diff --git a/configure.ac b/configure.ac
60c5b2
index 76ac26f..b4aea34 100644
60c5b2
--- a/configure.ac
60c5b2
+++ b/configure.ac
60c5b2
@@ -263,7 +263,7 @@ AC_SUBST([ASFLAGS_config])
60c5b2
 AC_ISC_POSIX
60c5b2
 AC_C_CONST
60c5b2
 AC_HEADER_STDC
60c5b2
-AC_CHECK_HEADERS_ONCE(fcntl.h limits.h memory.h time.h)
60c5b2
+AC_CHECK_HEADERS_ONCE(fcntl.h limits.h memory.h time.h sys/sdt.h)
60c5b2
 AC_CHECK_FUNCS_ONCE([chown fchmod fchown lstat siginterrupt])
60c5b2
 AC_HEADER_DIRENT
60c5b2
 AC_TYPE_SIGNAL
60c5b2
diff --git a/dfltcc.c b/dfltcc.c
60c5b2
index ba62968..ed3be8d 100644
60c5b2
--- a/dfltcc.c
60c5b2
+++ b/dfltcc.c
60c5b2
@@ -22,7 +22,7 @@
60c5b2
 #include <stdbool.h>
60c5b2
 #include <stdlib.h>
60c5b2
 
60c5b2
-#ifdef DFLTCC_USDT
60c5b2
+#ifdef HAVE_SYS_SDT_H
60c5b2
 # include <sys/sdt.h>
60c5b2
 #endif
60c5b2
 
60c5b2
@@ -39,11 +39,11 @@
60c5b2
 
60c5b2
 typedef enum
60c5b2
 {
60c5b2
- DFLTCC_CC_OK = 0,
60c5b2
- DFLTCC_CC_OP1_TOO_SHORT = 1,
60c5b2
- DFLTCC_CC_OP2_TOO_SHORT = 2,
60c5b2
- DFLTCC_CC_OP2_CORRUPT = 2,
60c5b2
- DFLTCC_CC_AGAIN = 3,
60c5b2
+  DFLTCC_CC_OK = 0,
60c5b2
+  DFLTCC_CC_OP1_TOO_SHORT = 1,
60c5b2
+  DFLTCC_CC_OP2_TOO_SHORT = 2,
60c5b2
+  DFLTCC_CC_OP2_CORRUPT = 2,
60c5b2
+  DFLTCC_CC_AGAIN = 3,
60c5b2
 } dfltcc_cc;
60c5b2
 
60c5b2
 #define DFLTCC_QAF 0
60c5b2
@@ -51,8 +51,6 @@ typedef enum
60c5b2
 #define DFLTCC_CMPR 2
60c5b2
 #define DFLTCC_XPND 4
60c5b2
 #define HBT_CIRCULAR (1 << 7)
60c5b2
-/* #define HB_BITS 15 */
60c5b2
-/* #define HB_SIZE (1 << HB_BITS) */
60c5b2
 #define DFLTCC_FACILITY 151
60c5b2
 #define DFLTCC_FMT0 0
60c5b2
 #define CVT_CRC32 0
60c5b2
@@ -155,9 +153,16 @@ is_dfltcc_enabled (void)
60c5b2
   if (env && !strcmp (env, "0"))
60c5b2
     return 0;
60c5b2
 
60c5b2
-  register int r0 __asm__ ("r0") = sizeof facilities / 8;
60c5b2
-  __asm__ ("stfle %[facilities]\n"
60c5b2
-           : [facilities] "=Q"(facilities) : [r0] "r"(r0) : "cc", "memory");
60c5b2
+  memset (facilities, 0, sizeof facilities);
60c5b2
+  register char r0 __asm__ ("r0") = sizeof facilities / 8 - 1;
60c5b2
+  /* STFLE is supported since z9-109 and only in z/Architecture mode.  When
60c5b2
+   * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
60c5b2
+   * is 64-bit, it's always z/Architecture mode at runtime.  */
60c5b2
+  __asm__ (".machinemode push\n"
60c5b2
+           ".machinemode zarch\n"
60c5b2
+           "stfle %[facilities]\n"
60c5b2
+           ".machinemode pop\n"
60c5b2
+           : [facilities] "=Q"(facilities), [r0] "+r"(r0) :: "cc");
60c5b2
   return is_bit_set (facilities, DFLTCC_FACILITY);
60c5b2
 }
60c5b2
 
60c5b2
@@ -180,12 +185,12 @@ dfltcc (int fn, void *param,
60c5b2
   int cc;
60c5b2
 
60c5b2
   __asm__ volatile (
60c5b2
-#ifdef DFLTCC_USDT
60c5b2
+#ifdef HAVE_SYS_SDT_H
60c5b2
                     STAP_PROBE_ASM (zlib, dfltcc_entry,
60c5b2
                                     STAP_PROBE_ASM_TEMPLATE (5))
60c5b2
 #endif
60c5b2
                     ".insn rrf,0xb9390000,%[r2],%[r4],%[hist],0\n"
60c5b2
-#ifdef DFLTCC_USDT
60c5b2
+#ifdef HAVE_SYS_SDT_H
60c5b2
                     STAP_PROBE_ASM (zlib, dfltcc_exit,
60c5b2
                                     STAP_PROBE_ASM_TEMPLATE (5))
60c5b2
 #endif
60c5b2
@@ -198,7 +203,7 @@ dfltcc (int fn, void *param,
60c5b2
                     : [r0] "r" (r0)
60c5b2
                       , [r1] "r" (r1)
60c5b2
                       , [hist] "r" (hist)
60c5b2
-#ifdef DFLTCC_USDT
60c5b2
+#ifdef HAVE_SYS_SDT_H
60c5b2
                       , STAP_PROBE_ASM_OPERANDS (5, r2, r3, r4, r5, hist)
60c5b2
 #endif
60c5b2
                     : "cc", "memory");
60c5b2
@@ -264,10 +269,16 @@ init_param (union aligned_dfltcc_param_v0 *ctx)
60c5b2
 }
60c5b2
 
60c5b2
 static void
60c5b2
-bi_close_block (struct dfltcc_param_v0 *param)
60c5b2
+bi_load (struct dfltcc_param_v0 *param)
60c5b2
 {
60c5b2
   bi_valid = param->sbb;
60c5b2
   bi_buf = bi_valid == 0 ? 0 : outbuf[outcnt] & ((1 << bi_valid) - 1);
60c5b2
+}
60c5b2
+
60c5b2
+static void
60c5b2
+bi_close_block (struct dfltcc_param_v0 *param)
60c5b2
+{
60c5b2
+  bi_load (param);
60c5b2
   send_bits (bi_reverse (param->eobs >> (15 - param->eobl), param->eobl),
60c5b2
              param->eobl);
60c5b2
   param->bcf = 0;
60c5b2
@@ -278,6 +289,7 @@ close_block (struct dfltcc_param_v0 *param)
60c5b2
 {
60c5b2
   bi_close_block (param);
60c5b2
   bi_windup ();
60c5b2
+  /* bi_windup has written out a possibly partial byte, fix up the position */
60c5b2
   param->sbb = (param->sbb + param->eobl) % 8;
60c5b2
   if (param->sbb != 0)
60c5b2
     {
60c5b2
@@ -291,6 +303,8 @@ close_stream (struct dfltcc_param_v0 *param)
60c5b2
 {
60c5b2
   if (param->bcf)
60c5b2
     bi_close_block (param);
60c5b2
+  else
60c5b2
+    bi_load (param);
60c5b2
   send_bits (1, 3); /* BFINAL=1, BTYPE=00 */
60c5b2
   bi_windup ();
60c5b2
   put_short (0x0000);
60c5b2
@@ -334,7 +348,16 @@ dfltcc_deflate (int pack_level)
60c5b2
     {
60c5b2
       /* Flush the output data.  */
60c5b2
       if (outcnt > OUTBUFSIZ - 8)
60c5b2
-        flush_outbuf ();
60c5b2
+        {
60c5b2
+          if (param->sbb == 0)
60c5b2
+            flush_outbuf ();
60c5b2
+          else
60c5b2
+            {
60c5b2
+              uch partial = outbuf[outcnt];
60c5b2
+              flush_outbuf ();
60c5b2
+              outbuf[outcnt] = partial;
60c5b2
+            }
60c5b2
+        }
60c5b2
 
60c5b2
       /* Close the block.  */
60c5b2
       if (param->bcf && total_in == block_threshold && !param->cf)
60c5b2
@@ -360,14 +383,16 @@ dfltcc_deflate (int pack_level)
60c5b2
         {
60c5b2
           if (total_in == 0 && block_threshold > 0)
60c5b2
             param->htt = HTT_FIXED;
60c5b2
-          else {
60c5b2
-            param->htt = HTT_DYNAMIC;
60c5b2
-            dfltcc_gdht (param);
60c5b2
-          }
60c5b2
+          else
60c5b2
+            {
60c5b2
+              param->htt = HTT_DYNAMIC;
60c5b2
+              dfltcc_gdht (param);
60c5b2
+            }
60c5b2
         }
60c5b2
 
60c5b2
       /* Compress inbuf into outbuf.  */
60c5b2
-      dfltcc_cmpr_xpnd (param, DFLTCC_CMPR);
60c5b2
+      while (dfltcc_cmpr_xpnd (param, DFLTCC_CMPR) == DFLTCC_CC_AGAIN)
60c5b2
+        ;
60c5b2
 
60c5b2
       /* Unmask the input data.  */
60c5b2
       insize += extra;
60c5b2
@@ -413,7 +438,9 @@ dfltcc_inflate (void)
60c5b2
         }
60c5b2
 
60c5b2
         /* Decompress inbuf into outbuf.  */
60c5b2
-        dfltcc_cc cc = dfltcc_cmpr_xpnd (param, DFLTCC_XPND);
60c5b2
+        dfltcc_cc cc;
60c5b2
+        while ((cc = dfltcc_cmpr_xpnd (param, DFLTCC_XPND)) == DFLTCC_CC_AGAIN)
60c5b2
+          ;
60c5b2
         if (cc == DFLTCC_CC_OK)
60c5b2
           {
60c5b2
             /* The entire deflate stream has been successfully decompressed.  */
60c5b2
@@ -422,6 +449,9 @@ dfltcc_inflate (void)
60c5b2
         if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0)
60c5b2
           {
60c5b2
             /* The deflate stream is corrupted.  */
60c5b2
+            fprintf (stderr, "Operation-Ending-Supplemental Code 0x%x\n",
60c5b2
+                     param->oesc);
60c5b2
+            flush_outbuf ();
60c5b2
             return 2;
60c5b2
           }
60c5b2
         /* There must be more data to decompress.  */