1 // SPDX-License-Identifier: GPL-2.0-only
3 * BPF JIT compiler for ARM64
5 * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
8 #define pr_fmt(fmt) "bpf_jit: " fmt
10 #include <linux/bpf.h>
11 #include <linux/filter.h>
12 #include <linux/printk.h>
13 #include <linux/slab.h>
15 #include <asm/byteorder.h>
16 #include <asm/cacheflush.h>
17 #include <asm/debug-monitors.h>
18 #include <asm/set_memory.h>
22 #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
23 #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
24 #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
25 #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
27 /* Map BPF registers to A64 registers */
28 static const int bpf2a64
[] = {
29 /* return value from in-kernel function, and exit value from eBPF */
30 [BPF_REG_0
] = A64_R(7),
31 /* arguments from eBPF program to in-kernel function */
32 [BPF_REG_1
] = A64_R(0),
33 [BPF_REG_2
] = A64_R(1),
34 [BPF_REG_3
] = A64_R(2),
35 [BPF_REG_4
] = A64_R(3),
36 [BPF_REG_5
] = A64_R(4),
37 /* callee saved registers that in-kernel function will preserve */
38 [BPF_REG_6
] = A64_R(19),
39 [BPF_REG_7
] = A64_R(20),
40 [BPF_REG_8
] = A64_R(21),
41 [BPF_REG_9
] = A64_R(22),
42 /* read-only frame pointer to access stack */
43 [BPF_REG_FP
] = A64_R(25),
44 /* temporary registers for internal BPF JIT */
45 [TMP_REG_1
] = A64_R(10),
46 [TMP_REG_2
] = A64_R(11),
47 [TMP_REG_3
] = A64_R(12),
49 [TCALL_CNT
] = A64_R(26),
50 /* temporary register for blinding constants */
51 [BPF_REG_AX
] = A64_R(9),
55 const struct bpf_prog
*prog
;
63 static inline void emit(const u32 insn
, struct jit_ctx
*ctx
)
65 if (ctx
->image
!= NULL
)
66 ctx
->image
[ctx
->idx
] = cpu_to_le32(insn
);
71 static inline void emit_a64_mov_i(const int is64
, const int reg
,
72 const s32 val
, struct jit_ctx
*ctx
)
75 u16 lo
= val
& 0xffff;
79 emit(A64_MOVN(is64
, reg
, (u16
)~lo
, 0), ctx
);
81 emit(A64_MOVN(is64
, reg
, (u16
)~hi
, 16), ctx
);
83 emit(A64_MOVK(is64
, reg
, lo
, 0), ctx
);
86 emit(A64_MOVZ(is64
, reg
, lo
, 0), ctx
);
88 emit(A64_MOVK(is64
, reg
, hi
, 16), ctx
);
92 static int i64_i16_blocks(const u64 val
, bool inverse
)
94 return (((val
>> 0) & 0xffff) != (inverse
? 0xffff : 0x0000)) +
95 (((val
>> 16) & 0xffff) != (inverse
? 0xffff : 0x0000)) +
96 (((val
>> 32) & 0xffff) != (inverse
? 0xffff : 0x0000)) +
97 (((val
>> 48) & 0xffff) != (inverse
? 0xffff : 0x0000));
100 static inline void emit_a64_mov_i64(const int reg
, const u64 val
,
103 u64 nrm_tmp
= val
, rev_tmp
= ~val
;
107 if (!(nrm_tmp
>> 32))
108 return emit_a64_mov_i(0, reg
, (u32
)val
, ctx
);
110 inverse
= i64_i16_blocks(nrm_tmp
, true) < i64_i16_blocks(nrm_tmp
, false);
111 shift
= max(round_down((inverse
? (fls64(rev_tmp
) - 1) :
112 (fls64(nrm_tmp
) - 1)), 16), 0);
114 emit(A64_MOVN(1, reg
, (rev_tmp
>> shift
) & 0xffff, shift
), ctx
);
116 emit(A64_MOVZ(1, reg
, (nrm_tmp
>> shift
) & 0xffff, shift
), ctx
);
119 if (((nrm_tmp
>> shift
) & 0xffff) != (inverse
? 0xffff : 0x0000))
120 emit(A64_MOVK(1, reg
, (nrm_tmp
>> shift
) & 0xffff, shift
), ctx
);
126 * Kernel addresses in the vmalloc space use at most 48 bits, and the
127 * remaining bits are guaranteed to be 0x1. So we can compose the address
128 * with a fixed length movn/movk/movk sequence.
130 static inline void emit_addr_mov_i64(const int reg
, const u64 val
,
136 emit(A64_MOVN(1, reg
, ~tmp
& 0xffff, shift
), ctx
);
140 emit(A64_MOVK(1, reg
, tmp
& 0xffff, shift
), ctx
);
144 static inline int bpf2a64_offset(int bpf_to
, int bpf_from
,
145 const struct jit_ctx
*ctx
)
147 int to
= ctx
->offset
[bpf_to
];
148 /* -1 to account for the Branch instruction */
149 int from
= ctx
->offset
[bpf_from
] - 1;
154 static void jit_fill_hole(void *area
, unsigned int size
)
157 /* We are guaranteed to have aligned memory. */
158 for (ptr
= area
; size
>= sizeof(u32
); size
-= sizeof(u32
))
159 *ptr
++ = cpu_to_le32(AARCH64_BREAK_FAULT
);
162 static inline int epilogue_offset(const struct jit_ctx
*ctx
)
164 int to
= ctx
->epilogue_offset
;
170 /* Stack must be multiples of 16B */
171 #define STACK_ALIGN(sz) (((sz) + 15) & ~15)
173 /* Tail call offset to jump into */
174 #define PROLOGUE_OFFSET 7
176 static int build_prologue(struct jit_ctx
*ctx
, bool ebpf_from_cbpf
)
178 const struct bpf_prog
*prog
= ctx
->prog
;
179 const u8 r6
= bpf2a64
[BPF_REG_6
];
180 const u8 r7
= bpf2a64
[BPF_REG_7
];
181 const u8 r8
= bpf2a64
[BPF_REG_8
];
182 const u8 r9
= bpf2a64
[BPF_REG_9
];
183 const u8 fp
= bpf2a64
[BPF_REG_FP
];
184 const u8 tcc
= bpf2a64
[TCALL_CNT
];
185 const int idx0
= ctx
->idx
;
189 * BPF prog stack layout
192 * original A64_SP => 0:+-----+ BPF prologue
194 * current A64_FP => -16:+-----+
195 * | ... | callee saved registers
196 * BPF fp register => -64:+-----+ <= (BPF_FP)
198 * | ... | BPF prog stack
200 * +-----+ <= (BPF_FP - prog->aux->stack_depth)
202 * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
204 * | ... | Function call stack
211 /* Save FP and LR registers to stay align with ARM64 AAPCS */
212 emit(A64_PUSH(A64_FP
, A64_LR
, A64_SP
), ctx
);
213 emit(A64_MOV(1, A64_FP
, A64_SP
), ctx
);
215 /* Save callee-saved registers */
216 emit(A64_PUSH(r6
, r7
, A64_SP
), ctx
);
217 emit(A64_PUSH(r8
, r9
, A64_SP
), ctx
);
218 emit(A64_PUSH(fp
, tcc
, A64_SP
), ctx
);
220 /* Set up BPF prog stack base register */
221 emit(A64_MOV(1, fp
, A64_SP
), ctx
);
223 if (!ebpf_from_cbpf
) {
224 /* Initialize tail_call_cnt */
225 emit(A64_MOVZ(1, tcc
, 0, 0), ctx
);
227 cur_offset
= ctx
->idx
- idx0
;
228 if (cur_offset
!= PROLOGUE_OFFSET
) {
229 pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
230 cur_offset
, PROLOGUE_OFFSET
);
235 ctx
->stack_size
= STACK_ALIGN(prog
->aux
->stack_depth
);
237 /* Set up function call stack */
238 emit(A64_SUB_I(1, A64_SP
, A64_SP
, ctx
->stack_size
), ctx
);
242 static int out_offset
= -1; /* initialized on the first pass of build_body() */
243 static int emit_bpf_tail_call(struct jit_ctx
*ctx
)
245 /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
246 const u8 r2
= bpf2a64
[BPF_REG_2
];
247 const u8 r3
= bpf2a64
[BPF_REG_3
];
249 const u8 tmp
= bpf2a64
[TMP_REG_1
];
250 const u8 prg
= bpf2a64
[TMP_REG_2
];
251 const u8 tcc
= bpf2a64
[TCALL_CNT
];
252 const int idx0
= ctx
->idx
;
253 #define cur_offset (ctx->idx - idx0)
254 #define jmp_offset (out_offset - (cur_offset))
257 /* if (index >= array->map.max_entries)
260 off
= offsetof(struct bpf_array
, map
.max_entries
);
261 emit_a64_mov_i64(tmp
, off
, ctx
);
262 emit(A64_LDR32(tmp
, r2
, tmp
), ctx
);
263 emit(A64_MOV(0, r3
, r3
), ctx
);
264 emit(A64_CMP(0, r3
, tmp
), ctx
);
265 emit(A64_B_(A64_COND_CS
, jmp_offset
), ctx
);
267 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
271 emit_a64_mov_i64(tmp
, MAX_TAIL_CALL_CNT
, ctx
);
272 emit(A64_CMP(1, tcc
, tmp
), ctx
);
273 emit(A64_B_(A64_COND_HI
, jmp_offset
), ctx
);
274 emit(A64_ADD_I(1, tcc
, tcc
, 1), ctx
);
276 /* prog = array->ptrs[index];
280 off
= offsetof(struct bpf_array
, ptrs
);
281 emit_a64_mov_i64(tmp
, off
, ctx
);
282 emit(A64_ADD(1, tmp
, r2
, tmp
), ctx
);
283 emit(A64_LSL(1, prg
, r3
, 3), ctx
);
284 emit(A64_LDR64(prg
, tmp
, prg
), ctx
);
285 emit(A64_CBZ(1, prg
, jmp_offset
), ctx
);
287 /* goto *(prog->bpf_func + prologue_offset); */
288 off
= offsetof(struct bpf_prog
, bpf_func
);
289 emit_a64_mov_i64(tmp
, off
, ctx
);
290 emit(A64_LDR64(tmp
, prg
, tmp
), ctx
);
291 emit(A64_ADD_I(1, tmp
, tmp
, sizeof(u32
) * PROLOGUE_OFFSET
), ctx
);
292 emit(A64_ADD_I(1, A64_SP
, A64_SP
, ctx
->stack_size
), ctx
);
293 emit(A64_BR(tmp
), ctx
);
296 if (out_offset
== -1)
297 out_offset
= cur_offset
;
298 if (cur_offset
!= out_offset
) {
299 pr_err_once("tail_call out_offset = %d, expected %d!\n",
300 cur_offset
, out_offset
);
308 static void build_epilogue(struct jit_ctx
*ctx
)
310 const u8 r0
= bpf2a64
[BPF_REG_0
];
311 const u8 r6
= bpf2a64
[BPF_REG_6
];
312 const u8 r7
= bpf2a64
[BPF_REG_7
];
313 const u8 r8
= bpf2a64
[BPF_REG_8
];
314 const u8 r9
= bpf2a64
[BPF_REG_9
];
315 const u8 fp
= bpf2a64
[BPF_REG_FP
];
317 /* We're done with BPF stack */
318 emit(A64_ADD_I(1, A64_SP
, A64_SP
, ctx
->stack_size
), ctx
);
320 /* Restore fs (x25) and x26 */
321 emit(A64_POP(fp
, A64_R(26), A64_SP
), ctx
);
323 /* Restore callee-saved register */
324 emit(A64_POP(r8
, r9
, A64_SP
), ctx
);
325 emit(A64_POP(r6
, r7
, A64_SP
), ctx
);
327 /* Restore FP/LR registers */
328 emit(A64_POP(A64_FP
, A64_LR
, A64_SP
), ctx
);
330 /* Set return value */
331 emit(A64_MOV(1, A64_R(0), r0
), ctx
);
333 emit(A64_RET(A64_LR
), ctx
);
336 /* JITs an eBPF instruction.
338 * 0 - successfully JITed an 8-byte eBPF instruction.
339 * >0 - successfully JITed a 16-byte eBPF instruction.
340 * <0 - failed to JIT.
342 static int build_insn(const struct bpf_insn
*insn
, struct jit_ctx
*ctx
,
345 const u8 code
= insn
->code
;
346 const u8 dst
= bpf2a64
[insn
->dst_reg
];
347 const u8 src
= bpf2a64
[insn
->src_reg
];
348 const u8 tmp
= bpf2a64
[TMP_REG_1
];
349 const u8 tmp2
= bpf2a64
[TMP_REG_2
];
350 const u8 tmp3
= bpf2a64
[TMP_REG_3
];
351 const s16 off
= insn
->off
;
352 const s32 imm
= insn
->imm
;
353 const int i
= insn
- ctx
->prog
->insnsi
;
354 const bool is64
= BPF_CLASS(code
) == BPF_ALU64
||
355 BPF_CLASS(code
) == BPF_JMP
;
356 const bool isdw
= BPF_SIZE(code
) == BPF_DW
;
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", \
368 #define check_imm19(imm) check_imm(19, imm)
369 #define check_imm26(imm) check_imm(26, imm)
373 case BPF_ALU
| BPF_MOV
| BPF_X
:
374 case BPF_ALU64
| BPF_MOV
| BPF_X
:
375 emit(A64_MOV(is64
, dst
, src
), ctx
);
377 /* dst = dst OP src */
378 case BPF_ALU
| BPF_ADD
| BPF_X
:
379 case BPF_ALU64
| BPF_ADD
| BPF_X
:
380 emit(A64_ADD(is64
, dst
, dst
, src
), ctx
);
382 case BPF_ALU
| BPF_SUB
| BPF_X
:
383 case BPF_ALU64
| BPF_SUB
| BPF_X
:
384 emit(A64_SUB(is64
, dst
, dst
, src
), ctx
);
386 case BPF_ALU
| BPF_AND
| BPF_X
:
387 case BPF_ALU64
| BPF_AND
| BPF_X
:
388 emit(A64_AND(is64
, dst
, dst
, src
), ctx
);
390 case BPF_ALU
| BPF_OR
| BPF_X
:
391 case BPF_ALU64
| BPF_OR
| BPF_X
:
392 emit(A64_ORR(is64
, dst
, dst
, src
), ctx
);
394 case BPF_ALU
| BPF_XOR
| BPF_X
:
395 case BPF_ALU64
| BPF_XOR
| BPF_X
:
396 emit(A64_EOR(is64
, dst
, dst
, src
), ctx
);
398 case BPF_ALU
| BPF_MUL
| BPF_X
:
399 case BPF_ALU64
| BPF_MUL
| BPF_X
:
400 emit(A64_MUL(is64
, dst
, dst
, src
), ctx
);
402 case BPF_ALU
| BPF_DIV
| BPF_X
:
403 case BPF_ALU64
| BPF_DIV
| BPF_X
:
404 case BPF_ALU
| BPF_MOD
| BPF_X
:
405 case BPF_ALU64
| BPF_MOD
| BPF_X
:
406 switch (BPF_OP(code
)) {
408 emit(A64_UDIV(is64
, dst
, dst
, src
), ctx
);
411 emit(A64_UDIV(is64
, tmp
, dst
, src
), ctx
);
412 emit(A64_MSUB(is64
, dst
, dst
, tmp
, src
), ctx
);
416 case BPF_ALU
| BPF_LSH
| BPF_X
:
417 case BPF_ALU64
| BPF_LSH
| BPF_X
:
418 emit(A64_LSLV(is64
, dst
, dst
, src
), ctx
);
420 case BPF_ALU
| BPF_RSH
| BPF_X
:
421 case BPF_ALU64
| BPF_RSH
| BPF_X
:
422 emit(A64_LSRV(is64
, dst
, dst
, src
), ctx
);
424 case BPF_ALU
| BPF_ARSH
| BPF_X
:
425 case BPF_ALU64
| BPF_ARSH
| BPF_X
:
426 emit(A64_ASRV(is64
, dst
, dst
, src
), ctx
);
429 case BPF_ALU
| BPF_NEG
:
430 case BPF_ALU64
| BPF_NEG
:
431 emit(A64_NEG(is64
, dst
, dst
), ctx
);
433 /* dst = BSWAP##imm(dst) */
434 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
435 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
436 #ifdef CONFIG_CPU_BIG_ENDIAN
437 if (BPF_SRC(code
) == BPF_FROM_BE
)
439 #else /* !CONFIG_CPU_BIG_ENDIAN */
440 if (BPF_SRC(code
) == BPF_FROM_LE
)
445 emit(A64_REV16(is64
, dst
, dst
), ctx
);
446 /* zero-extend 16 bits into 64 bits */
447 emit(A64_UXTH(is64
, dst
, dst
), ctx
);
450 emit(A64_REV32(is64
, dst
, dst
), ctx
);
451 /* upper 32 bits already cleared */
454 emit(A64_REV64(dst
, dst
), ctx
);
461 /* zero-extend 16 bits into 64 bits */
462 emit(A64_UXTH(is64
, dst
, dst
), ctx
);
465 /* zero-extend 32 bits into 64 bits */
466 emit(A64_UXTW(is64
, dst
, dst
), ctx
);
474 case BPF_ALU
| BPF_MOV
| BPF_K
:
475 case BPF_ALU64
| BPF_MOV
| BPF_K
:
476 emit_a64_mov_i(is64
, dst
, imm
, ctx
);
478 /* dst = dst OP imm */
479 case BPF_ALU
| BPF_ADD
| BPF_K
:
480 case BPF_ALU64
| BPF_ADD
| BPF_K
:
481 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
482 emit(A64_ADD(is64
, dst
, dst
, tmp
), ctx
);
484 case BPF_ALU
| BPF_SUB
| BPF_K
:
485 case BPF_ALU64
| BPF_SUB
| BPF_K
:
486 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
487 emit(A64_SUB(is64
, dst
, dst
, tmp
), ctx
);
489 case BPF_ALU
| BPF_AND
| BPF_K
:
490 case BPF_ALU64
| BPF_AND
| BPF_K
:
491 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
492 emit(A64_AND(is64
, dst
, dst
, tmp
), ctx
);
494 case BPF_ALU
| BPF_OR
| BPF_K
:
495 case BPF_ALU64
| BPF_OR
| BPF_K
:
496 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
497 emit(A64_ORR(is64
, dst
, dst
, tmp
), ctx
);
499 case BPF_ALU
| BPF_XOR
| BPF_K
:
500 case BPF_ALU64
| BPF_XOR
| BPF_K
:
501 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
502 emit(A64_EOR(is64
, dst
, dst
, tmp
), ctx
);
504 case BPF_ALU
| BPF_MUL
| BPF_K
:
505 case BPF_ALU64
| BPF_MUL
| BPF_K
:
506 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
507 emit(A64_MUL(is64
, dst
, dst
, tmp
), ctx
);
509 case BPF_ALU
| BPF_DIV
| BPF_K
:
510 case BPF_ALU64
| BPF_DIV
| BPF_K
:
511 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
512 emit(A64_UDIV(is64
, dst
, dst
, tmp
), ctx
);
514 case BPF_ALU
| BPF_MOD
| BPF_K
:
515 case BPF_ALU64
| BPF_MOD
| BPF_K
:
516 emit_a64_mov_i(is64
, tmp2
, imm
, ctx
);
517 emit(A64_UDIV(is64
, tmp
, dst
, tmp2
), ctx
);
518 emit(A64_MSUB(is64
, dst
, dst
, tmp
, tmp2
), ctx
);
520 case BPF_ALU
| BPF_LSH
| BPF_K
:
521 case BPF_ALU64
| BPF_LSH
| BPF_K
:
522 emit(A64_LSL(is64
, dst
, dst
, imm
), ctx
);
524 case BPF_ALU
| BPF_RSH
| BPF_K
:
525 case BPF_ALU64
| BPF_RSH
| BPF_K
:
526 emit(A64_LSR(is64
, dst
, dst
, imm
), ctx
);
528 case BPF_ALU
| BPF_ARSH
| BPF_K
:
529 case BPF_ALU64
| BPF_ARSH
| BPF_K
:
530 emit(A64_ASR(is64
, dst
, dst
, imm
), ctx
);
534 case BPF_JMP
| BPF_JA
:
535 jmp_offset
= bpf2a64_offset(i
+ off
, i
, ctx
);
536 check_imm26(jmp_offset
);
537 emit(A64_B(jmp_offset
), ctx
);
539 /* IF (dst COND src) JUMP off */
540 case BPF_JMP
| BPF_JEQ
| BPF_X
:
541 case BPF_JMP
| BPF_JGT
| BPF_X
:
542 case BPF_JMP
| BPF_JLT
| BPF_X
:
543 case BPF_JMP
| BPF_JGE
| BPF_X
:
544 case BPF_JMP
| BPF_JLE
| BPF_X
:
545 case BPF_JMP
| BPF_JNE
| BPF_X
:
546 case BPF_JMP
| BPF_JSGT
| BPF_X
:
547 case BPF_JMP
| BPF_JSLT
| BPF_X
:
548 case BPF_JMP
| BPF_JSGE
| BPF_X
:
549 case BPF_JMP
| BPF_JSLE
| BPF_X
:
550 case BPF_JMP32
| BPF_JEQ
| BPF_X
:
551 case BPF_JMP32
| BPF_JGT
| BPF_X
:
552 case BPF_JMP32
| BPF_JLT
| BPF_X
:
553 case BPF_JMP32
| BPF_JGE
| BPF_X
:
554 case BPF_JMP32
| BPF_JLE
| BPF_X
:
555 case BPF_JMP32
| BPF_JNE
| BPF_X
:
556 case BPF_JMP32
| BPF_JSGT
| BPF_X
:
557 case BPF_JMP32
| BPF_JSLT
| BPF_X
:
558 case BPF_JMP32
| BPF_JSGE
| BPF_X
:
559 case BPF_JMP32
| BPF_JSLE
| BPF_X
:
560 emit(A64_CMP(is64
, dst
, src
), ctx
);
562 jmp_offset
= bpf2a64_offset(i
+ off
, i
, ctx
);
563 check_imm19(jmp_offset
);
564 switch (BPF_OP(code
)) {
566 jmp_cond
= A64_COND_EQ
;
569 jmp_cond
= A64_COND_HI
;
572 jmp_cond
= A64_COND_CC
;
575 jmp_cond
= A64_COND_CS
;
578 jmp_cond
= A64_COND_LS
;
582 jmp_cond
= A64_COND_NE
;
585 jmp_cond
= A64_COND_GT
;
588 jmp_cond
= A64_COND_LT
;
591 jmp_cond
= A64_COND_GE
;
594 jmp_cond
= A64_COND_LE
;
599 emit(A64_B_(jmp_cond
, jmp_offset
), ctx
);
601 case BPF_JMP
| BPF_JSET
| BPF_X
:
602 case BPF_JMP32
| BPF_JSET
| BPF_X
:
603 emit(A64_TST(is64
, dst
, src
), ctx
);
605 /* IF (dst COND imm) JUMP off */
606 case BPF_JMP
| BPF_JEQ
| BPF_K
:
607 case BPF_JMP
| BPF_JGT
| BPF_K
:
608 case BPF_JMP
| BPF_JLT
| BPF_K
:
609 case BPF_JMP
| BPF_JGE
| BPF_K
:
610 case BPF_JMP
| BPF_JLE
| BPF_K
:
611 case BPF_JMP
| BPF_JNE
| BPF_K
:
612 case BPF_JMP
| BPF_JSGT
| BPF_K
:
613 case BPF_JMP
| BPF_JSLT
| BPF_K
:
614 case BPF_JMP
| BPF_JSGE
| BPF_K
:
615 case BPF_JMP
| BPF_JSLE
| BPF_K
:
616 case BPF_JMP32
| BPF_JEQ
| BPF_K
:
617 case BPF_JMP32
| BPF_JGT
| BPF_K
:
618 case BPF_JMP32
| BPF_JLT
| BPF_K
:
619 case BPF_JMP32
| BPF_JGE
| BPF_K
:
620 case BPF_JMP32
| BPF_JLE
| BPF_K
:
621 case BPF_JMP32
| BPF_JNE
| BPF_K
:
622 case BPF_JMP32
| BPF_JSGT
| BPF_K
:
623 case BPF_JMP32
| BPF_JSLT
| BPF_K
:
624 case BPF_JMP32
| BPF_JSGE
| BPF_K
:
625 case BPF_JMP32
| BPF_JSLE
| BPF_K
:
626 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
627 emit(A64_CMP(is64
, dst
, tmp
), ctx
);
629 case BPF_JMP
| BPF_JSET
| BPF_K
:
630 case BPF_JMP32
| BPF_JSET
| BPF_K
:
631 emit_a64_mov_i(is64
, tmp
, imm
, ctx
);
632 emit(A64_TST(is64
, dst
, tmp
), ctx
);
635 case BPF_JMP
| BPF_CALL
:
637 const u8 r0
= bpf2a64
[BPF_REG_0
];
638 bool func_addr_fixed
;
642 ret
= bpf_jit_get_func_addr(ctx
->prog
, insn
, extra_pass
,
643 &func_addr
, &func_addr_fixed
);
646 emit_addr_mov_i64(tmp
, func_addr
, ctx
);
647 emit(A64_BLR(tmp
), ctx
);
648 emit(A64_MOV(1, r0
, A64_R(0)), ctx
);
652 case BPF_JMP
| BPF_TAIL_CALL
:
653 if (emit_bpf_tail_call(ctx
))
656 /* function return */
657 case BPF_JMP
| BPF_EXIT
:
658 /* Optimization: when last instruction is EXIT,
659 simply fallthrough to epilogue. */
660 if (i
== ctx
->prog
->len
- 1)
662 jmp_offset
= epilogue_offset(ctx
);
663 check_imm26(jmp_offset
);
664 emit(A64_B(jmp_offset
), ctx
);
668 case BPF_LD
| BPF_IMM
| BPF_DW
:
670 const struct bpf_insn insn1
= insn
[1];
673 imm64
= (u64
)insn1
.imm
<< 32 | (u32
)imm
;
674 emit_a64_mov_i64(dst
, imm64
, ctx
);
679 /* LDX: dst = *(size *)(src + off) */
680 case BPF_LDX
| BPF_MEM
| BPF_W
:
681 case BPF_LDX
| BPF_MEM
| BPF_H
:
682 case BPF_LDX
| BPF_MEM
| BPF_B
:
683 case BPF_LDX
| BPF_MEM
| BPF_DW
:
684 emit_a64_mov_i(1, tmp
, off
, ctx
);
685 switch (BPF_SIZE(code
)) {
687 emit(A64_LDR32(dst
, src
, tmp
), ctx
);
690 emit(A64_LDRH(dst
, src
, tmp
), ctx
);
693 emit(A64_LDRB(dst
, src
, tmp
), ctx
);
696 emit(A64_LDR64(dst
, src
, tmp
), ctx
);
701 /* ST: *(size *)(dst + off) = imm */
702 case BPF_ST
| BPF_MEM
| BPF_W
:
703 case BPF_ST
| BPF_MEM
| BPF_H
:
704 case BPF_ST
| BPF_MEM
| BPF_B
:
705 case BPF_ST
| BPF_MEM
| BPF_DW
:
706 /* Load imm to a register then store it */
707 emit_a64_mov_i(1, tmp2
, off
, ctx
);
708 emit_a64_mov_i(1, tmp
, imm
, ctx
);
709 switch (BPF_SIZE(code
)) {
711 emit(A64_STR32(tmp
, dst
, tmp2
), ctx
);
714 emit(A64_STRH(tmp
, dst
, tmp2
), ctx
);
717 emit(A64_STRB(tmp
, dst
, tmp2
), ctx
);
720 emit(A64_STR64(tmp
, dst
, tmp2
), ctx
);
725 /* STX: *(size *)(dst + off) = src */
726 case BPF_STX
| BPF_MEM
| BPF_W
:
727 case BPF_STX
| BPF_MEM
| BPF_H
:
728 case BPF_STX
| BPF_MEM
| BPF_B
:
729 case BPF_STX
| BPF_MEM
| BPF_DW
:
730 emit_a64_mov_i(1, tmp
, off
, ctx
);
731 switch (BPF_SIZE(code
)) {
733 emit(A64_STR32(src
, dst
, tmp
), ctx
);
736 emit(A64_STRH(src
, dst
, tmp
), ctx
);
739 emit(A64_STRB(src
, dst
, tmp
), ctx
);
742 emit(A64_STR64(src
, dst
, tmp
), ctx
);
747 /* STX XADD: lock *(u32 *)(dst + off) += src */
748 case BPF_STX
| BPF_XADD
| BPF_W
:
749 /* STX XADD: lock *(u64 *)(dst + off) += src */
750 case BPF_STX
| BPF_XADD
| BPF_DW
:
754 emit_a64_mov_i(1, tmp
, off
, ctx
);
755 emit(A64_ADD(1, tmp
, tmp
, dst
), ctx
);
758 if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS
)) {
759 emit(A64_STADD(isdw
, reg
, src
), ctx
);
761 emit(A64_LDXR(isdw
, tmp2
, reg
), ctx
);
762 emit(A64_ADD(isdw
, tmp2
, tmp2
, src
), ctx
);
763 emit(A64_STXR(isdw
, tmp2
, reg
, tmp3
), ctx
);
765 check_imm19(jmp_offset
);
766 emit(A64_CBNZ(0, tmp3
, jmp_offset
), ctx
);
771 pr_err_once("unknown opcode %02x\n", code
);
778 static int build_body(struct jit_ctx
*ctx
, bool extra_pass
)
780 const struct bpf_prog
*prog
= ctx
->prog
;
783 for (i
= 0; i
< prog
->len
; i
++) {
784 const struct bpf_insn
*insn
= &prog
->insnsi
[i
];
787 ret
= build_insn(insn
, ctx
, extra_pass
);
790 if (ctx
->image
== NULL
)
791 ctx
->offset
[i
] = ctx
->idx
;
794 if (ctx
->image
== NULL
)
795 ctx
->offset
[i
] = ctx
->idx
;
803 static int validate_code(struct jit_ctx
*ctx
)
807 for (i
= 0; i
< ctx
->idx
; i
++) {
808 u32 a64_insn
= le32_to_cpu(ctx
->image
[i
]);
810 if (a64_insn
== AARCH64_BREAK_FAULT
)
817 static inline void bpf_flush_icache(void *start
, void *end
)
819 flush_icache_range((unsigned long)start
, (unsigned long)end
);
822 struct arm64_jit_data
{
823 struct bpf_binary_header
*header
;
828 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*prog
)
830 struct bpf_prog
*tmp
, *orig_prog
= prog
;
831 struct bpf_binary_header
*header
;
832 struct arm64_jit_data
*jit_data
;
833 bool was_classic
= bpf_prog_was_classic(prog
);
834 bool tmp_blinded
= false;
835 bool extra_pass
= false;
840 if (!prog
->jit_requested
)
843 tmp
= bpf_jit_blind_constants(prog
);
844 /* If blinding was requested and we failed during blinding,
845 * we must fall back to the interpreter.
854 jit_data
= prog
->aux
->jit_data
;
856 jit_data
= kzalloc(sizeof(*jit_data
), GFP_KERNEL
);
861 prog
->aux
->jit_data
= jit_data
;
863 if (jit_data
->ctx
.offset
) {
865 image_ptr
= jit_data
->image
;
866 header
= jit_data
->header
;
868 image_size
= sizeof(u32
) * ctx
.idx
;
871 memset(&ctx
, 0, sizeof(ctx
));
874 ctx
.offset
= kcalloc(prog
->len
, sizeof(int), GFP_KERNEL
);
875 if (ctx
.offset
== NULL
) {
880 /* 1. Initial fake pass to compute ctx->idx. */
882 /* Fake pass to fill in ctx->offset. */
883 if (build_body(&ctx
, extra_pass
)) {
888 if (build_prologue(&ctx
, was_classic
)) {
893 ctx
.epilogue_offset
= ctx
.idx
;
894 build_epilogue(&ctx
);
896 /* Now we know the actual image size. */
897 image_size
= sizeof(u32
) * ctx
.idx
;
898 header
= bpf_jit_binary_alloc(image_size
, &image_ptr
,
899 sizeof(u32
), jit_fill_hole
);
900 if (header
== NULL
) {
905 /* 2. Now, the actual pass. */
907 ctx
.image
= (__le32
*)image_ptr
;
911 build_prologue(&ctx
, was_classic
);
913 if (build_body(&ctx
, extra_pass
)) {
914 bpf_jit_binary_free(header
);
919 build_epilogue(&ctx
);
921 /* 3. Extra pass to validate JITed code. */
922 if (validate_code(&ctx
)) {
923 bpf_jit_binary_free(header
);
928 /* And we're done. */
929 if (bpf_jit_enable
> 1)
930 bpf_jit_dump(prog
->len
, image_size
, 2, ctx
.image
);
932 bpf_flush_icache(header
, ctx
.image
+ ctx
.idx
);
934 if (!prog
->is_func
|| extra_pass
) {
935 if (extra_pass
&& ctx
.idx
!= jit_data
->ctx
.idx
) {
936 pr_err_once("multi-func JIT bug %d != %d\n",
937 ctx
.idx
, jit_data
->ctx
.idx
);
938 bpf_jit_binary_free(header
);
939 prog
->bpf_func
= NULL
;
943 bpf_jit_binary_lock_ro(header
);
946 jit_data
->image
= image_ptr
;
947 jit_data
->header
= header
;
949 prog
->bpf_func
= (void *)ctx
.image
;
951 prog
->jited_len
= image_size
;
953 if (!prog
->is_func
|| extra_pass
) {
954 bpf_prog_fill_jited_linfo(prog
, ctx
.offset
);
958 prog
->aux
->jit_data
= NULL
;
962 bpf_jit_prog_release_other(prog
, prog
== orig_prog
?
967 void *bpf_jit_alloc_exec(unsigned long size
)
969 return __vmalloc_node_range(size
, PAGE_SIZE
, BPF_JIT_REGION_START
,
970 BPF_JIT_REGION_END
, GFP_KERNEL
,
971 PAGE_KERNEL
, 0, NUMA_NO_NODE
,
972 __builtin_return_address(0));
975 void bpf_jit_free_exec(void *addr
)