1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define KMSG_COMPONENT "bpf_jit"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22#include <linux/netdevice.h>
23#include <linux/filter.h>
24#include <linux/init.h>
25#include <linux/bpf.h>
26#include <asm/cacheflush.h>
27#include <asm/dis.h>
28#include <asm/set_memory.h>
29#include "bpf_jit.h"
30
31int bpf_jit_enable __read_mostly;
32
33struct bpf_jit {
34 u32 seen;
35 u32 seen_reg[16];
36 u32 *addrs;
37 u8 *prg_buf;
38 int size;
39 int size_prg;
40 int prg;
41 int lit_start;
42 int lit;
43 int base_ip;
44 int ret0_ip;
45 int exit_ip;
46 int tail_call_start;
47 int labels[1];
48};
49
50#define BPF_SIZE_MAX 0xffff
51
52#define SEEN_SKB 1
53#define SEEN_MEM 2
54#define SEEN_RET0 4
55#define SEEN_LITERAL 8
56#define SEEN_FUNC 16
57#define SEEN_TAIL_CALL 32
58#define SEEN_REG_AX 64
59#define SEEN_STACK (SEEN_FUNC | SEEN_MEM | SEEN_SKB)
60
61
62
63
64#define REG_W0 (MAX_BPF_JIT_REG + 0)
65#define REG_W1 (MAX_BPF_JIT_REG + 1)
66#define REG_SKB_DATA (MAX_BPF_JIT_REG + 2)
67#define REG_L (MAX_BPF_JIT_REG + 3)
68#define REG_15 (MAX_BPF_JIT_REG + 4)
69#define REG_0 REG_W0
70#define REG_1 REG_W1
71#define REG_2 BPF_REG_1
72#define REG_14 BPF_REG_0
73
74
75
76
77static const int reg2hex[] = {
78
79 [BPF_REG_0] = 14,
80
81 [BPF_REG_1] = 2,
82 [BPF_REG_2] = 3,
83 [BPF_REG_3] = 4,
84 [BPF_REG_4] = 5,
85 [BPF_REG_5] = 6,
86
87 [BPF_REG_6] = 7,
88 [BPF_REG_7] = 8,
89 [BPF_REG_8] = 9,
90 [BPF_REG_9] = 10,
91
92 [BPF_REG_FP] = 13,
93
94 [BPF_REG_AX] = 12,
95
96 [REG_SKB_DATA] = 12,
97
98 [REG_W0] = 0,
99 [REG_W1] = 1,
100 [REG_L] = 11,
101 [REG_15] = 15,
102};
103
104static inline u32 reg(u32 dst_reg, u32 src_reg)
105{
106 return reg2hex[dst_reg] << 4 | reg2hex[src_reg];
107}
108
109static inline u32 reg_high(u32 reg)
110{
111 return reg2hex[reg] << 4;
112}
113
114static inline void reg_set_seen(struct bpf_jit *jit, u32 b1)
115{
116 u32 r1 = reg2hex[b1];
117
118 if (!jit->seen_reg[r1] && r1 >= 6 && r1 <= 15)
119 jit->seen_reg[r1] = 1;
120}
121
122#define REG_SET_SEEN(b1) \
123({ \
124 reg_set_seen(jit, b1); \
125})
126
127#define REG_SEEN(b1) jit->seen_reg[reg2hex[(b1)]]
128
129
130
131
132
133#define _EMIT2(op) \
134({ \
135 if (jit->prg_buf) \
136 *(u16 *) (jit->prg_buf + jit->prg) = op; \
137 jit->prg += 2; \
138})
139
140#define EMIT2(op, b1, b2) \
141({ \
142 _EMIT2(op | reg(b1, b2)); \
143 REG_SET_SEEN(b1); \
144 REG_SET_SEEN(b2); \
145})
146
147#define _EMIT4(op) \
148({ \
149 if (jit->prg_buf) \
150 *(u32 *) (jit->prg_buf + jit->prg) = op; \
151 jit->prg += 4; \
152})
153
154#define EMIT4(op, b1, b2) \
155({ \
156 _EMIT4(op | reg(b1, b2)); \
157 REG_SET_SEEN(b1); \
158 REG_SET_SEEN(b2); \
159})
160
161#define EMIT4_RRF(op, b1, b2, b3) \
162({ \
163 _EMIT4(op | reg_high(b3) << 8 | reg(b1, b2)); \
164 REG_SET_SEEN(b1); \
165 REG_SET_SEEN(b2); \
166 REG_SET_SEEN(b3); \
167})
168
169#define _EMIT4_DISP(op, disp) \
170({ \
171 unsigned int __disp = (disp) & 0xfff; \
172 _EMIT4(op | __disp); \
173})
174
175#define EMIT4_DISP(op, b1, b2, disp) \
176({ \
177 _EMIT4_DISP(op | reg_high(b1) << 16 | \
178 reg_high(b2) << 8, disp); \
179 REG_SET_SEEN(b1); \
180 REG_SET_SEEN(b2); \
181})
182
183#define EMIT4_IMM(op, b1, imm) \
184({ \
185 unsigned int __imm = (imm) & 0xffff; \
186 _EMIT4(op | reg_high(b1) << 16 | __imm); \
187 REG_SET_SEEN(b1); \
188})
189
190#define EMIT4_PCREL(op, pcrel) \
191({ \
192 long __pcrel = ((pcrel) >> 1) & 0xffff; \
193 _EMIT4(op | __pcrel); \
194})
195
196#define _EMIT6(op1, op2) \
197({ \
198 if (jit->prg_buf) { \
199 *(u32 *) (jit->prg_buf + jit->prg) = op1; \
200 *(u16 *) (jit->prg_buf + jit->prg + 4) = op2; \
201 } \
202 jit->prg += 6; \
203})
204
205#define _EMIT6_DISP(op1, op2, disp) \
206({ \
207 unsigned int __disp = (disp) & 0xfff; \
208 _EMIT6(op1 | __disp, op2); \
209})
210
211#define _EMIT6_DISP_LH(op1, op2, disp) \
212({ \
213 u32 _disp = (u32) disp; \
214 unsigned int __disp_h = _disp & 0xff000; \
215 unsigned int __disp_l = _disp & 0x00fff; \
216 _EMIT6(op1 | __disp_l, op2 | __disp_h >> 4); \
217})
218
219#define EMIT6_DISP_LH(op1, op2, b1, b2, b3, disp) \
220({ \
221 _EMIT6_DISP_LH(op1 | reg(b1, b2) << 16 | \
222 reg_high(b3) << 8, op2, disp); \
223 REG_SET_SEEN(b1); \
224 REG_SET_SEEN(b2); \
225 REG_SET_SEEN(b3); \
226})
227
228#define EMIT6_PCREL_LABEL(op1, op2, b1, b2, label, mask) \
229({ \
230 int rel = (jit->labels[label] - jit->prg) >> 1; \
231 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), \
232 op2 | mask << 12); \
233 REG_SET_SEEN(b1); \
234 REG_SET_SEEN(b2); \
235})
236
237#define EMIT6_PCREL_IMM_LABEL(op1, op2, b1, imm, label, mask) \
238({ \
239 int rel = (jit->labels[label] - jit->prg) >> 1; \
240 _EMIT6(op1 | (reg_high(b1) | mask) << 16 | \
241 (rel & 0xffff), op2 | (imm & 0xff) << 8); \
242 REG_SET_SEEN(b1); \
243 BUILD_BUG_ON(((unsigned long) imm) > 0xff); \
244})
245
246#define EMIT6_PCREL(op1, op2, b1, b2, i, off, mask) \
247({ \
248 \
249 int rel = (addrs[i + off + 1] - (addrs[i + 1] - 6)) / 2;\
250 _EMIT6(op1 | reg(b1, b2) << 16 | (rel & 0xffff), op2 | mask); \
251 REG_SET_SEEN(b1); \
252 REG_SET_SEEN(b2); \
253})
254
255#define _EMIT6_IMM(op, imm) \
256({ \
257 unsigned int __imm = (imm); \
258 _EMIT6(op | (__imm >> 16), __imm & 0xffff); \
259})
260
261#define EMIT6_IMM(op, b1, imm) \
262({ \
263 _EMIT6_IMM(op | reg_high(b1) << 16, imm); \
264 REG_SET_SEEN(b1); \
265})
266
267#define EMIT_CONST_U32(val) \
268({ \
269 unsigned int ret; \
270 ret = jit->lit - jit->base_ip; \
271 jit->seen |= SEEN_LITERAL; \
272 if (jit->prg_buf) \
273 *(u32 *) (jit->prg_buf + jit->lit) = (u32) val; \
274 jit->lit += 4; \
275 ret; \
276})
277
278#define EMIT_CONST_U64(val) \
279({ \
280 unsigned int ret; \
281 ret = jit->lit - jit->base_ip; \
282 jit->seen |= SEEN_LITERAL; \
283 if (jit->prg_buf) \
284 *(u64 *) (jit->prg_buf + jit->lit) = (u64) val; \
285 jit->lit += 8; \
286 ret; \
287})
288
289#define EMIT_ZERO(b1) \
290({ \
291 \
292 EMIT4(0xb9160000, b1, b1); \
293 REG_SET_SEEN(b1); \
294})
295
296
297
298
299static void jit_fill_hole(void *area, unsigned int size)
300{
301 memset(area, 0, size);
302}
303
304
305
306
307static void save_regs(struct bpf_jit *jit, u32 rs, u32 re)
308{
309 u32 off = STK_OFF_R6 + (rs - 6) * 8;
310
311 if (rs == re)
312
313 _EMIT6(0xe300f000 | rs << 20 | off, 0x0024);
314 else
315
316 _EMIT6_DISP(0xeb00f000 | rs << 20 | re << 16, 0x0024, off);
317}
318
319
320
321
322static void restore_regs(struct bpf_jit *jit, u32 rs, u32 re, u32 stack_depth)
323{
324 u32 off = STK_OFF_R6 + (rs - 6) * 8;
325
326 if (jit->seen & SEEN_STACK)
327 off += STK_OFF + stack_depth;
328
329 if (rs == re)
330
331 _EMIT6(0xe300f000 | rs << 20 | off, 0x0004);
332 else
333
334 _EMIT6_DISP(0xeb00f000 | rs << 20 | re << 16, 0x0004, off);
335}
336
337
338
339
340static int get_start(struct bpf_jit *jit, int start)
341{
342 int i;
343
344 for (i = start; i <= 15; i++) {
345 if (jit->seen_reg[i])
346 return i;
347 }
348 return 0;
349}
350
351
352
353
354static int get_end(struct bpf_jit *jit, int start)
355{
356 int i;
357
358 for (i = start; i < 15; i++) {
359 if (!jit->seen_reg[i] && !jit->seen_reg[i + 1])
360 return i - 1;
361 }
362 return jit->seen_reg[15] ? 15 : 14;
363}
364
365#define REGS_SAVE 1
366#define REGS_RESTORE 0
367
368
369
370
371static void save_restore_regs(struct bpf_jit *jit, int op, u32 stack_depth)
372{
373
374 int re = 6, rs;
375
376 do {
377 rs = get_start(jit, re);
378 if (!rs)
379 break;
380 re = get_end(jit, rs + 1);
381 if (op == REGS_SAVE)
382 save_regs(jit, rs, re);
383 else
384 restore_regs(jit, rs, re, stack_depth);
385 re++;
386 } while (re <= 15);
387}
388
389
390
391
392
393
394static void emit_load_skb_data_hlen(struct bpf_jit *jit)
395{
396
397 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1, REG_0, BPF_REG_1,
398 offsetof(struct sk_buff, len));
399
400 EMIT4_DISP(0x5b000000, REG_W1, BPF_REG_1,
401 offsetof(struct sk_buff, data_len));
402
403 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0, REG_15, STK_OFF_HLEN);
404 if (!(jit->seen & SEEN_REG_AX))
405
406 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA, REG_0,
407 BPF_REG_1, offsetof(struct sk_buff, data));
408}
409
410
411
412
413
414
415
416static void bpf_jit_prologue(struct bpf_jit *jit, u32 stack_depth)
417{
418 if (jit->seen & SEEN_TAIL_CALL) {
419
420 _EMIT6(0xd703f000 | STK_OFF_TCCNT, 0xf000 | STK_OFF_TCCNT);
421 } else {
422
423 EMIT4_PCREL(0xa7f40000, 6);
424 _EMIT2(0);
425 }
426
427 jit->tail_call_start = jit->prg;
428
429 save_restore_regs(jit, REGS_SAVE, stack_depth);
430
431 if (jit->seen & SEEN_LITERAL) {
432
433 EMIT2(0x0d00, REG_L, REG_0);
434 jit->base_ip = jit->prg;
435 }
436
437 if (jit->seen & SEEN_STACK) {
438 if (jit->seen & SEEN_FUNC)
439
440 EMIT4(0xb9040000, REG_W1, REG_15);
441
442 EMIT4_DISP(0x41000000, BPF_REG_FP, REG_15, STK_160_UNUSED);
443
444 EMIT4_IMM(0xa70b0000, REG_15, -(STK_OFF + stack_depth));
445 if (jit->seen & SEEN_FUNC)
446
447 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W1, REG_0,
448 REG_15, 152);
449 }
450 if (jit->seen & SEEN_SKB) {
451 emit_load_skb_data_hlen(jit);
452
453 EMIT6_DISP_LH(0xe3000000, 0x0024, BPF_REG_1, REG_0, REG_15,
454 STK_OFF_SKBP);
455 }
456}
457
458
459
460
461static void bpf_jit_epilogue(struct bpf_jit *jit, u32 stack_depth)
462{
463
464 if (jit->seen & SEEN_RET0) {
465 jit->ret0_ip = jit->prg;
466
467 EMIT4_IMM(0xa7090000, BPF_REG_0, 0);
468 }
469 jit->exit_ip = jit->prg;
470
471 EMIT4(0xb9040000, REG_2, BPF_REG_0);
472
473 save_restore_regs(jit, REGS_RESTORE, stack_depth);
474
475 _EMIT2(0x07fe);
476}
477
478
479
480
481
482
483
484static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i)
485{
486 struct bpf_insn *insn = &fp->insnsi[i];
487 int jmp_off, last, insn_count = 1;
488 unsigned int func_addr, mask;
489 u32 dst_reg = insn->dst_reg;
490 u32 src_reg = insn->src_reg;
491 u32 *addrs = jit->addrs;
492 s32 imm = insn->imm;
493 s16 off = insn->off;
494
495 if (dst_reg == BPF_REG_AX || src_reg == BPF_REG_AX)
496 jit->seen |= SEEN_REG_AX;
497 switch (insn->code) {
498
499
500
501 case BPF_ALU | BPF_MOV | BPF_X:
502
503 EMIT4(0xb9160000, dst_reg, src_reg);
504 break;
505 case BPF_ALU64 | BPF_MOV | BPF_X:
506
507 EMIT4(0xb9040000, dst_reg, src_reg);
508 break;
509 case BPF_ALU | BPF_MOV | BPF_K:
510
511 EMIT6_IMM(0xc00f0000, dst_reg, imm);
512 break;
513 case BPF_ALU64 | BPF_MOV | BPF_K:
514
515 EMIT6_IMM(0xc0010000, dst_reg, imm);
516 break;
517
518
519
520 case BPF_LD | BPF_IMM | BPF_DW:
521 {
522
523 u64 imm64;
524
525 imm64 = (u64)(u32) insn[0].imm | ((u64)(u32) insn[1].imm) << 32;
526
527 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg, REG_0, REG_L,
528 EMIT_CONST_U64(imm64));
529 insn_count = 2;
530 break;
531 }
532
533
534
535 case BPF_ALU | BPF_ADD | BPF_X:
536
537 EMIT2(0x1a00, dst_reg, src_reg);
538 EMIT_ZERO(dst_reg);
539 break;
540 case BPF_ALU64 | BPF_ADD | BPF_X:
541
542 EMIT4(0xb9080000, dst_reg, src_reg);
543 break;
544 case BPF_ALU | BPF_ADD | BPF_K:
545 if (!imm)
546 break;
547
548 EMIT6_IMM(0xc20b0000, dst_reg, imm);
549 EMIT_ZERO(dst_reg);
550 break;
551 case BPF_ALU64 | BPF_ADD | BPF_K:
552 if (!imm)
553 break;
554
555 EMIT6_IMM(0xc2080000, dst_reg, imm);
556 break;
557
558
559
560 case BPF_ALU | BPF_SUB | BPF_X:
561
562 EMIT2(0x1b00, dst_reg, src_reg);
563 EMIT_ZERO(dst_reg);
564 break;
565 case BPF_ALU64 | BPF_SUB | BPF_X:
566
567 EMIT4(0xb9090000, dst_reg, src_reg);
568 break;
569 case BPF_ALU | BPF_SUB | BPF_K:
570 if (!imm)
571 break;
572
573 EMIT6_IMM(0xc20b0000, dst_reg, -imm);
574 EMIT_ZERO(dst_reg);
575 break;
576 case BPF_ALU64 | BPF_SUB | BPF_K:
577 if (!imm)
578 break;
579
580 EMIT6_IMM(0xc2080000, dst_reg, -imm);
581 break;
582
583
584
585 case BPF_ALU | BPF_MUL | BPF_X:
586
587 EMIT4(0xb2520000, dst_reg, src_reg);
588 EMIT_ZERO(dst_reg);
589 break;
590 case BPF_ALU64 | BPF_MUL | BPF_X:
591
592 EMIT4(0xb90c0000, dst_reg, src_reg);
593 break;
594 case BPF_ALU | BPF_MUL | BPF_K:
595 if (imm == 1)
596 break;
597
598 EMIT6_IMM(0xc2010000, dst_reg, imm);
599 EMIT_ZERO(dst_reg);
600 break;
601 case BPF_ALU64 | BPF_MUL | BPF_K:
602 if (imm == 1)
603 break;
604
605 EMIT6_IMM(0xc2000000, dst_reg, imm);
606 break;
607
608
609
610 case BPF_ALU | BPF_DIV | BPF_X:
611 case BPF_ALU | BPF_MOD | BPF_X:
612 {
613 int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
614
615 jit->seen |= SEEN_RET0;
616
617 EMIT2(0x1200, src_reg, src_reg);
618
619 EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
620
621 EMIT4_IMM(0xa7080000, REG_W0, 0);
622
623 EMIT2(0x1800, REG_W1, dst_reg);
624
625 EMIT4(0xb9970000, REG_W0, src_reg);
626
627 EMIT4(0xb9160000, dst_reg, rc_reg);
628 break;
629 }
630 case BPF_ALU64 | BPF_DIV | BPF_X:
631 case BPF_ALU64 | BPF_MOD | BPF_X:
632 {
633 int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
634
635 jit->seen |= SEEN_RET0;
636
637 EMIT4(0xb9020000, src_reg, src_reg);
638
639 EMIT4_PCREL(0xa7840000, jit->ret0_ip - jit->prg);
640
641 EMIT4_IMM(0xa7090000, REG_W0, 0);
642
643 EMIT4(0xb9040000, REG_W1, dst_reg);
644
645 EMIT4(0xb9870000, REG_W0, src_reg);
646
647 EMIT4(0xb9040000, dst_reg, rc_reg);
648 break;
649 }
650 case BPF_ALU | BPF_DIV | BPF_K:
651 case BPF_ALU | BPF_MOD | BPF_K:
652 {
653 int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
654
655 if (imm == 1) {
656 if (BPF_OP(insn->code) == BPF_MOD)
657
658 EMIT4_IMM(0xa7090000, dst_reg, 0);
659 break;
660 }
661
662 EMIT4_IMM(0xa7080000, REG_W0, 0);
663
664 EMIT2(0x1800, REG_W1, dst_reg);
665
666 EMIT6_DISP_LH(0xe3000000, 0x0097, REG_W0, REG_0, REG_L,
667 EMIT_CONST_U32(imm));
668
669 EMIT4(0xb9160000, dst_reg, rc_reg);
670 break;
671 }
672 case BPF_ALU64 | BPF_DIV | BPF_K:
673 case BPF_ALU64 | BPF_MOD | BPF_K:
674 {
675 int rc_reg = BPF_OP(insn->code) == BPF_DIV ? REG_W1 : REG_W0;
676
677 if (imm == 1) {
678 if (BPF_OP(insn->code) == BPF_MOD)
679
680 EMIT4_IMM(0xa7090000, dst_reg, 0);
681 break;
682 }
683
684 EMIT4_IMM(0xa7090000, REG_W0, 0);
685
686 EMIT4(0xb9040000, REG_W1, dst_reg);
687
688 EMIT6_DISP_LH(0xe3000000, 0x0087, REG_W0, REG_0, REG_L,
689 EMIT_CONST_U64(imm));
690
691 EMIT4(0xb9040000, dst_reg, rc_reg);
692 break;
693 }
694
695
696
697 case BPF_ALU | BPF_AND | BPF_X:
698
699 EMIT2(0x1400, dst_reg, src_reg);
700 EMIT_ZERO(dst_reg);
701 break;
702 case BPF_ALU64 | BPF_AND | BPF_X:
703
704 EMIT4(0xb9800000, dst_reg, src_reg);
705 break;
706 case BPF_ALU | BPF_AND | BPF_K:
707
708 EMIT6_IMM(0xc00b0000, dst_reg, imm);
709 EMIT_ZERO(dst_reg);
710 break;
711 case BPF_ALU64 | BPF_AND | BPF_K:
712
713 EMIT6_DISP_LH(0xe3000000, 0x0080, dst_reg, REG_0, REG_L,
714 EMIT_CONST_U64(imm));
715 break;
716
717
718
719 case BPF_ALU | BPF_OR | BPF_X:
720
721 EMIT2(0x1600, dst_reg, src_reg);
722 EMIT_ZERO(dst_reg);
723 break;
724 case BPF_ALU64 | BPF_OR | BPF_X:
725
726 EMIT4(0xb9810000, dst_reg, src_reg);
727 break;
728 case BPF_ALU | BPF_OR | BPF_K:
729
730 EMIT6_IMM(0xc00d0000, dst_reg, imm);
731 EMIT_ZERO(dst_reg);
732 break;
733 case BPF_ALU64 | BPF_OR | BPF_K:
734
735 EMIT6_DISP_LH(0xe3000000, 0x0081, dst_reg, REG_0, REG_L,
736 EMIT_CONST_U64(imm));
737 break;
738
739
740
741 case BPF_ALU | BPF_XOR | BPF_X:
742
743 EMIT2(0x1700, dst_reg, src_reg);
744 EMIT_ZERO(dst_reg);
745 break;
746 case BPF_ALU64 | BPF_XOR | BPF_X:
747
748 EMIT4(0xb9820000, dst_reg, src_reg);
749 break;
750 case BPF_ALU | BPF_XOR | BPF_K:
751 if (!imm)
752 break;
753
754 EMIT6_IMM(0xc0070000, dst_reg, imm);
755 EMIT_ZERO(dst_reg);
756 break;
757 case BPF_ALU64 | BPF_XOR | BPF_K:
758
759 EMIT6_DISP_LH(0xe3000000, 0x0082, dst_reg, REG_0, REG_L,
760 EMIT_CONST_U64(imm));
761 break;
762
763
764
765 case BPF_ALU | BPF_LSH | BPF_X:
766
767 EMIT4_DISP(0x89000000, dst_reg, src_reg, 0);
768 EMIT_ZERO(dst_reg);
769 break;
770 case BPF_ALU64 | BPF_LSH | BPF_X:
771
772 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg, dst_reg, src_reg, 0);
773 break;
774 case BPF_ALU | BPF_LSH | BPF_K:
775 if (imm == 0)
776 break;
777
778 EMIT4_DISP(0x89000000, dst_reg, REG_0, imm);
779 EMIT_ZERO(dst_reg);
780 break;
781 case BPF_ALU64 | BPF_LSH | BPF_K:
782 if (imm == 0)
783 break;
784
785 EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg, dst_reg, REG_0, imm);
786 break;
787
788
789
790 case BPF_ALU | BPF_RSH | BPF_X:
791
792 EMIT4_DISP(0x88000000, dst_reg, src_reg, 0);
793 EMIT_ZERO(dst_reg);
794 break;
795 case BPF_ALU64 | BPF_RSH | BPF_X:
796
797 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg, dst_reg, src_reg, 0);
798 break;
799 case BPF_ALU | BPF_RSH | BPF_K:
800 if (imm == 0)
801 break;
802
803 EMIT4_DISP(0x88000000, dst_reg, REG_0, imm);
804 EMIT_ZERO(dst_reg);
805 break;
806 case BPF_ALU64 | BPF_RSH | BPF_K:
807 if (imm == 0)
808 break;
809
810 EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg, dst_reg, REG_0, imm);
811 break;
812
813
814
815 case BPF_ALU64 | BPF_ARSH | BPF_X:
816
817 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg, dst_reg, src_reg, 0);
818 break;
819 case BPF_ALU64 | BPF_ARSH | BPF_K:
820 if (imm == 0)
821 break;
822
823 EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg, dst_reg, REG_0, imm);
824 break;
825
826
827
828 case BPF_ALU | BPF_NEG:
829
830 EMIT2(0x1300, dst_reg, dst_reg);
831 EMIT_ZERO(dst_reg);
832 break;
833 case BPF_ALU64 | BPF_NEG:
834
835 EMIT4(0xb9130000, dst_reg, dst_reg);
836 break;
837
838
839
840 case BPF_ALU | BPF_END | BPF_FROM_BE:
841
842 switch (imm) {
843 case 16:
844
845 EMIT4(0xb9850000, dst_reg, dst_reg);
846 break;
847 case 32:
848
849 EMIT4(0xb9160000, dst_reg, dst_reg);
850 break;
851 case 64:
852 break;
853 }
854 break;
855 case BPF_ALU | BPF_END | BPF_FROM_LE:
856 switch (imm) {
857 case 16:
858
859 EMIT4(0xb91f0000, dst_reg, dst_reg);
860
861 EMIT4_DISP(0x88000000, dst_reg, REG_0, 16);
862
863 EMIT4(0xb9850000, dst_reg, dst_reg);
864 break;
865 case 32:
866
867 EMIT4(0xb91f0000, dst_reg, dst_reg);
868
869 EMIT4(0xb9160000, dst_reg, dst_reg);
870 break;
871 case 64:
872
873 EMIT4(0xb90f0000, dst_reg, dst_reg);
874 break;
875 }
876 break;
877
878
879
880 case BPF_STX | BPF_MEM | BPF_B:
881
882 EMIT6_DISP_LH(0xe3000000, 0x0072, src_reg, dst_reg, REG_0, off);
883 jit->seen |= SEEN_MEM;
884 break;
885 case BPF_STX | BPF_MEM | BPF_H:
886
887 EMIT6_DISP_LH(0xe3000000, 0x0070, src_reg, dst_reg, REG_0, off);
888 jit->seen |= SEEN_MEM;
889 break;
890 case BPF_STX | BPF_MEM | BPF_W:
891
892 EMIT6_DISP_LH(0xe3000000, 0x0050, src_reg, dst_reg, REG_0, off);
893 jit->seen |= SEEN_MEM;
894 break;
895 case BPF_STX | BPF_MEM | BPF_DW:
896
897 EMIT6_DISP_LH(0xe3000000, 0x0024, src_reg, dst_reg, REG_0, off);
898 jit->seen |= SEEN_MEM;
899 break;
900 case BPF_ST | BPF_MEM | BPF_B:
901
902 EMIT4_IMM(0xa7080000, REG_W0, (u8) imm);
903
904 EMIT6_DISP_LH(0xe3000000, 0x0072, REG_W0, dst_reg, REG_0, off);
905 jit->seen |= SEEN_MEM;
906 break;
907 case BPF_ST | BPF_MEM | BPF_H:
908
909 EMIT4_IMM(0xa7080000, REG_W0, (u16) imm);
910
911 EMIT6_DISP_LH(0xe3000000, 0x0070, REG_W0, dst_reg, REG_0, off);
912 jit->seen |= SEEN_MEM;
913 break;
914 case BPF_ST | BPF_MEM | BPF_W:
915
916 EMIT6_IMM(0xc00f0000, REG_W0, (u32) imm);
917
918 EMIT6_DISP_LH(0xe3000000, 0x0050, REG_W0, dst_reg, REG_0, off);
919 jit->seen |= SEEN_MEM;
920 break;
921 case BPF_ST | BPF_MEM | BPF_DW:
922
923 EMIT6_IMM(0xc0010000, REG_W0, imm);
924
925 EMIT6_DISP_LH(0xe3000000, 0x0024, REG_W0, dst_reg, REG_0, off);
926 jit->seen |= SEEN_MEM;
927 break;
928
929
930
931 case BPF_STX | BPF_XADD | BPF_W:
932
933 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W0, src_reg,
934 dst_reg, off);
935 jit->seen |= SEEN_MEM;
936 break;
937 case BPF_STX | BPF_XADD | BPF_DW:
938
939 EMIT6_DISP_LH(0xeb000000, 0x00ea, REG_W0, src_reg,
940 dst_reg, off);
941 jit->seen |= SEEN_MEM;
942 break;
943
944
945
946 case BPF_LDX | BPF_MEM | BPF_B:
947
948 EMIT6_DISP_LH(0xe3000000, 0x0090, dst_reg, src_reg, REG_0, off);
949 jit->seen |= SEEN_MEM;
950 break;
951 case BPF_LDX | BPF_MEM | BPF_H:
952
953 EMIT6_DISP_LH(0xe3000000, 0x0091, dst_reg, src_reg, REG_0, off);
954 jit->seen |= SEEN_MEM;
955 break;
956 case BPF_LDX | BPF_MEM | BPF_W:
957
958 jit->seen |= SEEN_MEM;
959 EMIT6_DISP_LH(0xe3000000, 0x0016, dst_reg, src_reg, REG_0, off);
960 break;
961 case BPF_LDX | BPF_MEM | BPF_DW:
962
963 jit->seen |= SEEN_MEM;
964 EMIT6_DISP_LH(0xe3000000, 0x0004, dst_reg, src_reg, REG_0, off);
965 break;
966
967
968
969 case BPF_JMP | BPF_CALL:
970 {
971
972
973
974 const u64 func = (u64)__bpf_call_base + imm;
975
976 REG_SET_SEEN(BPF_REG_5);
977 jit->seen |= SEEN_FUNC;
978
979 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_W1, REG_0, REG_L,
980 EMIT_CONST_U64(func));
981
982 EMIT2(0x0d00, REG_14, REG_W1);
983
984 EMIT4(0xb9040000, BPF_REG_0, REG_2);
985 if ((jit->seen & SEEN_SKB) &&
986 bpf_helper_changes_pkt_data((void *)func)) {
987
988 EMIT6_DISP_LH(0xe3000000, 0x0004, BPF_REG_1, REG_0,
989 REG_15, STK_OFF_SKBP);
990 emit_load_skb_data_hlen(jit);
991 }
992 break;
993 }
994 case BPF_JMP | BPF_TAIL_CALL:
995
996
997
998
999
1000
1001 jit->seen |= SEEN_TAIL_CALL;
1002
1003
1004
1005
1006
1007
1008
1009 EMIT6_DISP_LH(0xe3000000, 0x0016, REG_W1, REG_0, BPF_REG_2,
1010 offsetof(struct bpf_array, map.max_entries));
1011
1012 EMIT6_PCREL_LABEL(0xec000000, 0x0065, BPF_REG_3,
1013 REG_W1, 0, 0xa);
1014
1015
1016
1017
1018
1019
1020 if (jit->seen & SEEN_STACK)
1021 off = STK_OFF_TCCNT + STK_OFF + fp->aux->stack_depth;
1022 else
1023 off = STK_OFF_TCCNT;
1024
1025 EMIT4_IMM(0xa7080000, REG_W0, 1);
1026
1027 EMIT6_DISP_LH(0xeb000000, 0x00fa, REG_W1, REG_W0, REG_15, off);
1028
1029 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007f, REG_W1,
1030 MAX_TAIL_CALL_CNT, 0, 0x2);
1031
1032
1033
1034
1035
1036
1037
1038
1039 EMIT6_DISP_LH(0xeb000000, 0x000d, REG_1, BPF_REG_3, REG_0, 3);
1040
1041 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1, BPF_REG_2,
1042 REG_1, offsetof(struct bpf_array, ptrs));
1043
1044 EMIT6_PCREL_IMM_LABEL(0xec000000, 0x007d, REG_1, 0, 0, 0x8);
1045
1046
1047
1048
1049 save_restore_regs(jit, REGS_RESTORE, fp->aux->stack_depth);
1050
1051
1052
1053
1054
1055
1056 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_1, REG_1, REG_0,
1057 offsetof(struct bpf_prog, bpf_func));
1058
1059 _EMIT4(0x47f01000 + jit->tail_call_start);
1060
1061 jit->labels[0] = jit->prg;
1062 break;
1063 case BPF_JMP | BPF_EXIT:
1064 last = (i == fp->len - 1) ? 1 : 0;
1065 if (last && !(jit->seen & SEEN_RET0))
1066 break;
1067
1068 EMIT4_PCREL(0xa7f40000, jit->exit_ip - jit->prg);
1069 break;
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090 case BPF_JMP | BPF_JA:
1091 mask = 0xf000;
1092 goto branch_oc;
1093 case BPF_JMP | BPF_JSGT | BPF_K:
1094 mask = 0x2000;
1095 goto branch_ks;
1096 case BPF_JMP | BPF_JSLT | BPF_K:
1097 mask = 0x4000;
1098 goto branch_ks;
1099 case BPF_JMP | BPF_JSGE | BPF_K:
1100 mask = 0xa000;
1101 goto branch_ks;
1102 case BPF_JMP | BPF_JSLE | BPF_K:
1103 mask = 0xc000;
1104 goto branch_ks;
1105 case BPF_JMP | BPF_JGT | BPF_K:
1106 mask = 0x2000;
1107 goto branch_ku;
1108 case BPF_JMP | BPF_JLT | BPF_K:
1109 mask = 0x4000;
1110 goto branch_ku;
1111 case BPF_JMP | BPF_JGE | BPF_K:
1112 mask = 0xa000;
1113 goto branch_ku;
1114 case BPF_JMP | BPF_JLE | BPF_K:
1115 mask = 0xc000;
1116 goto branch_ku;
1117 case BPF_JMP | BPF_JNE | BPF_K:
1118 mask = 0x7000;
1119 goto branch_ku;
1120 case BPF_JMP | BPF_JEQ | BPF_K:
1121 mask = 0x8000;
1122 goto branch_ku;
1123 case BPF_JMP | BPF_JSET | BPF_K:
1124 mask = 0x7000;
1125
1126 EMIT6_IMM(0xc0010000, REG_W1, imm);
1127
1128 EMIT4(0xb9800000, REG_W1, dst_reg);
1129 goto branch_oc;
1130
1131 case BPF_JMP | BPF_JSGT | BPF_X:
1132 mask = 0x2000;
1133 goto branch_xs;
1134 case BPF_JMP | BPF_JSLT | BPF_X:
1135 mask = 0x4000;
1136 goto branch_xs;
1137 case BPF_JMP | BPF_JSGE | BPF_X:
1138 mask = 0xa000;
1139 goto branch_xs;
1140 case BPF_JMP | BPF_JSLE | BPF_X:
1141 mask = 0xc000;
1142 goto branch_xs;
1143 case BPF_JMP | BPF_JGT | BPF_X:
1144 mask = 0x2000;
1145 goto branch_xu;
1146 case BPF_JMP | BPF_JLT | BPF_X:
1147 mask = 0x4000;
1148 goto branch_xu;
1149 case BPF_JMP | BPF_JGE | BPF_X:
1150 mask = 0xa000;
1151 goto branch_xu;
1152 case BPF_JMP | BPF_JLE | BPF_X:
1153 mask = 0xc000;
1154 goto branch_xu;
1155 case BPF_JMP | BPF_JNE | BPF_X:
1156 mask = 0x7000;
1157 goto branch_xu;
1158 case BPF_JMP | BPF_JEQ | BPF_X:
1159 mask = 0x8000;
1160 goto branch_xu;
1161 case BPF_JMP | BPF_JSET | BPF_X:
1162 mask = 0x7000;
1163
1164 EMIT4_RRF(0xb9e40000, REG_W1, dst_reg, src_reg);
1165 goto branch_oc;
1166branch_ks:
1167
1168 EMIT6_IMM(0xc0010000, REG_W1, imm);
1169
1170 EMIT6_PCREL(0xec000000, 0x0064, dst_reg, REG_W1, i, off, mask);
1171 break;
1172branch_ku:
1173
1174 EMIT6_IMM(0xc0010000, REG_W1, imm);
1175
1176 EMIT6_PCREL(0xec000000, 0x0065, dst_reg, REG_W1, i, off, mask);
1177 break;
1178branch_xs:
1179
1180 EMIT6_PCREL(0xec000000, 0x0064, dst_reg, src_reg, i, off, mask);
1181 break;
1182branch_xu:
1183
1184 EMIT6_PCREL(0xec000000, 0x0065, dst_reg, src_reg, i, off, mask);
1185 break;
1186branch_oc:
1187
1188 jmp_off = addrs[i + off + 1] - (addrs[i + 1] - 4);
1189 EMIT4_PCREL(0xa7040000 | mask << 8, jmp_off);
1190 break;
1191
1192
1193
1194 case BPF_LD | BPF_ABS | BPF_B:
1195 case BPF_LD | BPF_IND | BPF_B:
1196 if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
1197 func_addr = __pa(sk_load_byte_pos);
1198 else
1199 func_addr = __pa(sk_load_byte);
1200 goto call_fn;
1201 case BPF_LD | BPF_ABS | BPF_H:
1202 case BPF_LD | BPF_IND | BPF_H:
1203 if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
1204 func_addr = __pa(sk_load_half_pos);
1205 else
1206 func_addr = __pa(sk_load_half);
1207 goto call_fn;
1208 case BPF_LD | BPF_ABS | BPF_W:
1209 case BPF_LD | BPF_IND | BPF_W:
1210 if ((BPF_MODE(insn->code) == BPF_ABS) && (imm >= 0))
1211 func_addr = __pa(sk_load_word_pos);
1212 else
1213 func_addr = __pa(sk_load_word);
1214 goto call_fn;
1215call_fn:
1216 jit->seen |= SEEN_SKB | SEEN_RET0 | SEEN_FUNC;
1217 REG_SET_SEEN(REG_14);
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 EMIT6_IMM(0xc00f0000, REG_W1, func_addr);
1236
1237
1238 EMIT6_IMM(0xc0010000, BPF_REG_2, imm);
1239 if (BPF_MODE(insn->code) == BPF_IND)
1240
1241 EMIT4(0xb9180000, BPF_REG_2, src_reg);
1242
1243
1244 if (jit->seen & SEEN_REG_AX)
1245
1246 EMIT6_DISP_LH(0xe3000000, 0x0004, REG_SKB_DATA, REG_0,
1247 BPF_REG_6, offsetof(struct sk_buff, data));
1248
1249 EMIT2(0x0d00, BPF_REG_5, REG_W1);
1250
1251
1252
1253
1254
1255
1256 EMIT4_PCREL(0xa7740000, jit->ret0_ip - jit->prg);
1257 break;
1258 default:
1259 pr_err("Unknown opcode %02x\n", insn->code);
1260 return -1;
1261 }
1262 return insn_count;
1263}
1264
1265
1266
1267
1268static int bpf_jit_prog(struct bpf_jit *jit, struct bpf_prog *fp)
1269{
1270 int i, insn_count;
1271
1272 jit->lit = jit->lit_start;
1273 jit->prg = 0;
1274
1275 bpf_jit_prologue(jit, fp->aux->stack_depth);
1276 for (i = 0; i < fp->len; i += insn_count) {
1277 insn_count = bpf_jit_insn(jit, fp, i);
1278 if (insn_count < 0)
1279 return -1;
1280
1281 jit->addrs[i + insn_count] = jit->prg;
1282 }
1283 bpf_jit_epilogue(jit, fp->aux->stack_depth);
1284
1285 jit->lit_start = jit->prg;
1286 jit->size = jit->lit;
1287 jit->size_prg = jit->prg;
1288 return 0;
1289}
1290
1291
1292
1293
1294struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
1295{
1296 struct bpf_prog *tmp, *orig_fp = fp;
1297 struct bpf_binary_header *header;
1298 bool tmp_blinded = false;
1299 struct bpf_jit jit;
1300 int pass;
1301
1302 if (!bpf_jit_enable)
1303 return orig_fp;
1304
1305 tmp = bpf_jit_blind_constants(fp);
1306
1307
1308
1309
1310 if (IS_ERR(tmp))
1311 return orig_fp;
1312 if (tmp != fp) {
1313 tmp_blinded = true;
1314 fp = tmp;
1315 }
1316
1317 memset(&jit, 0, sizeof(jit));
1318 jit.addrs = kcalloc(fp->len + 1, sizeof(*jit.addrs), GFP_KERNEL);
1319 if (jit.addrs == NULL) {
1320 fp = orig_fp;
1321 goto out;
1322 }
1323
1324
1325
1326
1327
1328 for (pass = 1; pass <= 3; pass++) {
1329 if (bpf_jit_prog(&jit, fp)) {
1330 fp = orig_fp;
1331 goto free_addrs;
1332 }
1333 }
1334
1335
1336
1337 if (jit.size >= BPF_SIZE_MAX) {
1338 fp = orig_fp;
1339 goto free_addrs;
1340 }
1341 header = bpf_jit_binary_alloc(jit.size, &jit.prg_buf, 2, jit_fill_hole);
1342 if (!header) {
1343 fp = orig_fp;
1344 goto free_addrs;
1345 }
1346 if (bpf_jit_prog(&jit, fp)) {
1347 fp = orig_fp;
1348 goto free_addrs;
1349 }
1350 if (bpf_jit_enable > 1) {
1351 bpf_jit_dump(fp->len, jit.size, pass, jit.prg_buf);
1352 print_fn_code(jit.prg_buf, jit.size_prg);
1353 }
1354 bpf_jit_binary_lock_ro(header);
1355 fp->bpf_func = (void *) jit.prg_buf;
1356 fp->jited = 1;
1357 fp->jited_len = jit.size;
1358free_addrs:
1359 kfree(jit.addrs);
1360out:
1361 if (tmp_blinded)
1362 bpf_jit_prog_release_other(fp, fp == orig_fp ?
1363 tmp : orig_fp);
1364 return fp;
1365}
1366