1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/moduleloader.h>
3 #include <linux/workqueue.h>
4 #include <linux/netdevice.h>
5 #include <linux/filter.h>
6 #include <linux/cache.h>
7 #include <linux/if_vlan.h>
9 #include <asm/cacheflush.h>
10 #include <asm/ptrace.h>
12 #include "bpf_jit_32.h"
14 static inline bool is_simm13(unsigned int value)
16 return value + 0x1000 < 0x2000;
19 #define SEEN_DATAREF 1 /* might call external helpers */
20 #define SEEN_XREG 2 /* ebx is used */
21 #define SEEN_MEM 4 /* use mem[] for temporary storage */
23 #define S13(X) ((X) & 0x1fff)
24 #define IMMED 0x00002000
25 #define RD(X) ((X) << 25)
26 #define RS1(X) ((X) << 14)
28 #define OP(X) ((X) << 30)
29 #define OP2(X) ((X) << 22)
30 #define OP3(X) ((X) << 19)
31 #define COND(X) ((X) << 25)
33 #define F2(X, Y) (OP(X) | OP2(Y))
34 #define F3(X, Y) (OP(X) | OP3(Y))
36 #define CONDN COND(0x0)
37 #define CONDE COND(0x1)
38 #define CONDLE COND(0x2)
39 #define CONDL COND(0x3)
40 #define CONDLEU COND(0x4)
41 #define CONDCS COND(0x5)
42 #define CONDNEG COND(0x6)
43 #define CONDVC COND(0x7)
44 #define CONDA COND(0x8)
45 #define CONDNE COND(0x9)
46 #define CONDG COND(0xa)
47 #define CONDGE COND(0xb)
48 #define CONDGU COND(0xc)
49 #define CONDCC COND(0xd)
50 #define CONDPOS COND(0xe)
51 #define CONDVS COND(0xf)
53 #define CONDGEU CONDCC
56 #define WDISP22(X) (((X) >> 2) & 0x3fffff)
58 #define BA (F2(0, 2) | CONDA)
59 #define BGU (F2(0, 2) | CONDGU)
60 #define BLEU (F2(0, 2) | CONDLEU)
61 #define BGEU (F2(0, 2) | CONDGEU)
62 #define BLU (F2(0, 2) | CONDLU)
63 #define BE (F2(0, 2) | CONDE)
64 #define BNE (F2(0, 2) | CONDNE)
68 #define SETHI(K, REG) \
69 (F2(0, 0x4) | RD(REG) | (((K) >> 10) & 0x3fffff))
70 #define OR_LO(K, REG) \
71 (F3(2, 0x02) | IMMED | RS1(REG) | ((K) & 0x3ff) | RD(REG))
73 #define ADD F3(2, 0x00)
74 #define AND F3(2, 0x01)
75 #define ANDCC F3(2, 0x11)
76 #define OR F3(2, 0x02)
77 #define XOR F3(2, 0x03)
78 #define SUB F3(2, 0x04)
79 #define SUBCC F3(2, 0x14)
80 #define MUL F3(2, 0x0a) /* umul */
81 #define DIV F3(2, 0x0e) /* udiv */
82 #define SLL F3(2, 0x25)
83 #define SRL F3(2, 0x26)
84 #define JMPL F3(2, 0x38)
86 #define BR F2(0, 0x01)
87 #define RD_Y F3(2, 0x28)
88 #define WR_Y F3(2, 0x30)
90 #define LD32 F3(3, 0x00)
91 #define LD8 F3(3, 0x01)
92 #define LD16 F3(3, 0x02)
93 #define LD64 F3(3, 0x0b)
94 #define ST32 F3(3, 0x04)
97 #define BASE_STACKFRAME 96
99 #define LD32I (LD32 | IMMED)
100 #define LD8I (LD8 | IMMED)
101 #define LD16I (LD16 | IMMED)
102 #define LD64I (LD64 | IMMED)
103 #define LDPTRI (LDPTR | IMMED)
104 #define ST32I (ST32 | IMMED)
108 *prog++ = SETHI(0, G0); \
112 do { /* sub %g0, r_A, r_A */ \
113 *prog++ = SUB | RS1(G0) | RS2(r_A) | RD(r_A); \
116 #define emit_reg_move(FROM, TO) \
117 do { /* or %g0, FROM, TO */ \
118 *prog++ = OR | RS1(G0) | RS2(FROM) | RD(TO); \
121 #define emit_clear(REG) \
122 do { /* or %g0, %g0, REG */ \
123 *prog++ = OR | RS1(G0) | RS2(G0) | RD(REG); \
126 #define emit_set_const(K, REG) \
127 do { /* sethi %hi(K), REG */ \
128 *prog++ = SETHI(K, REG); \
129 /* or REG, %lo(K), REG */ \
130 *prog++ = OR_LO(K, REG); \
137 #define emit_alu_X(OPCODE) \
140 *prog++ = OPCODE | RS1(r_A) | RS2(r_X) | RD(r_A); \
149 * sethi %hi(K), r_TMP
150 * or r_TMP, %lo(K), r_TMP
153 * depending upon whether K fits in a signed 13-bit
154 * immediate instruction field. Emit nothing if K
157 #define emit_alu_K(OPCODE, K) \
159 if (K || OPCODE == AND || OPCODE == MUL) { \
160 unsigned int _insn = OPCODE; \
161 _insn |= RS1(r_A) | RD(r_A); \
162 if (is_simm13(K)) { \
163 *prog++ = _insn | IMMED | S13(K); \
165 emit_set_const(K, r_TMP); \
166 *prog++ = _insn | RS2(r_TMP); \
171 #define emit_loadimm(K, DEST) \
173 if (is_simm13(K)) { \
174 /* or %g0, K, DEST */ \
175 *prog++ = OR | IMMED | RS1(G0) | S13(K) | RD(DEST); \
177 emit_set_const(K, DEST); \
181 #define emit_loadptr(BASE, STRUCT, FIELD, DEST) \
182 do { unsigned int _off = offsetof(STRUCT, FIELD); \
183 BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(void *)); \
184 *prog++ = LDPTRI | RS1(BASE) | S13(_off) | RD(DEST); \
187 #define emit_load32(BASE, STRUCT, FIELD, DEST) \
188 do { unsigned int _off = offsetof(STRUCT, FIELD); \
189 BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u32)); \
190 *prog++ = LD32I | RS1(BASE) | S13(_off) | RD(DEST); \
193 #define emit_load16(BASE, STRUCT, FIELD, DEST) \
194 do { unsigned int _off = offsetof(STRUCT, FIELD); \
195 BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u16)); \
196 *prog++ = LD16I | RS1(BASE) | S13(_off) | RD(DEST); \
199 #define __emit_load8(BASE, STRUCT, FIELD, DEST) \
200 do { unsigned int _off = offsetof(STRUCT, FIELD); \
201 *prog++ = LD8I | RS1(BASE) | S13(_off) | RD(DEST); \
204 #define emit_load8(BASE, STRUCT, FIELD, DEST) \
205 do { BUILD_BUG_ON(sizeof_field(STRUCT, FIELD) != sizeof(u8)); \
206 __emit_load8(BASE, STRUCT, FIELD, DEST); \
211 #define emit_ldmem(OFF, DEST) \
212 do { *prog++ = LD32I | RS1(SP) | S13(BIAS - (OFF)) | RD(DEST); \
215 #define emit_stmem(OFF, SRC) \
216 do { *prog++ = ST32I | RS1(SP) | S13(BIAS - (OFF)) | RD(SRC); \
220 #define emit_load_cpu(REG) \
221 emit_load32(G6, struct thread_info, cpu, REG)
223 #define emit_load_cpu(REG) emit_clear(REG)
226 #define emit_skb_loadptr(FIELD, DEST) \
227 emit_loadptr(r_SKB, struct sk_buff, FIELD, DEST)
228 #define emit_skb_load32(FIELD, DEST) \
229 emit_load32(r_SKB, struct sk_buff, FIELD, DEST)
230 #define emit_skb_load16(FIELD, DEST) \
231 emit_load16(r_SKB, struct sk_buff, FIELD, DEST)
232 #define __emit_skb_load8(FIELD, DEST) \
233 __emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
234 #define emit_skb_load8(FIELD, DEST) \
235 emit_load8(r_SKB, struct sk_buff, FIELD, DEST)
237 #define emit_jmpl(BASE, IMM_OFF, LREG) \
238 *prog++ = (JMPL | IMMED | RS1(BASE) | S13(IMM_OFF) | RD(LREG))
240 #define emit_call(FUNC) \
241 do { void *_here = image + addrs[i] - 8; \
242 unsigned int _off = (void *)(FUNC) - _here; \
243 *prog++ = CALL | (((_off) >> 2) & 0x3fffffff); \
247 #define emit_branch(BR_OPC, DEST) \
248 do { unsigned int _here = addrs[i] - 8; \
249 *prog++ = BR_OPC | WDISP22((DEST) - _here); \
252 #define emit_branch_off(BR_OPC, OFF) \
253 do { *prog++ = BR_OPC | WDISP22(OFF); \
256 #define emit_jump(DEST) emit_branch(BA, DEST)
258 #define emit_read_y(REG) *prog++ = RD_Y | RD(REG)
259 #define emit_write_y(REG) *prog++ = WR_Y | IMMED | RS1(REG) | S13(0)
261 #define emit_cmp(R1, R2) \
262 *prog++ = (SUBCC | RS1(R1) | RS2(R2) | RD(G0))
264 #define emit_cmpi(R1, IMM) \
265 *prog++ = (SUBCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
267 #define emit_btst(R1, R2) \
268 *prog++ = (ANDCC | RS1(R1) | RS2(R2) | RD(G0))
270 #define emit_btsti(R1, IMM) \
271 *prog++ = (ANDCC | IMMED | RS1(R1) | S13(IMM) | RD(G0));
273 #define emit_sub(R1, R2, R3) \
274 *prog++ = (SUB | RS1(R1) | RS2(R2) | RD(R3))
276 #define emit_subi(R1, IMM, R3) \
277 *prog++ = (SUB | IMMED | RS1(R1) | S13(IMM) | RD(R3))
279 #define emit_add(R1, R2, R3) \
280 *prog++ = (ADD | RS1(R1) | RS2(R2) | RD(R3))
282 #define emit_addi(R1, IMM, R3) \
283 *prog++ = (ADD | IMMED | RS1(R1) | S13(IMM) | RD(R3))
285 #define emit_and(R1, R2, R3) \
286 *prog++ = (AND | RS1(R1) | RS2(R2) | RD(R3))
288 #define emit_andi(R1, IMM, R3) \
289 *prog++ = (AND | IMMED | RS1(R1) | S13(IMM) | RD(R3))
291 #define emit_alloc_stack(SZ) \
292 *prog++ = (SUB | IMMED | RS1(SP) | S13(SZ) | RD(SP))
294 #define emit_release_stack(SZ) \
295 *prog++ = (ADD | IMMED | RS1(SP) | S13(SZ) | RD(SP))
297 /* A note about branch offset calculations. The addrs[] array,
298 * indexed by BPF instruction, records the address after all the
299 * sparc instructions emitted for that BPF instruction.
301 * The most common case is to emit a branch at the end of such
302 * a code sequence. So this would be two instructions, the
303 * branch and its delay slot.
305 * Therefore by default the branch emitters calculate the branch
308 * destination - (addrs[i] - 8)
310 * This "addrs[i] - 8" is the address of the branch itself or
311 * what "." would be in assembler notation. The "8" part is
312 * how we take into consideration the branch and its delay
313 * slot mentioned above.
315 * Sometimes we need to emit a branch earlier in the code
316 * sequence. And in these situations we adjust "destination"
317 * to accommodate this difference. For example, if we needed
318 * to emit a branch (and its delay slot) right before the
319 * final instruction emitted for a BPF opcode, we'd use
320 * "destination + 4" instead of just plain "destination" above.
322 * This is why you see all of these funny emit_branch() and
323 * emit_jump() calls with adjusted offsets.
326 void bpf_jit_compile(struct bpf_prog *fp)
328 unsigned int cleanup_addr, proglen, oldproglen = 0;
329 u32 temp[8], *prog, *func, seen = 0, pass;
330 const struct sock_filter *filter = fp->insns;
331 int i, flen = fp->len, pc_ret0 = -1;
338 addrs = kmalloc_array(flen, sizeof(*addrs), GFP_KERNEL);
342 /* Before first pass, make a rough estimation of addrs[]
343 * each bpf instruction is translated to less than 64 bytes
345 for (proglen = 0, i = 0; i < flen; i++) {
349 cleanup_addr = proglen; /* epilogue address */
351 for (pass = 0; pass < 10; pass++) {
352 u8 seen_or_pass0 = (pass == 0) ? (SEEN_XREG | SEEN_DATAREF | SEEN_MEM) : seen;
354 /* no prologue/epilogue for trivial filters (RET something) */
360 if (seen_or_pass0 & SEEN_MEM) {
361 unsigned int sz = BASE_STACKFRAME;
362 sz += BPF_MEMWORDS * sizeof(u32);
363 emit_alloc_stack(sz);
366 /* Make sure we dont leek kernel memory. */
367 if (seen_or_pass0 & SEEN_XREG)
370 /* If this filter needs to access skb data,
371 * load %o4 and %o5 with:
372 * %o4 = skb->len - skb->data_len
374 * And also back up %o7 into r_saved_O7 so we can
375 * invoke the stubs using 'call'.
377 if (seen_or_pass0 & SEEN_DATAREF) {
378 emit_load32(r_SKB, struct sk_buff, len, r_HEADLEN);
379 emit_load32(r_SKB, struct sk_buff, data_len, r_TMP);
380 emit_sub(r_HEADLEN, r_TMP, r_HEADLEN);
381 emit_loadptr(r_SKB, struct sk_buff, data, r_SKB_DATA);
384 emit_reg_move(O7, r_saved_O7);
386 /* Make sure we dont leak kernel information to the user. */
387 if (bpf_needs_clear_a(&filter[0]))
388 emit_clear(r_A); /* A = 0 */
390 for (i = 0; i < flen; i++) {
391 unsigned int K = filter[i].k;
392 unsigned int t_offset;
393 unsigned int f_offset;
395 u16 code = bpf_anc_helper(&filter[i]);
399 case BPF_ALU | BPF_ADD | BPF_X: /* A += X; */
402 case BPF_ALU | BPF_ADD | BPF_K: /* A += K; */
405 case BPF_ALU | BPF_SUB | BPF_X: /* A -= X; */
408 case BPF_ALU | BPF_SUB | BPF_K: /* A -= K */
411 case BPF_ALU | BPF_AND | BPF_X: /* A &= X */
414 case BPF_ALU | BPF_AND | BPF_K: /* A &= K */
417 case BPF_ALU | BPF_OR | BPF_X: /* A |= X */
420 case BPF_ALU | BPF_OR | BPF_K: /* A |= K */
423 case BPF_ANC | SKF_AD_ALU_XOR_X: /* A ^= X; */
424 case BPF_ALU | BPF_XOR | BPF_X:
427 case BPF_ALU | BPF_XOR | BPF_K: /* A ^= K */
430 case BPF_ALU | BPF_LSH | BPF_X: /* A <<= X */
433 case BPF_ALU | BPF_LSH | BPF_K: /* A <<= K */
436 case BPF_ALU | BPF_RSH | BPF_X: /* A >>= X */
439 case BPF_ALU | BPF_RSH | BPF_K: /* A >>= K */
442 case BPF_ALU | BPF_MUL | BPF_X: /* A *= X; */
445 case BPF_ALU | BPF_MUL | BPF_K: /* A *= K */
448 case BPF_ALU | BPF_DIV | BPF_K: /* A /= K with K != 0*/
452 /* The Sparc v8 architecture requires
453 * three instructions between a %y
454 * register write and the first use.
461 case BPF_ALU | BPF_DIV | BPF_X: /* A /= X; */
464 t_offset = addrs[pc_ret0 - 1];
465 emit_branch(BE, t_offset + 20);
466 emit_nop(); /* delay slot */
468 emit_branch_off(BNE, 16);
470 emit_jump(cleanup_addr + 20);
474 /* The Sparc v8 architecture requires
475 * three instructions between a %y
476 * register write and the first use.
483 case BPF_ALU | BPF_NEG:
486 case BPF_RET | BPF_K:
492 emit_loadimm(K, r_A);
495 case BPF_RET | BPF_A:
498 emit_jump(cleanup_addr);
502 if (seen_or_pass0 & SEEN_MEM) {
503 unsigned int sz = BASE_STACKFRAME;
504 sz += BPF_MEMWORDS * sizeof(u32);
505 emit_release_stack(sz);
508 /* jmpl %r_saved_O7 + 8, %g0 */
509 emit_jmpl(r_saved_O7, 8, G0);
510 emit_reg_move(r_A, O0); /* delay slot */
512 case BPF_MISC | BPF_TAX:
514 emit_reg_move(r_A, r_X);
516 case BPF_MISC | BPF_TXA:
518 emit_reg_move(r_X, r_A);
520 case BPF_ANC | SKF_AD_CPU:
523 case BPF_ANC | SKF_AD_PROTOCOL:
524 emit_skb_load16(protocol, r_A);
526 case BPF_ANC | SKF_AD_PKTTYPE:
527 __emit_skb_load8(__pkt_type_offset, r_A);
528 emit_andi(r_A, PKT_TYPE_MAX, r_A);
531 case BPF_ANC | SKF_AD_IFINDEX:
532 emit_skb_loadptr(dev, r_A);
534 emit_branch(BE_PTR, cleanup_addr + 4);
536 emit_load32(r_A, struct net_device, ifindex, r_A);
538 case BPF_ANC | SKF_AD_MARK:
539 emit_skb_load32(mark, r_A);
541 case BPF_ANC | SKF_AD_QUEUE:
542 emit_skb_load16(queue_mapping, r_A);
544 case BPF_ANC | SKF_AD_HATYPE:
545 emit_skb_loadptr(dev, r_A);
547 emit_branch(BE_PTR, cleanup_addr + 4);
549 emit_load16(r_A, struct net_device, type, r_A);
551 case BPF_ANC | SKF_AD_RXHASH:
552 emit_skb_load32(hash, r_A);
554 case BPF_ANC | SKF_AD_VLAN_TAG:
555 emit_skb_load16(vlan_tci, r_A);
557 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
558 emit_skb_load32(vlan_all, r_A);
560 emit_branch_off(BE, 12);
562 emit_loadimm(1, r_A);
564 case BPF_LD | BPF_W | BPF_LEN:
565 emit_skb_load32(len, r_A);
567 case BPF_LDX | BPF_W | BPF_LEN:
568 emit_skb_load32(len, r_X);
570 case BPF_LD | BPF_IMM:
571 emit_loadimm(K, r_A);
573 case BPF_LDX | BPF_IMM:
574 emit_loadimm(K, r_X);
576 case BPF_LD | BPF_MEM:
578 emit_ldmem(K * 4, r_A);
580 case BPF_LDX | BPF_MEM:
581 seen |= SEEN_MEM | SEEN_XREG;
582 emit_ldmem(K * 4, r_X);
586 emit_stmem(K * 4, r_A);
589 seen |= SEEN_MEM | SEEN_XREG;
590 emit_stmem(K * 4, r_X);
593 #define CHOOSE_LOAD_FUNC(K, func) \
594 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
596 case BPF_LD | BPF_W | BPF_ABS:
597 func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_word);
598 common_load: seen |= SEEN_DATAREF;
599 emit_loadimm(K, r_OFF);
602 case BPF_LD | BPF_H | BPF_ABS:
603 func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_half);
605 case BPF_LD | BPF_B | BPF_ABS:
606 func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte);
608 case BPF_LDX | BPF_B | BPF_MSH:
609 func = CHOOSE_LOAD_FUNC(K, bpf_jit_load_byte_msh);
611 case BPF_LD | BPF_W | BPF_IND:
612 func = bpf_jit_load_word;
613 common_load_ind: seen |= SEEN_DATAREF | SEEN_XREG;
616 emit_addi(r_X, K, r_OFF);
618 emit_loadimm(K, r_TMP);
619 emit_add(r_X, r_TMP, r_OFF);
622 emit_reg_move(r_X, r_OFF);
626 case BPF_LD | BPF_H | BPF_IND:
627 func = bpf_jit_load_half;
628 goto common_load_ind;
629 case BPF_LD | BPF_B | BPF_IND:
630 func = bpf_jit_load_byte;
631 goto common_load_ind;
632 case BPF_JMP | BPF_JA:
633 emit_jump(addrs[i + K]);
637 #define COND_SEL(CODE, TOP, FOP) \
643 COND_SEL(BPF_JMP | BPF_JGT | BPF_K, BGU, BLEU);
644 COND_SEL(BPF_JMP | BPF_JGE | BPF_K, BGEU, BLU);
645 COND_SEL(BPF_JMP | BPF_JEQ | BPF_K, BE, BNE);
646 COND_SEL(BPF_JMP | BPF_JSET | BPF_K, BNE, BE);
647 COND_SEL(BPF_JMP | BPF_JGT | BPF_X, BGU, BLEU);
648 COND_SEL(BPF_JMP | BPF_JGE | BPF_X, BGEU, BLU);
649 COND_SEL(BPF_JMP | BPF_JEQ | BPF_X, BE, BNE);
650 COND_SEL(BPF_JMP | BPF_JSET | BPF_X, BNE, BE);
652 cond_branch: f_offset = addrs[i + filter[i].jf];
653 t_offset = addrs[i + filter[i].jt];
655 /* same targets, can avoid doing the test :) */
656 if (filter[i].jt == filter[i].jf) {
663 case BPF_JMP | BPF_JGT | BPF_X:
664 case BPF_JMP | BPF_JGE | BPF_X:
665 case BPF_JMP | BPF_JEQ | BPF_X:
669 case BPF_JMP | BPF_JSET | BPF_X:
673 case BPF_JMP | BPF_JEQ | BPF_K:
674 case BPF_JMP | BPF_JGT | BPF_K:
675 case BPF_JMP | BPF_JGE | BPF_K:
679 emit_loadimm(K, r_TMP);
680 emit_cmp(r_A, r_TMP);
683 case BPF_JMP | BPF_JSET | BPF_K:
687 emit_loadimm(K, r_TMP);
688 emit_btst(r_A, r_TMP);
692 if (filter[i].jt != 0) {
695 emit_branch(t_op, t_offset);
696 emit_nop(); /* delay slot */
703 emit_branch(f_op, f_offset);
704 emit_nop(); /* delay slot */
708 /* hmm, too complex filter, give up with jit compiler */
711 ilen = (void *) prog - (void *) temp;
713 if (unlikely(proglen + ilen > oldproglen)) {
714 pr_err("bpb_jit_compile fatal error\n");
716 module_memfree(image);
719 memcpy(image + proglen, temp, ilen);
725 /* last bpf instruction is always a RET :
726 * use it to give the cleanup instruction(s) addr
728 cleanup_addr = proglen - 8; /* jmpl; mov r_A,%o0; */
729 if (seen_or_pass0 & SEEN_MEM)
730 cleanup_addr -= 4; /* add %sp, X, %sp; */
733 if (proglen != oldproglen)
734 pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n",
735 proglen, oldproglen);
738 if (proglen == oldproglen) {
739 image = module_alloc(proglen);
743 oldproglen = proglen;
746 if (bpf_jit_enable > 1)
747 bpf_jit_dump(flen, proglen, pass + 1, image);
750 fp->bpf_func = (void *)image;
758 void bpf_jit_free(struct bpf_prog *fp)
761 module_memfree(fp->bpf_func);
763 bpf_prog_unlock_free(fp);