arm64: bpf: fix div-by-zero case
[linux-2.6-block.git] / arch / arm64 / net / bpf_jit_comp.c
1 /*
2  * BPF JIT compiler for ARM64
3  *
4  * Copyright (C) 2014-2015 Zi Shen Lim <zlim.lnx@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #define pr_fmt(fmt) "bpf_jit: " fmt
20
21 #include <linux/filter.h>
22 #include <linux/printk.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25
26 #include <asm/byteorder.h>
27 #include <asm/cacheflush.h>
28 #include <asm/debug-monitors.h>
29
30 #include "bpf_jit.h"
31
32 int bpf_jit_enable __read_mostly;
33
34 #define TMP_REG_1 (MAX_BPF_REG + 0)
35 #define TMP_REG_2 (MAX_BPF_REG + 1)
36
37 /* Map BPF registers to A64 registers */
38 static const int bpf2a64[] = {
39         /* return value from in-kernel function, and exit value from eBPF */
40         [BPF_REG_0] = A64_R(7),
41         /* arguments from eBPF program to in-kernel function */
42         [BPF_REG_1] = A64_R(0),
43         [BPF_REG_2] = A64_R(1),
44         [BPF_REG_3] = A64_R(2),
45         [BPF_REG_4] = A64_R(3),
46         [BPF_REG_5] = A64_R(4),
47         /* callee saved registers that in-kernel function will preserve */
48         [BPF_REG_6] = A64_R(19),
49         [BPF_REG_7] = A64_R(20),
50         [BPF_REG_8] = A64_R(21),
51         [BPF_REG_9] = A64_R(22),
52         /* read-only frame pointer to access stack */
53         [BPF_REG_FP] = A64_FP,
54         /* temporary register for internal BPF JIT */
55         [TMP_REG_1] = A64_R(23),
56         [TMP_REG_2] = A64_R(24),
57 };
58
59 struct jit_ctx {
60         const struct bpf_prog *prog;
61         int idx;
62         int tmp_used;
63         int epilogue_offset;
64         int *offset;
65         u32 *image;
66 };
67
68 static inline void emit(const u32 insn, struct jit_ctx *ctx)
69 {
70         if (ctx->image != NULL)
71                 ctx->image[ctx->idx] = cpu_to_le32(insn);
72
73         ctx->idx++;
74 }
75
76 static inline void emit_a64_mov_i64(const int reg, const u64 val,
77                                     struct jit_ctx *ctx)
78 {
79         u64 tmp = val;
80         int shift = 0;
81
82         emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
83         tmp >>= 16;
84         shift += 16;
85         while (tmp) {
86                 if (tmp & 0xffff)
87                         emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
88                 tmp >>= 16;
89                 shift += 16;
90         }
91 }
92
93 static inline void emit_a64_mov_i(const int is64, const int reg,
94                                   const s32 val, struct jit_ctx *ctx)
95 {
96         u16 hi = val >> 16;
97         u16 lo = val & 0xffff;
98
99         if (hi & 0x8000) {
100                 if (hi == 0xffff) {
101                         emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
102                 } else {
103                         emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
104                         emit(A64_MOVK(is64, reg, lo, 0), ctx);
105                 }
106         } else {
107                 emit(A64_MOVZ(is64, reg, lo, 0), ctx);
108                 if (hi)
109                         emit(A64_MOVK(is64, reg, hi, 16), ctx);
110         }
111 }
112
113 static inline int bpf2a64_offset(int bpf_to, int bpf_from,
114                                  const struct jit_ctx *ctx)
115 {
116         int to = ctx->offset[bpf_to];
117         /* -1 to account for the Branch instruction */
118         int from = ctx->offset[bpf_from] - 1;
119
120         return to - from;
121 }
122
123 static void jit_fill_hole(void *area, unsigned int size)
124 {
125         u32 *ptr;
126         /* We are guaranteed to have aligned memory. */
127         for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
128                 *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
129 }
130
131 static inline int epilogue_offset(const struct jit_ctx *ctx)
132 {
133         int to = ctx->epilogue_offset;
134         int from = ctx->idx;
135
136         return to - from;
137 }
138
139 /* Stack must be multiples of 16B */
140 #define STACK_ALIGN(sz) (((sz) + 15) & ~15)
141
142 static void build_prologue(struct jit_ctx *ctx)
143 {
144         const u8 r6 = bpf2a64[BPF_REG_6];
145         const u8 r7 = bpf2a64[BPF_REG_7];
146         const u8 r8 = bpf2a64[BPF_REG_8];
147         const u8 r9 = bpf2a64[BPF_REG_9];
148         const u8 fp = bpf2a64[BPF_REG_FP];
149         const u8 ra = bpf2a64[BPF_REG_A];
150         const u8 rx = bpf2a64[BPF_REG_X];
151         const u8 tmp1 = bpf2a64[TMP_REG_1];
152         const u8 tmp2 = bpf2a64[TMP_REG_2];
153         int stack_size = MAX_BPF_STACK;
154
155         stack_size += 4; /* extra for skb_copy_bits buffer */
156         stack_size = STACK_ALIGN(stack_size);
157
158         /* Save callee-saved register */
159         emit(A64_PUSH(r6, r7, A64_SP), ctx);
160         emit(A64_PUSH(r8, r9, A64_SP), ctx);
161         if (ctx->tmp_used)
162                 emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx);
163
164         /* Set up BPF stack */
165         emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
166
167         /* Set up frame pointer */
168         emit(A64_MOV(1, fp, A64_SP), ctx);
169
170         /* Clear registers A and X */
171         emit_a64_mov_i64(ra, 0, ctx);
172         emit_a64_mov_i64(rx, 0, ctx);
173 }
174
175 static void build_epilogue(struct jit_ctx *ctx)
176 {
177         const u8 r0 = bpf2a64[BPF_REG_0];
178         const u8 r6 = bpf2a64[BPF_REG_6];
179         const u8 r7 = bpf2a64[BPF_REG_7];
180         const u8 r8 = bpf2a64[BPF_REG_8];
181         const u8 r9 = bpf2a64[BPF_REG_9];
182         const u8 fp = bpf2a64[BPF_REG_FP];
183         const u8 tmp1 = bpf2a64[TMP_REG_1];
184         const u8 tmp2 = bpf2a64[TMP_REG_2];
185         int stack_size = MAX_BPF_STACK;
186
187         stack_size += 4; /* extra for skb_copy_bits buffer */
188         stack_size = STACK_ALIGN(stack_size);
189
190         /* We're done with BPF stack */
191         emit(A64_ADD_I(1, A64_SP, A64_SP, stack_size), ctx);
192
193         /* Restore callee-saved register */
194         if (ctx->tmp_used)
195                 emit(A64_POP(tmp1, tmp2, A64_SP), ctx);
196         emit(A64_POP(r8, r9, A64_SP), ctx);
197         emit(A64_POP(r6, r7, A64_SP), ctx);
198
199         /* Restore frame pointer */
200         emit(A64_MOV(1, fp, A64_SP), ctx);
201
202         /* Set return value */
203         emit(A64_MOV(1, A64_R(0), r0), ctx);
204
205         emit(A64_RET(A64_LR), ctx);
206 }
207
208 /* JITs an eBPF instruction.
209  * Returns:
210  * 0  - successfully JITed an 8-byte eBPF instruction.
211  * >0 - successfully JITed a 16-byte eBPF instruction.
212  * <0 - failed to JIT.
213  */
214 static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
215 {
216         const u8 code = insn->code;
217         const u8 dst = bpf2a64[insn->dst_reg];
218         const u8 src = bpf2a64[insn->src_reg];
219         const u8 tmp = bpf2a64[TMP_REG_1];
220         const u8 tmp2 = bpf2a64[TMP_REG_2];
221         const s16 off = insn->off;
222         const s32 imm = insn->imm;
223         const int i = insn - ctx->prog->insnsi;
224         const bool is64 = BPF_CLASS(code) == BPF_ALU64;
225         u8 jmp_cond;
226         s32 jmp_offset;
227
228 #define check_imm(bits, imm) do {                               \
229         if ((((imm) > 0) && ((imm) >> (bits))) ||               \
230             (((imm) < 0) && (~(imm) >> (bits)))) {              \
231                 pr_info("[%2d] imm=%d(0x%x) out of range\n",    \
232                         i, imm, imm);                           \
233                 return -EINVAL;                                 \
234         }                                                       \
235 } while (0)
236 #define check_imm19(imm) check_imm(19, imm)
237 #define check_imm26(imm) check_imm(26, imm)
238
239         switch (code) {
240         /* dst = src */
241         case BPF_ALU | BPF_MOV | BPF_X:
242         case BPF_ALU64 | BPF_MOV | BPF_X:
243                 emit(A64_MOV(is64, dst, src), ctx);
244                 break;
245         /* dst = dst OP src */
246         case BPF_ALU | BPF_ADD | BPF_X:
247         case BPF_ALU64 | BPF_ADD | BPF_X:
248                 emit(A64_ADD(is64, dst, dst, src), ctx);
249                 break;
250         case BPF_ALU | BPF_SUB | BPF_X:
251         case BPF_ALU64 | BPF_SUB | BPF_X:
252                 emit(A64_SUB(is64, dst, dst, src), ctx);
253                 break;
254         case BPF_ALU | BPF_AND | BPF_X:
255         case BPF_ALU64 | BPF_AND | BPF_X:
256                 emit(A64_AND(is64, dst, dst, src), ctx);
257                 break;
258         case BPF_ALU | BPF_OR | BPF_X:
259         case BPF_ALU64 | BPF_OR | BPF_X:
260                 emit(A64_ORR(is64, dst, dst, src), ctx);
261                 break;
262         case BPF_ALU | BPF_XOR | BPF_X:
263         case BPF_ALU64 | BPF_XOR | BPF_X:
264                 emit(A64_EOR(is64, dst, dst, src), ctx);
265                 break;
266         case BPF_ALU | BPF_MUL | BPF_X:
267         case BPF_ALU64 | BPF_MUL | BPF_X:
268                 emit(A64_MUL(is64, dst, dst, src), ctx);
269                 break;
270         case BPF_ALU | BPF_DIV | BPF_X:
271         case BPF_ALU64 | BPF_DIV | BPF_X:
272         {
273                 const u8 r0 = bpf2a64[BPF_REG_0];
274
275                 /* if (src == 0) return 0 */
276                 jmp_offset = 3; /* skip ahead to else path */
277                 check_imm19(jmp_offset);
278                 emit(A64_CBNZ(is64, src, jmp_offset), ctx);
279                 emit(A64_MOVZ(1, r0, 0, 0), ctx);
280                 jmp_offset = epilogue_offset(ctx);
281                 check_imm26(jmp_offset);
282                 emit(A64_B(jmp_offset), ctx);
283                 /* else */
284                 emit(A64_UDIV(is64, dst, dst, src), ctx);
285                 break;
286         }
287         case BPF_ALU | BPF_MOD | BPF_X:
288         case BPF_ALU64 | BPF_MOD | BPF_X:
289                 ctx->tmp_used = 1;
290                 emit(A64_UDIV(is64, tmp, dst, src), ctx);
291                 emit(A64_MUL(is64, tmp, tmp, src), ctx);
292                 emit(A64_SUB(is64, dst, dst, tmp), ctx);
293                 break;
294         case BPF_ALU | BPF_LSH | BPF_X:
295         case BPF_ALU64 | BPF_LSH | BPF_X:
296                 emit(A64_LSLV(is64, dst, dst, src), ctx);
297                 break;
298         case BPF_ALU | BPF_RSH | BPF_X:
299         case BPF_ALU64 | BPF_RSH | BPF_X:
300                 emit(A64_LSRV(is64, dst, dst, src), ctx);
301                 break;
302         case BPF_ALU | BPF_ARSH | BPF_X:
303         case BPF_ALU64 | BPF_ARSH | BPF_X:
304                 emit(A64_ASRV(is64, dst, dst, src), ctx);
305                 break;
306         /* dst = -dst */
307         case BPF_ALU | BPF_NEG:
308         case BPF_ALU64 | BPF_NEG:
309                 emit(A64_NEG(is64, dst, dst), ctx);
310                 break;
311         /* dst = BSWAP##imm(dst) */
312         case BPF_ALU | BPF_END | BPF_FROM_LE:
313         case BPF_ALU | BPF_END | BPF_FROM_BE:
314 #ifdef CONFIG_CPU_BIG_ENDIAN
315                 if (BPF_SRC(code) == BPF_FROM_BE)
316                         goto emit_bswap_uxt;
317 #else /* !CONFIG_CPU_BIG_ENDIAN */
318                 if (BPF_SRC(code) == BPF_FROM_LE)
319                         goto emit_bswap_uxt;
320 #endif
321                 switch (imm) {
322                 case 16:
323                         emit(A64_REV16(is64, dst, dst), ctx);
324                         /* zero-extend 16 bits into 64 bits */
325                         emit(A64_UXTH(is64, dst, dst), ctx);
326                         break;
327                 case 32:
328                         emit(A64_REV32(is64, dst, dst), ctx);
329                         /* upper 32 bits already cleared */
330                         break;
331                 case 64:
332                         emit(A64_REV64(dst, dst), ctx);
333                         break;
334                 }
335                 break;
336 emit_bswap_uxt:
337                 switch (imm) {
338                 case 16:
339                         /* zero-extend 16 bits into 64 bits */
340                         emit(A64_UXTH(is64, dst, dst), ctx);
341                         break;
342                 case 32:
343                         /* zero-extend 32 bits into 64 bits */
344                         emit(A64_UXTW(is64, dst, dst), ctx);
345                         break;
346                 case 64:
347                         /* nop */
348                         break;
349                 }
350                 break;
351         /* dst = imm */
352         case BPF_ALU | BPF_MOV | BPF_K:
353         case BPF_ALU64 | BPF_MOV | BPF_K:
354                 emit_a64_mov_i(is64, dst, imm, ctx);
355                 break;
356         /* dst = dst OP imm */
357         case BPF_ALU | BPF_ADD | BPF_K:
358         case BPF_ALU64 | BPF_ADD | BPF_K:
359                 ctx->tmp_used = 1;
360                 emit_a64_mov_i(is64, tmp, imm, ctx);
361                 emit(A64_ADD(is64, dst, dst, tmp), ctx);
362                 break;
363         case BPF_ALU | BPF_SUB | BPF_K:
364         case BPF_ALU64 | BPF_SUB | BPF_K:
365                 ctx->tmp_used = 1;
366                 emit_a64_mov_i(is64, tmp, imm, ctx);
367                 emit(A64_SUB(is64, dst, dst, tmp), ctx);
368                 break;
369         case BPF_ALU | BPF_AND | BPF_K:
370         case BPF_ALU64 | BPF_AND | BPF_K:
371                 ctx->tmp_used = 1;
372                 emit_a64_mov_i(is64, tmp, imm, ctx);
373                 emit(A64_AND(is64, dst, dst, tmp), ctx);
374                 break;
375         case BPF_ALU | BPF_OR | BPF_K:
376         case BPF_ALU64 | BPF_OR | BPF_K:
377                 ctx->tmp_used = 1;
378                 emit_a64_mov_i(is64, tmp, imm, ctx);
379                 emit(A64_ORR(is64, dst, dst, tmp), ctx);
380                 break;
381         case BPF_ALU | BPF_XOR | BPF_K:
382         case BPF_ALU64 | BPF_XOR | BPF_K:
383                 ctx->tmp_used = 1;
384                 emit_a64_mov_i(is64, tmp, imm, ctx);
385                 emit(A64_EOR(is64, dst, dst, tmp), ctx);
386                 break;
387         case BPF_ALU | BPF_MUL | BPF_K:
388         case BPF_ALU64 | BPF_MUL | BPF_K:
389                 ctx->tmp_used = 1;
390                 emit_a64_mov_i(is64, tmp, imm, ctx);
391                 emit(A64_MUL(is64, dst, dst, tmp), ctx);
392                 break;
393         case BPF_ALU | BPF_DIV | BPF_K:
394         case BPF_ALU64 | BPF_DIV | BPF_K:
395                 ctx->tmp_used = 1;
396                 emit_a64_mov_i(is64, tmp, imm, ctx);
397                 emit(A64_UDIV(is64, dst, dst, tmp), ctx);
398                 break;
399         case BPF_ALU | BPF_MOD | BPF_K:
400         case BPF_ALU64 | BPF_MOD | BPF_K:
401                 ctx->tmp_used = 1;
402                 emit_a64_mov_i(is64, tmp2, imm, ctx);
403                 emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
404                 emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
405                 emit(A64_SUB(is64, dst, dst, tmp), ctx);
406                 break;
407         case BPF_ALU | BPF_LSH | BPF_K:
408         case BPF_ALU64 | BPF_LSH | BPF_K:
409                 emit(A64_LSL(is64, dst, dst, imm), ctx);
410                 break;
411         case BPF_ALU | BPF_RSH | BPF_K:
412         case BPF_ALU64 | BPF_RSH | BPF_K:
413                 emit(A64_LSR(is64, dst, dst, imm), ctx);
414                 break;
415         case BPF_ALU | BPF_ARSH | BPF_K:
416         case BPF_ALU64 | BPF_ARSH | BPF_K:
417                 emit(A64_ASR(is64, dst, dst, imm), ctx);
418                 break;
419
420         /* JUMP off */
421         case BPF_JMP | BPF_JA:
422                 jmp_offset = bpf2a64_offset(i + off, i, ctx);
423                 check_imm26(jmp_offset);
424                 emit(A64_B(jmp_offset), ctx);
425                 break;
426         /* IF (dst COND src) JUMP off */
427         case BPF_JMP | BPF_JEQ | BPF_X:
428         case BPF_JMP | BPF_JGT | BPF_X:
429         case BPF_JMP | BPF_JGE | BPF_X:
430         case BPF_JMP | BPF_JNE | BPF_X:
431         case BPF_JMP | BPF_JSGT | BPF_X:
432         case BPF_JMP | BPF_JSGE | BPF_X:
433                 emit(A64_CMP(1, dst, src), ctx);
434 emit_cond_jmp:
435                 jmp_offset = bpf2a64_offset(i + off, i, ctx);
436                 check_imm19(jmp_offset);
437                 switch (BPF_OP(code)) {
438                 case BPF_JEQ:
439                         jmp_cond = A64_COND_EQ;
440                         break;
441                 case BPF_JGT:
442                         jmp_cond = A64_COND_HI;
443                         break;
444                 case BPF_JGE:
445                         jmp_cond = A64_COND_CS;
446                         break;
447                 case BPF_JNE:
448                         jmp_cond = A64_COND_NE;
449                         break;
450                 case BPF_JSGT:
451                         jmp_cond = A64_COND_GT;
452                         break;
453                 case BPF_JSGE:
454                         jmp_cond = A64_COND_GE;
455                         break;
456                 default:
457                         return -EFAULT;
458                 }
459                 emit(A64_B_(jmp_cond, jmp_offset), ctx);
460                 break;
461         case BPF_JMP | BPF_JSET | BPF_X:
462                 emit(A64_TST(1, dst, src), ctx);
463                 goto emit_cond_jmp;
464         /* IF (dst COND imm) JUMP off */
465         case BPF_JMP | BPF_JEQ | BPF_K:
466         case BPF_JMP | BPF_JGT | BPF_K:
467         case BPF_JMP | BPF_JGE | BPF_K:
468         case BPF_JMP | BPF_JNE | BPF_K:
469         case BPF_JMP | BPF_JSGT | BPF_K:
470         case BPF_JMP | BPF_JSGE | BPF_K:
471                 ctx->tmp_used = 1;
472                 emit_a64_mov_i(1, tmp, imm, ctx);
473                 emit(A64_CMP(1, dst, tmp), ctx);
474                 goto emit_cond_jmp;
475         case BPF_JMP | BPF_JSET | BPF_K:
476                 ctx->tmp_used = 1;
477                 emit_a64_mov_i(1, tmp, imm, ctx);
478                 emit(A64_TST(1, dst, tmp), ctx);
479                 goto emit_cond_jmp;
480         /* function call */
481         case BPF_JMP | BPF_CALL:
482         {
483                 const u8 r0 = bpf2a64[BPF_REG_0];
484                 const u64 func = (u64)__bpf_call_base + imm;
485
486                 ctx->tmp_used = 1;
487                 emit_a64_mov_i64(tmp, func, ctx);
488                 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
489                 emit(A64_MOV(1, A64_FP, A64_SP), ctx);
490                 emit(A64_BLR(tmp), ctx);
491                 emit(A64_MOV(1, r0, A64_R(0)), ctx);
492                 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
493                 break;
494         }
495         /* function return */
496         case BPF_JMP | BPF_EXIT:
497                 /* Optimization: when last instruction is EXIT,
498                    simply fallthrough to epilogue. */
499                 if (i == ctx->prog->len - 1)
500                         break;
501                 jmp_offset = epilogue_offset(ctx);
502                 check_imm26(jmp_offset);
503                 emit(A64_B(jmp_offset), ctx);
504                 break;
505
506         /* dst = imm64 */
507         case BPF_LD | BPF_IMM | BPF_DW:
508         {
509                 const struct bpf_insn insn1 = insn[1];
510                 u64 imm64;
511
512                 if (insn1.code != 0 || insn1.src_reg != 0 ||
513                     insn1.dst_reg != 0 || insn1.off != 0) {
514                         /* Note: verifier in BPF core must catch invalid
515                          * instructions.
516                          */
517                         pr_err_once("Invalid BPF_LD_IMM64 instruction\n");
518                         return -EINVAL;
519                 }
520
521                 imm64 = (u64)insn1.imm << 32 | (u32)imm;
522                 emit_a64_mov_i64(dst, imm64, ctx);
523
524                 return 1;
525         }
526
527         /* LDX: dst = *(size *)(src + off) */
528         case BPF_LDX | BPF_MEM | BPF_W:
529         case BPF_LDX | BPF_MEM | BPF_H:
530         case BPF_LDX | BPF_MEM | BPF_B:
531         case BPF_LDX | BPF_MEM | BPF_DW:
532                 ctx->tmp_used = 1;
533                 emit_a64_mov_i(1, tmp, off, ctx);
534                 switch (BPF_SIZE(code)) {
535                 case BPF_W:
536                         emit(A64_LDR32(dst, src, tmp), ctx);
537                         break;
538                 case BPF_H:
539                         emit(A64_LDRH(dst, src, tmp), ctx);
540                         break;
541                 case BPF_B:
542                         emit(A64_LDRB(dst, src, tmp), ctx);
543                         break;
544                 case BPF_DW:
545                         emit(A64_LDR64(dst, src, tmp), ctx);
546                         break;
547                 }
548                 break;
549
550         /* ST: *(size *)(dst + off) = imm */
551         case BPF_ST | BPF_MEM | BPF_W:
552         case BPF_ST | BPF_MEM | BPF_H:
553         case BPF_ST | BPF_MEM | BPF_B:
554         case BPF_ST | BPF_MEM | BPF_DW:
555                 goto notyet;
556
557         /* STX: *(size *)(dst + off) = src */
558         case BPF_STX | BPF_MEM | BPF_W:
559         case BPF_STX | BPF_MEM | BPF_H:
560         case BPF_STX | BPF_MEM | BPF_B:
561         case BPF_STX | BPF_MEM | BPF_DW:
562                 ctx->tmp_used = 1;
563                 emit_a64_mov_i(1, tmp, off, ctx);
564                 switch (BPF_SIZE(code)) {
565                 case BPF_W:
566                         emit(A64_STR32(src, dst, tmp), ctx);
567                         break;
568                 case BPF_H:
569                         emit(A64_STRH(src, dst, tmp), ctx);
570                         break;
571                 case BPF_B:
572                         emit(A64_STRB(src, dst, tmp), ctx);
573                         break;
574                 case BPF_DW:
575                         emit(A64_STR64(src, dst, tmp), ctx);
576                         break;
577                 }
578                 break;
579         /* STX XADD: lock *(u32 *)(dst + off) += src */
580         case BPF_STX | BPF_XADD | BPF_W:
581         /* STX XADD: lock *(u64 *)(dst + off) += src */
582         case BPF_STX | BPF_XADD | BPF_DW:
583                 goto notyet;
584
585         /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
586         case BPF_LD | BPF_ABS | BPF_W:
587         case BPF_LD | BPF_ABS | BPF_H:
588         case BPF_LD | BPF_ABS | BPF_B:
589         /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
590         case BPF_LD | BPF_IND | BPF_W:
591         case BPF_LD | BPF_IND | BPF_H:
592         case BPF_LD | BPF_IND | BPF_B:
593         {
594                 const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */
595                 const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */
596                 const u8 fp = bpf2a64[BPF_REG_FP];
597                 const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */
598                 const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */
599                 const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */
600                 const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */
601                 const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */
602                 int size;
603
604                 emit(A64_MOV(1, r1, r6), ctx);
605                 emit_a64_mov_i(0, r2, imm, ctx);
606                 if (BPF_MODE(code) == BPF_IND)
607                         emit(A64_ADD(0, r2, r2, src), ctx);
608                 switch (BPF_SIZE(code)) {
609                 case BPF_W:
610                         size = 4;
611                         break;
612                 case BPF_H:
613                         size = 2;
614                         break;
615                 case BPF_B:
616                         size = 1;
617                         break;
618                 default:
619                         return -EINVAL;
620                 }
621                 emit_a64_mov_i64(r3, size, ctx);
622                 emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx);
623                 emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
624                 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
625                 emit(A64_MOV(1, A64_FP, A64_SP), ctx);
626                 emit(A64_BLR(r5), ctx);
627                 emit(A64_MOV(1, r0, A64_R(0)), ctx);
628                 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
629
630                 jmp_offset = epilogue_offset(ctx);
631                 check_imm19(jmp_offset);
632                 emit(A64_CBZ(1, r0, jmp_offset), ctx);
633                 emit(A64_MOV(1, r5, r0), ctx);
634                 switch (BPF_SIZE(code)) {
635                 case BPF_W:
636                         emit(A64_LDR32(r0, r5, A64_ZR), ctx);
637 #ifndef CONFIG_CPU_BIG_ENDIAN
638                         emit(A64_REV32(0, r0, r0), ctx);
639 #endif
640                         break;
641                 case BPF_H:
642                         emit(A64_LDRH(r0, r5, A64_ZR), ctx);
643 #ifndef CONFIG_CPU_BIG_ENDIAN
644                         emit(A64_REV16(0, r0, r0), ctx);
645 #endif
646                         break;
647                 case BPF_B:
648                         emit(A64_LDRB(r0, r5, A64_ZR), ctx);
649                         break;
650                 }
651                 break;
652         }
653 notyet:
654                 pr_info_once("*** NOT YET: opcode %02x ***\n", code);
655                 return -EFAULT;
656
657         default:
658                 pr_err_once("unknown opcode %02x\n", code);
659                 return -EINVAL;
660         }
661
662         return 0;
663 }
664
665 static int build_body(struct jit_ctx *ctx)
666 {
667         const struct bpf_prog *prog = ctx->prog;
668         int i;
669
670         for (i = 0; i < prog->len; i++) {
671                 const struct bpf_insn *insn = &prog->insnsi[i];
672                 int ret;
673
674                 ret = build_insn(insn, ctx);
675
676                 if (ctx->image == NULL)
677                         ctx->offset[i] = ctx->idx;
678
679                 if (ret > 0) {
680                         i++;
681                         continue;
682                 }
683                 if (ret)
684                         return ret;
685         }
686
687         return 0;
688 }
689
690 static inline void bpf_flush_icache(void *start, void *end)
691 {
692         flush_icache_range((unsigned long)start, (unsigned long)end);
693 }
694
695 void bpf_jit_compile(struct bpf_prog *prog)
696 {
697         /* Nothing to do here. We support Internal BPF. */
698 }
699
700 void bpf_int_jit_compile(struct bpf_prog *prog)
701 {
702         struct bpf_binary_header *header;
703         struct jit_ctx ctx;
704         int image_size;
705         u8 *image_ptr;
706
707         if (!bpf_jit_enable)
708                 return;
709
710         if (!prog || !prog->len)
711                 return;
712
713         memset(&ctx, 0, sizeof(ctx));
714         ctx.prog = prog;
715
716         ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
717         if (ctx.offset == NULL)
718                 return;
719
720         /* 1. Initial fake pass to compute ctx->idx. */
721
722         /* Fake pass to fill in ctx->offset and ctx->tmp_used. */
723         if (build_body(&ctx))
724                 goto out;
725
726         build_prologue(&ctx);
727
728         ctx.epilogue_offset = ctx.idx;
729         build_epilogue(&ctx);
730
731         /* Now we know the actual image size. */
732         image_size = sizeof(u32) * ctx.idx;
733         header = bpf_jit_binary_alloc(image_size, &image_ptr,
734                                       sizeof(u32), jit_fill_hole);
735         if (header == NULL)
736                 goto out;
737
738         /* 2. Now, the actual pass. */
739
740         ctx.image = (u32 *)image_ptr;
741         ctx.idx = 0;
742
743         build_prologue(&ctx);
744
745         if (build_body(&ctx)) {
746                 bpf_jit_binary_free(header);
747                 goto out;
748         }
749
750         build_epilogue(&ctx);
751
752         /* And we're done. */
753         if (bpf_jit_enable > 1)
754                 bpf_jit_dump(prog->len, image_size, 2, ctx.image);
755
756         bpf_flush_icache(ctx.image, ctx.image + ctx.idx);
757
758         set_memory_ro((unsigned long)header, header->pages);
759         prog->bpf_func = (void *)ctx.image;
760         prog->jited = true;
761 out:
762         kfree(ctx.offset);
763 }
764
765 void bpf_jit_free(struct bpf_prog *prog)
766 {
767         unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK;
768         struct bpf_binary_header *header = (void *)addr;
769
770         if (!prog->jited)
771                 goto free_filter;
772
773         set_memory_rw(addr, header->pages);
774         bpf_jit_binary_free(header);
775
776 free_filter:
777         bpf_prog_unlock_free(prog);
778 }