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