1 From 36366e367ee93ced84fddb8fae6675e12985f5a4 Mon Sep 17 00:00:00 2001
2 From: Paul Burton <paulburton@kernel.org>
3 Date: Thu, 5 Dec 2019 10:23:18 -0800
4 Subject: [PATCH] MIPS: BPF: Restore MIPS32 cBPF JIT
6 Commit 716850ab104d ("MIPS: eBPF: Initial eBPF support for MIPS32
7 architecture.") enabled our eBPF JIT for MIPS32 kernels, whereas it has
8 previously only been availailable for MIPS64. It was my understanding at
9 the time that the BPF test suite was passing & JITing a comparable
10 number of tests to our cBPF JIT [1], but it turns out that was not the
13 The eBPF JIT has a number of problems on MIPS32:
15 - Most notably various code paths still result in emission of MIPS64
16 instructions which will cause reserved instruction exceptions & kernel
17 panics when run on MIPS32 CPUs.
19 - The eBPF JIT doesn't account for differences between the O32 ABI used
20 by MIPS32 kernels versus the N64 ABI used by MIPS64 kernels. Notably
21 arguments beyond the first 4 are passed on the stack in O32, and this
22 is entirely unhandled when JITing a BPF_CALL instruction. Stack space
23 must be reserved for arguments even if they all fit in registers, and
24 the callee is free to assume that stack space has been reserved for
25 its use - with the eBPF JIT this is not the case, so calling any
26 function can result in clobbering values on the stack & unpredictable
27 behaviour. Function arguments in eBPF are always 64-bit values which
28 is also entirely unhandled - the JIT still uses a single (32-bit)
29 register per argument. As a result all function arguments are always
30 passed incorrectly when JITing a BPF_CALL instruction, leading to
31 kernel crashes or strange behavior.
33 - The JIT attempts to bail our on use of ALU64 instructions or 64-bit
34 memory access instructions. The code doing this at the start of
35 build_one_insn() incorrectly checks whether BPF_OP() equals BPF_DW,
36 when it should really be checking BPF_SIZE() & only doing so when
37 BPF_CLASS() is one of BPF_{LD,LDX,ST,STX}. This results in false
38 positives that cause more bailouts than intended, and that in turns
39 hides some of the problems described above.
41 - The kernel's cBPF->eBPF translation makes heavy use of 64-bit eBPF
42 instructions that the MIPS32 eBPF JIT bails out on, leading to most
43 cBPF programs not being JITed at all.
45 Until these problems are resolved, revert the removal of the cBPF JIT
46 performed by commit 716850ab104d ("MIPS: eBPF: Initial eBPF support for
47 MIPS32 architecture."). Together with commit f8fffebdea75 ("MIPS: BPF:
48 Disable MIPS32 eBPF JIT") this restores MIPS32 BPF JIT behavior back to
49 the same state it was prior to the introduction of the broken eBPF JIT
52 [1] https://lore.kernel.org/linux-mips/MWHPR2201MB13583388481F01A422CE7D66D4410@MWHPR2201MB1358.namprd22.prod.outlook.com/
54 Signed-off-by: Paul Burton <paulburton@kernel.org>
55 Fixes: 716850ab104d ("MIPS: eBPF: Initial eBPF support for MIPS32 architecture.")
56 Cc: Daniel Borkmann <daniel@iogearbox.net>
57 Cc: Hassan Naveed <hnaveed@wavecomp.com>
58 Cc: Tony Ambardar <itugrok@yahoo.com>
59 Cc: bpf@vger.kernel.org
60 Cc: netdev@vger.kernel.org
61 Cc: linux-mips@vger.kernel.org
62 Cc: linux-kernel@vger.kernel.org
64 arch/mips/Kconfig | 1 +
65 arch/mips/net/Makefile | 1 +
66 arch/mips/net/bpf_jit.c | 1270 +++++++++++++++++++++++++++++++++++
67 arch/mips/net/bpf_jit_asm.S | 285 ++++++++
68 4 files changed, 1557 insertions(+)
69 create mode 100644 arch/mips/net/bpf_jit.c
70 create mode 100644 arch/mips/net/bpf_jit_asm.S
72 --- a/arch/mips/Kconfig
73 +++ b/arch/mips/Kconfig
74 @@ -46,6 +46,7 @@ config MIPS
75 select HAVE_ARCH_TRACEHOOK
76 select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES
77 select HAVE_ASM_MODVERSIONS
78 + select HAVE_CBPF_JIT if !64BIT && !CPU_MICROMIPS
79 select HAVE_EBPF_JIT if 64BIT && !CPU_MICROMIPS && TARGET_ISA_REV >= 2
80 select HAVE_CONTEXT_TRACKING
81 select HAVE_COPY_THREAD_TLS
82 --- a/arch/mips/net/Makefile
83 +++ b/arch/mips/net/Makefile
85 # SPDX-License-Identifier: GPL-2.0-only
86 # MIPS networking code
88 +obj-$(CONFIG_MIPS_CBPF_JIT) += bpf_jit.o bpf_jit_asm.o
89 obj-$(CONFIG_MIPS_EBPF_JIT) += ebpf_jit.o
91 +++ b/arch/mips/net/bpf_jit.c
94 + * Just-In-Time compiler for BPF filters on MIPS
96 + * Copyright (c) 2014 Imagination Technologies Ltd.
97 + * Author: Markos Chandras <markos.chandras@imgtec.com>
99 + * This program is free software; you can redistribute it and/or modify it
100 + * under the terms of the GNU General Public License as published by the
101 + * Free Software Foundation; version 2 of the License.
104 +#include <linux/bitops.h>
105 +#include <linux/compiler.h>
106 +#include <linux/errno.h>
107 +#include <linux/filter.h>
108 +#include <linux/if_vlan.h>
109 +#include <linux/moduleloader.h>
110 +#include <linux/netdevice.h>
111 +#include <linux/string.h>
112 +#include <linux/slab.h>
113 +#include <linux/types.h>
114 +#include <asm/asm.h>
115 +#include <asm/bitops.h>
116 +#include <asm/cacheflush.h>
117 +#include <asm/cpu-features.h>
118 +#include <asm/uasm.h>
120 +#include "bpf_jit.h"
123 + * r_skb_hl SKB header length
124 + * r_data SKB data pointer
126 + * r_A BPF register A
127 + * r_X BPF register X
129 + * r_M *scratch memory
130 + * r_skb_len SKB length
132 + * On entry (*bpf_func)(*skb, *filter)
133 + * a0 = MIPS_R_A0 = skb;
134 + * a1 = MIPS_R_A1 = filter;
146 + * saved reg 0 <-- r_sp
147 + * <no argument area>
151 + * <--------------------- len ------------------------>
152 + * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
153 + * ----------------------------------------------------
155 + * ----------------------------------------------------
158 +#define ptr typeof(unsigned long)
160 +#define SCRATCH_OFF(k) (4 * (k))
163 +#define SEEN_CALL (1 << BPF_MEMWORDS)
164 +#define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
165 +#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
166 +#define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
167 +#define SEEN_OFF SEEN_SREG(2)
168 +#define SEEN_A SEEN_SREG(3)
169 +#define SEEN_X SEEN_SREG(4)
170 +#define SEEN_SKB SEEN_SREG(5)
171 +#define SEEN_MEM SEEN_SREG(6)
172 +/* SEEN_SK_DATA also implies skb_hl an skb_len */
173 +#define SEEN_SKB_DATA (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0))
175 +/* Arguments used by JIT */
176 +#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
178 +#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
181 + * struct jit_ctx - JIT context
182 + * @skf: The sk_filter
183 + * @prologue_bytes: Number of bytes for prologue
184 + * @idx: Instruction index
185 + * @flags: JIT flags
186 + * @offsets: Instruction offsets
187 + * @target: Memory location for the compiled filter
190 + const struct bpf_prog *skf;
191 + unsigned int prologue_bytes;
199 +static inline int optimize_div(u32 *k)
201 + /* power of 2 divides can be implemented with right shift */
202 + if (!(*k & (*k-1))) {
210 +static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
212 +/* Simply emit the instruction if the JIT memory space has been allocated */
213 +#define emit_instr(ctx, func, ...) \
215 + if ((ctx)->target != NULL) { \
216 + u32 *p = &(ctx)->target[ctx->idx]; \
217 + uasm_i_##func(&p, ##__VA_ARGS__); \
223 + * Similar to emit_instr but it must be used when we need to emit
224 + * 32-bit or 64-bit instructions
226 +#define emit_long_instr(ctx, func, ...) \
228 + if ((ctx)->target != NULL) { \
229 + u32 *p = &(ctx)->target[ctx->idx]; \
230 + UASM_i_##func(&p, ##__VA_ARGS__); \
235 +/* Determine if immediate is within the 16-bit signed range */
236 +static inline bool is_range16(s32 imm)
238 + return !(imm >= SBIT(15) || imm < -SBIT(15));
241 +static inline void emit_addu(unsigned int dst, unsigned int src1,
242 + unsigned int src2, struct jit_ctx *ctx)
244 + emit_instr(ctx, addu, dst, src1, src2);
247 +static inline void emit_nop(struct jit_ctx *ctx)
249 + emit_instr(ctx, nop);
252 +/* Load a u32 immediate to a register */
253 +static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
255 + if (ctx->target != NULL) {
256 + /* addiu can only handle s16 */
257 + if (!is_range16(imm)) {
258 + u32 *p = &ctx->target[ctx->idx];
259 + uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
260 + p = &ctx->target[ctx->idx + 1];
261 + uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
263 + u32 *p = &ctx->target[ctx->idx];
264 + uasm_i_addiu(&p, dst, r_zero, imm);
269 + if (!is_range16(imm))
273 +static inline void emit_or(unsigned int dst, unsigned int src1,
274 + unsigned int src2, struct jit_ctx *ctx)
276 + emit_instr(ctx, or, dst, src1, src2);
279 +static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
280 + struct jit_ctx *ctx)
282 + if (imm >= BIT(16)) {
283 + emit_load_imm(r_tmp, imm, ctx);
284 + emit_or(dst, src, r_tmp, ctx);
286 + emit_instr(ctx, ori, dst, src, imm);
290 +static inline void emit_daddiu(unsigned int dst, unsigned int src,
291 + int imm, struct jit_ctx *ctx)
294 + * Only used for stack, so the imm is relatively small
295 + * and it fits in 15-bits
297 + emit_instr(ctx, daddiu, dst, src, imm);
300 +static inline void emit_addiu(unsigned int dst, unsigned int src,
301 + u32 imm, struct jit_ctx *ctx)
303 + if (!is_range16(imm)) {
304 + emit_load_imm(r_tmp, imm, ctx);
305 + emit_addu(dst, r_tmp, src, ctx);
307 + emit_instr(ctx, addiu, dst, src, imm);
311 +static inline void emit_and(unsigned int dst, unsigned int src1,
312 + unsigned int src2, struct jit_ctx *ctx)
314 + emit_instr(ctx, and, dst, src1, src2);
317 +static inline void emit_andi(unsigned int dst, unsigned int src,
318 + u32 imm, struct jit_ctx *ctx)
320 + /* If imm does not fit in u16 then load it to register */
321 + if (imm >= BIT(16)) {
322 + emit_load_imm(r_tmp, imm, ctx);
323 + emit_and(dst, src, r_tmp, ctx);
325 + emit_instr(ctx, andi, dst, src, imm);
329 +static inline void emit_xor(unsigned int dst, unsigned int src1,
330 + unsigned int src2, struct jit_ctx *ctx)
332 + emit_instr(ctx, xor, dst, src1, src2);
335 +static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
337 + /* If imm does not fit in u16 then load it to register */
338 + if (imm >= BIT(16)) {
339 + emit_load_imm(r_tmp, imm, ctx);
340 + emit_xor(dst, src, r_tmp, ctx);
342 + emit_instr(ctx, xori, dst, src, imm);
346 +static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
348 + emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
351 +static inline void emit_subu(unsigned int dst, unsigned int src1,
352 + unsigned int src2, struct jit_ctx *ctx)
354 + emit_instr(ctx, subu, dst, src1, src2);
357 +static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
359 + emit_subu(reg, r_zero, reg, ctx);
362 +static inline void emit_sllv(unsigned int dst, unsigned int src,
363 + unsigned int sa, struct jit_ctx *ctx)
365 + emit_instr(ctx, sllv, dst, src, sa);
368 +static inline void emit_sll(unsigned int dst, unsigned int src,
369 + unsigned int sa, struct jit_ctx *ctx)
371 + /* sa is 5-bits long */
373 + /* Shifting >= 32 results in zero */
374 + emit_jit_reg_move(dst, r_zero, ctx);
376 + emit_instr(ctx, sll, dst, src, sa);
379 +static inline void emit_srlv(unsigned int dst, unsigned int src,
380 + unsigned int sa, struct jit_ctx *ctx)
382 + emit_instr(ctx, srlv, dst, src, sa);
385 +static inline void emit_srl(unsigned int dst, unsigned int src,
386 + unsigned int sa, struct jit_ctx *ctx)
388 + /* sa is 5-bits long */
390 + /* Shifting >= 32 results in zero */
391 + emit_jit_reg_move(dst, r_zero, ctx);
393 + emit_instr(ctx, srl, dst, src, sa);
396 +static inline void emit_slt(unsigned int dst, unsigned int src1,
397 + unsigned int src2, struct jit_ctx *ctx)
399 + emit_instr(ctx, slt, dst, src1, src2);
402 +static inline void emit_sltu(unsigned int dst, unsigned int src1,
403 + unsigned int src2, struct jit_ctx *ctx)
405 + emit_instr(ctx, sltu, dst, src1, src2);
408 +static inline void emit_sltiu(unsigned dst, unsigned int src,
409 + unsigned int imm, struct jit_ctx *ctx)
411 + /* 16 bit immediate */
412 + if (!is_range16((s32)imm)) {
413 + emit_load_imm(r_tmp, imm, ctx);
414 + emit_sltu(dst, src, r_tmp, ctx);
416 + emit_instr(ctx, sltiu, dst, src, imm);
421 +/* Store register on the stack */
422 +static inline void emit_store_stack_reg(ptr reg, ptr base,
423 + unsigned int offset,
424 + struct jit_ctx *ctx)
426 + emit_long_instr(ctx, SW, reg, offset, base);
429 +static inline void emit_store(ptr reg, ptr base, unsigned int offset,
430 + struct jit_ctx *ctx)
432 + emit_instr(ctx, sw, reg, offset, base);
435 +static inline void emit_load_stack_reg(ptr reg, ptr base,
436 + unsigned int offset,
437 + struct jit_ctx *ctx)
439 + emit_long_instr(ctx, LW, reg, offset, base);
442 +static inline void emit_load(unsigned int reg, unsigned int base,
443 + unsigned int offset, struct jit_ctx *ctx)
445 + emit_instr(ctx, lw, reg, offset, base);
448 +static inline void emit_load_byte(unsigned int reg, unsigned int base,
449 + unsigned int offset, struct jit_ctx *ctx)
451 + emit_instr(ctx, lb, reg, offset, base);
454 +static inline void emit_half_load(unsigned int reg, unsigned int base,
455 + unsigned int offset, struct jit_ctx *ctx)
457 + emit_instr(ctx, lh, reg, offset, base);
460 +static inline void emit_half_load_unsigned(unsigned int reg, unsigned int base,
461 + unsigned int offset, struct jit_ctx *ctx)
463 + emit_instr(ctx, lhu, reg, offset, base);
466 +static inline void emit_mul(unsigned int dst, unsigned int src1,
467 + unsigned int src2, struct jit_ctx *ctx)
469 + emit_instr(ctx, mul, dst, src1, src2);
472 +static inline void emit_div(unsigned int dst, unsigned int src,
473 + struct jit_ctx *ctx)
475 + if (ctx->target != NULL) {
476 + u32 *p = &ctx->target[ctx->idx];
477 + uasm_i_divu(&p, dst, src);
478 + p = &ctx->target[ctx->idx + 1];
479 + uasm_i_mflo(&p, dst);
481 + ctx->idx += 2; /* 2 insts */
484 +static inline void emit_mod(unsigned int dst, unsigned int src,
485 + struct jit_ctx *ctx)
487 + if (ctx->target != NULL) {
488 + u32 *p = &ctx->target[ctx->idx];
489 + uasm_i_divu(&p, dst, src);
490 + p = &ctx->target[ctx->idx + 1];
491 + uasm_i_mfhi(&p, dst);
493 + ctx->idx += 2; /* 2 insts */
496 +static inline void emit_dsll(unsigned int dst, unsigned int src,
497 + unsigned int sa, struct jit_ctx *ctx)
499 + emit_instr(ctx, dsll, dst, src, sa);
502 +static inline void emit_dsrl32(unsigned int dst, unsigned int src,
503 + unsigned int sa, struct jit_ctx *ctx)
505 + emit_instr(ctx, dsrl32, dst, src, sa);
508 +static inline void emit_wsbh(unsigned int dst, unsigned int src,
509 + struct jit_ctx *ctx)
511 + emit_instr(ctx, wsbh, dst, src);
514 +/* load pointer to register */
515 +static inline void emit_load_ptr(unsigned int dst, unsigned int src,
516 + int imm, struct jit_ctx *ctx)
518 + /* src contains the base addr of the 32/64-pointer */
519 + emit_long_instr(ctx, LW, dst, imm, src);
522 +/* load a function pointer to register */
523 +static inline void emit_load_func(unsigned int reg, ptr imm,
524 + struct jit_ctx *ctx)
526 + if (IS_ENABLED(CONFIG_64BIT)) {
527 + /* At this point imm is always 64-bit */
528 + emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
529 + emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
530 + emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
531 + emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
532 + emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
534 + emit_load_imm(reg, imm, ctx);
538 +/* Move to real MIPS register */
539 +static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
541 + emit_long_instr(ctx, ADDU, dst, src, r_zero);
544 +/* Move to JIT (32-bit) register */
545 +static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
547 + emit_addu(dst, src, r_zero, ctx);
550 +/* Compute the immediate value for PC-relative branches. */
551 +static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
553 + if (ctx->target == NULL)
557 + * We want a pc-relative branch. We only do forward branches
558 + * so tgt is always after pc. tgt is the instruction offset
559 + * we want to jump to.
562 + * I: target_offset <- sign_extend(offset)
563 + * I+1: PC += target_offset (delay slot)
565 + * ctx->idx currently points to the branch instruction
566 + * but the offset is added to the delay slot so we need
569 + return ctx->offsets[tgt] -
570 + (ctx->idx * 4 - ctx->prologue_bytes) - 4;
573 +static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
574 + unsigned int imm, struct jit_ctx *ctx)
576 + if (ctx->target != NULL) {
577 + u32 *p = &ctx->target[ctx->idx];
581 + uasm_i_beq(&p, reg1, reg2, imm);
584 + uasm_i_bne(&p, reg1, reg2, imm);
586 + case MIPS_COND_ALL:
590 + pr_warn("%s: Unhandled branch conditional: %d\n",
597 +static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
599 + emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
602 +static inline void emit_jalr(unsigned int link, unsigned int reg,
603 + struct jit_ctx *ctx)
605 + emit_instr(ctx, jalr, link, reg);
608 +static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
610 + emit_instr(ctx, jr, reg);
613 +static inline u16 align_sp(unsigned int num)
615 + /* Double word alignment for 32-bit, quadword for 64-bit */
616 + unsigned int align = IS_ENABLED(CONFIG_64BIT) ? 16 : 8;
617 + num = (num + (align - 1)) & -align;
621 +static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
623 + int i = 0, real_off = 0;
624 + u32 sflags, tmp_flags;
626 + /* Adjust the stack pointer */
628 + emit_stack_offset(-align_sp(offset), ctx);
630 + tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
631 + /* sflags is essentially a bitmap */
632 + while (tmp_flags) {
633 + if ((sflags >> i) & 0x1) {
634 + emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
642 + /* save return address */
643 + if (ctx->flags & SEEN_CALL) {
644 + emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
648 + /* Setup r_M leaving the alignment gap if necessary */
649 + if (ctx->flags & SEEN_MEM) {
650 + if (real_off % (SZREG * 2))
652 + emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
656 +static void restore_bpf_jit_regs(struct jit_ctx *ctx,
657 + unsigned int offset)
659 + int i, real_off = 0;
660 + u32 sflags, tmp_flags;
662 + tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
663 + /* sflags is a bitmap */
665 + while (tmp_flags) {
666 + if ((sflags >> i) & 0x1) {
667 + emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
675 + /* restore return address */
676 + if (ctx->flags & SEEN_CALL)
677 + emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
679 + /* Restore the sp and discard the scrach memory */
681 + emit_stack_offset(align_sp(offset), ctx);
684 +static unsigned int get_stack_depth(struct jit_ctx *ctx)
689 + /* How may s* regs do we need to preserved? */
690 + sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
692 + if (ctx->flags & SEEN_MEM)
693 + sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
695 + if (ctx->flags & SEEN_CALL)
696 + sp_off += SZREG; /* Space for our ra register */
701 +static void build_prologue(struct jit_ctx *ctx)
705 + /* Calculate the total offset for the stack pointer */
706 + sp_off = get_stack_depth(ctx);
707 + save_bpf_jit_regs(ctx, sp_off);
709 + if (ctx->flags & SEEN_SKB)
710 + emit_reg_move(r_skb, MIPS_R_A0, ctx);
712 + if (ctx->flags & SEEN_SKB_DATA) {
713 + /* Load packet length */
714 + emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
716 + emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
718 + /* Load the data pointer */
719 + emit_load_ptr(r_skb_data, r_skb,
720 + offsetof(struct sk_buff, data), ctx);
721 + /* Load the header length */
722 + emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
725 + if (ctx->flags & SEEN_X)
726 + emit_jit_reg_move(r_X, r_zero, ctx);
729 + * Do not leak kernel data to userspace, we only need to clear
730 + * r_A if it is ever used. In fact if it is never used, we
731 + * will not save/restore it, so clearing it in this case would
732 + * corrupt the state of the caller.
734 + if (bpf_needs_clear_a(&ctx->skf->insns[0]) &&
735 + (ctx->flags & SEEN_A))
736 + emit_jit_reg_move(r_A, r_zero, ctx);
739 +static void build_epilogue(struct jit_ctx *ctx)
741 + unsigned int sp_off;
743 + /* Calculate the total offset for the stack pointer */
745 + sp_off = get_stack_depth(ctx);
746 + restore_bpf_jit_regs(ctx, sp_off);
749 + emit_jr(r_ra, ctx);
753 +#define CHOOSE_LOAD_FUNC(K, func) \
754 + ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
757 +static int build_body(struct jit_ctx *ctx)
759 + const struct bpf_prog *prog = ctx->skf;
760 + const struct sock_filter *inst;
761 + unsigned int i, off, condt;
762 + u32 k, b_off __maybe_unused;
763 + u8 (*sk_load_func)(unsigned long *skb, int offset);
765 + for (i = 0; i < prog->len; i++) {
768 + inst = &(prog->insns[i]);
769 + pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
770 + __func__, inst->code, inst->jt, inst->jf, inst->k);
772 + code = bpf_anc_helper(inst);
774 + if (ctx->target == NULL)
775 + ctx->offsets[i] = ctx->idx * 4;
778 + case BPF_LD | BPF_IMM:
779 + /* A <- k ==> li r_A, k */
780 + ctx->flags |= SEEN_A;
781 + emit_load_imm(r_A, k, ctx);
783 + case BPF_LD | BPF_W | BPF_LEN:
784 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
785 + /* A <- len ==> lw r_A, offset(skb) */
786 + ctx->flags |= SEEN_SKB | SEEN_A;
787 + off = offsetof(struct sk_buff, len);
788 + emit_load(r_A, r_skb, off, ctx);
790 + case BPF_LD | BPF_MEM:
791 + /* A <- M[k] ==> lw r_A, offset(M) */
792 + ctx->flags |= SEEN_MEM | SEEN_A;
793 + emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
795 + case BPF_LD | BPF_W | BPF_ABS:
797 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
799 + case BPF_LD | BPF_H | BPF_ABS:
801 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
803 + case BPF_LD | BPF_B | BPF_ABS:
805 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
807 + emit_load_imm(r_off, k, ctx);
809 + ctx->flags |= SEEN_CALL | SEEN_OFF |
810 + SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
812 + emit_load_func(r_s0, (ptr)sk_load_func, ctx);
813 + emit_reg_move(MIPS_R_A0, r_skb, ctx);
814 + emit_jalr(MIPS_R_RA, r_s0, ctx);
815 + /* Load second argument to delay slot */
816 + emit_reg_move(MIPS_R_A1, r_off, ctx);
817 + /* Check the error value */
818 + emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
820 + /* Load return register on DS for failures */
821 + emit_reg_move(r_ret, r_zero, ctx);
822 + /* Return with error */
823 + emit_b(b_imm(prog->len, ctx), ctx);
826 + case BPF_LD | BPF_W | BPF_IND:
827 + /* A <- P[X + k:4] */
828 + sk_load_func = sk_load_word;
830 + case BPF_LD | BPF_H | BPF_IND:
831 + /* A <- P[X + k:2] */
832 + sk_load_func = sk_load_half;
834 + case BPF_LD | BPF_B | BPF_IND:
835 + /* A <- P[X + k:1] */
836 + sk_load_func = sk_load_byte;
838 + ctx->flags |= SEEN_OFF | SEEN_X;
839 + emit_addiu(r_off, r_X, k, ctx);
841 + case BPF_LDX | BPF_IMM:
843 + ctx->flags |= SEEN_X;
844 + emit_load_imm(r_X, k, ctx);
846 + case BPF_LDX | BPF_MEM:
848 + ctx->flags |= SEEN_X | SEEN_MEM;
849 + emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
851 + case BPF_LDX | BPF_W | BPF_LEN:
853 + ctx->flags |= SEEN_X | SEEN_SKB;
854 + off = offsetof(struct sk_buff, len);
855 + emit_load(r_X, r_skb, off, ctx);
857 + case BPF_LDX | BPF_B | BPF_MSH:
858 + /* X <- 4 * (P[k:1] & 0xf) */
859 + ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
860 + /* Load offset to a1 */
861 + emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
863 + * This may emit two instructions so it may not fit
864 + * in the delay slot. So use a0 in the delay slot.
866 + emit_load_imm(MIPS_R_A1, k, ctx);
867 + emit_jalr(MIPS_R_RA, r_s0, ctx);
868 + emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
869 + /* Check the error value */
870 + emit_bcond(MIPS_COND_NE, r_ret, 0,
871 + b_imm(prog->len, ctx), ctx);
872 + emit_reg_move(r_ret, r_zero, ctx);
874 + /* X <- P[1:K] & 0xf */
875 + emit_andi(r_X, r_A, 0xf, ctx);
877 + emit_b(b_imm(i + 1, ctx), ctx);
878 + emit_sll(r_X, r_X, 2, ctx); /* delay slot */
882 + ctx->flags |= SEEN_MEM | SEEN_A;
883 + emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
887 + ctx->flags |= SEEN_MEM | SEEN_X;
888 + emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
890 + case BPF_ALU | BPF_ADD | BPF_K:
892 + ctx->flags |= SEEN_A;
893 + emit_addiu(r_A, r_A, k, ctx);
895 + case BPF_ALU | BPF_ADD | BPF_X:
897 + ctx->flags |= SEEN_A | SEEN_X;
898 + emit_addu(r_A, r_A, r_X, ctx);
900 + case BPF_ALU | BPF_SUB | BPF_K:
902 + ctx->flags |= SEEN_A;
903 + emit_addiu(r_A, r_A, -k, ctx);
905 + case BPF_ALU | BPF_SUB | BPF_X:
907 + ctx->flags |= SEEN_A | SEEN_X;
908 + emit_subu(r_A, r_A, r_X, ctx);
910 + case BPF_ALU | BPF_MUL | BPF_K:
912 + /* Load K to scratch register before MUL */
913 + ctx->flags |= SEEN_A;
914 + emit_load_imm(r_s0, k, ctx);
915 + emit_mul(r_A, r_A, r_s0, ctx);
917 + case BPF_ALU | BPF_MUL | BPF_X:
919 + ctx->flags |= SEEN_A | SEEN_X;
920 + emit_mul(r_A, r_A, r_X, ctx);
922 + case BPF_ALU | BPF_DIV | BPF_K:
926 + if (optimize_div(&k)) {
927 + ctx->flags |= SEEN_A;
928 + emit_srl(r_A, r_A, k, ctx);
931 + ctx->flags |= SEEN_A;
932 + emit_load_imm(r_s0, k, ctx);
933 + emit_div(r_A, r_s0, ctx);
935 + case BPF_ALU | BPF_MOD | BPF_K:
938 + ctx->flags |= SEEN_A;
939 + emit_jit_reg_move(r_A, r_zero, ctx);
941 + ctx->flags |= SEEN_A;
942 + emit_load_imm(r_s0, k, ctx);
943 + emit_mod(r_A, r_s0, ctx);
946 + case BPF_ALU | BPF_DIV | BPF_X:
948 + ctx->flags |= SEEN_X | SEEN_A;
949 + /* Check if r_X is zero */
950 + emit_bcond(MIPS_COND_EQ, r_X, r_zero,
951 + b_imm(prog->len, ctx), ctx);
952 + emit_load_imm(r_ret, 0, ctx); /* delay slot */
953 + emit_div(r_A, r_X, ctx);
955 + case BPF_ALU | BPF_MOD | BPF_X:
957 + ctx->flags |= SEEN_X | SEEN_A;
958 + /* Check if r_X is zero */
959 + emit_bcond(MIPS_COND_EQ, r_X, r_zero,
960 + b_imm(prog->len, ctx), ctx);
961 + emit_load_imm(r_ret, 0, ctx); /* delay slot */
962 + emit_mod(r_A, r_X, ctx);
964 + case BPF_ALU | BPF_OR | BPF_K:
966 + ctx->flags |= SEEN_A;
967 + emit_ori(r_A, r_A, k, ctx);
969 + case BPF_ALU | BPF_OR | BPF_X:
971 + ctx->flags |= SEEN_A;
972 + emit_ori(r_A, r_A, r_X, ctx);
974 + case BPF_ALU | BPF_XOR | BPF_K:
976 + ctx->flags |= SEEN_A;
977 + emit_xori(r_A, r_A, k, ctx);
979 + case BPF_ANC | SKF_AD_ALU_XOR_X:
980 + case BPF_ALU | BPF_XOR | BPF_X:
982 + ctx->flags |= SEEN_A;
983 + emit_xor(r_A, r_A, r_X, ctx);
985 + case BPF_ALU | BPF_AND | BPF_K:
987 + ctx->flags |= SEEN_A;
988 + emit_andi(r_A, r_A, k, ctx);
990 + case BPF_ALU | BPF_AND | BPF_X:
992 + ctx->flags |= SEEN_A | SEEN_X;
993 + emit_and(r_A, r_A, r_X, ctx);
995 + case BPF_ALU | BPF_LSH | BPF_K:
997 + ctx->flags |= SEEN_A;
998 + emit_sll(r_A, r_A, k, ctx);
1000 + case BPF_ALU | BPF_LSH | BPF_X:
1002 + ctx->flags |= SEEN_A | SEEN_X;
1003 + emit_sllv(r_A, r_A, r_X, ctx);
1005 + case BPF_ALU | BPF_RSH | BPF_K:
1007 + ctx->flags |= SEEN_A;
1008 + emit_srl(r_A, r_A, k, ctx);
1010 + case BPF_ALU | BPF_RSH | BPF_X:
1011 + ctx->flags |= SEEN_A | SEEN_X;
1012 + emit_srlv(r_A, r_A, r_X, ctx);
1014 + case BPF_ALU | BPF_NEG:
1016 + ctx->flags |= SEEN_A;
1017 + emit_neg(r_A, ctx);
1019 + case BPF_JMP | BPF_JA:
1021 + emit_b(b_imm(i + k + 1, ctx), ctx);
1024 + case BPF_JMP | BPF_JEQ | BPF_K:
1025 + /* pc += ( A == K ) ? pc->jt : pc->jf */
1026 + condt = MIPS_COND_EQ | MIPS_COND_K;
1028 + case BPF_JMP | BPF_JEQ | BPF_X:
1029 + ctx->flags |= SEEN_X;
1030 + /* pc += ( A == X ) ? pc->jt : pc->jf */
1031 + condt = MIPS_COND_EQ | MIPS_COND_X;
1033 + case BPF_JMP | BPF_JGE | BPF_K:
1034 + /* pc += ( A >= K ) ? pc->jt : pc->jf */
1035 + condt = MIPS_COND_GE | MIPS_COND_K;
1037 + case BPF_JMP | BPF_JGE | BPF_X:
1038 + ctx->flags |= SEEN_X;
1039 + /* pc += ( A >= X ) ? pc->jt : pc->jf */
1040 + condt = MIPS_COND_GE | MIPS_COND_X;
1042 + case BPF_JMP | BPF_JGT | BPF_K:
1043 + /* pc += ( A > K ) ? pc->jt : pc->jf */
1044 + condt = MIPS_COND_GT | MIPS_COND_K;
1046 + case BPF_JMP | BPF_JGT | BPF_X:
1047 + ctx->flags |= SEEN_X;
1048 + /* pc += ( A > X ) ? pc->jt : pc->jf */
1049 + condt = MIPS_COND_GT | MIPS_COND_X;
1051 + /* Greater or Equal */
1052 + if ((condt & MIPS_COND_GE) ||
1053 + (condt & MIPS_COND_GT)) {
1054 + if (condt & MIPS_COND_K) { /* K */
1055 + ctx->flags |= SEEN_A;
1056 + emit_sltiu(r_s0, r_A, k, ctx);
1058 + ctx->flags |= SEEN_A |
1060 + emit_sltu(r_s0, r_A, r_X, ctx);
1062 + /* A < (K|X) ? r_scrach = 1 */
1063 + b_off = b_imm(i + inst->jf + 1, ctx);
1064 + emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
1067 + /* A > (K|X) ? scratch = 0 */
1068 + if (condt & MIPS_COND_GT) {
1069 + /* Checking for equality */
1070 + ctx->flags |= SEEN_A | SEEN_X;
1071 + if (condt & MIPS_COND_K)
1072 + emit_load_imm(r_s0, k, ctx);
1074 + emit_jit_reg_move(r_s0, r_X,
1076 + b_off = b_imm(i + inst->jf + 1, ctx);
1077 + emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1080 + /* Finally, A > K|X */
1081 + b_off = b_imm(i + inst->jt + 1, ctx);
1082 + emit_b(b_off, ctx);
1085 + /* A >= (K|X) so jump */
1086 + b_off = b_imm(i + inst->jt + 1, ctx);
1087 + emit_b(b_off, ctx);
1092 + if (condt & MIPS_COND_K) { /* K */
1093 + ctx->flags |= SEEN_A;
1094 + emit_load_imm(r_s0, k, ctx);
1096 + b_off = b_imm(i + inst->jt + 1, ctx);
1097 + emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1101 + b_off = b_imm(i + inst->jf + 1,
1103 + emit_bcond(MIPS_COND_NE, r_A, r_s0,
1108 + ctx->flags |= SEEN_A | SEEN_X;
1109 + b_off = b_imm(i + inst->jt + 1,
1111 + emit_bcond(MIPS_COND_EQ, r_A, r_X,
1115 + b_off = b_imm(i + inst->jf + 1, ctx);
1116 + emit_bcond(MIPS_COND_NE, r_A, r_X,
1122 + case BPF_JMP | BPF_JSET | BPF_K:
1123 + ctx->flags |= SEEN_A;
1124 + /* pc += (A & K) ? pc -> jt : pc -> jf */
1125 + emit_load_imm(r_s1, k, ctx);
1126 + emit_and(r_s0, r_A, r_s1, ctx);
1128 + b_off = b_imm(i + inst->jt + 1, ctx);
1129 + emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1132 + b_off = b_imm(i + inst->jf + 1, ctx);
1133 + emit_b(b_off, ctx);
1136 + case BPF_JMP | BPF_JSET | BPF_X:
1137 + ctx->flags |= SEEN_X | SEEN_A;
1138 + /* pc += (A & X) ? pc -> jt : pc -> jf */
1139 + emit_and(r_s0, r_A, r_X, ctx);
1141 + b_off = b_imm(i + inst->jt + 1, ctx);
1142 + emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1145 + b_off = b_imm(i + inst->jf + 1, ctx);
1146 + emit_b(b_off, ctx);
1149 + case BPF_RET | BPF_A:
1150 + ctx->flags |= SEEN_A;
1151 + if (i != prog->len - 1)
1153 + * If this is not the last instruction
1154 + * then jump to the epilogue
1156 + emit_b(b_imm(prog->len, ctx), ctx);
1157 + emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1159 + case BPF_RET | BPF_K:
1161 + * It can emit two instructions so it does not fit on
1164 + emit_load_imm(r_ret, k, ctx);
1165 + if (i != prog->len - 1) {
1167 + * If this is not the last instruction
1168 + * then jump to the epilogue
1170 + emit_b(b_imm(prog->len, ctx), ctx);
1174 + case BPF_MISC | BPF_TAX:
1176 + ctx->flags |= SEEN_X | SEEN_A;
1177 + emit_jit_reg_move(r_X, r_A, ctx);
1179 + case BPF_MISC | BPF_TXA:
1181 + ctx->flags |= SEEN_A | SEEN_X;
1182 + emit_jit_reg_move(r_A, r_X, ctx);
1185 + case BPF_ANC | SKF_AD_PROTOCOL:
1186 + /* A = ntohs(skb->protocol */
1187 + ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1188 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1190 + off = offsetof(struct sk_buff, protocol);
1191 + emit_half_load(r_A, r_skb, off, ctx);
1192 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1193 + /* This needs little endian fixup */
1194 + if (cpu_has_wsbh) {
1195 + /* R2 and later have the wsbh instruction */
1196 + emit_wsbh(r_A, r_A, ctx);
1198 + /* Get first byte */
1199 + emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1201 + emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1202 + /* Get second byte */
1203 + emit_srl(r_tmp_imm, r_A, 8, ctx);
1204 + emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1205 + /* Put everyting together in r_A */
1206 + emit_or(r_A, r_tmp, r_tmp_imm, ctx);
1210 + case BPF_ANC | SKF_AD_CPU:
1211 + ctx->flags |= SEEN_A | SEEN_OFF;
1212 + /* A = current_thread_info()->cpu */
1213 + BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1215 + off = offsetof(struct thread_info, cpu);
1216 + /* $28/gp points to the thread_info struct */
1217 + emit_load(r_A, 28, off, ctx);
1219 + case BPF_ANC | SKF_AD_IFINDEX:
1220 + /* A = skb->dev->ifindex */
1221 + case BPF_ANC | SKF_AD_HATYPE:
1222 + /* A = skb->dev->type */
1223 + ctx->flags |= SEEN_SKB | SEEN_A;
1224 + off = offsetof(struct sk_buff, dev);
1225 + /* Load *dev pointer */
1226 + emit_load_ptr(r_s0, r_skb, off, ctx);
1227 + /* error (0) in the delay slot */
1228 + emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1229 + b_imm(prog->len, ctx), ctx);
1230 + emit_reg_move(r_ret, r_zero, ctx);
1231 + if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
1232 + BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1233 + off = offsetof(struct net_device, ifindex);
1234 + emit_load(r_A, r_s0, off, ctx);
1235 + } else { /* (code == (BPF_ANC | SKF_AD_HATYPE) */
1236 + BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
1237 + off = offsetof(struct net_device, type);
1238 + emit_half_load_unsigned(r_A, r_s0, off, ctx);
1241 + case BPF_ANC | SKF_AD_MARK:
1242 + ctx->flags |= SEEN_SKB | SEEN_A;
1243 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1244 + off = offsetof(struct sk_buff, mark);
1245 + emit_load(r_A, r_skb, off, ctx);
1247 + case BPF_ANC | SKF_AD_RXHASH:
1248 + ctx->flags |= SEEN_SKB | SEEN_A;
1249 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1250 + off = offsetof(struct sk_buff, hash);
1251 + emit_load(r_A, r_skb, off, ctx);
1253 + case BPF_ANC | SKF_AD_VLAN_TAG:
1254 + ctx->flags |= SEEN_SKB | SEEN_A;
1255 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1257 + off = offsetof(struct sk_buff, vlan_tci);
1258 + emit_half_load_unsigned(r_A, r_skb, off, ctx);
1260 + case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
1261 + ctx->flags |= SEEN_SKB | SEEN_A;
1262 + emit_load_byte(r_A, r_skb, PKT_VLAN_PRESENT_OFFSET(), ctx);
1263 + if (PKT_VLAN_PRESENT_BIT)
1264 + emit_srl(r_A, r_A, PKT_VLAN_PRESENT_BIT, ctx);
1265 + if (PKT_VLAN_PRESENT_BIT < 7)
1266 + emit_andi(r_A, r_A, 1, ctx);
1268 + case BPF_ANC | SKF_AD_PKTTYPE:
1269 + ctx->flags |= SEEN_SKB;
1271 + emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
1272 + /* Keep only the last 3 bits */
1273 + emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
1274 +#ifdef __BIG_ENDIAN_BITFIELD
1275 + /* Get the actual packet type to the lower 3 bits */
1276 + emit_srl(r_A, r_A, 5, ctx);
1279 + case BPF_ANC | SKF_AD_QUEUE:
1280 + ctx->flags |= SEEN_SKB | SEEN_A;
1281 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1282 + queue_mapping) != 2);
1283 + BUILD_BUG_ON(offsetof(struct sk_buff,
1284 + queue_mapping) > 0xff);
1285 + off = offsetof(struct sk_buff, queue_mapping);
1286 + emit_half_load_unsigned(r_A, r_skb, off, ctx);
1289 + pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1295 + /* compute offsets only during the first pass */
1296 + if (ctx->target == NULL)
1297 + ctx->offsets[i] = ctx->idx * 4;
1302 +void bpf_jit_compile(struct bpf_prog *fp)
1304 + struct jit_ctx ctx;
1305 + unsigned int alloc_size, tmp_idx;
1307 + if (!bpf_jit_enable)
1310 + memset(&ctx, 0, sizeof(ctx));
1312 + ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
1313 + if (ctx.offsets == NULL)
1318 + if (build_body(&ctx))
1321 + tmp_idx = ctx.idx;
1322 + build_prologue(&ctx);
1323 + ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1324 + /* just to complete the ctx.idx count */
1325 + build_epilogue(&ctx);
1327 + alloc_size = 4 * ctx.idx;
1328 + ctx.target = module_alloc(alloc_size);
1329 + if (ctx.target == NULL)
1333 + memset(ctx.target, 0, alloc_size);
1337 + /* Generate the actual JIT code */
1338 + build_prologue(&ctx);
1340 + build_epilogue(&ctx);
1342 + /* Update the icache */
1343 + flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1345 + if (bpf_jit_enable > 1)
1346 + /* Dump JIT code */
1347 + bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1349 + fp->bpf_func = (void *)ctx.target;
1353 + kfree(ctx.offsets);
1356 +void bpf_jit_free(struct bpf_prog *fp)
1359 + module_memfree(fp->bpf_func);
1361 + bpf_prog_unlock_free(fp);
1364 +++ b/arch/mips/net/bpf_jit_asm.S
1367 + * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
1370 + * Copyright (C) 2015 Imagination Technologies Ltd.
1371 + * Author: Markos Chandras <markos.chandras@imgtec.com>
1373 + * This program is free software; you can redistribute it and/or modify it
1374 + * under the terms of the GNU General Public License as published by the
1375 + * Free Software Foundation; version 2 of the License.
1378 +#include <asm/asm.h>
1379 +#include <asm/isa-rev.h>
1380 +#include <asm/regdef.h>
1381 +#include "bpf_jit.h"
1385 + * r_skb_hl skb header length
1386 + * r_skb_data skb data
1387 + * r_off(a1) offset register
1388 + * r_A BPF register A
1389 + * r_X PF register X
1391 + * r_M *scratch memory
1392 + * r_skb_le skb length
1393 + * r_s0 Scratch register 0
1394 + * r_s1 Scratch register 1
1398 + * a1: offset (imm or imm + X)
1400 + * All non-BPF-ABI registers are free for use. On return, we only
1401 + * care about r_ret. The BPF-ABI registers are assumed to remain
1402 + * unmodified during the entire filter operation.
1407 +#define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
1409 + /* We know better :) so prevent assembler reordering etc */
1412 +#define is_offset_negative(TYPE) \
1413 + /* If offset is negative we have more work to do */ \
1414 + slti t0, offset, 0; \
1415 + bgtz t0, bpf_slow_path_##TYPE##_neg; \
1416 + /* Be careful what follows in DS. */
1418 +#define is_offset_in_header(SIZE, TYPE) \
1419 + /* Reading from header? */ \
1420 + addiu $r_s0, $r_skb_hl, -SIZE; \
1421 + slt t0, $r_s0, offset; \
1422 + bgtz t0, bpf_slow_path_##TYPE; \
1425 + is_offset_negative(word)
1426 +FEXPORT(sk_load_word_positive)
1427 + is_offset_in_header(4, word)
1428 + /* Offset within header boundaries */
1429 + PTR_ADDU t1, $r_skb_data, offset
1433 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1434 +# if MIPS_ISA_REV >= 2
1442 + andi t2, t2, 0xff00
1443 + andi t1, $r_A, 0xff00
1454 + is_offset_negative(half)
1455 +FEXPORT(sk_load_half_positive)
1456 + is_offset_in_header(2, half)
1457 + /* Offset within header boundaries */
1458 + PTR_ADDU t1, $r_skb_data, offset
1460 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1461 +# if MIPS_ISA_REV >= 2
1466 + andi t0, t0, 0xff00
1475 + is_offset_negative(byte)
1476 +FEXPORT(sk_load_byte_positive)
1477 + is_offset_in_header(1, byte)
1478 + /* Offset within header boundaries */
1479 + PTR_ADDU t1, $r_skb_data, offset
1486 + * call skb_copy_bits:
1487 + * (prototype in linux/skbuff.h)
1489 + * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
1491 + * o32 mandates we leave 4 spaces for argument registers in case
1492 + * the callee needs to use them. Even though we don't care about
1493 + * the argument registers ourselves, we need to allocate that space
1494 + * to remain ABI compliant since the callee may want to use that space.
1495 + * We also allocate 2 more spaces for $r_ra and our return register (*to).
1497 + * n64 is a bit different. The *caller* will allocate the space to preserve
1498 + * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
1499 + * good reason but it does not matter that much really.
1501 + * (void *to) is returned in r_s0
1504 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1505 +#define DS_OFFSET(SIZE) (4 * SZREG)
1507 +#define DS_OFFSET(SIZE) ((4 * SZREG) + (4 - SIZE))
1509 +#define bpf_slow_path_common(SIZE) \
1510 + /* Quick check. Are we within reasonable boundaries? */ \
1511 + LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
1512 + sltu $r_s0, offset, $r_s1; \
1513 + beqz $r_s0, fault; \
1514 + /* Load 4th argument in DS */ \
1515 + LONG_ADDIU a3, zero, SIZE; \
1516 + PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
1517 + PTR_LA t0, skb_copy_bits; \
1518 + PTR_S $r_ra, (5 * SZREG)($r_sp); \
1519 + /* Assign low slot to a2 */ \
1520 + PTR_ADDIU a2, $r_sp, DS_OFFSET(SIZE); \
1522 + /* Reset our destination slot (DS but it's ok) */ \
1523 + INT_S zero, (4 * SZREG)($r_sp); \
1525 + * skb_copy_bits returns 0 on success and -EFAULT \
1526 + * on error. Our data live in a2. Do not bother with \
1527 + * our data if an error has been returned. \
1529 + /* Restore our frame */ \
1530 + PTR_L $r_ra, (5 * SZREG)($r_sp); \
1531 + INT_L $r_s0, (4 * SZREG)($r_sp); \
1533 + PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
1534 + move $r_ret, zero; \
1536 +NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
1537 + bpf_slow_path_common(4)
1538 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1539 +# if MIPS_ISA_REV >= 2
1548 + andi t2, t2, 0xff00
1549 + andi t1, $r_s0, 0xff00
1560 + END(bpf_slow_path_word)
1562 +NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
1563 + bpf_slow_path_common(2)
1564 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1565 +# if MIPS_ISA_REV >= 2
1570 + andi t1, $r_s0, 0xff00
1571 + andi t0, t0, 0xff00
1581 + END(bpf_slow_path_half)
1583 +NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
1584 + bpf_slow_path_common(1)
1588 + END(bpf_slow_path_byte)
1591 + * Negative entry points
1593 + .macro bpf_is_end_of_data
1595 + /* Reading link layer data? */
1596 + slt t1, offset, t0
1598 + /* Be careful what follows in DS. */
1601 + * call skb_copy_bits:
1602 + * (prototype in linux/filter.h)
1604 + * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
1605 + * int k, unsigned int size)
1607 + * see above (bpf_slow_path_common) for ABI restrictions
1609 +#define bpf_negative_common(SIZE) \
1610 + PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
1611 + PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
1612 + PTR_S $r_ra, (5 * SZREG)($r_sp); \
1615 + PTR_L $r_ra, (5 * SZREG)($r_sp); \
1616 + /* Check return pointer */ \
1618 + PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
1619 + /* Preserve our pointer */ \
1621 + /* Set return value */ \
1622 + move $r_ret, zero; \
1624 +bpf_slow_path_word_neg:
1625 + bpf_is_end_of_data
1626 +NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
1627 + bpf_negative_common(4)
1630 + END(sk_load_word_negative)
1632 +bpf_slow_path_half_neg:
1633 + bpf_is_end_of_data
1634 +NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
1635 + bpf_negative_common(2)
1637 + lhu $r_A, 0($r_s0)
1638 + END(sk_load_half_negative)
1640 +bpf_slow_path_byte_neg:
1641 + bpf_is_end_of_data
1642 +NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
1643 + bpf_negative_common(1)
1645 + lbu $r_A, 0($r_s0)
1646 + END(sk_load_byte_negative)
1650 + addiu $r_ret, zero, 1