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