kernel: bump 5.4 to 5.4.58
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 070-v5.5-MIPS-BPF-Restore-MIPS32-cBPF-JIT.patch
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
5
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
11 case.
12
13 The eBPF JIT has a number of problems on MIPS32:
14
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.
18
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.
32
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.
40
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.
44
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
50 support.
51
52 [1] https://lore.kernel.org/linux-mips/MWHPR2201MB13583388481F01A422CE7D66D4410@MWHPR2201MB1358.namprd22.prod.outlook.com/
53
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
63 ---
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
71
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
84 @@ -1,4 +1,5 @@
85 # SPDX-License-Identifier: GPL-2.0-only
86 # MIPS networking code
87
88 +obj-$(CONFIG_MIPS_CBPF_JIT) += bpf_jit.o bpf_jit_asm.o
89 obj-$(CONFIG_MIPS_EBPF_JIT) += ebpf_jit.o
90 --- /dev/null
91 +++ b/arch/mips/net/bpf_jit.c
92 @@ -0,0 +1,1270 @@
93 +/*
94 + * Just-In-Time compiler for BPF filters on MIPS
95 + *
96 + * Copyright (c) 2014 Imagination Technologies Ltd.
97 + * Author: Markos Chandras <markos.chandras@imgtec.com>
98 + *
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.
102 + */
103 +
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>
119 +
120 +#include "bpf_jit.h"
121 +
122 +/* ABI
123 + * r_skb_hl SKB header length
124 + * r_data SKB data pointer
125 + * r_off Offset
126 + * r_A BPF register A
127 + * r_X BPF register X
128 + * r_skb *skb
129 + * r_M *scratch memory
130 + * r_skb_len SKB length
131 + *
132 + * On entry (*bpf_func)(*skb, *filter)
133 + * a0 = MIPS_R_A0 = skb;
134 + * a1 = MIPS_R_A1 = filter;
135 + *
136 + * Stack
137 + * ...
138 + * M[15]
139 + * M[14]
140 + * M[13]
141 + * ...
142 + * M[0] <-- r_M
143 + * saved reg k-1
144 + * saved reg k-2
145 + * ...
146 + * saved reg 0 <-- r_sp
147 + * <no argument area>
148 + *
149 + * Packet layout
150 + *
151 + * <--------------------- len ------------------------>
152 + * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
153 + * ----------------------------------------------------
154 + * | skb->data |
155 + * ----------------------------------------------------
156 + */
157 +
158 +#define ptr typeof(unsigned long)
159 +
160 +#define SCRATCH_OFF(k) (4 * (k))
161 +
162 +/* JIT flags */
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))
174 +
175 +/* Arguments used by JIT */
176 +#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
177 +
178 +#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
179 +
180 +/**
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
188 + */
189 +struct jit_ctx {
190 + const struct bpf_prog *skf;
191 + unsigned int prologue_bytes;
192 + u32 idx;
193 + u32 flags;
194 + u32 *offsets;
195 + u32 *target;
196 +};
197 +
198 +
199 +static inline int optimize_div(u32 *k)
200 +{
201 + /* power of 2 divides can be implemented with right shift */
202 + if (!(*k & (*k-1))) {
203 + *k = ilog2(*k);
204 + return 1;
205 + }
206 +
207 + return 0;
208 +}
209 +
210 +static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
211 +
212 +/* Simply emit the instruction if the JIT memory space has been allocated */
213 +#define emit_instr(ctx, func, ...) \
214 +do { \
215 + if ((ctx)->target != NULL) { \
216 + u32 *p = &(ctx)->target[ctx->idx]; \
217 + uasm_i_##func(&p, ##__VA_ARGS__); \
218 + } \
219 + (ctx)->idx++; \
220 +} while (0)
221 +
222 +/*
223 + * Similar to emit_instr but it must be used when we need to emit
224 + * 32-bit or 64-bit instructions
225 + */
226 +#define emit_long_instr(ctx, func, ...) \
227 +do { \
228 + if ((ctx)->target != NULL) { \
229 + u32 *p = &(ctx)->target[ctx->idx]; \
230 + UASM_i_##func(&p, ##__VA_ARGS__); \
231 + } \
232 + (ctx)->idx++; \
233 +} while (0)
234 +
235 +/* Determine if immediate is within the 16-bit signed range */
236 +static inline bool is_range16(s32 imm)
237 +{
238 + return !(imm >= SBIT(15) || imm < -SBIT(15));
239 +}
240 +
241 +static inline void emit_addu(unsigned int dst, unsigned int src1,
242 + unsigned int src2, struct jit_ctx *ctx)
243 +{
244 + emit_instr(ctx, addu, dst, src1, src2);
245 +}
246 +
247 +static inline void emit_nop(struct jit_ctx *ctx)
248 +{
249 + emit_instr(ctx, nop);
250 +}
251 +
252 +/* Load a u32 immediate to a register */
253 +static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
254 +{
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);
262 + } else {
263 + u32 *p = &ctx->target[ctx->idx];
264 + uasm_i_addiu(&p, dst, r_zero, imm);
265 + }
266 + }
267 + ctx->idx++;
268 +
269 + if (!is_range16(imm))
270 + ctx->idx++;
271 +}
272 +
273 +static inline void emit_or(unsigned int dst, unsigned int src1,
274 + unsigned int src2, struct jit_ctx *ctx)
275 +{
276 + emit_instr(ctx, or, dst, src1, src2);
277 +}
278 +
279 +static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
280 + struct jit_ctx *ctx)
281 +{
282 + if (imm >= BIT(16)) {
283 + emit_load_imm(r_tmp, imm, ctx);
284 + emit_or(dst, src, r_tmp, ctx);
285 + } else {
286 + emit_instr(ctx, ori, dst, src, imm);
287 + }
288 +}
289 +
290 +static inline void emit_daddiu(unsigned int dst, unsigned int src,
291 + int imm, struct jit_ctx *ctx)
292 +{
293 + /*
294 + * Only used for stack, so the imm is relatively small
295 + * and it fits in 15-bits
296 + */
297 + emit_instr(ctx, daddiu, dst, src, imm);
298 +}
299 +
300 +static inline void emit_addiu(unsigned int dst, unsigned int src,
301 + u32 imm, struct jit_ctx *ctx)
302 +{
303 + if (!is_range16(imm)) {
304 + emit_load_imm(r_tmp, imm, ctx);
305 + emit_addu(dst, r_tmp, src, ctx);
306 + } else {
307 + emit_instr(ctx, addiu, dst, src, imm);
308 + }
309 +}
310 +
311 +static inline void emit_and(unsigned int dst, unsigned int src1,
312 + unsigned int src2, struct jit_ctx *ctx)
313 +{
314 + emit_instr(ctx, and, dst, src1, src2);
315 +}
316 +
317 +static inline void emit_andi(unsigned int dst, unsigned int src,
318 + u32 imm, struct jit_ctx *ctx)
319 +{
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);
324 + } else {
325 + emit_instr(ctx, andi, dst, src, imm);
326 + }
327 +}
328 +
329 +static inline void emit_xor(unsigned int dst, unsigned int src1,
330 + unsigned int src2, struct jit_ctx *ctx)
331 +{
332 + emit_instr(ctx, xor, dst, src1, src2);
333 +}
334 +
335 +static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
336 +{
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);
341 + } else {
342 + emit_instr(ctx, xori, dst, src, imm);
343 + }
344 +}
345 +
346 +static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
347 +{
348 + emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
349 +}
350 +
351 +static inline void emit_subu(unsigned int dst, unsigned int src1,
352 + unsigned int src2, struct jit_ctx *ctx)
353 +{
354 + emit_instr(ctx, subu, dst, src1, src2);
355 +}
356 +
357 +static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
358 +{
359 + emit_subu(reg, r_zero, reg, ctx);
360 +}
361 +
362 +static inline void emit_sllv(unsigned int dst, unsigned int src,
363 + unsigned int sa, struct jit_ctx *ctx)
364 +{
365 + emit_instr(ctx, sllv, dst, src, sa);
366 +}
367 +
368 +static inline void emit_sll(unsigned int dst, unsigned int src,
369 + unsigned int sa, struct jit_ctx *ctx)
370 +{
371 + /* sa is 5-bits long */
372 + if (sa >= BIT(5))
373 + /* Shifting >= 32 results in zero */
374 + emit_jit_reg_move(dst, r_zero, ctx);
375 + else
376 + emit_instr(ctx, sll, dst, src, sa);
377 +}
378 +
379 +static inline void emit_srlv(unsigned int dst, unsigned int src,
380 + unsigned int sa, struct jit_ctx *ctx)
381 +{
382 + emit_instr(ctx, srlv, dst, src, sa);
383 +}
384 +
385 +static inline void emit_srl(unsigned int dst, unsigned int src,
386 + unsigned int sa, struct jit_ctx *ctx)
387 +{
388 + /* sa is 5-bits long */
389 + if (sa >= BIT(5))
390 + /* Shifting >= 32 results in zero */
391 + emit_jit_reg_move(dst, r_zero, ctx);
392 + else
393 + emit_instr(ctx, srl, dst, src, sa);
394 +}
395 +
396 +static inline void emit_slt(unsigned int dst, unsigned int src1,
397 + unsigned int src2, struct jit_ctx *ctx)
398 +{
399 + emit_instr(ctx, slt, dst, src1, src2);
400 +}
401 +
402 +static inline void emit_sltu(unsigned int dst, unsigned int src1,
403 + unsigned int src2, struct jit_ctx *ctx)
404 +{
405 + emit_instr(ctx, sltu, dst, src1, src2);
406 +}
407 +
408 +static inline void emit_sltiu(unsigned dst, unsigned int src,
409 + unsigned int imm, struct jit_ctx *ctx)
410 +{
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);
415 + } else {
416 + emit_instr(ctx, sltiu, dst, src, imm);
417 + }
418 +
419 +}
420 +
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)
425 +{
426 + emit_long_instr(ctx, SW, reg, offset, base);
427 +}
428 +
429 +static inline void emit_store(ptr reg, ptr base, unsigned int offset,
430 + struct jit_ctx *ctx)
431 +{
432 + emit_instr(ctx, sw, reg, offset, base);
433 +}
434 +
435 +static inline void emit_load_stack_reg(ptr reg, ptr base,
436 + unsigned int offset,
437 + struct jit_ctx *ctx)
438 +{
439 + emit_long_instr(ctx, LW, reg, offset, base);
440 +}
441 +
442 +static inline void emit_load(unsigned int reg, unsigned int base,
443 + unsigned int offset, struct jit_ctx *ctx)
444 +{
445 + emit_instr(ctx, lw, reg, offset, base);
446 +}
447 +
448 +static inline void emit_load_byte(unsigned int reg, unsigned int base,
449 + unsigned int offset, struct jit_ctx *ctx)
450 +{
451 + emit_instr(ctx, lb, reg, offset, base);
452 +}
453 +
454 +static inline void emit_half_load(unsigned int reg, unsigned int base,
455 + unsigned int offset, struct jit_ctx *ctx)
456 +{
457 + emit_instr(ctx, lh, reg, offset, base);
458 +}
459 +
460 +static inline void emit_half_load_unsigned(unsigned int reg, unsigned int base,
461 + unsigned int offset, struct jit_ctx *ctx)
462 +{
463 + emit_instr(ctx, lhu, reg, offset, base);
464 +}
465 +
466 +static inline void emit_mul(unsigned int dst, unsigned int src1,
467 + unsigned int src2, struct jit_ctx *ctx)
468 +{
469 + emit_instr(ctx, mul, dst, src1, src2);
470 +}
471 +
472 +static inline void emit_div(unsigned int dst, unsigned int src,
473 + struct jit_ctx *ctx)
474 +{
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);
480 + }
481 + ctx->idx += 2; /* 2 insts */
482 +}
483 +
484 +static inline void emit_mod(unsigned int dst, unsigned int src,
485 + struct jit_ctx *ctx)
486 +{
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);
492 + }
493 + ctx->idx += 2; /* 2 insts */
494 +}
495 +
496 +static inline void emit_dsll(unsigned int dst, unsigned int src,
497 + unsigned int sa, struct jit_ctx *ctx)
498 +{
499 + emit_instr(ctx, dsll, dst, src, sa);
500 +}
501 +
502 +static inline void emit_dsrl32(unsigned int dst, unsigned int src,
503 + unsigned int sa, struct jit_ctx *ctx)
504 +{
505 + emit_instr(ctx, dsrl32, dst, src, sa);
506 +}
507 +
508 +static inline void emit_wsbh(unsigned int dst, unsigned int src,
509 + struct jit_ctx *ctx)
510 +{
511 + emit_instr(ctx, wsbh, dst, src);
512 +}
513 +
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)
517 +{
518 + /* src contains the base addr of the 32/64-pointer */
519 + emit_long_instr(ctx, LW, dst, imm, src);
520 +}
521 +
522 +/* load a function pointer to register */
523 +static inline void emit_load_func(unsigned int reg, ptr imm,
524 + struct jit_ctx *ctx)
525 +{
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);
533 + } else {
534 + emit_load_imm(reg, imm, ctx);
535 + }
536 +}
537 +
538 +/* Move to real MIPS register */
539 +static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
540 +{
541 + emit_long_instr(ctx, ADDU, dst, src, r_zero);
542 +}
543 +
544 +/* Move to JIT (32-bit) register */
545 +static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
546 +{
547 + emit_addu(dst, src, r_zero, ctx);
548 +}
549 +
550 +/* Compute the immediate value for PC-relative branches. */
551 +static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
552 +{
553 + if (ctx->target == NULL)
554 + return 0;
555 +
556 + /*
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.
560 +
561 + * Branch on MIPS:
562 + * I: target_offset <- sign_extend(offset)
563 + * I+1: PC += target_offset (delay slot)
564 + *
565 + * ctx->idx currently points to the branch instruction
566 + * but the offset is added to the delay slot so we need
567 + * to subtract 4.
568 + */
569 + return ctx->offsets[tgt] -
570 + (ctx->idx * 4 - ctx->prologue_bytes) - 4;
571 +}
572 +
573 +static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
574 + unsigned int imm, struct jit_ctx *ctx)
575 +{
576 + if (ctx->target != NULL) {
577 + u32 *p = &ctx->target[ctx->idx];
578 +
579 + switch (cond) {
580 + case MIPS_COND_EQ:
581 + uasm_i_beq(&p, reg1, reg2, imm);
582 + break;
583 + case MIPS_COND_NE:
584 + uasm_i_bne(&p, reg1, reg2, imm);
585 + break;
586 + case MIPS_COND_ALL:
587 + uasm_i_b(&p, imm);
588 + break;
589 + default:
590 + pr_warn("%s: Unhandled branch conditional: %d\n",
591 + __func__, cond);
592 + }
593 + }
594 + ctx->idx++;
595 +}
596 +
597 +static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
598 +{
599 + emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
600 +}
601 +
602 +static inline void emit_jalr(unsigned int link, unsigned int reg,
603 + struct jit_ctx *ctx)
604 +{
605 + emit_instr(ctx, jalr, link, reg);
606 +}
607 +
608 +static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
609 +{
610 + emit_instr(ctx, jr, reg);
611 +}
612 +
613 +static inline u16 align_sp(unsigned int num)
614 +{
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;
618 + return num;
619 +}
620 +
621 +static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
622 +{
623 + int i = 0, real_off = 0;
624 + u32 sflags, tmp_flags;
625 +
626 + /* Adjust the stack pointer */
627 + if (offset)
628 + emit_stack_offset(-align_sp(offset), ctx);
629 +
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,
635 + ctx);
636 + real_off += SZREG;
637 + }
638 + i++;
639 + tmp_flags >>= 1;
640 + }
641 +
642 + /* save return address */
643 + if (ctx->flags & SEEN_CALL) {
644 + emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
645 + real_off += SZREG;
646 + }
647 +
648 + /* Setup r_M leaving the alignment gap if necessary */
649 + if (ctx->flags & SEEN_MEM) {
650 + if (real_off % (SZREG * 2))
651 + real_off += SZREG;
652 + emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
653 + }
654 +}
655 +
656 +static void restore_bpf_jit_regs(struct jit_ctx *ctx,
657 + unsigned int offset)
658 +{
659 + int i, real_off = 0;
660 + u32 sflags, tmp_flags;
661 +
662 + tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
663 + /* sflags is a bitmap */
664 + i = 0;
665 + while (tmp_flags) {
666 + if ((sflags >> i) & 0x1) {
667 + emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
668 + ctx);
669 + real_off += SZREG;
670 + }
671 + i++;
672 + tmp_flags >>= 1;
673 + }
674 +
675 + /* restore return address */
676 + if (ctx->flags & SEEN_CALL)
677 + emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
678 +
679 + /* Restore the sp and discard the scrach memory */
680 + if (offset)
681 + emit_stack_offset(align_sp(offset), ctx);
682 +}
683 +
684 +static unsigned int get_stack_depth(struct jit_ctx *ctx)
685 +{
686 + int sp_off = 0;
687 +
688 +
689 + /* How may s* regs do we need to preserved? */
690 + sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
691 +
692 + if (ctx->flags & SEEN_MEM)
693 + sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
694 +
695 + if (ctx->flags & SEEN_CALL)
696 + sp_off += SZREG; /* Space for our ra register */
697 +
698 + return sp_off;
699 +}
700 +
701 +static void build_prologue(struct jit_ctx *ctx)
702 +{
703 + int sp_off;
704 +
705 + /* Calculate the total offset for the stack pointer */
706 + sp_off = get_stack_depth(ctx);
707 + save_bpf_jit_regs(ctx, sp_off);
708 +
709 + if (ctx->flags & SEEN_SKB)
710 + emit_reg_move(r_skb, MIPS_R_A0, ctx);
711 +
712 + if (ctx->flags & SEEN_SKB_DATA) {
713 + /* Load packet length */
714 + emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
715 + ctx);
716 + emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
717 + ctx);
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);
723 + }
724 +
725 + if (ctx->flags & SEEN_X)
726 + emit_jit_reg_move(r_X, r_zero, ctx);
727 +
728 + /*
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.
733 + */
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);
737 +}
738 +
739 +static void build_epilogue(struct jit_ctx *ctx)
740 +{
741 + unsigned int sp_off;
742 +
743 + /* Calculate the total offset for the stack pointer */
744 +
745 + sp_off = get_stack_depth(ctx);
746 + restore_bpf_jit_regs(ctx, sp_off);
747 +
748 + /* Return */
749 + emit_jr(r_ra, ctx);
750 + emit_nop(ctx);
751 +}
752 +
753 +#define CHOOSE_LOAD_FUNC(K, func) \
754 + ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
755 + func##_positive)
756 +
757 +static int build_body(struct jit_ctx *ctx)
758 +{
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);
764 +
765 + for (i = 0; i < prog->len; i++) {
766 + u16 code;
767 +
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);
771 + k = inst->k;
772 + code = bpf_anc_helper(inst);
773 +
774 + if (ctx->target == NULL)
775 + ctx->offsets[i] = ctx->idx * 4;
776 +
777 + switch (code) {
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);
782 + break;
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);
789 + break;
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);
794 + break;
795 + case BPF_LD | BPF_W | BPF_ABS:
796 + /* A <- P[k:4] */
797 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
798 + goto load;
799 + case BPF_LD | BPF_H | BPF_ABS:
800 + /* A <- P[k:2] */
801 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
802 + goto load;
803 + case BPF_LD | BPF_B | BPF_ABS:
804 + /* A <- P[k:1] */
805 + sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
806 +load:
807 + emit_load_imm(r_off, k, ctx);
808 +load_common:
809 + ctx->flags |= SEEN_CALL | SEEN_OFF |
810 + SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
811 +
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),
819 + 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);
824 + emit_nop(ctx);
825 + break;
826 + case BPF_LD | BPF_W | BPF_IND:
827 + /* A <- P[X + k:4] */
828 + sk_load_func = sk_load_word;
829 + goto load_ind;
830 + case BPF_LD | BPF_H | BPF_IND:
831 + /* A <- P[X + k:2] */
832 + sk_load_func = sk_load_half;
833 + goto load_ind;
834 + case BPF_LD | BPF_B | BPF_IND:
835 + /* A <- P[X + k:1] */
836 + sk_load_func = sk_load_byte;
837 +load_ind:
838 + ctx->flags |= SEEN_OFF | SEEN_X;
839 + emit_addiu(r_off, r_X, k, ctx);
840 + goto load_common;
841 + case BPF_LDX | BPF_IMM:
842 + /* X <- k */
843 + ctx->flags |= SEEN_X;
844 + emit_load_imm(r_X, k, ctx);
845 + break;
846 + case BPF_LDX | BPF_MEM:
847 + /* X <- M[k] */
848 + ctx->flags |= SEEN_X | SEEN_MEM;
849 + emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
850 + break;
851 + case BPF_LDX | BPF_W | BPF_LEN:
852 + /* X <- len */
853 + ctx->flags |= SEEN_X | SEEN_SKB;
854 + off = offsetof(struct sk_buff, len);
855 + emit_load(r_X, r_skb, off, ctx);
856 + break;
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);
862 + /*
863 + * This may emit two instructions so it may not fit
864 + * in the delay slot. So use a0 in the delay slot.
865 + */
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);
873 + /* We are good */
874 + /* X <- P[1:K] & 0xf */
875 + emit_andi(r_X, r_A, 0xf, ctx);
876 + /* X << 2 */
877 + emit_b(b_imm(i + 1, ctx), ctx);
878 + emit_sll(r_X, r_X, 2, ctx); /* delay slot */
879 + break;
880 + case BPF_ST:
881 + /* M[k] <- A */
882 + ctx->flags |= SEEN_MEM | SEEN_A;
883 + emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
884 + break;
885 + case BPF_STX:
886 + /* M[k] <- X */
887 + ctx->flags |= SEEN_MEM | SEEN_X;
888 + emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
889 + break;
890 + case BPF_ALU | BPF_ADD | BPF_K:
891 + /* A += K */
892 + ctx->flags |= SEEN_A;
893 + emit_addiu(r_A, r_A, k, ctx);
894 + break;
895 + case BPF_ALU | BPF_ADD | BPF_X:
896 + /* A += X */
897 + ctx->flags |= SEEN_A | SEEN_X;
898 + emit_addu(r_A, r_A, r_X, ctx);
899 + break;
900 + case BPF_ALU | BPF_SUB | BPF_K:
901 + /* A -= K */
902 + ctx->flags |= SEEN_A;
903 + emit_addiu(r_A, r_A, -k, ctx);
904 + break;
905 + case BPF_ALU | BPF_SUB | BPF_X:
906 + /* A -= X */
907 + ctx->flags |= SEEN_A | SEEN_X;
908 + emit_subu(r_A, r_A, r_X, ctx);
909 + break;
910 + case BPF_ALU | BPF_MUL | BPF_K:
911 + /* A *= 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);
916 + break;
917 + case BPF_ALU | BPF_MUL | BPF_X:
918 + /* A *= X */
919 + ctx->flags |= SEEN_A | SEEN_X;
920 + emit_mul(r_A, r_A, r_X, ctx);
921 + break;
922 + case BPF_ALU | BPF_DIV | BPF_K:
923 + /* A /= k */
924 + if (k == 1)
925 + break;
926 + if (optimize_div(&k)) {
927 + ctx->flags |= SEEN_A;
928 + emit_srl(r_A, r_A, k, ctx);
929 + break;
930 + }
931 + ctx->flags |= SEEN_A;
932 + emit_load_imm(r_s0, k, ctx);
933 + emit_div(r_A, r_s0, ctx);
934 + break;
935 + case BPF_ALU | BPF_MOD | BPF_K:
936 + /* A %= k */
937 + if (k == 1) {
938 + ctx->flags |= SEEN_A;
939 + emit_jit_reg_move(r_A, r_zero, ctx);
940 + } else {
941 + ctx->flags |= SEEN_A;
942 + emit_load_imm(r_s0, k, ctx);
943 + emit_mod(r_A, r_s0, ctx);
944 + }
945 + break;
946 + case BPF_ALU | BPF_DIV | BPF_X:
947 + /* A /= 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);
954 + break;
955 + case BPF_ALU | BPF_MOD | BPF_X:
956 + /* A %= 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);
963 + break;
964 + case BPF_ALU | BPF_OR | BPF_K:
965 + /* A |= K */
966 + ctx->flags |= SEEN_A;
967 + emit_ori(r_A, r_A, k, ctx);
968 + break;
969 + case BPF_ALU | BPF_OR | BPF_X:
970 + /* A |= X */
971 + ctx->flags |= SEEN_A;
972 + emit_ori(r_A, r_A, r_X, ctx);
973 + break;
974 + case BPF_ALU | BPF_XOR | BPF_K:
975 + /* A ^= k */
976 + ctx->flags |= SEEN_A;
977 + emit_xori(r_A, r_A, k, ctx);
978 + break;
979 + case BPF_ANC | SKF_AD_ALU_XOR_X:
980 + case BPF_ALU | BPF_XOR | BPF_X:
981 + /* A ^= X */
982 + ctx->flags |= SEEN_A;
983 + emit_xor(r_A, r_A, r_X, ctx);
984 + break;
985 + case BPF_ALU | BPF_AND | BPF_K:
986 + /* A &= K */
987 + ctx->flags |= SEEN_A;
988 + emit_andi(r_A, r_A, k, ctx);
989 + break;
990 + case BPF_ALU | BPF_AND | BPF_X:
991 + /* A &= X */
992 + ctx->flags |= SEEN_A | SEEN_X;
993 + emit_and(r_A, r_A, r_X, ctx);
994 + break;
995 + case BPF_ALU | BPF_LSH | BPF_K:
996 + /* A <<= K */
997 + ctx->flags |= SEEN_A;
998 + emit_sll(r_A, r_A, k, ctx);
999 + break;
1000 + case BPF_ALU | BPF_LSH | BPF_X:
1001 + /* A <<= X */
1002 + ctx->flags |= SEEN_A | SEEN_X;
1003 + emit_sllv(r_A, r_A, r_X, ctx);
1004 + break;
1005 + case BPF_ALU | BPF_RSH | BPF_K:
1006 + /* A >>= K */
1007 + ctx->flags |= SEEN_A;
1008 + emit_srl(r_A, r_A, k, ctx);
1009 + break;
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);
1013 + break;
1014 + case BPF_ALU | BPF_NEG:
1015 + /* A = -A */
1016 + ctx->flags |= SEEN_A;
1017 + emit_neg(r_A, ctx);
1018 + break;
1019 + case BPF_JMP | BPF_JA:
1020 + /* pc += K */
1021 + emit_b(b_imm(i + k + 1, ctx), ctx);
1022 + emit_nop(ctx);
1023 + break;
1024 + case BPF_JMP | BPF_JEQ | BPF_K:
1025 + /* pc += ( A == K ) ? pc->jt : pc->jf */
1026 + condt = MIPS_COND_EQ | MIPS_COND_K;
1027 + goto jmp_cmp;
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;
1032 + goto jmp_cmp;
1033 + case BPF_JMP | BPF_JGE | BPF_K:
1034 + /* pc += ( A >= K ) ? pc->jt : pc->jf */
1035 + condt = MIPS_COND_GE | MIPS_COND_K;
1036 + goto jmp_cmp;
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;
1041 + goto jmp_cmp;
1042 + case BPF_JMP | BPF_JGT | BPF_K:
1043 + /* pc += ( A > K ) ? pc->jt : pc->jf */
1044 + condt = MIPS_COND_GT | MIPS_COND_K;
1045 + goto jmp_cmp;
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;
1050 +jmp_cmp:
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);
1057 + } else { /* X */
1058 + ctx->flags |= SEEN_A |
1059 + SEEN_X;
1060 + emit_sltu(r_s0, r_A, r_X, ctx);
1061 + }
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,
1065 + ctx);
1066 + emit_nop(ctx);
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);
1073 + else
1074 + emit_jit_reg_move(r_s0, r_X,
1075 + ctx);
1076 + b_off = b_imm(i + inst->jf + 1, ctx);
1077 + emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1078 + b_off, ctx);
1079 + emit_nop(ctx);
1080 + /* Finally, A > K|X */
1081 + b_off = b_imm(i + inst->jt + 1, ctx);
1082 + emit_b(b_off, ctx);
1083 + emit_nop(ctx);
1084 + } else {
1085 + /* A >= (K|X) so jump */
1086 + b_off = b_imm(i + inst->jt + 1, ctx);
1087 + emit_b(b_off, ctx);
1088 + emit_nop(ctx);
1089 + }
1090 + } else {
1091 + /* A == K|X */
1092 + if (condt & MIPS_COND_K) { /* K */
1093 + ctx->flags |= SEEN_A;
1094 + emit_load_imm(r_s0, k, ctx);
1095 + /* jump true */
1096 + b_off = b_imm(i + inst->jt + 1, ctx);
1097 + emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1098 + b_off, ctx);
1099 + emit_nop(ctx);
1100 + /* jump false */
1101 + b_off = b_imm(i + inst->jf + 1,
1102 + ctx);
1103 + emit_bcond(MIPS_COND_NE, r_A, r_s0,
1104 + b_off, ctx);
1105 + emit_nop(ctx);
1106 + } else { /* X */
1107 + /* jump true */
1108 + ctx->flags |= SEEN_A | SEEN_X;
1109 + b_off = b_imm(i + inst->jt + 1,
1110 + ctx);
1111 + emit_bcond(MIPS_COND_EQ, r_A, r_X,
1112 + b_off, ctx);
1113 + emit_nop(ctx);
1114 + /* jump false */
1115 + b_off = b_imm(i + inst->jf + 1, ctx);
1116 + emit_bcond(MIPS_COND_NE, r_A, r_X,
1117 + b_off, ctx);
1118 + emit_nop(ctx);
1119 + }
1120 + }
1121 + break;
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);
1127 + /* jump true */
1128 + b_off = b_imm(i + inst->jt + 1, ctx);
1129 + emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1130 + emit_nop(ctx);
1131 + /* jump false */
1132 + b_off = b_imm(i + inst->jf + 1, ctx);
1133 + emit_b(b_off, ctx);
1134 + emit_nop(ctx);
1135 + break;
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);
1140 + /* jump true */
1141 + b_off = b_imm(i + inst->jt + 1, ctx);
1142 + emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1143 + emit_nop(ctx);
1144 + /* jump false */
1145 + b_off = b_imm(i + inst->jf + 1, ctx);
1146 + emit_b(b_off, ctx);
1147 + emit_nop(ctx);
1148 + break;
1149 + case BPF_RET | BPF_A:
1150 + ctx->flags |= SEEN_A;
1151 + if (i != prog->len - 1)
1152 + /*
1153 + * If this is not the last instruction
1154 + * then jump to the epilogue
1155 + */
1156 + emit_b(b_imm(prog->len, ctx), ctx);
1157 + emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1158 + break;
1159 + case BPF_RET | BPF_K:
1160 + /*
1161 + * It can emit two instructions so it does not fit on
1162 + * the delay slot.
1163 + */
1164 + emit_load_imm(r_ret, k, ctx);
1165 + if (i != prog->len - 1) {
1166 + /*
1167 + * If this is not the last instruction
1168 + * then jump to the epilogue
1169 + */
1170 + emit_b(b_imm(prog->len, ctx), ctx);
1171 + emit_nop(ctx);
1172 + }
1173 + break;
1174 + case BPF_MISC | BPF_TAX:
1175 + /* X = A */
1176 + ctx->flags |= SEEN_X | SEEN_A;
1177 + emit_jit_reg_move(r_X, r_A, ctx);
1178 + break;
1179 + case BPF_MISC | BPF_TXA:
1180 + /* A = X */
1181 + ctx->flags |= SEEN_A | SEEN_X;
1182 + emit_jit_reg_move(r_A, r_X, ctx);
1183 + break;
1184 + /* AUX */
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,
1189 + protocol) != 2);
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);
1197 + } else {
1198 + /* Get first byte */
1199 + emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1200 + /* Shift it */
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);
1207 + }
1208 +#endif
1209 + break;
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,
1214 + cpu) != 4);
1215 + off = offsetof(struct thread_info, cpu);
1216 + /* $28/gp points to the thread_info struct */
1217 + emit_load(r_A, 28, off, ctx);
1218 + break;
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);
1239 + }
1240 + break;
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);
1246 + break;
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);
1252 + break;
1253 + case BPF_ANC | SKF_AD_VLAN_TAG:
1254 + ctx->flags |= SEEN_SKB | SEEN_A;
1255 + BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1256 + vlan_tci) != 2);
1257 + off = offsetof(struct sk_buff, vlan_tci);
1258 + emit_half_load_unsigned(r_A, r_skb, off, ctx);
1259 + break;
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);
1267 + break;
1268 + case BPF_ANC | SKF_AD_PKTTYPE:
1269 + ctx->flags |= SEEN_SKB;
1270 +
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);
1277 +#endif
1278 + break;
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);
1287 + break;
1288 + default:
1289 + pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1290 + inst->code);
1291 + return -1;
1292 + }
1293 + }
1294 +
1295 + /* compute offsets only during the first pass */
1296 + if (ctx->target == NULL)
1297 + ctx->offsets[i] = ctx->idx * 4;
1298 +
1299 + return 0;
1300 +}
1301 +
1302 +void bpf_jit_compile(struct bpf_prog *fp)
1303 +{
1304 + struct jit_ctx ctx;
1305 + unsigned int alloc_size, tmp_idx;
1306 +
1307 + if (!bpf_jit_enable)
1308 + return;
1309 +
1310 + memset(&ctx, 0, sizeof(ctx));
1311 +
1312 + ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
1313 + if (ctx.offsets == NULL)
1314 + return;
1315 +
1316 + ctx.skf = fp;
1317 +
1318 + if (build_body(&ctx))
1319 + goto out;
1320 +
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);
1326 +
1327 + alloc_size = 4 * ctx.idx;
1328 + ctx.target = module_alloc(alloc_size);
1329 + if (ctx.target == NULL)
1330 + goto out;
1331 +
1332 + /* Clean it */
1333 + memset(ctx.target, 0, alloc_size);
1334 +
1335 + ctx.idx = 0;
1336 +
1337 + /* Generate the actual JIT code */
1338 + build_prologue(&ctx);
1339 + build_body(&ctx);
1340 + build_epilogue(&ctx);
1341 +
1342 + /* Update the icache */
1343 + flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1344 +
1345 + if (bpf_jit_enable > 1)
1346 + /* Dump JIT code */
1347 + bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1348 +
1349 + fp->bpf_func = (void *)ctx.target;
1350 + fp->jited = 1;
1351 +
1352 +out:
1353 + kfree(ctx.offsets);
1354 +}
1355 +
1356 +void bpf_jit_free(struct bpf_prog *fp)
1357 +{
1358 + if (fp->jited)
1359 + module_memfree(fp->bpf_func);
1360 +
1361 + bpf_prog_unlock_free(fp);
1362 +}
1363 --- /dev/null
1364 +++ b/arch/mips/net/bpf_jit_asm.S
1365 @@ -0,0 +1,285 @@
1366 +/*
1367 + * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
1368 + * compiler.
1369 + *
1370 + * Copyright (C) 2015 Imagination Technologies Ltd.
1371 + * Author: Markos Chandras <markos.chandras@imgtec.com>
1372 + *
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.
1376 + */
1377 +
1378 +#include <asm/asm.h>
1379 +#include <asm/isa-rev.h>
1380 +#include <asm/regdef.h>
1381 +#include "bpf_jit.h"
1382 +
1383 +/* ABI
1384 + *
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
1390 + * r_skb(a0) *skb
1391 + * r_M *scratch memory
1392 + * r_skb_le skb length
1393 + * r_s0 Scratch register 0
1394 + * r_s1 Scratch register 1
1395 + *
1396 + * On entry:
1397 + * a0: *skb
1398 + * a1: offset (imm or imm + X)
1399 + *
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.
1403 + */
1404 +
1405 +#define skb a0
1406 +#define offset a1
1407 +#define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
1408 +
1409 + /* We know better :) so prevent assembler reordering etc */
1410 + .set noreorder
1411 +
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. */
1417 +
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; \
1423 +
1424 +LEAF(sk_load_word)
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
1430 + .set reorder
1431 + lw $r_A, 0(t1)
1432 + .set noreorder
1433 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1434 +# if MIPS_ISA_REV >= 2
1435 + wsbh t0, $r_A
1436 + rotr $r_A, t0, 16
1437 +# else
1438 + sll t0, $r_A, 24
1439 + srl t1, $r_A, 24
1440 + srl t2, $r_A, 8
1441 + or t0, t0, t1
1442 + andi t2, t2, 0xff00
1443 + andi t1, $r_A, 0xff00
1444 + or t0, t0, t2
1445 + sll t1, t1, 8
1446 + or $r_A, t0, t1
1447 +# endif
1448 +#endif
1449 + jr $r_ra
1450 + move $r_ret, zero
1451 + END(sk_load_word)
1452 +
1453 +LEAF(sk_load_half)
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
1459 + lhu $r_A, 0(t1)
1460 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1461 +# if MIPS_ISA_REV >= 2
1462 + wsbh $r_A, $r_A
1463 +# else
1464 + sll t0, $r_A, 8
1465 + srl t1, $r_A, 8
1466 + andi t0, t0, 0xff00
1467 + or $r_A, t0, t1
1468 +# endif
1469 +#endif
1470 + jr $r_ra
1471 + move $r_ret, zero
1472 + END(sk_load_half)
1473 +
1474 +LEAF(sk_load_byte)
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
1480 + lbu $r_A, 0(t1)
1481 + jr $r_ra
1482 + move $r_ret, zero
1483 + END(sk_load_byte)
1484 +
1485 +/*
1486 + * call skb_copy_bits:
1487 + * (prototype in linux/skbuff.h)
1488 + *
1489 + * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
1490 + *
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).
1496 + *
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.
1500 + *
1501 + * (void *to) is returned in r_s0
1502 + *
1503 + */
1504 +#ifdef CONFIG_CPU_LITTLE_ENDIAN
1505 +#define DS_OFFSET(SIZE) (4 * SZREG)
1506 +#else
1507 +#define DS_OFFSET(SIZE) ((4 * SZREG) + (4 - SIZE))
1508 +#endif
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); \
1521 + jalr t0; \
1522 + /* Reset our destination slot (DS but it's ok) */ \
1523 + INT_S zero, (4 * SZREG)($r_sp); \
1524 + /* \
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. \
1528 + */ \
1529 + /* Restore our frame */ \
1530 + PTR_L $r_ra, (5 * SZREG)($r_sp); \
1531 + INT_L $r_s0, (4 * SZREG)($r_sp); \
1532 + bltz v0, fault; \
1533 + PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
1534 + move $r_ret, zero; \
1535 +
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
1540 + wsbh t0, $r_s0
1541 + jr $r_ra
1542 + rotr $r_A, t0, 16
1543 +# else
1544 + sll t0, $r_s0, 24
1545 + srl t1, $r_s0, 24
1546 + srl t2, $r_s0, 8
1547 + or t0, t0, t1
1548 + andi t2, t2, 0xff00
1549 + andi t1, $r_s0, 0xff00
1550 + or t0, t0, t2
1551 + sll t1, t1, 8
1552 + jr $r_ra
1553 + or $r_A, t0, t1
1554 +# endif
1555 +#else
1556 + jr $r_ra
1557 + move $r_A, $r_s0
1558 +#endif
1559 +
1560 + END(bpf_slow_path_word)
1561 +
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
1566 + jr $r_ra
1567 + wsbh $r_A, $r_s0
1568 +# else
1569 + sll t0, $r_s0, 8
1570 + andi t1, $r_s0, 0xff00
1571 + andi t0, t0, 0xff00
1572 + srl t1, t1, 8
1573 + jr $r_ra
1574 + or $r_A, t0, t1
1575 +# endif
1576 +#else
1577 + jr $r_ra
1578 + move $r_A, $r_s0
1579 +#endif
1580 +
1581 + END(bpf_slow_path_half)
1582 +
1583 +NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
1584 + bpf_slow_path_common(1)
1585 + jr $r_ra
1586 + move $r_A, $r_s0
1587 +
1588 + END(bpf_slow_path_byte)
1589 +
1590 +/*
1591 + * Negative entry points
1592 + */
1593 + .macro bpf_is_end_of_data
1594 + li t0, SKF_LL_OFF
1595 + /* Reading link layer data? */
1596 + slt t1, offset, t0
1597 + bgtz t1, fault
1598 + /* Be careful what follows in DS. */
1599 + .endm
1600 +/*
1601 + * call skb_copy_bits:
1602 + * (prototype in linux/filter.h)
1603 + *
1604 + * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
1605 + * int k, unsigned int size)
1606 + *
1607 + * see above (bpf_slow_path_common) for ABI restrictions
1608 + */
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); \
1613 + jalr t0; \
1614 + li a2, SIZE; \
1615 + PTR_L $r_ra, (5 * SZREG)($r_sp); \
1616 + /* Check return pointer */ \
1617 + beqz v0, fault; \
1618 + PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
1619 + /* Preserve our pointer */ \
1620 + move $r_s0, v0; \
1621 + /* Set return value */ \
1622 + move $r_ret, zero; \
1623 +
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)
1628 + jr $r_ra
1629 + lw $r_A, 0($r_s0)
1630 + END(sk_load_word_negative)
1631 +
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)
1636 + jr $r_ra
1637 + lhu $r_A, 0($r_s0)
1638 + END(sk_load_half_negative)
1639 +
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)
1644 + jr $r_ra
1645 + lbu $r_A, 0($r_s0)
1646 + END(sk_load_byte_negative)
1647 +
1648 +fault:
1649 + jr $r_ra
1650 + addiu $r_ret, zero, 1