2 * Just-In-Time compiler for eBPF filters on MIPS
4 * Copyright (c) 2017 Cavium, Inc.
8 * Copyright (c) 2014 Imagination Technologies Ltd.
9 * Author: Markos Chandras <markos.chandras@imgtec.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; version 2 of the License.
16 #include <linux/bitops.h>
17 #include <linux/errno.h>
18 #include <linux/filter.h>
19 #include <linux/bpf.h>
20 #include <linux/slab.h>
21 #include <asm/bitops.h>
22 #include <asm/byteorder.h>
23 #include <asm/cacheflush.h>
24 #include <asm/cpu-features.h>
27 /* Registers used by JIT */
30 #define MIPS_R_V0 2 /* BPF_R0 */
32 #define MIPS_R_A0 4 /* BPF_R1 */
33 #define MIPS_R_A1 5 /* BPF_R2 */
34 #define MIPS_R_A2 6 /* BPF_R3 */
35 #define MIPS_R_A3 7 /* BPF_R4 */
36 #define MIPS_R_A4 8 /* BPF_R5 */
37 #define MIPS_R_T4 12 /* BPF_AX */
41 #define MIPS_R_S0 16 /* BPF_R6 */
42 #define MIPS_R_S1 17 /* BPF_R7 */
43 #define MIPS_R_S2 18 /* BPF_R8 */
44 #define MIPS_R_S3 19 /* BPF_R9 */
45 #define MIPS_R_S4 20 /* BPF_TCC */
55 #define EBPF_SAVE_S0 BIT(0)
56 #define EBPF_SAVE_S1 BIT(1)
57 #define EBPF_SAVE_S2 BIT(2)
58 #define EBPF_SAVE_S3 BIT(3)
59 #define EBPF_SAVE_S4 BIT(4)
60 #define EBPF_SAVE_RA BIT(5)
61 #define EBPF_SEEN_FP BIT(6)
62 #define EBPF_SEEN_TC BIT(7)
63 #define EBPF_TCC_IN_V1 BIT(8)
66 * For the mips64 ISA, we need to track the value range or type for
67 * each JIT register. The BPF machine requires zero extended 32-bit
68 * values, but the mips64 ISA requires sign extended 32-bit values.
69 * At each point in the BPF program we track the state of every
70 * register so that we can zero extend or sign extend as the BPF
76 /* not known to be 32-bit compatible. */
78 /* 32-bit compatible, no truncation needed for 64-bit ops. */
80 /* 32-bit compatible, need truncation for 64-bit ops. */
82 /* 32-bit zero extended. */
84 /* 32-bit no sign/zero extension needed. */
89 * high bit of offsets indicates if long branch conversion done at
92 #define OFFSETS_B_CONV BIT(31)
95 * struct jit_ctx - JIT context
97 * @stack_size: eBPF stack size
98 * @tmp_offset: eBPF $sp offset to 8-byte temporary memory
99 * @idx: Instruction index
101 * @offsets: Instruction offsets
102 * @target: Memory location for the compiled filter
103 * @reg_val_types Packed enum reg_val_type for each register.
106 const struct bpf_prog
*skf
;
114 unsigned int long_b_conversion
:1;
115 unsigned int gen_b_offsets
:1;
118 static void set_reg_val_type(u64
*rvt
, int reg
, enum reg_val_type type
)
120 *rvt
&= ~(7ull << (reg
* 3));
121 *rvt
|= ((u64
)type
<< (reg
* 3));
124 static enum reg_val_type
get_reg_val_type(const struct jit_ctx
*ctx
,
127 return (ctx
->reg_val_types
[index
] >> (reg
* 3)) & 7;
130 /* Simply emit the instruction if the JIT memory space has been allocated */
131 #define emit_instr(ctx, func, ...) \
133 if ((ctx)->target != NULL) { \
134 u32 *p = &(ctx)->target[ctx->idx]; \
135 uasm_i_##func(&p, ##__VA_ARGS__); \
140 static unsigned int j_target(struct jit_ctx
*ctx
, int target_idx
)
142 unsigned long target_va
, base_va
;
148 base_va
= (unsigned long)ctx
->target
;
149 target_va
= base_va
+ (ctx
->offsets
[target_idx
] & ~OFFSETS_B_CONV
);
151 if ((base_va
& ~0x0ffffffful
) != (target_va
& ~0x0ffffffful
))
152 return (unsigned int)-1;
153 r
= target_va
& 0x0ffffffful
;
157 /* Compute the immediate value for PC-relative branches. */
158 static u32
b_imm(unsigned int tgt
, struct jit_ctx
*ctx
)
160 if (!ctx
->gen_b_offsets
)
164 * We want a pc-relative branch. tgt is the instruction offset
165 * we want to jump to.
168 * I: target_offset <- sign_extend(offset)
169 * I+1: PC += target_offset (delay slot)
171 * ctx->idx currently points to the branch instruction
172 * but the offset is added to the delay slot so we need
175 return (ctx
->offsets
[tgt
] & ~OFFSETS_B_CONV
) -
179 int bpf_jit_enable __read_mostly
;
181 enum which_ebpf_reg
{
189 * For eBPF, the register mapping naturally falls out of the
190 * requirements of eBPF and the MIPS n64 ABI. We don't maintain a
191 * separate frame pointer, so BPF_REG_10 relative accesses are
192 * adjusted to be $sp relative.
194 int ebpf_to_mips_reg(struct jit_ctx
*ctx
, const struct bpf_insn
*insn
,
195 enum which_ebpf_reg w
)
197 int ebpf_reg
= (w
== src_reg
|| w
== src_reg_no_fp
) ?
198 insn
->src_reg
: insn
->dst_reg
;
214 ctx
->flags
|= EBPF_SAVE_S0
;
217 ctx
->flags
|= EBPF_SAVE_S1
;
220 ctx
->flags
|= EBPF_SAVE_S2
;
223 ctx
->flags
|= EBPF_SAVE_S3
;
226 if (w
== dst_reg
|| w
== src_reg_no_fp
)
228 ctx
->flags
|= EBPF_SEEN_FP
;
230 * Needs special handling, return something that
231 * cannot be clobbered just in case.
238 WARN(1, "Illegal bpf reg: %d\n", ebpf_reg
);
243 * eBPF stack frame will be something like:
245 * Entry $sp ------> +--------------------------------+
247 * +--------------------------------+
249 * +--------------------------------+
251 * +--------------------------------+
253 * +--------------------------------+
255 * +--------------------------------+
257 * +--------------------------------+
258 * | tmp-storage (if $ra saved) |
259 * $sp + tmp_offset --> +--------------------------------+ <--BPF_REG_10
260 * | BPF_REG_10 relative storage |
261 * | MAX_BPF_STACK (optional) |
265 * $sp --------> +--------------------------------+
267 * If BPF_REG_10 is never referenced, then the MAX_BPF_STACK sized
268 * area is not allocated.
270 static int gen_int_prologue(struct jit_ctx
*ctx
)
272 int stack_adjust
= 0;
276 if (ctx
->flags
& EBPF_SAVE_RA
)
278 * If RA we are doing a function call and may need
279 * extra 8-byte tmp area.
282 if (ctx
->flags
& EBPF_SAVE_S0
)
284 if (ctx
->flags
& EBPF_SAVE_S1
)
286 if (ctx
->flags
& EBPF_SAVE_S2
)
288 if (ctx
->flags
& EBPF_SAVE_S3
)
290 if (ctx
->flags
& EBPF_SAVE_S4
)
293 BUILD_BUG_ON(MAX_BPF_STACK
& 7);
294 locals_size
= (ctx
->flags
& EBPF_SEEN_FP
) ? MAX_BPF_STACK
: 0;
296 stack_adjust
+= locals_size
;
297 ctx
->tmp_offset
= locals_size
;
299 ctx
->stack_size
= stack_adjust
;
302 * First instruction initializes the tail call count (TCC).
303 * On tail call we skip this instruction, and the TCC is
304 * passed in $v1 from the caller.
306 emit_instr(ctx
, daddiu
, MIPS_R_V1
, MIPS_R_ZERO
, MAX_TAIL_CALL_CNT
);
308 emit_instr(ctx
, daddiu
, MIPS_R_SP
, MIPS_R_SP
, -stack_adjust
);
312 store_offset
= stack_adjust
- 8;
314 if (ctx
->flags
& EBPF_SAVE_RA
) {
315 emit_instr(ctx
, sd
, MIPS_R_RA
, store_offset
, MIPS_R_SP
);
318 if (ctx
->flags
& EBPF_SAVE_S0
) {
319 emit_instr(ctx
, sd
, MIPS_R_S0
, store_offset
, MIPS_R_SP
);
322 if (ctx
->flags
& EBPF_SAVE_S1
) {
323 emit_instr(ctx
, sd
, MIPS_R_S1
, store_offset
, MIPS_R_SP
);
326 if (ctx
->flags
& EBPF_SAVE_S2
) {
327 emit_instr(ctx
, sd
, MIPS_R_S2
, store_offset
, MIPS_R_SP
);
330 if (ctx
->flags
& EBPF_SAVE_S3
) {
331 emit_instr(ctx
, sd
, MIPS_R_S3
, store_offset
, MIPS_R_SP
);
334 if (ctx
->flags
& EBPF_SAVE_S4
) {
335 emit_instr(ctx
, sd
, MIPS_R_S4
, store_offset
, MIPS_R_SP
);
339 if ((ctx
->flags
& EBPF_SEEN_TC
) && !(ctx
->flags
& EBPF_TCC_IN_V1
))
340 emit_instr(ctx
, daddu
, MIPS_R_S4
, MIPS_R_V1
, MIPS_R_ZERO
);
345 static int build_int_epilogue(struct jit_ctx
*ctx
, int dest_reg
)
347 const struct bpf_prog
*prog
= ctx
->skf
;
348 int stack_adjust
= ctx
->stack_size
;
349 int store_offset
= stack_adjust
- 8;
352 if (dest_reg
== MIPS_R_RA
&&
353 get_reg_val_type(ctx
, prog
->len
, BPF_REG_0
) == REG_32BIT_ZERO_EX
)
354 /* Don't let zero extended value escape. */
355 emit_instr(ctx
, sll
, r0
, r0
, 0);
357 if (ctx
->flags
& EBPF_SAVE_RA
) {
358 emit_instr(ctx
, ld
, MIPS_R_RA
, store_offset
, MIPS_R_SP
);
361 if (ctx
->flags
& EBPF_SAVE_S0
) {
362 emit_instr(ctx
, ld
, MIPS_R_S0
, store_offset
, MIPS_R_SP
);
365 if (ctx
->flags
& EBPF_SAVE_S1
) {
366 emit_instr(ctx
, ld
, MIPS_R_S1
, store_offset
, MIPS_R_SP
);
369 if (ctx
->flags
& EBPF_SAVE_S2
) {
370 emit_instr(ctx
, ld
, MIPS_R_S2
, store_offset
, MIPS_R_SP
);
373 if (ctx
->flags
& EBPF_SAVE_S3
) {
374 emit_instr(ctx
, ld
, MIPS_R_S3
, store_offset
, MIPS_R_SP
);
377 if (ctx
->flags
& EBPF_SAVE_S4
) {
378 emit_instr(ctx
, ld
, MIPS_R_S4
, store_offset
, MIPS_R_SP
);
381 emit_instr(ctx
, jr
, dest_reg
);
384 emit_instr(ctx
, daddiu
, MIPS_R_SP
, MIPS_R_SP
, stack_adjust
);
386 emit_instr(ctx
, nop
);
391 static void gen_imm_to_reg(const struct bpf_insn
*insn
, int reg
,
394 if (insn
->imm
>= S16_MIN
&& insn
->imm
<= S16_MAX
) {
395 emit_instr(ctx
, addiu
, reg
, MIPS_R_ZERO
, insn
->imm
);
397 int lower
= (s16
)(insn
->imm
& 0xffff);
398 int upper
= insn
->imm
- lower
;
400 emit_instr(ctx
, lui
, reg
, upper
>> 16);
401 emit_instr(ctx
, addiu
, reg
, reg
, lower
);
406 static int gen_imm_insn(const struct bpf_insn
*insn
, struct jit_ctx
*ctx
,
409 int upper_bound
, lower_bound
;
410 int dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
415 switch (BPF_OP(insn
->code
)) {
418 upper_bound
= S16_MAX
;
419 lower_bound
= S16_MIN
;
422 upper_bound
= -(int)S16_MIN
;
423 lower_bound
= -(int)S16_MAX
;
428 upper_bound
= 0xffff;
434 /* Shift amounts are truncated, no need for bounds */
435 upper_bound
= S32_MAX
;
436 lower_bound
= S32_MIN
;
443 * Immediate move clobbers the register, so no sign/zero
446 if (BPF_CLASS(insn
->code
) == BPF_ALU64
&&
447 BPF_OP(insn
->code
) != BPF_MOV
&&
448 get_reg_val_type(ctx
, idx
, insn
->dst_reg
) == REG_32BIT
)
449 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
450 /* BPF_ALU | BPF_LSH doesn't need separate sign extension */
451 if (BPF_CLASS(insn
->code
) == BPF_ALU
&&
452 BPF_OP(insn
->code
) != BPF_LSH
&&
453 BPF_OP(insn
->code
) != BPF_MOV
&&
454 get_reg_val_type(ctx
, idx
, insn
->dst_reg
) != REG_32BIT
)
455 emit_instr(ctx
, sll
, dst
, dst
, 0);
457 if (insn
->imm
>= lower_bound
&& insn
->imm
<= upper_bound
) {
458 /* single insn immediate case */
459 switch (BPF_OP(insn
->code
) | BPF_CLASS(insn
->code
)) {
460 case BPF_ALU64
| BPF_MOV
:
461 emit_instr(ctx
, daddiu
, dst
, MIPS_R_ZERO
, insn
->imm
);
463 case BPF_ALU64
| BPF_AND
:
464 case BPF_ALU
| BPF_AND
:
465 emit_instr(ctx
, andi
, dst
, dst
, insn
->imm
);
467 case BPF_ALU64
| BPF_OR
:
468 case BPF_ALU
| BPF_OR
:
469 emit_instr(ctx
, ori
, dst
, dst
, insn
->imm
);
471 case BPF_ALU64
| BPF_XOR
:
472 case BPF_ALU
| BPF_XOR
:
473 emit_instr(ctx
, xori
, dst
, dst
, insn
->imm
);
475 case BPF_ALU64
| BPF_ADD
:
476 emit_instr(ctx
, daddiu
, dst
, dst
, insn
->imm
);
478 case BPF_ALU64
| BPF_SUB
:
479 emit_instr(ctx
, daddiu
, dst
, dst
, -insn
->imm
);
481 case BPF_ALU64
| BPF_RSH
:
482 emit_instr(ctx
, dsrl_safe
, dst
, dst
, insn
->imm
& 0x3f);
484 case BPF_ALU
| BPF_RSH
:
485 emit_instr(ctx
, srl
, dst
, dst
, insn
->imm
& 0x1f);
487 case BPF_ALU64
| BPF_LSH
:
488 emit_instr(ctx
, dsll_safe
, dst
, dst
, insn
->imm
& 0x3f);
490 case BPF_ALU
| BPF_LSH
:
491 emit_instr(ctx
, sll
, dst
, dst
, insn
->imm
& 0x1f);
493 case BPF_ALU64
| BPF_ARSH
:
494 emit_instr(ctx
, dsra_safe
, dst
, dst
, insn
->imm
& 0x3f);
496 case BPF_ALU
| BPF_ARSH
:
497 emit_instr(ctx
, sra
, dst
, dst
, insn
->imm
& 0x1f);
499 case BPF_ALU
| BPF_MOV
:
500 emit_instr(ctx
, addiu
, dst
, MIPS_R_ZERO
, insn
->imm
);
502 case BPF_ALU
| BPF_ADD
:
503 emit_instr(ctx
, addiu
, dst
, dst
, insn
->imm
);
505 case BPF_ALU
| BPF_SUB
:
506 emit_instr(ctx
, addiu
, dst
, dst
, -insn
->imm
);
512 /* multi insn immediate case */
513 if (BPF_OP(insn
->code
) == BPF_MOV
) {
514 gen_imm_to_reg(insn
, dst
, ctx
);
516 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
517 switch (BPF_OP(insn
->code
) | BPF_CLASS(insn
->code
)) {
518 case BPF_ALU64
| BPF_AND
:
519 case BPF_ALU
| BPF_AND
:
520 emit_instr(ctx
, and, dst
, dst
, MIPS_R_AT
);
522 case BPF_ALU64
| BPF_OR
:
523 case BPF_ALU
| BPF_OR
:
524 emit_instr(ctx
, or, dst
, dst
, MIPS_R_AT
);
526 case BPF_ALU64
| BPF_XOR
:
527 case BPF_ALU
| BPF_XOR
:
528 emit_instr(ctx
, xor, dst
, dst
, MIPS_R_AT
);
530 case BPF_ALU64
| BPF_ADD
:
531 emit_instr(ctx
, daddu
, dst
, dst
, MIPS_R_AT
);
533 case BPF_ALU64
| BPF_SUB
:
534 emit_instr(ctx
, dsubu
, dst
, dst
, MIPS_R_AT
);
536 case BPF_ALU
| BPF_ADD
:
537 emit_instr(ctx
, addu
, dst
, dst
, MIPS_R_AT
);
539 case BPF_ALU
| BPF_SUB
:
540 emit_instr(ctx
, subu
, dst
, dst
, MIPS_R_AT
);
551 static void * __must_check
552 ool_skb_header_pointer(const struct sk_buff
*skb
, int offset
,
553 int len
, void *buffer
)
555 return skb_header_pointer(skb
, offset
, len
, buffer
);
558 static int size_to_len(const struct bpf_insn
*insn
)
560 switch (BPF_SIZE(insn
->code
)) {
573 static void emit_const_to_reg(struct jit_ctx
*ctx
, int dst
, u64 value
)
575 if (value
>= 0xffffffffffff8000ull
|| value
< 0x8000ull
) {
576 emit_instr(ctx
, daddiu
, dst
, MIPS_R_ZERO
, (int)value
);
577 } else if (value
>= 0xffffffff80000000ull
||
578 (value
< 0x80000000 && value
> 0xffff)) {
579 emit_instr(ctx
, lui
, dst
, (s32
)(s16
)(value
>> 16));
580 emit_instr(ctx
, ori
, dst
, dst
, (unsigned int)(value
& 0xffff));
583 bool seen_part
= false;
584 int needed_shift
= 0;
586 for (i
= 0; i
< 4; i
++) {
587 u64 part
= (value
>> (16 * (3 - i
))) & 0xffff;
589 if (seen_part
&& needed_shift
> 0 && (part
|| i
== 3)) {
590 emit_instr(ctx
, dsll_safe
, dst
, dst
, needed_shift
);
594 if (i
== 0 || (!seen_part
&& i
< 3 && part
< 0x8000)) {
595 emit_instr(ctx
, lui
, dst
, (s32
)(s16
)part
);
598 emit_instr(ctx
, ori
, dst
,
599 seen_part
? dst
: MIPS_R_ZERO
,
610 static int emit_bpf_tail_call(struct jit_ctx
*ctx
, int this_idx
)
614 ctx
->flags
|= EBPF_SEEN_TC
;
616 * if (index >= array->map.max_entries)
619 off
= offsetof(struct bpf_array
, map
.max_entries
);
620 emit_instr(ctx
, lwu
, MIPS_R_T5
, off
, MIPS_R_A1
);
621 emit_instr(ctx
, sltu
, MIPS_R_AT
, MIPS_R_T5
, MIPS_R_A2
);
622 b_off
= b_imm(this_idx
+ 1, ctx
);
623 emit_instr(ctx
, bne
, MIPS_R_AT
, MIPS_R_ZERO
, b_off
);
629 emit_instr(ctx
, daddiu
, MIPS_R_T5
,
630 (ctx
->flags
& EBPF_TCC_IN_V1
) ? MIPS_R_V1
: MIPS_R_S4
, -1);
631 b_off
= b_imm(this_idx
+ 1, ctx
);
632 emit_instr(ctx
, bltz
, MIPS_R_T5
, b_off
);
634 * prog = array->ptrs[index];
639 emit_instr(ctx
, dsll
, MIPS_R_T8
, MIPS_R_A2
, 3);
640 emit_instr(ctx
, daddu
, MIPS_R_T8
, MIPS_R_T8
, MIPS_R_A1
);
641 off
= offsetof(struct bpf_array
, ptrs
);
642 emit_instr(ctx
, ld
, MIPS_R_AT
, off
, MIPS_R_T8
);
643 b_off
= b_imm(this_idx
+ 1, ctx
);
644 emit_instr(ctx
, beq
, MIPS_R_AT
, MIPS_R_ZERO
, b_off
);
646 emit_instr(ctx
, nop
);
648 /* goto *(prog->bpf_func + 4); */
649 off
= offsetof(struct bpf_prog
, bpf_func
);
650 emit_instr(ctx
, ld
, MIPS_R_T9
, off
, MIPS_R_AT
);
651 /* All systems are go... propagate TCC */
652 emit_instr(ctx
, daddu
, MIPS_R_V1
, MIPS_R_T5
, MIPS_R_ZERO
);
653 /* Skip first instruction (TCC initialization) */
654 emit_instr(ctx
, daddiu
, MIPS_R_T9
, MIPS_R_T9
, 4);
655 return build_int_epilogue(ctx
, MIPS_R_T9
);
658 static bool use_bbit_insns(void)
660 switch (current_cpu_type()) {
661 case CPU_CAVIUM_OCTEON
:
662 case CPU_CAVIUM_OCTEON_PLUS
:
663 case CPU_CAVIUM_OCTEON2
:
664 case CPU_CAVIUM_OCTEON3
:
671 static bool is_bad_offset(int b_off
)
673 return b_off
> 0x1ffff || b_off
< -0x20000;
676 /* Returns the number of insn slots consumed. */
677 static int build_one_insn(const struct bpf_insn
*insn
, struct jit_ctx
*ctx
,
678 int this_idx
, int exit_idx
)
680 int src
, dst
, r
, td
, ts
, mem_off
, b_off
;
681 bool need_swap
, did_move
, cmp_eq
;
686 switch (insn
->code
) {
687 case BPF_ALU64
| BPF_ADD
| BPF_K
: /* ALU64_IMM */
688 case BPF_ALU64
| BPF_SUB
| BPF_K
: /* ALU64_IMM */
689 case BPF_ALU64
| BPF_OR
| BPF_K
: /* ALU64_IMM */
690 case BPF_ALU64
| BPF_AND
| BPF_K
: /* ALU64_IMM */
691 case BPF_ALU64
| BPF_LSH
| BPF_K
: /* ALU64_IMM */
692 case BPF_ALU64
| BPF_RSH
| BPF_K
: /* ALU64_IMM */
693 case BPF_ALU64
| BPF_XOR
| BPF_K
: /* ALU64_IMM */
694 case BPF_ALU64
| BPF_ARSH
| BPF_K
: /* ALU64_IMM */
695 case BPF_ALU64
| BPF_MOV
| BPF_K
: /* ALU64_IMM */
696 case BPF_ALU
| BPF_MOV
| BPF_K
: /* ALU32_IMM */
697 case BPF_ALU
| BPF_ADD
| BPF_K
: /* ALU32_IMM */
698 case BPF_ALU
| BPF_SUB
| BPF_K
: /* ALU32_IMM */
699 case BPF_ALU
| BPF_OR
| BPF_K
: /* ALU64_IMM */
700 case BPF_ALU
| BPF_AND
| BPF_K
: /* ALU64_IMM */
701 case BPF_ALU
| BPF_LSH
| BPF_K
: /* ALU64_IMM */
702 case BPF_ALU
| BPF_RSH
| BPF_K
: /* ALU64_IMM */
703 case BPF_ALU
| BPF_XOR
| BPF_K
: /* ALU64_IMM */
704 case BPF_ALU
| BPF_ARSH
| BPF_K
: /* ALU64_IMM */
705 r
= gen_imm_insn(insn
, ctx
, this_idx
);
709 case BPF_ALU64
| BPF_MUL
| BPF_K
: /* ALU64_IMM */
710 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
713 if (get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
) == REG_32BIT
)
714 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
715 if (insn
->imm
== 1) /* Mult by 1 is a nop */
717 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
718 emit_instr(ctx
, dmultu
, MIPS_R_AT
, dst
);
719 emit_instr(ctx
, mflo
, dst
);
721 case BPF_ALU64
| BPF_NEG
| BPF_K
: /* ALU64_IMM */
722 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
725 if (get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
) == REG_32BIT
)
726 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
727 emit_instr(ctx
, dsubu
, dst
, MIPS_R_ZERO
, dst
);
729 case BPF_ALU
| BPF_MUL
| BPF_K
: /* ALU_IMM */
730 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
733 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
734 if (td
== REG_64BIT
|| td
== REG_32BIT_ZERO_EX
) {
736 emit_instr(ctx
, sll
, dst
, dst
, 0);
738 if (insn
->imm
== 1) /* Mult by 1 is a nop */
740 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
741 emit_instr(ctx
, multu
, dst
, MIPS_R_AT
);
742 emit_instr(ctx
, mflo
, dst
);
744 case BPF_ALU
| BPF_NEG
| BPF_K
: /* ALU_IMM */
745 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
748 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
749 if (td
== REG_64BIT
|| td
== REG_32BIT_ZERO_EX
) {
751 emit_instr(ctx
, sll
, dst
, dst
, 0);
753 emit_instr(ctx
, subu
, dst
, MIPS_R_ZERO
, dst
);
755 case BPF_ALU
| BPF_DIV
| BPF_K
: /* ALU_IMM */
756 case BPF_ALU
| BPF_MOD
| BPF_K
: /* ALU_IMM */
757 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
760 if (insn
->imm
== 0) { /* Div by zero */
761 b_off
= b_imm(exit_idx
, ctx
);
762 if (is_bad_offset(b_off
))
764 emit_instr(ctx
, beq
, MIPS_R_ZERO
, MIPS_R_ZERO
, b_off
);
765 emit_instr(ctx
, addu
, MIPS_R_V0
, MIPS_R_ZERO
, MIPS_R_ZERO
);
767 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
768 if (td
== REG_64BIT
|| td
== REG_32BIT_ZERO_EX
)
770 emit_instr(ctx
, sll
, dst
, dst
, 0);
771 if (insn
->imm
== 1) {
772 /* div by 1 is a nop, mod by 1 is zero */
773 if (BPF_OP(insn
->code
) == BPF_MOD
)
774 emit_instr(ctx
, addu
, dst
, MIPS_R_ZERO
, MIPS_R_ZERO
);
777 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
778 emit_instr(ctx
, divu
, dst
, MIPS_R_AT
);
779 if (BPF_OP(insn
->code
) == BPF_DIV
)
780 emit_instr(ctx
, mflo
, dst
);
782 emit_instr(ctx
, mfhi
, dst
);
784 case BPF_ALU64
| BPF_DIV
| BPF_K
: /* ALU_IMM */
785 case BPF_ALU64
| BPF_MOD
| BPF_K
: /* ALU_IMM */
786 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
789 if (insn
->imm
== 0) { /* Div by zero */
790 b_off
= b_imm(exit_idx
, ctx
);
791 if (is_bad_offset(b_off
))
793 emit_instr(ctx
, beq
, MIPS_R_ZERO
, MIPS_R_ZERO
, b_off
);
794 emit_instr(ctx
, addu
, MIPS_R_V0
, MIPS_R_ZERO
, MIPS_R_ZERO
);
796 if (get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
) == REG_32BIT
)
797 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
799 if (insn
->imm
== 1) {
800 /* div by 1 is a nop, mod by 1 is zero */
801 if (BPF_OP(insn
->code
) == BPF_MOD
)
802 emit_instr(ctx
, addu
, dst
, MIPS_R_ZERO
, MIPS_R_ZERO
);
805 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
806 emit_instr(ctx
, ddivu
, dst
, MIPS_R_AT
);
807 if (BPF_OP(insn
->code
) == BPF_DIV
)
808 emit_instr(ctx
, mflo
, dst
);
810 emit_instr(ctx
, mfhi
, dst
);
812 case BPF_ALU64
| BPF_MOV
| BPF_X
: /* ALU64_REG */
813 case BPF_ALU64
| BPF_ADD
| BPF_X
: /* ALU64_REG */
814 case BPF_ALU64
| BPF_SUB
| BPF_X
: /* ALU64_REG */
815 case BPF_ALU64
| BPF_XOR
| BPF_X
: /* ALU64_REG */
816 case BPF_ALU64
| BPF_OR
| BPF_X
: /* ALU64_REG */
817 case BPF_ALU64
| BPF_AND
| BPF_X
: /* ALU64_REG */
818 case BPF_ALU64
| BPF_MUL
| BPF_X
: /* ALU64_REG */
819 case BPF_ALU64
| BPF_DIV
| BPF_X
: /* ALU64_REG */
820 case BPF_ALU64
| BPF_MOD
| BPF_X
: /* ALU64_REG */
821 case BPF_ALU64
| BPF_LSH
| BPF_X
: /* ALU64_REG */
822 case BPF_ALU64
| BPF_RSH
| BPF_X
: /* ALU64_REG */
823 case BPF_ALU64
| BPF_ARSH
| BPF_X
: /* ALU64_REG */
824 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg
);
825 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
826 if (src
< 0 || dst
< 0)
828 if (get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
) == REG_32BIT
)
829 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
831 if (insn
->src_reg
== BPF_REG_10
) {
832 if (BPF_OP(insn
->code
) == BPF_MOV
) {
833 emit_instr(ctx
, daddiu
, dst
, MIPS_R_SP
, MAX_BPF_STACK
);
836 emit_instr(ctx
, daddiu
, MIPS_R_AT
, MIPS_R_SP
, MAX_BPF_STACK
);
839 } else if (get_reg_val_type(ctx
, this_idx
, insn
->src_reg
) == REG_32BIT
) {
840 int tmp_reg
= MIPS_R_AT
;
842 if (BPF_OP(insn
->code
) == BPF_MOV
) {
846 emit_instr(ctx
, daddu
, tmp_reg
, src
, MIPS_R_ZERO
);
847 emit_instr(ctx
, dinsu
, tmp_reg
, MIPS_R_ZERO
, 32, 32);
850 switch (BPF_OP(insn
->code
)) {
853 emit_instr(ctx
, daddu
, dst
, src
, MIPS_R_ZERO
);
856 emit_instr(ctx
, daddu
, dst
, dst
, src
);
859 emit_instr(ctx
, dsubu
, dst
, dst
, src
);
862 emit_instr(ctx
, xor, dst
, dst
, src
);
865 emit_instr(ctx
, or, dst
, dst
, src
);
868 emit_instr(ctx
, and, dst
, dst
, src
);
871 emit_instr(ctx
, dmultu
, dst
, src
);
872 emit_instr(ctx
, mflo
, dst
);
876 b_off
= b_imm(exit_idx
, ctx
);
877 if (is_bad_offset(b_off
))
879 emit_instr(ctx
, beq
, src
, MIPS_R_ZERO
, b_off
);
880 emit_instr(ctx
, movz
, MIPS_R_V0
, MIPS_R_ZERO
, src
);
881 emit_instr(ctx
, ddivu
, dst
, src
);
882 if (BPF_OP(insn
->code
) == BPF_DIV
)
883 emit_instr(ctx
, mflo
, dst
);
885 emit_instr(ctx
, mfhi
, dst
);
888 emit_instr(ctx
, dsllv
, dst
, dst
, src
);
891 emit_instr(ctx
, dsrlv
, dst
, dst
, src
);
894 emit_instr(ctx
, dsrav
, dst
, dst
, src
);
897 pr_err("ALU64_REG NOT HANDLED\n");
901 case BPF_ALU
| BPF_MOV
| BPF_X
: /* ALU_REG */
902 case BPF_ALU
| BPF_ADD
| BPF_X
: /* ALU_REG */
903 case BPF_ALU
| BPF_SUB
| BPF_X
: /* ALU_REG */
904 case BPF_ALU
| BPF_XOR
| BPF_X
: /* ALU_REG */
905 case BPF_ALU
| BPF_OR
| BPF_X
: /* ALU_REG */
906 case BPF_ALU
| BPF_AND
| BPF_X
: /* ALU_REG */
907 case BPF_ALU
| BPF_MUL
| BPF_X
: /* ALU_REG */
908 case BPF_ALU
| BPF_DIV
| BPF_X
: /* ALU_REG */
909 case BPF_ALU
| BPF_MOD
| BPF_X
: /* ALU_REG */
910 case BPF_ALU
| BPF_LSH
| BPF_X
: /* ALU_REG */
911 case BPF_ALU
| BPF_RSH
| BPF_X
: /* ALU_REG */
912 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg_no_fp
);
913 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
914 if (src
< 0 || dst
< 0)
916 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
917 if (td
== REG_64BIT
|| td
== REG_32BIT_ZERO_EX
) {
919 emit_instr(ctx
, sll
, dst
, dst
, 0);
922 ts
= get_reg_val_type(ctx
, this_idx
, insn
->src_reg
);
923 if (ts
== REG_64BIT
|| ts
== REG_32BIT_ZERO_EX
) {
924 int tmp_reg
= MIPS_R_AT
;
926 if (BPF_OP(insn
->code
) == BPF_MOV
) {
931 emit_instr(ctx
, sll
, tmp_reg
, src
, 0);
934 switch (BPF_OP(insn
->code
)) {
937 emit_instr(ctx
, addu
, dst
, src
, MIPS_R_ZERO
);
940 emit_instr(ctx
, addu
, dst
, dst
, src
);
943 emit_instr(ctx
, subu
, dst
, dst
, src
);
946 emit_instr(ctx
, xor, dst
, dst
, src
);
949 emit_instr(ctx
, or, dst
, dst
, src
);
952 emit_instr(ctx
, and, dst
, dst
, src
);
955 emit_instr(ctx
, mul
, dst
, dst
, src
);
959 b_off
= b_imm(exit_idx
, ctx
);
960 if (is_bad_offset(b_off
))
962 emit_instr(ctx
, beq
, src
, MIPS_R_ZERO
, b_off
);
963 emit_instr(ctx
, movz
, MIPS_R_V0
, MIPS_R_ZERO
, src
);
964 emit_instr(ctx
, divu
, dst
, src
);
965 if (BPF_OP(insn
->code
) == BPF_DIV
)
966 emit_instr(ctx
, mflo
, dst
);
968 emit_instr(ctx
, mfhi
, dst
);
971 emit_instr(ctx
, sllv
, dst
, dst
, src
);
974 emit_instr(ctx
, srlv
, dst
, dst
, src
);
977 pr_err("ALU_REG NOT HANDLED\n");
981 case BPF_JMP
| BPF_EXIT
:
982 if (this_idx
+ 1 < exit_idx
) {
983 b_off
= b_imm(exit_idx
, ctx
);
984 if (is_bad_offset(b_off
))
986 emit_instr(ctx
, beq
, MIPS_R_ZERO
, MIPS_R_ZERO
, b_off
);
987 emit_instr(ctx
, nop
);
990 case BPF_JMP
| BPF_JEQ
| BPF_K
: /* JMP_IMM */
991 case BPF_JMP
| BPF_JNE
| BPF_K
: /* JMP_IMM */
992 cmp_eq
= (BPF_OP(insn
->code
) == BPF_JEQ
);
993 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg_fp_ok
);
996 if (insn
->imm
== 0) {
999 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
1003 case BPF_JMP
| BPF_JEQ
| BPF_X
: /* JMP_REG */
1004 case BPF_JMP
| BPF_JNE
| BPF_X
:
1005 case BPF_JMP
| BPF_JSGT
| BPF_X
:
1006 case BPF_JMP
| BPF_JSGE
| BPF_X
:
1007 case BPF_JMP
| BPF_JGT
| BPF_X
:
1008 case BPF_JMP
| BPF_JGE
| BPF_X
:
1009 case BPF_JMP
| BPF_JSET
| BPF_X
:
1010 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg_no_fp
);
1011 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1012 if (src
< 0 || dst
< 0)
1014 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
1015 ts
= get_reg_val_type(ctx
, this_idx
, insn
->src_reg
);
1016 if (td
== REG_32BIT
&& ts
!= REG_32BIT
) {
1017 emit_instr(ctx
, sll
, MIPS_R_AT
, src
, 0);
1019 } else if (ts
== REG_32BIT
&& td
!= REG_32BIT
) {
1020 emit_instr(ctx
, sll
, MIPS_R_AT
, dst
, 0);
1023 if (BPF_OP(insn
->code
) == BPF_JSET
) {
1024 emit_instr(ctx
, and, MIPS_R_AT
, dst
, src
);
1028 } else if (BPF_OP(insn
->code
) == BPF_JSGT
) {
1029 emit_instr(ctx
, dsubu
, MIPS_R_AT
, dst
, src
);
1030 if ((insn
+ 1)->code
== (BPF_JMP
| BPF_EXIT
) && insn
->off
== 1) {
1031 b_off
= b_imm(exit_idx
, ctx
);
1032 if (is_bad_offset(b_off
))
1034 emit_instr(ctx
, blez
, MIPS_R_AT
, b_off
);
1035 emit_instr(ctx
, nop
);
1036 return 2; /* We consumed the exit. */
1038 b_off
= b_imm(this_idx
+ insn
->off
+ 1, ctx
);
1039 if (is_bad_offset(b_off
))
1041 emit_instr(ctx
, bgtz
, MIPS_R_AT
, b_off
);
1042 emit_instr(ctx
, nop
);
1044 } else if (BPF_OP(insn
->code
) == BPF_JSGE
) {
1045 emit_instr(ctx
, slt
, MIPS_R_AT
, dst
, src
);
1049 } else if (BPF_OP(insn
->code
) == BPF_JGT
) {
1050 /* dst or src could be AT */
1051 emit_instr(ctx
, dsubu
, MIPS_R_T8
, dst
, src
);
1052 emit_instr(ctx
, sltu
, MIPS_R_AT
, dst
, src
);
1053 /* SP known to be non-zero, movz becomes boolean not */
1054 emit_instr(ctx
, movz
, MIPS_R_T9
, MIPS_R_SP
, MIPS_R_T8
);
1055 emit_instr(ctx
, movn
, MIPS_R_T9
, MIPS_R_ZERO
, MIPS_R_T8
);
1056 emit_instr(ctx
, or, MIPS_R_AT
, MIPS_R_T9
, MIPS_R_AT
);
1060 } else if (BPF_OP(insn
->code
) == BPF_JGE
) {
1061 emit_instr(ctx
, sltu
, MIPS_R_AT
, dst
, src
);
1065 } else { /* JNE/JEQ case */
1066 cmp_eq
= (BPF_OP(insn
->code
) == BPF_JEQ
);
1070 * If the next insn is EXIT and we are jumping arround
1071 * only it, invert the sense of the compare and
1072 * conditionally jump to the exit. Poor man's branch
1075 if ((insn
+ 1)->code
== (BPF_JMP
| BPF_EXIT
) && insn
->off
== 1) {
1076 b_off
= b_imm(exit_idx
, ctx
);
1077 if (is_bad_offset(b_off
)) {
1078 target
= j_target(ctx
, exit_idx
);
1079 if (target
== (unsigned int)-1)
1083 if (!(ctx
->offsets
[this_idx
] & OFFSETS_B_CONV
)) {
1084 ctx
->offsets
[this_idx
] |= OFFSETS_B_CONV
;
1085 ctx
->long_b_conversion
= 1;
1090 emit_instr(ctx
, bne
, dst
, src
, b_off
);
1092 emit_instr(ctx
, beq
, dst
, src
, b_off
);
1093 emit_instr(ctx
, nop
);
1094 if (ctx
->offsets
[this_idx
] & OFFSETS_B_CONV
) {
1095 emit_instr(ctx
, j
, target
);
1096 emit_instr(ctx
, nop
);
1098 return 2; /* We consumed the exit. */
1100 b_off
= b_imm(this_idx
+ insn
->off
+ 1, ctx
);
1101 if (is_bad_offset(b_off
)) {
1102 target
= j_target(ctx
, this_idx
+ insn
->off
+ 1);
1103 if (target
== (unsigned int)-1)
1107 if (!(ctx
->offsets
[this_idx
] & OFFSETS_B_CONV
)) {
1108 ctx
->offsets
[this_idx
] |= OFFSETS_B_CONV
;
1109 ctx
->long_b_conversion
= 1;
1114 emit_instr(ctx
, beq
, dst
, src
, b_off
);
1116 emit_instr(ctx
, bne
, dst
, src
, b_off
);
1117 emit_instr(ctx
, nop
);
1118 if (ctx
->offsets
[this_idx
] & OFFSETS_B_CONV
) {
1119 emit_instr(ctx
, j
, target
);
1120 emit_instr(ctx
, nop
);
1123 case BPF_JMP
| BPF_JSGT
| BPF_K
: /* JMP_IMM */
1124 case BPF_JMP
| BPF_JSGE
| BPF_K
: /* JMP_IMM */
1125 cmp_eq
= (BPF_OP(insn
->code
) == BPF_JSGE
);
1126 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg_fp_ok
);
1130 if (insn
->imm
== 0) {
1131 if ((insn
+ 1)->code
== (BPF_JMP
| BPF_EXIT
) && insn
->off
== 1) {
1132 b_off
= b_imm(exit_idx
, ctx
);
1133 if (is_bad_offset(b_off
))
1136 emit_instr(ctx
, bltz
, dst
, b_off
);
1138 emit_instr(ctx
, blez
, dst
, b_off
);
1139 emit_instr(ctx
, nop
);
1140 return 2; /* We consumed the exit. */
1142 b_off
= b_imm(this_idx
+ insn
->off
+ 1, ctx
);
1143 if (is_bad_offset(b_off
))
1146 emit_instr(ctx
, bgez
, dst
, b_off
);
1148 emit_instr(ctx
, bgtz
, dst
, b_off
);
1149 emit_instr(ctx
, nop
);
1153 * only "LT" compare available, so we must use imm + 1
1156 t64s
= insn
->imm
+ (cmp_eq
? 0 : 1);
1157 if (t64s
>= S16_MIN
&& t64s
<= S16_MAX
) {
1158 emit_instr(ctx
, slti
, MIPS_R_AT
, dst
, (int)t64s
);
1164 emit_const_to_reg(ctx
, MIPS_R_AT
, (u64
)t64s
);
1165 emit_instr(ctx
, slt
, MIPS_R_AT
, dst
, MIPS_R_AT
);
1171 case BPF_JMP
| BPF_JGT
| BPF_K
:
1172 case BPF_JMP
| BPF_JGE
| BPF_K
:
1173 cmp_eq
= (BPF_OP(insn
->code
) == BPF_JGE
);
1174 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg_fp_ok
);
1178 * only "LT" compare available, so we must use imm + 1
1181 t64s
= (u64
)(u32
)(insn
->imm
) + (cmp_eq
? 0 : 1);
1182 if (t64s
>= 0 && t64s
<= S16_MAX
) {
1183 emit_instr(ctx
, sltiu
, MIPS_R_AT
, dst
, (int)t64s
);
1189 emit_const_to_reg(ctx
, MIPS_R_AT
, (u64
)t64s
);
1190 emit_instr(ctx
, sltu
, MIPS_R_AT
, dst
, MIPS_R_AT
);
1196 case BPF_JMP
| BPF_JSET
| BPF_K
: /* JMP_IMM */
1197 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg_fp_ok
);
1201 if (use_bbit_insns() && hweight32((u32
)insn
->imm
) == 1) {
1202 if ((insn
+ 1)->code
== (BPF_JMP
| BPF_EXIT
) && insn
->off
== 1) {
1203 b_off
= b_imm(exit_idx
, ctx
);
1204 if (is_bad_offset(b_off
))
1206 emit_instr(ctx
, bbit0
, dst
, ffs((u32
)insn
->imm
) - 1, b_off
);
1207 emit_instr(ctx
, nop
);
1208 return 2; /* We consumed the exit. */
1210 b_off
= b_imm(this_idx
+ insn
->off
+ 1, ctx
);
1211 if (is_bad_offset(b_off
))
1213 emit_instr(ctx
, bbit1
, dst
, ffs((u32
)insn
->imm
) - 1, b_off
);
1214 emit_instr(ctx
, nop
);
1217 t64
= (u32
)insn
->imm
;
1218 emit_const_to_reg(ctx
, MIPS_R_AT
, t64
);
1219 emit_instr(ctx
, and, MIPS_R_AT
, dst
, MIPS_R_AT
);
1225 case BPF_JMP
| BPF_JA
:
1227 * Prefer relative branch for easier debugging, but
1228 * fall back if needed.
1230 b_off
= b_imm(this_idx
+ insn
->off
+ 1, ctx
);
1231 if (is_bad_offset(b_off
)) {
1232 target
= j_target(ctx
, this_idx
+ insn
->off
+ 1);
1233 if (target
== (unsigned int)-1)
1235 emit_instr(ctx
, j
, target
);
1237 emit_instr(ctx
, b
, b_off
);
1239 emit_instr(ctx
, nop
);
1241 case BPF_LD
| BPF_DW
| BPF_IMM
:
1242 if (insn
->src_reg
!= 0)
1244 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1247 t64
= ((u64
)(u32
)insn
->imm
) | ((u64
)(insn
+ 1)->imm
<< 32);
1248 emit_const_to_reg(ctx
, dst
, t64
);
1249 return 2; /* Double slot insn */
1251 case BPF_JMP
| BPF_CALL
:
1252 ctx
->flags
|= EBPF_SAVE_RA
;
1253 t64s
= (s64
)insn
->imm
+ (s64
)__bpf_call_base
;
1254 emit_const_to_reg(ctx
, MIPS_R_T9
, (u64
)t64s
);
1255 emit_instr(ctx
, jalr
, MIPS_R_RA
, MIPS_R_T9
);
1257 emit_instr(ctx
, nop
);
1260 case BPF_JMP
| BPF_TAIL_CALL
:
1261 if (emit_bpf_tail_call(ctx
, this_idx
))
1265 case BPF_LD
| BPF_B
| BPF_ABS
:
1266 case BPF_LD
| BPF_H
| BPF_ABS
:
1267 case BPF_LD
| BPF_W
| BPF_ABS
:
1268 case BPF_LD
| BPF_DW
| BPF_ABS
:
1269 ctx
->flags
|= EBPF_SAVE_RA
;
1271 gen_imm_to_reg(insn
, MIPS_R_A1
, ctx
);
1272 emit_instr(ctx
, addiu
, MIPS_R_A2
, MIPS_R_ZERO
, size_to_len(insn
));
1274 if (insn
->imm
< 0) {
1275 emit_const_to_reg(ctx
, MIPS_R_T9
, (u64
)bpf_internal_load_pointer_neg_helper
);
1277 emit_const_to_reg(ctx
, MIPS_R_T9
, (u64
)ool_skb_header_pointer
);
1278 emit_instr(ctx
, daddiu
, MIPS_R_A3
, MIPS_R_SP
, ctx
->tmp_offset
);
1282 case BPF_LD
| BPF_B
| BPF_IND
:
1283 case BPF_LD
| BPF_H
| BPF_IND
:
1284 case BPF_LD
| BPF_W
| BPF_IND
:
1285 case BPF_LD
| BPF_DW
| BPF_IND
:
1286 ctx
->flags
|= EBPF_SAVE_RA
;
1287 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg_no_fp
);
1290 ts
= get_reg_val_type(ctx
, this_idx
, insn
->src_reg
);
1291 if (ts
== REG_32BIT_ZERO_EX
) {
1293 emit_instr(ctx
, sll
, MIPS_R_A1
, src
, 0);
1296 if (insn
->imm
>= S16_MIN
&& insn
->imm
<= S16_MAX
) {
1297 emit_instr(ctx
, daddiu
, MIPS_R_A1
, src
, insn
->imm
);
1299 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
1300 emit_instr(ctx
, daddu
, MIPS_R_A1
, MIPS_R_AT
, src
);
1302 /* truncate to 32-bit int */
1303 emit_instr(ctx
, sll
, MIPS_R_A1
, MIPS_R_A1
, 0);
1304 emit_instr(ctx
, daddiu
, MIPS_R_A3
, MIPS_R_SP
, ctx
->tmp_offset
);
1305 emit_instr(ctx
, slt
, MIPS_R_AT
, MIPS_R_A1
, MIPS_R_ZERO
);
1307 emit_const_to_reg(ctx
, MIPS_R_T8
, (u64
)bpf_internal_load_pointer_neg_helper
);
1308 emit_const_to_reg(ctx
, MIPS_R_T9
, (u64
)ool_skb_header_pointer
);
1309 emit_instr(ctx
, addiu
, MIPS_R_A2
, MIPS_R_ZERO
, size_to_len(insn
));
1310 emit_instr(ctx
, movn
, MIPS_R_T9
, MIPS_R_T8
, MIPS_R_AT
);
1313 emit_instr(ctx
, jalr
, MIPS_R_RA
, MIPS_R_T9
);
1314 /* delay slot move */
1315 emit_instr(ctx
, daddu
, MIPS_R_A0
, MIPS_R_S0
, MIPS_R_ZERO
);
1317 /* Check the error value */
1318 b_off
= b_imm(exit_idx
, ctx
);
1319 if (is_bad_offset(b_off
)) {
1320 target
= j_target(ctx
, exit_idx
);
1321 if (target
== (unsigned int)-1)
1324 if (!(ctx
->offsets
[this_idx
] & OFFSETS_B_CONV
)) {
1325 ctx
->offsets
[this_idx
] |= OFFSETS_B_CONV
;
1326 ctx
->long_b_conversion
= 1;
1328 emit_instr(ctx
, bne
, MIPS_R_V0
, MIPS_R_ZERO
, 4 * 3);
1329 emit_instr(ctx
, nop
);
1330 emit_instr(ctx
, j
, target
);
1331 emit_instr(ctx
, nop
);
1333 emit_instr(ctx
, beq
, MIPS_R_V0
, MIPS_R_ZERO
, b_off
);
1334 emit_instr(ctx
, nop
);
1343 switch (BPF_SIZE(insn
->code
)) {
1345 emit_instr(ctx
, lbu
, dst
, 0, MIPS_R_V0
);
1348 emit_instr(ctx
, lhu
, dst
, 0, MIPS_R_V0
);
1350 emit_instr(ctx
, wsbh
, dst
, dst
);
1353 emit_instr(ctx
, lw
, dst
, 0, MIPS_R_V0
);
1355 emit_instr(ctx
, wsbh
, dst
, dst
);
1356 emit_instr(ctx
, rotr
, dst
, dst
, 16);
1360 emit_instr(ctx
, ld
, dst
, 0, MIPS_R_V0
);
1362 emit_instr(ctx
, dsbh
, dst
, dst
);
1363 emit_instr(ctx
, dshd
, dst
, dst
);
1369 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
1370 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
1371 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1374 td
= get_reg_val_type(ctx
, this_idx
, insn
->dst_reg
);
1375 if (insn
->imm
== 64 && td
== REG_32BIT
)
1376 emit_instr(ctx
, dinsu
, dst
, MIPS_R_ZERO
, 32, 32);
1378 if (insn
->imm
!= 64 &&
1379 (td
== REG_64BIT
|| td
== REG_32BIT_ZERO_EX
)) {
1381 emit_instr(ctx
, sll
, dst
, dst
, 0);
1385 need_swap
= (BPF_SRC(insn
->code
) == BPF_FROM_LE
);
1387 need_swap
= (BPF_SRC(insn
->code
) == BPF_FROM_BE
);
1389 if (insn
->imm
== 16) {
1391 emit_instr(ctx
, wsbh
, dst
, dst
);
1392 emit_instr(ctx
, andi
, dst
, dst
, 0xffff);
1393 } else if (insn
->imm
== 32) {
1395 emit_instr(ctx
, wsbh
, dst
, dst
);
1396 emit_instr(ctx
, rotr
, dst
, dst
, 16);
1398 } else { /* 64-bit*/
1400 emit_instr(ctx
, dsbh
, dst
, dst
);
1401 emit_instr(ctx
, dshd
, dst
, dst
);
1406 case BPF_ST
| BPF_B
| BPF_MEM
:
1407 case BPF_ST
| BPF_H
| BPF_MEM
:
1408 case BPF_ST
| BPF_W
| BPF_MEM
:
1409 case BPF_ST
| BPF_DW
| BPF_MEM
:
1410 if (insn
->dst_reg
== BPF_REG_10
) {
1411 ctx
->flags
|= EBPF_SEEN_FP
;
1413 mem_off
= insn
->off
+ MAX_BPF_STACK
;
1415 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1418 mem_off
= insn
->off
;
1420 gen_imm_to_reg(insn
, MIPS_R_AT
, ctx
);
1421 switch (BPF_SIZE(insn
->code
)) {
1423 emit_instr(ctx
, sb
, MIPS_R_AT
, mem_off
, dst
);
1426 emit_instr(ctx
, sh
, MIPS_R_AT
, mem_off
, dst
);
1429 emit_instr(ctx
, sw
, MIPS_R_AT
, mem_off
, dst
);
1432 emit_instr(ctx
, sd
, MIPS_R_AT
, mem_off
, dst
);
1437 case BPF_LDX
| BPF_B
| BPF_MEM
:
1438 case BPF_LDX
| BPF_H
| BPF_MEM
:
1439 case BPF_LDX
| BPF_W
| BPF_MEM
:
1440 case BPF_LDX
| BPF_DW
| BPF_MEM
:
1441 if (insn
->src_reg
== BPF_REG_10
) {
1442 ctx
->flags
|= EBPF_SEEN_FP
;
1444 mem_off
= insn
->off
+ MAX_BPF_STACK
;
1446 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg_no_fp
);
1449 mem_off
= insn
->off
;
1451 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1454 switch (BPF_SIZE(insn
->code
)) {
1456 emit_instr(ctx
, lbu
, dst
, mem_off
, src
);
1459 emit_instr(ctx
, lhu
, dst
, mem_off
, src
);
1462 emit_instr(ctx
, lw
, dst
, mem_off
, src
);
1465 emit_instr(ctx
, ld
, dst
, mem_off
, src
);
1470 case BPF_STX
| BPF_B
| BPF_MEM
:
1471 case BPF_STX
| BPF_H
| BPF_MEM
:
1472 case BPF_STX
| BPF_W
| BPF_MEM
:
1473 case BPF_STX
| BPF_DW
| BPF_MEM
:
1474 case BPF_STX
| BPF_W
| BPF_XADD
:
1475 case BPF_STX
| BPF_DW
| BPF_XADD
:
1476 if (insn
->dst_reg
== BPF_REG_10
) {
1477 ctx
->flags
|= EBPF_SEEN_FP
;
1479 mem_off
= insn
->off
+ MAX_BPF_STACK
;
1481 dst
= ebpf_to_mips_reg(ctx
, insn
, dst_reg
);
1484 mem_off
= insn
->off
;
1486 src
= ebpf_to_mips_reg(ctx
, insn
, src_reg_no_fp
);
1489 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
1490 switch (BPF_SIZE(insn
->code
)) {
1492 if (get_reg_val_type(ctx
, this_idx
, insn
->src_reg
) == REG_32BIT
) {
1493 emit_instr(ctx
, sll
, MIPS_R_AT
, src
, 0);
1496 emit_instr(ctx
, ll
, MIPS_R_T8
, mem_off
, dst
);
1497 emit_instr(ctx
, addu
, MIPS_R_T8
, MIPS_R_T8
, src
);
1498 emit_instr(ctx
, sc
, MIPS_R_T8
, mem_off
, dst
);
1500 * On failure back up to LL (-4
1501 * instructions of 4 bytes each
1503 emit_instr(ctx
, beq
, MIPS_R_T8
, MIPS_R_ZERO
, -4 * 4);
1504 emit_instr(ctx
, nop
);
1507 if (get_reg_val_type(ctx
, this_idx
, insn
->src_reg
) == REG_32BIT
) {
1508 emit_instr(ctx
, daddu
, MIPS_R_AT
, src
, MIPS_R_ZERO
);
1509 emit_instr(ctx
, dinsu
, MIPS_R_AT
, MIPS_R_ZERO
, 32, 32);
1512 emit_instr(ctx
, lld
, MIPS_R_T8
, mem_off
, dst
);
1513 emit_instr(ctx
, daddu
, MIPS_R_T8
, MIPS_R_T8
, src
);
1514 emit_instr(ctx
, scd
, MIPS_R_T8
, mem_off
, dst
);
1515 emit_instr(ctx
, beq
, MIPS_R_T8
, MIPS_R_ZERO
, -4 * 4);
1516 emit_instr(ctx
, nop
);
1519 } else { /* BPF_MEM */
1520 switch (BPF_SIZE(insn
->code
)) {
1522 emit_instr(ctx
, sb
, src
, mem_off
, dst
);
1525 emit_instr(ctx
, sh
, src
, mem_off
, dst
);
1528 emit_instr(ctx
, sw
, src
, mem_off
, dst
);
1531 if (get_reg_val_type(ctx
, this_idx
, insn
->src_reg
) == REG_32BIT
) {
1532 emit_instr(ctx
, daddu
, MIPS_R_AT
, src
, MIPS_R_ZERO
);
1533 emit_instr(ctx
, dinsu
, MIPS_R_AT
, MIPS_R_ZERO
, 32, 32);
1536 emit_instr(ctx
, sd
, src
, mem_off
, dst
);
1543 pr_err("NOT HANDLED %d - (%02x)\n",
1544 this_idx
, (unsigned int)insn
->code
);
1550 #define RVT_VISITED_MASK 0xc000000000000000ull
1551 #define RVT_FALL_THROUGH 0x4000000000000000ull
1552 #define RVT_BRANCH_TAKEN 0x8000000000000000ull
1553 #define RVT_DONE (RVT_FALL_THROUGH | RVT_BRANCH_TAKEN)
1555 static int build_int_body(struct jit_ctx
*ctx
)
1557 const struct bpf_prog
*prog
= ctx
->skf
;
1558 const struct bpf_insn
*insn
;
1561 for (i
= 0; i
< prog
->len
; ) {
1562 insn
= prog
->insnsi
+ i
;
1563 if ((ctx
->reg_val_types
[i
] & RVT_VISITED_MASK
) == 0) {
1564 /* dead instruction, don't emit it. */
1569 if (ctx
->target
== NULL
)
1570 ctx
->offsets
[i
] = (ctx
->offsets
[i
] & OFFSETS_B_CONV
) | (ctx
->idx
* 4);
1572 r
= build_one_insn(insn
, ctx
, i
, prog
->len
);
1577 /* epilogue offset */
1578 if (ctx
->target
== NULL
)
1579 ctx
->offsets
[i
] = ctx
->idx
* 4;
1582 * All exits have an offset of the epilogue, some offsets may
1583 * not have been set due to banch-around threading, so set
1586 if (ctx
->target
== NULL
)
1587 for (i
= 0; i
< prog
->len
; i
++) {
1588 insn
= prog
->insnsi
+ i
;
1589 if (insn
->code
== (BPF_JMP
| BPF_EXIT
))
1590 ctx
->offsets
[i
] = ctx
->idx
* 4;
1595 /* return the last idx processed, or negative for error */
1596 static int reg_val_propagate_range(struct jit_ctx
*ctx
, u64 initial_rvt
,
1597 int start_idx
, bool follow_taken
)
1599 const struct bpf_prog
*prog
= ctx
->skf
;
1600 const struct bpf_insn
*insn
;
1601 u64 exit_rvt
= initial_rvt
;
1602 u64
*rvt
= ctx
->reg_val_types
;
1606 for (idx
= start_idx
; idx
< prog
->len
; idx
++) {
1607 rvt
[idx
] = (rvt
[idx
] & RVT_VISITED_MASK
) | exit_rvt
;
1608 insn
= prog
->insnsi
+ idx
;
1609 switch (BPF_CLASS(insn
->code
)) {
1611 switch (BPF_OP(insn
->code
)) {
1623 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1626 if (BPF_SRC(insn
->code
)) {
1627 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1629 /* IMM to REG move*/
1631 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1633 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1637 if (insn
->imm
== 64)
1638 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1639 else if (insn
->imm
== 32)
1640 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1641 else /* insn->imm == 16 */
1642 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1645 rvt
[idx
] |= RVT_DONE
;
1648 switch (BPF_OP(insn
->code
)) {
1650 if (BPF_SRC(insn
->code
)) {
1651 /* REG to REG move*/
1652 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1654 /* IMM to REG move*/
1656 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1658 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT_32BIT
);
1662 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1664 rvt
[idx
] |= RVT_DONE
;
1667 switch (BPF_SIZE(insn
->code
)) {
1669 if (BPF_MODE(insn
->code
) == BPF_IMM
) {
1672 val
= (s64
)((u32
)insn
->imm
| ((u64
)(insn
+ 1)->imm
<< 32));
1673 if (val
> 0 && val
<= S32_MAX
)
1674 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1675 else if (val
>= S32_MIN
&& val
<= S32_MAX
)
1676 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT_32BIT
);
1678 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1679 rvt
[idx
] |= RVT_DONE
;
1682 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1687 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1690 if (BPF_MODE(insn
->code
) == BPF_IMM
)
1691 set_reg_val_type(&exit_rvt
, insn
->dst_reg
,
1692 insn
->imm
>= 0 ? REG_32BIT_POS
: REG_32BIT
);
1694 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1697 rvt
[idx
] |= RVT_DONE
;
1700 switch (BPF_SIZE(insn
->code
)) {
1702 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_64BIT
);
1706 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT_POS
);
1709 set_reg_val_type(&exit_rvt
, insn
->dst_reg
, REG_32BIT
);
1712 rvt
[idx
] |= RVT_DONE
;
1715 switch (BPF_OP(insn
->code
)) {
1717 rvt
[idx
] = RVT_DONE
| exit_rvt
;
1718 rvt
[prog
->len
] = exit_rvt
;
1721 rvt
[idx
] |= RVT_DONE
;
1732 rvt
[idx
] |= RVT_BRANCH_TAKEN
;
1734 follow_taken
= false;
1736 rvt
[idx
] |= RVT_FALL_THROUGH
;
1740 set_reg_val_type(&exit_rvt
, BPF_REG_0
, REG_64BIT
);
1741 /* Upon call return, argument registers are clobbered. */
1742 for (reg
= BPF_REG_0
; reg
<= BPF_REG_5
; reg
++)
1743 set_reg_val_type(&exit_rvt
, reg
, REG_64BIT
);
1745 rvt
[idx
] |= RVT_DONE
;
1748 WARN(1, "Unhandled BPF_JMP case.\n");
1749 rvt
[idx
] |= RVT_DONE
;
1754 rvt
[idx
] |= RVT_DONE
;
1762 * Track the value range (i.e. 32-bit vs. 64-bit) of each register at
1763 * each eBPF insn. This allows unneeded sign and zero extension
1764 * operations to be omitted.
1766 * Doesn't handle yet confluence of control paths with conflicting
1767 * ranges, but it is good enough for most sane code.
1769 static int reg_val_propagate(struct jit_ctx
*ctx
)
1771 const struct bpf_prog
*prog
= ctx
->skf
;
1777 * 11 registers * 3 bits/reg leaves top bits free for other
1778 * uses. Bit-62..63 used to see if we have visited an insn.
1782 /* Upon entry, argument registers are 64-bit. */
1783 for (reg
= BPF_REG_1
; reg
<= BPF_REG_5
; reg
++)
1784 set_reg_val_type(&exit_rvt
, reg
, REG_64BIT
);
1787 * First follow all conditional branches on the fall-through
1788 * edge of control flow..
1790 reg_val_propagate_range(ctx
, exit_rvt
, 0, false);
1793 * Then repeatedly find the first conditional branch where
1794 * both edges of control flow have not been taken, and follow
1795 * the branch taken edge. We will end up restarting the
1796 * search once per conditional branch insn.
1798 for (i
= 0; i
< prog
->len
; i
++) {
1799 u64 rvt
= ctx
->reg_val_types
[i
];
1801 if ((rvt
& RVT_VISITED_MASK
) == RVT_DONE
||
1802 (rvt
& RVT_VISITED_MASK
) == 0)
1804 if ((rvt
& RVT_VISITED_MASK
) == RVT_FALL_THROUGH
) {
1805 reg_val_propagate_range(ctx
, rvt
& ~RVT_VISITED_MASK
, i
, true);
1806 } else { /* RVT_BRANCH_TAKEN */
1807 WARN(1, "Unexpected RVT_BRANCH_TAKEN case.\n");
1808 reg_val_propagate_range(ctx
, rvt
& ~RVT_VISITED_MASK
, i
, false);
1810 goto restart_search
;
1813 * Eventually all conditional branches have been followed on
1814 * both branches and we are done. Any insn that has not been
1815 * visited at this point is dead.
1821 static void jit_fill_hole(void *area
, unsigned int size
)
1825 /* We are guaranteed to have aligned memory. */
1826 for (p
= area
; size
>= sizeof(u32
); size
-= sizeof(u32
))
1827 uasm_i_break(&p
, BRK_BUG
); /* Increments p */
1830 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*prog
)
1832 struct bpf_prog
*orig_prog
= prog
;
1833 bool tmp_blinded
= false;
1834 struct bpf_prog
*tmp
;
1835 struct bpf_binary_header
*header
= NULL
;
1837 unsigned int image_size
;
1840 if (!bpf_jit_enable
|| !cpu_has_mips64r2
)
1843 tmp
= bpf_jit_blind_constants(prog
);
1844 /* If blinding was requested and we failed during blinding,
1845 * we must fall back to the interpreter.
1854 memset(&ctx
, 0, sizeof(ctx
));
1856 ctx
.offsets
= kcalloc(prog
->len
+ 1, sizeof(*ctx
.offsets
), GFP_KERNEL
);
1857 if (ctx
.offsets
== NULL
)
1860 ctx
.reg_val_types
= kcalloc(prog
->len
+ 1, sizeof(*ctx
.reg_val_types
), GFP_KERNEL
);
1861 if (ctx
.reg_val_types
== NULL
)
1866 if (reg_val_propagate(&ctx
))
1870 * First pass discovers used resources and instruction offsets
1871 * assuming short branches are used.
1873 if (build_int_body(&ctx
))
1877 * If no calls are made (EBPF_SAVE_RA), then tail call count
1878 * in $v1, else we must save in n$s4.
1880 if (ctx
.flags
& EBPF_SEEN_TC
) {
1881 if (ctx
.flags
& EBPF_SAVE_RA
)
1882 ctx
.flags
|= EBPF_SAVE_S4
;
1884 ctx
.flags
|= EBPF_TCC_IN_V1
;
1888 * Second pass generates offsets, if any branches are out of
1889 * range a jump-around long sequence is generated, and we have
1890 * to try again from the beginning to generate the new
1891 * offsets. This is done until no additional conversions are
1896 ctx
.gen_b_offsets
= 1;
1897 ctx
.long_b_conversion
= 0;
1898 if (gen_int_prologue(&ctx
))
1900 if (build_int_body(&ctx
))
1902 if (build_int_epilogue(&ctx
, MIPS_R_RA
))
1904 } while (ctx
.long_b_conversion
);
1906 image_size
= 4 * ctx
.idx
;
1908 header
= bpf_jit_binary_alloc(image_size
, &image_ptr
,
1909 sizeof(u32
), jit_fill_hole
);
1913 ctx
.target
= (u32
*)image_ptr
;
1915 /* Third pass generates the code */
1917 if (gen_int_prologue(&ctx
))
1919 if (build_int_body(&ctx
))
1921 if (build_int_epilogue(&ctx
, MIPS_R_RA
))
1924 /* Update the icache */
1925 flush_icache_range((unsigned long)ctx
.target
,
1926 (unsigned long)(ctx
.target
+ ctx
.idx
* sizeof(u32
)));
1928 if (bpf_jit_enable
> 1)
1930 bpf_jit_dump(prog
->len
, image_size
, 2, ctx
.target
);
1932 bpf_jit_binary_lock_ro(header
);
1933 prog
->bpf_func
= (void *)ctx
.target
;
1935 prog
->jited_len
= image_size
;
1938 bpf_jit_prog_release_other(prog
, prog
== orig_prog
?
1941 kfree(ctx
.reg_val_types
);
1948 bpf_jit_binary_free(header
);