bpf/arm: add basic arithmetic operations
[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_mov_hw(bool is64, const uint8_t val)
48 {
49         if (val == 16 || val == 0)
50                 return 0;
51         else if (is64 && val != 64 && val != 48 && val != 32)
52                 return 1;
53
54         return 0;
55 }
56
57 static int
58 check_reg(uint8_t r)
59 {
60         return (r > 31) ? 1 : 0;
61 }
62
63 static int
64 is_first_pass(struct a64_jit_ctx *ctx)
65 {
66         return (ctx->ins == NULL);
67 }
68
69 static int
70 check_invalid_args(struct a64_jit_ctx *ctx, uint32_t limit)
71 {
72         uint32_t idx;
73
74         if (is_first_pass(ctx))
75                 return 0;
76
77         for (idx = 0; idx < limit; idx++) {
78                 if (rte_le_to_cpu_32(ctx->ins[idx]) == A64_INVALID_OP_CODE) {
79                         RTE_BPF_LOG(ERR,
80                                 "%s: invalid opcode at %u;\n", __func__, idx);
81                         return -EINVAL;
82                 }
83         }
84         return 0;
85 }
86
87 /* Emit an instruction */
88 static inline void
89 emit_insn(struct a64_jit_ctx *ctx, uint32_t insn, int error)
90 {
91         if (error)
92                 insn = A64_INVALID_OP_CODE;
93
94         if (ctx->ins)
95                 ctx->ins[ctx->idx] = rte_cpu_to_le_32(insn);
96
97         ctx->idx++;
98 }
99
100 static void
101 emit_ret(struct a64_jit_ctx *ctx)
102 {
103         emit_insn(ctx, 0xd65f03c0, 0);
104 }
105
106 static void
107 emit_add_sub_imm(struct a64_jit_ctx *ctx, bool is64, bool sub, uint8_t rd,
108                  uint8_t rn, int16_t imm12)
109 {
110         uint32_t insn, imm;
111
112         imm = mask_imm(12, imm12);
113         insn = (!!is64) << 31;
114         insn |= (!!sub) << 30;
115         insn |= 0x11000000;
116         insn |= rd;
117         insn |= rn << 5;
118         insn |= imm << 10;
119
120         emit_insn(ctx, insn,
121                   check_reg(rd) || check_reg(rn) || check_imm(12, imm12));
122 }
123
124 static void
125 emit_add_imm_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn, uint16_t imm12)
126 {
127         emit_add_sub_imm(ctx, 1, 0, rd, rn, imm12);
128 }
129
130 static void
131 emit_sub_imm_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn, uint16_t imm12)
132 {
133         emit_add_sub_imm(ctx, 1, 1, rd, rn, imm12);
134 }
135
136 static void
137 emit_mov(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rn)
138 {
139         emit_add_sub_imm(ctx, is64, 0, rd, rn, 0);
140 }
141
142 static void
143 emit_mov_64(struct a64_jit_ctx *ctx, uint8_t rd, uint8_t rn)
144 {
145         emit_mov(ctx, 1, rd, rn);
146 }
147
148 static void
149 emit_ls_pair_64(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2, uint8_t rn,
150                 bool push, bool load, bool pre_index)
151 {
152         uint32_t insn;
153
154         insn = (!!load) << 22;
155         insn |= (!!pre_index) << 24;
156         insn |= 0xa8800000;
157         insn |= rt;
158         insn |= rn << 5;
159         insn |= rt2 << 10;
160         if (push)
161                 insn |= 0x7e << 15; /* 0x7e means -2 with imm7 */
162         else
163                 insn |= 0x2 << 15;
164
165         emit_insn(ctx, insn, check_reg(rn) || check_reg(rt) || check_reg(rt2));
166
167 }
168
169 /* Emit stp rt, rt2, [sp, #-16]! */
170 static void
171 emit_stack_push(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2)
172 {
173         emit_ls_pair_64(ctx, rt, rt2, A64_SP, 1, 0, 1);
174 }
175
176 /* Emit ldp rt, rt2, [sp, #16] */
177 static void
178 emit_stack_pop(struct a64_jit_ctx *ctx, uint8_t rt, uint8_t rt2)
179 {
180         emit_ls_pair_64(ctx, rt, rt2, A64_SP, 0, 1, 0);
181 }
182
183 #define A64_MOVN 0
184 #define A64_MOVZ 2
185 #define A64_MOVK 3
186 static void
187 mov_imm(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t type,
188         uint16_t imm16, uint8_t shift)
189 {
190         uint32_t insn;
191
192         insn = (!!is64) << 31;
193         insn |= type << 29;
194         insn |= 0x25 << 23;
195         insn |= (shift/16) << 21;
196         insn |= imm16 << 5;
197         insn |= rd;
198
199         emit_insn(ctx, insn, check_reg(rd) || check_mov_hw(is64, shift));
200 }
201
202 static void
203 emit_mov_imm32(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint32_t val)
204 {
205         uint16_t upper = val >> 16;
206         uint16_t lower = val & 0xffff;
207
208         /* Positive number */
209         if ((val & 1UL << 31) == 0) {
210                 mov_imm(ctx, is64, rd, A64_MOVZ, lower, 0);
211                 if (upper)
212                         mov_imm(ctx, is64, rd, A64_MOVK, upper, 16);
213         } else { /* Negative number */
214                 if (upper == 0xffff) {
215                         mov_imm(ctx, is64, rd, A64_MOVN, ~lower, 0);
216                 } else {
217                         mov_imm(ctx, is64, rd, A64_MOVN, ~upper, 16);
218                         if (lower != 0xffff)
219                                 mov_imm(ctx, is64, rd, A64_MOVK, lower, 0);
220                 }
221         }
222 }
223
224 static int
225 u16_blocks_weight(const uint64_t val, bool one)
226 {
227         return (((val >>  0) & 0xffff) == (one ? 0xffff : 0x0000)) +
228                (((val >> 16) & 0xffff) == (one ? 0xffff : 0x0000)) +
229                (((val >> 32) & 0xffff) == (one ? 0xffff : 0x0000)) +
230                (((val >> 48) & 0xffff) == (one ? 0xffff : 0x0000));
231 }
232
233 static void
234 emit_mov_imm(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint64_t val)
235 {
236         uint64_t nval = ~val;
237         int movn, sr;
238
239         if (is64 == 0)
240                 return emit_mov_imm32(ctx, 0, rd, (uint32_t)(val & 0xffffffff));
241
242         /* Find MOVN or MOVZ first */
243         movn = u16_blocks_weight(val, true) > u16_blocks_weight(val, false);
244         /* Find shift right value */
245         sr = movn ? rte_fls_u64(nval) - 1 : rte_fls_u64(val) - 1;
246         sr = RTE_ALIGN_FLOOR(sr, 16);
247         sr = RTE_MAX(sr, 0);
248
249         if (movn)
250                 mov_imm(ctx, 1, rd, A64_MOVN, (nval >> sr) & 0xffff, sr);
251         else
252                 mov_imm(ctx, 1, rd, A64_MOVZ, (val >> sr) & 0xffff, sr);
253
254         sr -= 16;
255         while (sr >= 0) {
256                 if (((val >> sr) & 0xffff) != (movn ? 0xffff : 0x0000))
257                         mov_imm(ctx, 1, rd, A64_MOVK, (val >> sr) & 0xffff, sr);
258                 sr -= 16;
259         }
260 }
261
262 #define A64_ADD 0x58
263 #define A64_SUB 0x258
264 static void
265 emit_add_sub(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rn,
266              uint8_t rm, uint16_t op)
267 {
268         uint32_t insn;
269
270         insn = (!!is64) << 31;
271         insn |= op << 21; /* shift == 0 */
272         insn |= rm << 16;
273         insn |= rn << 5;
274         insn |= rd;
275
276         emit_insn(ctx, insn, check_reg(rd) || check_reg(rm));
277 }
278
279 static void
280 emit_add(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rm)
281 {
282         emit_add_sub(ctx, is64, rd, rd, rm, A64_ADD);
283 }
284
285 static void
286 emit_sub(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rm)
287 {
288         emit_add_sub(ctx, is64, rd, rd, rm, A64_SUB);
289 }
290
291 static void
292 emit_mul(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rm)
293 {
294         uint32_t insn;
295
296         insn = (!!is64) << 31;
297         insn |= 0xd8 << 21;
298         insn |= rm << 16;
299         insn |= A64_ZR << 10;
300         insn |= rd << 5;
301         insn |= rd;
302
303         emit_insn(ctx, insn, check_reg(rd) || check_reg(rm));
304 }
305
306 #define A64_UDIV 0x2
307 static void
308 emit_data_process_two_src(struct a64_jit_ctx *ctx, bool is64, uint8_t rd,
309                           uint8_t rn, uint8_t rm, uint16_t op)
310
311 {
312         uint32_t insn;
313
314         insn = (!!is64) << 31;
315         insn |= 0xd6 << 21;
316         insn |= rm << 16;
317         insn |= op << 10;
318         insn |= rn << 5;
319         insn |= rd;
320
321         emit_insn(ctx, insn, check_reg(rd) || check_reg(rm));
322 }
323
324 static void
325 emit_div(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rm)
326 {
327         emit_data_process_two_src(ctx, is64, rd, rd, rm, A64_UDIV);
328 }
329
330 static void
331 emit_msub(struct a64_jit_ctx *ctx, bool is64, uint8_t rd, uint8_t rn,
332           uint8_t rm, uint8_t ra)
333 {
334         uint32_t insn;
335
336         insn = (!!is64) << 31;
337         insn |= 0xd8 << 21;
338         insn |= rm << 16;
339         insn |= 0x1 << 15;
340         insn |= ra << 10;
341         insn |= rn << 5;
342         insn |= rd;
343
344         emit_insn(ctx, insn, check_reg(rd) || check_reg(rn) || check_reg(rm) ||
345                   check_reg(ra));
346 }
347
348 static void
349 emit_mod(struct a64_jit_ctx *ctx, bool is64, uint8_t tmp, uint8_t rd,
350          uint8_t rm)
351 {
352         emit_data_process_two_src(ctx, is64, tmp, rd, rm, A64_UDIV);
353         emit_msub(ctx, is64, rd, tmp, rm, rd);
354 }
355
356 static uint8_t
357 ebpf_to_a64_reg(struct a64_jit_ctx *ctx, uint8_t reg)
358 {
359         const uint32_t ebpf2a64_has_call[] = {
360                 /* Map A64 R7 register as EBPF return register */
361                 [EBPF_REG_0] = A64_R(7),
362                 /* Map A64 arguments register as EBPF arguments register */
363                 [EBPF_REG_1] = A64_R(0),
364                 [EBPF_REG_2] = A64_R(1),
365                 [EBPF_REG_3] = A64_R(2),
366                 [EBPF_REG_4] = A64_R(3),
367                 [EBPF_REG_5] = A64_R(4),
368                 /* Map A64 callee save register as EBPF callee save register */
369                 [EBPF_REG_6] = A64_R(19),
370                 [EBPF_REG_7] = A64_R(20),
371                 [EBPF_REG_8] = A64_R(21),
372                 [EBPF_REG_9] = A64_R(22),
373                 [EBPF_FP]    = A64_R(25),
374                 /* Map A64 scratch registers as temporary storage */
375                 [TMP_REG_1] = A64_R(9),
376                 [TMP_REG_2] = A64_R(10),
377                 [TMP_REG_3] = A64_R(11),
378         };
379
380         const uint32_t ebpf2a64_no_call[] = {
381                 /* Map A64 R7 register as EBPF return register */
382                 [EBPF_REG_0] = A64_R(7),
383                 /* Map A64 arguments register as EBPF arguments register */
384                 [EBPF_REG_1] = A64_R(0),
385                 [EBPF_REG_2] = A64_R(1),
386                 [EBPF_REG_3] = A64_R(2),
387                 [EBPF_REG_4] = A64_R(3),
388                 [EBPF_REG_5] = A64_R(4),
389                 /*
390                  * EBPF program does not have EBPF_CALL op code,
391                  * Map A64 scratch registers as EBPF callee save registers.
392                  */
393                 [EBPF_REG_6] = A64_R(9),
394                 [EBPF_REG_7] = A64_R(10),
395                 [EBPF_REG_8] = A64_R(11),
396                 [EBPF_REG_9] = A64_R(12),
397                 /* Map A64 FP register as EBPF FP register */
398                 [EBPF_FP]    = A64_FP,
399                 /* Map remaining A64 scratch registers as temporary storage */
400                 [TMP_REG_1] = A64_R(13),
401                 [TMP_REG_2] = A64_R(14),
402                 [TMP_REG_3] = A64_R(15),
403         };
404
405         if (ctx->foundcall)
406                 return ebpf2a64_has_call[reg];
407         else
408                 return ebpf2a64_no_call[reg];
409 }
410
411 /*
412  * Procedure call standard for the arm64
413  * -------------------------------------
414  * R0..R7  - Parameter/result registers
415  * R8      - Indirect result location register
416  * R9..R15 - Scratch registers
417  * R15     - Platform Register
418  * R16     - First intra-procedure-call scratch register
419  * R17     - Second intra-procedure-call temporary register
420  * R19-R28 - Callee saved registers
421  * R29     - Frame pointer
422  * R30     - Link register
423  * R31     - Stack pointer
424  */
425 static void
426 emit_prologue_has_call(struct a64_jit_ctx *ctx)
427 {
428         uint8_t r6, r7, r8, r9, fp;
429
430         r6 = ebpf_to_a64_reg(ctx, EBPF_REG_6);
431         r7 = ebpf_to_a64_reg(ctx, EBPF_REG_7);
432         r8 = ebpf_to_a64_reg(ctx, EBPF_REG_8);
433         r9 = ebpf_to_a64_reg(ctx, EBPF_REG_9);
434         fp = ebpf_to_a64_reg(ctx, EBPF_FP);
435
436         /*
437          * eBPF prog stack layout
438          *
439          *                               high
440          *       eBPF prologue       0:+-----+ <= original A64_SP
441          *                             |FP/LR|
442          *                         -16:+-----+ <= current A64_FP
443          *    Callee saved registers   | ... |
444          *             EBPF_FP =>  -64:+-----+
445          *                             |     |
446          *       eBPF prog stack       | ... |
447          *                             |     |
448          * (EBPF_FP - bpf->stack_sz)=> +-----+
449          * Pad for A64_SP 16B alignment| PAD |
450          * (EBPF_FP - ctx->stack_sz)=> +-----+ <= current A64_SP
451          *                             |     |
452          *                             | ... | Function call stack
453          *                             |     |
454          *                             +-----+
455          *                              low
456          */
457         emit_stack_push(ctx, A64_FP, A64_LR);
458         emit_mov_64(ctx, A64_FP, A64_SP);
459         emit_stack_push(ctx, r6, r7);
460         emit_stack_push(ctx, r8, r9);
461         /*
462          * There is no requirement to save A64_R(28) in stack. Doing it here,
463          * because, A64_SP needs be to 16B aligned and STR vs STP
464          * takes same number of cycles(typically).
465          */
466         emit_stack_push(ctx, fp, A64_R(28));
467         emit_mov_64(ctx, fp, A64_SP);
468         if (ctx->stack_sz)
469                 emit_sub_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
470 }
471
472 static void
473 emit_epilogue_has_call(struct a64_jit_ctx *ctx)
474 {
475         uint8_t r6, r7, r8, r9, fp, r0;
476
477         r6 = ebpf_to_a64_reg(ctx, EBPF_REG_6);
478         r7 = ebpf_to_a64_reg(ctx, EBPF_REG_7);
479         r8 = ebpf_to_a64_reg(ctx, EBPF_REG_8);
480         r9 = ebpf_to_a64_reg(ctx, EBPF_REG_9);
481         fp = ebpf_to_a64_reg(ctx, EBPF_FP);
482         r0 = ebpf_to_a64_reg(ctx, EBPF_REG_0);
483
484         if (ctx->stack_sz)
485                 emit_add_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
486         emit_stack_pop(ctx, fp, A64_R(28));
487         emit_stack_pop(ctx, r8, r9);
488         emit_stack_pop(ctx, r6, r7);
489         emit_stack_pop(ctx, A64_FP, A64_LR);
490         emit_mov_64(ctx, A64_R(0), r0);
491         emit_ret(ctx);
492 }
493
494 static void
495 emit_prologue_no_call(struct a64_jit_ctx *ctx)
496 {
497         /*
498          * eBPF prog stack layout without EBPF_CALL opcode
499          *
500          *                               high
501          *    eBPF prologue(EBPF_FP) 0:+-----+ <= original A64_SP/current A64_FP
502          *                             |     |
503          *                             | ... |
504          *            eBPF prog stack  |     |
505          *                             |     |
506          * (EBPF_FP - bpf->stack_sz)=> +-----+
507          * Pad for A64_SP 16B alignment| PAD |
508          * (EBPF_FP - ctx->stack_sz)=> +-----+ <= current A64_SP
509          *                             |     |
510          *                             | ... | Function call stack
511          *                             |     |
512          *                             +-----+
513          *                              low
514          */
515         if (ctx->stack_sz) {
516                 emit_mov_64(ctx, A64_FP, A64_SP);
517                 emit_sub_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
518         }
519 }
520
521 static void
522 emit_epilogue_no_call(struct a64_jit_ctx *ctx)
523 {
524         if (ctx->stack_sz)
525                 emit_add_imm_64(ctx, A64_SP, A64_SP, ctx->stack_sz);
526         emit_mov_64(ctx, A64_R(0), ebpf_to_a64_reg(ctx, EBPF_REG_0));
527         emit_ret(ctx);
528 }
529
530 static void
531 emit_prologue(struct a64_jit_ctx *ctx)
532 {
533         if (ctx->foundcall)
534                 emit_prologue_has_call(ctx);
535         else
536                 emit_prologue_no_call(ctx);
537
538         ctx->program_start = ctx->idx;
539 }
540
541 static void
542 emit_epilogue(struct a64_jit_ctx *ctx)
543 {
544         ctx->program_sz = ctx->idx - ctx->program_start;
545
546         if (ctx->foundcall)
547                 emit_epilogue_has_call(ctx);
548         else
549                 emit_epilogue_no_call(ctx);
550 }
551
552 static void
553 emit_cbnz(struct a64_jit_ctx *ctx, bool is64, uint8_t rt, int32_t imm19)
554 {
555         uint32_t insn, imm;
556
557         imm = mask_imm(19, imm19);
558         insn = (!!is64) << 31;
559         insn |= 0x35 << 24;
560         insn |= imm << 5;
561         insn |= rt;
562
563         emit_insn(ctx, insn, check_reg(rt) || check_imm(19, imm19));
564 }
565
566 static void
567 emit_b(struct a64_jit_ctx *ctx, int32_t imm26)
568 {
569         uint32_t insn, imm;
570
571         imm = mask_imm(26, imm26);
572         insn = 0x5 << 26;
573         insn |= imm;
574
575         emit_insn(ctx, insn, check_imm(26, imm26));
576 }
577
578 static void
579 emit_return_zero_if_src_zero(struct a64_jit_ctx *ctx, bool is64, uint8_t src)
580 {
581         uint8_t r0 = ebpf_to_a64_reg(ctx, EBPF_REG_0);
582         uint16_t jump_to_epilogue;
583
584         emit_cbnz(ctx, is64, src, 3);
585         emit_mov_imm(ctx, is64, r0, 0);
586         jump_to_epilogue = (ctx->program_start + ctx->program_sz) - ctx->idx;
587         emit_b(ctx, jump_to_epilogue);
588 }
589
590 static void
591 check_program_has_call(struct a64_jit_ctx *ctx, struct rte_bpf *bpf)
592 {
593         const struct ebpf_insn *ins;
594         uint8_t op;
595         uint32_t i;
596
597         for (i = 0; i != bpf->prm.nb_ins; i++) {
598                 ins = bpf->prm.ins + i;
599                 op = ins->code;
600
601                 switch (op) {
602                 /* Call imm */
603                 case (BPF_JMP | EBPF_CALL):
604                         ctx->foundcall = 1;
605                         return;
606                 }
607         }
608 }
609
610 /*
611  * Walk through eBPF code and translate them to arm64 one.
612  */
613 static int
614 emit(struct a64_jit_ctx *ctx, struct rte_bpf *bpf)
615 {
616         uint8_t op, dst, src, tmp1, tmp2;
617         const struct ebpf_insn *ins;
618         int32_t imm;
619         uint32_t i;
620         bool is64;
621         int rc;
622
623         /* Reset context fields */
624         ctx->idx = 0;
625         /* arm64 SP must be aligned to 16 */
626         ctx->stack_sz = RTE_ALIGN_MUL_CEIL(bpf->stack_sz, 16);
627         tmp1 = ebpf_to_a64_reg(ctx, TMP_REG_1);
628         tmp2 = ebpf_to_a64_reg(ctx, TMP_REG_2);
629
630         emit_prologue(ctx);
631
632         for (i = 0; i != bpf->prm.nb_ins; i++) {
633
634                 ins = bpf->prm.ins + i;
635                 op = ins->code;
636                 imm = ins->imm;
637
638                 dst = ebpf_to_a64_reg(ctx, ins->dst_reg);
639                 src = ebpf_to_a64_reg(ctx, ins->src_reg);
640                 is64 = (BPF_CLASS(op) == EBPF_ALU64);
641
642                 switch (op) {
643                 /* dst = src */
644                 case (BPF_ALU | EBPF_MOV | BPF_X):
645                 case (EBPF_ALU64 | EBPF_MOV | BPF_X):
646                         emit_mov(ctx, is64, dst, src);
647                         break;
648                 /* dst = imm */
649                 case (BPF_ALU | EBPF_MOV | BPF_K):
650                 case (EBPF_ALU64 | EBPF_MOV | BPF_K):
651                         emit_mov_imm(ctx, is64, dst, imm);
652                         break;
653                 /* dst += src */
654                 case (BPF_ALU | BPF_ADD | BPF_X):
655                 case (EBPF_ALU64 | BPF_ADD | BPF_X):
656                         emit_add(ctx, is64, dst, src);
657                         break;
658                 /* dst += imm */
659                 case (BPF_ALU | BPF_ADD | BPF_K):
660                 case (EBPF_ALU64 | BPF_ADD | BPF_K):
661                         emit_mov_imm(ctx, is64, tmp1, imm);
662                         emit_add(ctx, is64, dst, tmp1);
663                         break;
664                 /* dst -= src */
665                 case (BPF_ALU | BPF_SUB | BPF_X):
666                 case (EBPF_ALU64 | BPF_SUB | BPF_X):
667                         emit_sub(ctx, is64, dst, src);
668                         break;
669                 /* dst -= imm */
670                 case (BPF_ALU | BPF_SUB | BPF_K):
671                 case (EBPF_ALU64 | BPF_SUB | BPF_K):
672                         emit_mov_imm(ctx, is64, tmp1, imm);
673                         emit_sub(ctx, is64, dst, tmp1);
674                         break;
675                 /* dst *= src */
676                 case (BPF_ALU | BPF_MUL | BPF_X):
677                 case (EBPF_ALU64 | BPF_MUL | BPF_X):
678                         emit_mul(ctx, is64, dst, src);
679                         break;
680                 /* dst *= imm */
681                 case (BPF_ALU | BPF_MUL | BPF_K):
682                 case (EBPF_ALU64 | BPF_MUL | BPF_K):
683                         emit_mov_imm(ctx, is64, tmp1, imm);
684                         emit_mul(ctx, is64, dst, tmp1);
685                         break;
686                 /* dst /= src */
687                 case (BPF_ALU | BPF_DIV | BPF_X):
688                 case (EBPF_ALU64 | BPF_DIV | BPF_X):
689                         emit_return_zero_if_src_zero(ctx, is64, src);
690                         emit_div(ctx, is64, dst, src);
691                         break;
692                 /* dst /= imm */
693                 case (BPF_ALU | BPF_DIV | BPF_K):
694                 case (EBPF_ALU64 | BPF_DIV | BPF_K):
695                         emit_mov_imm(ctx, is64, tmp1, imm);
696                         emit_div(ctx, is64, dst, tmp1);
697                         break;
698                 /* dst %= src */
699                 case (BPF_ALU | BPF_MOD | BPF_X):
700                 case (EBPF_ALU64 | BPF_MOD | BPF_X):
701                         emit_return_zero_if_src_zero(ctx, is64, src);
702                         emit_mod(ctx, is64, tmp1, dst, src);
703                         break;
704                 /* dst %= imm */
705                 case (BPF_ALU | BPF_MOD | BPF_K):
706                 case (EBPF_ALU64 | BPF_MOD | BPF_K):
707                         emit_mov_imm(ctx, is64, tmp1, imm);
708                         emit_mod(ctx, is64, tmp2, dst, tmp1);
709                         break;
710                 /* Return r0 */
711                 case (BPF_JMP | EBPF_EXIT):
712                         emit_epilogue(ctx);
713                         break;
714                 default:
715                         RTE_BPF_LOG(ERR,
716                                 "%s(%p): invalid opcode %#x at pc: %u;\n",
717                                 __func__, bpf, ins->code, i);
718                         return -EINVAL;
719                 }
720         }
721         rc = check_invalid_args(ctx, ctx->idx);
722
723         return rc;
724 }
725
726 /*
727  * Produce a native ISA version of the given BPF code.
728  */
729 int
730 bpf_jit_arm64(struct rte_bpf *bpf)
731 {
732         struct a64_jit_ctx ctx;
733         size_t size;
734         int rc;
735
736         /* Init JIT context */
737         memset(&ctx, 0, sizeof(ctx));
738
739         /* Find eBPF program has call class or not */
740         check_program_has_call(&ctx, bpf);
741
742         /* First pass to calculate total code size and valid jump offsets */
743         rc = emit(&ctx, bpf);
744         if (rc)
745                 goto finish;
746
747         size = ctx.idx * sizeof(uint32_t);
748         /* Allocate JIT program memory */
749         ctx.ins = mmap(NULL, size, PROT_READ | PROT_WRITE,
750                                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
751         if (ctx.ins == MAP_FAILED) {
752                 rc = -ENOMEM;
753                 goto finish;
754         }
755
756         /* Second pass to generate code */
757         rc = emit(&ctx, bpf);
758         if (rc)
759                 goto munmap;
760
761         rc = mprotect(ctx.ins, size, PROT_READ | PROT_EXEC) != 0;
762         if (rc) {
763                 rc = -errno;
764                 goto munmap;
765         }
766
767         /* Flush the icache */
768         __builtin___clear_cache(ctx.ins, ctx.ins + ctx.idx);
769
770         bpf->jit.func = (void *)ctx.ins;
771         bpf->jit.sz = size;
772
773         goto finish;
774
775 munmap:
776         munmap(ctx.ins, size);
777 finish:
778         return rc;
779 }