GNU Linux-libre 4.4.297-gnu1
[releases.git] / arch / mips / net / bpf_jit.c
1 /*
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/asm.h>
24 #include <asm/bitops.h>
25 #include <asm/cacheflush.h>
26 #include <asm/cpu-features.h>
27 #include <asm/uasm.h>
28
29 #include "bpf_jit.h"
30
31 /* ABI
32  * r_skb_hl     SKB header length
33  * r_data       SKB data pointer
34  * r_off        Offset
35  * r_A          BPF register A
36  * r_X          BPF register X
37  * r_skb        *skb
38  * r_M          *scratch memory
39  * r_skb_len    SKB length
40  *
41  * On entry (*bpf_func)(*skb, *filter)
42  * a0 = MIPS_R_A0 = skb;
43  * a1 = MIPS_R_A1 = filter;
44  *
45  * Stack
46  * ...
47  * M[15]
48  * M[14]
49  * M[13]
50  * ...
51  * M[0] <-- r_M
52  * saved reg k-1
53  * saved reg k-2
54  * ...
55  * saved reg 0 <-- r_sp
56  * <no argument area>
57  *
58  *                     Packet layout
59  *
60  * <--------------------- len ------------------------>
61  * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
62  * ----------------------------------------------------
63  * |                  skb->data                       |
64  * ----------------------------------------------------
65  */
66
67 #define ptr typeof(unsigned long)
68
69 #define SCRATCH_OFF(k)          (4 * (k))
70
71 /* JIT flags */
72 #define SEEN_CALL               (1 << BPF_MEMWORDS)
73 #define SEEN_SREG_SFT           (BPF_MEMWORDS + 1)
74 #define SEEN_SREG_BASE          (1 << SEEN_SREG_SFT)
75 #define SEEN_SREG(x)            (SEEN_SREG_BASE << (x))
76 #define SEEN_OFF                SEEN_SREG(2)
77 #define SEEN_A                  SEEN_SREG(3)
78 #define SEEN_X                  SEEN_SREG(4)
79 #define SEEN_SKB                SEEN_SREG(5)
80 #define SEEN_MEM                SEEN_SREG(6)
81 /* SEEN_SK_DATA also implies skb_hl an skb_len */
82 #define SEEN_SKB_DATA           (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0))
83
84 /* Arguments used by JIT */
85 #define ARGS_USED_BY_JIT        2 /* only applicable to 64-bit */
86
87 #define SBIT(x)                 (1 << (x)) /* Signed version of BIT() */
88
89 /**
90  * struct jit_ctx - JIT context
91  * @skf:                The sk_filter
92  * @prologue_bytes:     Number of bytes for prologue
93  * @idx:                Instruction index
94  * @flags:              JIT flags
95  * @offsets:            Instruction offsets
96  * @target:             Memory location for the compiled filter
97  */
98 struct jit_ctx {
99         const struct bpf_prog *skf;
100         unsigned int prologue_bytes;
101         u32 idx;
102         u32 flags;
103         u32 *offsets;
104         u32 *target;
105 };
106
107
108 static inline int optimize_div(u32 *k)
109 {
110         /* power of 2 divides can be implemented with right shift */
111         if (!(*k & (*k-1))) {
112                 *k = ilog2(*k);
113                 return 1;
114         }
115
116         return 0;
117 }
118
119 static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
120
121 /* Simply emit the instruction if the JIT memory space has been allocated */
122 #define emit_instr(ctx, func, ...)                      \
123 do {                                                    \
124         if ((ctx)->target != NULL) {                    \
125                 u32 *p = &(ctx)->target[ctx->idx];      \
126                 uasm_i_##func(&p, ##__VA_ARGS__);       \
127         }                                               \
128         (ctx)->idx++;                                   \
129 } while (0)
130
131 /*
132  * Similar to emit_instr but it must be used when we need to emit
133  * 32-bit or 64-bit instructions
134  */
135 #define emit_long_instr(ctx, func, ...)                 \
136 do {                                                    \
137         if ((ctx)->target != NULL) {                    \
138                 u32 *p = &(ctx)->target[ctx->idx];      \
139                 UASM_i_##func(&p, ##__VA_ARGS__);       \
140         }                                               \
141         (ctx)->idx++;                                   \
142 } while (0)
143
144 /* Determine if immediate is within the 16-bit signed range */
145 static inline bool is_range16(s32 imm)
146 {
147         return !(imm >= SBIT(15) || imm < -SBIT(15));
148 }
149
150 static inline void emit_addu(unsigned int dst, unsigned int src1,
151                              unsigned int src2, struct jit_ctx *ctx)
152 {
153         emit_instr(ctx, addu, dst, src1, src2);
154 }
155
156 static inline void emit_nop(struct jit_ctx *ctx)
157 {
158         emit_instr(ctx, nop);
159 }
160
161 /* Load a u32 immediate to a register */
162 static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
163 {
164         if (ctx->target != NULL) {
165                 /* addiu can only handle s16 */
166                 if (!is_range16(imm)) {
167                         u32 *p = &ctx->target[ctx->idx];
168                         uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
169                         p = &ctx->target[ctx->idx + 1];
170                         uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
171                 } else {
172                         u32 *p = &ctx->target[ctx->idx];
173                         uasm_i_addiu(&p, dst, r_zero, imm);
174                 }
175         }
176         ctx->idx++;
177
178         if (!is_range16(imm))
179                 ctx->idx++;
180 }
181
182 static inline void emit_or(unsigned int dst, unsigned int src1,
183                            unsigned int src2, struct jit_ctx *ctx)
184 {
185         emit_instr(ctx, or, dst, src1, src2);
186 }
187
188 static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
189                             struct jit_ctx *ctx)
190 {
191         if (imm >= BIT(16)) {
192                 emit_load_imm(r_tmp, imm, ctx);
193                 emit_or(dst, src, r_tmp, ctx);
194         } else {
195                 emit_instr(ctx, ori, dst, src, imm);
196         }
197 }
198
199 static inline void emit_daddiu(unsigned int dst, unsigned int src,
200                                int imm, struct jit_ctx *ctx)
201 {
202         /*
203          * Only used for stack, so the imm is relatively small
204          * and it fits in 15-bits
205          */
206         emit_instr(ctx, daddiu, dst, src, imm);
207 }
208
209 static inline void emit_addiu(unsigned int dst, unsigned int src,
210                               u32 imm, struct jit_ctx *ctx)
211 {
212         if (!is_range16(imm)) {
213                 emit_load_imm(r_tmp, imm, ctx);
214                 emit_addu(dst, r_tmp, src, ctx);
215         } else {
216                 emit_instr(ctx, addiu, dst, src, imm);
217         }
218 }
219
220 static inline void emit_and(unsigned int dst, unsigned int src1,
221                             unsigned int src2, struct jit_ctx *ctx)
222 {
223         emit_instr(ctx, and, dst, src1, src2);
224 }
225
226 static inline void emit_andi(unsigned int dst, unsigned int src,
227                              u32 imm, struct jit_ctx *ctx)
228 {
229         /* If imm does not fit in u16 then load it to register */
230         if (imm >= BIT(16)) {
231                 emit_load_imm(r_tmp, imm, ctx);
232                 emit_and(dst, src, r_tmp, ctx);
233         } else {
234                 emit_instr(ctx, andi, dst, src, imm);
235         }
236 }
237
238 static inline void emit_xor(unsigned int dst, unsigned int src1,
239                             unsigned int src2, struct jit_ctx *ctx)
240 {
241         emit_instr(ctx, xor, dst, src1, src2);
242 }
243
244 static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
245 {
246         /* If imm does not fit in u16 then load it to register */
247         if (imm >= BIT(16)) {
248                 emit_load_imm(r_tmp, imm, ctx);
249                 emit_xor(dst, src, r_tmp, ctx);
250         } else {
251                 emit_instr(ctx, xori, dst, src, imm);
252         }
253 }
254
255 static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
256 {
257         emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
258 }
259
260 static inline void emit_subu(unsigned int dst, unsigned int src1,
261                              unsigned int src2, struct jit_ctx *ctx)
262 {
263         emit_instr(ctx, subu, dst, src1, src2);
264 }
265
266 static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
267 {
268         emit_subu(reg, r_zero, reg, ctx);
269 }
270
271 static inline void emit_sllv(unsigned int dst, unsigned int src,
272                              unsigned int sa, struct jit_ctx *ctx)
273 {
274         emit_instr(ctx, sllv, dst, src, sa);
275 }
276
277 static inline void emit_sll(unsigned int dst, unsigned int src,
278                             unsigned int sa, struct jit_ctx *ctx)
279 {
280         /* sa is 5-bits long */
281         if (sa >= BIT(5))
282                 /* Shifting >= 32 results in zero */
283                 emit_jit_reg_move(dst, r_zero, ctx);
284         else
285                 emit_instr(ctx, sll, dst, src, sa);
286 }
287
288 static inline void emit_srlv(unsigned int dst, unsigned int src,
289                              unsigned int sa, struct jit_ctx *ctx)
290 {
291         emit_instr(ctx, srlv, dst, src, sa);
292 }
293
294 static inline void emit_srl(unsigned int dst, unsigned int src,
295                             unsigned int sa, struct jit_ctx *ctx)
296 {
297         /* sa is 5-bits long */
298         if (sa >= BIT(5))
299                 /* Shifting >= 32 results in zero */
300                 emit_jit_reg_move(dst, r_zero, ctx);
301         else
302                 emit_instr(ctx, srl, dst, src, sa);
303 }
304
305 static inline void emit_slt(unsigned int dst, unsigned int src1,
306                             unsigned int src2, struct jit_ctx *ctx)
307 {
308         emit_instr(ctx, slt, dst, src1, src2);
309 }
310
311 static inline void emit_sltu(unsigned int dst, unsigned int src1,
312                              unsigned int src2, struct jit_ctx *ctx)
313 {
314         emit_instr(ctx, sltu, dst, src1, src2);
315 }
316
317 static inline void emit_sltiu(unsigned dst, unsigned int src,
318                               unsigned int imm, struct jit_ctx *ctx)
319 {
320         /* 16 bit immediate */
321         if (!is_range16((s32)imm)) {
322                 emit_load_imm(r_tmp, imm, ctx);
323                 emit_sltu(dst, src, r_tmp, ctx);
324         } else {
325                 emit_instr(ctx, sltiu, dst, src, imm);
326         }
327
328 }
329
330 /* Store register on the stack */
331 static inline void emit_store_stack_reg(ptr reg, ptr base,
332                                         unsigned int offset,
333                                         struct jit_ctx *ctx)
334 {
335         emit_long_instr(ctx, SW, reg, offset, base);
336 }
337
338 static inline void emit_store(ptr reg, ptr base, unsigned int offset,
339                               struct jit_ctx *ctx)
340 {
341         emit_instr(ctx, sw, reg, offset, base);
342 }
343
344 static inline void emit_load_stack_reg(ptr reg, ptr base,
345                                        unsigned int offset,
346                                        struct jit_ctx *ctx)
347 {
348         emit_long_instr(ctx, LW, reg, offset, base);
349 }
350
351 static inline void emit_load(unsigned int reg, unsigned int base,
352                              unsigned int offset, struct jit_ctx *ctx)
353 {
354         emit_instr(ctx, lw, reg, offset, base);
355 }
356
357 static inline void emit_load_byte(unsigned int reg, unsigned int base,
358                                   unsigned int offset, struct jit_ctx *ctx)
359 {
360         emit_instr(ctx, lb, reg, offset, base);
361 }
362
363 static inline void emit_half_load(unsigned int reg, unsigned int base,
364                                   unsigned int offset, struct jit_ctx *ctx)
365 {
366         emit_instr(ctx, lh, reg, offset, base);
367 }
368
369 static inline void emit_mul(unsigned int dst, unsigned int src1,
370                             unsigned int src2, struct jit_ctx *ctx)
371 {
372         emit_instr(ctx, mul, dst, src1, src2);
373 }
374
375 static inline void emit_div(unsigned int dst, unsigned int src,
376                             struct jit_ctx *ctx)
377 {
378         if (ctx->target != NULL) {
379                 u32 *p = &ctx->target[ctx->idx];
380                 uasm_i_divu(&p, dst, src);
381                 p = &ctx->target[ctx->idx + 1];
382                 uasm_i_mflo(&p, dst);
383         }
384         ctx->idx += 2; /* 2 insts */
385 }
386
387 static inline void emit_mod(unsigned int dst, unsigned int src,
388                             struct jit_ctx *ctx)
389 {
390         if (ctx->target != NULL) {
391                 u32 *p = &ctx->target[ctx->idx];
392                 uasm_i_divu(&p, dst, src);
393                 p = &ctx->target[ctx->idx + 1];
394                 uasm_i_mfhi(&p, dst);
395         }
396         ctx->idx += 2; /* 2 insts */
397 }
398
399 static inline void emit_dsll(unsigned int dst, unsigned int src,
400                              unsigned int sa, struct jit_ctx *ctx)
401 {
402         emit_instr(ctx, dsll, dst, src, sa);
403 }
404
405 static inline void emit_dsrl32(unsigned int dst, unsigned int src,
406                                unsigned int sa, struct jit_ctx *ctx)
407 {
408         emit_instr(ctx, dsrl32, dst, src, sa);
409 }
410
411 static inline void emit_wsbh(unsigned int dst, unsigned int src,
412                              struct jit_ctx *ctx)
413 {
414         emit_instr(ctx, wsbh, dst, src);
415 }
416
417 /* load pointer to register */
418 static inline void emit_load_ptr(unsigned int dst, unsigned int src,
419                                      int imm, struct jit_ctx *ctx)
420 {
421         /* src contains the base addr of the 32/64-pointer */
422         emit_long_instr(ctx, LW, dst, imm, src);
423 }
424
425 /* load a function pointer to register */
426 static inline void emit_load_func(unsigned int reg, ptr imm,
427                                   struct jit_ctx *ctx)
428 {
429         if (config_enabled(CONFIG_64BIT)) {
430                 /* At this point imm is always 64-bit */
431                 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
432                 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
433                 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
434                 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
435                 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
436         } else {
437                 emit_load_imm(reg, imm, ctx);
438         }
439 }
440
441 /* Move to real MIPS register */
442 static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
443 {
444         emit_long_instr(ctx, ADDU, dst, src, r_zero);
445 }
446
447 /* Move to JIT (32-bit) register */
448 static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
449 {
450         emit_addu(dst, src, r_zero, ctx);
451 }
452
453 /* Compute the immediate value for PC-relative branches. */
454 static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
455 {
456         if (ctx->target == NULL)
457                 return 0;
458
459         /*
460          * We want a pc-relative branch. We only do forward branches
461          * so tgt is always after pc. tgt is the instruction offset
462          * we want to jump to.
463
464          * Branch on MIPS:
465          * I: target_offset <- sign_extend(offset)
466          * I+1: PC += target_offset (delay slot)
467          *
468          * ctx->idx currently points to the branch instruction
469          * but the offset is added to the delay slot so we need
470          * to subtract 4.
471          */
472         return ctx->offsets[tgt] -
473                 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
474 }
475
476 static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
477                              unsigned int imm, struct jit_ctx *ctx)
478 {
479         if (ctx->target != NULL) {
480                 u32 *p = &ctx->target[ctx->idx];
481
482                 switch (cond) {
483                 case MIPS_COND_EQ:
484                         uasm_i_beq(&p, reg1, reg2, imm);
485                         break;
486                 case MIPS_COND_NE:
487                         uasm_i_bne(&p, reg1, reg2, imm);
488                         break;
489                 case MIPS_COND_ALL:
490                         uasm_i_b(&p, imm);
491                         break;
492                 default:
493                         pr_warn("%s: Unhandled branch conditional: %d\n",
494                                 __func__, cond);
495                 }
496         }
497         ctx->idx++;
498 }
499
500 static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
501 {
502         emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
503 }
504
505 static inline void emit_jalr(unsigned int link, unsigned int reg,
506                              struct jit_ctx *ctx)
507 {
508         emit_instr(ctx, jalr, link, reg);
509 }
510
511 static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
512 {
513         emit_instr(ctx, jr, reg);
514 }
515
516 static inline u16 align_sp(unsigned int num)
517 {
518         /* Double word alignment for 32-bit, quadword for 64-bit */
519         unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
520         num = (num + (align - 1)) & -align;
521         return num;
522 }
523
524 static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
525 {
526         int i = 0, real_off = 0;
527         u32 sflags, tmp_flags;
528
529         /* Adjust the stack pointer */
530         if (offset)
531                 emit_stack_offset(-align_sp(offset), ctx);
532
533         tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
534         /* sflags is essentially a bitmap */
535         while (tmp_flags) {
536                 if ((sflags >> i) & 0x1) {
537                         emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
538                                              ctx);
539                         real_off += SZREG;
540                 }
541                 i++;
542                 tmp_flags >>= 1;
543         }
544
545         /* save return address */
546         if (ctx->flags & SEEN_CALL) {
547                 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
548                 real_off += SZREG;
549         }
550
551         /* Setup r_M leaving the alignment gap if necessary */
552         if (ctx->flags & SEEN_MEM) {
553                 if (real_off % (SZREG * 2))
554                         real_off += SZREG;
555                 emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
556         }
557 }
558
559 static void restore_bpf_jit_regs(struct jit_ctx *ctx,
560                                  unsigned int offset)
561 {
562         int i, real_off = 0;
563         u32 sflags, tmp_flags;
564
565         tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
566         /* sflags is a bitmap */
567         i = 0;
568         while (tmp_flags) {
569                 if ((sflags >> i) & 0x1) {
570                         emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
571                                             ctx);
572                         real_off += SZREG;
573                 }
574                 i++;
575                 tmp_flags >>= 1;
576         }
577
578         /* restore return address */
579         if (ctx->flags & SEEN_CALL)
580                 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
581
582         /* Restore the sp and discard the scrach memory */
583         if (offset)
584                 emit_stack_offset(align_sp(offset), ctx);
585 }
586
587 static unsigned int get_stack_depth(struct jit_ctx *ctx)
588 {
589         int sp_off = 0;
590
591
592         /* How may s* regs do we need to preserved? */
593         sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
594
595         if (ctx->flags & SEEN_MEM)
596                 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
597
598         if (ctx->flags & SEEN_CALL)
599                 sp_off += SZREG; /* Space for our ra register */
600
601         return sp_off;
602 }
603
604 static void build_prologue(struct jit_ctx *ctx)
605 {
606         int sp_off;
607
608         /* Calculate the total offset for the stack pointer */
609         sp_off = get_stack_depth(ctx);
610         save_bpf_jit_regs(ctx, sp_off);
611
612         if (ctx->flags & SEEN_SKB)
613                 emit_reg_move(r_skb, MIPS_R_A0, ctx);
614
615         if (ctx->flags & SEEN_SKB_DATA) {
616                 /* Load packet length */
617                 emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
618                           ctx);
619                 emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
620                           ctx);
621                 /* Load the data pointer */
622                 emit_load_ptr(r_skb_data, r_skb,
623                               offsetof(struct sk_buff, data), ctx);
624                 /* Load the header length */
625                 emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
626         }
627
628         if (ctx->flags & SEEN_X)
629                 emit_jit_reg_move(r_X, r_zero, ctx);
630
631         /*
632          * Do not leak kernel data to userspace, we only need to clear
633          * r_A if it is ever used.  In fact if it is never used, we
634          * will not save/restore it, so clearing it in this case would
635          * corrupt the state of the caller.
636          */
637         if (bpf_needs_clear_a(&ctx->skf->insns[0]) &&
638             (ctx->flags & SEEN_A))
639                 emit_jit_reg_move(r_A, r_zero, ctx);
640 }
641
642 static void build_epilogue(struct jit_ctx *ctx)
643 {
644         unsigned int sp_off;
645
646         /* Calculate the total offset for the stack pointer */
647
648         sp_off = get_stack_depth(ctx);
649         restore_bpf_jit_regs(ctx, sp_off);
650
651         /* Return */
652         emit_jr(r_ra, ctx);
653         emit_nop(ctx);
654 }
655
656 #define CHOOSE_LOAD_FUNC(K, func) \
657         ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
658          func##_positive)
659
660 static int build_body(struct jit_ctx *ctx)
661 {
662         const struct bpf_prog *prog = ctx->skf;
663         const struct sock_filter *inst;
664         unsigned int i, off, condt;
665         u32 k, b_off __maybe_unused;
666         u8 (*sk_load_func)(unsigned long *skb, int offset);
667
668         for (i = 0; i < prog->len; i++) {
669                 u16 code;
670
671                 inst = &(prog->insns[i]);
672                 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
673                          __func__, inst->code, inst->jt, inst->jf, inst->k);
674                 k = inst->k;
675                 code = bpf_anc_helper(inst);
676
677                 if (ctx->target == NULL)
678                         ctx->offsets[i] = ctx->idx * 4;
679
680                 switch (code) {
681                 case BPF_LD | BPF_IMM:
682                         /* A <- k ==> li r_A, k */
683                         ctx->flags |= SEEN_A;
684                         emit_load_imm(r_A, k, ctx);
685                         break;
686                 case BPF_LD | BPF_W | BPF_LEN:
687                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
688                         /* A <- len ==> lw r_A, offset(skb) */
689                         ctx->flags |= SEEN_SKB | SEEN_A;
690                         off = offsetof(struct sk_buff, len);
691                         emit_load(r_A, r_skb, off, ctx);
692                         break;
693                 case BPF_LD | BPF_MEM:
694                         /* A <- M[k] ==> lw r_A, offset(M) */
695                         ctx->flags |= SEEN_MEM | SEEN_A;
696                         emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
697                         break;
698                 case BPF_LD | BPF_W | BPF_ABS:
699                         /* A <- P[k:4] */
700                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
701                         goto load;
702                 case BPF_LD | BPF_H | BPF_ABS:
703                         /* A <- P[k:2] */
704                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
705                         goto load;
706                 case BPF_LD | BPF_B | BPF_ABS:
707                         /* A <- P[k:1] */
708                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
709 load:
710                         emit_load_imm(r_off, k, ctx);
711 load_common:
712                         ctx->flags |= SEEN_CALL | SEEN_OFF |
713                                 SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
714
715                         emit_load_func(r_s0, (ptr)sk_load_func, ctx);
716                         emit_reg_move(MIPS_R_A0, r_skb, ctx);
717                         emit_jalr(MIPS_R_RA, r_s0, ctx);
718                         /* Load second argument to delay slot */
719                         emit_reg_move(MIPS_R_A1, r_off, ctx);
720                         /* Check the error value */
721                         emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
722                                    ctx);
723                         /* Load return register on DS for failures */
724                         emit_reg_move(r_ret, r_zero, ctx);
725                         /* Return with error */
726                         emit_b(b_imm(prog->len, ctx), ctx);
727                         emit_nop(ctx);
728                         break;
729                 case BPF_LD | BPF_W | BPF_IND:
730                         /* A <- P[X + k:4] */
731                         sk_load_func = sk_load_word;
732                         goto load_ind;
733                 case BPF_LD | BPF_H | BPF_IND:
734                         /* A <- P[X + k:2] */
735                         sk_load_func = sk_load_half;
736                         goto load_ind;
737                 case BPF_LD | BPF_B | BPF_IND:
738                         /* A <- P[X + k:1] */
739                         sk_load_func = sk_load_byte;
740 load_ind:
741                         ctx->flags |= SEEN_OFF | SEEN_X;
742                         emit_addiu(r_off, r_X, k, ctx);
743                         goto load_common;
744                 case BPF_LDX | BPF_IMM:
745                         /* X <- k */
746                         ctx->flags |= SEEN_X;
747                         emit_load_imm(r_X, k, ctx);
748                         break;
749                 case BPF_LDX | BPF_MEM:
750                         /* X <- M[k] */
751                         ctx->flags |= SEEN_X | SEEN_MEM;
752                         emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
753                         break;
754                 case BPF_LDX | BPF_W | BPF_LEN:
755                         /* X <- len */
756                         ctx->flags |= SEEN_X | SEEN_SKB;
757                         off = offsetof(struct sk_buff, len);
758                         emit_load(r_X, r_skb, off, ctx);
759                         break;
760                 case BPF_LDX | BPF_B | BPF_MSH:
761                         /* X <- 4 * (P[k:1] & 0xf) */
762                         ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
763                         /* Load offset to a1 */
764                         emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
765                         /*
766                          * This may emit two instructions so it may not fit
767                          * in the delay slot. So use a0 in the delay slot.
768                          */
769                         emit_load_imm(MIPS_R_A1, k, ctx);
770                         emit_jalr(MIPS_R_RA, r_s0, ctx);
771                         emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
772                         /* Check the error value */
773                         emit_bcond(MIPS_COND_NE, r_ret, 0,
774                                    b_imm(prog->len, ctx), ctx);
775                         emit_reg_move(r_ret, r_zero, ctx);
776                         /* We are good */
777                         /* X <- P[1:K] & 0xf */
778                         emit_andi(r_X, r_A, 0xf, ctx);
779                         /* X << 2 */
780                         emit_b(b_imm(i + 1, ctx), ctx);
781                         emit_sll(r_X, r_X, 2, ctx); /* delay slot */
782                         break;
783                 case BPF_ST:
784                         /* M[k] <- A */
785                         ctx->flags |= SEEN_MEM | SEEN_A;
786                         emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
787                         break;
788                 case BPF_STX:
789                         /* M[k] <- X */
790                         ctx->flags |= SEEN_MEM | SEEN_X;
791                         emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
792                         break;
793                 case BPF_ALU | BPF_ADD | BPF_K:
794                         /* A += K */
795                         ctx->flags |= SEEN_A;
796                         emit_addiu(r_A, r_A, k, ctx);
797                         break;
798                 case BPF_ALU | BPF_ADD | BPF_X:
799                         /* A += X */
800                         ctx->flags |= SEEN_A | SEEN_X;
801                         emit_addu(r_A, r_A, r_X, ctx);
802                         break;
803                 case BPF_ALU | BPF_SUB | BPF_K:
804                         /* A -= K */
805                         ctx->flags |= SEEN_A;
806                         emit_addiu(r_A, r_A, -k, ctx);
807                         break;
808                 case BPF_ALU | BPF_SUB | BPF_X:
809                         /* A -= X */
810                         ctx->flags |= SEEN_A | SEEN_X;
811                         emit_subu(r_A, r_A, r_X, ctx);
812                         break;
813                 case BPF_ALU | BPF_MUL | BPF_K:
814                         /* A *= K */
815                         /* Load K to scratch register before MUL */
816                         ctx->flags |= SEEN_A;
817                         emit_load_imm(r_s0, k, ctx);
818                         emit_mul(r_A, r_A, r_s0, ctx);
819                         break;
820                 case BPF_ALU | BPF_MUL | BPF_X:
821                         /* A *= X */
822                         ctx->flags |= SEEN_A | SEEN_X;
823                         emit_mul(r_A, r_A, r_X, ctx);
824                         break;
825                 case BPF_ALU | BPF_DIV | BPF_K:
826                         /* A /= k */
827                         if (k == 1)
828                                 break;
829                         if (optimize_div(&k)) {
830                                 ctx->flags |= SEEN_A;
831                                 emit_srl(r_A, r_A, k, ctx);
832                                 break;
833                         }
834                         ctx->flags |= SEEN_A;
835                         emit_load_imm(r_s0, k, ctx);
836                         emit_div(r_A, r_s0, ctx);
837                         break;
838                 case BPF_ALU | BPF_MOD | BPF_K:
839                         /* A %= k */
840                         if (k == 1) {
841                                 ctx->flags |= SEEN_A;
842                                 emit_jit_reg_move(r_A, r_zero, ctx);
843                         } else {
844                                 ctx->flags |= SEEN_A;
845                                 emit_load_imm(r_s0, k, ctx);
846                                 emit_mod(r_A, r_s0, ctx);
847                         }
848                         break;
849                 case BPF_ALU | BPF_DIV | BPF_X:
850                         /* A /= X */
851                         ctx->flags |= SEEN_X | SEEN_A;
852                         /* Check if r_X is zero */
853                         emit_bcond(MIPS_COND_EQ, r_X, r_zero,
854                                    b_imm(prog->len, ctx), ctx);
855                         emit_load_imm(r_ret, 0, ctx); /* delay slot */
856                         emit_div(r_A, r_X, ctx);
857                         break;
858                 case BPF_ALU | BPF_MOD | BPF_X:
859                         /* A %= X */
860                         ctx->flags |= SEEN_X | SEEN_A;
861                         /* Check if r_X is zero */
862                         emit_bcond(MIPS_COND_EQ, r_X, r_zero,
863                                    b_imm(prog->len, ctx), ctx);
864                         emit_load_imm(r_ret, 0, ctx); /* delay slot */
865                         emit_mod(r_A, r_X, ctx);
866                         break;
867                 case BPF_ALU | BPF_OR | BPF_K:
868                         /* A |= K */
869                         ctx->flags |= SEEN_A;
870                         emit_ori(r_A, r_A, k, ctx);
871                         break;
872                 case BPF_ALU | BPF_OR | BPF_X:
873                         /* A |= X */
874                         ctx->flags |= SEEN_A;
875                         emit_ori(r_A, r_A, r_X, ctx);
876                         break;
877                 case BPF_ALU | BPF_XOR | BPF_K:
878                         /* A ^= k */
879                         ctx->flags |= SEEN_A;
880                         emit_xori(r_A, r_A, k, ctx);
881                         break;
882                 case BPF_ANC | SKF_AD_ALU_XOR_X:
883                 case BPF_ALU | BPF_XOR | BPF_X:
884                         /* A ^= X */
885                         ctx->flags |= SEEN_A;
886                         emit_xor(r_A, r_A, r_X, ctx);
887                         break;
888                 case BPF_ALU | BPF_AND | BPF_K:
889                         /* A &= K */
890                         ctx->flags |= SEEN_A;
891                         emit_andi(r_A, r_A, k, ctx);
892                         break;
893                 case BPF_ALU | BPF_AND | BPF_X:
894                         /* A &= X */
895                         ctx->flags |= SEEN_A | SEEN_X;
896                         emit_and(r_A, r_A, r_X, ctx);
897                         break;
898                 case BPF_ALU | BPF_LSH | BPF_K:
899                         /* A <<= K */
900                         ctx->flags |= SEEN_A;
901                         emit_sll(r_A, r_A, k, ctx);
902                         break;
903                 case BPF_ALU | BPF_LSH | BPF_X:
904                         /* A <<= X */
905                         ctx->flags |= SEEN_A | SEEN_X;
906                         emit_sllv(r_A, r_A, r_X, ctx);
907                         break;
908                 case BPF_ALU | BPF_RSH | BPF_K:
909                         /* A >>= K */
910                         ctx->flags |= SEEN_A;
911                         emit_srl(r_A, r_A, k, ctx);
912                         break;
913                 case BPF_ALU | BPF_RSH | BPF_X:
914                         ctx->flags |= SEEN_A | SEEN_X;
915                         emit_srlv(r_A, r_A, r_X, ctx);
916                         break;
917                 case BPF_ALU | BPF_NEG:
918                         /* A = -A */
919                         ctx->flags |= SEEN_A;
920                         emit_neg(r_A, ctx);
921                         break;
922                 case BPF_JMP | BPF_JA:
923                         /* pc += K */
924                         emit_b(b_imm(i + k + 1, ctx), ctx);
925                         emit_nop(ctx);
926                         break;
927                 case BPF_JMP | BPF_JEQ | BPF_K:
928                         /* pc += ( A == K ) ? pc->jt : pc->jf */
929                         condt = MIPS_COND_EQ | MIPS_COND_K;
930                         goto jmp_cmp;
931                 case BPF_JMP | BPF_JEQ | BPF_X:
932                         ctx->flags |= SEEN_X;
933                         /* pc += ( A == X ) ? pc->jt : pc->jf */
934                         condt = MIPS_COND_EQ | MIPS_COND_X;
935                         goto jmp_cmp;
936                 case BPF_JMP | BPF_JGE | BPF_K:
937                         /* pc += ( A >= K ) ? pc->jt : pc->jf */
938                         condt = MIPS_COND_GE | MIPS_COND_K;
939                         goto jmp_cmp;
940                 case BPF_JMP | BPF_JGE | BPF_X:
941                         ctx->flags |= SEEN_X;
942                         /* pc += ( A >= X ) ? pc->jt : pc->jf */
943                         condt = MIPS_COND_GE | MIPS_COND_X;
944                         goto jmp_cmp;
945                 case BPF_JMP | BPF_JGT | BPF_K:
946                         /* pc += ( A > K ) ? pc->jt : pc->jf */
947                         condt = MIPS_COND_GT | MIPS_COND_K;
948                         goto jmp_cmp;
949                 case BPF_JMP | BPF_JGT | BPF_X:
950                         ctx->flags |= SEEN_X;
951                         /* pc += ( A > X ) ? pc->jt : pc->jf */
952                         condt = MIPS_COND_GT | MIPS_COND_X;
953 jmp_cmp:
954                         /* Greater or Equal */
955                         if ((condt & MIPS_COND_GE) ||
956                             (condt & MIPS_COND_GT)) {
957                                 if (condt & MIPS_COND_K) { /* K */
958                                         ctx->flags |= SEEN_A;
959                                         emit_sltiu(r_s0, r_A, k, ctx);
960                                 } else { /* X */
961                                         ctx->flags |= SEEN_A |
962                                                 SEEN_X;
963                                         emit_sltu(r_s0, r_A, r_X, ctx);
964                                 }
965                                 /* A < (K|X) ? r_scrach = 1 */
966                                 b_off = b_imm(i + inst->jf + 1, ctx);
967                                 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
968                                            ctx);
969                                 emit_nop(ctx);
970                                 /* A > (K|X) ? scratch = 0 */
971                                 if (condt & MIPS_COND_GT) {
972                                         /* Checking for equality */
973                                         ctx->flags |= SEEN_A | SEEN_X;
974                                         if (condt & MIPS_COND_K)
975                                                 emit_load_imm(r_s0, k, ctx);
976                                         else
977                                                 emit_jit_reg_move(r_s0, r_X,
978                                                                   ctx);
979                                         b_off = b_imm(i + inst->jf + 1, ctx);
980                                         emit_bcond(MIPS_COND_EQ, r_A, r_s0,
981                                                    b_off, ctx);
982                                         emit_nop(ctx);
983                                         /* Finally, A > K|X */
984                                         b_off = b_imm(i + inst->jt + 1, ctx);
985                                         emit_b(b_off, ctx);
986                                         emit_nop(ctx);
987                                 } else {
988                                         /* A >= (K|X) so jump */
989                                         b_off = b_imm(i + inst->jt + 1, ctx);
990                                         emit_b(b_off, ctx);
991                                         emit_nop(ctx);
992                                 }
993                         } else {
994                                 /* A == K|X */
995                                 if (condt & MIPS_COND_K) { /* K */
996                                         ctx->flags |= SEEN_A;
997                                         emit_load_imm(r_s0, k, ctx);
998                                         /* jump true */
999                                         b_off = b_imm(i + inst->jt + 1, ctx);
1000                                         emit_bcond(MIPS_COND_EQ, r_A, r_s0,
1001                                                    b_off, ctx);
1002                                         emit_nop(ctx);
1003                                         /* jump false */
1004                                         b_off = b_imm(i + inst->jf + 1,
1005                                                       ctx);
1006                                         emit_bcond(MIPS_COND_NE, r_A, r_s0,
1007                                                    b_off, ctx);
1008                                         emit_nop(ctx);
1009                                 } else { /* X */
1010                                         /* jump true */
1011                                         ctx->flags |= SEEN_A | SEEN_X;
1012                                         b_off = b_imm(i + inst->jt + 1,
1013                                                       ctx);
1014                                         emit_bcond(MIPS_COND_EQ, r_A, r_X,
1015                                                    b_off, ctx);
1016                                         emit_nop(ctx);
1017                                         /* jump false */
1018                                         b_off = b_imm(i + inst->jf + 1, ctx);
1019                                         emit_bcond(MIPS_COND_NE, r_A, r_X,
1020                                                    b_off, ctx);
1021                                         emit_nop(ctx);
1022                                 }
1023                         }
1024                         break;
1025                 case BPF_JMP | BPF_JSET | BPF_K:
1026                         ctx->flags |= SEEN_A;
1027                         /* pc += (A & K) ? pc -> jt : pc -> jf */
1028                         emit_load_imm(r_s1, k, ctx);
1029                         emit_and(r_s0, r_A, r_s1, ctx);
1030                         /* jump true */
1031                         b_off = b_imm(i + inst->jt + 1, ctx);
1032                         emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1033                         emit_nop(ctx);
1034                         /* jump false */
1035                         b_off = b_imm(i + inst->jf + 1, ctx);
1036                         emit_b(b_off, ctx);
1037                         emit_nop(ctx);
1038                         break;
1039                 case BPF_JMP | BPF_JSET | BPF_X:
1040                         ctx->flags |= SEEN_X | SEEN_A;
1041                         /* pc += (A & X) ? pc -> jt : pc -> jf */
1042                         emit_and(r_s0, r_A, r_X, ctx);
1043                         /* jump true */
1044                         b_off = b_imm(i + inst->jt + 1, ctx);
1045                         emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1046                         emit_nop(ctx);
1047                         /* jump false */
1048                         b_off = b_imm(i + inst->jf + 1, ctx);
1049                         emit_b(b_off, ctx);
1050                         emit_nop(ctx);
1051                         break;
1052                 case BPF_RET | BPF_A:
1053                         ctx->flags |= SEEN_A;
1054                         if (i != prog->len - 1)
1055                                 /*
1056                                  * If this is not the last instruction
1057                                  * then jump to the epilogue
1058                                  */
1059                                 emit_b(b_imm(prog->len, ctx), ctx);
1060                         emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1061                         break;
1062                 case BPF_RET | BPF_K:
1063                         /*
1064                          * It can emit two instructions so it does not fit on
1065                          * the delay slot.
1066                          */
1067                         emit_load_imm(r_ret, k, ctx);
1068                         if (i != prog->len - 1) {
1069                                 /*
1070                                  * If this is not the last instruction
1071                                  * then jump to the epilogue
1072                                  */
1073                                 emit_b(b_imm(prog->len, ctx), ctx);
1074                                 emit_nop(ctx);
1075                         }
1076                         break;
1077                 case BPF_MISC | BPF_TAX:
1078                         /* X = A */
1079                         ctx->flags |= SEEN_X | SEEN_A;
1080                         emit_jit_reg_move(r_X, r_A, ctx);
1081                         break;
1082                 case BPF_MISC | BPF_TXA:
1083                         /* A = X */
1084                         ctx->flags |= SEEN_A | SEEN_X;
1085                         emit_jit_reg_move(r_A, r_X, ctx);
1086                         break;
1087                 /* AUX */
1088                 case BPF_ANC | SKF_AD_PROTOCOL:
1089                         /* A = ntohs(skb->protocol */
1090                         ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1091                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1092                                                   protocol) != 2);
1093                         off = offsetof(struct sk_buff, protocol);
1094                         emit_half_load(r_A, r_skb, off, ctx);
1095 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1096                         /* This needs little endian fixup */
1097                         if (cpu_has_wsbh) {
1098                                 /* R2 and later have the wsbh instruction */
1099                                 emit_wsbh(r_A, r_A, ctx);
1100                         } else {
1101                                 /* Get first byte */
1102                                 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1103                                 /* Shift it */
1104                                 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1105                                 /* Get second byte */
1106                                 emit_srl(r_tmp_imm, r_A, 8, ctx);
1107                                 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1108                                 /* Put everyting together in r_A */
1109                                 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
1110                         }
1111 #endif
1112                         break;
1113                 case BPF_ANC | SKF_AD_CPU:
1114                         ctx->flags |= SEEN_A | SEEN_OFF;
1115                         /* A = current_thread_info()->cpu */
1116                         BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1117                                                   cpu) != 4);
1118                         off = offsetof(struct thread_info, cpu);
1119                         /* $28/gp points to the thread_info struct */
1120                         emit_load(r_A, 28, off, ctx);
1121                         break;
1122                 case BPF_ANC | SKF_AD_IFINDEX:
1123                         /* A = skb->dev->ifindex */
1124                         ctx->flags |= SEEN_SKB | SEEN_A;
1125                         off = offsetof(struct sk_buff, dev);
1126                         /* Load *dev pointer */
1127                         emit_load_ptr(r_s0, r_skb, off, ctx);
1128                         /* error (0) in the delay slot */
1129                         emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
1130                                    b_imm(prog->len, ctx), ctx);
1131                         emit_reg_move(r_ret, r_zero, ctx);
1132                         BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
1133                                                   ifindex) != 4);
1134                         off = offsetof(struct net_device, ifindex);
1135                         emit_load(r_A, r_s0, off, ctx);
1136                         break;
1137                 case BPF_ANC | SKF_AD_MARK:
1138                         ctx->flags |= SEEN_SKB | SEEN_A;
1139                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1140                         off = offsetof(struct sk_buff, mark);
1141                         emit_load(r_A, r_skb, off, ctx);
1142                         break;
1143                 case BPF_ANC | SKF_AD_RXHASH:
1144                         ctx->flags |= SEEN_SKB | SEEN_A;
1145                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1146                         off = offsetof(struct sk_buff, hash);
1147                         emit_load(r_A, r_skb, off, ctx);
1148                         break;
1149                 case BPF_ANC | SKF_AD_VLAN_TAG:
1150                 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
1151                         ctx->flags |= SEEN_SKB | SEEN_A;
1152                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1153                                                   vlan_tci) != 2);
1154                         off = offsetof(struct sk_buff, vlan_tci);
1155                         emit_half_load(r_s0, r_skb, off, ctx);
1156                         if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
1157                                 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
1158                         } else {
1159                                 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
1160                                 /* return 1 if present */
1161                                 emit_sltu(r_A, r_zero, r_A, ctx);
1162                         }
1163                         break;
1164                 case BPF_ANC | SKF_AD_PKTTYPE:
1165                         ctx->flags |= SEEN_SKB;
1166
1167                         emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
1168                         /* Keep only the last 3 bits */
1169                         emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
1170 #ifdef __BIG_ENDIAN_BITFIELD
1171                         /* Get the actual packet type to the lower 3 bits */
1172                         emit_srl(r_A, r_A, 5, ctx);
1173 #endif
1174                         break;
1175                 case BPF_ANC | SKF_AD_QUEUE:
1176                         ctx->flags |= SEEN_SKB | SEEN_A;
1177                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1178                                                   queue_mapping) != 2);
1179                         BUILD_BUG_ON(offsetof(struct sk_buff,
1180                                               queue_mapping) > 0xff);
1181                         off = offsetof(struct sk_buff, queue_mapping);
1182                         emit_half_load(r_A, r_skb, off, ctx);
1183                         break;
1184                 default:
1185                         pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1186                                  inst->code);
1187                         return -1;
1188                 }
1189         }
1190
1191         /* compute offsets only during the first pass */
1192         if (ctx->target == NULL)
1193                 ctx->offsets[i] = ctx->idx * 4;
1194
1195         return 0;
1196 }
1197
1198 int bpf_jit_enable __read_mostly;
1199
1200 void bpf_jit_compile(struct bpf_prog *fp)
1201 {
1202         struct jit_ctx ctx;
1203         unsigned int alloc_size, tmp_idx;
1204
1205         if (!bpf_jit_enable)
1206                 return;
1207
1208         memset(&ctx, 0, sizeof(ctx));
1209
1210         ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
1211         if (ctx.offsets == NULL)
1212                 return;
1213
1214         ctx.skf = fp;
1215
1216         if (build_body(&ctx))
1217                 goto out;
1218
1219         tmp_idx = ctx.idx;
1220         build_prologue(&ctx);
1221         ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1222         /* just to complete the ctx.idx count */
1223         build_epilogue(&ctx);
1224
1225         alloc_size = 4 * ctx.idx;
1226         ctx.target = module_alloc(alloc_size);
1227         if (ctx.target == NULL)
1228                 goto out;
1229
1230         /* Clean it */
1231         memset(ctx.target, 0, alloc_size);
1232
1233         ctx.idx = 0;
1234
1235         /* Generate the actual JIT code */
1236         build_prologue(&ctx);
1237         build_body(&ctx);
1238         build_epilogue(&ctx);
1239
1240         /* Update the icache */
1241         flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1242
1243         if (bpf_jit_enable > 1)
1244                 /* Dump JIT code */
1245                 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1246
1247         fp->bpf_func = (void *)ctx.target;
1248         fp->jited = 1;
1249
1250 out:
1251         kfree(ctx.offsets);
1252 }
1253
1254 void bpf_jit_free(struct bpf_prog *fp)
1255 {
1256         if (fp->jited)
1257                 module_memfree(fp->bpf_func);
1258
1259         bpf_prog_unlock_free(fp);
1260 }