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/processor.h>
16 #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 /* ahi %r15,<offset> */
160 EMIT4_IMM(0xa7fa0000, (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
) {
279 case BPF_S_LDX_B_MSH
:
283 case BPF_S_ANC_PROTOCOL
:
284 case BPF_S_ANC_PKTTYPE
:
285 case BPF_S_ANC_IFINDEX
:
287 case BPF_S_ANC_QUEUE
:
288 case BPF_S_ANC_HATYPE
:
289 case BPF_S_ANC_RXHASH
:
291 case BPF_S_ANC_VLAN_TAG
:
292 case BPF_S_ANC_VLAN_TAG_PRESENT
:
294 /* first instruction sets A register */
302 static int bpf_jit_insn(struct bpf_jit
*jit
, struct sock_filter
*filter
,
303 unsigned int *addrs
, int i
, int last
)
310 switch (filter
->code
) {
311 case BPF_S_ALU_ADD_X
: /* A += X */
312 jit
->seen
|= SEEN_XREG
;
316 case BPF_S_ALU_ADD_K
: /* A += K */
321 EMIT4_IMM(0xa75a0000, K
);
322 else if (test_facility(21))
324 EMIT6_IMM(0xc25b0000, K
);
326 /* a %r5,<d(K)>(%r13) */
327 EMIT4_DISP(0x5a50d000, EMIT_CONST(K
));
329 case BPF_S_ALU_SUB_X
: /* A -= X */
330 jit
->seen
|= SEEN_XREG
;
334 case BPF_S_ALU_SUB_K
: /* A -= K */
339 EMIT4_IMM(0xa75a0000, -K
);
340 else if (test_facility(21))
342 EMIT6_IMM(0xc25b0000, -K
);
344 /* s %r5,<d(K)>(%r13) */
345 EMIT4_DISP(0x5b50d000, EMIT_CONST(K
));
347 case BPF_S_ALU_MUL_X
: /* A *= X */
348 jit
->seen
|= SEEN_XREG
;
352 case BPF_S_ALU_MUL_K
: /* A *= K */
355 EMIT4_IMM(0xa75c0000, K
);
356 else if (test_facility(34))
358 EMIT6_IMM(0xc2510000, K
);
360 /* ms %r5,<d(K)>(%r13) */
361 EMIT4_DISP(0x7150d000, EMIT_CONST(K
));
363 case BPF_S_ALU_DIV_X
: /* A /= X */
364 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
368 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
374 case BPF_S_ALU_DIV_K
: /* A = reciprocal_divide(A, K) */
375 /* m %r4,<d(K)>(%r13) */
376 EMIT4_DISP(0x5c40d000, EMIT_CONST(K
));
380 case BPF_S_ALU_MOD_X
: /* A %= X */
381 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
385 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
393 case BPF_S_ALU_MOD_K
: /* A %= K */
396 /* d %r4,<d(K)>(%r13) */
397 EMIT4_DISP(0x5d40d000, EMIT_CONST(K
));
401 case BPF_S_ALU_AND_X
: /* A &= X */
402 jit
->seen
|= SEEN_XREG
;
406 case BPF_S_ALU_AND_K
: /* A &= K */
407 if (test_facility(21))
409 EMIT6_IMM(0xc05b0000, K
);
411 /* n %r5,<d(K)>(%r13) */
412 EMIT4_DISP(0x5450d000, EMIT_CONST(K
));
414 case BPF_S_ALU_OR_X
: /* A |= X */
415 jit
->seen
|= SEEN_XREG
;
419 case BPF_S_ALU_OR_K
: /* A |= K */
420 if (test_facility(21))
422 EMIT6_IMM(0xc05d0000, K
);
424 /* o %r5,<d(K)>(%r13) */
425 EMIT4_DISP(0x5650d000, EMIT_CONST(K
));
427 case BPF_S_ANC_ALU_XOR_X
: /* A ^= X; */
428 case BPF_S_ALU_XOR_X
:
429 jit
->seen
|= SEEN_XREG
;
433 case BPF_S_ALU_XOR_K
: /* A ^= K */
436 /* x %r5,<d(K)>(%r13) */
437 EMIT4_DISP(0x5750d000, EMIT_CONST(K
));
439 case BPF_S_ALU_LSH_X
: /* A <<= X; */
440 jit
->seen
|= SEEN_XREG
;
441 /* sll %r5,0(%r12) */
444 case BPF_S_ALU_LSH_K
: /* A <<= K */
448 EMIT4_DISP(0x89500000, K
);
450 case BPF_S_ALU_RSH_X
: /* A >>= X; */
451 jit
->seen
|= SEEN_XREG
;
452 /* srl %r5,0(%r12) */
455 case BPF_S_ALU_RSH_K
: /* A >>= K; */
459 EMIT4_DISP(0x88500000, K
);
461 case BPF_S_ALU_NEG
: /* A = -A */
465 case BPF_S_JMP_JA
: /* ip += K */
466 offset
= addrs
[i
+ K
] + jit
->start
- jit
->prg
;
467 EMIT4_PCREL(0xa7f40000, offset
);
469 case BPF_S_JMP_JGT_K
: /* ip += (A > K) ? jt : jf */
470 mask
= 0x200000; /* jh */
472 case BPF_S_JMP_JGE_K
: /* ip += (A >= K) ? jt : jf */
473 mask
= 0xa00000; /* jhe */
475 case BPF_S_JMP_JEQ_K
: /* ip += (A == K) ? jt : jf */
476 mask
= 0x800000; /* je */
477 kbranch
: /* Emit compare if the branch targets are different */
478 if (filter
->jt
!= filter
->jf
) {
481 EMIT4_IMM(0xa75e0000, K
);
482 else if (test_facility(21))
484 EMIT6_IMM(0xc25f0000, K
);
486 /* c %r5,<d(K)>(%r13) */
487 EMIT4_DISP(0x5950d000, EMIT_CONST(K
));
489 branch
: if (filter
->jt
== filter
->jf
) {
493 offset
= addrs
[i
+ filter
->jt
] + jit
->start
- jit
->prg
;
494 EMIT4_PCREL(0xa7f40000, offset
);
497 if (filter
->jt
!= 0) {
498 /* brc <mask>,<jt> */
499 offset
= addrs
[i
+ filter
->jt
] + jit
->start
- jit
->prg
;
500 EMIT4_PCREL(0xa7040000 | mask
, offset
);
502 if (filter
->jf
!= 0) {
503 /* brc <mask^15>,<jf> */
504 offset
= addrs
[i
+ filter
->jf
] + jit
->start
- jit
->prg
;
505 EMIT4_PCREL(0xa7040000 | (mask
^ 0xf00000), offset
);
508 case BPF_S_JMP_JSET_K
: /* ip += (A & K) ? jt : jf */
509 mask
= 0x700000; /* jnz */
510 /* Emit test if the branch targets are different */
511 if (filter
->jt
!= filter
->jf
) {
515 /* n %r4,<d(K)>(%r13) */
516 EMIT4_DISP(0x5440d000, EMIT_CONST(K
));
519 EMIT4_IMM(0xa7510000, K
);
522 case BPF_S_JMP_JGT_X
: /* ip += (A > X) ? jt : jf */
523 mask
= 0x200000; /* jh */
525 case BPF_S_JMP_JGE_X
: /* ip += (A >= X) ? jt : jf */
526 mask
= 0xa00000; /* jhe */
528 case BPF_S_JMP_JEQ_X
: /* ip += (A == X) ? jt : jf */
529 mask
= 0x800000; /* je */
530 xbranch
: /* Emit compare if the branch targets are different */
531 if (filter
->jt
!= filter
->jf
) {
532 jit
->seen
|= SEEN_XREG
;
537 case BPF_S_JMP_JSET_X
: /* ip += (A & X) ? jt : jf */
538 mask
= 0x700000; /* jnz */
539 /* Emit test if the branch targets are different */
540 if (filter
->jt
!= filter
->jf
) {
541 jit
->seen
|= SEEN_XREG
;
548 case BPF_S_LD_W_ABS
: /* A = *(u32 *) (skb->data+K) */
549 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_WORD
;
550 offset
= jit
->off_load_word
;
552 case BPF_S_LD_H_ABS
: /* A = *(u16 *) (skb->data+K) */
553 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_HALF
;
554 offset
= jit
->off_load_half
;
556 case BPF_S_LD_B_ABS
: /* A = *(u8 *) (skb->data+K) */
557 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_BYTE
;
558 offset
= jit
->off_load_byte
;
559 load_abs
: if ((int) K
< 0)
561 call_fn
: /* lg %r1,<d(function)>(%r13) */
562 EMIT6_DISP(0xe310d000, 0x0004, offset
);
563 /* l %r3,<d(K)>(%r13) */
564 EMIT4_DISP(0x5830d000, EMIT_CONST(K
));
568 EMIT4_PCREL(0xa7740000, (jit
->ret0_ip
- jit
->prg
));
570 case BPF_S_LD_W_IND
: /* A = *(u32 *) (skb->data+K+X) */
571 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IWORD
;
572 offset
= jit
->off_load_iword
;
574 case BPF_S_LD_H_IND
: /* A = *(u16 *) (skb->data+K+X) */
575 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IHALF
;
576 offset
= jit
->off_load_ihalf
;
578 case BPF_S_LD_B_IND
: /* A = *(u8 *) (skb->data+K+X) */
579 jit
->seen
|= SEEN_DATAREF
| SEEN_RET0
| SEEN_LOAD_IBYTE
;
580 offset
= jit
->off_load_ibyte
;
582 case BPF_S_LDX_B_MSH
:
583 /* X = (*(u8 *)(skb->data+K) & 0xf) << 2 */
584 jit
->seen
|= SEEN_RET0
;
587 EMIT4_PCREL(0xa7f40000, (jit
->ret0_ip
- jit
->prg
));
590 jit
->seen
|= SEEN_DATAREF
| SEEN_LOAD_BMSH
;
591 offset
= jit
->off_load_bmsh
;
593 case BPF_S_LD_W_LEN
: /* A = skb->len; */
594 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, len
) != 4);
595 /* l %r5,<d(len)>(%r2) */
596 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, len
));
598 case BPF_S_LDX_W_LEN
: /* X = skb->len; */
599 jit
->seen
|= SEEN_XREG
;
600 /* l %r12,<d(len)>(%r2) */
601 EMIT4_DISP(0x58c02000, offsetof(struct sk_buff
, len
));
603 case BPF_S_LD_IMM
: /* A = K */
606 EMIT4_IMM(0xa7580000, K
);
607 else if (test_facility(21))
609 EMIT6_IMM(0xc05f0000, K
);
611 /* l %r5,<d(K)>(%r13) */
612 EMIT4_DISP(0x5850d000, EMIT_CONST(K
));
614 case BPF_S_LDX_IMM
: /* X = K */
615 jit
->seen
|= SEEN_XREG
;
618 EMIT4_IMM(0xa7c80000, K
);
619 else if (test_facility(21))
621 EMIT6_IMM(0xc0cf0000, K
);
623 /* l %r12,<d(K)>(%r13) */
624 EMIT4_DISP(0x58c0d000, EMIT_CONST(K
));
626 case BPF_S_LD_MEM
: /* A = mem[K] */
627 jit
->seen
|= SEEN_MEM
;
628 /* l %r5,<K>(%r15) */
629 EMIT4_DISP(0x5850f000,
630 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
632 case BPF_S_LDX_MEM
: /* X = mem[K] */
633 jit
->seen
|= SEEN_XREG
| SEEN_MEM
;
634 /* l %r12,<K>(%r15) */
635 EMIT4_DISP(0x58c0f000,
636 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
638 case BPF_S_MISC_TAX
: /* X = A */
639 jit
->seen
|= SEEN_XREG
;
643 case BPF_S_MISC_TXA
: /* A = X */
644 jit
->seen
|= SEEN_XREG
;
650 jit
->seen
|= SEEN_RET0
;
654 EMIT4_PCREL(0xa7f40000, jit
->ret0_ip
- jit
->prg
);
658 EMIT4_IMM(0xa7290000, K
);
660 /* llgf %r2,<K>(%r13) */
661 EMIT6_DISP(0xe320d000, 0x0016, EMIT_CONST(K
));
663 if (last
&& !(jit
->seen
& SEEN_RET0
))
665 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
672 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
674 case BPF_S_ST
: /* mem[K] = A */
675 jit
->seen
|= SEEN_MEM
;
676 /* st %r5,<K>(%r15) */
677 EMIT4_DISP(0x5050f000,
678 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
680 case BPF_S_STX
: /* mem[K] = X : mov %ebx,off8(%rbp) */
681 jit
->seen
|= SEEN_XREG
| SEEN_MEM
;
682 /* st %r12,<K>(%r15) */
683 EMIT4_DISP(0x50c0f000,
684 (jit
->seen
& SEEN_DATAREF
) ? 160 + K
*4 : K
*4);
686 case BPF_S_ANC_PROTOCOL
: /* A = ntohs(skb->protocol); */
687 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
690 /* icm %r5,3,<d(protocol)>(%r2) */
691 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, protocol
));
693 case BPF_S_ANC_IFINDEX
: /* if (!skb->dev) return 0;
694 * A = skb->dev->ifindex */
695 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
696 jit
->seen
|= SEEN_RET0
;
697 /* lg %r1,<d(dev)>(%r2) */
698 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff
, dev
));
702 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
703 /* l %r5,<d(ifindex)>(%r1) */
704 EMIT4_DISP(0x58501000, offsetof(struct net_device
, ifindex
));
706 case BPF_S_ANC_MARK
: /* A = skb->mark */
707 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
708 /* l %r5,<d(mark)>(%r2) */
709 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, mark
));
711 case BPF_S_ANC_QUEUE
: /* A = skb->queue_mapping */
712 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
715 /* icm %r5,3,<d(queue_mapping)>(%r2) */
716 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, queue_mapping
));
718 case BPF_S_ANC_HATYPE
: /* if (!skb->dev) return 0;
719 * A = skb->dev->type */
720 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, type
) != 2);
721 jit
->seen
|= SEEN_RET0
;
722 /* lg %r1,<d(dev)>(%r2) */
723 EMIT6_DISP(0xe3102000, 0x0004, offsetof(struct sk_buff
, dev
));
727 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
730 /* icm %r5,3,<d(type)>(%r1) */
731 EMIT4_DISP(0xbf531000, offsetof(struct net_device
, type
));
733 case BPF_S_ANC_RXHASH
: /* A = skb->rxhash */
734 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, rxhash
) != 4);
735 /* l %r5,<d(rxhash)>(%r2) */
736 EMIT4_DISP(0x58502000, offsetof(struct sk_buff
, rxhash
));
738 case BPF_S_ANC_VLAN_TAG
:
739 case BPF_S_ANC_VLAN_TAG_PRESENT
:
740 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
741 BUILD_BUG_ON(VLAN_TAG_PRESENT
!= 0x1000);
744 /* icm %r5,3,<d(vlan_tci)>(%r2) */
745 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, vlan_tci
));
746 if (filter
->code
== BPF_S_ANC_VLAN_TAG
) {
747 /* nill %r5,0xefff */
748 EMIT4_IMM(0xa5570000, ~VLAN_TAG_PRESENT
);
750 /* nill %r5,0x1000 */
751 EMIT4_IMM(0xa5570000, VLAN_TAG_PRESENT
);
753 EMIT4_DISP(0x88500000, 12);
756 case BPF_S_ANC_PKTTYPE
:
757 if (pkt_type_offset
< 0)
761 /* ic %r5,<d(pkt_type_offset)>(%r2) */
762 EMIT4_DISP(0x43502000, pkt_type_offset
);
764 EMIT4_DISP(0x88500000, 5);
766 case BPF_S_ANC_CPU
: /* A = smp_processor_id() */
768 /* l %r5,<d(cpu_nr)> */
769 EMIT4_DISP(0x58500000, offsetof(struct _lowcore
, cpu_nr
));
775 default: /* too complex, give up */
778 addrs
[i
] = jit
->prg
- jit
->start
;
785 * Note: for security reasons, bpf code will follow a randomly
786 * sized amount of illegal instructions.
788 struct bpf_binary_header
{
793 static struct bpf_binary_header
*bpf_alloc_binary(unsigned int bpfsize
,
796 struct bpf_binary_header
*header
;
797 unsigned int sz
, hole
;
799 /* Most BPF filters are really small, but if some of them fill a page,
800 * allow at least 128 extra bytes for illegal instructions.
802 sz
= round_up(bpfsize
+ sizeof(*header
) + 128, PAGE_SIZE
);
803 header
= module_alloc(sz
);
806 memset(header
, 0, sz
);
807 header
->pages
= sz
/ PAGE_SIZE
;
808 hole
= sz
- bpfsize
+ sizeof(*header
);
809 /* Insert random number of illegal instructions before BPF code
810 * and make sure the first instruction starts at an even address.
812 *image_ptr
= &header
->image
[(prandom_u32() % hole
) & -2];
816 void bpf_jit_compile(struct sk_filter
*fp
)
818 struct bpf_binary_header
*header
= NULL
;
819 unsigned long size
, prg_len
, lit_len
;
820 struct bpf_jit jit
, cjit
;
826 addrs
= kcalloc(fp
->len
, sizeof(*addrs
), GFP_KERNEL
);
829 memset(&jit
, 0, sizeof(cjit
));
830 memset(&cjit
, 0, sizeof(cjit
));
832 for (pass
= 0; pass
< 10; pass
++) {
836 bpf_jit_prologue(&jit
);
837 bpf_jit_noleaks(&jit
, fp
->insns
);
838 for (i
= 0; i
< fp
->len
; i
++) {
839 if (bpf_jit_insn(&jit
, fp
->insns
+ i
, addrs
, i
,
843 bpf_jit_epilogue(&jit
);
845 WARN_ON(jit
.prg
> cjit
.prg
|| jit
.lit
> cjit
.lit
);
846 if (memcmp(&jit
, &cjit
, sizeof(jit
)) == 0)
848 } else if (jit
.prg
== cjit
.prg
&& jit
.lit
== cjit
.lit
) {
849 prg_len
= jit
.prg
- jit
.start
;
850 lit_len
= jit
.lit
- jit
.mid
;
851 size
= prg_len
+ lit_len
;
852 if (size
>= BPF_SIZE_MAX
)
854 header
= bpf_alloc_binary(size
, &jit
.start
);
857 jit
.prg
= jit
.mid
= jit
.start
+ prg_len
;
858 jit
.lit
= jit
.end
= jit
.start
+ prg_len
+ lit_len
;
859 jit
.base_ip
+= (unsigned long) jit
.start
;
860 jit
.exit_ip
+= (unsigned long) jit
.start
;
861 jit
.ret0_ip
+= (unsigned long) jit
.start
;
865 if (bpf_jit_enable
> 1) {
866 bpf_jit_dump(fp
->len
, jit
.end
- jit
.start
, pass
, jit
.start
);
868 print_fn_code(jit
.start
, jit
.mid
- jit
.start
);
871 set_memory_ro((unsigned long)header
, header
->pages
);
872 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
;
883 if (fp
->bpf_func
== sk_run_filter
)
885 set_memory_rw(addr
, header
->pages
);
886 module_free(NULL
, header
);