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