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 <asm/cacheflush.h>
13 #include <asm/processor.h>
14 #include <asm/facility.h>
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
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
[];
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 */
74 if (jit->prg + 2 <= jit->mid) \
75 *(u16 *) jit->prg = op; \
81 if (jit->prg + 4 <= jit->mid) \
82 *(u32 *) jit->prg = op; \
86 #define EMIT4_DISP(op, disp) \
88 unsigned int __disp = (disp) & 0xfff; \
92 #define EMIT4_IMM(op, imm) \
94 unsigned int __imm = (imm) & 0xffff; \
98 #define EMIT4_PCREL(op, pcrel) \
100 long __pcrel = ((pcrel) >> 1) & 0xffff; \
101 EMIT4(op | __pcrel); \
104 #define EMIT6(op1, op2) \
106 if (jit->prg + 6 <= jit->mid) { \
107 *(u32 *) jit->prg = op1; \
108 *(u16 *) (jit->prg + 4) = op2; \
113 #define EMIT6_DISP(op1, op2, disp) \
115 unsigned int __disp = (disp) & 0xfff; \
116 EMIT6(op1 | __disp, op2); \
119 #define EMIT6_IMM(op, imm) \
121 unsigned int __imm = (imm); \
122 EMIT6(op | (__imm >> 16), __imm & 0xffff); \
125 #define EMIT_CONST(val) \
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; \
136 #define EMIT_FN_CONST(bit, fn) \
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; \
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);
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
) {
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
)
200 if (jit
->seen
& SEEN_RET0
) {
201 jit
->ret0_ip
= jit
->prg
;
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);
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
)
237 /* Clear A if the first register does not set it. */
238 switch (filter
[0].code
) {
249 case BPF_S_ANC_PROTOCOL
:
250 case BPF_S_ANC_PKTTYPE
:
251 case BPF_S_ANC_IFINDEX
:
253 case BPF_S_ANC_QUEUE
:
254 case BPF_S_ANC_HATYPE
:
255 case BPF_S_ANC_RXHASH
:
257 case BPF_S_ANC_VLAN_TAG
:
258 case BPF_S_ANC_VLAN_TAG_PRESENT
:
260 /* first instruction sets A register */
268 static int bpf_jit_insn(struct bpf_jit
*jit
, struct sock_filter
*filter
,
269 unsigned int *addrs
, int i
, int last
)
276 switch (filter
->code
) {
277 case BPF_S_ALU_ADD_X
: /* A += X */
278 jit
->seen
|= SEEN_XREG
;
282 case BPF_S_ALU_ADD_K
: /* A += K */
287 EMIT4_IMM(0xa75a0000, K
);
288 else if (test_facility(21))
290 EMIT6_IMM(0xc25b0000, K
);
292 /* a %r5,<d(K)>(%r13) */
293 EMIT4_DISP(0x5a50d000, EMIT_CONST(K
));
295 case BPF_S_ALU_SUB_X
: /* A -= X */
296 jit
->seen
|= SEEN_XREG
;
300 case BPF_S_ALU_SUB_K
: /* A -= K */
305 EMIT4_IMM(0xa75a0000, -K
);
306 else if (test_facility(21))
308 EMIT6_IMM(0xc25b0000, -K
);
310 /* s %r5,<d(K)>(%r13) */
311 EMIT4_DISP(0x5b50d000, EMIT_CONST(K
));
313 case BPF_S_ALU_MUL_X
: /* A *= X */
314 jit
->seen
|= SEEN_XREG
;
318 case BPF_S_ALU_MUL_K
: /* A *= K */
321 EMIT4_IMM(0xa75c0000, K
);
322 else if (test_facility(34))
324 EMIT6_IMM(0xc2510000, K
);
326 /* ms %r5,<d(K)>(%r13) */
327 EMIT4_DISP(0x7150d000, EMIT_CONST(K
));
329 case BPF_S_ALU_DIV_X
: /* A /= X */
330 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
334 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
340 case BPF_S_ALU_DIV_K
: /* A /= K */
345 /* dl %r4,<d(K)>(%r13) */
346 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K
));
348 case BPF_S_ALU_MOD_X
: /* A %= X */
349 jit
->seen
|= SEEN_XREG
| SEEN_RET0
;
353 EMIT4_PCREL(0xa7840000, (jit
->ret0_ip
- jit
->prg
));
361 case BPF_S_ALU_MOD_K
: /* A %= K */
369 /* dl %r4,<d(K)>(%r13) */
370 EMIT6_DISP(0xe340d000, 0x0097, EMIT_CONST(K
));
374 case BPF_S_ALU_AND_X
: /* A &= X */
375 jit
->seen
|= SEEN_XREG
;
379 case BPF_S_ALU_AND_K
: /* A &= K */
380 if (test_facility(21))
382 EMIT6_IMM(0xc05b0000, K
);
384 /* n %r5,<d(K)>(%r13) */
385 EMIT4_DISP(0x5450d000, EMIT_CONST(K
));
387 case BPF_S_ALU_OR_X
: /* A |= X */
388 jit
->seen
|= SEEN_XREG
;
392 case BPF_S_ALU_OR_K
: /* A |= K */
393 if (test_facility(21))
395 EMIT6_IMM(0xc05d0000, K
);
397 /* o %r5,<d(K)>(%r13) */
398 EMIT4_DISP(0x5650d000, EMIT_CONST(K
));
400 case BPF_S_ANC_ALU_XOR_X
: /* A ^= X; */
401 case BPF_S_ALU_XOR_X
:
402 jit
->seen
|= SEEN_XREG
;
406 case BPF_S_ALU_XOR_K
: /* A ^= K */
409 /* x %r5,<d(K)>(%r13) */
410 EMIT4_DISP(0x5750d000, EMIT_CONST(K
));
412 case BPF_S_ALU_LSH_X
: /* A <<= X; */
413 jit
->seen
|= SEEN_XREG
;
414 /* sll %r5,0(%r12) */
417 case BPF_S_ALU_LSH_K
: /* A <<= K */
421 EMIT4_DISP(0x89500000, K
);
423 case BPF_S_ALU_RSH_X
: /* A >>= X; */
424 jit
->seen
|= SEEN_XREG
;
425 /* srl %r5,0(%r12) */
428 case BPF_S_ALU_RSH_K
: /* A >>= K; */
432 EMIT4_DISP(0x88500000, K
);
434 case BPF_S_ALU_NEG
: /* A = -A */
438 case BPF_S_JMP_JA
: /* ip += K */
439 offset
= addrs
[i
+ K
] + jit
->start
- jit
->prg
;
440 EMIT4_PCREL(0xa7f40000, offset
);
442 case BPF_S_JMP_JGT_K
: /* ip += (A > K) ? jt : jf */
443 mask
= 0x200000; /* jh */
445 case BPF_S_JMP_JGE_K
: /* ip += (A >= K) ? jt : jf */
446 mask
= 0xa00000; /* jhe */
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
) {
454 EMIT4_IMM(0xa75e0000, K
);
455 else if (test_facility(21))
457 EMIT6_IMM(0xc25f0000, K
);
459 /* c %r5,<d(K)>(%r13) */
460 EMIT4_DISP(0x5950d000, EMIT_CONST(K
));
462 branch
: if (filter
->jt
== filter
->jf
) {
466 offset
= addrs
[i
+ filter
->jt
] + jit
->start
- jit
->prg
;
467 EMIT4_PCREL(0xa7f40000, offset
);
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
);
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
) {
488 /* n %r4,<d(K)>(%r13) */
489 EMIT4_DISP(0x5440d000, EMIT_CONST(K
));
492 EMIT4_IMM(0xa7510000, K
);
495 case BPF_S_JMP_JGT_X
: /* ip += (A > X) ? jt : jf */
496 mask
= 0x200000; /* jh */
498 case BPF_S_JMP_JGE_X
: /* ip += (A >= X) ? jt : jf */
499 mask
= 0xa00000; /* jhe */
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
;
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
;
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
;
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
;
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)
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
));
541 EMIT4_PCREL(0xa7740000, (jit
->ret0_ip
- jit
->prg
));
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
;
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
;
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
;
555 case BPF_S_LDX_B_MSH
:
556 /* X = (*(u8 *)(skb->data+K) & 0xf) << 2 */
557 jit
->seen
|= SEEN_RET0
;
560 EMIT4_PCREL(0xa7f40000, (jit
->ret0_ip
- jit
->prg
));
563 jit
->seen
|= SEEN_DATAREF
| SEEN_LOAD_BMSH
;
564 offset
= jit
->off_load_bmsh
;
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
));
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
));
576 case BPF_S_LD_IMM
: /* A = K */
579 EMIT4_IMM(0xa7580000, K
);
580 else if (test_facility(21))
582 EMIT6_IMM(0xc05f0000, K
);
584 /* l %r5,<d(K)>(%r13) */
585 EMIT4_DISP(0x5850d000, EMIT_CONST(K
));
587 case BPF_S_LDX_IMM
: /* X = K */
588 jit
->seen
|= SEEN_XREG
;
591 EMIT4_IMM(0xa7c80000, K
);
592 else if (test_facility(21))
594 EMIT6_IMM(0xc0cf0000, K
);
596 /* l %r12,<d(K)>(%r13) */
597 EMIT4_DISP(0x58c0d000, EMIT_CONST(K
));
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);
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);
611 case BPF_S_MISC_TAX
: /* X = A */
612 jit
->seen
|= SEEN_XREG
;
616 case BPF_S_MISC_TXA
: /* A = X */
617 jit
->seen
|= SEEN_XREG
;
623 jit
->seen
|= SEEN_RET0
;
627 EMIT4_PCREL(0xa7f40000, jit
->ret0_ip
- jit
->prg
);
631 EMIT4_IMM(0xa7290000, K
);
633 /* llgf %r2,<K>(%r13) */
634 EMIT6_DISP(0xe320d000, 0x0016, EMIT_CONST(K
));
636 if (last
&& !(jit
->seen
& SEEN_RET0
))
638 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
645 EMIT4_PCREL(0xa7f40000, jit
->exit_ip
- jit
->prg
);
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);
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);
659 case BPF_S_ANC_PROTOCOL
: /* A = ntohs(skb->protocol); */
660 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
663 /* icm %r5,3,<d(protocol)>(%r2) */
664 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, protocol
));
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
));
675 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
676 /* l %r5,<d(ifindex)>(%r1) */
677 EMIT4_DISP(0x58501000, offsetof(struct net_device
, ifindex
));
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
));
684 case BPF_S_ANC_QUEUE
: /* A = skb->queue_mapping */
685 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
688 /* icm %r5,3,<d(queue_mapping)>(%r2) */
689 EMIT4_DISP(0xbf532000, offsetof(struct sk_buff
, queue_mapping
));
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
));
700 EMIT4_PCREL(0xa7840000, jit
->ret0_ip
- jit
->prg
);
703 /* icm %r5,3,<d(type)>(%r1) */
704 EMIT4_DISP(0xbf531000, offsetof(struct net_device
, type
));
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
));
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);
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
);
723 /* nill %r5,0x1000 */
724 EMIT4_IMM(0xa5570000, VLAN_TAG_PRESENT
);
726 EMIT4_DISP(0x88500000, 12);
729 case BPF_S_ANC_CPU
: /* A = smp_processor_id() */
731 /* l %r5,<d(cpu_nr)> */
732 EMIT4_DISP(0x58500000, offsetof(struct _lowcore
, cpu_nr
));
738 default: /* too complex, give up */
741 addrs
[i
] = jit
->prg
- jit
->start
;
747 void bpf_jit_compile(struct sk_filter
*fp
)
749 unsigned long size
, prg_len
, lit_len
;
750 struct bpf_jit jit
, cjit
;
756 addrs
= kcalloc(fp
->len
, sizeof(*addrs
), GFP_KERNEL
);
759 memset(&jit
, 0, sizeof(cjit
));
760 memset(&cjit
, 0, sizeof(cjit
));
762 for (pass
= 0; pass
< 10; pass
++) {
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
,
773 bpf_jit_epilogue(&jit
);
775 WARN_ON(jit
.prg
> cjit
.prg
|| jit
.lit
> cjit
.lit
);
776 if (memcmp(&jit
, &cjit
, sizeof(jit
)) == 0)
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
)
785 jit
.start
= module_alloc(size
);
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
;
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
);
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);
808 fp
->bpf_func
= (void *) jit
.start
;
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
)
827 work
= (struct work_struct
*)fp
->bpf_func
;
828 INIT_WORK(work
, jit_free_defer
);