]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/mips/net/bpf_jit.c
MIPS: BPF: Use unsigned access for unsigned SKB fields.
[karo-tx-linux.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_half_load_unsigned(unsigned int reg, unsigned int base,
369                                            unsigned int offset, struct jit_ctx *ctx)
370 {
371         emit_instr(ctx, lhu, reg, offset, base);
372 }
373
374 static inline void emit_mul(unsigned int dst, unsigned int src1,
375                             unsigned int src2, struct jit_ctx *ctx)
376 {
377         emit_instr(ctx, mul, dst, src1, src2);
378 }
379
380 static inline void emit_div(unsigned int dst, unsigned int src,
381                             struct jit_ctx *ctx)
382 {
383         if (ctx->target != NULL) {
384                 u32 *p = &ctx->target[ctx->idx];
385                 uasm_i_divu(&p, dst, src);
386                 p = &ctx->target[ctx->idx + 1];
387                 uasm_i_mflo(&p, dst);
388         }
389         ctx->idx += 2; /* 2 insts */
390 }
391
392 static inline void emit_mod(unsigned int dst, unsigned int src,
393                             struct jit_ctx *ctx)
394 {
395         if (ctx->target != NULL) {
396                 u32 *p = &ctx->target[ctx->idx];
397                 uasm_i_divu(&p, dst, src);
398                 p = &ctx->target[ctx->idx + 1];
399                 uasm_i_mfhi(&p, dst);
400         }
401         ctx->idx += 2; /* 2 insts */
402 }
403
404 static inline void emit_dsll(unsigned int dst, unsigned int src,
405                              unsigned int sa, struct jit_ctx *ctx)
406 {
407         emit_instr(ctx, dsll, dst, src, sa);
408 }
409
410 static inline void emit_dsrl32(unsigned int dst, unsigned int src,
411                                unsigned int sa, struct jit_ctx *ctx)
412 {
413         emit_instr(ctx, dsrl32, dst, src, sa);
414 }
415
416 static inline void emit_wsbh(unsigned int dst, unsigned int src,
417                              struct jit_ctx *ctx)
418 {
419         emit_instr(ctx, wsbh, dst, src);
420 }
421
422 /* load pointer to register */
423 static inline void emit_load_ptr(unsigned int dst, unsigned int src,
424                                      int imm, struct jit_ctx *ctx)
425 {
426         /* src contains the base addr of the 32/64-pointer */
427         emit_long_instr(ctx, LW, dst, imm, src);
428 }
429
430 /* load a function pointer to register */
431 static inline void emit_load_func(unsigned int reg, ptr imm,
432                                   struct jit_ctx *ctx)
433 {
434         if (IS_ENABLED(CONFIG_64BIT)) {
435                 /* At this point imm is always 64-bit */
436                 emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
437                 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
438                 emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
439                 emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
440                 emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
441         } else {
442                 emit_load_imm(reg, imm, ctx);
443         }
444 }
445
446 /* Move to real MIPS register */
447 static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
448 {
449         emit_long_instr(ctx, ADDU, dst, src, r_zero);
450 }
451
452 /* Move to JIT (32-bit) register */
453 static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
454 {
455         emit_addu(dst, src, r_zero, ctx);
456 }
457
458 /* Compute the immediate value for PC-relative branches. */
459 static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
460 {
461         if (ctx->target == NULL)
462                 return 0;
463
464         /*
465          * We want a pc-relative branch. We only do forward branches
466          * so tgt is always after pc. tgt is the instruction offset
467          * we want to jump to.
468
469          * Branch on MIPS:
470          * I: target_offset <- sign_extend(offset)
471          * I+1: PC += target_offset (delay slot)
472          *
473          * ctx->idx currently points to the branch instruction
474          * but the offset is added to the delay slot so we need
475          * to subtract 4.
476          */
477         return ctx->offsets[tgt] -
478                 (ctx->idx * 4 - ctx->prologue_bytes) - 4;
479 }
480
481 static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
482                              unsigned int imm, struct jit_ctx *ctx)
483 {
484         if (ctx->target != NULL) {
485                 u32 *p = &ctx->target[ctx->idx];
486
487                 switch (cond) {
488                 case MIPS_COND_EQ:
489                         uasm_i_beq(&p, reg1, reg2, imm);
490                         break;
491                 case MIPS_COND_NE:
492                         uasm_i_bne(&p, reg1, reg2, imm);
493                         break;
494                 case MIPS_COND_ALL:
495                         uasm_i_b(&p, imm);
496                         break;
497                 default:
498                         pr_warn("%s: Unhandled branch conditional: %d\n",
499                                 __func__, cond);
500                 }
501         }
502         ctx->idx++;
503 }
504
505 static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
506 {
507         emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
508 }
509
510 static inline void emit_jalr(unsigned int link, unsigned int reg,
511                              struct jit_ctx *ctx)
512 {
513         emit_instr(ctx, jalr, link, reg);
514 }
515
516 static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
517 {
518         emit_instr(ctx, jr, reg);
519 }
520
521 static inline u16 align_sp(unsigned int num)
522 {
523         /* Double word alignment for 32-bit, quadword for 64-bit */
524         unsigned int align = IS_ENABLED(CONFIG_64BIT) ? 16 : 8;
525         num = (num + (align - 1)) & -align;
526         return num;
527 }
528
529 static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
530 {
531         int i = 0, real_off = 0;
532         u32 sflags, tmp_flags;
533
534         /* Adjust the stack pointer */
535         emit_stack_offset(-align_sp(offset), ctx);
536
537         tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
538         /* sflags is essentially a bitmap */
539         while (tmp_flags) {
540                 if ((sflags >> i) & 0x1) {
541                         emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
542                                              ctx);
543                         real_off += SZREG;
544                 }
545                 i++;
546                 tmp_flags >>= 1;
547         }
548
549         /* save return address */
550         if (ctx->flags & SEEN_CALL) {
551                 emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
552                 real_off += SZREG;
553         }
554
555         /* Setup r_M leaving the alignment gap if necessary */
556         if (ctx->flags & SEEN_MEM) {
557                 if (real_off % (SZREG * 2))
558                         real_off += SZREG;
559                 emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
560         }
561 }
562
563 static void restore_bpf_jit_regs(struct jit_ctx *ctx,
564                                  unsigned int offset)
565 {
566         int i, real_off = 0;
567         u32 sflags, tmp_flags;
568
569         tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
570         /* sflags is a bitmap */
571         i = 0;
572         while (tmp_flags) {
573                 if ((sflags >> i) & 0x1) {
574                         emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
575                                             ctx);
576                         real_off += SZREG;
577                 }
578                 i++;
579                 tmp_flags >>= 1;
580         }
581
582         /* restore return address */
583         if (ctx->flags & SEEN_CALL)
584                 emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
585
586         /* Restore the sp and discard the scrach memory */
587         emit_stack_offset(align_sp(offset), ctx);
588 }
589
590 static unsigned int get_stack_depth(struct jit_ctx *ctx)
591 {
592         int sp_off = 0;
593
594
595         /* How may s* regs do we need to preserved? */
596         sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * SZREG;
597
598         if (ctx->flags & SEEN_MEM)
599                 sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
600
601         if (ctx->flags & SEEN_CALL)
602                 sp_off += SZREG; /* Space for our ra register */
603
604         return sp_off;
605 }
606
607 static void build_prologue(struct jit_ctx *ctx)
608 {
609         int sp_off;
610
611         /* Calculate the total offset for the stack pointer */
612         sp_off = get_stack_depth(ctx);
613         save_bpf_jit_regs(ctx, sp_off);
614
615         if (ctx->flags & SEEN_SKB)
616                 emit_reg_move(r_skb, MIPS_R_A0, ctx);
617
618         if (ctx->flags & SEEN_SKB_DATA) {
619                 /* Load packet length */
620                 emit_load(r_skb_len, r_skb, offsetof(struct sk_buff, len),
621                           ctx);
622                 emit_load(r_tmp, r_skb, offsetof(struct sk_buff, data_len),
623                           ctx);
624                 /* Load the data pointer */
625                 emit_load_ptr(r_skb_data, r_skb,
626                               offsetof(struct sk_buff, data), ctx);
627                 /* Load the header length */
628                 emit_subu(r_skb_hl, r_skb_len, r_tmp, ctx);
629         }
630
631         if (ctx->flags & SEEN_X)
632                 emit_jit_reg_move(r_X, r_zero, ctx);
633
634         /* Do not leak kernel data to userspace */
635         if (bpf_needs_clear_a(&ctx->skf->insns[0]))
636                 emit_jit_reg_move(r_A, r_zero, ctx);
637 }
638
639 static void build_epilogue(struct jit_ctx *ctx)
640 {
641         unsigned int sp_off;
642
643         /* Calculate the total offset for the stack pointer */
644
645         sp_off = get_stack_depth(ctx);
646         restore_bpf_jit_regs(ctx, sp_off);
647
648         /* Return */
649         emit_jr(r_ra, ctx);
650         emit_nop(ctx);
651 }
652
653 #define CHOOSE_LOAD_FUNC(K, func) \
654         ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative : func) : \
655          func##_positive)
656
657 static int build_body(struct jit_ctx *ctx)
658 {
659         const struct bpf_prog *prog = ctx->skf;
660         const struct sock_filter *inst;
661         unsigned int i, off, condt;
662         u32 k, b_off __maybe_unused;
663         u8 (*sk_load_func)(unsigned long *skb, int offset);
664
665         for (i = 0; i < prog->len; i++) {
666                 u16 code;
667
668                 inst = &(prog->insns[i]);
669                 pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
670                          __func__, inst->code, inst->jt, inst->jf, inst->k);
671                 k = inst->k;
672                 code = bpf_anc_helper(inst);
673
674                 if (ctx->target == NULL)
675                         ctx->offsets[i] = ctx->idx * 4;
676
677                 switch (code) {
678                 case BPF_LD | BPF_IMM:
679                         /* A <- k ==> li r_A, k */
680                         ctx->flags |= SEEN_A;
681                         emit_load_imm(r_A, k, ctx);
682                         break;
683                 case BPF_LD | BPF_W | BPF_LEN:
684                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
685                         /* A <- len ==> lw r_A, offset(skb) */
686                         ctx->flags |= SEEN_SKB | SEEN_A;
687                         off = offsetof(struct sk_buff, len);
688                         emit_load(r_A, r_skb, off, ctx);
689                         break;
690                 case BPF_LD | BPF_MEM:
691                         /* A <- M[k] ==> lw r_A, offset(M) */
692                         ctx->flags |= SEEN_MEM | SEEN_A;
693                         emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
694                         break;
695                 case BPF_LD | BPF_W | BPF_ABS:
696                         /* A <- P[k:4] */
697                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_word);
698                         goto load;
699                 case BPF_LD | BPF_H | BPF_ABS:
700                         /* A <- P[k:2] */
701                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_half);
702                         goto load;
703                 case BPF_LD | BPF_B | BPF_ABS:
704                         /* A <- P[k:1] */
705                         sk_load_func = CHOOSE_LOAD_FUNC(k, sk_load_byte);
706 load:
707                         emit_load_imm(r_off, k, ctx);
708 load_common:
709                         ctx->flags |= SEEN_CALL | SEEN_OFF |
710                                 SEEN_SKB | SEEN_A | SEEN_SKB_DATA;
711
712                         emit_load_func(r_s0, (ptr)sk_load_func, ctx);
713                         emit_reg_move(MIPS_R_A0, r_skb, ctx);
714                         emit_jalr(MIPS_R_RA, r_s0, ctx);
715                         /* Load second argument to delay slot */
716                         emit_reg_move(MIPS_R_A1, r_off, ctx);
717                         /* Check the error value */
718                         emit_bcond(MIPS_COND_EQ, r_ret, 0, b_imm(i + 1, ctx),
719                                    ctx);
720                         /* Load return register on DS for failures */
721                         emit_reg_move(r_ret, r_zero, ctx);
722                         /* Return with error */
723                         emit_b(b_imm(prog->len, ctx), ctx);
724                         emit_nop(ctx);
725                         break;
726                 case BPF_LD | BPF_W | BPF_IND:
727                         /* A <- P[X + k:4] */
728                         sk_load_func = sk_load_word;
729                         goto load_ind;
730                 case BPF_LD | BPF_H | BPF_IND:
731                         /* A <- P[X + k:2] */
732                         sk_load_func = sk_load_half;
733                         goto load_ind;
734                 case BPF_LD | BPF_B | BPF_IND:
735                         /* A <- P[X + k:1] */
736                         sk_load_func = sk_load_byte;
737 load_ind:
738                         ctx->flags |= SEEN_OFF | SEEN_X;
739                         emit_addiu(r_off, r_X, k, ctx);
740                         goto load_common;
741                 case BPF_LDX | BPF_IMM:
742                         /* X <- k */
743                         ctx->flags |= SEEN_X;
744                         emit_load_imm(r_X, k, ctx);
745                         break;
746                 case BPF_LDX | BPF_MEM:
747                         /* X <- M[k] */
748                         ctx->flags |= SEEN_X | SEEN_MEM;
749                         emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
750                         break;
751                 case BPF_LDX | BPF_W | BPF_LEN:
752                         /* X <- len */
753                         ctx->flags |= SEEN_X | SEEN_SKB;
754                         off = offsetof(struct sk_buff, len);
755                         emit_load(r_X, r_skb, off, ctx);
756                         break;
757                 case BPF_LDX | BPF_B | BPF_MSH:
758                         /* X <- 4 * (P[k:1] & 0xf) */
759                         ctx->flags |= SEEN_X | SEEN_CALL | SEEN_SKB;
760                         /* Load offset to a1 */
761                         emit_load_func(r_s0, (ptr)sk_load_byte, ctx);
762                         /*
763                          * This may emit two instructions so it may not fit
764                          * in the delay slot. So use a0 in the delay slot.
765                          */
766                         emit_load_imm(MIPS_R_A1, k, ctx);
767                         emit_jalr(MIPS_R_RA, r_s0, ctx);
768                         emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
769                         /* Check the error value */
770                         emit_bcond(MIPS_COND_NE, r_ret, 0,
771                                    b_imm(prog->len, ctx), ctx);
772                         emit_reg_move(r_ret, r_zero, ctx);
773                         /* We are good */
774                         /* X <- P[1:K] & 0xf */
775                         emit_andi(r_X, r_A, 0xf, ctx);
776                         /* X << 2 */
777                         emit_b(b_imm(i + 1, ctx), ctx);
778                         emit_sll(r_X, r_X, 2, ctx); /* delay slot */
779                         break;
780                 case BPF_ST:
781                         /* M[k] <- A */
782                         ctx->flags |= SEEN_MEM | SEEN_A;
783                         emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
784                         break;
785                 case BPF_STX:
786                         /* M[k] <- X */
787                         ctx->flags |= SEEN_MEM | SEEN_X;
788                         emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
789                         break;
790                 case BPF_ALU | BPF_ADD | BPF_K:
791                         /* A += K */
792                         ctx->flags |= SEEN_A;
793                         emit_addiu(r_A, r_A, k, ctx);
794                         break;
795                 case BPF_ALU | BPF_ADD | BPF_X:
796                         /* A += X */
797                         ctx->flags |= SEEN_A | SEEN_X;
798                         emit_addu(r_A, r_A, r_X, ctx);
799                         break;
800                 case BPF_ALU | BPF_SUB | BPF_K:
801                         /* A -= K */
802                         ctx->flags |= SEEN_A;
803                         emit_addiu(r_A, r_A, -k, ctx);
804                         break;
805                 case BPF_ALU | BPF_SUB | BPF_X:
806                         /* A -= X */
807                         ctx->flags |= SEEN_A | SEEN_X;
808                         emit_subu(r_A, r_A, r_X, ctx);
809                         break;
810                 case BPF_ALU | BPF_MUL | BPF_K:
811                         /* A *= K */
812                         /* Load K to scratch register before MUL */
813                         ctx->flags |= SEEN_A;
814                         emit_load_imm(r_s0, k, ctx);
815                         emit_mul(r_A, r_A, r_s0, ctx);
816                         break;
817                 case BPF_ALU | BPF_MUL | BPF_X:
818                         /* A *= X */
819                         ctx->flags |= SEEN_A | SEEN_X;
820                         emit_mul(r_A, r_A, r_X, ctx);
821                         break;
822                 case BPF_ALU | BPF_DIV | BPF_K:
823                         /* A /= k */
824                         if (k == 1)
825                                 break;
826                         if (optimize_div(&k)) {
827                                 ctx->flags |= SEEN_A;
828                                 emit_srl(r_A, r_A, k, ctx);
829                                 break;
830                         }
831                         ctx->flags |= SEEN_A;
832                         emit_load_imm(r_s0, k, ctx);
833                         emit_div(r_A, r_s0, ctx);
834                         break;
835                 case BPF_ALU | BPF_MOD | BPF_K:
836                         /* A %= k */
837                         if (k == 1) {
838                                 ctx->flags |= SEEN_A;
839                                 emit_jit_reg_move(r_A, r_zero, ctx);
840                         } else {
841                                 ctx->flags |= SEEN_A;
842                                 emit_load_imm(r_s0, k, ctx);
843                                 emit_mod(r_A, r_s0, ctx);
844                         }
845                         break;
846                 case BPF_ALU | BPF_DIV | BPF_X:
847                         /* A /= X */
848                         ctx->flags |= SEEN_X | SEEN_A;
849                         /* Check if r_X is zero */
850                         emit_bcond(MIPS_COND_EQ, r_X, r_zero,
851                                    b_imm(prog->len, ctx), ctx);
852                         emit_load_imm(r_ret, 0, ctx); /* delay slot */
853                         emit_div(r_A, r_X, ctx);
854                         break;
855                 case BPF_ALU | BPF_MOD | BPF_X:
856                         /* A %= X */
857                         ctx->flags |= SEEN_X | SEEN_A;
858                         /* Check if r_X is zero */
859                         emit_bcond(MIPS_COND_EQ, r_X, r_zero,
860                                    b_imm(prog->len, ctx), ctx);
861                         emit_load_imm(r_ret, 0, ctx); /* delay slot */
862                         emit_mod(r_A, r_X, ctx);
863                         break;
864                 case BPF_ALU | BPF_OR | BPF_K:
865                         /* A |= K */
866                         ctx->flags |= SEEN_A;
867                         emit_ori(r_A, r_A, k, ctx);
868                         break;
869                 case BPF_ALU | BPF_OR | BPF_X:
870                         /* A |= X */
871                         ctx->flags |= SEEN_A;
872                         emit_ori(r_A, r_A, r_X, ctx);
873                         break;
874                 case BPF_ALU | BPF_XOR | BPF_K:
875                         /* A ^= k */
876                         ctx->flags |= SEEN_A;
877                         emit_xori(r_A, r_A, k, ctx);
878                         break;
879                 case BPF_ANC | SKF_AD_ALU_XOR_X:
880                 case BPF_ALU | BPF_XOR | BPF_X:
881                         /* A ^= X */
882                         ctx->flags |= SEEN_A;
883                         emit_xor(r_A, r_A, r_X, ctx);
884                         break;
885                 case BPF_ALU | BPF_AND | BPF_K:
886                         /* A &= K */
887                         ctx->flags |= SEEN_A;
888                         emit_andi(r_A, r_A, k, ctx);
889                         break;
890                 case BPF_ALU | BPF_AND | BPF_X:
891                         /* A &= X */
892                         ctx->flags |= SEEN_A | SEEN_X;
893                         emit_and(r_A, r_A, r_X, ctx);
894                         break;
895                 case BPF_ALU | BPF_LSH | BPF_K:
896                         /* A <<= K */
897                         ctx->flags |= SEEN_A;
898                         emit_sll(r_A, r_A, k, ctx);
899                         break;
900                 case BPF_ALU | BPF_LSH | BPF_X:
901                         /* A <<= X */
902                         ctx->flags |= SEEN_A | SEEN_X;
903                         emit_sllv(r_A, r_A, r_X, ctx);
904                         break;
905                 case BPF_ALU | BPF_RSH | BPF_K:
906                         /* A >>= K */
907                         ctx->flags |= SEEN_A;
908                         emit_srl(r_A, r_A, k, ctx);
909                         break;
910                 case BPF_ALU | BPF_RSH | BPF_X:
911                         ctx->flags |= SEEN_A | SEEN_X;
912                         emit_srlv(r_A, r_A, r_X, ctx);
913                         break;
914                 case BPF_ALU | BPF_NEG:
915                         /* A = -A */
916                         ctx->flags |= SEEN_A;
917                         emit_neg(r_A, ctx);
918                         break;
919                 case BPF_JMP | BPF_JA:
920                         /* pc += K */
921                         emit_b(b_imm(i + k + 1, ctx), ctx);
922                         emit_nop(ctx);
923                         break;
924                 case BPF_JMP | BPF_JEQ | BPF_K:
925                         /* pc += ( A == K ) ? pc->jt : pc->jf */
926                         condt = MIPS_COND_EQ | MIPS_COND_K;
927                         goto jmp_cmp;
928                 case BPF_JMP | BPF_JEQ | BPF_X:
929                         ctx->flags |= SEEN_X;
930                         /* pc += ( A == X ) ? pc->jt : pc->jf */
931                         condt = MIPS_COND_EQ | MIPS_COND_X;
932                         goto jmp_cmp;
933                 case BPF_JMP | BPF_JGE | BPF_K:
934                         /* pc += ( A >= K ) ? pc->jt : pc->jf */
935                         condt = MIPS_COND_GE | MIPS_COND_K;
936                         goto jmp_cmp;
937                 case BPF_JMP | BPF_JGE | BPF_X:
938                         ctx->flags |= SEEN_X;
939                         /* pc += ( A >= X ) ? pc->jt : pc->jf */
940                         condt = MIPS_COND_GE | MIPS_COND_X;
941                         goto jmp_cmp;
942                 case BPF_JMP | BPF_JGT | BPF_K:
943                         /* pc += ( A > K ) ? pc->jt : pc->jf */
944                         condt = MIPS_COND_GT | MIPS_COND_K;
945                         goto jmp_cmp;
946                 case BPF_JMP | BPF_JGT | BPF_X:
947                         ctx->flags |= SEEN_X;
948                         /* pc += ( A > X ) ? pc->jt : pc->jf */
949                         condt = MIPS_COND_GT | MIPS_COND_X;
950 jmp_cmp:
951                         /* Greater or Equal */
952                         if ((condt & MIPS_COND_GE) ||
953                             (condt & MIPS_COND_GT)) {
954                                 if (condt & MIPS_COND_K) { /* K */
955                                         ctx->flags |= SEEN_A;
956                                         emit_sltiu(r_s0, r_A, k, ctx);
957                                 } else { /* X */
958                                         ctx->flags |= SEEN_A |
959                                                 SEEN_X;
960                                         emit_sltu(r_s0, r_A, r_X, ctx);
961                                 }
962                                 /* A < (K|X) ? r_scrach = 1 */
963                                 b_off = b_imm(i + inst->jf + 1, ctx);
964                                 emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
965                                            ctx);
966                                 emit_nop(ctx);
967                                 /* A > (K|X) ? scratch = 0 */
968                                 if (condt & MIPS_COND_GT) {
969                                         /* Checking for equality */
970                                         ctx->flags |= SEEN_A | SEEN_X;
971                                         if (condt & MIPS_COND_K)
972                                                 emit_load_imm(r_s0, k, ctx);
973                                         else
974                                                 emit_jit_reg_move(r_s0, r_X,
975                                                                   ctx);
976                                         b_off = b_imm(i + inst->jf + 1, ctx);
977                                         emit_bcond(MIPS_COND_EQ, r_A, r_s0,
978                                                    b_off, ctx);
979                                         emit_nop(ctx);
980                                         /* Finally, A > K|X */
981                                         b_off = b_imm(i + inst->jt + 1, ctx);
982                                         emit_b(b_off, ctx);
983                                         emit_nop(ctx);
984                                 } else {
985                                         /* A >= (K|X) so jump */
986                                         b_off = b_imm(i + inst->jt + 1, ctx);
987                                         emit_b(b_off, ctx);
988                                         emit_nop(ctx);
989                                 }
990                         } else {
991                                 /* A == K|X */
992                                 if (condt & MIPS_COND_K) { /* K */
993                                         ctx->flags |= SEEN_A;
994                                         emit_load_imm(r_s0, k, ctx);
995                                         /* jump true */
996                                         b_off = b_imm(i + inst->jt + 1, ctx);
997                                         emit_bcond(MIPS_COND_EQ, r_A, r_s0,
998                                                    b_off, ctx);
999                                         emit_nop(ctx);
1000                                         /* jump false */
1001                                         b_off = b_imm(i + inst->jf + 1,
1002                                                       ctx);
1003                                         emit_bcond(MIPS_COND_NE, r_A, r_s0,
1004                                                    b_off, ctx);
1005                                         emit_nop(ctx);
1006                                 } else { /* X */
1007                                         /* jump true */
1008                                         ctx->flags |= SEEN_A | SEEN_X;
1009                                         b_off = b_imm(i + inst->jt + 1,
1010                                                       ctx);
1011                                         emit_bcond(MIPS_COND_EQ, r_A, r_X,
1012                                                    b_off, ctx);
1013                                         emit_nop(ctx);
1014                                         /* jump false */
1015                                         b_off = b_imm(i + inst->jf + 1, ctx);
1016                                         emit_bcond(MIPS_COND_NE, r_A, r_X,
1017                                                    b_off, ctx);
1018                                         emit_nop(ctx);
1019                                 }
1020                         }
1021                         break;
1022                 case BPF_JMP | BPF_JSET | BPF_K:
1023                         ctx->flags |= SEEN_A;
1024                         /* pc += (A & K) ? pc -> jt : pc -> jf */
1025                         emit_load_imm(r_s1, k, ctx);
1026                         emit_and(r_s0, r_A, r_s1, ctx);
1027                         /* jump true */
1028                         b_off = b_imm(i + inst->jt + 1, ctx);
1029                         emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1030                         emit_nop(ctx);
1031                         /* jump false */
1032                         b_off = b_imm(i + inst->jf + 1, ctx);
1033                         emit_b(b_off, ctx);
1034                         emit_nop(ctx);
1035                         break;
1036                 case BPF_JMP | BPF_JSET | BPF_X:
1037                         ctx->flags |= SEEN_X | SEEN_A;
1038                         /* pc += (A & X) ? pc -> jt : pc -> jf */
1039                         emit_and(r_s0, r_A, r_X, ctx);
1040                         /* jump true */
1041                         b_off = b_imm(i + inst->jt + 1, ctx);
1042                         emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
1043                         emit_nop(ctx);
1044                         /* jump false */
1045                         b_off = b_imm(i + inst->jf + 1, ctx);
1046                         emit_b(b_off, ctx);
1047                         emit_nop(ctx);
1048                         break;
1049                 case BPF_RET | BPF_A:
1050                         ctx->flags |= SEEN_A;
1051                         if (i != prog->len - 1)
1052                                 /*
1053                                  * If this is not the last instruction
1054                                  * then jump to the epilogue
1055                                  */
1056                                 emit_b(b_imm(prog->len, ctx), ctx);
1057                         emit_reg_move(r_ret, r_A, ctx); /* delay slot */
1058                         break;
1059                 case BPF_RET | BPF_K:
1060                         /*
1061                          * It can emit two instructions so it does not fit on
1062                          * the delay slot.
1063                          */
1064                         emit_load_imm(r_ret, k, ctx);
1065                         if (i != prog->len - 1) {
1066                                 /*
1067                                  * If this is not the last instruction
1068                                  * then jump to the epilogue
1069                                  */
1070                                 emit_b(b_imm(prog->len, ctx), ctx);
1071                                 emit_nop(ctx);
1072                         }
1073                         break;
1074                 case BPF_MISC | BPF_TAX:
1075                         /* X = A */
1076                         ctx->flags |= SEEN_X | SEEN_A;
1077                         emit_jit_reg_move(r_X, r_A, ctx);
1078                         break;
1079                 case BPF_MISC | BPF_TXA:
1080                         /* A = X */
1081                         ctx->flags |= SEEN_A | SEEN_X;
1082                         emit_jit_reg_move(r_A, r_X, ctx);
1083                         break;
1084                 /* AUX */
1085                 case BPF_ANC | SKF_AD_PROTOCOL:
1086                         /* A = ntohs(skb->protocol */
1087                         ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
1088                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1089                                                   protocol) != 2);
1090                         off = offsetof(struct sk_buff, protocol);
1091                         emit_half_load(r_A, r_skb, off, ctx);
1092 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1093                         /* This needs little endian fixup */
1094                         if (cpu_has_wsbh) {
1095                                 /* R2 and later have the wsbh instruction */
1096                                 emit_wsbh(r_A, r_A, ctx);
1097                         } else {
1098                                 /* Get first byte */
1099                                 emit_andi(r_tmp_imm, r_A, 0xff, ctx);
1100                                 /* Shift it */
1101                                 emit_sll(r_tmp, r_tmp_imm, 8, ctx);
1102                                 /* Get second byte */
1103                                 emit_srl(r_tmp_imm, r_A, 8, ctx);
1104                                 emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
1105                                 /* Put everyting together in r_A */
1106                                 emit_or(r_A, r_tmp, r_tmp_imm, ctx);
1107                         }
1108 #endif
1109                         break;
1110                 case BPF_ANC | SKF_AD_CPU:
1111                         ctx->flags |= SEEN_A | SEEN_OFF;
1112                         /* A = current_thread_info()->cpu */
1113                         BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
1114                                                   cpu) != 4);
1115                         off = offsetof(struct thread_info, cpu);
1116                         /* $28/gp points to the thread_info struct */
1117                         emit_load(r_A, 28, off, ctx);
1118                         break;
1119                 case BPF_ANC | SKF_AD_IFINDEX:
1120                         /* A = skb->dev->ifindex */
1121                 case BPF_ANC | SKF_AD_HATYPE:
1122                         /* A = skb->dev->type */
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                         if (code == (BPF_ANC | SKF_AD_IFINDEX)) {
1132                                 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1133                                 off = offsetof(struct net_device, ifindex);
1134                                 emit_load(r_A, r_s0, off, ctx);
1135                         } else { /* (code == (BPF_ANC | SKF_AD_HATYPE) */
1136                                 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
1137                                 off = offsetof(struct net_device, type);
1138                                 emit_half_load_unsigned(r_A, r_s0, off, ctx);
1139                         }
1140                         break;
1141                 case BPF_ANC | SKF_AD_MARK:
1142                         ctx->flags |= SEEN_SKB | SEEN_A;
1143                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1144                         off = offsetof(struct sk_buff, mark);
1145                         emit_load(r_A, r_skb, off, ctx);
1146                         break;
1147                 case BPF_ANC | SKF_AD_RXHASH:
1148                         ctx->flags |= SEEN_SKB | SEEN_A;
1149                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1150                         off = offsetof(struct sk_buff, hash);
1151                         emit_load(r_A, r_skb, off, ctx);
1152                         break;
1153                 case BPF_ANC | SKF_AD_VLAN_TAG:
1154                 case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
1155                         ctx->flags |= SEEN_SKB | SEEN_A;
1156                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1157                                                   vlan_tci) != 2);
1158                         off = offsetof(struct sk_buff, vlan_tci);
1159                         emit_half_load_unsigned(r_s0, r_skb, off, ctx);
1160                         if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
1161                                 emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
1162                         } else {
1163                                 emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
1164                                 /* return 1 if present */
1165                                 emit_sltu(r_A, r_zero, r_A, ctx);
1166                         }
1167                         break;
1168                 case BPF_ANC | SKF_AD_PKTTYPE:
1169                         ctx->flags |= SEEN_SKB;
1170
1171                         emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
1172                         /* Keep only the last 3 bits */
1173                         emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
1174 #ifdef __BIG_ENDIAN_BITFIELD
1175                         /* Get the actual packet type to the lower 3 bits */
1176                         emit_srl(r_A, r_A, 5, ctx);
1177 #endif
1178                         break;
1179                 case BPF_ANC | SKF_AD_QUEUE:
1180                         ctx->flags |= SEEN_SKB | SEEN_A;
1181                         BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
1182                                                   queue_mapping) != 2);
1183                         BUILD_BUG_ON(offsetof(struct sk_buff,
1184                                               queue_mapping) > 0xff);
1185                         off = offsetof(struct sk_buff, queue_mapping);
1186                         emit_half_load_unsigned(r_A, r_skb, off, ctx);
1187                         break;
1188                 default:
1189                         pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
1190                                  inst->code);
1191                         return -1;
1192                 }
1193         }
1194
1195         /* compute offsets only during the first pass */
1196         if (ctx->target == NULL)
1197                 ctx->offsets[i] = ctx->idx * 4;
1198
1199         return 0;
1200 }
1201
1202 int bpf_jit_enable __read_mostly;
1203
1204 void bpf_jit_compile(struct bpf_prog *fp)
1205 {
1206         struct jit_ctx ctx;
1207         unsigned int alloc_size, tmp_idx;
1208
1209         if (!bpf_jit_enable)
1210                 return;
1211
1212         memset(&ctx, 0, sizeof(ctx));
1213
1214         ctx.offsets = kcalloc(fp->len + 1, sizeof(*ctx.offsets), GFP_KERNEL);
1215         if (ctx.offsets == NULL)
1216                 return;
1217
1218         ctx.skf = fp;
1219
1220         if (build_body(&ctx))
1221                 goto out;
1222
1223         tmp_idx = ctx.idx;
1224         build_prologue(&ctx);
1225         ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1226         /* just to complete the ctx.idx count */
1227         build_epilogue(&ctx);
1228
1229         alloc_size = 4 * ctx.idx;
1230         ctx.target = module_alloc(alloc_size);
1231         if (ctx.target == NULL)
1232                 goto out;
1233
1234         /* Clean it */
1235         memset(ctx.target, 0, alloc_size);
1236
1237         ctx.idx = 0;
1238
1239         /* Generate the actual JIT code */
1240         build_prologue(&ctx);
1241         build_body(&ctx);
1242         build_epilogue(&ctx);
1243
1244         /* Update the icache */
1245         flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
1246
1247         if (bpf_jit_enable > 1)
1248                 /* Dump JIT code */
1249                 bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1250
1251         fp->bpf_func = (void *)ctx.target;
1252         fp->jited = 1;
1253
1254 out:
1255         kfree(ctx.offsets);
1256 }
1257
1258 void bpf_jit_free(struct bpf_prog *fp)
1259 {
1260         if (fp->jited)
1261                 module_memfree(fp->bpf_func);
1262
1263         bpf_prog_unlock_free(fp);
1264 }