GNU Linux-libre 4.9.284-gnu1
[releases.git] / arch / powerpc / net / bpf_jit_comp64.c
1 /*
2  * bpf_jit_comp64.c: eBPF JIT compiler
3  *
4  * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
5  *                IBM Corporation
6  *
7  * Based on the powerpc classic BPF JIT compiler by Matt Evans
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2
12  * of the License.
13  */
14 #include <linux/moduleloader.h>
15 #include <asm/cacheflush.h>
16 #include <linux/netdevice.h>
17 #include <linux/filter.h>
18 #include <linux/if_vlan.h>
19 #include <asm/kprobes.h>
20 #include <linux/bpf.h>
21
22 #include "bpf_jit64.h"
23
24 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
25 {
26         int *p = area;
27
28         /* Fill whole space with trap instructions */
29         while (p < (int *)((char *)area + size))
30                 *p++ = BREAKPOINT_INSTRUCTION;
31 }
32
33 static inline void bpf_flush_icache(void *start, void *end)
34 {
35         smp_wmb();
36         flush_icache_range((unsigned long)start, (unsigned long)end);
37 }
38
39 static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
40 {
41         return (ctx->seen & (1 << (31 - b2p[i])));
42 }
43
44 static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
45 {
46         ctx->seen |= (1 << (31 - b2p[i]));
47 }
48
49 static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
50 {
51         /*
52          * We only need a stack frame if:
53          * - we call other functions (kernel helpers), or
54          * - the bpf program uses its stack area
55          * The latter condition is deduced from the usage of BPF_REG_FP
56          */
57         return ctx->seen & SEEN_FUNC || bpf_is_seen_register(ctx, BPF_REG_FP);
58 }
59
60 /*
61  * When not setting up our own stackframe, the redzone usage is:
62  *
63  *              [       prev sp         ] <-------------
64  *              [         ...           ]               |
65  * sp (r1) ---> [    stack pointer      ] --------------
66  *              [   nv gpr save area    ] 8*8
67  *              [    tail_call_cnt      ] 8
68  *              [    local_tmp_var      ] 8
69  *              [   unused red zone     ] 208 bytes protected
70  */
71 static int bpf_jit_stack_local(struct codegen_context *ctx)
72 {
73         if (bpf_has_stack_frame(ctx))
74                 return STACK_FRAME_MIN_SIZE + MAX_BPF_STACK;
75         else
76                 return -(BPF_PPC_STACK_SAVE + 16);
77 }
78
79 static int bpf_jit_stack_tailcallcnt(struct codegen_context *ctx)
80 {
81         return bpf_jit_stack_local(ctx) + 8;
82 }
83
84 static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
85 {
86         if (reg >= BPF_PPC_NVR_MIN && reg < 32)
87                 return (bpf_has_stack_frame(ctx) ? BPF_PPC_STACKFRAME : 0)
88                                                         - (8 * (32 - reg));
89
90         pr_err("BPF JIT is asking about unknown registers");
91         BUG();
92 }
93
94 static void bpf_jit_emit_skb_loads(u32 *image, struct codegen_context *ctx)
95 {
96         /*
97          * Load skb->len and skb->data_len
98          * r3 points to skb
99          */
100         PPC_LWZ(b2p[SKB_HLEN_REG], 3, offsetof(struct sk_buff, len));
101         PPC_LWZ(b2p[TMP_REG_1], 3, offsetof(struct sk_buff, data_len));
102         /* header_len = len - data_len */
103         PPC_SUB(b2p[SKB_HLEN_REG], b2p[SKB_HLEN_REG], b2p[TMP_REG_1]);
104
105         /* skb->data pointer */
106         PPC_BPF_LL(b2p[SKB_DATA_REG], 3, offsetof(struct sk_buff, data));
107 }
108
109 static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
110 {
111         int i;
112
113         /*
114          * Initialize tail_call_cnt if we do tail calls.
115          * Otherwise, put in NOPs so that it can be skipped when we are
116          * invoked through a tail call.
117          */
118         if (ctx->seen & SEEN_TAILCALL) {
119                 PPC_LI(b2p[TMP_REG_1], 0);
120                 /* this goes in the redzone */
121                 PPC_BPF_STL(b2p[TMP_REG_1], 1, -(BPF_PPC_STACK_SAVE + 8));
122         } else {
123                 PPC_NOP();
124                 PPC_NOP();
125         }
126
127 #define BPF_TAILCALL_PROLOGUE_SIZE      8
128
129         if (bpf_has_stack_frame(ctx)) {
130                 /*
131                  * We need a stack frame, but we don't necessarily need to
132                  * save/restore LR unless we call other functions
133                  */
134                 if (ctx->seen & SEEN_FUNC) {
135                         EMIT(PPC_INST_MFLR | __PPC_RT(R0));
136                         PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
137                 }
138
139                 PPC_BPF_STLU(1, 1, -BPF_PPC_STACKFRAME);
140         }
141
142         /*
143          * Back up non-volatile regs -- BPF registers 6-10
144          * If we haven't created our own stack frame, we save these
145          * in the protected zone below the previous stack frame
146          */
147         for (i = BPF_REG_6; i <= BPF_REG_10; i++)
148                 if (bpf_is_seen_register(ctx, i))
149                         PPC_BPF_STL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
150
151         /*
152          * Save additional non-volatile regs if we cache skb
153          * Also, setup skb data
154          */
155         if (ctx->seen & SEEN_SKB) {
156                 PPC_BPF_STL(b2p[SKB_HLEN_REG], 1,
157                                 bpf_jit_stack_offsetof(ctx, b2p[SKB_HLEN_REG]));
158                 PPC_BPF_STL(b2p[SKB_DATA_REG], 1,
159                                 bpf_jit_stack_offsetof(ctx, b2p[SKB_DATA_REG]));
160                 bpf_jit_emit_skb_loads(image, ctx);
161         }
162
163         /* Setup frame pointer to point to the bpf stack area */
164         if (bpf_is_seen_register(ctx, BPF_REG_FP))
165                 PPC_ADDI(b2p[BPF_REG_FP], 1,
166                                 STACK_FRAME_MIN_SIZE + MAX_BPF_STACK);
167 }
168
169 static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
170 {
171         int i;
172
173         /* Restore NVRs */
174         for (i = BPF_REG_6; i <= BPF_REG_10; i++)
175                 if (bpf_is_seen_register(ctx, i))
176                         PPC_BPF_LL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
177
178         /* Restore non-volatile registers used for skb cache */
179         if (ctx->seen & SEEN_SKB) {
180                 PPC_BPF_LL(b2p[SKB_HLEN_REG], 1,
181                                 bpf_jit_stack_offsetof(ctx, b2p[SKB_HLEN_REG]));
182                 PPC_BPF_LL(b2p[SKB_DATA_REG], 1,
183                                 bpf_jit_stack_offsetof(ctx, b2p[SKB_DATA_REG]));
184         }
185
186         /* Tear down our stack frame */
187         if (bpf_has_stack_frame(ctx)) {
188                 PPC_ADDI(1, 1, BPF_PPC_STACKFRAME);
189                 if (ctx->seen & SEEN_FUNC) {
190                         PPC_BPF_LL(0, 1, PPC_LR_STKOFF);
191                         PPC_MTLR(0);
192                 }
193         }
194 }
195
196 static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
197 {
198         bpf_jit_emit_common_epilogue(image, ctx);
199
200         /* Move result to r3 */
201         PPC_MR(3, b2p[BPF_REG_0]);
202
203         PPC_BLR();
204 }
205
206 static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64 func)
207 {
208         unsigned int i, ctx_idx = ctx->idx;
209
210         /* Load function address into r12 */
211         PPC_LI64(12, func);
212
213         /* For bpf-to-bpf function calls, the callee's address is unknown
214          * until the last extra pass. As seen above, we use PPC_LI64() to
215          * load the callee's address, but this may optimize the number of
216          * instructions required based on the nature of the address.
217          *
218          * Since we don't want the number of instructions emitted to change,
219          * we pad the optimized PPC_LI64() call with NOPs to guarantee that
220          * we always have a five-instruction sequence, which is the maximum
221          * that PPC_LI64() can emit.
222          */
223         for (i = ctx->idx - ctx_idx; i < 5; i++)
224                 PPC_NOP();
225
226 #ifdef PPC64_ELF_ABI_v1
227         /*
228          * Load TOC from function descriptor at offset 8.
229          * We can clobber r2 since we get called through a
230          * function pointer (so caller will save/restore r2)
231          * and since we don't use a TOC ourself.
232          */
233         PPC_BPF_LL(2, 12, 8);
234         /* Load actual entry point from function descriptor */
235         PPC_BPF_LL(12, 12, 0);
236 #endif
237
238         PPC_MTLR(12);
239         PPC_BLRL();
240 }
241
242 static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
243 {
244         /*
245          * By now, the eBPF program has already setup parameters in r3, r4 and r5
246          * r3/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
247          * r4/BPF_REG_2 - pointer to bpf_array
248          * r5/BPF_REG_3 - index in bpf_array
249          */
250         int b2p_bpf_array = b2p[BPF_REG_2];
251         int b2p_index = b2p[BPF_REG_3];
252
253         /*
254          * if (index >= array->map.max_entries)
255          *   goto out;
256          */
257         PPC_LWZ(b2p[TMP_REG_1], b2p_bpf_array, offsetof(struct bpf_array, map.max_entries));
258         PPC_RLWINM(b2p_index, b2p_index, 0, 0, 31);
259         PPC_CMPLW(b2p_index, b2p[TMP_REG_1]);
260         PPC_BCC(COND_GE, out);
261
262         /*
263          * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
264          *   goto out;
265          */
266         PPC_BPF_LL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
267         PPC_CMPLWI(b2p[TMP_REG_1], MAX_TAIL_CALL_CNT);
268         PPC_BCC(COND_GT, out);
269
270         /*
271          * tail_call_cnt++;
272          */
273         PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], 1);
274         PPC_BPF_STL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
275
276         /* prog = array->ptrs[index]; */
277         PPC_MULI(b2p[TMP_REG_1], b2p_index, 8);
278         PPC_ADD(b2p[TMP_REG_1], b2p[TMP_REG_1], b2p_bpf_array);
279         PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_array, ptrs));
280
281         /*
282          * if (prog == NULL)
283          *   goto out;
284          */
285         PPC_CMPLDI(b2p[TMP_REG_1], 0);
286         PPC_BCC(COND_EQ, out);
287
288         /* goto *(prog->bpf_func + prologue_size); */
289         PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_prog, bpf_func));
290 #ifdef PPC64_ELF_ABI_v1
291         /* skip past the function descriptor */
292         PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1],
293                         FUNCTION_DESCR_SIZE + BPF_TAILCALL_PROLOGUE_SIZE);
294 #else
295         PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], BPF_TAILCALL_PROLOGUE_SIZE);
296 #endif
297         PPC_MTCTR(b2p[TMP_REG_1]);
298
299         /* tear down stack, restore NVRs, ... */
300         bpf_jit_emit_common_epilogue(image, ctx);
301
302         PPC_BCTR();
303         /* out: */
304 }
305
306 /* Assemble the body code between the prologue & epilogue */
307 static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
308                               struct codegen_context *ctx,
309                               u32 *addrs)
310 {
311         const struct bpf_insn *insn = fp->insnsi;
312         int flen = fp->len;
313         int i;
314
315         /* Start of epilogue code - will only be valid 2nd pass onwards */
316         u32 exit_addr = addrs[flen];
317
318         for (i = 0; i < flen; i++) {
319                 u32 code = insn[i].code;
320                 u32 dst_reg = b2p[insn[i].dst_reg];
321                 u32 src_reg = b2p[insn[i].src_reg];
322                 s16 off = insn[i].off;
323                 s32 imm = insn[i].imm;
324                 u64 imm64;
325                 u8 *func;
326                 u32 true_cond;
327                 u32 tmp_idx;
328
329                 /*
330                  * addrs[] maps a BPF bytecode address into a real offset from
331                  * the start of the body code.
332                  */
333                 addrs[i] = ctx->idx * 4;
334
335                 /*
336                  * As an optimization, we note down which non-volatile registers
337                  * are used so that we can only save/restore those in our
338                  * prologue and epilogue. We do this here regardless of whether
339                  * the actual BPF instruction uses src/dst registers or not
340                  * (for instance, BPF_CALL does not use them). The expectation
341                  * is that those instructions will have src_reg/dst_reg set to
342                  * 0. Even otherwise, we just lose some prologue/epilogue
343                  * optimization but everything else should work without
344                  * any issues.
345                  */
346                 if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
347                         bpf_set_seen_register(ctx, insn[i].dst_reg);
348                 if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
349                         bpf_set_seen_register(ctx, insn[i].src_reg);
350
351                 switch (code) {
352                 /*
353                  * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
354                  */
355                 case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
356                 case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
357                         PPC_ADD(dst_reg, dst_reg, src_reg);
358                         goto bpf_alu32_trunc;
359                 case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
360                 case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
361                         PPC_SUB(dst_reg, dst_reg, src_reg);
362                         goto bpf_alu32_trunc;
363                 case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
364                 case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
365                 case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
366                 case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
367                         if (BPF_OP(code) == BPF_SUB)
368                                 imm = -imm;
369                         if (imm) {
370                                 if (imm >= -32768 && imm < 32768)
371                                         PPC_ADDI(dst_reg, dst_reg, IMM_L(imm));
372                                 else {
373                                         PPC_LI32(b2p[TMP_REG_1], imm);
374                                         PPC_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]);
375                                 }
376                         }
377                         goto bpf_alu32_trunc;
378                 case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
379                 case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
380                         if (BPF_CLASS(code) == BPF_ALU)
381                                 PPC_MULW(dst_reg, dst_reg, src_reg);
382                         else
383                                 PPC_MULD(dst_reg, dst_reg, src_reg);
384                         goto bpf_alu32_trunc;
385                 case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
386                 case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
387                         if (imm >= -32768 && imm < 32768)
388                                 PPC_MULI(dst_reg, dst_reg, IMM_L(imm));
389                         else {
390                                 PPC_LI32(b2p[TMP_REG_1], imm);
391                                 if (BPF_CLASS(code) == BPF_ALU)
392                                         PPC_MULW(dst_reg, dst_reg,
393                                                         b2p[TMP_REG_1]);
394                                 else
395                                         PPC_MULD(dst_reg, dst_reg,
396                                                         b2p[TMP_REG_1]);
397                         }
398                         goto bpf_alu32_trunc;
399                 case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
400                 case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
401                         PPC_CMPWI(src_reg, 0);
402                         PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
403                         PPC_LI(b2p[BPF_REG_0], 0);
404                         PPC_JMP(exit_addr);
405                         if (BPF_OP(code) == BPF_MOD) {
406                                 PPC_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg);
407                                 PPC_MULW(b2p[TMP_REG_1], src_reg,
408                                                 b2p[TMP_REG_1]);
409                                 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
410                         } else
411                                 PPC_DIVWU(dst_reg, dst_reg, src_reg);
412                         goto bpf_alu32_trunc;
413                 case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
414                 case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
415                         PPC_CMPDI(src_reg, 0);
416                         PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
417                         PPC_LI(b2p[BPF_REG_0], 0);
418                         PPC_JMP(exit_addr);
419                         if (BPF_OP(code) == BPF_MOD) {
420                                 PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
421                                 PPC_MULD(b2p[TMP_REG_1], src_reg,
422                                                 b2p[TMP_REG_1]);
423                                 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
424                         } else
425                                 PPC_DIVDU(dst_reg, dst_reg, src_reg);
426                         break;
427                 case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
428                 case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
429                 case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
430                 case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
431                         if (imm == 0)
432                                 return -EINVAL;
433                         else if (imm == 1)
434                                 goto bpf_alu32_trunc;
435
436                         PPC_LI32(b2p[TMP_REG_1], imm);
437                         switch (BPF_CLASS(code)) {
438                         case BPF_ALU:
439                                 if (BPF_OP(code) == BPF_MOD) {
440                                         PPC_DIVWU(b2p[TMP_REG_2], dst_reg,
441                                                         b2p[TMP_REG_1]);
442                                         PPC_MULW(b2p[TMP_REG_1],
443                                                         b2p[TMP_REG_1],
444                                                         b2p[TMP_REG_2]);
445                                         PPC_SUB(dst_reg, dst_reg,
446                                                         b2p[TMP_REG_1]);
447                                 } else
448                                         PPC_DIVWU(dst_reg, dst_reg,
449                                                         b2p[TMP_REG_1]);
450                                 break;
451                         case BPF_ALU64:
452                                 if (BPF_OP(code) == BPF_MOD) {
453                                         PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
454                                                         b2p[TMP_REG_1]);
455                                         PPC_MULD(b2p[TMP_REG_1],
456                                                         b2p[TMP_REG_1],
457                                                         b2p[TMP_REG_2]);
458                                         PPC_SUB(dst_reg, dst_reg,
459                                                         b2p[TMP_REG_1]);
460                                 } else
461                                         PPC_DIVDU(dst_reg, dst_reg,
462                                                         b2p[TMP_REG_1]);
463                                 break;
464                         }
465                         goto bpf_alu32_trunc;
466                 case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
467                 case BPF_ALU64 | BPF_NEG: /* dst = -dst */
468                         PPC_NEG(dst_reg, dst_reg);
469                         goto bpf_alu32_trunc;
470
471                 /*
472                  * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
473                  */
474                 case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
475                 case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
476                         PPC_AND(dst_reg, dst_reg, src_reg);
477                         goto bpf_alu32_trunc;
478                 case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
479                 case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
480                         if (!IMM_H(imm))
481                                 PPC_ANDI(dst_reg, dst_reg, IMM_L(imm));
482                         else {
483                                 /* Sign-extended */
484                                 PPC_LI32(b2p[TMP_REG_1], imm);
485                                 PPC_AND(dst_reg, dst_reg, b2p[TMP_REG_1]);
486                         }
487                         goto bpf_alu32_trunc;
488                 case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
489                 case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
490                         PPC_OR(dst_reg, dst_reg, src_reg);
491                         goto bpf_alu32_trunc;
492                 case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
493                 case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
494                         if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
495                                 /* Sign-extended */
496                                 PPC_LI32(b2p[TMP_REG_1], imm);
497                                 PPC_OR(dst_reg, dst_reg, b2p[TMP_REG_1]);
498                         } else {
499                                 if (IMM_L(imm))
500                                         PPC_ORI(dst_reg, dst_reg, IMM_L(imm));
501                                 if (IMM_H(imm))
502                                         PPC_ORIS(dst_reg, dst_reg, IMM_H(imm));
503                         }
504                         goto bpf_alu32_trunc;
505                 case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
506                 case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
507                         PPC_XOR(dst_reg, dst_reg, src_reg);
508                         goto bpf_alu32_trunc;
509                 case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
510                 case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
511                         if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
512                                 /* Sign-extended */
513                                 PPC_LI32(b2p[TMP_REG_1], imm);
514                                 PPC_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]);
515                         } else {
516                                 if (IMM_L(imm))
517                                         PPC_XORI(dst_reg, dst_reg, IMM_L(imm));
518                                 if (IMM_H(imm))
519                                         PPC_XORIS(dst_reg, dst_reg, IMM_H(imm));
520                         }
521                         goto bpf_alu32_trunc;
522                 case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
523                         /* slw clears top 32 bits */
524                         PPC_SLW(dst_reg, dst_reg, src_reg);
525                         break;
526                 case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
527                         PPC_SLD(dst_reg, dst_reg, src_reg);
528                         break;
529                 case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
530                         /* with imm 0, we still need to clear top 32 bits */
531                         PPC_SLWI(dst_reg, dst_reg, imm);
532                         break;
533                 case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
534                         if (imm != 0)
535                                 PPC_SLDI(dst_reg, dst_reg, imm);
536                         break;
537                 case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
538                         PPC_SRW(dst_reg, dst_reg, src_reg);
539                         break;
540                 case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
541                         PPC_SRD(dst_reg, dst_reg, src_reg);
542                         break;
543                 case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
544                         PPC_SRWI(dst_reg, dst_reg, imm);
545                         break;
546                 case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
547                         if (imm != 0)
548                                 PPC_SRDI(dst_reg, dst_reg, imm);
549                         break;
550                 case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
551                         PPC_SRAD(dst_reg, dst_reg, src_reg);
552                         break;
553                 case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
554                         if (imm != 0)
555                                 PPC_SRADI(dst_reg, dst_reg, imm);
556                         break;
557
558                 /*
559                  * MOV
560                  */
561                 case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
562                 case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
563                         PPC_MR(dst_reg, src_reg);
564                         goto bpf_alu32_trunc;
565                 case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
566                 case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
567                         PPC_LI32(dst_reg, imm);
568                         if (imm < 0)
569                                 goto bpf_alu32_trunc;
570                         break;
571
572 bpf_alu32_trunc:
573                 /* Truncate to 32-bits */
574                 if (BPF_CLASS(code) == BPF_ALU)
575                         PPC_RLWINM(dst_reg, dst_reg, 0, 0, 31);
576                 break;
577
578                 /*
579                  * BPF_FROM_BE/LE
580                  */
581                 case BPF_ALU | BPF_END | BPF_FROM_LE:
582                 case BPF_ALU | BPF_END | BPF_FROM_BE:
583 #ifdef __BIG_ENDIAN__
584                         if (BPF_SRC(code) == BPF_FROM_BE)
585                                 goto emit_clear;
586 #else /* !__BIG_ENDIAN__ */
587                         if (BPF_SRC(code) == BPF_FROM_LE)
588                                 goto emit_clear;
589 #endif
590                         switch (imm) {
591                         case 16:
592                                 /* Rotate 8 bits left & mask with 0x0000ff00 */
593                                 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23);
594                                 /* Rotate 8 bits right & insert LSB to reg */
595                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31);
596                                 /* Move result back to dst_reg */
597                                 PPC_MR(dst_reg, b2p[TMP_REG_1]);
598                                 break;
599                         case 32:
600                                 /*
601                                  * Rotate word left by 8 bits:
602                                  * 2 bytes are already in their final position
603                                  * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
604                                  */
605                                 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31);
606                                 /* Rotate 24 bits and insert byte 1 */
607                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7);
608                                 /* Rotate 24 bits and insert byte 3 */
609                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23);
610                                 PPC_MR(dst_reg, b2p[TMP_REG_1]);
611                                 break;
612                         case 64:
613                                 /*
614                                  * Way easier and faster(?) to store the value
615                                  * into stack and then use ldbrx
616                                  *
617                                  * ctx->seen will be reliable in pass2, but
618                                  * the instructions generated will remain the
619                                  * same across all passes
620                                  */
621                                 PPC_BPF_STL(dst_reg, 1, bpf_jit_stack_local(ctx));
622                                 PPC_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx));
623                                 PPC_LDBRX(dst_reg, 0, b2p[TMP_REG_1]);
624                                 break;
625                         }
626                         break;
627
628 emit_clear:
629                         switch (imm) {
630                         case 16:
631                                 /* zero-extend 16 bits into 64 bits */
632                                 PPC_RLDICL(dst_reg, dst_reg, 0, 48);
633                                 break;
634                         case 32:
635                                 /* zero-extend 32 bits into 64 bits */
636                                 PPC_RLDICL(dst_reg, dst_reg, 0, 32);
637                                 break;
638                         case 64:
639                                 /* nop */
640                                 break;
641                         }
642                         break;
643
644                 /*
645                  * BPF_ST(X)
646                  */
647                 case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
648                 case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
649                         if (BPF_CLASS(code) == BPF_ST) {
650                                 PPC_LI(b2p[TMP_REG_1], imm);
651                                 src_reg = b2p[TMP_REG_1];
652                         }
653                         PPC_STB(src_reg, dst_reg, off);
654                         break;
655                 case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
656                 case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
657                         if (BPF_CLASS(code) == BPF_ST) {
658                                 PPC_LI(b2p[TMP_REG_1], imm);
659                                 src_reg = b2p[TMP_REG_1];
660                         }
661                         PPC_STH(src_reg, dst_reg, off);
662                         break;
663                 case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
664                 case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
665                         if (BPF_CLASS(code) == BPF_ST) {
666                                 PPC_LI32(b2p[TMP_REG_1], imm);
667                                 src_reg = b2p[TMP_REG_1];
668                         }
669                         PPC_STW(src_reg, dst_reg, off);
670                         break;
671                 case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
672                 case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
673                         if (BPF_CLASS(code) == BPF_ST) {
674                                 PPC_LI32(b2p[TMP_REG_1], imm);
675                                 src_reg = b2p[TMP_REG_1];
676                         }
677                         PPC_BPF_STL(src_reg, dst_reg, off);
678                         break;
679
680                 /*
681                  * BPF_STX XADD (atomic_add)
682                  */
683                 /* *(u32 *)(dst + off) += src */
684                 case BPF_STX | BPF_XADD | BPF_W:
685                         /* Get EA into TMP_REG_1 */
686                         PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
687                         tmp_idx = ctx->idx * 4;
688                         /* load value from memory into TMP_REG_2 */
689                         PPC_BPF_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
690                         /* add value from src_reg into this */
691                         PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
692                         /* store result back */
693                         PPC_BPF_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
694                         /* we're done if this succeeded */
695                         PPC_BCC_SHORT(COND_NE, tmp_idx);
696                         break;
697                 /* *(u64 *)(dst + off) += src */
698                 case BPF_STX | BPF_XADD | BPF_DW:
699                         PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
700                         tmp_idx = ctx->idx * 4;
701                         PPC_BPF_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
702                         PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
703                         PPC_BPF_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
704                         PPC_BCC_SHORT(COND_NE, tmp_idx);
705                         break;
706
707                 /*
708                  * BPF_LDX
709                  */
710                 /* dst = *(u8 *)(ul) (src + off) */
711                 case BPF_LDX | BPF_MEM | BPF_B:
712                         PPC_LBZ(dst_reg, src_reg, off);
713                         break;
714                 /* dst = *(u16 *)(ul) (src + off) */
715                 case BPF_LDX | BPF_MEM | BPF_H:
716                         PPC_LHZ(dst_reg, src_reg, off);
717                         break;
718                 /* dst = *(u32 *)(ul) (src + off) */
719                 case BPF_LDX | BPF_MEM | BPF_W:
720                         PPC_LWZ(dst_reg, src_reg, off);
721                         break;
722                 /* dst = *(u64 *)(ul) (src + off) */
723                 case BPF_LDX | BPF_MEM | BPF_DW:
724                         PPC_BPF_LL(dst_reg, src_reg, off);
725                         break;
726
727                 /*
728                  * Doubleword load
729                  * 16 byte instruction that uses two 'struct bpf_insn'
730                  */
731                 case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
732                         imm64 = ((u64)(u32) insn[i].imm) |
733                                     (((u64)(u32) insn[i+1].imm) << 32);
734                         /* Adjust for two bpf instructions */
735                         addrs[++i] = ctx->idx * 4;
736                         PPC_LI64(dst_reg, imm64);
737                         break;
738
739                 /*
740                  * Return/Exit
741                  */
742                 case BPF_JMP | BPF_EXIT:
743                         /*
744                          * If this isn't the very last instruction, branch to
745                          * the epilogue. If we _are_ the last instruction,
746                          * we'll just fall through to the epilogue.
747                          */
748                         if (i != flen - 1)
749                                 PPC_JMP(exit_addr);
750                         /* else fall through to the epilogue */
751                         break;
752
753                 /*
754                  * Call kernel helper
755                  */
756                 case BPF_JMP | BPF_CALL:
757                         ctx->seen |= SEEN_FUNC;
758                         func = (u8 *) __bpf_call_base + imm;
759
760                         /* Save skb pointer if we need to re-cache skb data */
761                         if (bpf_helper_changes_skb_data(func))
762                                 PPC_BPF_STL(3, 1, bpf_jit_stack_local(ctx));
763
764                         bpf_jit_emit_func_call(image, ctx, (u64)func);
765
766                         /* move return value from r3 to BPF_REG_0 */
767                         PPC_MR(b2p[BPF_REG_0], 3);
768
769                         /* refresh skb cache */
770                         if (bpf_helper_changes_skb_data(func)) {
771                                 /* reload skb pointer to r3 */
772                                 PPC_BPF_LL(3, 1, bpf_jit_stack_local(ctx));
773                                 bpf_jit_emit_skb_loads(image, ctx);
774                         }
775                         break;
776
777                 /*
778                  * Jumps and branches
779                  */
780                 case BPF_JMP | BPF_JA:
781                         PPC_JMP(addrs[i + 1 + off]);
782                         break;
783
784                 case BPF_JMP | BPF_JGT | BPF_K:
785                 case BPF_JMP | BPF_JGT | BPF_X:
786                 case BPF_JMP | BPF_JSGT | BPF_K:
787                 case BPF_JMP | BPF_JSGT | BPF_X:
788                         true_cond = COND_GT;
789                         goto cond_branch;
790                 case BPF_JMP | BPF_JGE | BPF_K:
791                 case BPF_JMP | BPF_JGE | BPF_X:
792                 case BPF_JMP | BPF_JSGE | BPF_K:
793                 case BPF_JMP | BPF_JSGE | BPF_X:
794                         true_cond = COND_GE;
795                         goto cond_branch;
796                 case BPF_JMP | BPF_JEQ | BPF_K:
797                 case BPF_JMP | BPF_JEQ | BPF_X:
798                         true_cond = COND_EQ;
799                         goto cond_branch;
800                 case BPF_JMP | BPF_JNE | BPF_K:
801                 case BPF_JMP | BPF_JNE | BPF_X:
802                         true_cond = COND_NE;
803                         goto cond_branch;
804                 case BPF_JMP | BPF_JSET | BPF_K:
805                 case BPF_JMP | BPF_JSET | BPF_X:
806                         true_cond = COND_NE;
807                         /* Fall through */
808
809 cond_branch:
810                         switch (code) {
811                         case BPF_JMP | BPF_JGT | BPF_X:
812                         case BPF_JMP | BPF_JGE | BPF_X:
813                         case BPF_JMP | BPF_JEQ | BPF_X:
814                         case BPF_JMP | BPF_JNE | BPF_X:
815                                 /* unsigned comparison */
816                                 PPC_CMPLD(dst_reg, src_reg);
817                                 break;
818                         case BPF_JMP | BPF_JSGT | BPF_X:
819                         case BPF_JMP | BPF_JSGE | BPF_X:
820                                 /* signed comparison */
821                                 PPC_CMPD(dst_reg, src_reg);
822                                 break;
823                         case BPF_JMP | BPF_JSET | BPF_X:
824                                 PPC_AND_DOT(b2p[TMP_REG_1], dst_reg, src_reg);
825                                 break;
826                         case BPF_JMP | BPF_JNE | BPF_K:
827                         case BPF_JMP | BPF_JEQ | BPF_K:
828                         case BPF_JMP | BPF_JGT | BPF_K:
829                         case BPF_JMP | BPF_JGE | BPF_K:
830                                 /*
831                                  * Need sign-extended load, so only positive
832                                  * values can be used as imm in cmpldi
833                                  */
834                                 if (imm >= 0 && imm < 32768)
835                                         PPC_CMPLDI(dst_reg, imm);
836                                 else {
837                                         /* sign-extending load */
838                                         PPC_LI32(b2p[TMP_REG_1], imm);
839                                         /* ... but unsigned comparison */
840                                         PPC_CMPLD(dst_reg, b2p[TMP_REG_1]);
841                                 }
842                                 break;
843                         case BPF_JMP | BPF_JSGT | BPF_K:
844                         case BPF_JMP | BPF_JSGE | BPF_K:
845                                 /*
846                                  * signed comparison, so any 16-bit value
847                                  * can be used in cmpdi
848                                  */
849                                 if (imm >= -32768 && imm < 32768)
850                                         PPC_CMPDI(dst_reg, imm);
851                                 else {
852                                         PPC_LI32(b2p[TMP_REG_1], imm);
853                                         PPC_CMPD(dst_reg, b2p[TMP_REG_1]);
854                                 }
855                                 break;
856                         case BPF_JMP | BPF_JSET | BPF_K:
857                                 /* andi does not sign-extend the immediate */
858                                 if (imm >= 0 && imm < 32768)
859                                         /* PPC_ANDI is _only/always_ dot-form */
860                                         PPC_ANDI(b2p[TMP_REG_1], dst_reg, imm);
861                                 else {
862                                         PPC_LI32(b2p[TMP_REG_1], imm);
863                                         PPC_AND_DOT(b2p[TMP_REG_1], dst_reg,
864                                                     b2p[TMP_REG_1]);
865                                 }
866                                 break;
867                         }
868                         PPC_BCC(true_cond, addrs[i + 1 + off]);
869                         break;
870
871                 /*
872                  * Loads from packet header/data
873                  * Assume 32-bit input value in imm and X (src_reg)
874                  */
875
876                 /* Absolute loads */
877                 case BPF_LD | BPF_W | BPF_ABS:
878                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_word);
879                         goto common_load_abs;
880                 case BPF_LD | BPF_H | BPF_ABS:
881                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_half);
882                         goto common_load_abs;
883                 case BPF_LD | BPF_B | BPF_ABS:
884                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_byte);
885 common_load_abs:
886                         /*
887                          * Load from [imm]
888                          * Load into r4, which can just be passed onto
889                          *  skb load helpers as the second parameter
890                          */
891                         PPC_LI32(4, imm);
892                         goto common_load;
893
894                 /* Indirect loads */
895                 case BPF_LD | BPF_W | BPF_IND:
896                         func = (u8 *)sk_load_word;
897                         goto common_load_ind;
898                 case BPF_LD | BPF_H | BPF_IND:
899                         func = (u8 *)sk_load_half;
900                         goto common_load_ind;
901                 case BPF_LD | BPF_B | BPF_IND:
902                         func = (u8 *)sk_load_byte;
903 common_load_ind:
904                         /*
905                          * Load from [src_reg + imm]
906                          * Treat src_reg as a 32-bit value
907                          */
908                         PPC_EXTSW(4, src_reg);
909                         if (imm) {
910                                 if (imm >= -32768 && imm < 32768)
911                                         PPC_ADDI(4, 4, IMM_L(imm));
912                                 else {
913                                         PPC_LI32(b2p[TMP_REG_1], imm);
914                                         PPC_ADD(4, 4, b2p[TMP_REG_1]);
915                                 }
916                         }
917
918 common_load:
919                         ctx->seen |= SEEN_SKB;
920                         ctx->seen |= SEEN_FUNC;
921                         bpf_jit_emit_func_call(image, ctx, (u64)func);
922
923                         /*
924                          * Helper returns 'lt' condition on error, and an
925                          * appropriate return value in BPF_REG_0
926                          */
927                         PPC_BCC(COND_LT, exit_addr);
928                         break;
929
930                 /*
931                  * Tail call
932                  */
933                 case BPF_JMP | BPF_CALL | BPF_X:
934                         ctx->seen |= SEEN_TAILCALL;
935                         bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
936                         break;
937
938                 default:
939                         /*
940                          * The filter contains something cruel & unusual.
941                          * We don't handle it, but also there shouldn't be
942                          * anything missing from our list.
943                          */
944                         pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
945                                         code, i);
946                         return -ENOTSUPP;
947                 }
948         }
949
950         /* Set end-of-body-code address for exit. */
951         addrs[i] = ctx->idx * 4;
952
953         return 0;
954 }
955
956 void bpf_jit_compile(struct bpf_prog *fp) { }
957
958 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
959 {
960         u32 proglen;
961         u32 alloclen;
962         u8 *image = NULL;
963         u32 *code_base;
964         u32 *addrs;
965         struct codegen_context cgctx;
966         int pass;
967         int flen;
968         struct bpf_binary_header *bpf_hdr;
969         struct bpf_prog *org_fp = fp;
970         struct bpf_prog *tmp_fp;
971         bool bpf_blinded = false;
972
973         if (!bpf_jit_enable)
974                 return org_fp;
975
976         tmp_fp = bpf_jit_blind_constants(org_fp);
977         if (IS_ERR(tmp_fp))
978                 return org_fp;
979
980         if (tmp_fp != org_fp) {
981                 bpf_blinded = true;
982                 fp = tmp_fp;
983         }
984
985         flen = fp->len;
986         addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
987         if (addrs == NULL) {
988                 fp = org_fp;
989                 goto out;
990         }
991
992         memset(&cgctx, 0, sizeof(struct codegen_context));
993
994         /* Scouting faux-generate pass 0 */
995         if (bpf_jit_build_body(fp, 0, &cgctx, addrs)) {
996                 /* We hit something illegal or unsupported. */
997                 fp = org_fp;
998                 goto out;
999         }
1000
1001         /*
1002          * Pretend to build prologue, given the features we've seen.  This will
1003          * update ctgtx.idx as it pretends to output instructions, then we can
1004          * calculate total size from idx.
1005          */
1006         bpf_jit_build_prologue(0, &cgctx);
1007         bpf_jit_build_epilogue(0, &cgctx);
1008
1009         proglen = cgctx.idx * 4;
1010         alloclen = proglen + FUNCTION_DESCR_SIZE;
1011
1012         bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
1013                         bpf_jit_fill_ill_insns);
1014         if (!bpf_hdr) {
1015                 fp = org_fp;
1016                 goto out;
1017         }
1018
1019         code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
1020
1021         /* Code generation passes 1-2 */
1022         for (pass = 1; pass < 3; pass++) {
1023                 /* Now build the prologue, body code & epilogue for real. */
1024                 cgctx.idx = 0;
1025                 bpf_jit_build_prologue(code_base, &cgctx);
1026                 bpf_jit_build_body(fp, code_base, &cgctx, addrs);
1027                 bpf_jit_build_epilogue(code_base, &cgctx);
1028
1029                 if (bpf_jit_enable > 1)
1030                         pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
1031                                 proglen - (cgctx.idx * 4), cgctx.seen);
1032         }
1033
1034         if (bpf_jit_enable > 1)
1035                 /*
1036                  * Note that we output the base address of the code_base
1037                  * rather than image, since opcodes are in code_base.
1038                  */
1039                 bpf_jit_dump(flen, proglen, pass, code_base);
1040
1041         if (image) {
1042                 bpf_flush_icache(bpf_hdr, image + alloclen);
1043 #ifdef PPC64_ELF_ABI_v1
1044                 /* Function descriptor nastiness: Address + TOC */
1045                 ((u64 *)image)[0] = (u64)code_base;
1046                 ((u64 *)image)[1] = local_paca->kernel_toc;
1047 #endif
1048                 fp->bpf_func = (void *)image;
1049                 fp->jited = 1;
1050         }
1051
1052 out:
1053         kfree(addrs);
1054
1055         if (bpf_blinded)
1056                 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
1057
1058         return fp;
1059 }
1060
1061 void bpf_jit_free(struct bpf_prog *fp)
1062 {
1063         unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1064         struct bpf_binary_header *bpf_hdr = (void *)addr;
1065
1066         if (fp->jited)
1067                 bpf_jit_binary_free(bpf_hdr);
1068
1069         bpf_prog_unlock_free(fp);
1070 }