Debian Codegraph @

Modules/ThirdParty/ZLIB/src/itkzlib-ng/arch/s390/dfltcc_detail.h

raw
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 unsigned char *orig_t2 = t2;
170 size_t t3 = len1 ? *len1 : 0;
171 z_const unsigned char *t4 = op2 ? *op2 : NULL;
172 size_t t5 = len2 ? *len2 : 0;
173 Z_REGISTER int r0 __asm__("r0") = fn;
174 Z_REGISTER void *r1 __asm__("r1") = param;
175 Z_REGISTER unsigned char *r2 __asm__("r2") = t2;
176 Z_REGISTER size_t r3 __asm__("r3") = t3;
177 Z_REGISTER z_const unsigned char *r4 __asm__("r4") = t4;
178 Z_REGISTER size_t r5 __asm__("r5") = t5;
179 int cc;
180
181 __asm__ volatile(
182 #ifdef HAVE_SYS_SDT_H
183 STAP_PROBE_ASM(zlib, dfltcc_entry, STAP_PROBE_ASM_TEMPLATE(5))
184 #endif
185 ".insn rrf,0xb9390000,%[r2],%[r4],%[hist],0\n"
186 #ifdef HAVE_SYS_SDT_H
187 STAP_PROBE_ASM(zlib, dfltcc_exit, STAP_PROBE_ASM_TEMPLATE(5))
188 #endif
189 "ipm %[cc]\n"
190 : [r2] "+r" (r2)
191 , [r3] "+r" (r3)
192 , [r4] "+r" (r4)
193 , [r5] "+r" (r5)
194 , [cc] "=r" (cc)
195 : [r0] "r" (r0)
196 , [r1] "r" (r1)
197 , [hist] "r" (hist)
198 #ifdef HAVE_SYS_SDT_H
199 , STAP_PROBE_ASM_OPERANDS(5, r2, r3, r4, r5, hist)
200 #endif
201 : "cc", "memory");
202 t2 = r2; t3 = r3; t4 = r4; t5 = r5;
203
204 switch (fn & DFLTCC_FN_MASK) {
205 case DFLTCC_QAF:
206 __msan_unpoison(param, DFLTCC_SIZEOF_QAF);
207 break;
208 case DFLTCC_GDHT:
209 __msan_unpoison(param, DFLTCC_SIZEOF_GDHT_V0);
210 break;
211 case DFLTCC_CMPR:
212 __msan_unpoison(param, DFLTCC_SIZEOF_CMPR_XPND_V0);
213 __msan_unpoison(orig_t2, t2 - orig_t2 + (((struct dfltcc_param_v0 *)param)->sbb == 0 ? 0 : 1));
214 break;
215 case DFLTCC_XPND:
216 __msan_unpoison(param, DFLTCC_SIZEOF_CMPR_XPND_V0);
217 __msan_unpoison(orig_t2, t2 - orig_t2);
218 break;
219 }
220
221 if (op1)
222 *op1 = t2;
223 if (len1)
224 *len1 = t3;
225 if (op2)
226 *op2 = t4;
227 if (len2)
228 *len2 = t5;
229 return (cc >> 28) & 3;
230 }
231
232 /*
233 Extension of inflate_state and deflate_state. Must be doubleword-aligned.
234 */
235 struct dfltcc_state {
236 struct dfltcc_param_v0 param; /* Parameter block. */
237 struct dfltcc_qaf_param af; /* Available functions. */
238 char msg[64]; /* Buffer for strm->msg */
239 };
240
241 #define ALIGN_UP(p, size) (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1))
242
243 #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((char *)(state) + ALIGN_UP(sizeof(*state), 8)))
244
245 static inline void *dfltcc_alloc_state(PREFIX3(streamp) strm, uInt size, uInt extension_size) {
246 return ZALLOC(strm, 1, ALIGN_UP(size, 8) + extension_size);
247 }
248
249 static inline void dfltcc_reset_state(struct dfltcc_state *dfltcc_state) {
250 /* Initialize available functions */
251 if (is_dfltcc_enabled()) {
252 dfltcc(DFLTCC_QAF, &dfltcc_state->param, NULL, NULL, NULL, NULL, NULL);
253 memmove(&dfltcc_state->af, &dfltcc_state->param, sizeof(dfltcc_state->af));
254 } else
255 memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
256
257 /* Initialize parameter block */
258 memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param));
259 dfltcc_state->param.nt = 1;
260 dfltcc_state->param.ribm = DFLTCC_RIBM;
261 }
262
263 static inline void dfltcc_copy_state(void *dst, const void *src, uInt size, uInt extension_size) {
264 memcpy(dst, src, ALIGN_UP(size, 8) + extension_size);
265 }
266
267 static inline void append_history(struct dfltcc_param_v0 *param, unsigned char *history,
268 const unsigned char *buf, uInt count) {
269 size_t offset;
270 size_t n;
271
272 /* Do not use more than 32K */
273 if (count > HB_SIZE) {
274 buf += count - HB_SIZE;
275 count = HB_SIZE;
276 }
277 offset = (param->ho + param->hl) % HB_SIZE;
278 if (offset + count <= HB_SIZE)
279 /* Circular history buffer does not wrap - copy one chunk */
280 memcpy(history + offset, buf, count);
281 else {
282 /* Circular history buffer wraps - copy two chunks */
283 n = HB_SIZE - offset;
284 memcpy(history + offset, buf, n);
285 memcpy(history, buf + n, count - n);
286 }
287 n = param->hl + count;
288 if (n <= HB_SIZE)
289 /* All history fits into buffer - no need to discard anything */
290 param->hl = n;
291 else {
292 /* History does not fit into buffer - discard extra bytes */
293 param->ho = (param->ho + (n - HB_SIZE)) % HB_SIZE;
294 param->hl = HB_SIZE;
295 }
296 }
297
298 static inline void get_history(struct dfltcc_param_v0 *param, const unsigned char *history,
299 unsigned char *buf) {
300 if (param->ho + param->hl <= HB_SIZE)
301 /* Circular history buffer does not wrap - copy one chunk */
302 memcpy(buf, history + param->ho, param->hl);
303 else {
304 /* Circular history buffer wraps - copy two chunks */
305 memcpy(buf, history + param->ho, HB_SIZE - param->ho);
306 memcpy(buf + HB_SIZE - param->ho, history, param->ho + param->hl - HB_SIZE);
307 }
308 }