bpf/arm: add prologue and epilogue
[dpdk.git] / lib / librte_bpf / bpf_jit_arm64.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <errno.h>
6 #include <stdbool.h>
7
8 #include <rte_common.h>
9
10 #include "bpf_impl.h"
11
12 #define A64_REG_MASK(r)         ((r) & 0x1f)
13 #define A64_INVALID_OP_CODE     (0xffffffff)
14
15 #define TMP_REG_1               (EBPF_REG_10 + 1)
16 #define TMP_REG_2               (EBPF_REG_10 + 2)
17 #define TMP_REG_3               (EBPF_REG_10 + 3)
18
19 #define EBPF_FP                 (EBPF_REG_10)
20 #define EBPF_OP_GET(op)         (BPF_OP(op) >> 4)
21
22 #define A64_R(x)                x
23 #define A64_FP                  29
24 #define A64_LR                  30
25 #define A64_SP                  31
26 #define A64_ZR                  31
27
28 #define check_imm(n, val) (((val) >= 0) ? !!((val) >> (n)) : !!((~val) >> (n)))
29 #define mask_imm(n, val) ((val) & ((1 << (n)) - 1))
30
31 struct ebpf_a64_map {
32         uint32_t off; /* eBPF to arm64 insn offset mapping for jump */
33         uint8_t off_to_b; /* Offset to branch instruction delta */
34 };
35
36 struct a64_jit_ctx {
37         size_t stack_sz;          /* Stack size */
38         uint32_t *ins;            /* ARM64 instructions. NULL if first pass */
39         struct ebpf_a64_map *map; /* eBPF to arm64 insn mapping for jump */
40         uint32_t idx;             /* Current instruction index */
41         uint32_t program_start;   /* Program index, Just after prologue */
42         uint32_t program_sz;      /* Program size. Found in first pass */
43         uint8_t foundcall;        /* Found EBPF_CALL class code in eBPF pgm */
44 };
45
46 static int
47 check_reg(uint8_t r)
48 {
49         return (r > 31) ? 1 : 0;
50 }
51
52 static int
53 is_first_pass(struct a64_jit_ctx *ctx)
54 {
55         return (ctx->ins == NULL);
56 }
57
58 static int
59 check_invalid_args(struct a64_jit_ctx *ctx, uint32_t limit)
60 {
61         uint32_t idx;
62
63         if (is_first_pass(ctx))
64                 return 0;
65
66         for (idx = 0; idx < limit; idx++) {
67                 if (rte_le_to_cpu_32(ctx->ins[idx]) == A64_INVALID_OP_CODE) {
68                         RTE_BPF_LOG(ERR,
69                                 "%s: invalid opcode at %u;\n", __func__, idx);
70                         return -EINVAL;
71                 }
72         }
73         return 0;
74 }
75
76 /* Emit an instruction */
77 static inline void
78 emit_insn(struct a64_jit_ctx *ctx, uint32_t insn, int error)
79 {
80         if (error)
81                 insn = A64_INVALID_OP_CODE;
82
83         if (ctx->ins)
84                 ctx->ins[ctx->idx] = rte_cpu_to_le_32(insn);
85
86         ctx->idx++;
87 }
88
89 static void
90 emit_ret(struct a64_jit_ctx *ctx)
91 {
92         emit_insn(ctx, 0xd65f03c0, 0);
93 }
94
95 static void
96 emit_add_sub_imm(struct a64_jit_ctx *ctx, bool is64, bool sub, uint8_t rd,
97                  uint8_t rn, int16_t imm12)
98 {
99         uint32_t insn, imm;
100
101         imm = mask_imm(12, imm12);
102         insn = (!!is64) << 31;
103         insn |= (!!sub) << 30;
104         insn |= 0x11000000;
105         insn |= rd;
106         insn |= rn << 5;
107         insn |= imm << 10;
108
109         emit_insn(ctx, insn,
110                   check_reg(rd) || check_reg(rn) || check_imm(12, imm12));
111 }
112
113 static void
114 emit_add_imm_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn, uint16_t imm12)
115 {
116         emit_add_sub_imm(ctx, 1, 0, rd, rn, imm12);
117 }
118
119 static void
120 emit_sub_imm_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn, uint16_t imm12)
121 {
122         emit_add_sub_imm(ctx, 1, 1, rd, rn, imm12);
123 }
124
125 static void
126 emit_mov(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rn)
127 {
128         emit_add_sub_imm(ctx, is64, 0, rd, rn, 0);
129 }
130
131 static void
132 emit_mov_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn)
133 {
134         emit_mov(ctx, 1, rd, rn);
135 }
136
137 static void
138 emit_ls_pair_64(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2, uint8_t rn,
139                 bool push, bool load, bool pre_index)
140 {
141         uint32_t insn;
142
143         insn = (!!load) << 22;
144         insn |= (!!pre_index) << 24;
145         insn |= 0xa8800000;
146         insn |= rt;
147         insn |= rn << 5;
148         insn |= rt2 << 10;
149         if (push)
150                 insn |= 0x7e << 15; /* 0x7e means -2 with imm7 */
151         else
152                 insn |= 0x2 << 15;
153
154         emit_insn(ctx, insn, check_reg(rn) || check_reg(rt) || check_reg(rt2));
155
156 }
157
158 /* Emit stp rt, rt2, [sp, #-16]! */
159 static void
160 emit_stack_push(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2)
161 {
162         emit_ls_pair_64(ctx, rt, rt2, A64_SP, 1, 0, 1);
163 }
164
165 /* Emit ldp rt, rt2, [sp, #16] */
166 static void
167 emit_stack_pop(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2)
168 {
169         emit_ls_pair_64(ctx, rt, rt2, A64_SP, 0, 1, 0);
170 }
171
172 static uint8_t
173 ebpf_to_a64_reg(struct a64_jit_ctx *ctx, uint8_t reg)
174 {
175         const uint32_t ebpf2a64_has_call[] = {
176                 /* Map A64 R7 register as EBPF return register */
177                 [EBPF_REG_0] = A64_R(7),
178                 /* Map A64 arguments register as EBPF arguments register */
179                 [EBPF_REG_1] = A64_R(0),
180                 [EBPF_REG_2] = A64_R(1),
181                 [EBPF_REG_3] = A64_R(2),
182                 [EBPF_REG_4] = A64_R(3),
183                 [EBPF_REG_5] = A64_R(4),
184                 /* Map A64 callee save register as EBPF callee save register */
185                 [EBPF_REG_6] = A64_R(19),
186                 [EBPF_REG_7] = A64_R(20),
187                 [EBPF_REG_8] = A64_R(21),
188                 [EBPF_REG_9] = A64_R(22),
189                 [EBPF_FP]    = A64_R(25),
190                 /* Map A64 scratch registers as temporary storage */
191                 [TMP_REG_1] = A64_R(9),
192                 [TMP_REG_2] = A64_R(10),
193                 [TMP_REG_3] = A64_R(11),
194         };
195
196         const uint32_t ebpf2a64_no_call[] = {
197                 /* Map A64 R7 register as EBPF return register */
198                 [EBPF_REG_0] = A64_R(7),
199                 /* Map A64 arguments register as EBPF arguments register */
200                 [EBPF_REG_1] = A64_R(0),
201                 [EBPF_REG_2] = A64_R(1),
202                 [EBPF_REG_3] = A64_R(2),
203                 [EBPF_REG_4] = A64_R(3),
204                 [EBPF_REG_5] = A64_R(4),
205                 /*
206                  * EBPF program does not have EBPF_CALL op code,
207                  * Map A64 scratch registers as EBPF callee save registers.
208                  */
209                 [EBPF_REG_6] = A64_R(9),
210                 [EBPF_REG_7] = A64_R(10),
211                 [EBPF_REG_8] = A64_R(11),
212                 [EBPF_REG_9] = A64_R(12),
213                 /* Map A64 FP register as EBPF FP register */
214                 [EBPF_FP]    = A64_FP,
215                 /* Map remaining A64 scratch registers as temporary storage */
216                 [TMP_REG_1] = A64_R(13),
217                 [TMP_REG_2] = A64_R(14),
218                 [TMP_REG_3] = A64_R(15),
219         };
220
221         if (ctx->foundcall)
222                 return ebpf2a64_has_call[reg];
223         else
224                 return ebpf2a64_no_call[reg];
225 }
226
227 /*
228  * Procedure call standard for the arm64
229  * -------------------------------------
230  * R0..R7  - Parameter/result registers
231  * R8      - Indirect result location register
232  * R9..R15 - Scratch registers
233  * R15     - Platform Register
234  * R16     - First intra-procedure-call scratch register
235  * R17     - Second intra-procedure-call temporary register
236  * R19-R28 - Callee saved registers
237  * R29     - Frame pointer
238  * R30     - Link register
239  * R31     - Stack pointer
240  */
241 static void
242 emit_prologue_has_call(struct a64_jit_ctx *ctx)
243 {
244         uint8_t r6, r7, r8, r9, fp;
245
246         r6 = ebpf_to_a64_reg(ctx, EBPF_REG_6);
247         r7 = ebpf_to_a64_reg(ctx, EBPF_REG_7);
248         r8 = ebpf_to_a64_reg(ctx, EBPF_REG_8);
249         r9 = ebpf_to_a64_reg(ctx, EBPF_REG_9);
250         fp = ebpf_to_a64_reg(ctx, EBPF_FP);
251
252         /*
253          * eBPF prog stack layout
254          *
255          *                               high
256          *       eBPF prologue       0:+-----+ <= original A64_SP
257          *                             |FP/LR|
258          *                         -16:+-----+ <= current A64_FP
259          *    Callee saved registers   | ... |
260          *             EBPF_FP =>  -64:+-----+
261          *                             |     |
262          *       eBPF prog stack       | ... |
263          *                             |     |
264          * (EBPF_FP - bpf->stack_sz)=> +-----+
265          * Pad for A64_SP 16B alignment| PAD |
266          * (EBPF_FP - ctx->stack_sz)=> +-----+ <= current A64_SP
267          *                             |     |
268          *                             | ... | Function call stack
269          *                             |     |
270          *                             +-----+
271          *                              low
272          */
273         emit_stack_push(ctx, A64_FP, A64_LR);
274         emit_mov_64(ctx, A64_FP, A64_SP);
275         emit_stack_push(ctx, r6, r7);
276         emit_stack_push(ctx, r8, r9);
277         /*
278          * There is no requirement to save A64_R(28) in stack. Doing it here,
279          * because, A64_SP needs be to 16B aligned and STR vs STP
280          * takes same number of cycles(typically).
281          */
282         emit_stack_push(ctx, fp, A64_R(28));
283         emit_mov_64(ctx, fp, A64_SP);
284         if (ctx->stack_sz)
285                 emit_sub_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
286 }
287
288 static void
289 emit_epilogue_has_call(struct a64_jit_ctx *ctx)
290 {
291         uint8_t r6, r7, r8, r9, fp, r0;
292
293         r6 = ebpf_to_a64_reg(ctx, EBPF_REG_6);
294         r7 = ebpf_to_a64_reg(ctx, EBPF_REG_7);
295         r8 = ebpf_to_a64_reg(ctx, EBPF_REG_8);
296         r9 = ebpf_to_a64_reg(ctx, EBPF_REG_9);
297         fp = ebpf_to_a64_reg(ctx, EBPF_FP);
298         r0 = ebpf_to_a64_reg(ctx, EBPF_REG_0);
299
300         if (ctx->stack_sz)
301                 emit_add_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
302         emit_stack_pop(ctx, fp, A64_R(28));
303         emit_stack_pop(ctx, r8, r9);
304         emit_stack_pop(ctx, r6, r7);
305         emit_stack_pop(ctx, A64_FP, A64_LR);
306         emit_mov_64(ctx, A64_R(0), r0);
307         emit_ret(ctx);
308 }
309
310 static void
311 emit_prologue_no_call(struct a64_jit_ctx *ctx)
312 {
313         /*
314          * eBPF prog stack layout without EBPF_CALL opcode
315          *
316          *                               high
317          *    eBPF prologue(EBPF_FP) 0:+-----+ <= original A64_SP/current A64_FP
318          *                             |     |
319          *                             | ... |
320          *            eBPF prog stack  |     |
321          *                             |     |
322          * (EBPF_FP - bpf->stack_sz)=> +-----+
323          * Pad for A64_SP 16B alignment| PAD |
324          * (EBPF_FP - ctx->stack_sz)=> +-----+ <= current A64_SP
325          *                             |     |
326          *                             | ... | Function call stack
327          *                             |     |
328          *                             +-----+
329          *                              low
330          */
331         if (ctx->stack_sz) {
332                 emit_mov_64(ctx, A64_FP, A64_SP);
333                 emit_sub_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
334         }
335 }
336
337 static void
338 emit_epilogue_no_call(struct a64_jit_ctx *ctx)
339 {
340         if (ctx->stack_sz)
341                 emit_add_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
342         emit_mov_64(ctx, A64_R(0), ebpf_to_a64_reg(ctx, EBPF_REG_0));
343         emit_ret(ctx);
344 }
345
346 static void
347 emit_prologue(struct a64_jit_ctx *ctx)
348 {
349         if (ctx->foundcall)
350                 emit_prologue_has_call(ctx);
351         else
352                 emit_prologue_no_call(ctx);
353
354         ctx->program_start = ctx->idx;
355 }
356
357 static void
358 emit_epilogue(struct a64_jit_ctx *ctx)
359 {
360         ctx->program_sz = ctx->idx - ctx->program_start;
361
362         if (ctx->foundcall)
363                 emit_epilogue_has_call(ctx);
364         else
365                 emit_epilogue_no_call(ctx);
366 }
367
368 static void
369 check_program_has_call(struct a64_jit_ctx *ctx, struct rte_bpf *bpf)
370 {
371         const struct ebpf_insn *ins;
372         uint8_t op;
373         uint32_t i;
374
375         for (i = 0; i != bpf->prm.nb_ins; i++) {
376                 ins = bpf->prm.ins + i;
377                 op = ins->code;
378
379                 switch (op) {
380                 /* Call imm */
381                 case (BPF_JMP | EBPF_CALL):
382                         ctx->foundcall = 1;
383                         return;
384                 }
385         }
386 }
387
388 /*
389  * Walk through eBPF code and translate them to arm64 one.
390  */
391 static int
392 emit(struct a64_jit_ctx *ctx, struct rte_bpf *bpf)
393 {
394         uint8_t op;
395         const struct ebpf_insn *ins;
396         uint32_t i;
397         int rc;
398
399         /* Reset context fields */
400         ctx->idx = 0;
401         /* arm64 SP must be aligned to 16 */
402         ctx->stack_sz = RTE_ALIGN_MUL_CEIL(bpf->stack_sz, 16);
403
404         emit_prologue(ctx);
405
406         for (i = 0; i != bpf->prm.nb_ins; i++) {
407
408                 ins = bpf->prm.ins + i;
409                 op = ins->code;
410
411                 switch (op) {
412                 /* Return r0 */
413                 case (BPF_JMP | EBPF_EXIT):
414                         emit_epilogue(ctx);
415                         break;
416                 default:
417                         RTE_BPF_LOG(ERR,
418                                 "%s(%p): invalid opcode %#x at pc: %u;\n",
419                                 __func__, bpf, ins->code, i);
420                         return -EINVAL;
421                 }
422         }
423         rc = check_invalid_args(ctx, ctx->idx);
424
425         return rc;
426 }
427
428 /*
429  * Produce a native ISA version of the given BPF code.
430  */
431 int
432 bpf_jit_arm64(struct rte_bpf *bpf)
433 {
434         struct a64_jit_ctx ctx;
435         size_t size;
436         int rc;
437
438         /* Init JIT context */
439         memset(&ctx, 0, sizeof(ctx));
440
441         /* Find eBPF program has call class or not */
442         check_program_has_call(&ctx, bpf);
443
444         /* First pass to calculate total code size and valid jump offsets */
445         rc = emit(&ctx, bpf);
446         if (rc)
447                 goto finish;
448
449         size = ctx.idx * sizeof(uint32_t);
450         /* Allocate JIT program memory */
451         ctx.ins = mmap(NULL, size, PROT_READ | PROT_WRITE,
452                                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
453         if (ctx.ins == MAP_FAILED) {
454                 rc = -ENOMEM;
455                 goto finish;
456         }
457
458         /* Second pass to generate code */
459         rc = emit(&ctx, bpf);
460         if (rc)
461                 goto munmap;
462
463         rc = mprotect(ctx.ins, size, PROT_READ | PROT_EXEC) != 0;
464         if (rc) {
465                 rc = -errno;
466                 goto munmap;
467         }
468
469         /* Flush the icache */
470         __builtin___clear_cache(ctx.ins, ctx.ins + ctx.idx);
471
472         bpf->jit.func = (void *)ctx.ins;
473         bpf->jit.sz = size;
474
475         goto finish;
476
477 munmap:
478         munmap(ctx.ins, size);
479 finish:
480         return rc;
481 }