blob: 6e3963425b64cfe753f1df2abaa04d4fc559d630 [file] [log] [blame]
Markos Chandrasc6610de2014-04-08 12:47:14 +01001/*
2 * Just-In-Time compiler for BPF filters on MIPS
3 *
4 * Copyright (c) 2014 Imagination Technologies Ltd.
5 * Author: Markos Chandras <markos.chandras@imgtec.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
10 */
11
12#include <linux/bitops.h>
13#include <linux/compiler.h>
14#include <linux/errno.h>
15#include <linux/filter.h>
16#include <linux/if_vlan.h>
17#include <linux/kconfig.h>
18#include <linux/moduleloader.h>
19#include <linux/netdevice.h>
20#include <linux/string.h>
21#include <linux/slab.h>
22#include <linux/types.h>
23#include <asm/bitops.h>
24#include <asm/cacheflush.h>
25#include <asm/cpu-features.h>
26#include <asm/uasm.h>
27
28#include "bpf_jit.h"
29
30/* ABI
31 *
32 * s0 1st scratch register
33 * s1 2nd scratch register
34 * s2 offset register
35 * s3 BPF register A
36 * s4 BPF register X
37 * s5 *skb
38 * s6 *scratch memory
39 *
40 * On entry (*bpf_func)(*skb, *filter)
41 * a0 = MIPS_R_A0 = skb;
42 * a1 = MIPS_R_A1 = filter;
43 *
44 * Stack
45 * ...
46 * M[15]
47 * M[14]
48 * M[13]
49 * ...
50 * M[0] <-- r_M
51 * saved reg k-1
52 * saved reg k-2
53 * ...
54 * saved reg 0 <-- r_sp
55 * <no argument area>
56 *
57 * Packet layout
58 *
59 * <--------------------- len ------------------------>
60 * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
61 * ----------------------------------------------------
62 * | skb->data |
63 * ----------------------------------------------------
64 */
65
66#define RSIZE (sizeof(unsigned long))
67#define ptr typeof(unsigned long)
68
69/* ABI specific return values */
70#ifdef CONFIG_32BIT /* O32 */
71#ifdef CONFIG_CPU_LITTLE_ENDIAN
72#define r_err MIPS_R_V1
73#define r_val MIPS_R_V0
74#else /* CONFIG_CPU_LITTLE_ENDIAN */
75#define r_err MIPS_R_V0
76#define r_val MIPS_R_V1
77#endif
78#else /* N64 */
79#define r_err MIPS_R_V0
80#define r_val MIPS_R_V0
81#endif
82
83#define r_ret MIPS_R_V0
84
85/*
86 * Use 2 scratch registers to avoid pipeline interlocks.
87 * There is no overhead during epilogue and prologue since
88 * any of the $s0-$s6 registers will only be preserved if
89 * they are going to actually be used.
90 */
91#define r_s0 MIPS_R_S0 /* scratch reg 1 */
92#define r_s1 MIPS_R_S1 /* scratch reg 2 */
93#define r_off MIPS_R_S2
94#define r_A MIPS_R_S3
95#define r_X MIPS_R_S4
96#define r_skb MIPS_R_S5
97#define r_M MIPS_R_S6
98#define r_tmp_imm MIPS_R_T6 /* No need to preserve this */
99#define r_tmp MIPS_R_T7 /* No need to preserve this */
100#define r_zero MIPS_R_ZERO
101#define r_sp MIPS_R_SP
102#define r_ra MIPS_R_RA
103
104#define SCRATCH_OFF(k) (4 * (k))
105
106/* JIT flags */
107#define SEEN_CALL (1 << BPF_MEMWORDS)
108#define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
109#define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
110#define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
111#define SEEN_S0 SEEN_SREG(0)
112#define SEEN_S1 SEEN_SREG(1)
113#define SEEN_OFF SEEN_SREG(2)
114#define SEEN_A SEEN_SREG(3)
115#define SEEN_X SEEN_SREG(4)
116#define SEEN_SKB SEEN_SREG(5)
117#define SEEN_MEM SEEN_SREG(6)
118
119/* Arguments used by JIT */
120#define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
121
Markos Chandrasc6610de2014-04-08 12:47:14 +0100122#define SBIT(x) (1 << (x)) /* Signed version of BIT() */
123
124/**
125 * struct jit_ctx - JIT context
126 * @skf: The sk_filter
127 * @prologue_bytes: Number of bytes for prologue
128 * @idx: Instruction index
129 * @flags: JIT flags
130 * @offsets: Instruction offsets
131 * @target: Memory location for the compiled filter
132 */
133struct jit_ctx {
134 const struct sk_filter *skf;
135 unsigned int prologue_bytes;
136 u32 idx;
137 u32 flags;
138 u32 *offsets;
139 u32 *target;
140};
141
142
143static inline int optimize_div(u32 *k)
144{
145 /* power of 2 divides can be implemented with right shift */
146 if (!(*k & (*k-1))) {
147 *k = ilog2(*k);
148 return 1;
149 }
150
151 return 0;
152}
153
Markos Chandras95782bf2014-06-25 09:37:21 +0100154static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
155
Markos Chandrasc6610de2014-04-08 12:47:14 +0100156/* Simply emit the instruction if the JIT memory space has been allocated */
157#define emit_instr(ctx, func, ...) \
158do { \
159 if ((ctx)->target != NULL) { \
160 u32 *p = &(ctx)->target[ctx->idx]; \
161 uasm_i_##func(&p, ##__VA_ARGS__); \
162 } \
163 (ctx)->idx++; \
164} while (0)
165
166/* Determine if immediate is within the 16-bit signed range */
167static inline bool is_range16(s32 imm)
168{
Markos Chandras10c4d612014-06-23 10:38:55 +0100169 return !(imm >= SBIT(15) || imm < -SBIT(15));
Markos Chandrasc6610de2014-04-08 12:47:14 +0100170}
171
172static inline void emit_addu(unsigned int dst, unsigned int src1,
173 unsigned int src2, struct jit_ctx *ctx)
174{
175 emit_instr(ctx, addu, dst, src1, src2);
176}
177
178static inline void emit_nop(struct jit_ctx *ctx)
179{
180 emit_instr(ctx, nop);
181}
182
183/* Load a u32 immediate to a register */
184static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
185{
186 if (ctx->target != NULL) {
187 /* addiu can only handle s16 */
Markos Chandras10c4d612014-06-23 10:38:55 +0100188 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100189 u32 *p = &ctx->target[ctx->idx];
190 uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
191 p = &ctx->target[ctx->idx + 1];
192 uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
193 } else {
194 u32 *p = &ctx->target[ctx->idx];
195 uasm_i_addiu(&p, dst, r_zero, imm);
196 }
197 }
198 ctx->idx++;
199
Markos Chandras10c4d612014-06-23 10:38:55 +0100200 if (!is_range16(imm))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100201 ctx->idx++;
202}
203
204static inline void emit_or(unsigned int dst, unsigned int src1,
205 unsigned int src2, struct jit_ctx *ctx)
206{
207 emit_instr(ctx, or, dst, src1, src2);
208}
209
210static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
211 struct jit_ctx *ctx)
212{
213 if (imm >= BIT(16)) {
214 emit_load_imm(r_tmp, imm, ctx);
215 emit_or(dst, src, r_tmp, ctx);
216 } else {
217 emit_instr(ctx, ori, dst, src, imm);
218 }
219}
220
221
222static inline void emit_daddu(unsigned int dst, unsigned int src1,
223 unsigned int src2, struct jit_ctx *ctx)
224{
225 emit_instr(ctx, daddu, dst, src1, src2);
226}
227
228static inline void emit_daddiu(unsigned int dst, unsigned int src,
229 int imm, struct jit_ctx *ctx)
230{
231 /*
232 * Only used for stack, so the imm is relatively small
233 * and it fits in 15-bits
234 */
235 emit_instr(ctx, daddiu, dst, src, imm);
236}
237
238static inline void emit_addiu(unsigned int dst, unsigned int src,
239 u32 imm, struct jit_ctx *ctx)
240{
Markos Chandras10c4d612014-06-23 10:38:55 +0100241 if (!is_range16(imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100242 emit_load_imm(r_tmp, imm, ctx);
243 emit_addu(dst, r_tmp, src, ctx);
244 } else {
245 emit_instr(ctx, addiu, dst, src, imm);
246 }
247}
248
249static inline void emit_and(unsigned int dst, unsigned int src1,
250 unsigned int src2, struct jit_ctx *ctx)
251{
252 emit_instr(ctx, and, dst, src1, src2);
253}
254
255static inline void emit_andi(unsigned int dst, unsigned int src,
256 u32 imm, struct jit_ctx *ctx)
257{
258 /* If imm does not fit in u16 then load it to register */
259 if (imm >= BIT(16)) {
260 emit_load_imm(r_tmp, imm, ctx);
261 emit_and(dst, src, r_tmp, ctx);
262 } else {
263 emit_instr(ctx, andi, dst, src, imm);
264 }
265}
266
267static inline void emit_xor(unsigned int dst, unsigned int src1,
268 unsigned int src2, struct jit_ctx *ctx)
269{
270 emit_instr(ctx, xor, dst, src1, src2);
271}
272
273static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
274{
275 /* If imm does not fit in u16 then load it to register */
276 if (imm >= BIT(16)) {
277 emit_load_imm(r_tmp, imm, ctx);
278 emit_xor(dst, src, r_tmp, ctx);
279 } else {
280 emit_instr(ctx, xori, dst, src, imm);
281 }
282}
283
284static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
285{
286 if (config_enabled(CONFIG_64BIT))
287 emit_instr(ctx, daddiu, r_sp, r_sp, offset);
288 else
289 emit_instr(ctx, addiu, r_sp, r_sp, offset);
290
291}
292
293static inline void emit_subu(unsigned int dst, unsigned int src1,
294 unsigned int src2, struct jit_ctx *ctx)
295{
296 emit_instr(ctx, subu, dst, src1, src2);
297}
298
299static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
300{
301 emit_subu(reg, r_zero, reg, ctx);
302}
303
304static inline void emit_sllv(unsigned int dst, unsigned int src,
305 unsigned int sa, struct jit_ctx *ctx)
306{
307 emit_instr(ctx, sllv, dst, src, sa);
308}
309
310static inline void emit_sll(unsigned int dst, unsigned int src,
311 unsigned int sa, struct jit_ctx *ctx)
312{
313 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100314 if (sa >= BIT(5))
315 /* Shifting >= 32 results in zero */
316 emit_jit_reg_move(dst, r_zero, ctx);
317 else
318 emit_instr(ctx, sll, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100319}
320
321static inline void emit_srlv(unsigned int dst, unsigned int src,
322 unsigned int sa, struct jit_ctx *ctx)
323{
324 emit_instr(ctx, srlv, dst, src, sa);
325}
326
327static inline void emit_srl(unsigned int dst, unsigned int src,
328 unsigned int sa, struct jit_ctx *ctx)
329{
330 /* sa is 5-bits long */
Markos Chandras95782bf2014-06-25 09:37:21 +0100331 if (sa >= BIT(5))
332 /* Shifting >= 32 results in zero */
333 emit_jit_reg_move(dst, r_zero, ctx);
334 else
335 emit_instr(ctx, srl, dst, src, sa);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100336}
337
Markos Chandras55393ee2014-06-23 10:38:48 +0100338static inline void emit_slt(unsigned int dst, unsigned int src1,
339 unsigned int src2, struct jit_ctx *ctx)
340{
341 emit_instr(ctx, slt, dst, src1, src2);
342}
343
Markos Chandrasc6610de2014-04-08 12:47:14 +0100344static inline void emit_sltu(unsigned int dst, unsigned int src1,
345 unsigned int src2, struct jit_ctx *ctx)
346{
347 emit_instr(ctx, sltu, dst, src1, src2);
348}
349
350static inline void emit_sltiu(unsigned dst, unsigned int src,
351 unsigned int imm, struct jit_ctx *ctx)
352{
353 /* 16 bit immediate */
Markos Chandras10c4d612014-06-23 10:38:55 +0100354 if (!is_range16((s32)imm)) {
Markos Chandrasc6610de2014-04-08 12:47:14 +0100355 emit_load_imm(r_tmp, imm, ctx);
356 emit_sltu(dst, src, r_tmp, ctx);
357 } else {
358 emit_instr(ctx, sltiu, dst, src, imm);
359 }
360
361}
362
363/* Store register on the stack */
364static inline void emit_store_stack_reg(ptr reg, ptr base,
365 unsigned int offset,
366 struct jit_ctx *ctx)
367{
368 if (config_enabled(CONFIG_64BIT))
369 emit_instr(ctx, sd, reg, offset, base);
370 else
371 emit_instr(ctx, sw, reg, offset, base);
372}
373
374static inline void emit_store(ptr reg, ptr base, unsigned int offset,
375 struct jit_ctx *ctx)
376{
377 emit_instr(ctx, sw, reg, offset, base);
378}
379
380static inline void emit_load_stack_reg(ptr reg, ptr base,
381 unsigned int offset,
382 struct jit_ctx *ctx)
383{
384 if (config_enabled(CONFIG_64BIT))
385 emit_instr(ctx, ld, reg, offset, base);
386 else
387 emit_instr(ctx, lw, reg, offset, base);
388}
389
390static inline void emit_load(unsigned int reg, unsigned int base,
391 unsigned int offset, struct jit_ctx *ctx)
392{
393 emit_instr(ctx, lw, reg, offset, base);
394}
395
396static inline void emit_load_byte(unsigned int reg, unsigned int base,
397 unsigned int offset, struct jit_ctx *ctx)
398{
399 emit_instr(ctx, lb, reg, offset, base);
400}
401
402static inline void emit_half_load(unsigned int reg, unsigned int base,
403 unsigned int offset, struct jit_ctx *ctx)
404{
405 emit_instr(ctx, lh, reg, offset, base);
406}
407
408static inline void emit_mul(unsigned int dst, unsigned int src1,
409 unsigned int src2, struct jit_ctx *ctx)
410{
411 emit_instr(ctx, mul, dst, src1, src2);
412}
413
414static inline void emit_div(unsigned int dst, unsigned int src,
415 struct jit_ctx *ctx)
416{
417 if (ctx->target != NULL) {
418 u32 *p = &ctx->target[ctx->idx];
419 uasm_i_divu(&p, dst, src);
420 p = &ctx->target[ctx->idx + 1];
Markos Chandras35a8e162014-06-23 10:38:47 +0100421 uasm_i_mflo(&p, dst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100422 }
423 ctx->idx += 2; /* 2 insts */
424}
425
426static inline void emit_mod(unsigned int dst, unsigned int src,
427 struct jit_ctx *ctx)
428{
429 if (ctx->target != NULL) {
430 u32 *p = &ctx->target[ctx->idx];
431 uasm_i_divu(&p, dst, src);
432 p = &ctx->target[ctx->idx + 1];
433 uasm_i_mflo(&p, dst);
434 }
435 ctx->idx += 2; /* 2 insts */
436}
437
438static inline void emit_dsll(unsigned int dst, unsigned int src,
439 unsigned int sa, struct jit_ctx *ctx)
440{
441 emit_instr(ctx, dsll, dst, src, sa);
442}
443
444static inline void emit_dsrl32(unsigned int dst, unsigned int src,
445 unsigned int sa, struct jit_ctx *ctx)
446{
447 emit_instr(ctx, dsrl32, dst, src, sa);
448}
449
450static inline void emit_wsbh(unsigned int dst, unsigned int src,
451 struct jit_ctx *ctx)
452{
453 emit_instr(ctx, wsbh, dst, src);
454}
455
Markos Chandrasb6a14a92014-06-25 09:39:38 +0100456/* load pointer to register */
457static inline void emit_load_ptr(unsigned int dst, unsigned int src,
458 int imm, struct jit_ctx *ctx)
459{
460 /* src contains the base addr of the 32/64-pointer */
461 if (config_enabled(CONFIG_64BIT))
462 emit_instr(ctx, ld, dst, imm, src);
463 else
464 emit_instr(ctx, lw, dst, imm, src);
465}
466
Markos Chandrasc6610de2014-04-08 12:47:14 +0100467/* load a function pointer to register */
468static inline void emit_load_func(unsigned int reg, ptr imm,
469 struct jit_ctx *ctx)
470{
471 if (config_enabled(CONFIG_64BIT)) {
472 /* At this point imm is always 64-bit */
473 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
474 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
475 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
476 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
477 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
478 } else {
479 emit_load_imm(reg, imm, ctx);
480 }
481}
482
483/* Move to real MIPS register */
484static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
485{
486 if (config_enabled(CONFIG_64BIT))
487 emit_daddu(dst, src, r_zero, ctx);
488 else
489 emit_addu(dst, src, r_zero, ctx);
490}
491
492/* Move to JIT (32-bit) register */
493static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
494{
495 emit_addu(dst, src, r_zero, ctx);
496}
497
498/* Compute the immediate value for PC-relative branches. */
499static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
500{
501 if (ctx->target == NULL)
502 return 0;
503
504 /*
505 * We want a pc-relative branch. We only do forward branches
506 * so tgt is always after pc. tgt is the instruction offset
507 * we want to jump to.
508
509 * Branch on MIPS:
510 * I: target_offset <- sign_extend(offset)
511 * I+1: PC += target_offset (delay slot)
512 *
513 * ctx->idx currently points to the branch instruction
514 * but the offset is added to the delay slot so we need
515 * to subtract 4.
516 */
517 return ctx->offsets[tgt] -
518 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
519}
520
521static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
522 unsigned int imm, struct jit_ctx *ctx)
523{
524 if (ctx->target != NULL) {
525 u32 *p = &ctx->target[ctx->idx];
526
527 switch (cond) {
528 case MIPS_COND_EQ:
529 uasm_i_beq(&p, reg1, reg2, imm);
530 break;
531 case MIPS_COND_NE:
532 uasm_i_bne(&p, reg1, reg2, imm);
533 break;
534 case MIPS_COND_ALL:
535 uasm_i_b(&p, imm);
536 break;
537 default:
538 pr_warn("%s: Unhandled branch conditional: %d\n",
539 __func__, cond);
540 }
541 }
542 ctx->idx++;
543}
544
545static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
546{
547 emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
548}
549
550static inline void emit_jalr(unsigned int link, unsigned int reg,
551 struct jit_ctx *ctx)
552{
553 emit_instr(ctx, jalr, link, reg);
554}
555
556static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
557{
558 emit_instr(ctx, jr, reg);
559}
560
561static inline u16 align_sp(unsigned int num)
562{
563 /* Double word alignment for 32-bit, quadword for 64-bit */
564 unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
565 num = (num + (align - 1)) & -align;
566 return num;
567}
568
Markos Chandrasc6610de2014-04-08 12:47:14 +0100569static bool is_load_to_a(u16 inst)
570{
571 switch (inst) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200572 case BPF_LD | BPF_W | BPF_LEN:
573 case BPF_LD | BPF_W | BPF_ABS:
574 case BPF_LD | BPF_H | BPF_ABS:
575 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100576 return true;
577 default:
578 return false;
579 }
580}
581
582static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
583{
584 int i = 0, real_off = 0;
585 u32 sflags, tmp_flags;
586
587 /* Adjust the stack pointer */
588 emit_stack_offset(-align_sp(offset), ctx);
589
590 if (ctx->flags & SEEN_CALL) {
591 /* Argument save area */
592 if (config_enabled(CONFIG_64BIT))
593 /* Bottom of current frame */
594 real_off = align_sp(offset) - RSIZE;
595 else
596 /* Top of previous frame */
597 real_off = align_sp(offset) + RSIZE;
598 emit_store_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
599 emit_store_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
600
601 real_off = 0;
602 }
603
604 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
605 /* sflags is essentially a bitmap */
606 while (tmp_flags) {
607 if ((sflags >> i) & 0x1) {
608 emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
609 ctx);
610 real_off += RSIZE;
611 }
612 i++;
613 tmp_flags >>= 1;
614 }
615
616 /* save return address */
617 if (ctx->flags & SEEN_CALL) {
618 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
619 real_off += RSIZE;
620 }
621
622 /* Setup r_M leaving the alignment gap if necessary */
623 if (ctx->flags & SEEN_MEM) {
624 if (real_off % (RSIZE * 2))
625 real_off += RSIZE;
626 emit_addiu(r_M, r_sp, real_off, ctx);
627 }
628}
629
630static void restore_bpf_jit_regs(struct jit_ctx *ctx,
631 unsigned int offset)
632{
633 int i, real_off = 0;
634 u32 sflags, tmp_flags;
635
636 if (ctx->flags & SEEN_CALL) {
637 if (config_enabled(CONFIG_64BIT))
638 /* Bottom of current frame */
639 real_off = align_sp(offset) - RSIZE;
640 else
641 /* Top of previous frame */
642 real_off = align_sp(offset) + RSIZE;
643 emit_load_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
644 emit_load_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
645
646 real_off = 0;
647 }
648
649 tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
650 /* sflags is a bitmap */
651 i = 0;
652 while (tmp_flags) {
653 if ((sflags >> i) & 0x1) {
654 emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
655 ctx);
656 real_off += RSIZE;
657 }
658 i++;
659 tmp_flags >>= 1;
660 }
661
662 /* restore return address */
663 if (ctx->flags & SEEN_CALL)
664 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
665
666 /* Restore the sp and discard the scrach memory */
667 emit_stack_offset(align_sp(offset), ctx);
668}
669
670static unsigned int get_stack_depth(struct jit_ctx *ctx)
671{
672 int sp_off = 0;
673
674
675 /* How may s* regs do we need to preserved? */
676 sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * RSIZE;
677
678 if (ctx->flags & SEEN_MEM)
679 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
680
681 if (ctx->flags & SEEN_CALL)
682 /*
683 * The JIT code make calls to external functions using 2
684 * arguments. Therefore, for o32 we don't need to allocate
685 * space because we don't care if the argumetns are lost
686 * across calls. We do need however to preserve incoming
687 * arguments but the space is already allocated for us by
688 * the caller. On the other hand, for n64, we need to allocate
689 * this space ourselves. We need to preserve $ra as well.
690 */
691 sp_off += config_enabled(CONFIG_64BIT) ?
692 (ARGS_USED_BY_JIT + 1) * RSIZE : RSIZE;
693
694 /*
695 * Subtract the bytes for the last registers since we only care about
696 * the location on the stack pointer.
697 */
698 return sp_off - RSIZE;
699}
700
701static void build_prologue(struct jit_ctx *ctx)
702{
703 u16 first_inst = ctx->skf->insns[0].code;
704 int sp_off;
705
706 /* Calculate the total offset for the stack pointer */
707 sp_off = get_stack_depth(ctx);
708 save_bpf_jit_regs(ctx, sp_off);
709
710 if (ctx->flags & SEEN_SKB)
711 emit_reg_move(r_skb, MIPS_R_A0, ctx);
712
Markos Chandrase5bb48b2014-06-23 10:38:56 +0100713 if (ctx->flags & SEEN_X)
Markos Chandrasc6610de2014-04-08 12:47:14 +0100714 emit_jit_reg_move(r_X, r_zero, ctx);
715
716 /* Do not leak kernel data to userspace */
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200717 if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
Markos Chandrasc6610de2014-04-08 12:47:14 +0100718 emit_jit_reg_move(r_A, r_zero, ctx);
719}
720
721static void build_epilogue(struct jit_ctx *ctx)
722{
723 unsigned int sp_off;
724
725 /* Calculate the total offset for the stack pointer */
726
727 sp_off = get_stack_depth(ctx);
728 restore_bpf_jit_regs(ctx, sp_off);
729
730 /* Return */
731 emit_jr(r_ra, ctx);
732 emit_nop(ctx);
733}
734
735static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
736{
737 u8 ret;
738 int err;
739
740 err = skb_copy_bits(skb, offset, &ret, 1);
741
742 return (u64)err << 32 | ret;
743}
744
745static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
746{
747 u16 ret;
748 int err;
749
750 err = skb_copy_bits(skb, offset, &ret, 2);
751
752 return (u64)err << 32 | ntohs(ret);
753}
754
755static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
756{
757 u32 ret;
758 int err;
759
760 err = skb_copy_bits(skb, offset, &ret, 4);
761
762 return (u64)err << 32 | ntohl(ret);
763}
764
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +0100765#ifdef __BIG_ENDIAN_BITFIELD
766#define PKT_TYPE_MAX (7 << 5)
767#else
768#define PKT_TYPE_MAX 7
769#endif
Markos Chandrasc6610de2014-04-08 12:47:14 +0100770static int pkt_type_offset(void)
771{
772 struct sk_buff skb_probe = {
773 .pkt_type = ~0,
774 };
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +0100775 u8 *ct = (u8 *)&skb_probe;
Markos Chandrasc6610de2014-04-08 12:47:14 +0100776 unsigned int off;
777
778 for (off = 0; off < sizeof(struct sk_buff); off++) {
779 if (ct[off] == PKT_TYPE_MAX)
780 return off;
781 }
782 pr_err_once("Please fix pkt_type_offset(), as pkt_type couldn't be found\n");
783 return -1;
784}
785
786static int build_body(struct jit_ctx *ctx)
787{
788 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
789 const struct sk_filter *prog = ctx->skf;
790 const struct sock_filter *inst;
791 unsigned int i, off, load_order, condt;
792 u32 k, b_off __maybe_unused;
793
794 for (i = 0; i < prog->len; i++) {
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200795 u16 code;
796
Markos Chandrasc6610de2014-04-08 12:47:14 +0100797 inst = &(prog->insns[i]);
798 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
799 __func__, inst->code, inst->jt, inst->jf, inst->k);
800 k = inst->k;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200801 code = bpf_anc_helper(inst);
Markos Chandrasc6610de2014-04-08 12:47:14 +0100802
803 if (ctx->target == NULL)
804 ctx->offsets[i] = ctx->idx * 4;
805
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200806 switch (code) {
807 case BPF_LD | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100808 /* A <- k ==> li r_A, k */
809 ctx->flags |= SEEN_A;
810 emit_load_imm(r_A, k, ctx);
811 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200812 case BPF_LD | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100813 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
814 /* A <- len ==> lw r_A, offset(skb) */
815 ctx->flags |= SEEN_SKB | SEEN_A;
816 off = offsetof(struct sk_buff, len);
817 emit_load(r_A, r_skb, off, ctx);
818 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200819 case BPF_LD | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100820 /* A <- M[k] ==> lw r_A, offset(M) */
821 ctx->flags |= SEEN_MEM | SEEN_A;
822 emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
823 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200824 case BPF_LD | BPF_W | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100825 /* A <- P[k:4] */
826 load_order = 2;
827 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200828 case BPF_LD | BPF_H | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100829 /* A <- P[k:2] */
830 load_order = 1;
831 goto load;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200832 case BPF_LD | BPF_B | BPF_ABS:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100833 /* A <- P[k:1] */
834 load_order = 0;
835load:
Markos Chandras55393ee2014-06-23 10:38:48 +0100836 /* the interpreter will deal with the negative K */
837 if ((int)k < 0)
838 return -ENOTSUPP;
839
Markos Chandrasc6610de2014-04-08 12:47:14 +0100840 emit_load_imm(r_off, k, ctx);
841load_common:
Markos Chandras55393ee2014-06-23 10:38:48 +0100842 /*
843 * We may got here from the indirect loads so
844 * return if offset is negative.
845 */
846 emit_slt(r_s0, r_off, r_zero, ctx);
847 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
848 b_imm(prog->len, ctx), ctx);
849 emit_reg_move(r_ret, r_zero, ctx);
850
Markos Chandrasc6610de2014-04-08 12:47:14 +0100851 ctx->flags |= SEEN_CALL | SEEN_OFF | SEEN_S0 |
852 SEEN_SKB | SEEN_A;
853
854 emit_load_func(r_s0, (ptr)load_func[load_order],
855 ctx);
856 emit_reg_move(MIPS_R_A0, r_skb, ctx);
857 emit_jalr(MIPS_R_RA, r_s0, ctx);
858 /* Load second argument to delay slot */
859 emit_reg_move(MIPS_R_A1, r_off, ctx);
860 /* Check the error value */
861 if (config_enabled(CONFIG_64BIT)) {
862 /* Get error code from the top 32-bits */
863 emit_dsrl32(r_s0, r_val, 0, ctx);
864 /* Branch to 3 instructions ahead */
865 emit_bcond(MIPS_COND_NE, r_s0, r_zero, 3 << 2,
866 ctx);
867 } else {
868 /* Branch to 3 instructions ahead */
869 emit_bcond(MIPS_COND_NE, r_err, r_zero, 3 << 2,
870 ctx);
871 }
872 emit_nop(ctx);
873 /* We are good */
874 emit_b(b_imm(i + 1, ctx), ctx);
875 emit_jit_reg_move(r_A, r_val, ctx);
876 /* Return with error */
877 emit_b(b_imm(prog->len, ctx), ctx);
878 emit_reg_move(r_ret, r_zero, ctx);
879 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200880 case BPF_LD | BPF_W | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100881 /* A <- P[X + k:4] */
882 load_order = 2;
883 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200884 case BPF_LD | BPF_H | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100885 /* A <- P[X + k:2] */
886 load_order = 1;
887 goto load_ind;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200888 case BPF_LD | BPF_B | BPF_IND:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100889 /* A <- P[X + k:1] */
890 load_order = 0;
891load_ind:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100892 ctx->flags |= SEEN_OFF | SEEN_X;
893 emit_addiu(r_off, r_X, k, ctx);
894 goto load_common;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200895 case BPF_LDX | BPF_IMM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100896 /* X <- k */
897 ctx->flags |= SEEN_X;
898 emit_load_imm(r_X, k, ctx);
899 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200900 case BPF_LDX | BPF_MEM:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100901 /* X <- M[k] */
902 ctx->flags |= SEEN_X | SEEN_MEM;
903 emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
904 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200905 case BPF_LDX | BPF_W | BPF_LEN:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100906 /* X <- len */
907 ctx->flags |= SEEN_X | SEEN_SKB;
908 off = offsetof(struct sk_buff, len);
909 emit_load(r_X, r_skb, off, ctx);
910 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200911 case BPF_LDX | BPF_B | BPF_MSH:
Markos Chandras55393ee2014-06-23 10:38:48 +0100912 /* the interpreter will deal with the negative K */
913 if ((int)k < 0)
914 return -ENOTSUPP;
915
Markos Chandrasc6610de2014-04-08 12:47:14 +0100916 /* X <- 4 * (P[k:1] & 0xf) */
917 ctx->flags |= SEEN_X | SEEN_CALL | SEEN_S0 | SEEN_SKB;
918 /* Load offset to a1 */
919 emit_load_func(r_s0, (ptr)jit_get_skb_b, ctx);
920 /*
921 * This may emit two instructions so it may not fit
922 * in the delay slot. So use a0 in the delay slot.
923 */
924 emit_load_imm(MIPS_R_A1, k, ctx);
925 emit_jalr(MIPS_R_RA, r_s0, ctx);
926 emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
927 /* Check the error value */
928 if (config_enabled(CONFIG_64BIT)) {
929 /* Top 32-bits of $v0 on 64-bit */
930 emit_dsrl32(r_s0, r_val, 0, ctx);
931 emit_bcond(MIPS_COND_NE, r_s0, r_zero,
932 3 << 2, ctx);
933 } else {
934 emit_bcond(MIPS_COND_NE, r_err, r_zero,
935 3 << 2, ctx);
936 }
937 /* No need for delay slot */
938 /* We are good */
939 /* X <- P[1:K] & 0xf */
940 emit_andi(r_X, r_val, 0xf, ctx);
941 /* X << 2 */
942 emit_b(b_imm(i + 1, ctx), ctx);
943 emit_sll(r_X, r_X, 2, ctx); /* delay slot */
944 /* Return with error */
945 emit_b(b_imm(prog->len, ctx), ctx);
946 emit_load_imm(r_ret, 0, ctx); /* delay slot */
947 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200948 case BPF_ST:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100949 /* M[k] <- A */
950 ctx->flags |= SEEN_MEM | SEEN_A;
951 emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
952 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200953 case BPF_STX:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100954 /* M[k] <- X */
955 ctx->flags |= SEEN_MEM | SEEN_X;
956 emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
957 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200958 case BPF_ALU | BPF_ADD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100959 /* A += K */
960 ctx->flags |= SEEN_A;
961 emit_addiu(r_A, r_A, k, ctx);
962 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200963 case BPF_ALU | BPF_ADD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100964 /* A += X */
965 ctx->flags |= SEEN_A | SEEN_X;
966 emit_addu(r_A, r_A, r_X, ctx);
967 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200968 case BPF_ALU | BPF_SUB | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100969 /* A -= K */
970 ctx->flags |= SEEN_A;
971 emit_addiu(r_A, r_A, -k, ctx);
972 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200973 case BPF_ALU | BPF_SUB | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100974 /* A -= X */
975 ctx->flags |= SEEN_A | SEEN_X;
976 emit_subu(r_A, r_A, r_X, ctx);
977 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200978 case BPF_ALU | BPF_MUL | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100979 /* A *= K */
980 /* Load K to scratch register before MUL */
981 ctx->flags |= SEEN_A | SEEN_S0;
982 emit_load_imm(r_s0, k, ctx);
983 emit_mul(r_A, r_A, r_s0, ctx);
984 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200985 case BPF_ALU | BPF_MUL | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100986 /* A *= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +0100987 ctx->flags |= SEEN_A | SEEN_X;
988 emit_mul(r_A, r_A, r_X, ctx);
989 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +0200990 case BPF_ALU | BPF_DIV | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +0100991 /* A /= k */
992 if (k == 1)
993 break;
994 if (optimize_div(&k)) {
995 ctx->flags |= SEEN_A;
996 emit_srl(r_A, r_A, k, ctx);
997 break;
998 }
999 ctx->flags |= SEEN_A | SEEN_S0;
1000 emit_load_imm(r_s0, k, ctx);
1001 emit_div(r_A, r_s0, ctx);
1002 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001003 case BPF_ALU | BPF_MOD | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001004 /* A %= k */
1005 if (k == 1 || optimize_div(&k)) {
1006 ctx->flags |= SEEN_A;
1007 emit_jit_reg_move(r_A, r_zero, ctx);
1008 } else {
1009 ctx->flags |= SEEN_A | SEEN_S0;
1010 emit_load_imm(r_s0, k, ctx);
1011 emit_mod(r_A, r_s0, ctx);
1012 }
1013 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001014 case BPF_ALU | BPF_DIV | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001015 /* A /= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001016 ctx->flags |= SEEN_X | SEEN_A;
1017 /* Check if r_X is zero */
1018 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
1019 b_imm(prog->len, ctx), ctx);
1020 emit_load_imm(r_val, 0, ctx); /* delay slot */
1021 emit_div(r_A, r_X, ctx);
1022 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001023 case BPF_ALU | BPF_MOD | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001024 /* A %= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001025 ctx->flags |= SEEN_X | SEEN_A;
1026 /* Check if r_X is zero */
1027 emit_bcond(MIPS_COND_EQ, r_X, r_zero,
1028 b_imm(prog->len, ctx), ctx);
1029 emit_load_imm(r_val, 0, ctx); /* delay slot */
1030 emit_mod(r_A, r_X, ctx);
1031 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001032 case BPF_ALU | BPF_OR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001033 /* A |= K */
1034 ctx->flags |= SEEN_A;
1035 emit_ori(r_A, r_A, k, ctx);
1036 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001037 case BPF_ALU | BPF_OR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001038 /* A |= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001039 ctx->flags |= SEEN_A;
1040 emit_ori(r_A, r_A, r_X, ctx);
1041 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001042 case BPF_ALU | BPF_XOR | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001043 /* A ^= k */
1044 ctx->flags |= SEEN_A;
1045 emit_xori(r_A, r_A, k, ctx);
1046 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001047 case BPF_ANC | SKF_AD_ALU_XOR_X:
1048 case BPF_ALU | BPF_XOR | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001049 /* A ^= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001050 ctx->flags |= SEEN_A;
1051 emit_xor(r_A, r_A, r_X, ctx);
1052 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001053 case BPF_ALU | BPF_AND | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001054 /* A &= K */
1055 ctx->flags |= SEEN_A;
1056 emit_andi(r_A, r_A, k, ctx);
1057 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001058 case BPF_ALU | BPF_AND | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001059 /* A &= X */
Markos Chandrasc6610de2014-04-08 12:47:14 +01001060 ctx->flags |= SEEN_A | SEEN_X;
1061 emit_and(r_A, r_A, r_X, ctx);
1062 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001063 case BPF_ALU | BPF_LSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001064 /* A <<= K */
1065 ctx->flags |= SEEN_A;
1066 emit_sll(r_A, r_A, k, ctx);
1067 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001068 case BPF_ALU | BPF_LSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001069 /* A <<= X */
1070 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001071 emit_sllv(r_A, r_A, r_X, ctx);
1072 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001073 case BPF_ALU | BPF_RSH | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001074 /* A >>= K */
1075 ctx->flags |= SEEN_A;
1076 emit_srl(r_A, r_A, k, ctx);
1077 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001078 case BPF_ALU | BPF_RSH | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001079 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001080 emit_srlv(r_A, r_A, r_X, ctx);
1081 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001082 case BPF_ALU | BPF_NEG:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001083 /* A = -A */
1084 ctx->flags |= SEEN_A;
1085 emit_neg(r_A, ctx);
1086 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001087 case BPF_JMP | BPF_JA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001088 /* pc += K */
1089 emit_b(b_imm(i + k + 1, ctx), ctx);
1090 emit_nop(ctx);
1091 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001092 case BPF_JMP | BPF_JEQ | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001093 /* pc += ( A == K ) ? pc->jt : pc->jf */
1094 condt = MIPS_COND_EQ | MIPS_COND_K;
1095 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001096 case BPF_JMP | BPF_JEQ | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001097 ctx->flags |= SEEN_X;
1098 /* pc += ( A == X ) ? pc->jt : pc->jf */
1099 condt = MIPS_COND_EQ | MIPS_COND_X;
1100 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001101 case BPF_JMP | BPF_JGE | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001102 /* pc += ( A >= K ) ? pc->jt : pc->jf */
1103 condt = MIPS_COND_GE | MIPS_COND_K;
1104 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001105 case BPF_JMP | BPF_JGE | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001106 ctx->flags |= SEEN_X;
1107 /* pc += ( A >= X ) ? pc->jt : pc->jf */
1108 condt = MIPS_COND_GE | MIPS_COND_X;
1109 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001110 case BPF_JMP | BPF_JGT | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001111 /* pc += ( A > K ) ? pc->jt : pc->jf */
1112 condt = MIPS_COND_GT | MIPS_COND_K;
1113 goto jmp_cmp;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001114 case BPF_JMP | BPF_JGT | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001115 ctx->flags |= SEEN_X;
1116 /* pc += ( A > X ) ? pc->jt : pc->jf */
1117 condt = MIPS_COND_GT | MIPS_COND_X;
1118jmp_cmp:
1119 /* Greater or Equal */
1120 if ((condt & MIPS_COND_GE) ||
1121 (condt & MIPS_COND_GT)) {
1122 if (condt & MIPS_COND_K) { /* K */
1123 ctx->flags |= SEEN_S0 | SEEN_A;
1124 emit_sltiu(r_s0, r_A, k, ctx);
1125 } else { /* X */
1126 ctx->flags |= SEEN_S0 | SEEN_A |
1127 SEEN_X;
1128 emit_sltu(r_s0, r_A, r_X, ctx);
1129 }
1130 /* A < (K|X) ? r_scrach = 1 */
1131 b_off = b_imm(i + inst->jf + 1, ctx);
Markos Chandras1ab24a42014-06-23 10:38:51 +01001132 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
Markos Chandrasc6610de2014-04-08 12:47:14 +01001133 ctx);
1134 emit_nop(ctx);
1135 /* A > (K|X) ? scratch = 0 */
1136 if (condt & MIPS_COND_GT) {
1137 /* Checking for equality */
1138 ctx->flags |= SEEN_S0 | SEEN_A | SEEN_X;
1139 if (condt & MIPS_COND_K)
1140 emit_load_imm(r_s0, k, ctx);
1141 else
1142 emit_jit_reg_move(r_s0, r_X,
1143 ctx);
1144 b_off = b_imm(i + inst->jf + 1, ctx);
1145 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1146 b_off, ctx);
1147 emit_nop(ctx);
1148 /* Finally, A > K|X */
1149 b_off = b_imm(i + inst->jt + 1, ctx);
1150 emit_b(b_off, ctx);
1151 emit_nop(ctx);
1152 } else {
1153 /* A >= (K|X) so jump */
1154 b_off = b_imm(i + inst->jt + 1, ctx);
1155 emit_b(b_off, ctx);
1156 emit_nop(ctx);
1157 }
1158 } else {
1159 /* A == K|X */
1160 if (condt & MIPS_COND_K) { /* K */
1161 ctx->flags |= SEEN_S0 | SEEN_A;
1162 emit_load_imm(r_s0, k, ctx);
1163 /* jump true */
1164 b_off = b_imm(i + inst->jt + 1, ctx);
1165 emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1166 b_off, ctx);
1167 emit_nop(ctx);
1168 /* jump false */
1169 b_off = b_imm(i + inst->jf + 1,
1170 ctx);
1171 emit_bcond(MIPS_COND_NE, r_A, r_s0,
1172 b_off, ctx);
1173 emit_nop(ctx);
1174 } else { /* X */
1175 /* jump true */
1176 ctx->flags |= SEEN_A | SEEN_X;
1177 b_off = b_imm(i + inst->jt + 1,
1178 ctx);
1179 emit_bcond(MIPS_COND_EQ, r_A, r_X,
1180 b_off, ctx);
1181 emit_nop(ctx);
1182 /* jump false */
1183 b_off = b_imm(i + inst->jf + 1, ctx);
1184 emit_bcond(MIPS_COND_NE, r_A, r_X,
1185 b_off, ctx);
1186 emit_nop(ctx);
1187 }
1188 }
1189 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001190 case BPF_JMP | BPF_JSET | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001191 ctx->flags |= SEEN_S0 | SEEN_S1 | SEEN_A;
1192 /* pc += (A & K) ? pc -> jt : pc -> jf */
1193 emit_load_imm(r_s1, k, ctx);
1194 emit_and(r_s0, r_A, r_s1, ctx);
1195 /* jump true */
1196 b_off = b_imm(i + inst->jt + 1, ctx);
1197 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1198 emit_nop(ctx);
1199 /* jump false */
1200 b_off = b_imm(i + inst->jf + 1, ctx);
1201 emit_b(b_off, ctx);
1202 emit_nop(ctx);
1203 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001204 case BPF_JMP | BPF_JSET | BPF_X:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001205 ctx->flags |= SEEN_S0 | SEEN_X | SEEN_A;
1206 /* pc += (A & X) ? pc -> jt : pc -> jf */
1207 emit_and(r_s0, r_A, r_X, ctx);
1208 /* jump true */
1209 b_off = b_imm(i + inst->jt + 1, ctx);
1210 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1211 emit_nop(ctx);
1212 /* jump false */
1213 b_off = b_imm(i + inst->jf + 1, ctx);
1214 emit_b(b_off, ctx);
1215 emit_nop(ctx);
1216 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001217 case BPF_RET | BPF_A:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001218 ctx->flags |= SEEN_A;
1219 if (i != prog->len - 1)
1220 /*
1221 * If this is not the last instruction
1222 * then jump to the epilogue
1223 */
1224 emit_b(b_imm(prog->len, ctx), ctx);
1225 emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1226 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001227 case BPF_RET | BPF_K:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001228 /*
1229 * It can emit two instructions so it does not fit on
1230 * the delay slot.
1231 */
1232 emit_load_imm(r_ret, k, ctx);
1233 if (i != prog->len - 1) {
1234 /*
1235 * If this is not the last instruction
1236 * then jump to the epilogue
1237 */
1238 emit_b(b_imm(prog->len, ctx), ctx);
1239 emit_nop(ctx);
1240 }
1241 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001242 case BPF_MISC | BPF_TAX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001243 /* X = A */
1244 ctx->flags |= SEEN_X | SEEN_A;
1245 emit_jit_reg_move(r_X, r_A, ctx);
1246 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001247 case BPF_MISC | BPF_TXA:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001248 /* A = X */
1249 ctx->flags |= SEEN_A | SEEN_X;
Markos Chandrasc6610de2014-04-08 12:47:14 +01001250 emit_jit_reg_move(r_A, r_X, ctx);
1251 break;
1252 /* AUX */
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001253 case BPF_ANC | SKF_AD_PROTOCOL:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001254 /* A = ntohs(skb->protocol */
1255 ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1256 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1257 protocol) != 2);
1258 off = offsetof(struct sk_buff, protocol);
1259 emit_half_load(r_A, r_skb, off, ctx);
1260#ifdef CONFIG_CPU_LITTLE_ENDIAN
1261 /* This needs little endian fixup */
Ralf Baechleb4f16c92014-06-03 13:05:55 +02001262 if (cpu_has_mips_r2) {
1263 /* R2 and later have the wsbh instruction */
1264 emit_wsbh(r_A, r_A, ctx);
1265 } else {
Markos Chandrasc6610de2014-04-08 12:47:14 +01001266 /* Get first byte */
1267 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1268 /* Shift it */
1269 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1270 /* Get second byte */
1271 emit_srl(r_tmp_imm, r_A, 8, ctx);
1272 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1273 /* Put everyting together in r_A */
1274 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001275 }
1276#endif
1277 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001278 case BPF_ANC | SKF_AD_CPU:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001279 ctx->flags |= SEEN_A | SEEN_OFF;
1280 /* A = current_thread_info()->cpu */
1281 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1282 cpu) != 4);
1283 off = offsetof(struct thread_info, cpu);
1284 /* $28/gp points to the thread_info struct */
1285 emit_load(r_A, 28, off, ctx);
1286 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001287 case BPF_ANC | SKF_AD_IFINDEX:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001288 /* A = skb->dev->ifindex */
1289 ctx->flags |= SEEN_SKB | SEEN_A | SEEN_S0;
1290 off = offsetof(struct sk_buff, dev);
Markos Chandrasb6a14a92014-06-25 09:39:38 +01001291 /* Load *dev pointer */
1292 emit_load_ptr(r_s0, r_skb, off, ctx);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001293 /* error (0) in the delay slot */
1294 emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1295 b_imm(prog->len, ctx), ctx);
1296 emit_reg_move(r_ret, r_zero, ctx);
1297 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
1298 ifindex) != 4);
1299 off = offsetof(struct net_device, ifindex);
1300 emit_load(r_A, r_s0, off, ctx);
1301 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001302 case BPF_ANC | SKF_AD_MARK:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001303 ctx->flags |= SEEN_SKB | SEEN_A;
1304 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1305 off = offsetof(struct sk_buff, mark);
1306 emit_load(r_A, r_skb, off, ctx);
1307 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001308 case BPF_ANC | SKF_AD_RXHASH:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001309 ctx->flags |= SEEN_SKB | SEEN_A;
1310 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1311 off = offsetof(struct sk_buff, hash);
1312 emit_load(r_A, r_skb, off, ctx);
1313 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001314 case BPF_ANC | SKF_AD_VLAN_TAG:
1315 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001316 ctx->flags |= SEEN_SKB | SEEN_S0 | SEEN_A;
1317 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1318 vlan_tci) != 2);
1319 off = offsetof(struct sk_buff, vlan_tci);
1320 emit_half_load(r_s0, r_skb, off, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001321 if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
Markos Chandras6e86c592014-06-23 10:38:52 +01001322 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001323 } else {
Markos Chandras9ee16062014-06-23 10:38:49 +01001324 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
Markos Chandras91a41d72014-06-23 10:38:53 +01001325 /* return 1 if present */
1326 emit_sltu(r_A, r_zero, r_A, ctx);
1327 }
Markos Chandrasc6610de2014-04-08 12:47:14 +01001328 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001329 case BPF_ANC | SKF_AD_PKTTYPE:
Markos Chandras9eebfe42014-06-23 10:38:50 +01001330 ctx->flags |= SEEN_SKB;
1331
Markos Chandrasc6610de2014-04-08 12:47:14 +01001332 off = pkt_type_offset();
1333
1334 if (off < 0)
1335 return -1;
1336 emit_load_byte(r_tmp, r_skb, off, ctx);
1337 /* Keep only the last 3 bits */
1338 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
Markos Chandrasb4fe0ec2014-06-23 10:38:58 +01001339#ifdef __BIG_ENDIAN_BITFIELD
1340 /* Get the actual packet type to the lower 3 bits */
1341 emit_srl(r_A, r_A, 5, ctx);
1342#endif
Markos Chandrasc6610de2014-04-08 12:47:14 +01001343 break;
Daniel Borkmanna83d0812014-06-17 12:16:18 +02001344 case BPF_ANC | SKF_AD_QUEUE:
Markos Chandrasc6610de2014-04-08 12:47:14 +01001345 ctx->flags |= SEEN_SKB | SEEN_A;
1346 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1347 queue_mapping) != 2);
1348 BUILD_BUG_ON(offsetof(struct sk_buff,
1349 queue_mapping) > 0xff);
1350 off = offsetof(struct sk_buff, queue_mapping);
1351 emit_half_load(r_A, r_skb, off, ctx);
1352 break;
1353 default:
Markos Chandras78b95b62014-06-23 10:38:54 +01001354 pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1355 inst->code);
Markos Chandrasc6610de2014-04-08 12:47:14 +01001356 return -1;
1357 }
1358 }
1359
1360 /* compute offsets only during the first pass */
1361 if (ctx->target == NULL)
1362 ctx->offsets[i] = ctx->idx * 4;
1363
1364 return 0;
1365}
1366
1367int bpf_jit_enable __read_mostly;
1368
1369void bpf_jit_compile(struct sk_filter *fp)
1370{
1371 struct jit_ctx ctx;
1372 unsigned int alloc_size, tmp_idx;
1373
1374 if (!bpf_jit_enable)
1375 return;
1376
1377 memset(&ctx, 0, sizeof(ctx));
1378
1379 ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL);
1380 if (ctx.offsets == NULL)
1381 return;
1382
1383 ctx.skf = fp;
1384
1385 if (build_body(&ctx))
1386 goto out;
1387
1388 tmp_idx = ctx.idx;
1389 build_prologue(&ctx);
1390 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1391 /* just to complete the ctx.idx count */
1392 build_epilogue(&ctx);
1393
1394 alloc_size = 4 * ctx.idx;
1395 ctx.target = module_alloc(alloc_size);
1396 if (ctx.target == NULL)
1397 goto out;
1398
1399 /* Clean it */
1400 memset(ctx.target, 0, alloc_size);
1401
1402 ctx.idx = 0;
1403
1404 /* Generate the actual JIT code */
1405 build_prologue(&ctx);
1406 build_body(&ctx);
1407 build_epilogue(&ctx);
1408
1409 /* Update the icache */
1410 flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1411
1412 if (bpf_jit_enable > 1)
1413 /* Dump JIT code */
1414 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1415
1416 fp->bpf_func = (void *)ctx.target;
1417 fp->jited = 1;
1418
1419out:
1420 kfree(ctx.offsets);
1421}
1422
1423void bpf_jit_free(struct sk_filter *fp)
1424{
1425 if (fp->jited)
1426 module_free(NULL, fp->bpf_func);
1427 kfree(fp);
1428}