1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 26ac0ba5aSNaveen N. Rao /* 36ac0ba5aSNaveen N. Rao * bpf_jit.h: BPF JIT compiler for PPC 40ca87f05SMatt Evans * 50ca87f05SMatt Evans * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation 6156d0e29SNaveen N. Rao * 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> 70ca87f05SMatt Evans */ 80ca87f05SMatt Evans #ifndef _BPF_JIT_H 90ca87f05SMatt Evans #define _BPF_JIT_H 100ca87f05SMatt Evans 110ca87f05SMatt Evans #ifndef __ASSEMBLY__ 120ca87f05SMatt Evans 13156d0e29SNaveen N. Rao #include <asm/types.h> 1406541865SBalamuruhan S #include <asm/ppc-opcode.h> 15156d0e29SNaveen N. Rao 16156d0e29SNaveen N. Rao #ifdef PPC64_ELF_ABI_v1 170ca87f05SMatt Evans #define FUNCTION_DESCR_SIZE 24 1809ca5ab2SDenis Kirjanov #else 1909ca5ab2SDenis Kirjanov #define FUNCTION_DESCR_SIZE 0 2009ca5ab2SDenis Kirjanov #endif 210ca87f05SMatt Evans 220ca87f05SMatt Evans #define PLANT_INSTR(d, idx, instr) \ 230ca87f05SMatt Evans do { if (d) { (d)[idx] = instr; } idx++; } while (0) 240ca87f05SMatt Evans #define EMIT(instr) PLANT_INSTR(image, ctx->idx, instr) 250ca87f05SMatt Evans 260ca87f05SMatt Evans /* Long jump; (unconditional 'branch') */ 270ca87f05SMatt Evans #define PPC_JMP(dest) EMIT(PPC_INST_BRANCH | \ 280ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & 0x03fffffc)) 29ee7c3ec3SChristophe Leroy /* blr; (unconditional 'branch' with link) to absolute address */ 30ee7c3ec3SChristophe Leroy #define PPC_BL_ABS(dest) EMIT(PPC_INST_BL | \ 31ee7c3ec3SChristophe Leroy (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc)) 320ca87f05SMatt Evans /* "cond" here covers BO:BI fields. */ 330ca87f05SMatt Evans #define PPC_BCC_SHORT(cond, dest) EMIT(PPC_INST_BRANCH_COND | \ 340ca87f05SMatt Evans (((cond) & 0x3ff) << 16) | \ 350ca87f05SMatt Evans (((dest) - (ctx->idx * 4)) & \ 360ca87f05SMatt Evans 0xfffc)) 37aaf2f7e0SNaveen N. Rao /* Sign-extended 32-bit immediate load */ 38aaf2f7e0SNaveen N. Rao #define PPC_LI32(d, i) do { \ 39aaf2f7e0SNaveen N. Rao if ((int)(uintptr_t)(i) >= -32768 && \ 40aaf2f7e0SNaveen N. Rao (int)(uintptr_t)(i) < 32768) \ 413a181237SBalamuruhan S EMIT(PPC_RAW_LI(d, i)); \ 42aaf2f7e0SNaveen N. Rao else { \ 433a181237SBalamuruhan S EMIT(PPC_RAW_LIS(d, IMM_H(i))); \ 44aaf2f7e0SNaveen N. Rao if (IMM_L(i)) \ 453a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, IMM_L(i))); \ 460ca87f05SMatt Evans } } while(0) 47aaf2f7e0SNaveen N. Rao 4851c66ad8SChristophe Leroy #ifdef CONFIG_PPC32 4951c66ad8SChristophe Leroy #define PPC_EX32(r, i) EMIT(PPC_RAW_LI((r), (i) < 0 ? -1 : 0)) 5051c66ad8SChristophe Leroy #endif 5151c66ad8SChristophe Leroy 520ca87f05SMatt Evans #define PPC_LI64(d, i) do { \ 53b1a05787SNaveen N. Rao if ((long)(i) >= -2147483648 && \ 54b1a05787SNaveen N. Rao (long)(i) < 2147483648) \ 550ca87f05SMatt Evans PPC_LI32(d, i); \ 560ca87f05SMatt Evans else { \ 57b1a05787SNaveen N. Rao if (!((uintptr_t)(i) & 0xffff800000000000ULL)) \ 583a181237SBalamuruhan S EMIT(PPC_RAW_LI(d, ((uintptr_t)(i) >> 32) & \ 593a181237SBalamuruhan S 0xffff)); \ 60b1a05787SNaveen N. Rao else { \ 613a181237SBalamuruhan S EMIT(PPC_RAW_LIS(d, ((uintptr_t)(i) >> 48))); \ 620ca87f05SMatt Evans if ((uintptr_t)(i) & 0x0000ffff00000000ULL) \ 633a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, \ 643a181237SBalamuruhan S ((uintptr_t)(i) >> 32) & 0xffff)); \ 65b1a05787SNaveen N. Rao } \ 663a181237SBalamuruhan S EMIT(PPC_RAW_SLDI(d, d, 32)); \ 670ca87f05SMatt Evans if ((uintptr_t)(i) & 0x00000000ffff0000ULL) \ 683a181237SBalamuruhan S EMIT(PPC_RAW_ORIS(d, d, \ 693a181237SBalamuruhan S ((uintptr_t)(i) >> 16) & 0xffff)); \ 700ca87f05SMatt Evans if ((uintptr_t)(i) & 0x000000000000ffffULL) \ 713a181237SBalamuruhan S EMIT(PPC_RAW_ORI(d, d, (uintptr_t)(i) & \ 723a181237SBalamuruhan S 0xffff)); \ 73b1a05787SNaveen N. Rao } } while (0) 740ca87f05SMatt Evans 7509ca5ab2SDenis Kirjanov #ifdef CONFIG_PPC64 7609ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI64(d, i); } while(0) 7709ca5ab2SDenis Kirjanov #else 7809ca5ab2SDenis Kirjanov #define PPC_FUNC_ADDR(d,i) do { PPC_LI32(d, i); } while(0) 7909ca5ab2SDenis Kirjanov #endif 8009ca5ab2SDenis Kirjanov 810ca87f05SMatt Evans /* 820ca87f05SMatt Evans * The fly in the ointment of code size changing from pass to pass is 830ca87f05SMatt Evans * avoided by padding the short branch case with a NOP. If code size differs 840ca87f05SMatt Evans * with different branch reaches we will have the issue of code moving from 850ca87f05SMatt Evans * one pass to the next and will need a few passes to converge on a stable 860ca87f05SMatt Evans * state. 870ca87f05SMatt Evans */ 880ca87f05SMatt Evans #define PPC_BCC(cond, dest) do { \ 89*4549c3eaSNaveen N. Rao if (is_offset_in_cond_branch_range((long)(dest) - (ctx->idx * 4))) { \ 900ca87f05SMatt Evans PPC_BCC_SHORT(cond, dest); \ 913a181237SBalamuruhan S EMIT(PPC_RAW_NOP()); \ 920ca87f05SMatt Evans } else { \ 930ca87f05SMatt Evans /* Flip the 'T or F' bit to invert comparison */ \ 940ca87f05SMatt Evans PPC_BCC_SHORT(cond ^ COND_CMP_TRUE, (ctx->idx+2)*4); \ 950ca87f05SMatt Evans PPC_JMP(dest); \ 960ca87f05SMatt Evans } } while(0) 970ca87f05SMatt Evans 980ca87f05SMatt Evans /* To create a branch condition, select a bit of cr0... */ 990ca87f05SMatt Evans #define CR0_LT 0 1000ca87f05SMatt Evans #define CR0_GT 1 1010ca87f05SMatt Evans #define CR0_EQ 2 1020ca87f05SMatt Evans /* ...and modify BO[3] */ 1030ca87f05SMatt Evans #define COND_CMP_TRUE 0x100 1040ca87f05SMatt Evans #define COND_CMP_FALSE 0x000 1050ca87f05SMatt Evans /* Together, they make all required comparisons: */ 1060ca87f05SMatt Evans #define COND_GT (CR0_GT | COND_CMP_TRUE) 1070ca87f05SMatt Evans #define COND_GE (CR0_LT | COND_CMP_FALSE) 1080ca87f05SMatt Evans #define COND_EQ (CR0_EQ | COND_CMP_TRUE) 1090ca87f05SMatt Evans #define COND_NE (CR0_EQ | COND_CMP_FALSE) 1100ca87f05SMatt Evans #define COND_LT (CR0_LT | COND_CMP_TRUE) 11120dbf5ccSDaniel Borkmann #define COND_LE (CR0_GT | COND_CMP_FALSE) 1120ca87f05SMatt Evans 113c426810fSChristophe Leroy #define SEEN_FUNC 0x20000000 /* might call external helpers */ 114c426810fSChristophe Leroy #define SEEN_STACK 0x40000000 /* uses BPF stack */ 115c426810fSChristophe Leroy #define SEEN_TAILCALL 0x80000000 /* uses tail calls */ 116f1b1583dSChristophe Leroy 11740272035SChristophe Leroy #define SEEN_VREG_MASK 0x1ff80000 /* Volatile registers r3-r12 */ 11840272035SChristophe Leroy #define SEEN_NVREG_MASK 0x0003ffff /* Non volatile registers r14-r31 */ 11940272035SChristophe Leroy 12040272035SChristophe Leroy #ifdef CONFIG_PPC64 12140272035SChristophe Leroy extern const int b2p[MAX_BPF_JIT_REG + 2]; 12240272035SChristophe Leroy #else 12340272035SChristophe Leroy extern const int b2p[MAX_BPF_JIT_REG + 1]; 12440272035SChristophe Leroy #endif 12540272035SChristophe Leroy 126f1b1583dSChristophe Leroy struct codegen_context { 127f1b1583dSChristophe Leroy /* 128f1b1583dSChristophe Leroy * This is used to track register usage as well 129f1b1583dSChristophe Leroy * as calls to external helpers. 130f1b1583dSChristophe Leroy * - register usage is tracked with corresponding 131c426810fSChristophe Leroy * bits (r3-r31) 132f1b1583dSChristophe Leroy * - rest of the bits can be used to track other 133c426810fSChristophe Leroy * things -- for now, we use bits 0 to 2 134f1b1583dSChristophe Leroy * encoded in SEEN_* macros above 135f1b1583dSChristophe Leroy */ 136f1b1583dSChristophe Leroy unsigned int seen; 137f1b1583dSChristophe Leroy unsigned int idx; 138f1b1583dSChristophe Leroy unsigned int stack_size; 13940272035SChristophe Leroy int b2p[ARRAY_SIZE(b2p)]; 140f1b1583dSChristophe Leroy }; 141f1b1583dSChristophe Leroy 142f1b1583dSChristophe Leroy static inline void bpf_flush_icache(void *start, void *end) 143f1b1583dSChristophe Leroy { 144f1b1583dSChristophe Leroy smp_wmb(); /* smp write barrier */ 145f1b1583dSChristophe Leroy flush_icache_range((unsigned long)start, (unsigned long)end); 146f1b1583dSChristophe Leroy } 147f1b1583dSChristophe Leroy 148f1b1583dSChristophe Leroy static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i) 149f1b1583dSChristophe Leroy { 150f1b1583dSChristophe Leroy return ctx->seen & (1 << (31 - i)); 151f1b1583dSChristophe Leroy } 152f1b1583dSChristophe Leroy 153f1b1583dSChristophe Leroy static inline void bpf_set_seen_register(struct codegen_context *ctx, int i) 154f1b1583dSChristophe Leroy { 155f1b1583dSChristophe Leroy ctx->seen |= 1 << (31 - i); 156f1b1583dSChristophe Leroy } 157f1b1583dSChristophe Leroy 15840272035SChristophe Leroy static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i) 15940272035SChristophe Leroy { 16040272035SChristophe Leroy ctx->seen &= ~(1 << (31 - i)); 16140272035SChristophe Leroy } 16240272035SChristophe Leroy 1634ea76e90SChristophe Leroy void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func); 1644ea76e90SChristophe Leroy int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx, 1654ea76e90SChristophe Leroy u32 *addrs, bool extra_pass); 1664ea76e90SChristophe Leroy void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx); 1674ea76e90SChristophe Leroy void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx); 16840272035SChristophe Leroy void bpf_jit_realloc_regs(struct codegen_context *ctx); 1694ea76e90SChristophe Leroy 1700ca87f05SMatt Evans #endif 1710ca87f05SMatt Evans 1720ca87f05SMatt Evans #endif 173