GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm / net / bpf_jit_32.c
1 /*
2  * Just-In-Time compiler for eBPF filters on 32bit ARM
3  *
4  * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
5  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.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/bpf.h>
13 #include <linux/bitops.h>
14 #include <linux/compiler.h>
15 #include <linux/errno.h>
16 #include <linux/filter.h>
17 #include <linux/netdevice.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/if_vlan.h>
21
22 #include <asm/cacheflush.h>
23 #include <asm/hwcap.h>
24 #include <asm/opcodes.h>
25 #include <asm/system_info.h>
26
27 #include "bpf_jit_32.h"
28
29 /*
30  * eBPF prog stack layout:
31  *
32  *                         high
33  * original ARM_SP =>     +-----+
34  *                        |     | callee saved registers
35  *                        +-----+ <= (BPF_FP + SCRATCH_SIZE)
36  *                        | ... | eBPF JIT scratch space
37  * eBPF fp register =>    +-----+
38  *   (BPF_FP)             | ... | eBPF prog stack
39  *                        +-----+
40  *                        |RSVD | JIT scratchpad
41  * current ARM_SP =>      +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
42  *                        | ... | caller-saved registers
43  *                        +-----+
44  *                        | ... | arguments passed on stack
45  * ARM_SP during call =>  +-----|
46  *                        |     |
47  *                        | ... | Function call stack
48  *                        |     |
49  *                        +-----+
50  *                          low
51  *
52  * The callee saved registers depends on whether frame pointers are enabled.
53  * With frame pointers (to be compliant with the ABI):
54  *
55  *                              high
56  * original ARM_SP =>     +--------------+ \
57  *                        |      pc      | |
58  * current ARM_FP =>      +--------------+ } callee saved registers
59  *                        |r4-r9,fp,ip,lr| |
60  *                        +--------------+ /
61  *                              low
62  *
63  * Without frame pointers:
64  *
65  *                              high
66  * original ARM_SP =>     +--------------+
67  *                        |  r4-r9,fp,lr | callee saved registers
68  * current ARM_FP =>      +--------------+
69  *                              low
70  *
71  * When popping registers off the stack at the end of a BPF function, we
72  * reference them via the current ARM_FP register.
73  *
74  * Some eBPF operations are implemented via a call to a helper function.
75  * Such calls are "invisible" in the eBPF code, so it is up to the calling
76  * program to preserve any caller-saved ARM registers during the call. The
77  * JIT emits code to push and pop those registers onto the stack, immediately
78  * above the callee stack frame.
79  */
80 #define CALLEE_MASK     (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
81                          1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \
82                          1 << ARM_FP)
83 #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
84 #define CALLEE_POP_MASK  (CALLEE_MASK | 1 << ARM_PC)
85
86 #define CALLER_MASK     (1 << ARM_R0 | 1 << ARM_R1 | 1 << ARM_R2 | 1 << ARM_R3)
87
88 enum {
89         /* Stack layout - these are offsets from (top of stack - 4) */
90         BPF_R2_HI,
91         BPF_R2_LO,
92         BPF_R3_HI,
93         BPF_R3_LO,
94         BPF_R4_HI,
95         BPF_R4_LO,
96         BPF_R5_HI,
97         BPF_R5_LO,
98         BPF_R7_HI,
99         BPF_R7_LO,
100         BPF_R8_HI,
101         BPF_R8_LO,
102         BPF_R9_HI,
103         BPF_R9_LO,
104         BPF_FP_HI,
105         BPF_FP_LO,
106         BPF_TC_HI,
107         BPF_TC_LO,
108         BPF_AX_HI,
109         BPF_AX_LO,
110         /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
111          * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
112          * BPF_REG_FP and Tail call counts.
113          */
114         BPF_JIT_SCRATCH_REGS,
115 };
116
117 /*
118  * Negative "register" values indicate the register is stored on the stack
119  * and are the offset from the top of the eBPF JIT scratch space.
120  */
121 #define STACK_OFFSET(k) (-4 - (k) * 4)
122 #define SCRATCH_SIZE    (BPF_JIT_SCRATCH_REGS * 4)
123
124 #ifdef CONFIG_FRAME_POINTER
125 #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
126 #else
127 #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
128 #endif
129
130 #define TMP_REG_1       (MAX_BPF_JIT_REG + 0)   /* TEMP Register 1 */
131 #define TMP_REG_2       (MAX_BPF_JIT_REG + 1)   /* TEMP Register 2 */
132 #define TCALL_CNT       (MAX_BPF_JIT_REG + 2)   /* Tail Call Count */
133
134 #define FLAG_IMM_OVERFLOW       (1 << 0)
135
136 /*
137  * Map eBPF registers to ARM 32bit registers or stack scratch space.
138  *
139  * 1. First argument is passed using the arm 32bit registers and rest of the
140  * arguments are passed on stack scratch space.
141  * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
142  * arguments are mapped to scratch space on stack.
143  * 3. We need two 64 bit temp registers to do complex operations on eBPF
144  * registers.
145  *
146  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
147  * registers, we have to map each eBPF registers with two arm 32 bit regs or
148  * scratch memory space and we have to build eBPF 64 bit register from those.
149  *
150  */
151 static const s8 bpf2a32[][2] = {
152         /* return value from in-kernel function, and exit value from eBPF */
153         [BPF_REG_0] = {ARM_R1, ARM_R0},
154         /* arguments from eBPF program to in-kernel function */
155         [BPF_REG_1] = {ARM_R3, ARM_R2},
156         /* Stored on stack scratch space */
157         [BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
158         [BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
159         [BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
160         [BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
161         /* callee saved registers that in-kernel function will preserve */
162         [BPF_REG_6] = {ARM_R5, ARM_R4},
163         /* Stored on stack scratch space */
164         [BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
165         [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
166         [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
167         /* Read only Frame Pointer to access Stack */
168         [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
169         /* Temporary Register for internal BPF JIT, can be used
170          * for constant blindings and others.
171          */
172         [TMP_REG_1] = {ARM_R7, ARM_R6},
173         [TMP_REG_2] = {ARM_R9, ARM_R8},
174         /* Tail call count. Stored on stack scratch space. */
175         [TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
176         /* temporary register for blinding constants.
177          * Stored on stack scratch space.
178          */
179         [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
180 };
181
182 #define dst_lo  dst[1]
183 #define dst_hi  dst[0]
184 #define src_lo  src[1]
185 #define src_hi  src[0]
186
187 /*
188  * JIT Context:
189  *
190  * prog                 :       bpf_prog
191  * idx                  :       index of current last JITed instruction.
192  * prologue_bytes       :       bytes used in prologue.
193  * epilogue_offset      :       offset of epilogue starting.
194  * offsets              :       array of eBPF instruction offsets in
195  *                              JITed code.
196  * target               :       final JITed code.
197  * epilogue_bytes       :       no of bytes used in epilogue.
198  * imm_count            :       no of immediate counts used for global
199  *                              variables.
200  * imms                 :       array of global variable addresses.
201  */
202
203 struct jit_ctx {
204         const struct bpf_prog *prog;
205         unsigned int idx;
206         unsigned int prologue_bytes;
207         unsigned int epilogue_offset;
208         unsigned int cpu_architecture;
209         u32 flags;
210         u32 *offsets;
211         u32 *target;
212         u32 stack_size;
213 #if __LINUX_ARM_ARCH__ < 7
214         u16 epilogue_bytes;
215         u16 imm_count;
216         u32 *imms;
217 #endif
218 };
219
220 /*
221  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
222  * (where the assembly routines like __aeabi_uidiv could cause problems).
223  */
224 static u32 jit_udiv32(u32 dividend, u32 divisor)
225 {
226         return dividend / divisor;
227 }
228
229 static u32 jit_mod32(u32 dividend, u32 divisor)
230 {
231         return dividend % divisor;
232 }
233
234 static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
235 {
236         inst |= (cond << 28);
237         inst = __opcode_to_mem_arm(inst);
238
239         if (ctx->target != NULL)
240                 ctx->target[ctx->idx] = inst;
241
242         ctx->idx++;
243 }
244
245 /*
246  * Emit an instruction that will be executed unconditionally.
247  */
248 static inline void emit(u32 inst, struct jit_ctx *ctx)
249 {
250         _emit(ARM_COND_AL, inst, ctx);
251 }
252
253 /*
254  * This is rather horrid, but necessary to convert an integer constant
255  * to an immediate operand for the opcodes, and be able to detect at
256  * build time whether the constant can't be converted (iow, usable in
257  * BUILD_BUG_ON()).
258  */
259 #define imm12val(v, s) (rol32(v, (s)) | (s) << 7)
260 #define const_imm8m(x)                                  \
261         ({ int r;                                       \
262            u32 v = (x);                                 \
263            if (!(v & ~0x000000ff))                      \
264                 r = imm12val(v, 0);                     \
265            else if (!(v & ~0xc000003f))                 \
266                 r = imm12val(v, 2);                     \
267            else if (!(v & ~0xf000000f))                 \
268                 r = imm12val(v, 4);                     \
269            else if (!(v & ~0xfc000003))                 \
270                 r = imm12val(v, 6);                     \
271            else if (!(v & ~0xff000000))                 \
272                 r = imm12val(v, 8);                     \
273            else if (!(v & ~0x3fc00000))                 \
274                 r = imm12val(v, 10);                    \
275            else if (!(v & ~0x0ff00000))                 \
276                 r = imm12val(v, 12);                    \
277            else if (!(v & ~0x03fc0000))                 \
278                 r = imm12val(v, 14);                    \
279            else if (!(v & ~0x00ff0000))                 \
280                 r = imm12val(v, 16);                    \
281            else if (!(v & ~0x003fc000))                 \
282                 r = imm12val(v, 18);                    \
283            else if (!(v & ~0x000ff000))                 \
284                 r = imm12val(v, 20);                    \
285            else if (!(v & ~0x0003fc00))                 \
286                 r = imm12val(v, 22);                    \
287            else if (!(v & ~0x0000ff00))                 \
288                 r = imm12val(v, 24);                    \
289            else if (!(v & ~0x00003fc0))                 \
290                 r = imm12val(v, 26);                    \
291            else if (!(v & ~0x00000ff0))                 \
292                 r = imm12val(v, 28);                    \
293            else if (!(v & ~0x000003fc))                 \
294                 r = imm12val(v, 30);                    \
295            else                                         \
296                 r = -1;                                 \
297            r; })
298
299 /*
300  * Checks if immediate value can be converted to imm12(12 bits) value.
301  */
302 static int imm8m(u32 x)
303 {
304         u32 rot;
305
306         for (rot = 0; rot < 16; rot++)
307                 if ((x & ~ror32(0xff, 2 * rot)) == 0)
308                         return rol32(x, 2 * rot) | (rot << 8);
309         return -1;
310 }
311
312 #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x))
313
314 static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
315 {
316         op |= rt << 12 | rn << 16;
317         if (imm12 >= 0)
318                 op |= ARM_INST_LDST__U;
319         else
320                 imm12 = -imm12;
321         return op | (imm12 & ARM_INST_LDST__IMM12);
322 }
323
324 static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
325 {
326         op |= rt << 12 | rn << 16;
327         if (imm8 >= 0)
328                 op |= ARM_INST_LDST__U;
329         else
330                 imm8 = -imm8;
331         return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
332 }
333
334 #define ARM_LDR_I(rt, rn, off)  arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
335 #define ARM_LDRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
336 #define ARM_LDRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off)
337 #define ARM_LDRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
338
339 #define ARM_STR_I(rt, rn, off)  arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
340 #define ARM_STRB_I(rt, rn, off) arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
341 #define ARM_STRD_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off)
342 #define ARM_STRH_I(rt, rn, off) arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
343
344 /*
345  * Initializes the JIT space with undefined instructions.
346  */
347 static void jit_fill_hole(void *area, unsigned int size)
348 {
349         u32 *ptr;
350         /* We are guaranteed to have aligned memory. */
351         for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
352                 *ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
353 }
354
355 #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
356 /* EABI requires the stack to be aligned to 64-bit boundaries */
357 #define STACK_ALIGNMENT 8
358 #else
359 /* Stack must be aligned to 32-bit boundaries */
360 #define STACK_ALIGNMENT 4
361 #endif
362
363 /* total stack size used in JITed code */
364 #define _STACK_SIZE     (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
365 #define STACK_SIZE      ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
366
367 #if __LINUX_ARM_ARCH__ < 7
368
369 static u16 imm_offset(u32 k, struct jit_ctx *ctx)
370 {
371         unsigned int i = 0, offset;
372         u16 imm;
373
374         /* on the "fake" run we just count them (duplicates included) */
375         if (ctx->target == NULL) {
376                 ctx->imm_count++;
377                 return 0;
378         }
379
380         while ((i < ctx->imm_count) && ctx->imms[i]) {
381                 if (ctx->imms[i] == k)
382                         break;
383                 i++;
384         }
385
386         if (ctx->imms[i] == 0)
387                 ctx->imms[i] = k;
388
389         /* constants go just after the epilogue */
390         offset =  ctx->offsets[ctx->prog->len - 1] * 4;
391         offset += ctx->prologue_bytes;
392         offset += ctx->epilogue_bytes;
393         offset += i * 4;
394
395         ctx->target[offset / 4] = k;
396
397         /* PC in ARM mode == address of the instruction + 8 */
398         imm = offset - (8 + ctx->idx * 4);
399
400         if (imm & ~0xfff) {
401                 /*
402                  * literal pool is too far, signal it into flags. we
403                  * can only detect it on the second pass unfortunately.
404                  */
405                 ctx->flags |= FLAG_IMM_OVERFLOW;
406                 return 0;
407         }
408
409         return imm;
410 }
411
412 #endif /* __LINUX_ARM_ARCH__ */
413
414 static inline int bpf2a32_offset(int bpf_to, int bpf_from,
415                                  const struct jit_ctx *ctx) {
416         int to, from;
417
418         if (ctx->target == NULL)
419                 return 0;
420         to = ctx->offsets[bpf_to];
421         from = ctx->offsets[bpf_from];
422
423         return to - from - 1;
424 }
425
426 /*
427  * Move an immediate that's not an imm8m to a core register.
428  */
429 static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
430 {
431 #if __LINUX_ARM_ARCH__ < 7
432         emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
433 #else
434         emit(ARM_MOVW(rd, val & 0xffff), ctx);
435         if (val > 0xffff)
436                 emit(ARM_MOVT(rd, val >> 16), ctx);
437 #endif
438 }
439
440 static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
441 {
442         int imm12 = imm8m(val);
443
444         if (imm12 >= 0)
445                 emit(ARM_MOV_I(rd, imm12), ctx);
446         else
447                 emit_mov_i_no8m(rd, val, ctx);
448 }
449
450 static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
451 {
452         if (elf_hwcap & HWCAP_THUMB)
453                 emit(ARM_BX(tgt_reg), ctx);
454         else
455                 emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
456 }
457
458 static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
459 {
460 #if __LINUX_ARM_ARCH__ < 5
461         emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
462         emit_bx_r(tgt_reg, ctx);
463 #else
464         emit(ARM_BLX_R(tgt_reg), ctx);
465 #endif
466 }
467
468 static inline int epilogue_offset(const struct jit_ctx *ctx)
469 {
470         int to, from;
471         /* No need for 1st dummy run */
472         if (ctx->target == NULL)
473                 return 0;
474         to = ctx->epilogue_offset;
475         from = ctx->idx;
476
477         return to - from - 2;
478 }
479
480 static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
481 {
482         const int exclude_mask = BIT(ARM_R0) | BIT(ARM_R1);
483         const s8 *tmp = bpf2a32[TMP_REG_1];
484
485 #if __LINUX_ARM_ARCH__ == 7
486         if (elf_hwcap & HWCAP_IDIVA) {
487                 if (op == BPF_DIV)
488                         emit(ARM_UDIV(rd, rm, rn), ctx);
489                 else {
490                         emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
491                         emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
492                 }
493                 return;
494         }
495 #endif
496
497         /*
498          * For BPF_ALU | BPF_DIV | BPF_K instructions
499          * As ARM_R1 and ARM_R0 contains 1st argument of bpf
500          * function, we need to save it on caller side to save
501          * it from getting destroyed within callee.
502          * After the return from the callee, we restore ARM_R0
503          * ARM_R1.
504          */
505         if (rn != ARM_R1) {
506                 emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
507                 emit(ARM_MOV_R(ARM_R1, rn), ctx);
508         }
509         if (rm != ARM_R0) {
510                 emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
511                 emit(ARM_MOV_R(ARM_R0, rm), ctx);
512         }
513
514         /* Push caller-saved registers on stack */
515         emit(ARM_PUSH(CALLER_MASK & ~exclude_mask), ctx);
516
517         /* Call appropriate function */
518         emit_mov_i(ARM_IP, op == BPF_DIV ?
519                    (u32)jit_udiv32 : (u32)jit_mod32, ctx);
520         emit_blx_r(ARM_IP, ctx);
521
522         /* Restore caller-saved registers from stack */
523         emit(ARM_POP(CALLER_MASK & ~exclude_mask), ctx);
524
525         /* Save return value */
526         if (rd != ARM_R0)
527                 emit(ARM_MOV_R(rd, ARM_R0), ctx);
528
529         /* Restore ARM_R0 and ARM_R1 */
530         if (rn != ARM_R1)
531                 emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
532         if (rm != ARM_R0)
533                 emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
534 }
535
536 /* Is the translated BPF register on stack? */
537 static bool is_stacked(s8 reg)
538 {
539         return reg < 0;
540 }
541
542 /* If a BPF register is on the stack (stk is true), load it to the
543  * supplied temporary register and return the temporary register
544  * for subsequent operations, otherwise just use the CPU register.
545  */
546 static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
547 {
548         if (is_stacked(reg)) {
549                 emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
550                 reg = tmp;
551         }
552         return reg;
553 }
554
555 static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
556                                    struct jit_ctx *ctx)
557 {
558         if (is_stacked(reg[1])) {
559                 if (__LINUX_ARM_ARCH__ >= 6 ||
560                     ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
561                         emit(ARM_LDRD_I(tmp[1], ARM_FP,
562                                         EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
563                 } else {
564                         emit(ARM_LDR_I(tmp[1], ARM_FP,
565                                        EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
566                         emit(ARM_LDR_I(tmp[0], ARM_FP,
567                                        EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
568                 }
569                 reg = tmp;
570         }
571         return reg;
572 }
573
574 /* If a BPF register is on the stack (stk is true), save the register
575  * back to the stack.  If the source register is not the same, then
576  * move it into the correct register.
577  */
578 static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
579 {
580         if (is_stacked(reg))
581                 emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
582         else if (reg != src)
583                 emit(ARM_MOV_R(reg, src), ctx);
584 }
585
586 static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
587                               struct jit_ctx *ctx)
588 {
589         if (is_stacked(reg[1])) {
590                 if (__LINUX_ARM_ARCH__ >= 6 ||
591                     ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
592                         emit(ARM_STRD_I(src[1], ARM_FP,
593                                        EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
594                 } else {
595                         emit(ARM_STR_I(src[1], ARM_FP,
596                                        EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
597                         emit(ARM_STR_I(src[0], ARM_FP,
598                                        EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
599                 }
600         } else {
601                 if (reg[1] != src[1])
602                         emit(ARM_MOV_R(reg[1], src[1]), ctx);
603                 if (reg[0] != src[0])
604                         emit(ARM_MOV_R(reg[0], src[0]), ctx);
605         }
606 }
607
608 static inline void emit_a32_mov_i(const s8 dst, const u32 val,
609                                   struct jit_ctx *ctx)
610 {
611         const s8 *tmp = bpf2a32[TMP_REG_1];
612
613         if (is_stacked(dst)) {
614                 emit_mov_i(tmp[1], val, ctx);
615                 arm_bpf_put_reg32(dst, tmp[1], ctx);
616         } else {
617                 emit_mov_i(dst, val, ctx);
618         }
619 }
620
621 static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx)
622 {
623         const s8 *tmp = bpf2a32[TMP_REG_1];
624         const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
625
626         emit_mov_i(rd[1], (u32)val, ctx);
627         emit_mov_i(rd[0], val >> 32, ctx);
628
629         arm_bpf_put_reg64(dst, rd, ctx);
630 }
631
632 /* Sign extended move */
633 static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[],
634                                        const u32 val, struct jit_ctx *ctx) {
635         u64 val64 = val;
636
637         if (is64 && (val & (1<<31)))
638                 val64 |= 0xffffffff00000000ULL;
639         emit_a32_mov_i64(dst, val64, ctx);
640 }
641
642 static inline void emit_a32_add_r(const u8 dst, const u8 src,
643                               const bool is64, const bool hi,
644                               struct jit_ctx *ctx) {
645         /* 64 bit :
646          *      adds dst_lo, dst_lo, src_lo
647          *      adc dst_hi, dst_hi, src_hi
648          * 32 bit :
649          *      add dst_lo, dst_lo, src_lo
650          */
651         if (!hi && is64)
652                 emit(ARM_ADDS_R(dst, dst, src), ctx);
653         else if (hi && is64)
654                 emit(ARM_ADC_R(dst, dst, src), ctx);
655         else
656                 emit(ARM_ADD_R(dst, dst, src), ctx);
657 }
658
659 static inline void emit_a32_sub_r(const u8 dst, const u8 src,
660                                   const bool is64, const bool hi,
661                                   struct jit_ctx *ctx) {
662         /* 64 bit :
663          *      subs dst_lo, dst_lo, src_lo
664          *      sbc dst_hi, dst_hi, src_hi
665          * 32 bit :
666          *      sub dst_lo, dst_lo, src_lo
667          */
668         if (!hi && is64)
669                 emit(ARM_SUBS_R(dst, dst, src), ctx);
670         else if (hi && is64)
671                 emit(ARM_SBC_R(dst, dst, src), ctx);
672         else
673                 emit(ARM_SUB_R(dst, dst, src), ctx);
674 }
675
676 static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
677                               const bool hi, const u8 op, struct jit_ctx *ctx){
678         switch (BPF_OP(op)) {
679         /* dst = dst + src */
680         case BPF_ADD:
681                 emit_a32_add_r(dst, src, is64, hi, ctx);
682                 break;
683         /* dst = dst - src */
684         case BPF_SUB:
685                 emit_a32_sub_r(dst, src, is64, hi, ctx);
686                 break;
687         /* dst = dst | src */
688         case BPF_OR:
689                 emit(ARM_ORR_R(dst, dst, src), ctx);
690                 break;
691         /* dst = dst & src */
692         case BPF_AND:
693                 emit(ARM_AND_R(dst, dst, src), ctx);
694                 break;
695         /* dst = dst ^ src */
696         case BPF_XOR:
697                 emit(ARM_EOR_R(dst, dst, src), ctx);
698                 break;
699         /* dst = dst * src */
700         case BPF_MUL:
701                 emit(ARM_MUL(dst, dst, src), ctx);
702                 break;
703         /* dst = dst << src */
704         case BPF_LSH:
705                 emit(ARM_LSL_R(dst, dst, src), ctx);
706                 break;
707         /* dst = dst >> src */
708         case BPF_RSH:
709                 emit(ARM_LSR_R(dst, dst, src), ctx);
710                 break;
711         /* dst = dst >> src (signed)*/
712         case BPF_ARSH:
713                 emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
714                 break;
715         }
716 }
717
718 /* ALU operation (32 bit)
719  * dst = dst (op) src
720  */
721 static inline void emit_a32_alu_r(const s8 dst, const s8 src,
722                                   struct jit_ctx *ctx, const bool is64,
723                                   const bool hi, const u8 op) {
724         const s8 *tmp = bpf2a32[TMP_REG_1];
725         s8 rn, rd;
726
727         rn = arm_bpf_get_reg32(src, tmp[1], ctx);
728         rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
729         /* ALU operation */
730         emit_alu_r(rd, rn, is64, hi, op, ctx);
731         arm_bpf_put_reg32(dst, rd, ctx);
732 }
733
734 /* ALU operation (64 bit) */
735 static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
736                                   const s8 src[], struct jit_ctx *ctx,
737                                   const u8 op) {
738         const s8 *tmp = bpf2a32[TMP_REG_1];
739         const s8 *tmp2 = bpf2a32[TMP_REG_2];
740         const s8 *rd;
741
742         rd = arm_bpf_get_reg64(dst, tmp, ctx);
743         if (is64) {
744                 const s8 *rs;
745
746                 rs = arm_bpf_get_reg64(src, tmp2, ctx);
747
748                 /* ALU operation */
749                 emit_alu_r(rd[1], rs[1], true, false, op, ctx);
750                 emit_alu_r(rd[0], rs[0], true, true, op, ctx);
751         } else {
752                 s8 rs;
753
754                 rs = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
755
756                 /* ALU operation */
757                 emit_alu_r(rd[1], rs, true, false, op, ctx);
758                 emit_a32_mov_i(rd[0], 0, ctx);
759         }
760
761         arm_bpf_put_reg64(dst, rd, ctx);
762 }
763
764 /* dst = src (4 bytes)*/
765 static inline void emit_a32_mov_r(const s8 dst, const s8 src,
766                                   struct jit_ctx *ctx) {
767         const s8 *tmp = bpf2a32[TMP_REG_1];
768         s8 rt;
769
770         rt = arm_bpf_get_reg32(src, tmp[0], ctx);
771         arm_bpf_put_reg32(dst, rt, ctx);
772 }
773
774 /* dst = src */
775 static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
776                                   const s8 src[],
777                                   struct jit_ctx *ctx) {
778         if (!is64) {
779                 emit_a32_mov_r(dst_lo, src_lo, ctx);
780                 /* Zero out high 4 bytes */
781                 emit_a32_mov_i(dst_hi, 0, ctx);
782         } else if (__LINUX_ARM_ARCH__ < 6 &&
783                    ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
784                 /* complete 8 byte move */
785                 emit_a32_mov_r(dst_lo, src_lo, ctx);
786                 emit_a32_mov_r(dst_hi, src_hi, ctx);
787         } else if (is_stacked(src_lo) && is_stacked(dst_lo)) {
788                 const u8 *tmp = bpf2a32[TMP_REG_1];
789
790                 emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
791                 emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
792         } else if (is_stacked(src_lo)) {
793                 emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
794         } else if (is_stacked(dst_lo)) {
795                 emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
796         } else {
797                 emit(ARM_MOV_R(dst[0], src[0]), ctx);
798                 emit(ARM_MOV_R(dst[1], src[1]), ctx);
799         }
800 }
801
802 /* Shift operations */
803 static inline void emit_a32_alu_i(const s8 dst, const u32 val,
804                                 struct jit_ctx *ctx, const u8 op) {
805         const s8 *tmp = bpf2a32[TMP_REG_1];
806         s8 rd;
807
808         rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
809
810         /* Do shift operation */
811         switch (op) {
812         case BPF_LSH:
813                 emit(ARM_LSL_I(rd, rd, val), ctx);
814                 break;
815         case BPF_RSH:
816                 emit(ARM_LSR_I(rd, rd, val), ctx);
817                 break;
818         case BPF_NEG:
819                 emit(ARM_RSB_I(rd, rd, val), ctx);
820                 break;
821         }
822
823         arm_bpf_put_reg32(dst, rd, ctx);
824 }
825
826 /* dst = ~dst (64 bit) */
827 static inline void emit_a32_neg64(const s8 dst[],
828                                 struct jit_ctx *ctx){
829         const s8 *tmp = bpf2a32[TMP_REG_1];
830         const s8 *rd;
831
832         /* Setup Operand */
833         rd = arm_bpf_get_reg64(dst, tmp, ctx);
834
835         /* Do Negate Operation */
836         emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
837         emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
838
839         arm_bpf_put_reg64(dst, rd, ctx);
840 }
841
842 /* dst = dst << src */
843 static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
844                                     struct jit_ctx *ctx) {
845         const s8 *tmp = bpf2a32[TMP_REG_1];
846         const s8 *tmp2 = bpf2a32[TMP_REG_2];
847         const s8 *rd;
848         s8 rt;
849
850         /* Setup Operands */
851         rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
852         rd = arm_bpf_get_reg64(dst, tmp, ctx);
853
854         /* Do LSH operation */
855         emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
856         emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
857         emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
858         emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
859         emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
860         emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
861
862         arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
863         arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
864 }
865
866 /* dst = dst >> src (signed)*/
867 static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
868                                      struct jit_ctx *ctx) {
869         const s8 *tmp = bpf2a32[TMP_REG_1];
870         const s8 *tmp2 = bpf2a32[TMP_REG_2];
871         const s8 *rd;
872         s8 rt;
873
874         /* Setup Operands */
875         rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
876         rd = arm_bpf_get_reg64(dst, tmp, ctx);
877
878         /* Do the ARSH operation */
879         emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
880         emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
881         emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
882         emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
883         _emit(ARM_COND_MI, ARM_B(0), ctx);
884         emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
885         emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
886
887         arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
888         arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
889 }
890
891 /* dst = dst >> src */
892 static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
893                                     struct jit_ctx *ctx) {
894         const s8 *tmp = bpf2a32[TMP_REG_1];
895         const s8 *tmp2 = bpf2a32[TMP_REG_2];
896         const s8 *rd;
897         s8 rt;
898
899         /* Setup Operands */
900         rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
901         rd = arm_bpf_get_reg64(dst, tmp, ctx);
902
903         /* Do RSH operation */
904         emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
905         emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
906         emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
907         emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
908         emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
909         emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
910
911         arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
912         arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
913 }
914
915 /* dst = dst << val */
916 static inline void emit_a32_lsh_i64(const s8 dst[],
917                                     const u32 val, struct jit_ctx *ctx){
918         const s8 *tmp = bpf2a32[TMP_REG_1];
919         const s8 *tmp2 = bpf2a32[TMP_REG_2];
920         const s8 *rd;
921
922         /* Setup operands */
923         rd = arm_bpf_get_reg64(dst, tmp, ctx);
924
925         /* Do LSH operation */
926         if (val < 32) {
927                 emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
928                 emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
929                 emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
930         } else {
931                 if (val == 32)
932                         emit(ARM_MOV_R(rd[0], rd[1]), ctx);
933                 else
934                         emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
935                 emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
936         }
937
938         arm_bpf_put_reg64(dst, rd, ctx);
939 }
940
941 /* dst = dst >> val */
942 static inline void emit_a32_rsh_i64(const s8 dst[],
943                                     const u32 val, struct jit_ctx *ctx) {
944         const s8 *tmp = bpf2a32[TMP_REG_1];
945         const s8 *tmp2 = bpf2a32[TMP_REG_2];
946         const s8 *rd;
947
948         /* Setup operands */
949         rd = arm_bpf_get_reg64(dst, tmp, ctx);
950
951         /* Do LSR operation */
952         if (val == 0) {
953                 /* An immediate value of 0 encodes a shift amount of 32
954                  * for LSR. To shift by 0, don't do anything.
955                  */
956         } else if (val < 32) {
957                 emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
958                 emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
959                 emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
960         } else if (val == 32) {
961                 emit(ARM_MOV_R(rd[1], rd[0]), ctx);
962                 emit(ARM_MOV_I(rd[0], 0), ctx);
963         } else {
964                 emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
965                 emit(ARM_MOV_I(rd[0], 0), ctx);
966         }
967
968         arm_bpf_put_reg64(dst, rd, ctx);
969 }
970
971 /* dst = dst >> val (signed) */
972 static inline void emit_a32_arsh_i64(const s8 dst[],
973                                      const u32 val, struct jit_ctx *ctx){
974         const s8 *tmp = bpf2a32[TMP_REG_1];
975         const s8 *tmp2 = bpf2a32[TMP_REG_2];
976         const s8 *rd;
977
978         /* Setup operands */
979         rd = arm_bpf_get_reg64(dst, tmp, ctx);
980
981         /* Do ARSH operation */
982         if (val == 0) {
983                 /* An immediate value of 0 encodes a shift amount of 32
984                  * for ASR. To shift by 0, don't do anything.
985                  */
986         } else if (val < 32) {
987                 emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
988                 emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
989                 emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
990         } else if (val == 32) {
991                 emit(ARM_MOV_R(rd[1], rd[0]), ctx);
992                 emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
993         } else {
994                 emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
995                 emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
996         }
997
998         arm_bpf_put_reg64(dst, rd, ctx);
999 }
1000
1001 static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
1002                                     struct jit_ctx *ctx) {
1003         const s8 *tmp = bpf2a32[TMP_REG_1];
1004         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1005         const s8 *rd, *rt;
1006
1007         /* Setup operands for multiplication */
1008         rd = arm_bpf_get_reg64(dst, tmp, ctx);
1009         rt = arm_bpf_get_reg64(src, tmp2, ctx);
1010
1011         /* Do Multiplication */
1012         emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
1013         emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
1014         emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
1015
1016         emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
1017         emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
1018
1019         arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
1020         arm_bpf_put_reg32(dst_hi, rd[0], ctx);
1021 }
1022
1023 static bool is_ldst_imm(s16 off, const u8 size)
1024 {
1025         s16 off_max = 0;
1026
1027         switch (size) {
1028         case BPF_B:
1029         case BPF_W:
1030                 off_max = 0xfff;
1031                 break;
1032         case BPF_H:
1033                 off_max = 0xff;
1034                 break;
1035         case BPF_DW:
1036                 /* Need to make sure off+4 does not overflow. */
1037                 off_max = 0xfff - 4;
1038                 break;
1039         }
1040         return -off_max <= off && off <= off_max;
1041 }
1042
1043 /* *(size *)(dst + off) = src */
1044 static inline void emit_str_r(const s8 dst, const s8 src[],
1045                               s16 off, struct jit_ctx *ctx, const u8 sz){
1046         const s8 *tmp = bpf2a32[TMP_REG_1];
1047         s8 rd;
1048
1049         rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
1050
1051         if (!is_ldst_imm(off, sz)) {
1052                 emit_a32_mov_i(tmp[0], off, ctx);
1053                 emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
1054                 rd = tmp[0];
1055                 off = 0;
1056         }
1057         switch (sz) {
1058         case BPF_B:
1059                 /* Store a Byte */
1060                 emit(ARM_STRB_I(src_lo, rd, off), ctx);
1061                 break;
1062         case BPF_H:
1063                 /* Store a HalfWord */
1064                 emit(ARM_STRH_I(src_lo, rd, off), ctx);
1065                 break;
1066         case BPF_W:
1067                 /* Store a Word */
1068                 emit(ARM_STR_I(src_lo, rd, off), ctx);
1069                 break;
1070         case BPF_DW:
1071                 /* Store a Double Word */
1072                 emit(ARM_STR_I(src_lo, rd, off), ctx);
1073                 emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
1074                 break;
1075         }
1076 }
1077
1078 /* dst = *(size*)(src + off) */
1079 static inline void emit_ldx_r(const s8 dst[], const s8 src,
1080                               s16 off, struct jit_ctx *ctx, const u8 sz){
1081         const s8 *tmp = bpf2a32[TMP_REG_1];
1082         const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
1083         s8 rm = src;
1084
1085         if (!is_ldst_imm(off, sz)) {
1086                 emit_a32_mov_i(tmp[0], off, ctx);
1087                 emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
1088                 rm = tmp[0];
1089                 off = 0;
1090         } else if (rd[1] == rm) {
1091                 emit(ARM_MOV_R(tmp[0], rm), ctx);
1092                 rm = tmp[0];
1093         }
1094         switch (sz) {
1095         case BPF_B:
1096                 /* Load a Byte */
1097                 emit(ARM_LDRB_I(rd[1], rm, off), ctx);
1098                 emit_a32_mov_i(rd[0], 0, ctx);
1099                 break;
1100         case BPF_H:
1101                 /* Load a HalfWord */
1102                 emit(ARM_LDRH_I(rd[1], rm, off), ctx);
1103                 emit_a32_mov_i(rd[0], 0, ctx);
1104                 break;
1105         case BPF_W:
1106                 /* Load a Word */
1107                 emit(ARM_LDR_I(rd[1], rm, off), ctx);
1108                 emit_a32_mov_i(rd[0], 0, ctx);
1109                 break;
1110         case BPF_DW:
1111                 /* Load a Double Word */
1112                 emit(ARM_LDR_I(rd[1], rm, off), ctx);
1113                 emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
1114                 break;
1115         }
1116         arm_bpf_put_reg64(dst, rd, ctx);
1117 }
1118
1119 /* Arithmatic Operation */
1120 static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
1121                              const u8 rn, struct jit_ctx *ctx, u8 op) {
1122         switch (op) {
1123         case BPF_JSET:
1124                 emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
1125                 emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
1126                 emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
1127                 break;
1128         case BPF_JEQ:
1129         case BPF_JNE:
1130         case BPF_JGT:
1131         case BPF_JGE:
1132         case BPF_JLE:
1133         case BPF_JLT:
1134                 emit(ARM_CMP_R(rd, rm), ctx);
1135                 _emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
1136                 break;
1137         case BPF_JSLE:
1138         case BPF_JSGT:
1139                 emit(ARM_CMP_R(rn, rt), ctx);
1140                 emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
1141                 break;
1142         case BPF_JSLT:
1143         case BPF_JSGE:
1144                 emit(ARM_CMP_R(rt, rn), ctx);
1145                 emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
1146                 break;
1147         }
1148 }
1149
1150 static int out_offset = -1; /* initialized on the first pass of build_body() */
1151 static int emit_bpf_tail_call(struct jit_ctx *ctx)
1152 {
1153
1154         /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
1155         const s8 *r2 = bpf2a32[BPF_REG_2];
1156         const s8 *r3 = bpf2a32[BPF_REG_3];
1157         const s8 *tmp = bpf2a32[TMP_REG_1];
1158         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1159         const s8 *tcc = bpf2a32[TCALL_CNT];
1160         const s8 *tc;
1161         const int idx0 = ctx->idx;
1162 #define cur_offset (ctx->idx - idx0)
1163 #define jmp_offset (out_offset - (cur_offset) - 2)
1164         u32 lo, hi;
1165         s8 r_array, r_index;
1166         int off;
1167
1168         /* if (index >= array->map.max_entries)
1169          *      goto out;
1170          */
1171         BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
1172                      ARM_INST_LDST__IMM12);
1173         off = offsetof(struct bpf_array, map.max_entries);
1174         r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
1175         /* index is 32-bit for arrays */
1176         r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
1177         /* array->map.max_entries */
1178         emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
1179         /* index >= array->map.max_entries */
1180         emit(ARM_CMP_R(r_index, tmp[1]), ctx);
1181         _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
1182
1183         /* tmp2[0] = array, tmp2[1] = index */
1184
1185         /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
1186          *      goto out;
1187          * tail_call_cnt++;
1188          */
1189         lo = (u32)MAX_TAIL_CALL_CNT;
1190         hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1191         tc = arm_bpf_get_reg64(tcc, tmp, ctx);
1192         emit(ARM_CMP_I(tc[0], hi), ctx);
1193         _emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
1194         _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
1195         emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
1196         emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
1197         arm_bpf_put_reg64(tcc, tmp, ctx);
1198
1199         /* prog = array->ptrs[index]
1200          * if (prog == NULL)
1201          *      goto out;
1202          */
1203         BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
1204         off = imm8m(offsetof(struct bpf_array, ptrs));
1205         emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
1206         emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
1207         emit(ARM_CMP_I(tmp[1], 0), ctx);
1208         _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
1209
1210         /* goto *(prog->bpf_func + prologue_size); */
1211         BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
1212                      ARM_INST_LDST__IMM12);
1213         off = offsetof(struct bpf_prog, bpf_func);
1214         emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
1215         emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1216         emit_bx_r(tmp[1], ctx);
1217
1218         /* out: */
1219         if (out_offset == -1)
1220                 out_offset = cur_offset;
1221         if (cur_offset != out_offset) {
1222                 pr_err_once("tail_call out_offset = %d, expected %d!\n",
1223                             cur_offset, out_offset);
1224                 return -1;
1225         }
1226         return 0;
1227 #undef cur_offset
1228 #undef jmp_offset
1229 }
1230
1231 /* 0xabcd => 0xcdab */
1232 static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
1233 {
1234 #if __LINUX_ARM_ARCH__ < 6
1235         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1236
1237         emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
1238         emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
1239         emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
1240         emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
1241 #else /* ARMv6+ */
1242         emit(ARM_REV16(rd, rn), ctx);
1243 #endif
1244 }
1245
1246 /* 0xabcdefgh => 0xghefcdab */
1247 static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
1248 {
1249 #if __LINUX_ARM_ARCH__ < 6
1250         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1251
1252         emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
1253         emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
1254         emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
1255
1256         emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
1257         emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
1258         emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
1259         emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
1260         emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
1261         emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
1262         emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
1263
1264 #else /* ARMv6+ */
1265         emit(ARM_REV(rd, rn), ctx);
1266 #endif
1267 }
1268
1269 // push the scratch stack register on top of the stack
1270 static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
1271 {
1272         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1273         const s8 *rt;
1274         u16 reg_set = 0;
1275
1276         rt = arm_bpf_get_reg64(src, tmp2, ctx);
1277
1278         reg_set = (1 << rt[1]) | (1 << rt[0]);
1279         emit(ARM_PUSH(reg_set), ctx);
1280 }
1281
1282 static void build_prologue(struct jit_ctx *ctx)
1283 {
1284         const s8 r0 = bpf2a32[BPF_REG_0][1];
1285         const s8 r2 = bpf2a32[BPF_REG_1][1];
1286         const s8 r3 = bpf2a32[BPF_REG_1][0];
1287         const s8 r4 = bpf2a32[BPF_REG_6][1];
1288         const s8 fplo = bpf2a32[BPF_REG_FP][1];
1289         const s8 fphi = bpf2a32[BPF_REG_FP][0];
1290         const s8 *tcc = bpf2a32[TCALL_CNT];
1291
1292         /* Save callee saved registers. */
1293 #ifdef CONFIG_FRAME_POINTER
1294         u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
1295         emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
1296         emit(ARM_PUSH(reg_set), ctx);
1297         emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
1298 #else
1299         emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
1300         emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
1301 #endif
1302         /* Save frame pointer for later */
1303         emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx);
1304
1305         ctx->stack_size = imm8m(STACK_SIZE);
1306
1307         /* Set up function call stack */
1308         emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
1309
1310         /* Set up BPF prog stack base register */
1311         emit_a32_mov_r(fplo, ARM_IP, ctx);
1312         emit_a32_mov_i(fphi, 0, ctx);
1313
1314         /* mov r4, 0 */
1315         emit(ARM_MOV_I(r4, 0), ctx);
1316
1317         /* Move BPF_CTX to BPF_R1 */
1318         emit(ARM_MOV_R(r3, r4), ctx);
1319         emit(ARM_MOV_R(r2, r0), ctx);
1320         /* Initialize Tail Count */
1321         emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[0])), ctx);
1322         emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[1])), ctx);
1323         /* end of prologue */
1324 }
1325
1326 /* restore callee saved registers. */
1327 static void build_epilogue(struct jit_ctx *ctx)
1328 {
1329 #ifdef CONFIG_FRAME_POINTER
1330         /* When using frame pointers, some additional registers need to
1331          * be loaded. */
1332         u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
1333         emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
1334         emit(ARM_LDM(ARM_SP, reg_set), ctx);
1335 #else
1336         /* Restore callee saved registers. */
1337         emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
1338         emit(ARM_POP(CALLEE_POP_MASK), ctx);
1339 #endif
1340 }
1341
1342 /*
1343  * Convert an eBPF instruction to native instruction, i.e
1344  * JITs an eBPF instruction.
1345  * Returns :
1346  *      0  - Successfully JITed an 8-byte eBPF instruction
1347  *      >0 - Successfully JITed a 16-byte eBPF instruction
1348  *      <0 - Failed to JIT.
1349  */
1350 static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
1351 {
1352         const u8 code = insn->code;
1353         const s8 *dst = bpf2a32[insn->dst_reg];
1354         const s8 *src = bpf2a32[insn->src_reg];
1355         const s8 *tmp = bpf2a32[TMP_REG_1];
1356         const s8 *tmp2 = bpf2a32[TMP_REG_2];
1357         const s16 off = insn->off;
1358         const s32 imm = insn->imm;
1359         const int i = insn - ctx->prog->insnsi;
1360         const bool is64 = BPF_CLASS(code) == BPF_ALU64;
1361         const s8 *rd, *rs;
1362         s8 rd_lo, rt, rm, rn;
1363         s32 jmp_offset;
1364
1365 #define check_imm(bits, imm) do {                               \
1366         if ((imm) >= (1 << ((bits) - 1)) ||                     \
1367             (imm) < -(1 << ((bits) - 1))) {                     \
1368                 pr_info("[%2d] imm=%d(0x%x) out of range\n",    \
1369                         i, imm, imm);                           \
1370                 return -EINVAL;                                 \
1371         }                                                       \
1372 } while (0)
1373 #define check_imm24(imm) check_imm(24, imm)
1374
1375         switch (code) {
1376         /* ALU operations */
1377
1378         /* dst = src */
1379         case BPF_ALU | BPF_MOV | BPF_K:
1380         case BPF_ALU | BPF_MOV | BPF_X:
1381         case BPF_ALU64 | BPF_MOV | BPF_K:
1382         case BPF_ALU64 | BPF_MOV | BPF_X:
1383                 switch (BPF_SRC(code)) {
1384                 case BPF_X:
1385                         emit_a32_mov_r64(is64, dst, src, ctx);
1386                         break;
1387                 case BPF_K:
1388                         /* Sign-extend immediate value to destination reg */
1389                         emit_a32_mov_se_i64(is64, dst, imm, ctx);
1390                         break;
1391                 }
1392                 break;
1393         /* dst = dst + src/imm */
1394         /* dst = dst - src/imm */
1395         /* dst = dst | src/imm */
1396         /* dst = dst & src/imm */
1397         /* dst = dst ^ src/imm */
1398         /* dst = dst * src/imm */
1399         /* dst = dst << src */
1400         /* dst = dst >> src */
1401         case BPF_ALU | BPF_ADD | BPF_K:
1402         case BPF_ALU | BPF_ADD | BPF_X:
1403         case BPF_ALU | BPF_SUB | BPF_K:
1404         case BPF_ALU | BPF_SUB | BPF_X:
1405         case BPF_ALU | BPF_OR | BPF_K:
1406         case BPF_ALU | BPF_OR | BPF_X:
1407         case BPF_ALU | BPF_AND | BPF_K:
1408         case BPF_ALU | BPF_AND | BPF_X:
1409         case BPF_ALU | BPF_XOR | BPF_K:
1410         case BPF_ALU | BPF_XOR | BPF_X:
1411         case BPF_ALU | BPF_MUL | BPF_K:
1412         case BPF_ALU | BPF_MUL | BPF_X:
1413         case BPF_ALU | BPF_LSH | BPF_X:
1414         case BPF_ALU | BPF_RSH | BPF_X:
1415         case BPF_ALU | BPF_ARSH | BPF_K:
1416         case BPF_ALU | BPF_ARSH | BPF_X:
1417         case BPF_ALU64 | BPF_ADD | BPF_K:
1418         case BPF_ALU64 | BPF_ADD | BPF_X:
1419         case BPF_ALU64 | BPF_SUB | BPF_K:
1420         case BPF_ALU64 | BPF_SUB | BPF_X:
1421         case BPF_ALU64 | BPF_OR | BPF_K:
1422         case BPF_ALU64 | BPF_OR | BPF_X:
1423         case BPF_ALU64 | BPF_AND | BPF_K:
1424         case BPF_ALU64 | BPF_AND | BPF_X:
1425         case BPF_ALU64 | BPF_XOR | BPF_K:
1426         case BPF_ALU64 | BPF_XOR | BPF_X:
1427                 switch (BPF_SRC(code)) {
1428                 case BPF_X:
1429                         emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
1430                         break;
1431                 case BPF_K:
1432                         /* Move immediate value to the temporary register
1433                          * and then do the ALU operation on the temporary
1434                          * register as this will sign-extend the immediate
1435                          * value into temporary reg and then it would be
1436                          * safe to do the operation on it.
1437                          */
1438                         emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
1439                         emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
1440                         break;
1441                 }
1442                 break;
1443         /* dst = dst / src(imm) */
1444         /* dst = dst % src(imm) */
1445         case BPF_ALU | BPF_DIV | BPF_K:
1446         case BPF_ALU | BPF_DIV | BPF_X:
1447         case BPF_ALU | BPF_MOD | BPF_K:
1448         case BPF_ALU | BPF_MOD | BPF_X:
1449                 rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
1450                 switch (BPF_SRC(code)) {
1451                 case BPF_X:
1452                         rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
1453                         break;
1454                 case BPF_K:
1455                         rt = tmp2[0];
1456                         emit_a32_mov_i(rt, imm, ctx);
1457                         break;
1458                 default:
1459                         rt = src_lo;
1460                         break;
1461                 }
1462                 emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
1463                 arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
1464                 emit_a32_mov_i(dst_hi, 0, ctx);
1465                 break;
1466         case BPF_ALU64 | BPF_DIV | BPF_K:
1467         case BPF_ALU64 | BPF_DIV | BPF_X:
1468         case BPF_ALU64 | BPF_MOD | BPF_K:
1469         case BPF_ALU64 | BPF_MOD | BPF_X:
1470                 goto notyet;
1471         /* dst = dst >> imm */
1472         /* dst = dst << imm */
1473         case BPF_ALU | BPF_RSH | BPF_K:
1474         case BPF_ALU | BPF_LSH | BPF_K:
1475                 if (unlikely(imm > 31))
1476                         return -EINVAL;
1477                 if (imm)
1478                         emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
1479                 emit_a32_mov_i(dst_hi, 0, ctx);
1480                 break;
1481         /* dst = dst << imm */
1482         case BPF_ALU64 | BPF_LSH | BPF_K:
1483                 if (unlikely(imm > 63))
1484                         return -EINVAL;
1485                 emit_a32_lsh_i64(dst, imm, ctx);
1486                 break;
1487         /* dst = dst >> imm */
1488         case BPF_ALU64 | BPF_RSH | BPF_K:
1489                 if (unlikely(imm > 63))
1490                         return -EINVAL;
1491                 emit_a32_rsh_i64(dst, imm, ctx);
1492                 break;
1493         /* dst = dst << src */
1494         case BPF_ALU64 | BPF_LSH | BPF_X:
1495                 emit_a32_lsh_r64(dst, src, ctx);
1496                 break;
1497         /* dst = dst >> src */
1498         case BPF_ALU64 | BPF_RSH | BPF_X:
1499                 emit_a32_rsh_r64(dst, src, ctx);
1500                 break;
1501         /* dst = dst >> src (signed) */
1502         case BPF_ALU64 | BPF_ARSH | BPF_X:
1503                 emit_a32_arsh_r64(dst, src, ctx);
1504                 break;
1505         /* dst = dst >> imm (signed) */
1506         case BPF_ALU64 | BPF_ARSH | BPF_K:
1507                 if (unlikely(imm > 63))
1508                         return -EINVAL;
1509                 emit_a32_arsh_i64(dst, imm, ctx);
1510                 break;
1511         /* dst = ~dst */
1512         case BPF_ALU | BPF_NEG:
1513                 emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
1514                 emit_a32_mov_i(dst_hi, 0, ctx);
1515                 break;
1516         /* dst = ~dst (64 bit) */
1517         case BPF_ALU64 | BPF_NEG:
1518                 emit_a32_neg64(dst, ctx);
1519                 break;
1520         /* dst = dst * src/imm */
1521         case BPF_ALU64 | BPF_MUL | BPF_X:
1522         case BPF_ALU64 | BPF_MUL | BPF_K:
1523                 switch (BPF_SRC(code)) {
1524                 case BPF_X:
1525                         emit_a32_mul_r64(dst, src, ctx);
1526                         break;
1527                 case BPF_K:
1528                         /* Move immediate value to the temporary register
1529                          * and then do the multiplication on it as this
1530                          * will sign-extend the immediate value into temp
1531                          * reg then it would be safe to do the operation
1532                          * on it.
1533                          */
1534                         emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
1535                         emit_a32_mul_r64(dst, tmp2, ctx);
1536                         break;
1537                 }
1538                 break;
1539         /* dst = htole(dst) */
1540         /* dst = htobe(dst) */
1541         case BPF_ALU | BPF_END | BPF_FROM_LE:
1542         case BPF_ALU | BPF_END | BPF_FROM_BE:
1543                 rd = arm_bpf_get_reg64(dst, tmp, ctx);
1544                 if (BPF_SRC(code) == BPF_FROM_LE)
1545                         goto emit_bswap_uxt;
1546                 switch (imm) {
1547                 case 16:
1548                         emit_rev16(rd[1], rd[1], ctx);
1549                         goto emit_bswap_uxt;
1550                 case 32:
1551                         emit_rev32(rd[1], rd[1], ctx);
1552                         goto emit_bswap_uxt;
1553                 case 64:
1554                         emit_rev32(ARM_LR, rd[1], ctx);
1555                         emit_rev32(rd[1], rd[0], ctx);
1556                         emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
1557                         break;
1558                 }
1559                 goto exit;
1560 emit_bswap_uxt:
1561                 switch (imm) {
1562                 case 16:
1563                         /* zero-extend 16 bits into 64 bits */
1564 #if __LINUX_ARM_ARCH__ < 6
1565                         emit_a32_mov_i(tmp2[1], 0xffff, ctx);
1566                         emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
1567 #else /* ARMv6+ */
1568                         emit(ARM_UXTH(rd[1], rd[1]), ctx);
1569 #endif
1570                         emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1571                         break;
1572                 case 32:
1573                         /* zero-extend 32 bits into 64 bits */
1574                         emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1575                         break;
1576                 case 64:
1577                         /* nop */
1578                         break;
1579                 }
1580 exit:
1581                 arm_bpf_put_reg64(dst, rd, ctx);
1582                 break;
1583         /* dst = imm64 */
1584         case BPF_LD | BPF_IMM | BPF_DW:
1585         {
1586                 u64 val = (u32)imm | (u64)insn[1].imm << 32;
1587
1588                 emit_a32_mov_i64(dst, val, ctx);
1589
1590                 return 1;
1591         }
1592         /* LDX: dst = *(size *)(src + off) */
1593         case BPF_LDX | BPF_MEM | BPF_W:
1594         case BPF_LDX | BPF_MEM | BPF_H:
1595         case BPF_LDX | BPF_MEM | BPF_B:
1596         case BPF_LDX | BPF_MEM | BPF_DW:
1597                 rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
1598                 emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
1599                 break;
1600         /* speculation barrier */
1601         case BPF_ST | BPF_NOSPEC:
1602                 break;
1603         /* ST: *(size *)(dst + off) = imm */
1604         case BPF_ST | BPF_MEM | BPF_W:
1605         case BPF_ST | BPF_MEM | BPF_H:
1606         case BPF_ST | BPF_MEM | BPF_B:
1607         case BPF_ST | BPF_MEM | BPF_DW:
1608                 switch (BPF_SIZE(code)) {
1609                 case BPF_DW:
1610                         /* Sign-extend immediate value into temp reg */
1611                         emit_a32_mov_se_i64(true, tmp2, imm, ctx);
1612                         break;
1613                 case BPF_W:
1614                 case BPF_H:
1615                 case BPF_B:
1616                         emit_a32_mov_i(tmp2[1], imm, ctx);
1617                         break;
1618                 }
1619                 emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
1620                 break;
1621         /* STX XADD: lock *(u32 *)(dst + off) += src */
1622         case BPF_STX | BPF_XADD | BPF_W:
1623         /* STX XADD: lock *(u64 *)(dst + off) += src */
1624         case BPF_STX | BPF_XADD | BPF_DW:
1625                 goto notyet;
1626         /* STX: *(size *)(dst + off) = src */
1627         case BPF_STX | BPF_MEM | BPF_W:
1628         case BPF_STX | BPF_MEM | BPF_H:
1629         case BPF_STX | BPF_MEM | BPF_B:
1630         case BPF_STX | BPF_MEM | BPF_DW:
1631                 rs = arm_bpf_get_reg64(src, tmp2, ctx);
1632                 emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
1633                 break;
1634         /* PC += off if dst == src */
1635         /* PC += off if dst > src */
1636         /* PC += off if dst >= src */
1637         /* PC += off if dst < src */
1638         /* PC += off if dst <= src */
1639         /* PC += off if dst != src */
1640         /* PC += off if dst > src (signed) */
1641         /* PC += off if dst >= src (signed) */
1642         /* PC += off if dst < src (signed) */
1643         /* PC += off if dst <= src (signed) */
1644         /* PC += off if dst & src */
1645         case BPF_JMP | BPF_JEQ | BPF_X:
1646         case BPF_JMP | BPF_JGT | BPF_X:
1647         case BPF_JMP | BPF_JGE | BPF_X:
1648         case BPF_JMP | BPF_JNE | BPF_X:
1649         case BPF_JMP | BPF_JSGT | BPF_X:
1650         case BPF_JMP | BPF_JSGE | BPF_X:
1651         case BPF_JMP | BPF_JSET | BPF_X:
1652         case BPF_JMP | BPF_JLE | BPF_X:
1653         case BPF_JMP | BPF_JLT | BPF_X:
1654         case BPF_JMP | BPF_JSLT | BPF_X:
1655         case BPF_JMP | BPF_JSLE | BPF_X:
1656                 /* Setup source registers */
1657                 rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
1658                 rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
1659                 goto go_jmp;
1660         /* PC += off if dst == imm */
1661         /* PC += off if dst > imm */
1662         /* PC += off if dst >= imm */
1663         /* PC += off if dst < imm */
1664         /* PC += off if dst <= imm */
1665         /* PC += off if dst != imm */
1666         /* PC += off if dst > imm (signed) */
1667         /* PC += off if dst >= imm (signed) */
1668         /* PC += off if dst < imm (signed) */
1669         /* PC += off if dst <= imm (signed) */
1670         /* PC += off if dst & imm */
1671         case BPF_JMP | BPF_JEQ | BPF_K:
1672         case BPF_JMP | BPF_JGT | BPF_K:
1673         case BPF_JMP | BPF_JGE | BPF_K:
1674         case BPF_JMP | BPF_JNE | BPF_K:
1675         case BPF_JMP | BPF_JSGT | BPF_K:
1676         case BPF_JMP | BPF_JSGE | BPF_K:
1677         case BPF_JMP | BPF_JSET | BPF_K:
1678         case BPF_JMP | BPF_JLT | BPF_K:
1679         case BPF_JMP | BPF_JLE | BPF_K:
1680         case BPF_JMP | BPF_JSLT | BPF_K:
1681         case BPF_JMP | BPF_JSLE | BPF_K:
1682                 if (off == 0)
1683                         break;
1684                 rm = tmp2[0];
1685                 rn = tmp2[1];
1686                 /* Sign-extend immediate value */
1687                 emit_a32_mov_se_i64(true, tmp2, imm, ctx);
1688 go_jmp:
1689                 /* Setup destination register */
1690                 rd = arm_bpf_get_reg64(dst, tmp, ctx);
1691
1692                 /* Check for the condition */
1693                 emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code));
1694
1695                 /* Setup JUMP instruction */
1696                 jmp_offset = bpf2a32_offset(i+off, i, ctx);
1697                 switch (BPF_OP(code)) {
1698                 case BPF_JNE:
1699                 case BPF_JSET:
1700                         _emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
1701                         break;
1702                 case BPF_JEQ:
1703                         _emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
1704                         break;
1705                 case BPF_JGT:
1706                         _emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
1707                         break;
1708                 case BPF_JGE:
1709                         _emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
1710                         break;
1711                 case BPF_JSGT:
1712                         _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
1713                         break;
1714                 case BPF_JSGE:
1715                         _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
1716                         break;
1717                 case BPF_JLE:
1718                         _emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
1719                         break;
1720                 case BPF_JLT:
1721                         _emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
1722                         break;
1723                 case BPF_JSLT:
1724                         _emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
1725                         break;
1726                 case BPF_JSLE:
1727                         _emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
1728                         break;
1729                 }
1730                 break;
1731         /* JMP OFF */
1732         case BPF_JMP | BPF_JA:
1733         {
1734                 if (off == 0)
1735                         break;
1736                 jmp_offset = bpf2a32_offset(i+off, i, ctx);
1737                 check_imm24(jmp_offset);
1738                 emit(ARM_B(jmp_offset), ctx);
1739                 break;
1740         }
1741         /* tail call */
1742         case BPF_JMP | BPF_TAIL_CALL:
1743                 if (emit_bpf_tail_call(ctx))
1744                         return -EFAULT;
1745                 break;
1746         /* function call */
1747         case BPF_JMP | BPF_CALL:
1748         {
1749                 const s8 *r0 = bpf2a32[BPF_REG_0];
1750                 const s8 *r1 = bpf2a32[BPF_REG_1];
1751                 const s8 *r2 = bpf2a32[BPF_REG_2];
1752                 const s8 *r3 = bpf2a32[BPF_REG_3];
1753                 const s8 *r4 = bpf2a32[BPF_REG_4];
1754                 const s8 *r5 = bpf2a32[BPF_REG_5];
1755                 const u32 func = (u32)__bpf_call_base + (u32)imm;
1756
1757                 emit_a32_mov_r64(true, r0, r1, ctx);
1758                 emit_a32_mov_r64(true, r1, r2, ctx);
1759                 emit_push_r64(r5, ctx);
1760                 emit_push_r64(r4, ctx);
1761                 emit_push_r64(r3, ctx);
1762
1763                 emit_a32_mov_i(tmp[1], func, ctx);
1764                 emit_blx_r(tmp[1], ctx);
1765
1766                 emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
1767                 break;
1768         }
1769         /* function return */
1770         case BPF_JMP | BPF_EXIT:
1771                 /* Optimization: when last instruction is EXIT
1772                  * simply fallthrough to epilogue.
1773                  */
1774                 if (i == ctx->prog->len - 1)
1775                         break;
1776                 jmp_offset = epilogue_offset(ctx);
1777                 check_imm24(jmp_offset);
1778                 emit(ARM_B(jmp_offset), ctx);
1779                 break;
1780 notyet:
1781                 pr_info_once("*** NOT YET: opcode %02x ***\n", code);
1782                 return -EFAULT;
1783         default:
1784                 pr_err_once("unknown opcode %02x\n", code);
1785                 return -EINVAL;
1786         }
1787
1788         if (ctx->flags & FLAG_IMM_OVERFLOW)
1789                 /*
1790                  * this instruction generated an overflow when
1791                  * trying to access the literal pool, so
1792                  * delegate this filter to the kernel interpreter.
1793                  */
1794                 return -1;
1795         return 0;
1796 }
1797
1798 static int build_body(struct jit_ctx *ctx)
1799 {
1800         const struct bpf_prog *prog = ctx->prog;
1801         unsigned int i;
1802
1803         for (i = 0; i < prog->len; i++) {
1804                 const struct bpf_insn *insn = &(prog->insnsi[i]);
1805                 int ret;
1806
1807                 ret = build_insn(insn, ctx);
1808
1809                 /* It's used with loading the 64 bit immediate value. */
1810                 if (ret > 0) {
1811                         i++;
1812                         if (ctx->target == NULL)
1813                                 ctx->offsets[i] = ctx->idx;
1814                         continue;
1815                 }
1816
1817                 if (ctx->target == NULL)
1818                         ctx->offsets[i] = ctx->idx;
1819
1820                 /* If unsuccesfull, return with error code */
1821                 if (ret)
1822                         return ret;
1823         }
1824         return 0;
1825 }
1826
1827 static int validate_code(struct jit_ctx *ctx)
1828 {
1829         int i;
1830
1831         for (i = 0; i < ctx->idx; i++) {
1832                 if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
1833                         return -1;
1834         }
1835
1836         return 0;
1837 }
1838
1839 void bpf_jit_compile(struct bpf_prog *prog)
1840 {
1841         /* Nothing to do here. We support Internal BPF. */
1842 }
1843
1844 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
1845 {
1846         struct bpf_prog *tmp, *orig_prog = prog;
1847         struct bpf_binary_header *header;
1848         bool tmp_blinded = false;
1849         struct jit_ctx ctx;
1850         unsigned int tmp_idx;
1851         unsigned int image_size;
1852         u8 *image_ptr;
1853
1854         /* If BPF JIT was not enabled then we must fall back to
1855          * the interpreter.
1856          */
1857         if (!prog->jit_requested)
1858                 return orig_prog;
1859
1860         /* If constant blinding was enabled and we failed during blinding
1861          * then we must fall back to the interpreter. Otherwise, we save
1862          * the new JITed code.
1863          */
1864         tmp = bpf_jit_blind_constants(prog);
1865
1866         if (IS_ERR(tmp))
1867                 return orig_prog;
1868         if (tmp != prog) {
1869                 tmp_blinded = true;
1870                 prog = tmp;
1871         }
1872
1873         memset(&ctx, 0, sizeof(ctx));
1874         ctx.prog = prog;
1875         ctx.cpu_architecture = cpu_architecture();
1876
1877         /* Not able to allocate memory for offsets[] , then
1878          * we must fall back to the interpreter
1879          */
1880         ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
1881         if (ctx.offsets == NULL) {
1882                 prog = orig_prog;
1883                 goto out;
1884         }
1885
1886         /* 1) fake pass to find in the length of the JITed code,
1887          * to compute ctx->offsets and other context variables
1888          * needed to compute final JITed code.
1889          * Also, calculate random starting pointer/start of JITed code
1890          * which is prefixed by random number of fault instructions.
1891          *
1892          * If the first pass fails then there is no chance of it
1893          * being successful in the second pass, so just fall back
1894          * to the interpreter.
1895          */
1896         if (build_body(&ctx)) {
1897                 prog = orig_prog;
1898                 goto out_off;
1899         }
1900
1901         tmp_idx = ctx.idx;
1902         build_prologue(&ctx);
1903         ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1904
1905         ctx.epilogue_offset = ctx.idx;
1906
1907 #if __LINUX_ARM_ARCH__ < 7
1908         tmp_idx = ctx.idx;
1909         build_epilogue(&ctx);
1910         ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1911
1912         ctx.idx += ctx.imm_count;
1913         if (ctx.imm_count) {
1914                 ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
1915                 if (ctx.imms == NULL) {
1916                         prog = orig_prog;
1917                         goto out_off;
1918                 }
1919         }
1920 #else
1921         /* there's nothing about the epilogue on ARMv7 */
1922         build_epilogue(&ctx);
1923 #endif
1924         /* Now we can get the actual image size of the JITed arm code.
1925          * Currently, we are not considering the THUMB-2 instructions
1926          * for jit, although it can decrease the size of the image.
1927          *
1928          * As each arm instruction is of length 32bit, we are translating
1929          * number of JITed intructions into the size required to store these
1930          * JITed code.
1931          */
1932         image_size = sizeof(u32) * ctx.idx;
1933
1934         /* Now we know the size of the structure to make */
1935         header = bpf_jit_binary_alloc(image_size, &image_ptr,
1936                                       sizeof(u32), jit_fill_hole);
1937         /* Not able to allocate memory for the structure then
1938          * we must fall back to the interpretation
1939          */
1940         if (header == NULL) {
1941                 prog = orig_prog;
1942                 goto out_imms;
1943         }
1944
1945         /* 2.) Actual pass to generate final JIT code */
1946         ctx.target = (u32 *) image_ptr;
1947         ctx.idx = 0;
1948
1949         build_prologue(&ctx);
1950
1951         /* If building the body of the JITed code fails somehow,
1952          * we fall back to the interpretation.
1953          */
1954         if (build_body(&ctx) < 0) {
1955                 image_ptr = NULL;
1956                 bpf_jit_binary_free(header);
1957                 prog = orig_prog;
1958                 goto out_imms;
1959         }
1960         build_epilogue(&ctx);
1961
1962         /* 3.) Extra pass to validate JITed Code */
1963         if (validate_code(&ctx)) {
1964                 image_ptr = NULL;
1965                 bpf_jit_binary_free(header);
1966                 prog = orig_prog;
1967                 goto out_imms;
1968         }
1969         flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
1970
1971         if (bpf_jit_enable > 1)
1972                 /* there are 2 passes here */
1973                 bpf_jit_dump(prog->len, image_size, 2, ctx.target);
1974
1975         bpf_jit_binary_lock_ro(header);
1976         prog->bpf_func = (void *)ctx.target;
1977         prog->jited = 1;
1978         prog->jited_len = image_size;
1979
1980 out_imms:
1981 #if __LINUX_ARM_ARCH__ < 7
1982         if (ctx.imm_count)
1983                 kfree(ctx.imms);
1984 #endif
1985 out_off:
1986         kfree(ctx.offsets);
1987 out:
1988         if (tmp_blinded)
1989                 bpf_jit_prog_release_other(prog, prog == orig_prog ?
1990                                            tmp : orig_prog);
1991         return prog;
1992 }
1993