blob: 71088952ed270eacd1249273fe1798c02993b332 [file] [log] [blame]
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001/*
2 * BPF JIT compiler for ARM64
3 *
4 * Copyright (C) 2014 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>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070022#include <linux/printk.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
Daniel Borkmannb569c1c2014-09-16 08:48:50 +010025
Zi Shen Lime54bcde2014-08-26 21:15:30 -070026#include <asm/byteorder.h>
27#include <asm/cacheflush.h>
Daniel Borkmannb569c1c2014-09-16 08:48:50 +010028#include <asm/debug-monitors.h>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070029
30#include "bpf_jit.h"
31
32int 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 */
38static 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
59struct jit_ctx {
60 const struct bpf_prog *prog;
61 int idx;
62 int tmp_used;
63 int body_offset;
64 int *offset;
65 u32 *image;
66};
67
68static 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
76static 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
93static 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
113static inline int bpf2a64_offset(int bpf_to, int bpf_from,
114 const struct jit_ctx *ctx)
115{
116 int to = ctx->offset[bpf_to + 1];
117 /* -1 to account for the Branch instruction */
118 int from = ctx->offset[bpf_from + 1] - 1;
119
120 return to - from;
121}
122
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100123static 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
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700131static inline int epilogue_offset(const struct jit_ctx *ctx)
132{
133 int to = ctx->offset[ctx->prog->len - 1];
134 int from = ctx->idx - ctx->body_offset;
135
136 return to - from;
137}
138
139/* Stack must be multiples of 16B */
140#define STACK_ALIGN(sz) (((sz) + 15) & ~15)
141
142static 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
175static 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
208static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
209{
210 const u8 code = insn->code;
211 const u8 dst = bpf2a64[insn->dst_reg];
212 const u8 src = bpf2a64[insn->src_reg];
213 const u8 tmp = bpf2a64[TMP_REG_1];
214 const u8 tmp2 = bpf2a64[TMP_REG_2];
215 const s16 off = insn->off;
216 const s32 imm = insn->imm;
217 const int i = insn - ctx->prog->insnsi;
218 const bool is64 = BPF_CLASS(code) == BPF_ALU64;
219 u8 jmp_cond;
220 s32 jmp_offset;
221
222 switch (code) {
223 /* dst = src */
224 case BPF_ALU | BPF_MOV | BPF_X:
225 case BPF_ALU64 | BPF_MOV | BPF_X:
226 emit(A64_MOV(is64, dst, src), ctx);
227 break;
228 /* dst = dst OP src */
229 case BPF_ALU | BPF_ADD | BPF_X:
230 case BPF_ALU64 | BPF_ADD | BPF_X:
231 emit(A64_ADD(is64, dst, dst, src), ctx);
232 break;
233 case BPF_ALU | BPF_SUB | BPF_X:
234 case BPF_ALU64 | BPF_SUB | BPF_X:
235 emit(A64_SUB(is64, dst, dst, src), ctx);
236 break;
237 case BPF_ALU | BPF_AND | BPF_X:
238 case BPF_ALU64 | BPF_AND | BPF_X:
239 emit(A64_AND(is64, dst, dst, src), ctx);
240 break;
241 case BPF_ALU | BPF_OR | BPF_X:
242 case BPF_ALU64 | BPF_OR | BPF_X:
243 emit(A64_ORR(is64, dst, dst, src), ctx);
244 break;
245 case BPF_ALU | BPF_XOR | BPF_X:
246 case BPF_ALU64 | BPF_XOR | BPF_X:
247 emit(A64_EOR(is64, dst, dst, src), ctx);
248 break;
249 case BPF_ALU | BPF_MUL | BPF_X:
250 case BPF_ALU64 | BPF_MUL | BPF_X:
251 emit(A64_MUL(is64, dst, dst, src), ctx);
252 break;
253 case BPF_ALU | BPF_DIV | BPF_X:
254 case BPF_ALU64 | BPF_DIV | BPF_X:
255 emit(A64_UDIV(is64, dst, dst, src), ctx);
256 break;
257 case BPF_ALU | BPF_MOD | BPF_X:
258 case BPF_ALU64 | BPF_MOD | BPF_X:
259 ctx->tmp_used = 1;
260 emit(A64_UDIV(is64, tmp, dst, src), ctx);
261 emit(A64_MUL(is64, tmp, tmp, src), ctx);
262 emit(A64_SUB(is64, dst, dst, tmp), ctx);
263 break;
264 /* dst = -dst */
265 case BPF_ALU | BPF_NEG:
266 case BPF_ALU64 | BPF_NEG:
267 emit(A64_NEG(is64, dst, dst), ctx);
268 break;
269 /* dst = BSWAP##imm(dst) */
270 case BPF_ALU | BPF_END | BPF_FROM_LE:
271 case BPF_ALU | BPF_END | BPF_FROM_BE:
272#ifdef CONFIG_CPU_BIG_ENDIAN
273 if (BPF_SRC(code) == BPF_FROM_BE)
274 break;
275#else /* !CONFIG_CPU_BIG_ENDIAN */
276 if (BPF_SRC(code) == BPF_FROM_LE)
277 break;
278#endif
279 switch (imm) {
280 case 16:
281 emit(A64_REV16(is64, dst, dst), ctx);
282 break;
283 case 32:
284 emit(A64_REV32(is64, dst, dst), ctx);
285 break;
286 case 64:
287 emit(A64_REV64(dst, dst), ctx);
288 break;
289 }
290 break;
291 /* dst = imm */
292 case BPF_ALU | BPF_MOV | BPF_K:
293 case BPF_ALU64 | BPF_MOV | BPF_K:
294 emit_a64_mov_i(is64, dst, imm, ctx);
295 break;
296 /* dst = dst OP imm */
297 case BPF_ALU | BPF_ADD | BPF_K:
298 case BPF_ALU64 | BPF_ADD | BPF_K:
299 ctx->tmp_used = 1;
300 emit_a64_mov_i(is64, tmp, imm, ctx);
301 emit(A64_ADD(is64, dst, dst, tmp), ctx);
302 break;
303 case BPF_ALU | BPF_SUB | BPF_K:
304 case BPF_ALU64 | BPF_SUB | BPF_K:
305 ctx->tmp_used = 1;
306 emit_a64_mov_i(is64, tmp, imm, ctx);
307 emit(A64_SUB(is64, dst, dst, tmp), ctx);
308 break;
309 case BPF_ALU | BPF_AND | BPF_K:
310 case BPF_ALU64 | BPF_AND | BPF_K:
311 ctx->tmp_used = 1;
312 emit_a64_mov_i(is64, tmp, imm, ctx);
313 emit(A64_AND(is64, dst, dst, tmp), ctx);
314 break;
315 case BPF_ALU | BPF_OR | BPF_K:
316 case BPF_ALU64 | BPF_OR | BPF_K:
317 ctx->tmp_used = 1;
318 emit_a64_mov_i(is64, tmp, imm, ctx);
319 emit(A64_ORR(is64, dst, dst, tmp), ctx);
320 break;
321 case BPF_ALU | BPF_XOR | BPF_K:
322 case BPF_ALU64 | BPF_XOR | BPF_K:
323 ctx->tmp_used = 1;
324 emit_a64_mov_i(is64, tmp, imm, ctx);
325 emit(A64_EOR(is64, dst, dst, tmp), ctx);
326 break;
327 case BPF_ALU | BPF_MUL | BPF_K:
328 case BPF_ALU64 | BPF_MUL | BPF_K:
329 ctx->tmp_used = 1;
330 emit_a64_mov_i(is64, tmp, imm, ctx);
331 emit(A64_MUL(is64, dst, dst, tmp), ctx);
332 break;
333 case BPF_ALU | BPF_DIV | BPF_K:
334 case BPF_ALU64 | BPF_DIV | BPF_K:
335 ctx->tmp_used = 1;
336 emit_a64_mov_i(is64, tmp, imm, ctx);
337 emit(A64_UDIV(is64, dst, dst, tmp), ctx);
338 break;
339 case BPF_ALU | BPF_MOD | BPF_K:
340 case BPF_ALU64 | BPF_MOD | BPF_K:
341 ctx->tmp_used = 1;
342 emit_a64_mov_i(is64, tmp2, imm, ctx);
343 emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
344 emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
345 emit(A64_SUB(is64, dst, dst, tmp), ctx);
346 break;
347 case BPF_ALU | BPF_LSH | BPF_K:
348 case BPF_ALU64 | BPF_LSH | BPF_K:
349 emit(A64_LSL(is64, dst, dst, imm), ctx);
350 break;
351 case BPF_ALU | BPF_RSH | BPF_K:
352 case BPF_ALU64 | BPF_RSH | BPF_K:
353 emit(A64_LSR(is64, dst, dst, imm), ctx);
354 break;
355 case BPF_ALU | BPF_ARSH | BPF_K:
356 case BPF_ALU64 | BPF_ARSH | BPF_K:
357 emit(A64_ASR(is64, dst, dst, imm), ctx);
358 break;
359
360#define check_imm(bits, imm) do { \
361 if ((((imm) > 0) && ((imm) >> (bits))) || \
362 (((imm) < 0) && (~(imm) >> (bits)))) { \
363 pr_info("[%2d] imm=%d(0x%x) out of range\n", \
364 i, imm, imm); \
365 return -EINVAL; \
366 } \
367} while (0)
368#define check_imm19(imm) check_imm(19, imm)
369#define check_imm26(imm) check_imm(26, imm)
370
371 /* JUMP off */
372 case BPF_JMP | BPF_JA:
373 jmp_offset = bpf2a64_offset(i + off, i, ctx);
374 check_imm26(jmp_offset);
375 emit(A64_B(jmp_offset), ctx);
376 break;
377 /* IF (dst COND src) JUMP off */
378 case BPF_JMP | BPF_JEQ | BPF_X:
379 case BPF_JMP | BPF_JGT | BPF_X:
380 case BPF_JMP | BPF_JGE | BPF_X:
381 case BPF_JMP | BPF_JNE | BPF_X:
382 case BPF_JMP | BPF_JSGT | BPF_X:
383 case BPF_JMP | BPF_JSGE | BPF_X:
384 emit(A64_CMP(1, dst, src), ctx);
385emit_cond_jmp:
386 jmp_offset = bpf2a64_offset(i + off, i, ctx);
387 check_imm19(jmp_offset);
388 switch (BPF_OP(code)) {
389 case BPF_JEQ:
390 jmp_cond = A64_COND_EQ;
391 break;
392 case BPF_JGT:
393 jmp_cond = A64_COND_HI;
394 break;
395 case BPF_JGE:
396 jmp_cond = A64_COND_CS;
397 break;
398 case BPF_JNE:
399 jmp_cond = A64_COND_NE;
400 break;
401 case BPF_JSGT:
402 jmp_cond = A64_COND_GT;
403 break;
404 case BPF_JSGE:
405 jmp_cond = A64_COND_GE;
406 break;
407 default:
408 return -EFAULT;
409 }
410 emit(A64_B_(jmp_cond, jmp_offset), ctx);
411 break;
412 case BPF_JMP | BPF_JSET | BPF_X:
413 emit(A64_TST(1, dst, src), ctx);
414 goto emit_cond_jmp;
415 /* IF (dst COND imm) JUMP off */
416 case BPF_JMP | BPF_JEQ | BPF_K:
417 case BPF_JMP | BPF_JGT | BPF_K:
418 case BPF_JMP | BPF_JGE | BPF_K:
419 case BPF_JMP | BPF_JNE | BPF_K:
420 case BPF_JMP | BPF_JSGT | BPF_K:
421 case BPF_JMP | BPF_JSGE | BPF_K:
422 ctx->tmp_used = 1;
423 emit_a64_mov_i(1, tmp, imm, ctx);
424 emit(A64_CMP(1, dst, tmp), ctx);
425 goto emit_cond_jmp;
426 case BPF_JMP | BPF_JSET | BPF_K:
427 ctx->tmp_used = 1;
428 emit_a64_mov_i(1, tmp, imm, ctx);
429 emit(A64_TST(1, dst, tmp), ctx);
430 goto emit_cond_jmp;
431 /* function call */
432 case BPF_JMP | BPF_CALL:
433 {
434 const u8 r0 = bpf2a64[BPF_REG_0];
435 const u64 func = (u64)__bpf_call_base + imm;
436
437 ctx->tmp_used = 1;
438 emit_a64_mov_i64(tmp, func, ctx);
439 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
440 emit(A64_MOV(1, A64_FP, A64_SP), ctx);
441 emit(A64_BLR(tmp), ctx);
442 emit(A64_MOV(1, r0, A64_R(0)), ctx);
443 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
444 break;
445 }
446 /* function return */
447 case BPF_JMP | BPF_EXIT:
448 if (i == ctx->prog->len - 1)
449 break;
450 jmp_offset = epilogue_offset(ctx);
451 check_imm26(jmp_offset);
452 emit(A64_B(jmp_offset), ctx);
453 break;
454
455 /* LDX: dst = *(size *)(src + off) */
456 case BPF_LDX | BPF_MEM | BPF_W:
457 case BPF_LDX | BPF_MEM | BPF_H:
458 case BPF_LDX | BPF_MEM | BPF_B:
459 case BPF_LDX | BPF_MEM | BPF_DW:
460 ctx->tmp_used = 1;
461 emit_a64_mov_i(1, tmp, off, ctx);
462 switch (BPF_SIZE(code)) {
463 case BPF_W:
464 emit(A64_LDR32(dst, src, tmp), ctx);
465 break;
466 case BPF_H:
467 emit(A64_LDRH(dst, src, tmp), ctx);
468 break;
469 case BPF_B:
470 emit(A64_LDRB(dst, src, tmp), ctx);
471 break;
472 case BPF_DW:
473 emit(A64_LDR64(dst, src, tmp), ctx);
474 break;
475 }
476 break;
477
478 /* ST: *(size *)(dst + off) = imm */
479 case BPF_ST | BPF_MEM | BPF_W:
480 case BPF_ST | BPF_MEM | BPF_H:
481 case BPF_ST | BPF_MEM | BPF_B:
482 case BPF_ST | BPF_MEM | BPF_DW:
483 goto notyet;
484
485 /* STX: *(size *)(dst + off) = src */
486 case BPF_STX | BPF_MEM | BPF_W:
487 case BPF_STX | BPF_MEM | BPF_H:
488 case BPF_STX | BPF_MEM | BPF_B:
489 case BPF_STX | BPF_MEM | BPF_DW:
490 ctx->tmp_used = 1;
491 emit_a64_mov_i(1, tmp, off, ctx);
492 switch (BPF_SIZE(code)) {
493 case BPF_W:
494 emit(A64_STR32(src, dst, tmp), ctx);
495 break;
496 case BPF_H:
497 emit(A64_STRH(src, dst, tmp), ctx);
498 break;
499 case BPF_B:
500 emit(A64_STRB(src, dst, tmp), ctx);
501 break;
502 case BPF_DW:
503 emit(A64_STR64(src, dst, tmp), ctx);
504 break;
505 }
506 break;
507 /* STX XADD: lock *(u32 *)(dst + off) += src */
508 case BPF_STX | BPF_XADD | BPF_W:
509 /* STX XADD: lock *(u64 *)(dst + off) += src */
510 case BPF_STX | BPF_XADD | BPF_DW:
511 goto notyet;
512
513 /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
514 case BPF_LD | BPF_ABS | BPF_W:
515 case BPF_LD | BPF_ABS | BPF_H:
516 case BPF_LD | BPF_ABS | BPF_B:
517 /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
518 case BPF_LD | BPF_IND | BPF_W:
519 case BPF_LD | BPF_IND | BPF_H:
520 case BPF_LD | BPF_IND | BPF_B:
521 {
522 const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */
523 const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */
524 const u8 fp = bpf2a64[BPF_REG_FP];
525 const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */
526 const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */
527 const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */
528 const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */
529 const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */
530 int size;
531
532 emit(A64_MOV(1, r1, r6), ctx);
533 emit_a64_mov_i(0, r2, imm, ctx);
534 if (BPF_MODE(code) == BPF_IND)
535 emit(A64_ADD(0, r2, r2, src), ctx);
536 switch (BPF_SIZE(code)) {
537 case BPF_W:
538 size = 4;
539 break;
540 case BPF_H:
541 size = 2;
542 break;
543 case BPF_B:
544 size = 1;
545 break;
546 default:
547 return -EINVAL;
548 }
549 emit_a64_mov_i64(r3, size, ctx);
550 emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx);
551 emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
552 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
553 emit(A64_MOV(1, A64_FP, A64_SP), ctx);
554 emit(A64_BLR(r5), ctx);
555 emit(A64_MOV(1, r0, A64_R(0)), ctx);
556 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
557
558 jmp_offset = epilogue_offset(ctx);
559 check_imm19(jmp_offset);
560 emit(A64_CBZ(1, r0, jmp_offset), ctx);
561 emit(A64_MOV(1, r5, r0), ctx);
562 switch (BPF_SIZE(code)) {
563 case BPF_W:
564 emit(A64_LDR32(r0, r5, A64_ZR), ctx);
565#ifndef CONFIG_CPU_BIG_ENDIAN
566 emit(A64_REV32(0, r0, r0), ctx);
567#endif
568 break;
569 case BPF_H:
570 emit(A64_LDRH(r0, r5, A64_ZR), ctx);
571#ifndef CONFIG_CPU_BIG_ENDIAN
572 emit(A64_REV16(0, r0, r0), ctx);
573#endif
574 break;
575 case BPF_B:
576 emit(A64_LDRB(r0, r5, A64_ZR), ctx);
577 break;
578 }
579 break;
580 }
581notyet:
582 pr_info_once("*** NOT YET: opcode %02x ***\n", code);
583 return -EFAULT;
584
585 default:
586 pr_err_once("unknown opcode %02x\n", code);
587 return -EINVAL;
588 }
589
590 return 0;
591}
592
593static int build_body(struct jit_ctx *ctx)
594{
595 const struct bpf_prog *prog = ctx->prog;
596 int i;
597
598 for (i = 0; i < prog->len; i++) {
599 const struct bpf_insn *insn = &prog->insnsi[i];
600 int ret;
601
602 if (ctx->image == NULL)
603 ctx->offset[i] = ctx->idx;
604
605 ret = build_insn(insn, ctx);
606 if (ret)
607 return ret;
608 }
609
610 return 0;
611}
612
613static inline void bpf_flush_icache(void *start, void *end)
614{
615 flush_icache_range((unsigned long)start, (unsigned long)end);
616}
617
618void bpf_jit_compile(struct bpf_prog *prog)
619{
620 /* Nothing to do here. We support Internal BPF. */
621}
622
623void bpf_int_jit_compile(struct bpf_prog *prog)
624{
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100625 struct bpf_binary_header *header;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700626 struct jit_ctx ctx;
627 int image_size;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100628 u8 *image_ptr;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700629
630 if (!bpf_jit_enable)
631 return;
632
633 if (!prog || !prog->len)
634 return;
635
636 memset(&ctx, 0, sizeof(ctx));
637 ctx.prog = prog;
638
639 ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
640 if (ctx.offset == NULL)
641 return;
642
643 /* 1. Initial fake pass to compute ctx->idx. */
644
645 /* Fake pass to fill in ctx->offset. */
646 if (build_body(&ctx))
647 goto out;
648
649 build_prologue(&ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700650 build_epilogue(&ctx);
651
652 /* Now we know the actual image size. */
653 image_size = sizeof(u32) * ctx.idx;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100654 header = bpf_jit_binary_alloc(image_size, &image_ptr,
655 sizeof(u32), jit_fill_hole);
656 if (header == NULL)
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700657 goto out;
658
659 /* 2. Now, the actual pass. */
660
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100661 ctx.image = (u32 *)image_ptr;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700662 ctx.idx = 0;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100663
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700664 build_prologue(&ctx);
665
666 ctx.body_offset = ctx.idx;
Daniel Borkmann60ef0492014-09-11 10:36:48 +0100667 if (build_body(&ctx)) {
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100668 bpf_jit_binary_free(header);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700669 goto out;
Daniel Borkmann60ef0492014-09-11 10:36:48 +0100670 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700671
672 build_epilogue(&ctx);
673
674 /* And we're done. */
675 if (bpf_jit_enable > 1)
676 bpf_jit_dump(prog->len, image_size, 2, ctx.image);
677
678 bpf_flush_icache(ctx.image, ctx.image + ctx.idx);
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100679
680 set_memory_ro((unsigned long)header, header->pages);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700681 prog->bpf_func = (void *)ctx.image;
682 prog->jited = 1;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700683out:
684 kfree(ctx.offset);
685}
686
687void bpf_jit_free(struct bpf_prog *prog)
688{
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100689 unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK;
690 struct bpf_binary_header *header = (void *)addr;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700691
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100692 if (!prog->jited)
693 goto free_filter;
694
695 set_memory_rw(addr, header->pages);
696 bpf_jit_binary_free(header);
697
698free_filter:
699 bpf_prog_unlock_free(prog);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700700}