usblp: do not set TASK_INTERRUPTIBLE before lock
[linux/fpc-iii.git] / arch / s390 / net / bpf_jit_comp.c
blob3bc6b7e43b2494ff54ee2725ea06020c1b58d01c
1 /*
2 * BPF Jit compiler for s390.
4 * Copyright IBM Corp. 2012
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8 #include <linux/moduleloader.h>
9 #include <linux/netdevice.h>
10 #include <linux/if_vlan.h>
11 #include <linux/filter.h>
12 #include <asm/cacheflush.h>
13 #include <asm/processor.h>
14 #include <asm/facility.h>
17 * Conventions:
18 * %r2 = skb pointer
19 * %r3 = offset parameter
20 * %r4 = scratch register / length parameter
21 * %r5 = BPF A accumulator
22 * %r8 = return address
23 * %r9 = save register for skb pointer
24 * %r10 = skb->data
25 * %r11 = skb->len - skb->data_len (headlen)
26 * %r12 = BPF X accumulator
27 * %r13 = literal pool pointer
28 * 0(%r15) - 63(%r15) scratch memory array with BPF_MEMWORDS
30 int bpf_jit_enable __read_mostly;
33 * assembly code in arch/x86/net/bpf_jit.S
35 extern u8 sk_load_word[], sk_load_half[], sk_load_byte[], sk_load_byte_msh[];
36 extern u8 sk_load_word_ind[], sk_load_half_ind[], sk_load_byte_ind[];
38 struct bpf_jit {
39 unsigned int seen;
40 u8 *start;
41 u8 *prg;
42 u8 *mid;
43 u8 *lit;
44 u8 *end;
45 u8 *base_ip;
46 u8 *ret0_ip;
47 u8 *exit_ip;
48 unsigned int off_load_word;
49 unsigned int off_load_half;
50 unsigned int off_load_byte;
51 unsigned int off_load_bmsh;
52 unsigned int off_load_iword;
53 unsigned int off_load_ihalf;
54 unsigned int off_load_ibyte;
57 #define BPF_SIZE_MAX 4096 /* Max size for program */
59 #define SEEN_DATAREF 1 /* might call external helpers */
60 #define SEEN_XREG 2 /* ebx is used */
61 #define SEEN_MEM 4 /* use mem[] for temporary storage */
62 #define SEEN_RET0 8 /* pc_ret0 points to a valid return 0 */
63 #define SEEN_LITERAL 16 /* code uses literals */
64 #define SEEN_LOAD_WORD 32 /* code uses sk_load_word */
65 #define SEEN_LOAD_HALF 64 /* code uses sk_load_half */
66 #define SEEN_LOAD_BYTE 128 /* code uses sk_load_byte */
67 #define SEEN_LOAD_BMSH 256 /* code uses sk_load_byte_msh */
68 #define SEEN_LOAD_IWORD 512 /* code uses sk_load_word_ind */
69 #define SEEN_LOAD_IHALF 1024 /* code uses sk_load_half_ind */
70 #define SEEN_LOAD_IBYTE 2048 /* code uses sk_load_byte_ind */
72 #define EMIT2(op) \
73 ({ \
74 if (jit->prg + 2 <= jit->mid) \
75 *(u16 *) jit->prg = op; \
76 jit->prg += 2; \
79 #define EMIT4(op) \
80 ({ \
81 if (jit->prg + 4 <= jit->mid) \
82 *(u32 *) jit->prg = op; \
83 jit->prg += 4; \
86 #define EMIT4_DISP(op, disp) \
87 ({ \
88 unsigned int __disp = (disp) & 0xfff; \
89 EMIT4(op | __disp); \
92 #define EMIT4_IMM(op, imm) \
93 ({ \
94 unsigned int __imm = (imm) & 0xffff; \
95 EMIT4(op | __imm); \
98 #define EMIT4_PCREL(op, pcrel) \
99 ({ \
100 long __pcrel = ((pcrel) >> 1) & 0xffff; \
101 EMIT4(op | __pcrel); \
104 #define EMIT6(op1, op2) \
105 ({ \
106 if (jit->prg + 6 <= jit->mid) { \
107 *(u32 *) jit->prg = op1; \
108 *(u16 *) (jit->prg + 4) = op2; \
110 jit->prg += 6; \
113 #define EMIT6_DISP(op1, op2, disp) \
114 ({ \
115 unsigned int __disp = (disp) & 0xfff; \
116 EMIT6(op1 | __disp, op2); \
119 #define EMIT6_IMM(op, imm) \
120 ({ \
121 unsigned int __imm = (imm); \
122 EMIT6(op | (__imm >> 16), __imm & 0xffff); \
125 #define EMIT_CONST(val) \
126 ({ \
127 unsigned int ret; \
128 ret = (unsigned int) (jit->lit - jit->base_ip); \
129 jit->seen |= SEEN_LITERAL; \
130 if (jit->lit + 4 <= jit->end) \
131 *(u32 *) jit->lit = val; \
132 jit->lit += 4; \
133 ret; \
136 #define EMIT_FN_CONST(bit, fn) \
137 ({ \
138 unsigned int ret; \
139 ret = (unsigned int) (jit->lit - jit->base_ip); \
140 if (jit->seen & bit) { \
141 jit->seen |= SEEN_LITERAL; \
142 if (jit->lit + 8 <= jit->end) \
143 *(void **) jit->lit = fn; \
144 jit->lit += 8; \
146 ret; \
149 static void bpf_jit_prologue(struct bpf_jit *jit)
151 /* Save registers and create stack frame if necessary */
152 if (jit->seen & SEEN_DATAREF) {
153 /* stmg %r8,%r15,88(%r15) */
154 EMIT6(0xeb8ff058, 0x0024);
155 /* lgr %r14,%r15 */
156 EMIT4(0xb90400ef);
157 /* ahi %r15,<offset> */
158 EMIT4_IMM(0xa7fa0000, (jit->seen & SEEN_MEM) ? -112 : -80);
159 /* stg %r14,152(%r15) */
160 EMIT6(0xe3e0f098, 0x0024);
161 } else if ((jit->seen & SEEN_XREG) && (jit->seen & SEEN_LITERAL))
162 /* stmg %r12,%r13,120(%r15) */
163 EMIT6(0xebcdf078, 0x0024);
164 else if (jit->seen & SEEN_XREG)
165 /* stg %r12,120(%r15) */
166 EMIT6(0xe3c0f078, 0x0024);
167 else if (jit->seen & SEEN_LITERAL)
168 /* stg %r13,128(%r15) */
169 EMIT6(0xe3d0f080, 0x0024);
171 /* Setup literal pool */
172 if (jit->seen & SEEN_LITERAL) {
173 /* basr %r13,0 */
174 EMIT2(0x0dd0);
175 jit->base_ip = jit->prg;
177 jit->off_load_word = EMIT_FN_CONST(SEEN_LOAD_WORD, sk_load_word);
178 jit->off_load_half = EMIT_FN_CONST(SEEN_LOAD_HALF, sk_load_half);
179 jit->off_load_byte = EMIT_FN_CONST(SEEN_LOAD_BYTE, sk_load_byte);
180 jit->off_load_bmsh = EMIT_FN_CONST(SEEN_LOAD_BMSH, sk_load_byte_msh);
181 jit->off_load_iword = EMIT_FN_CONST(SEEN_LOAD_IWORD, sk_load_word_ind);
182 jit->off_load_ihalf = EMIT_FN_CONST(SEEN_LOAD_IHALF, sk_load_half_ind);
183 jit->off_load_ibyte = EMIT_FN_CONST(SEEN_LOAD_IBYTE, sk_load_byte_ind);
185 /* Filter needs to access skb data */
186 if (jit->seen & SEEN_DATAREF) {
187 /* l %r11,<len>(%r2) */
188 EMIT4_DISP(0x58b02000, offsetof(struct sk_buff, len));
189 /* s %r11,<data_len>(%r2) */
190 EMIT4_DISP(0x5bb02000, offsetof(struct sk_buff, data_len));
191 /* lg %r10,<data>(%r2) */
192 EMIT6_DISP(0xe3a02000, 0x0004,
193 offsetof(struct sk_buff, data));
197 static void bpf_jit_epilogue(struct bpf_jit *jit)
199 /* Return 0 */
200 if (jit->seen & SEEN_RET0) {
201 jit->ret0_ip = jit->prg;
202 /* lghi %r2,0 */
203 EMIT4(0xa7290000);
205 jit->exit_ip = jit->prg;
206 /* Restore registers */
207 if (jit->seen & SEEN_DATAREF)
208 /* lmg %r8,%r15,<offset>(%r15) */
209 EMIT6_DISP(0xeb8ff000, 0x0004,
210 (jit->seen & SEEN_MEM) ? 200 : 168);
211 else if ((jit->seen & SEEN_XREG) && (jit->seen & SEEN_LITERAL))
212 /* lmg %r12,%r13,120(%r15) */
213 EMIT6(0xebcdf078, 0x0004);
214 else if (jit->seen & SEEN_XREG)
215 /* lg %r12,120(%r15) */
216 EMIT6(0xe3c0f078, 0x0004);
217 else if (jit->seen & SEEN_LITERAL)
218 /* lg %r13,128(%r15) */
219 EMIT6(0xe3d0f080, 0x0004);
220 /* br %r14 */
221 EMIT2(0x07fe);
225 * make sure we dont leak kernel information to user
227 static void bpf_jit_noleaks(struct bpf_jit *jit, struct sock_filter *filter)
229 /* Clear temporary memory if (seen & SEEN_MEM) */
230 if (jit->seen & SEEN_MEM)
231 /* xc 0(64,%r15),0(%r15) */
232 EMIT6(0xd73ff000, 0xf000);
233 /* Clear X if (seen & SEEN_XREG) */
234 if (jit->seen & SEEN_XREG)
235 /* lhi %r12,0 */
236 EMIT4(0xa7c80000);
237 /* Clear A if the first register does not set it. */
238 switch (filter[0].code) {
239 case BPF_S_LD_W_ABS:
240 case BPF_S_LD_H_ABS:
241 case BPF_S_LD_B_ABS:
242 case BPF_S_LD_W_LEN:
243 case BPF_S_LD_W_IND:
244 case BPF_S_LD_H_IND:
245 case BPF_S_LD_B_IND:
246 case BPF_S_LD_IMM:
247 case BPF_S_LD_MEM:
248 case BPF_S_MISC_TXA:
249 case BPF_S_ANC_PROTOCOL:
250 case BPF_S_ANC_PKTTYPE:
251 case BPF_S_ANC_IFINDEX:
252 case BPF_S_ANC_MARK:
253 case BPF_S_ANC_QUEUE:
254 case BPF_S_ANC_HATYPE:
255 case BPF_S_ANC_RXHASH:
256 case BPF_S_ANC_CPU:
257 case BPF_S_ANC_VLAN_TAG:
258 case BPF_S_ANC_VLAN_TAG_PRESENT:
259 case BPF_S_RET_K:
260 /* first instruction sets A register */
261 break;
262 default: /* A = 0 */
263 /* lhi %r5,0 */
264 EMIT4(0xa7580000);
268 static int bpf_jit_insn(struct bpf_jit *jit, struct sock_filter *filter,
269 unsigned int *addrs, int i, int last)
271 unsigned int K;
272 int offset;
273 unsigned int mask;
275 K = filter->k;
276 switch (filter->code) {
277 case BPF_S_ALU_ADD_X: /* A += X */
278 jit->seen |= SEEN_XREG;
279 /* ar %r5,%r12 */
280 EMIT2(0x1a5c);
281 break;
282 case BPF_S_ALU_ADD_K: /* A += K */
283 if (!K)
284 break;
285 if (K <= 16383)
286 /* ahi %r5,<K> */
287 EMIT4_IMM(0xa75a0000, K);
288 else if (test_facility(21))
289 /* alfi %r5,<K> */
290 EMIT6_IMM(0xc25b0000, K);
291 else
292 /* a %r5,<d(K)>(%r13) */
293 EMIT4_DISP(0x5a50d000, EMIT_CONST(K));
294 break;
295 case BPF_S_ALU_SUB_X: /* A -= X */
296 jit->seen |= SEEN_XREG;
297 /* sr %r5,%r12 */
298 EMIT2(0x1b5c);
299 break;
300 case BPF_S_ALU_SUB_K: /* A -= K */
301 if (!K)
302 break;
303 if (K <= 16384)
304 /* ahi %r5,-K */
305 EMIT4_IMM(0xa75a0000, -K);
306 else if (test_facility(21))
307 /* alfi %r5,-K */
308 EMIT6_IMM(0xc25b0000, -K);
309 else
310 /* s %r5,<d(K)>(%r13) */
311 EMIT4_DISP(0x5b50d000, EMIT_CONST(K));
312 break;
313 case BPF_S_ALU_MUL_X: /* A *= X */
314 jit->seen |= SEEN_XREG;
315 /* msr %r5,%r12 */
316 EMIT4(0xb252005c);
317 break;
318 case BPF_S_ALU_MUL_K: /* A *= K */
319 if (K <= 16383)
320 /* mhi %r5,K */
321 EMIT4_IMM(0xa75c0000, K);
322 else if (test_facility(34))
323 /* msfi %r5,<K> */
324 EMIT6_IMM(0xc2510000, K);
325 else
326 /* ms %r5,<d(K)>(%r13) */
327 EMIT4_DISP(0x7150d000, EMIT_CONST(K));
328 break;
329 case BPF_S_ALU_DIV_X: /* A /= X */
330 jit->seen |= SEEN_XREG | SEEN_RET0;
331 /* ltr %r12,%r12 */
332 EMIT2(0x12cc);
333 /* jz <ret0> */
334 EMIT4_PCREL(0xa7840000, (jit->ret0_ip - jit->prg));
335 /* lhi %r4,0 */
336 EMIT4(0xa7480000);
337 /* dlr %r4,%r12 */
338 EMIT4(0xb997004c);
339 break;
340 case BPF_S_ALU_DIV_K: /* A /= K */
341 if (K == 1)
342 break;
343 /* lhi %r4,0 */
344 EMIT4(0xa7480000);
345 /* dl %r4,<d(K)>(%r13) */
346 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K));
347 break;
348 case BPF_S_ALU_MOD_X: /* A %= X */
349 jit->seen |= SEEN_XREG | SEEN_RET0;
350 /* ltr %r12,%r12 */
351 EMIT2(0x12cc);
352 /* jz <ret0> */
353 EMIT4_PCREL(0xa7840000, (jit->ret0_ip - jit->prg));
354 /* lhi %r4,0 */
355 EMIT4(0xa7480000);
356 /* dlr %r4,%r12 */
357 EMIT4(0xb997004c);
358 /* lr %r5,%r4 */
359 EMIT2(0x1854);
360 break;
361 case BPF_S_ALU_MOD_K: /* A %= K */
362 if (K == 1) {
363 /* lhi %r5,0 */
364 EMIT4(0xa7580000);
365 break;
367 /* lhi %r4,0 */
368 EMIT4(0xa7480000);
369 /* dl %r4,<d(K)>(%r13) */
370 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K));
371 /* lr %r5,%r4 */
372 EMIT2(0x1854);
373 break;
374 case BPF_S_ALU_AND_X: /* A &= X */
375 jit->seen |= SEEN_XREG;
376 /* nr %r5,%r12 */
377 EMIT2(0x145c);
378 break;
379 case BPF_S_ALU_AND_K: /* A &= K */
380 if (test_facility(21))
381 /* nilf %r5,<K> */
382 EMIT6_IMM(0xc05b0000, K);
383 else
384 /* n %r5,<d(K)>(%r13) */
385 EMIT4_DISP(0x5450d000, EMIT_CONST(K));
386 break;
387 case BPF_S_ALU_OR_X: /* A |= X */
388 jit->seen |= SEEN_XREG;
389 /* or %r5,%r12 */
390 EMIT2(0x165c);
391 break;
392 case BPF_S_ALU_OR_K: /* A |= K */
393 if (test_facility(21))
394 /* oilf %r5,<K> */
395 EMIT6_IMM(0xc05d0000, K);
396 else
397 /* o %r5,<d(K)>(%r13) */
398 EMIT4_DISP(0x5650d000, EMIT_CONST(K));
399 break;
400 case BPF_S_ANC_ALU_XOR_X: /* A ^= X; */
401 case BPF_S_ALU_XOR_X:
402 jit->seen |= SEEN_XREG;
403 /* xr %r5,%r12 */
404 EMIT2(0x175c);
405 break;
406 case BPF_S_ALU_XOR_K: /* A ^= K */
407 if (!K)
408 break;
409 /* x %r5,<d(K)>(%r13) */
410 EMIT4_DISP(0x5750d000, EMIT_CONST(K));
411 break;
412 case BPF_S_ALU_LSH_X: /* A <<= X; */
413 jit->seen |= SEEN_XREG;
414 /* sll %r5,0(%r12) */
415 EMIT4(0x8950c000);
416 break;
417 case BPF_S_ALU_LSH_K: /* A <<= K */
418 if (K == 0)
419 break;
420 /* sll %r5,K */
421 EMIT4_DISP(0x89500000, K);
422 break;
423 case BPF_S_ALU_RSH_X: /* A >>= X; */
424 jit->seen |= SEEN_XREG;
425 /* srl %r5,0(%r12) */
426 EMIT4(0x8850c000);
427 break;
428 case BPF_S_ALU_RSH_K: /* A >>= K; */
429 if (K == 0)
430 break;
431 /* srl %r5,K */
432 EMIT4_DISP(0x88500000, K);
433 break;
434 case BPF_S_ALU_NEG: /* A = -A */
435 /* lnr %r5,%r5 */
436 EMIT2(0x1155);
437 break;
438 case BPF_S_JMP_JA: /* ip += K */
439 offset = addrs[i + K] + jit->start - jit->prg;
440 EMIT4_PCREL(0xa7f40000, offset);
441 break;
442 case BPF_S_JMP_JGT_K: /* ip += (A > K) ? jt : jf */
443 mask = 0x200000; /* jh */
444 goto kbranch;
445 case BPF_S_JMP_JGE_K: /* ip += (A >= K) ? jt : jf */
446 mask = 0xa00000; /* jhe */
447 goto kbranch;
448 case BPF_S_JMP_JEQ_K: /* ip += (A == K) ? jt : jf */
449 mask = 0x800000; /* je */
450 kbranch: /* Emit compare if the branch targets are different */
451 if (filter->jt != filter->jf) {
452 if (K <= 16383)
453 /* chi %r5,<K> */
454 EMIT4_IMM(0xa75e0000, K);
455 else if (test_facility(21))
456 /* clfi %r5,<K> */
457 EMIT6_IMM(0xc25f0000, K);
458 else
459 /* c %r5,<d(K)>(%r13) */
460 EMIT4_DISP(0x5950d000, EMIT_CONST(K));
462 branch: if (filter->jt == filter->jf) {
463 if (filter->jt == 0)
464 break;
465 /* j <jt> */
466 offset = addrs[i + filter->jt] + jit->start - jit->prg;
467 EMIT4_PCREL(0xa7f40000, offset);
468 break;
470 if (filter->jt != 0) {
471 /* brc <mask>,<jt> */
472 offset = addrs[i + filter->jt] + jit->start - jit->prg;
473 EMIT4_PCREL(0xa7040000 | mask, offset);
475 if (filter->jf != 0) {
476 /* brc <mask^15>,<jf> */
477 offset = addrs[i + filter->jf] + jit->start - jit->prg;
478 EMIT4_PCREL(0xa7040000 | (mask ^ 0xf00000), offset);
480 break;
481 case BPF_S_JMP_JSET_K: /* ip += (A & K) ? jt : jf */
482 mask = 0x700000; /* jnz */
483 /* Emit test if the branch targets are different */
484 if (filter->jt != filter->jf) {
485 if (K > 65535) {
486 /* lr %r4,%r5 */
487 EMIT2(0x1845);
488 /* n %r4,<d(K)>(%r13) */
489 EMIT4_DISP(0x5440d000, EMIT_CONST(K));
490 } else
491 /* tmll %r5,K */
492 EMIT4_IMM(0xa7510000, K);
494 goto branch;
495 case BPF_S_JMP_JGT_X: /* ip += (A > X) ? jt : jf */
496 mask = 0x200000; /* jh */
497 goto xbranch;
498 case BPF_S_JMP_JGE_X: /* ip += (A >= X) ? jt : jf */
499 mask = 0xa00000; /* jhe */
500 goto xbranch;
501 case BPF_S_JMP_JEQ_X: /* ip += (A == X) ? jt : jf */
502 mask = 0x800000; /* je */
503 xbranch: /* Emit compare if the branch targets are different */
504 if (filter->jt != filter->jf) {
505 jit->seen |= SEEN_XREG;
506 /* cr %r5,%r12 */
507 EMIT2(0x195c);
509 goto branch;
510 case BPF_S_JMP_JSET_X: /* ip += (A & X) ? jt : jf */
511 mask = 0x700000; /* jnz */
512 /* Emit test if the branch targets are different */
513 if (filter->jt != filter->jf) {
514 jit->seen |= SEEN_XREG;
515 /* lr %r4,%r5 */
516 EMIT2(0x1845);
517 /* nr %r4,%r12 */
518 EMIT2(0x144c);
520 goto branch;
521 case BPF_S_LD_W_ABS: /* A = *(u32 *) (skb->data+K) */
522 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_WORD;
523 offset = jit->off_load_word;
524 goto load_abs;
525 case BPF_S_LD_H_ABS: /* A = *(u16 *) (skb->data+K) */
526 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_HALF;
527 offset = jit->off_load_half;
528 goto load_abs;
529 case BPF_S_LD_B_ABS: /* A = *(u8 *) (skb->data+K) */
530 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_BYTE;
531 offset = jit->off_load_byte;
532 load_abs: if ((int) K < 0)
533 goto out;
534 call_fn: /* lg %r1,<d(function)>(%r13) */
535 EMIT6_DISP(0xe310d000, 0x0004, offset);
536 /* l %r3,<d(K)>(%r13) */
537 EMIT4_DISP(0x5830d000, EMIT_CONST(K));
538 /* basr %r8,%r1 */
539 EMIT2(0x0d81);
540 /* jnz <ret0> */
541 EMIT4_PCREL(0xa7740000, (jit->ret0_ip - jit->prg));
542 break;
543 case BPF_S_LD_W_IND: /* A = *(u32 *) (skb->data+K+X) */
544 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_IWORD;
545 offset = jit->off_load_iword;
546 goto call_fn;
547 case BPF_S_LD_H_IND: /* A = *(u16 *) (skb->data+K+X) */
548 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_IHALF;
549 offset = jit->off_load_ihalf;
550 goto call_fn;
551 case BPF_S_LD_B_IND: /* A = *(u8 *) (skb->data+K+X) */
552 jit->seen |= SEEN_DATAREF | SEEN_RET0 | SEEN_LOAD_IBYTE;
553 offset = jit->off_load_ibyte;
554 goto call_fn;
555 case BPF_S_LDX_B_MSH:
556 /* X = (*(u8 *)(skb->data+K) & 0xf) << 2 */
557 jit->seen |= SEEN_RET0;
558 if ((int) K < 0) {
559 /* j <ret0> */
560 EMIT4_PCREL(0xa7f40000, (jit->ret0_ip - jit->prg));
561 break;
563 jit->seen |= SEEN_DATAREF | SEEN_LOAD_BMSH;
564 offset = jit->off_load_bmsh;
565 goto call_fn;
566 case BPF_S_LD_W_LEN: /* A = skb->len; */
567 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
568 /* l %r5,<d(len)>(%r2) */
569 EMIT4_DISP(0x58502000, offsetof(struct sk_buff, len));
570 break;
571 case BPF_S_LDX_W_LEN: /* X = skb->len; */
572 jit->seen |= SEEN_XREG;
573 /* l %r12,<d(len)>(%r2) */
574 EMIT4_DISP(0x58c02000, offsetof(struct sk_buff, len));
575 break;
576 case BPF_S_LD_IMM: /* A = K */
577 if (K <= 16383)
578 /* lhi %r5,K */
579 EMIT4_IMM(0xa7580000, K);
580 else if (test_facility(21))
581 /* llilf %r5,<K> */
582 EMIT6_IMM(0xc05f0000, K);
583 else
584 /* l %r5,<d(K)>(%r13) */
585 EMIT4_DISP(0x5850d000, EMIT_CONST(K));
586 break;
587 case BPF_S_LDX_IMM: /* X = K */
588 jit->seen |= SEEN_XREG;
589 if (K <= 16383)
590 /* lhi %r12,<K> */
591 EMIT4_IMM(0xa7c80000, K);
592 else if (test_facility(21))
593 /* llilf %r12,<K> */
594 EMIT6_IMM(0xc0cf0000, K);
595 else
596 /* l %r12,<d(K)>(%r13) */
597 EMIT4_DISP(0x58c0d000, EMIT_CONST(K));
598 break;
599 case BPF_S_LD_MEM: /* A = mem[K] */
600 jit->seen |= SEEN_MEM;
601 /* l %r5,<K>(%r15) */
602 EMIT4_DISP(0x5850f000,
603 (jit->seen & SEEN_DATAREF) ? 160 + K*4 : K*4);
604 break;
605 case BPF_S_LDX_MEM: /* X = mem[K] */
606 jit->seen |= SEEN_XREG | SEEN_MEM;
607 /* l %r12,<K>(%r15) */
608 EMIT4_DISP(0x58c0f000,
609 (jit->seen & SEEN_DATAREF) ? 160 + K*4 : K*4);
610 break;
611 case BPF_S_MISC_TAX: /* X = A */
612 jit->seen |= SEEN_XREG;
613 /* lr %r12,%r5 */
614 EMIT2(0x18c5);
615 break;
616 case BPF_S_MISC_TXA: /* A = X */
617 jit->seen |= SEEN_XREG;
618 /* lr %r5,%r12 */
619 EMIT2(0x185c);
620 break;
621 case BPF_S_RET_K:
622 if (K == 0) {
623 jit->seen |= SEEN_RET0;
624 if (last)
625 break;
626 /* j <ret0> */
627 EMIT4_PCREL(0xa7f40000, jit->ret0_ip - jit->prg);
628 } else {
629 if (K <= 16383)
630 /* lghi %r2,K */
631 EMIT4_IMM(0xa7290000, K);
632 else
633 /* llgf %r2,<K>(%r13) */
634 EMIT6_DISP(0xe320d000, 0x0016, EMIT_CONST(K));
635 /* j <exit> */
636 if (last && !(jit->seen & SEEN_RET0))
637 break;
638 EMIT4_PCREL(0xa7f40000, jit->exit_ip - jit->prg);
640 break;
641 case BPF_S_RET_A:
642 /* llgfr %r2,%r5 */
643 EMIT4(0xb9160025);
644 /* j <exit> */
645 EMIT4_PCREL(0xa7f40000, jit->exit_ip - jit->prg);
646 break;
647 case BPF_S_ST: /* mem[K] = A */
648 jit->seen |= SEEN_MEM;
649 /* st %r5,<K>(%r15) */
650 EMIT4_DISP(0x5050f000,
651 (jit->seen & SEEN_DATAREF) ? 160 + K*4 : K*4);
652 break;
653 case BPF_S_STX: /* mem[K] = X : mov %ebx,off8(%rbp) */
654 jit->seen |= SEEN_XREG | SEEN_MEM;
655 /* st %r12,<K>(%r15) */
656 EMIT4_DISP(0x50c0f000,
657 (jit->seen & SEEN_DATAREF) ? 160 + K*4 : K*4);
658 break;
659 case BPF_S_ANC_PROTOCOL: /* A = ntohs(skb->protocol); */
660 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
661 /* lhi %r5,0 */
662 EMIT4(0xa7580000);
663 /* icm %r5,3,<d(protocol)>(%r2) */
664 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff, protocol));
665 break;
666 case BPF_S_ANC_IFINDEX: /* if (!skb->dev) return 0;
667 * A = skb->dev->ifindex */
668 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
669 jit->seen |= SEEN_RET0;
670 /* lg %r1,<d(dev)>(%r2) */
671 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff, dev));
672 /* ltgr %r1,%r1 */
673 EMIT4(0xb9020011);
674 /* jz <ret0> */
675 EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
676 /* l %r5,<d(ifindex)>(%r1) */
677 EMIT4_DISP(0x58501000, offsetof(struct net_device, ifindex));
678 break;
679 case BPF_S_ANC_MARK: /* A = skb->mark */
680 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
681 /* l %r5,<d(mark)>(%r2) */
682 EMIT4_DISP(0x58502000, offsetof(struct sk_buff, mark));
683 break;
684 case BPF_S_ANC_QUEUE: /* A = skb->queue_mapping */
685 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
686 /* lhi %r5,0 */
687 EMIT4(0xa7580000);
688 /* icm %r5,3,<d(queue_mapping)>(%r2) */
689 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff, queue_mapping));
690 break;
691 case BPF_S_ANC_HATYPE: /* if (!skb->dev) return 0;
692 * A = skb->dev->type */
693 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
694 jit->seen |= SEEN_RET0;
695 /* lg %r1,<d(dev)>(%r2) */
696 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff, dev));
697 /* ltgr %r1,%r1 */
698 EMIT4(0xb9020011);
699 /* jz <ret0> */
700 EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
701 /* lhi %r5,0 */
702 EMIT4(0xa7580000);
703 /* icm %r5,3,<d(type)>(%r1) */
704 EMIT4_DISP(0xbf531000, offsetof(struct net_device, type));
705 break;
706 case BPF_S_ANC_RXHASH: /* A = skb->rxhash */
707 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
708 /* l %r5,<d(rxhash)>(%r2) */
709 EMIT4_DISP(0x58502000, offsetof(struct sk_buff, rxhash));
710 break;
711 case BPF_S_ANC_VLAN_TAG:
712 case BPF_S_ANC_VLAN_TAG_PRESENT:
713 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
714 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
715 /* lhi %r5,0 */
716 EMIT4(0xa7580000);
717 /* icm %r5,3,<d(vlan_tci)>(%r2) */
718 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff, vlan_tci));
719 if (filter->code == BPF_S_ANC_VLAN_TAG) {
720 /* nill %r5,0xefff */
721 EMIT4_IMM(0xa5570000, ~VLAN_TAG_PRESENT);
722 } else {
723 /* nill %r5,0x1000 */
724 EMIT4_IMM(0xa5570000, VLAN_TAG_PRESENT);
725 /* srl %r5,12 */
726 EMIT4_DISP(0x88500000, 12);
728 break;
729 case BPF_S_ANC_CPU: /* A = smp_processor_id() */
730 #ifdef CONFIG_SMP
731 /* l %r5,<d(cpu_nr)> */
732 EMIT4_DISP(0x58500000, offsetof(struct _lowcore, cpu_nr));
733 #else
734 /* lhi %r5,0 */
735 EMIT4(0xa7580000);
736 #endif
737 break;
738 default: /* too complex, give up */
739 goto out;
741 addrs[i] = jit->prg - jit->start;
742 return 0;
743 out:
744 return -1;
747 void bpf_jit_compile(struct sk_filter *fp)
749 unsigned long size, prg_len, lit_len;
750 struct bpf_jit jit, cjit;
751 unsigned int *addrs;
752 int pass, i;
754 if (!bpf_jit_enable)
755 return;
756 addrs = kcalloc(fp->len, sizeof(*addrs), GFP_KERNEL);
757 if (addrs == NULL)
758 return;
759 memset(&jit, 0, sizeof(cjit));
760 memset(&cjit, 0, sizeof(cjit));
762 for (pass = 0; pass < 10; pass++) {
763 jit.prg = jit.start;
764 jit.lit = jit.mid;
766 bpf_jit_prologue(&jit);
767 bpf_jit_noleaks(&jit, fp->insns);
768 for (i = 0; i < fp->len; i++) {
769 if (bpf_jit_insn(&jit, fp->insns + i, addrs, i,
770 i == fp->len - 1))
771 goto out;
773 bpf_jit_epilogue(&jit);
774 if (jit.start) {
775 WARN_ON(jit.prg > cjit.prg || jit.lit > cjit.lit);
776 if (memcmp(&jit, &cjit, sizeof(jit)) == 0)
777 break;
778 } else if (jit.prg == cjit.prg && jit.lit == cjit.lit) {
779 prg_len = jit.prg - jit.start;
780 lit_len = jit.lit - jit.mid;
781 size = max_t(unsigned long, prg_len + lit_len,
782 sizeof(struct work_struct));
783 if (size >= BPF_SIZE_MAX)
784 goto out;
785 jit.start = module_alloc(size);
786 if (!jit.start)
787 goto out;
788 jit.prg = jit.mid = jit.start + prg_len;
789 jit.lit = jit.end = jit.start + prg_len + lit_len;
790 jit.base_ip += (unsigned long) jit.start;
791 jit.exit_ip += (unsigned long) jit.start;
792 jit.ret0_ip += (unsigned long) jit.start;
794 cjit = jit;
796 if (bpf_jit_enable > 1) {
797 pr_err("flen=%d proglen=%lu pass=%d image=%p\n",
798 fp->len, jit.end - jit.start, pass, jit.start);
799 if (jit.start) {
800 printk(KERN_ERR "JIT code:\n");
801 print_fn_code(jit.start, jit.mid - jit.start);
802 print_hex_dump(KERN_ERR, "JIT literals:\n",
803 DUMP_PREFIX_ADDRESS, 16, 1,
804 jit.mid, jit.end - jit.mid, false);
807 if (jit.start)
808 fp->bpf_func = (void *) jit.start;
809 out:
810 kfree(addrs);
813 static void jit_free_defer(struct work_struct *arg)
815 module_free(NULL, arg);
818 /* run from softirq, we must use a work_struct to call
819 * module_free() from process context
821 void bpf_jit_free(struct sk_filter *fp)
823 struct work_struct *work;
825 if (fp->bpf_func == sk_run_filter)
826 return;
827 work = (struct work_struct *)fp->bpf_func;
828 INIT_WORK(work, jit_free_defer);
829 schedule_work(work);