blob: f7b194878a99a0931ef43250ad5bb1b04498517b [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Zi Shen Lime54bcde2014-08-26 21:15:30 -07002/*
3 * BPF JIT compiler for ARM64
4 *
Zi Shen Lim42ff7122016-01-13 23:33:22 -08005 * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
Zi Shen Lime54bcde2014-08-26 21:15:30 -07006 */
7
8#define pr_fmt(fmt) "bpf_jit: " fmt
9
Jean-Philippe Brucker80083422020-07-28 17:21:26 +020010#include <linux/bitfield.h>
Zi Shen Limddb55992016-06-08 21:18:48 -070011#include <linux/bpf.h>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070012#include <linux/filter.h>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070013#include <linux/printk.h>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070014#include <linux/slab.h>
Daniel Borkmannb569c1c2014-09-16 08:48:50 +010015
Zi Shen Lime54bcde2014-08-26 21:15:30 -070016#include <asm/byteorder.h>
17#include <asm/cacheflush.h>
Daniel Borkmannb569c1c2014-09-16 08:48:50 +010018#include <asm/debug-monitors.h>
Laura Abbottd4bbc302017-05-08 15:58:05 -070019#include <asm/set_memory.h>
Zi Shen Lime54bcde2014-08-26 21:15:30 -070020
21#include "bpf_jit.h"
22
Daniel Borkmann26eb0422016-05-13 19:08:34 +020023#define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
24#define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
Zi Shen Limddb55992016-06-08 21:18:48 -070025#define TCALL_CNT (MAX_BPF_JIT_REG + 2)
Daniel Borkmann7005cad2017-06-07 13:45:37 +020026#define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
Zi Shen Lime54bcde2014-08-26 21:15:30 -070027
28/* Map BPF registers to A64 registers */
29static const int bpf2a64[] = {
30 /* return value from in-kernel function, and exit value from eBPF */
31 [BPF_REG_0] = A64_R(7),
32 /* arguments from eBPF program to in-kernel function */
33 [BPF_REG_1] = A64_R(0),
34 [BPF_REG_2] = A64_R(1),
35 [BPF_REG_3] = A64_R(2),
36 [BPF_REG_4] = A64_R(3),
37 [BPF_REG_5] = A64_R(4),
38 /* callee saved registers that in-kernel function will preserve */
39 [BPF_REG_6] = A64_R(19),
40 [BPF_REG_7] = A64_R(20),
41 [BPF_REG_8] = A64_R(21),
42 [BPF_REG_9] = A64_R(22),
43 /* read-only frame pointer to access stack */
Yang Shiec0738d2015-11-16 14:35:35 -080044 [BPF_REG_FP] = A64_R(25),
Yang Shi4c1cd4f2016-05-16 16:36:26 -070045 /* temporary registers for internal BPF JIT */
46 [TMP_REG_1] = A64_R(10),
47 [TMP_REG_2] = A64_R(11),
Daniel Borkmann7005cad2017-06-07 13:45:37 +020048 [TMP_REG_3] = A64_R(12),
Zi Shen Limddb55992016-06-08 21:18:48 -070049 /* tail_call_cnt */
50 [TCALL_CNT] = A64_R(26),
Daniel Borkmann26eb0422016-05-13 19:08:34 +020051 /* temporary register for blinding constants */
52 [BPF_REG_AX] = A64_R(9),
Zi Shen Lime54bcde2014-08-26 21:15:30 -070053};
54
55struct jit_ctx {
56 const struct bpf_prog *prog;
57 int idx;
Zi Shen Lim51c9fbb12014-12-03 08:38:01 +000058 int epilogue_offset;
Zi Shen Lime54bcde2014-08-26 21:15:30 -070059 int *offset;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +020060 int exentry_idx;
Luc Van Oostenryck425e1ed2017-06-28 16:58:03 +020061 __le32 *image;
Daniel Borkmannf1c9eed2017-06-11 03:55:27 +020062 u32 stack_size;
Zi Shen Lime54bcde2014-08-26 21:15:30 -070063};
64
65static inline void emit(const u32 insn, struct jit_ctx *ctx)
66{
67 if (ctx->image != NULL)
68 ctx->image[ctx->idx] = cpu_to_le32(insn);
69
70 ctx->idx++;
71}
72
Zi Shen Lime54bcde2014-08-26 21:15:30 -070073static inline void emit_a64_mov_i(const int is64, const int reg,
74 const s32 val, struct jit_ctx *ctx)
75{
76 u16 hi = val >> 16;
77 u16 lo = val & 0xffff;
78
79 if (hi & 0x8000) {
80 if (hi == 0xffff) {
81 emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
82 } else {
83 emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
Daniel Borkmann6d2eea62018-05-14 23:22:32 +020084 if (lo != 0xffff)
85 emit(A64_MOVK(is64, reg, lo, 0), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -070086 }
87 } else {
88 emit(A64_MOVZ(is64, reg, lo, 0), ctx);
89 if (hi)
90 emit(A64_MOVK(is64, reg, hi, 16), ctx);
91 }
92}
93
Daniel Borkmann6d2eea62018-05-14 23:22:32 +020094static int i64_i16_blocks(const u64 val, bool inverse)
95{
96 return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
97 (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
98 (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
99 (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
100}
101
102static inline void emit_a64_mov_i64(const int reg, const u64 val,
103 struct jit_ctx *ctx)
104{
105 u64 nrm_tmp = val, rev_tmp = ~val;
106 bool inverse;
107 int shift;
108
109 if (!(nrm_tmp >> 32))
110 return emit_a64_mov_i(0, reg, (u32)val, ctx);
111
112 inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
113 shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
114 (fls64(nrm_tmp) - 1)), 16), 0);
115 if (inverse)
116 emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
117 else
118 emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
119 shift -= 16;
120 while (shift >= 0) {
121 if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
122 emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
123 shift -= 16;
124 }
125}
126
127/*
Ard Biesheuvelcc2b8ed2018-11-23 18:29:02 +0100128 * Kernel addresses in the vmalloc space use at most 48 bits, and the
129 * remaining bits are guaranteed to be 0x1. So we can compose the address
130 * with a fixed length movn/movk/movk sequence.
Daniel Borkmann6d2eea62018-05-14 23:22:32 +0200131 */
132static inline void emit_addr_mov_i64(const int reg, const u64 val,
133 struct jit_ctx *ctx)
134{
135 u64 tmp = val;
136 int shift = 0;
137
Ard Biesheuvelcc2b8ed2018-11-23 18:29:02 +0100138 emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
139 while (shift < 32) {
Daniel Borkmann6d2eea62018-05-14 23:22:32 +0200140 tmp >>= 16;
141 shift += 16;
142 emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
143 }
144}
145
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300146static inline int bpf2a64_offset(int bpf_insn, int off,
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700147 const struct jit_ctx *ctx)
148{
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300149 /* BPF JMP offset is relative to the next instruction */
150 bpf_insn++;
151 /*
152 * Whereas arm64 branch instructions encode the offset
153 * from the branch itself, so we must subtract 1 from the
154 * instruction offset.
155 */
156 return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700157}
158
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100159static void jit_fill_hole(void *area, unsigned int size)
160{
Luc Van Oostenryck425e1ed2017-06-28 16:58:03 +0200161 __le32 *ptr;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100162 /* We are guaranteed to have aligned memory. */
163 for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
164 *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
165}
166
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700167static inline int epilogue_offset(const struct jit_ctx *ctx)
168{
Zi Shen Lim51c9fbb12014-12-03 08:38:01 +0000169 int to = ctx->epilogue_offset;
170 int from = ctx->idx;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700171
172 return to - from;
173}
174
Luke Nelsonfd868f12020-05-08 11:15:46 -0700175static bool is_addsub_imm(u32 imm)
176{
177 /* Either imm12 or shifted imm12. */
178 return !(imm & ~0xfff) || !(imm & ~0xfff000);
179}
180
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700181/* Stack must be multiples of 16B */
182#define STACK_ALIGN(sz) (((sz) + 15) & ~15)
183
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100184/* Tail call offset to jump into */
Mark Brownfa76cfe2020-05-06 20:51:32 +0100185#if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
186#define PROLOGUE_OFFSET 8
187#else
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100188#define PROLOGUE_OFFSET 7
Mark Brownfa76cfe2020-05-06 20:51:32 +0100189#endif
Zi Shen Limddb55992016-06-08 21:18:48 -0700190
Daniel Borkmann56ea6a82018-05-14 23:22:33 +0200191static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700192{
Daniel Borkmannf1c9eed2017-06-11 03:55:27 +0200193 const struct bpf_prog *prog = ctx->prog;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700194 const u8 r6 = bpf2a64[BPF_REG_6];
195 const u8 r7 = bpf2a64[BPF_REG_7];
196 const u8 r8 = bpf2a64[BPF_REG_8];
197 const u8 r9 = bpf2a64[BPF_REG_9];
198 const u8 fp = bpf2a64[BPF_REG_FP];
Zi Shen Limddb55992016-06-08 21:18:48 -0700199 const u8 tcc = bpf2a64[TCALL_CNT];
200 const int idx0 = ctx->idx;
201 int cur_offset;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700202
Yang Shiec0738d2015-11-16 14:35:35 -0800203 /*
204 * BPF prog stack layout
205 *
206 * high
207 * original A64_SP => 0:+-----+ BPF prologue
208 * |FP/LR|
209 * current A64_FP => -16:+-----+
210 * | ... | callee saved registers
Yang Shi4c1cd4f2016-05-16 16:36:26 -0700211 * BPF fp register => -64:+-----+ <= (BPF_FP)
Yang Shiec0738d2015-11-16 14:35:35 -0800212 * | |
213 * | ... | BPF prog stack
214 * | |
Daniel Borkmannf1c9eed2017-06-11 03:55:27 +0200215 * +-----+ <= (BPF_FP - prog->aux->stack_depth)
Daniel Borkmann09ece3d2018-05-14 23:22:31 +0200216 * |RSVD | padding
Daniel Borkmannf1c9eed2017-06-11 03:55:27 +0200217 * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
Yang Shiec0738d2015-11-16 14:35:35 -0800218 * | |
219 * | ... | Function call stack
220 * | |
221 * +-----+
222 * low
223 *
224 */
225
Mark Brownfa76cfe2020-05-06 20:51:32 +0100226 /* BTI landing pad */
227 if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
228 emit(A64_BTI_C, ctx);
229
Yang Shiec0738d2015-11-16 14:35:35 -0800230 /* Save FP and LR registers to stay align with ARM64 AAPCS */
231 emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
232 emit(A64_MOV(1, A64_FP, A64_SP), ctx);
233
Zi Shen Limddb55992016-06-08 21:18:48 -0700234 /* Save callee-saved registers */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700235 emit(A64_PUSH(r6, r7, A64_SP), ctx);
236 emit(A64_PUSH(r8, r9, A64_SP), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700237 emit(A64_PUSH(fp, tcc, A64_SP), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700238
Zi Shen Limddb55992016-06-08 21:18:48 -0700239 /* Set up BPF prog stack base register */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700240 emit(A64_MOV(1, fp, A64_SP), ctx);
241
Daniel Borkmann56ea6a82018-05-14 23:22:33 +0200242 if (!ebpf_from_cbpf) {
243 /* Initialize tail_call_cnt */
244 emit(A64_MOVZ(1, tcc, 0, 0), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700245
Daniel Borkmann56ea6a82018-05-14 23:22:33 +0200246 cur_offset = ctx->idx - idx0;
247 if (cur_offset != PROLOGUE_OFFSET) {
248 pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
249 cur_offset, PROLOGUE_OFFSET);
250 return -1;
251 }
Mark Brownfa76cfe2020-05-06 20:51:32 +0100252
253 /* BTI landing pad for the tail call, done with a BR */
254 if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
255 emit(A64_BTI_J, ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700256 }
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100257
Daniel Borkmann09ece3d2018-05-14 23:22:31 +0200258 ctx->stack_size = STACK_ALIGN(prog->aux->stack_depth);
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100259
260 /* Set up function call stack */
261 emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700262 return 0;
263}
264
265static int out_offset = -1; /* initialized on the first pass of build_body() */
266static int emit_bpf_tail_call(struct jit_ctx *ctx)
267{
268 /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
269 const u8 r2 = bpf2a64[BPF_REG_2];
270 const u8 r3 = bpf2a64[BPF_REG_3];
271
272 const u8 tmp = bpf2a64[TMP_REG_1];
273 const u8 prg = bpf2a64[TMP_REG_2];
274 const u8 tcc = bpf2a64[TCALL_CNT];
275 const int idx0 = ctx->idx;
276#define cur_offset (ctx->idx - idx0)
277#define jmp_offset (out_offset - (cur_offset))
278 size_t off;
279
280 /* if (index >= array->map.max_entries)
281 * goto out;
282 */
283 off = offsetof(struct bpf_array, map.max_entries);
284 emit_a64_mov_i64(tmp, off, ctx);
285 emit(A64_LDR32(tmp, r2, tmp), ctx);
Daniel Borkmann16338a92018-02-23 01:03:43 +0100286 emit(A64_MOV(0, r3, r3), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700287 emit(A64_CMP(0, r3, tmp), ctx);
Daniel Borkmann16338a92018-02-23 01:03:43 +0100288 emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700289
290 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
291 * goto out;
292 * tail_call_cnt++;
293 */
294 emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
295 emit(A64_CMP(1, tcc, tmp), ctx);
Daniel Borkmann16338a92018-02-23 01:03:43 +0100296 emit(A64_B_(A64_COND_HI, jmp_offset), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700297 emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
298
299 /* prog = array->ptrs[index];
300 * if (prog == NULL)
301 * goto out;
302 */
303 off = offsetof(struct bpf_array, ptrs);
304 emit_a64_mov_i64(tmp, off, ctx);
Daniel Borkmannd8b54112017-05-11 01:53:15 +0200305 emit(A64_ADD(1, tmp, r2, tmp), ctx);
306 emit(A64_LSL(1, prg, r3, 3), ctx);
307 emit(A64_LDR64(prg, tmp, prg), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700308 emit(A64_CBZ(1, prg, jmp_offset), ctx);
309
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100310 /* goto *(prog->bpf_func + prologue_offset); */
Zi Shen Limddb55992016-06-08 21:18:48 -0700311 off = offsetof(struct bpf_prog, bpf_func);
312 emit_a64_mov_i64(tmp, off, ctx);
313 emit(A64_LDR64(tmp, prg, tmp), ctx);
314 emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
Daniel Borkmanna2284d92018-01-16 03:46:08 +0100315 emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
Zi Shen Limddb55992016-06-08 21:18:48 -0700316 emit(A64_BR(tmp), ctx);
317
318 /* out: */
319 if (out_offset == -1)
320 out_offset = cur_offset;
321 if (cur_offset != out_offset) {
322 pr_err_once("tail_call out_offset = %d, expected %d!\n",
323 cur_offset, out_offset);
324 return -1;
325 }
326 return 0;
327#undef cur_offset
328#undef jmp_offset
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700329}
330
331static void build_epilogue(struct jit_ctx *ctx)
332{
333 const u8 r0 = bpf2a64[BPF_REG_0];
334 const u8 r6 = bpf2a64[BPF_REG_6];
335 const u8 r7 = bpf2a64[BPF_REG_7];
336 const u8 r8 = bpf2a64[BPF_REG_8];
337 const u8 r9 = bpf2a64[BPF_REG_9];
338 const u8 fp = bpf2a64[BPF_REG_FP];
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700339
340 /* We're done with BPF stack */
Daniel Borkmannf1c9eed2017-06-11 03:55:27 +0200341 emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700342
Yang Shiec0738d2015-11-16 14:35:35 -0800343 /* Restore fs (x25) and x26 */
344 emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
345
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700346 /* Restore callee-saved register */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700347 emit(A64_POP(r8, r9, A64_SP), ctx);
348 emit(A64_POP(r6, r7, A64_SP), ctx);
349
Yang Shiec0738d2015-11-16 14:35:35 -0800350 /* Restore FP/LR registers */
351 emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700352
353 /* Set return value */
354 emit(A64_MOV(1, A64_R(0), r0), ctx);
355
356 emit(A64_RET(A64_LR), ctx);
357}
358
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200359#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
360#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
361
362int arm64_bpf_fixup_exception(const struct exception_table_entry *ex,
363 struct pt_regs *regs)
364{
365 off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
366 int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
367
368 regs->regs[dst_reg] = 0;
369 regs->pc = (unsigned long)&ex->fixup - offset;
370 return 1;
371}
372
373/* For accesses to BTF pointers, add an entry to the exception table */
374static int add_exception_handler(const struct bpf_insn *insn,
375 struct jit_ctx *ctx,
376 int dst_reg)
377{
378 off_t offset;
379 unsigned long pc;
380 struct exception_table_entry *ex;
381
382 if (!ctx->image)
383 /* First pass */
384 return 0;
385
386 if (BPF_MODE(insn->code) != BPF_PROBE_MEM)
387 return 0;
388
389 if (!ctx->prog->aux->extable ||
390 WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
391 return -EINVAL;
392
393 ex = &ctx->prog->aux->extable[ctx->exentry_idx];
394 pc = (unsigned long)&ctx->image[ctx->idx - 1];
395
396 offset = pc - (long)&ex->insn;
397 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
398 return -ERANGE;
399 ex->insn = offset;
400
401 /*
402 * Since the extable follows the program, the fixup offset is always
403 * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
404 * to keep things simple, and put the destination register in the upper
405 * bits. We don't need to worry about buildtime or runtime sort
406 * modifying the upper bits because the table is already sorted, and
407 * isn't part of the main exception table.
408 */
409 offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
410 if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
411 return -ERANGE;
412
413 ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
414 FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
415
416 ctx->exentry_idx++;
417 return 0;
418}
419
Zi Shen Lim30d3d942014-09-16 21:29:23 +0100420/* JITs an eBPF instruction.
421 * Returns:
422 * 0 - successfully JITed an 8-byte eBPF instruction.
423 * >0 - successfully JITed a 16-byte eBPF instruction.
424 * <0 - failed to JIT.
425 */
Daniel Borkmann8c11ea52018-11-26 14:05:39 +0100426static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
427 bool extra_pass)
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700428{
429 const u8 code = insn->code;
430 const u8 dst = bpf2a64[insn->dst_reg];
431 const u8 src = bpf2a64[insn->src_reg];
432 const u8 tmp = bpf2a64[TMP_REG_1];
433 const u8 tmp2 = bpf2a64[TMP_REG_2];
Daniel Borkmann7005cad2017-06-07 13:45:37 +0200434 const u8 tmp3 = bpf2a64[TMP_REG_3];
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700435 const s16 off = insn->off;
436 const s32 imm = insn->imm;
437 const int i = insn - ctx->prog->insnsi;
Jiong Wang654b65a2019-01-26 12:26:08 -0500438 const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
439 BPF_CLASS(code) == BPF_JMP;
Daniel Borkmann85f68fe2017-05-01 02:57:20 +0200440 const bool isdw = BPF_SIZE(code) == BPF_DW;
Daniel Borkmann34b8ab02019-04-26 21:48:22 +0200441 u8 jmp_cond, reg;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700442 s32 jmp_offset;
Luke Nelsonfd495912020-05-08 11:15:45 -0700443 u32 a64_insn;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200444 int ret;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700445
Zi Shen Lim251599e2015-11-03 22:56:44 -0800446#define check_imm(bits, imm) do { \
447 if ((((imm) > 0) && ((imm) >> (bits))) || \
448 (((imm) < 0) && (~(imm) >> (bits)))) { \
449 pr_info("[%2d] imm=%d(0x%x) out of range\n", \
450 i, imm, imm); \
451 return -EINVAL; \
452 } \
453} while (0)
454#define check_imm19(imm) check_imm(19, imm)
455#define check_imm26(imm) check_imm(26, imm)
456
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700457 switch (code) {
458 /* dst = src */
459 case BPF_ALU | BPF_MOV | BPF_X:
460 case BPF_ALU64 | BPF_MOV | BPF_X:
461 emit(A64_MOV(is64, dst, src), ctx);
462 break;
463 /* dst = dst OP src */
464 case BPF_ALU | BPF_ADD | BPF_X:
465 case BPF_ALU64 | BPF_ADD | BPF_X:
466 emit(A64_ADD(is64, dst, dst, src), ctx);
467 break;
468 case BPF_ALU | BPF_SUB | BPF_X:
469 case BPF_ALU64 | BPF_SUB | BPF_X:
470 emit(A64_SUB(is64, dst, dst, src), ctx);
471 break;
472 case BPF_ALU | BPF_AND | BPF_X:
473 case BPF_ALU64 | BPF_AND | BPF_X:
474 emit(A64_AND(is64, dst, dst, src), ctx);
475 break;
476 case BPF_ALU | BPF_OR | BPF_X:
477 case BPF_ALU64 | BPF_OR | BPF_X:
478 emit(A64_ORR(is64, dst, dst, src), ctx);
479 break;
480 case BPF_ALU | BPF_XOR | BPF_X:
481 case BPF_ALU64 | BPF_XOR | BPF_X:
482 emit(A64_EOR(is64, dst, dst, src), ctx);
483 break;
484 case BPF_ALU | BPF_MUL | BPF_X:
485 case BPF_ALU64 | BPF_MUL | BPF_X:
486 emit(A64_MUL(is64, dst, dst, src), ctx);
487 break;
488 case BPF_ALU | BPF_DIV | BPF_X:
489 case BPF_ALU64 | BPF_DIV | BPF_X:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700490 case BPF_ALU | BPF_MOD | BPF_X:
491 case BPF_ALU64 | BPF_MOD | BPF_X:
Zi Shen Lim14e589f2015-11-04 20:43:59 -0800492 switch (BPF_OP(code)) {
493 case BPF_DIV:
494 emit(A64_UDIV(is64, dst, dst, src), ctx);
495 break;
496 case BPF_MOD:
Zi Shen Lim14e589f2015-11-04 20:43:59 -0800497 emit(A64_UDIV(is64, tmp, dst, src), ctx);
Jerin Jacob504792e2019-09-02 11:44:48 +0530498 emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
Zi Shen Lim14e589f2015-11-04 20:43:59 -0800499 break;
500 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700501 break;
Zi Shen Limd65a6342014-09-16 19:37:35 +0100502 case BPF_ALU | BPF_LSH | BPF_X:
503 case BPF_ALU64 | BPF_LSH | BPF_X:
504 emit(A64_LSLV(is64, dst, dst, src), ctx);
505 break;
506 case BPF_ALU | BPF_RSH | BPF_X:
507 case BPF_ALU64 | BPF_RSH | BPF_X:
508 emit(A64_LSRV(is64, dst, dst, src), ctx);
509 break;
510 case BPF_ALU | BPF_ARSH | BPF_X:
511 case BPF_ALU64 | BPF_ARSH | BPF_X:
512 emit(A64_ASRV(is64, dst, dst, src), ctx);
513 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700514 /* dst = -dst */
515 case BPF_ALU | BPF_NEG:
516 case BPF_ALU64 | BPF_NEG:
517 emit(A64_NEG(is64, dst, dst), ctx);
518 break;
519 /* dst = BSWAP##imm(dst) */
520 case BPF_ALU | BPF_END | BPF_FROM_LE:
521 case BPF_ALU | BPF_END | BPF_FROM_BE:
522#ifdef CONFIG_CPU_BIG_ENDIAN
523 if (BPF_SRC(code) == BPF_FROM_BE)
Xi Wangd63903b2015-06-25 18:39:15 -0700524 goto emit_bswap_uxt;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700525#else /* !CONFIG_CPU_BIG_ENDIAN */
526 if (BPF_SRC(code) == BPF_FROM_LE)
Xi Wangd63903b2015-06-25 18:39:15 -0700527 goto emit_bswap_uxt;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700528#endif
529 switch (imm) {
530 case 16:
531 emit(A64_REV16(is64, dst, dst), ctx);
Xi Wangd63903b2015-06-25 18:39:15 -0700532 /* zero-extend 16 bits into 64 bits */
533 emit(A64_UXTH(is64, dst, dst), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700534 break;
535 case 32:
536 emit(A64_REV32(is64, dst, dst), ctx);
Xi Wangd63903b2015-06-25 18:39:15 -0700537 /* upper 32 bits already cleared */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700538 break;
539 case 64:
540 emit(A64_REV64(dst, dst), ctx);
541 break;
542 }
543 break;
Xi Wangd63903b2015-06-25 18:39:15 -0700544emit_bswap_uxt:
545 switch (imm) {
546 case 16:
547 /* zero-extend 16 bits into 64 bits */
548 emit(A64_UXTH(is64, dst, dst), ctx);
549 break;
550 case 32:
551 /* zero-extend 32 bits into 64 bits */
552 emit(A64_UXTW(is64, dst, dst), ctx);
553 break;
554 case 64:
555 /* nop */
556 break;
557 }
558 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700559 /* dst = imm */
560 case BPF_ALU | BPF_MOV | BPF_K:
561 case BPF_ALU64 | BPF_MOV | BPF_K:
562 emit_a64_mov_i(is64, dst, imm, ctx);
563 break;
564 /* dst = dst OP imm */
565 case BPF_ALU | BPF_ADD | BPF_K:
566 case BPF_ALU64 | BPF_ADD | BPF_K:
Luke Nelsonfd868f12020-05-08 11:15:46 -0700567 if (is_addsub_imm(imm)) {
568 emit(A64_ADD_I(is64, dst, dst, imm), ctx);
569 } else if (is_addsub_imm(-imm)) {
570 emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
571 } else {
572 emit_a64_mov_i(is64, tmp, imm, ctx);
573 emit(A64_ADD(is64, dst, dst, tmp), ctx);
574 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700575 break;
576 case BPF_ALU | BPF_SUB | BPF_K:
577 case BPF_ALU64 | BPF_SUB | BPF_K:
Luke Nelsonfd868f12020-05-08 11:15:46 -0700578 if (is_addsub_imm(imm)) {
579 emit(A64_SUB_I(is64, dst, dst, imm), ctx);
580 } else if (is_addsub_imm(-imm)) {
581 emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
582 } else {
583 emit_a64_mov_i(is64, tmp, imm, ctx);
584 emit(A64_SUB(is64, dst, dst, tmp), ctx);
585 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700586 break;
587 case BPF_ALU | BPF_AND | BPF_K:
588 case BPF_ALU64 | BPF_AND | BPF_K:
Luke Nelsonfd495912020-05-08 11:15:45 -0700589 a64_insn = A64_AND_I(is64, dst, dst, imm);
590 if (a64_insn != AARCH64_BREAK_FAULT) {
591 emit(a64_insn, ctx);
592 } else {
593 emit_a64_mov_i(is64, tmp, imm, ctx);
594 emit(A64_AND(is64, dst, dst, tmp), ctx);
595 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700596 break;
597 case BPF_ALU | BPF_OR | BPF_K:
598 case BPF_ALU64 | BPF_OR | BPF_K:
Luke Nelsonfd495912020-05-08 11:15:45 -0700599 a64_insn = A64_ORR_I(is64, dst, dst, imm);
600 if (a64_insn != AARCH64_BREAK_FAULT) {
601 emit(a64_insn, ctx);
602 } else {
603 emit_a64_mov_i(is64, tmp, imm, ctx);
604 emit(A64_ORR(is64, dst, dst, tmp), ctx);
605 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700606 break;
607 case BPF_ALU | BPF_XOR | BPF_K:
608 case BPF_ALU64 | BPF_XOR | BPF_K:
Luke Nelsonfd495912020-05-08 11:15:45 -0700609 a64_insn = A64_EOR_I(is64, dst, dst, imm);
610 if (a64_insn != AARCH64_BREAK_FAULT) {
611 emit(a64_insn, ctx);
612 } else {
613 emit_a64_mov_i(is64, tmp, imm, ctx);
614 emit(A64_EOR(is64, dst, dst, tmp), ctx);
615 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700616 break;
617 case BPF_ALU | BPF_MUL | BPF_K:
618 case BPF_ALU64 | BPF_MUL | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700619 emit_a64_mov_i(is64, tmp, imm, ctx);
620 emit(A64_MUL(is64, dst, dst, tmp), ctx);
621 break;
622 case BPF_ALU | BPF_DIV | BPF_K:
623 case BPF_ALU64 | BPF_DIV | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700624 emit_a64_mov_i(is64, tmp, imm, ctx);
625 emit(A64_UDIV(is64, dst, dst, tmp), ctx);
626 break;
627 case BPF_ALU | BPF_MOD | BPF_K:
628 case BPF_ALU64 | BPF_MOD | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700629 emit_a64_mov_i(is64, tmp2, imm, ctx);
630 emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
Jerin Jacob504792e2019-09-02 11:44:48 +0530631 emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700632 break;
633 case BPF_ALU | BPF_LSH | BPF_K:
634 case BPF_ALU64 | BPF_LSH | BPF_K:
635 emit(A64_LSL(is64, dst, dst, imm), ctx);
636 break;
637 case BPF_ALU | BPF_RSH | BPF_K:
638 case BPF_ALU64 | BPF_RSH | BPF_K:
639 emit(A64_LSR(is64, dst, dst, imm), ctx);
640 break;
641 case BPF_ALU | BPF_ARSH | BPF_K:
642 case BPF_ALU64 | BPF_ARSH | BPF_K:
643 emit(A64_ASR(is64, dst, dst, imm), ctx);
644 break;
645
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700646 /* JUMP off */
647 case BPF_JMP | BPF_JA:
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300648 jmp_offset = bpf2a64_offset(i, off, ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700649 check_imm26(jmp_offset);
650 emit(A64_B(jmp_offset), ctx);
651 break;
652 /* IF (dst COND src) JUMP off */
653 case BPF_JMP | BPF_JEQ | BPF_X:
654 case BPF_JMP | BPF_JGT | BPF_X:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200655 case BPF_JMP | BPF_JLT | BPF_X:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700656 case BPF_JMP | BPF_JGE | BPF_X:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200657 case BPF_JMP | BPF_JLE | BPF_X:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700658 case BPF_JMP | BPF_JNE | BPF_X:
659 case BPF_JMP | BPF_JSGT | BPF_X:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200660 case BPF_JMP | BPF_JSLT | BPF_X:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700661 case BPF_JMP | BPF_JSGE | BPF_X:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200662 case BPF_JMP | BPF_JSLE | BPF_X:
Jiong Wang654b65a2019-01-26 12:26:08 -0500663 case BPF_JMP32 | BPF_JEQ | BPF_X:
664 case BPF_JMP32 | BPF_JGT | BPF_X:
665 case BPF_JMP32 | BPF_JLT | BPF_X:
666 case BPF_JMP32 | BPF_JGE | BPF_X:
667 case BPF_JMP32 | BPF_JLE | BPF_X:
668 case BPF_JMP32 | BPF_JNE | BPF_X:
669 case BPF_JMP32 | BPF_JSGT | BPF_X:
670 case BPF_JMP32 | BPF_JSLT | BPF_X:
671 case BPF_JMP32 | BPF_JSGE | BPF_X:
672 case BPF_JMP32 | BPF_JSLE | BPF_X:
673 emit(A64_CMP(is64, dst, src), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700674emit_cond_jmp:
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300675 jmp_offset = bpf2a64_offset(i, off, ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700676 check_imm19(jmp_offset);
677 switch (BPF_OP(code)) {
678 case BPF_JEQ:
679 jmp_cond = A64_COND_EQ;
680 break;
681 case BPF_JGT:
682 jmp_cond = A64_COND_HI;
683 break;
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200684 case BPF_JLT:
685 jmp_cond = A64_COND_CC;
686 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700687 case BPF_JGE:
688 jmp_cond = A64_COND_CS;
689 break;
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200690 case BPF_JLE:
691 jmp_cond = A64_COND_LS;
692 break;
Zi Shen Lim98397fc2016-05-12 23:37:58 -0700693 case BPF_JSET:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700694 case BPF_JNE:
695 jmp_cond = A64_COND_NE;
696 break;
697 case BPF_JSGT:
698 jmp_cond = A64_COND_GT;
699 break;
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200700 case BPF_JSLT:
701 jmp_cond = A64_COND_LT;
702 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700703 case BPF_JSGE:
704 jmp_cond = A64_COND_GE;
705 break;
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200706 case BPF_JSLE:
707 jmp_cond = A64_COND_LE;
708 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700709 default:
710 return -EFAULT;
711 }
712 emit(A64_B_(jmp_cond, jmp_offset), ctx);
713 break;
714 case BPF_JMP | BPF_JSET | BPF_X:
Jiong Wang654b65a2019-01-26 12:26:08 -0500715 case BPF_JMP32 | BPF_JSET | BPF_X:
716 emit(A64_TST(is64, dst, src), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700717 goto emit_cond_jmp;
718 /* IF (dst COND imm) JUMP off */
719 case BPF_JMP | BPF_JEQ | BPF_K:
720 case BPF_JMP | BPF_JGT | BPF_K:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200721 case BPF_JMP | BPF_JLT | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700722 case BPF_JMP | BPF_JGE | BPF_K:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200723 case BPF_JMP | BPF_JLE | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700724 case BPF_JMP | BPF_JNE | BPF_K:
725 case BPF_JMP | BPF_JSGT | BPF_K:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200726 case BPF_JMP | BPF_JSLT | BPF_K:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700727 case BPF_JMP | BPF_JSGE | BPF_K:
Daniel Borkmannc362b2f2017-08-10 01:39:57 +0200728 case BPF_JMP | BPF_JSLE | BPF_K:
Jiong Wang654b65a2019-01-26 12:26:08 -0500729 case BPF_JMP32 | BPF_JEQ | BPF_K:
730 case BPF_JMP32 | BPF_JGT | BPF_K:
731 case BPF_JMP32 | BPF_JLT | BPF_K:
732 case BPF_JMP32 | BPF_JGE | BPF_K:
733 case BPF_JMP32 | BPF_JLE | BPF_K:
734 case BPF_JMP32 | BPF_JNE | BPF_K:
735 case BPF_JMP32 | BPF_JSGT | BPF_K:
736 case BPF_JMP32 | BPF_JSLT | BPF_K:
737 case BPF_JMP32 | BPF_JSGE | BPF_K:
738 case BPF_JMP32 | BPF_JSLE | BPF_K:
Luke Nelsonfd868f12020-05-08 11:15:46 -0700739 if (is_addsub_imm(imm)) {
740 emit(A64_CMP_I(is64, dst, imm), ctx);
741 } else if (is_addsub_imm(-imm)) {
742 emit(A64_CMN_I(is64, dst, -imm), ctx);
743 } else {
744 emit_a64_mov_i(is64, tmp, imm, ctx);
745 emit(A64_CMP(is64, dst, tmp), ctx);
746 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700747 goto emit_cond_jmp;
748 case BPF_JMP | BPF_JSET | BPF_K:
Jiong Wang654b65a2019-01-26 12:26:08 -0500749 case BPF_JMP32 | BPF_JSET | BPF_K:
Luke Nelsonfd495912020-05-08 11:15:45 -0700750 a64_insn = A64_TST_I(is64, dst, imm);
751 if (a64_insn != AARCH64_BREAK_FAULT) {
752 emit(a64_insn, ctx);
753 } else {
754 emit_a64_mov_i(is64, tmp, imm, ctx);
755 emit(A64_TST(is64, dst, tmp), ctx);
756 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700757 goto emit_cond_jmp;
758 /* function call */
759 case BPF_JMP | BPF_CALL:
760 {
761 const u8 r0 = bpf2a64[BPF_REG_0];
Daniel Borkmann8c11ea52018-11-26 14:05:39 +0100762 bool func_addr_fixed;
763 u64 func_addr;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700764
Daniel Borkmann8c11ea52018-11-26 14:05:39 +0100765 ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
766 &func_addr, &func_addr_fixed);
767 if (ret < 0)
768 return ret;
Ard Biesheuvelcc2b8ed2018-11-23 18:29:02 +0100769 emit_addr_mov_i64(tmp, func_addr, ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700770 emit(A64_BLR(tmp), ctx);
771 emit(A64_MOV(1, r0, A64_R(0)), ctx);
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700772 break;
773 }
Zi Shen Limddb55992016-06-08 21:18:48 -0700774 /* tail call */
Alexei Starovoitov71189fa2017-05-30 13:31:27 -0700775 case BPF_JMP | BPF_TAIL_CALL:
Zi Shen Limddb55992016-06-08 21:18:48 -0700776 if (emit_bpf_tail_call(ctx))
777 return -EFAULT;
778 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700779 /* function return */
780 case BPF_JMP | BPF_EXIT:
Zi Shen Lim51c9fbb12014-12-03 08:38:01 +0000781 /* Optimization: when last instruction is EXIT,
782 simply fallthrough to epilogue. */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700783 if (i == ctx->prog->len - 1)
784 break;
785 jmp_offset = epilogue_offset(ctx);
786 check_imm26(jmp_offset);
787 emit(A64_B(jmp_offset), ctx);
788 break;
789
Zi Shen Lim30d3d942014-09-16 21:29:23 +0100790 /* dst = imm64 */
791 case BPF_LD | BPF_IMM | BPF_DW:
792 {
793 const struct bpf_insn insn1 = insn[1];
794 u64 imm64;
795
Xi Wang1e4df6b2015-05-08 06:39:51 +0100796 imm64 = (u64)insn1.imm << 32 | (u32)imm;
Zi Shen Lim30d3d942014-09-16 21:29:23 +0100797 emit_a64_mov_i64(dst, imm64, ctx);
798
799 return 1;
800 }
801
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700802 /* LDX: dst = *(size *)(src + off) */
803 case BPF_LDX | BPF_MEM | BPF_W:
804 case BPF_LDX | BPF_MEM | BPF_H:
805 case BPF_LDX | BPF_MEM | BPF_B:
806 case BPF_LDX | BPF_MEM | BPF_DW:
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200807 case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
808 case BPF_LDX | BPF_PROBE_MEM | BPF_W:
809 case BPF_LDX | BPF_PROBE_MEM | BPF_H:
810 case BPF_LDX | BPF_PROBE_MEM | BPF_B:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700811 emit_a64_mov_i(1, tmp, off, ctx);
812 switch (BPF_SIZE(code)) {
813 case BPF_W:
814 emit(A64_LDR32(dst, src, tmp), ctx);
815 break;
816 case BPF_H:
817 emit(A64_LDRH(dst, src, tmp), ctx);
818 break;
819 case BPF_B:
820 emit(A64_LDRB(dst, src, tmp), ctx);
821 break;
822 case BPF_DW:
823 emit(A64_LDR64(dst, src, tmp), ctx);
824 break;
825 }
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200826
827 ret = add_exception_handler(insn, ctx, dst);
828 if (ret)
829 return ret;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700830 break;
831
832 /* ST: *(size *)(dst + off) = imm */
833 case BPF_ST | BPF_MEM | BPF_W:
834 case BPF_ST | BPF_MEM | BPF_H:
835 case BPF_ST | BPF_MEM | BPF_B:
836 case BPF_ST | BPF_MEM | BPF_DW:
Yang Shidf849ba2015-11-30 14:24:07 -0800837 /* Load imm to a register then store it */
Yang Shidf849ba2015-11-30 14:24:07 -0800838 emit_a64_mov_i(1, tmp2, off, ctx);
839 emit_a64_mov_i(1, tmp, imm, ctx);
840 switch (BPF_SIZE(code)) {
841 case BPF_W:
842 emit(A64_STR32(tmp, dst, tmp2), ctx);
843 break;
844 case BPF_H:
845 emit(A64_STRH(tmp, dst, tmp2), ctx);
846 break;
847 case BPF_B:
848 emit(A64_STRB(tmp, dst, tmp2), ctx);
849 break;
850 case BPF_DW:
851 emit(A64_STR64(tmp, dst, tmp2), ctx);
852 break;
853 }
854 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700855
856 /* STX: *(size *)(dst + off) = src */
857 case BPF_STX | BPF_MEM | BPF_W:
858 case BPF_STX | BPF_MEM | BPF_H:
859 case BPF_STX | BPF_MEM | BPF_B:
860 case BPF_STX | BPF_MEM | BPF_DW:
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700861 emit_a64_mov_i(1, tmp, off, ctx);
862 switch (BPF_SIZE(code)) {
863 case BPF_W:
864 emit(A64_STR32(src, dst, tmp), ctx);
865 break;
866 case BPF_H:
867 emit(A64_STRH(src, dst, tmp), ctx);
868 break;
869 case BPF_B:
870 emit(A64_STRB(src, dst, tmp), ctx);
871 break;
872 case BPF_DW:
873 emit(A64_STR64(src, dst, tmp), ctx);
874 break;
875 }
876 break;
Daniel Borkmann34b8ab02019-04-26 21:48:22 +0200877
Brendan Jackman91c960b2021-01-14 18:17:44 +0000878 case BPF_STX | BPF_ATOMIC | BPF_W:
879 case BPF_STX | BPF_ATOMIC | BPF_DW:
880 if (insn->imm != BPF_ADD) {
881 pr_err_once("unknown atomic op code %02x\n", insn->imm);
882 return -EINVAL;
883 }
884
885 /* STX XADD: lock *(u32 *)(dst + off) += src
886 * and
887 * STX XADD: lock *(u64 *)(dst + off) += src
888 */
889
Daniel Borkmann34b8ab02019-04-26 21:48:22 +0200890 if (!off) {
891 reg = dst;
892 } else {
893 emit_a64_mov_i(1, tmp, off, ctx);
894 emit(A64_ADD(1, tmp, tmp, dst), ctx);
895 reg = tmp;
896 }
897 if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS)) {
898 emit(A64_STADD(isdw, reg, src), ctx);
899 } else {
900 emit(A64_LDXR(isdw, tmp2, reg), ctx);
901 emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
902 emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
903 jmp_offset = -3;
904 check_imm19(jmp_offset);
905 emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
906 }
Daniel Borkmann85f68fe2017-05-01 02:57:20 +0200907 break;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700908
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700909 default:
910 pr_err_once("unknown opcode %02x\n", code);
911 return -EINVAL;
912 }
913
914 return 0;
915}
916
Daniel Borkmann8c11ea52018-11-26 14:05:39 +0100917static int build_body(struct jit_ctx *ctx, bool extra_pass)
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700918{
919 const struct bpf_prog *prog = ctx->prog;
920 int i;
921
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300922 /*
923 * - offset[0] offset of the end of prologue,
924 * start of the 1st instruction.
925 * - offset[1] - offset of the end of 1st instruction,
926 * start of the 2nd instruction
927 * [....]
928 * - offset[3] - offset of the end of 3rd instruction,
929 * start of 4th instruction
930 */
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700931 for (i = 0; i < prog->len; i++) {
932 const struct bpf_insn *insn = &prog->insnsi[i];
933 int ret;
934
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300935 if (ctx->image == NULL)
936 ctx->offset[i] = ctx->idx;
Daniel Borkmann8c11ea52018-11-26 14:05:39 +0100937 ret = build_insn(insn, ctx, extra_pass);
Zi Shen Lim30d3d942014-09-16 21:29:23 +0100938 if (ret > 0) {
939 i++;
Daniel Borkmannddc665a2017-05-02 20:34:54 +0200940 if (ctx->image == NULL)
941 ctx->offset[i] = ctx->idx;
Zi Shen Lim30d3d942014-09-16 21:29:23 +0100942 continue;
943 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700944 if (ret)
945 return ret;
946 }
Ilias Apalodimas32f68652020-09-17 11:49:25 +0300947 /*
948 * offset is allocated with prog->len + 1 so fill in
949 * the last element with the offset after the last
950 * instruction (end of program)
951 */
952 if (ctx->image == NULL)
953 ctx->offset[i] = ctx->idx;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700954
955 return 0;
956}
957
Zi Shen Lim42ff7122016-01-13 23:33:22 -0800958static int validate_code(struct jit_ctx *ctx)
959{
960 int i;
961
962 for (i = 0; i < ctx->idx; i++) {
963 u32 a64_insn = le32_to_cpu(ctx->image[i]);
964
965 if (a64_insn == AARCH64_BREAK_FAULT)
966 return -1;
967 }
968
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200969 if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
970 return -1;
971
Zi Shen Lim42ff7122016-01-13 23:33:22 -0800972 return 0;
973}
974
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700975static inline void bpf_flush_icache(void *start, void *end)
976{
977 flush_icache_range((unsigned long)start, (unsigned long)end);
978}
979
Alexei Starovoitovdb496942017-12-14 17:55:16 -0800980struct arm64_jit_data {
981 struct bpf_binary_header *header;
982 u8 *image;
983 struct jit_ctx ctx;
984};
985
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200986struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700987{
Jean-Philippe Brucker80083422020-07-28 17:21:26 +0200988 int image_size, prog_size, extable_size;
Daniel Borkmann26eb0422016-05-13 19:08:34 +0200989 struct bpf_prog *tmp, *orig_prog = prog;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100990 struct bpf_binary_header *header;
Alexei Starovoitovdb496942017-12-14 17:55:16 -0800991 struct arm64_jit_data *jit_data;
Daniel Borkmann56ea6a82018-05-14 23:22:33 +0200992 bool was_classic = bpf_prog_was_classic(prog);
Daniel Borkmann26eb0422016-05-13 19:08:34 +0200993 bool tmp_blinded = false;
Alexei Starovoitovdb496942017-12-14 17:55:16 -0800994 bool extra_pass = false;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700995 struct jit_ctx ctx;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +0100996 u8 *image_ptr;
Zi Shen Lime54bcde2014-08-26 21:15:30 -0700997
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -0800998 if (!prog->jit_requested)
Daniel Borkmann26eb0422016-05-13 19:08:34 +0200999 return orig_prog;
1000
1001 tmp = bpf_jit_blind_constants(prog);
1002 /* If blinding was requested and we failed during blinding,
1003 * we must fall back to the interpreter.
1004 */
1005 if (IS_ERR(tmp))
1006 return orig_prog;
1007 if (tmp != prog) {
1008 tmp_blinded = true;
1009 prog = tmp;
1010 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001011
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001012 jit_data = prog->aux->jit_data;
1013 if (!jit_data) {
1014 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1015 if (!jit_data) {
1016 prog = orig_prog;
1017 goto out;
1018 }
1019 prog->aux->jit_data = jit_data;
1020 }
1021 if (jit_data->ctx.offset) {
1022 ctx = jit_data->ctx;
1023 image_ptr = jit_data->image;
1024 header = jit_data->header;
1025 extra_pass = true;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001026 prog_size = sizeof(u32) * ctx.idx;
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001027 goto skip_init_ctx;
1028 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001029 memset(&ctx, 0, sizeof(ctx));
1030 ctx.prog = prog;
1031
Ilias Apalodimas32f68652020-09-17 11:49:25 +03001032 ctx.offset = kcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001033 if (ctx.offset == NULL) {
1034 prog = orig_prog;
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001035 goto out_off;
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001036 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001037
1038 /* 1. Initial fake pass to compute ctx->idx. */
1039
Yang Shi4c1cd4f2016-05-16 16:36:26 -07001040 /* Fake pass to fill in ctx->offset. */
Daniel Borkmann8c11ea52018-11-26 14:05:39 +01001041 if (build_body(&ctx, extra_pass)) {
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001042 prog = orig_prog;
1043 goto out_off;
1044 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001045
Daniel Borkmann56ea6a82018-05-14 23:22:33 +02001046 if (build_prologue(&ctx, was_classic)) {
Zi Shen Limddb55992016-06-08 21:18:48 -07001047 prog = orig_prog;
1048 goto out_off;
1049 }
Zi Shen Lim51c9fbb12014-12-03 08:38:01 +00001050
1051 ctx.epilogue_offset = ctx.idx;
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001052 build_epilogue(&ctx);
1053
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001054 extable_size = prog->aux->num_exentries *
1055 sizeof(struct exception_table_entry);
1056
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001057 /* Now we know the actual image size. */
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001058 prog_size = sizeof(u32) * ctx.idx;
1059 image_size = prog_size + extable_size;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +01001060 header = bpf_jit_binary_alloc(image_size, &image_ptr,
1061 sizeof(u32), jit_fill_hole);
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001062 if (header == NULL) {
1063 prog = orig_prog;
1064 goto out_off;
1065 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001066
1067 /* 2. Now, the actual pass. */
1068
Luc Van Oostenryck425e1ed2017-06-28 16:58:03 +02001069 ctx.image = (__le32 *)image_ptr;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001070 if (extable_size)
1071 prog->aux->extable = (void *)image_ptr + prog_size;
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001072skip_init_ctx:
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001073 ctx.idx = 0;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001074 ctx.exentry_idx = 0;
Daniel Borkmannb569c1c2014-09-16 08:48:50 +01001075
Daniel Borkmann56ea6a82018-05-14 23:22:33 +02001076 build_prologue(&ctx, was_classic);
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001077
Daniel Borkmann8c11ea52018-11-26 14:05:39 +01001078 if (build_body(&ctx, extra_pass)) {
Daniel Borkmannb569c1c2014-09-16 08:48:50 +01001079 bpf_jit_binary_free(header);
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001080 prog = orig_prog;
1081 goto out_off;
Daniel Borkmann60ef0492014-09-11 10:36:48 +01001082 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001083
1084 build_epilogue(&ctx);
1085
Zi Shen Lim42ff7122016-01-13 23:33:22 -08001086 /* 3. Extra pass to validate JITed code. */
1087 if (validate_code(&ctx)) {
1088 bpf_jit_binary_free(header);
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001089 prog = orig_prog;
1090 goto out_off;
Zi Shen Lim42ff7122016-01-13 23:33:22 -08001091 }
1092
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001093 /* And we're done. */
1094 if (bpf_jit_enable > 1)
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001095 bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001096
Daniel Borkmannc3d4c682015-11-14 01:16:18 +01001097 bpf_flush_icache(header, ctx.image + ctx.idx);
Daniel Borkmannb569c1c2014-09-16 08:48:50 +01001098
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001099 if (!prog->is_func || extra_pass) {
1100 if (extra_pass && ctx.idx != jit_data->ctx.idx) {
1101 pr_err_once("multi-func JIT bug %d != %d\n",
1102 ctx.idx, jit_data->ctx.idx);
1103 bpf_jit_binary_free(header);
1104 prog->bpf_func = NULL;
1105 prog->jited = 0;
1106 goto out_off;
1107 }
1108 bpf_jit_binary_lock_ro(header);
1109 } else {
1110 jit_data->ctx = ctx;
1111 jit_data->image = image_ptr;
1112 jit_data->header = header;
1113 }
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001114 prog->bpf_func = (void *)ctx.image;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001115 prog->jited = 1;
Jean-Philippe Brucker80083422020-07-28 17:21:26 +02001116 prog->jited_len = prog_size;
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001117
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001118 if (!prog->is_func || extra_pass) {
Ilias Apalodimas32f68652020-09-17 11:49:25 +03001119 bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001120out_off:
Alexei Starovoitovdb496942017-12-14 17:55:16 -08001121 kfree(ctx.offset);
1122 kfree(jit_data);
1123 prog->aux->jit_data = NULL;
1124 }
Daniel Borkmann26eb0422016-05-13 19:08:34 +02001125out:
1126 if (tmp_blinded)
1127 bpf_jit_prog_release_other(prog, prog == orig_prog ?
1128 tmp : orig_prog);
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001129 return prog;
Zi Shen Lime54bcde2014-08-26 21:15:30 -07001130}
Ard Biesheuvel91fc957c2018-11-23 23:18:04 +01001131
1132void *bpf_jit_alloc_exec(unsigned long size)
1133{
1134 return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
1135 BPF_JIT_REGION_END, GFP_KERNEL,
Ard Biesheuvel3f750702019-05-23 11:22:56 +01001136 PAGE_KERNEL, 0, NUMA_NO_NODE,
Ard Biesheuvel91fc957c2018-11-23 23:18:04 +01001137 __builtin_return_address(0));
1138}
1139
1140void bpf_jit_free_exec(void *addr)
1141{
1142 return vfree(addr);
1143}