GNU Linux-libre 4.9.318-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 int 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
304         /* out: */
305         return 0;
306 }
307
308 /* Assemble the body code between the prologue & epilogue */
309 static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
310                               struct codegen_context *ctx,
311                               u32 *addrs)
312 {
313         const struct bpf_insn *insn = fp->insnsi;
314         int flen = fp->len;
315         int i, ret;
316
317         /* Start of epilogue code - will only be valid 2nd pass onwards */
318         u32 exit_addr = addrs[flen];
319
320         for (i = 0; i < flen; i++) {
321                 u32 code = insn[i].code;
322                 u32 dst_reg = b2p[insn[i].dst_reg];
323                 u32 src_reg = b2p[insn[i].src_reg];
324                 s16 off = insn[i].off;
325                 s32 imm = insn[i].imm;
326                 u64 imm64;
327                 u8 *func;
328                 u32 true_cond;
329                 u32 tmp_idx;
330
331                 /*
332                  * addrs[] maps a BPF bytecode address into a real offset from
333                  * the start of the body code.
334                  */
335                 addrs[i] = ctx->idx * 4;
336
337                 /*
338                  * As an optimization, we note down which non-volatile registers
339                  * are used so that we can only save/restore those in our
340                  * prologue and epilogue. We do this here regardless of whether
341                  * the actual BPF instruction uses src/dst registers or not
342                  * (for instance, BPF_CALL does not use them). The expectation
343                  * is that those instructions will have src_reg/dst_reg set to
344                  * 0. Even otherwise, we just lose some prologue/epilogue
345                  * optimization but everything else should work without
346                  * any issues.
347                  */
348                 if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
349                         bpf_set_seen_register(ctx, insn[i].dst_reg);
350                 if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
351                         bpf_set_seen_register(ctx, insn[i].src_reg);
352
353                 switch (code) {
354                 /*
355                  * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
356                  */
357                 case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
358                 case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
359                         PPC_ADD(dst_reg, dst_reg, src_reg);
360                         goto bpf_alu32_trunc;
361                 case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
362                 case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
363                         PPC_SUB(dst_reg, dst_reg, src_reg);
364                         goto bpf_alu32_trunc;
365                 case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
366                 case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
367                         if (!imm) {
368                                 goto bpf_alu32_trunc;
369                         } else if (imm >= -32768 && imm < 32768) {
370                                 PPC_ADDI(dst_reg, dst_reg, IMM_L(imm));
371                         } else {
372                                 PPC_LI32(b2p[TMP_REG_1], imm);
373                                 PPC_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]);
374                         }
375                         goto bpf_alu32_trunc;
376                 case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
377                 case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
378                         if (!imm) {
379                                 goto bpf_alu32_trunc;
380                         } else if (imm > -32768 && imm <= 32768) {
381                                 PPC_ADDI(dst_reg, dst_reg, IMM_L(-imm));
382                         } else {
383                                 PPC_LI32(b2p[TMP_REG_1], imm);
384                                 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
385                         }
386                         goto bpf_alu32_trunc;
387                 case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
388                 case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
389                         if (BPF_CLASS(code) == BPF_ALU)
390                                 PPC_MULW(dst_reg, dst_reg, src_reg);
391                         else
392                                 PPC_MULD(dst_reg, dst_reg, src_reg);
393                         goto bpf_alu32_trunc;
394                 case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
395                 case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
396                         if (imm >= -32768 && imm < 32768)
397                                 PPC_MULI(dst_reg, dst_reg, IMM_L(imm));
398                         else {
399                                 PPC_LI32(b2p[TMP_REG_1], imm);
400                                 if (BPF_CLASS(code) == BPF_ALU)
401                                         PPC_MULW(dst_reg, dst_reg,
402                                                         b2p[TMP_REG_1]);
403                                 else
404                                         PPC_MULD(dst_reg, dst_reg,
405                                                         b2p[TMP_REG_1]);
406                         }
407                         goto bpf_alu32_trunc;
408                 case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
409                 case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
410                         PPC_CMPWI(src_reg, 0);
411                         PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
412                         PPC_LI(b2p[BPF_REG_0], 0);
413                         PPC_JMP(exit_addr);
414                         if (BPF_OP(code) == BPF_MOD) {
415                                 PPC_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg);
416                                 PPC_MULW(b2p[TMP_REG_1], src_reg,
417                                                 b2p[TMP_REG_1]);
418                                 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
419                         } else
420                                 PPC_DIVWU(dst_reg, dst_reg, src_reg);
421                         goto bpf_alu32_trunc;
422                 case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
423                 case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
424                         PPC_CMPDI(src_reg, 0);
425                         PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
426                         PPC_LI(b2p[BPF_REG_0], 0);
427                         PPC_JMP(exit_addr);
428                         if (BPF_OP(code) == BPF_MOD) {
429                                 PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
430                                 PPC_MULD(b2p[TMP_REG_1], src_reg,
431                                                 b2p[TMP_REG_1]);
432                                 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
433                         } else
434                                 PPC_DIVDU(dst_reg, dst_reg, src_reg);
435                         break;
436                 case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
437                 case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
438                 case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
439                 case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
440                         if (imm == 0)
441                                 return -EINVAL;
442                         if (imm == 1) {
443                                 if (BPF_OP(code) == BPF_DIV) {
444                                         goto bpf_alu32_trunc;
445                                 } else {
446                                         PPC_LI(dst_reg, 0);
447                                         break;
448                                 }
449                         }
450
451                         PPC_LI32(b2p[TMP_REG_1], imm);
452                         switch (BPF_CLASS(code)) {
453                         case BPF_ALU:
454                                 if (BPF_OP(code) == BPF_MOD) {
455                                         PPC_DIVWU(b2p[TMP_REG_2], dst_reg,
456                                                         b2p[TMP_REG_1]);
457                                         PPC_MULW(b2p[TMP_REG_1],
458                                                         b2p[TMP_REG_1],
459                                                         b2p[TMP_REG_2]);
460                                         PPC_SUB(dst_reg, dst_reg,
461                                                         b2p[TMP_REG_1]);
462                                 } else
463                                         PPC_DIVWU(dst_reg, dst_reg,
464                                                         b2p[TMP_REG_1]);
465                                 break;
466                         case BPF_ALU64:
467                                 if (BPF_OP(code) == BPF_MOD) {
468                                         PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
469                                                         b2p[TMP_REG_1]);
470                                         PPC_MULD(b2p[TMP_REG_1],
471                                                         b2p[TMP_REG_1],
472                                                         b2p[TMP_REG_2]);
473                                         PPC_SUB(dst_reg, dst_reg,
474                                                         b2p[TMP_REG_1]);
475                                 } else
476                                         PPC_DIVDU(dst_reg, dst_reg,
477                                                         b2p[TMP_REG_1]);
478                                 break;
479                         }
480                         goto bpf_alu32_trunc;
481                 case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
482                 case BPF_ALU64 | BPF_NEG: /* dst = -dst */
483                         PPC_NEG(dst_reg, dst_reg);
484                         goto bpf_alu32_trunc;
485
486                 /*
487                  * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
488                  */
489                 case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
490                 case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
491                         PPC_AND(dst_reg, dst_reg, src_reg);
492                         goto bpf_alu32_trunc;
493                 case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
494                 case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
495                         if (!IMM_H(imm))
496                                 PPC_ANDI(dst_reg, dst_reg, IMM_L(imm));
497                         else {
498                                 /* Sign-extended */
499                                 PPC_LI32(b2p[TMP_REG_1], imm);
500                                 PPC_AND(dst_reg, dst_reg, b2p[TMP_REG_1]);
501                         }
502                         goto bpf_alu32_trunc;
503                 case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
504                 case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
505                         PPC_OR(dst_reg, dst_reg, src_reg);
506                         goto bpf_alu32_trunc;
507                 case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
508                 case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
509                         if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
510                                 /* Sign-extended */
511                                 PPC_LI32(b2p[TMP_REG_1], imm);
512                                 PPC_OR(dst_reg, dst_reg, b2p[TMP_REG_1]);
513                         } else {
514                                 if (IMM_L(imm))
515                                         PPC_ORI(dst_reg, dst_reg, IMM_L(imm));
516                                 if (IMM_H(imm))
517                                         PPC_ORIS(dst_reg, dst_reg, IMM_H(imm));
518                         }
519                         goto bpf_alu32_trunc;
520                 case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
521                 case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
522                         PPC_XOR(dst_reg, dst_reg, src_reg);
523                         goto bpf_alu32_trunc;
524                 case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
525                 case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
526                         if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
527                                 /* Sign-extended */
528                                 PPC_LI32(b2p[TMP_REG_1], imm);
529                                 PPC_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]);
530                         } else {
531                                 if (IMM_L(imm))
532                                         PPC_XORI(dst_reg, dst_reg, IMM_L(imm));
533                                 if (IMM_H(imm))
534                                         PPC_XORIS(dst_reg, dst_reg, IMM_H(imm));
535                         }
536                         goto bpf_alu32_trunc;
537                 case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
538                         /* slw clears top 32 bits */
539                         PPC_SLW(dst_reg, dst_reg, src_reg);
540                         break;
541                 case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
542                         PPC_SLD(dst_reg, dst_reg, src_reg);
543                         break;
544                 case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
545                         /* with imm 0, we still need to clear top 32 bits */
546                         PPC_SLWI(dst_reg, dst_reg, imm);
547                         break;
548                 case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
549                         if (imm != 0)
550                                 PPC_SLDI(dst_reg, dst_reg, imm);
551                         break;
552                 case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
553                         PPC_SRW(dst_reg, dst_reg, src_reg);
554                         break;
555                 case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
556                         PPC_SRD(dst_reg, dst_reg, src_reg);
557                         break;
558                 case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
559                         PPC_SRWI(dst_reg, dst_reg, imm);
560                         break;
561                 case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
562                         if (imm != 0)
563                                 PPC_SRDI(dst_reg, dst_reg, imm);
564                         break;
565                 case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
566                         PPC_SRAD(dst_reg, dst_reg, src_reg);
567                         break;
568                 case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
569                         if (imm != 0)
570                                 PPC_SRADI(dst_reg, dst_reg, imm);
571                         break;
572
573                 /*
574                  * MOV
575                  */
576                 case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
577                 case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
578                         PPC_MR(dst_reg, src_reg);
579                         goto bpf_alu32_trunc;
580                 case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
581                 case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
582                         PPC_LI32(dst_reg, imm);
583                         if (imm < 0)
584                                 goto bpf_alu32_trunc;
585                         break;
586
587 bpf_alu32_trunc:
588                 /* Truncate to 32-bits */
589                 if (BPF_CLASS(code) == BPF_ALU)
590                         PPC_RLWINM(dst_reg, dst_reg, 0, 0, 31);
591                 break;
592
593                 /*
594                  * BPF_FROM_BE/LE
595                  */
596                 case BPF_ALU | BPF_END | BPF_FROM_LE:
597                 case BPF_ALU | BPF_END | BPF_FROM_BE:
598 #ifdef __BIG_ENDIAN__
599                         if (BPF_SRC(code) == BPF_FROM_BE)
600                                 goto emit_clear;
601 #else /* !__BIG_ENDIAN__ */
602                         if (BPF_SRC(code) == BPF_FROM_LE)
603                                 goto emit_clear;
604 #endif
605                         switch (imm) {
606                         case 16:
607                                 /* Rotate 8 bits left & mask with 0x0000ff00 */
608                                 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23);
609                                 /* Rotate 8 bits right & insert LSB to reg */
610                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31);
611                                 /* Move result back to dst_reg */
612                                 PPC_MR(dst_reg, b2p[TMP_REG_1]);
613                                 break;
614                         case 32:
615                                 /*
616                                  * Rotate word left by 8 bits:
617                                  * 2 bytes are already in their final position
618                                  * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
619                                  */
620                                 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31);
621                                 /* Rotate 24 bits and insert byte 1 */
622                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7);
623                                 /* Rotate 24 bits and insert byte 3 */
624                                 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23);
625                                 PPC_MR(dst_reg, b2p[TMP_REG_1]);
626                                 break;
627                         case 64:
628                                 /*
629                                  * Way easier and faster(?) to store the value
630                                  * into stack and then use ldbrx
631                                  *
632                                  * ctx->seen will be reliable in pass2, but
633                                  * the instructions generated will remain the
634                                  * same across all passes
635                                  */
636                                 PPC_BPF_STL(dst_reg, 1, bpf_jit_stack_local(ctx));
637                                 PPC_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx));
638                                 PPC_LDBRX(dst_reg, 0, b2p[TMP_REG_1]);
639                                 break;
640                         }
641                         break;
642
643 emit_clear:
644                         switch (imm) {
645                         case 16:
646                                 /* zero-extend 16 bits into 64 bits */
647                                 PPC_RLDICL(dst_reg, dst_reg, 0, 48);
648                                 break;
649                         case 32:
650                                 /* zero-extend 32 bits into 64 bits */
651                                 PPC_RLDICL(dst_reg, dst_reg, 0, 32);
652                                 break;
653                         case 64:
654                                 /* nop */
655                                 break;
656                         }
657                         break;
658
659                 /*
660                  * BPF_ST(X)
661                  */
662                 case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
663                 case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
664                         if (BPF_CLASS(code) == BPF_ST) {
665                                 PPC_LI(b2p[TMP_REG_1], imm);
666                                 src_reg = b2p[TMP_REG_1];
667                         }
668                         PPC_STB(src_reg, dst_reg, off);
669                         break;
670                 case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
671                 case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
672                         if (BPF_CLASS(code) == BPF_ST) {
673                                 PPC_LI(b2p[TMP_REG_1], imm);
674                                 src_reg = b2p[TMP_REG_1];
675                         }
676                         PPC_STH(src_reg, dst_reg, off);
677                         break;
678                 case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
679                 case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
680                         if (BPF_CLASS(code) == BPF_ST) {
681                                 PPC_LI32(b2p[TMP_REG_1], imm);
682                                 src_reg = b2p[TMP_REG_1];
683                         }
684                         PPC_STW(src_reg, dst_reg, off);
685                         break;
686                 case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
687                 case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
688                         if (BPF_CLASS(code) == BPF_ST) {
689                                 PPC_LI32(b2p[TMP_REG_1], imm);
690                                 src_reg = b2p[TMP_REG_1];
691                         }
692                         PPC_BPF_STL(src_reg, dst_reg, off);
693                         break;
694
695                 /*
696                  * BPF_STX XADD (atomic_add)
697                  */
698                 /* *(u32 *)(dst + off) += src */
699                 case BPF_STX | BPF_XADD | BPF_W:
700                         /* Get EA into TMP_REG_1 */
701                         PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
702                         tmp_idx = ctx->idx * 4;
703                         /* load value from memory into TMP_REG_2 */
704                         PPC_BPF_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
705                         /* add value from src_reg into this */
706                         PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
707                         /* store result back */
708                         PPC_BPF_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
709                         /* we're done if this succeeded */
710                         PPC_BCC_SHORT(COND_NE, tmp_idx);
711                         break;
712                 /* *(u64 *)(dst + off) += src */
713                 case BPF_STX | BPF_XADD | BPF_DW:
714                         PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
715                         tmp_idx = ctx->idx * 4;
716                         PPC_BPF_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
717                         PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
718                         PPC_BPF_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
719                         PPC_BCC_SHORT(COND_NE, tmp_idx);
720                         break;
721
722                 /*
723                  * BPF_LDX
724                  */
725                 /* dst = *(u8 *)(ul) (src + off) */
726                 case BPF_LDX | BPF_MEM | BPF_B:
727                         PPC_LBZ(dst_reg, src_reg, off);
728                         break;
729                 /* dst = *(u16 *)(ul) (src + off) */
730                 case BPF_LDX | BPF_MEM | BPF_H:
731                         PPC_LHZ(dst_reg, src_reg, off);
732                         break;
733                 /* dst = *(u32 *)(ul) (src + off) */
734                 case BPF_LDX | BPF_MEM | BPF_W:
735                         PPC_LWZ(dst_reg, src_reg, off);
736                         break;
737                 /* dst = *(u64 *)(ul) (src + off) */
738                 case BPF_LDX | BPF_MEM | BPF_DW:
739                         PPC_BPF_LL(dst_reg, src_reg, off);
740                         break;
741
742                 /*
743                  * Doubleword load
744                  * 16 byte instruction that uses two 'struct bpf_insn'
745                  */
746                 case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
747                         imm64 = ((u64)(u32) insn[i].imm) |
748                                     (((u64)(u32) insn[i+1].imm) << 32);
749                         /* Adjust for two bpf instructions */
750                         addrs[++i] = ctx->idx * 4;
751                         PPC_LI64(dst_reg, imm64);
752                         break;
753
754                 /*
755                  * Return/Exit
756                  */
757                 case BPF_JMP | BPF_EXIT:
758                         /*
759                          * If this isn't the very last instruction, branch to
760                          * the epilogue. If we _are_ the last instruction,
761                          * we'll just fall through to the epilogue.
762                          */
763                         if (i != flen - 1)
764                                 PPC_JMP(exit_addr);
765                         /* else fall through to the epilogue */
766                         break;
767
768                 /*
769                  * Call kernel helper
770                  */
771                 case BPF_JMP | BPF_CALL:
772                         ctx->seen |= SEEN_FUNC;
773                         func = (u8 *) __bpf_call_base + imm;
774
775                         /* Save skb pointer if we need to re-cache skb data */
776                         if (bpf_helper_changes_skb_data(func))
777                                 PPC_BPF_STL(3, 1, bpf_jit_stack_local(ctx));
778
779                         bpf_jit_emit_func_call(image, ctx, (u64)func);
780
781                         /* move return value from r3 to BPF_REG_0 */
782                         PPC_MR(b2p[BPF_REG_0], 3);
783
784                         /* refresh skb cache */
785                         if (bpf_helper_changes_skb_data(func)) {
786                                 /* reload skb pointer to r3 */
787                                 PPC_BPF_LL(3, 1, bpf_jit_stack_local(ctx));
788                                 bpf_jit_emit_skb_loads(image, ctx);
789                         }
790                         break;
791
792                 /*
793                  * Jumps and branches
794                  */
795                 case BPF_JMP | BPF_JA:
796                         PPC_JMP(addrs[i + 1 + off]);
797                         break;
798
799                 case BPF_JMP | BPF_JGT | BPF_K:
800                 case BPF_JMP | BPF_JGT | BPF_X:
801                 case BPF_JMP | BPF_JSGT | BPF_K:
802                 case BPF_JMP | BPF_JSGT | BPF_X:
803                         true_cond = COND_GT;
804                         goto cond_branch;
805                 case BPF_JMP | BPF_JGE | BPF_K:
806                 case BPF_JMP | BPF_JGE | BPF_X:
807                 case BPF_JMP | BPF_JSGE | BPF_K:
808                 case BPF_JMP | BPF_JSGE | BPF_X:
809                         true_cond = COND_GE;
810                         goto cond_branch;
811                 case BPF_JMP | BPF_JEQ | BPF_K:
812                 case BPF_JMP | BPF_JEQ | BPF_X:
813                         true_cond = COND_EQ;
814                         goto cond_branch;
815                 case BPF_JMP | BPF_JNE | BPF_K:
816                 case BPF_JMP | BPF_JNE | BPF_X:
817                         true_cond = COND_NE;
818                         goto cond_branch;
819                 case BPF_JMP | BPF_JSET | BPF_K:
820                 case BPF_JMP | BPF_JSET | BPF_X:
821                         true_cond = COND_NE;
822                         /* Fall through */
823
824 cond_branch:
825                         switch (code) {
826                         case BPF_JMP | BPF_JGT | BPF_X:
827                         case BPF_JMP | BPF_JGE | BPF_X:
828                         case BPF_JMP | BPF_JEQ | BPF_X:
829                         case BPF_JMP | BPF_JNE | BPF_X:
830                                 /* unsigned comparison */
831                                 PPC_CMPLD(dst_reg, src_reg);
832                                 break;
833                         case BPF_JMP | BPF_JSGT | BPF_X:
834                         case BPF_JMP | BPF_JSGE | BPF_X:
835                                 /* signed comparison */
836                                 PPC_CMPD(dst_reg, src_reg);
837                                 break;
838                         case BPF_JMP | BPF_JSET | BPF_X:
839                                 PPC_AND_DOT(b2p[TMP_REG_1], dst_reg, src_reg);
840                                 break;
841                         case BPF_JMP | BPF_JNE | BPF_K:
842                         case BPF_JMP | BPF_JEQ | BPF_K:
843                         case BPF_JMP | BPF_JGT | BPF_K:
844                         case BPF_JMP | BPF_JGE | BPF_K:
845                                 /*
846                                  * Need sign-extended load, so only positive
847                                  * values can be used as imm in cmpldi
848                                  */
849                                 if (imm >= 0 && imm < 32768)
850                                         PPC_CMPLDI(dst_reg, imm);
851                                 else {
852                                         /* sign-extending load */
853                                         PPC_LI32(b2p[TMP_REG_1], imm);
854                                         /* ... but unsigned comparison */
855                                         PPC_CMPLD(dst_reg, b2p[TMP_REG_1]);
856                                 }
857                                 break;
858                         case BPF_JMP | BPF_JSGT | BPF_K:
859                         case BPF_JMP | BPF_JSGE | BPF_K:
860                                 /*
861                                  * signed comparison, so any 16-bit value
862                                  * can be used in cmpdi
863                                  */
864                                 if (imm >= -32768 && imm < 32768)
865                                         PPC_CMPDI(dst_reg, imm);
866                                 else {
867                                         PPC_LI32(b2p[TMP_REG_1], imm);
868                                         PPC_CMPD(dst_reg, b2p[TMP_REG_1]);
869                                 }
870                                 break;
871                         case BPF_JMP | BPF_JSET | BPF_K:
872                                 /* andi does not sign-extend the immediate */
873                                 if (imm >= 0 && imm < 32768)
874                                         /* PPC_ANDI is _only/always_ dot-form */
875                                         PPC_ANDI(b2p[TMP_REG_1], dst_reg, imm);
876                                 else {
877                                         PPC_LI32(b2p[TMP_REG_1], imm);
878                                         PPC_AND_DOT(b2p[TMP_REG_1], dst_reg,
879                                                     b2p[TMP_REG_1]);
880                                 }
881                                 break;
882                         }
883                         PPC_BCC(true_cond, addrs[i + 1 + off]);
884                         break;
885
886                 /*
887                  * Loads from packet header/data
888                  * Assume 32-bit input value in imm and X (src_reg)
889                  */
890
891                 /* Absolute loads */
892                 case BPF_LD | BPF_W | BPF_ABS:
893                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_word);
894                         goto common_load_abs;
895                 case BPF_LD | BPF_H | BPF_ABS:
896                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_half);
897                         goto common_load_abs;
898                 case BPF_LD | BPF_B | BPF_ABS:
899                         func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_byte);
900 common_load_abs:
901                         /*
902                          * Load from [imm]
903                          * Load into r4, which can just be passed onto
904                          *  skb load helpers as the second parameter
905                          */
906                         PPC_LI32(4, imm);
907                         goto common_load;
908
909                 /* Indirect loads */
910                 case BPF_LD | BPF_W | BPF_IND:
911                         func = (u8 *)sk_load_word;
912                         goto common_load_ind;
913                 case BPF_LD | BPF_H | BPF_IND:
914                         func = (u8 *)sk_load_half;
915                         goto common_load_ind;
916                 case BPF_LD | BPF_B | BPF_IND:
917                         func = (u8 *)sk_load_byte;
918 common_load_ind:
919                         /*
920                          * Load from [src_reg + imm]
921                          * Treat src_reg as a 32-bit value
922                          */
923                         PPC_EXTSW(4, src_reg);
924                         if (imm) {
925                                 if (imm >= -32768 && imm < 32768)
926                                         PPC_ADDI(4, 4, IMM_L(imm));
927                                 else {
928                                         PPC_LI32(b2p[TMP_REG_1], imm);
929                                         PPC_ADD(4, 4, b2p[TMP_REG_1]);
930                                 }
931                         }
932
933 common_load:
934                         ctx->seen |= SEEN_SKB;
935                         ctx->seen |= SEEN_FUNC;
936                         bpf_jit_emit_func_call(image, ctx, (u64)func);
937
938                         /*
939                          * Helper returns 'lt' condition on error, and an
940                          * appropriate return value in BPF_REG_0
941                          */
942                         PPC_BCC(COND_LT, exit_addr);
943                         break;
944
945                 /*
946                  * Tail call
947                  */
948                 case BPF_JMP | BPF_CALL | BPF_X:
949                         ctx->seen |= SEEN_TAILCALL;
950                         ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
951                         if (ret < 0)
952                                 return ret;
953                         break;
954
955                 default:
956                         /*
957                          * The filter contains something cruel & unusual.
958                          * We don't handle it, but also there shouldn't be
959                          * anything missing from our list.
960                          */
961                         pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
962                                         code, i);
963                         return -ENOTSUPP;
964                 }
965         }
966
967         /* Set end-of-body-code address for exit. */
968         addrs[i] = ctx->idx * 4;
969
970         return 0;
971 }
972
973 void bpf_jit_compile(struct bpf_prog *fp) { }
974
975 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
976 {
977         u32 proglen;
978         u32 alloclen;
979         u8 *image = NULL;
980         u32 *code_base;
981         u32 *addrs;
982         struct codegen_context cgctx;
983         int pass;
984         int flen;
985         struct bpf_binary_header *bpf_hdr;
986         struct bpf_prog *org_fp = fp;
987         struct bpf_prog *tmp_fp;
988         bool bpf_blinded = false;
989
990         if (!bpf_jit_enable)
991                 return org_fp;
992
993         tmp_fp = bpf_jit_blind_constants(org_fp);
994         if (IS_ERR(tmp_fp))
995                 return org_fp;
996
997         if (tmp_fp != org_fp) {
998                 bpf_blinded = true;
999                 fp = tmp_fp;
1000         }
1001
1002         flen = fp->len;
1003         addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
1004         if (addrs == NULL) {
1005                 fp = org_fp;
1006                 goto out;
1007         }
1008
1009         memset(&cgctx, 0, sizeof(struct codegen_context));
1010
1011         /* Scouting faux-generate pass 0 */
1012         if (bpf_jit_build_body(fp, 0, &cgctx, addrs)) {
1013                 /* We hit something illegal or unsupported. */
1014                 fp = org_fp;
1015                 goto out;
1016         }
1017
1018         /*
1019          * Pretend to build prologue, given the features we've seen.  This will
1020          * update ctgtx.idx as it pretends to output instructions, then we can
1021          * calculate total size from idx.
1022          */
1023         bpf_jit_build_prologue(0, &cgctx);
1024         bpf_jit_build_epilogue(0, &cgctx);
1025
1026         proglen = cgctx.idx * 4;
1027         alloclen = proglen + FUNCTION_DESCR_SIZE;
1028
1029         bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
1030                         bpf_jit_fill_ill_insns);
1031         if (!bpf_hdr) {
1032                 fp = org_fp;
1033                 goto out;
1034         }
1035
1036         code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
1037
1038         /* Code generation passes 1-2 */
1039         for (pass = 1; pass < 3; pass++) {
1040                 /* Now build the prologue, body code & epilogue for real. */
1041                 cgctx.idx = 0;
1042                 bpf_jit_build_prologue(code_base, &cgctx);
1043                 bpf_jit_build_body(fp, code_base, &cgctx, addrs);
1044                 bpf_jit_build_epilogue(code_base, &cgctx);
1045
1046                 if (bpf_jit_enable > 1)
1047                         pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
1048                                 proglen - (cgctx.idx * 4), cgctx.seen);
1049         }
1050
1051         if (bpf_jit_enable > 1)
1052                 /*
1053                  * Note that we output the base address of the code_base
1054                  * rather than image, since opcodes are in code_base.
1055                  */
1056                 bpf_jit_dump(flen, proglen, pass, code_base);
1057
1058         if (image) {
1059                 bpf_flush_icache(bpf_hdr, image + alloclen);
1060 #ifdef PPC64_ELF_ABI_v1
1061                 /* Function descriptor nastiness: Address + TOC */
1062                 ((u64 *)image)[0] = (u64)code_base;
1063                 ((u64 *)image)[1] = local_paca->kernel_toc;
1064 #endif
1065                 fp->bpf_func = (void *)image;
1066                 fp->jited = 1;
1067         }
1068
1069 out:
1070         kfree(addrs);
1071
1072         if (bpf_blinded)
1073                 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
1074
1075         return fp;
1076 }
1077
1078 void bpf_jit_free(struct bpf_prog *fp)
1079 {
1080         unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1081         struct bpf_binary_header *bpf_hdr = (void *)addr;
1082
1083         if (fp->jited)
1084                 bpf_jit_binary_free(bpf_hdr);
1085
1086         bpf_prog_unlock_free(fp);
1087 }