2 * BPF Jit compiler for s390.
4 * Copyright IBM Corp. 2012
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 #include <linux/moduleloader.h>
9 #include <linux/netdevice.h>
10 #include <linux/if_vlan.h>
11 #include <linux/filter.h>
12 #include <linux/random.h>
13 #include <linux/init.h>
14 #include <asm/cacheflush.h>
15 #include <asm/facility.h>
21 * %r3 = offset parameter
22 * %r4 = scratch register / length parameter
23 * %r5 = BPF A accumulator
24 * %r8 = return address
25 * %r9 = save register for skb pointer
27 * %r11 = skb->len - skb->data_len (headlen)
28 * %r12 = BPF X accumulator
29 * %r13 = literal pool pointer
30 * 0(%r15) - 63(%r15) scratch memory array with BPF_MEMWORDS
32 int bpf_jit_enable __read_mostly
;
35 * assembly code in arch/x86/net/bpf_jit.S
37 extern u8 sk_load_word
[], sk_load_half
[], sk_load_byte
[], sk_load_byte_msh
[];
38 extern u8 sk_load_word_ind
[], sk_load_half_ind
[], sk_load_byte_ind
[];
50 unsigned int off_load_word
;
51 unsigned int off_load_half
;
52 unsigned int off_load_byte
;
53 unsigned int off_load_bmsh
;
54 unsigned int off_load_iword
;
55 unsigned int off_load_ihalf
;
56 unsigned int off_load_ibyte
;
59 #define BPF_SIZE_MAX 4096 /* Max size for program */
61 #define SEEN_DATAREF 1 /* might call external helpers */
62 #define SEEN_XREG 2 /* ebx is used */
63 #define SEEN_MEM 4 /* use mem[] for temporary storage */
64 #define SEEN_RET0 8 /* pc_ret0 points to a valid return 0 */
65 #define SEEN_LITERAL 16 /* code uses literals */
66 #define SEEN_LOAD_WORD 32 /* code uses sk_load_word */
67 #define SEEN_LOAD_HALF 64 /* code uses sk_load_half */
68 #define SEEN_LOAD_BYTE 128 /* code uses sk_load_byte */
69 #define SEEN_LOAD_BMSH 256 /* code uses sk_load_byte_msh */
70 #define SEEN_LOAD_IWORD 512 /* code uses sk_load_word_ind */
71 #define SEEN_LOAD_IHALF 1024 /* code uses sk_load_half_ind */
72 #define SEEN_LOAD_IBYTE 2048 /* code uses sk_load_byte_ind */
76 if (jit->prg + 2 <= jit->mid) \
77 *(u16 *) jit->prg = op; \
83 if (jit->prg + 4 <= jit->mid) \
84 *(u32 *) jit->prg = op; \
88 #define EMIT4_DISP(op, disp) \
90 unsigned int __disp = (disp) & 0xfff; \
94 #define EMIT4_IMM(op, imm) \
96 unsigned int __imm = (imm) & 0xffff; \
100 #define EMIT4_PCREL(op, pcrel) \
102 long __pcrel = ((pcrel) >> 1) & 0xffff; \
103 EMIT4(op | __pcrel); \
106 #define EMIT6(op1, op2) \
108 if (jit->prg + 6 <= jit->mid) { \
109 *(u32 *) jit->prg = op1; \
110 *(u16 *) (jit->prg + 4) = op2; \
115 #define EMIT6_DISP(op1, op2, disp) \
117 unsigned int __disp = (disp) & 0xfff; \
118 EMIT6(op1 | __disp, op2); \
121 #define EMIT6_IMM(op, imm) \
123 unsigned int __imm = (imm); \
124 EMIT6(op | (__imm >> 16), __imm & 0xffff); \
127 #define EMIT_CONST(val) \
130 ret = (unsigned int) (jit->lit - jit->base_ip); \
131 jit->seen |= SEEN_LITERAL; \
132 if (jit->lit + 4 <= jit->end) \
133 *(u32 *) jit->lit = val; \
138 #define EMIT_FN_CONST(bit, fn) \
141 ret = (unsigned int) (jit->lit - jit->base_ip); \
142 if (jit->seen & bit) { \
143 jit->seen |= SEEN_LITERAL; \
144 if (jit->lit + 8 <= jit->end) \
145 *(void **) jit->lit = fn; \
151 static void bpf_jit_prologue(struct bpf_jit
*jit
)
153 /* Save registers and create stack frame if necessary */
154 if (jit
->seen
& SEEN_DATAREF
) {
155 /* stmg %r8,%r15,88(%r15) */
156 EMIT6(0xeb8ff058, 0x0024);
159 /* aghi %r15,<offset> */
160 EMIT4_IMM(0xa7fb0000, (jit
->seen
& SEEN_MEM
) ? -112 : -80);
161 /* stg %r14,152(%r15) */
162 EMIT6(0xe3e0f098, 0x0024);
163 } else if ((jit
->seen
& SEEN_XREG
) && (jit
->seen
& SEEN_LITERAL
))
164 /* stmg %r12,%r13,120(%r15) */
165 EMIT6(0xebcdf078, 0x0024);
166 else if (jit
->seen
& SEEN_XREG
)
167 /* stg %r12,120(%r15) */
168 EMIT6(0xe3c0f078, 0x0024);
169 else if (jit
->seen
& SEEN_LITERAL
)
170 /* stg %r13,128(%r15) */
171 EMIT6(0xe3d0f080, 0x0024);
173 /* Setup literal pool */
174 if (jit
->seen
& SEEN_LITERAL
) {
177 jit
->base_ip
= jit
->prg
;
179 jit
->off_load_word
= EMIT_FN_CONST(SEEN_LOAD_WORD
, sk_load_word
);
180 jit
->off_load_half
= EMIT_FN_CONST(SEEN_LOAD_HALF
, sk_load_half
);
181 jit
->off_load_byte
= EMIT_FN_CONST(SEEN_LOAD_BYTE
, sk_load_byte
);
182 jit
->off_load_bmsh
= EMIT_FN_CONST(SEEN_LOAD_BMSH
, sk_load_byte_msh
);
183 jit
->off_load_iword
= EMIT_FN_CONST(SEEN_LOAD_IWORD
, sk_load_word_ind
);
184 jit
->off_load_ihalf
= EMIT_FN_CONST(SEEN_LOAD_IHALF
, sk_load_half_ind
);
185 jit
->off_load_ibyte
= EMIT_FN_CONST(SEEN_LOAD_IBYTE
, sk_load_byte_ind
);
187 /* Filter needs to access skb data */
188 if (jit
->seen
& SEEN_DATAREF
) {
189 /* l %r11,<len>(%r2) */
190 EMIT4_DISP(0x58b02000, offsetof(struct sk_buff
, len
));
191 /* s %r11,<data_len>(%r2) */
192 EMIT4_DISP(0x5bb02000, offsetof(struct sk_buff
, data_len
));
193 /* lg %r10,<data>(%r2) */
194 EMIT6_DISP(0xe3a02000, 0x0004,
195 offsetof(struct sk_buff
, data
));
199 static void bpf_jit_epilogue(struct bpf_jit
*jit
)
202 if (jit
->seen
& SEEN_RET0
) {
203 jit
->ret0_ip
= jit
->prg
;
207 jit
->exit_ip
= jit
->prg
;
208 /* Restore registers */
209 if (jit
->seen
& SEEN_DATAREF
)
210 /* lmg %r8,%r15,<offset>(%r15) */
211 EMIT6_DISP(0xeb8ff000, 0x0004,
212 (jit
->seen
& SEEN_MEM
) ? 200 : 168);
213 else if ((jit
->seen
& SEEN_XREG
) && (jit
->seen
& SEEN_LITERAL
))
214 /* lmg %r12,%r13,120(%r15) */
215 EMIT6(0xebcdf078, 0x0004);
216 else if (jit
->seen
& SEEN_XREG
)
217 /* lg %r12,120(%r15) */
218 EMIT6(0xe3c0f078, 0x0004);
219 else if (jit
->seen
& SEEN_LITERAL
)
220 /* lg %r13,128(%r15) */
221 EMIT6(0xe3d0f080, 0x0004);
226 /* Helper to find the offset of pkt_type in sk_buff
227 * Make sure its still a 3bit field starting at the MSBs within a byte.
229 #define PKT_TYPE_MAX 0xe0
230 static int pkt_type_offset
;
232 static int __init
bpf_pkt_type_offset_init(void)
234 struct sk_buff skb_probe
= {
237 char *ct
= (char *)&skb_probe
;
240 pkt_type_offset
= -1;
241 for (off
= 0; off
< sizeof(struct sk_buff
); off
++) {
244 if (ct
[off
] == PKT_TYPE_MAX
)
245 pkt_type_offset
= off
;
247 /* Found non matching bit pattern, fix needed. */
249 pkt_type_offset
= -1;
255 device_initcall(bpf_pkt_type_offset_init
);
258 * make sure we dont leak kernel information to user
260 static void bpf_jit_noleaks(struct bpf_jit
*jit
, struct sock_filter
*filter
)
262 /* Clear temporary memory if (seen & SEEN_MEM) */
263 if (jit
->seen
& SEEN_MEM
)
264 /* xc 0(64,%r15),0(%r15) */
265 EMIT6(0xd73ff000, 0xf000);
266 /* Clear X if (seen & SEEN_XREG) */
267 if (jit
->seen
& SEEN_XREG
)
270 /* Clear A if the first register does not set it. */
271 switch (filter
[0].code
) {
272 case BPF_LD
| BPF_W
| BPF_ABS
:
273 case BPF_LD
| BPF_H
| BPF_ABS
:
274 case BPF_LD
| BPF_B
| BPF_ABS
:
275 case BPF_LD
| BPF_W
| BPF_LEN
:
276 case BPF_LD
| BPF_W
| BPF_IND
:
277 case BPF_LD
| BPF_H
| BPF_IND
:
278 case BPF_LD
| BPF_B
| BPF_IND
:
279 case BPF_LD
| BPF_IMM
:
280 case BPF_LD
| BPF_MEM
:
281 case BPF_MISC
| BPF_TXA
:
282 case BPF_RET
| BPF_K
:
283 /* first instruction sets A register */
291 static int bpf_jit_insn(struct bpf_jit
*jit
, struct sock_filter
*filter
,
292 unsigned int *addrs
, int i
, int last
)
300 code
= bpf_anc_helper(filter
);
303 case BPF_ALU
| BPF_ADD
| BPF_X
: /* A += X */
304 jit
->seen
|= SEEN_XREG
;
308 case BPF_ALU
| BPF_ADD
| BPF_K
: /* A += K */
313 EMIT4_IMM(0xa75a0000, K
);
314 else if (test_facility(21))
316 EMIT6_IMM(0xc25b0000, K
);
318 /* a %r5,<d(K)>(%r13) */
319 EMIT4_DISP(0x5a50d000, EMIT_CONST(K
));
321 case BPF_ALU
| BPF_SUB
| BPF_X
: /* A -= X */
322 jit
->seen
|= SEEN_XREG
;
326 case BPF_ALU
| BPF_SUB
| BPF_K
: /* A -= K */
331 EMIT4_IMM(0xa75a0000, -K
);
332 else if (test_facility(21))
334 EMIT6_IMM(0xc25b0000, -K
);
336 /* s %r5,<d(K)>(%r13) */
337 EMIT4_DISP(0x5b50d000, EMIT_CONST(K
));
339 case BPF_ALU
| BPF_MUL
| BPF_X
: /* A *= X */
340 jit
->seen
|= SEEN_XREG
;
344 case BPF_ALU
| BPF_MUL
| BPF_K
: /* A *= K */
347 EMIT4_IMM(0xa75c0000, K
);
348 else if (test_facility(34))
350 EMIT6_IMM(0xc2510000, K
);
352 /* ms %r5,<d(K)>(%r13) */
353 EMIT4_DISP(0x7150d000, EMIT_CONST(K
));
355 case BPF_ALU
| BPF_DIV
| BPF_X
: /* A /= X */
356 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
360 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
366 case BPF_ALU
| BPF_DIV
| BPF_K
: /* A /= K */
371 /* dl %r4,<d(K)>(%r13) */
372 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K
));
374 case BPF_ALU
| BPF_MOD
| BPF_X
: /* A %= X */
375 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
379 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
387 case BPF_ALU
| BPF_MOD
| BPF_K
: /* A %= K */
395 /* dl %r4,<d(K)>(%r13) */
396 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K
));
400 case BPF_ALU
| BPF_AND
| BPF_X
: /* A &= X */
401 jit
->seen
|= SEEN_XREG
;
405 case BPF_ALU
| BPF_AND
| BPF_K
: /* A &= K */
406 if (test_facility(21))
408 EMIT6_IMM(0xc05b0000, K
);
410 /* n %r5,<d(K)>(%r13) */
411 EMIT4_DISP(0x5450d000, EMIT_CONST(K
));
413 case BPF_ALU
| BPF_OR
| BPF_X
: /* A |= X */
414 jit
->seen
|= SEEN_XREG
;
418 case BPF_ALU
| BPF_OR
| BPF_K
: /* A |= K */
419 if (test_facility(21))
421 EMIT6_IMM(0xc05d0000, K
);
423 /* o %r5,<d(K)>(%r13) */
424 EMIT4_DISP(0x5650d000, EMIT_CONST(K
));
426 case BPF_ANC
| SKF_AD_ALU_XOR_X
: /* A ^= X; */
427 case BPF_ALU
| BPF_XOR
| BPF_X
:
428 jit
->seen
|= SEEN_XREG
;
432 case BPF_ALU
| BPF_XOR
| BPF_K
: /* A ^= K */
435 /* x %r5,<d(K)>(%r13) */
436 EMIT4_DISP(0x5750d000, EMIT_CONST(K
));
438 case BPF_ALU
| BPF_LSH
| BPF_X
: /* A <<= X; */
439 jit
->seen
|= SEEN_XREG
;
440 /* sll %r5,0(%r12) */
443 case BPF_ALU
| BPF_LSH
| BPF_K
: /* A <<= K */
447 EMIT4_DISP(0x89500000, K
);
449 case BPF_ALU
| BPF_RSH
| BPF_X
: /* A >>= X; */
450 jit
->seen
|= SEEN_XREG
;
451 /* srl %r5,0(%r12) */
454 case BPF_ALU
| BPF_RSH
| BPF_K
: /* A >>= K; */
458 EMIT4_DISP(0x88500000, K
);
460 case BPF_ALU
| BPF_NEG
: /* A = -A */
464 case BPF_JMP
| BPF_JA
: /* ip += K */
465 offset
= addrs
[i
+ K
] + jit
->start
- jit
->prg
;
466 EMIT4_PCREL(0xa7f40000, offset
);
468 case BPF_JMP
| BPF_JGT
| BPF_K
: /* ip += (A > K) ? jt : jf */
469 mask
= 0x200000; /* jh */
471 case BPF_JMP
| BPF_JGE
| BPF_K
: /* ip += (A >= K) ? jt : jf */
472 mask
= 0xa00000; /* jhe */
474 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* ip += (A == K) ? jt : jf */
475 mask
= 0x800000; /* je */
476 kbranch
: /* Emit compare if the branch targets are different */
477 if (filter
->jt
!= filter
->jf
) {
480 EMIT4_IMM(0xa75e0000, K
);
481 else if (test_facility(21))
483 EMIT6_IMM(0xc25f0000, K
);
485 /* c %r5,<d(K)>(%r13) */
486 EMIT4_DISP(0x5950d000, EMIT_CONST(K
));
488 branch
: if (filter
->jt
== filter
->jf
) {
492 offset
= addrs
[i
+ filter
->jt
] + jit
->start
- jit
->prg
;
493 EMIT4_PCREL(0xa7f40000, offset
);
496 if (filter
->jt
!= 0) {
497 /* brc <mask>,<jt> */
498 offset
= addrs
[i
+ filter
->jt
] + jit
->start
- jit
->prg
;
499 EMIT4_PCREL(0xa7040000 | mask
, offset
);
501 if (filter
->jf
!= 0) {
502 /* brc <mask^15>,<jf> */
503 offset
= addrs
[i
+ filter
->jf
] + jit
->start
- jit
->prg
;
504 EMIT4_PCREL(0xa7040000 | (mask
^ 0xf00000), offset
);
507 case BPF_JMP
| BPF_JSET
| BPF_K
: /* ip += (A & K) ? jt : jf */
508 mask
= 0x700000; /* jnz */
509 /* Emit test if the branch targets are different */
510 if (filter
->jt
!= filter
->jf
) {
514 /* n %r4,<d(K)>(%r13) */
515 EMIT4_DISP(0x5440d000, EMIT_CONST(K
));
518 EMIT4_IMM(0xa7510000, K
);
521 case BPF_JMP
| BPF_JGT
| BPF_X
: /* ip += (A > X) ? jt : jf */
522 mask
= 0x200000; /* jh */
524 case BPF_JMP
| BPF_JGE
| BPF_X
: /* ip += (A >= X) ? jt : jf */
525 mask
= 0xa00000; /* jhe */
527 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* ip += (A == X) ? jt : jf */
528 mask
= 0x800000; /* je */
529 xbranch
: /* Emit compare if the branch targets are different */
530 if (filter
->jt
!= filter
->jf
) {
531 jit
->seen
|= SEEN_XREG
;
536 case BPF_JMP
| BPF_JSET
| BPF_X
: /* ip += (A & X) ? jt : jf */
537 mask
= 0x700000; /* jnz */
538 /* Emit test if the branch targets are different */
539 if (filter
->jt
!= filter
->jf
) {
540 jit
->seen
|= SEEN_XREG
;
547 case BPF_LD
| BPF_W
| BPF_ABS
: /* A = *(u32 *) (skb->data+K) */
548 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_WORD
;
549 offset
= jit
->off_load_word
;
551 case BPF_LD
| BPF_H
| BPF_ABS
: /* A = *(u16 *) (skb->data+K) */
552 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_HALF
;
553 offset
= jit
->off_load_half
;
555 case BPF_LD
| BPF_B
| BPF_ABS
: /* A = *(u8 *) (skb->data+K) */
556 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_BYTE
;
557 offset
= jit
->off_load_byte
;
558 load_abs
: if ((int) K
< 0)
560 call_fn
: /* lg %r1,<d(function)>(%r13) */
561 EMIT6_DISP(0xe310d000, 0x0004, offset
);
562 /* l %r3,<d(K)>(%r13) */
563 EMIT4_DISP(0x5830d000, EMIT_CONST(K
));
567 EMIT4_PCREL(0xa7740000, (jit
->ret0_ip
- jit
->prg
));
569 case BPF_LD
| BPF_W
| BPF_IND
: /* A = *(u32 *) (skb->data+K+X) */
570 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IWORD
;
571 offset
= jit
->off_load_iword
;
573 case BPF_LD
| BPF_H
| BPF_IND
: /* A = *(u16 *) (skb->data+K+X) */
574 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IHALF
;
575 offset
= jit
->off_load_ihalf
;
577 case BPF_LD
| BPF_B
| BPF_IND
: /* A = *(u8 *) (skb->data+K+X) */
578 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IBYTE
;
579 offset
= jit
->off_load_ibyte
;
581 case BPF_LDX
| BPF_B
| BPF_MSH
:
582 /* X = (*(u8 *)(skb->data+K) & 0xf) << 2 */
583 jit
->seen
|= SEEN_RET0
;
586 EMIT4_PCREL(0xa7f40000, (jit
->ret0_ip
- jit
->prg
));
589 jit
->seen
|= SEEN_DATAREF
| SEEN_LOAD_BMSH
;
590 offset
= jit
->off_load_bmsh
;
592 case BPF_LD
| BPF_W
| BPF_LEN
: /* A = skb->len; */
593 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, len
) != 4);
594 /* l %r5,<d(len)>(%r2) */
595 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, len
));
597 case BPF_LDX
| BPF_W
| BPF_LEN
: /* X = skb->len; */
598 jit
->seen
|= SEEN_XREG
;
599 /* l %r12,<d(len)>(%r2) */
600 EMIT4_DISP(0x58c02000, offsetof(struct sk_buff
, len
));
602 case BPF_LD
| BPF_IMM
: /* A = K */
605 EMIT4_IMM(0xa7580000, K
);
606 else if (test_facility(21))
608 EMIT6_IMM(0xc05f0000, K
);
610 /* l %r5,<d(K)>(%r13) */
611 EMIT4_DISP(0x5850d000, EMIT_CONST(K
));
613 case BPF_LDX
| BPF_IMM
: /* X = K */
614 jit
->seen
|= SEEN_XREG
;
617 EMIT4_IMM(0xa7c80000, K
);
618 else if (test_facility(21))
620 EMIT6_IMM(0xc0cf0000, K
);
622 /* l %r12,<d(K)>(%r13) */
623 EMIT4_DISP(0x58c0d000, EMIT_CONST(K
));
625 case BPF_LD
| BPF_MEM
: /* A = mem[K] */
626 jit
->seen
|= SEEN_MEM
;
627 /* l %r5,<K>(%r15) */
628 EMIT4_DISP(0x5850f000,
629 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
631 case BPF_LDX
| BPF_MEM
: /* X = mem[K] */
632 jit
->seen
|= SEEN_XREG
| SEEN_MEM
;
633 /* l %r12,<K>(%r15) */
634 EMIT4_DISP(0x58c0f000,
635 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
637 case BPF_MISC
| BPF_TAX
: /* X = A */
638 jit
->seen
|= SEEN_XREG
;
642 case BPF_MISC
| BPF_TXA
: /* A = X */
643 jit
->seen
|= SEEN_XREG
;
647 case BPF_RET
| BPF_K
:
649 jit
->seen
|= SEEN_RET0
;
653 EMIT4_PCREL(0xa7f40000, jit
->ret0_ip
- jit
->prg
);
657 EMIT4_IMM(0xa7290000, K
);
659 /* llgf %r2,<K>(%r13) */
660 EMIT6_DISP(0xe320d000, 0x0016, EMIT_CONST(K
));
662 if (last
&& !(jit
->seen
& SEEN_RET0
))
664 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
667 case BPF_RET
| BPF_A
:
671 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
673 case BPF_ST
: /* mem[K] = A */
674 jit
->seen
|= SEEN_MEM
;
675 /* st %r5,<K>(%r15) */
676 EMIT4_DISP(0x5050f000,
677 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
679 case BPF_STX
: /* mem[K] = X : mov %ebx,off8(%rbp) */
680 jit
->seen
|= SEEN_XREG
| SEEN_MEM
;
681 /* st %r12,<K>(%r15) */
682 EMIT4_DISP(0x50c0f000,
683 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
685 case BPF_ANC
| SKF_AD_PROTOCOL
: /* A = ntohs(skb->protocol); */
686 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
689 /* icm %r5,3,<d(protocol)>(%r2) */
690 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, protocol
));
692 case BPF_ANC
| SKF_AD_IFINDEX
: /* if (!skb->dev) return 0;
693 * A = skb->dev->ifindex */
694 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
695 jit
->seen
|= SEEN_RET0
;
696 /* lg %r1,<d(dev)>(%r2) */
697 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff
, dev
));
701 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
702 /* l %r5,<d(ifindex)>(%r1) */
703 EMIT4_DISP(0x58501000, offsetof(struct net_device
, ifindex
));
705 case BPF_ANC
| SKF_AD_MARK
: /* A = skb->mark */
706 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
707 /* l %r5,<d(mark)>(%r2) */
708 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, mark
));
710 case BPF_ANC
| SKF_AD_QUEUE
: /* A = skb->queue_mapping */
711 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
714 /* icm %r5,3,<d(queue_mapping)>(%r2) */
715 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, queue_mapping
));
717 case BPF_ANC
| SKF_AD_HATYPE
: /* if (!skb->dev) return 0;
718 * A = skb->dev->type */
719 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, type
) != 2);
720 jit
->seen
|= SEEN_RET0
;
721 /* lg %r1,<d(dev)>(%r2) */
722 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff
, dev
));
726 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
729 /* icm %r5,3,<d(type)>(%r1) */
730 EMIT4_DISP(0xbf531000, offsetof(struct net_device
, type
));
732 case BPF_ANC
| SKF_AD_RXHASH
: /* A = skb->hash */
733 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, hash
) != 4);
734 /* l %r5,<d(hash)>(%r2) */
735 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, hash
));
737 case BPF_ANC
| SKF_AD_VLAN_TAG
:
738 case BPF_ANC
| SKF_AD_VLAN_TAG_PRESENT
:
739 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
740 BUILD_BUG_ON(VLAN_TAG_PRESENT
!= 0x1000);
743 /* icm %r5,3,<d(vlan_tci)>(%r2) */
744 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, vlan_tci
));
745 if (code
== (BPF_ANC
| SKF_AD_VLAN_TAG
)) {
746 /* nill %r5,0xefff */
747 EMIT4_IMM(0xa5570000, ~VLAN_TAG_PRESENT
);
749 /* nill %r5,0x1000 */
750 EMIT4_IMM(0xa5570000, VLAN_TAG_PRESENT
);
752 EMIT4_DISP(0x88500000, 12);
755 case BPF_ANC
| SKF_AD_PKTTYPE
:
756 if (pkt_type_offset
< 0)
760 /* ic %r5,<d(pkt_type_offset)>(%r2) */
761 EMIT4_DISP(0x43502000, pkt_type_offset
);
763 EMIT4_DISP(0x88500000, 5);
765 case BPF_ANC
| SKF_AD_CPU
: /* A = smp_processor_id() */
767 /* l %r5,<d(cpu_nr)> */
768 EMIT4_DISP(0x58500000, offsetof(struct _lowcore
, cpu_nr
));
774 default: /* too complex, give up */
777 addrs
[i
] = jit
->prg
- jit
->start
;
784 * Note: for security reasons, bpf code will follow a randomly
785 * sized amount of illegal instructions.
787 struct bpf_binary_header
{
792 static struct bpf_binary_header
*bpf_alloc_binary(unsigned int bpfsize
,
795 struct bpf_binary_header
*header
;
796 unsigned int sz
, hole
;
798 /* Most BPF filters are really small, but if some of them fill a page,
799 * allow at least 128 extra bytes for illegal instructions.
801 sz
= round_up(bpfsize
+ sizeof(*header
) + 128, PAGE_SIZE
);
802 header
= module_alloc(sz
);
805 memset(header
, 0, sz
);
806 header
->pages
= sz
/ PAGE_SIZE
;
807 hole
= min(sz
- (bpfsize
+ sizeof(*header
)), PAGE_SIZE
- sizeof(*header
));
808 /* Insert random number of illegal instructions before BPF code
809 * and make sure the first instruction starts at an even address.
811 *image_ptr
= &header
->image
[(prandom_u32() % hole
) & -2];
815 void bpf_jit_compile(struct sk_filter
*fp
)
817 struct bpf_binary_header
*header
= NULL
;
818 unsigned long size
, prg_len
, lit_len
;
819 struct bpf_jit jit
, cjit
;
825 addrs
= kcalloc(fp
->len
, sizeof(*addrs
), GFP_KERNEL
);
828 memset(&jit
, 0, sizeof(cjit
));
829 memset(&cjit
, 0, sizeof(cjit
));
831 for (pass
= 0; pass
< 10; pass
++) {
835 bpf_jit_prologue(&jit
);
836 bpf_jit_noleaks(&jit
, fp
->insns
);
837 for (i
= 0; i
< fp
->len
; i
++) {
838 if (bpf_jit_insn(&jit
, fp
->insns
+ i
, addrs
, i
,
842 bpf_jit_epilogue(&jit
);
844 WARN_ON(jit
.prg
> cjit
.prg
|| jit
.lit
> cjit
.lit
);
845 if (memcmp(&jit
, &cjit
, sizeof(jit
)) == 0)
847 } else if (jit
.prg
== cjit
.prg
&& jit
.lit
== cjit
.lit
) {
848 prg_len
= jit
.prg
- jit
.start
;
849 lit_len
= jit
.lit
- jit
.mid
;
850 size
= prg_len
+ lit_len
;
851 if (size
>= BPF_SIZE_MAX
)
853 header
= bpf_alloc_binary(size
, &jit
.start
);
856 jit
.prg
= jit
.mid
= jit
.start
+ prg_len
;
857 jit
.lit
= jit
.end
= jit
.start
+ prg_len
+ lit_len
;
858 jit
.base_ip
+= (unsigned long) jit
.start
;
859 jit
.exit_ip
+= (unsigned long) jit
.start
;
860 jit
.ret0_ip
+= (unsigned long) jit
.start
;
864 if (bpf_jit_enable
> 1) {
865 bpf_jit_dump(fp
->len
, jit
.end
- jit
.start
, pass
, jit
.start
);
867 print_fn_code(jit
.start
, jit
.mid
- jit
.start
);
870 set_memory_ro((unsigned long)header
, header
->pages
);
871 fp
->bpf_func
= (void *) jit
.start
;
878 void bpf_jit_free(struct sk_filter
*fp
)
880 unsigned long addr
= (unsigned long)fp
->bpf_func
& PAGE_MASK
;
881 struct bpf_binary_header
*header
= (void *)addr
;
886 set_memory_rw(addr
, header
->pages
);
887 module_free(NULL
, header
);