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