2 * bpf_jit_comp64.c: eBPF JIT compiler
4 * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
7 * Based on the powerpc classic BPF JIT compiler by Matt Evans
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
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>
22 #include "bpf_jit64.h"
24 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
28 /* Fill whole space with trap instructions */
29 while (p < (int *)((char *)area + size))
30 *p++ = BREAKPOINT_INSTRUCTION;
33 static inline void bpf_flush_icache(void *start, void *end)
36 flush_icache_range((unsigned long)start, (unsigned long)end);
39 static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
41 return (ctx->seen & (1 << (31 - b2p[i])));
44 static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
46 ctx->seen |= (1 << (31 - b2p[i]));
49 static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
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
57 return ctx->seen & SEEN_FUNC || bpf_is_seen_register(ctx, BPF_REG_FP);
61 * When not setting up our own stackframe, the redzone usage is:
63 * [ prev sp ] <-------------
65 * sp (r1) ---> [ stack pointer ] --------------
66 * [ nv gpr save area ] 8*8
69 * [ unused red zone ] 208 bytes protected
71 static int bpf_jit_stack_local(struct codegen_context *ctx)
73 if (bpf_has_stack_frame(ctx))
74 return STACK_FRAME_MIN_SIZE + MAX_BPF_STACK;
76 return -(BPF_PPC_STACK_SAVE + 16);
79 static int bpf_jit_stack_tailcallcnt(struct codegen_context *ctx)
81 return bpf_jit_stack_local(ctx) + 8;
84 static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
86 if (reg >= BPF_PPC_NVR_MIN && reg < 32)
87 return (bpf_has_stack_frame(ctx) ? BPF_PPC_STACKFRAME : 0)
90 pr_err("BPF JIT is asking about unknown registers");
94 static void bpf_jit_emit_skb_loads(u32 *image, struct codegen_context *ctx)
97 * Load skb->len and skb->data_len
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]);
105 /* skb->data pointer */
106 PPC_BPF_LL(b2p[SKB_DATA_REG], 3, offsetof(struct sk_buff, data));
109 static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
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.
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));
127 #define BPF_TAILCALL_PROLOGUE_SIZE 8
129 if (bpf_has_stack_frame(ctx)) {
131 * We need a stack frame, but we don't necessarily need to
132 * save/restore LR unless we call other functions
134 if (ctx->seen & SEEN_FUNC) {
135 EMIT(PPC_INST_MFLR | __PPC_RT(R0));
136 PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
139 PPC_BPF_STLU(1, 1, -BPF_PPC_STACKFRAME);
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
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]));
152 * Save additional non-volatile regs if we cache skb
153 * Also, setup skb data
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);
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);
169 static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
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]));
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]));
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);
196 static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
198 bpf_jit_emit_common_epilogue(image, ctx);
200 /* Move result to r3 */
201 PPC_MR(3, b2p[BPF_REG_0]);
206 static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64 func)
208 unsigned int i, ctx_idx = ctx->idx;
210 /* Load function address into r12 */
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.
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.
223 for (i = ctx->idx - ctx_idx; i < 5; i++)
226 #ifdef PPC64_ELF_ABI_v1
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.
233 PPC_BPF_LL(2, 12, 8);
234 /* Load actual entry point from function descriptor */
235 PPC_BPF_LL(12, 12, 0);
242 static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
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
250 int b2p_bpf_array = b2p[BPF_REG_2];
251 int b2p_index = b2p[BPF_REG_3];
254 * if (index >= array->map.max_entries)
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);
263 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
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);
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));
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));
285 PPC_CMPLDI(b2p[TMP_REG_1], 0);
286 PPC_BCC(COND_EQ, out);
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);
295 PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], BPF_TAILCALL_PROLOGUE_SIZE);
297 PPC_MTCTR(b2p[TMP_REG_1]);
299 /* tear down stack, restore NVRs, ... */
300 bpf_jit_emit_common_epilogue(image, ctx);
306 /* Assemble the body code between the prologue & epilogue */
307 static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
308 struct codegen_context *ctx,
311 const struct bpf_insn *insn = fp->insnsi;
315 /* Start of epilogue code - will only be valid 2nd pass onwards */
316 u32 exit_addr = addrs[flen];
318 for (i = 0; i < flen; i++) {
319 u32 code = insn[i].code;
320 u32 dst_reg = b2p[insn[i].dst_reg];
321 u32 src_reg = b2p[insn[i].src_reg];
322 s16 off = insn[i].off;
323 s32 imm = insn[i].imm;
330 * addrs[] maps a BPF bytecode address into a real offset from
331 * the start of the body code.
333 addrs[i] = ctx->idx * 4;
336 * As an optimization, we note down which non-volatile registers
337 * are used so that we can only save/restore those in our
338 * prologue and epilogue. We do this here regardless of whether
339 * the actual BPF instruction uses src/dst registers or not
340 * (for instance, BPF_CALL does not use them). The expectation
341 * is that those instructions will have src_reg/dst_reg set to
342 * 0. Even otherwise, we just lose some prologue/epilogue
343 * optimization but everything else should work without
346 if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
347 bpf_set_seen_register(ctx, insn[i].dst_reg);
348 if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
349 bpf_set_seen_register(ctx, insn[i].src_reg);
353 * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
355 case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
356 case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
357 PPC_ADD(dst_reg, dst_reg, src_reg);
358 goto bpf_alu32_trunc;
359 case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
360 case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
361 PPC_SUB(dst_reg, dst_reg, src_reg);
362 goto bpf_alu32_trunc;
363 case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
364 case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
365 case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
366 case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
367 if (BPF_OP(code) == BPF_SUB)
370 if (imm >= -32768 && imm < 32768)
371 PPC_ADDI(dst_reg, dst_reg, IMM_L(imm));
373 PPC_LI32(b2p[TMP_REG_1], imm);
374 PPC_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]);
377 goto bpf_alu32_trunc;
378 case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
379 case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
380 if (BPF_CLASS(code) == BPF_ALU)
381 PPC_MULW(dst_reg, dst_reg, src_reg);
383 PPC_MULD(dst_reg, dst_reg, src_reg);
384 goto bpf_alu32_trunc;
385 case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
386 case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
387 if (imm >= -32768 && imm < 32768)
388 PPC_MULI(dst_reg, dst_reg, IMM_L(imm));
390 PPC_LI32(b2p[TMP_REG_1], imm);
391 if (BPF_CLASS(code) == BPF_ALU)
392 PPC_MULW(dst_reg, dst_reg,
395 PPC_MULD(dst_reg, dst_reg,
398 goto bpf_alu32_trunc;
399 case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
400 case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
401 PPC_CMPWI(src_reg, 0);
402 PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
403 PPC_LI(b2p[BPF_REG_0], 0);
405 if (BPF_OP(code) == BPF_MOD) {
406 PPC_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg);
407 PPC_MULW(b2p[TMP_REG_1], src_reg,
409 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
411 PPC_DIVWU(dst_reg, dst_reg, src_reg);
412 goto bpf_alu32_trunc;
413 case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
414 case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
415 PPC_CMPDI(src_reg, 0);
416 PPC_BCC_SHORT(COND_NE, (ctx->idx * 4) + 12);
417 PPC_LI(b2p[BPF_REG_0], 0);
419 if (BPF_OP(code) == BPF_MOD) {
420 PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
421 PPC_MULD(b2p[TMP_REG_1], src_reg,
423 PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
425 PPC_DIVDU(dst_reg, dst_reg, src_reg);
427 case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
428 case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
429 case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
430 case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
434 goto bpf_alu32_trunc;
436 PPC_LI32(b2p[TMP_REG_1], imm);
437 switch (BPF_CLASS(code)) {
439 if (BPF_OP(code) == BPF_MOD) {
440 PPC_DIVWU(b2p[TMP_REG_2], dst_reg,
442 PPC_MULW(b2p[TMP_REG_1],
445 PPC_SUB(dst_reg, dst_reg,
448 PPC_DIVWU(dst_reg, dst_reg,
452 if (BPF_OP(code) == BPF_MOD) {
453 PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
455 PPC_MULD(b2p[TMP_REG_1],
458 PPC_SUB(dst_reg, dst_reg,
461 PPC_DIVDU(dst_reg, dst_reg,
465 goto bpf_alu32_trunc;
466 case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
467 case BPF_ALU64 | BPF_NEG: /* dst = -dst */
468 PPC_NEG(dst_reg, dst_reg);
469 goto bpf_alu32_trunc;
472 * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
474 case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
475 case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
476 PPC_AND(dst_reg, dst_reg, src_reg);
477 goto bpf_alu32_trunc;
478 case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
479 case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
481 PPC_ANDI(dst_reg, dst_reg, IMM_L(imm));
484 PPC_LI32(b2p[TMP_REG_1], imm);
485 PPC_AND(dst_reg, dst_reg, b2p[TMP_REG_1]);
487 goto bpf_alu32_trunc;
488 case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
489 case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
490 PPC_OR(dst_reg, dst_reg, src_reg);
491 goto bpf_alu32_trunc;
492 case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
493 case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
494 if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
496 PPC_LI32(b2p[TMP_REG_1], imm);
497 PPC_OR(dst_reg, dst_reg, b2p[TMP_REG_1]);
500 PPC_ORI(dst_reg, dst_reg, IMM_L(imm));
502 PPC_ORIS(dst_reg, dst_reg, IMM_H(imm));
504 goto bpf_alu32_trunc;
505 case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
506 case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
507 PPC_XOR(dst_reg, dst_reg, src_reg);
508 goto bpf_alu32_trunc;
509 case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
510 case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
511 if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
513 PPC_LI32(b2p[TMP_REG_1], imm);
514 PPC_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]);
517 PPC_XORI(dst_reg, dst_reg, IMM_L(imm));
519 PPC_XORIS(dst_reg, dst_reg, IMM_H(imm));
521 goto bpf_alu32_trunc;
522 case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
523 /* slw clears top 32 bits */
524 PPC_SLW(dst_reg, dst_reg, src_reg);
526 case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
527 PPC_SLD(dst_reg, dst_reg, src_reg);
529 case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
530 /* with imm 0, we still need to clear top 32 bits */
531 PPC_SLWI(dst_reg, dst_reg, imm);
533 case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
535 PPC_SLDI(dst_reg, dst_reg, imm);
537 case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
538 PPC_SRW(dst_reg, dst_reg, src_reg);
540 case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
541 PPC_SRD(dst_reg, dst_reg, src_reg);
543 case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
544 PPC_SRWI(dst_reg, dst_reg, imm);
546 case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
548 PPC_SRDI(dst_reg, dst_reg, imm);
550 case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
551 PPC_SRAD(dst_reg, dst_reg, src_reg);
553 case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
555 PPC_SRADI(dst_reg, dst_reg, imm);
561 case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
562 case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
563 PPC_MR(dst_reg, src_reg);
564 goto bpf_alu32_trunc;
565 case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
566 case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
567 PPC_LI32(dst_reg, imm);
569 goto bpf_alu32_trunc;
573 /* Truncate to 32-bits */
574 if (BPF_CLASS(code) == BPF_ALU)
575 PPC_RLWINM(dst_reg, dst_reg, 0, 0, 31);
581 case BPF_ALU | BPF_END | BPF_FROM_LE:
582 case BPF_ALU | BPF_END | BPF_FROM_BE:
583 #ifdef __BIG_ENDIAN__
584 if (BPF_SRC(code) == BPF_FROM_BE)
586 #else /* !__BIG_ENDIAN__ */
587 if (BPF_SRC(code) == BPF_FROM_LE)
592 /* Rotate 8 bits left & mask with 0x0000ff00 */
593 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23);
594 /* Rotate 8 bits right & insert LSB to reg */
595 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31);
596 /* Move result back to dst_reg */
597 PPC_MR(dst_reg, b2p[TMP_REG_1]);
601 * Rotate word left by 8 bits:
602 * 2 bytes are already in their final position
603 * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
605 PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31);
606 /* Rotate 24 bits and insert byte 1 */
607 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7);
608 /* Rotate 24 bits and insert byte 3 */
609 PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23);
610 PPC_MR(dst_reg, b2p[TMP_REG_1]);
614 * Way easier and faster(?) to store the value
615 * into stack and then use ldbrx
617 * ctx->seen will be reliable in pass2, but
618 * the instructions generated will remain the
619 * same across all passes
621 PPC_BPF_STL(dst_reg, 1, bpf_jit_stack_local(ctx));
622 PPC_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx));
623 PPC_LDBRX(dst_reg, 0, b2p[TMP_REG_1]);
631 /* zero-extend 16 bits into 64 bits */
632 PPC_RLDICL(dst_reg, dst_reg, 0, 48);
635 /* zero-extend 32 bits into 64 bits */
636 PPC_RLDICL(dst_reg, dst_reg, 0, 32);
647 case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
648 case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
649 if (BPF_CLASS(code) == BPF_ST) {
650 PPC_LI(b2p[TMP_REG_1], imm);
651 src_reg = b2p[TMP_REG_1];
653 PPC_STB(src_reg, dst_reg, off);
655 case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
656 case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
657 if (BPF_CLASS(code) == BPF_ST) {
658 PPC_LI(b2p[TMP_REG_1], imm);
659 src_reg = b2p[TMP_REG_1];
661 PPC_STH(src_reg, dst_reg, off);
663 case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
664 case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
665 if (BPF_CLASS(code) == BPF_ST) {
666 PPC_LI32(b2p[TMP_REG_1], imm);
667 src_reg = b2p[TMP_REG_1];
669 PPC_STW(src_reg, dst_reg, off);
671 case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
672 case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
673 if (BPF_CLASS(code) == BPF_ST) {
674 PPC_LI32(b2p[TMP_REG_1], imm);
675 src_reg = b2p[TMP_REG_1];
677 PPC_BPF_STL(src_reg, dst_reg, off);
681 * BPF_STX XADD (atomic_add)
683 /* *(u32 *)(dst + off) += src */
684 case BPF_STX | BPF_XADD | BPF_W:
685 /* Get EA into TMP_REG_1 */
686 PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
687 tmp_idx = ctx->idx * 4;
688 /* load value from memory into TMP_REG_2 */
689 PPC_BPF_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
690 /* add value from src_reg into this */
691 PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
692 /* store result back */
693 PPC_BPF_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
694 /* we're done if this succeeded */
695 PPC_BCC_SHORT(COND_NE, tmp_idx);
697 /* *(u64 *)(dst + off) += src */
698 case BPF_STX | BPF_XADD | BPF_DW:
699 PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
700 tmp_idx = ctx->idx * 4;
701 PPC_BPF_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
702 PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
703 PPC_BPF_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
704 PPC_BCC_SHORT(COND_NE, tmp_idx);
710 /* dst = *(u8 *)(ul) (src + off) */
711 case BPF_LDX | BPF_MEM | BPF_B:
712 PPC_LBZ(dst_reg, src_reg, off);
714 /* dst = *(u16 *)(ul) (src + off) */
715 case BPF_LDX | BPF_MEM | BPF_H:
716 PPC_LHZ(dst_reg, src_reg, off);
718 /* dst = *(u32 *)(ul) (src + off) */
719 case BPF_LDX | BPF_MEM | BPF_W:
720 PPC_LWZ(dst_reg, src_reg, off);
722 /* dst = *(u64 *)(ul) (src + off) */
723 case BPF_LDX | BPF_MEM | BPF_DW:
724 PPC_BPF_LL(dst_reg, src_reg, off);
729 * 16 byte instruction that uses two 'struct bpf_insn'
731 case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
732 imm64 = ((u64)(u32) insn[i].imm) |
733 (((u64)(u32) insn[i+1].imm) << 32);
734 /* Adjust for two bpf instructions */
735 addrs[++i] = ctx->idx * 4;
736 PPC_LI64(dst_reg, imm64);
742 case BPF_JMP | BPF_EXIT:
744 * If this isn't the very last instruction, branch to
745 * the epilogue. If we _are_ the last instruction,
746 * we'll just fall through to the epilogue.
750 /* else fall through to the epilogue */
756 case BPF_JMP | BPF_CALL:
757 ctx->seen |= SEEN_FUNC;
758 func = (u8 *) __bpf_call_base + imm;
760 /* Save skb pointer if we need to re-cache skb data */
761 if (bpf_helper_changes_skb_data(func))
762 PPC_BPF_STL(3, 1, bpf_jit_stack_local(ctx));
764 bpf_jit_emit_func_call(image, ctx, (u64)func);
766 /* move return value from r3 to BPF_REG_0 */
767 PPC_MR(b2p[BPF_REG_0], 3);
769 /* refresh skb cache */
770 if (bpf_helper_changes_skb_data(func)) {
771 /* reload skb pointer to r3 */
772 PPC_BPF_LL(3, 1, bpf_jit_stack_local(ctx));
773 bpf_jit_emit_skb_loads(image, ctx);
780 case BPF_JMP | BPF_JA:
781 PPC_JMP(addrs[i + 1 + off]);
784 case BPF_JMP | BPF_JGT | BPF_K:
785 case BPF_JMP | BPF_JGT | BPF_X:
786 case BPF_JMP | BPF_JSGT | BPF_K:
787 case BPF_JMP | BPF_JSGT | BPF_X:
790 case BPF_JMP | BPF_JGE | BPF_K:
791 case BPF_JMP | BPF_JGE | BPF_X:
792 case BPF_JMP | BPF_JSGE | BPF_K:
793 case BPF_JMP | BPF_JSGE | BPF_X:
796 case BPF_JMP | BPF_JEQ | BPF_K:
797 case BPF_JMP | BPF_JEQ | BPF_X:
800 case BPF_JMP | BPF_JNE | BPF_K:
801 case BPF_JMP | BPF_JNE | BPF_X:
804 case BPF_JMP | BPF_JSET | BPF_K:
805 case BPF_JMP | BPF_JSET | BPF_X:
811 case BPF_JMP | BPF_JGT | BPF_X:
812 case BPF_JMP | BPF_JGE | BPF_X:
813 case BPF_JMP | BPF_JEQ | BPF_X:
814 case BPF_JMP | BPF_JNE | BPF_X:
815 /* unsigned comparison */
816 PPC_CMPLD(dst_reg, src_reg);
818 case BPF_JMP | BPF_JSGT | BPF_X:
819 case BPF_JMP | BPF_JSGE | BPF_X:
820 /* signed comparison */
821 PPC_CMPD(dst_reg, src_reg);
823 case BPF_JMP | BPF_JSET | BPF_X:
824 PPC_AND_DOT(b2p[TMP_REG_1], dst_reg, src_reg);
826 case BPF_JMP | BPF_JNE | BPF_K:
827 case BPF_JMP | BPF_JEQ | BPF_K:
828 case BPF_JMP | BPF_JGT | BPF_K:
829 case BPF_JMP | BPF_JGE | BPF_K:
831 * Need sign-extended load, so only positive
832 * values can be used as imm in cmpldi
834 if (imm >= 0 && imm < 32768)
835 PPC_CMPLDI(dst_reg, imm);
837 /* sign-extending load */
838 PPC_LI32(b2p[TMP_REG_1], imm);
839 /* ... but unsigned comparison */
840 PPC_CMPLD(dst_reg, b2p[TMP_REG_1]);
843 case BPF_JMP | BPF_JSGT | BPF_K:
844 case BPF_JMP | BPF_JSGE | BPF_K:
846 * signed comparison, so any 16-bit value
847 * can be used in cmpdi
849 if (imm >= -32768 && imm < 32768)
850 PPC_CMPDI(dst_reg, imm);
852 PPC_LI32(b2p[TMP_REG_1], imm);
853 PPC_CMPD(dst_reg, b2p[TMP_REG_1]);
856 case BPF_JMP | BPF_JSET | BPF_K:
857 /* andi does not sign-extend the immediate */
858 if (imm >= 0 && imm < 32768)
859 /* PPC_ANDI is _only/always_ dot-form */
860 PPC_ANDI(b2p[TMP_REG_1], dst_reg, imm);
862 PPC_LI32(b2p[TMP_REG_1], imm);
863 PPC_AND_DOT(b2p[TMP_REG_1], dst_reg,
868 PPC_BCC(true_cond, addrs[i + 1 + off]);
872 * Loads from packet header/data
873 * Assume 32-bit input value in imm and X (src_reg)
877 case BPF_LD | BPF_W | BPF_ABS:
878 func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_word);
879 goto common_load_abs;
880 case BPF_LD | BPF_H | BPF_ABS:
881 func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_half);
882 goto common_load_abs;
883 case BPF_LD | BPF_B | BPF_ABS:
884 func = (u8 *)CHOOSE_LOAD_FUNC(imm, sk_load_byte);
888 * Load into r4, which can just be passed onto
889 * skb load helpers as the second parameter
895 case BPF_LD | BPF_W | BPF_IND:
896 func = (u8 *)sk_load_word;
897 goto common_load_ind;
898 case BPF_LD | BPF_H | BPF_IND:
899 func = (u8 *)sk_load_half;
900 goto common_load_ind;
901 case BPF_LD | BPF_B | BPF_IND:
902 func = (u8 *)sk_load_byte;
905 * Load from [src_reg + imm]
906 * Treat src_reg as a 32-bit value
908 PPC_EXTSW(4, src_reg);
910 if (imm >= -32768 && imm < 32768)
911 PPC_ADDI(4, 4, IMM_L(imm));
913 PPC_LI32(b2p[TMP_REG_1], imm);
914 PPC_ADD(4, 4, b2p[TMP_REG_1]);
919 ctx->seen |= SEEN_SKB;
920 ctx->seen |= SEEN_FUNC;
921 bpf_jit_emit_func_call(image, ctx, (u64)func);
924 * Helper returns 'lt' condition on error, and an
925 * appropriate return value in BPF_REG_0
927 PPC_BCC(COND_LT, exit_addr);
933 case BPF_JMP | BPF_CALL | BPF_X:
934 ctx->seen |= SEEN_TAILCALL;
935 bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
940 * The filter contains something cruel & unusual.
941 * We don't handle it, but also there shouldn't be
942 * anything missing from our list.
944 pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
950 /* Set end-of-body-code address for exit. */
951 addrs[i] = ctx->idx * 4;
956 void bpf_jit_compile(struct bpf_prog *fp) { }
958 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
965 struct codegen_context cgctx;
968 struct bpf_binary_header *bpf_hdr;
969 struct bpf_prog *org_fp = fp;
970 struct bpf_prog *tmp_fp;
971 bool bpf_blinded = false;
976 tmp_fp = bpf_jit_blind_constants(org_fp);
980 if (tmp_fp != org_fp) {
986 addrs = kzalloc((flen+1) * sizeof(*addrs), GFP_KERNEL);
992 memset(&cgctx, 0, sizeof(struct codegen_context));
994 /* Scouting faux-generate pass 0 */
995 if (bpf_jit_build_body(fp, 0, &cgctx, addrs)) {
996 /* We hit something illegal or unsupported. */
1002 * Pretend to build prologue, given the features we've seen. This will
1003 * update ctgtx.idx as it pretends to output instructions, then we can
1004 * calculate total size from idx.
1006 bpf_jit_build_prologue(0, &cgctx);
1007 bpf_jit_build_epilogue(0, &cgctx);
1009 proglen = cgctx.idx * 4;
1010 alloclen = proglen + FUNCTION_DESCR_SIZE;
1012 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
1013 bpf_jit_fill_ill_insns);
1019 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
1021 /* Code generation passes 1-2 */
1022 for (pass = 1; pass < 3; pass++) {
1023 /* Now build the prologue, body code & epilogue for real. */
1025 bpf_jit_build_prologue(code_base, &cgctx);
1026 bpf_jit_build_body(fp, code_base, &cgctx, addrs);
1027 bpf_jit_build_epilogue(code_base, &cgctx);
1029 if (bpf_jit_enable > 1)
1030 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
1031 proglen - (cgctx.idx * 4), cgctx.seen);
1034 if (bpf_jit_enable > 1)
1036 * Note that we output the base address of the code_base
1037 * rather than image, since opcodes are in code_base.
1039 bpf_jit_dump(flen, proglen, pass, code_base);
1042 bpf_flush_icache(bpf_hdr, image + alloclen);
1043 #ifdef PPC64_ELF_ABI_v1
1044 /* Function descriptor nastiness: Address + TOC */
1045 ((u64 *)image)[0] = (u64)code_base;
1046 ((u64 *)image)[1] = local_paca->kernel_toc;
1048 fp->bpf_func = (void *)image;
1056 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
1061 void bpf_jit_free(struct bpf_prog *fp)
1063 unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1064 struct bpf_binary_header *bpf_hdr = (void *)addr;
1067 bpf_jit_binary_free(bpf_hdr);
1069 bpf_prog_unlock_free(fp);