Debian Codegraph @

ext/zlib-ng/arch/s390/dfltcc_detail.h

raw symbols SCIP
1 #include "../../zbuild.h"
2 #include <stdio.h>
3
4 #ifdef HAVE_SYS_SDT_H
5 #include <sys/sdt.h>
6 #endif
7
8 /*
9 Tuning parameters.
10 */
11 #ifndef DFLTCC_LEVEL_MASK
12 #define DFLTCC_LEVEL_MASK 0x2
13 #endif
14 #ifndef DFLTCC_BLOCK_SIZE
15 #define DFLTCC_BLOCK_SIZE 1048576
16 #endif
17 #ifndef DFLTCC_FIRST_FHT_BLOCK_SIZE
18 #define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
19 #endif
20 #ifndef DFLTCC_DHT_MIN_SAMPLE_SIZE
21 #define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
22 #endif
23 #ifndef DFLTCC_RIBM
24 #define DFLTCC_RIBM 0
25 #endif
26
27 /*
28 Parameter Block for Query Available Functions.
29 */
30 #define static_assert(c, msg) __attribute__((unused)) static char static_assert_failed_ ## msg[c ? 1 : -1]
31
32 struct dfltcc_qaf_param {
33 char fns[16];
34 char reserved1[8];
35 char fmts[2];
36 char reserved2[6];
37 };
38
39 #define DFLTCC_SIZEOF_QAF 32
40 static_assert(sizeof(struct dfltcc_qaf_param) == DFLTCC_SIZEOF_QAF, qaf);
41
42 static inline int is_bit_set(const char *bits, int n) {
43 return bits[n / 8] & (1 << (7 - (n % 8)));
44 }
45
46 static inline void clear_bit(char *bits, int n) {
47 bits[n / 8] &= ~(1 << (7 - (n % 8)));
48 }
49
50 #define DFLTCC_FACILITY 151
51
52 static inline int is_dfltcc_enabled(void) {
53 uint64_t facilities[(DFLTCC_FACILITY / 64) + 1];
54 Z_REGISTER uint8_t r0 __asm__("r0");
55
56 memset(facilities, 0, sizeof(facilities));
57 r0 = sizeof(facilities) / sizeof(facilities[0]) - 1;
58 /* STFLE is supported since z9-109 and only in z/Architecture mode. When
59 * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
60 * is 64-bit, it's always z/Architecture mode at runtime.
61 */
62 __asm__ volatile(
63 #ifndef __clang__
64 ".machinemode push\n"
65 ".machinemode zarch\n"
66 #endif
67 "stfle %[facilities]\n"
68 #ifndef __clang__
69 ".machinemode pop\n"
70 #endif
71 : [facilities] "=Q" (facilities), [r0] "+r" (r0) :: "cc");
72 return is_bit_set((const char *)facilities, DFLTCC_FACILITY);
73 }
74
75 #define DFLTCC_FMT0 0
76
77 /*
78 Parameter Block for Generate Dynamic-Huffman Table, Compress and Expand.
79 */
80 #define CVT_CRC32 0
81 #define CVT_ADLER32 1
82 #define HTT_FIXED 0
83 #define HTT_DYNAMIC 1
84
85 struct dfltcc_param_v0 {
86 uint16_t pbvn; /* Parameter-Block-Version Number */
87 uint8_t mvn; /* Model-Version Number */
88 uint8_t ribm; /* Reserved for IBM use */
89 uint32_t reserved32 : 31;
90 uint32_t cf : 1; /* Continuation Flag */
91 uint8_t reserved64[8];
92 uint32_t nt : 1; /* New Task */
93 uint32_t reserved129 : 1;
94 uint32_t cvt : 1; /* Check Value Type */
95 uint32_t reserved131 : 1;
96 uint32_t htt : 1; /* Huffman-Table Type */
97 uint32_t bcf : 1; /* Block-Continuation Flag */
98 uint32_t bcc : 1; /* Block Closing Control */
99 uint32_t bhf : 1; /* Block Header Final */
100 uint32_t reserved136 : 1;
101 uint32_t reserved137 : 1;
102 uint32_t dhtgc : 1; /* DHT Generation Control */
103 uint32_t reserved139 : 5;
104 uint32_t reserved144 : 5;
105 uint32_t sbb : 3; /* Sub-Byte Boundary */
106 uint8_t oesc; /* Operation-Ending-Supplemental Code */
107 uint32_t reserved160 : 12;
108 uint32_t ifs : 4; /* Incomplete-Function Status */
109 uint16_t ifl; /* Incomplete-Function Length */
110 uint8_t reserved192[8];
111 uint8_t reserved256[8];
112 uint8_t reserved320[4];
113 uint16_t hl; /* History Length */
114 uint32_t reserved368 : 1;
115 uint16_t ho : 15; /* History Offset */
116 uint32_t cv; /* Check Value */
117 uint32_t eobs : 15; /* End-of-block Symbol */
118 uint32_t reserved431: 1;
119 uint8_t eobl : 4; /* End-of-block Length */
120 uint32_t reserved436 : 12;
121 uint32_t reserved448 : 4;
122 uint16_t cdhtl : 12; /* Compressed-Dynamic-Huffman Table
123 Length */
124 uint8_t reserved464[6];
125 uint8_t cdht[288]; /* Compressed-Dynamic-Huffman Table */
126 uint8_t reserved[24];
127 uint8_t ribm2[8]; /* Reserved for IBM use */
128 uint8_t csb[1152]; /* Continuation-State Buffer */
129 };
130
131 #define DFLTCC_SIZEOF_GDHT_V0 384
132 #define DFLTCC_SIZEOF_CMPR_XPND_V0 1536
133 static_assert(offsetof(struct dfltcc_param_v0, csb) == DFLTCC_SIZEOF_GDHT_V0, gdht_v0);
134 static_assert(sizeof(struct dfltcc_param_v0) == DFLTCC_SIZEOF_CMPR_XPND_V0, cmpr_xpnd_v0);
135
136 static inline z_const char *oesc_msg(char *buf, int oesc) {
137 if (oesc == 0x00)
138 return NULL; /* Successful completion */
139 else {
140 sprintf(buf, "Operation-Ending-Supplemental Code is 0x%.2X", oesc);
141 return buf;
142 }
143 }
144
145 /*
146 C wrapper for the DEFLATE CONVERSION CALL instruction.
147 */
148 typedef enum {
149 DFLTCC_CC_OK = 0,
150 DFLTCC_CC_OP1_TOO_SHORT = 1,
151 DFLTCC_CC_OP2_TOO_SHORT = 2,
152 DFLTCC_CC_OP2_CORRUPT = 2,
153 DFLTCC_CC_AGAIN = 3,
154 } dfltcc_cc;
155
156 #define DFLTCC_QAF 0
157 #define DFLTCC_GDHT 1
158 #define DFLTCC_CMPR 2
159 #define DFLTCC_XPND 4
160 #define HBT_CIRCULAR (1 << 7)
161 #define DFLTCC_FN_MASK ((1 << 7) - 1)
162 #define HB_BITS 15
163 #define HB_SIZE (1 << HB_BITS)
164
165 static inline dfltcc_cc dfltcc(int fn, void *param,
166 unsigned char **op1, size_t *len1,
167 z_const unsigned char **op2, size_t *len2, void *hist) {
168 unsigned char *t2 = op1 ? *op1 : NULL;
169 #ifdef Z_MEMORY_SANITIZER
170 unsigned char *orig_t2 = t2;
171 #endif
172 size_t t3 = len1 ? *len1 : 0;
173 z_const unsigned char *t4 = op2 ? *op2 : NULL;
174 size_t t5 = len2 ? *len2 : 0;
175 Z_REGISTER int r0 __asm__("r0") = fn;
176 Z_REGISTER void *r1 __asm__("r1") = param;
177 Z_REGISTER unsigned char *r2 __asm__("r2") = t2;
178 Z_REGISTER size_t r3 __asm__("r3") = t3;
179 Z_REGISTER z_const unsigned char *r4 __asm__("r4") = t4;
180 Z_REGISTER size_t r5 __asm__("r5") = t5;
181 int cc;
182
183 __asm__ volatile(
184 #ifdef HAVE_SYS_SDT_H
185 STAP_PROBE_ASM(zlib, dfltcc_entry, STAP_PROBE_ASM_TEMPLATE(5))
186 #endif
187 ".insn rrf,0xb9390000,%[r2],%[r4],%[hist],0\n"
188 #ifdef HAVE_SYS_SDT_H
189 STAP_PROBE_ASM(zlib, dfltcc_exit, STAP_PROBE_ASM_TEMPLATE(5))
190 #endif
191 "ipm %[cc]\n"
192 : [r2] "+r" (r2)
193 , [r3] "+r" (r3)
194 , [r4] "+r" (r4)
195 , [r5] "+r" (r5)
196 , [cc] "=r" (cc)
197 : [r0] "r" (r0)
198 , [r1] "r" (r1)
199 , [hist] "r" (hist)
200 #ifdef HAVE_SYS_SDT_H
201 , STAP_PROBE_ASM_OPERANDS(5, r2, r3, r4, r5, hist)
202 #endif
203 : "cc", "memory");
204 t2 = r2; t3 = r3; t4 = r4; t5 = r5;
205
206 #ifdef Z_MEMORY_SANITIZER
207 switch (fn & DFLTCC_FN_MASK) {
208 case DFLTCC_QAF:
209 __msan_unpoison(param, DFLTCC_SIZEOF_QAF);
210 break;
211 case DFLTCC_GDHT:
212 __msan_unpoison(param, DFLTCC_SIZEOF_GDHT_V0);
213 break;
214 case DFLTCC_CMPR:
215 __msan_unpoison(param, DFLTCC_SIZEOF_CMPR_XPND_V0);
216 __msan_unpoison(orig_t2, t2 - orig_t2 + (((struct dfltcc_param_v0 *)param)->sbb == 0 ? 0 : 1));
217 break;
218 case DFLTCC_XPND:
219 __msan_unpoison(param, DFLTCC_SIZEOF_CMPR_XPND_V0);
220 __msan_unpoison(orig_t2, t2 - orig_t2);
221 break;
222 }
223 #endif
224
225 if (op1)
226 *op1 = t2;
227 if (len1)
228 *len1 = t3;
229 if (op2)
230 *op2 = t4;
231 if (len2)
232 *len2 = t5;
233 return (cc >> 28) & 3;
234 }
235
236 /*
237 Extension of inflate_state and deflate_state. Must be doubleword-aligned.
238 */
239 struct dfltcc_state {
240 struct dfltcc_param_v0 param; /* Parameter block. */
241 struct dfltcc_qaf_param af; /* Available functions. */
242 char msg[64]; /* Buffer for strm->msg */
243 };
244
245 #define ALIGN_UP(p, size) (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1))
246
247 #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((char *)(state) + ALIGN_UP(sizeof(*state), 8)))
248
249 static inline void *dfltcc_alloc_state(PREFIX3(streamp) strm, uInt size, uInt extension_size) {
250 return ZALLOC(strm, 1, ALIGN_UP(size, 8) + extension_size);
251 }
252
253 static inline void dfltcc_reset_state(struct dfltcc_state *dfltcc_state) {
254 /* Initialize available functions */
255 if (is_dfltcc_enabled()) {
256 dfltcc(DFLTCC_QAF, &dfltcc_state->param, NULL, NULL, NULL, NULL, NULL);
257 memmove(&dfltcc_state->af, &dfltcc_state->param, sizeof(dfltcc_state->af));
258 } else
259 memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
260
261 /* Initialize parameter block */
262 memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param));
263 dfltcc_state->param.nt = 1;
264 dfltcc_state->param.ribm = DFLTCC_RIBM;
265 }
266
267 static inline void dfltcc_copy_state(void *dst, const void *src, uInt size, uInt extension_size) {
268 memcpy(dst, src, ALIGN_UP(size, 8) + extension_size);
269 }